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 0x03, 0x11, 5*4
260 .byte 0xDD, 0xB3, 7*4
261 .byte 0x2A, 0x79, 10*4
262 .byte 0x07, 0xAA, 13*4
263 .byte 0x51, 0xC2, 14*4
264 .byte 0 ; just for alignment
267 /*******************************************************************************
268 * uint32_t addelment(uint8_t j, const uint32_t* m, const uint32_t* h){
270 * r = pgm_read_dword(k_lut+j);
271 * r += rotl_addel(((uint32_t*)m)[j&0xf], j+0);
272 * r += rotl_addel(((uint32_t*)m)[(j+3)&0xf], j+3);
273 * r -= rotl_addel(((uint32_t*)m)[(j+10)&0xf], j+10);
274 * r ^= ((uint32_t*)h)[(j+7)&0xf];
337 /******************************************************************************/
363 rcall load_acc_from_X
370 rcall store_acc_to_dec_X
373 rcall load_rotate_add_M
375 rcall load_rotate_add_M
378 rcall load_rotate_add_M
409 /******************************************************************************/
418 ; .byte 0x2B, 0x64, 0x66, 0x03, 0x51, 0x55, 0x87, 0x55
419 .byte 0x55, 0x87, 0x55, 0x51, 0x03, 0x66, 0x64, 0x2B
421 ; .byte (2<<1), (7<<1), (4<<1), (3<<1), (4<<1)+1, (6<<1)+1, (6<<1)
422 .byte (8<<1)+1, (6<<1), (6<<1)+1, (4<<1)+1, (3<<1), (4<<1), (7<<1), (2<<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 /******************************************************************************/
510 bmw_small_nextBlock_early:
513 .global bmw_small_nextBlock
514 .global bmw224_nextBlock
515 .global bmw256_nextBlock
522 stack_alloc_large 32*4, r28, r29
531 ; push_range 28, 29 /* push Q */
532 ; push_range 22, 25 /* push M & H */
533 /* increment counter */
538 rcall load_acc_from_X
544 rcall store_acc_to_dec_X
564 /* calculate W and store it in Q */
568 /* load initial index */
570 /* load values from hacktable */
571 ldi r30, lo8(f0_hacktable-3)
572 ldi r31, hi8(f0_hacktable-3)
596 rcall load_acc_from_X
707 rcall load_acc_from_X
709 10: rcall load32_from_X
730 /* copy m(Y) into h */
738 ;--- /* calc first half of h0..h15 */
741 ldi r30, lo8(f2_1_shift_table)
742 ldi r31, hi8(f2_1_shift_table)
764 25: rcall shiftleft32
765 26: rcall mov32_to_acc
775 27: rcall shiftright32
776 28: rcall eor32_to_acc
794 sbiw r28, 4*8 /* Y points to q[24] */
797 sbiw r28, 33 /* Y points to q[0] */
800 /* xor q[24..31] into q[0..7] */
802 /* xor q[23] into q[8] */
806 /* xor q[16..22] into q[9..15] */
813 ldi r30, lo8(f2_2_shift_table)
814 ldi r31, hi8(f2_2_shift_table)
834 sbiw r26, 8*4 /* X points to h8 */
836 sbiw r28, 4*4 /* Y points to h4 */
859 stack_free_large3 32*4+4
871 /******************************************************************************/
890 /******************************************************************************/
897 .global bmw_small_lastBlock
898 .global bmw224_lastBlock
899 .global bmw256_lastBlock
903 /* while(length_b >= BMW_SMALL_BLOCKSIZE){
904 bmw_small_nextBlock(ctx, block);
905 length_b -= BMW_SMALL_BLOCKSIZE;
906 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
917 rcall bmw_small_nextBlock_early
932 /* memset(pctx.buffer, 0, 64);
933 memcpy(pctx.buffer, block, (length_b+7)/8);
934 pctx.buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
944 /* copy (#r24) bytes to stack buffer */
949 301: /* calculate the appended byte */
969 /* if(length_b+1>64*8-64){ ; = 64*7-1 = 447 max(length_b)=511
970 bmw_small_nextBlock(ctx, pctx.buffer);
971 memset(pctx.buffer, 0, 64-8);
980 rcall bmw_small_nextBlock_early
987 rcall load32_from_Z_stub
993 /* *((uint64_t*)&(pctx.buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
994 bmw_small_nextBlock(ctx, pctx.buffer);
997 rcall load32_from_Z_stub
1017 rcall bmw_small_nextBlock_early
1018 /* memset(pctx.buffer, 0xaa, 64);
1020 pctx.buffer[i*4] = i+0xa0;
1033 /* bmw_small_nextBlock((bmw_small_ctx_t*)&pctx, ctx->h);
1034 memcpy(ctx->h, pctx.buffer, 64);
1038 rcall bmw_small_nextBlock
1052 /*******************************************************************************
1053 * void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
1054 * memcpy(dest, &(ctx->h[9]), 224/8);
1057 * param dest: r24:r25
1058 * param ctx: r22:r23
1060 .global bmw224_ctx2hash
1067 /*******************************************************************************
1068 * void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
1069 * memcpy(dest, &(ctx->h[8]), 256/8);
1072 * param dest: r24:r25
1073 * param ctx: r22:r23
1075 .global bmw256_ctx2hash
1087 /*******************************************************************************
1088 * void bmw256(void* dest, const void* msg, uint32_t length_b){
1089 * bmw_small_ctx_t ctx;
1090 * bmw256_init(&ctx);
1091 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1092 * bmw_small_nextBlock(&ctx, msg);
1093 * length_b -= BMW_SMALL_BLOCKSIZE;
1094 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1096 * bmw_small_lastBlock(&ctx, msg, length_b);
1097 * bmw256_ctx2hash(dest, &ctx);
1100 * param dest: r24:r25
1101 * param msg: r22:r23
1102 * param length_b: r18:r21
1120 /*******************************************************************************
1121 * void bmw224(void* dest, const void* msg, uint32_t length_b){
1122 * bmw_small_ctx_t ctx;
1123 * bmw224_init(&ctx);
1124 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1125 * bmw_small_nextBlock(&ctx, msg);
1126 * length_b -= BMW_SMALL_BLOCKSIZE;
1127 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1129 * bmw_small_lastBlock(&ctx, msg, length_b);
1130 * bmw224_ctx2hash(dest, &ctx);
1133 * param dest: r24:r25
1134 * param msg: r22:r23
1135 * param length_b: r18:r21
1158 stack_alloc_large 64+4
1169 ldi r30, pm_lo8(init_lut)
1170 ldi r31, pm_hi8(init_lut)
1178 rcall bmw_small_nextBlock_early
1190 rcall bmw_small_lastBlock
1193 ldi r30, pm_lo8(c2h_lut)
1194 ldi r31, pm_hi8(c2h_lut)
1198 stack_free_large 64+4
1206 rjmp bmw224_ctx2hash
1207 rjmp bmw256_ctx2hash
1209 /*******************************************************************************
1210 * void bmw224_init(bmw224_ctx_t* ctx){
1212 * ctx->h[0] = 0x00010203;
1213 * for(i=1; i<16; ++i){
1214 * ctx->h[i] = ctx->h[i-1]+ 0x04040404;
1219 * param ctx: r24:r25
1250 /******************************************************************************/
1259 ldi r24, lo8(qdbg_str)
1260 ldi r25, hi8(qdbg_str)
1263 10: ldi r24, lo8(qdbg_str1)
1264 ldi r25, hi8(qdbg_str1)
1267 call cli_hexdump_byte
1268 ldi r24, lo8(qdbg_str2)
1269 ldi r25, hi8(qdbg_str2)
1274 call cli_hexdump_rev
1282 qdbg_str: .asciz "\r\nDBG Q: "
1283 qdbg_str1: .asciz "\r\n Q["
1284 qdbg_str2: .asciz "] = "
1295 ldi r24, lo8(Xdbg_str)
1296 ldi r25, hi8(Xdbg_str)
1303 10: ldi r24, lo8(Xdbg_str1)
1304 ldi r25, hi8(Xdbg_str1)
1311 call cli_hexdump_byte
1312 ldi r24, lo8(Xdbg_str2)
1313 ldi r25, hi8(Xdbg_str2)
1318 call cli_hexdump_rev
1328 Xdbg_str: .asciz "\r\nDBG "
1329 Xdbg_str1: .asciz "\r\n "
1330 Xdbg_str2: .asciz "] = "
1338 ldi r24, lo8(Xdbg_str)
1339 ldi r25, hi8(Xdbg_str)
1342 call cli_hexdump_byte
1344 call cli_hexdump_byte
1346 call cli_hexdump_byte
1348 call cli_hexdump_byte
1358 ldi r24, lo8(Xdbg_str)
1359 ldi r25, hi8(Xdbg_str)
1362 call cli_hexdump_byte
1364 call cli_hexdump_byte
1366 call cli_hexdump_byte
1368 call cli_hexdump_byte