From ff9f1b74e757f2f67b11c51f65b12fffaa02e18d Mon Sep 17 00:00:00 2001 From: bg Date: Mon, 13 Apr 2009 17:11:25 +0000 Subject: [PATCH] fixing a bug in threefish-256 decryption assembler module --- test_src/main-threefish-test.c | 26 +++++++++++++++++--------- threefish1024_enc.c | 8 ++++++-- threefish1024_enc_asm.S | 12 ++++++++++++ threefish256_dec_asm.S | 2 +- threefish256_enc.c | 8 ++++++-- threefish256_enc_asm.S | 13 +++++++++++++ threefish256_enc_small.S | 12 ++++++++++++ threefish512_enc.c | 8 ++++++-- threefish512_enc_asm.S | 12 ++++++++++++ 9 files changed, 85 insertions(+), 16 deletions(-) diff --git a/test_src/main-threefish-test.c b/test_src/main-threefish-test.c index 668eca0..7d05eb8 100644 --- a/test_src/main-threefish-test.c +++ b/test_src/main-threefish-test.c @@ -41,10 +41,22 @@ char* algo_name = "Threefish"; * additional validation-functions * *****************************************************************************/ +void threefish256_dump(threefish256_ctx_t* ctx){ + uint8_t i; + cli_putstr_P(PSTR("\r\n=== ctx dump (256) === \r\n k: ")); + for(i=0; i<5; ++i){ + cli_hexdump(&(ctx->k[i]), 8); + cli_putc(' '); + } + cli_putstr_P(PSTR("\r\n t: ")); + for(i=0; i<3; ++i){ + cli_hexdump(&(ctx->t[i]), 8); + cli_putc(' '); + } +} + 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); + threefish256_init(key, NULL, ctx); } void testrun_nessie_threefish256(void){ @@ -61,9 +73,7 @@ void testrun_nessie_threefish256(void){ } void threefish512_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){ - uint8_t null[16]; - memset(null, 0, 16); - threefish512_init(key, null, ctx); + threefish512_init(key, NULL, ctx); } void testrun_nessie_threefish512(void){ @@ -80,9 +90,7 @@ void testrun_nessie_threefish512(void){ } void threefish1024_dummy_init(const uint8_t* key, uint16_t keysize_b, void* ctx){ - uint8_t null[16]; - memset(null, 0, 16); - threefish1024_init(key, null, ctx); + threefish1024_init(key, NULL, ctx); } void testrun_nessie_threefish1024(void){ diff --git a/threefish1024_enc.c b/threefish1024_enc.c index 0bb8e9b..41a3590 100644 --- a/threefish1024_enc.c +++ b/threefish1024_enc.c @@ -62,13 +62,17 @@ void permute_16(void* data){ 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); + if(tweak){ + memcpy(ctx->t, tweak, 2*8); + ctx->t[2] = T(0) ^ T(1); + }else{ + memset(ctx, 0, 3*8); + } uint8_t i; ctx->k[16] = THREEFISH_KEY_CONST; for(i=0; i<16; ++i){ ctx->k[16] ^= K(i); } - ctx->t[2] = T(0) ^ T(1); } static diff --git a/threefish1024_enc_asm.S b/threefish1024_enc_asm.S index bb75f04..38bf04c 100644 --- a/threefish1024_enc_asm.S +++ b/threefish1024_enc_asm.S @@ -103,6 +103,17 @@ threefish1024_init: st Z+, A7 /* now the tweak */ movw r26, r22 + tst r27 + brne 3f + tst r26 + brne 3f + ldi r26, 3*8 +1: + st Z+, r1 + dec r26 + brne 1b + rjmp 9f +3: ld A0, X+ ld A1, X+ ld A2, X+ @@ -151,6 +162,7 @@ threefish1024_init: st Z+, A5 st Z+, A6 st Z+, A7 +9: pop_range 14, 17 ret diff --git a/threefish256_dec_asm.S b/threefish256_dec_asm.S index e55ca68..d2316c6 100644 --- a/threefish256_dec_asm.S +++ b/threefish256_dec_asm.S @@ -152,7 +152,7 @@ threefish256_dec: sbc r0, r1 st X+, r0 ld r0, X - adc r0, r1 + sbc r0, r1 st X+, r0 tst S brne 3f diff --git a/threefish256_enc.c b/threefish256_enc.c index 2739f6a..8bfbf9d 100644 --- a/threefish256_enc.c +++ b/threefish256_enc.c @@ -46,13 +46,17 @@ void permute_4(void* data){ 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); + if(tweak){ + memcpy(ctx->t, tweak, 2*8); + ctx->t[2] = T(0) ^ T(1); + }else{ + memset(ctx->t, 0, 3*8); + } uint8_t i; ctx->k[4] = THREEFISH_KEY_CONST; for(i=0; i<4; ++i){ ctx->k[4] ^= K(i); } - ctx->t[2] = T(0) ^ T(1); } static diff --git a/threefish256_enc_asm.S b/threefish256_enc_asm.S index ccd457d..f18e7f4 100644 --- a/threefish256_enc_asm.S +++ b/threefish256_enc_asm.S @@ -103,6 +103,17 @@ threefish256_init: st Z+, A6 st Z+, A7 /* now the tweak */ + tst r23 + brne 3f + tst r22 + brne 3f + ldi r26, 3*8 +2: + st Z+, r1 + dec r26 + brne 2b + rjmp 9f +3: movw r26, r22 ld A0, X+ ld A1, X+ @@ -144,6 +155,7 @@ threefish256_init: ld r0, X+ eor A7, r0 st Z+, r0 + st Z+, A0 st Z+, A1 st Z+, A2 @@ -152,6 +164,7 @@ threefish256_init: st Z+, A5 st Z+, A6 st Z+, A7 +9: pop_range 14, 17 ret diff --git a/threefish256_enc_small.S b/threefish256_enc_small.S index c1b1152..da08359 100644 --- a/threefish256_enc_small.S +++ b/threefish256_enc_small.S @@ -104,6 +104,17 @@ threefish256_init: st Z+, A7 /* now the tweak */ movw r26, r22 + tst r27 + brne 3f + tst r26 + brne 3f + ldi r26, 3*8 +1: + st Z+, r1 + dec r26 + brne 1b + rjmp 9f +3: ld A0, X+ ld A1, X+ ld A2, X+ @@ -152,6 +163,7 @@ threefish256_init: st Z+, A5 st Z+, A6 st Z+, A7 +9: pop_range 14, 17 ret diff --git a/threefish512_enc.c b/threefish512_enc.c index fb044b5..620276f 100644 --- a/threefish512_enc.c +++ b/threefish512_enc.c @@ -68,13 +68,17 @@ void permute_inv8(void* data){ 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); + if(tweak){ + memcpy(ctx->t, tweak, 2*8); + ctx->t[2] = T(0) ^ T(1); + }else{ + memset(ctx->t, 0, 3*8); + } uint8_t i; ctx->k[8] = THREEFISH_KEY_CONST; for(i=0; i<8; ++i){ ctx->k[8] ^= K(i); } - ctx->t[2] = T(0) ^ T(1); } static diff --git a/threefish512_enc_asm.S b/threefish512_enc_asm.S index bb6ff2d..cc2967f 100644 --- a/threefish512_enc_asm.S +++ b/threefish512_enc_asm.S @@ -103,6 +103,17 @@ threefish512_init: st Z+, A7 /* now the tweak */ movw r26, r22 + tst r27 + brne 3f + tst r26 + brne 3f + ldi r26, 3*8 +1: + st Z+, r1 + dec r26 + brne 1b + rjmp 9f +3: ld A0, X+ ld A1, X+ ld A2, X+ @@ -151,6 +162,7 @@ threefish512_init: st Z+, A5 st Z+, A6 st Z+, A7 +9: pop_range 14, 17 ret