X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=twister.c;h=f57ca328fcd596a7a33c62f6a8936d42c8d699b5;hb=0076b72ccd398bf7571c5144ba36eda8a24f6c70;hp=a3ab4b507bd11338b5da8ec002dfbd24f139fd00;hpb=cca97d0e0c937aea8b7e0ae5b931da5f80d9ff52;p=avr-crypto-lib.git diff --git a/twister.c b/twister.c index a3ab4b5..f57ca32 100644 --- a/twister.c +++ b/twister.c @@ -17,6 +17,7 @@ along with this program. If not, see . */ +#include "config.h" #include #include #include @@ -24,9 +25,11 @@ #include "twister_tables.h" #include "memxor.h" -#include "gf256mul.h" +#ifndef TWISTER_MUL_TABLE +# include "gf256mul.h" +#endif -//#define DEBUG +#undef DEBUG #ifdef DEBUG # include "uart.h" @@ -41,7 +44,7 @@ #ifdef DEBUG void print_twister_state(twister_state_t* ctx){ - uint8_t i,j; + uint8_t i; uart_putstr_P(PSTR("\r\nState:\r\n matrix:\r\n")); for(i=0; i<8; ++i){ uart_putstr_P(PSTR("\t[ ")); @@ -70,10 +73,13 @@ void shiftrow(void* row, uint8_t shift){ } #define MDS(a,b) pgm_read_byte(&(twister_mds[a][b])) -//#define MULT(a,b) pgm_read_byte(&(twister_multab[a-1][b])) -#define MULT(a,b) gf256mul(a,b, 0x4D) -void blank_round(twister_state_t* ctx){ +#ifdef TWISTER_MUL_TABLE +# define MULT(a,b) pgm_read_byte(&(twister_multab[a][b])) +#else +# define MULT(a,b) gf256mul(a,b, 0x4D) +#endif +void twister_blank_round(twister_state_t* ctx){ uint8_t i,j; uint8_t tmp[8][8]; DEBUG_PRINT(ctx, "blank init"); @@ -91,35 +97,59 @@ void blank_round(twister_state_t* ctx){ shiftrow(&(tmp[i][0]), i); } /* mix columns */ - for( i=0; i<8; i++ ){ - // multiply with mds matrix - for( j=0; j<8; j++ ){ - ctx->s[j][i] = - MULT( MDS(j,0), tmp[0][i] ) ^ - MULT( MDS(j,1), tmp[1][i] ) ^ - MULT( MDS(j,2), tmp[2][i] ) ^ - MULT( MDS(j,3), tmp[3][i] ) ^ - MULT( MDS(j,4), tmp[4][i] ) ^ - MULT( MDS(j,5), tmp[5][i] ) ^ - MULT( MDS(j,6), tmp[6][i] ) ^ - MULT( MDS(j,7), tmp[7][i] ) ; - - } + for( i=0; i<8; i++ ){ + // multiply with mds matrix + for( j=0; j<8; j++ ){ + ctx->s[j][i] = + MULT( MDS(j,0), tmp[0][i] ) ^ + MULT( MDS(j,1), tmp[1][i] ) ^ + MULT( MDS(j,2), tmp[2][i] ) ^ + MULT( MDS(j,3), tmp[3][i] ) ^ + MULT( MDS(j,4), tmp[4][i] ) ^ + MULT( MDS(j,5), tmp[5][i] ) ^ + MULT( MDS(j,6), tmp[6][i] ) ^ + MULT( MDS(j,7), tmp[7][i] ) ; + + } } DEBUG_PRINT(ctx, "post MDS"); } -void mini_round(twister_state_t* ctx, void* msg){ +void twister_mini_round(twister_state_t* ctx, void* msg){ /* inject message */ uint8_t i; for(i=0; i<8; ++i){ ctx->s[7][7-i] ^= *((uint8_t*)msg); msg = (uint8_t*)msg +1; } - blank_round(ctx); + twister_blank_round(ctx); } - - +void twister_ctx2hash(void* dest, twister_state_t* ctx, uint16_t hashsize_b){ + uint8_t tmp[8][8]; + uint8_t j; + uint16_t i=hashsize_b; + while(i>=64){ + i-=64; + memcpy(tmp,ctx->s, 64); + twister_blank_round(ctx); + memxor(ctx->s, tmp, 64); + twister_blank_round(ctx); + for(j=0; j<8; ++j){ + *((uint8_t*)dest) = ctx->s[7-j][0] ^ tmp[7-j][0]; + dest = (uint8_t*)dest + 1; + } + } + if(i>=32){ + memcpy(tmp,ctx->s, 64); + twister_blank_round(ctx); + memxor(ctx->s, tmp, 64); + twister_blank_round(ctx); + for(j=0; j<4; ++j){ + *((uint8_t*)dest) = ctx->s[3-j][0] ^ tmp[3-j][0]; + dest = (uint8_t*)dest + 1; + } + } +}