]> git.cryptolib.org Git - avr-crypto-lib.git/blob - groestl/groestl_large.c
972a956e99a5e16d0e3fd07d841d695bc039eee0
[avr-crypto-lib.git] / groestl / groestl_large.c
1 /* groestl_large.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2009  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  * \file    groestl_large.c
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-06-11
24  * \license GPLv3 or later
25  * 
26  */
27
28 #include "groestl_large.h"
29 #include "aes_sbox.h"
30 #include "gf256mul.h"
31 #include "memxor.h"
32 #include <stdint.h>
33 #include <avr/pgmspace.h>
34 #include <string.h>
35
36 #define ROUNDS 14
37 #define POLYNOM 0x1b
38
39 #define DEBUG 0
40
41 #if DEBUG
42  #include "cli.h"
43  void dump_m(const uint8_t *m){
44          uint8_t i,j;
45          for(i=0; i<16; ++i){
46                 cli_putstr_P(PSTR("\r\n"));
47                 for(j=0; j<8; ++j){
48                         cli_putc(' ');
49                         cli_hexdump(m+8*i+j, 1);
50                 }
51          }
52  }
53 #else
54  #define dump_m(m)
55 #endif
56
57 static const uint8_t matrix[] PROGMEM = {
58  2, 2, 3, 4, 5, 3, 5, 7,
59  7, 2, 2, 3, 4, 5, 3, 5,
60  5, 7, 2, 2, 3, 4, 5, 3,
61  3, 5, 7, 2, 2, 3, 4, 5,
62  5, 3, 5, 7, 2, 2, 3, 4,
63  4, 5, 3, 5, 7, 2, 2, 3,
64  3, 4, 5, 3, 5, 7, 2, 2,
65  2, 3, 4, 5, 3, 5, 7, 2
66 };
67
68 static
69 void shift_columns(uint8_t *a, PGM_VOID_P shifts){
70         uint8_t s;
71         uint8_t tmp[16];
72         uint8_t i,j;
73         for(i=0; i<8; ++i){
74                 s = pgm_read_byte(shifts);
75                 shifts = (uint8_t*)shifts + 1;
76                 if(s==0){
77                         continue;
78                 }
79                 for(j=0;j<16;++j){
80                         tmp[j] = a[i+j*8];
81                 }
82                 for(j=0; j<16; ++j){
83                         a[i+((j-s+16)%16)*8] = tmp[j];
84                 }
85         }
86 }
87
88 static const uint8_t p_shifts[] PROGMEM = {
89                 0, 1, 2, 3, 4, 5, 6, 11
90 };
91
92 static const uint8_t q_shifts[] PROGMEM = {
93                 1, 3, 5, 11, 0, 2, 4, 6
94 };
95
96 void groestl_large_rounds(uint8_t *m, uint8_t q){
97         uint8_t r,i,j;
98         uint8_t tmp[16];
99         for(r=0; r<ROUNDS; ++r){
100                 if(q){
101                         for(i=0; i<(8*16); ++i){
102                                 m[i] ^= 0xff;
103                         }
104                         for(i=0; i<16; ++i){
105                                 m[7+i*8] ^= r ^ (i<<4);
106                         }
107                 }else{
108                         for(i=0; i<16; ++i){
109                                 m[i*8] ^= r ^ (i<<4);
110                         }
111                 }       
112 #if DEBUG               
113                 if(r<2){
114                         cli_putstr_P(PSTR("\r\npost add-const"));
115                         dump_m(m);
116                 }
117 #endif
118                 for(i=0;i<16*8; ++i){
119                         m[i] = pgm_read_byte(aes_sbox+m[i]);
120                 }
121                 if(!q){
122                         shift_columns(m, p_shifts);
123                 }else{
124                         shift_columns(m, q_shifts);
125                 }
126                 
127 #if DEBUG               
128                 if(r<2){
129                         cli_putstr_P(PSTR("\r\npost shift-bytes"));
130                         dump_m(m);
131                 }
132 #endif          
133                 for(i=0; i<16; ++i){
134                         memcpy(tmp, m+8*i, 8);
135                         for(j=0; j<8; ++j){
136                                 m[j+i*8] = gf256mul(pgm_read_byte(matrix+8*j+0),tmp[0], POLYNOM)
137                                         ^ gf256mul(pgm_read_byte(matrix+8*j+1),tmp[1], POLYNOM)
138                                         ^ gf256mul(pgm_read_byte(matrix+8*j+2),tmp[2], POLYNOM)
139                                         ^ gf256mul(pgm_read_byte(matrix+8*j+3),tmp[3], POLYNOM)
140                                         ^ gf256mul(pgm_read_byte(matrix+8*j+4),tmp[4], POLYNOM)
141                                         ^ gf256mul(pgm_read_byte(matrix+8*j+5),tmp[5], POLYNOM)
142                                         ^ gf256mul(pgm_read_byte(matrix+8*j+6),tmp[6], POLYNOM)
143                                         ^ gf256mul(pgm_read_byte(matrix+8*j+7),tmp[7], POLYNOM);
144                         }
145                 }
146 #if DEBUG
147                 if(r<2){
148                         cli_putstr_P(PSTR("\r\npost mix-bytes"));
149                         dump_m(m);
150                 }
151 #endif                  
152         }
153 }
154
155 void groestl384_init(groestl384_ctx_t *ctx){
156         memset(ctx->h, 0, 16*8);
157         ctx->h[8*16-1] = (uint8_t)384;
158         ctx->h[8*16-2] = (uint8_t)(384>>8);
159         ctx->counter = 0;
160 }
161
162 void groestl512_init(groestl512_ctx_t *ctx){
163         memset(ctx->h, 0, 16*8);
164         ctx->h[8*16-2] = 2;
165         ctx->counter = 0;
166 }
167
168 void groestl_large_nextBlock(groestl_large_ctx_t *ctx, const void *block){
169         uint8_t tmp1[128], tmp2[128];
170 /*
171         for(i=0; i<8; ++i){
172                 for(j=0; j<8; ++j){
173                         tmp1[j*8+i] = ((uint8_t*)block)[i*8+j];
174                 }
175         }
176 */ 
177         memcpy(tmp1, block, 128);
178         memcpy(tmp2, tmp1, 128);
179         memxor(tmp1, ctx->h, 128);
180         groestl_large_rounds(tmp1, 0);
181         groestl_large_rounds(tmp2, 1);
182         memxor(ctx->h, tmp1, 128);
183         memxor(ctx->h, tmp2, 128);
184         ctx->counter++;
185 }
186
187 void groestl_large_lastBlock(groestl_large_ctx_t *ctx, const void *block, uint16_t length_b){
188         uint8_t buffer[128];
189         while(length_b>=GROESTL_LARGE_BLOCKSIZE){
190                 groestl_large_nextBlock(ctx, block);
191                 length_b -= GROESTL_LARGE_BLOCKSIZE;
192                 block = (uint8_t*)block + GROESTL_LARGE_BLOCKSIZE_B;
193         }
194         memset(buffer, 0, 128);
195         memcpy(buffer, block, (length_b+7)/8);
196         buffer[length_b/8] |= 0x80>>(length_b%8);
197         if(length_b>1024-65){
198                 groestl_large_nextBlock(ctx, buffer);
199                 memset(buffer, 0, 128-4);
200         }
201         ctx->counter++;
202         buffer[128-1]  = (uint8_t)(ctx->counter);
203         buffer[128-2]  = (uint8_t)((ctx->counter)>>8);
204         buffer[128-3]  = (uint8_t)((ctx->counter)>>16);
205         buffer[128-4]  = (uint8_t)((ctx->counter)>>24);
206         groestl_large_nextBlock(ctx, buffer);
207 }
208
209 void groestl_large_ctx2hash(void *dest, const groestl_large_ctx_t *ctx, uint16_t outlength_b){
210         uint8_t tmp[128];
211         memcpy(tmp, ctx->h, 128);
212         groestl_large_rounds(tmp, 0);
213         memxor(tmp, ctx->h, 128);
214 #if DEBUG
215         cli_putstr_P(PSTR("\r\npost finalisation"));
216         dump_m(tmp);
217 #endif          
218         memcpy(dest, tmp+128-outlength_b/8, outlength_b/8);
219 }
220
221 void groestl384_ctx2hash(void *dest, const groestl384_ctx_t *ctx){
222         groestl_large_ctx2hash(dest, ctx, 384);
223 }
224
225 void groestl512_ctx2hash(void *dest, const groestl512_ctx_t *ctx){
226         groestl_large_ctx2hash(dest, ctx, 512);
227 }
228
229 void groestl384_nextBlock(groestl384_ctx_t *ctx, const void *block){
230         groestl_large_nextBlock(ctx, block);
231 }
232
233 void groestl512_nextBlock(groestl512_ctx_t *ctx, const void *block){
234         groestl_large_nextBlock(ctx, block);
235 }
236
237 void groestl384_lastBlock(groestl384_ctx_t *ctx, const void *block, uint16_t length_b){
238         groestl_large_lastBlock(ctx, block, length_b);
239 }
240
241 void groestl512_lastBlock(groestl512_ctx_t *ctx, const void *block, uint16_t length_b){
242         groestl_large_lastBlock(ctx, block, length_b);
243 }
244
245 void groestl384(void *dest, const void *msg, uint32_t length_b){
246         groestl_large_ctx_t ctx;
247         groestl384_init(&ctx);
248         while(length_b>=GROESTL_LARGE_BLOCKSIZE){
249                 groestl_large_nextBlock(&ctx, msg);
250                 length_b -= GROESTL_LARGE_BLOCKSIZE;
251                 msg = (uint8_t*)msg + GROESTL_LARGE_BLOCKSIZE_B;
252         }
253         groestl_large_lastBlock(&ctx, msg, length_b);
254         groestl_large_ctx2hash(dest, &ctx, 384);
255 }
256
257 void groestl512(void *dest, const void *msg, uint32_t length_b){
258         groestl_large_ctx_t ctx;
259         groestl512_init(&ctx);
260         while(length_b>=GROESTL_LARGE_BLOCKSIZE){
261                 groestl_large_nextBlock(&ctx, msg);
262                 length_b -= GROESTL_LARGE_BLOCKSIZE;
263                 msg = (uint8_t*)msg + GROESTL_LARGE_BLOCKSIZE_B;
264         }
265         groestl_large_lastBlock(&ctx, msg, length_b);
266         groestl_large_ctx2hash(dest, &ctx, 512);
267 }
268
269
270