]> git.cryptolib.org Git - avr-crypto-lib.git/blob - seed-stub.c
oops some imortant thigs were commented out
[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 "uart.h"
32 #include "debug.h"
33
34 /* key constants */
35 uint32_t seed_kc[16] PROGMEM ={
36         0xb979379e, 
37         0x73f36e3c,
38         0xe6e6dd78, 
39         0xcccdbbf1, 
40         0x999b77e3, 
41         0x3337efc6, 
42         0x676ede8d, 
43         0xcfdcbc1b, 
44         0x9eb97937,
45         0x3c73f36e,     
46         0x78e6e6dd,
47         0xf1cccdbb,
48         0xe3999b77,
49         0xc63337ef,
50         0x8d676ede,
51         0x1bcfdcbc
52 };
53
54
55 static uint64_t f_function(uint64_t a, uint32_t k0, uint32_t k1);
56 uint32_t g_function(uint32_t x);
57
58 uint32_t bigendian_sum32(uint32_t a, uint32_t b);
59 uint32_t bigendian_sub32(uint32_t a, uint32_t b);
60
61 /******************************************************************************/
62 static inline
63 uint64_t bigendian_rotl8_64(uint64_t a){
64         /*
65         changeendian64(&a);
66         a = (a<<8) | (a>>(64-8));
67         changeendian64(&a);
68         */
69         a = (a>>8) | (a<<(64-8));
70         return a;
71 }
72
73 /******************************************************************************/
74 static inline
75 uint64_t bigendian_rotr8_64(uint64_t a){
76         /*
77         changeendian64(&a);
78         a = (a>>8) | (a<<(64-8));
79         changeendian64(&a);
80         */
81         a = (a<<8) | (a>>(64-8));
82         return a;
83 }
84
85 /******************************************************************************/
86 static
87 uint64_t f_function(uint64_t a, uint32_t k0, uint32_t k1){
88         uint32_t c,d;
89
90         c = a & 0x00000000FFFFFFFFLL;
91         d = (a>>32) & 0x00000000FFFFFFFFLL;
92         
93         c ^= k0;        d ^= k1;
94         d ^= c;
95         d = g_function(d);
96         c = bigendian_sum32(c,d);
97         c = g_function(c);
98         d = bigendian_sum32(c,d);
99         d = g_function(d);
100         c = bigendian_sum32(c,d);
101         a = ((uint64_t)d << 32) | c;
102         return a;
103 }
104
105
106 /******************************************************************************/
107 typedef struct {
108         uint32_t k0, k1;
109 } keypair_t;
110
111 static
112 keypair_t getnextkeys(uint32_t *keystate, uint8_t curround){
113         keypair_t ret;
114         if (curround>15){
115                 /* ERROR */
116                 ret.k0 = ret.k1 = 0;
117         } else {
118         /*      ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
119                 ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
120                 ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
121                 ret.k0 = bigendian_sub32(ret.k0, pgm_read_dword(&(seed_kc[curround])));
122                 ret.k0 = g_function(ret.k0);
123                 ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
124                 ret.k1 = bigendian_sum32(ret.k1, pgm_read_dword(&(seed_kc[curround])));
125                 ret.k1 = g_function(ret.k1);
126                 
127                 if (curround & 1){
128                         /* odd round (1,3,5, ...) */
129                         ((uint64_t*)keystate)[1] = bigendian_rotl8_64( ((uint64_t*)keystate)[1] );
130                 } else {
131                         /* even round (0,2,4, ...) */
132                         ((uint64_t*)keystate)[0] = bigendian_rotr8_64(((uint64_t*)keystate)[0]);
133                 }
134         }
135         return ret;
136 }
137
138
139 /******************************************************************************/
140 static
141 keypair_t getprevkeys(uint32_t *keystate, uint8_t curround){
142         keypair_t ret;
143         if (curround>15){
144                 /* ERROR */
145                 ret.k0 = ret.k1 = 0;
146         } else {
147                 if (curround & 1){
148                         /* odd round (1,3,5, ..., 15) */
149                         ((uint64_t*)keystate)[1] = bigendian_rotr8_64( ((uint64_t*)keystate)[1] );
150                 } else {
151                         /* even round (0,2,4, ..., 14) */
152                         ((uint64_t*)keystate)[0] = bigendian_rotl8_64(((uint64_t*)keystate)[0]);
153                 }
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         return ret;
164 }
165
166 /******************************************************************************/
167
168 typedef struct{
169         uint32_t k[4];
170 } seed_ctx_t;
171
172 /******************************************************************************/
173
174 void seed_init(uint8_t * key, seed_ctx_t * ctx){
175         memcpy(ctx->k, key, 128/8);
176 }
177
178 /******************************************************************************/
179
180 #define L (((uint64_t*)buffer)[0])
181 #define R (((uint64_t*)buffer)[1])
182
183 void seed_enc(void * buffer, seed_ctx_t * ctx){
184         uint8_t r;
185         keypair_t k;
186         for(r=0; r<8; ++r){
187                         k = getnextkeys(ctx->k, 2*r);
188 /*
189         DEBUG_S("\r\n\tDBG ka,0: "); uart_hexdump(&k.k0, 4);
190         DEBUG_S("\r\n\tDBG ka,1: "); uart_hexdump(&k.k1, 4);
191         DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+0, 8);
192         DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+8, 8);
193 */
194                         L ^= f_function(R,k.k0,k.k1);
195                         
196                         k = getnextkeys(ctx->k, 2*r+1);
197 /*
198         DEBUG_S("\r\n\tDBG kb,0: "); uart_hexdump(&k.k0, 4);
199         DEBUG_S("\r\n\tDBG kb,1: "); uart_hexdump(&k.k1, 4);
200         DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+8, 8);
201         DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+0, 8);
202 */
203                         R ^= f_function(L,k.k0,k.k1);
204         }
205         /* just an exchange without temp. variable */
206         L ^= R;
207         R ^= L;
208         L ^= R;
209 }
210
211 /******************************************************************************/
212
213 #define L (((uint64_t*)buffer)[0])
214 #define R (((uint64_t*)buffer)[1])
215
216 void seed_dec(void * buffer, seed_ctx_t * ctx){
217         int8_t r;
218         keypair_t k;
219         for(r=7; r>=0; --r){
220                         k = getprevkeys(ctx->k, 2*r+1);
221 /*
222         DEBUG_S("\r\n\tDBG ka,0: "); uart_hexdump(&k.k0, 4);
223         DEBUG_S("\r\n\tDBG ka,1: "); uart_hexdump(&k.k1, 4);
224         DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+0, 8);
225         DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+8, 8);
226 */
227                         L ^= f_function(R,k.k0,k.k1);
228                         
229                         k = getprevkeys(ctx->k, 2*r+0);
230 /*
231         DEBUG_S("\r\n\tDBG kb,0: "); uart_hexdump(&k.k0, 4);
232         DEBUG_S("\r\n\tDBG kb,1: "); uart_hexdump(&k.k1, 4);
233         DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+8, 8);
234         DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+0, 8);
235 */
236                         R ^= f_function(L,k.k0,k.k1);
237         }
238         /* just an exchange without temp. variable */
239         L ^= R;
240         R ^= L;
241         L ^= R;
242 }
243
244
245
246
247
248
249
250
251
252
253