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