]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bmw_small.c
+BlueMidnightWish
[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 "bmw_small.h"
31
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))))
36
37 #define BUG24 1
38
39 #define DEBUG 0
40 #if DEBUG
41  #include "cli.h"
42  
43  void ctx_dump(const bmw_small_ctx_t* ctx){
44         uint8_t i;
45         cli_putstr_P(PSTR("\r\n==== ctx dump ===="));
46         for(i=0; i<16;++i){
47                 cli_putstr_P(PSTR("\r\n h["));
48                 cli_hexdump(&i, 1);
49                 cli_putstr_P(PSTR("] = "));
50                 cli_hexdump_rev(&(ctx->h[i]), 4);
51         }
52         cli_putstr_P(PSTR("\r\n counter = "));
53         cli_hexdump(&(ctx->counter), 4);
54  }
55  
56  void dump_x(const uint32_t* q, uint8_t elements, char x){
57         uint8_t i;
58         cli_putstr_P(PSTR("\r\n==== "));
59         cli_putc(x);
60         cli_putstr_P(PSTR(" dump ===="));
61         for(i=0; i<elements;++i){
62                 cli_putstr_P(PSTR("\r\n "));
63                 cli_putc(x);
64                 cli_putstr_P(PSTR("["));
65                 cli_hexdump(&i, 1);
66                 cli_putstr_P(PSTR("] = "));
67                 cli_hexdump_rev(&(q[i]), 4);
68         }
69  }
70 #else
71  #define ctx_dump(x)
72  #define dump_x(a,b,c)
73 #endif
74
75 uint32_t bmw_small_s0(uint32_t x){
76         uint32_t r;
77         r =   SHR32(x, 1)
78                 ^ SHL32(x, 3)
79                 ^ ROTL32(x, 4)
80                 ^ ROTR32(x, 13);
81         return r;       
82 }
83
84 uint32_t bmw_small_s1(uint32_t x){
85         uint32_t r;
86         r =   SHR32(x, 1)
87                 ^ SHL32(x, 2)
88                 ^ ROTL32(x, 8)
89                 ^ ROTR32(x, 9);
90         return r;       
91 }
92
93 uint32_t bmw_small_s2(uint32_t x){
94         uint32_t r;
95         r =   SHR32(x, 2)
96                 ^ SHL32(x, 1)
97                 ^ ROTL32(x, 12)
98                 ^ ROTR32(x, 7);
99         return r;       
100 }
101
102 uint32_t bmw_small_s3(uint32_t x){
103         uint32_t r;
104         r =   SHR32(x, 2)
105                 ^ SHL32(x, 2)
106                 ^ ROTL32(x, 15)
107                 ^ ROTR32(x, 3);
108         return r;       
109 }
110
111 uint32_t bmw_small_s4(uint32_t x){
112         uint32_t r;
113         r =   SHR32(x, 1)
114                 ^ x;
115         return r;       
116 }
117
118 uint32_t bmw_small_s5(uint32_t x){
119         uint32_t r;
120         r =   SHR32(x, 2)
121                 ^ x;
122         return r;       
123 }
124
125 uint32_t bmw_small_r1(uint32_t x){
126         uint32_t r;
127         r =   ROTL32(x, 3);
128         return r;       
129 }
130
131 uint32_t bmw_small_r2(uint32_t x){
132         uint32_t r;
133         r =   ROTL32(x, 7);
134         return r;       
135 }
136
137 uint32_t bmw_small_r3(uint32_t x){
138         uint32_t r;
139         r =   ROTL32(x, 13);
140         return r;       
141 }
142
143 uint32_t bmw_small_r4(uint32_t x){
144         uint32_t r;
145         r =   ROTL32(x, 16);
146         return r;       
147 }
148
149 uint32_t bmw_small_r5(uint32_t x){
150         uint32_t r;
151         r =   ROTR32(x, 13);
152         return r;       
153 }
154
155 uint32_t bmw_small_r6(uint32_t x){
156         uint32_t r;
157         r =   ROTR32(x, 9);
158         return r;       
159 }
160
161 uint32_t bmw_small_r7(uint32_t x){
162         uint32_t r;
163         r =   ROTR32(x, 5);
164         return r;       
165 }
166
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};
169         uint32_t r;
170         uint8_t i;
171         r = 0x05555555*(j+16);
172         for(i=0; i<16; ++i){
173                 r += s[i%4](q[j+i]);
174         }
175         r += ((uint32_t*)m)[j];
176         r += ((uint32_t*)m)[j+3];
177         r -= ((uint32_t*)m)[j+10];
178         return r;
179 }
180
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,
184                                                              bmw_small_r7};
185         uint32_t r;
186         uint8_t i;
187         r = 0x05555555*(j+16);
188         for(i=0; i<14; i+=2){
189                 r += q[j+i];
190         }
191         for(i=0; i<14; i+=2){
192                 r += rf[i/2](q[j+i+1]);
193         }
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];
199         return r;
200 }
201
202 void bmw_small_f0(uint32_t* q, const uint32_t* h, const void* m){
203         uint32_t t[16];
204         uint8_t i;
205         uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
206                                    bmw_small_s3, bmw_small_s4 };
207         for(i=0; i<16; ++i){
208                 t[i] = h[i] ^ ((uint32_t*)m)[i];
209         }
210         dump_x(t, 16, 'T');
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]); 
227         dump_x(q, 16, 'W');
228         for(i=0; i<16; ++i){
229                 q[i] = s[i%5](q[i]);
230         }       
231 }
232
233 void bmw_small_f1(uint32_t* q, const void* m){
234         uint8_t i;
235         q[16] = bmw_small_expand1(0, q, m);
236         q[17] = bmw_small_expand1(1, q, m);
237         for(i=2; i<16; ++i){
238                 q[16+i] = bmw_small_expand2(i, q, m);
239         }
240 }
241
242 void bmw_small_f2(uint32_t* h, const uint32_t* q, const void* m){
243         uint32_t xl=0, xh;
244         uint8_t i;
245         for(i=16;i<24;++i){
246                 xl ^= q[i];
247         }
248         xh = xl;
249         for(i=24;i<32;++i){
250                 xh ^= q[i];
251         }
252 #if DEBUG       
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);
257 #endif
258         memcpy(h, m, 16*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);
267         for(i=0; i<8; ++i){
268                 h[i] += xl ^ q[24+i] ^ q[i];
269         }
270         for(i=0; i<8; ++i){
271                 h[8+i] ^= xh ^ q[24+i];
272                 h[8+i] += ROTL32(h[(4+i)%8],i+9);
273         }
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];
282 }
283
284 void bmw_small_nextBlock(bmw_small_ctx_t* ctx, const void* block){
285         uint32_t q[32];
286         dump_x(block, 16, 'M');
287         bmw_small_f0(q, ctx->h, block);
288         dump_x(q, 16, 'Q');
289         bmw_small_f1(q, block);
290         dump_x(q, 32, 'Q');
291         bmw_small_f2(ctx->h, q, block);
292         ctx->counter += 1;
293         ctx_dump(ctx);
294 }
295
296 void bmw_small_lastBlock(bmw_small_ctx_t* ctx, const void* block, uint16_t length_b){
297         uint8_t buffer[64];
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;
302         }
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);
309                 ctx->counter -= 1;
310         }
311         *((uint64_t*)&(buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
312         bmw_small_nextBlock(ctx, buffer);
313 }
314
315 void bmw224_init(bmw224_ctx_t* ctx){
316         uint8_t i;
317         ctx->h[0] = 0x00010203;
318         for(i=1; i<16; ++i){
319                 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
320         }
321 #if BUG24       
322         ctx->h[13] = 0x24353637;
323 #endif
324         ctx->counter=0;
325         ctx_dump(ctx);
326 }
327
328 void bmw256_init(bmw256_ctx_t* ctx){
329         uint8_t i;
330         ctx->h[0] = 0x40414243;
331         for(i=1; i<16; ++i){
332                 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
333         }
334         ctx->counter=0;
335         ctx_dump(ctx);
336 }
337
338 void bmw224_nextBlock(bmw224_ctx_t* ctx, const void* block){
339         bmw_small_nextBlock(ctx, block);
340 }
341
342 void bmw256_nextBlock(bmw256_ctx_t* ctx, const void* block){
343         bmw_small_nextBlock(ctx, block);
344 }
345
346 void bmw224_lastBlock(bmw224_ctx_t* ctx, const void* block, uint16_t length_b){
347         bmw_small_lastBlock(ctx, block, length_b);
348 }
349
350 void bmw256_lastBlock(bmw256_ctx_t* ctx, const void* block, uint16_t length_b){
351         bmw_small_lastBlock(ctx, block, length_b);
352 }
353
354 void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
355         memcpy(dest, &(ctx->h[9]), 224/8);
356 }
357
358 void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
359         memcpy(dest, &(ctx->h[8]), 256/8);
360 }
361
362 void bmw224(void* dest, const void* msg, uint32_t length_b){
363         bmw_small_ctx_t ctx;
364         bmw224_init(&ctx);
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;
369         }
370         bmw_small_lastBlock(&ctx, msg, length_b);
371         bmw224_ctx2hash(dest, &ctx);
372 }
373
374 void bmw256(void* dest, const void* msg, uint32_t length_b){
375         bmw_small_ctx_t ctx;
376         bmw256_init(&ctx);
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;
381         }
382         bmw_small_lastBlock(&ctx, msg, length_b);
383         bmw256_ctx2hash(dest, &ctx);
384 }
385