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