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