]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bmw/bmw_small.c
modified structure
[avr-crypto-lib.git] / bmw / bmw_small.c
1 /* bmw_small.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2009  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    bmw_small.c
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-04-27
24  * \license GPLv3 or later
25  * 
26  */
27
28 #include <stdint.h>
29 #include <string.h>
30 #include <avr/pgmspace.h>
31 #include "bmw_small.h"
32
33
34 #define SHL32(a,n) ((a)<<(n))
35 #define SHR32(a,n) ((a)>>(n))
36 #define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
37 #define ROTR32(a,n) (((a)>>(n))|((a)<<(32-(n))))
38
39 #define BUG24   0
40 #define F0_HACK 1
41
42 #define DEBUG 0
43 #if DEBUG
44  #include "cli.h"
45  
46  void ctx_dump(const bmw_small_ctx_t* ctx){
47         uint8_t i;
48         cli_putstr_P(PSTR("\r\n==== ctx dump ===="));
49         for(i=0; i<16;++i){
50                 cli_putstr_P(PSTR("\r\n h["));
51                 cli_hexdump(&i, 1);
52                 cli_putstr_P(PSTR("] = "));
53                 cli_hexdump_rev(&(ctx->h[i]), 4);
54         }
55         cli_putstr_P(PSTR("\r\n counter = "));
56         cli_hexdump(&(ctx->counter), 4);
57  }
58  
59  void dump_x(const uint32_t* q, uint8_t elements, char x){
60         uint8_t i;
61         cli_putstr_P(PSTR("\r\n==== "));
62         cli_putc(x);
63         cli_putstr_P(PSTR(" dump ===="));
64         for(i=0; i<elements;++i){
65                 cli_putstr_P(PSTR("\r\n "));
66                 cli_putc(x);
67                 cli_putstr_P(PSTR("["));
68                 cli_hexdump(&i, 1);
69                 cli_putstr_P(PSTR("] = "));
70                 cli_hexdump_rev(&(q[i]), 4);
71         }
72  }
73 #else
74  #define ctx_dump(x)
75  #define dump_x(a,b,c)
76 #endif
77
78 uint32_t bmw_small_s0(uint32_t x){
79         uint32_t r;
80         r =   SHR32(x, 1)
81                 ^ SHL32(x, 3)
82                 ^ ROTL32(x, 4)
83                 ^ ROTR32(x, 13);
84         return r;       
85 }
86
87 uint32_t bmw_small_s1(uint32_t x){
88         uint32_t r;
89         r =   SHR32(x, 1)
90                 ^ SHL32(x, 2)
91                 ^ ROTL32(x, 8)
92                 ^ ROTR32(x, 9);
93         return r;       
94 }
95
96 uint32_t bmw_small_s2(uint32_t x){
97         uint32_t r;
98         r =   SHR32(x, 2)
99                 ^ SHL32(x, 1)
100                 ^ ROTL32(x, 12)
101                 ^ ROTR32(x, 7);
102         return r;       
103 }
104
105 uint32_t bmw_small_s3(uint32_t x){
106         uint32_t r;
107         r =   SHR32(x, 2)
108                 ^ SHL32(x, 2)
109                 ^ ROTL32(x, 15)
110                 ^ ROTR32(x, 3);
111         return r;       
112 }
113
114 uint32_t bmw_small_s4(uint32_t x){
115         uint32_t r;
116         r =   SHR32(x, 1)
117                 ^ x;
118         return r;       
119 }
120
121 uint32_t bmw_small_s5(uint32_t x){
122         uint32_t r;
123         r =   SHR32(x, 2)
124                 ^ x;
125         return r;       
126 }
127
128 uint32_t bmw_small_r1(uint32_t x){
129         uint32_t r;
130         r =   ROTL32(x, 3);
131         return r;       
132 }
133
134 uint32_t bmw_small_r2(uint32_t x){
135         uint32_t r;
136         r =   ROTL32(x, 7);
137         return r;       
138 }
139
140 uint32_t bmw_small_r3(uint32_t x){
141         uint32_t r;
142         r =   ROTL32(x, 13);
143         return r;       
144 }
145
146 uint32_t bmw_small_r4(uint32_t x){
147         uint32_t r;
148         r =   ROTL32(x, 16);
149         return r;       
150 }
151
152 uint32_t bmw_small_r5(uint32_t x){
153         uint32_t r;
154         r =   ROTR32(x, 13);
155         return r;       
156 }
157
158 uint32_t bmw_small_r6(uint32_t x){
159         uint32_t r;
160         r =   ROTR32(x, 9);
161         return r;       
162 }
163
164 uint32_t bmw_small_r7(uint32_t x){
165         uint32_t r;
166         r =   ROTR32(x, 5);
167         return r;       
168 }
169 /*
170 #define K 0x05555555L
171 static
172 uint32_t k_lut[] PROGMEM = {
173         16L*K, 17L*K, 18L*K, 19L*K, 20L*K, 21L*K, 22L*K, 23L*K,
174         24L*K, 25L*K, 26L*K, 27L*K, 28L*K, 29L*K, 30L*K, 31L*K
175 };
176 */
177 /* same as above but precomputed to avoid compiler warnings */
178
179 static
180 uint32_t k_lut[] PROGMEM = {
181         0x55555550L, 0x5aaaaaa5L, 0x5ffffffaL, 
182         0x6555554fL, 0x6aaaaaa4L, 0x6ffffff9L, 
183         0x7555554eL, 0x7aaaaaa3L, 0x7ffffff8L, 
184         0x8555554dL, 0x8aaaaaa2L, 0x8ffffff7L,
185         0x9555554cL, 0x9aaaaaa1L, 0x9ffffff6L, 
186         0xa555554bL };
187
188
189 uint32_t bmw_small_expand1(uint8_t j, const uint32_t* q, const void* m){
190         uint32_t(*s[])(uint32_t) = {bmw_small_s1, bmw_small_s2, bmw_small_s3, bmw_small_s0};
191         uint32_t r;
192         uint8_t i;
193         /* r = 0x05555555*(j+16); */
194         r = pgm_read_dword(k_lut+j);
195         for(i=0; i<16; ++i){
196                 r += s[i%4](q[j+i]);
197         }
198         r += ((uint32_t*)m)[j];
199         r += ((uint32_t*)m)[j+3];
200         r -= ((uint32_t*)m)[j+10];
201         return r;
202 }
203
204 uint32_t bmw_small_expand2(uint8_t j, const uint32_t* q, const void* m){
205         uint32_t(*rf[])(uint32_t) = {bmw_small_r1, bmw_small_r2, bmw_small_r3,
206                                      bmw_small_r4, bmw_small_r5, bmw_small_r6,
207                                                              bmw_small_r7};
208         uint32_t r;
209         uint8_t i;
210         /* r = 0x05555555*(j+16); */
211         r = pgm_read_dword(k_lut+j);
212         for(i=0; i<14; i+=2){
213                 r += q[j+i];
214         }
215         for(i=0; i<14; i+=2){
216                 r += rf[i/2](q[j+i+1]);
217         }
218         r += bmw_small_s5(q[j+14]);
219         r += bmw_small_s4(q[j+15]);
220         r += ((uint32_t*)m)[j];
221         r += ((uint32_t*)m)[(j+3)%16];
222         r -= ((uint32_t*)m)[(j+10)%16];
223         return r;
224 }
225 #if F0_HACK
226 static
227 uint8_t f0_lut[] PROGMEM = {
228          5<<1, ( 7<<1)+1, (10<<1)+0, (13<<1)+0, (14<<1)+0,
229          6<<1, ( 8<<1)+1, (11<<1)+0, (14<<1)+0, (15<<1)+1,
230          0<<1, ( 7<<1)+0, ( 9<<1)+0, (12<<1)+1, (15<<1)+0,
231          0<<1, ( 1<<1)+1, ( 8<<1)+0, (10<<1)+1, (13<<1)+0,
232          1<<1, ( 2<<1)+0, ( 9<<1)+0, (11<<1)+1, (14<<1)+1,
233          3<<1, ( 2<<1)+1, (10<<1)+0, (12<<1)+1, (15<<1)+0,
234          4<<1, ( 0<<1)+1, ( 3<<1)+1, (11<<1)+1, (13<<1)+0, 
235          1<<1, ( 4<<1)+1, ( 5<<1)+1, (12<<1)+1, (14<<1)+1,
236          2<<1, ( 5<<1)+1, ( 6<<1)+1, (13<<1)+0, (15<<1)+1,
237          0<<1, ( 3<<1)+1, ( 6<<1)+0, ( 7<<1)+1, (14<<1)+0,
238          8<<1, ( 1<<1)+1, ( 4<<1)+1, ( 7<<1)+1, (15<<1)+0,
239          8<<1, ( 0<<1)+1, ( 2<<1)+1, ( 5<<1)+1, ( 9<<1)+0,
240          1<<1, ( 3<<1)+0, ( 6<<1)+1, ( 9<<1)+1, (10<<1)+0,
241          2<<1, ( 4<<1)+0, ( 7<<1)+0, (10<<1)+0, (11<<1)+0,
242          3<<1, ( 5<<1)+1, ( 8<<1)+0, (11<<1)+1, (12<<1)+1,
243         12<<1, ( 4<<1)+1, ( 6<<1)+1, ( 9<<1)+1, (13<<1)+0
244 };
245
246 void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
247         uint8_t i,j=-1,v,sign,l=0;
248         uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
249                                    bmw_small_s3, bmw_small_s4 };
250         for(i=0; i<16; ++i){
251                 h[i] ^= ((uint32_t*)m)[i];
252         }
253         dump_x(h, 16, 'T');
254         // memset(q, 0, 4*16);
255         for(i=0; i<5*16; ++i){
256                 v = pgm_read_byte(f0_lut+i);
257                 sign = v&1;
258                 v >>=1;
259                 if(i==l){
260                         j++;
261                         l+=5;
262                         q[j] = h[v];
263                         continue;
264                 }
265                 if(sign){
266                         q[j] -= h[v];
267                 }else{
268                         q[j] += h[v];
269                 }
270         }
271         dump_x(q, 16, 'W');
272         for(i=0; i<16; ++i){
273                 q[i] = s[i%5](q[i]);
274         }       
275 }
276
277 #else
278 void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
279         uint8_t i;
280         uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
281                                    bmw_small_s3, bmw_small_s4 };
282         for(i=0; i<16; ++i){
283                 h[i] ^= ((uint32_t*)m)[i];
284         }
285         dump_x(h, 16, 'T');
286         q[ 0] = (h[ 5] - h[ 7] + h[10] + h[13] + h[14]);
287         q[ 1] = (h[ 6] - h[ 8] + h[11] + h[14] - h[15]);
288         q[ 2] = (h[ 0] + h[ 7] + h[ 9] - h[12] + h[15]);
289         q[ 3] = (h[ 0] - h[ 1] + h[ 8] - h[10] + h[13]);
290         q[ 4] = (h[ 1] + h[ 2] + h[ 9] - h[11] - h[14]);
291         q[ 5] = (h[ 3] - h[ 2] + h[10] - h[12] + h[15]);
292         q[ 6] = (h[ 4] - h[ 0] - h[ 3] - h[11] + h[13]); 
293         q[ 7] = (h[ 1] - h[ 4] - h[ 5] - h[12] - h[14]);
294         q[ 8] = (h[ 2] - h[ 5] - h[ 6] + h[13] - h[15]);
295         q[ 9] = (h[ 0] - h[ 3] + h[ 6] - h[ 7] + h[14]);
296         q[10] = (h[ 8] - h[ 1] - h[ 4] - h[ 7] + h[15]);
297         q[11] = (h[ 8] - h[ 0] - h[ 2] - h[ 5] + h[ 9]);
298         q[12] = (h[ 1] + h[ 3] - h[ 6] - h[ 9] + h[10]);
299         q[13] = (h[ 2] + h[ 4] + h[ 7] + h[10] + h[11]);
300         q[14] = (h[ 3] - h[ 5] + h[ 8] - h[11] - h[12]);
301         q[15] = (h[12] - h[ 4] - h[ 6] - h[ 9] + h[13]); 
302         dump_x(q, 16, 'W');
303         for(i=0; i<16; ++i){
304                 q[i] = s[i%5](q[i]);
305         }       
306 }
307 #endif
308
309 void bmw_small_f1(uint32_t* q, const void* m){
310         uint8_t i;
311         q[16] = bmw_small_expand1(0, q, m);
312         q[17] = bmw_small_expand1(1, q, m);
313         for(i=2; i<16; ++i){
314                 q[16+i] = bmw_small_expand2(i, q, m);
315         }
316 }
317
318 void bmw_small_f2(uint32_t* h, const uint32_t* q, const void* m){
319         uint32_t xl=0, xh;
320         uint8_t i;
321         for(i=16;i<24;++i){
322                 xl ^= q[i];
323         }
324         xh = xl;
325         for(i=24;i<32;++i){
326                 xh ^= q[i];
327         }
328 #if DEBUG       
329         cli_putstr_P(PSTR("\r\n XL = "));
330         cli_hexdump_rev(&xl, 4);
331         cli_putstr_P(PSTR("\r\n XH = "));
332         cli_hexdump_rev(&xh, 4);
333 #endif
334         memcpy(h, m, 16*4);
335         h[0] ^= SHL32(xh, 5) ^ SHR32(q[16], 5);
336         h[1] ^= SHR32(xh, 7) ^ SHL32(q[17], 8);
337         h[2] ^= SHR32(xh, 5) ^ SHL32(q[18], 5);
338         h[3] ^= SHR32(xh, 1) ^ SHL32(q[19], 5);
339         h[4] ^= SHR32(xh, 3) ^ q[20];
340         h[5] ^= SHL32(xh, 6) ^ SHR32(q[21], 6);
341         h[6] ^= SHR32(xh, 4) ^ SHL32(q[22], 6);
342         h[7] ^= SHR32(xh,11) ^ SHL32(q[23], 2);
343         for(i=0; i<8; ++i){
344                 h[i] += xl ^ q[24+i] ^ q[i];
345         }
346         for(i=0; i<8; ++i){
347                 h[8+i] ^= xh ^ q[24+i];
348                 h[8+i] += ROTL32(h[(4+i)%8],i+9);
349         }
350         h[ 8] += SHL32(xl, 8) ^ q[23] ^ q[ 8];
351         h[ 9] += SHR32(xl, 6) ^ q[16] ^ q[ 9];
352         h[10] += SHL32(xl, 6) ^ q[17] ^ q[10];
353         h[11] += SHL32(xl, 4) ^ q[18] ^ q[11];
354         h[12] += SHR32(xl, 3) ^ q[19] ^ q[12];
355         h[13] += SHR32(xl, 4) ^ q[20] ^ q[13];
356         h[14] += SHR32(xl, 7) ^ q[21] ^ q[14];
357         h[15] += SHR32(xl, 2) ^ q[22] ^ q[15];
358 }
359
360 void bmw_small_nextBlock(bmw_small_ctx_t* ctx, const void* block){
361         uint32_t q[32];
362         dump_x(block, 16, 'M');
363         bmw_small_f0(q, ctx->h, block);
364         dump_x(q, 16, 'Q');
365         bmw_small_f1(q, block);
366         dump_x(q, 32, 'Q');
367         bmw_small_f2(ctx->h, q, block);
368         ctx->counter += 1;
369         ctx_dump(ctx);
370 }
371
372 void bmw_small_lastBlock(bmw_small_ctx_t* ctx, const void* block, uint16_t length_b){
373         uint8_t buffer[64];
374         while(length_b >= BMW_SMALL_BLOCKSIZE){
375                 bmw_small_nextBlock(ctx, block);
376                 length_b -= BMW_SMALL_BLOCKSIZE;
377                 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
378         }
379         memset(buffer, 0, 64);
380         memcpy(buffer, block, (length_b+7)/8);
381         buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
382         if(length_b+1>64*8-64){
383                 bmw_small_nextBlock(ctx, buffer);
384                 memset(buffer, 0, 64-8);
385                 ctx->counter -= 1;
386         }
387         *((uint64_t*)&(buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
388         bmw_small_nextBlock(ctx, buffer);
389 }
390
391 void bmw224_init(bmw224_ctx_t* ctx){
392         uint8_t i;
393         ctx->h[0] = 0x00010203;
394         for(i=1; i<16; ++i){
395                 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
396         }
397 #if BUG24       
398         ctx->h[13] = 0x24353637;
399 #endif
400         ctx->counter=0;
401         ctx_dump(ctx);
402 }
403
404 void bmw256_init(bmw256_ctx_t* ctx){
405         uint8_t i;
406         ctx->h[0] = 0x40414243;
407         for(i=1; i<16; ++i){
408                 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
409         }
410         ctx->counter=0;
411         ctx_dump(ctx);
412 }
413
414 void bmw224_nextBlock(bmw224_ctx_t* ctx, const void* block){
415         bmw_small_nextBlock(ctx, block);
416 }
417
418 void bmw256_nextBlock(bmw256_ctx_t* ctx, const void* block){
419         bmw_small_nextBlock(ctx, block);
420 }
421
422 void bmw224_lastBlock(bmw224_ctx_t* ctx, const void* block, uint16_t length_b){
423         bmw_small_lastBlock(ctx, block, length_b);
424 }
425
426 void bmw256_lastBlock(bmw256_ctx_t* ctx, const void* block, uint16_t length_b){
427         bmw_small_lastBlock(ctx, block, length_b);
428 }
429
430 void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
431         memcpy(dest, &(ctx->h[9]), 224/8);
432 }
433
434 void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
435         memcpy(dest, &(ctx->h[8]), 256/8);
436 }
437
438 void bmw224(void* dest, const void* msg, uint32_t length_b){
439         bmw_small_ctx_t ctx;
440         bmw224_init(&ctx);
441         while(length_b>=BMW_SMALL_BLOCKSIZE){
442                 bmw_small_nextBlock(&ctx, msg);
443                 length_b -= BMW_SMALL_BLOCKSIZE;
444                 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
445         }
446         bmw_small_lastBlock(&ctx, msg, length_b);
447         bmw224_ctx2hash(dest, &ctx);
448 }
449
450 void bmw256(void* dest, const void* msg, uint32_t length_b){
451         bmw_small_ctx_t ctx;
452         bmw256_init(&ctx);
453         while(length_b>=BMW_SMALL_BLOCKSIZE){
454                 bmw_small_nextBlock(&ctx, msg);
455                 length_b -= BMW_SMALL_BLOCKSIZE;
456                 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
457         }
458         bmw_small_lastBlock(&ctx, msg, length_b);
459         bmw256_ctx2hash(dest, &ctx);
460 }
461