3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2010 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 "bmw_small.h"
33 #define SHL32(a,n) ((a)<<(n))
34 #define SHR32(a,n) ((a)>>(n))
35 #define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
36 #define ROTR32(a,n) (((a)>>(n))|((a)<<(32-(n))))
57 void ctx_dump(const bmw_small_ctx_t* ctx){
59 cli_putstr("\r\n==== ctx dump ====");
61 cli_putstr("\r\n h[");
64 cli_hexdump_rev(&(ctx->h[i]), 4);
66 cli_putstr("\r\n counter = ");
67 cli_hexdump(&(ctx->counter), 4);
70 void dump_x(const uint32_t* q, uint8_t elements, char x){
72 cli_putstr("\r\n==== ");
74 cli_putstr(" dump ====");
75 for(i=0; i<elements;++i){
81 cli_hexdump_rev(&(q[i]), 4);
89 #define S32_0(x) ( (SHR32((x), 1)) ^ \
94 #define S32_1(x) ( (SHR32((x), 1)) ^ \
99 #define S32_2(x) ( (SHR32((x), 2)) ^ \
101 (ROTL32((x), 12)) ^ \
104 #define S32_3(x) ( (SHR32((x), 2)) ^ \
106 (ROTL32((x), 15)) ^ \
109 #define S32_4(x) ( (SHR32((x), 1)) ^ (x))
111 #define S32_5(x) ( (SHR32((x), 2)) ^ (x))
113 uint32_t s32_0(uint32_t x){
117 uint32_t s32_1(uint32_t x){
121 uint32_t s32_2(uint32_t x){
125 uint32_t s32_3(uint32_t x){
129 uint32_t s32_4(uint32_t x){
133 uint32_t s32_5(uint32_t x){
138 uint32_t bmw_s32_0(uint32_t);
139 uint32_t bmw_s32_1(uint32_t);
140 uint32_t bmw_s32_2(uint32_t);
141 uint32_t bmw_s32_3(uint32_t);
142 uint32_t bmw_s32_4(uint32_t);
143 uint32_t bmw_s32_5(uint32_t);
144 void bmw_small_f0(uint32_t* q, uint32_t* h, const uint32_t* m);
146 #define R32_1(x) (ROTL32((x), 3))
147 #define R32_2(x) (ROTL32((x), 7))
148 #define R32_3(x) (ROTL32((x), 13))
149 #define R32_4(x) (ROTL32((x), 16))
150 #define R32_5(x) (ROTR32((x), 13))
151 #define R32_6(x) (ROTR32((x), 9))
152 #define R32_7(x) (ROTR32((x), 5))
154 #define K 0x05555555L
156 uint32_t k_lut[] PROGMEM = {
157 16L*K, 17L*K, 18L*K, 19L*K, 20L*K, 21L*K, 22L*K, 23L*K,
158 24L*K, 25L*K, 26L*K, 27L*K, 28L*K, 29L*K, 30L*K, 31L*K
161 /* same as above but precomputed to avoid compiler warnings */
165 0x55555550L, 0x5aaaaaa5L, 0x5ffffffaL,
166 0x6555554fL, 0x6aaaaaa4L, 0x6ffffff9L,
167 0x7555554eL, 0x7aaaaaa3L, 0x7ffffff8L,
168 0x8555554dL, 0x8aaaaaa2L, 0x8ffffff7L,
169 0x9555554cL, 0x9aaaaaa1L, 0x9ffffff6L,
173 uint32_t bmw_small_expand1(uint8_t j, const uint32_t* q, const void* m, const void* h){
175 /* r = 0x05555555*(j+16); */
177 r = ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
178 + ROTL32(((uint32_t*)m)[(j+3)&0xf], ((j+3)&0xf)+1 )
179 - ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
181 ) ^ ((uint32_t*)h)[(j+7)&0xf];
182 r += bmw_s32_1(q[j+ 0]) + bmw_s32_2(q[j+ 1]) + bmw_s32_3(q[j+ 2]) + bmw_s32_0(q[j+ 3])
183 + bmw_s32_1(q[j+ 4]) + bmw_s32_2(q[j+ 5]) + bmw_s32_3(q[j+ 6]) + bmw_s32_0(q[j+ 7])
184 + bmw_s32_1(q[j+ 8]) + bmw_s32_2(q[j+ 9]) + bmw_s32_3(q[j+10]) + bmw_s32_0(q[j+11])
185 + bmw_s32_1(q[j+12]) + bmw_s32_2(q[j+13]) + bmw_s32_3(q[j+14]) + bmw_s32_0(q[j+15]);
190 uint32_t bmw_small_expand2(uint8_t j, const uint32_t* q, const void* m, const void* h){
192 r = ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
193 + ROTL32(((uint32_t*)m)[(j+3)&0xf], ((j+3)&0xf)+1 )
194 - ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
196 ) ^ ((uint32_t*)h)[(j+7)&0xf];
197 r += (q[j+ 0]) + R32_1(q[j+ 1]) + (q[j+ 2]) + R32_2(q[j+ 3])
198 + (q[j+ 4]) + R32_3(q[j+ 5]) + (q[j+ 6]) + R32_4(q[j+ 7])
199 + (q[j+ 8]) + R32_5(q[j+ 9]) + (q[j+10]) + R32_6(q[j+11])
200 + (q[j+12]) + R32_7(q[j+13]) + S32_4(q[j+14]) + S32_5(q[j+15]);
205 /* to understand this implementation take a look at f0-opt-table.txt */
206 static uint16_t hack_table[5] = { 0x0311, 0xDDB3, 0x2A79, 0x07AA, 0x51C2 };
207 static uint8_t offset_table[5] = { 4+16, 6+16, 9+16, 12+16, 13+16 };
210 void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
213 uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
214 bmw_small_s3, bmw_small_s4 };
216 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
224 hack_reg=hack_table[c];
240 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
243 q[i] += h[(i+1)&0xf];
246 #endif /* F0_HACK==2*/
250 uint8_t f0_lut[] PROGMEM = {
251 5<<1, ( 7<<1)+1, (10<<1)+0, (13<<1)+0, (14<<1)+0,
252 6<<1, ( 8<<1)+1, (11<<1)+0, (14<<1)+0, (15<<1)+1,
253 0<<1, ( 7<<1)+0, ( 9<<1)+0, (12<<1)+1, (15<<1)+0,
254 0<<1, ( 1<<1)+1, ( 8<<1)+0, (10<<1)+1, (13<<1)+0,
255 1<<1, ( 2<<1)+0, ( 9<<1)+0, (11<<1)+1, (14<<1)+1,
256 3<<1, ( 2<<1)+1, (10<<1)+0, (12<<1)+1, (15<<1)+0,
257 4<<1, ( 0<<1)+1, ( 3<<1)+1, (11<<1)+1, (13<<1)+0,
258 1<<1, ( 4<<1)+1, ( 5<<1)+1, (12<<1)+1, (14<<1)+1,
259 2<<1, ( 5<<1)+1, ( 6<<1)+1, (13<<1)+0, (15<<1)+1,
260 0<<1, ( 3<<1)+1, ( 6<<1)+0, ( 7<<1)+1, (14<<1)+0,
261 8<<1, ( 1<<1)+1, ( 4<<1)+1, ( 7<<1)+1, (15<<1)+0,
262 8<<1, ( 0<<1)+1, ( 2<<1)+1, ( 5<<1)+1, ( 9<<1)+0,
263 1<<1, ( 3<<1)+0, ( 6<<1)+1, ( 9<<1)+1, (10<<1)+0,
264 2<<1, ( 4<<1)+0, ( 7<<1)+0, (10<<1)+0, (11<<1)+0,
265 3<<1, ( 5<<1)+1, ( 8<<1)+0, (11<<1)+1, (12<<1)+1,
266 12<<1, ( 4<<1)+1, ( 6<<1)+1, ( 9<<1)+1, (13<<1)+0
270 void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
271 uint8_t i,j=-1,v,sign,l=0;
272 uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
273 bmw_small_s3, bmw_small_s4 };
275 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
278 // memset(q, 0, 4*16);
279 for(i=0; i<5*16; ++i){
280 v = pgm_read_byte(f0_lut+i);
300 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
303 q[i] += h[(i+1)&0xf];
306 #endif /* F0_HACK==1 */
311 void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
314 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
317 q[ 0] = (h[ 5] - h[ 7] + h[10] + h[13] + h[14]);
318 q[ 1] = (h[ 6] - h[ 8] + h[11] + h[14] - h[15]);
319 q[ 2] = (h[ 0] + h[ 7] + h[ 9] - h[12] + h[15]);
320 q[ 3] = (h[ 0] - h[ 1] + h[ 8] - h[10] + h[13]);
321 q[ 4] = (h[ 1] + h[ 2] + h[ 9] - h[11] - h[14]);
322 q[ 5] = (h[ 3] - h[ 2] + h[10] - h[12] + h[15]);
323 q[ 6] = (h[ 4] - h[ 0] - h[ 3] - h[11] + h[13]);
324 q[ 7] = (h[ 1] - h[ 4] - h[ 5] - h[12] - h[14]);
325 q[ 8] = (h[ 2] - h[ 5] - h[ 6] + h[13] - h[15]);
326 q[ 9] = (h[ 0] - h[ 3] + h[ 6] - h[ 7] + h[14]);
327 q[10] = (h[ 8] - h[ 1] - h[ 4] - h[ 7] + h[15]);
328 q[11] = (h[ 8] - h[ 0] - h[ 2] - h[ 5] + h[ 9]);
329 q[12] = (h[ 1] + h[ 3] - h[ 6] - h[ 9] + h[10]);
330 q[13] = (h[ 2] + h[ 4] + h[ 7] + h[10] + h[11]);
331 q[14] = (h[ 3] - h[ 5] + h[ 8] - h[11] - h[12]);
332 q[15] = (h[12] - h[ 4] - h[ 6] - h[ 9] + h[13]);
334 q[ 0] = bmw_s32_0(q[ 0]);
335 q[ 1] = bmw_s32_1(q[ 1]);
336 q[ 2] = bmw_s32_2(q[ 2]);
337 q[ 3] = bmw_s32_3(q[ 3]);
338 q[ 4] = bmw_s32_4(q[ 4]);
339 q[ 5] = bmw_s32_0(q[ 5]);
340 q[ 6] = bmw_s32_1(q[ 6]);
341 q[ 7] = bmw_s32_2(q[ 7]);
342 q[ 8] = bmw_s32_3(q[ 8]);
343 q[ 9] = bmw_s32_4(q[ 9]);
344 q[10] = bmw_s32_0(q[10]);
345 q[11] = bmw_s32_1(q[11]);
346 q[12] = bmw_s32_2(q[12]);
347 q[13] = bmw_s32_3(q[13]);
348 q[14] = bmw_s32_4(q[14]);
349 q[15] = bmw_s32_0(q[15]);
351 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
354 q[i] += h[(i+1)&0xf];
358 #endif /* F0_HACK==0 */
361 void bmw_small_f1(uint32_t* q, const void* m, const void* h){
363 q[16] = bmw_small_expand1(0, q, m, h);
364 q[17] = bmw_small_expand1(1, q, m, h);
366 q[16+i] = bmw_small_expand2(i, q, m, h);
371 void bmw_small_f2(uint32_t* h, uint32_t* q, const void* m){
382 cli_putstr("\r\n XL = ");
383 cli_hexdump_rev(&xl, 4);
384 cli_putstr("\r\n XH = ");
385 cli_hexdump_rev(&xh, 4);
388 h[0] ^= SHL32(xh, 5) ^ SHR32(q[16], 5);
389 h[1] ^= SHR32(xh, 7) ^ SHL32(q[17], 8);
390 h[2] ^= SHR32(xh, 5) ^ SHL32(q[18], 5);
391 h[3] ^= SHR32(xh, 1) ^ SHL32(q[19], 5);
392 h[4] ^= SHR32(xh, 3) ^ q[20];
393 h[5] ^= SHL32(xh, 6) ^ SHR32(q[21], 6);
394 h[6] ^= SHR32(xh, 4) ^ SHL32(q[22], 6);
395 h[7] ^= SHR32(xh,11) ^ SHL32(q[23], 2);
397 h[i] += xl ^ q[24+i] ^ q[i];
400 h[8+i] ^= xh ^ q[24+i];
401 h[8+i] += ROTL32(h[(4+i)%8],i+9);
404 h[ 8] += SHL32(xl, 8) ^ q[23] ^ q[ 8];
405 h[ 9] += SHR32(xl, 6) ^ q[16] ^ q[ 9];
406 h[10] += SHL32(xl, 6) ^ q[17] ^ q[10];
407 h[11] += SHL32(xl, 4) ^ q[18] ^ q[11];
408 h[12] += SHR32(xl, 3) ^ q[19] ^ q[12];
409 h[13] += SHR32(xl, 4) ^ q[20] ^ q[13];
410 h[14] += SHR32(xl, 7) ^ q[21] ^ q[14];
411 h[15] += SHR32(xl, 2) ^ q[22] ^ q[15];
413 memxor(q+9, q+16, 7*4);
415 h[ 8] += SHL32(xl, 8) ^ q[ 8];
416 h[ 9] += SHR32(xl, 6) ^ q[ 9];
417 h[10] += SHL32(xl, 6) ^ q[10];
418 h[11] += SHL32(xl, 4) ^ q[11];
419 h[12] += SHR32(xl, 3) ^ q[12];
420 h[13] += SHR32(xl, 4) ^ q[13];
421 h[14] += SHR32(xl, 7) ^ q[14];
422 h[15] += SHR32(xl, 2) ^ q[15];
426 void bmw_small_nextBlock(bmw_small_ctx_t* ctx, const void* block){
428 dump_x(block, 16, 'M');
429 bmw_small_f0(q, ctx->h, block);
431 bmw_small_f1(q, block, ctx->h);
433 bmw_small_f2(ctx->h, q, block);
438 void bmw_small_lastBlock(bmw_small_ctx_t* ctx, const void* block, uint16_t length_b){
440 while(length_b >= BMW_SMALL_BLOCKSIZE){
441 bmw_small_nextBlock(ctx, block);
442 length_b -= BMW_SMALL_BLOCKSIZE;
443 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
445 memset(buffer, 0, 64);
446 memcpy(buffer, block, (length_b+7)/8);
447 buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
448 if(length_b+1>64*8-64){
449 bmw_small_nextBlock(ctx, buffer);
450 memset(buffer, 0, 64-8);
453 *((uint64_t*)&(buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
454 bmw_small_nextBlock(ctx, buffer);
458 memset(buffer, 0xaa, 64);
460 buffer[i*4] = i+0xa0;
462 // dump_x(buffer, 16, 'A');
463 dump_x(ctx->h, 16, 'M');
464 bmw_small_f0(q, (uint32_t*)buffer, ctx->h);
465 dump_x(buffer, 16, 'a');
467 bmw_small_f1(q, ctx->h, (uint32_t*)buffer);
469 bmw_small_f2((uint32_t*)buffer, q, ctx->h);
470 memcpy(ctx->h, buffer, 64);
474 void bmw224_init(bmw224_ctx_t* ctx){
476 ctx->h[0] = 0x00010203;
478 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
481 ctx->h[13] = 0x24353637;
487 void bmw256_init(bmw256_ctx_t* ctx){
489 ctx->h[0] = 0x40414243;
491 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
497 void bmw224_nextBlock(bmw224_ctx_t* ctx, const void* block){
498 bmw_small_nextBlock(ctx, block);
501 void bmw256_nextBlock(bmw256_ctx_t* ctx, const void* block){
502 bmw_small_nextBlock(ctx, block);
505 void bmw224_lastBlock(bmw224_ctx_t* ctx, const void* block, uint16_t length_b){
506 bmw_small_lastBlock(ctx, block, length_b);
509 void bmw256_lastBlock(bmw256_ctx_t* ctx, const void* block, uint16_t length_b){
510 bmw_small_lastBlock(ctx, block, length_b);
513 void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
514 memcpy(dest, &(ctx->h[9]), 224/8);
517 void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
518 memcpy(dest, &(ctx->h[8]), 256/8);
521 void bmw224(void* dest, const void* msg, uint32_t length_b){
524 while(length_b>=BMW_SMALL_BLOCKSIZE){
525 bmw_small_nextBlock(&ctx, msg);
526 length_b -= BMW_SMALL_BLOCKSIZE;
527 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
529 bmw_small_lastBlock(&ctx, msg, length_b);
530 bmw224_ctx2hash(dest, &ctx);
533 void bmw256(void* dest, const void* msg, uint32_t length_b){
536 while(length_b>=BMW_SMALL_BLOCKSIZE){
537 bmw_small_nextBlock(&ctx, msg);
538 length_b -= BMW_SMALL_BLOCKSIZE;
539 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
541 bmw_small_lastBlock(&ctx, msg, length_b);
542 bmw256_ctx2hash(dest, &ctx);