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