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"
36 /******************************************************************************/
38 param a: r22:r23:r24:r25
52 /******************************************************************************/
54 param a: r22:r23:r24:r25
77 /******************************************************************************/
79 param a: r22:r23:r24:r25
109 /******************************************************************************/
144 param x: r22:r23:r24:25
153 ldi r30, lo8(s_table)
154 ldi r31, hi8(s_table)
184 /******************************************************************************/
186 param dest: r26:r27 (X)
187 param src: r30:r31 (Z)
203 /******************************************************************************/
212 /******************************************************************************/
252 /******************************************************************************/
260 .byte 0x03, 0x11, 5*4
261 .byte 0xDD, 0xB3, 7*4
262 .byte 0x2A, 0x79, 10*4
263 .byte 0x07, 0xAA, 13*4
264 .byte 0x51, 0xC2, 14*4
265 .byte 0 ; just for alignment
268 /*******************************************************************************
269 * uint32_t addelment(uint8_t j, const uint32_t* m, const uint32_t* h){
271 * r = pgm_read_dword(k_lut+j);
272 * r += rotl_addel(((uint32_t*)m)[j&0xf], j+0);
273 * r += rotl_addel(((uint32_t*)m)[(j+3)&0xf], j+3);
274 * r -= rotl_addel(((uint32_t*)m)[(j+10)&0xf], j+10);
275 * r ^= ((uint32_t*)h)[(j+7)&0xf];
340 rcall load_acc_from_X
347 rcall store_acc_to_dec_X
350 rcall load_rotate_add_M
352 rcall load_rotate_add_M
355 rcall load_rotate_add_M
367 /******************************************************************************/
410 /******************************************************************************/
419 .byte 0x2B, 0x64, 0x66, 0x03, 0x51, 0x55, 0x87, 0x55
421 .byte (2<<1), (7<<1), (4<<1), (3<<1), (4<<1)+1, (6<<1)+1, (6<<1)
424 .byte 3,7,13,16,19,23,27
425 ; .byte 0 ; just for alignment
430 ldi r30, lo8(expand2_rot_table)
431 ldi r31, hi8(expand2_rot_table)
438 12: rcall add32_to_acc
454 /******************************************************************************/
460 /* for calling expand1/2
467 /******************************************************************************/
474 /******************************************************************************/
502 .global bmw_small_nextBlock
503 .global bmw224_nextBlock
504 .global bmw256_nextBlock
510 stack_alloc_large 32*4, r28, r29
519 ; push_range 28, 29 /* push Q */
520 ; push_range 22, 25 /* push M & H */
521 /* increment counter */
526 rcall load_acc_from_X
532 rcall store_acc_to_dec_X
552 /* calculate W and store it in Q */
556 /* load initial index */
558 /* load values from hacktable */
559 ldi r30, lo8(f0_hacktable-3)
560 ldi r31, hi8(f0_hacktable-3)
584 rcall load_acc_from_X
700 10: rcall load32_from_X
722 ;--- /* calc first half of h0..h15 */
737 15: ldi r30, lo8(f2_1_shift_table-9)
738 ldi r31, hi8(f2_1_shift_table-9)
753 25: rcall shiftleft32
754 26: rcall eor32_to_acc
764 27: rcall shiftright32
765 28: rcall eor32_to_acc
777 sbiw r26, 4*8 /* X points to q[24] */
780 sbiw r28, 33 /* Y points to q[0] */
782 sbiw r30, 1 /* Z points to h0 */
793 sbiw r26, 9*4 /* X points to q[23] */
794 rcall load_acc_from_X
802 sbiw r26, 8*4 /* X points to q[16] */
806 ldi r30, lo8(f2_2_shift_table-1)
807 ldi r31, hi8(f2_2_shift_table-1)
811 rcall load_acc_from_X
818 20: rcall shiftright32
829 sbiw r30, 8*4 /* Z points to h8 */
831 sbiw r26, 4*4 /* X points to h4 */
854 stack_free_large3 32*4+4
859 /******************************************************************************/
878 /******************************************************************************/
885 .global bmw_small_lastBlock
886 .global bmw224_lastBlock
887 .global bmw256_lastBlock
891 /* while(length_b >= BMW_SMALL_BLOCKSIZE){
892 bmw_small_nextBlock(ctx, block);
893 length_b -= BMW_SMALL_BLOCKSIZE;
894 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
907 rcall bmw_small_nextBlock
922 /* memset(pctx.buffer, 0, 64);
923 memcpy(pctx.buffer, block, (length_b+7)/8);
924 pctx.buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
934 /* copy (#r24) bytes to stack buffer */
939 301: /* calculate the appended byte */
959 /* if(length_b+1>64*8-64){ ; = 64*7-1 = 447 max(length_b)=511
960 bmw_small_nextBlock(ctx, pctx.buffer);
961 memset(pctx.buffer, 0, 64-8);
971 rcall bmw_small_nextBlock
978 rcall load32_from_Z_stub
984 /* *((uint64_t*)&(pctx.buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
985 bmw_small_nextBlock(ctx, pctx.buffer);
988 rcall load32_from_Z_stub
1012 rcall bmw_small_nextBlock
1013 /* memset(pctx.buffer, 0xaa, 64);
1015 pctx.buffer[i*4] = i+0xa0;
1028 /* bmw_small_nextBlock((bmw_small_ctx_t*)&pctx, ctx->h);
1029 memcpy(ctx->h, pctx.buffer, 64);
1033 rcall bmw_small_nextBlock
1049 /*******************************************************************************
1050 * void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
1051 * memcpy(dest, &(ctx->h[9]), 224/8);
1054 * param dest: r24:r25
1055 * param ctx: r22:r23
1057 .global bmw224_ctx2hash
1064 /*******************************************************************************
1065 * void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
1066 * memcpy(dest, &(ctx->h[8]), 256/8);
1069 * param dest: r24:r25
1070 * param ctx: r22:r23
1072 .global bmw256_ctx2hash
1084 /*******************************************************************************
1085 * void bmw256(void* dest, const void* msg, uint32_t length_b){
1086 * bmw_small_ctx_t ctx;
1087 * bmw256_init(&ctx);
1088 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1089 * bmw_small_nextBlock(&ctx, msg);
1090 * length_b -= BMW_SMALL_BLOCKSIZE;
1091 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1093 * bmw_small_lastBlock(&ctx, msg, length_b);
1094 * bmw256_ctx2hash(dest, &ctx);
1097 * param dest: r24:r25
1098 * param msg: r22:r23
1099 * param length_b: r18:r21
1117 /*******************************************************************************
1118 * void bmw224(void* dest, const void* msg, uint32_t length_b){
1119 * bmw_small_ctx_t ctx;
1120 * bmw224_init(&ctx);
1121 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1122 * bmw_small_nextBlock(&ctx, msg);
1123 * length_b -= BMW_SMALL_BLOCKSIZE;
1124 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1126 * bmw_small_lastBlock(&ctx, msg, length_b);
1127 * bmw224_ctx2hash(dest, &ctx);
1130 * param dest: r24:r25
1131 * param msg: r22:r23
1132 * param length_b: r18:r21
1152 stack_alloc_large 64+4
1160 ldi r30, pm_lo8(init_lut)
1161 ldi r31, pm_hi8(init_lut)
1171 rcall bmw_small_nextBlock
1183 rcall bmw_small_lastBlock
1186 ldi r30, pm_lo8(c2h_lut)
1187 ldi r31, pm_hi8(c2h_lut)
1191 stack_free_large 64+4
1201 rjmp bmw224_ctx2hash
1202 rjmp bmw256_ctx2hash
1204 /*******************************************************************************
1205 * void bmw224_init(bmw224_ctx_t* ctx){
1207 * ctx->h[0] = 0x00010203;
1208 * for(i=1; i<16; ++i){
1209 * ctx->h[i] = ctx->h[i-1]+ 0x04040404;
1214 * param ctx: r24:r25
1245 /******************************************************************************/
1254 ldi r24, lo8(qdbg_str)
1255 ldi r25, hi8(qdbg_str)
1258 10: ldi r24, lo8(qdbg_str1)
1259 ldi r25, hi8(qdbg_str1)
1262 call cli_hexdump_byte
1263 ldi r24, lo8(qdbg_str2)
1264 ldi r25, hi8(qdbg_str2)
1269 call cli_hexdump_rev
1277 qdbg_str: .asciz "\r\nDBG Q: "
1278 qdbg_str1: .asciz "\r\n Q["
1279 qdbg_str2: .asciz "] = "
1290 ldi r24, lo8(Xdbg_str)
1291 ldi r25, hi8(Xdbg_str)
1298 10: ldi r24, lo8(Xdbg_str1)
1299 ldi r25, hi8(Xdbg_str1)
1306 call cli_hexdump_byte
1307 ldi r24, lo8(Xdbg_str2)
1308 ldi r25, hi8(Xdbg_str2)
1313 call cli_hexdump_rev
1323 Xdbg_str: .asciz "\r\nDBG "
1324 Xdbg_str1: .asciz "\r\n "
1325 Xdbg_str2: .asciz "] = "
1333 ldi r24, lo8(Xdbg_str)
1334 ldi r25, hi8(Xdbg_str)
1337 call cli_hexdump_byte
1339 call cli_hexdump_byte
1341 call cli_hexdump_byte
1343 call cli_hexdump_byte
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