]> git.cryptolib.org Git - arm-crypto-lib.git/blob - groestl/groestl_small.c
AES makestub added
[arm-crypto-lib.git] / groestl / groestl_small.c
1 /* groestl_small.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  * \file    groestl_small.c
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
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 <string.h>
34
35 #define ROUNDS 10
36 #define POLYNOM 0x1b
37
38 #define DEBUG 0
39
40 #if DEBUG
41  #include "cli.h"
42  void dump_m(const uint8_t* m){
43          uint8_t i,j;
44          for(i=0; i<8; ++i){
45                 cli_putstr("\r\n");
46                 for(j=0; j<8; ++j){
47                         cli_putc(' ');
48                         cli_hexdump(m+8*i+j, 1);
49                 }
50          }
51  }
52 #else
53  #define dump_m(m)
54 #endif
55
56 static const uint8_t matrix[] = {
57  2, 2, 3, 4, 5, 3, 5, 7,
58  7, 2, 2, 3, 4, 5, 3, 5,
59  5, 7, 2, 2, 3, 4, 5, 3,
60  3, 5, 7, 2, 2, 3, 4, 5,
61  5, 3, 5, 7, 2, 2, 3, 4,
62  4, 5, 3, 5, 7, 2, 2, 3,
63  3, 4, 5, 3, 5, 7, 2, 2,
64  2, 3, 4, 5, 3, 5, 7, 2
65 };
66
67 void groestl_small_rounds(uint8_t *m, uint8_t q){
68         uint8_t r,i,j;
69         uint8_t tmp[8];
70         for(r=0; r<ROUNDS; ++r){
71                 if(q){
72                         m[7] ^= 0xff ^ r;
73                 }else{
74                         m[0] ^= r;
75                 }
76 #if DEBUG
77                 if(r<2){
78                         cli_putstr("\r\npost add-const");
79                         dump_m(m);
80                 }
81 #endif
82                 for(i=0;i<8*8; ++i){
83                         m[i] = aes_sbox[m[i]];
84                 }
85                 for(i=1; i<8; ++i){
86                         for(j=0; j<8; ++j)
87                                 tmp[j] = m[i+8*j];
88                         for(j=0; j<8; ++j){
89                                 m[i+((j-i+8)%8)*8] = tmp[j];
90                         }
91                 }
92 #if DEBUG
93                 if(r<2){
94                         cli_putstr("\r\npost shift-bytes");
95                         dump_m(m);
96                 }
97 #endif
98                 for(i=0; i<8; ++i){
99                         memcpy(tmp, m+8*i, 8);
100                         for(j=0; j<8; ++j){
101                                 m[j+i*8] = gf256mul(matrix[8*j+0],tmp[0], POLYNOM)
102                                          ^ gf256mul(matrix[8*j+1],tmp[1], POLYNOM)
103                                          ^ gf256mul(matrix[8*j+2],tmp[2], POLYNOM)
104                                          ^ gf256mul(matrix[8*j+3],tmp[3], POLYNOM)
105                                          ^ gf256mul(matrix[8*j+4],tmp[4], POLYNOM)
106                                          ^ gf256mul(matrix[8*j+5],tmp[5], POLYNOM)
107                                          ^ gf256mul(matrix[8*j+6],tmp[6], POLYNOM)
108                                          ^ gf256mul(matrix[8*j+7],tmp[7], POLYNOM);
109                         }
110                 }
111 #if DEBUG
112                 if(r<2){
113                         cli_putstr("\r\npost mix-bytes");
114                         dump_m(m);
115                 }
116 #endif
117         }
118 }
119
120 void groestl224_init(groestl224_ctx_t* ctx){
121         memset(ctx->h, 0, 8*8);
122         ctx->h[8*8-1] = 224;
123         ctx->counter = 1;
124 }
125
126 void groestl256_init(groestl256_ctx_t* ctx){
127         memset(ctx->h, 0, 8*8);
128         ctx->h[8*8-2] = 1;
129         ctx->counter = 1;
130 }
131
132 void groestl_small_nextBlock(groestl_small_ctx_t* ctx, const void* block){
133         uint8_t tmp1[64], tmp2[64];
134 /*      for(i=0; i<8; ++i){
135                 for(j=0; j<8; ++j){
136                         tmp1[j*8+i] = ((uint8_t*)block)[i*8+j];
137                 }
138         }
139 */
140         memcpy(tmp1, block, 64);
141         memcpy(tmp2, tmp1, 64);
142         memxor(tmp1, ctx->h, 64);
143         groestl_small_rounds(tmp1, 0);
144         groestl_small_rounds(tmp2, 1);
145         memxor(ctx->h, tmp1, 64);
146         memxor(ctx->h, tmp2, 64);
147         ctx->counter++;
148 }
149
150 void groestl_small_lastBlock(groestl_small_ctx_t* ctx, const void* block, uint16_t length_b){
151         uint8_t buffer[64];
152         while(length_b>=GROESTL_SMALL_BLOCKSIZE){
153                 groestl_small_nextBlock(ctx, block);
154                 length_b -= GROESTL_SMALL_BLOCKSIZE;
155                 block = (uint8_t*)block + GROESTL_SMALL_BLOCKSIZE_B;
156         }
157         memset(buffer, 0, 64);
158         memcpy(buffer, block, (length_b+7)/8);
159         buffer[length_b/8] |= 0x80>>(length_b&0x7);
160         if(length_b>512-65){
161                 groestl_small_nextBlock(ctx, buffer);
162                 memset(buffer, 0, 64-4);
163         }
164 //      ctx->counter++;
165         buffer[64-1]  = (uint8_t)(ctx->counter);
166         buffer[64-2]  = (uint8_t)((ctx->counter)>>8);
167         buffer[64-3]  = (uint8_t)((ctx->counter)>>16);
168         buffer[64-4]  = (uint8_t)((ctx->counter)>>24);
169         groestl_small_nextBlock(ctx, buffer);
170 }
171
172 void groestl_small_ctx2hash(void* dest, const groestl_small_ctx_t* ctx, uint16_t outlength_b){
173         uint8_t tmp[64];
174         memcpy(tmp, ctx->h, 64);
175         groestl_small_rounds(tmp, 0);
176         memxor(tmp, ctx->h, 64);
177 #if DEBUG
178         cli_putstr("\r\npost finalisation");
179         dump_m(tmp);
180 #endif
181         memcpy(dest, tmp+64-outlength_b/8, outlength_b/8);
182 }
183
184 void groestl224_ctx2hash(void* dest, const groestl224_ctx_t* ctx){
185         groestl_small_ctx2hash(dest, ctx, 224);
186 }
187
188 void groestl256_ctx2hash(void* dest, const groestl256_ctx_t* ctx){
189         groestl_small_ctx2hash(dest, ctx, 256);
190 }
191
192 void groestl224_nextBlock(groestl224_ctx_t* ctx, const void* block){
193         groestl_small_nextBlock(ctx, block);
194 }
195
196 void groestl256_nextBlock(groestl256_ctx_t* ctx, const void* block){
197         groestl_small_nextBlock(ctx, block);
198 }
199
200 void groestl224_lastBlock(groestl224_ctx_t* ctx, const void* block, uint16_t length_b){
201         groestl_small_lastBlock(ctx, block, length_b);
202 }
203
204 void groestl256_lastBlock(groestl256_ctx_t* ctx, const void* block, uint16_t length_b){
205         groestl_small_lastBlock(ctx, block, length_b);
206 }
207
208 void groestl224(void* dest, const void* msg, uint32_t length_b){
209         groestl_small_ctx_t ctx;
210         groestl224_init(&ctx);
211         while(length_b>=GROESTL_SMALL_BLOCKSIZE){
212                 groestl_small_nextBlock(&ctx, msg);
213                 length_b -= GROESTL_SMALL_BLOCKSIZE;
214                 msg = (uint8_t*)msg + GROESTL_SMALL_BLOCKSIZE_B;
215         }
216         groestl_small_lastBlock(&ctx, msg, length_b);
217         groestl_small_ctx2hash(dest, &ctx, 224);
218 }
219
220 void groestl256(void* dest, const void* msg, uint32_t length_b){
221         groestl_small_ctx_t ctx;
222         groestl256_init(&ctx);
223         while(length_b>=GROESTL_SMALL_BLOCKSIZE){
224                 groestl_small_nextBlock(&ctx, msg);
225                 length_b -= GROESTL_SMALL_BLOCKSIZE;
226                 msg = (uint8_t*)msg + GROESTL_SMALL_BLOCKSIZE_B;
227         }
228         groestl_small_lastBlock(&ctx, msg, length_b);
229         groestl_small_ctx2hash(dest, &ctx, 256);
230 }
231
232
233