]> git.cryptolib.org Git - avr-crypto-lib.git/blob - blake/blake_large.c
first impression of Rabbit
[avr-crypto-lib.git] / blake / blake_large.c
1 /* blake_large.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_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 <avr/pgmspace.h>
31 #include "memxor.h"
32 #include "blake_large.h"
33 #include "blake_common.h"
34
35 static
36 uint64_t pgm_read_qword(void* p){
37         union{
38                 uint64_t v64;
39                 uint32_t v32[2];
40         }r;
41         r.v32[0] = pgm_read_dword(p);
42         r.v32[1] = pgm_read_dword((uint8_t*)p+4);
43         return r.v64;
44 }
45
46 static
47 uint64_t blake_c[] PROGMEM = {
48    0x243F6A8885A308D3LL, 0x13198A2E03707344LL,
49    0xA4093822299F31D0LL, 0x082EFA98EC4E6C89LL,
50    0x452821E638D01377LL, 0xBE5466CF34E90C6CLL,
51    0xC0AC29B7C97C50DDLL, 0x3F84D5B5B5470917LL,
52    0x9216D5D98979FB1BLL, 0xD1310BA698DFB5ACLL,
53    0x2FFD72DBD01ADFB7LL, 0xB8E1AFED6A267E96LL,
54    0xBA7C9045F12C7F99LL, 0x24A19947B3916CF7LL,
55    0x0801F2E2858EFC16LL, 0x636920D871574E69LL
56 };
57
58
59
60 #define ROTL64(a, n) (((a)<<(n))|((a)>>(64-(n))))
61 #define ROTR64(a, n) (((a)>>(n))|((a)<<(64-(n))))
62 #define CHANGE_ENDIAN32(a) (((a)<<24)| \
63                             ((0x0000ff00&(a))<<8)| \
64                                                     ((0x00ff0000&(a))>>8)| \
65                                                     (a)>>24 )
66
67 static
68 void blake_large_expand(uint64_t* v, const blake_large_ctx_t* ctx){
69         uint8_t i;
70         memcpy(v, ctx->h, 8*8);
71         for(i=0; i<8; ++i){
72                 v[8+i] = pgm_read_qword(&(blake_c[i]));
73         }
74         memxor((uint8_t*)v+8, ctx->s, 4*8);
75
76 }
77
78 static
79 void blake_large_changeendian(void* dest, const void* src){
80         uint8_t i;
81         uint32_t tmp;
82         for(i=0; i<32; i+=2){
83                 tmp = CHANGE_ENDIAN32(((uint32_t*)src)[i]);
84                 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(((uint32_t*)src)[i+1]);
85                 ((uint32_t*)dest)[i+1] = tmp;
86         }
87 }
88
89 static
90 void blake_large_compress(uint64_t* v,const void* m){
91         uint8_t r,i;
92         uint8_t a,b,c,d, s0, s1, sigma_idx=0;
93         for(r=0; r<16; ++r){
94                 for(i=0; i<8; ++i){
95                         a = pgm_read_byte(blake_index_lut+4*i+0);
96                         b = pgm_read_byte(blake_index_lut+4*i+1);
97                         c = pgm_read_byte(blake_index_lut+4*i+2);
98                         d = pgm_read_byte(blake_index_lut+4*i+3);
99                         s0 = pgm_read_byte(blake_sigma+sigma_idx);
100                         s1 = s0&0x0f;
101                         s0 >>= 4;
102                         ++sigma_idx;
103                         if(sigma_idx>=80){
104                                 sigma_idx-=80;
105                         }
106                         v[a] += v[b] + (((uint64_t*)m)[s0] ^ pgm_read_qword(&(blake_c[s1])));
107                         v[d]  = ROTR64(v[d]^v[a], 32);
108                         v[c] += v[d];
109                         v[b]  = ROTR64(v[b]^v[c], 25);
110                         v[a] += v[b] + (((uint64_t*)m)[s1] ^ pgm_read_qword(&(blake_c[s0])));
111                         v[d]  = ROTR64(v[d]^v[a], 16);
112                         v[c] += v[d];
113                         v[b]  = ROTR64(v[b]^v[c], 11);
114                 }
115         }
116 }
117
118 static
119 void blake_large_collapse(blake_large_ctx_t* ctx, uint64_t* v){
120         uint8_t i;
121         for(i=0; i<8; ++i){
122                 ctx->h[i] ^= ctx->s[i%4] ^ v[i] ^ v[8+i];
123         }
124 }
125
126 void blake_large_nextBlock(blake_large_ctx_t* ctx, const void* msg){
127         uint64_t v[16];
128         uint64_t m[16];
129         union {
130                 uint64_t v64;
131                 uint32_t v32[2];
132         }ctr;
133         blake_large_expand(v,ctx);
134         ctx->counter++;
135         ctr.v64 = ctx->counter*1024;
136         v[12] ^= ctr.v64;
137         v[13] ^= ctr.v64;
138         blake_large_changeendian(m, msg);
139         blake_large_compress(v, m);
140         blake_large_collapse(ctx, v);
141 }
142
143 void blake_large_lastBlock(blake_large_ctx_t* ctx, const void* msg, uint16_t length_b){
144         while(length_b>=BLAKE_LARGE_BLOCKSIZE){
145                 blake_large_nextBlock(ctx, msg);
146                 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
147                 length_b -= BLAKE_LARGE_BLOCKSIZE;
148         }
149         uint8_t buffer[128];
150         uint64_t v[16];
151         uint64_t ctr;
152         ctr = ctx->counter*1024+length_b;
153         memset(buffer, 0, 128);
154         memcpy(buffer, msg, (length_b+7)/8);
155         buffer[length_b/8] |= 0x80 >> (length_b&0x7);
156         blake_large_changeendian(buffer, buffer);
157         blake_large_expand(v, ctx);
158         if(length_b>1024-128-2){
159                 v[12] ^= ctr;
160                 v[13] ^= ctr;
161                 blake_large_compress(v, buffer);
162                 blake_large_collapse(ctx, v);
163                 memset(buffer, 0, 128-8);
164                 blake_large_expand(v, ctx);
165         } else {
166                 if(length_b){
167                         v[12] ^= ctr;
168                         v[13] ^= ctr;
169                 }
170         }
171         if(ctx->appendone)
172                 buffer[128-16-8] |= 0x01;
173         *((uint64_t*)(&(buffer[128-8]))) = ctr;
174         blake_large_compress(v, buffer);
175         blake_large_collapse(ctx, v);
176
177 }
178
179 uint64_t blake512_iv[] PROGMEM = {
180     0x6A09E667F3BCC908LL, 0xBB67AE8584CAA73BLL,
181     0x3C6EF372FE94F82BLL, 0xA54FF53A5F1D36F1LL,
182     0x510E527FADE682D1LL, 0x9B05688C2B3E6C1FLL,
183     0x1F83D9ABFB41BD6BLL, 0x5BE0CD19137E2179LL
184 };
185
186 void blake512_init(blake512_ctx_t* ctx){
187         uint8_t i;
188         for(i=0; i<8; ++i){
189                 ctx->h[i] = pgm_read_qword(&(blake512_iv[i]));
190         }
191         memset(ctx->s, 0, 4*8);
192         ctx->counter = 0;
193         ctx->appendone = 1;
194 }
195
196 uint64_t blake384_iv[] PROGMEM = {
197     0xCBBB9D5DC1059ED8LL, 0x629A292A367CD507LL,
198     0x9159015A3070DD17LL, 0x152FECD8F70E5939LL,
199     0x67332667FFC00B31LL, 0x8EB44A8768581511LL,
200     0xDB0C2E0D64F98FA7LL, 0x47B5481DBEFA4FA4LL
201 };
202
203 void blake384_init(blake384_ctx_t* ctx){
204         uint8_t i;
205         for(i=0; i<8; ++i){
206                 ctx->h[i] = pgm_read_qword(&(blake384_iv[i]));
207         }
208         memset(ctx->s, 0, 4*8);
209         ctx->counter = 0;
210         ctx->appendone = 0;
211 }
212
213 void blake512_ctx2hash(void* dest, const blake512_ctx_t* ctx){
214         uint8_t i;
215         for(i=0; i<8; ++i){
216                 ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
217                 ((uint32_t*)dest)[2*i+1] = CHANGE_ENDIAN32((uint32_t)ctx->h[i]);
218         }
219 }
220
221 void blake384_ctx2hash(void* dest, const blake384_ctx_t* ctx){
222         uint8_t i;
223         for(i=0; i<6; ++i){
224                 ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
225                 ((uint32_t*)dest)[2*i+1] = CHANGE_ENDIAN32((uint32_t)ctx->h[i]);
226         }
227 }
228
229 void blake512_nextBlock(blake512_ctx_t* ctx, const void* block){
230         blake_large_nextBlock(ctx, block);
231 }
232
233 void blake384_nextBlock(blake384_ctx_t* ctx, const void* block){
234         blake_large_nextBlock(ctx, block);
235 }
236
237 void blake512_lastBlock(blake512_ctx_t* ctx, const void* block, uint16_t length_b){
238         blake_large_lastBlock(ctx, block, length_b);
239 }
240
241 void blake384_lastBlock(blake384_ctx_t* ctx, const void* block, uint16_t length_b){
242         blake_large_lastBlock(ctx, block, length_b);
243 }
244
245 void blake512(void* dest, const void* msg, uint32_t length_b){
246         blake_large_ctx_t ctx;
247         blake512_init(&ctx);
248         while(length_b>=BLAKE_LARGE_BLOCKSIZE){
249                 blake_large_nextBlock(&ctx, msg);
250                 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
251                 length_b -= BLAKE_LARGE_BLOCKSIZE;
252         }
253         blake_large_lastBlock(&ctx, msg, length_b);
254         blake512_ctx2hash(dest, &ctx);
255 }
256
257 void blake384(void* dest, const void* msg, uint32_t length_b){
258         blake_large_ctx_t ctx;
259         blake384_init(&ctx);
260         while(length_b>=BLAKE_LARGE_BLOCKSIZE){
261                 blake_large_nextBlock(&ctx, msg);
262                 msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
263                 length_b -= BLAKE_LARGE_BLOCKSIZE;
264         }
265         blake_large_lastBlock(&ctx, msg, length_b);
266         blake384_ctx2hash(dest, &ctx);
267 }