]> git.cryptolib.org Git - avr-crypto-lib.git/blob - twister.c
a3ab4b507bd11338b5da8ec002dfbd24f139fd00
[avr-crypto-lib.git] / twister.c
1 /* twister.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19
20 #include <stdint.h>
21 #include <string.h>
22 #include <avr/pgmspace.h>
23 #include "twister.h"
24 #include "twister_tables.h"
25 #include "memxor.h"
26
27 #include "gf256mul.h"
28
29 //#define DEBUG
30
31 #ifdef DEBUG
32 # include "uart.h"
33 #endif
34
35 #ifdef DEBUG
36 # define DEBUG_PRINT(ctx, msg) debug_print((ctx), PSTR(msg)) 
37 #else
38 # define DEBUG_PRINT(ctx, msg) 
39 #endif 
40
41 #ifdef DEBUG
42
43 void print_twister_state(twister_state_t* ctx){
44         uint8_t i,j;
45         uart_putstr_P(PSTR("\r\nState:\r\n matrix:\r\n"));
46         for(i=0; i<8; ++i){
47                 uart_putstr_P(PSTR("\t[ "));
48                 uart_hexdump(&(ctx->s[i][0]), 8);
49                 uart_putstr_P(PSTR("]\r\n"));
50         }
51         uart_putstr_P(PSTR("counter: "));
52         uart_hexdump(&(ctx->counter), 8);
53
54         uart_putstr_P(PSTR("\r\nlength_counter_b: "));
55         uart_hexdump(&(ctx->length_counter_b), 8);
56         uart_putstr_P(PSTR("\r\n"));
57
58
59 void debug_print(twister_state_t* ctx, PGM_P msg){
60         uart_putstr_P(PSTR("\r\n"));
61         uart_putstr_P(msg);
62         print_twister_state(ctx);
63 }
64
65 #endif
66
67 static
68 void shiftrow(void* row, uint8_t shift){
69         *((uint64_t*)row) = *((uint64_t*)row)>>(8*shift) | *((uint64_t*)row)<<(64-8*shift);
70 }
71
72 #define MDS(a,b)  pgm_read_byte(&(twister_mds[a][b]))
73 //#define MULT(a,b) pgm_read_byte(&(twister_multab[a-1][b]))
74 #define MULT(a,b) gf256mul(a,b, 0x4D)
75
76 void blank_round(twister_state_t* ctx){
77         uint8_t i,j;
78         uint8_t tmp[8][8];
79         DEBUG_PRINT(ctx, "blank init");
80         /* add twist counter */
81         for(i=0; i<8; ++i)
82                 ctx->s[i][1] ^= ((uint8_t*)&(ctx->counter))[7-i];
83         ctx->counter--;
84 //      DEBUG_PRINT(ctx, "counter added");
85         /* sub bytes */
86         for(i=0; i<8; ++i)
87                 for(j=0;j<8;++j)
88                         tmp[i][j] = pgm_read_byte(twister_sbox+ctx->s[i][j]);
89         /* shift rows */
90         for(i=1;i<8; ++i){
91                 shiftrow(&(tmp[i][0]), i);
92         }
93         /* mix columns */
94         for( i=0; i<8; i++ ){\r
95                 // multiply with mds matrix\r
96                 for( j=0; j<8; j++ ){\r
97                         ctx->s[j][i] =\r
98                                 MULT( MDS(j,0), tmp[0][i] ) ^\r
99                                 MULT( MDS(j,1), tmp[1][i] ) ^\r
100                                 MULT( MDS(j,2), tmp[2][i] ) ^\r
101                                 MULT( MDS(j,3), tmp[3][i] ) ^\r
102                                 MULT( MDS(j,4), tmp[4][i] ) ^\r
103                                 MULT( MDS(j,5), tmp[5][i] ) ^\r
104                                 MULT( MDS(j,6), tmp[6][i] ) ^\r
105                                 MULT( MDS(j,7), tmp[7][i] ) ;\r
106                                 \r
107                 }       \r
108         }
109         DEBUG_PRINT(ctx, "post MDS");
110 }
111
112 void mini_round(twister_state_t* ctx, void* msg){
113         /* inject message */
114         uint8_t i;
115         for(i=0; i<8; ++i){
116                 ctx->s[7][7-i] ^= *((uint8_t*)msg);
117                 msg = (uint8_t*)msg +1; 
118         }
119         blank_round(ctx);
120 }
121
122
123
124
125