]> git.cryptolib.org Git - avr-crypto-lib.git/blob - seed_C.c
oops some imortant thigs were commented out
[avr-crypto-lib.git] / seed_C.c
1 /* seed_C.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  /**
20  * \file        seed_C.c
21  * \author      Daniel Otte 
22  * \date        2007-06-1
23  * \brief       SEED parts in C for AVR
24  * \par License 
25  * GPL
26  * 
27  */
28 #include <stdint.h>
29 #include <avr/pgmspace.h>
30 #include <string.h>
31 #include "seed_sbox.h"
32 #include "uart.h"
33 #include "debug.h"
34
35
36 static
37 uint32_t g_function(uint32_t x);
38 /******************************************************************************/
39
40 static
41 void changeendian32(uint32_t * a){
42         *a = (*a & 0x000000FF) << 24 |
43                  (*a & 0x0000FF00) <<  8 |
44                  (*a & 0x00FF0000) >>  8 |
45                  (*a & 0xFF000000) >> 24;
46 }
47
48 /******************************************************************************/
49 static
50 uint32_t bigendian_sum32(uint32_t a, uint32_t b){
51         changeendian32(&a);
52         changeendian32(&b);
53         a += b;
54         changeendian32(&a);
55         return a;
56 }
57
58 /******************************************************************************/
59 static 
60 uint32_t bigendian_sub32(uint32_t a, uint32_t b){
61         changeendian32(&a);
62         changeendian32(&b);
63         a -= b;
64         changeendian32(&a);
65         return a;
66 }
67
68 /******************************************************************************/
69 static inline
70 uint64_t bigendian_rotl8_64(uint64_t a){
71         /*
72         changeendian64(&a);
73         a = (a<<8) | (a>>(64-8));
74         changeendian64(&a);
75         */
76         a = (a>>8) | (a<<(64-8));
77         return a;
78 }
79
80 /******************************************************************************/
81 static inline
82 uint64_t bigendian_rotr8_64(uint64_t a){
83         /*
84         changeendian64(&a);
85         a = (a>>8) | (a<<(64-8));
86         changeendian64(&a);
87         */
88         a = (a<<8) | (a>>(64-8));
89         return a;
90 }
91
92 /******************************************************************************/
93 static
94 uint64_t f_function(uint64_t a, uint32_t k0, uint32_t k1){
95         uint32_t c,d;
96
97         c = a & 0x00000000FFFFFFFFLL;
98         d = (a>>32) & 0x00000000FFFFFFFFLL;
99         
100         c ^= k0;        d ^= k1;
101         d ^= c;
102         d = g_function(d);
103         c = bigendian_sum32(c,d);
104         c = g_function(c);
105         d = bigendian_sum32(c,d);
106         d = g_function(d);
107         c = bigendian_sum32(c,d);
108         a = ((uint64_t)d << 32) | c;
109         return a;
110 }
111
112 /******************************************************************************/
113 #define M0 0xfc
114 #define M1 0xf3
115 #define M2 0xcf
116 #define M3 0x3f
117
118 #define X3 (((uint8_t*)(&x))[0])
119 #define X2 (((uint8_t*)(&x))[1])
120 #define X1 (((uint8_t*)(&x))[2])
121 #define X0 (((uint8_t*)(&x))[3])
122
123 #define Z3 (((uint8_t*)(&z))[0])
124 #define Z2 (((uint8_t*)(&z))[1])
125 #define Z1 (((uint8_t*)(&z))[2])
126 #define Z0 (((uint8_t*)(&z))[3])
127
128 static
129 uint32_t g_function(uint32_t x){
130         uint32_t z;
131         /* sbox substitution */
132         X3 = pgm_read_byte(&(seed_sbox2[X3]));
133         X2 = pgm_read_byte(&(seed_sbox1[X2]));
134         X1 = pgm_read_byte(&(seed_sbox2[X1]));
135         X0 = pgm_read_byte(&(seed_sbox1[X0]));
136         /* now the permutation */
137         Z0 = (X0 & M0) ^ (X1 & M1) ^ (X2 & M2) ^ (X3 & M3);
138         Z1 = (X0 & M1) ^ (X1 & M2) ^ (X2 & M3) ^ (X3 & M0);
139         Z2 = (X0 & M2) ^ (X1 & M3) ^ (X2 & M0) ^ (X3 & M1);
140         Z3 = (X0 & M3) ^ (X1 & M0) ^ (X2 & M1) ^ (X3 & M2);
141         return z;
142 }
143 /******************************************************************************/
144 typedef struct {
145         uint32_t k0, k1;
146 } keypair_t;
147
148 keypair_t getnextkeys(uint32_t *keystate, uint8_t curround){
149         keypair_t ret;
150         if (curround>15){
151                 /* ERROR */
152                 ret.k0 = ret.k1 = 0;
153         } else {
154         /*      ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
155                 ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
156                 ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
157                 ret.k0 = bigendian_sub32(ret.k0, pgm_read_dword(&(seed_kc[curround])));
158                 ret.k0 = g_function(ret.k0);
159                 ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
160                 ret.k1 = bigendian_sum32(ret.k1, pgm_read_dword(&(seed_kc[curround])));
161                 ret.k1 = g_function(ret.k1);
162                 
163                 if (curround & 1){
164                         /* odd round (1,3,5, ...) */
165                         ((uint64_t*)keystate)[1] = bigendian_rotl8_64( ((uint64_t*)keystate)[1] );
166                 } else {
167                         /* even round (0,2,4, ...) */
168                         ((uint64_t*)keystate)[0] = bigendian_rotr8_64(((uint64_t*)keystate)[0]);
169                 }
170         }
171         return ret;
172 }
173
174
175 /******************************************************************************/
176
177 keypair_t getprevkeys(uint32_t *keystate, uint8_t curround){
178         keypair_t ret;
179         if (curround>15){
180                 /* ERROR */
181                 ret.k0 = ret.k1 = 0;
182         } else {
183                 if (curround & 1){
184                         /* odd round (1,3,5, ..., 15) */
185                         ((uint64_t*)keystate)[1] = bigendian_rotr8_64( ((uint64_t*)keystate)[1] );
186                 } else {
187                         /* even round (0,2,4, ..., 14) */
188                         ((uint64_t*)keystate)[0] = bigendian_rotl8_64(((uint64_t*)keystate)[0]);
189                 }
190         /*      ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
191                 ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
192                 ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
193                 ret.k0 = bigendian_sub32(ret.k0, pgm_read_dword(&(seed_kc[curround])));
194                 ret.k0 = g_function(ret.k0);
195                 ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
196                 ret.k1 = bigendian_sum32(ret.k1, pgm_read_dword(&(seed_kc[curround])));
197                 ret.k1 = g_function(ret.k1);
198                 }
199         return ret;
200 }
201
202 /******************************************************************************/
203
204 typedef struct{
205         uint32_t k[4];
206 } seed_ctx_t;
207
208 /******************************************************************************/
209
210 void seed_init(uint8_t * key, seed_ctx_t * ctx){
211         memcpy(ctx->k, key, 128/8);
212 }
213
214 /******************************************************************************/
215
216 #define L (((uint64_t*)buffer)[0])
217 #define R (((uint64_t*)buffer)[1])
218
219 void seed_enc(void * buffer, seed_ctx_t * ctx){
220         uint8_t r;
221         keypair_t k;
222         for(r=0; r<8; ++r){
223                         k = getnextkeys(ctx->k, 2*r);
224 /*
225         DEBUG_S("\r\n\tDBG ka,0: "); uart_hexdump(&k.k0, 4);
226         DEBUG_S("\r\n\tDBG ka,1: "); uart_hexdump(&k.k1, 4);
227         DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+0, 8);
228         DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+8, 8);
229 */
230                         L ^= f_function(R,k.k0,k.k1);
231                         
232                         k = getnextkeys(ctx->k, 2*r+1);
233 /*
234         DEBUG_S("\r\n\tDBG kb,0: "); uart_hexdump(&k.k0, 4);
235         DEBUG_S("\r\n\tDBG kb,1: "); uart_hexdump(&k.k1, 4);
236         DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+8, 8);
237         DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+0, 8);
238 */
239                         R ^= f_function(L,k.k0,k.k1);
240         }
241         /* just an exchange without temp. variable */
242         L ^= R;
243         R ^= L;
244         L ^= R;
245 }
246
247 /******************************************************************************/
248
249 #define L (((uint64_t*)buffer)[0])
250 #define R (((uint64_t*)buffer)[1])
251
252 void seed_dec(void * buffer, seed_ctx_t * ctx){
253         int8_t r;
254         keypair_t k;
255         for(r=7; r>=0; --r){
256                         k = getprevkeys(ctx->k, 2*r+1);
257 /*
258         DEBUG_S("\r\n\tDBG ka,0: "); uart_hexdump(&k.k0, 4);
259         DEBUG_S("\r\n\tDBG ka,1: "); uart_hexdump(&k.k1, 4);
260         DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+0, 8);
261         DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+8, 8);
262 */
263                         L ^= f_function(R,k.k0,k.k1);
264                         
265                         k = getprevkeys(ctx->k, 2*r+0);
266 /*
267         DEBUG_S("\r\n\tDBG kb,0: "); uart_hexdump(&k.k0, 4);
268         DEBUG_S("\r\n\tDBG kb,1: "); uart_hexdump(&k.k1, 4);
269         DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+8, 8);
270         DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+0, 8);
271 */
272                         R ^= f_function(L,k.k0,k.k1);
273         }
274         /* just an exchange without temp. variable */
275         L ^= R;
276         R ^= L;
277         L ^= R;
278 }
279
280
281
282
283
284
285
286
287
288
289