]> git.cryptolib.org Git - avr-crypto-lib.git/blob - blake/blake_small.c
52ac3f6901d7538f4d71660443f73ada813bccfc
[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 const uint32_t blake_c[] PROGMEM = {
36         0x243F6A88, 0x85A308D3,
37         0x13198A2E, 0x03707344,
38         0xA4093822, 0x299F31D0,
39         0x082EFA98, 0xEC4E6C89,
40         0x452821E6, 0x38D01377,
41         0xBE5466CF, 0x34E90C6C,
42         0xC0AC29B7, 0xC97C50DD,
43         0x3F84D5B5, 0xB5470917
44 };
45
46 #define ROTL32(a, n) (((a)<<(n))|((a)>>(32-(n))))
47 #define ROTR32(a, n) (((a)>>(n))|((a)<<(32-(n))))
48 #define CHANGE_ENDIAN32(a) (((a)<<24)| \
49                             ((0x0000ff00&(a))<<8)| \
50                                                     ((0x00ff0000&(a))>>8)| \
51                                                     (a)>>24 )
52 static
53 void blake_small_expand(uint32_t *v, const blake_small_ctx_t *ctx)
54 {
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 {
67     uint8_t i;
68     for (i = 0; i < 16; ++i) {
69         ((uint32_t*) dest)[i] = CHANGE_ENDIAN32(((uint32_t* )src)[i]);
70     }
71 }
72
73 static
74 void blake_small_compress(uint32_t *v, const void *m)
75 {
76     uint8_t r, i;
77     uint8_t a, b, c, d, s0, s1, sigma_idx = 0;
78     uint32_t lv[4];
79     for (r = 0; r < 14; ++r) {
80         for (i = 0; i < 8; ++i) {
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 + sigma_idx);
86             s1 = s0 & 0xf;
87             s0 >>= 4;
88             ++sigma_idx;
89             if (sigma_idx >= 80) {
90                 sigma_idx -= 80;
91             }
92             lv[0] = v[a];
93             lv[1] = v[b];
94             lv[2] = v[c];
95             lv[3] = v[d];
96
97             lv[0] += lv[1]
98                     + (((uint32_t*) m)[s0] ^ pgm_read_dword(&(blake_c[s1])));
99             lv[3] = ROTR32(lv[3] ^ lv[0], 16);
100             lv[2] += lv[3];
101             lv[1] = ROTR32(lv[1] ^ lv[2], 12);
102             lv[0] += lv[1]
103                     + (((uint32_t*) m)[s1] ^ pgm_read_dword(&(blake_c[s0])));
104             lv[3] = ROTR32(lv[3] ^ lv[0], 8);
105             lv[2] += lv[3];
106             lv[1] = ROTR32(lv[1] ^ lv[2], 7);
107
108             v[a] = lv[0];
109             v[b] = lv[1];
110             v[c] = lv[2];
111             v[d] = lv[3];
112         }
113     }
114 }
115
116 static
117 void blake_small_collapse(blake_small_ctx_t *ctx, uint32_t *v)
118 {
119     uint8_t i;
120     for (i = 0; i < 8; ++i) {
121         ctx->h[i] ^= ctx->s[i % 4] ^ v[i] ^ v[8 + i];
122     }
123 }
124
125 void blake_small_nextBlock(blake_small_ctx_t *ctx, const void *msg)
126 {
127     uint32_t v[16];
128     uint32_t m[16];
129     union {
130         uint64_t v64;
131         uint32_t v32[2];
132     } ctr;
133     blake_small_expand(v, ctx);
134     ctx->counter++;
135     ctr.v64 = ctx->counter * 512;
136     v[12] ^= ctr.v32[0];
137     v[13] ^= ctr.v32[0];
138     v[14] ^= ctr.v32[1];
139     v[15] ^= ctr.v32[1];
140     blake_small_changeendian(m, msg);
141     blake_small_compress(v, m);
142     blake_small_collapse(ctx, v);
143 }
144
145 void blake_small_lastBlock(blake_small_ctx_t *ctx, const void *msg,
146         uint16_t length_b)
147 {
148     while (length_b >= BLAKE_SMALL_BLOCKSIZE) {
149         blake_small_nextBlock(ctx, msg);
150         msg = (uint8_t*) msg + BLAKE_SMALL_BLOCKSIZE_B;
151         length_b -= BLAKE_SMALL_BLOCKSIZE;
152     }
153     union {
154         uint8_t v8[64];
155         uint32_t v32[16];
156     } buffer;
157     uint32_t v[16];
158     union {
159         uint64_t v64;
160         uint32_t v32[2];
161     } ctr;
162     ctr.v64 = ctx->counter * 512 + length_b;
163     memset(buffer.v8, 0, 64);
164     memcpy(buffer.v8, msg, (length_b + 7) / 8);
165     buffer.v8[length_b / 8] |= 0x80 >> (length_b & 0x7);
166     blake_small_changeendian(buffer.v8, buffer.v8);
167     blake_small_expand(v, ctx);
168     if (length_b > 512 - 64 - 2) {
169         v[12] ^= ctr.v32[0];
170         v[13] ^= ctr.v32[0];
171         v[14] ^= ctr.v32[1];
172         v[15] ^= ctr.v32[1];
173         blake_small_compress(v, buffer.v8);
174         blake_small_collapse(ctx, v);
175         memset(buffer.v8, 0, 64 - 8);
176         blake_small_expand(v, ctx);
177     } else {
178         if (length_b) {
179             v[12] ^= ctr.v32[0];
180             v[13] ^= ctr.v32[0];
181             v[14] ^= ctr.v32[1];
182             v[15] ^= ctr.v32[1];
183         }
184     }
185     if (ctx->appendone)
186         buffer.v8[64 - 8 - 4] |= 0x01;
187     buffer.v32[14] = ctr.v32[1];
188     buffer.v32[15] = ctr.v32[0];
189     blake_small_compress(v, buffer.v8);
190     blake_small_collapse(ctx, v);
191
192 }
193
194 const uint32_t blake256_iv[] PROGMEM = {
195         0x6A09E667L, 0xBB67AE85,
196         0x3C6EF372L, 0xA54FF53A,
197         0x510E527FL, 0x9B05688C,
198         0x1F83D9ABL, 0x5BE0CD19
199 };
200
201 void blake256_init(blake256_ctx_t *ctx)
202 {
203     uint8_t i;
204     for (i = 0; i < 8; ++i) {
205         ctx->h[i] = pgm_read_dword(&(blake256_iv[i]));
206     }
207     memset(ctx->s, 0, 4 * 4);
208     ctx->counter = 0;
209     ctx->appendone = 1;
210 }
211
212 const uint32_t blake224_iv[] PROGMEM = {
213         0xC1059ED8, 0x367CD507,
214         0x3070DD17, 0xF70E5939,
215         0xFFC00B31, 0x68581511,
216         0x64F98FA7, 0xBEFA4FA4
217 };
218
219 void blake224_init(blake224_ctx_t *ctx)
220 {
221     uint8_t i;
222     for (i = 0; i < 8; ++i) {
223         ctx->h[i] = pgm_read_dword(&(blake224_iv[i]));
224     }
225     memset(ctx->s, 0, 4 * 4);
226     ctx->counter = 0;
227     ctx->appendone = 0;
228 }
229
230 void blake256_ctx2hash(void *dest, const blake256_ctx_t *ctx)
231 {
232     uint8_t i;
233     for (i = 0; i < 8; ++i) {
234         ((uint32_t*) dest)[i] = CHANGE_ENDIAN32(ctx->h[i]);
235     }
236 }
237
238 void blake224_ctx2hash(void *dest, const blake224_ctx_t *ctx)
239 {
240     uint8_t i;
241     for (i = 0; i < 7; ++i) {
242         ((uint32_t*) dest)[i] = CHANGE_ENDIAN32(ctx->h[i]);
243     }
244 }
245
246 void blake256_nextBlock(blake256_ctx_t *ctx, const void *block)
247 {
248     blake_small_nextBlock(ctx, block);
249 }
250
251 void blake224_nextBlock(blake224_ctx_t *ctx, const void *block)
252 {
253     blake_small_nextBlock(ctx, block);
254 }
255
256 void blake256_lastBlock(blake256_ctx_t *ctx, const void *block,
257         uint16_t length_b)
258 {
259     blake_small_lastBlock(ctx, block, length_b);
260 }
261
262 void blake224_lastBlock(blake224_ctx_t *ctx, const void *block,
263         uint16_t length_b)
264 {
265     blake_small_lastBlock(ctx, block, length_b);
266 }
267
268 void blake256(void *dest, const void *msg, uint32_t length_b)
269 {
270     blake_small_ctx_t ctx;
271     blake256_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     blake256_ctx2hash(dest, &ctx);
279 }
280
281 void blake224(void *dest, const void *msg, uint32_t length_b)
282 {
283     blake_small_ctx_t ctx;
284     blake224_init(&ctx);
285     while (length_b >= BLAKE_SMALL_BLOCKSIZE) {
286         blake_small_nextBlock(&ctx, msg);
287         msg = (uint8_t*) msg + BLAKE_SMALL_BLOCKSIZE_B;
288         length_b -= BLAKE_SMALL_BLOCKSIZE;
289     }
290     blake_small_lastBlock(&ctx, msg, length_b);
291     blake224_ctx2hash(dest, &ctx);
292 }