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"
38 /******************************************************************************/
40 param a: r22:r23:r24:r25
54 /******************************************************************************/
56 param a: r22:r23:r24:r25
79 /******************************************************************************/
81 param a: r22:r23:r24:r25
111 /******************************************************************************/
139 param x: r22:r23:r24:25
148 ldi r30, lo8(s_table)
149 ldi r31, hi8(s_table)
177 /******************************************************************************/
179 param dest: r26:r27 (X)
180 param src: r30:r31 (Z)
197 /******************************************************************************/
206 /******************************************************************************/
251 /******************************************************************************/
259 ; .byte 0x2B, 0x64, 0x66, 0x03, 0x51, 0x55, 0x87, 0x55
260 .byte 0x55, 0x87, 0x55, 0x51, 0x03, 0x66, 0x64, 0x2B
262 ; .byte (2<<1), (7<<1), (4<<1), (3<<1), (4<<1)+1, (6<<1)+1, (6<<1)
263 .byte (8<<1)+1, (6<<1), (6<<1)+1, (4<<1)+1, (3<<1), (4<<1), (7<<1), (2<<1)
265 .byte 3,7,13,16,19,23,27
268 .byte 0x03, 0x11, 5*4
269 .byte 0xDD, 0xB3, 7*4
270 .byte 0x2A, 0x79, 10*4
271 .byte 0x07, 0xAA, 13*4
272 .byte 0x51, 0xC2, 14*4
275 /*******************************************************************************
276 * uint32_t addelment(uint8_t j, const uint32_t* m, const uint32_t* h){
278 * r = pgm_read_dword(k_lut+j);
279 * r += rotl_addel(((uint32_t*)m)[j&0xf], j+0);
280 * r += rotl_addel(((uint32_t*)m)[(j+3)&0xf], j+3);
281 * r -= rotl_addel(((uint32_t*)m)[(j+10)&0xf], j+10);
282 * r ^= ((uint32_t*)h)[(j+7)&0xf];
345 /******************************************************************************/
371 rcall load_acc_from_X
378 rcall store_acc_to_dec_X
381 rcall load_rotate_add_M
383 rcall load_rotate_add_M
386 rcall load_rotate_add_M
417 /******************************************************************************/
429 ldi r30, lo8(expand2_rot_table)
430 ldi r31, hi8(expand2_rot_table)
437 12: rcall add32_to_acc
453 /******************************************************************************/
459 /* for calling expand1/2
466 /******************************************************************************/
473 /******************************************************************************/
509 bmw_small_nextBlock_early:
512 .global bmw224_nextBlock
519 stack_alloc_large 32*4, r28, r29
528 ; push_range 28, 29 /* push Q */
529 ; push_range 22, 25 /* push M & H */
530 /* increment counter */
535 rcall load_acc_from_X
541 rcall store_acc_to_dec_X
561 /* calculate W and store it in Q */
565 /* load initial index */
567 /* load values from hacktable */
568 ldi r30, lo8(f0_hacktable-3)
569 ldi r31, hi8(f0_hacktable-3)
593 rcall load_acc_from_X
704 rcall load_acc_from_X
706 10: rcall load32_from_X
727 /* copy m(Y) into h */
735 ;--- /* calc first half of h0..h15 */
738 ldi r30, lo8(f2_1_shift_table)
739 ldi r31, hi8(f2_1_shift_table)
761 25: rcall shiftleft32
762 26: rcall mov32_to_acc
772 27: rcall shiftright32
773 28: rcall eor32_to_acc
777 rcall store_acc_to_dec_X
783 sbiw r28, 4*8 /* Y points to q[24] */
786 sbiw r28, 33 /* Y points to q[0] */
789 /* xor q[24..31] into q[0..7] */
791 /* xor q[23] into q[8] */
795 /* xor q[16..22] into q[9..15] */
802 ldi r30, lo8(f2_2_shift_table)
803 ldi r31, hi8(f2_2_shift_table)
823 sbiw r26, 8*4 /* X points to h8 */
825 sbiw r28, 4*4 /* Y points to h4 */
848 stack_free_large3 32*4+4
860 /******************************************************************************/
879 /******************************************************************************/
886 .global bmw224_lastBlock
890 /* while(length_b >= BMW_SMALL_BLOCKSIZE){
891 bmw_small_nextBlock(ctx, block);
892 length_b -= BMW_SMALL_BLOCKSIZE;
893 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
904 rcall bmw_small_nextBlock_early
919 /* memset(pctx.buffer, 0, 64);
920 memcpy(pctx.buffer, block, (length_b+7)/8);
921 pctx.buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
931 /* copy (#r24) bytes to stack buffer */
936 301: /* calculate the appended byte */
956 /* if(length_b+1>64*8-64){ ; = 64*7-1 = 447 max(length_b)=511
957 bmw_small_nextBlock(ctx, pctx.buffer);
958 memset(pctx.buffer, 0, 64-8);
967 rcall bmw_small_nextBlock_early
974 rcall load32_from_Z_stub
980 /* *((uint64_t*)&(pctx.buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
981 bmw_small_nextBlock(ctx, pctx.buffer);
984 rcall load32_from_Z_stub
1004 rcall bmw_small_nextBlock_early
1005 /* memset(pctx.buffer, 0xaa, 64);
1007 pctx.buffer[i*4] = i+0xa0;
1020 /* bmw_small_nextBlock((bmw_small_ctx_t*)&pctx, ctx->h);
1021 memcpy(ctx->h, pctx.buffer, 64);
1025 rcall bmw_small_nextBlock
1039 /*******************************************************************************
1040 * void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
1041 * memcpy(dest, &(ctx->h[9]), 224/8);
1044 * param dest: r24:r25
1045 * param ctx: r22:r23
1047 .global bmw224_ctx2hash
1060 /*******************************************************************************
1061 * void bmw224(void* dest, const void* msg, uint32_t length_b){
1062 * bmw_small_ctx_t ctx;
1063 * bmw224_init(&ctx);
1064 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1065 * bmw_small_nextBlock(&ctx, msg);
1066 * length_b -= BMW_SMALL_BLOCKSIZE;
1067 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1069 * bmw_small_lastBlock(&ctx, msg, length_b);
1070 * bmw224_ctx2hash(dest, &ctx);
1073 * param dest: r24:r25
1074 * param msg: r22:r23
1075 * param length_b: r18:r21
1092 stack_alloc_large 64+4
1104 rcall bmw_small_nextBlock_early
1116 rcall bmw_small_lastBlock
1119 rcall bmw224_ctx2hash
1120 stack_free_large 64+4
1123 /*******************************************************************************
1124 * void bmw224_init(bmw224_ctx_t* ctx){
1126 * ctx->h[0] = 0x00010203;
1127 * for(i=1; i<16; ++i){
1128 * ctx->h[i] = ctx->h[i-1]+ 0x04040404;
1133 * param ctx: r24:r25
1157 /******************************************************************************/
1166 ldi r24, lo8(qdbg_str)
1167 ldi r25, hi8(qdbg_str)
1170 10: ldi r24, lo8(qdbg_str1)
1171 ldi r25, hi8(qdbg_str1)
1174 call cli_hexdump_byte
1175 ldi r24, lo8(qdbg_str2)
1176 ldi r25, hi8(qdbg_str2)
1181 call cli_hexdump_rev
1189 qdbg_str: .asciz "\r\nDBG Q: "
1190 qdbg_str1: .asciz "\r\n Q["
1191 qdbg_str2: .asciz "] = "
1202 ldi r24, lo8(Xdbg_str)
1203 ldi r25, hi8(Xdbg_str)
1210 10: ldi r24, lo8(Xdbg_str1)
1211 ldi r25, hi8(Xdbg_str1)
1218 call cli_hexdump_byte
1219 ldi r24, lo8(Xdbg_str2)
1220 ldi r25, hi8(Xdbg_str2)
1225 call cli_hexdump_rev
1235 Xdbg_str: .asciz "\r\nDBG "
1236 Xdbg_str1: .asciz "\r\n "
1237 Xdbg_str2: .asciz "] = "
1245 ldi r24, lo8(Xdbg_str)
1246 ldi r25, hi8(Xdbg_str)
1249 call cli_hexdump_byte
1251 call cli_hexdump_byte
1253 call cli_hexdump_byte
1255 call cli_hexdump_byte
1265 ldi r24, lo8(Xdbg_str)
1266 ldi r25, hi8(Xdbg_str)
1269 call cli_hexdump_byte
1271 call cli_hexdump_byte
1273 call cli_hexdump_byte
1275 call cli_hexdump_byte