]> git.cryptolib.org Git - avr-crypto-lib.git/blob - blake_large.c
+blake, bug compatibility by default off
[avr-crypto-lib.git] / blake_large.c
1 /* blake_large.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2009  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    blake_large.c
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-05-08
24  * \license GPLv3 or later
25  * 
26  */
27
28 #include <stdint.h>
29 #include <string.h>
30 #include <avr/pgmspace.h>
31 #include "memxor.h"
32 #include "blake_large.h"
33 #include "blake_common.h"
34
35 #define DEBUG   0 
36 #define DEBUG_2 0 
37
38 #define BUG_3 0 /* bug compatibility with reference code */
39 #define BUG_4 0 /* bug compatibility with reference code */
40
41 #if DEBUG_2
42  #include "cli.h"
43 #endif
44
45 #if DEBUG
46  #include "cli.h"
47  void dump_v(uint64_t* v){
48         uint8_t i;
49         cli_putstr_P(PSTR("\r\n=== v dump ==="));
50         for(i=0; i<16; ++i){
51                 if(i%4==0)
52                         cli_putstr_P(PSTR("\r\n\t"));
53                 cli_hexdump_rev(v+i, 8);
54                 cli_putc(' ');
55         }
56  }
57 #else
58  #define dump_v(v)
59 #endif
60
61 uint64_t pgm_read_qword(void* p){
62         union{
63                 uint64_t v64;
64                 uint32_t v32[2];
65         }r;
66         r.v32[0] = pgm_read_dword(p);
67         r.v32[1] = pgm_read_dword((uint8_t*)p+4);
68         return r.v64;
69 }
70
71 static
72 uint64_t blake_c[] PROGMEM = {   
73    0x243F6A8885A308D3LL, 0x13198A2E03707344LL,
74    0xA4093822299F31D0LL, 0x082EFA98EC4E6C89LL,
75    0x452821E638D01377LL, 0xBE5466CF34E90C6CLL,
76    0xC0AC29B7C97C50DDLL, 0x3F84D5B5B5470917LL,
77    0x9216D5D98979FB1BLL, 0xD1310BA698DFB5ACLL,
78    0x2FFD72DBD01ADFB7LL, 0xB8E1AFED6A267E96LL,
79    0xBA7C9045F12C7F99LL, 0x24A19947B3916CF7LL,
80    0x0801F2E2858EFC16LL, 0x636920D871574E69LL
81
82 };
83
84
85
86 #define ROTL64(a, n) (((a)<<(n))|((a)>>(64-(n)))) 
87 #define ROTR64(a, n) (((a)>>(n))|((a)<<(64-(n)))) 
88 #define CHANGE_ENDIAN32(a) (((a)<<24)| \
89                             ((0x0000ff00&(a))<<8)| \
90                                                     ((0x00ff0000&(a))>>8)| \
91                                                     (a)>>24 )
92
93 void blake_large_g(uint8_t r, uint8_t i, uint64_t* v, const uint64_t* m){
94         uint8_t a,b,c,d, s0, s1;
95         a = pgm_read_byte(blake_index_lut+4*i+0);
96         b = pgm_read_byte(blake_index_lut+4*i+1);
97         c = pgm_read_byte(blake_index_lut+4*i+2);
98         d = pgm_read_byte(blake_index_lut+4*i+3);
99         s0 = pgm_read_byte(blake_sigma+16*r+2*i+0);
100         s1 = pgm_read_byte(blake_sigma+16*r+2*i+1);
101 #if DEBUG
102         if(i==0){
103                 cli_putstr_P(PSTR("\r\n s0 = "));
104                 cli_hexdump(&s0, 1);
105                 cli_putstr_P(PSTR("   s1 = "));
106                 cli_hexdump(&s1, 1);
107                 cli_putstr_P(PSTR("\r\n m[s0] = "));
108                 cli_hexdump_rev(m+s0, 4);
109                 cli_putstr_P(PSTR("\r\n m[s1] = "));
110                 cli_hexdump_rev(m+s1, 4);
111         }
112 #endif          
113         v[a] += v[b] + (m[s0] ^ pgm_read_qword(&(blake_c[s1])));
114         v[d]  = ROTR64(v[d]^v[a], 32);
115         v[c] += v[d];
116         v[b]  = ROTR64(v[b]^v[c], 25);  
117         v[a] += v[b] + (m[s1] ^ pgm_read_qword(&(blake_c[s0])));
118         v[d]  = ROTR64(v[d]^v[a], 16);
119         v[c] += v[d];
120         v[b]  = ROTR64(v[b]^v[c], 11);
121
122 }
123
124 void blake_large_expand(uint64_t* v, const blake_large_ctx_t* ctx){
125         uint8_t i;
126         memcpy(v, ctx->h, 8*8);
127         for(i=0; i<8; ++i){
128                 v[8+i] = pgm_read_qword(&(blake_c[i]));
129         }
130         memxor((uint8_t*)v+8, ctx->s, 4*8);
131         
132 }
133
134 void blake_large_changeendian(void* dest, const void* src){
135         uint8_t i;
136         uint32_t tmp;
137         for(i=0; i<32; i+=2){
138                 tmp = CHANGE_ENDIAN32(((uint32_t*)src)[i]);
139                 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(((uint32_t*)src)[i+1]);
140                 ((uint32_t*)dest)[i+1] = tmp;
141         }
142 }
143
144 void blake_large_compress(uint64_t* v,const void* m){
145         uint8_t r,i;
146 #if DEBUG       
147         cli_putstr_P(PSTR("\r\n== compress 64 =="));
148         dump_v(v);
149 #endif
150 #if DEBUG_2
151         cli_putstr_P(PSTR("\r\n=== message block ===\r\n m ="));
152         cli_hexdump_block(m, 1024/8, 4, 16);
153 #endif
154         for(r=0; r<14; ++r){
155                 for(i=0; i<8; ++i){
156                         blake_large_g(r%10, i, v, (uint64_t*)m);
157 #if DEBUG
158                         if(1){
159                                 cli_putstr_P(PSTR("\r\n ROUND: "));
160                                 cli_hexdump(&r,1);
161                                 cli_putstr_P(PSTR("    I: "));
162                                 cli_hexdump(&i,1);
163                                 dump_v(v);
164                         }
165 #endif
166                 }
167         }
168 }
169
170 void blake_large_collapse(blake_large_ctx_t* ctx, uint64_t* v){
171         uint8_t i;
172         for(i=0; i<8; ++i){
173                 ctx->h[i] ^= ctx->s[i%4] ^ v[i] ^ v[8+i];
174         }
175 }       
176
177 void blake_large_nextBlock(blake_large_ctx_t* ctx, const void* msg){
178         uint64_t v[16];
179         uint64_t m[16];
180         union {
181                 uint64_t v64;
182                 uint32_t v32[2];
183         }ctr;
184         blake_large_expand(v,ctx);
185         ctx->counter++;
186         ctr.v64 = ctx->counter*1024;
187         v[12] ^= ctr.v64;
188         v[13] ^= ctr.v64;
189         blake_large_changeendian(m, msg);
190         blake_large_compress(v, m);
191         blake_large_collapse(ctx, v);
192 }
193
194 void blake_large_lastBlock(blake_large_ctx_t* ctx, const void* msg, uint16_t length_b){
195         while(length_b>=BLAKE_LARGE_BLOCKSIZE){
196                 blake_large_nextBlock(ctx, msg);
197                 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
198                 length_b -= BLAKE_LARGE_BLOCKSIZE;
199         }
200         uint8_t buffer[128];
201         uint64_t v[16];
202         uint64_t ctr;
203         ctr = ctx->counter*1024+length_b;
204         memset(buffer, 0, 128);
205         memcpy(buffer, msg, (length_b+7)/8);
206         buffer[length_b/8] |= 0x80 >> (length_b%8);
207         blake_large_changeendian(buffer, buffer);
208         blake_large_expand(v, ctx);
209 #if BUG_3
210         uint8_t x=0;
211         if(length_b%1024<895 && length_b%8)
212                 x=0x40;
213         v[12] ^= ctr + x;
214         v[13] ^= ctr + x;
215         
216 #else   
217         v[12] ^= ctr;
218         v[13] ^= ctr;
219 #endif
220         if(length_b>1024-128-2){
221 #if BUG_4
222                 if(length_b<1017){
223                         blake_large_compress(v, buffer);
224                         blake_large_collapse(ctx, v);
225                 }       
226 #else   
227                 blake_large_compress(v, buffer);
228                 blake_large_collapse(ctx, v);
229 #endif
230                 memset(buffer, 0, 128-8);
231                 blake_large_expand(v, ctx);
232         }
233         if(ctx->appendone)
234                 buffer[128-16-8] |= 0x01;       
235         *((uint64_t*)(&(buffer[128-8]))) = ctr;
236         blake_large_compress(v, buffer);
237         blake_large_collapse(ctx, v);
238         
239 }
240
241 uint64_t blake64_iv[] PROGMEM = {
242     0x6A09E667F3BCC908LL, 0xBB67AE8584CAA73BLL,
243     0x3C6EF372FE94F82BLL, 0xA54FF53A5F1D36F1LL,
244     0x510E527FADE682D1LL, 0x9B05688C2B3E6C1FLL,
245     0x1F83D9ABFB41BD6BLL, 0x5BE0CD19137E2179LL
246 };
247
248 void blake64_init(blake64_ctx_t* ctx){
249         uint8_t i;
250         for(i=0; i<8; ++i){
251                 ctx->h[i] = pgm_read_qword(&(blake64_iv[i]));
252         }
253         memset(ctx->s, 0, 4*8);
254         ctx->counter = 0;
255         ctx->appendone = 1;
256 }
257
258 uint64_t blake48_iv[] PROGMEM = {
259     0xCBBB9D5DC1059ED8LL, 0x629A292A367CD507LL,
260     0x9159015A3070DD17LL, 0x152FECD8F70E5939LL,
261     0x67332667FFC00B31LL, 0x8EB44A8768581511LL,
262     0xDB0C2E0D64F98FA7LL, 0x47B5481DBEFA4FA4LL
263 };
264
265 void blake48_init(blake48_ctx_t* ctx){
266         uint8_t i;
267         for(i=0; i<8; ++i){
268                 ctx->h[i] = pgm_read_qword(&(blake48_iv[i]));
269         }
270         memset(ctx->s, 0, 4*8);
271         ctx->counter = 0;
272         ctx->appendone = 0;
273 }
274
275 void blake64_ctx2hash(void* dest, const blake64_ctx_t* ctx){
276         uint8_t i;
277         for(i=0; i<8; ++i){
278                 ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
279                 ((uint32_t*)dest)[2*i+1] = CHANGE_ENDIAN32((uint32_t)ctx->h[i]);
280         }
281 }
282
283 void blake48_ctx2hash(void* dest, const blake48_ctx_t* ctx){
284         uint8_t i;
285         for(i=0; i<6; ++i){
286                 ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
287                 ((uint32_t*)dest)[2*i+1] = CHANGE_ENDIAN32((uint32_t)ctx->h[i]);
288         }
289 }
290
291 void blake64_nextBlock(blake64_ctx_t* ctx, const void* block){
292         blake_large_nextBlock(ctx, block);
293 }
294
295 void blake48_nextBlock(blake48_ctx_t* ctx, const void* block){
296         blake_large_nextBlock(ctx, block);
297 }
298
299 void blake64_lastBlock(blake64_ctx_t* ctx, const void* block, uint16_t length_b){
300         blake_large_lastBlock(ctx, block, length_b);
301 }
302
303 void blake48_lastBlock(blake48_ctx_t* ctx, const void* block, uint16_t length_b){
304         blake_large_lastBlock(ctx, block, length_b);
305 }
306
307 void blake64(void* dest, const void* msg, uint32_t length_b){
308         blake_large_ctx_t ctx;
309         blake64_init(&ctx);
310         while(length_b>=BLAKE_LARGE_BLOCKSIZE){
311                 blake_large_nextBlock(&ctx, msg);
312                 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
313                 length_b -= BLAKE_LARGE_BLOCKSIZE;
314         }
315         blake_large_lastBlock(&ctx, msg, length_b);
316         blake64_ctx2hash(dest, &ctx);
317 }
318
319 void blake48(void* dest, const void* msg, uint32_t length_b){
320         blake_large_ctx_t ctx;
321         blake48_init(&ctx);
322         while(length_b>=BLAKE_LARGE_BLOCKSIZE){
323                 blake_large_nextBlock(&ctx, msg);
324                 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
325                 length_b -= BLAKE_LARGE_BLOCKSIZE;
326         }
327         blake_large_lastBlock(&ctx, msg, length_b);
328         blake48_ctx2hash(dest, &ctx);
329 }