]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bmw_large.c
+BlueMidnightWish
[avr-crypto-lib.git] / bmw_large.c
1 /* bmw_large.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_large.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_large.h"
31
32 #define SHL64(a,n) ((a)<<(n))
33 #define SHR64(a,n) ((a)>>(n))
34 #define ROTL64(a,n) (((a)<<(n))|((a)>>(64-(n))))
35 #define ROTR64(a,n) (((a)>>(n))|((a)<<(64-(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_large_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]), 8);
51         }
52         cli_putstr_P(PSTR("\r\n counter = "));
53         cli_hexdump(&(ctx->counter), 4);
54  }
55  
56  void dump_x(const uint64_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]), 8);
68         }
69  }
70 #else
71  #define ctx_dump(x)
72  #define dump_x(a,b,c)
73 #endif
74
75 uint64_t bmw_large_s0(uint64_t x){
76         uint64_t r;
77         r =   SHR64(x, 1)
78                 ^ SHL64(x, 3)
79                 ^ ROTL64(x, 4)
80                 ^ ROTR64(x, 64-37);
81         return r;       
82 }
83
84 uint64_t bmw_large_s1(uint64_t x){
85         uint64_t r;
86         r =   SHR64(x, 1)
87                 ^ SHL64(x, 2)
88                 ^ ROTL64(x,13)
89                 ^ ROTR64(x,64-43);
90         return r;       
91 }
92
93 uint64_t bmw_large_s2(uint64_t x){
94         uint64_t r;
95         r =   SHR64(x, 2)
96                 ^ SHL64(x, 1)
97                 ^ ROTL64(x, 19)
98                 ^ ROTR64(x, 64-53);
99         return r;       
100 }
101
102 uint64_t bmw_large_s3(uint64_t x){
103         uint64_t r;
104         r =   SHR64(x, 2)
105                 ^ SHL64(x, 2)
106                 ^ ROTL64(x, 28)
107                 ^ ROTR64(x, 64-59);
108         return r;       
109 }
110
111 uint64_t bmw_large_s4(uint64_t x){
112         uint64_t r;
113         r =   SHR64(x, 1)
114                 ^ x;
115         return r;       
116 }
117
118 uint64_t bmw_large_s5(uint64_t x){
119         uint64_t r;
120         r =   SHR64(x, 2)
121                 ^ x;
122         return r;       
123 }
124
125 uint64_t bmw_large_r1(uint64_t x){
126         uint64_t r;
127         r =   ROTL64(x, 5);
128         return r;       
129 }
130
131 uint64_t bmw_large_r2(uint64_t x){
132         uint64_t r;
133         r =   ROTL64(x, 11);
134         return r;       
135 }
136
137 uint64_t bmw_large_r3(uint64_t x){
138         uint64_t r;
139         r =   ROTL64(x, 27);
140         return r;       
141 }
142
143 uint64_t bmw_large_r4(uint64_t x){
144         uint64_t r;
145         r =   ROTL64(x, 32);
146         return r;       
147 }
148
149 uint64_t bmw_large_r5(uint64_t x){
150         uint64_t r;
151         r =   ROTR64(x, 64-37);
152         return r;       
153 }
154
155 uint64_t bmw_large_r6(uint64_t x){
156         uint64_t r;
157         r =   ROTR64(x, 64-43);
158         return r;       
159 }
160
161 uint64_t bmw_large_r7(uint64_t x){
162         uint64_t r;
163         r =   ROTR64(x, 64-53);
164         return r;       
165 }
166
167 uint64_t bmw_large_expand1(uint8_t j, const uint64_t* q, const void* m){
168         uint64_t(*s[])(uint64_t) = {bmw_large_s1, bmw_large_s2, bmw_large_s3, bmw_large_s0};
169         uint64_t r;
170         uint8_t i;
171         r = 0x0555555555555555LL*(j+16);
172         for(i=0; i<16; ++i){
173                 r += s[i%4](q[j+i]);
174         }
175         r += ((uint64_t*)m)[j];
176         r += ((uint64_t*)m)[j+3];
177         r -= ((uint64_t*)m)[j+10];
178         return r;
179 }
180
181 uint64_t bmw_large_expand2(uint8_t j, const uint64_t* q, const void* m){
182         uint64_t(*rf[])(uint64_t) = {bmw_large_r1, bmw_large_r2, bmw_large_r3,
183                                      bmw_large_r4, bmw_large_r5, bmw_large_r6,
184                                                              bmw_large_r7};
185         uint64_t r;
186         uint8_t i;
187         r = 0x0555555555555555LL*(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_large_s5(q[j+14]);
195         r += bmw_large_s4(q[j+15]);
196         r += ((uint64_t*)m)[j];
197         r += ((uint64_t*)m)[(j+3)%16];
198         r -= ((uint64_t*)m)[(j+10)%16];
199         return r;
200 }
201
202 void bmw_large_f0(uint64_t* q, const uint64_t* h, const void* m){
203         uint64_t t[16];
204         uint8_t i;
205         uint64_t(*s[])(uint64_t)={ bmw_large_s0, bmw_large_s1, bmw_large_s2,
206                                    bmw_large_s3, bmw_large_s4 };
207         for(i=0; i<16; ++i){
208                 t[i] = h[i] ^ ((uint64_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_large_f1(uint64_t* q, const void* m){
234         uint8_t i;
235         q[16] = bmw_large_expand1(0, q, m);
236         q[17] = bmw_large_expand1(1, q, m);
237         for(i=2; i<16; ++i){
238                 q[16+i] = bmw_large_expand2(i, q, m);
239         }
240 }
241
242 void bmw_large_f2(uint64_t* h, const uint64_t* q, const void* m){
243         uint64_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*8);
259         h[0] ^= SHL64(xh, 5) ^ SHR64(q[16], 5);
260         h[1] ^= SHR64(xh, 7) ^ SHL64(q[17], 8);
261         h[2] ^= SHR64(xh, 5) ^ SHL64(q[18], 5);
262         h[3] ^= SHR64(xh, 1) ^ SHL64(q[19], 5);
263         h[4] ^= SHR64(xh, 3) ^ q[20];
264         h[5] ^= SHL64(xh, 6) ^ SHR64(q[21], 6);
265         h[6] ^= SHR64(xh, 4) ^ SHL64(q[22], 6);
266         h[7] ^= SHR64(xh,11) ^ SHL64(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] += ROTL64(h[(4+i)%8],i+9);
273         }
274         h[ 8] += SHL64(xl, 8) ^ q[23] ^ q[ 8];
275         h[ 9] += SHR64(xl, 6) ^ q[16] ^ q[ 9];
276         h[10] += SHL64(xl, 6) ^ q[17] ^ q[10];
277         h[11] += SHL64(xl, 4) ^ q[18] ^ q[11];
278         h[12] += SHR64(xl, 3) ^ q[19] ^ q[12];
279         h[13] += SHR64(xl, 4) ^ q[20] ^ q[13];
280         h[14] += SHR64(xl, 7) ^ q[21] ^ q[14];
281         h[15] += SHR64(xl, 2) ^ q[22] ^ q[15];
282 }
283
284 void bmw_large_nextBlock(bmw_large_ctx_t* ctx, const void* block){
285         uint64_t q[32];
286         dump_x(block, 16, 'M');
287         bmw_large_f0(q, ctx->h, block);
288         dump_x(q, 16, 'Q');
289         bmw_large_f1(q, block);
290         dump_x(q, 32, 'Q');
291         bmw_large_f2(ctx->h, q, block);
292         ctx->counter += 1;
293         ctx_dump(ctx);
294 }
295
296 void bmw_large_lastBlock(bmw_large_ctx_t* ctx, const void* block, uint16_t length_b){
297         uint8_t buffer[128];
298         while(length_b >= BMW_LARGE_BLOCKSIZE){
299                 bmw_large_nextBlock(ctx, block);
300                 length_b -= BMW_LARGE_BLOCKSIZE;
301                 block = (uint8_t*)block + BMW_LARGE_BLOCKSIZE_B;
302         }
303         memset(buffer, 0, 128);
304         memcpy(buffer, block, (length_b+7)/8);
305         buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
306         if(length_b+1>128*8-64){
307                 bmw_large_nextBlock(ctx, buffer);
308                 memset(buffer, 0, 128-8);
309                 ctx->counter -= 1;
310         }
311         *((uint64_t*)&(buffer[128-8])) = (uint64_t)(ctx->counter*1024LL)+(uint64_t)length_b;
312         bmw_large_nextBlock(ctx, buffer);
313 }
314
315 void bmw384_init(bmw384_ctx_t* ctx){
316         uint8_t i;
317         ctx->h[0] = 0x0001020304050607LL;
318         for(i=1; i<16; ++i){
319                 ctx->h[i] = ctx->h[i-1]+ 0x0808080808080808LL;
320         }
321 #if BUG24       
322         ctx->h[6] = 0x3031323324353637LL;
323 #endif
324         ctx->counter=0;
325         ctx_dump(ctx);
326 }
327
328 void bmw512_init(bmw512_ctx_t* ctx){
329         uint8_t i;
330         ctx->h[0] = 0x8081828384858687LL;
331         for(i=1; i<16; ++i){
332                 ctx->h[i] = ctx->h[i-1]+ 0x0808080808080808LL;
333         }
334         ctx->counter=0;
335         ctx_dump(ctx);
336 }
337
338 void bmw384_nextBlock(bmw384_ctx_t* ctx, const void* block){
339         bmw_large_nextBlock(ctx, block);
340 }
341
342 void bmw512_nextBlock(bmw512_ctx_t* ctx, const void* block){
343         bmw_large_nextBlock(ctx, block);
344 }
345
346 void bmw384_lastBlock(bmw384_ctx_t* ctx, const void* block, uint16_t length_b){
347         bmw_large_lastBlock(ctx, block, length_b);
348 }
349
350 void bmw512_lastBlock(bmw512_ctx_t* ctx, const void* block, uint16_t length_b){
351         bmw_large_lastBlock(ctx, block, length_b);
352 }
353
354 void bmw384_ctx2hash(void* dest, const bmw384_ctx_t* ctx){
355         memcpy(dest, &(ctx->h[10]), 384/8);
356 }
357
358 void bmw512_ctx2hash(void* dest, const bmw512_ctx_t* ctx){
359         memcpy(dest, &(ctx->h[8]), 512/8);
360 }
361
362 void bmw384(void* dest, const void* msg, uint32_t length_b){
363         bmw_large_ctx_t ctx;
364         bmw384_init(&ctx);
365         while(length_b>=BMW_LARGE_BLOCKSIZE){
366                 bmw_large_nextBlock(&ctx, msg);
367                 length_b -= BMW_LARGE_BLOCKSIZE;
368                 msg = (uint8_t*)msg + BMW_LARGE_BLOCKSIZE_B;
369         }
370         bmw_large_lastBlock(&ctx, msg, length_b);
371         bmw384_ctx2hash(dest, &ctx);
372 }
373
374 void bmw512(void* dest, const void* msg, uint32_t length_b){
375         bmw_large_ctx_t ctx;
376         bmw512_init(&ctx);
377         while(length_b>=BMW_LARGE_BLOCKSIZE){
378                 bmw_large_nextBlock(&ctx, msg);
379                 length_b -= BMW_LARGE_BLOCKSIZE;
380                 msg = (uint8_t*)msg + BMW_LARGE_BLOCKSIZE_B;
381         }
382         bmw_large_lastBlock(&ctx, msg, length_b);
383         bmw512_ctx2hash(dest, &ctx);
384 }
385