]> git.cryptolib.org Git - avr-crypto-lib.git/blob - serpent.c
insereated GPLv3 stub
[avr-crypto-lib.git] / serpent.c
1 /* serpent.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /* serpent.c
20  * a bitsliced implementation of the serpent cipher for avr microcontrollers
21  * author: Daniel Otte 
22  * email:  daniel.otte@rub.de
23  * license: GPLv3
24  */
25
26 #include <stdint.h>
27 #include <string.h> /* memset() */
28 #include <avr/pgmspace.h>
29 #include "serpent.h"
30 #include "serpent-sboxes.h"
31
32 /******************************************************************************/
33
34 void memxor(void * dest, void * src, uint8_t size){
35         while(size--){
36                 *((uint8_t*)dest) ^= *((uint8_t*)src);
37                 dest = (uint8_t*)dest +1;
38                 src  = (uint8_t*)src  +1;
39         }
40 }
41
42 /******************************************************************************/
43
44 uint32_t rotl32(uint32_t a, uint8_t n){
45         return ((a<<n) | (a>>(32-n)));
46 }
47
48
49 uint32_t rotr32(uint32_t a, uint8_t n){
50         return ((a>>n) | (a<<(32-n)));
51 }
52
53
54 #define X0 (((uint32_t*)b)[0])
55 #define X1 (((uint32_t*)b)[1])
56 #define X2 (((uint32_t*)b)[2])
57 #define X3 (((uint32_t*)b)[3])
58
59 static void lt(uint8_t *b){
60         X0 = rotl32(X0, 13);
61         X2 = rotl32(X2,  3);
62         X1 ^= X0 ^ X2;
63         X3 ^= X2 ^ (X0 << 3);
64         X1 = rotl32(X1, 1);
65         X3 = rotl32(X3, 7);
66         X0 ^= X1 ^ X3;
67         X2 ^= X3 ^ (X1 << 7);
68         X0 = rotl32(X0, 5);
69         X2 = rotr32(X2, 10);
70 }
71
72 static void inv_lt(uint8_t *b){
73         X2 = rotl32(X2, 10);
74         X0 = rotr32(X0, 5);
75         X2 ^= X3 ^ (X1 << 7);
76         X0 ^= X1 ^ X3;
77         X3 = rotr32(X3, 7);
78         X1 = rotr32(X1, 1);
79         X3 ^= X2 ^ (X0 << 3);
80         X1 ^= X0 ^ X2;
81         X2 = rotr32(X2,  3);
82         X0 = rotr32(X0, 13);
83 }
84
85 #define GOLDEN_RATIO 0x9e3779b9l
86
87 static uint32_t gen_w(uint32_t * b, uint8_t i){
88         uint32_t ret;
89         ret = b[0] ^ b[3] ^ b[5] ^ b[7] ^ GOLDEN_RATIO ^ (uint32_t)i;
90         ret = rotl32(ret, 11);
91         return ret;
92
93
94 /* key must be 256bit (32 byte) large! */
95 void serpent_genctx(void * key, uint8_t keysize, serpent_ctx_t * ctx){
96         uint32_t buffer[8];
97         uint8_t i,j;
98         if(keysize){
99                 /* keysize is less than 256 bit, padding needed */
100                 memset(buffer, 0, 32);
101                 memcpy(buffer, key, (keysize+7)/8);
102                 ((uint8_t*)buffer)[keysize/8] |= 1<<(keysize%8);
103         } else {
104                 /* keysize is 256 bit */
105                 memcpy(buffer, key, 32); 
106         }
107         for(i=0; i<33; ++i){
108                 for(j=0; j<4; ++j){
109                         ctx->k[i][j] = gen_w(buffer, i*4+j);
110                         memmove(buffer, &(buffer[1]), 7*4); /* shift buffer one to the "left" */
111                         buffer[7] = ctx->k[i][j];
112                 }
113         }
114         for(i=0; i<33; ++i){
115                 sbox128(ctx->k[i],3-i);
116         }
117 }
118
119
120 void serpent_enc(void* buffer, serpent_ctx_t * ctx){
121         uint8_t i;
122         for(i=0; i<31; ++i){
123                 memxor((uint8_t*)buffer, ctx->k[i], 16);
124                 sbox128(buffer, i);
125                 lt((uint8_t*)buffer);
126         }
127         memxor((uint8_t*)buffer, ctx->k[i], 16);
128         sbox128(buffer, i);
129         ++i;
130         memxor((uint8_t*)buffer, ctx->k[i], 16);
131 }
132
133 void serpent_dec(void* buffer, serpent_ctx_t * ctx){
134         int8_t i=32;
135         
136         memxor((uint8_t*)buffer, ctx->k[i], 16);
137         --i;
138         inv_sbox128(buffer, i);
139         memxor((uint8_t*)buffer, ctx->k[i], 16);
140         --i;
141         for(; i>=0; --i){
142                 inv_lt((uint8_t*)buffer);
143                 inv_sbox128(buffer, i);
144                 memxor((uint8_t*)buffer, ctx->k[i], 16);
145         }
146 }
147
148
149
150
151
152