3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
22 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
30 #include <avr/pgmspace.h>
32 #include "blake_small.h"
33 #include "blake_common.h"
36 const 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
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)| \
54 void blake_small_expand(uint32_t* v, const blake_small_ctx_t* ctx){
56 memcpy(v, ctx->h, 8*4);
58 v[8+i] = pgm_read_dword(&(blake_c[i]));
60 memxor((uint8_t*)v+8, ctx->s, 4*4);
65 void blake_small_changeendian(void* dest, const void* src){
68 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(((uint32_t*)src)[i]);
73 void blake_small_compress(uint32_t* v,const void* m){
75 uint8_t a,b,c,d, s0, s1, sigma_idx=0;
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+sigma_idx);
94 lv[0] += lv[1] + (((uint32_t*)m)[s0] ^ pgm_read_dword(&(blake_c[s1])));
95 lv[3] = ROTR32(lv[3]^lv[0], 16);
97 lv[1] = ROTR32(lv[1]^lv[2], 12);
98 lv[0] += lv[1] + (((uint32_t*)m)[s1] ^ pgm_read_dword(&(blake_c[s0])));
99 lv[3] = ROTR32(lv[3]^lv[0], 8);
101 lv[1] = ROTR32(lv[1]^lv[2], 7);
112 void blake_small_collapse(blake_small_ctx_t* ctx, uint32_t* v){
115 ctx->h[i] ^= ctx->s[i%4] ^ v[i] ^ v[8+i];
119 void blake_small_nextBlock(blake_small_ctx_t* ctx, const void* msg){
126 blake_small_expand(v,ctx);
128 ctr.v64 = ctx->counter*512;
133 blake_small_changeendian(m, msg);
134 blake_small_compress(v, m);
135 blake_small_collapse(ctx, v);
138 void blake_small_lastBlock(blake_small_ctx_t* ctx, const void* msg, uint16_t length_b){
139 while(length_b>=BLAKE_SMALL_BLOCKSIZE){
140 blake_small_nextBlock(ctx, msg);
141 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
142 length_b -= BLAKE_SMALL_BLOCKSIZE;
153 ctr.v64 = ctx->counter*512+length_b;
154 memset(buffer.v8, 0, 64);
155 memcpy(buffer.v8, msg, (length_b+7)/8);
156 buffer.v8[length_b/8] |= 0x80 >> (length_b&0x7);
157 blake_small_changeendian(buffer.v8, buffer.v8);
158 blake_small_expand(v, ctx);
159 if(length_b>512-64-2){
164 blake_small_compress(v, buffer.v8);
165 blake_small_collapse(ctx, v);
166 memset(buffer.v8, 0, 64-8);
167 blake_small_expand(v, ctx);
177 buffer.v8[64-8-4] |= 0x01;
178 buffer.v32[14] = ctr.v32[1];
179 buffer.v32[15] = ctr.v32[0];
180 blake_small_compress(v, buffer.v8);
181 blake_small_collapse(ctx, v);
185 const uint32_t blake256_iv[] PROGMEM = {
186 0x6A09E667L, 0xBB67AE85,
187 0x3C6EF372L, 0xA54FF53A,
188 0x510E527FL, 0x9B05688C,
189 0x1F83D9ABL, 0x5BE0CD19
192 void blake256_init(blake256_ctx_t* ctx){
195 ctx->h[i] = pgm_read_dword(&(blake256_iv[i]));
197 memset(ctx->s, 0, 4*4);
202 const uint32_t blake224_iv[] PROGMEM = {
203 0xC1059ED8, 0x367CD507,
204 0x3070DD17, 0xF70E5939,
205 0xFFC00B31, 0x68581511,
206 0x64F98FA7, 0xBEFA4FA4
209 void blake224_init(blake224_ctx_t* ctx){
212 ctx->h[i] = pgm_read_dword(&(blake224_iv[i]));
214 memset(ctx->s, 0, 4*4);
219 void blake256_ctx2hash(void* dest, const blake256_ctx_t* ctx){
222 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(ctx->h[i]);
226 void blake224_ctx2hash(void* dest, const blake224_ctx_t* ctx){
229 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(ctx->h[i]);
233 void blake256_nextBlock(blake256_ctx_t* ctx, const void* block){
234 blake_small_nextBlock(ctx, block);
237 void blake224_nextBlock(blake224_ctx_t* ctx, const void* block){
238 blake_small_nextBlock(ctx, block);
241 void blake256_lastBlock(blake256_ctx_t* ctx, const void* block, uint16_t length_b){
242 blake_small_lastBlock(ctx, block, length_b);
245 void blake224_lastBlock(blake224_ctx_t* ctx, const void* block, uint16_t length_b){
246 blake_small_lastBlock(ctx, block, length_b);
249 void blake256(void* dest, const void* msg, uint32_t length_b){
250 blake_small_ctx_t ctx;
252 while(length_b>=BLAKE_SMALL_BLOCKSIZE){
253 blake_small_nextBlock(&ctx, msg);
254 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
255 length_b -= BLAKE_SMALL_BLOCKSIZE;
257 blake_small_lastBlock(&ctx, msg, length_b);
258 blake256_ctx2hash(dest, &ctx);
261 void blake224(void* dest, const void* msg, uint32_t length_b){
262 blake_small_ctx_t ctx;
264 while(length_b>=BLAKE_SMALL_BLOCKSIZE){
265 blake_small_nextBlock(&ctx, msg);
266 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
267 length_b -= BLAKE_SMALL_BLOCKSIZE;
269 blake_small_lastBlock(&ctx, msg, length_b);
270 blake224_ctx2hash(dest, &ctx);