bmw_small.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. * \file bmw_small.c
  18. * \author Daniel Otte
  19. * \email daniel.otte@rub.de
  20. * \date 2009-04-27
  21. * \license GPLv3 or later
  22. *
  23. */
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <crypto/bmw_small.h>
  27. #include <crypto/memxor.h>
  28. #define SHL32(a,n) ((a)<<(n))
  29. #define SHR32(a,n) ((a)>>(n))
  30. #define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
  31. #define ROTR32(a,n) (((a)>>(n))|((a)<<(32-(n))))
  32. #define TWEAK 1
  33. #if TWEAK
  34. # define BUG24 0
  35. #else
  36. # define BUG24 1
  37. #endif
  38. #define F0_HACK 0
  39. #define DEBUG 0
  40. #ifndef F0_HACK
  41. # define F0_HACK 0
  42. #endif
  43. #if DEBUG
  44. #include <crypto/cli.h>
  45. void ctx_dump(const bmw_small_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]), 4);
  53. }
  54. cli_putstr("\r\n counter = ");
  55. cli_hexdump(&(ctx->counter), 4);
  56. }
  57. void dump_x(const uint32_t* q, uint8_t elements, char x){
  58. uint8_t i;
  59. cli_putstr("\r\n==== ");
  60. cli_putc(x);
  61. cli_putstr(" dump ====");
  62. for(i=0; i<elements;++i){
  63. cli_putstr("\r\n ");
  64. cli_putc(x);
  65. cli_putstr("[");
  66. cli_hexdump(&i, 1);
  67. cli_putstr("] = ");
  68. cli_hexdump_rev(&(q[i]), 4);
  69. }
  70. }
  71. #else
  72. #define ctx_dump(x)
  73. #define dump_x(a,b,c)
  74. #endif
  75. #define S32_0(x) ( (SHR32((x), 1)) ^ \
  76. (SHL32((x), 3)) ^ \
  77. (ROTL32((x), 4)) ^ \
  78. (ROTR32((x), 13)) )
  79. #define S32_1(x) ( (SHR32((x), 1)) ^ \
  80. (SHL32((x), 2)) ^ \
  81. (ROTL32((x), 8)) ^ \
  82. (ROTR32((x), 9)) )
  83. #define S32_2(x) ( (SHR32((x), 2)) ^ \
  84. (SHL32((x), 1)) ^ \
  85. (ROTL32((x), 12)) ^ \
  86. (ROTR32((x), 7)) )
  87. #define S32_3(x) ( (SHR32((x), 2)) ^ \
  88. (SHL32((x), 2)) ^ \
  89. (ROTL32((x), 15)) ^ \
  90. (ROTR32((x), 3)) )
  91. #define S32_4(x) ( (SHR32((x), 1)) ^ (x))
  92. #define S32_5(x) ( (SHR32((x), 2)) ^ (x))
  93. #define R32_1(x) (ROTL32((x), 3))
  94. #define R32_2(x) (ROTL32((x), 7))
  95. #define R32_3(x) (ROTL32((x), 13))
  96. #define R32_4(x) (ROTL32((x), 16))
  97. #define R32_5(x) (ROTR32((x), 13))
  98. #define R32_6(x) (ROTR32((x), 9))
  99. #define R32_7(x) (ROTR32((x), 5))
  100. /*
  101. #define K 0x05555555L
  102. static
  103. uint32_t k_lut[] PROGMEM = {
  104. 16L*K, 17L*K, 18L*K, 19L*K, 20L*K, 21L*K, 22L*K, 23L*K,
  105. 24L*K, 25L*K, 26L*K, 27L*K, 28L*K, 29L*K, 30L*K, 31L*K
  106. };
  107. */
  108. /* same as above but precomputed to avoid compiler warnings */
  109. static
  110. uint32_t k_lut[] = {
  111. 0x55555550L, 0x5aaaaaa5L, 0x5ffffffaL,
  112. 0x6555554fL, 0x6aaaaaa4L, 0x6ffffff9L,
  113. 0x7555554eL, 0x7aaaaaa3L, 0x7ffffff8L,
  114. 0x8555554dL, 0x8aaaaaa2L, 0x8ffffff7L,
  115. 0x9555554cL, 0x9aaaaaa1L, 0x9ffffff6L,
  116. 0xa555554bL };
  117. static
  118. uint32_t bmw_small_expand1(uint8_t j, const uint32_t* q, const void* m, const void* h){
  119. uint32_t r;
  120. /* r = 0x05555555*(j+16); */
  121. r = ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
  122. + ROTL32(((uint32_t*)m)[(j+3)&0xf], ((j+3)&0xf)+1 )
  123. - ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
  124. + k_lut[j]
  125. ) ^ ((uint32_t*)h)[(j+7)&0xf];
  126. r += S32_1(q[j+ 0]) + S32_2(q[j+ 1]) + S32_3(q[j+ 2]) + S32_0(q[j+ 3])
  127. + S32_1(q[j+ 4]) + S32_2(q[j+ 5]) + S32_3(q[j+ 6]) + S32_0(q[j+ 7])
  128. + S32_1(q[j+ 8]) + S32_2(q[j+ 9]) + S32_3(q[j+10]) + S32_0(q[j+11])
  129. + S32_1(q[j+12]) + S32_2(q[j+13]) + S32_3(q[j+14]) + S32_0(q[j+15]);
  130. return r;
  131. }
  132. static
  133. uint32_t bmw_small_expand2(uint8_t j, const uint32_t* q, const void* m, const void* h){
  134. uint32_t r;
  135. r = ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
  136. + ROTL32(((uint32_t*)m)[(j+3)&0xf], ((j+3)&0xf)+1 )
  137. - ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
  138. + k_lut[j]
  139. ) ^ ((uint32_t*)h)[(j+7)&0xf];
  140. r += (q[j+ 0]) + R32_1(q[j+ 1]) + (q[j+ 2]) + R32_2(q[j+ 3])
  141. + (q[j+ 4]) + R32_3(q[j+ 5]) + (q[j+ 6]) + R32_4(q[j+ 7])
  142. + (q[j+ 8]) + R32_5(q[j+ 9]) + (q[j+10]) + R32_6(q[j+11])
  143. + (q[j+12]) + R32_7(q[j+13]) + S32_4(q[j+14]) + S32_5(q[j+15]);
  144. return r;
  145. }
  146. #if F0_HACK==2
  147. /* to understand this implementation take a look at f0-opt-table.txt */
  148. static uint16_t hack_table[5] = { 0x0311, 0xDDB3, 0x2A79, 0x07AA, 0x51C2 };
  149. static uint8_t offset_table[5] = { 4+16, 6+16, 9+16, 12+16, 13+16 };
  150. static
  151. void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
  152. uint16_t hack_reg;
  153. uint8_t c,i,j;
  154. uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
  155. bmw_small_s3, bmw_small_s4 };
  156. for(i=0; i<16; ++i){
  157. ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
  158. }
  159. dump_x(h, 16, 'T');
  160. memset(q, 0, 4*16);
  161. c=4;
  162. do{
  163. i=15;
  164. j=offset_table[c];
  165. hack_reg=hack_table[c];
  166. do{
  167. if(hack_reg&1){
  168. q[i]-= h[j&15];
  169. }else{
  170. q[i]+= h[j&15];
  171. }
  172. --j;
  173. hack_reg>>= 1;
  174. }while(i--!=0);
  175. }while(c--!=0);
  176. dump_x(q, 16, 'W');
  177. for(i=0; i<16; ++i){
  178. q[i] = s[i%5](q[i]);
  179. }
  180. for(i=0; i<16; ++i){
  181. ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
  182. }
  183. for(i=0; i<16; ++i){
  184. q[i] += h[(i+1)&0xf];
  185. }
  186. }
  187. #endif /* F0_HACK==2*/
  188. #if F0_HACK==1
  189. static
  190. uint8_t f0_lut[] PROGMEM = {
  191. 5<<1, ( 7<<1)+1, (10<<1)+0, (13<<1)+0, (14<<1)+0,
  192. 6<<1, ( 8<<1)+1, (11<<1)+0, (14<<1)+0, (15<<1)+1,
  193. 0<<1, ( 7<<1)+0, ( 9<<1)+0, (12<<1)+1, (15<<1)+0,
  194. 0<<1, ( 1<<1)+1, ( 8<<1)+0, (10<<1)+1, (13<<1)+0,
  195. 1<<1, ( 2<<1)+0, ( 9<<1)+0, (11<<1)+1, (14<<1)+1,
  196. 3<<1, ( 2<<1)+1, (10<<1)+0, (12<<1)+1, (15<<1)+0,
  197. 4<<1, ( 0<<1)+1, ( 3<<1)+1, (11<<1)+1, (13<<1)+0,
  198. 1<<1, ( 4<<1)+1, ( 5<<1)+1, (12<<1)+1, (14<<1)+1,
  199. 2<<1, ( 5<<1)+1, ( 6<<1)+1, (13<<1)+0, (15<<1)+1,
  200. 0<<1, ( 3<<1)+1, ( 6<<1)+0, ( 7<<1)+1, (14<<1)+0,
  201. 8<<1, ( 1<<1)+1, ( 4<<1)+1, ( 7<<1)+1, (15<<1)+0,
  202. 8<<1, ( 0<<1)+1, ( 2<<1)+1, ( 5<<1)+1, ( 9<<1)+0,
  203. 1<<1, ( 3<<1)+0, ( 6<<1)+1, ( 9<<1)+1, (10<<1)+0,
  204. 2<<1, ( 4<<1)+0, ( 7<<1)+0, (10<<1)+0, (11<<1)+0,
  205. 3<<1, ( 5<<1)+1, ( 8<<1)+0, (11<<1)+1, (12<<1)+1,
  206. 12<<1, ( 4<<1)+1, ( 6<<1)+1, ( 9<<1)+1, (13<<1)+0
  207. };
  208. static
  209. void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
  210. uint8_t i,j=-1,v,sign,l=0;
  211. uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
  212. bmw_small_s3, bmw_small_s4 };
  213. for(i=0; i<16; ++i){
  214. ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
  215. }
  216. dump_x(h, 16, 'T');
  217. // memset(q, 0, 4*16);
  218. for(i=0; i<5*16; ++i){
  219. v = pgm_read_byte(f0_lut+i);
  220. sign = v&1;
  221. v >>=1;
  222. if(i==l){
  223. j++;
  224. l+=5;
  225. q[j] = h[v];
  226. continue;
  227. }
  228. if(sign){
  229. q[j] -= h[v];
  230. }else{
  231. q[j] += h[v];
  232. }
  233. }
  234. dump_x(q, 16, 'W');
  235. for(i=0; i<16; ++i){
  236. q[i] = s[i%5](q[i]);
  237. }
  238. for(i=0; i<16; ++i){
  239. ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
  240. }
  241. for(i=0; i<16; ++i){
  242. q[i] += h[(i+1)&0xf];
  243. }
  244. }
  245. #endif /* F0_HACK==1 */
  246. #if F0_HACK==0
  247. static
  248. void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
  249. uint8_t i;
  250. for(i=0; i<16; ++i){
  251. ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
  252. }
  253. dump_x(h, 16, 'T');
  254. q[ 0] = (h[ 5] - h[ 7] + h[10] + h[13] + h[14]);
  255. q[ 1] = (h[ 6] - h[ 8] + h[11] + h[14] - h[15]);
  256. q[ 2] = (h[ 0] + h[ 7] + h[ 9] - h[12] + h[15]);
  257. q[ 3] = (h[ 0] - h[ 1] + h[ 8] - h[10] + h[13]);
  258. q[ 4] = (h[ 1] + h[ 2] + h[ 9] - h[11] - h[14]);
  259. q[ 5] = (h[ 3] - h[ 2] + h[10] - h[12] + h[15]);
  260. q[ 6] = (h[ 4] - h[ 0] - h[ 3] - h[11] + h[13]);
  261. q[ 7] = (h[ 1] - h[ 4] - h[ 5] - h[12] - h[14]);
  262. q[ 8] = (h[ 2] - h[ 5] - h[ 6] + h[13] - h[15]);
  263. q[ 9] = (h[ 0] - h[ 3] + h[ 6] - h[ 7] + h[14]);
  264. q[10] = (h[ 8] - h[ 1] - h[ 4] - h[ 7] + h[15]);
  265. q[11] = (h[ 8] - h[ 0] - h[ 2] - h[ 5] + h[ 9]);
  266. q[12] = (h[ 1] + h[ 3] - h[ 6] - h[ 9] + h[10]);
  267. q[13] = (h[ 2] + h[ 4] + h[ 7] + h[10] + h[11]);
  268. q[14] = (h[ 3] - h[ 5] + h[ 8] - h[11] - h[12]);
  269. q[15] = (h[12] - h[ 4] - h[ 6] - h[ 9] + h[13]);
  270. dump_x(q, 16, 'W');
  271. q[ 0] = S32_0(q[ 0]);
  272. q[ 1] = S32_1(q[ 1]);
  273. q[ 2] = S32_2(q[ 2]);
  274. q[ 3] = S32_3(q[ 3]);
  275. q[ 4] = S32_4(q[ 4]);
  276. q[ 5] = S32_0(q[ 5]);
  277. q[ 6] = S32_1(q[ 6]);
  278. q[ 7] = S32_2(q[ 7]);
  279. q[ 8] = S32_3(q[ 8]);
  280. q[ 9] = S32_4(q[ 9]);
  281. q[10] = S32_0(q[10]);
  282. q[11] = S32_1(q[11]);
  283. q[12] = S32_2(q[12]);
  284. q[13] = S32_3(q[13]);
  285. q[14] = S32_4(q[14]);
  286. q[15] = S32_0(q[15]);
  287. for(i=0; i<16; ++i){
  288. ((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
  289. }
  290. for(i=0; i<16; ++i){
  291. q[i] += h[(i+1)&0xf];
  292. }
  293. }
  294. #endif /* F0_HACK==0 */
  295. static
  296. void bmw_small_f1(uint32_t* q, const void* m, const void* h){
  297. uint8_t i;
  298. q[16] = bmw_small_expand1(0, q, m, h);
  299. q[17] = bmw_small_expand1(1, q, m, h);
  300. for(i=2; i<16; ++i){
  301. q[16+i] = bmw_small_expand2(i, q, m, h);
  302. }
  303. }
  304. static
  305. void bmw_small_f2(uint32_t* h, uint32_t* q, const void* m){
  306. uint32_t xl=0, xh;
  307. uint8_t i;
  308. for(i=16;i<24;++i){
  309. xl ^= q[i];
  310. }
  311. xh = xl;
  312. for(i=24;i<32;++i){
  313. xh ^= q[i];
  314. }
  315. #if DEBUG
  316. cli_putstr("\r\n XL = ");
  317. cli_hexdump_rev(&xl, 4);
  318. cli_putstr("\r\n XH = ");
  319. cli_hexdump_rev(&xh, 4);
  320. #endif
  321. memcpy(h, m, 16*4);
  322. h[0] ^= SHL32(xh, 5) ^ SHR32(q[16], 5);
  323. h[1] ^= SHR32(xh, 7) ^ SHL32(q[17], 8);
  324. h[2] ^= SHR32(xh, 5) ^ SHL32(q[18], 5);
  325. h[3] ^= SHR32(xh, 1) ^ SHL32(q[19], 5);
  326. h[4] ^= SHR32(xh, 3) ^ q[20];
  327. h[5] ^= SHL32(xh, 6) ^ SHR32(q[21], 6);
  328. h[6] ^= SHR32(xh, 4) ^ SHL32(q[22], 6);
  329. h[7] ^= SHR32(xh,11) ^ SHL32(q[23], 2);
  330. for(i=0; i<8; ++i){
  331. h[i] += xl ^ q[24+i] ^ q[i];
  332. }
  333. for(i=0; i<8; ++i){
  334. h[8+i] ^= xh ^ q[24+i];
  335. h[8+i] += ROTL32(h[(4+i)%8],i+9);
  336. }
  337. /*
  338. h[ 8] += SHL32(xl, 8) ^ q[23] ^ q[ 8];
  339. h[ 9] += SHR32(xl, 6) ^ q[16] ^ q[ 9];
  340. h[10] += SHL32(xl, 6) ^ q[17] ^ q[10];
  341. h[11] += SHL32(xl, 4) ^ q[18] ^ q[11];
  342. h[12] += SHR32(xl, 3) ^ q[19] ^ q[12];
  343. h[13] += SHR32(xl, 4) ^ q[20] ^ q[13];
  344. h[14] += SHR32(xl, 7) ^ q[21] ^ q[14];
  345. h[15] += SHR32(xl, 2) ^ q[22] ^ q[15];
  346. */
  347. memxor(q+9, q+16, 7*4);
  348. q[8] ^= q[23];
  349. h[ 8] += SHL32(xl, 8) ^ q[ 8];
  350. h[ 9] += SHR32(xl, 6) ^ q[ 9];
  351. h[10] += SHL32(xl, 6) ^ q[10];
  352. h[11] += SHL32(xl, 4) ^ q[11];
  353. h[12] += SHR32(xl, 3) ^ q[12];
  354. h[13] += SHR32(xl, 4) ^ q[13];
  355. h[14] += SHR32(xl, 7) ^ q[14];
  356. h[15] += SHR32(xl, 2) ^ q[15];
  357. }
  358. void bmw_small_nextBlock(bmw_small_ctx_t* ctx, const void* block){
  359. uint32_t q[32];
  360. dump_x(block, 16, 'M');
  361. bmw_small_f0(q, ctx->h, block);
  362. dump_x(q, 16, 'Q');
  363. bmw_small_f1(q, block, ctx->h);
  364. dump_x(q, 32, 'Q');
  365. bmw_small_f2(ctx->h, q, block);
  366. ctx->counter += 1;
  367. ctx_dump(ctx);
  368. }
  369. void bmw_small_lastBlock(bmw_small_ctx_t* ctx, const void* block, uint16_t length_b){
  370. union {
  371. uint8_t v8[64];
  372. uint32_t v32[16];
  373. uint64_t v64[ 8];
  374. } buffer;
  375. while(length_b >= BMW_SMALL_BLOCKSIZE){
  376. bmw_small_nextBlock(ctx, block);
  377. length_b -= BMW_SMALL_BLOCKSIZE;
  378. block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
  379. }
  380. memset(buffer.v8, 0, 64);
  381. memcpy(buffer.v8, block, (length_b+7)/8);
  382. buffer.v8[length_b>>3] |= 0x80 >> (length_b&0x07);
  383. if(length_b+1>64*8-64){
  384. bmw_small_nextBlock(ctx, buffer.v8);
  385. memset(buffer.v8, 0, 64-8);
  386. ctx->counter -= 1;
  387. }
  388. buffer.v64[7] = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
  389. bmw_small_nextBlock(ctx, buffer.v8);
  390. #if TWEAK
  391. uint8_t i;
  392. uint32_t q[32];
  393. memset(buffer.v8, 0xaa, 64);
  394. for(i=0; i<16;++i){
  395. buffer.v8[i*4] = i+0xa0;
  396. }
  397. // dump_x(buffer.v8, 16, 'A');
  398. dump_x(ctx->h, 16, 'M');
  399. bmw_small_f0(q, buffer.v32, ctx->h);
  400. dump_x(buffer.v8, 16, 'a');
  401. dump_x(q, 16, 'Q');
  402. bmw_small_f1(q, ctx->h, buffer.v32);
  403. dump_x(q, 32, 'Q');
  404. bmw_small_f2(buffer.v32, q, ctx->h);
  405. memcpy(ctx->h, buffer.v8, 64);
  406. #endif
  407. }
  408. void bmw224_init(bmw224_ctx_t* ctx){
  409. uint8_t i;
  410. ctx->h[0] = 0x00010203;
  411. for(i=1; i<16; ++i){
  412. ctx->h[i] = ctx->h[i-1]+ 0x04040404;
  413. }
  414. #if BUG24
  415. ctx->h[13] = 0x24353637;
  416. #endif
  417. ctx->counter=0;
  418. ctx_dump(ctx);
  419. }
  420. void bmw256_init(bmw256_ctx_t* ctx){
  421. uint8_t i;
  422. ctx->h[0] = 0x40414243;
  423. for(i=1; i<16; ++i){
  424. ctx->h[i] = ctx->h[i-1]+ 0x04040404;
  425. }
  426. ctx->counter=0;
  427. ctx_dump(ctx);
  428. }
  429. void bmw224_nextBlock(bmw224_ctx_t* ctx, const void* block){
  430. bmw_small_nextBlock(ctx, block);
  431. }
  432. void bmw256_nextBlock(bmw256_ctx_t* ctx, const void* block){
  433. bmw_small_nextBlock(ctx, block);
  434. }
  435. void bmw224_lastBlock(bmw224_ctx_t* ctx, const void* block, uint16_t length_b){
  436. bmw_small_lastBlock(ctx, block, length_b);
  437. }
  438. void bmw256_lastBlock(bmw256_ctx_t* ctx, const void* block, uint16_t length_b){
  439. bmw_small_lastBlock(ctx, block, length_b);
  440. }
  441. void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
  442. memcpy(dest, &(ctx->h[9]), 224/8);
  443. }
  444. void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
  445. memcpy(dest, &(ctx->h[8]), 256/8);
  446. }
  447. void bmw224(void* dest, const void* msg, uint32_t length_b){
  448. bmw_small_ctx_t ctx;
  449. bmw224_init(&ctx);
  450. while(length_b>=BMW_SMALL_BLOCKSIZE){
  451. bmw_small_nextBlock(&ctx, msg);
  452. length_b -= BMW_SMALL_BLOCKSIZE;
  453. msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
  454. }
  455. bmw_small_lastBlock(&ctx, msg, length_b);
  456. bmw224_ctx2hash(dest, &ctx);
  457. }
  458. void bmw256(void* dest, const void* msg, uint32_t length_b){
  459. bmw_small_ctx_t ctx;
  460. bmw256_init(&ctx);
  461. while(length_b>=BMW_SMALL_BLOCKSIZE){
  462. bmw_small_nextBlock(&ctx, msg);
  463. length_b -= BMW_SMALL_BLOCKSIZE;
  464. msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
  465. }
  466. bmw_small_lastBlock(&ctx, msg, length_b);
  467. bmw256_ctx2hash(dest, &ctx);
  468. }