]> git.cryptolib.org Git - arm-crypto-lib.git/blob - blake/blake_small.c
forgotten file (a lot of them)
[arm-crypto-lib.git] / blake / blake_small.c
1 /* blake_small.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_small.c
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-05-04
24  * \license GPLv3 or later
25  *
26  */
27
28 #include <stdint.h>
29 #include <string.h>
30 #include "memxor.h"
31 #include "blake_small.h"
32 #include "blake_common.h"
33
34 static const
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
44 };
45
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)| \
51                                                     (a)>>24 )
52 static
53 void blake_small_expand(uint32_t* v, const blake_small_ctx_t* ctx){
54         uint8_t i;
55         memcpy(v, ctx->h, 8*4);
56         for(i=0; i<8; ++i){
57                 v[8+i] = blake_c[i];
58         }
59         memxor((uint8_t*)v+8, ctx->s, 4*4);
60
61 }
62
63 static
64 void blake_small_changeendian(void* dest, const void* src){
65         uint8_t i;
66         for(i=0; i<16; ++i){
67                 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(((uint32_t*)src)[i]);
68         }
69 }
70
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]])
75
76 static
77 void blake_small_compress(uint32_t* v,const void* m){
78         uint8_t r,i;
79         uint16_t s, *p=(uint16_t*)blake_sigma;
80         union {
81                 uint32_t v32;
82                 uint8_t v8[4];
83         } idx;
84         for(r=0; r<14; ++r){
85                 for(i=0; i<8; ++i){
86                         idx.v32 = ((uint32_t*)blake_index_lut)[i];
87                         s = *p++;
88                         if(p==(uint16_t*)(blake_sigma+160)){
89                                 p=(uint16_t*)blake_sigma;
90                         }
91                         A += B + (((uint32_t*)m)[s&0xff] ^ blake_c[s>>8]);
92                         D  = ROTR32(A^D, 16);
93                         C += D;
94                         B  = ROTR32(B^C, 12);
95                         A += B + (((uint32_t*)m)[s>>8] ^ blake_c[s&0xff]);
96                         D  = ROTR32(A^D, 8);
97                         C += D;
98                         B  = ROTR32(B^C, 7);
99                 }
100         }
101 }
102
103 static
104 void blake_small_collapse(blake_small_ctx_t* ctx, uint32_t* v){
105         uint8_t i;
106         for(i=0; i<8; ++i){
107                 ctx->h[i] ^= ctx->s[i%4] ^ v[i] ^ v[8+i];
108         }
109 }
110
111 void blake_small_nextBlock(blake_small_ctx_t* ctx, const void* msg){
112         uint32_t v[16];
113         uint32_t m[16];
114         union {
115                 uint64_t v64;
116                 uint32_t v32[2];
117         }ctr;
118         blake_small_expand(v,ctx);
119         ctx->counter++;
120         ctr.v64 = ctx->counter*512;
121         v[12] ^= ctr.v32[0];
122         v[13] ^= ctr.v32[0];
123         v[14] ^= ctr.v32[1];
124         v[15] ^= ctr.v32[1];
125         blake_small_changeendian(m, msg);
126         blake_small_compress(v, m);
127         blake_small_collapse(ctx, v);
128 }
129
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;
135         }
136         union {
137                 uint8_t   v8[64];
138                 uint32_t v32[16];
139                 uint64_t v64[ 8];
140         } buffer;
141         uint32_t v[16];
142         union {
143                 uint64_t v64;
144                 uint32_t v32[2];
145         }ctr;
146         ctr.v64 = ctx->counter*512+length_b;
147         memset(buffer.v8, 0, 64);
148         memcpy(buffer.v8, msg, (length_b+7)/8);
149         buffer.v8[length_b/8] |= 0x80 >> (length_b&0x7);
150         blake_small_changeendian(buffer.v8, buffer.v8);
151         blake_small_expand(v, ctx);
152         if(length_b>512-64-2){
153                 v[12] ^= ctr.v32[0];
154                 v[13] ^= ctr.v32[0];
155                 v[14] ^= ctr.v32[1];
156                 v[15] ^= ctr.v32[1];
157                 blake_small_compress(v, buffer.v8);
158                 blake_small_collapse(ctx, v);
159                 memset(buffer.v8, 0, 64-8);
160                 blake_small_expand(v, ctx);
161         }else{
162                 if(length_b){
163                         v[12] ^= ctr.v32[0];
164                         v[13] ^= ctr.v32[0];
165                         v[14] ^= ctr.v32[1];
166                         v[15] ^= ctr.v32[1];
167                 }
168         }
169         if(ctx->appendone)
170                 buffer.v8[64-8-4] |= 0x01;
171         buffer.v32[14] = ctr.v32[1];
172         buffer.v32[15] = ctr.v32[0];
173         blake_small_compress(v, buffer.v8);
174         blake_small_collapse(ctx, v);
175
176 }
177
178 static const
179 uint32_t blake256_iv[] = {
180         0x6A09E667L, 0xBB67AE85,
181         0x3C6EF372L, 0xA54FF53A,
182         0x510E527FL, 0x9B05688C,
183         0x1F83D9ABL, 0x5BE0CD19
184 };
185
186 void blake256_init(blake256_ctx_t* ctx){
187         uint8_t i;
188         for(i=0; i<8; ++i){
189                 ctx->h[i] = blake256_iv[i];
190         }
191         memset(ctx->s, 0, 4*4);
192         ctx->counter = 0;
193         ctx->appendone = 1;
194 }
195
196 static const
197 uint32_t blake224_iv[] = {
198         0xC1059ED8, 0x367CD507,
199         0x3070DD17, 0xF70E5939,
200         0xFFC00B31, 0x68581511,
201         0x64F98FA7, 0xBEFA4FA4
202 };
203
204 void blake224_init(blake224_ctx_t* ctx){
205         uint8_t i;
206         for(i=0; i<8; ++i){
207                 ctx->h[i] = blake224_iv[i];
208         }
209         memset(ctx->s, 0, 4*4);
210         ctx->counter = 0;
211         ctx->appendone = 0;
212 }
213
214 void blake256_ctx2hash(void* dest, const blake256_ctx_t* ctx){
215         uint8_t i;
216         for(i=0; i<8; ++i){
217                 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(ctx->h[i]);
218         }
219 }
220
221 void blake224_ctx2hash(void* dest, const blake224_ctx_t* ctx){
222         uint8_t i;
223         for(i=0; i<7; ++i){
224                 ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(ctx->h[i]);
225         }
226 }
227
228 void blake256_nextBlock(blake256_ctx_t* ctx, const void* block){
229         blake_small_nextBlock(ctx, block);
230 }
231
232 void blake224_nextBlock(blake224_ctx_t* ctx, const void* block){
233         blake_small_nextBlock(ctx, block);
234 }
235
236 void blake256_lastBlock(blake256_ctx_t* ctx, const void* block, uint16_t length_b){
237         blake_small_lastBlock(ctx, block, length_b);
238 }
239
240 void blake224_lastBlock(blake224_ctx_t* ctx, const void* block, uint16_t length_b){
241         blake_small_lastBlock(ctx, block, length_b);
242 }
243
244 void blake256(void* dest, const void* msg, uint32_t length_b){
245         blake_small_ctx_t ctx;
246         blake256_init(&ctx);
247         while(length_b>=BLAKE_SMALL_BLOCKSIZE){
248                 blake_small_nextBlock(&ctx, msg);
249                 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
250                 length_b -= BLAKE_SMALL_BLOCKSIZE;
251         }
252         blake_small_lastBlock(&ctx, msg, length_b);
253         blake256_ctx2hash(dest, &ctx);
254 }
255
256 void blake224(void* dest, const void* msg, uint32_t length_b){
257         blake_small_ctx_t ctx;
258         blake224_init(&ctx);
259         while(length_b>=BLAKE_SMALL_BLOCKSIZE){
260                 blake_small_nextBlock(&ctx, msg);
261                 msg = (uint8_t*)msg + BLAKE_SMALL_BLOCKSIZE_B;
262                 length_b -= BLAKE_SMALL_BLOCKSIZE;
263         }
264         blake_small_lastBlock(&ctx, msg, length_b);
265         blake224_ctx2hash(dest, &ctx);
266 }