]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bmw/bmw_large.c
tweak for BMW
[avr-crypto-lib.git] / bmw / bmw_large.c
1 /* bmw_large.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_large.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_large.h"
32
33 #define SHL64(a,n) ((a)<<(n))
34 #define SHR64(a,n) ((a)>>(n))
35 #define ROTL64(a,n) (((a)<<(n))|((a)>>(64-(n))))
36 #define ROTR64(a,n) (((a)>>(n))|((a)<<(64-(n))))
37
38 #define TWEAK   1
39 #define BUG24   0
40 #define F0_HACK 1
41 #define DEBUG   0
42
43 #if DEBUG
44  #include "cli.h"
45
46  void ctx_dump(const bmw_large_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]), 8);
54         }
55         cli_putstr_P(PSTR("\r\n counter = "));
56         cli_hexdump(&(ctx->counter), 4);
57  }
58
59  void dump_x(const uint64_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]), 8);
71         }
72  }
73 #else
74  #define ctx_dump(x)
75  #define dump_x(a,b,c)
76 #endif
77
78 uint64_t bmw_large_s0(uint64_t x){
79         uint64_t r;
80         r =   SHR64(x, 1)
81                 ^ SHL64(x, 3)
82                 ^ ROTL64(x, 4)
83                 ^ ROTR64(x, 64-37);
84         return r;
85 }
86
87 uint64_t bmw_large_s1(uint64_t x){
88         uint64_t r;
89         r =   SHR64(x, 1)
90                 ^ SHL64(x, 2)
91                 ^ ROTL64(x,13)
92                 ^ ROTR64(x,64-43);
93         return r;
94 }
95
96 uint64_t bmw_large_s2(uint64_t x){
97         uint64_t r;
98         r =   SHR64(x, 2)
99                 ^ SHL64(x, 1)
100                 ^ ROTL64(x, 19)
101                 ^ ROTR64(x, 64-53);
102         return r;
103 }
104
105 uint64_t bmw_large_s3(uint64_t x){
106         uint64_t r;
107         r =   SHR64(x, 2)
108                 ^ SHL64(x, 2)
109                 ^ ROTL64(x, 28)
110                 ^ ROTR64(x, 64-59);
111         return r;
112 }
113
114 uint64_t bmw_large_s4(uint64_t x){
115         uint64_t r;
116         r =   SHR64(x, 1)
117                 ^ x;
118         return r;
119 }
120
121 uint64_t bmw_large_s5(uint64_t x){
122         uint64_t r;
123         r =   SHR64(x, 2)
124                 ^ x;
125         return r;
126 }
127
128 uint64_t bmw_large_r1(uint64_t x){
129         uint64_t r;
130         r =   ROTL64(x, 5);
131         return r;
132 }
133
134 uint64_t bmw_large_r2(uint64_t x){
135         uint64_t r;
136         r =   ROTL64(x, 11);
137         return r;
138 }
139
140 uint64_t bmw_large_r3(uint64_t x){
141         uint64_t r;
142         r =   ROTL64(x, 27);
143         return r;
144 }
145
146 uint64_t bmw_large_r4(uint64_t x){
147         uint64_t r;
148         r =   ROTL64(x, 32);
149         return r;
150 }
151
152 uint64_t bmw_large_r5(uint64_t x){
153         uint64_t r;
154         r =   ROTR64(x, 64-37);
155         return r;
156 }
157
158 uint64_t bmw_large_r6(uint64_t x){
159         uint64_t r;
160         r =   ROTR64(x, 64-43);
161         return r;
162 }
163
164 uint64_t bmw_large_r7(uint64_t x){
165         uint64_t r;
166         r =   ROTR64(x, 64-53);
167         return r;
168 }
169 /*
170 #define K    0x0555555555555555LL
171 #define MASK 0xFFFFFFFFFFFFFFFFLL
172 static
173 uint64_t k_lut[] PROGMEM = {
174         16LL*K, 17LL*K, 18LL*K, 19LL*K,
175         20LL*K, 21LL*K, 22LL*K, 23LL*K,
176         24LL*K, 25LL*K, 26LL*K, 27LL*K,
177         28LL*K, 29LL*K, 30LL*K, 31LL*K };
178 */
179 /* the same as above but precomputed to avoid compiler warnings */
180 static
181 uint64_t k_lut[] PROGMEM = {
182         0x5555555555555550LL, 0x5aaaaaaaaaaaaaa5LL, 0x5ffffffffffffffaLL,
183         0x655555555555554fLL, 0x6aaaaaaaaaaaaaa4LL, 0x6ffffffffffffff9LL,
184         0x755555555555554eLL, 0x7aaaaaaaaaaaaaa3LL, 0x7ffffffffffffff8LL,
185         0x855555555555554dLL, 0x8aaaaaaaaaaaaaa2LL, 0x8ffffffffffffff7LL,
186         0x955555555555554cLL, 0x9aaaaaaaaaaaaaa1LL, 0x9ffffffffffffff6LL,
187         0xa55555555555554bLL };
188
189 uint64_t bmw_large_expand1(uint8_t j, const uint64_t* q, const void* m, const void* h){
190         uint64_t(*s[])(uint64_t) = {bmw_large_s1, bmw_large_s2, bmw_large_s3, bmw_large_s0};
191         uint64_t a = 0;
192         union{
193                 uint64_t v64;
194                 uint32_t v32[2];
195         } r;
196         uint8_t i;
197         /* r = 0x0555555555555555LL*(j+16); */
198         r.v32[0] = pgm_read_dword(((uint8_t*)k_lut+8*j));
199         r.v32[1] = pgm_read_dword(((uint8_t*)k_lut+8*j+4));
200         for(i=0; i<16; ++i){
201                 a += s[i%4](q[j+i]);
202         }
203 #if TWEAK
204         a += (   ROTL64(((uint64_t*)m)[(j)&0xf],   ((j+ 0)&0xf)+1)
205                + ROTL64(((uint64_t*)m)[(j+3)&0xf], ((j+ 3)&0xf)+1)
206                + r.v64
207                - ROTL64(((uint64_t*)m)[(j+10)&0xf],((j+10)&0xf)+1)
208              ) ^ ((uint64_t*)h)[(j+7)&0xf];
209 #else
210         a += ((uint64_t*)m)[j&0xf];
211         a += ((uint64_t*)m)[(j+3)&0xf];
212         a -= ((uint64_t*)m)[(j+10)&0xf];
213         a += r.v64;
214 #endif
215         return a;
216 }
217
218 uint64_t bmw_large_expand2(uint8_t j, const uint64_t* q, const void* m, const void* h){
219         uint64_t(*rf[])(uint64_t) = {bmw_large_r1, bmw_large_r2, bmw_large_r3,
220                                      bmw_large_r4, bmw_large_r5, bmw_large_r6,
221                                                              bmw_large_r7};
222         uint64_t a=0;
223         union{
224                 uint64_t v64;
225                 uint32_t v32[2];
226         } r;
227         uint8_t i;
228         /* r = 0x0555555555555555LL*(j+16); */
229         r.v32[0] = pgm_read_dword(((uint8_t*)k_lut+8*j));
230         r.v32[1] = pgm_read_dword(((uint8_t*)k_lut+8*j+4));
231         for(i=0; i<14; i+=2){
232                 a += q[j+i];
233         }
234         for(i=0; i<14; i+=2){
235                 a += rf[i/2](q[j+i+1]);
236         }
237 #if TWEAK
238         a += bmw_large_s4(q[j+14]);
239         a += bmw_large_s5(q[j+15]);
240 #else
241         a += bmw_large_s5(q[j+14]);
242         a += bmw_large_s4(q[j+15]);
243 #endif
244 #if TWEAK
245         /*
246         if(j==(22-16)){
247                 uint64_t t;
248                 cli_putstr_P(PSTR("\n+++++++++ expand_2 ++++++++++++"));
249                 dump_x(&a, 1, 'a');
250                 dump_x(&r, 1, 'r');
251                 t=ROTL64(((uint64_t*)m)[j],   ((j+ 0)&0xf)+1);
252                 dump_x(&t, 1, '0');
253                 t=ROTL64(((uint64_t*)m)[j],   ((j+ 3)&0xf)+1);
254                 dump_x(&t, 1, '0');
255                 t=ROTL64(((uint64_t*)m)[j],   ((j+ 0)&0xf)+1);
256                 dump_x(&t, 1, '0');
257
258         }
259         */
260         a += (   ROTL64(((uint64_t*)m)[(j)&0xf],   ((j+ 0)&0xf)+1)
261                + ROTL64(((uint64_t*)m)[(j+3)&0xf], ((j+ 3)&0xf)+1)
262                + r.v64
263                - ROTL64(((uint64_t*)m)[(j+10)&0xf],((j+10)&0xf)+1)
264              ) ^ ((uint64_t*)h)[(j+7)&0xf];
265 #else
266         a += ((uint64_t*)m)[j&0xf];
267         a += ((uint64_t*)m)[(j+3)&0xf];
268         a -= ((uint64_t*)m)[(j+10)&0xf];
269         a += r.v64;
270 #endif
271         return a;
272 }
273
274 #if F0_HACK
275 static
276 uint8_t f0_lut[] PROGMEM ={
277          5<<1, ( 7<<1)+1, (10<<1)+0, (13<<1)+0, (14<<1)+0,
278          6<<1, ( 8<<1)+1, (11<<1)+0, (14<<1)+0, (15<<1)+1,
279          0<<1, ( 7<<1)+0, ( 9<<1)+0, (12<<1)+1, (15<<1)+0,
280          0<<1, ( 1<<1)+1, ( 8<<1)+0, (10<<1)+1, (13<<1)+0,
281          1<<1, ( 2<<1)+0, ( 9<<1)+0, (11<<1)+1, (14<<1)+1,
282          3<<1, ( 2<<1)+1, (10<<1)+0, (12<<1)+1, (15<<1)+0,
283          4<<1, ( 0<<1)+1, ( 3<<1)+1, (11<<1)+1, (13<<1)+0,
284          1<<1, ( 4<<1)+1, ( 5<<1)+1, (12<<1)+1, (14<<1)+1,
285          2<<1, ( 5<<1)+1, ( 6<<1)+1, (13<<1)+0, (15<<1)+1,
286          0<<1, ( 3<<1)+1, ( 6<<1)+0, ( 7<<1)+1, (14<<1)+0,
287          8<<1, ( 1<<1)+1, ( 4<<1)+1, ( 7<<1)+1, (15<<1)+0,
288          8<<1, ( 0<<1)+1, ( 2<<1)+1, ( 5<<1)+1, ( 9<<1)+0,
289          1<<1, ( 3<<1)+0, ( 6<<1)+1, ( 9<<1)+1, (10<<1)+0,
290          2<<1, ( 4<<1)+0, ( 7<<1)+0, (10<<1)+0, (11<<1)+0,
291          3<<1, ( 5<<1)+1, ( 8<<1)+0, (11<<1)+1, (12<<1)+1,
292         12<<1, ( 4<<1)+1, ( 6<<1)+1, ( 9<<1)+1, (13<<1)+0
293 };
294
295 void bmw_large_f0(uint64_t* q, const uint64_t* h, const void* m){
296         uint8_t i,j=-1,v,sign,l=0;
297         uint64_t(*s[])(uint64_t)={ bmw_large_s0, bmw_large_s1, bmw_large_s2,
298                                    bmw_large_s3, bmw_large_s4 };
299         for(i=0; i<16; ++i){
300                 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
301         }
302         dump_x(h, 16, 'T');
303 //      memset(q, 0, 4*16);
304         for(i=0; i<5*16; ++i){
305                 v = pgm_read_byte(f0_lut+i);
306                 sign = v&1;
307                 v >>=1;
308                 if(i==l){
309                         j++;
310                         l+=5;
311                         q[j] = h[v];
312                         continue;
313                 }
314                 if(sign){
315                         q[j] -= h[v];
316                 }else{
317                         q[j] += h[v];
318                 }
319         }
320         dump_x(q, 16, 'W');
321         for(i=0; i<16; ++i){
322                 q[i] = s[i%5](q[i]);
323         }
324 #if TWEAK
325         for(i=0; i<16; ++i){
326                 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
327         }
328         for(i=0; i<16; ++i){
329                 q[i] += h[(i+1)&0xf];
330         }
331 #endif
332 }
333
334 #else
335 void bmw_large_f0(uint64_t* q, const uint64_t* h, const void* m){
336         uint8_t i;
337         uint64_t(*s[])(uint64_t)={ bmw_large_s0, bmw_large_s1, bmw_large_s2,
338                                    bmw_large_s3, bmw_large_s4 };
339         for(i=0; i<16; ++i){
340                 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
341         }
342 //      dump_x(t, 16, 'T');
343         q[ 0] = (h[ 5] - h[ 7] + h[10] + h[13] + h[14]);
344         q[ 1] = (h[ 6] - h[ 8] + h[11] + h[14] - h[15]);
345         q[ 2] = (h[ 0] + h[ 7] + h[ 9] - h[12] + h[15]);
346         q[ 3] = (h[ 0] - h[ 1] + h[ 8] - h[10] + h[13]);
347         q[ 4] = (h[ 1] + h[ 2] + h[ 9] - h[11] - h[14]);
348         q[ 5] = (h[ 3] - h[ 2] + h[10] - h[12] + h[15]);
349         q[ 6] = (h[ 4] - h[ 0] - h[ 3] - h[11] + h[13]);
350         q[ 7] = (h[ 1] - h[ 4] - h[ 5] - h[12] - h[14]);
351         q[ 8] = (h[ 2] - h[ 5] - h[ 6] + h[13] - h[15]);
352         q[ 9] = (h[ 0] - h[ 3] + h[ 6] - h[ 7] + h[14]);
353         q[10] = (h[ 8] - h[ 1] - h[ 4] - h[ 7] + h[15]);
354         q[11] = (h[ 8] - h[ 0] - h[ 2] - h[ 5] + h[ 9]);
355         q[12] = (h[ 1] + h[ 3] - h[ 6] - h[ 9] + h[10]);
356         q[13] = (h[ 2] + h[ 4] + h[ 7] + h[10] + h[11]);
357         q[14] = (h[ 3] - h[ 5] + h[ 8] - h[11] - h[12]);
358         q[15] = (h[12] - h[ 4] - h[ 6] - h[ 9] + h[13]);
359         dump_x(q, 16, 'W');
360         for(i=0; i<16; ++i){
361                 q[i] = s[i%5](q[i]);
362         }
363 #if TWEAK
364         for(i=0; i<16; ++i){
365                 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
366         }
367         for(i=0; i<16; ++i){
368                 q[i] += h[(i+1)&0xf];
369         }
370 #endif
371
372 }
373 #endif
374
375 void bmw_large_f1(uint64_t* q, const void* m, const uint64_t* h){
376         uint8_t i;
377         q[16] = bmw_large_expand1(0, q, m, h);
378         q[17] = bmw_large_expand1(1, q, m, h);
379         for(i=2; i<16; ++i){
380                 q[16+i] = bmw_large_expand2(i, q, m, h);
381         }
382 }
383
384 void bmw_large_f2(uint64_t* h, const uint64_t* q, const void* m){
385         uint64_t xl=0, xh;
386         uint8_t i;
387         for(i=16;i<24;++i){
388                 xl ^= q[i];
389         }
390         xh = xl;
391         for(i=24;i<32;++i){
392                 xh ^= q[i];
393         }
394 #if DEBUG
395         cli_putstr_P(PSTR("\r\n XL = "));
396         cli_hexdump_rev(&xl, 4);
397         cli_putstr_P(PSTR("\r\n XH = "));
398         cli_hexdump_rev(&xh, 4);
399 #endif
400         memcpy(h, m, 16*8);
401         h[0] ^= SHL64(xh, 5) ^ SHR64(q[16], 5);
402         h[1] ^= SHR64(xh, 7) ^ SHL64(q[17], 8);
403         h[2] ^= SHR64(xh, 5) ^ SHL64(q[18], 5);
404         h[3] ^= SHR64(xh, 1) ^ SHL64(q[19], 5);
405         h[4] ^= SHR64(xh, 3) ^ q[20];
406         h[5] ^= SHL64(xh, 6) ^ SHR64(q[21], 6);
407         h[6] ^= SHR64(xh, 4) ^ SHL64(q[22], 6);
408         h[7] ^= SHR64(xh,11) ^ SHL64(q[23], 2);
409         for(i=0; i<8; ++i){
410                 h[i] += xl ^ q[24+i] ^ q[i];
411         }
412         for(i=0; i<8; ++i){
413                 h[8+i] ^= xh ^ q[24+i];
414                 h[8+i] += ROTL64(h[(4+i)%8],i+9);
415         }
416         h[ 8] += SHL64(xl, 8) ^ q[23] ^ q[ 8];
417         h[ 9] += SHR64(xl, 6) ^ q[16] ^ q[ 9];
418         h[10] += SHL64(xl, 6) ^ q[17] ^ q[10];
419         h[11] += SHL64(xl, 4) ^ q[18] ^ q[11];
420         h[12] += SHR64(xl, 3) ^ q[19] ^ q[12];
421         h[13] += SHR64(xl, 4) ^ q[20] ^ q[13];
422         h[14] += SHR64(xl, 7) ^ q[21] ^ q[14];
423         h[15] += SHR64(xl, 2) ^ q[22] ^ q[15];
424 }
425
426 void bmw_large_nextBlock(bmw_large_ctx_t* ctx, const void* block){
427         uint64_t q[32];
428         dump_x(block, 16, 'M');
429         bmw_large_f0(q, ctx->h, block);
430         dump_x(q, 16, 'Q');
431         bmw_large_f1(q, block, ctx->h);
432         dump_x(q, 32, 'Q');
433         bmw_large_f2(ctx->h, q, block);
434         ctx->counter += 1;
435         ctx_dump(ctx);
436 }
437
438 void bmw_large_lastBlock(bmw_large_ctx_t* ctx, const void* block, uint16_t length_b){
439         uint8_t buffer[128];
440         while(length_b >= BMW_LARGE_BLOCKSIZE){
441                 bmw_large_nextBlock(ctx, block);
442                 length_b -= BMW_LARGE_BLOCKSIZE;
443                 block = (uint8_t*)block + BMW_LARGE_BLOCKSIZE_B;
444         }
445         memset(buffer, 0, 128);
446         memcpy(buffer, block, (length_b+7)/8);
447         buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
448         if(length_b+1>128*8-64){
449                 bmw_large_nextBlock(ctx, buffer);
450                 memset(buffer, 0, 128-8);
451                 ctx->counter -= 1;
452         }
453         *((uint64_t*)&(buffer[128-8])) = (uint64_t)(ctx->counter*1024LL)+(uint64_t)length_b;
454         bmw_large_nextBlock(ctx, buffer);
455 #if TWEAK
456         uint8_t i;
457         uint64_t q[32];
458         memset(buffer, 0xaa, 128);
459         for(i=0; i<16; ++i){
460                 buffer[8*i] = i + 0xa0;
461         }
462         bmw_large_f0(q, (uint64_t*)buffer, ctx->h);
463         bmw_large_f1(q, ctx->h, (uint64_t*)buffer);
464         bmw_large_f2((uint64_t*)buffer, q, ctx->h);
465         memcpy(ctx->h, buffer, 128);
466 #endif
467 }
468
469 void bmw384_init(bmw384_ctx_t* ctx){
470         uint8_t i;
471         ctx->h[0] = 0x0001020304050607LL;
472         for(i=1; i<16; ++i){
473                 ctx->h[i] = ctx->h[i-1]+ 0x0808080808080808LL;
474         }
475 #if BUG24
476         ctx->h[6] = 0x3031323324353637LL;
477 #endif
478         ctx->counter=0;
479         ctx_dump(ctx);
480 }
481
482 void bmw512_init(bmw512_ctx_t* ctx){
483         uint8_t i;
484         ctx->h[0] = 0x8081828384858687LL;
485         for(i=1; i<16; ++i){
486                 ctx->h[i] = ctx->h[i-1]+ 0x0808080808080808LL;
487         }
488         ctx->counter=0;
489         ctx_dump(ctx);
490 }
491
492 void bmw384_nextBlock(bmw384_ctx_t* ctx, const void* block){
493         bmw_large_nextBlock(ctx, block);
494 }
495
496 void bmw512_nextBlock(bmw512_ctx_t* ctx, const void* block){
497         bmw_large_nextBlock(ctx, block);
498 }
499
500 void bmw384_lastBlock(bmw384_ctx_t* ctx, const void* block, uint16_t length_b){
501         bmw_large_lastBlock(ctx, block, length_b);
502 }
503
504 void bmw512_lastBlock(bmw512_ctx_t* ctx, const void* block, uint16_t length_b){
505         bmw_large_lastBlock(ctx, block, length_b);
506 }
507
508 void bmw384_ctx2hash(void* dest, const bmw384_ctx_t* ctx){
509         memcpy(dest, &(ctx->h[10]), 384/8);
510 }
511
512 void bmw512_ctx2hash(void* dest, const bmw512_ctx_t* ctx){
513         memcpy(dest, &(ctx->h[8]), 512/8);
514 }
515
516 void bmw384(void* dest, const void* msg, uint32_t length_b){
517         bmw_large_ctx_t ctx;
518         bmw384_init(&ctx);
519         while(length_b>=BMW_LARGE_BLOCKSIZE){
520                 bmw_large_nextBlock(&ctx, msg);
521                 length_b -= BMW_LARGE_BLOCKSIZE;
522                 msg = (uint8_t*)msg + BMW_LARGE_BLOCKSIZE_B;
523         }
524         bmw_large_lastBlock(&ctx, msg, length_b);
525         bmw384_ctx2hash(dest, &ctx);
526 }
527
528 void bmw512(void* dest, const void* msg, uint32_t length_b){
529         bmw_large_ctx_t ctx;
530         bmw512_init(&ctx);
531         while(length_b>=BMW_LARGE_BLOCKSIZE){
532                 bmw_large_nextBlock(&ctx, msg);
533                 length_b -= BMW_LARGE_BLOCKSIZE;
534                 msg = (uint8_t*)msg + BMW_LARGE_BLOCKSIZE_B;
535         }
536         bmw_large_lastBlock(&ctx, msg, length_b);
537         bmw512_ctx2hash(dest, &ctx);
538 }
539