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