]> git.cryptolib.org Git - avr-crypto-lib.git/blob - twister/twister-large.c
fixing E-Mail-Address & Copyright
[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) 2006-2015 Daniel Otte (bg@nerilex.org)
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         uint8_t i=7;
113         do{
114                 ctx->state.s[7][i] ^= ctx->checksum[col][i];
115
116         }while(i--);
117         twister_blank_round(&ctx->state);
118 }
119
120 /*********************************************************************/
121
122 void twister_large_lastBlock(twister_large_ctx_t *ctx, const void *msg, uint16_t length_b){
123         uint8_t tmp[64];        
124         while(length_b>=512){
125                 twister_large_nextBlock(ctx, msg);
126                 msg = ((uint8_t*)msg)+64;
127                 length_b -= 512;
128         }
129         memset(tmp, 0, 64);
130         memcpy(tmp, msg, (length_b+7)/8);
131         tmp[length_b/8] |= 0x80 >> (length_b&0x07);
132         twister_large_nextBlock(ctx, tmp);
133         ctx->state.length_counter_b -= 512 - length_b;
134         twister_mini_round(&(ctx->state), &(ctx->state.length_counter_b));
135
136         memcpy(tmp, ctx->state.s, 64);
137         twister_inject_chksum(ctx, 0);
138         twister_inject_chksum(ctx, 1);
139         twister_inject_chksum(ctx, 2);
140         memxor(ctx->state.s, tmp, 64);
141
142         memcpy(tmp, ctx->state.s, 64);
143         twister_inject_chksum(ctx, 3);
144         twister_inject_chksum(ctx, 4);
145         twister_inject_chksum(ctx, 5);
146         memxor(ctx->state.s, tmp, 64);
147
148         memcpy(tmp, ctx->state.s, 64);
149         twister_inject_chksum(ctx, 6);
150         twister_inject_chksum(ctx, 7);
151         twister_blank_round(&(ctx->state));
152         memxor(ctx->state.s, tmp, 64);
153
154 }
155
156 /*********************************************************************/
157
158 void twister_large_ctx2hash(void *dest, twister_large_ctx_t *ctx, uint16_t hashsize_b){
159         twister_ctx2hash(dest, &(ctx->state), hashsize_b);
160 }
161
162 /*********************************************************************/
163 /*********************************************************************/
164
165 void twister384_init(twister384_ctx_t *ctx){
166         twister_large_init(ctx, 384);
167 }
168
169 /*********************************************************************/
170
171 void twister384_nextBlock(twister384_ctx_t *ctx, const void *msg){
172         twister_large_nextBlock(ctx, msg);
173 }
174
175 /*********************************************************************/
176
177 void twister384_lastBlock(twister384_ctx_t *ctx, const void *msg, uint16_t length_b){
178         twister_large_lastBlock(ctx, msg, length_b);
179 }
180
181 /*********************************************************************/
182
183 void twister384_ctx2hash(void *dest, twister384_ctx_t *ctx){
184         twister_large_ctx2hash(dest, ctx, 384);
185 }
186
187 /*********************************************************************/
188
189 void twister384(void *dest, const void *msg, uint32_t msg_length_b){
190         twister_large_ctx_t ctx;
191         twister_large_init(&ctx, 384);
192         while(msg_length_b >=512){
193                 twister_large_nextBlock(&ctx, msg);
194                 msg = (uint8_t*)msg + 512/8;
195                 msg_length_b -= 512;
196         }
197         twister_large_lastBlock(&ctx, msg, msg_length_b);
198         twister_large_ctx2hash(dest, &ctx, 384);
199 }
200
201 /*********************************************************************/
202 /*********************************************************************/
203
204
205 void twister512_init(twister512_ctx_t *ctx){
206         twister_large_init(ctx, 512);
207 }
208
209 /*********************************************************************/
210
211 void twister512_nextBlock(twister512_ctx_t *ctx, const void *msg){
212         twister_large_nextBlock(ctx, msg);
213 }
214
215 /*********************************************************************/
216
217 void twister512_lastBlock(twister512_ctx_t *ctx, const void *msg, uint16_t length_b){
218         twister_large_lastBlock(ctx, msg, length_b);
219 }
220
221 /*********************************************************************/
222
223 void twister512_ctx2hash(void *dest, twister512_ctx_t *ctx){
224         twister_large_ctx2hash(dest, ctx, 512);
225 }
226
227 /*********************************************************************/
228
229 void twister512(void *dest, const void *msg, uint32_t msg_length_b){
230         twister_large_ctx_t ctx;
231         twister_large_init(&ctx, 512);
232         while(msg_length_b >=512){
233                 twister_large_nextBlock(&ctx, msg);
234                 msg = (uint8_t*)msg + 512/8;
235                 msg_length_b -= 512;
236         }
237         twister_large_lastBlock(&ctx, msg, msg_length_b);
238         twister_large_ctx2hash(dest, &ctx, 512);
239 }
240
241
242