3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
24 #include "twister-large.h"
26 /*********************************************************************/
29 void checksum_update(twister_large_ctx_t* ctx, uint8_t col){
38 sum = (int) ctx->checksum[col2][7-i]
39 + (int) ctx->state.s[7-i][0] /* col or 0 ???*/
41 ctx->checksum[col][7-i] ^= (uint8_t)sum;
47 /*********************************************************************/
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;
58 /*********************************************************************/
60 void twister_large_nextBlock(twister_large_ctx_t* ctx, const void* msg){
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;
69 checksum_update(ctx, 1);
70 twister_mini_round(&(ctx->state), msg);
71 msg = (uint8_t*)msg + 8;
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);
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;
84 twister_blank_round(&(ctx->state));
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);
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;
97 checksum_update(ctx, 6);
98 twister_mini_round(&(ctx->state), msg);
99 msg = (uint8_t*)msg + 8;
101 checksum_update(ctx, 7);
102 twister_mini_round(&(ctx->state), msg);
104 twister_blank_round(&(ctx->state));
105 memxor(ctx->state.s, tmp, 64);
106 ctx->state.length_counter_b += 512;
109 /*********************************************************************/
111 void twister_inject_chksum(twister_large_ctx_t* ctx, uint8_t col){
114 ctx->state.s[7][i] ^= ctx->checksum[col][i];
117 twister_blank_round(&ctx->state);
120 /*********************************************************************/
122 void twister_large_lastBlock(twister_large_ctx_t* ctx, const void* msg, uint16_t length_b){
124 while(length_b>=512){
125 twister_large_nextBlock(ctx, msg);
126 msg = ((uint8_t*)msg)+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));
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);
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);
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);
156 /*********************************************************************/
158 void twister_large_ctx2hash(void* dest, twister_large_ctx_t* ctx, uint16_t hashsize_b){
159 twister_ctx2hash(dest, &(ctx->state), hashsize_b);
162 /*********************************************************************/
163 /*********************************************************************/
165 void twister384_init(twister384_ctx_t* ctx){
166 twister_large_init(ctx, 384);
169 /*********************************************************************/
171 void twister384_nextBlock(twister384_ctx_t* ctx, const void* msg){
172 twister_large_nextBlock(ctx, msg);
175 /*********************************************************************/
177 void twister384_lastBlock(twister384_ctx_t* ctx, const void* msg, uint16_t length_b){
178 twister_large_lastBlock(ctx, msg, length_b);
181 /*********************************************************************/
183 void twister384_ctx2hash(void* dest, twister384_ctx_t* ctx){
184 twister_large_ctx2hash(dest, ctx, 384);
187 /*********************************************************************/
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;
197 twister_large_lastBlock(&ctx, msg, msg_length_b);
198 twister_large_ctx2hash(dest, &ctx, 384);
201 /*********************************************************************/
202 /*********************************************************************/
205 void twister512_init(twister512_ctx_t* ctx){
206 twister_large_init(ctx, 512);
209 /*********************************************************************/
211 void twister512_nextBlock(twister512_ctx_t* ctx, const void* msg){
212 twister_large_nextBlock(ctx, msg);
215 /*********************************************************************/
217 void twister512_lastBlock(twister512_ctx_t* ctx, const void* msg, uint16_t length_b){
218 twister_large_lastBlock(ctx, msg, length_b);
221 /*********************************************************************/
223 void twister512_ctx2hash(void* dest, twister512_ctx_t* ctx){
224 twister_large_ctx2hash(dest, ctx, 512);
227 /*********************************************************************/
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;
237 twister_large_lastBlock(&ctx, msg, msg_length_b);
238 twister_large_ctx2hash(dest, &ctx, 512);