+threefish256 decryption

This commit is contained in:
bg 2009-03-21 15:55:10 +00:00
parent 4147d732ef
commit ba4ac1b4a8
9 changed files with 152 additions and 21 deletions

View File

@ -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

View File

@ -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},

View File

@ -21,10 +21,10 @@
#include <stdint.h>
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;

View File

@ -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_ */

View File

@ -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};

69
threefish256_dec.c Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/*
* \author Daniel Otte
* \email daniel.otte@rub.de
* \date 2009-03-12
* \license GPLv3 or later
*
*
*
*/
#include <stdint.h>
#include <string.h>
#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);
}

View File

@ -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};

View File

@ -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};

39
threefish_invmix_c.c Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/*
* \author Daniel Otte
* \email daniel.otte@rub.de
* \date 2009-03-21
* \license GPLv3 or later
*
*
*
*/
#include <stdint.h>
#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;
}