1 /* bmw_small-tinyasm.S */
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/>.
21 * File: bmw_small-tinyasm.S
24 * License: GPLv3 or later
25 * Description: implementation of BlueMidnightWish
29 #include "avr-asm-macros.S"
31 /******************************************************************************/
33 param a: r22:r23:r24:r25
47 /******************************************************************************/
49 param a: r22:r23:r24:r25
72 /******************************************************************************/
74 param a: r22:r23:r24:r25
103 /******************************************************************************/
130 param x: r22:r23:r24:25
139 ldi r30, lo8(s_table)
140 ldi r31, hi8(s_table)
178 /******************************************************************************/
180 param dest: r26:r27 (X)
181 param src: r30:r31 (Z)
197 /******************************************************************************/
206 /******************************************************************************/
253 /******************************************************************************/
261 .byte 0x03, 0x11, 5*4
262 .byte 0xDD, 0xB3, 7*4
263 .byte 0x2A, 0x79, 10*4
264 .byte 0x07, 0xAA, 13*4
265 .byte 0x51, 0xC2, 14*4
266 .byte 0 ; just for alignment
269 /*******************************************************************************
270 * uint32_t addelment(uint8_t j, const uint32_t* m, const uint32_t* h){
272 * r = pgm_read_dword(k_lut+j);
273 * r += rotl_addel(((uint32_t*)m)[j&0xf], j+0);
274 * r += rotl_addel(((uint32_t*)m)[(j+3)&0xf], j+3);
275 * r -= rotl_addel(((uint32_t*)m)[(j+10)&0xf], j+10);
276 * r ^= ((uint32_t*)h)[(j+7)&0xf];
354 rcall load_acc_from_X
361 rcall store_acc_to_dec_X
365 rcall load_rotate_add_M
368 rcall load_rotate_add_M
372 rcall load_rotate_add_M
385 /******************************************************************************/
424 /******************************************************************************/
433 .byte 0x2B, 0x64, 0x66, 0x03, 0x51, 0x55, 0x87, 0x55
435 .byte (2<<1), (7<<1), (4<<1), (3<<1), (4<<1)+1, (6<<1)+1, (6<<1)
438 .byte 3,7,13,16,19,23,27
439 ; .byte 0 ; just for alignment
444 ldi r30, lo8(expand2_rot_table)
445 ldi r31, hi8(expand2_rot_table)
452 12: rcall add32_to_acc
461 rjmp store_acc_to_dec_X
464 /******************************************************************************/
470 /* for calling expand1/2
477 /******************************************************************************/
484 /******************************************************************************/
512 .global bmw_small_nextBlock
513 .global bmw224_nextBlock
514 .global bmw256_nextBlock
520 stack_alloc_large 32*4, r28, r29
529 ; push_range 28, 29 /* push Q */
530 ; push_range 22, 25 /* push M & H */
531 /* increment counter */
536 rcall load_acc_from_X
542 rcall store_acc_to_dec_X
562 /* calculate W and store it in Q */
566 /* load initial index */
568 /* load values from hacktable */
569 ldi r30, lo8(f0_hacktable-3)
570 ldi r31, hi8(f0_hacktable-3)
594 rcall load_acc_from_X
710 10: rcall load32_from_X
732 ;--- /* calc first half of h0..h15 */
747 15: ldi r30, lo8(f2_1_shift_table-9)
748 ldi r31, hi8(f2_1_shift_table-9)
763 25: rcall shiftleft32
764 26: rcall eor32_to_acc
774 27: rcall shiftright32
775 28: rcall eor32_to_acc
787 sbiw r26, 4*8 /* X points to q[24] */
790 sbiw r28, 33 /* Y points to q[0] */
792 sbiw r30, 1 /* Z points to h0 */
803 sbiw r26, 9*4 /* X points to q[23] */
804 rcall load_acc_from_X
812 sbiw r26, 8*4 /* X points to q[16] */
816 ldi r30, lo8(f2_2_shift_table-1)
817 ldi r31, hi8(f2_2_shift_table-1)
821 rcall load_acc_from_X
828 20: rcall shiftright32
839 sbiw r30, 8*4 /* Z points to h8 */
841 sbiw r26, 4*4 /* X points to h4 */
864 stack_free_large3 32*4+4
869 /******************************************************************************/
888 /******************************************************************************/
895 .global bmw_small_lastBlock
896 .global bmw224_lastBlock
897 .global bmw256_lastBlock
901 /* while(length_b >= BMW_SMALL_BLOCKSIZE){
902 bmw_small_nextBlock(ctx, block);
903 length_b -= BMW_SMALL_BLOCKSIZE;
904 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
917 rcall bmw_small_nextBlock
932 /* memset(pctx.buffer, 0, 64);
933 memcpy(pctx.buffer, block, (length_b+7)/8);
934 pctx.buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
944 /* copy (#r24) bytes to stack buffer */
949 301: /* calculate the appended byte */
969 /* if(length_b+1>64*8-64){ ; = 64*7-1 = 447 max(length_b)=511
970 bmw_small_nextBlock(ctx, pctx.buffer);
971 memset(pctx.buffer, 0, 64-8);
981 rcall bmw_small_nextBlock
988 rcall load32_from_Z_stub
994 /* *((uint64_t*)&(pctx.buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
995 bmw_small_nextBlock(ctx, pctx.buffer);
998 rcall load32_from_Z_stub
1024 rcall bmw_small_nextBlock
1025 /* memset(pctx.buffer, 0xaa, 64);
1027 pctx.buffer[i*4] = i+0xa0;
1040 /* bmw_small_nextBlock((bmw_small_ctx_t*)&pctx, ctx->h);
1041 memcpy(ctx->h, pctx.buffer, 64);
1045 rcall bmw_small_nextBlock
1061 /*******************************************************************************
1062 * void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
1063 * memcpy(dest, &(ctx->h[9]), 224/8);
1066 * param dest: r24:r25
1067 * param ctx: r22:r23
1069 .global bmw224_ctx2hash
1077 /*******************************************************************************
1078 * void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
1079 * memcpy(dest, &(ctx->h[8]), 256/8);
1082 * param dest: r24:r25
1083 * param ctx: r22:r23
1085 .global bmw256_ctx2hash
1098 /*******************************************************************************
1099 * void bmw256(void* dest, const void* msg, uint32_t length_b){
1100 * bmw_small_ctx_t ctx;
1101 * bmw256_init(&ctx);
1102 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1103 * bmw_small_nextBlock(&ctx, msg);
1104 * length_b -= BMW_SMALL_BLOCKSIZE;
1105 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1107 * bmw_small_lastBlock(&ctx, msg, length_b);
1108 * bmw256_ctx2hash(dest, &ctx);
1111 * param dest: r24:r25
1112 * param msg: r22:r23
1113 * param length_b: r18:r21
1131 /*******************************************************************************
1132 * void bmw224(void* dest, const void* msg, uint32_t length_b){
1133 * bmw_small_ctx_t ctx;
1134 * bmw224_init(&ctx);
1135 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1136 * bmw_small_nextBlock(&ctx, msg);
1137 * length_b -= BMW_SMALL_BLOCKSIZE;
1138 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1140 * bmw_small_lastBlock(&ctx, msg, length_b);
1141 * bmw224_ctx2hash(dest, &ctx);
1144 * param dest: r24:r25
1145 * param msg: r22:r23
1146 * param length_b: r18:r21
1166 stack_alloc_large 64+4
1174 ldi r30, pm_lo8(init_lut)
1175 ldi r31, pm_hi8(init_lut)
1185 rcall bmw_small_nextBlock
1197 rcall bmw_small_lastBlock
1200 ldi r30, pm_lo8(c2h_lut)
1201 ldi r31, pm_hi8(c2h_lut)
1205 stack_free_large 64+4
1215 rjmp bmw224_ctx2hash
1216 rjmp bmw256_ctx2hash
1218 /*******************************************************************************
1219 * void bmw224_init(bmw224_ctx_t* ctx){
1221 * ctx->h[0] = 0x00010203;
1222 * for(i=1; i<16; ++i){
1223 * ctx->h[i] = ctx->h[i-1]+ 0x04040404;
1228 * param ctx: r24:r25
1265 /******************************************************************************/
1274 ldi r24, lo8(qdbg_str)
1275 ldi r25, hi8(qdbg_str)
1278 10: ldi r24, lo8(qdbg_str1)
1279 ldi r25, hi8(qdbg_str1)
1282 call cli_hexdump_byte
1283 ldi r24, lo8(qdbg_str2)
1284 ldi r25, hi8(qdbg_str2)
1289 call cli_hexdump_rev
1297 qdbg_str: .asciz "\r\nDBG Q: "
1298 qdbg_str1: .asciz "\r\n Q["
1299 qdbg_str2: .asciz "] = "
1310 ldi r24, lo8(Xdbg_str)
1311 ldi r25, hi8(Xdbg_str)
1318 10: ldi r24, lo8(Xdbg_str1)
1319 ldi r25, hi8(Xdbg_str1)
1326 call cli_hexdump_byte
1327 ldi r24, lo8(Xdbg_str2)
1328 ldi r25, hi8(Xdbg_str2)
1333 call cli_hexdump_rev
1343 Xdbg_str: .asciz "\r\nDBG "
1344 Xdbg_str1: .asciz "\r\n "
1345 Xdbg_str2: .asciz "] = "
1353 ldi r24, lo8(Xdbg_str)
1354 ldi r25, hi8(Xdbg_str)
1357 call cli_hexdump_byte
1359 call cli_hexdump_byte
1361 call cli_hexdump_byte
1363 call cli_hexdump_byte
1373 ldi r24, lo8(Xdbg_str)
1374 ldi r25, hi8(Xdbg_str)
1377 call cli_hexdump_byte
1379 call cli_hexdump_byte
1381 call cli_hexdump_byte
1383 call cli_hexdump_byte