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 "bmw_small.h"
32 #define SHL32(a,n) ((a)<<(n))
33 #define SHR32(a,n) ((a)>>(n))
34 #define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
35 #define ROTR32(a,n) (((a)>>(n))|((a)<<(32-(n))))
43 void ctx_dump(const bmw_small_ctx_t* ctx){
45 cli_putstr_P(PSTR("\r\n==== ctx dump ===="));
47 cli_putstr_P(PSTR("\r\n h["));
49 cli_putstr_P(PSTR("] = "));
50 cli_hexdump_rev(&(ctx->h[i]), 4);
52 cli_putstr_P(PSTR("\r\n counter = "));
53 cli_hexdump(&(ctx->counter), 4);
56 void dump_x(const uint32_t* q, uint8_t elements, char x){
58 cli_putstr_P(PSTR("\r\n==== "));
60 cli_putstr_P(PSTR(" dump ===="));
61 for(i=0; i<elements;++i){
62 cli_putstr_P(PSTR("\r\n "));
64 cli_putstr_P(PSTR("["));
66 cli_putstr_P(PSTR("] = "));
67 cli_hexdump_rev(&(q[i]), 4);
75 uint32_t bmw_small_s0(uint32_t x){
84 uint32_t bmw_small_s1(uint32_t x){
93 uint32_t bmw_small_s2(uint32_t x){
102 uint32_t bmw_small_s3(uint32_t x){
111 uint32_t bmw_small_s4(uint32_t x){
118 uint32_t bmw_small_s5(uint32_t x){
125 uint32_t bmw_small_r1(uint32_t x){
131 uint32_t bmw_small_r2(uint32_t x){
137 uint32_t bmw_small_r3(uint32_t x){
143 uint32_t bmw_small_r4(uint32_t x){
149 uint32_t bmw_small_r5(uint32_t x){
155 uint32_t bmw_small_r6(uint32_t x){
161 uint32_t bmw_small_r7(uint32_t x){
167 uint32_t bmw_small_expand1(uint8_t j, const uint32_t* q, const void* m){
168 uint32_t(*s[])(uint32_t) = {bmw_small_s1, bmw_small_s2, bmw_small_s3, bmw_small_s0};
171 r = 0x05555555*(j+16);
175 r += ((uint32_t*)m)[j];
176 r += ((uint32_t*)m)[j+3];
177 r -= ((uint32_t*)m)[j+10];
181 uint32_t bmw_small_expand2(uint8_t j, const uint32_t* q, const void* m){
182 uint32_t(*rf[])(uint32_t) = {bmw_small_r1, bmw_small_r2, bmw_small_r3,
183 bmw_small_r4, bmw_small_r5, bmw_small_r6,
187 r = 0x05555555*(j+16);
188 for(i=0; i<14; i+=2){
191 for(i=0; i<14; i+=2){
192 r += rf[i/2](q[j+i+1]);
194 r += bmw_small_s5(q[j+14]);
195 r += bmw_small_s4(q[j+15]);
196 r += ((uint32_t*)m)[j];
197 r += ((uint32_t*)m)[(j+3)%16];
198 r -= ((uint32_t*)m)[(j+10)%16];
202 void bmw_small_f0(uint32_t* q, const uint32_t* h, const void* m){
205 uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
206 bmw_small_s3, bmw_small_s4 };
208 t[i] = h[i] ^ ((uint32_t*)m)[i];
211 q[ 0] = (t[ 5] - t[ 7] + t[10] + t[13] + t[14]);
212 q[ 1] = (t[ 6] - t[ 8] + t[11] + t[14] - t[15]);
213 q[ 2] = (t[ 0] + t[ 7] + t[ 9] - t[12] + t[15]);
214 q[ 3] = (t[ 0] - t[ 1] + t[ 8] - t[10] + t[13]);
215 q[ 4] = (t[ 1] + t[ 2] + t[ 9] - t[11] - t[14]);
216 q[ 5] = (t[ 3] - t[ 2] + t[10] - t[12] + t[15]);
217 q[ 6] = (t[ 4] - t[ 0] - t[ 3] - t[11] + t[13]);
218 q[ 7] = (t[ 1] - t[ 4] - t[ 5] - t[12] - t[14]);
219 q[ 8] = (t[ 2] - t[ 5] - t[ 6] + t[13] - t[15]);
220 q[ 9] = (t[ 0] - t[ 3] + t[ 6] - t[ 7] + t[14]);
221 q[10] = (t[ 8] - t[ 1] - t[ 4] - t[ 7] + t[15]);
222 q[11] = (t[ 8] - t[ 0] - t[ 2] - t[ 5] + t[ 9]);
223 q[12] = (t[ 1] + t[ 3] - t[ 6] - t[ 9] + t[10]);
224 q[13] = (t[ 2] + t[ 4] + t[ 7] + t[10] + t[11]);
225 q[14] = (t[ 3] - t[ 5] + t[ 8] - t[11] - t[12]);
226 q[15] = (t[12] - t[ 4] - t[ 6] - t[ 9] + t[13]);
233 void bmw_small_f1(uint32_t* q, const void* m){
235 q[16] = bmw_small_expand1(0, q, m);
236 q[17] = bmw_small_expand1(1, q, m);
238 q[16+i] = bmw_small_expand2(i, q, m);
242 void bmw_small_f2(uint32_t* h, const uint32_t* q, const void* m){
253 cli_putstr_P(PSTR("\r\n XL = "));
254 cli_hexdump_rev(&xl, 4);
255 cli_putstr_P(PSTR("\r\n XH = "));
256 cli_hexdump_rev(&xh, 4);
259 h[0] ^= SHL32(xh, 5) ^ SHR32(q[16], 5);
260 h[1] ^= SHR32(xh, 7) ^ SHL32(q[17], 8);
261 h[2] ^= SHR32(xh, 5) ^ SHL32(q[18], 5);
262 h[3] ^= SHR32(xh, 1) ^ SHL32(q[19], 5);
263 h[4] ^= SHR32(xh, 3) ^ q[20];
264 h[5] ^= SHL32(xh, 6) ^ SHR32(q[21], 6);
265 h[6] ^= SHR32(xh, 4) ^ SHL32(q[22], 6);
266 h[7] ^= SHR32(xh,11) ^ SHL32(q[23], 2);
268 h[i] += xl ^ q[24+i] ^ q[i];
271 h[8+i] ^= xh ^ q[24+i];
272 h[8+i] += ROTL32(h[(4+i)%8],i+9);
274 h[ 8] += SHL32(xl, 8) ^ q[23] ^ q[ 8];
275 h[ 9] += SHR32(xl, 6) ^ q[16] ^ q[ 9];
276 h[10] += SHL32(xl, 6) ^ q[17] ^ q[10];
277 h[11] += SHL32(xl, 4) ^ q[18] ^ q[11];
278 h[12] += SHR32(xl, 3) ^ q[19] ^ q[12];
279 h[13] += SHR32(xl, 4) ^ q[20] ^ q[13];
280 h[14] += SHR32(xl, 7) ^ q[21] ^ q[14];
281 h[15] += SHR32(xl, 2) ^ q[22] ^ q[15];
284 void bmw_small_nextBlock(bmw_small_ctx_t* ctx, const void* block){
286 dump_x(block, 16, 'M');
287 bmw_small_f0(q, ctx->h, block);
289 bmw_small_f1(q, block);
291 bmw_small_f2(ctx->h, q, block);
296 void bmw_small_lastBlock(bmw_small_ctx_t* ctx, const void* block, uint16_t length_b){
298 while(length_b >= BMW_SMALL_BLOCKSIZE){
299 bmw_small_nextBlock(ctx, block);
300 length_b -= BMW_SMALL_BLOCKSIZE;
301 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
303 memset(buffer, 0, 64);
304 memcpy(buffer, block, (length_b+7)/8);
305 buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
306 if(length_b+1>64*8-64){
307 bmw_small_nextBlock(ctx, buffer);
308 memset(buffer, 0, 64-8);
311 *((uint64_t*)&(buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
312 bmw_small_nextBlock(ctx, buffer);
315 void bmw224_init(bmw224_ctx_t* ctx){
317 ctx->h[0] = 0x00010203;
319 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
322 ctx->h[13] = 0x24353637;
328 void bmw256_init(bmw256_ctx_t* ctx){
330 ctx->h[0] = 0x40414243;
332 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
338 void bmw224_nextBlock(bmw224_ctx_t* ctx, const void* block){
339 bmw_small_nextBlock(ctx, block);
342 void bmw256_nextBlock(bmw256_ctx_t* ctx, const void* block){
343 bmw_small_nextBlock(ctx, block);
346 void bmw224_lastBlock(bmw224_ctx_t* ctx, const void* block, uint16_t length_b){
347 bmw_small_lastBlock(ctx, block, length_b);
350 void bmw256_lastBlock(bmw256_ctx_t* ctx, const void* block, uint16_t length_b){
351 bmw_small_lastBlock(ctx, block, length_b);
354 void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
355 memcpy(dest, &(ctx->h[9]), 224/8);
358 void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
359 memcpy(dest, &(ctx->h[8]), 256/8);
362 void bmw224(void* dest, const void* msg, uint32_t length_b){
365 while(length_b>=BMW_SMALL_BLOCKSIZE){
366 bmw_small_nextBlock(&ctx, msg);
367 length_b -= BMW_SMALL_BLOCKSIZE;
368 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
370 bmw_small_lastBlock(&ctx, msg, length_b);
371 bmw224_ctx2hash(dest, &ctx);
374 void bmw256(void* dest, const void* msg, uint32_t length_b){
377 while(length_b>=BMW_SMALL_BLOCKSIZE){
378 bmw_small_nextBlock(&ctx, msg);
379 length_b -= BMW_SMALL_BLOCKSIZE;
380 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
382 bmw_small_lastBlock(&ctx, msg, length_b);
383 bmw256_ctx2hash(dest, &ctx);