3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2010 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 "aes_enc_round.h"
40 #define INDEX(c,r) ((c)*16*4+(r)*16)
42 #define GF256MUL_1(a) (a)
43 #define GF256MUL_2(a) (gf256mul(2, (a), 0x1b))
44 #define GF256MUL_3(a) (gf256mul(3, (a), 0x1b))
46 static void mixcol(uint8_t* s){
53 t = tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
55 GF256MUL_2(tmp[0]^tmp[1])
59 GF256MUL_2(tmp[1]^tmp[2])
63 GF256MUL_2(tmp[2]^tmp[3])
67 GF256MUL_2(tmp[3]^tmp[0])
73 static void dump_state(void* s){
75 for(col=0; col<4; col++){
76 for(row=0; row<4; row++){
77 cli_putstr("\r\nrow ");
82 cli_hexdump((uint8_t*)s+col*16*4+row*16, 4);
84 cli_hexdump((uint8_t*)s+col*16*4+row*16+ 4, 4);
86 cli_hexdump((uint8_t*)s+col*16*4+row*16+ 8, 4);
88 cli_hexdump((uint8_t*)s+col*16*4+row*16+12, 4);
94 static void echo_compress(uint8_t* s, uint8_t iterations, uint64_t* c, void* salt){
105 cli_putstr("\r\n === ROUND ");
108 if(round<DEBUG_DEPTH){
113 aes_enc_round((aes_cipher_state_t*)(s+16*i), (aes_roundkey_t*)k);
114 aes_enc_round((aes_cipher_state_t*)(s+16*i), (aes_roundkey_t*)salt);
115 *((uint64_t*)(k)) += 1;
118 if(round<DEBUG_DEPTH){
119 cli_putstr("\r\nAfter SubWords");
126 memcpy(t, s+INDEX(0, 1), 16);
127 memcpy(s+INDEX(0, 1), s+INDEX(1, 1), 16);
128 memcpy(s+INDEX(1, 1), s+INDEX(2, 1), 16);
129 memcpy(s+INDEX(2, 1), s+INDEX(3, 1), 16);
130 memcpy(s+INDEX(3, 1), t, 16);
132 memcpy(t, s+INDEX(0, 2), 16);
133 memcpy(s+INDEX(0, 2), s+INDEX(2, 2), 16);
134 memcpy(s+INDEX(2, 2), t, 16);
135 memcpy(t, s+INDEX(1, 2), 16);
136 memcpy(s+INDEX(1, 2), s+INDEX(3, 2), 16);
137 memcpy(s+INDEX(3, 2), t, 16);
139 memcpy(t, s+INDEX(0, 3), 16);
140 memcpy(s+INDEX(0, 3), s+INDEX(3, 3), 16);
141 memcpy(s+INDEX(3, 3), s+INDEX(2, 3), 16);
142 memcpy(s+INDEX(2, 3), s+INDEX(1, 3), 16);
143 memcpy(s+INDEX(1, 3), t, 16);
145 if(round<DEBUG_DEPTH){
146 cli_putstr("\r\nAfter ShiftRows");
157 if(round<DEBUG_DEPTH){
158 cli_putstr("\r\nAfter MixColumns");
163 }while(--iterations);
167 /******************************************************************************/
169 static void compress512(void* v, void* m, uint64_t* c, void* salt){
172 memcpy(s, v, 16*4); /* load v into state */
173 memcpy(s+16*4, m, 16*12); /* load m into state */
175 echo_compress(s, 8, c, salt);
179 memxor(v, (uint8_t*)m+4*16*i, 4*16);
182 memxor(v, s+4*16*i, 4*16);
186 static void compress1024(void* v, void* m, uint64_t* c, void* salt){
188 memcpy(s, v, 16*8); /* load v into state */
189 memcpy(s+16*8, m, 16*8); /* load m into state */
191 echo_compress(s, 10, c, salt);
196 memxor(v, s+16*8, 16*8);
199 /******************************************************************************/
201 void echo_small_nextBlock(echo_small_ctx_t* ctx, void* block){
202 ctx->counter += ECHO_SMALL_BLOCKSIZE;
203 compress512(ctx->v, block, &(ctx->counter), ctx->salt);
206 void echo_small_lastBlock(echo_small_ctx_t* ctx, void* block, uint16_t length_b){
207 while(length_b>=ECHO_SMALL_BLOCKSIZE){
208 echo_small_nextBlock(ctx, block);
209 block = (uint8_t*)block + ECHO_SMALL_BLOCKSIZE_B;
210 length_b -= ECHO_SMALL_BLOCKSIZE;
212 uint8_t buffer[ECHO_SMALL_BLOCKSIZE_B];
214 memset(buffer, 0, ECHO_SMALL_BLOCKSIZE_B);
215 memcpy(buffer, block, (length_b+7)/8);
216 buffer[length_b/8] |= 0x80 >> (length_b&7);
217 total_len = (ctx->counter += length_b);
218 if(length_b>=ECHO_SMALL_BLOCKSIZE-144){
219 compress512(ctx->v, buffer, &total_len, ctx->salt);
220 memset(buffer, 0, ECHO_SMALL_BLOCKSIZE_B);
226 memcpy(buffer+ECHO_SMALL_BLOCKSIZE_B-18, &(ctx->id), 2);
227 memcpy(buffer+ECHO_SMALL_BLOCKSIZE_B-16, &total_len, 8);
228 compress512(ctx->v, buffer, &(ctx->counter), ctx->salt);
231 /******************************************************************************/
233 void echo_large_nextBlock(echo_large_ctx_t* ctx, void* block){
234 ctx->counter += ECHO_LARGE_BLOCKSIZE;
235 compress1024(ctx->v, block, &(ctx->counter), ctx->salt);
238 void echo_large_lastBlock(echo_large_ctx_t* ctx, void* block, uint16_t length_b){
239 while(length_b>=ECHO_LARGE_BLOCKSIZE){
240 echo_large_nextBlock(ctx, block);
241 block = (uint8_t*)block + ECHO_LARGE_BLOCKSIZE_B;
242 length_b -= ECHO_LARGE_BLOCKSIZE;
244 uint8_t buffer[ECHO_LARGE_BLOCKSIZE_B];
246 memset(buffer, 0, ECHO_LARGE_BLOCKSIZE_B);
247 memcpy(buffer, block, (length_b+7)/8);
248 buffer[length_b/8] |= 0x80 >> (length_b&7);
249 total_len = (ctx->counter += length_b);
250 if(length_b>=ECHO_LARGE_BLOCKSIZE-144){
251 compress1024(ctx->v, buffer, &total_len, ctx->salt);
252 memset(buffer, 0, ECHO_LARGE_BLOCKSIZE_B);
258 memcpy(buffer+ECHO_LARGE_BLOCKSIZE_B-18, &(ctx->id), 2);
259 memcpy(buffer+ECHO_LARGE_BLOCKSIZE_B-16, &total_len, 8);
260 compress1024(ctx->v, buffer, &(ctx->counter), ctx->salt);
262 /******************************************************************************/
264 void echo_ctx2hash(void* dest, uint16_t length_b, echo_small_ctx_t* ctx){
265 memcpy(dest, ctx->v, (length_b+7)/8);
268 void echo224_ctx2hash(void* dest, echo_small_ctx_t* ctx){
269 memcpy(dest, ctx->v, 224/8);
272 void echo256_ctx2hash(void* dest, echo_small_ctx_t* ctx){
273 memcpy(dest, ctx->v, 256/8);
276 /******************************************************************************/
278 void echo384_ctx2hash(void* dest, echo_large_ctx_t* ctx){
279 memcpy(dest, ctx->v, 384/8);
282 void echo512_ctx2hash(void* dest, echo_large_ctx_t* ctx){
283 memcpy(dest, ctx->v, 512/8);
286 /******************************************************************************/
288 void echo224_init(echo_small_ctx_t* ctx){
289 memset(ctx->v, 0, 4*16);
291 memset(ctx->salt, 0, 16);
293 ctx->v[0+16*0] = 0xE0;
294 ctx->v[0+16*1] = 0xE0;
295 ctx->v[0+16*2] = 0xE0;
296 ctx->v[0+16*3] = 0xE0;
299 void echo256_init(echo_small_ctx_t* ctx){
300 memset(ctx->v, 0, 4*16);
302 memset(ctx->salt, 0, 16);
304 ctx->v[1+16*0] = 0x01;
305 ctx->v[1+16*1] = 0x01;
306 ctx->v[1+16*2] = 0x01;
307 ctx->v[1+16*3] = 0x01;
310 /******************************************************************************/
312 void echo384_init(echo_large_ctx_t* ctx){
314 memset(ctx->v, 0, 8*16);
316 memset(ctx->salt, 0, 16);
319 ctx->v[0+16*i] = 0x80;
320 ctx->v[1+16*i] = 0x01;
324 void echo512_init(echo_large_ctx_t* ctx){
326 memset(ctx->v, 0, 8*16);
328 memset(ctx->salt, 0, 16);
331 ctx->v[1+16*i] = 0x02;
335 /******************************************************************************/