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
157 /******************************************************************************/
159 #define X(a) (((uint64_t*)data)[(a)])
160 void permute_16(void* data){
181 void add_key_16(void* data, const threefish1024_ctx_t* ctx, uint8_t s){
184 X(i) += ctx->k[(s+i)%17];
186 X(13) += ctx->k[(s+13)%17] + ctx->t[s%3];
187 X(14) += ctx->k[(s+14)%17] + ctx->t[(s+1)%3];
188 X(15) += ctx->k[(s+15)%17] + s;
190 void threefish1024_enc(void* data, const threefish1024_ctx_t* ctx){
192 uint8_t r0[8] = {55, 25, 33, 34, 28, 17, 58, 47};
193 uint8_t r1[8] = {43, 25, 8, 43, 7, 6, 7, 49};
194 uint8_t r2[8] = {37, 46, 18, 25, 47, 18, 32, 27};
195 uint8_t r3[8] = {40, 13, 57, 60, 48, 25, 45, 58};
196 uint8_t r4[8] = {16, 14, 21, 44, 51, 43, 19, 37};
197 uint8_t r5[8] = {22, 13, 12, 9, 9, 42, 18, 48};
198 uint8_t r6[8] = {38, 52, 32, 59, 35, 40, 2, 53};
199 uint8_t r7[8] = {12, 57, 54, 34, 41, 15, 56, 56};
202 add_key_16(data, ctx, s);
205 threefish_mix((uint8_t*)data + 0, r0[i%8]);
206 threefish_mix((uint8_t*)data + 16, r1[i%8]);
207 threefish_mix((uint8_t*)data + 32, r2[i%8]);
208 threefish_mix((uint8_t*)data + 48, r3[i%8]);
209 threefish_mix((uint8_t*)data + 64, r4[i%8]);
210 threefish_mix((uint8_t*)data + 80, r5[i%8]);
211 threefish_mix((uint8_t*)data + 96, r6[i%8]);
212 threefish_mix((uint8_t*)data +112, r7[i%8]);
216 add_key_16(data, ctx, s);
235 * param data: r24:r25
238 .global threefish1024_enc
253 ldi r30, lo8(threefish1024_slut17)
254 ldi r31, hi8(threefish1024_slut17)
299 ldi r30, lo8(threefish1024_slut17)
300 ldi r31, hi8(threefish1024_slut17)
344 /* now the remaining key */
346 ldi r30, lo8(threefish1024_slut3)
347 ldi r31, hi8(threefish1024_slut3)
353 adiw r30, 7*8 /* make Z pointing to (extended tweak) */
399 ldi r30, lo8(threefish1024_rc0)
400 ldi r31, hi8(threefish1024_rc0)
427 call threefish_mix_asm /* no rcall? */
431 call threefish_mix_asm /* no rcall? */
435 call threefish_mix_asm /* no rcall? */
439 call threefish_mix_asm /* no rcall? */
444 call threefish_mix_asm /* no rcall? */
449 call threefish_mix_asm /* no rcall? */
454 call threefish_mix_asm /* no rcall? */
459 call threefish_mix_asm /* no rcall? */
460 /* now the permutation */
461 movw r26, DATA0 /* X1 <-> X15 */
467 movw r26, DATA0 /* X1 <-> X9 */
473 movw r26, DATA0 /* X9 <-> X7 */
480 movw r26, DATA0 /* X3 <-> X11 */
486 movw r26, DATA0 /* X3 <-> X13 */
492 movw r26, DATA0 /* X13 <-> X5 */
499 movw r26, DATA0 /* X8 <-> X14 */
506 movw r26, DATA0 /* X8 <-> X10 */
513 movw r26, DATA0 /* X10 <-> X12 */
521 movw r26, DATA0 /* X4 <-> X6 */
535 threefish1024_slut17:
536 .byte 0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38
537 .byte 0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x78
538 .byte 0x80, 0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30
539 .byte 0x38, 0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70
540 .byte 0x78, 0x80, 0x00, 0x08, 0x10
542 .byte 0x00, 0x08, 0x10, 0x00, 0x08, 0x10, 0x00, 0x08
543 .byte 0x10, 0x00, 0x08, 0x10, 0x00, 0x08, 0x10, 0x00
544 .byte 0x08, 0x10, 0x00, 0x08, 0x10, 0x00, 0x08, 0x10
546 threefish1024_rc0: .byte 0x79, 0x31, 0x41, 0x42, 0x34, 0x21, 0x72, 0x69
547 threefish1024_rc1: .byte 0x53, 0x31, 0x10, 0x53, 0x19, 0x1a, 0x19, 0x61
548 threefish1024_rc2: .byte 0x5b, 0x6a, 0x22, 0x31, 0x69, 0x22, 0x40, 0x33
549 threefish1024_rc3: .byte 0x50, 0x2b, 0x71, 0x74, 0x60, 0x31, 0x6b, 0x72
550 threefish1024_rc4: .byte 0x20, 0x2a, 0x3b, 0x54, 0x63, 0x53, 0x23, 0x5b
551 threefish1024_rc5: .byte 0x3a, 0x2b, 0x14, 0x11, 0x11, 0x52, 0x22, 0x60
552 threefish1024_rc6: .byte 0x5a, 0x64, 0x40, 0x73, 0x43, 0x50, 0x02, 0x7b
553 threefish1024_rc7: .byte 0x14, 0x71, 0x7a, 0x42, 0x51, 0x29, 0x70, 0x70