]> git.cryptolib.org Git - avr-crypto-lib.git/blob - twister-big.c
twister now in ASM
[avr-crypto-lib.git] / twister-big.c
1 /* twister-big.c */
2
3 #include <stdint.h>
4 #include <string.h>
5 #include "memxor.h"
6 #include "twister.h"
7 #include "twister-big.h"
8
9 #undef DEBUG
10 #define DEBUG
11
12 /*********************************************************************/
13 /*********************************************************************/
14
15 #ifdef DEBUG
16 #include <avr/pgmspace.h>
17 #include "uart.h"
18 void print_checksum(twister_big_ctx_t* ctx, PGM_P s){
19         uint8_t i;
20         uart_putstr_P(PSTR("\r\n"));
21         uart_putstr_P(s);
22         uart_putstr_P(PSTR("\r\n checksum:\r\n"));
23         for(i=0; i<8; ++i){
24                 uart_putstr_P(PSTR("  [ "));
25                 uart_hexdump(&(ctx->checksum[i][0]), 8);
26                 uart_putstr_P(PSTR("]\r\n"));
27         }
28 }
29
30 /*********************************************************************/
31
32 void print_matrix(void* m, PGM_P s){
33         uint8_t i;
34         uart_putstr_P(PSTR("\r\n"));
35         uart_putstr_P(s);
36         uart_putstr_P(PSTR("\r\n matrix:\r\n"));
37         for(i=0; i<8; ++i){
38                 uart_putstr_P(PSTR("  [ "));
39                 uart_hexdump(((uint8_t*)m)+i*8, 8);
40                 uart_putstr_P(PSTR("]\r\n"));
41         }
42 }
43
44 /*********************************************************************/
45
46 #define DEBUG_CHKSUM(a,s) print_checksum((a),PSTR(s)) 
47 #else
48 #define DEBUG_CHKSUM(a,s) 
49 #endif
50
51
52 #ifdef DEBUG
53 # define DEBUG_PRINT(ctx, msg) debug_print((ctx), PSTR(msg)) 
54 #else
55 # define DEBUG_PRINT(ctx, msg) 
56 #endif 
57
58 #ifdef DEBUG
59
60 /*********************************************************************/
61
62 void print_twister_state(twister_state_t* ctx){
63         uint8_t i;
64         uart_putstr_P(PSTR("\r\nState:\r\n matrix:\r\n"));
65         for(i=0; i<8; ++i){
66                 uart_putstr_P(PSTR("\t[ "));
67                 uart_hexdump(&(ctx->s[i][0]), 8);
68                 uart_putstr_P(PSTR("]\r\n"));
69         }
70         uart_putstr_P(PSTR("counter: "));
71         uart_hexdump(&(ctx->counter), 8);
72
73         uart_putstr_P(PSTR("\r\nlength_counter_b: "));
74         uart_hexdump(&(ctx->length_counter_b), 8);
75         uart_putstr_P(PSTR("\r\n"));
76
77
78 /*********************************************************************/
79
80 void debug_print(twister_state_t* ctx, PGM_P msg){
81         uart_putstr_P(PSTR("\r\n"));
82         uart_putstr_P(msg);
83         print_twister_state(ctx);
84 }
85
86 #endif
87
88 /*********************************************************************/
89
90 static
91 void checksum_update(twister_big_ctx_t* ctx, uint8_t col){
92         uint8_t i, col2;
93         uint8_t carry = 0;
94         int sum = 0;
95         
96         col2 = (col+1)%8; 
97         
98         for( i=0; i<8; i++ ) 
99         {
100                 sum =  (int) ctx->checksum[col2][7-i] 
101                      + (int) ctx->state.s[7-i][0] /* col or 0 ???*/ 
102                      + carry;
103                 ctx->checksum[col][7-i] ^= (uint8_t)sum;
104                 carry = sum>>8;
105
106         }
107 //      DEBUG_CHKSUM(ctx, "post run");
108 }
109
110 /*********************************************************************/
111
112 void twister_big_init(twister_big_ctx_t* ctx, uint16_t hashsize_b){
113         memset(ctx->state.s,  0, 64);
114         memset(ctx->checksum, 0, 64);
115         ctx->state.counter=0xffffffffffffffffLL;
116         ctx->state.s[0][7] = hashsize_b>>8;
117         ctx->state.s[1][7] = hashsize_b&0xff;
118         ctx->state.length_counter_b = 0;
119 }
120
121 /*********************************************************************/
122
123 void twister_big_nextBlock(twister_big_ctx_t* ctx, void* msg){
124         uint8_t tmp[8][8];
125
126         /* 1st maxi round */
127         memcpy(tmp, ctx->state.s, 64);  
128         checksum_update(ctx, 0);
129         twister_mini_round(&(ctx->state), msg);
130         msg = (uint8_t*)msg + 8;
131
132         checksum_update(ctx, 1);
133         twister_mini_round(&(ctx->state), msg);
134         msg = (uint8_t*)msg + 8;
135         
136         checksum_update(ctx, 2);
137         twister_mini_round(&(ctx->state), msg);
138         msg = (uint8_t*)msg + 8;
139         memxor(ctx->state.s, tmp, 64);
140
141         /* 2nd maxi round */
142         memcpy(tmp, ctx->state.s, 64);  
143         checksum_update(ctx, 3);
144         twister_mini_round(&(ctx->state), msg);
145         msg = (uint8_t*)msg + 8;
146
147         twister_blank_round(&(ctx->state));
148         
149         checksum_update(ctx, 4);
150         twister_mini_round(&(ctx->state), msg);
151         msg = (uint8_t*)msg + 8;
152         memxor(ctx->state.s, tmp, 64);
153
154         /* 3rd maxi round */
155         memcpy(tmp, ctx->state.s, 64);  
156         checksum_update(ctx, 5);
157         twister_mini_round(&(ctx->state), msg);
158         msg = (uint8_t*)msg + 8;
159
160         checksum_update(ctx, 6);
161         twister_mini_round(&(ctx->state), msg);
162         msg = (uint8_t*)msg + 8;
163
164         checksum_update(ctx, 7);
165         twister_mini_round(&(ctx->state), msg);
166
167         twister_blank_round(&(ctx->state));     
168         memxor(ctx->state.s, tmp, 64);
169         ctx->state.length_counter_b += 512;
170 }
171
172 /*********************************************************************/
173
174 void twister_inject_chksum(twister_big_ctx_t* ctx, uint8_t col){
175         *((uint64_t*)(&ctx->state.s[7][0])) ^= *((uint64_t*)(&ctx->checksum[col][0]));
176         twister_blank_round(&ctx->state);
177 }
178
179 /*********************************************************************/
180
181 void twister_big_lastBlock(twister_big_ctx_t* ctx, void* msg, uint16_t length_b){
182         uint8_t tmp[64];        
183         while(length_b>512){
184                 twister_big_nextBlock(ctx, msg);
185                 msg = ((uint8_t*)msg)+64;
186                 length_b -= 512;
187         }
188         memset(tmp, 0, 64);
189         memcpy(tmp, msg, (length_b+7)/8);
190         tmp[length_b/8] |= 0x80 >> (length_b&0x07);
191         twister_big_nextBlock(ctx, tmp);
192         ctx->state.length_counter_b -= 512 - length_b;
193         twister_mini_round(&(ctx->state), &(ctx->state.length_counter_b));
194
195 //      DEBUG_PRINT(&(ctx->state), "pre check-round");
196
197         memcpy(tmp, ctx->state.s, 64);
198         twister_inject_chksum(ctx, 0);
199         twister_inject_chksum(ctx, 1);
200         twister_inject_chksum(ctx, 2);
201         memxor(ctx->state.s, tmp, 64);
202
203         memcpy(tmp, ctx->state.s, 64);
204         twister_inject_chksum(ctx, 3);
205         twister_inject_chksum(ctx, 4);
206         twister_inject_chksum(ctx, 5);
207         memxor(ctx->state.s, tmp, 64);
208
209         memcpy(tmp, ctx->state.s, 64);
210         twister_inject_chksum(ctx, 6);
211         twister_inject_chksum(ctx, 7);
212         twister_blank_round(&(ctx->state));
213         memxor(ctx->state.s, tmp, 64);
214
215
216 //      DEBUG_PRINT(&(ctx->state), "post check-round");
217 }
218
219 /*********************************************************************/
220
221 void twister_big_ctx2hash(void* dest, twister_big_ctx_t* ctx, uint16_t hashsize_b){
222         twister_ctx2hash(dest, &(ctx->state), hashsize_b);
223 }
224
225 /*********************************************************************/
226 /*********************************************************************/
227
228 void twister384_init(twister384_ctx_t* ctx){
229         twister_big_init(ctx, 384);
230 }
231
232 /*********************************************************************/
233
234 void twister384_nextBlock(twister384_ctx_t* ctx, void* msg){
235         twister_big_nextBlock(ctx, msg);
236 }
237
238 /*********************************************************************/
239
240 void twister384_lastBlock(twister384_ctx_t* ctx, void* msg, uint16_t length_b){
241         twister_big_lastBlock(ctx, msg, length_b);
242 }
243
244 /*********************************************************************/
245
246 void twister384_ctx2hash(void* dest, twister384_ctx_t* ctx){
247         twister_big_ctx2hash(dest, ctx, 384);
248 }
249
250 /*********************************************************************/
251
252 void twister384(void* dest, void* msg, uint32_t msg_length_b){
253         twister_big_ctx_t ctx;
254         twister_big_init(&ctx, 384);
255         while(msg_length_b >=512){
256                 twister_big_nextBlock(&ctx, msg);
257                 msg = (uint8_t*)msg + 512/8;
258                 msg_length_b -= 512;
259         }
260         twister_big_lastBlock(&ctx, msg, msg_length_b);
261         twister_big_ctx2hash(dest, &ctx, 384);
262 }
263
264 /*********************************************************************/
265 /*********************************************************************/
266
267
268 void twister512_init(twister512_ctx_t* ctx){
269         twister_big_init(ctx, 512);
270 }
271
272 /*********************************************************************/
273
274 void twister512_nextBlock(twister512_ctx_t* ctx, void* msg){
275         twister_big_nextBlock(ctx, msg);
276 }
277
278 /*********************************************************************/
279
280 void twister512_lastBlock(twister512_ctx_t* ctx, void* msg, uint16_t length_b){
281         twister_big_lastBlock(ctx, msg, length_b);
282 }
283
284 /*********************************************************************/
285
286 void twister512_ctx2hash(void* dest, twister512_ctx_t* ctx){
287         twister_big_ctx2hash(dest, ctx, 512);
288 }
289
290 /*********************************************************************/
291
292 void twister512(void* dest, void* msg, uint32_t msg_length_b){
293         twister_big_ctx_t ctx;
294         twister_big_init(&ctx, 512);
295         while(msg_length_b >=512){
296                 twister_big_nextBlock(&ctx, msg);
297                 msg = (uint8_t*)msg + 512/8;
298                 msg_length_b -= 512;
299         }
300         twister_big_lastBlock(&ctx, msg, msg_length_b);
301         twister_big_ctx2hash(dest, &ctx, 512);
302 }
303
304
305