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)
166 /******************************************************************************/
168 param dest: r26:r27 (X)
169 param src: r30:r31 (Z)
184 /******************************************************************************/
229 /******************************************************************************/
254 /******************************************************************************/
268 .byte 5*4,7*4,10*4,13*4,14*4
269 ; .byte 0 ; just for alignment
306 /* calculate W and store it in Q */
310 /* load initial index */
311 ldi r30, lo8(f0_indextable-1)
312 ldi r31, hi8(f0_indextable-1)
316 /* load values from hacktable */
317 ldi r30, lo8(f0_hacktable-2)
318 ldi r31, hi8(f0_hacktable-2)
349 ldi r30, lo8(f0_s_table)
350 ldi r31, hi8(f0_s_table)
391 /******************************************************************************/
394 .long 0x55555550, 0x5aaaaaa5, 0x5ffffffa, 0x6555554f
395 .long 0x6aaaaaa4, 0x6ffffff9, 0x7555554e, 0x7aaaaaa3
396 .long 0x7ffffff8, 0x8555554d, 0x8aaaaaa2, 0x8ffffff7
397 .long 0x9555554c, 0x9aaaaaa1, 0x9ffffff6, 0xa555554b
399 /*******************************************************************************
400 * uint32_t addelment(uint8_t j, const uint32_t* m, const uint32_t* h){
402 * r = pgm_read_dword(k_lut+j);
403 * r += rotl_addel(((uint32_t*)m)[j&0xf], j+0);
404 * r += rotl_addel(((uint32_t*)m)[(j+3)&0xf], j+3);
405 * r -= rotl_addel(((uint32_t*)m)[(j+10)&0xf], j+10);
406 * r ^= ((uint32_t*)h)[(j+7)&0xf];
489 ldi r30, lo8(const_lut)
490 ldi r31, hi8(const_lut)
499 rcall load_rotate_add_M
502 rcall load_rotate_add_M
506 rcall load_rotate_add_M
525 /******************************************************************************/
564 /******************************************************************************/
573 .byte 0,3,0,7,0,13,0,16,0,19,0,23,0,27
578 ldi r30, lo8(expand2_rot_table)
579 ldi r31, hi8(expand2_rot_table)
599 /******************************************************************************/
605 /* for calling expand1/2
639 /******************************************************************************/
646 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
647 .byte 0x2B, 0x64, 0x66, 0x03, 0x51, 0x55, 0x87, 0x55
649 .byte (2<<1), (7<<1), (4<<1), (3<<1), (4<<1)+1, (6<<1)+1, (6<<1)
650 .byte 0 ; just for alignment
713 ;--- /* calc first half of h0..h15 */
724 ldi r30, lo8(f2_1_shift_table-1)
725 ldi r31, hi8(f2_1_shift_table-1)
742 25: rcall shiftleft32
743 26: rcall eor32_to_acc
753 27: rcall shiftright32
754 28: rcall eor32_to_acc
766 sbiw r26, 4*8 /* X points to q[24] */
769 sbiw r28, 33 /* Y points to q[0] */
771 sbiw r30, 1 /* Z points to h0 */
782 sbiw r26, 9*4 /* X points to q[23] */
783 rcall load_acc_from_X
791 sbiw r26, 8*4 /* X points to q[16] */
795 ldi r30, lo8(f2_2_shift_table-1)
796 ldi r31, hi8(f2_2_shift_table-1)
800 rcall load_acc_from_X
807 20: rcall shiftright32
818 sbiw r30, 8*4 /* Z points to h8 */
820 sbiw r26, 4*4 /* X points to h4 */
840 /******************************************************************************/
860 .global bmw_small_nextBlock
861 .global bmw224_nextBlock
862 .global bmw256_nextBlock
868 stack_alloc_large 32*4, r28, r29
870 ; push_range 28, 29 /* push Q */
871 ; push_range 22, 25 /* push M & H */
872 /* increment counter */
877 rcall load_acc_from_X
909 stack_free_large3 32*4
914 /******************************************************************************/
929 .global bmw_small_lastBlock
930 .global bmw224_lastBlock
931 .global bmw256_lastBlock
935 /* while(length_b >= BMW_SMALL_BLOCKSIZE){
936 bmw_small_nextBlock(ctx, block);
937 length_b -= BMW_SMALL_BLOCKSIZE;
938 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
951 rcall bmw_small_nextBlock
966 /* memset(pctx.buffer, 0, 64);
967 memcpy(pctx.buffer, block, (length_b+7)/8);
968 pctx.buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
979 /* copy (#r24) bytes to stack buffer */
984 301: /* calculate the appended byte */
1004 /* if(length_b+1>64*8-64){ ; = 64*7-1 = 447 max(length_b)=511
1005 bmw_small_nextBlock(ctx, pctx.buffer);
1006 memset(pctx.buffer, 0, 64-8);
1016 rcall bmw_small_nextBlock
1034 /* *((uint64_t*)&(pctx.buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
1035 bmw_small_nextBlock(ctx, pctx.buffer);
1069 rcall bmw_small_nextBlock
1070 /* memset(pctx.buffer, 0xaa, 64);
1072 pctx.buffer[i*4] = i+0xa0;
1086 /* bmw_small_nextBlock((bmw_small_ctx_t*)&pctx, ctx->h);
1087 memcpy(ctx->h, pctx.buffer, 64);
1091 rcall bmw_small_nextBlock
1107 /*******************************************************************************
1108 * void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
1109 * memcpy(dest, &(ctx->h[9]), 224/8);
1112 * param dest: r24:r25
1113 * param ctx: r22:r23
1115 .global bmw224_ctx2hash
1123 /*******************************************************************************
1124 * void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
1125 * memcpy(dest, &(ctx->h[8]), 256/8);
1128 * param dest: r24:r25
1129 * param ctx: r22:r23
1131 .global bmw256_ctx2hash
1144 /*******************************************************************************
1145 * void bmw256(void* dest, const void* msg, uint32_t length_b){
1146 * bmw_small_ctx_t ctx;
1147 * bmw256_init(&ctx);
1148 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1149 * bmw_small_nextBlock(&ctx, msg);
1150 * length_b -= BMW_SMALL_BLOCKSIZE;
1151 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1153 * bmw_small_lastBlock(&ctx, msg, length_b);
1154 * bmw256_ctx2hash(dest, &ctx);
1157 * param dest: r24:r25
1158 * param msg: r22:r23
1159 * param length_b: r18:r21
1177 /*******************************************************************************
1178 * void bmw224(void* dest, const void* msg, uint32_t length_b){
1179 * bmw_small_ctx_t ctx;
1180 * bmw224_init(&ctx);
1181 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1182 * bmw_small_nextBlock(&ctx, msg);
1183 * length_b -= BMW_SMALL_BLOCKSIZE;
1184 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1186 * bmw_small_lastBlock(&ctx, msg, length_b);
1187 * bmw224_ctx2hash(dest, &ctx);
1190 * param dest: r24:r25
1191 * param msg: r22:r23
1192 * param length_b: r18:r21
1211 stack_alloc_large 64+4
1219 ldi r30, pm_lo8(init_lut)
1220 ldi r31, pm_hi8(init_lut)
1230 rcall bmw_small_nextBlock
1243 rcall bmw_small_lastBlock
1246 ldi r30, pm_lo8(c2h_lut)
1247 ldi r31, pm_hi8(c2h_lut)
1251 stack_free_large 64+4
1260 rjmp bmw224_ctx2hash
1261 rjmp bmw256_ctx2hash
1263 /*******************************************************************************
1264 * void bmw224_init(bmw224_ctx_t* ctx){
1266 * ctx->h[0] = 0x00010203;
1267 * for(i=1; i<16; ++i){
1268 * ctx->h[i] = ctx->h[i-1]+ 0x04040404;
1273 * param ctx: r24:r25
1316 /******************************************************************************/
1325 ldi r24, lo8(qdbg_str)
1326 ldi r25, hi8(qdbg_str)
1329 10: ldi r24, lo8(qdbg_str1)
1330 ldi r25, hi8(qdbg_str1)
1333 call cli_hexdump_byte
1334 ldi r24, lo8(qdbg_str2)
1335 ldi r25, hi8(qdbg_str2)
1340 call cli_hexdump_rev
1348 qdbg_str: .asciz "\r\nDBG Q: "
1349 qdbg_str1: .asciz "\r\n Q["
1350 qdbg_str2: .asciz "] = "
1361 ldi r24, lo8(Xdbg_str)
1362 ldi r25, hi8(Xdbg_str)
1369 10: ldi r24, lo8(Xdbg_str1)
1370 ldi r25, hi8(Xdbg_str1)
1377 call cli_hexdump_byte
1378 ldi r24, lo8(Xdbg_str2)
1379 ldi r25, hi8(Xdbg_str2)
1384 call cli_hexdump_rev
1394 Xdbg_str: .asciz "\r\nDBG "
1395 Xdbg_str1: .asciz "\r\n "
1396 Xdbg_str2: .asciz "] = "
1404 ldi r24, lo8(Xdbg_str)
1405 ldi r25, hi8(Xdbg_str)
1408 call cli_hexdump_byte
1410 call cli_hexdump_byte
1412 call cli_hexdump_byte
1414 call cli_hexdump_byte
1424 ldi r24, lo8(Xdbg_str)
1425 ldi r25, hi8(Xdbg_str)
1428 call cli_hexdump_byte
1430 call cli_hexdump_byte
1432 call cli_hexdump_byte
1434 call cli_hexdump_byte