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 bmw_small_nextBlock
513 .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
705 rcall load_acc_from_X
707 10: rcall load32_from_X
728 /* copy m(Y) into h */
736 ;--- /* calc first half of h0..h15 */
739 ldi r30, lo8(f2_1_shift_table)
740 ldi r31, hi8(f2_1_shift_table)
762 25: rcall shiftleft32
763 26: rcall mov32_to_acc
773 27: rcall shiftright32
774 28: rcall eor32_to_acc
778 rcall store_acc_to_dec_X
784 sbiw r28, 4*8 /* Y points to q[24] */
787 sbiw r28, 33 /* Y points to q[0] */
790 /* xor q[24..31] into q[0..7] */
792 /* xor q[23] into q[8] */
796 /* xor q[16..22] into q[9..15] */
803 ldi r30, lo8(f2_2_shift_table)
804 ldi r31, hi8(f2_2_shift_table)
824 sbiw r26, 8*4 /* X points to h8 */
826 sbiw r28, 4*4 /* Y points to h4 */
849 stack_free_large3 32*4+4
861 /******************************************************************************/
880 /******************************************************************************/
887 .global bmw_small_lastBlock
888 .global bmw256_lastBlock
892 /* while(length_b >= BMW_SMALL_BLOCKSIZE){
893 bmw_small_nextBlock(ctx, block);
894 length_b -= BMW_SMALL_BLOCKSIZE;
895 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
906 rcall bmw_small_nextBlock_early
921 /* memset(pctx.buffer, 0, 64);
922 memcpy(pctx.buffer, block, (length_b+7)/8);
923 pctx.buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
933 /* copy (#r24) bytes to stack buffer */
938 301: /* calculate the appended byte */
958 /* if(length_b+1>64*8-64){ ; = 64*7-1 = 447 max(length_b)=511
959 bmw_small_nextBlock(ctx, pctx.buffer);
960 memset(pctx.buffer, 0, 64-8);
969 rcall bmw_small_nextBlock_early
976 rcall load32_from_Z_stub
982 /* *((uint64_t*)&(pctx.buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
983 bmw_small_nextBlock(ctx, pctx.buffer);
986 rcall load32_from_Z_stub
1006 rcall bmw_small_nextBlock_early
1007 /* memset(pctx.buffer, 0xaa, 64);
1009 pctx.buffer[i*4] = i+0xa0;
1022 /* bmw_small_nextBlock((bmw_small_ctx_t*)&pctx, ctx->h);
1023 memcpy(ctx->h, pctx.buffer, 64);
1027 rcall bmw_small_nextBlock
1041 /*******************************************************************************
1042 * void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
1043 * memcpy(dest, &(ctx->h[8]), 256/8);
1046 * param dest: r24:r25
1047 * param ctx: r22:r23
1049 .global bmw256_ctx2hash
1061 /*******************************************************************************
1062 * void bmw256(void* dest, const void* msg, uint32_t length_b){
1063 * bmw_small_ctx_t ctx;
1064 * bmw256_init(&ctx);
1065 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1066 * bmw_small_nextBlock(&ctx, msg);
1067 * length_b -= BMW_SMALL_BLOCKSIZE;
1068 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1070 * bmw_small_lastBlock(&ctx, msg, length_b);
1071 * bmw256_ctx2hash(dest, &ctx);
1074 * param dest: r24:r25
1075 * param msg: r22:r23
1076 * param length_b: r18:r21
1090 /*******************************************************************************
1091 * void bmw224(void* dest, const void* msg, uint32_t length_b){
1092 * bmw_small_ctx_t ctx;
1093 * bmw224_init(&ctx);
1094 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1095 * bmw_small_nextBlock(&ctx, msg);
1096 * length_b -= BMW_SMALL_BLOCKSIZE;
1097 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1099 * bmw_small_lastBlock(&ctx, msg, length_b);
1100 * bmw224_ctx2hash(dest, &ctx);
1103 * param dest: r24:r25
1104 * param msg: r22:r23
1105 * param length_b: r18:r21
1124 stack_alloc_large 64+4
1136 rcall bmw_small_nextBlock_early
1148 rcall bmw_small_lastBlock
1151 rcall bmw256_ctx2hash
1152 stack_free_large 64+4
1155 /******************************************************************************/
1178 /******************************************************************************/
1187 ldi r24, lo8(qdbg_str)
1188 ldi r25, hi8(qdbg_str)
1191 10: ldi r24, lo8(qdbg_str1)
1192 ldi r25, hi8(qdbg_str1)
1195 call cli_hexdump_byte
1196 ldi r24, lo8(qdbg_str2)
1197 ldi r25, hi8(qdbg_str2)
1202 call cli_hexdump_rev
1210 qdbg_str: .asciz "\r\nDBG Q: "
1211 qdbg_str1: .asciz "\r\n Q["
1212 qdbg_str2: .asciz "] = "
1223 ldi r24, lo8(Xdbg_str)
1224 ldi r25, hi8(Xdbg_str)
1231 10: ldi r24, lo8(Xdbg_str1)
1232 ldi r25, hi8(Xdbg_str1)
1239 call cli_hexdump_byte
1240 ldi r24, lo8(Xdbg_str2)
1241 ldi r25, hi8(Xdbg_str2)
1246 call cli_hexdump_rev
1256 Xdbg_str: .asciz "\r\nDBG "
1257 Xdbg_str1: .asciz "\r\n "
1258 Xdbg_str2: .asciz "] = "
1266 ldi r24, lo8(Xdbg_str)
1267 ldi r25, hi8(Xdbg_str)
1270 call cli_hexdump_byte
1272 call cli_hexdump_byte
1274 call cli_hexdump_byte
1276 call cli_hexdump_byte
1286 ldi r24, lo8(Xdbg_str)
1287 ldi r25, hi8(Xdbg_str)
1290 call cli_hexdump_byte
1292 call cli_hexdump_byte
1294 call cli_hexdump_byte
1296 call cli_hexdump_byte