X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=twister.c;h=494a7a11f72cfd44bc7efad4d777ecf0bcf9cc50;hb=96789d49fd89502f9c20dbc0611e401b1a417880;hp=a3ab4b507bd11338b5da8ec002dfbd24f139fd00;hpb=4469def3e210fc904cfefc5b158fc6d4dc5f6e47;p=avr-crypto-lib.git
diff --git a/twister.c b/twister.c
index a3ab4b5..494a7a1 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,102 +25,86 @@
#include "twister_tables.h"
#include "memxor.h"
-#include "gf256mul.h"
-
-//#define DEBUG
-
-#ifdef DEBUG
-# include "uart.h"
+#ifndef TWISTER_MUL_TABLE
+# include "gf256mul.h"
#endif
-#ifdef DEBUG
-# define DEBUG_PRINT(ctx, msg) debug_print((ctx), PSTR(msg))
-#else
-# define DEBUG_PRINT(ctx, msg)
-#endif
-
-#ifdef DEBUG
-
-void print_twister_state(twister_state_t* ctx){
- uint8_t i,j;
- uart_putstr_P(PSTR("\r\nState:\r\n matrix:\r\n"));
- for(i=0; i<8; ++i){
- uart_putstr_P(PSTR("\t[ "));
- uart_hexdump(&(ctx->s[i][0]), 8);
- uart_putstr_P(PSTR("]\r\n"));
- }
- uart_putstr_P(PSTR("counter: "));
- uart_hexdump(&(ctx->counter), 8);
-
- uart_putstr_P(PSTR("\r\nlength_counter_b: "));
- uart_hexdump(&(ctx->length_counter_b), 8);
- uart_putstr_P(PSTR("\r\n"));
-}
-
-void debug_print(twister_state_t* ctx, PGM_P msg){
- uart_putstr_P(PSTR("\r\n"));
- uart_putstr_P(msg);
- print_twister_state(ctx);
-}
+#define MDS(a,b) pgm_read_byte(&(twister_mds[(a)][(b)]))
+#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
-static
-void shiftrow(void* row, uint8_t shift){
- *((uint64_t*)row) = *((uint64_t*)row)>>(8*shift) | *((uint64_t*)row)<<(64-8*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){
- uint8_t i,j;
+void twister_blank_round(twister_state_t* ctx){
+ uint8_t i,j,k;
uint8_t tmp[8][8];
- DEBUG_PRINT(ctx, "blank init");
/* add twist counter */
- for(i=0; i<8; ++i)
+ for(i=0; i<8; ++i){
ctx->s[i][1] ^= ((uint8_t*)&(ctx->counter))[7-i];
+ }
ctx->counter--;
-// DEBUG_PRINT(ctx, "counter added");
/* sub bytes */
- for(i=0; i<8; ++i)
- for(j=0;j<8;++j)
+ for(i=0; i<8; ++i){
+ for(j=0;j<8;++j){
tmp[i][j] = pgm_read_byte(twister_sbox+ctx->s[i][j]);
- /* shift rows */
- for(i=1;i<8; ++i){
- 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] ) ;
-
- }
+ /* mix columns with integrates shift rows */
+ for( i=0; i<8; i++ ){
+ // multiply with mds matrix
+ for( j=0; j<8; j++ ){
+ k=(i+1)&7;
+ ctx->s[j][i] =
+ MULT( MDS(j,0), tmp[0][i] ) ^
+ MULT( MDS(j,1), tmp[1][k] ) ^
+ MULT( MDS(j,2), tmp[2][(++k)&7] ) ^
+ MULT( MDS(j,3), tmp[3][(++k)&7] ) ^
+ MULT( MDS(j,4), tmp[4][(++k)&7] ) ^
+ MULT( MDS(j,5), tmp[5][(++k)&7] ) ^
+ MULT( MDS(j,6), tmp[6][(++k)&7] ) ^
+ MULT( MDS(j,7), tmp[7][(++k)&7] ) ;
+
+ }
}
- DEBUG_PRINT(ctx, "post MDS");
}
-void mini_round(twister_state_t* ctx, void* msg){
+void twister_mini_round(twister_state_t* ctx, const 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;
+ }
+ }
+}