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