]> git.cryptolib.org Git - avr-crypto-lib.git/blob - twister/twister-large.c
1775488ef3c128e30bb932363af4bfe892e6400a
[avr-crypto-lib.git] / twister / twister-large.c
1 /* twister-large.c */
2 /*
3     This file is part of the AVR-Crypto-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 "memxor.h"
23 #include "twister.h"
24 #include "twister-large.h"
25
26 /*********************************************************************/
27
28 static
29 void checksum_update(twister_large_ctx_t* ctx, uint8_t col){
30         uint8_t i, col2;
31         uint8_t carry = 0;
32         int sum = 0;
33         
34         col2 = (col+1)%8; 
35         
36         for( i=0; i<8; i++ ) 
37         {
38                 sum =  (int) ctx->checksum[col2][7-i] 
39                      + (int) ctx->state.s[7-i][0] /* col or 0 ???*/ 
40                      + carry;
41                 ctx->checksum[col][7-i] ^= (uint8_t)sum;
42                 carry = sum>>8;
43
44         }
45 }
46
47 /*********************************************************************/
48
49 void twister_large_init(twister_large_ctx_t* ctx, uint16_t hashsize_b){
50         memset(ctx->state.s,  0, 64);
51         memset(ctx->checksum, 0, 64);
52         ctx->state.counter=0xffffffffffffffffLL;
53         ctx->state.s[0][7] = hashsize_b>>8;
54         ctx->state.s[1][7] = hashsize_b&0xff;
55         ctx->state.length_counter_b = 0;
56 }
57
58 /*********************************************************************/
59
60 void twister_large_nextBlock(twister_large_ctx_t* ctx, const void* msg){
61         uint8_t tmp[8][8];
62
63         /* 1st maxi round */
64         memcpy(tmp, ctx->state.s, 64);  
65         checksum_update(ctx, 0);
66         twister_mini_round(&(ctx->state), msg);
67         msg = (uint8_t*)msg + 8;
68
69         checksum_update(ctx, 1);
70         twister_mini_round(&(ctx->state), msg);
71         msg = (uint8_t*)msg + 8;
72         
73         checksum_update(ctx, 2);
74         twister_mini_round(&(ctx->state), msg);
75         msg = (uint8_t*)msg + 8;
76         memxor(ctx->state.s, tmp, 64);
77
78         /* 2nd maxi round */
79         memcpy(tmp, ctx->state.s, 64);  
80         checksum_update(ctx, 3);
81         twister_mini_round(&(ctx->state), msg);
82         msg = (uint8_t*)msg + 8;
83
84         twister_blank_round(&(ctx->state));
85         
86         checksum_update(ctx, 4);
87         twister_mini_round(&(ctx->state), msg);
88         msg = (uint8_t*)msg + 8;
89         memxor(ctx->state.s, tmp, 64);
90
91         /* 3rd maxi round */
92         memcpy(tmp, ctx->state.s, 64);  
93         checksum_update(ctx, 5);
94         twister_mini_round(&(ctx->state), msg);
95         msg = (uint8_t*)msg + 8;
96
97         checksum_update(ctx, 6);
98         twister_mini_round(&(ctx->state), msg);
99         msg = (uint8_t*)msg + 8;
100
101         checksum_update(ctx, 7);
102         twister_mini_round(&(ctx->state), msg);
103
104         twister_blank_round(&(ctx->state));     
105         memxor(ctx->state.s, tmp, 64);
106         ctx->state.length_counter_b += 512;
107 }
108
109 /*********************************************************************/
110
111 void twister_inject_chksum(twister_large_ctx_t* ctx, uint8_t col){
112         *((uint64_t*)(&ctx->state.s[7][0])) ^= *((uint64_t*)(&ctx->checksum[col][0]));
113         twister_blank_round(&ctx->state);
114 }
115
116 /*********************************************************************/
117
118 void twister_large_lastBlock(twister_large_ctx_t* ctx, const void* msg, uint16_t length_b){
119         uint8_t tmp[64];        
120         while(length_b>=512){
121                 twister_large_nextBlock(ctx, msg);
122                 msg = ((uint8_t*)msg)+64;
123                 length_b -= 512;
124         }
125         memset(tmp, 0, 64);
126         memcpy(tmp, msg, (length_b+7)/8);
127         tmp[length_b/8] |= 0x80 >> (length_b&0x07);
128         twister_large_nextBlock(ctx, tmp);
129         ctx->state.length_counter_b -= 512 - length_b;
130         twister_mini_round(&(ctx->state), &(ctx->state.length_counter_b));
131
132         memcpy(tmp, ctx->state.s, 64);
133         twister_inject_chksum(ctx, 0);
134         twister_inject_chksum(ctx, 1);
135         twister_inject_chksum(ctx, 2);
136         memxor(ctx->state.s, tmp, 64);
137
138         memcpy(tmp, ctx->state.s, 64);
139         twister_inject_chksum(ctx, 3);
140         twister_inject_chksum(ctx, 4);
141         twister_inject_chksum(ctx, 5);
142         memxor(ctx->state.s, tmp, 64);
143
144         memcpy(tmp, ctx->state.s, 64);
145         twister_inject_chksum(ctx, 6);
146         twister_inject_chksum(ctx, 7);
147         twister_blank_round(&(ctx->state));
148         memxor(ctx->state.s, tmp, 64);
149
150 }
151
152 /*********************************************************************/
153
154 void twister_large_ctx2hash(void* dest, twister_large_ctx_t* ctx, uint16_t hashsize_b){
155         twister_ctx2hash(dest, &(ctx->state), hashsize_b);
156 }
157
158 /*********************************************************************/
159 /*********************************************************************/
160
161 void twister384_init(twister384_ctx_t* ctx){
162         twister_large_init(ctx, 384);
163 }
164
165 /*********************************************************************/
166
167 void twister384_nextBlock(twister384_ctx_t* ctx, const void* msg){
168         twister_large_nextBlock(ctx, msg);
169 }
170
171 /*********************************************************************/
172
173 void twister384_lastBlock(twister384_ctx_t* ctx, const void* msg, uint16_t length_b){
174         twister_large_lastBlock(ctx, msg, length_b);
175 }
176
177 /*********************************************************************/
178
179 void twister384_ctx2hash(void* dest, twister384_ctx_t* ctx){
180         twister_large_ctx2hash(dest, ctx, 384);
181 }
182
183 /*********************************************************************/
184
185 void twister384(void* dest, const void* msg, uint32_t msg_length_b){
186         twister_large_ctx_t ctx;
187         twister_large_init(&ctx, 384);
188         while(msg_length_b >=512){
189                 twister_large_nextBlock(&ctx, msg);
190                 msg = (uint8_t*)msg + 512/8;
191                 msg_length_b -= 512;
192         }
193         twister_large_lastBlock(&ctx, msg, msg_length_b);
194         twister_large_ctx2hash(dest, &ctx, 384);
195 }
196
197 /*********************************************************************/
198 /*********************************************************************/
199
200
201 void twister512_init(twister512_ctx_t* ctx){
202         twister_large_init(ctx, 512);
203 }
204
205 /*********************************************************************/
206
207 void twister512_nextBlock(twister512_ctx_t* ctx, const void* msg){
208         twister_large_nextBlock(ctx, msg);
209 }
210
211 /*********************************************************************/
212
213 void twister512_lastBlock(twister512_ctx_t* ctx, const void* msg, uint16_t length_b){
214         twister_large_lastBlock(ctx, msg, length_b);
215 }
216
217 /*********************************************************************/
218
219 void twister512_ctx2hash(void* dest, twister512_ctx_t* ctx){
220         twister_large_ctx2hash(dest, ctx, 512);
221 }
222
223 /*********************************************************************/
224
225 void twister512(void* dest, const void* msg, uint32_t msg_length_b){
226         twister_large_ctx_t ctx;
227         twister_large_init(&ctx, 512);
228         while(msg_length_b >=512){
229                 twister_large_nextBlock(&ctx, msg);
230                 msg = (uint8_t*)msg + 512/8;
231                 msg_length_b -= 512;
232         }
233         twister_large_lastBlock(&ctx, msg, msg_length_b);
234         twister_large_ctx2hash(dest, &ctx, 512);
235 }
236
237
238