]> git.cryptolib.org Git - arm-crypto-lib.git/blob - echo/echo.c
updated build system
[arm-crypto-lib.git] / echo / echo.c
1 /* echo.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  Daniel Otte (daniel.otte@rub.de)
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
21 #include "echo.h"
22 #include "gf256mul/gf256mul.h"
23 #include "memxor/memxor.h"
24 #include "aes_enc_round.h"
25 #include <stdint.h>
26 #include <string.h>
27
28 #ifdef DEBUG
29 #undef DEBUG
30 #endif
31
32 #define DEBUG 0
33
34 #if DEBUG
35 #define DEBUG_DEPTH 2
36 #include "cli.h"
37 #endif
38
39
40 #define INDEX(c,r) ((c)*16*4+(r)*16)
41
42 #define GF256MUL_1(a) (a)
43 #define GF256MUL_2(a) (gf256mul(2, (a), 0x1b))
44 #define GF256MUL_3(a) (gf256mul(3, (a), 0x1b))
45
46 static void mixcol(uint8_t* s){
47         uint8_t t, tmp[4];
48         tmp[0] = *(s+16*0);
49         tmp[1] = *(s+16*1);
50         tmp[2] = *(s+16*2);
51         tmp[3] = *(s+16*3);
52
53         t = tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
54         *(s+16*0) =
55                   GF256MUL_2(tmp[0]^tmp[1])
56                 ^ tmp[0]
57                 ^ t;
58         *(s+16*1) =
59                   GF256MUL_2(tmp[1]^tmp[2])
60                 ^ tmp[1]
61                 ^ t;
62         *(s+16*2) =
63                   GF256MUL_2(tmp[2]^tmp[3])
64                 ^ tmp[2]
65                 ^ t;
66         *(s+16*3) =
67                   GF256MUL_2(tmp[3]^tmp[0])
68                 ^ tmp[3]
69                 ^ t;
70 }
71
72 #if DEBUG
73 static void dump_state(void* s){
74         uint8_t row, col;
75         for(col=0; col<4; col++){
76                 for(row=0; row<4; row++){
77                         cli_putstr("\r\nrow ");
78                         cli_putc('0'+row);
79                         cli_putstr(", col ");
80                         cli_putc('0'+col);
81                         cli_putstr(": ");
82                         cli_hexdump((uint8_t*)s+col*16*4+row*16, 4);
83                         cli_putc(' ');
84                         cli_hexdump((uint8_t*)s+col*16*4+row*16+ 4, 4);
85                         cli_putc(' ');
86                         cli_hexdump((uint8_t*)s+col*16*4+row*16+ 8, 4);
87                         cli_putc(' ');
88                         cli_hexdump((uint8_t*)s+col*16*4+row*16+12, 4);
89                 }
90         }
91 }
92 #endif
93
94 static void echo_compress(uint8_t* s, uint8_t iterations, uint64_t* c, void* salt){
95         uint8_t i, j;
96         uint8_t k[16];
97 #if DEBUG
98         uint8_t round=0;
99 #endif
100         memcpy(k, c, 8);
101         memset(k+8, 0, 8);
102         do{
103                 /* BIG.SubWords */
104 #if DEBUG
105         cli_putstr("\r\n === ROUND ");
106         cli_putc('0'+round);
107         cli_putstr(" ===");
108         if(round<DEBUG_DEPTH){
109                 dump_state(s);
110         }
111 #endif
112                 for(i=0; i<16; ++i){
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;
116                 }
117 #if DEBUG
118                 if(round<DEBUG_DEPTH){
119                         cli_putstr("\r\nAfter SubWords");
120                         dump_state(s);
121                 }
122 #endif
123                 /* BIG.ShiftRows */
124                 uint8_t t[16];
125                 /* "Row" 1 */
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);
131                 /* "Row" 2 */
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);
138                 /* "Row" 3 */
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);
144 #if DEBUG
145                 if(round<DEBUG_DEPTH){
146                         cli_putstr("\r\nAfter ShiftRows");
147                         dump_state(s);
148                 }
149 #endif
150                 /* BIG.MixColumns */
151                 for(i=0; i<4; i+=1){
152                         for(j=0; j<16; ++j){
153                                 mixcol(s+i*64+j);
154                         }
155                 }
156 #if DEBUG
157                 if(round<DEBUG_DEPTH){
158                         cli_putstr("\r\nAfter MixColumns");
159                         dump_state(s);
160                 }
161                 round++;
162 #endif
163         }while(--iterations);
164
165 }
166
167 /******************************************************************************/
168
169 static void compress512(void* v, void* m, uint64_t* c, void* salt){
170         uint8_t s[16*16];
171         uint8_t i;
172         memcpy(s, v, 16*4);           /* load v into state */
173         memcpy(s+16*4, m, 16*12);     /* load m into state */
174
175         echo_compress(s, 8, c, salt);
176
177         /* BIG.Final */
178         for(i=0; i<3; ++i){
179                 memxor(v, (uint8_t*)m+4*16*i, 4*16);
180         }
181         for(i=0; i<4; ++i){
182                 memxor(v, s+4*16*i, 4*16);
183         }
184 }
185
186 static void compress1024(void* v, void* m, uint64_t* c, void* salt){
187         uint8_t s[16*16];
188         memcpy(s, v, 16*8);           /* load v into state */
189         memcpy(s+16*8, m, 16*8);      /* load m into state */
190
191         echo_compress(s, 10, c, salt);
192
193         /* BIG.Final */
194         memxor(v, m, 16*8);
195         memxor(v, s, 16*8);
196         memxor(v, s+16*8, 16*8);
197 }
198
199 /******************************************************************************/
200
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);
204 }
205
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;
211         }
212         uint8_t buffer[ECHO_SMALL_BLOCKSIZE_B];
213         uint64_t total_len;
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);
221                 ctx->counter = 0;
222         }
223         if(length_b==0){
224                 ctx->counter = 0;
225         }
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);
229 }
230
231 /******************************************************************************/
232
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);
236 }
237
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;
243         }
244         uint8_t buffer[ECHO_LARGE_BLOCKSIZE_B];
245         uint64_t total_len;
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);
253                 ctx->counter = 0;
254         }
255         if(length_b==0){
256                 ctx->counter = 0;
257         }
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);
261 }
262 /******************************************************************************/
263
264 void echo_ctx2hash(void* dest, uint16_t length_b, echo_small_ctx_t* ctx){
265         memcpy(dest, ctx->v, (length_b+7)/8);
266 }
267
268 void echo224_ctx2hash(void* dest, echo_small_ctx_t* ctx){
269         memcpy(dest, ctx->v, 224/8);
270 }
271
272 void echo256_ctx2hash(void* dest, echo_small_ctx_t* ctx){
273         memcpy(dest, ctx->v, 256/8);
274 }
275
276 /******************************************************************************/
277
278 void echo384_ctx2hash(void* dest, echo_large_ctx_t* ctx){
279         memcpy(dest, ctx->v, 384/8);
280 }
281
282 void echo512_ctx2hash(void* dest, echo_large_ctx_t* ctx){
283         memcpy(dest, ctx->v, 512/8);
284 }
285
286 /******************************************************************************/
287
288 void echo224_init(echo_small_ctx_t* ctx){
289         memset(ctx->v, 0, 4*16);
290         ctx->counter = 0;
291         memset(ctx->salt, 0, 16);
292         ctx->id = 0x00E0;
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;
297 }
298
299 void echo256_init(echo_small_ctx_t* ctx){
300         memset(ctx->v, 0, 4*16);
301         ctx->counter = 0;
302         memset(ctx->salt, 0, 16);
303         ctx->id = 0x0100;
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;
308 }
309
310 /******************************************************************************/
311
312 void echo384_init(echo_large_ctx_t* ctx){
313         uint8_t i;
314         memset(ctx->v, 0, 8*16);
315         ctx->counter = 0;
316         memset(ctx->salt, 0, 16);
317         ctx->id = 0x0180;
318         for(i=0; i<8; ++i){
319                 ctx->v[0+16*i] = 0x80;
320                 ctx->v[1+16*i] = 0x01;
321         }
322 }
323
324 void echo512_init(echo_large_ctx_t* ctx){
325         uint8_t i;
326         memset(ctx->v, 0, 8*16);
327         ctx->counter = 0;
328         memset(ctx->salt, 0, 16);
329         ctx->id = 0x0200;
330         for(i=0; i<8; ++i){
331                 ctx->v[1+16*i] = 0x02;
332         }
333 }
334
335 /******************************************************************************/