3 This file is part of the Crypto-avr-lib/microcrypt-lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <avr/pgmspace.h>
25 #include "twister_tables.h"
28 #ifndef TWISTER_MUL_TABLE
29 # include "gf256mul.h"
33 void shiftrow(void* row, uint8_t shift){
34 *((uint64_t*)row) = *((uint64_t*)row)>>(8*shift) | *((uint64_t*)row)<<(64-8*shift);
37 #define MDS(a,b) pgm_read_byte(&(twister_mds[(a)][(b)]))
39 #ifdef TWISTER_MUL_TABLE
40 # define MULT(a,b) pgm_read_byte(&(twister_multab[a][b]))
42 # define MULT(a,b) gf256mul((a),(b), 0x4D)
44 void twister_blank_round(twister_state_t* ctx){
47 /* add twist counter */
49 ctx->s[i][1] ^= ((uint8_t*)&(ctx->counter))[7-i];
55 tmp[i][j] = pgm_read_byte(twister_sbox+ctx->s[i][j]);
60 // shiftrow(&(tmp[i][0]), i);
64 // multiply with mds matrix
68 MULT( MDS(j,0), tmp[0][i] ) ^
69 MULT( MDS(j,1), tmp[1][k] ) ^
70 MULT( MDS(j,2), tmp[2][(++k)&7] ) ^
71 MULT( MDS(j,3), tmp[3][(++k)&7] ) ^
72 MULT( MDS(j,4), tmp[4][(++k)&7] ) ^
73 MULT( MDS(j,5), tmp[5][(++k)&7] ) ^
74 MULT( MDS(j,6), tmp[6][(++k)&7] ) ^
75 MULT( MDS(j,7), tmp[7][(++k)&7] ) ;
81 void twister_mini_round(twister_state_t* ctx, void* msg){
85 ctx->s[7][7-i] ^= *((uint8_t*)msg);
86 msg = (uint8_t*)msg +1;
88 twister_blank_round(ctx);
91 void twister_ctx2hash(void* dest, twister_state_t* ctx, uint16_t hashsize_b){
94 uint16_t i=hashsize_b;
97 memcpy(tmp,ctx->s, 64);
98 twister_blank_round(ctx);
99 memxor(ctx->s, tmp, 64);
100 twister_blank_round(ctx);
102 *((uint8_t*)dest) = ctx->s[7-j][0] ^ tmp[7-j][0];
103 dest = (uint8_t*)dest + 1;
107 memcpy(tmp,ctx->s, 64);
108 twister_blank_round(ctx);
109 memxor(ctx->s, tmp, 64);
110 twister_blank_round(ctx);
112 *((uint8_t*)dest) = ctx->s[3-j][0] ^ tmp[3-j][0];
113 dest = (uint8_t*)dest + 1;