]> git.cryptolib.org Git - avr-crypto-lib.git/blob - groestl/groestl_small.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / groestl / groestl_small.c
1 /* groestl_small.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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_small.c
21  * \author  Daniel Otte
22  * \email   bg@nerilex.org
23  * \date    2009-05-19
24  * \license GPLv3 or later
25  *
26  */
27
28 #include "groestl_small.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 10
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<8; ++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
69 static const uint8_t p_shifts[] PROGMEM = {
70                 0, 1, 2, 3, 4, 5, 6, 7
71 };
72
73 static const uint8_t q_shifts[] PROGMEM = {
74                 1, 3, 5, 7, 0, 2, 4, 6
75 };
76
77
78 static
79 void shift_columns(uint8_t *a, PGM_VOID_P shifts){
80         uint8_t s;
81         uint8_t tmp[8];
82         uint8_t i,j;
83         for(i=0; i<8; ++i){
84                 s = pgm_read_byte(shifts);
85                 shifts = (uint8_t*)shifts + 1;
86                 if(s==0){
87                         continue;
88                 }
89                 for(j=0;j<8;++j){
90                         tmp[j] = a[i+j*8];
91                 }
92                 for(j=0; j<8; ++j){
93                         a[i+((j-s+8)%8)*8] = tmp[j];
94                 }
95         }
96 }
97
98
99
100 void groestl_small_rounds(uint8_t *m, uint8_t q){
101         uint8_t r,i,j;
102         uint8_t tmp[8];
103 #if DEBUG
104         cli_putstr_P(PSTR("\r\n:: BEGIN "));
105         cli_putc(q?'Q':'P');
106 #endif
107
108         for(r=0; r<ROUNDS; ++r){
109                 if(q){
110                         for(i=0; i<8*8; ++i){
111                                 m[i] ^= 0xff;
112                         }
113                         for(i=0; i<8; ++i){
114                                 m[7+i*8] ^=  r ^ (i<<4);
115                         }
116                 }else{
117                         for(i=0; i<8; ++i){
118                                 m[i*8] ^= r ^ (i<<4);
119                         }
120                 }
121 #if DEBUG
122         //      if(r<2){
123                         cli_putstr_P(PSTR("\r\npost add-const"));
124                         dump_m(m);
125         //      }
126 #endif
127                 for(i=0;i<8*8; ++i){
128                         m[i] = pgm_read_byte(aes_sbox+m[i]);
129                 }
130                 if(!q){
131                         shift_columns(m, p_shifts);
132                 }else{
133                         shift_columns(m, q_shifts);
134                 }
135 #if DEBUG
136                 if(r<2){
137                         cli_putstr_P(PSTR("\r\npost shift-bytes"));
138                         dump_m(m);
139                 }
140 #endif
141                 for(i=0; i<8; ++i){
142                         memcpy(tmp, m+8*i, 8);
143                         for(j=0; j<8; ++j){
144                                 m[j+i*8] = gf256mul(pgm_read_byte(matrix+8*j+0),tmp[0], POLYNOM)
145                                         ^ gf256mul(pgm_read_byte(matrix+8*j+1),tmp[1], POLYNOM)
146                                         ^ gf256mul(pgm_read_byte(matrix+8*j+2),tmp[2], POLYNOM)
147                                         ^ gf256mul(pgm_read_byte(matrix+8*j+3),tmp[3], POLYNOM)
148                                         ^ gf256mul(pgm_read_byte(matrix+8*j+4),tmp[4], POLYNOM)
149                                         ^ gf256mul(pgm_read_byte(matrix+8*j+5),tmp[5], POLYNOM)
150                                         ^ gf256mul(pgm_read_byte(matrix+8*j+6),tmp[6], POLYNOM)
151                                         ^ gf256mul(pgm_read_byte(matrix+8*j+7),tmp[7], POLYNOM);
152                         }
153                 }
154 #if DEBUG
155                 if(r<2){
156                         cli_putstr_P(PSTR("\r\npost mix-bytes"));
157                         dump_m(m);
158                 }
159 #endif
160         }
161 }
162
163 void groestl224_init(groestl224_ctx_t *ctx){
164         memset(ctx->h, 0, 8*8);
165         ctx->h[8*8-1] = 224;
166         ctx->counter = 1;
167 }
168
169 void groestl256_init(groestl256_ctx_t *ctx){
170         memset(ctx->h, 0, 8*8);
171         ctx->h[8*8-2] = 1;
172         ctx->counter = 1;
173 }
174
175 void groestl_small_nextBlock(groestl_small_ctx_t *ctx, const void *block){
176         uint8_t tmp1[64], tmp2[64];
177 /*      for(i=0; i<8; ++i){
178                 for(j=0; j<8; ++j){
179                         tmp1[j*8+i] = ((uint8_t*)block)[i*8+j];
180                 }
181         }
182 */
183         memcpy(tmp1, block, 64);
184         memcpy(tmp2, tmp1, 64);
185         memxor(tmp1, ctx->h, 64);
186         groestl_small_rounds(tmp1, 0);
187         groestl_small_rounds(tmp2, 1);
188         memxor(ctx->h, tmp1, 64);
189         memxor(ctx->h, tmp2, 64);
190         ctx->counter++;
191 }
192
193 void groestl_small_lastBlock(groestl_small_ctx_t *ctx, const void *block, uint16_t length_b){
194         uint8_t buffer[64];
195         while(length_b>=GROESTL_SMALL_BLOCKSIZE){
196                 groestl_small_nextBlock(ctx, block);
197                 length_b -= GROESTL_SMALL_BLOCKSIZE;
198                 block = (uint8_t*)block + GROESTL_SMALL_BLOCKSIZE_B;
199         }
200         memset(buffer, 0, 64);
201         memcpy(buffer, block, (length_b+7)/8);
202         buffer[length_b/8] |= 0x80>>(length_b&0x7);
203         if(length_b>512-65){
204                 groestl_small_nextBlock(ctx, buffer);
205                 memset(buffer, 0, 64-4);
206         }
207 //      ctx->counter++;
208         buffer[64-1]  = (uint8_t)(ctx->counter);
209         buffer[64-2]  = (uint8_t)((ctx->counter)>>8);
210         buffer[64-3]  = (uint8_t)((ctx->counter)>>16);
211         buffer[64-4]  = (uint8_t)((ctx->counter)>>24);
212         groestl_small_nextBlock(ctx, buffer);
213 }
214
215 void groestl_small_ctx2hash(void *dest, const groestl_small_ctx_t *ctx, uint16_t outlength_b){
216         uint8_t tmp[64];
217         memcpy(tmp, ctx->h, 64);
218         groestl_small_rounds(tmp, 0);
219         memxor(tmp, ctx->h, 64);
220 #if DEBUG
221         cli_putstr_P(PSTR("\r\npost finalisation"));
222         dump_m(tmp);
223 #endif
224         memcpy(dest, tmp+64-outlength_b/8, outlength_b/8);
225 }
226
227 void groestl224_ctx2hash(void *dest, const groestl224_ctx_t *ctx){
228         groestl_small_ctx2hash(dest, ctx, 224);
229 }
230
231 void groestl256_ctx2hash(void *dest, const groestl256_ctx_t *ctx){
232         groestl_small_ctx2hash(dest, ctx, 256);
233 }
234
235 void groestl224_nextBlock(groestl224_ctx_t *ctx, const void *block){
236         groestl_small_nextBlock(ctx, block);
237 }
238
239 void groestl256_nextBlock(groestl256_ctx_t *ctx, const void *block){
240         groestl_small_nextBlock(ctx, block);
241 }
242
243 void groestl224_lastBlock(groestl224_ctx_t *ctx, const void *block, uint16_t length_b){
244         groestl_small_lastBlock(ctx, block, length_b);
245 }
246
247 void groestl256_lastBlock(groestl256_ctx_t *ctx, const void *block, uint16_t length_b){
248         groestl_small_lastBlock(ctx, block, length_b);
249 }
250
251 void groestl224(void *dest, const void *msg, uint32_t length_b){
252         groestl_small_ctx_t ctx;
253         groestl224_init(&ctx);
254         while(length_b>=GROESTL_SMALL_BLOCKSIZE){
255                 groestl_small_nextBlock(&ctx, msg);
256                 length_b -= GROESTL_SMALL_BLOCKSIZE;
257                 msg = (uint8_t*)msg + GROESTL_SMALL_BLOCKSIZE_B;
258         }
259         groestl_small_lastBlock(&ctx, msg, length_b);
260         groestl_small_ctx2hash(dest, &ctx, 224);
261 }
262
263 void groestl256(void *dest, const void *msg, uint32_t length_b){
264         groestl_small_ctx_t ctx;
265         groestl256_init(&ctx);
266         while(length_b>=GROESTL_SMALL_BLOCKSIZE){
267                 groestl_small_nextBlock(&ctx, msg);
268                 length_b -= GROESTL_SMALL_BLOCKSIZE;
269                 msg = (uint8_t*)msg + GROESTL_SMALL_BLOCKSIZE_B;
270         }
271         groestl_small_lastBlock(&ctx, msg, length_b);
272         groestl_small_ctx2hash(dest, &ctx, 256);
273 }
274
275
276