]> git.cryptolib.org Git - arm-crypto-lib.git/blob - groestl/groestl_large.c
switching to dedicated endian switching function
[arm-crypto-lib.git] / groestl / groestl_large.c
1 /* groestl_large.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_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 <string.h>
34
35 #define ROUNDS 14
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<16; ++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 static
68 void shift_columns(uint8_t* a, const uint8_t *shifts){
69         uint8_t tmp[16];
70         uint8_t i,j,s;
71         for(i=0; i<8; ++i){
72                 s = *shifts++;
73                 if(s==0){
74                         continue;
75                 }
76                 for(j=0;j<16;++j){
77                         tmp[j] = a[i+j*8];
78                 }
79                 for(j=0;j<16;++j){
80                         a[i+((j-s+16)%16)*8] = tmp[j];
81                 }
82         }
83 }
84
85 static const uint8_t p_shifts[8] = { 0, 1, 2, 3, 4, 5, 6, 11 };
86 static const uint8_t q_shifts[8] = { 1, 3, 5, 11, 0, 2, 4, 6 };
87
88 static
89 void groestl_large_rounds(uint8_t *m, uint8_t q){
90         uint8_t r,i,j;
91         uint8_t tmp[16];
92         for(r=0; r<ROUNDS; ++r){
93                 if(q){
94                         for(i=0;i<64/2; ++i){
95                                 ((uint32_t*)m)[i] ^= 0xffffffff;
96                         }
97                         for(i=0; i<16; ++i){
98                                 m[i*8+7] ^= r ^ (i<<4);
99                         }
100                 }else{
101                         for(i=0; i<16; ++i){
102                                 m[i*8] ^= r ^ (i<<4);
103                         }
104                 }       
105 #if DEBUG               
106                 if(r<2){
107                         cli_putstr("\r\npost add-const");
108                         dump_m(m);
109                 }
110 #endif
111                 for(i=0;i<16*8; ++i){
112                         m[i] = aes_sbox[m[i]];
113                 }
114                 if(q){
115                         shift_columns(m, q_shifts);
116                 }else{
117                         shift_columns(m, p_shifts);
118                 }
119                 
120 #if DEBUG               
121                 if(r<2){
122                         cli_putstr("\r\npost shift-bytes");
123                         dump_m(m);
124                 }
125 #endif          
126                 for(i=0; i<16; ++i){
127                         memcpy(tmp, m+8*i, 8);
128                         for(j=0; j<8; ++j){
129                                 m[j+i*8] = gf256mul(matrix[8*j+0],tmp[0], POLYNOM)
130                                          ^ gf256mul(matrix[8*j+1],tmp[1], POLYNOM)
131                                          ^ gf256mul(matrix[8*j+2],tmp[2], POLYNOM)
132                                          ^ gf256mul(matrix[8*j+3],tmp[3], POLYNOM)
133                                          ^ gf256mul(matrix[8*j+4],tmp[4], POLYNOM)
134                                          ^ gf256mul(matrix[8*j+5],tmp[5], POLYNOM)
135                                          ^ gf256mul(matrix[8*j+6],tmp[6], POLYNOM)
136                                          ^ gf256mul(matrix[8*j+7],tmp[7], POLYNOM);
137                         }
138                 }
139 #if DEBUG
140                 if(r<2){
141                         cli_putstr("\r\npost mix-bytes");
142                         dump_m(m);
143                 }
144 #endif                  
145         }
146 }
147
148 void groestl384_init(groestl384_ctx_t* ctx){
149         memset(ctx->h, 0, 16*8);
150         ctx->h[8*16-1] = (uint8_t)384;
151         ctx->h[8*16-2] = (uint8_t)(384>>8);
152         ctx->counter = 0;
153 }
154
155 void groestl512_init(groestl512_ctx_t* ctx){
156         memset(ctx->h, 0, 16*8);
157         ctx->h[8*16-2] = 2;
158         ctx->counter = 0;
159 }
160
161 void groestl_large_nextBlock(groestl_large_ctx_t* ctx, const void* block){
162         uint8_t tmp1[128], tmp2[128];
163 /*
164         for(i=0; i<8; ++i){
165                 for(j=0; j<8; ++j){
166                         tmp1[j*8+i] = ((uint8_t*)block)[i*8+j];
167                 }
168         }
169 */ 
170         memcpy(tmp1, block, 128);
171         memcpy(tmp2, tmp1, 128);
172         memxor(tmp1, ctx->h, 128);
173         groestl_large_rounds(tmp1, 0);
174         groestl_large_rounds(tmp2, 1);
175         memxor(ctx->h, tmp1, 128);
176         memxor(ctx->h, tmp2, 128);
177         ctx->counter++;
178 }
179
180 void groestl_large_lastBlock(groestl_large_ctx_t* ctx, const void* block, uint16_t length_b){
181         uint8_t buffer[128];
182         while(length_b>=GROESTL_LARGE_BLOCKSIZE){
183                 groestl_large_nextBlock(ctx, block);
184                 length_b -= GROESTL_LARGE_BLOCKSIZE;
185                 block = (uint8_t*)block + GROESTL_LARGE_BLOCKSIZE_B;
186         }
187         memset(buffer, 0, 128);
188         memcpy(buffer, block, (length_b+7)/8);
189         buffer[length_b/8] |= 0x80>>(length_b%8);
190         if(length_b>1024-65){
191                 groestl_large_nextBlock(ctx, buffer);
192                 memset(buffer, 0, 128-4);
193         }
194         ctx->counter++;
195         buffer[128-1]  = (uint8_t)(ctx->counter);
196         buffer[128-2]  = (uint8_t)((ctx->counter)>>8);
197         buffer[128-3]  = (uint8_t)((ctx->counter)>>16);
198         buffer[128-4]  = (uint8_t)((ctx->counter)>>24);
199         groestl_large_nextBlock(ctx, buffer);
200 }
201
202 void groestl_large_ctx2hash(void* dest, const groestl_large_ctx_t* ctx, uint16_t outlength_b){
203         uint8_t tmp[128];
204         memcpy(tmp, ctx->h, 128);
205         groestl_large_rounds(tmp, 0);
206         memxor(tmp, ctx->h, 128);
207 #if DEBUG
208         cli_putstr("\r\npost finalisation");
209         dump_m(tmp);
210 #endif          
211         memcpy(dest, tmp+128-outlength_b/8, outlength_b/8);
212 }
213
214 void groestl384_ctx2hash(void* dest, const groestl384_ctx_t* ctx){
215         groestl_large_ctx2hash(dest, ctx, 384);
216 }
217
218 void groestl512_ctx2hash(void* dest, const groestl512_ctx_t* ctx){
219         groestl_large_ctx2hash(dest, ctx, 512);
220 }
221
222 void groestl384_nextBlock(groestl384_ctx_t* ctx, const void* block){
223         groestl_large_nextBlock(ctx, block);
224 }
225
226 void groestl512_nextBlock(groestl512_ctx_t* ctx, const void* block){
227         groestl_large_nextBlock(ctx, block);
228 }
229
230 void groestl384_lastBlock(groestl384_ctx_t* ctx, const void* block, uint16_t length_b){
231         groestl_large_lastBlock(ctx, block, length_b);
232 }
233
234 void groestl512_lastBlock(groestl512_ctx_t* ctx, const void* block, uint16_t length_b){
235         groestl_large_lastBlock(ctx, block, length_b);
236 }
237
238 void groestl384(void* dest, const void* msg, uint32_t length_b){
239         groestl_large_ctx_t ctx;
240         groestl384_init(&ctx);
241         while(length_b>=GROESTL_LARGE_BLOCKSIZE){
242                 groestl_large_nextBlock(&ctx, msg);
243                 length_b -= GROESTL_LARGE_BLOCKSIZE;
244                 msg = (uint8_t*)msg + GROESTL_LARGE_BLOCKSIZE_B;
245         }
246         groestl_large_lastBlock(&ctx, msg, length_b);
247         groestl_large_ctx2hash(dest, &ctx, 384);
248 }
249
250 void groestl512(void* dest, const void* msg, uint32_t length_b){
251         groestl_large_ctx_t ctx;
252         groestl512_init(&ctx);
253         while(length_b>=GROESTL_LARGE_BLOCKSIZE){
254                 groestl_large_nextBlock(&ctx, msg);
255                 length_b -= GROESTL_LARGE_BLOCKSIZE;
256                 msg = (uint8_t*)msg + GROESTL_LARGE_BLOCKSIZE_B;
257         }
258         groestl_large_lastBlock(&ctx, msg, length_b);
259         groestl_large_ctx2hash(dest, &ctx, 512);
260 }
261
262
263