6f195a5bb0ff3d833c139e4b745ebd7678b6904b
[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          64
58 #define SHA1_DIGEST_LEN         20
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[i] = be32_to_cpu(block[i]))
67 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
68                                      ^block[(i+2)&15]^block[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 static void SHA1Transform(uint32_t state[5], const uint8_t buffer[64])
80 {
81         uint32_t a, b, c, d, e;
82         uint32_t block[16];
83
84         memcpy(&block, buffer, 64);
85
86         /* Copy context->state[] to working vars */
87         a = state[0];
88         b = state[1];
89         c = state[2];
90         d = state[3];
91         e = state[4];
92         /* 4 rounds of 20 operations each. Loop unrolled. */
93         R0(a,b,c,d,e, 0);
94         R0(e,a,b,c,d, 1);
95         R0(d,e,a,b,c, 2);
96         R0(c,d,e,a,b, 3);
97         R0(b,c,d,e,a, 4);
98         R0(a,b,c,d,e, 5);
99         R0(e,a,b,c,d, 6);
100         R0(d,e,a,b,c, 7);
101         R0(c,d,e,a,b, 8);
102         R0(b,c,d,e,a, 9);
103         R0(a,b,c,d,e,10);
104         R0(e,a,b,c,d,11);
105         R0(d,e,a,b,c,12);
106         R0(c,d,e,a,b,13);
107         R0(b,c,d,e,a,14);
108         R0(a,b,c,d,e,15);
109         R1(e,a,b,c,d,16);
110         R1(d,e,a,b,c,17);
111         R1(c,d,e,a,b,18);
112         R1(b,c,d,e,a,19);
113         R2(a,b,c,d,e,20);
114         R2(e,a,b,c,d,21);
115         R2(d,e,a,b,c,22);
116         R2(c,d,e,a,b,23);
117         R2(b,c,d,e,a,24);
118         R2(a,b,c,d,e,25);
119         R2(e,a,b,c,d,26);
120         R2(d,e,a,b,c,27);
121         R2(c,d,e,a,b,28);
122         R2(b,c,d,e,a,29);
123         R2(a,b,c,d,e,30);
124         R2(e,a,b,c,d,31);
125         R2(d,e,a,b,c,32);
126         R2(c,d,e,a,b,33);
127         R2(b,c,d,e,a,34);
128         R2(a,b,c,d,e,35);
129         R2(e,a,b,c,d,36);
130         R2(d,e,a,b,c,37);
131         R2(c,d,e,a,b,38);
132         R2(b,c,d,e,a,39);
133         R3(a,b,c,d,e,40);
134         R3(e,a,b,c,d,41);
135         R3(d,e,a,b,c,42);
136         R3(c,d,e,a,b,43);
137         R3(b,c,d,e,a,44);
138         R3(a,b,c,d,e,45);
139         R3(e,a,b,c,d,46);
140         R3(d,e,a,b,c,47);
141         R3(c,d,e,a,b,48);
142         R3(b,c,d,e,a,49);
143         R3(a,b,c,d,e,50);
144         R3(e,a,b,c,d,51);
145         R3(d,e,a,b,c,52);
146         R3(c,d,e,a,b,53);
147         R3(b,c,d,e,a,54);
148         R3(a,b,c,d,e,55);
149         R3(e,a,b,c,d,56);
150         R3(d,e,a,b,c,57);
151         R3(c,d,e,a,b,58);
152         R3(b,c,d,e,a,59);
153         R4(a,b,c,d,e,60);
154         R4(e,a,b,c,d,61);
155         R4(d,e,a,b,c,62);
156         R4(c,d,e,a,b,63);
157         R4(b,c,d,e,a,64);
158         R4(a,b,c,d,e,65);
159         R4(e,a,b,c,d,66);
160         R4(d,e,a,b,c,67);
161         R4(c,d,e,a,b,68);
162         R4(b,c,d,e,a,69);
163         R4(a,b,c,d,e,70);
164         R4(e,a,b,c,d,71);
165         R4(d,e,a,b,c,72);
166         R4(c,d,e,a,b,73);
167         R4(b,c,d,e,a,74);
168         R4(a,b,c,d,e,75);
169         R4(e,a,b,c,d,76);
170         R4(d,e,a,b,c,77);
171         R4(c,d,e,a,b,78);
172         R4(b,c,d,e,a,79);
173         /* Add the working vars back into context.state[] */
174         state[0] += a;
175         state[1] += b;
176         state[2] += c;
177         state[3] += d;
178         state[4] += e;
179         /* Wipe variables */
180         a = b = c = d = e = 0;
181 }
182
183 static void SHA1_begin(Hash* h)
184 {
185         SHA1_Context *context = (SHA1_Context*)h;
186         /* SHA1 initialization constants */
187         context->state[0] = 0x67452301;
188         context->state[1] = 0xEFCDAB89;
189         context->state[2] = 0x98BADCFE;
190         context->state[3] = 0x10325476;
191         context->state[4] = 0xC3D2E1F0;
192         context->count[0] = context->count[1] = 0;
193 }
194
195 /* Run your data through this. */
196
197 static void SHA1_update(Hash* h, const void* vdata, size_t len)
198 {
199         SHA1_Context *context = (SHA1_Context*)h;
200         const uint8_t *data = (const uint8_t*)vdata;
201         size_t i, j;
202
203         j = (context->count[0] >> 3) & 63;
204         if ((context->count[0] += len << 3) < (len << 3))
205                 context->count[1]++;
206         context->count[1] += (len >> 29);
207         if ((j + len) > 63) {
208                 memcpy(&context->buffer[j], data, (i = 64-j));
209                 SHA1Transform(context->state, context->buffer);
210                 for ( ; i + 63 < len; i += 64) {
211                         SHA1Transform(context->state, &data[i]);
212                 }
213                 j = 0;
214         } else
215                 i = 0;
216         memcpy(&context->buffer[j], &data[i], len - i);
217 }
218
219
220 /* Add padding and return the message digest. */
221
222 static uint8_t *SHA1_final(Hash* h)
223 {
224         SHA1_Context *context = (SHA1_Context*)h;
225         uint32_t i;
226         uint8_t finalcount[8];
227
228         for (i = 0; i < 8; i++)
229                 finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
230                                            >> ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */
231
232         SHA1_update(h, "\200", 1);
233         while ((context->count[0] & 504) != 448)
234                 SHA1_update(h, "\0", 1);
235         SHA1_update(h, finalcount, 8);  /* Should cause a SHA1Transform() */
236
237         for (i = 0; i < 20; i++)
238                 context->buffer[i] = (uint8_t)
239                                      ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
240
241         PURGE(i);
242         PURGE(finalcount);
243         return context->buffer;
244 }
245
246
247 /*************************************************************/
248
249 void SHA1_init(SHA1_Context* ctx)
250 {
251         ctx->h.block_len = SHA1_BLOCK_LEN;
252         ctx->h.digest_len = SHA1_DIGEST_LEN;
253         ctx->h.begin = SHA1_begin;
254         ctx->h.update = SHA1_update;
255         ctx->h.final = SHA1_final;
256 }