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 /******************************************************************************/
121 param x: r22:r23:r24:25
128 ldi r30, lo8(s_table)
129 ldi r31, hi8(s_table)
165 /******************************************************************************/
167 param dest: r26:r27 (X)
168 param src: r30:r31 (Z)
184 /******************************************************************************/
193 /******************************************************************************/
225 /******************************************************************************/
239 .byte 5*4,7*4,10*4,13*4,14*4
240 ; .byte 0 ; just for alignment
248 /******************************************************************************/
251 .long 0x55555550, 0x5aaaaaa5, 0x5ffffffa, 0x6555554f
252 .long 0x6aaaaaa4, 0x6ffffff9, 0x7555554e, 0x7aaaaaa3
253 .long 0x7ffffff8, 0x8555554d, 0x8aaaaaa2, 0x8ffffff7
254 .long 0x9555554c, 0x9aaaaaa1, 0x9ffffff6, 0xa555554b
256 /*******************************************************************************
257 * uint32_t addelment(uint8_t j, const uint32_t* m, const uint32_t* h){
259 * r = pgm_read_dword(k_lut+j);
260 * r += rotl_addel(((uint32_t*)m)[j&0xf], j+0);
261 * r += rotl_addel(((uint32_t*)m)[(j+3)&0xf], j+3);
262 * r -= rotl_addel(((uint32_t*)m)[(j+10)&0xf], j+10);
263 * r ^= ((uint32_t*)h)[(j+7)&0xf];
343 ldi r30, lo8(const_lut)
344 ldi r31, hi8(const_lut)
353 rcall load_rotate_add_M
356 rcall load_rotate_add_M
360 rcall load_rotate_add_M
379 /******************************************************************************/
413 /******************************************************************************/
422 .byte 3,7,13,16,19,23,27,0
427 ldi r30, lo8(expand2_rot_table)
428 ldi r31, hi8(expand2_rot_table)
435 12: rcall add32_to_acc
453 /******************************************************************************/
459 /* for calling expand1/2
466 /******************************************************************************/
473 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
474 .byte 0x2B, 0x64, 0x66, 0x03, 0x51, 0x55, 0x87, 0x55
476 .byte (2<<1), (7<<1), (4<<1), (3<<1), (4<<1)+1, (6<<1)+1, (6<<1)
477 .byte 0 ; just for alignment
479 /******************************************************************************/
507 .global bmw_small_nextBlock
508 .global bmw224_nextBlock
509 .global bmw256_nextBlock
515 stack_alloc_large 32*4, r28, r29
517 ; push_range 28, 29 /* push Q */
518 ; push_range 22, 25 /* push M & H */
519 /* increment counter */
524 rcall load_acc_from_X
553 /* calculate W and store it in Q */
557 /* load initial index */
558 ldi r30, lo8(f0_indextable-1)
559 ldi r31, hi8(f0_indextable-1)
563 /* load values from hacktable */
564 ldi r30, lo8(f0_hacktable-2)
565 ldi r31, hi8(f0_hacktable-2)
612 ldi r30, lo8(f0_s_table)
613 ldi r31, hi8(f0_s_table)
707 10: rcall load32_from_X
729 ;--- /* calc first half of h0..h15 */
738 ldi r30, lo8(f2_1_shift_table-1)
739 ldi r31, hi8(f2_1_shift_table-1)
756 25: rcall shiftleft32
757 26: rcall eor32_to_acc
767 27: rcall shiftright32
768 28: rcall eor32_to_acc
780 sbiw r26, 4*8 /* X points to q[24] */
783 sbiw r28, 33 /* Y points to q[0] */
785 sbiw r30, 1 /* Z points to h0 */
796 sbiw r26, 9*4 /* X points to q[23] */
797 rcall load_acc_from_X
805 sbiw r26, 8*4 /* X points to q[16] */
809 ldi r30, lo8(f2_2_shift_table-1)
810 ldi r31, hi8(f2_2_shift_table-1)
814 rcall load_acc_from_X
821 20: rcall shiftright32
832 sbiw r30, 8*4 /* Z points to h8 */
834 sbiw r26, 4*4 /* X points to h4 */
857 stack_free_large3 32*4
862 /******************************************************************************/
881 /******************************************************************************/
888 .global bmw_small_lastBlock
889 .global bmw224_lastBlock
890 .global bmw256_lastBlock
894 /* while(length_b >= BMW_SMALL_BLOCKSIZE){
895 bmw_small_nextBlock(ctx, block);
896 length_b -= BMW_SMALL_BLOCKSIZE;
897 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
910 rcall bmw_small_nextBlock
925 /* memset(pctx.buffer, 0, 64);
926 memcpy(pctx.buffer, block, (length_b+7)/8);
927 pctx.buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
938 /* copy (#r24) bytes to stack buffer */
943 301: /* calculate the appended byte */
963 /* if(length_b+1>64*8-64){ ; = 64*7-1 = 447 max(length_b)=511
964 bmw_small_nextBlock(ctx, pctx.buffer);
965 memset(pctx.buffer, 0, 64-8);
975 rcall bmw_small_nextBlock
982 rcall load32_from_Z_stub
988 /* *((uint64_t*)&(pctx.buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
989 bmw_small_nextBlock(ctx, pctx.buffer);
992 rcall load32_from_Z_stub
1018 rcall bmw_small_nextBlock
1019 /* memset(pctx.buffer, 0xaa, 64);
1021 pctx.buffer[i*4] = i+0xa0;
1035 /* bmw_small_nextBlock((bmw_small_ctx_t*)&pctx, ctx->h);
1036 memcpy(ctx->h, pctx.buffer, 64);
1040 rcall bmw_small_nextBlock
1056 /*******************************************************************************
1057 * void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
1058 * memcpy(dest, &(ctx->h[9]), 224/8);
1061 * param dest: r24:r25
1062 * param ctx: r22:r23
1064 .global bmw224_ctx2hash
1072 /*******************************************************************************
1073 * void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
1074 * memcpy(dest, &(ctx->h[8]), 256/8);
1077 * param dest: r24:r25
1078 * param ctx: r22:r23
1080 .global bmw256_ctx2hash
1093 /*******************************************************************************
1094 * void bmw256(void* dest, const void* msg, uint32_t length_b){
1095 * bmw_small_ctx_t ctx;
1096 * bmw256_init(&ctx);
1097 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1098 * bmw_small_nextBlock(&ctx, msg);
1099 * length_b -= BMW_SMALL_BLOCKSIZE;
1100 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1102 * bmw_small_lastBlock(&ctx, msg, length_b);
1103 * bmw256_ctx2hash(dest, &ctx);
1106 * param dest: r24:r25
1107 * param msg: r22:r23
1108 * param length_b: r18:r21
1126 /*******************************************************************************
1127 * void bmw224(void* dest, const void* msg, uint32_t length_b){
1128 * bmw_small_ctx_t ctx;
1129 * bmw224_init(&ctx);
1130 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1131 * bmw_small_nextBlock(&ctx, msg);
1132 * length_b -= BMW_SMALL_BLOCKSIZE;
1133 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1135 * bmw_small_lastBlock(&ctx, msg, length_b);
1136 * bmw224_ctx2hash(dest, &ctx);
1139 * param dest: r24:r25
1140 * param msg: r22:r23
1141 * param length_b: r18:r21
1161 stack_alloc_large 64+4
1169 ldi r30, pm_lo8(init_lut)
1170 ldi r31, pm_hi8(init_lut)
1180 rcall bmw_small_nextBlock
1192 rcall bmw_small_lastBlock
1195 ldi r30, pm_lo8(c2h_lut)
1196 ldi r31, pm_hi8(c2h_lut)
1200 stack_free_large 64+4
1210 rjmp bmw224_ctx2hash
1211 rjmp bmw256_ctx2hash
1213 /*******************************************************************************
1214 * void bmw224_init(bmw224_ctx_t* ctx){
1216 * ctx->h[0] = 0x00010203;
1217 * for(i=1; i<16; ++i){
1218 * ctx->h[i] = ctx->h[i-1]+ 0x04040404;
1223 * param ctx: r24:r25
1266 /******************************************************************************/
1275 ldi r24, lo8(qdbg_str)
1276 ldi r25, hi8(qdbg_str)
1279 10: ldi r24, lo8(qdbg_str1)
1280 ldi r25, hi8(qdbg_str1)
1283 call cli_hexdump_byte
1284 ldi r24, lo8(qdbg_str2)
1285 ldi r25, hi8(qdbg_str2)
1290 call cli_hexdump_rev
1298 qdbg_str: .asciz "\r\nDBG Q: "
1299 qdbg_str1: .asciz "\r\n Q["
1300 qdbg_str2: .asciz "] = "
1311 ldi r24, lo8(Xdbg_str)
1312 ldi r25, hi8(Xdbg_str)
1319 10: ldi r24, lo8(Xdbg_str1)
1320 ldi r25, hi8(Xdbg_str1)
1327 call cli_hexdump_byte
1328 ldi r24, lo8(Xdbg_str2)
1329 ldi r25, hi8(Xdbg_str2)
1334 call cli_hexdump_rev
1344 Xdbg_str: .asciz "\r\nDBG "
1345 Xdbg_str1: .asciz "\r\n "
1346 Xdbg_str2: .asciz "] = "
1354 ldi r24, lo8(Xdbg_str)
1355 ldi r25, hi8(Xdbg_str)
1358 call cli_hexdump_byte
1360 call cli_hexdump_byte
1362 call cli_hexdump_byte
1364 call cli_hexdump_byte
1374 ldi r24, lo8(Xdbg_str)
1375 ldi r25, hi8(Xdbg_str)
1378 call cli_hexdump_byte
1380 call cli_hexdump_byte
1382 call cli_hexdump_byte
1384 call cli_hexdump_byte