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_small.h"
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))))
46 void ctx_dump(const bmw_small_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]), 4);
55 cli_putstr_P(PSTR("\r\n counter = "));
56 cli_hexdump(&(ctx->counter), 4);
59 void dump_x(const uint32_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]), 4);
78 uint32_t bmw_small_s0(uint32_t x){
87 uint32_t bmw_small_s1(uint32_t x){
96 uint32_t bmw_small_s2(uint32_t x){
105 uint32_t bmw_small_s3(uint32_t x){
114 uint32_t bmw_small_s4(uint32_t x){
121 uint32_t bmw_small_s5(uint32_t x){
128 uint32_t bmw_small_r1(uint32_t x){
134 uint32_t bmw_small_r2(uint32_t x){
140 uint32_t bmw_small_r3(uint32_t x){
146 uint32_t bmw_small_r4(uint32_t x){
152 uint32_t bmw_small_r5(uint32_t x){
158 uint32_t bmw_small_r6(uint32_t x){
164 uint32_t bmw_small_r7(uint32_t x){
170 #define K 0x05555555L
172 uint32_t k_lut[] PROGMEM = {
173 16L*K, 17L*K, 18L*K, 19L*K, 20L*K, 21L*K, 22L*K, 23L*K,
174 24L*K, 25L*K, 26L*K, 27L*K, 28L*K, 29L*K, 30L*K, 31L*K
177 uint32_t bmw_small_expand1(uint8_t j, const uint32_t* q, const void* m){
178 uint32_t(*s[])(uint32_t) = {bmw_small_s1, bmw_small_s2, bmw_small_s3, bmw_small_s0};
181 /* r = 0x05555555*(j+16); */
182 r = pgm_read_dword(k_lut+j);
186 r += ((uint32_t*)m)[j];
187 r += ((uint32_t*)m)[j+3];
188 r -= ((uint32_t*)m)[j+10];
192 uint32_t bmw_small_expand2(uint8_t j, const uint32_t* q, const void* m){
193 uint32_t(*rf[])(uint32_t) = {bmw_small_r1, bmw_small_r2, bmw_small_r3,
194 bmw_small_r4, bmw_small_r5, bmw_small_r6,
198 /* r = 0x05555555*(j+16); */
199 r = pgm_read_dword(k_lut+j);
200 for(i=0; i<14; i+=2){
203 for(i=0; i<14; i+=2){
204 r += rf[i/2](q[j+i+1]);
206 r += bmw_small_s5(q[j+14]);
207 r += bmw_small_s4(q[j+15]);
208 r += ((uint32_t*)m)[j];
209 r += ((uint32_t*)m)[(j+3)%16];
210 r -= ((uint32_t*)m)[(j+10)%16];
215 uint8_t f0_lut[] PROGMEM = {
216 5<<1, ( 7<<1)+1, (10<<1)+0, (13<<1)+0, (14<<1)+0,
217 6<<1, ( 8<<1)+1, (11<<1)+0, (14<<1)+0, (15<<1)+1,
218 0<<1, ( 7<<1)+0, ( 9<<1)+0, (12<<1)+1, (15<<1)+0,
219 0<<1, ( 1<<1)+1, ( 8<<1)+0, (10<<1)+1, (13<<1)+0,
220 1<<1, ( 2<<1)+0, ( 9<<1)+0, (11<<1)+1, (14<<1)+1,
221 3<<1, ( 2<<1)+1, (10<<1)+0, (12<<1)+1, (15<<1)+0,
222 4<<1, ( 0<<1)+1, ( 3<<1)+1, (11<<1)+1, (13<<1)+0,
223 1<<1, ( 4<<1)+1, ( 5<<1)+1, (12<<1)+1, (14<<1)+1,
224 2<<1, ( 5<<1)+1, ( 6<<1)+1, (13<<1)+0, (15<<1)+1,
225 0<<1, ( 3<<1)+1, ( 6<<1)+0, ( 7<<1)+1, (14<<1)+0,
226 8<<1, ( 1<<1)+1, ( 4<<1)+1, ( 7<<1)+1, (15<<1)+0,
227 8<<1, ( 0<<1)+1, ( 2<<1)+1, ( 5<<1)+1, ( 9<<1)+0,
228 1<<1, ( 3<<1)+0, ( 6<<1)+1, ( 9<<1)+1, (10<<1)+0,
229 2<<1, ( 4<<1)+0, ( 7<<1)+0, (10<<1)+0, (11<<1)+0,
230 3<<1, ( 5<<1)+1, ( 8<<1)+0, (11<<1)+1, (12<<1)+1,
231 12<<1, ( 4<<1)+1, ( 6<<1)+1, ( 9<<1)+1, (13<<1)+0
234 void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
235 uint8_t i,j=0,v,sign,l=4;
236 uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
237 bmw_small_s3, bmw_small_s4 };
239 h[i] ^= ((uint32_t*)m)[i];
243 for(i=0; i<5*16; ++i){
244 v = pgm_read_byte(f0_lut+i);
264 void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
266 uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
267 bmw_small_s3, bmw_small_s4 };
269 h[i] ^= ((uint32_t*)m)[i];
272 q[ 0] = (h[ 5] - h[ 7] + h[10] + h[13] + h[14]);
273 q[ 1] = (h[ 6] - h[ 8] + h[11] + h[14] - h[15]);
274 q[ 2] = (h[ 0] + h[ 7] + h[ 9] - h[12] + h[15]);
275 q[ 3] = (h[ 0] - h[ 1] + h[ 8] - h[10] + h[13]);
276 q[ 4] = (h[ 1] + h[ 2] + h[ 9] - h[11] - h[14]);
277 q[ 5] = (h[ 3] - h[ 2] + h[10] - h[12] + h[15]);
278 q[ 6] = (h[ 4] - h[ 0] - h[ 3] - h[11] + h[13]);
279 q[ 7] = (h[ 1] - h[ 4] - h[ 5] - h[12] - h[14]);
280 q[ 8] = (h[ 2] - h[ 5] - h[ 6] + h[13] - h[15]);
281 q[ 9] = (h[ 0] - h[ 3] + h[ 6] - h[ 7] + h[14]);
282 q[10] = (h[ 8] - h[ 1] - h[ 4] - h[ 7] + h[15]);
283 q[11] = (h[ 8] - h[ 0] - h[ 2] - h[ 5] + h[ 9]);
284 q[12] = (h[ 1] + h[ 3] - h[ 6] - h[ 9] + h[10]);
285 q[13] = (h[ 2] + h[ 4] + h[ 7] + h[10] + h[11]);
286 q[14] = (h[ 3] - h[ 5] + h[ 8] - h[11] - h[12]);
287 q[15] = (h[12] - h[ 4] - h[ 6] - h[ 9] + h[13]);
295 void bmw_small_f1(uint32_t* q, const void* m){
297 q[16] = bmw_small_expand1(0, q, m);
298 q[17] = bmw_small_expand1(1, q, m);
300 q[16+i] = bmw_small_expand2(i, q, m);
304 void bmw_small_f2(uint32_t* h, const uint32_t* q, const void* m){
315 cli_putstr_P(PSTR("\r\n XL = "));
316 cli_hexdump_rev(&xl, 4);
317 cli_putstr_P(PSTR("\r\n XH = "));
318 cli_hexdump_rev(&xh, 4);
321 h[0] ^= SHL32(xh, 5) ^ SHR32(q[16], 5);
322 h[1] ^= SHR32(xh, 7) ^ SHL32(q[17], 8);
323 h[2] ^= SHR32(xh, 5) ^ SHL32(q[18], 5);
324 h[3] ^= SHR32(xh, 1) ^ SHL32(q[19], 5);
325 h[4] ^= SHR32(xh, 3) ^ q[20];
326 h[5] ^= SHL32(xh, 6) ^ SHR32(q[21], 6);
327 h[6] ^= SHR32(xh, 4) ^ SHL32(q[22], 6);
328 h[7] ^= SHR32(xh,11) ^ SHL32(q[23], 2);
330 h[i] += xl ^ q[24+i] ^ q[i];
333 h[8+i] ^= xh ^ q[24+i];
334 h[8+i] += ROTL32(h[(4+i)%8],i+9);
336 h[ 8] += SHL32(xl, 8) ^ q[23] ^ q[ 8];
337 h[ 9] += SHR32(xl, 6) ^ q[16] ^ q[ 9];
338 h[10] += SHL32(xl, 6) ^ q[17] ^ q[10];
339 h[11] += SHL32(xl, 4) ^ q[18] ^ q[11];
340 h[12] += SHR32(xl, 3) ^ q[19] ^ q[12];
341 h[13] += SHR32(xl, 4) ^ q[20] ^ q[13];
342 h[14] += SHR32(xl, 7) ^ q[21] ^ q[14];
343 h[15] += SHR32(xl, 2) ^ q[22] ^ q[15];
346 void bmw_small_nextBlock(bmw_small_ctx_t* ctx, const void* block){
348 dump_x(block, 16, 'M');
349 bmw_small_f0(q, ctx->h, block);
351 bmw_small_f1(q, block);
353 bmw_small_f2(ctx->h, q, block);
358 void bmw_small_lastBlock(bmw_small_ctx_t* ctx, const void* block, uint16_t length_b){
360 while(length_b >= BMW_SMALL_BLOCKSIZE){
361 bmw_small_nextBlock(ctx, block);
362 length_b -= BMW_SMALL_BLOCKSIZE;
363 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
365 memset(buffer, 0, 64);
366 memcpy(buffer, block, (length_b+7)/8);
367 buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
368 if(length_b+1>64*8-64){
369 bmw_small_nextBlock(ctx, buffer);
370 memset(buffer, 0, 64-8);
373 *((uint64_t*)&(buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
374 bmw_small_nextBlock(ctx, buffer);
377 void bmw224_init(bmw224_ctx_t* ctx){
379 ctx->h[0] = 0x00010203;
381 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
384 ctx->h[13] = 0x24353637;
390 void bmw256_init(bmw256_ctx_t* ctx){
392 ctx->h[0] = 0x40414243;
394 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
400 void bmw224_nextBlock(bmw224_ctx_t* ctx, const void* block){
401 bmw_small_nextBlock(ctx, block);
404 void bmw256_nextBlock(bmw256_ctx_t* ctx, const void* block){
405 bmw_small_nextBlock(ctx, block);
408 void bmw224_lastBlock(bmw224_ctx_t* ctx, const void* block, uint16_t length_b){
409 bmw_small_lastBlock(ctx, block, length_b);
412 void bmw256_lastBlock(bmw256_ctx_t* ctx, const void* block, uint16_t length_b){
413 bmw_small_lastBlock(ctx, block, length_b);
416 void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
417 memcpy(dest, &(ctx->h[9]), 224/8);
420 void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
421 memcpy(dest, &(ctx->h[8]), 256/8);
424 void bmw224(void* dest, const void* msg, uint32_t length_b){
427 while(length_b>=BMW_SMALL_BLOCKSIZE){
428 bmw_small_nextBlock(&ctx, msg);
429 length_b -= BMW_SMALL_BLOCKSIZE;
430 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
432 bmw_small_lastBlock(&ctx, msg, length_b);
433 bmw224_ctx2hash(dest, &ctx);
436 void bmw256(void* dest, const void* msg, uint32_t length_b){
439 while(length_b>=BMW_SMALL_BLOCKSIZE){
440 bmw_small_nextBlock(&ctx, msg);
441 length_b -= BMW_SMALL_BLOCKSIZE;
442 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
444 bmw_small_lastBlock(&ctx, msg, length_b);
445 bmw256_ctx2hash(dest, &ctx);