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)])
43 void threefish256_init(void* key, void* tweak, threefish256_ctx_t* ctx){
44 memcpy(ctx->k, key, 4*8);
45 memcpy(ctx->t, tweak, 2*8);
47 ctx->k[4] = THREEFISH_KEY_CONST;
51 ctx->t[2] = T(0) ^ T(1);
56 * param tweak: r22:r23
59 .global threefish256_init
158 /******************************************************************************/
160 #define X(a) (((uint64_t*)data)[(a)])
161 void permute_4(void* data){
167 void add_key_4(void* data, threefish256_ctx_t* ctx, uint8_t s){ /* s: 0..19 * /
168 X(0) += ctx->k[(s+0)%5];
169 X(1) += ctx->k[(s+1)%5] + ctx->t[s%3];
170 X(2) += ctx->k[(s+2)%5] + ctx->t[(s+1)%3];
171 X(3) += ctx->k[(s+3)%5] + s;
173 void threefish256_enc(void* data, threefish256_ctx_t* ctx){
175 uint8_t r0[8] = { 5, 36, 13, 58, 26, 53, 11, 59};
176 uint8_t r1[8] = {56, 28, 46, 44, 20, 35, 42, 50};
179 add_key_4(data, ctx, s);
182 threefish_mix(data, r0[i%8]);
183 threefish_mix((uint8_t*)data + 16, r1[i%8]);
187 add_key_4(data, ctx, s);
201 * param data: r24:r25
204 .global threefish256_enc
219 ldi r30, lo8(threefish256_slut5)
220 ldi r31, hi8(threefish256_slut5)
246 /* now the remaining key */
248 ldi r30, lo8(threefish256_slut3)
249 ldi r31, hi8(threefish256_slut3)
299 ldi r30, lo8(threefish256_rc0)
300 ldi r31, hi8(threefish256_rc0)
309 call threefish_mix_asm /* no rcall? */
313 call threefish_mix_asm /* no rcall? */
314 /* now the permutation */
355 .byte 0x00, 0x08, 0x10, 0x18, 0x20, 0x00, 0x08, 0x10
356 .byte 0x18, 0x20, 0x00, 0x08, 0x10, 0x18, 0x20, 0x00
357 .byte 0x08, 0x10, 0x18, 0x20, 0x00, 0x08, 0x10
359 .byte 0x00, 0x08, 0x10, 0x00, 0x08, 0x10, 0x00, 0x08
360 .byte 0x10, 0x00, 0x08, 0x10, 0x00, 0x08, 0x10, 0x00
361 .byte 0x08, 0x10, 0x00, 0x08, 0x10, 0x00, 0x08
362 ;threefish256_rc0: .byte 5, 36, 13, 58, 26, 53, 11, 59
363 ;threefish256_rc1: .byte 56, 28, 46, 44, 20, 35, 42, 50
364 threefish256_rc0: .byte 0x1b, 0x44, 0x2b, 0x72, 0x32, 0x7b, 0x13, 0x73
365 threefish256_rc1: .byte 0x70, 0x34, 0x6a, 0x54, 0x24, 0x43, 0x52, 0x62