3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006-2011 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/>.
21 #include <avr/pgmspace.h>
25 #include "whirlpool.h"
34 u 0 1 2 3 4 5 6 7 8 9 A B C D E F
35 E(u) 1 B 9 C D 6 F 3 E 8 7 4 A 2 5 0
36 E -1(u) F 0 D 7 B E 5 A 9 2 C 1 3 4 8 6
38 static uint8_t eeinv_box[16] PROGMEM = {
39 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
40 0x1F, 0xB0, 0x9D, 0xC7, 0xDB, 0x6E, 0xF5, 0x3A,
41 0xE9, 0x82, 0x7C, 0x41, 0xA3, 0x24, 0x58, 0x06
44 static uint8_t r_box[16] PROGMEM = {
45 0x77, 0xCC, 0xBB, 0xDD, 0xEE, 0x44, 0x99, 0xFF,
46 0x66, 0x33, 0x88, 0xAA, 0x22, 0x55, 0x11, 0x00
49 uint8_t whirlpool_sbox(uint8_t a){
51 b = pgm_read_byte(eeinv_box+(a&0x0f));
52 d = pgm_read_byte(eeinv_box+(a>>4));
54 b = (b&0x0f)|(d&0xf0);
55 b ^= pgm_read_byte(r_box+c);
56 c = pgm_read_byte(eeinv_box+(b&0x0f))&0x0f;
57 c |= pgm_read_byte(eeinv_box+(b>>4))&0xf0;
61 static void gamma(uint8_t* a){
64 *a = whirlpool_sbox(*a);
69 static void pi(uint8_t* a){
74 b[j] = a[i+8*((8+j-i)%8)];
82 static uint8_t theta_matrix[8] PROGMEM = {
83 0x1, 0x1, 0x4, 0x1, 0x8, 0x5, 0x2, 0x9
88 static void theta(uint8_t* a){
89 uint8_t b[8], c, accu;
95 c = pgm_read_byte(theta_matrix+((8+j-k)%8));
96 accu ^= gf256mul(a[8*i+k], c, POLYNOM);
104 static void w_round(uint8_t* a, const uint8_t* k){
107 cli_putstr_P(PSTR("\r\n pre-pi:"));
108 cli_hexdump_block(a, 64, 4, 8);
112 cli_putstr_P(PSTR("\r\n post-pi & pre-theta:"));
113 cli_hexdump_block(a, 64, 4, 8);
117 cli_putstr_P(PSTR("\r\n post-theta:"));
118 cli_hexdump_block(a, 64, 4, 8);
123 static void w_enc(uint8_t *a, const uint8_t* k){
125 cli_putstr_P(PSTR("\r\n== w_enc ==\r\n w'_00:"));
126 cli_hexdump_block(a, 64, 4, 8);
127 cli_putstr_P(PSTR("\r\n k_00:"));
128 cli_hexdump_block(k, 64, 4, 8);
130 uint8_t rk[64], rc[64];
137 rc[i] = whirlpool_sbox(r*8+i);
142 cli_putstr_P(PSTR("\r\n w'_"));
143 cli_hexdump_byte(r+1);
145 cli_hexdump_block(a, 64, 4, 8);
146 cli_putstr_P(PSTR("\r\n k_"));
147 cli_hexdump_byte(r+1);
149 cli_hexdump_block(rk, 64, 4, 8);
154 void whirlpool_init(whirlpool_ctx_t* ctx){
155 memset(ctx->s, 0, 64);
159 void whirlpool_nextBlock(whirlpool_ctx_t* ctx, const void* block){
162 memcpy(state, block, 64);
163 w_enc(state, (uint8_t*)(ctx->s));
164 memxor(ctx->s, state, 64);
165 memxor((ctx->s), block, 64);
168 void whirlpool_lastBlock(whirlpool_ctx_t* ctx, const void* block, uint16_t length_b){
169 while(length_b>=512){
170 whirlpool_nextBlock(ctx, block);
171 block = (uint8_t*)block + 64;
177 length = ctx->blocks*512+length_b;
178 memset(buffer, 0, 64);
179 memcpy(buffer, block, (length_b+7)/8);
180 buffer[length_b/8] |= 0x80>>(length_b&7);
182 whirlpool_nextBlock(ctx, buffer);
183 memset(buffer, 0, 56);
186 buffer[56+(--i)] = length&0xff;
189 whirlpool_nextBlock(ctx, buffer);
192 void whirlpool_ctx2hash(void* dest, const whirlpool_ctx_t* ctx){
193 memcpy(dest, (ctx->s), 64);