]> git.cryptolib.org Git - avr-crypto-lib.git/blob - blake/blake_small.c
[blake] fixing bugs and removing bug compatibility mode
[avr-crypto-lib.git] / blake / blake_small.c
1 /* blake_small.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_small.c
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-05-04
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_small.h"
33 #include "blake_common.h"
34
35 static
36 uint32_t blake_c[] PROGMEM = {
37    0x243F6A88, 0x85A308D3,
38    0x13198A2E, 0x03707344,
39    0xA4093822, 0x299F31D0,
40    0x082EFA98, 0xEC4E6C89,
41    0x452821E6, 0x38D01377,
42    0xBE5466CF, 0x34E90C6C,
43    0xC0AC29B7, 0xC97C50DD,
44    0x3F84D5B5, 0xB5470917
45 };
46
47 #define ROTL32(a, n) (((a)<<(n))|((a)>>(32-(n))))
48 #define ROTR32(a, n) (((a)>>(n))|((a)<<(32-(n))))
49 #define CHANGE_ENDIAN32(a) (((a)<<24)| \
50                             ((0x0000ff00&(a))<<8)| \
51                                                     ((0x00ff0000&(a))>>8)| \
52                                                     (a)>>24 )
53 static
54 void blake_small_expand(uint32_t* v, const blake_small_ctx_t* ctx){
55         uint8_t i;
56         memcpy(v, ctx->h, 8*4);
57         for(i=0; i<8; ++i){
58                 v[8+i] = pgm_read_dword(&(blake_c[i]));
59         }
60         memxor((uint8_t*)v+8, ctx->s, 4*4);
61
62 }
63
64 static
65 void blake_small_changeendian(void* dest, const void* src){
66         uint8_t i;
67         for(i=0; i<16; ++i){
68                 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(((uint32_t*)src)[i]);
69         }
70 }
71
72 static
73 void blake_small_compress(uint32_t* v,const void* m){
74         uint8_t r,i;
75         uint8_t a,b,c,d, s0, s1;
76         uint32_t lv[4];
77         for(r=0; r<10; ++r){
78                 for(i=0; i<8; ++i){
79                         a = pgm_read_byte(blake_index_lut+4*i+0);
80                         b = pgm_read_byte(blake_index_lut+4*i+1);
81                         c = pgm_read_byte(blake_index_lut+4*i+2);
82                         d = pgm_read_byte(blake_index_lut+4*i+3);
83                         s0 = pgm_read_byte(blake_sigma+16*r+2*i+0);
84                         s1 = pgm_read_byte(blake_sigma+16*r+2*i+1);
85
86                         lv[0] = v[a];
87                         lv[1] = v[b];
88                         lv[2] = v[c];
89                         lv[3] = v[d];
90
91                         lv[0] += lv[1] + (((uint32_t*)m)[s0] ^ pgm_read_dword(&(blake_c[s1])));
92                         lv[3]  = ROTR32(lv[3]^lv[0], 16);
93                         lv[2] += lv[3];
94                         lv[1]  = ROTR32(lv[1]^lv[2], 12);
95                         lv[0] += lv[1] + (((uint32_t*)m)[s1] ^ pgm_read_dword(&(blake_c[s0])));
96                         lv[3]  = ROTR32(lv[3]^lv[0], 8);
97                         lv[2] += lv[3];
98                         lv[1]  = ROTR32(lv[1]^lv[2], 7);
99
100                         v[a] = lv[0];
101                         v[b] = lv[1];
102                         v[c] = lv[2];
103                         v[d] = lv[3];
104                 }
105         }
106 }
107
108 static
109 void blake_small_collapse(blake_small_ctx_t* ctx, uint32_t* v){
110         uint8_t i;
111         for(i=0; i<8; ++i){
112                 ctx->h[i] ^= ctx->s[i%4] ^ v[i] ^ v[8+i];
113         }
114 }
115
116 void blake_small_nextBlock(blake_small_ctx_t* ctx, const void* msg){
117         uint32_t v[16];
118         uint32_t m[16];
119         union {
120                 uint64_t v64;
121                 uint32_t v32[2];
122         }ctr;
123         blake_small_expand(v,ctx);
124         ctx->counter++;
125         ctr.v64 = ctx->counter*512;
126         v[12] ^= ctr.v32[0];
127         v[13] ^= ctr.v32[0];
128         v[14] ^= ctr.v32[1];
129         v[15] ^= ctr.v32[1];
130         blake_small_changeendian(m, msg);
131         blake_small_compress(v, m);
132         blake_small_collapse(ctx, v);
133 }
134
135 void blake_small_lastBlock(blake_small_ctx_t* ctx, const void* msg, uint16_t length_b){
136         while(length_b>=BLAKE_SMALL_BLOCKSIZE){
137                 blake_small_nextBlock(ctx, msg);
138                 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
139                 length_b -= BLAKE_SMALL_BLOCKSIZE;
140         }
141         uint8_t buffer[64];
142         uint32_t v[16];
143         union {
144                 uint64_t v64;
145                 uint32_t v32[2];
146         }ctr;
147         ctr.v64 = ctx->counter*512+length_b;
148         memset(buffer, 0, 64);
149         memcpy(buffer, msg, (length_b+7)/8);
150         buffer[length_b/8] |= 0x80 >> (length_b&0x7);
151         blake_small_changeendian(buffer, buffer);
152         blake_small_expand(v, ctx);
153         if(length_b>512-64-2){
154                 v[12] ^= ctr.v32[0];
155                 v[13] ^= ctr.v32[0];
156                 v[14] ^= ctr.v32[1];
157                 v[15] ^= ctr.v32[1];
158                 blake_small_compress(v, buffer);
159                 blake_small_collapse(ctx, v);
160                 memset(buffer, 0, 64-8);
161                 blake_small_expand(v, ctx);
162         }else{
163                 if(length_b){
164                         v[12] ^= ctr.v32[0];
165                         v[13] ^= ctr.v32[0];
166                         v[14] ^= ctr.v32[1];
167                         v[15] ^= ctr.v32[1];
168                 }
169         }
170         if(ctx->appendone)
171                 buffer[64-8-4] |= 0x01;
172         *((uint32_t*)(&(buffer[64-8]))) = ctr.v32[1];
173         *((uint32_t*)(&(buffer[64-4]))) = ctr.v32[0];
174         blake_small_compress(v, buffer);
175         blake_small_collapse(ctx, v);
176
177 }
178
179 uint32_t blake32_iv[] PROGMEM = {
180         0x6A09E667L, 0xBB67AE85,
181         0x3C6EF372L, 0xA54FF53A,
182         0x510E527FL, 0x9B05688C,
183         0x1F83D9ABL, 0x5BE0CD19
184 };
185
186 void blake32_init(blake32_ctx_t* ctx){
187         uint8_t i;
188         for(i=0; i<8; ++i){
189                 ctx->h[i] = pgm_read_dword(&(blake32_iv[i]));
190         }
191         memset(ctx->s, 0, 4*4);
192         ctx->counter = 0;
193         ctx->appendone = 1;
194 }
195
196 uint32_t blake28_iv[] PROGMEM = {
197         0xC1059ED8, 0x367CD507,
198         0x3070DD17, 0xF70E5939,
199         0xFFC00B31, 0x68581511,
200         0x64F98FA7, 0xBEFA4FA4
201 };
202
203 void blake28_init(blake28_ctx_t* ctx){
204         uint8_t i;
205         for(i=0; i<8; ++i){
206                 ctx->h[i] = pgm_read_dword(&(blake28_iv[i]));
207         }
208         memset(ctx->s, 0, 4*4);
209         ctx->counter = 0;
210         ctx->appendone = 0;
211 }
212
213 void blake32_ctx2hash(void* dest, const blake32_ctx_t* ctx){
214         uint8_t i;
215         for(i=0; i<8; ++i){
216                 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(ctx->h[i]);
217         }
218 }
219
220 void blake28_ctx2hash(void* dest, const blake28_ctx_t* ctx){
221         uint8_t i;
222         for(i=0; i<7; ++i){
223                 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(ctx->h[i]);
224         }
225 }
226
227 void blake32_nextBlock(blake32_ctx_t* ctx, const void* block){
228         blake_small_nextBlock(ctx, block);
229 }
230
231 void blake28_nextBlock(blake28_ctx_t* ctx, const void* block){
232         blake_small_nextBlock(ctx, block);
233 }
234
235 void blake32_lastBlock(blake32_ctx_t* ctx, const void* block, uint16_t length_b){
236         blake_small_lastBlock(ctx, block, length_b);
237 }
238
239 void blake28_lastBlock(blake28_ctx_t* ctx, const void* block, uint16_t length_b){
240         blake_small_lastBlock(ctx, block, length_b);
241 }
242
243 void blake32(void* dest, const void* msg, uint32_t length_b){
244         blake_small_ctx_t ctx;
245         blake32_init(&ctx);
246         while(length_b>=BLAKE_SMALL_BLOCKSIZE){
247                 blake_small_nextBlock(&ctx, msg);
248                 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
249                 length_b -= BLAKE_SMALL_BLOCKSIZE;
250         }
251         blake_small_lastBlock(&ctx, msg, length_b);
252         blake32_ctx2hash(dest, &ctx);
253 }
254
255 void blake28(void* dest, const void* msg, uint32_t length_b){
256         blake_small_ctx_t ctx;
257         blake28_init(&ctx);
258         while(length_b>=BLAKE_SMALL_BLOCKSIZE){
259                 blake_small_nextBlock(&ctx, msg);
260                 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
261                 length_b -= BLAKE_SMALL_BLOCKSIZE;
262         }
263         blake_small_lastBlock(&ctx, msg, length_b);
264         blake28_ctx2hash(dest, &ctx);
265 }