3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
20 * \file groestl_small.c
22 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
28 #include "groestl_small.h"
42 void dump_m(const uint8_t* m){
48 cli_hexdump(m+8*i+j, 1);
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
68 void shift_columns(uint8_t* a, const uint8_t *shifts){
80 a[i+((j-s+8)%8)*8] = tmp[j];
85 static const uint8_t p_shifts[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
86 static const uint8_t q_shifts[8] = { 1, 3, 5, 7, 0, 2, 4, 6 };
89 void groestl_small_rounds(uint8_t *m, uint8_t q){
92 for(r=0; r<ROUNDS; ++r){
94 for(i=0; i<64/4; ++i){
95 ((uint32_t*)m)[i] ^= 0xffffffff;
98 m[7+i*8] ^= r ^ (i<<4);
102 m[i*8] ^= r ^ (i<<4);
107 cli_putstr("\r\npost add-const");
112 m[i] = aes_sbox[m[i]];
115 shift_columns(m, q_shifts);
117 shift_columns(m, p_shifts);
121 cli_putstr("\r\npost shift-bytes");
126 memcpy(tmp, m+8*i, 8);
128 m[j+i*8] = gf256mul(matrix[8*j+0],tmp[0], POLYNOM)
129 ^ gf256mul(matrix[8*j+1],tmp[1], POLYNOM)
130 ^ gf256mul(matrix[8*j+2],tmp[2], POLYNOM)
131 ^ gf256mul(matrix[8*j+3],tmp[3], POLYNOM)
132 ^ gf256mul(matrix[8*j+4],tmp[4], POLYNOM)
133 ^ gf256mul(matrix[8*j+5],tmp[5], POLYNOM)
134 ^ gf256mul(matrix[8*j+6],tmp[6], POLYNOM)
135 ^ gf256mul(matrix[8*j+7],tmp[7], POLYNOM);
140 cli_putstr("\r\npost mix-bytes");
147 void groestl224_init(groestl224_ctx_t* ctx){
148 memset(ctx->h, 0, 8*8);
153 void groestl256_init(groestl256_ctx_t* ctx){
154 memset(ctx->h, 0, 8*8);
159 void groestl_small_nextBlock(groestl_small_ctx_t* ctx, const void* block){
160 uint8_t tmp1[64], tmp2[64];
161 /* for(i=0; i<8; ++i){
163 tmp1[j*8+i] = ((uint8_t*)block)[i*8+j];
167 memcpy(tmp1, block, 64);
168 memcpy(tmp2, tmp1, 64);
169 memxor(tmp1, ctx->h, 64);
170 groestl_small_rounds(tmp1, 0);
171 groestl_small_rounds(tmp2, 1);
172 memxor(ctx->h, tmp1, 64);
173 memxor(ctx->h, tmp2, 64);
177 void groestl_small_lastBlock(groestl_small_ctx_t* ctx, const void* block, uint16_t length_b){
179 while(length_b>=GROESTL_SMALL_BLOCKSIZE){
180 groestl_small_nextBlock(ctx, block);
181 length_b -= GROESTL_SMALL_BLOCKSIZE;
182 block = (uint8_t*)block + GROESTL_SMALL_BLOCKSIZE_B;
184 memset(buffer, 0, 64);
185 memcpy(buffer, block, (length_b+7)/8);
186 buffer[length_b/8] |= 0x80>>(length_b&0x7);
188 groestl_small_nextBlock(ctx, buffer);
189 memset(buffer, 0, 64-4);
192 buffer[64-1] = (uint8_t)(ctx->counter);
193 buffer[64-2] = (uint8_t)((ctx->counter)>>8);
194 buffer[64-3] = (uint8_t)((ctx->counter)>>16);
195 buffer[64-4] = (uint8_t)((ctx->counter)>>24);
196 groestl_small_nextBlock(ctx, buffer);
199 void groestl_small_ctx2hash(void* dest, const groestl_small_ctx_t* ctx, uint16_t outlength_b){
201 memcpy(tmp, ctx->h, 64);
202 groestl_small_rounds(tmp, 0);
203 memxor(tmp, ctx->h, 64);
205 cli_putstr("\r\npost finalisation");
208 memcpy(dest, tmp+64-outlength_b/8, outlength_b/8);
211 void groestl224_ctx2hash(void* dest, const groestl224_ctx_t* ctx){
212 groestl_small_ctx2hash(dest, ctx, 224);
215 void groestl256_ctx2hash(void* dest, const groestl256_ctx_t* ctx){
216 groestl_small_ctx2hash(dest, ctx, 256);
219 void groestl224_nextBlock(groestl224_ctx_t* ctx, const void* block){
220 groestl_small_nextBlock(ctx, block);
223 void groestl256_nextBlock(groestl256_ctx_t* ctx, const void* block){
224 groestl_small_nextBlock(ctx, block);
227 void groestl224_lastBlock(groestl224_ctx_t* ctx, const void* block, uint16_t length_b){
228 groestl_small_lastBlock(ctx, block, length_b);
231 void groestl256_lastBlock(groestl256_ctx_t* ctx, const void* block, uint16_t length_b){
232 groestl_small_lastBlock(ctx, block, length_b);
235 void groestl224(void* dest, const void* msg, uint32_t length_b){
236 groestl_small_ctx_t ctx;
237 groestl224_init(&ctx);
238 while(length_b>=GROESTL_SMALL_BLOCKSIZE){
239 groestl_small_nextBlock(&ctx, msg);
240 length_b -= GROESTL_SMALL_BLOCKSIZE;
241 msg = (uint8_t*)msg + GROESTL_SMALL_BLOCKSIZE_B;
243 groestl_small_lastBlock(&ctx, msg, length_b);
244 groestl_small_ctx2hash(dest, &ctx, 224);
247 void groestl256(void* dest, const void* msg, uint32_t length_b){
248 groestl_small_ctx_t ctx;
249 groestl256_init(&ctx);
250 while(length_b>=GROESTL_SMALL_BLOCKSIZE){
251 groestl_small_nextBlock(&ctx, msg);
252 length_b -= GROESTL_SMALL_BLOCKSIZE;
253 msg = (uint8_t*)msg + GROESTL_SMALL_BLOCKSIZE_B;
255 groestl_small_lastBlock(&ctx, msg, length_b);
256 groestl_small_ctx2hash(dest, &ctx, 256);