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
126 ldi r30, lo8(s_table)
127 ldi r31, hi8(s_table)
162 /******************************************************************************/
164 param dest: r26:r27 (X)
165 param src: r30:r31 (Z)
180 /******************************************************************************/
225 /******************************************************************************/
239 /******************************************************************************/
253 .byte 5*4,7*4,10*4,13*4,14*4
254 ; .byte 0 ; just for alignment
291 /* calculate W and store it in Q */
295 /* load initial index */
296 ldi r30, lo8(f0_indextable-1)
297 ldi r31, hi8(f0_indextable-1)
301 /* load values from hacktable */
302 ldi r30, lo8(f0_hacktable-2)
303 ldi r31, hi8(f0_hacktable-2)
334 ldi r30, lo8(f0_s_table)
335 ldi r31, hi8(f0_s_table)
387 /******************************************************************************/
390 .long 0x55555550, 0x5aaaaaa5, 0x5ffffffa, 0x6555554f
391 .long 0x6aaaaaa4, 0x6ffffff9, 0x7555554e, 0x7aaaaaa3
392 .long 0x7ffffff8, 0x8555554d, 0x8aaaaaa2, 0x8ffffff7
393 .long 0x9555554c, 0x9aaaaaa1, 0x9ffffff6, 0xa555554b
395 /*******************************************************************************
396 * uint32_t addelment(uint8_t j, const uint32_t* m, const uint32_t* h){
398 * r = pgm_read_dword(k_lut+j);
399 * r += rotl_addel(((uint32_t*)m)[j&0xf], j+0);
400 * r += rotl_addel(((uint32_t*)m)[(j+3)&0xf], j+3);
401 * r -= rotl_addel(((uint32_t*)m)[(j+10)&0xf], j+10);
402 * r ^= ((uint32_t*)h)[(j+7)&0xf];
485 ldi r30, lo8(const_lut)
486 ldi r31, hi8(const_lut)
495 rcall load_rotate_add_M
498 rcall load_rotate_add_M
502 rcall load_rotate_add_M
521 /******************************************************************************/
560 /******************************************************************************/
569 .byte 0,3,0,7,0,13,0,16,0,19,0,23,0,27
574 ldi r30, lo8(expand2_rot_table)
575 ldi r31, hi8(expand2_rot_table)
595 /******************************************************************************/
601 /* for calling expand1/2
632 /******************************************************************************/
639 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
640 .byte 0x2B, 0x64, 0x66, 0x03, 0x51, 0x55, 0x87, 0x55
642 .byte (2<<1), (7<<1), (4<<1), (3<<1), (4<<1)+1, (6<<1)+1, (6<<1)
643 .byte 0 ; just for alignment
706 ;--- /* calc first half of h0..h15 */
717 ldi r30, lo8(f2_1_shift_table-1)
718 ldi r31, hi8(f2_1_shift_table-1)
735 25: rcall shiftleft32
736 26: rcall eor32_to_acc
746 27: rcall shiftright32
747 28: rcall eor32_to_acc
759 sbiw r26, 4*8 /* X points to q[24] */
762 sbiw r28, 33 /* Y points to q[0] */
764 sbiw r30, 1 /* Z points to h0 */
775 sbiw r26, 9*4 /* X points to q[23] */
776 rcall load_acc_from_X
784 sbiw r26, 8*4 /* X points to q[16] */
788 ldi r30, lo8(f2_2_shift_table-1)
789 ldi r31, hi8(f2_2_shift_table-1)
793 rcall load_acc_from_X
800 20: rcall shiftright32
811 sbiw r30, 8*4 /* Z points to h8 */
813 sbiw r26, 4*4 /* X points to h4 */
833 /******************************************************************************/
853 .global bmw_small_nextBlock
854 .global bmw224_nextBlock
855 .global bmw256_nextBlock
861 stack_alloc_large 32*4, r28, r29
863 push_range 28, 29 /* push Q */
864 push_range 22, 25 /* push M & H */
865 /* increment counter */
870 rcall load_acc_from_X
908 stack_free_large3 32*4
913 /******************************************************************************/
928 .global bmw_small_lastBlock
929 .global bmw224_lastBlock
930 .global bmw256_lastBlock
934 /* while(length_b >= BMW_SMALL_BLOCKSIZE){
935 bmw_small_nextBlock(ctx, block);
936 length_b -= BMW_SMALL_BLOCKSIZE;
937 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
950 rcall bmw_small_nextBlock
965 /* memset(pctx.buffer, 0, 64);
966 memcpy(pctx.buffer, block, (length_b+7)/8);
967 pctx.buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
978 /* copy (#r24) bytes to stack buffer */
983 301: /* calculate the appended byte */
1003 /* if(length_b+1>64*8-64){ ; = 64*7-1 = 447 max(length_b)=511
1004 bmw_small_nextBlock(ctx, pctx.buffer);
1005 memset(pctx.buffer, 0, 64-8);
1015 rcall bmw_small_nextBlock
1033 /* *((uint64_t*)&(pctx.buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
1034 bmw_small_nextBlock(ctx, pctx.buffer);
1068 rcall bmw_small_nextBlock
1069 /* memset(pctx.buffer, 0xaa, 64);
1071 pctx.buffer[i*4] = i+0xa0;
1085 /* bmw_small_nextBlock((bmw_small_ctx_t*)&pctx, ctx->h);
1086 memcpy(ctx->h, pctx.buffer, 64);
1090 rcall bmw_small_nextBlock
1106 /*******************************************************************************
1107 * void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
1108 * memcpy(dest, &(ctx->h[9]), 224/8);
1111 * param dest: r24:r25
1112 * param ctx: r22:r23
1114 .global bmw224_ctx2hash
1122 /*******************************************************************************
1123 * void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
1124 * memcpy(dest, &(ctx->h[8]), 256/8);
1127 * param dest: r24:r25
1128 * param ctx: r22:r23
1130 .global bmw256_ctx2hash
1143 /*******************************************************************************
1144 * void bmw256(void* dest, const void* msg, uint32_t length_b){
1145 * bmw_small_ctx_t ctx;
1146 * bmw256_init(&ctx);
1147 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1148 * bmw_small_nextBlock(&ctx, msg);
1149 * length_b -= BMW_SMALL_BLOCKSIZE;
1150 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1152 * bmw_small_lastBlock(&ctx, msg, length_b);
1153 * bmw256_ctx2hash(dest, &ctx);
1156 * param dest: r24:r25
1157 * param msg: r22:r23
1158 * param length_b: r18:r21
1176 /*******************************************************************************
1177 * void bmw224(void* dest, const void* msg, uint32_t length_b){
1178 * bmw_small_ctx_t ctx;
1179 * bmw224_init(&ctx);
1180 * while(length_b>=BMW_SMALL_BLOCKSIZE){
1181 * bmw_small_nextBlock(&ctx, msg);
1182 * length_b -= BMW_SMALL_BLOCKSIZE;
1183 * msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
1185 * bmw_small_lastBlock(&ctx, msg, length_b);
1186 * bmw224_ctx2hash(dest, &ctx);
1189 * param dest: r24:r25
1190 * param msg: r22:r23
1191 * param length_b: r18:r21
1210 stack_alloc_large 64+4
1218 ldi r30, pm_lo8(init_lut)
1219 ldi r31, pm_hi8(init_lut)
1229 rcall bmw_small_nextBlock
1242 rcall bmw_small_lastBlock
1245 ldi r30, pm_lo8(c2h_lut)
1246 ldi r31, pm_hi8(c2h_lut)
1250 stack_free_large 64+4
1259 rjmp bmw224_ctx2hash
1260 rjmp bmw256_ctx2hash
1262 /*******************************************************************************
1263 * void bmw224_init(bmw224_ctx_t* ctx){
1265 * ctx->h[0] = 0x00010203;
1266 * for(i=1; i<16; ++i){
1267 * ctx->h[i] = ctx->h[i-1]+ 0x04040404;
1272 * param ctx: r24:r25
1315 /******************************************************************************/
1324 ldi r24, lo8(qdbg_str)
1325 ldi r25, hi8(qdbg_str)
1328 10: ldi r24, lo8(qdbg_str1)
1329 ldi r25, hi8(qdbg_str1)
1332 call cli_hexdump_byte
1333 ldi r24, lo8(qdbg_str2)
1334 ldi r25, hi8(qdbg_str2)
1339 call cli_hexdump_rev
1347 qdbg_str: .asciz "\r\nDBG Q: "
1348 qdbg_str1: .asciz "\r\n Q["
1349 qdbg_str2: .asciz "] = "
1360 ldi r24, lo8(Xdbg_str)
1361 ldi r25, hi8(Xdbg_str)
1368 10: ldi r24, lo8(Xdbg_str1)
1369 ldi r25, hi8(Xdbg_str1)
1376 call cli_hexdump_byte
1377 ldi r24, lo8(Xdbg_str2)
1378 ldi r25, hi8(Xdbg_str2)
1383 call cli_hexdump_rev
1393 Xdbg_str: .asciz "\r\nDBG "
1394 Xdbg_str1: .asciz "\r\n "
1395 Xdbg_str2: .asciz "] = "
1403 ldi r24, lo8(Xdbg_str)
1404 ldi r25, hi8(Xdbg_str)
1407 call cli_hexdump_byte
1409 call cli_hexdump_byte
1411 call cli_hexdump_byte
1413 call cli_hexdump_byte
1423 ldi r24, lo8(Xdbg_str)
1424 ldi r25, hi8(Xdbg_str)
1427 call cli_hexdump_byte
1429 call cli_hexdump_byte
1431 call cli_hexdump_byte
1433 call cli_hexdump_byte