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 #define R32_1(x) (ROTL32((x), 3))
114 #define R32_2(x) (ROTL32((x), 7))
115 #define R32_3(x) (ROTL32((x), 13))
116 #define R32_4(x) (ROTL32((x), 16))
117 #define R32_5(x) (ROTR32((x), 13))
118 #define R32_6(x) (ROTR32((x), 9))
119 #define R32_7(x) (ROTR32((x), 5))
121 #define K 0x05555555L
123 uint32_t k_lut[] PROGMEM = {
124 16L*K, 17L*K, 18L*K, 19L*K, 20L*K, 21L*K, 22L*K, 23L*K,
125 24L*K, 25L*K, 26L*K, 27L*K, 28L*K, 29L*K, 30L*K, 31L*K
128 /* same as above but precomputed to avoid compiler warnings */
132 0x55555550L, 0x5aaaaaa5L, 0x5ffffffaL,
133 0x6555554fL, 0x6aaaaaa4L, 0x6ffffff9L,
134 0x7555554eL, 0x7aaaaaa3L, 0x7ffffff8L,
135 0x8555554dL, 0x8aaaaaa2L, 0x8ffffff7L,
136 0x9555554cL, 0x9aaaaaa1L, 0x9ffffff6L,
140 uint32_t bmw_small_expand1(uint8_t j, const uint32_t* q, const void* m, const void* h){
142 /* r = 0x05555555*(j+16); */
144 r = ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
145 + ROTL32(((uint32_t*)m)[(j+3)&0xf], ((j+3)&0xf)+1 )
146 - ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
148 ) ^ ((uint32_t*)h)[(j+7)&0xf];
149 r += S32_1(q[j+ 0]) + S32_2(q[j+ 1]) + S32_3(q[j+ 2]) + S32_0(q[j+ 3])
150 + S32_1(q[j+ 4]) + S32_2(q[j+ 5]) + S32_3(q[j+ 6]) + S32_0(q[j+ 7])
151 + S32_1(q[j+ 8]) + S32_2(q[j+ 9]) + S32_3(q[j+10]) + S32_0(q[j+11])
152 + S32_1(q[j+12]) + S32_2(q[j+13]) + S32_3(q[j+14]) + S32_0(q[j+15]);
157 uint32_t bmw_small_expand2(uint8_t j, const uint32_t* q, const void* m, const void* h){
159 r = ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
160 + ROTL32(((uint32_t*)m)[(j+3)&0xf], ((j+3)&0xf)+1 )
161 - ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
163 ) ^ ((uint32_t*)h)[(j+7)&0xf];
164 r += (q[j+ 0]) + R32_1(q[j+ 1]) + (q[j+ 2]) + R32_2(q[j+ 3])
165 + (q[j+ 4]) + R32_3(q[j+ 5]) + (q[j+ 6]) + R32_4(q[j+ 7])
166 + (q[j+ 8]) + R32_5(q[j+ 9]) + (q[j+10]) + R32_6(q[j+11])
167 + (q[j+12]) + R32_7(q[j+13]) + S32_4(q[j+14]) + S32_5(q[j+15]);
172 /* to understand this implementation take a look at f0-opt-table.txt */
173 static uint16_t hack_table[5] = { 0x0311, 0xDDB3, 0x2A79, 0x07AA, 0x51C2 };
174 static uint8_t offset_table[5] = { 4+16, 6+16, 9+16, 12+16, 13+16 };
177 void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
180 uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
181 bmw_small_s3, bmw_small_s4 };
183 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
191 hack_reg=hack_table[c];
207 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
210 q[i] += h[(i+1)&0xf];
213 #endif /* F0_HACK==2*/
217 uint8_t f0_lut[] PROGMEM = {
218 5<<1, ( 7<<1)+1, (10<<1)+0, (13<<1)+0, (14<<1)+0,
219 6<<1, ( 8<<1)+1, (11<<1)+0, (14<<1)+0, (15<<1)+1,
220 0<<1, ( 7<<1)+0, ( 9<<1)+0, (12<<1)+1, (15<<1)+0,
221 0<<1, ( 1<<1)+1, ( 8<<1)+0, (10<<1)+1, (13<<1)+0,
222 1<<1, ( 2<<1)+0, ( 9<<1)+0, (11<<1)+1, (14<<1)+1,
223 3<<1, ( 2<<1)+1, (10<<1)+0, (12<<1)+1, (15<<1)+0,
224 4<<1, ( 0<<1)+1, ( 3<<1)+1, (11<<1)+1, (13<<1)+0,
225 1<<1, ( 4<<1)+1, ( 5<<1)+1, (12<<1)+1, (14<<1)+1,
226 2<<1, ( 5<<1)+1, ( 6<<1)+1, (13<<1)+0, (15<<1)+1,
227 0<<1, ( 3<<1)+1, ( 6<<1)+0, ( 7<<1)+1, (14<<1)+0,
228 8<<1, ( 1<<1)+1, ( 4<<1)+1, ( 7<<1)+1, (15<<1)+0,
229 8<<1, ( 0<<1)+1, ( 2<<1)+1, ( 5<<1)+1, ( 9<<1)+0,
230 1<<1, ( 3<<1)+0, ( 6<<1)+1, ( 9<<1)+1, (10<<1)+0,
231 2<<1, ( 4<<1)+0, ( 7<<1)+0, (10<<1)+0, (11<<1)+0,
232 3<<1, ( 5<<1)+1, ( 8<<1)+0, (11<<1)+1, (12<<1)+1,
233 12<<1, ( 4<<1)+1, ( 6<<1)+1, ( 9<<1)+1, (13<<1)+0
237 void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
238 uint8_t i,j=-1,v,sign,l=0;
239 uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
240 bmw_small_s3, bmw_small_s4 };
242 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
245 // memset(q, 0, 4*16);
246 for(i=0; i<5*16; ++i){
247 v = pgm_read_byte(f0_lut+i);
267 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
270 q[i] += h[(i+1)&0xf];
273 #endif /* F0_HACK==1 */
277 void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
280 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
283 q[ 0] = (h[ 5] - h[ 7] + h[10] + h[13] + h[14]);
284 q[ 1] = (h[ 6] - h[ 8] + h[11] + h[14] - h[15]);
285 q[ 2] = (h[ 0] + h[ 7] + h[ 9] - h[12] + h[15]);
286 q[ 3] = (h[ 0] - h[ 1] + h[ 8] - h[10] + h[13]);
287 q[ 4] = (h[ 1] + h[ 2] + h[ 9] - h[11] - h[14]);
288 q[ 5] = (h[ 3] - h[ 2] + h[10] - h[12] + h[15]);
289 q[ 6] = (h[ 4] - h[ 0] - h[ 3] - h[11] + h[13]);
290 q[ 7] = (h[ 1] - h[ 4] - h[ 5] - h[12] - h[14]);
291 q[ 8] = (h[ 2] - h[ 5] - h[ 6] + h[13] - h[15]);
292 q[ 9] = (h[ 0] - h[ 3] + h[ 6] - h[ 7] + h[14]);
293 q[10] = (h[ 8] - h[ 1] - h[ 4] - h[ 7] + h[15]);
294 q[11] = (h[ 8] - h[ 0] - h[ 2] - h[ 5] + h[ 9]);
295 q[12] = (h[ 1] + h[ 3] - h[ 6] - h[ 9] + h[10]);
296 q[13] = (h[ 2] + h[ 4] + h[ 7] + h[10] + h[11]);
297 q[14] = (h[ 3] - h[ 5] + h[ 8] - h[11] - h[12]);
298 q[15] = (h[12] - h[ 4] - h[ 6] - h[ 9] + h[13]);
300 q[ 0] = S32_0(q[ 0]);
301 q[ 1] = S32_1(q[ 1]);
302 q[ 2] = S32_2(q[ 2]);
303 q[ 3] = S32_3(q[ 3]);
304 q[ 4] = S32_4(q[ 4]);
305 q[ 5] = S32_0(q[ 5]);
306 q[ 6] = S32_1(q[ 6]);
307 q[ 7] = S32_2(q[ 7]);
308 q[ 8] = S32_3(q[ 8]);
309 q[ 9] = S32_4(q[ 9]);
310 q[10] = S32_0(q[10]);
311 q[11] = S32_1(q[11]);
312 q[12] = S32_2(q[12]);
313 q[13] = S32_3(q[13]);
314 q[14] = S32_4(q[14]);
315 q[15] = S32_0(q[15]);
317 ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
320 q[i] += h[(i+1)&0xf];
323 #endif /* F0_HACK==0 */
326 void bmw_small_f1(uint32_t* q, const void* m, const void* h){
328 q[16] = bmw_small_expand1(0, q, m, h);
329 q[17] = bmw_small_expand1(1, q, m, h);
331 q[16+i] = bmw_small_expand2(i, q, m, h);
336 void bmw_small_f2(uint32_t* h, uint32_t* q, const void* m){
347 cli_putstr("\r\n XL = ");
348 cli_hexdump_rev(&xl, 4);
349 cli_putstr("\r\n XH = ");
350 cli_hexdump_rev(&xh, 4);
353 h[0] ^= SHL32(xh, 5) ^ SHR32(q[16], 5);
354 h[1] ^= SHR32(xh, 7) ^ SHL32(q[17], 8);
355 h[2] ^= SHR32(xh, 5) ^ SHL32(q[18], 5);
356 h[3] ^= SHR32(xh, 1) ^ SHL32(q[19], 5);
357 h[4] ^= SHR32(xh, 3) ^ q[20];
358 h[5] ^= SHL32(xh, 6) ^ SHR32(q[21], 6);
359 h[6] ^= SHR32(xh, 4) ^ SHL32(q[22], 6);
360 h[7] ^= SHR32(xh,11) ^ SHL32(q[23], 2);
362 h[i] += xl ^ q[24+i] ^ q[i];
365 h[8+i] ^= xh ^ q[24+i];
366 h[8+i] += ROTL32(h[(4+i)%8],i+9);
369 h[ 8] += SHL32(xl, 8) ^ q[23] ^ q[ 8];
370 h[ 9] += SHR32(xl, 6) ^ q[16] ^ q[ 9];
371 h[10] += SHL32(xl, 6) ^ q[17] ^ q[10];
372 h[11] += SHL32(xl, 4) ^ q[18] ^ q[11];
373 h[12] += SHR32(xl, 3) ^ q[19] ^ q[12];
374 h[13] += SHR32(xl, 4) ^ q[20] ^ q[13];
375 h[14] += SHR32(xl, 7) ^ q[21] ^ q[14];
376 h[15] += SHR32(xl, 2) ^ q[22] ^ q[15];
378 memxor(q+9, q+16, 7*4);
380 h[ 8] += SHL32(xl, 8) ^ q[ 8];
381 h[ 9] += SHR32(xl, 6) ^ q[ 9];
382 h[10] += SHL32(xl, 6) ^ q[10];
383 h[11] += SHL32(xl, 4) ^ q[11];
384 h[12] += SHR32(xl, 3) ^ q[12];
385 h[13] += SHR32(xl, 4) ^ q[13];
386 h[14] += SHR32(xl, 7) ^ q[14];
387 h[15] += SHR32(xl, 2) ^ q[15];
391 void bmw_small_nextBlock(bmw_small_ctx_t* ctx, const void* block){
393 dump_x(block, 16, 'M');
394 bmw_small_f0(q, ctx->h, block);
396 bmw_small_f1(q, block, ctx->h);
398 bmw_small_f2(ctx->h, q, block);
403 void bmw_small_lastBlock(bmw_small_ctx_t* ctx, const void* block, uint16_t length_b){
409 while(length_b >= BMW_SMALL_BLOCKSIZE){
410 bmw_small_nextBlock(ctx, block);
411 length_b -= BMW_SMALL_BLOCKSIZE;
412 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
414 memset(buffer.v8, 0, 64);
415 memcpy(buffer.v8, block, (length_b+7)/8);
416 buffer.v8[length_b>>3] |= 0x80 >> (length_b&0x07);
417 if(length_b+1>64*8-64){
418 bmw_small_nextBlock(ctx, buffer.v8);
419 memset(buffer.v8, 0, 64-8);
422 buffer.v64[7] = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
423 bmw_small_nextBlock(ctx, buffer.v8);
427 memset(buffer.v8, 0xaa, 64);
429 buffer.v8[i*4] = i+0xa0;
431 // dump_x(buffer.v8, 16, 'A');
432 dump_x(ctx->h, 16, 'M');
433 bmw_small_f0(q, buffer.v32, ctx->h);
434 dump_x(buffer.v8, 16, 'a');
436 bmw_small_f1(q, ctx->h, buffer.v32);
438 bmw_small_f2(buffer.v32, q, ctx->h);
439 memcpy(ctx->h, buffer.v8, 64);
443 void bmw224_init(bmw224_ctx_t* ctx){
445 ctx->h[0] = 0x00010203;
447 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
450 ctx->h[13] = 0x24353637;
456 void bmw256_init(bmw256_ctx_t* ctx){
458 ctx->h[0] = 0x40414243;
460 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
466 void bmw224_nextBlock(bmw224_ctx_t* ctx, const void* block){
467 bmw_small_nextBlock(ctx, block);
470 void bmw256_nextBlock(bmw256_ctx_t* ctx, const void* block){
471 bmw_small_nextBlock(ctx, block);
474 void bmw224_lastBlock(bmw224_ctx_t* ctx, const void* block, uint16_t length_b){
475 bmw_small_lastBlock(ctx, block, length_b);
478 void bmw256_lastBlock(bmw256_ctx_t* ctx, const void* block, uint16_t length_b){
479 bmw_small_lastBlock(ctx, block, length_b);
482 void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
483 memcpy(dest, &(ctx->h[9]), 224/8);
486 void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
487 memcpy(dest, &(ctx->h[8]), 256/8);
490 void bmw224(void* dest, const void* msg, uint32_t length_b){
493 while(length_b>=BMW_SMALL_BLOCKSIZE){
494 bmw_small_nextBlock(&ctx, msg);
495 length_b -= BMW_SMALL_BLOCKSIZE;
496 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
498 bmw_small_lastBlock(&ctx, msg, length_b);
499 bmw224_ctx2hash(dest, &ctx);
502 void bmw256(void* dest, const void* msg, uint32_t length_b){
505 while(length_b>=BMW_SMALL_BLOCKSIZE){
506 bmw_small_nextBlock(&ctx, msg);
507 length_b -= BMW_SMALL_BLOCKSIZE;
508 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
510 bmw_small_lastBlock(&ctx, msg, length_b);
511 bmw256_ctx2hash(dest, &ctx);