3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 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/>.
38 void aes_encrypt_round(void* state, void* key);
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_P(PSTR("\r\nrow "));
79 cli_putstr_P(PSTR(", col "));
81 cli_putstr_P(PSTR(": "));
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 compress512(void* v, void* m, uint64_t* c, void* salt){
99 memcpy(s, v, 16*4); /* load v into state */
100 memcpy(s+16*4, m, 16*12); /* load m into state */
107 cli_putstr_P(PSTR("\r\n === ROUND "));
109 cli_putstr_P(PSTR(" ==="));
115 aes_encrypt_round(s+16*j, k);
116 aes_encrypt_round(s+16*j, salt);
117 *((uint64_t*)(k)) += 1;
121 cli_putstr_P(PSTR("\r\nAfter SubWords"));
128 memcpy(t, s+INDEX(0, 1), 16);
129 memcpy(s+INDEX(0, 1), s+INDEX(1, 1), 16);
130 memcpy(s+INDEX(1, 1), s+INDEX(2, 1), 16);
131 memcpy(s+INDEX(2, 1), s+INDEX(3, 1), 16);
132 memcpy(s+INDEX(3, 1), t, 16);
134 memcpy(t, s+INDEX(0, 2), 16);
135 memcpy(s+INDEX(0, 2), s+INDEX(2, 2), 16);
136 memcpy(s+INDEX(2, 2), t, 16);
137 memcpy(t, s+INDEX(1, 2), 16);
138 memcpy(s+INDEX(1, 2), s+INDEX(3, 2), 16);
139 memcpy(s+INDEX(3, 2), t, 16);
141 memcpy(t, s+INDEX(0, 3), 16);
142 memcpy(s+INDEX(0, 3), s+INDEX(3, 3), 16);
143 memcpy(s+INDEX(3, 3), s+INDEX(2, 3), 16);
144 memcpy(s+INDEX(2, 3), s+INDEX(1, 3), 16);
145 memcpy(s+INDEX(1, 3), t, 16);
148 cli_putstr_P(PSTR("\r\nAfter ShiftRows"));
160 cli_putstr_P(PSTR("\r\nAfter MixColumns"));
168 memxor(v, (uint8_t*)m+4*16*i, 4*16);
171 memxor(v, s+4*16*i, 4*16);
175 void echo_small_nextBlock(echo_small_ctx_t* ctx, void* block){
176 ctx->counter += ECHO_SMALL_BLOCKSIZE;
177 compress512(ctx->v, block, &(ctx->counter), ctx->salt);
180 void echo_small_lastBlock(echo_small_ctx_t* ctx, void* block, uint16_t length_b){
181 while(length_b>=ECHO_SMALL_BLOCKSIZE){
182 echo_small_nextBlock(ctx, block);
183 block = (uint8_t*)block + ECHO_SMALL_BLOCKSIZE_B;
184 length_b -= ECHO_SMALL_BLOCKSIZE;
186 uint8_t buffer[ECHO_SMALL_BLOCKSIZE_B];
188 memset(buffer, 0, ECHO_SMALL_BLOCKSIZE_B);
189 memcpy(buffer, block, (length_b+7)/8);
190 buffer[length_b/8] |= 0x80 >> (length_b&7);
191 total_len = (ctx->counter += length_b);
192 if(length_b>=ECHO_SMALL_BLOCKSIZE-144){
193 compress512(ctx->v, buffer, &total_len, ctx->salt);
194 memset(buffer, 0, ECHO_SMALL_BLOCKSIZE_B);
200 memcpy(buffer+ECHO_SMALL_BLOCKSIZE_B-18, &(ctx->id), 2);
201 memcpy(buffer+ECHO_SMALL_BLOCKSIZE_B-16, &total_len, 8);
202 compress512(ctx->v, buffer, &(ctx->counter), ctx->salt);
205 /******************************************************************************/
207 void echo_small_ctx2hash(void* dest, uint16_t length_b, echo_small_ctx_t* ctx){
208 memcpy(dest, ctx->v, (length_b+7)/8);
211 void echo224_ctx2hash(void* dest, echo_small_ctx_t* ctx){
212 memcpy(dest, ctx->v, 224/8);
215 void echo256_ctx2hash(void* dest, echo_small_ctx_t* ctx){
216 memcpy(dest, ctx->v, 256/8);
219 /******************************************************************************/
221 void echo224_init(echo_small_ctx_t* ctx){
222 memset(ctx->v, 0, 4*16);
224 memset(ctx->salt, 0, 16);
226 ctx->v[0+16*0] = 0xE0;
227 ctx->v[0+16*1] = 0xE0;
228 ctx->v[0+16*2] = 0xE0;
229 ctx->v[0+16*3] = 0xE0;
232 void echo256_init(echo_small_ctx_t* ctx){
233 memset(ctx->v, 0, 4*16);
235 memset(ctx->salt, 0, 16);
237 ctx->v[1+16*0] = 0x01;
238 ctx->v[1+16*1] = 0x01;
239 ctx->v[1+16*2] = 0x01;
240 ctx->v[1+16*3] = 0x01;
243 /******************************************************************************/