3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
22 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
30 #include "bmw_large.h"
32 #define SHL64(a,n) ((a)<<(n))
33 #define SHR64(a,n) ((a)>>(n))
34 #define ROTL64(a,n) (((a)<<(n))|((a)>>(64-(n))))
35 #define ROTR64(a,n) (((a)>>(n))|((a)<<(64-(n))))
43 void ctx_dump(const bmw_large_ctx_t* ctx){
45 cli_putstr_P(PSTR("\r\n==== ctx dump ===="));
47 cli_putstr_P(PSTR("\r\n h["));
49 cli_putstr_P(PSTR("] = "));
50 cli_hexdump_rev(&(ctx->h[i]), 8);
52 cli_putstr_P(PSTR("\r\n counter = "));
53 cli_hexdump(&(ctx->counter), 4);
56 void dump_x(const uint64_t* q, uint8_t elements, char x){
58 cli_putstr_P(PSTR("\r\n==== "));
60 cli_putstr_P(PSTR(" dump ===="));
61 for(i=0; i<elements;++i){
62 cli_putstr_P(PSTR("\r\n "));
64 cli_putstr_P(PSTR("["));
66 cli_putstr_P(PSTR("] = "));
67 cli_hexdump_rev(&(q[i]), 8);
75 uint64_t bmw_large_s0(uint64_t x){
84 uint64_t bmw_large_s1(uint64_t x){
93 uint64_t bmw_large_s2(uint64_t x){
102 uint64_t bmw_large_s3(uint64_t x){
111 uint64_t bmw_large_s4(uint64_t x){
118 uint64_t bmw_large_s5(uint64_t x){
125 uint64_t bmw_large_r1(uint64_t x){
131 uint64_t bmw_large_r2(uint64_t x){
137 uint64_t bmw_large_r3(uint64_t x){
143 uint64_t bmw_large_r4(uint64_t x){
149 uint64_t bmw_large_r5(uint64_t x){
151 r = ROTR64(x, 64-37);
155 uint64_t bmw_large_r6(uint64_t x){
157 r = ROTR64(x, 64-43);
161 uint64_t bmw_large_r7(uint64_t x){
163 r = ROTR64(x, 64-53);
167 uint64_t bmw_large_expand1(uint8_t j, const uint64_t* q, const void* m){
168 uint64_t(*s[])(uint64_t) = {bmw_large_s1, bmw_large_s2, bmw_large_s3, bmw_large_s0};
171 r = 0x0555555555555555LL*(j+16);
175 r += ((uint64_t*)m)[j];
176 r += ((uint64_t*)m)[j+3];
177 r -= ((uint64_t*)m)[j+10];
181 uint64_t bmw_large_expand2(uint8_t j, const uint64_t* q, const void* m){
182 uint64_t(*rf[])(uint64_t) = {bmw_large_r1, bmw_large_r2, bmw_large_r3,
183 bmw_large_r4, bmw_large_r5, bmw_large_r6,
187 r = 0x0555555555555555LL*(j+16);
188 for(i=0; i<14; i+=2){
191 for(i=0; i<14; i+=2){
192 r += rf[i/2](q[j+i+1]);
194 r += bmw_large_s5(q[j+14]);
195 r += bmw_large_s4(q[j+15]);
196 r += ((uint64_t*)m)[j];
197 r += ((uint64_t*)m)[(j+3)%16];
198 r -= ((uint64_t*)m)[(j+10)%16];
202 void bmw_large_f0(uint64_t* q, const uint64_t* h, const void* m){
205 uint64_t(*s[])(uint64_t)={ bmw_large_s0, bmw_large_s1, bmw_large_s2,
206 bmw_large_s3, bmw_large_s4 };
208 t[i] = h[i] ^ ((uint64_t*)m)[i];
211 q[ 0] = (t[ 5] - t[ 7] + t[10] + t[13] + t[14]);
212 q[ 1] = (t[ 6] - t[ 8] + t[11] + t[14] - t[15]);
213 q[ 2] = (t[ 0] + t[ 7] + t[ 9] - t[12] + t[15]);
214 q[ 3] = (t[ 0] - t[ 1] + t[ 8] - t[10] + t[13]);
215 q[ 4] = (t[ 1] + t[ 2] + t[ 9] - t[11] - t[14]);
216 q[ 5] = (t[ 3] - t[ 2] + t[10] - t[12] + t[15]);
217 q[ 6] = (t[ 4] - t[ 0] - t[ 3] - t[11] + t[13]);
218 q[ 7] = (t[ 1] - t[ 4] - t[ 5] - t[12] - t[14]);
219 q[ 8] = (t[ 2] - t[ 5] - t[ 6] + t[13] - t[15]);
220 q[ 9] = (t[ 0] - t[ 3] + t[ 6] - t[ 7] + t[14]);
221 q[10] = (t[ 8] - t[ 1] - t[ 4] - t[ 7] + t[15]);
222 q[11] = (t[ 8] - t[ 0] - t[ 2] - t[ 5] + t[ 9]);
223 q[12] = (t[ 1] + t[ 3] - t[ 6] - t[ 9] + t[10]);
224 q[13] = (t[ 2] + t[ 4] + t[ 7] + t[10] + t[11]);
225 q[14] = (t[ 3] - t[ 5] + t[ 8] - t[11] - t[12]);
226 q[15] = (t[12] - t[ 4] - t[ 6] - t[ 9] + t[13]);
233 void bmw_large_f1(uint64_t* q, const void* m){
235 q[16] = bmw_large_expand1(0, q, m);
236 q[17] = bmw_large_expand1(1, q, m);
238 q[16+i] = bmw_large_expand2(i, q, m);
242 void bmw_large_f2(uint64_t* h, const uint64_t* q, const void* m){
253 cli_putstr_P(PSTR("\r\n XL = "));
254 cli_hexdump_rev(&xl, 4);
255 cli_putstr_P(PSTR("\r\n XH = "));
256 cli_hexdump_rev(&xh, 4);
259 h[0] ^= SHL64(xh, 5) ^ SHR64(q[16], 5);
260 h[1] ^= SHR64(xh, 7) ^ SHL64(q[17], 8);
261 h[2] ^= SHR64(xh, 5) ^ SHL64(q[18], 5);
262 h[3] ^= SHR64(xh, 1) ^ SHL64(q[19], 5);
263 h[4] ^= SHR64(xh, 3) ^ q[20];
264 h[5] ^= SHL64(xh, 6) ^ SHR64(q[21], 6);
265 h[6] ^= SHR64(xh, 4) ^ SHL64(q[22], 6);
266 h[7] ^= SHR64(xh,11) ^ SHL64(q[23], 2);
268 h[i] += xl ^ q[24+i] ^ q[i];
271 h[8+i] ^= xh ^ q[24+i];
272 h[8+i] += ROTL64(h[(4+i)%8],i+9);
274 h[ 8] += SHL64(xl, 8) ^ q[23] ^ q[ 8];
275 h[ 9] += SHR64(xl, 6) ^ q[16] ^ q[ 9];
276 h[10] += SHL64(xl, 6) ^ q[17] ^ q[10];
277 h[11] += SHL64(xl, 4) ^ q[18] ^ q[11];
278 h[12] += SHR64(xl, 3) ^ q[19] ^ q[12];
279 h[13] += SHR64(xl, 4) ^ q[20] ^ q[13];
280 h[14] += SHR64(xl, 7) ^ q[21] ^ q[14];
281 h[15] += SHR64(xl, 2) ^ q[22] ^ q[15];
284 void bmw_large_nextBlock(bmw_large_ctx_t* ctx, const void* block){
286 dump_x(block, 16, 'M');
287 bmw_large_f0(q, ctx->h, block);
289 bmw_large_f1(q, block);
291 bmw_large_f2(ctx->h, q, block);
296 void bmw_large_lastBlock(bmw_large_ctx_t* ctx, const void* block, uint16_t length_b){
298 while(length_b >= BMW_LARGE_BLOCKSIZE){
299 bmw_large_nextBlock(ctx, block);
300 length_b -= BMW_LARGE_BLOCKSIZE;
301 block = (uint8_t*)block + BMW_LARGE_BLOCKSIZE_B;
303 memset(buffer, 0, 128);
304 memcpy(buffer, block, (length_b+7)/8);
305 buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
306 if(length_b+1>128*8-64){
307 bmw_large_nextBlock(ctx, buffer);
308 memset(buffer, 0, 128-8);
311 *((uint64_t*)&(buffer[128-8])) = (uint64_t)(ctx->counter*1024LL)+(uint64_t)length_b;
312 bmw_large_nextBlock(ctx, buffer);
315 void bmw384_init(bmw384_ctx_t* ctx){
317 ctx->h[0] = 0x0001020304050607LL;
319 ctx->h[i] = ctx->h[i-1]+ 0x0808080808080808LL;
322 ctx->h[6] = 0x3031323324353637LL;
328 void bmw512_init(bmw512_ctx_t* ctx){
330 ctx->h[0] = 0x8081828384858687LL;
332 ctx->h[i] = ctx->h[i-1]+ 0x0808080808080808LL;
338 void bmw384_nextBlock(bmw384_ctx_t* ctx, const void* block){
339 bmw_large_nextBlock(ctx, block);
342 void bmw512_nextBlock(bmw512_ctx_t* ctx, const void* block){
343 bmw_large_nextBlock(ctx, block);
346 void bmw384_lastBlock(bmw384_ctx_t* ctx, const void* block, uint16_t length_b){
347 bmw_large_lastBlock(ctx, block, length_b);
350 void bmw512_lastBlock(bmw512_ctx_t* ctx, const void* block, uint16_t length_b){
351 bmw_large_lastBlock(ctx, block, length_b);
354 void bmw384_ctx2hash(void* dest, const bmw384_ctx_t* ctx){
355 memcpy(dest, &(ctx->h[10]), 384/8);
358 void bmw512_ctx2hash(void* dest, const bmw512_ctx_t* ctx){
359 memcpy(dest, &(ctx->h[8]), 512/8);
362 void bmw384(void* dest, const void* msg, uint32_t length_b){
365 while(length_b>=BMW_LARGE_BLOCKSIZE){
366 bmw_large_nextBlock(&ctx, msg);
367 length_b -= BMW_LARGE_BLOCKSIZE;
368 msg = (uint8_t*)msg + BMW_LARGE_BLOCKSIZE_B;
370 bmw_large_lastBlock(&ctx, msg, length_b);
371 bmw384_ctx2hash(dest, &ctx);
374 void bmw512(void* dest, const void* msg, uint32_t length_b){
377 while(length_b>=BMW_LARGE_BLOCKSIZE){
378 bmw_large_nextBlock(&ctx, msg);
379 length_b -= BMW_LARGE_BLOCKSIZE;
380 msg = (uint8_t*)msg + BMW_LARGE_BLOCKSIZE_B;
382 bmw_large_lastBlock(&ctx, msg, length_b);
383 bmw512_ctx2hash(dest, &ctx);