3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
22 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
30 #include <avr/pgmspace.h>
31 #include "bmw_large.h"
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))))
46 void ctx_dump(const bmw_large_ctx_t* ctx){
48 cli_putstr_P(PSTR("\r\n==== ctx dump ===="));
50 cli_putstr_P(PSTR("\r\n h["));
52 cli_putstr_P(PSTR("] = "));
53 cli_hexdump_rev(&(ctx->h[i]), 8);
55 cli_putstr_P(PSTR("\r\n counter = "));
56 cli_hexdump(&(ctx->counter), 4);
59 void dump_x(const uint64_t* q, uint8_t elements, char x){
61 cli_putstr_P(PSTR("\r\n==== "));
63 cli_putstr_P(PSTR(" dump ===="));
64 for(i=0; i<elements;++i){
65 cli_putstr_P(PSTR("\r\n "));
67 cli_putstr_P(PSTR("["));
69 cli_putstr_P(PSTR("] = "));
70 cli_hexdump_rev(&(q[i]), 8);
79 uint64_t bmw_large_s0(uint64_t x){
89 uint64_t bmw_large_s1(uint64_t x){
99 uint64_t bmw_large_s2(uint64_t x){
109 uint64_t bmw_large_s3(uint64_t x){
119 uint64_t bmw_large_s4(uint64_t x){
127 uint64_t bmw_large_s5(uint64_t x){
135 uint64_t bmw_large_r1(uint64_t x){
142 uint64_t bmw_large_r2(uint64_t x){
149 uint64_t bmw_large_r3(uint64_t x){
156 uint64_t bmw_large_r4(uint64_t x){
163 uint64_t bmw_large_r5(uint64_t x){
165 r = ROTR64(x, 64-37);
170 uint64_t bmw_large_r6(uint64_t x){
172 r = ROTR64(x, 64-43);
177 uint64_t bmw_large_r7(uint64_t x){
179 r = ROTR64(x, 64-53);
183 #define K 0x0555555555555555LL
184 #define MASK 0xFFFFFFFFFFFFFFFFLL
186 uint64_t k_lut[] PROGMEM = {
187 16LL*K, 17LL*K, 18LL*K, 19LL*K,
188 20LL*K, 21LL*K, 22LL*K, 23LL*K,
189 24LL*K, 25LL*K, 26LL*K, 27LL*K,
190 28LL*K, 29LL*K, 30LL*K, 31LL*K };
192 /* the same as above but precomputed to avoid compiler warnings */
194 uint64_t k_lut[] PROGMEM = {
195 0x5555555555555550LL, 0x5aaaaaaaaaaaaaa5LL, 0x5ffffffffffffffaLL,
196 0x655555555555554fLL, 0x6aaaaaaaaaaaaaa4LL, 0x6ffffffffffffff9LL,
197 0x755555555555554eLL, 0x7aaaaaaaaaaaaaa3LL, 0x7ffffffffffffff8LL,
198 0x855555555555554dLL, 0x8aaaaaaaaaaaaaa2LL, 0x8ffffffffffffff7LL,
199 0x955555555555554cLL, 0x9aaaaaaaaaaaaaa1LL, 0x9ffffffffffffff6LL,
200 0xa55555555555554bLL };
203 uint64_t bmw_large_expand1(uint8_t j, const uint64_t* q, const void* m, const void* h){
204 uint64_t(*s[])(uint64_t) = {bmw_large_s1, bmw_large_s2, bmw_large_s3, bmw_large_s0};
211 /* r = 0x0555555555555555LL*(j+16); */
212 r.v32[0] = pgm_read_dword(((uint8_t*)k_lut+8*j));
213 r.v32[1] = pgm_read_dword(((uint8_t*)k_lut+8*j+4));
218 a += ( ROTL64(((uint64_t*)m)[(j)&0xf], ((j+ 0)&0xf)+1)
219 + ROTL64(((uint64_t*)m)[(j+3)&0xf], ((j+ 3)&0xf)+1)
221 - ROTL64(((uint64_t*)m)[(j+10)&0xf],((j+10)&0xf)+1)
222 ) ^ ((uint64_t*)h)[(j+7)&0xf];
224 a += ((uint64_t*)m)[j&0xf];
225 a += ((uint64_t*)m)[(j+3)&0xf];
226 a -= ((uint64_t*)m)[(j+10)&0xf];
233 uint64_t bmw_large_expand2(uint8_t j, const uint64_t* q, const void* m, const void* h){
234 uint64_t(*rf[])(uint64_t) = {bmw_large_r1, bmw_large_r2, bmw_large_r3,
235 bmw_large_r4, bmw_large_r5, bmw_large_r6,
243 /* r = 0x0555555555555555LL*(j+16); */
244 r.v32[0] = pgm_read_dword(((uint8_t*)k_lut+8*j));
245 r.v32[1] = pgm_read_dword(((uint8_t*)k_lut+8*j+4));
246 for(i=0; i<14; i+=2){
249 for(i=0; i<14; i+=2){
250 a += rf[i/2](q[j+i+1]);
253 a += bmw_large_s4(q[j+14]);
254 a += bmw_large_s5(q[j+15]);
256 a += bmw_large_s5(q[j+14]);
257 a += bmw_large_s4(q[j+15]);
263 cli_putstr_P(PSTR("\n+++++++++ expand_2 ++++++++++++"));
266 t=ROTL64(((uint64_t*)m)[j], ((j+ 0)&0xf)+1);
268 t=ROTL64(((uint64_t*)m)[j], ((j+ 3)&0xf)+1);
270 t=ROTL64(((uint64_t*)m)[j], ((j+ 0)&0xf)+1);
275 a += ( ROTL64(((uint64_t*)m)[(j)&0xf], ((j+ 0)&0xf)+1)
276 + ROTL64(((uint64_t*)m)[(j+3)&0xf], ((j+ 3)&0xf)+1)
278 - ROTL64(((uint64_t*)m)[(j+10)&0xf],((j+10)&0xf)+1)
279 ) ^ ((uint64_t*)h)[(j+7)&0xf];
281 a += ((uint64_t*)m)[j&0xf];
282 a += ((uint64_t*)m)[(j+3)&0xf];
283 a -= ((uint64_t*)m)[(j+10)&0xf];
290 /* to understand this implementation take a look at f0-opt-table.txt */
291 static uint16_t hack_table[5] PROGMEM = { 0x0311, 0xDDB3, 0x2A79, 0x07AA, 0x51C2 };
292 static uint8_t offset_table[5] PROGMEM = { 4+16, 6+16, 9+16, 12+16, 13+16 };
296 void bmw_large_f0(uint64_t* q, const uint64_t* h, const void* m){
299 uint64_t(*s[])(uint64_t)={ bmw_large_s0, bmw_large_s1, bmw_large_s2,
300 bmw_large_s3, bmw_large_s4 };
302 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
309 j=pgm_read_byte(offset_table+c);
310 hack_reg=pgm_read_word(&(hack_table[c]));
327 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
330 q[i] += h[(i+1)&0xf];
334 #endif /* F0_HACK==2 */
338 uint8_t f0_lut[] PROGMEM ={
339 5<<1, ( 7<<1)+1, (10<<1)+0, (13<<1)+0, (14<<1)+0,
340 6<<1, ( 8<<1)+1, (11<<1)+0, (14<<1)+0, (15<<1)+1,
341 0<<1, ( 7<<1)+0, ( 9<<1)+0, (12<<1)+1, (15<<1)+0,
342 0<<1, ( 1<<1)+1, ( 8<<1)+0, (10<<1)+1, (13<<1)+0,
343 1<<1, ( 2<<1)+0, ( 9<<1)+0, (11<<1)+1, (14<<1)+1,
344 3<<1, ( 2<<1)+1, (10<<1)+0, (12<<1)+1, (15<<1)+0,
345 4<<1, ( 0<<1)+1, ( 3<<1)+1, (11<<1)+1, (13<<1)+0,
346 1<<1, ( 4<<1)+1, ( 5<<1)+1, (12<<1)+1, (14<<1)+1,
347 2<<1, ( 5<<1)+1, ( 6<<1)+1, (13<<1)+0, (15<<1)+1,
348 0<<1, ( 3<<1)+1, ( 6<<1)+0, ( 7<<1)+1, (14<<1)+0,
349 8<<1, ( 1<<1)+1, ( 4<<1)+1, ( 7<<1)+1, (15<<1)+0,
350 8<<1, ( 0<<1)+1, ( 2<<1)+1, ( 5<<1)+1, ( 9<<1)+0,
351 1<<1, ( 3<<1)+0, ( 6<<1)+1, ( 9<<1)+1, (10<<1)+0,
352 2<<1, ( 4<<1)+0, ( 7<<1)+0, (10<<1)+0, (11<<1)+0,
353 3<<1, ( 5<<1)+1, ( 8<<1)+0, (11<<1)+1, (12<<1)+1,
354 12<<1, ( 4<<1)+1, ( 6<<1)+1, ( 9<<1)+1, (13<<1)+0
358 void bmw_large_f0(uint64_t* q, const uint64_t* h, const void* m){
359 uint8_t i,j=-1,v,sign,l=0;
360 uint64_t(*s[])(uint64_t)={ bmw_large_s0, bmw_large_s1, bmw_large_s2,
361 bmw_large_s3, bmw_large_s4 };
363 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
366 // memset(q, 0, 4*16);
367 for(i=0; i<5*16; ++i){
368 v = pgm_read_byte(f0_lut+i);
389 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
392 q[i] += h[(i+1)&0xf];
396 #endif /* F0_HACK==1 */
400 void bmw_large_f0(uint64_t* q, const uint64_t* h, const void* m){
402 uint64_t(*s[])(uint64_t)={ bmw_large_s0, bmw_large_s1, bmw_large_s2,
403 bmw_large_s3, bmw_large_s4 };
405 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
407 // dump_x(t, 16, 'T');
408 q[ 0] = (h[ 5] - h[ 7] + h[10] + h[13] + h[14]);
409 q[ 1] = (h[ 6] - h[ 8] + h[11] + h[14] - h[15]);
410 q[ 2] = (h[ 0] + h[ 7] + h[ 9] - h[12] + h[15]);
411 q[ 3] = (h[ 0] - h[ 1] + h[ 8] - h[10] + h[13]);
412 q[ 4] = (h[ 1] + h[ 2] + h[ 9] - h[11] - h[14]);
413 q[ 5] = (h[ 3] - h[ 2] + h[10] - h[12] + h[15]);
414 q[ 6] = (h[ 4] - h[ 0] - h[ 3] - h[11] + h[13]);
415 q[ 7] = (h[ 1] - h[ 4] - h[ 5] - h[12] - h[14]);
416 q[ 8] = (h[ 2] - h[ 5] - h[ 6] + h[13] - h[15]);
417 q[ 9] = (h[ 0] - h[ 3] + h[ 6] - h[ 7] + h[14]);
418 q[10] = (h[ 8] - h[ 1] - h[ 4] - h[ 7] + h[15]);
419 q[11] = (h[ 8] - h[ 0] - h[ 2] - h[ 5] + h[ 9]);
420 q[12] = (h[ 1] + h[ 3] - h[ 6] - h[ 9] + h[10]);
421 q[13] = (h[ 2] + h[ 4] + h[ 7] + h[10] + h[11]);
422 q[14] = (h[ 3] - h[ 5] + h[ 8] - h[11] - h[12]);
423 q[15] = (h[12] - h[ 4] - h[ 6] - h[ 9] + h[13]);
430 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
433 q[i] += h[(i+1)&0xf];
438 #endif /* F0_HACK==0 */
441 void bmw_large_f1(uint64_t* q, const void* m, const uint64_t* h){
443 q[16] = bmw_large_expand1(0, q, m, h);
444 q[17] = bmw_large_expand1(1, q, m, h);
446 q[16+i] = bmw_large_expand2(i, q, m, h);
451 void bmw_large_f2(uint64_t* h, const uint64_t* q, const void* m){
462 cli_putstr_P(PSTR("\r\n XL = "));
463 cli_hexdump_rev(&xl, 4);
464 cli_putstr_P(PSTR("\r\n XH = "));
465 cli_hexdump_rev(&xh, 4);
468 h[0] ^= SHL64(xh, 5) ^ SHR64(q[16], 5);
469 h[1] ^= SHR64(xh, 7) ^ SHL64(q[17], 8);
470 h[2] ^= SHR64(xh, 5) ^ SHL64(q[18], 5);
471 h[3] ^= SHR64(xh, 1) ^ SHL64(q[19], 5);
472 h[4] ^= SHR64(xh, 3) ^ q[20];
473 h[5] ^= SHL64(xh, 6) ^ SHR64(q[21], 6);
474 h[6] ^= SHR64(xh, 4) ^ SHL64(q[22], 6);
475 h[7] ^= SHR64(xh,11) ^ SHL64(q[23], 2);
477 h[i] += xl ^ q[24+i] ^ q[i];
480 h[8+i] ^= xh ^ q[24+i];
481 h[8+i] += ROTL64(h[(4+i)%8],i+9);
483 h[ 8] += SHL64(xl, 8) ^ q[23] ^ q[ 8];
484 h[ 9] += SHR64(xl, 6) ^ q[16] ^ q[ 9];
485 h[10] += SHL64(xl, 6) ^ q[17] ^ q[10];
486 h[11] += SHL64(xl, 4) ^ q[18] ^ q[11];
487 h[12] += SHR64(xl, 3) ^ q[19] ^ q[12];
488 h[13] += SHR64(xl, 4) ^ q[20] ^ q[13];
489 h[14] += SHR64(xl, 7) ^ q[21] ^ q[14];
490 h[15] += SHR64(xl, 2) ^ q[22] ^ q[15];
493 void bmw_large_nextBlock(bmw_large_ctx_t* ctx, const void* block){
495 dump_x(block, 16, 'M');
496 bmw_large_f0(q, ctx->h, block);
498 bmw_large_f1(q, block, ctx->h);
500 bmw_large_f2(ctx->h, q, block);
505 void bmw_large_lastBlock(bmw_large_ctx_t* ctx, const void* block, uint16_t length_b){
507 while(length_b >= BMW_LARGE_BLOCKSIZE){
508 bmw_large_nextBlock(ctx, block);
509 length_b -= BMW_LARGE_BLOCKSIZE;
510 block = (uint8_t*)block + BMW_LARGE_BLOCKSIZE_B;
512 memset(buffer, 0, 128);
513 memcpy(buffer, block, (length_b+7)/8);
514 buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
515 if(length_b+1>128*8-64){
516 bmw_large_nextBlock(ctx, buffer);
517 memset(buffer, 0, 128-8);
520 *((uint64_t*)&(buffer[128-8])) = (uint64_t)(ctx->counter*1024LL)+(uint64_t)length_b;
521 bmw_large_nextBlock(ctx, buffer);
525 memset(buffer, 0xaa, 128);
527 buffer[8*i] = i + 0xa0;
529 bmw_large_f0(q, (uint64_t*)buffer, ctx->h);
530 bmw_large_f1(q, ctx->h, (uint64_t*)buffer);
531 bmw_large_f2((uint64_t*)buffer, q, ctx->h);
532 memcpy(ctx->h, buffer, 128);
536 void bmw384_init(bmw384_ctx_t* ctx){
538 ctx->h[0] = 0x0001020304050607LL;
540 ctx->h[i] = ctx->h[i-1]+ 0x0808080808080808LL;
543 ctx->h[6] = 0x3031323324353637LL;
549 void bmw512_init(bmw512_ctx_t* ctx){
551 ctx->h[0] = 0x8081828384858687LL;
553 ctx->h[i] = ctx->h[i-1]+ 0x0808080808080808LL;
559 void bmw384_nextBlock(bmw384_ctx_t* ctx, const void* block){
560 bmw_large_nextBlock(ctx, block);
563 void bmw512_nextBlock(bmw512_ctx_t* ctx, const void* block){
564 bmw_large_nextBlock(ctx, block);
567 void bmw384_lastBlock(bmw384_ctx_t* ctx, const void* block, uint16_t length_b){
568 bmw_large_lastBlock(ctx, block, length_b);
571 void bmw512_lastBlock(bmw512_ctx_t* ctx, const void* block, uint16_t length_b){
572 bmw_large_lastBlock(ctx, block, length_b);
575 void bmw384_ctx2hash(void* dest, const bmw384_ctx_t* ctx){
576 memcpy(dest, &(ctx->h[10]), 384/8);
579 void bmw512_ctx2hash(void* dest, const bmw512_ctx_t* ctx){
580 memcpy(dest, &(ctx->h[8]), 512/8);
583 void bmw384(void* dest, const void* msg, uint32_t length_b){
586 while(length_b>=BMW_LARGE_BLOCKSIZE){
587 bmw_large_nextBlock(&ctx, msg);
588 length_b -= BMW_LARGE_BLOCKSIZE;
589 msg = (uint8_t*)msg + BMW_LARGE_BLOCKSIZE_B;
591 bmw_large_lastBlock(&ctx, msg, length_b);
592 bmw384_ctx2hash(dest, &ctx);
595 void bmw512(void* dest, const void* msg, uint32_t length_b){
598 while(length_b>=BMW_LARGE_BLOCKSIZE){
599 bmw_large_nextBlock(&ctx, msg);
600 length_b -= BMW_LARGE_BLOCKSIZE;
601 msg = (uint8_t*)msg + BMW_LARGE_BLOCKSIZE_B;
603 bmw_large_lastBlock(&ctx, msg, length_b);
604 bmw512_ctx2hash(dest, &ctx);