Add SHA-1 unrolled implementation (optimized for 32-bit processors)
[bertos.git] / bertos / sec / hash / sha1.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos 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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief SHA-1 Hashing algorithm.
34  * \author Giovanni Bajo <rasky@develer.com>
35  */
36
37 /*
38  * Derived from:
39  * SHA-1 in C
40  * By Steve Reid <steve@edmweb.com>
41  * 100% Public Domain
42  */
43
44 /* #define LITTLE_ENDIAN * This should be #define'd if true. */
45 /* #define SHA1HANDSOFF * Copies data before messing with it. */
46
47 #include "sha1.h"
48
49 #include <cfg/compiler.h>
50 #include <cfg/debug.h>
51 #include <cfg/macros.h>
52 #include <cpu/byteorder.h>  // CPU_BYTE_ORDER
53 #include <string.h>
54 #include <stdlib.h>
55 #include <sec/util.h>
56
57 #define SHA1_BLOCK_LEN          16
58 #define SHA1_DIGEST_LEN         16
59
60 static void SHA1Transform(uint32_t state[5], const uint8_t buffer[64]);
61
62 #define rol(value, bits)  ROTL(value, bits)
63
64 /* blk0() and blk() perform the initial expand. */
65 /* I got the idea of expanding during the round function from SSLeay */
66 #define blk0(i) (block->l[i] = be32_to_cpu(block->l[i]))
67 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
68                                      ^block->l[(i+2)&15]^block->l[i&15],1))
69
70 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
71 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
72 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
73 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
74 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
75 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
76
77
78 /* Hash a single 512-bit block. This is the core of the algorithm. */
79 typedef union {
80         uint8_t c[64];
81         uint32_t l[16];
82 } CHAR64LONG16;
83 static CHAR64LONG16 workspace;
84
85 static void SHA1Transform(uint32_t state[5], const uint8_t buffer[64])
86 {
87         uint32_t a, b, c, d, e;
88
89         CHAR64LONG16* block = &workspace;
90         memcpy(block, buffer, 64);
91
92         /* Copy context->state[] to working vars */
93         a = state[0];
94         b = state[1];
95         c = state[2];
96         d = state[3];
97         e = state[4];
98         /* 4 rounds of 20 operations each. Loop unrolled. */
99         R0(a,b,c,d,e, 0);
100         R0(e,a,b,c,d, 1);
101         R0(d,e,a,b,c, 2);
102         R0(c,d,e,a,b, 3);
103         R0(b,c,d,e,a, 4);
104         R0(a,b,c,d,e, 5);
105         R0(e,a,b,c,d, 6);
106         R0(d,e,a,b,c, 7);
107         R0(c,d,e,a,b, 8);
108         R0(b,c,d,e,a, 9);
109         R0(a,b,c,d,e,10);
110         R0(e,a,b,c,d,11);
111         R0(d,e,a,b,c,12);
112         R0(c,d,e,a,b,13);
113         R0(b,c,d,e,a,14);
114         R0(a,b,c,d,e,15);
115         R1(e,a,b,c,d,16);
116         R1(d,e,a,b,c,17);
117         R1(c,d,e,a,b,18);
118         R1(b,c,d,e,a,19);
119         R2(a,b,c,d,e,20);
120         R2(e,a,b,c,d,21);
121         R2(d,e,a,b,c,22);
122         R2(c,d,e,a,b,23);
123         R2(b,c,d,e,a,24);
124         R2(a,b,c,d,e,25);
125         R2(e,a,b,c,d,26);
126         R2(d,e,a,b,c,27);
127         R2(c,d,e,a,b,28);
128         R2(b,c,d,e,a,29);
129         R2(a,b,c,d,e,30);
130         R2(e,a,b,c,d,31);
131         R2(d,e,a,b,c,32);
132         R2(c,d,e,a,b,33);
133         R2(b,c,d,e,a,34);
134         R2(a,b,c,d,e,35);
135         R2(e,a,b,c,d,36);
136         R2(d,e,a,b,c,37);
137         R2(c,d,e,a,b,38);
138         R2(b,c,d,e,a,39);
139         R3(a,b,c,d,e,40);
140         R3(e,a,b,c,d,41);
141         R3(d,e,a,b,c,42);
142         R3(c,d,e,a,b,43);
143         R3(b,c,d,e,a,44);
144         R3(a,b,c,d,e,45);
145         R3(e,a,b,c,d,46);
146         R3(d,e,a,b,c,47);
147         R3(c,d,e,a,b,48);
148         R3(b,c,d,e,a,49);
149         R3(a,b,c,d,e,50);
150         R3(e,a,b,c,d,51);
151         R3(d,e,a,b,c,52);
152         R3(c,d,e,a,b,53);
153         R3(b,c,d,e,a,54);
154         R3(a,b,c,d,e,55);
155         R3(e,a,b,c,d,56);
156         R3(d,e,a,b,c,57);
157         R3(c,d,e,a,b,58);
158         R3(b,c,d,e,a,59);
159         R4(a,b,c,d,e,60);
160         R4(e,a,b,c,d,61);
161         R4(d,e,a,b,c,62);
162         R4(c,d,e,a,b,63);
163         R4(b,c,d,e,a,64);
164         R4(a,b,c,d,e,65);
165         R4(e,a,b,c,d,66);
166         R4(d,e,a,b,c,67);
167         R4(c,d,e,a,b,68);
168         R4(b,c,d,e,a,69);
169         R4(a,b,c,d,e,70);
170         R4(e,a,b,c,d,71);
171         R4(d,e,a,b,c,72);
172         R4(c,d,e,a,b,73);
173         R4(b,c,d,e,a,74);
174         R4(a,b,c,d,e,75);
175         R4(e,a,b,c,d,76);
176         R4(d,e,a,b,c,77);
177         R4(c,d,e,a,b,78);
178         R4(b,c,d,e,a,79);
179         /* Add the working vars back into context.state[] */
180         state[0] += a;
181         state[1] += b;
182         state[2] += c;
183         state[3] += d;
184         state[4] += e;
185         /* Wipe variables */
186         a = b = c = d = e = 0;
187 }
188
189 static void SHA1_begin(Hash* h)
190 {
191         SHA1_Context *context = (SHA1_Context*)h;
192         /* SHA1 initialization constants */
193         context->state[0] = 0x67452301;
194         context->state[1] = 0xEFCDAB89;
195         context->state[2] = 0x98BADCFE;
196         context->state[3] = 0x10325476;
197         context->state[4] = 0xC3D2E1F0;
198         context->count[0] = context->count[1] = 0;
199 }
200
201 /* Run your data through this. */
202
203 static void SHA1_update(Hash* h, const void* vdata, size_t len)
204 {
205         SHA1_Context *context = (SHA1_Context*)h;
206         const uint8_t *data = (const uint8_t*)vdata;
207         size_t i, j;
208
209         j = (context->count[0] >> 3) & 63;
210         if ((context->count[0] += len << 3) < (len << 3))
211                 context->count[1]++;
212         context->count[1] += (len >> 29);
213         if ((j + len) > 63) {
214                 memcpy(&context->buffer[j], data, (i = 64-j));
215                 SHA1Transform(context->state, context->buffer);
216                 for ( ; i + 63 < len; i += 64) {
217                         SHA1Transform(context->state, &data[i]);
218                 }
219                 j = 0;
220         } else
221                 i = 0;
222         memcpy(&context->buffer[j], &data[i], len - i);
223 }
224
225
226 /* Add padding and return the message digest. */
227
228 static uint8_t *SHA1_final(Hash* h)
229 {
230         SHA1_Context *context = (SHA1_Context*)h;
231         uint32_t i;
232         uint8_t finalcount[8];
233
234         for (i = 0; i < 8; i++)
235                 finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
236                                            >> ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */
237
238         SHA1_update(h, "\200", 1);
239         while ((context->count[0] & 504) != 448)
240                 SHA1_update(h, "\0", 1);
241         SHA1_update(h, finalcount, 8);  /* Should cause a SHA1Transform() */
242
243         for (i = 0; i < 20; i++)
244                 context->buffer[i] = (uint8_t)
245                                      ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
246
247         PURGE(i);
248         PURGE(finalcount);
249         return context->buffer;
250 }
251
252
253 /*************************************************************/
254
255 void SHA1_init(SHA1_Context* ctx)
256 {
257         ctx->h.block_len = SHA1_BLOCK_LEN;
258         ctx->h.digest_len = SHA1_DIGEST_LEN;
259         ctx->h.begin = SHA1_begin;
260         ctx->h.update = SHA1_update;
261         ctx->h.final = SHA1_final;
262 }
263
264 #include <drv/timer.h>
265
266 void SHA1_benchmark(int numk)
267 {
268         SHA1_Context context;
269         SHA1_init(&context);
270
271         static uint8_t buf[512];
272         memset(buf, 0x12, sizeof(buf));
273
274         ticks_t t = timer_clock();
275
276         for (int j=0;j<64;++j) {
277                 SHA1_begin(&context.h);
278                 for (int i=0; i<numk*2; ++i)
279                         SHA1_update(&context.h, buf, 512);
280                 SHA1_final(&context.h);
281         }
282
283         t = timer_clock() - t;
284
285         utime_t usec = ticks_to_us(t) / 64;
286         kprintf("%s @ %dMhz: SHA1 of %dKiB of data: %lu.%lu ms\n", CPU_CORE_NAME, CPU_FREQ/1000000, numk, (usec/1000), (usec % 1000));
287 }
288
289 #if 0
290 /*
291 Test Vectors (from FIPS PUB 180-1)
292 "abc"
293   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
294 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
295   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
296 A million repetitions of "a"
297   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
298 */
299
300 int main(int argc, char** argv)
301 {
302         SHA1_test();
303 }
304
305 #endif