1 /* threefish1024_enc_asm.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 * \email daniel.otte@rub.de
23 * \license GPLv3 or later
26 #include "avr-asm-macros.S"
28 /******************************************************************************/
38 #define THREEFISH_KEY_CONST 0x5555.5555.5555.5555.LL / * 2**64/3 * /
40 #define K(s) (((uint64_t*)key)[(s)])
41 #define T(s) (((uint64_t*)tweak)[(s)])
42 void threefish1024_init(const void *key, const void *tweak, threefish512_ctx_t *ctx){
43 memcpy(ctx->k, key, 16*8);
44 memcpy(ctx->t, tweak, 2*8);
46 ctx->k[16] = THREEFISH_KEY_CONST;
50 ctx->t[2] = T(0) ^ T(1);
55 * param tweak: r22:r23
58 .global threefish1024_init
69 ldi A6, 0x22 ; 0x1BD1.1BDA.A9FC.1A22
178 /******************************************************************************/
180 #define X(a) (((uint64_t*)data)[(a)])
181 void permute_16(void *data){
202 void add_key_16(void *data, const threefish1024_ctx_t *ctx, uint8_t s){
205 X(i) += ctx->k[(s+i)%17];
207 X(13) += ctx->k[(s+13)%17] + ctx->t[s%3];
208 X(14) += ctx->k[(s+14)%17] + ctx->t[(s+1)%3];
209 X(15) += ctx->k[(s+15)%17] + s;
211 void threefish1024_enc(void *data, const threefish1024_ctx_t *ctx){
213 uint8_t r0[8] = {55, 25, 33, 34, 28, 17, 58, 47};
214 uint8_t r1[8] = {43, 25, 8, 43, 7, 6, 7, 49};
215 uint8_t r2[8] = {37, 46, 18, 25, 47, 18, 32, 27};
216 uint8_t r3[8] = {40, 13, 57, 60, 48, 25, 45, 58};
217 uint8_t r4[8] = {16, 14, 21, 44, 51, 43, 19, 37};
218 uint8_t r5[8] = {22, 13, 12, 9, 9, 42, 18, 48};
219 uint8_t r6[8] = {38, 52, 32, 59, 35, 40, 2, 53};
220 uint8_t r7[8] = {12, 57, 54, 34, 41, 15, 56, 56};
223 add_key_16(data, ctx, s);
226 threefish_mix((uint8_t*)data + 0, r0[i%8]);
227 threefish_mix((uint8_t*)data + 16, r1[i%8]);
228 threefish_mix((uint8_t*)data + 32, r2[i%8]);
229 threefish_mix((uint8_t*)data + 48, r3[i%8]);
230 threefish_mix((uint8_t*)data + 64, r4[i%8]);
231 threefish_mix((uint8_t*)data + 80, r5[i%8]);
232 threefish_mix((uint8_t*)data + 96, r6[i%8]);
233 threefish_mix((uint8_t*)data +112, r7[i%8]);
237 add_key_16(data, ctx, s);
256 * param data: r24:r25
259 .global threefish1024_enc
274 ldi r30, lo8(threefish1024_slut17)
275 ldi r31, hi8(threefish1024_slut17)
320 ldi r30, lo8(threefish1024_slut17)
321 ldi r31, hi8(threefish1024_slut17)
365 /* now the remaining key */
367 ldi r30, lo8(threefish1024_slut3)
368 ldi r31, hi8(threefish1024_slut3)
374 adiw r30, 7*8 /* make Z pointing to (extended tweak) */
420 ldi r30, lo8(threefish1024_rc0)
421 ldi r31, hi8(threefish1024_rc0)
448 call threefish_mix_asm /* no rcall? */
452 call threefish_mix_asm /* no rcall? */
456 call threefish_mix_asm /* no rcall? */
460 call threefish_mix_asm /* no rcall? */
465 call threefish_mix_asm /* no rcall? */
470 call threefish_mix_asm /* no rcall? */
475 call threefish_mix_asm /* no rcall? */
480 call threefish_mix_asm /* no rcall? */
481 /* now the permutation */
482 movw r26, DATA0 /* X1 <-> X15 */
488 movw r26, DATA0 /* X1 <-> X9 */
494 movw r26, DATA0 /* X9 <-> X7 */
501 movw r26, DATA0 /* X3 <-> X11 */
507 movw r26, DATA0 /* X3 <-> X13 */
513 movw r26, DATA0 /* X13 <-> X5 */
520 movw r26, DATA0 /* X8 <-> X14 */
527 movw r26, DATA0 /* X8 <-> X10 */
534 movw r26, DATA0 /* X10 <-> X12 */
542 movw r26, DATA0 /* X4 <-> X6 */
556 threefish1024_slut17:
557 .byte 0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38
558 .byte 0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x78
559 .byte 0x80, 0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30
560 .byte 0x38, 0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70
561 .byte 0x78, 0x80, 0x00, 0x08, 0x10
563 .byte 0x00, 0x08, 0x10, 0x00, 0x08, 0x10, 0x00, 0x08
564 .byte 0x10, 0x00, 0x08, 0x10, 0x00, 0x08, 0x10, 0x00
565 .byte 0x08, 0x10, 0x00, 0x08, 0x10, 0x00, 0x08, 0x10
567 /* old round constants
568 threefish1024_rc0: .byte 0x79, 0x31, 0x41, 0x42, 0x34, 0x21, 0x72, 0x69
569 threefish1024_rc1: .byte 0x53, 0x31, 0x10, 0x53, 0x19, 0x1a, 0x19, 0x61
570 threefish1024_rc2: .byte 0x5b, 0x6a, 0x22, 0x31, 0x69, 0x22, 0x40, 0x33
571 threefish1024_rc3: .byte 0x50, 0x2b, 0x71, 0x74, 0x60, 0x31, 0x6b, 0x72
572 threefish1024_rc4: .byte 0x20, 0x2a, 0x3b, 0x54, 0x63, 0x53, 0x23, 0x5b
573 threefish1024_rc5: .byte 0x3a, 0x2b, 0x14, 0x11, 0x11, 0x52, 0x22, 0x60
574 threefish1024_rc6: .byte 0x5a, 0x64, 0x40, 0x73, 0x43, 0x50, 0x02, 0x7b
575 threefish1024_rc7: .byte 0x14, 0x71, 0x7a, 0x42, 0x51, 0x29, 0x70, 0x70
577 threefish1024_rc0: .byte 0x30, 0x5a, 0x41, 0x1b, 0x51, 0x20, 0x49, 0x11
578 threefish1024_rc1: .byte 0x2b, 0x23, 0x04, 0x24, 0x11, 0x42, 0x54, 0x60
579 threefish1024_rc2: .byte 0x10, 0x12, 0x63, 0x60, 0x5b, 0x70, 0x69, 0x43
580 threefish1024_rc3: .byte 0x69, 0x79, 0x2b, 0x51, 0x49, 0x63, 0x6a, 0x64
581 threefish1024_rc4: .byte 0x10, 0x61, 0x42, 0x69, 0x14, 0x04, 0x23, 0x39
582 threefish1024_rc5: .byte 0x21, 0x22, 0x51, 0x34, 0x69, 0x7b, 0x52, 0x49
583 threefish1024_rc6: .byte 0x3a, 0x39, 0x73, 0x20, 0x54, 0x52, 0x54, 0x5b
584 threefish1024_rc7: .byte 0x5b, 0x64, 0x21, 0x31, 0x4a, 0x51, 0x31, 0x24