3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2010 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
31 #include "blake_small.h"
32 #include "blake_common.h"
35 uint32_t blake_c[] = {
36 0x243F6A88, 0x85A308D3,
37 0x13198A2E, 0x03707344,
38 0xA4093822, 0x299F31D0,
39 0x082EFA98, 0xEC4E6C89,
40 0x452821E6, 0x38D01377,
41 0xBE5466CF, 0x34E90C6C,
42 0xC0AC29B7, 0xC97C50DD,
43 0x3F84D5B5, 0xB5470917
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)| \
53 void blake_small_expand(uint32_t* v, const blake_small_ctx_t* ctx){
55 memcpy(v, ctx->h, 8*4);
59 memxor((uint8_t*)v+8, ctx->s, 4*4);
64 void blake_small_changeendian(void* dest, const void* src){
67 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(((uint32_t*)src)[i]);
71 #define A (v[idx.v8[0]])
72 #define B (v[idx.v8[1]])
73 #define C (v[idx.v8[2]])
74 #define D (v[idx.v8[3]])
77 void blake_small_compress(uint32_t* v,const void* m){
79 uint16_t s, *p=(uint16_t*)blake_sigma;
86 idx.v32 = ((uint32_t*)blake_index_lut)[i];
88 if(p==(uint16_t*)(blake_sigma+160)){
89 p=(uint16_t*)blake_sigma;
91 A += B + (((uint32_t*)m)[s&0xff] ^ blake_c[s>>8]);
95 A += B + (((uint32_t*)m)[s>>8] ^ blake_c[s&0xff]);
104 void blake_small_collapse(blake_small_ctx_t* ctx, uint32_t* v){
107 ctx->h[i] ^= ctx->s[i%4] ^ v[i] ^ v[8+i];
111 void blake_small_nextBlock(blake_small_ctx_t* ctx, const void* msg){
118 blake_small_expand(v,ctx);
120 ctr.v64 = ctx->counter*512;
125 blake_small_changeendian(m, msg);
126 blake_small_compress(v, m);
127 blake_small_collapse(ctx, v);
130 void blake_small_lastBlock(blake_small_ctx_t* ctx, const void* msg, uint16_t length_b){
131 while(length_b>=BLAKE_SMALL_BLOCKSIZE){
132 blake_small_nextBlock(ctx, msg);
133 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
134 length_b -= BLAKE_SMALL_BLOCKSIZE;
142 ctr.v64 = ctx->counter*512+length_b;
143 memset(buffer, 0, 64);
144 memcpy(buffer, msg, (length_b+7)/8);
145 buffer[length_b/8] |= 0x80 >> (length_b&0x7);
146 blake_small_changeendian(buffer, buffer);
147 blake_small_expand(v, ctx);
148 if(length_b>512-64-2){
153 blake_small_compress(v, buffer);
154 blake_small_collapse(ctx, v);
155 memset(buffer, 0, 64-8);
156 blake_small_expand(v, ctx);
166 buffer[64-8-4] |= 0x01;
167 *((uint32_t*)(&(buffer[64-8]))) = ctr.v32[1];
168 *((uint32_t*)(&(buffer[64-4]))) = ctr.v32[0];
169 blake_small_compress(v, buffer);
170 blake_small_collapse(ctx, v);
175 uint32_t blake256_iv[] = {
176 0x6A09E667L, 0xBB67AE85,
177 0x3C6EF372L, 0xA54FF53A,
178 0x510E527FL, 0x9B05688C,
179 0x1F83D9ABL, 0x5BE0CD19
182 void blake256_init(blake256_ctx_t* ctx){
185 ctx->h[i] = blake256_iv[i];
187 memset(ctx->s, 0, 4*4);
193 uint32_t blake224_iv[] = {
194 0xC1059ED8, 0x367CD507,
195 0x3070DD17, 0xF70E5939,
196 0xFFC00B31, 0x68581511,
197 0x64F98FA7, 0xBEFA4FA4
200 void blake224_init(blake224_ctx_t* ctx){
203 ctx->h[i] = blake224_iv[i];
205 memset(ctx->s, 0, 4*4);
210 void blake256_ctx2hash(void* dest, const blake256_ctx_t* ctx){
213 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(ctx->h[i]);
217 void blake224_ctx2hash(void* dest, const blake224_ctx_t* ctx){
220 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(ctx->h[i]);
224 void blake256_nextBlock(blake256_ctx_t* ctx, const void* block){
225 blake_small_nextBlock(ctx, block);
228 void blake224_nextBlock(blake224_ctx_t* ctx, const void* block){
229 blake_small_nextBlock(ctx, block);
232 void blake256_lastBlock(blake256_ctx_t* ctx, const void* block, uint16_t length_b){
233 blake_small_lastBlock(ctx, block, length_b);
236 void blake224_lastBlock(blake224_ctx_t* ctx, const void* block, uint16_t length_b){
237 blake_small_lastBlock(ctx, block, length_b);
240 void blake256(void* dest, const void* msg, uint32_t length_b){
241 blake_small_ctx_t ctx;
243 while(length_b>=BLAKE_SMALL_BLOCKSIZE){
244 blake_small_nextBlock(&ctx, msg);
245 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
246 length_b -= BLAKE_SMALL_BLOCKSIZE;
248 blake_small_lastBlock(&ctx, msg, length_b);
249 blake256_ctx2hash(dest, &ctx);
252 void blake224(void* dest, const void* msg, uint32_t length_b){
253 blake_small_ctx_t ctx;
255 while(length_b>=BLAKE_SMALL_BLOCKSIZE){
256 blake_small_nextBlock(&ctx, msg);
257 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
258 length_b -= BLAKE_SMALL_BLOCKSIZE;
260 blake_small_lastBlock(&ctx, msg, length_b);
261 blake224_ctx2hash(dest, &ctx);