7 #include "twister-big.h"
12 /*********************************************************************/
13 /*********************************************************************/
16 #include <avr/pgmspace.h>
18 void print_checksum(twister_big_ctx_t* ctx, PGM_P s){
20 uart_putstr_P(PSTR("\r\n"));
22 uart_putstr_P(PSTR("\r\n checksum:\r\n"));
24 uart_putstr_P(PSTR(" [ "));
25 uart_hexdump(&(ctx->checksum[i][0]), 8);
26 uart_putstr_P(PSTR("]\r\n"));
30 /*********************************************************************/
32 void print_matrix(void* m, PGM_P s){
34 uart_putstr_P(PSTR("\r\n"));
36 uart_putstr_P(PSTR("\r\n matrix:\r\n"));
38 uart_putstr_P(PSTR(" [ "));
39 uart_hexdump(((uint8_t*)m)+i*8, 8);
40 uart_putstr_P(PSTR("]\r\n"));
44 /*********************************************************************/
46 #define DEBUG_CHKSUM(a,s) print_checksum((a),PSTR(s))
48 #define DEBUG_CHKSUM(a,s)
53 # define DEBUG_PRINT(ctx, msg) debug_print((ctx), PSTR(msg))
55 # define DEBUG_PRINT(ctx, msg)
60 /*********************************************************************/
62 void print_twister_state(twister_state_t* ctx){
64 uart_putstr_P(PSTR("\r\nState:\r\n matrix:\r\n"));
66 uart_putstr_P(PSTR("\t[ "));
67 uart_hexdump(&(ctx->s[i][0]), 8);
68 uart_putstr_P(PSTR("]\r\n"));
70 uart_putstr_P(PSTR("counter: "));
71 uart_hexdump(&(ctx->counter), 8);
73 uart_putstr_P(PSTR("\r\nlength_counter_b: "));
74 uart_hexdump(&(ctx->length_counter_b), 8);
75 uart_putstr_P(PSTR("\r\n"));
78 /*********************************************************************/
80 void debug_print(twister_state_t* ctx, PGM_P msg){
81 uart_putstr_P(PSTR("\r\n"));
83 print_twister_state(ctx);
88 /*********************************************************************/
91 void checksum_update(twister_big_ctx_t* ctx, uint8_t col){
100 sum = (int) ctx->checksum[col2][7-i]
101 + (int) ctx->state.s[7-i][0] /* col or 0 ???*/
103 ctx->checksum[col][7-i] ^= (uint8_t)sum;
107 // DEBUG_CHKSUM(ctx, "post run");
110 /*********************************************************************/
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;
121 /*********************************************************************/
123 void twister_big_nextBlock(twister_big_ctx_t* ctx, void* msg){
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;
132 checksum_update(ctx, 1);
133 twister_mini_round(&(ctx->state), msg);
134 msg = (uint8_t*)msg + 8;
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);
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;
147 twister_blank_round(&(ctx->state));
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);
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;
160 checksum_update(ctx, 6);
161 twister_mini_round(&(ctx->state), msg);
162 msg = (uint8_t*)msg + 8;
164 checksum_update(ctx, 7);
165 twister_mini_round(&(ctx->state), msg);
167 twister_blank_round(&(ctx->state));
168 memxor(ctx->state.s, tmp, 64);
169 ctx->state.length_counter_b += 512;
172 /*********************************************************************/
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);
179 /*********************************************************************/
181 void twister_big_lastBlock(twister_big_ctx_t* ctx, void* msg, uint16_t length_b){
184 twister_big_nextBlock(ctx, msg);
185 msg = ((uint8_t*)msg)+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));
195 // DEBUG_PRINT(&(ctx->state), "pre check-round");
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);
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);
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);
216 // DEBUG_PRINT(&(ctx->state), "post check-round");
219 /*********************************************************************/
221 void twister_big_ctx2hash(void* dest, twister_big_ctx_t* ctx, uint16_t hashsize_b){
222 twister_ctx2hash(dest, &(ctx->state), hashsize_b);
225 /*********************************************************************/
226 /*********************************************************************/
228 void twister384_init(twister384_ctx_t* ctx){
229 twister_big_init(ctx, 384);
232 /*********************************************************************/
234 void twister384_nextBlock(twister384_ctx_t* ctx, void* msg){
235 twister_big_nextBlock(ctx, msg);
238 /*********************************************************************/
240 void twister384_lastBlock(twister384_ctx_t* ctx, void* msg, uint16_t length_b){
241 twister_big_lastBlock(ctx, msg, length_b);
244 /*********************************************************************/
246 void twister384_ctx2hash(void* dest, twister384_ctx_t* ctx){
247 twister_big_ctx2hash(dest, ctx, 384);
250 /*********************************************************************/
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;
260 twister_big_lastBlock(&ctx, msg, msg_length_b);
261 twister_big_ctx2hash(dest, &ctx, 384);
264 /*********************************************************************/
265 /*********************************************************************/
268 void twister512_init(twister512_ctx_t* ctx){
269 twister_big_init(ctx, 512);
272 /*********************************************************************/
274 void twister512_nextBlock(twister512_ctx_t* ctx, void* msg){
275 twister_big_nextBlock(ctx, msg);
278 /*********************************************************************/
280 void twister512_lastBlock(twister512_ctx_t* ctx, void* msg, uint16_t length_b){
281 twister_big_lastBlock(ctx, msg, length_b);
284 /*********************************************************************/
286 void twister512_ctx2hash(void* dest, twister512_ctx_t* ctx){
287 twister_big_ctx2hash(dest, ctx, 512);
290 /*********************************************************************/
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;
300 twister_big_lastBlock(&ctx, msg, msg_length_b);
301 twister_big_ctx2hash(dest, &ctx, 512);