]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bmw_small.c
e1ddeffd9b7cb1f36cc1f679652ac77bfc0fd64c
[avr-crypto-lib.git] / bmw_small.c
1 /* bmw_small.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2009  Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19 /*
20  * \file    bmw_small.c
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-04-27
24  * \license GPLv3 or later
25  * 
26  */
27
28 #include <stdint.h>
29 #include <string.h>
30 #include <avr/pgmspace.h>
31 #include "bmw_small.h"
32
33
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))))
38
39 #define BUG24 1
40
41 #define DEBUG 0
42 #if DEBUG
43  #include "cli.h"
44  
45  void ctx_dump(const bmw_small_ctx_t* ctx){
46         uint8_t i;
47         cli_putstr_P(PSTR("\r\n==== ctx dump ===="));
48         for(i=0; i<16;++i){
49                 cli_putstr_P(PSTR("\r\n h["));
50                 cli_hexdump(&i, 1);
51                 cli_putstr_P(PSTR("] = "));
52                 cli_hexdump_rev(&(ctx->h[i]), 4);
53         }
54         cli_putstr_P(PSTR("\r\n counter = "));
55         cli_hexdump(&(ctx->counter), 4);
56  }
57  
58  void dump_x(const uint32_t* q, uint8_t elements, char x){
59         uint8_t i;
60         cli_putstr_P(PSTR("\r\n==== "));
61         cli_putc(x);
62         cli_putstr_P(PSTR(" dump ===="));
63         for(i=0; i<elements;++i){
64                 cli_putstr_P(PSTR("\r\n "));
65                 cli_putc(x);
66                 cli_putstr_P(PSTR("["));
67                 cli_hexdump(&i, 1);
68                 cli_putstr_P(PSTR("] = "));
69                 cli_hexdump_rev(&(q[i]), 4);
70         }
71  }
72 #else
73  #define ctx_dump(x)
74  #define dump_x(a,b,c)
75 #endif
76
77 uint32_t bmw_small_s0(uint32_t x){
78         uint32_t r;
79         r =   SHR32(x, 1)
80                 ^ SHL32(x, 3)
81                 ^ ROTL32(x, 4)
82                 ^ ROTR32(x, 13);
83         return r;       
84 }
85
86 uint32_t bmw_small_s1(uint32_t x){
87         uint32_t r;
88         r =   SHR32(x, 1)
89                 ^ SHL32(x, 2)
90                 ^ ROTL32(x, 8)
91                 ^ ROTR32(x, 9);
92         return r;       
93 }
94
95 uint32_t bmw_small_s2(uint32_t x){
96         uint32_t r;
97         r =   SHR32(x, 2)
98                 ^ SHL32(x, 1)
99                 ^ ROTL32(x, 12)
100                 ^ ROTR32(x, 7);
101         return r;       
102 }
103
104 uint32_t bmw_small_s3(uint32_t x){
105         uint32_t r;
106         r =   SHR32(x, 2)
107                 ^ SHL32(x, 2)
108                 ^ ROTL32(x, 15)
109                 ^ ROTR32(x, 3);
110         return r;       
111 }
112
113 uint32_t bmw_small_s4(uint32_t x){
114         uint32_t r;
115         r =   SHR32(x, 1)
116                 ^ x;
117         return r;       
118 }
119
120 uint32_t bmw_small_s5(uint32_t x){
121         uint32_t r;
122         r =   SHR32(x, 2)
123                 ^ x;
124         return r;       
125 }
126
127 uint32_t bmw_small_r1(uint32_t x){
128         uint32_t r;
129         r =   ROTL32(x, 3);
130         return r;       
131 }
132
133 uint32_t bmw_small_r2(uint32_t x){
134         uint32_t r;
135         r =   ROTL32(x, 7);
136         return r;       
137 }
138
139 uint32_t bmw_small_r3(uint32_t x){
140         uint32_t r;
141         r =   ROTL32(x, 13);
142         return r;       
143 }
144
145 uint32_t bmw_small_r4(uint32_t x){
146         uint32_t r;
147         r =   ROTL32(x, 16);
148         return r;       
149 }
150
151 uint32_t bmw_small_r5(uint32_t x){
152         uint32_t r;
153         r =   ROTR32(x, 13);
154         return r;       
155 }
156
157 uint32_t bmw_small_r6(uint32_t x){
158         uint32_t r;
159         r =   ROTR32(x, 9);
160         return r;       
161 }
162
163 uint32_t bmw_small_r7(uint32_t x){
164         uint32_t r;
165         r =   ROTR32(x, 5);
166         return r;       
167 }
168
169 #define K 0x05555555L
170 uint32_t k_lut[] PROGMEM = {
171         16L*K, 17L*K, 18L*K, 19L*K, 20L*K, 21L*K, 22L*K, 23L*K,
172         24L*K, 25L*K, 26L*K, 27L*K, 28L*K, 29L*K, 30L*K, 31L*K
173 };
174
175 uint32_t bmw_small_expand1(uint8_t j, const uint32_t* q, const void* m){
176         uint32_t(*s[])(uint32_t) = {bmw_small_s1, bmw_small_s2, bmw_small_s3, bmw_small_s0};
177         uint32_t r;
178         uint8_t i;
179         /* r = 0x05555555*(j+16); */
180         r = pgm_read_dword(k_lut+j);
181         for(i=0; i<16; ++i){
182                 r += s[i%4](q[j+i]);
183         }
184         r += ((uint32_t*)m)[j];
185         r += ((uint32_t*)m)[j+3];
186         r -= ((uint32_t*)m)[j+10];
187         return r;
188 }
189
190 uint32_t bmw_small_expand2(uint8_t j, const uint32_t* q, const void* m){
191         uint32_t(*rf[])(uint32_t) = {bmw_small_r1, bmw_small_r2, bmw_small_r3,
192                                      bmw_small_r4, bmw_small_r5, bmw_small_r6,
193                                                              bmw_small_r7};
194         uint32_t r;
195         uint8_t i;
196         /* r = 0x05555555*(j+16); */
197         r = pgm_read_dword(k_lut+j);
198         for(i=0; i<14; i+=2){
199                 r += q[j+i];
200         }
201         for(i=0; i<14; i+=2){
202                 r += rf[i/2](q[j+i+1]);
203         }
204         r += bmw_small_s5(q[j+14]);
205         r += bmw_small_s4(q[j+15]);
206         r += ((uint32_t*)m)[j];
207         r += ((uint32_t*)m)[(j+3)%16];
208         r -= ((uint32_t*)m)[(j+10)%16];
209         return r;
210 }
211
212 void bmw_small_f0(uint32_t* q, const uint32_t* h, const void* m){
213         uint32_t t[16];
214         uint8_t i;
215         uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
216                                    bmw_small_s3, bmw_small_s4 };
217         for(i=0; i<16; ++i){
218                 t[i] = h[i] ^ ((uint32_t*)m)[i];
219         }
220         dump_x(t, 16, 'T');
221         q[ 0] = (t[ 5] - t[ 7] + t[10] + t[13] + t[14]);
222         q[ 1] = (t[ 6] - t[ 8] + t[11] + t[14] - t[15]);
223         q[ 2] = (t[ 0] + t[ 7] + t[ 9] - t[12] + t[15]);
224         q[ 3] = (t[ 0] - t[ 1] + t[ 8] - t[10] + t[13]);
225         q[ 4] = (t[ 1] + t[ 2] + t[ 9] - t[11] - t[14]);
226         q[ 5] = (t[ 3] - t[ 2] + t[10] - t[12] + t[15]);
227         q[ 6] = (t[ 4] - t[ 0] - t[ 3] - t[11] + t[13]); 
228         q[ 7] = (t[ 1] - t[ 4] - t[ 5] - t[12] - t[14]);
229         q[ 8] = (t[ 2] - t[ 5] - t[ 6] + t[13] - t[15]);
230         q[ 9] = (t[ 0] - t[ 3] + t[ 6] - t[ 7] + t[14]);
231         q[10] = (t[ 8] - t[ 1] - t[ 4] - t[ 7] + t[15]);
232         q[11] = (t[ 8] - t[ 0] - t[ 2] - t[ 5] + t[ 9]);
233         q[12] = (t[ 1] + t[ 3] - t[ 6] - t[ 9] + t[10]);
234         q[13] = (t[ 2] + t[ 4] + t[ 7] + t[10] + t[11]);
235         q[14] = (t[ 3] - t[ 5] + t[ 8] - t[11] - t[12]);
236         q[15] = (t[12] - t[ 4] - t[ 6] - t[ 9] + t[13]); 
237         dump_x(q, 16, 'W');
238         for(i=0; i<16; ++i){
239                 q[i] = s[i%5](q[i]);
240         }       
241 }
242
243 void bmw_small_f1(uint32_t* q, const void* m){
244         uint8_t i;
245         q[16] = bmw_small_expand1(0, q, m);
246         q[17] = bmw_small_expand1(1, q, m);
247         for(i=2; i<16; ++i){
248                 q[16+i] = bmw_small_expand2(i, q, m);
249         }
250 }
251
252 void bmw_small_f2(uint32_t* h, const uint32_t* q, const void* m){
253         uint32_t xl=0, xh;
254         uint8_t i;
255         for(i=16;i<24;++i){
256                 xl ^= q[i];
257         }
258         xh = xl;
259         for(i=24;i<32;++i){
260                 xh ^= q[i];
261         }
262 #if DEBUG       
263         cli_putstr_P(PSTR("\r\n XL = "));
264         cli_hexdump_rev(&xl, 4);
265         cli_putstr_P(PSTR("\r\n XH = "));
266         cli_hexdump_rev(&xh, 4);
267 #endif
268         memcpy(h, m, 16*4);
269         h[0] ^= SHL32(xh, 5) ^ SHR32(q[16], 5);
270         h[1] ^= SHR32(xh, 7) ^ SHL32(q[17], 8);
271         h[2] ^= SHR32(xh, 5) ^ SHL32(q[18], 5);
272         h[3] ^= SHR32(xh, 1) ^ SHL32(q[19], 5);
273         h[4] ^= SHR32(xh, 3) ^ q[20];
274         h[5] ^= SHL32(xh, 6) ^ SHR32(q[21], 6);
275         h[6] ^= SHR32(xh, 4) ^ SHL32(q[22], 6);
276         h[7] ^= SHR32(xh,11) ^ SHL32(q[23], 2);
277         for(i=0; i<8; ++i){
278                 h[i] += xl ^ q[24+i] ^ q[i];
279         }
280         for(i=0; i<8; ++i){
281                 h[8+i] ^= xh ^ q[24+i];
282                 h[8+i] += ROTL32(h[(4+i)%8],i+9);
283         }
284         h[ 8] += SHL32(xl, 8) ^ q[23] ^ q[ 8];
285         h[ 9] += SHR32(xl, 6) ^ q[16] ^ q[ 9];
286         h[10] += SHL32(xl, 6) ^ q[17] ^ q[10];
287         h[11] += SHL32(xl, 4) ^ q[18] ^ q[11];
288         h[12] += SHR32(xl, 3) ^ q[19] ^ q[12];
289         h[13] += SHR32(xl, 4) ^ q[20] ^ q[13];
290         h[14] += SHR32(xl, 7) ^ q[21] ^ q[14];
291         h[15] += SHR32(xl, 2) ^ q[22] ^ q[15];
292 }
293
294 void bmw_small_nextBlock(bmw_small_ctx_t* ctx, const void* block){
295         uint32_t q[32];
296         dump_x(block, 16, 'M');
297         bmw_small_f0(q, ctx->h, block);
298         dump_x(q, 16, 'Q');
299         bmw_small_f1(q, block);
300         dump_x(q, 32, 'Q');
301         bmw_small_f2(ctx->h, q, block);
302         ctx->counter += 1;
303         ctx_dump(ctx);
304 }
305
306 void bmw_small_lastBlock(bmw_small_ctx_t* ctx, const void* block, uint16_t length_b){
307         uint8_t buffer[64];
308         while(length_b >= BMW_SMALL_BLOCKSIZE){
309                 bmw_small_nextBlock(ctx, block);
310                 length_b -= BMW_SMALL_BLOCKSIZE;
311                 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
312         }
313         memset(buffer, 0, 64);
314         memcpy(buffer, block, (length_b+7)/8);
315         buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
316         if(length_b+1>64*8-64){
317                 bmw_small_nextBlock(ctx, buffer);
318                 memset(buffer, 0, 64-8);
319                 ctx->counter -= 1;
320         }
321         *((uint64_t*)&(buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
322         bmw_small_nextBlock(ctx, buffer);
323 }
324
325 void bmw224_init(bmw224_ctx_t* ctx){
326         uint8_t i;
327         ctx->h[0] = 0x00010203;
328         for(i=1; i<16; ++i){
329                 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
330         }
331 #if BUG24       
332         ctx->h[13] = 0x24353637;
333 #endif
334         ctx->counter=0;
335         ctx_dump(ctx);
336 }
337
338 void bmw256_init(bmw256_ctx_t* ctx){
339         uint8_t i;
340         ctx->h[0] = 0x40414243;
341         for(i=1; i<16; ++i){
342                 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
343         }
344         ctx->counter=0;
345         ctx_dump(ctx);
346 }
347
348 void bmw224_nextBlock(bmw224_ctx_t* ctx, const void* block){
349         bmw_small_nextBlock(ctx, block);
350 }
351
352 void bmw256_nextBlock(bmw256_ctx_t* ctx, const void* block){
353         bmw_small_nextBlock(ctx, block);
354 }
355
356 void bmw224_lastBlock(bmw224_ctx_t* ctx, const void* block, uint16_t length_b){
357         bmw_small_lastBlock(ctx, block, length_b);
358 }
359
360 void bmw256_lastBlock(bmw256_ctx_t* ctx, const void* block, uint16_t length_b){
361         bmw_small_lastBlock(ctx, block, length_b);
362 }
363
364 void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
365         memcpy(dest, &(ctx->h[9]), 224/8);
366 }
367
368 void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
369         memcpy(dest, &(ctx->h[8]), 256/8);
370 }
371
372 void bmw224(void* dest, const void* msg, uint32_t length_b){
373         bmw_small_ctx_t ctx;
374         bmw224_init(&ctx);
375         while(length_b>=BMW_SMALL_BLOCKSIZE){
376                 bmw_small_nextBlock(&ctx, msg);
377                 length_b -= BMW_SMALL_BLOCKSIZE;
378                 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
379         }
380         bmw_small_lastBlock(&ctx, msg, length_b);
381         bmw224_ctx2hash(dest, &ctx);
382 }
383
384 void bmw256(void* dest, const void* msg, uint32_t length_b){
385         bmw_small_ctx_t ctx;
386         bmw256_init(&ctx);
387         while(length_b>=BMW_SMALL_BLOCKSIZE){
388                 bmw_small_nextBlock(&ctx, msg);
389                 length_b -= BMW_SMALL_BLOCKSIZE;
390                 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
391         }
392         bmw_small_lastBlock(&ctx, msg, length_b);
393         bmw256_ctx2hash(dest, &ctx);
394 }
395