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
30 #include "memxor/memxor.h"
31 #include "blake_large.h"
32 #include "blake_common.h"
35 uint64_t blake_c[] = {
36 0x243F6A8885A308D3LL, 0x13198A2E03707344LL,
37 0xA4093822299F31D0LL, 0x082EFA98EC4E6C89LL,
38 0x452821E638D01377LL, 0xBE5466CF34E90C6CLL,
39 0xC0AC29B7C97C50DDLL, 0x3F84D5B5B5470917LL,
40 0x9216D5D98979FB1BLL, 0xD1310BA698DFB5ACLL,
41 0x2FFD72DBD01ADFB7LL, 0xB8E1AFED6A267E96LL,
42 0xBA7C9045F12C7F99LL, 0x24A19947B3916CF7LL,
43 0x0801F2E2858EFC16LL, 0x636920D871574E69LL
48 #define ROTL64(a, n) (((a)<<(n))|((a)>>(64-(n))))
49 #define ROTR64(a, n) (((a)>>(n))|((a)<<(64-(n))))
50 #define CHANGE_ENDIAN32(a) (((a)<<24)| \
51 ((0x0000ff00&(a))<<8)| \
52 ((0x00ff0000&(a))>>8)| \
56 void blake_large_expand(uint64_t* v, const blake_large_ctx_t* ctx){
58 memcpy(v, ctx->h, 8*8);
62 memxor((uint8_t*)v+8, ctx->s, 4*8);
67 void blake_large_changeendian(void* dest, const void* src){
71 tmp = CHANGE_ENDIAN32(((uint32_t*)src)[i]);
72 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(((uint32_t*)src)[i+1]);
73 ((uint32_t*)dest)[i+1] = tmp;
77 #define A (v[idx.v8[0]])
78 #define B (v[idx.v8[1]])
79 #define C (v[idx.v8[2]])
80 #define D (v[idx.v8[3]])
83 void blake_large_compress(uint64_t* v,const void* m){
92 idx.v32 = ((uint32_t*)blake_index_lut)[i];
93 s0 = blake_sigma[16*r+2*i+0];
94 s1 = blake_sigma[16*r+2*i+1];
95 A += B + (((uint64_t*)m)[s0] ^ blake_c[s1]);
99 A += B + (((uint64_t*)m)[s1] ^ blake_c[s0]);
108 void blake_large_collapse(blake_large_ctx_t* ctx, uint64_t* v){
111 ctx->h[i] ^= ctx->s[i%4] ^ v[i] ^ v[8+i];
115 void blake_large_nextBlock(blake_large_ctx_t* ctx, const void* msg){
122 blake_large_expand(v,ctx);
124 ctr.v64 = ctx->counter*1024;
127 blake_large_changeendian(m, msg);
128 blake_large_compress(v, m);
129 blake_large_collapse(ctx, v);
132 void blake_large_lastBlock(blake_large_ctx_t* ctx, const void* msg, uint16_t length_b){
133 while(length_b>=BLAKE_LARGE_BLOCKSIZE){
134 blake_large_nextBlock(ctx, msg);
135 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
136 length_b -= BLAKE_LARGE_BLOCKSIZE;
141 ctr = ctx->counter*1024+length_b;
142 memset(buffer, 0, 128);
143 memcpy(buffer, msg, (length_b+7)/8);
144 buffer[length_b/8] |= 0x80 >> (length_b&0x7);
145 blake_large_changeendian(buffer, buffer);
146 blake_large_expand(v, ctx);
147 if(length_b>1024-128-2){
150 blake_large_compress(v, buffer);
151 blake_large_collapse(ctx, v);
152 memset(buffer, 0, 128-8);
153 blake_large_expand(v, ctx);
161 buffer[128-16-8] |= 0x01;
162 *((uint64_t*)(&(buffer[128-8]))) = ctr;
163 blake_large_compress(v, buffer);
164 blake_large_collapse(ctx, v);
169 uint64_t blake64_iv[] = {
170 0x6A09E667F3BCC908LL, 0xBB67AE8584CAA73BLL,
171 0x3C6EF372FE94F82BLL, 0xA54FF53A5F1D36F1LL,
172 0x510E527FADE682D1LL, 0x9B05688C2B3E6C1FLL,
173 0x1F83D9ABFB41BD6BLL, 0x5BE0CD19137E2179LL
176 void blake64_init(blake64_ctx_t* ctx){
179 ctx->h[i] = blake64_iv[i];
181 memset(ctx->s, 0, 4*8);
187 uint64_t blake48_iv[] = {
188 0xCBBB9D5DC1059ED8LL, 0x629A292A367CD507LL,
189 0x9159015A3070DD17LL, 0x152FECD8F70E5939LL,
190 0x67332667FFC00B31LL, 0x8EB44A8768581511LL,
191 0xDB0C2E0D64F98FA7LL, 0x47B5481DBEFA4FA4LL
194 void blake48_init(blake48_ctx_t* ctx){
197 ctx->h[i] = blake48_iv[i];
199 memset(ctx->s, 0, 4*8);
204 void blake64_ctx2hash(void* dest, const blake64_ctx_t* ctx){
207 ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
208 ((uint32_t*)dest)[2*i+1] = CHANGE_ENDIAN32((uint32_t)ctx->h[i]);
212 void blake48_ctx2hash(void* dest, const blake48_ctx_t* ctx){
215 ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
216 ((uint32_t*)dest)[2*i+1] = CHANGE_ENDIAN32((uint32_t)ctx->h[i]);
220 void blake64_nextBlock(blake64_ctx_t* ctx, const void* block){
221 blake_large_nextBlock(ctx, block);
224 void blake48_nextBlock(blake48_ctx_t* ctx, const void* block){
225 blake_large_nextBlock(ctx, block);
228 void blake64_lastBlock(blake64_ctx_t* ctx, const void* block, uint16_t length_b){
229 blake_large_lastBlock(ctx, block, length_b);
232 void blake48_lastBlock(blake48_ctx_t* ctx, const void* block, uint16_t length_b){
233 blake_large_lastBlock(ctx, block, length_b);
236 void blake64(void* dest, const void* msg, uint32_t length_b){
237 blake_large_ctx_t ctx;
239 while(length_b>=BLAKE_LARGE_BLOCKSIZE){
240 blake_large_nextBlock(&ctx, msg);
241 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
242 length_b -= BLAKE_LARGE_BLOCKSIZE;
244 blake_large_lastBlock(&ctx, msg, length_b);
245 blake64_ctx2hash(dest, &ctx);
248 void blake48(void* dest, const void* msg, uint32_t length_b){
249 blake_large_ctx_t ctx;
251 while(length_b>=BLAKE_LARGE_BLOCKSIZE){
252 blake_large_nextBlock(&ctx, msg);
253 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
254 length_b -= BLAKE_LARGE_BLOCKSIZE;
256 blake_large_lastBlock(&ctx, msg, length_b);
257 blake48_ctx2hash(dest, &ctx);