]> git.cryptolib.org Git - arm-crypto-lib.git/blob - blake/blake_large.c
more precise type for arguments of bcal_nessie_multiple()
[arm-crypto-lib.git] / blake / blake_large.c
1 /* blake_large.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  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_large.c
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-05-08
24  * \license GPLv3 or later
25  *
26  */
27
28 #include <stdint.h>
29 #include <string.h>
30 #include "memxor.h"
31 #include "blake_large.h"
32 #include "blake_common.h"
33
34 static const
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
44 };
45
46
47
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)| \
53                                                     (a)>>24 )
54
55 static
56 void blake_large_expand(uint64_t* v, const blake_large_ctx_t* ctx){
57         uint8_t i;
58         memcpy(v, ctx->h, 8*8);
59         for(i=0; i<8; ++i){
60                 v[8+i] = blake_c[i];
61         }
62         memxor((uint8_t*)v+8, ctx->s, 4*8);
63
64 }
65
66 static
67 void blake_large_changeendian(void* dest, const void* src){
68         uint8_t i;
69         uint32_t tmp;
70         for(i=0; i<32; i+=2){
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;
74         }
75 }
76
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]])
81
82 static
83 void blake_large_compress(uint64_t* v,const void* m){
84         uint8_t r,i;
85         uint16_t s, *p=(uint16_t*)blake_sigma;
86         union {
87                 uint32_t v32;
88                 uint8_t v8[4];
89         }idx;
90         for(r=0; r<16; ++r){
91                 for(i=0; i<8; ++i){
92                         idx.v32 = ((uint32_t*)blake_index_lut)[i];
93                         s = *p++;
94                         if(p==(uint16_t*)(blake_sigma + 160)){
95                                 p=(uint16_t*)blake_sigma;
96                         }
97                         A += B + (((uint64_t*)m)[s&0xff] ^ blake_c[s>>8]);
98                         D  = ROTR64(D^A, 32);
99                         C += D;
100                         B  = ROTR64(B^C, 25);
101                         A += B + (((uint64_t*)m)[s>>8] ^ blake_c[s&0xff]);
102                         D  = ROTR64(D^A, 16);
103                         C += D;
104                         B  = ROTR64(B^C, 11);
105                 }
106         }
107 }
108
109 static
110 void blake_large_collapse(blake_large_ctx_t* ctx, uint64_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_large_nextBlock(blake_large_ctx_t* ctx, const void* msg){
118         uint64_t v[16];
119         uint64_t m[16];
120         union {
121                 uint64_t v64;
122                 uint32_t v32[2];
123         }ctr;
124         blake_large_expand(v,ctx);
125         ctx->counter++;
126         ctr.v64 = ctx->counter*1024;
127         v[12] ^= ctr.v64;
128         v[13] ^= ctr.v64;
129         blake_large_changeendian(m, msg);
130         blake_large_compress(v, m);
131         blake_large_collapse(ctx, v);
132 }
133
134 void blake_large_lastBlock(blake_large_ctx_t* ctx, const void* msg, uint16_t length_b){
135         while(length_b>=BLAKE_LARGE_BLOCKSIZE){
136                 blake_large_nextBlock(ctx, msg);
137                 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
138                 length_b -= BLAKE_LARGE_BLOCKSIZE;
139         }
140         union {
141                 uint8_t   v8[128];
142                 uint64_t v64[ 16];
143         } buffer;
144         uint64_t v[16];
145         uint64_t ctr;
146         ctr = ctx->counter*1024+length_b;
147         memset(buffer.v8, 0, 128);
148         memcpy(buffer.v8, msg, (length_b+7)/8);
149         buffer.v8[length_b/8] |= 0x80 >> (length_b&0x7);
150         blake_large_changeendian(buffer.v8, buffer.v8);
151         blake_large_expand(v, ctx);
152         if(length_b>1024-128-2){
153                 v[12] ^= ctr;
154                 v[13] ^= ctr;
155                 blake_large_compress(v, buffer.v8);
156                 blake_large_collapse(ctx, v);
157                 memset(buffer.v8, 0, 128-8);
158                 blake_large_expand(v, ctx);
159         } else {
160                 if(length_b){
161                         v[12] ^= ctr;
162                         v[13] ^= ctr;
163                 }
164         }
165         if(ctx->appendone)
166                 buffer.v8[128-16-8] |= 0x01;
167         buffer.v64[15] = ctr;
168         blake_large_compress(v, buffer.v8);
169         blake_large_collapse(ctx, v);
170
171 }
172
173 static const
174 uint64_t blake512_iv[] = {
175     0x6A09E667F3BCC908LL, 0xBB67AE8584CAA73BLL,
176     0x3C6EF372FE94F82BLL, 0xA54FF53A5F1D36F1LL,
177     0x510E527FADE682D1LL, 0x9B05688C2B3E6C1FLL,
178     0x1F83D9ABFB41BD6BLL, 0x5BE0CD19137E2179LL
179 };
180
181 void blake512_init(blake512_ctx_t* ctx){
182         uint8_t i;
183         for(i=0; i<8; ++i){
184                 ctx->h[i] = blake512_iv[i];
185         }
186         memset(ctx->s, 0, 4*8);
187         ctx->counter = 0;
188         ctx->appendone = 1;
189 }
190
191 static const
192 uint64_t blake384_iv[] = {
193     0xCBBB9D5DC1059ED8LL, 0x629A292A367CD507LL,
194     0x9159015A3070DD17LL, 0x152FECD8F70E5939LL,
195     0x67332667FFC00B31LL, 0x8EB44A8768581511LL,
196     0xDB0C2E0D64F98FA7LL, 0x47B5481DBEFA4FA4LL
197 };
198
199 void blake384_init(blake384_ctx_t* ctx){
200         uint8_t i;
201         for(i=0; i<8; ++i){
202                 ctx->h[i] = blake384_iv[i];
203         }
204         memset(ctx->s, 0, 4*8);
205         ctx->counter = 0;
206         ctx->appendone = 0;
207 }
208
209 void blake512_ctx2hash(void* dest, const blake512_ctx_t* ctx){
210         uint8_t i;
211         for(i=0; i<8; ++i){
212                 ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
213                 ((uint32_t*)dest)[2*i+1] = CHANGE_ENDIAN32((uint32_t)ctx->h[i]);
214         }
215 }
216
217 void blake384_ctx2hash(void* dest, const blake384_ctx_t* ctx){
218         uint8_t i;
219         for(i=0; i<6; ++i){
220                 ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
221                 ((uint32_t*)dest)[2*i+1] = CHANGE_ENDIAN32((uint32_t)ctx->h[i]);
222         }
223 }
224
225 void blake512_nextBlock(blake512_ctx_t* ctx, const void* block){
226         blake_large_nextBlock(ctx, block);
227 }
228
229 void blake384_nextBlock(blake384_ctx_t* ctx, const void* block){
230         blake_large_nextBlock(ctx, block);
231 }
232
233 void blake512_lastBlock(blake512_ctx_t* ctx, const void* block, uint16_t length_b){
234         blake_large_lastBlock(ctx, block, length_b);
235 }
236
237 void blake384_lastBlock(blake384_ctx_t* ctx, const void* block, uint16_t length_b){
238         blake_large_lastBlock(ctx, block, length_b);
239 }
240
241 void blake512(void* dest, const void* msg, uint32_t length_b){
242         blake_large_ctx_t ctx;
243         blake512_init(&ctx);
244         while(length_b>=BLAKE_LARGE_BLOCKSIZE){
245                 blake_large_nextBlock(&ctx, msg);
246                 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
247                 length_b -= BLAKE_LARGE_BLOCKSIZE;
248         }
249         blake_large_lastBlock(&ctx, msg, length_b);
250         blake512_ctx2hash(dest, &ctx);
251 }
252
253 void blake384(void* dest, const void* msg, uint32_t length_b){
254         blake_large_ctx_t ctx;
255         blake384_init(&ctx);
256         while(length_b>=BLAKE_LARGE_BLOCKSIZE){
257                 blake_large_nextBlock(&ctx, msg);
258                 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
259                 length_b -= BLAKE_LARGE_BLOCKSIZE;
260         }
261         blake_large_lastBlock(&ctx, msg, length_b);
262         blake384_ctx2hash(dest, &ctx);
263 }