]> git.cryptolib.org Git - avr-crypto-lib.git/blob - whirlpool/whirlpool.c
now including whirlpool
[avr-crypto-lib.git] / whirlpool / whirlpool.c
1 /* whirlpool.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2011 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 #include <stdint.h>
21 #include <avr/pgmspace.h>
22 #include <string.h>
23 #include "gf256mul.h"
24 #include "memxor.h"
25 #include "whirlpool.h"
26
27 #define DEBUG 0
28
29 #if DEBUG
30 #include "cli.h"
31 #endif
32
33 /*
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
37 */
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
42 };
43
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
47 };
48
49 uint8_t whirlpool_sbox(uint8_t a){
50         uint8_t b,c,d;
51         b = pgm_read_byte(eeinv_box+(a&0x0f));
52         d = pgm_read_byte(eeinv_box+(a>>4));
53         c = ((d>>4)^b)&0x0f;
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;
58         return c;
59 }
60
61 static void gamma(uint8_t* a){
62         uint8_t i;
63         for(i=0; i<64; ++i){
64                 *a = whirlpool_sbox(*a);
65                 ++a;
66         }
67 }
68
69 static void pi(uint8_t* a){
70         uint8_t b[8];
71         uint8_t i,j;
72         for(i=1; i<8; ++i){
73                 for(j=0; j<8; ++j){
74                         b[j] = a[i+8*((8+j-i)%8)];
75                 }
76                 for(j=0; j<8; ++j){
77                         a[j*8+i] = b[j];
78                 }
79         }
80 }
81
82 static uint8_t theta_matrix[8] PROGMEM = {
83         0x1, 0x1, 0x4, 0x1, 0x8, 0x5, 0x2, 0x9
84 };
85
86 #define POLYNOM 0x1D
87
88 static void theta(uint8_t* a){
89         uint8_t b[8], c, accu;
90         uint8_t i,j,k;
91         for(i=0; i<8; ++i){
92                 for(j=0; j<8;++j){
93                         accu = 0;
94                         for(k=0; k<8; ++k){
95                                 c = pgm_read_byte(theta_matrix+((8+j-k)%8));
96                                 accu ^= gf256mul(a[8*i+k], c, POLYNOM);
97                         }
98                         b[j] = accu;
99                 }
100                 memcpy(a+8*i, b, 8);
101         }
102 }
103
104 static void w_round(uint8_t* a, const uint8_t* k){
105         gamma(a);
106 #if DEBUG
107         cli_putstr_P(PSTR("\r\n pre-pi:"));
108         cli_hexdump_block(a, 64, 4, 8);
109 #endif
110         pi(a);
111 #if DEBUG
112         cli_putstr_P(PSTR("\r\n post-pi & pre-theta:"));
113         cli_hexdump_block(a, 64, 4, 8);
114 #endif
115         theta(a);
116 #if DEBUG
117         cli_putstr_P(PSTR("\r\n post-theta:"));
118         cli_hexdump_block(a, 64, 4, 8);
119 #endif
120         memxor(a, k, 64);
121 }
122
123 static void w_enc(uint8_t *a, const uint8_t* k){
124 #if DEBUG
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);
129 #endif
130         uint8_t rk[64], rc[64];
131         uint8_t r,i;
132         memxor(a, k, 64);
133         memcpy(rk, k, 64);
134         memset(rc+8, 0, 56);
135         for(r=0; r<10; ++r){
136                 for(i=0; i<8; ++i){
137                         rc[i] = whirlpool_sbox(r*8+i);
138                 }
139                 w_round(rk, rc);
140                 w_round(a, rk);
141 #if DEBUG
142                 cli_putstr_P(PSTR("\r\n w'_"));
143                 cli_hexdump_byte(r+1);
144                 cli_putc(':');
145                 cli_hexdump_block(a, 64, 4, 8);
146                 cli_putstr_P(PSTR("\r\n k_"));
147                 cli_hexdump_byte(r+1);
148                 cli_putc(':');
149                 cli_hexdump_block(rk, 64, 4, 8);
150 #endif
151         }
152 }
153
154 void whirlpool_init(whirlpool_ctx_t* ctx){
155         memset(ctx->s, 0, 64);
156         ctx->blocks = 0;
157 }
158
159 void whirlpool_nextBlock(whirlpool_ctx_t* ctx, const void* block){
160         uint8_t state[64];
161         ctx->blocks += 1;
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);
166 }
167
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;
172                 length_b -= 512;
173         }
174         uint8_t buffer[64];
175         uint8_t i=8;
176         uint64_t length;
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);
181         if(length_b>255){
182                 whirlpool_nextBlock(ctx, buffer);
183                 memset(buffer, 0, 56);
184         }
185         do{
186                 buffer[56+(--i)] = length&0xff;
187                 length >>= 8;
188         }while(i);
189         whirlpool_nextBlock(ctx, buffer);
190 }
191
192 void whirlpool_ctx2hash(void* dest, const whirlpool_ctx_t* ctx){
193         memcpy(dest, (ctx->s), 64);
194 }