From ba4ac1b4a8020026fb6f1a45a1e04e16017262f7 Mon Sep 17 00:00:00 2001 From: bg Date: Sat, 21 Mar 2009 15:55:10 +0000 Subject: [PATCH] +threefish256 decryption --- mkfiles/threefish.mk | 3 +- test_src/main-threefish-test.c | 22 ++++++++++- test_src/nessie_bc_test.h | 6 +-- threefish.h | 16 ++++---- threefish1024_enc.c | 6 +-- threefish256_dec.c | 69 ++++++++++++++++++++++++++++++++++ threefish256_enc.c | 6 +-- threefish512_enc.c | 6 +-- threefish_invmix_c.c | 39 +++++++++++++++++++ 9 files changed, 152 insertions(+), 21 deletions(-) create mode 100644 threefish256_dec.c create mode 100644 threefish_invmix_c.c diff --git a/mkfiles/threefish.mk b/mkfiles/threefish.mk index ab81e89..fa58d23 100644 --- a/mkfiles/threefish.mk +++ b/mkfiles/threefish.mk @@ -6,7 +6,8 @@ BLOCK_CIPHERS += $(ALGO_NAME) $(ALGO_NAME)_OBJ := threefish256_enc_asm.o threefish512_enc.o threefish1024_enc.o\ - threefish_mix.o threefish_mix_4c.o + threefish_mix.o threefish_mix_4c.o threefish_invmix_c.o\ + threefish256_dec.o $(ALGO_NAME)_TEST_BIN := main-threefish-test.o debug.o uart.o hexdigit_tab.o \ nessie_bc_test.o dbz_strings.o nessie_common.o cli.o string-extras.o performance_test.o $(ALGO_NAME)_NESSIE_TEST := test nessie diff --git a/test_src/main-threefish-test.c b/test_src/main-threefish-test.c index 74a2aca..1dba3ac 100644 --- a/test_src/main-threefish-test.c +++ b/test_src/main-threefish-test.c @@ -40,6 +40,26 @@ char* algo_name = "Threefish"; /***************************************************************************** * additional validation-functions * *****************************************************************************/ + +void threefish256_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){ + uint8_t null[16]; + memset(null, 0, 16); + threefish256_init(key, null, ctx); +} + +void testrun_nessie_threefish(void){ + nessie_bc_ctx.keysize_b = 256; + nessie_bc_ctx.blocksize_B = 32; + nessie_bc_ctx.ctx_size_B = sizeof(threefish256_ctx_t); + nessie_bc_ctx.name = "Threefish256"; + nessie_bc_ctx.cipher_genctx = threefish256_dummy_init; + nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)threefish256_enc; + nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)threefish256_dec; + nessie_bc_ctx.cipher_free = NULL; + + nessie_bc_run(); +} + void testrun_stdtest_threefish256(void){ uint8_t key[32], data[32]; uint8_t tweak[16]; @@ -357,7 +377,7 @@ const char performance_str[] PROGMEM = "performance"; const char echo_str[] PROGMEM = "echo"; cmdlist_entry_t cmdlist[] PROGMEM = { -// { nessie_str, NULL, testrun_nessie_noekeon}, + { nessie_str, NULL, testrun_nessie_threefish}, { test_str, NULL, testrun_stdtest_threefish}, { inittest_str, NULL, init_test}, { performance_str, NULL, testrun_performance_threefish}, diff --git a/test_src/nessie_bc_test.h b/test_src/nessie_bc_test.h index a641f5a..350ea4b 100644 --- a/test_src/nessie_bc_test.h +++ b/test_src/nessie_bc_test.h @@ -21,10 +21,10 @@ #include -typedef void (*nessie_bc_gen_fpt)(uint8_t* key, uint16_t keysize_b, void* ctx); +typedef void (*nessie_bc_gen_fpt)(const uint8_t* key, uint16_t keysize_b, void* ctx); typedef void (*nessie_bc_free_fpt)(void* ctx); -typedef void (*nessie_bc_enc_fpt)(void* buffer, void* ctx); -typedef void (*nessie_bc_dec_fpt)(void* buffer, void* ctx); +typedef void (*nessie_bc_enc_fpt)(void* buffer, const void* ctx); +typedef void (*nessie_bc_dec_fpt)(void* buffer, const void* ctx); typedef struct nessie_bc_ctx_st{ uint16_t keysize_b; diff --git a/threefish.h b/threefish.h index 0d3a413..eaa369c 100644 --- a/threefish.h +++ b/threefish.h @@ -55,14 +55,16 @@ typedef struct{ void threefish_mix(void* data, uint8_t rot); -void threefish256_init_c(void* key, void* tweak, threefish256_ctx_t* ctx); +void threefish_invmix(void* data, uint8_t rot); -void threefish256_init(void* key, void* tweak, threefish256_ctx_t* ctx); -void threefish512_init(void* key, void* tweak, threefish512_ctx_t* ctx); -void threefish1024_init(void* key, void* tweak, threefish1024_ctx_t* ctx); +void threefish256_init(const void* key, const void* tweak, threefish256_ctx_t* ctx); +void threefish512_init(const void* key, const void* tweak, threefish512_ctx_t* ctx); +void threefish1024_init(const void* key, const void* tweak, threefish1024_ctx_t* ctx); -void threefish256_enc(void* data, threefish256_ctx_t* ctx); -void threefish512_enc(void* data, threefish512_ctx_t* ctx); -void threefish1024_enc(void* data, threefish1024_ctx_t* ctx); +void threefish256_enc(void* data, const threefish256_ctx_t* ctx); +void threefish512_enc(void* data, const threefish512_ctx_t* ctx); +void threefish1024_enc(void* data, const threefish1024_ctx_t* ctx); + +void threefish256_dec(void* data, const threefish256_ctx_t* ctx); #endif /* THREEFISH_H_ */ diff --git a/threefish1024_enc.c b/threefish1024_enc.c index 4f6f9d4..0bb8e9b 100644 --- a/threefish1024_enc.c +++ b/threefish1024_enc.c @@ -60,7 +60,7 @@ void permute_16(void* data){ #define K(s) (((uint64_t*)key)[(s)]) #define T(s) (((uint64_t*)tweak)[(s)]) -void threefish1024_init(void* key, void* tweak, threefish1024_ctx_t* ctx){ +void threefish1024_init(const void* key, const void* tweak, threefish1024_ctx_t* ctx){ memcpy(ctx->k, key, 16*8); memcpy(ctx->t, tweak, 2*8); uint8_t i; @@ -72,7 +72,7 @@ void threefish1024_init(void* key, void* tweak, threefish1024_ctx_t* ctx){ } static -void add_key_16(void* data, threefish1024_ctx_t* ctx, uint8_t s){ +void add_key_16(void* data, const threefish1024_ctx_t* ctx, uint8_t s){ uint8_t i; for(i=0; i<13; ++i){ X(i) += ctx->k[(s+i)%17]; @@ -82,7 +82,7 @@ void add_key_16(void* data, threefish1024_ctx_t* ctx, uint8_t s){ X(15) += ctx->k[(s+15)%17] + s; } -void threefish1024_enc(void* data, threefish1024_ctx_t* ctx){ +void threefish1024_enc(void* data, const threefish1024_ctx_t* ctx){ uint8_t i=0,s=0; uint8_t r0[8] = {55, 25, 33, 34, 28, 17, 58, 47}; uint8_t r1[8] = {43, 25, 8, 43, 7, 6, 7, 49}; diff --git a/threefish256_dec.c b/threefish256_dec.c new file mode 100644 index 0000000..d23eb93 --- /dev/null +++ b/threefish256_dec.c @@ -0,0 +1,69 @@ +/* threefish256_enc.c */ +/* + This file is part of the AVR-Crypto-Lib. + Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +/* + * \author Daniel Otte + * \email daniel.otte@rub.de + * \date 2009-03-12 + * \license GPLv3 or later + * + * + * + */ + +#include +#include +#include "threefish.h" + +#define X(a) (((uint64_t*)data)[(a)]) +static +void permute_4(void* data){ + uint64_t t; + t = X(1); + X(1) = X(3); + X(3) = t; +} + +#define K(s) (((uint64_t*)key)[(s)]) +#define T(s) (((uint64_t*)tweak)[(s)]) + +static +void add_key_4(void* data, const threefish256_ctx_t* ctx, uint8_t s){ + X(0) -= ctx->k[(s+0)%5]; + X(1) -= ctx->k[(s+1)%5] + ctx->t[s%3]; + X(2) -= ctx->k[(s+2)%5] + ctx->t[(s+1)%3]; + X(3) -= ctx->k[(s+3)%5] + s; +} + +void threefish256_dec(void* data, const threefish256_ctx_t* ctx){ + uint8_t i=0,s=18; + uint8_t r0[8] = {59, 11, 53, 26, 58, 13, 36, 5}; + uint8_t r1[8] = {50, 42, 35, 20, 44, 46, 28, 56}; + do{ + if(i%4==0){ + add_key_4(data, ctx, s); + --s; + } + permute_4(data); + threefish_invmix(data, r0[i%8]); + threefish_invmix((uint8_t*)data + 16, r1[i%8]); + ++i; + }while(i!=72); + add_key_4(data, ctx, s); +} + diff --git a/threefish256_enc.c b/threefish256_enc.c index afb1a25..2739f6a 100644 --- a/threefish256_enc.c +++ b/threefish256_enc.c @@ -44,7 +44,7 @@ void permute_4(void* data){ #define K(s) (((uint64_t*)key)[(s)]) #define T(s) (((uint64_t*)tweak)[(s)]) -void threefish256_init(void* key, void* tweak, threefish256_ctx_t* ctx){ +void threefish256_init(const void* key, const void* tweak, threefish256_ctx_t* ctx){ memcpy(ctx->k, key, 4*8); memcpy(ctx->t, tweak, 2*8); uint8_t i; @@ -56,14 +56,14 @@ void threefish256_init(void* key, void* tweak, threefish256_ctx_t* ctx){ } static -void add_key_4(void* data, threefish256_ctx_t* ctx, uint8_t s){ +void add_key_4(void* data, const threefish256_ctx_t* ctx, uint8_t s){ X(0) += ctx->k[(s+0)%5]; X(1) += ctx->k[(s+1)%5] + ctx->t[s%3]; X(2) += ctx->k[(s+2)%5] + ctx->t[(s+1)%3]; X(3) += ctx->k[(s+3)%5] + s; } -void threefish256_enc(void* data, threefish256_ctx_t* ctx){ +void threefish256_enc(void* data, const threefish256_ctx_t* ctx){ uint8_t i=0,s=0; uint8_t r0[8] = { 5, 36, 13, 58, 26, 53, 11, 59}; uint8_t r1[8] = {56, 28, 46, 44, 20, 35, 42, 50}; diff --git a/threefish512_enc.c b/threefish512_enc.c index abb8509..fb044b5 100644 --- a/threefish512_enc.c +++ b/threefish512_enc.c @@ -66,7 +66,7 @@ void permute_inv8(void* data){ #define K(s) (((uint64_t*)key)[(s)]) #define T(s) (((uint64_t*)tweak)[(s)]) -void threefish512_init(void* key, void* tweak, threefish512_ctx_t* ctx){ +void threefish512_init(const void* key, const void* tweak, threefish512_ctx_t* ctx){ memcpy(ctx->k, key, 8*8); memcpy(ctx->t, tweak, 2*8); uint8_t i; @@ -78,7 +78,7 @@ void threefish512_init(void* key, void* tweak, threefish512_ctx_t* ctx){ } static -void add_key_8(void* data, threefish512_ctx_t* ctx, uint8_t s){ +void add_key_8(void* data, const threefish512_ctx_t* ctx, uint8_t s){ uint8_t i; for(i=0; i<5; ++i){ X(i) += ctx->k[(s+i)%9]; @@ -88,7 +88,7 @@ void add_key_8(void* data, threefish512_ctx_t* ctx, uint8_t s){ X(7) += ctx->k[(s+7)%9] + s; } -void threefish512_enc(void* data, threefish512_ctx_t* ctx){ +void threefish512_enc(void* data, const threefish512_ctx_t* ctx){ uint8_t i=0,s=0; uint8_t r0[8] = {38, 48, 34, 26, 33, 39, 29, 33}; uint8_t r1[8] = {30, 20, 14, 12, 49, 27, 26, 51}; diff --git a/threefish_invmix_c.c b/threefish_invmix_c.c new file mode 100644 index 0000000..f664888 --- /dev/null +++ b/threefish_invmix_c.c @@ -0,0 +1,39 @@ +/* threefish_invmix_c.c */ +/* + This file is part of the AVR-Crypto-Lib. + Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +/* + * \author Daniel Otte + * \email daniel.otte@rub.de + * \date 2009-03-21 + * \license GPLv3 or later + * + * + * + */ + +#include + +#define X0 (((uint64_t*)data)[0]) +#define X1 (((uint64_t*)data)[1]) +void threefish_invmix(void* data, uint8_t rot){ + uint64_t x; + x = X1; + x ^= X0; + X1 = ((x>>rot)|(x<<(64-rot))); + X0 -= X1; +} -- 2.39.5