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