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-big.h"
26 /*********************************************************************/
29 void checksum_update(twister_big_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_big_init(twister_big_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_big_nextBlock(twister_big_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_big_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);
116 /*********************************************************************/
118 void twister_big_lastBlock(twister_big_ctx_t* ctx, const void* msg, uint16_t length_b){
121 twister_big_nextBlock(ctx, msg);
122 msg = ((uint8_t*)msg)+64;
126 memcpy(tmp, msg, (length_b+7)/8);
127 tmp[length_b/8] |= 0x80 >> (length_b&0x07);
128 twister_big_nextBlock(ctx, tmp);
129 ctx->state.length_counter_b -= 512 - length_b;
130 twister_mini_round(&(ctx->state), &(ctx->state.length_counter_b));
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);
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);
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);
152 /*********************************************************************/
154 void twister_big_ctx2hash(void* dest, twister_big_ctx_t* ctx, uint16_t hashsize_b){
155 twister_ctx2hash(dest, &(ctx->state), hashsize_b);
158 /*********************************************************************/
159 /*********************************************************************/
161 void twister384_init(twister384_ctx_t* ctx){
162 twister_big_init(ctx, 384);
165 /*********************************************************************/
167 void twister384_nextBlock(twister384_ctx_t* ctx, const void* msg){
168 twister_big_nextBlock(ctx, msg);
171 /*********************************************************************/
173 void twister384_lastBlock(twister384_ctx_t* ctx, const void* msg, uint16_t length_b){
174 twister_big_lastBlock(ctx, msg, length_b);
177 /*********************************************************************/
179 void twister384_ctx2hash(void* dest, twister384_ctx_t* ctx){
180 twister_big_ctx2hash(dest, ctx, 384);
183 /*********************************************************************/
185 void twister384(void* dest, const void* msg, uint32_t msg_length_b){
186 twister_big_ctx_t ctx;
187 twister_big_init(&ctx, 384);
188 while(msg_length_b >=512){
189 twister_big_nextBlock(&ctx, msg);
190 msg = (uint8_t*)msg + 512/8;
193 twister_big_lastBlock(&ctx, msg, msg_length_b);
194 twister_big_ctx2hash(dest, &ctx, 384);
197 /*********************************************************************/
198 /*********************************************************************/
201 void twister512_init(twister512_ctx_t* ctx){
202 twister_big_init(ctx, 512);
205 /*********************************************************************/
207 void twister512_nextBlock(twister512_ctx_t* ctx, const void* msg){
208 twister_big_nextBlock(ctx, msg);
211 /*********************************************************************/
213 void twister512_lastBlock(twister512_ctx_t* ctx, const void* msg, uint16_t length_b){
214 twister_big_lastBlock(ctx, msg, length_b);
217 /*********************************************************************/
219 void twister512_ctx2hash(void* dest, twister512_ctx_t* ctx){
220 twister_big_ctx2hash(dest, ctx, 512);
223 /*********************************************************************/
225 void twister512(void* dest, const void* msg, uint32_t msg_length_b){
226 twister_big_ctx_t ctx;
227 twister_big_init(&ctx, 512);
228 while(msg_length_b >=512){
229 twister_big_nextBlock(&ctx, msg);
230 msg = (uint8_t*)msg + 512/8;
233 twister_big_lastBlock(&ctx, msg, msg_length_b);
234 twister_big_ctx2hash(dest, &ctx, 512);