]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bmw/bmw_large.c
fixing style, typos and uart
[avr-crypto-lib.git] / bmw / 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 <avr/pgmspace.h>
31 #include "bmw_large.h"
32
33 #define SHL64(a,n)  shiftl64(a,n)
34 #define SHR64(a,n)  shiftr64(a,n)
35 #define ROTL64(a,n) rotl64(a,n)
36 #define ROTR64(a,n) rotr64(a,n)
37
38 #define TWEAK   1
39 #define BUG24   0
40 #define F0_HACK 2
41 #define DEBUG   0
42
43 #if DEBUG
44  #include "cli.h"
45
46  void ctx_dump(const bmw_large_ctx_t *ctx){
47         uint8_t i;
48         cli_putstr_P(PSTR("\r\n==== ctx dump ===="));
49         for(i=0; i<16;++i){
50                 cli_putstr_P(PSTR("\r\n h["));
51                 cli_hexdump(&i, 1);
52                 cli_putstr_P(PSTR("] = "));
53                 cli_hexdump_rev(&(ctx->h[i]), 8);
54         }
55         cli_putstr_P(PSTR("\r\n counter = "));
56         cli_hexdump(&(ctx->counter), 4);
57  }
58
59  void dump_x(const uint64_t *q, uint8_t elements, char x){
60         uint8_t i;
61         cli_putstr_P(PSTR("\r\n==== "));
62         cli_putc(x);
63         cli_putstr_P(PSTR(" dump ===="));
64         for(i=0; i<elements;++i){
65                 cli_putstr_P(PSTR("\r\n "));
66                 cli_putc(x);
67                 cli_putstr_P(PSTR("["));
68                 cli_hexdump(&i, 1);
69                 cli_putstr_P(PSTR("] = "));
70                 cli_hexdump_rev(&(q[i]), 8);
71         }
72  }
73 #else
74  #define ctx_dump(x)
75  #define dump_x(a,b,c)
76 #endif
77
78 static
79 uint64_t rotl64(uint64_t a, uint8_t r){
80         return (a<<r)|(a>>(64-r));
81 }
82
83 static
84 uint64_t rotr64(uint64_t a, uint8_t r){
85         return (a>>r)|(a<<(64-r));
86 }
87
88 static
89 uint64_t shiftl64(uint64_t a, uint8_t r){
90         return (a<<r);
91 }
92
93 static
94 uint64_t shiftr64(uint64_t a, uint8_t r){
95         return (a>>r);
96 }
97
98 static
99 uint64_t bmw_large_s0(uint64_t x){
100         uint64_t r;
101         r =   SHR64(x, 1)
102                 ^ SHL64(x, 3)
103                 ^ ROTL64(x, 4)
104                 ^ ROTR64(x, 64-37);
105         return r;
106 }
107
108 static
109 uint64_t bmw_large_s1(uint64_t x){
110         uint64_t r;
111         r =   SHR64(x, 1)
112                 ^ SHL64(x, 2)
113                 ^ ROTL64(x,13)
114                 ^ ROTR64(x,64-43);
115         return r;
116 }
117
118 static
119 uint64_t bmw_large_s2(uint64_t x){
120         uint64_t r;
121         r =   SHR64(x, 2)
122                 ^ SHL64(x, 1)
123                 ^ ROTL64(x, 19)
124                 ^ ROTR64(x, 64-53);
125         return r;
126 }
127
128 static
129 uint64_t bmw_large_s3(uint64_t x){
130         uint64_t r;
131         r =   SHR64(x, 2)
132                 ^ SHL64(x, 2)
133                 ^ ROTL64(x, 28)
134                 ^ ROTR64(x, 64-59);
135         return r;
136 }
137
138 static
139 uint64_t bmw_large_s4(uint64_t x){
140         uint64_t r;
141         r =   SHR64(x, 1)
142                 ^ x;
143         return r;
144 }
145
146 static
147 uint64_t bmw_large_s5(uint64_t x){
148         uint64_t r;
149         r =   SHR64(x, 2)
150                 ^ x;
151         return r;
152 }
153
154 static
155 uint64_t bmw_large_r1(uint64_t x){
156         uint64_t r;
157         r =   ROTL64(x, 5);
158         return r;
159 }
160
161 static
162 uint64_t bmw_large_r2(uint64_t x){
163         uint64_t r;
164         r =   ROTL64(x, 11);
165         return r;
166 }
167
168 static
169 uint64_t bmw_large_r3(uint64_t x){
170         uint64_t r;
171         r =   ROTL64(x, 27);
172         return r;
173 }
174
175 static
176 uint64_t bmw_large_r4(uint64_t x){
177         uint64_t r;
178         r =   ROTL64(x, 32);
179         return r;
180 }
181
182 static
183 uint64_t bmw_large_r5(uint64_t x){
184         uint64_t r;
185         r =   ROTR64(x, 64-37);
186         return r;
187 }
188
189 static
190 uint64_t bmw_large_r6(uint64_t x){
191         uint64_t r;
192         r =   ROTR64(x, 64-43);
193         return r;
194 }
195
196 static
197 uint64_t bmw_large_r7(uint64_t x){
198         uint64_t r;
199         r =   ROTR64(x, 64-53);
200         return r;
201 }
202 /*
203 #define K    0x0555555555555555LL
204 #define MASK 0xFFFFFFFFFFFFFFFFLL
205 static
206 const uint64_t k_lut[] PROGMEM = {
207         16LL*K, 17LL*K, 18LL*K, 19LL*K,
208         20LL*K, 21LL*K, 22LL*K, 23LL*K,
209         24LL*K, 25LL*K, 26LL*K, 27LL*K,
210         28LL*K, 29LL*K, 30LL*K, 31LL*K };
211 */
212 /* the same as above but precomputed to avoid compiler warnings */
213 static
214 const uint64_t k_lut[] PROGMEM = {
215         0x5555555555555550LL, 0x5aaaaaaaaaaaaaa5LL, 0x5ffffffffffffffaLL,
216         0x655555555555554fLL, 0x6aaaaaaaaaaaaaa4LL, 0x6ffffffffffffff9LL,
217         0x755555555555554eLL, 0x7aaaaaaaaaaaaaa3LL, 0x7ffffffffffffff8LL,
218         0x855555555555554dLL, 0x8aaaaaaaaaaaaaa2LL, 0x8ffffffffffffff7LL,
219         0x955555555555554cLL, 0x9aaaaaaaaaaaaaa1LL, 0x9ffffffffffffff6LL,
220         0xa55555555555554bLL };
221
222 static
223 uint64_t bmw_large_expand1(uint8_t j, const uint64_t *q, const void *m, const void *h){
224         uint64_t(*s[])(uint64_t) = {bmw_large_s1, bmw_large_s2, bmw_large_s3, bmw_large_s0};
225         uint64_t a = 0;
226         union{
227                 uint64_t v64;
228                 uint32_t v32[2];
229         } r;
230         uint8_t i;
231         /* r = 0x0555555555555555LL*(j+16); */
232         r.v32[0] = pgm_read_dword(((uint8_t*)k_lut+8*j));
233         r.v32[1] = pgm_read_dword(((uint8_t*)k_lut+8*j+4));
234         for(i=0; i<16; ++i){
235                 a += s[i%4](q[j+i]);
236         }
237 #if TWEAK
238         a += (   ROTL64(((uint64_t*)m)[(j)&0xf],   ((j+ 0)&0xf)+1)
239                + ROTL64(((uint64_t*)m)[(j+3)&0xf], ((j+ 3)&0xf)+1)
240                + r.v64
241                - ROTL64(((uint64_t*)m)[(j+10)&0xf],((j+10)&0xf)+1)
242              ) ^ ((uint64_t*)h)[(j+7)&0xf];
243 #else
244         a += ((uint64_t*)m)[j&0xf];
245         a += ((uint64_t*)m)[(j+3)&0xf];
246         a -= ((uint64_t*)m)[(j+10)&0xf];
247         a += r.v64;
248 #endif
249         return a;
250 }
251
252 static
253 uint64_t bmw_large_expand2(uint8_t j, const uint64_t *q, const void *m, const void *h){
254         uint64_t(*rf[])(uint64_t) = {bmw_large_r1, bmw_large_r2, bmw_large_r3,
255                                      bmw_large_r4, bmw_large_r5, bmw_large_r6,
256                                                              bmw_large_r7};
257         uint64_t a=0;
258         union{
259                 uint64_t v64;
260                 uint32_t v32[2];
261         } r;
262         uint8_t i;
263         /* r = 0x0555555555555555LL*(j+16); */
264         r.v32[0] = pgm_read_dword(((uint8_t*)k_lut+8*j));
265         r.v32[1] = pgm_read_dword(((uint8_t*)k_lut+8*j+4));
266         for(i=0; i<14; i+=2){
267                 a += q[j+i];
268         }
269         for(i=0; i<14; i+=2){
270                 a += rf[i/2](q[j+i+1]);
271         }
272 #if TWEAK
273         a += bmw_large_s4(q[j+14]);
274         a += bmw_large_s5(q[j+15]);
275 #else
276         a += bmw_large_s5(q[j+14]);
277         a += bmw_large_s4(q[j+15]);
278 #endif
279 #if TWEAK
280         /*
281         if(j==(22-16)){
282                 uint64_t t;
283                 cli_putstr_P(PSTR("\n+++++++++ expand_2 ++++++++++++"));
284                 dump_x(&a, 1, 'a');
285                 dump_x(&r, 1, 'r');
286                 t=ROTL64(((uint64_t*)m)[j],   ((j+ 0)&0xf)+1);
287                 dump_x(&t, 1, '0');
288                 t=ROTL64(((uint64_t*)m)[j],   ((j+ 3)&0xf)+1);
289                 dump_x(&t, 1, '0');
290                 t=ROTL64(((uint64_t*)m)[j],   ((j+ 0)&0xf)+1);
291                 dump_x(&t, 1, '0');
292
293         }
294         */
295         a += (   ROTL64(((uint64_t*)m)[(j)&0xf],   ((j+ 0)&0xf)+1)
296                + ROTL64(((uint64_t*)m)[(j+3)&0xf], ((j+ 3)&0xf)+1)
297                + r.v64
298                - ROTL64(((uint64_t*)m)[(j+10)&0xf],((j+10)&0xf)+1)
299              ) ^ ((uint64_t*)h)[(j+7)&0xf];
300 #else
301         a += ((uint64_t*)m)[j&0xf];
302         a += ((uint64_t*)m)[(j+3)&0xf];
303         a -= ((uint64_t*)m)[(j+10)&0xf];
304         a += r.v64;
305 #endif
306         return a;
307 }
308
309 #if F0_HACK==2
310 /* to understand this implementation take a look at f0-opt-table.txt */
311 static const uint16_t hack_table[5] PROGMEM = { 0x0311, 0xDDB3, 0x2A79, 0x07AA, 0x51C2 };
312 static const uint8_t  offset_table[5] PROGMEM = { 4+16, 6+16, 9+16, 12+16, 13+16 };
313
314
315 static
316 void bmw_large_f0(uint64_t *q, const uint64_t *h, const void *m){
317         uint16_t hack_reg;
318         uint8_t i,j,c;
319         uint64_t(*s[])(uint64_t)={ bmw_large_s0, bmw_large_s1, bmw_large_s2,
320                                    bmw_large_s3, bmw_large_s4 };
321         for(i=0; i<16; ++i){
322                 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
323         }
324         dump_x(h, 16, 'T');
325         memset(q, 0, 8*16);
326         c=4;
327         do{
328                 i=15;
329                 j=pgm_read_byte(offset_table+c);
330                 hack_reg=pgm_read_word(&(hack_table[c]));
331                 do{
332                         if(hack_reg&1){
333                                 q[i]-= h[j&15];
334                         }else{
335                                 q[i]+= h[j&15];
336                         }
337                         --j;
338                         hack_reg>>= 1;
339                 }while(i--!=0);
340         }while(c--!=0);
341         dump_x(q, 16, 'W');
342         for(i=0; i<16; ++i){
343                 q[i] = s[i%5](q[i]);
344         }
345 #if TWEAK
346         for(i=0; i<16; ++i){
347                 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
348         }
349         for(i=0; i<16; ++i){
350                 q[i] += h[(i+1)&0xf];
351         }
352 #endif /* TWEAK */
353 }
354 #endif /* F0_HACK==2 */
355
356 #if F0_HACK==1
357 static
358 const uint8_t f0_lut[] PROGMEM ={
359          5<<1, ( 7<<1)+1, (10<<1)+0, (13<<1)+0, (14<<1)+0,
360          6<<1, ( 8<<1)+1, (11<<1)+0, (14<<1)+0, (15<<1)+1,
361          0<<1, ( 7<<1)+0, ( 9<<1)+0, (12<<1)+1, (15<<1)+0,
362          0<<1, ( 1<<1)+1, ( 8<<1)+0, (10<<1)+1, (13<<1)+0,
363          1<<1, ( 2<<1)+0, ( 9<<1)+0, (11<<1)+1, (14<<1)+1,
364          3<<1, ( 2<<1)+1, (10<<1)+0, (12<<1)+1, (15<<1)+0,
365          4<<1, ( 0<<1)+1, ( 3<<1)+1, (11<<1)+1, (13<<1)+0,
366          1<<1, ( 4<<1)+1, ( 5<<1)+1, (12<<1)+1, (14<<1)+1,
367          2<<1, ( 5<<1)+1, ( 6<<1)+1, (13<<1)+0, (15<<1)+1,
368          0<<1, ( 3<<1)+1, ( 6<<1)+0, ( 7<<1)+1, (14<<1)+0,
369          8<<1, ( 1<<1)+1, ( 4<<1)+1, ( 7<<1)+1, (15<<1)+0,
370          8<<1, ( 0<<1)+1, ( 2<<1)+1, ( 5<<1)+1, ( 9<<1)+0,
371          1<<1, ( 3<<1)+0, ( 6<<1)+1, ( 9<<1)+1, (10<<1)+0,
372          2<<1, ( 4<<1)+0, ( 7<<1)+0, (10<<1)+0, (11<<1)+0,
373          3<<1, ( 5<<1)+1, ( 8<<1)+0, (11<<1)+1, (12<<1)+1,
374         12<<1, ( 4<<1)+1, ( 6<<1)+1, ( 9<<1)+1, (13<<1)+0
375 };
376
377 static
378 void bmw_large_f0(uint64_t *q, const uint64_t *h, const void *m){
379         uint8_t i,j=-1,v,sign,l=0;
380         uint64_t(*s[])(uint64_t)={ bmw_large_s0, bmw_large_s1, bmw_large_s2,
381                                    bmw_large_s3, bmw_large_s4 };
382         for(i=0; i<16; ++i){
383                 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
384         }
385         dump_x(h, 16, 'T');
386 //      memset(q, 0, 4*16);
387         for(i=0; i<5*16; ++i){
388                 v = pgm_read_byte(f0_lut+i);
389                 sign = v&1;
390                 v >>=1;
391                 if(i==l){
392                         j++;
393                         l+=5;
394                         q[j] = h[v];
395                         continue;
396                 }
397                 if(sign){
398                         q[j] -= h[v];
399                 }else{
400                         q[j] += h[v];
401                 }
402         }
403         dump_x(q, 16, 'W');
404         for(i=0; i<16; ++i){
405                 q[i] = s[i%5](q[i]);
406         }
407 #if TWEAK
408         for(i=0; i<16; ++i){
409                 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
410         }
411         for(i=0; i<16; ++i){
412                 q[i] += h[(i+1)&0xf];
413         }
414 #endif /* TWEAK */
415 }
416 #endif /* F0_HACK==1 */
417
418 #if F0_HACK==0
419 static
420 void bmw_large_f0(uint64_t *q, const uint64_t *h, const void *m){
421         uint8_t i;
422         uint64_t(*s[])(uint64_t)={ bmw_large_s0, bmw_large_s1, bmw_large_s2,
423                                    bmw_large_s3, bmw_large_s4 };
424         for(i=0; i<16; ++i){
425                 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
426         }
427 //      dump_x(t, 16, 'T');
428         q[ 0] = (h[ 5] - h[ 7] + h[10] + h[13] + h[14]);
429         q[ 1] = (h[ 6] - h[ 8] + h[11] + h[14] - h[15]);
430         q[ 2] = (h[ 0] + h[ 7] + h[ 9] - h[12] + h[15]);
431         q[ 3] = (h[ 0] - h[ 1] + h[ 8] - h[10] + h[13]);
432         q[ 4] = (h[ 1] + h[ 2] + h[ 9] - h[11] - h[14]);
433         q[ 5] = (h[ 3] - h[ 2] + h[10] - h[12] + h[15]);
434         q[ 6] = (h[ 4] - h[ 0] - h[ 3] - h[11] + h[13]);
435         q[ 7] = (h[ 1] - h[ 4] - h[ 5] - h[12] - h[14]);
436         q[ 8] = (h[ 2] - h[ 5] - h[ 6] + h[13] - h[15]);
437         q[ 9] = (h[ 0] - h[ 3] + h[ 6] - h[ 7] + h[14]);
438         q[10] = (h[ 8] - h[ 1] - h[ 4] - h[ 7] + h[15]);
439         q[11] = (h[ 8] - h[ 0] - h[ 2] - h[ 5] + h[ 9]);
440         q[12] = (h[ 1] + h[ 3] - h[ 6] - h[ 9] + h[10]);
441         q[13] = (h[ 2] + h[ 4] + h[ 7] + h[10] + h[11]);
442         q[14] = (h[ 3] - h[ 5] + h[ 8] - h[11] - h[12]);
443         q[15] = (h[12] - h[ 4] - h[ 6] - h[ 9] + h[13]);
444         dump_x(q, 16, 'W');
445         for(i=0; i<16; ++i){
446                 q[i] = s[i%5](q[i]);
447         }
448 #if TWEAK
449         for(i=0; i<16; ++i){
450                 ((uint64_t*)h)[i] ^= ((uint64_t*)m)[i];
451         }
452         for(i=0; i<16; ++i){
453                 q[i] += h[(i+1)&0xf];
454         }
455 #endif /* TWEAK */
456
457 }
458 #endif /* F0_HACK==0 */
459
460 static
461 void bmw_large_f1(uint64_t *q, const void *m, const uint64_t *h){
462         uint8_t i;
463         q[16] = bmw_large_expand1(0, q, m, h);
464         q[17] = bmw_large_expand1(1, q, m, h);
465         for(i=2; i<16; ++i){
466                 q[16+i] = bmw_large_expand2(i, q, m, h);
467         }
468 }
469
470 static
471 void bmw_large_f2(uint64_t *h, const uint64_t *q, const void *m){
472         uint64_t xl=0, xh;
473         uint8_t i;
474         for(i=16;i<24;++i){
475                 xl ^= q[i];
476         }
477         xh = xl;
478         for(i=24;i<32;++i){
479                 xh ^= q[i];
480         }
481 #if DEBUG
482         cli_putstr_P(PSTR("\r\n XL = "));
483         cli_hexdump_rev(&xl, 4);
484         cli_putstr_P(PSTR("\r\n XH = "));
485         cli_hexdump_rev(&xh, 4);
486 #endif
487         memcpy(h, m, 16*8);
488         h[0] ^= SHL64(xh, 5) ^ SHR64(q[16], 5);
489         h[1] ^= SHR64(xh, 7) ^ SHL64(q[17], 8);
490         h[2] ^= SHR64(xh, 5) ^ SHL64(q[18], 5);
491         h[3] ^= SHR64(xh, 1) ^ SHL64(q[19], 5);
492         h[4] ^= SHR64(xh, 3) ^ q[20];
493         h[5] ^= SHL64(xh, 6) ^ SHR64(q[21], 6);
494         h[6] ^= SHR64(xh, 4) ^ SHL64(q[22], 6);
495         h[7] ^= SHR64(xh,11) ^ SHL64(q[23], 2);
496         for(i=0; i<8; ++i){
497                 h[i] += xl ^ q[24+i] ^ q[i];
498         }
499         for(i=0; i<8; ++i){
500                 h[8+i] ^= xh ^ q[24+i];
501                 h[8+i] += ROTL64(h[(4+i)%8],i+9);
502         }
503         h[ 8] += SHL64(xl, 8) ^ q[23] ^ q[ 8];
504         h[ 9] += SHR64(xl, 6) ^ q[16] ^ q[ 9];
505         h[10] += SHL64(xl, 6) ^ q[17] ^ q[10];
506         h[11] += SHL64(xl, 4) ^ q[18] ^ q[11];
507         h[12] += SHR64(xl, 3) ^ q[19] ^ q[12];
508         h[13] += SHR64(xl, 4) ^ q[20] ^ q[13];
509         h[14] += SHR64(xl, 7) ^ q[21] ^ q[14];
510         h[15] += SHR64(xl, 2) ^ q[22] ^ q[15];
511 }
512
513 void bmw_large_nextBlock(bmw_large_ctx_t *ctx, const void *block){
514         uint64_t q[32];
515         dump_x(block, 16, 'M');
516         bmw_large_f0(q, ctx->h, block);
517         dump_x(q, 16, 'Q');
518         bmw_large_f1(q, block, ctx->h);
519         dump_x(q, 32, 'Q');
520         bmw_large_f2(ctx->h, q, block);
521         ctx->counter += 1;
522         ctx_dump(ctx);
523 }
524
525 void bmw_large_lastBlock(bmw_large_ctx_t *ctx, const void *block, uint16_t length_b){
526         union {
527                 uint8_t   v8[128];
528                 uint64_t v64[ 16];
529         } buffer;
530         while(length_b >= BMW_LARGE_BLOCKSIZE){
531                 bmw_large_nextBlock(ctx, block);
532                 length_b -= BMW_LARGE_BLOCKSIZE;
533                 block = (uint8_t*)block + BMW_LARGE_BLOCKSIZE_B;
534         }
535         memset(buffer.v8, 0, 128);
536         memcpy(buffer.v8, block, (length_b+7)/8);
537         buffer.v8[length_b>>3] |= 0x80 >> (length_b&0x07);
538         if(length_b+1>128*8-64){
539                 bmw_large_nextBlock(ctx, buffer.v8);
540                 memset(buffer.v8, 0, 128-8);
541                 ctx->counter -= 1;
542         }
543         buffer.v64[15] = (uint64_t)(ctx->counter*1024LL)+(uint64_t)length_b;
544         bmw_large_nextBlock(ctx, buffer.v8);
545 #if TWEAK
546         uint8_t i;
547         uint64_t q[32];
548         memset(buffer.v8, 0xaa, 128);
549         for(i=0; i<16; ++i){
550                 buffer.v8[8*i] = i + 0xa0;
551         }
552         bmw_large_f0(q, buffer.v64, ctx->h);
553         bmw_large_f1(q, ctx->h, buffer.v64);
554         bmw_large_f2(buffer.v64, q, ctx->h);
555         memcpy(ctx->h, buffer.v8, 128);
556 #endif
557 }
558
559 void bmw384_init(bmw384_ctx_t *ctx){
560         uint8_t i;
561         ctx->h[0] = 0x0001020304050607LL;
562         for(i=1; i<16; ++i){
563                 ctx->h[i] = ctx->h[i-1]+ 0x0808080808080808LL;
564         }
565 #if BUG24
566         ctx->h[6] = 0x3031323324353637LL;
567 #endif
568         ctx->counter=0;
569         ctx_dump(ctx);
570 }
571
572 void bmw512_init(bmw512_ctx_t *ctx){
573         uint8_t i;
574         ctx->h[0] = 0x8081828384858687LL;
575         for(i=1; i<16; ++i){
576                 ctx->h[i] = ctx->h[i-1]+ 0x0808080808080808LL;
577         }
578         ctx->counter=0;
579         ctx_dump(ctx);
580 }
581
582 void bmw384_nextBlock(bmw384_ctx_t *ctx, const void *block){
583         bmw_large_nextBlock(ctx, block);
584 }
585
586 void bmw512_nextBlock(bmw512_ctx_t *ctx, const void *block){
587         bmw_large_nextBlock(ctx, block);
588 }
589
590 void bmw384_lastBlock(bmw384_ctx_t *ctx, const void *block, uint16_t length_b){
591         bmw_large_lastBlock(ctx, block, length_b);
592 }
593
594 void bmw512_lastBlock(bmw512_ctx_t *ctx, const void *block, uint16_t length_b){
595         bmw_large_lastBlock(ctx, block, length_b);
596 }
597
598 void bmw384_ctx2hash(void *dest, const bmw384_ctx_t *ctx){
599         memcpy(dest, &(ctx->h[10]), 384/8);
600 }
601
602 void bmw512_ctx2hash(void *dest, const bmw512_ctx_t *ctx){
603         memcpy(dest, &(ctx->h[8]), 512/8);
604 }
605
606 void bmw384(void *dest, const void *msg, uint32_t length_b){
607         bmw_large_ctx_t ctx;
608         bmw384_init(&ctx);
609         while(length_b>=BMW_LARGE_BLOCKSIZE){
610                 bmw_large_nextBlock(&ctx, msg);
611                 length_b -= BMW_LARGE_BLOCKSIZE;
612                 msg = (uint8_t*)msg + BMW_LARGE_BLOCKSIZE_B;
613         }
614         bmw_large_lastBlock(&ctx, msg, length_b);
615         bmw384_ctx2hash(dest, &ctx);
616 }
617
618 void bmw512(void *dest, const void *msg, uint32_t length_b){
619         bmw_large_ctx_t ctx;
620         bmw512_init(&ctx);
621         while(length_b>=BMW_LARGE_BLOCKSIZE){
622                 bmw_large_nextBlock(&ctx, msg);
623                 length_b -= BMW_LARGE_BLOCKSIZE;
624                 msg = (uint8_t*)msg + BMW_LARGE_BLOCKSIZE_B;
625         }
626         bmw_large_lastBlock(&ctx, msg, length_b);
627         bmw512_ctx2hash(dest, &ctx);
628 }
629