summaryrefslogtreecommitdiff
path: root/utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/sha1.c
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2008-07-11 15:50:46 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2008-07-11 15:50:46 +0000
commit14c7f45cdae826f88dc539c8c38dd95caf305731 (patch)
tree832da054b7cfb2dc6fd63339af736625f31d21aa /utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/sha1.c
parent7c84ede3781c27db73403bd6302f320c76a58c8c (diff)
downloadrockbox-14c7f45cdae826f88dc539c8c38dd95caf305731.tar.gz
rockbox-14c7f45cdae826f88dc539c8c38dd95caf305731.zip
Add zook's ZenUtils to SVN
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18010 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/sha1.c')
-rwxr-xr-xutils/zenutils/libraries/beecrypt-4.1.2/beecrypt/sha1.c329
1 files changed, 329 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/sha1.c b/utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/sha1.c
new file mode 100755
index 0000000000..fe7c2cd8b4
--- /dev/null
+++ b/utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/sha1.c
@@ -0,0 +1,329 @@
1/*
2 * Copyright (c) 1997, 1998, 1999, 2000, 2001 Virtual Unlimited B.V.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19
20/*!\file sha1.c
21 * \brief SHA-1 hash function, as specified by NIST FIPS 180-1.
22 * \author Bob Deblier <bob.deblier@pandora.be>
23 * \ingroup HASH_m HASH_sha1_m
24 */
25
26#define BEECRYPT_DLL_EXPORT
27
28#if HAVE_CONFIG_H
29# include "config.h"
30#endif
31
32#include "beecrypt/sha1.h"
33
34#if HAVE_ENDIAN_H && HAVE_ASM_BYTEORDER_H
35# include <endian.h>
36#endif
37
38#include "beecrypt/endianness.h"
39
40/*!\addtogroup HASH_sha1_m
41 * \{
42 */
43
44static const uint32_t k[4] = { 0x5a827999U, 0x6ed9eba1U, 0x8f1bbcdcU, 0xca62c1d6U };
45
46static const uint32_t hinit[5] = { 0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U };
47
48const hashFunction sha1 = {
49 "SHA-1",
50 sizeof(sha1Param),
51 64,
52 20,
53 (hashFunctionReset) sha1Reset,
54 (hashFunctionUpdate) sha1Update,
55 (hashFunctionDigest) sha1Digest
56};
57
58int sha1Reset(register sha1Param* p)
59{
60 memcpy(p->h, hinit, 5 * sizeof(uint32_t));
61 memset(p->data, 0, 80 * sizeof(uint32_t));
62 #if (MP_WBITS == 64)
63 mpzero(1, p->length);
64 #elif (MP_WBITS == 32)
65 mpzero(2, p->length);
66 #else
67 # error
68 #endif
69 p->offset = 0;
70 return 0;
71}
72
73#define SUBROUND1(a, b, c, d, e, w, k) \
74 e = ROTL32(a, 5) + ((b&(c^d))^d) + e + w + k; \
75 b = ROTR32(b, 2)
76#define SUBROUND2(a, b, c, d, e, w, k) \
77 e = ROTL32(a, 5) + (b^c^d) + e + w + k; \
78 b = ROTR32(b, 2)
79#define SUBROUND3(a, b, c, d, e, w, k) \
80 e = ROTL32(a, 5) + (((b|c)&d)|(b&c)) + e + w + k; \
81 b = ROTR32(b, 2)
82#define SUBROUND4(a, b, c, d, e, w, k) \
83 e = ROTL32(a, 5) + (b^c^d) + e + w + k; \
84 b = ROTR32(b, 2)
85
86#ifndef ASM_SHA1PROCESS
87void sha1Process(sha1Param* sp)
88{
89 register uint32_t a, b, c, d, e;
90 register uint32_t *w;
91 register byte t;
92
93 #if WORDS_BIGENDIAN
94 w = sp->data + 16;
95 #else
96 w = sp->data;
97 t = 16;
98 while (t--)
99 {
100 register uint32_t temp = swapu32(*w);
101 *(w++) = temp;
102 }
103 #endif
104
105 t = 64;
106 while (t--)
107 {
108 register uint32_t temp = w[-3] ^ w[-8] ^ w[-14] ^ w[-16];
109 *(w++) = ROTL32(temp, 1);
110 }
111
112 w = sp->data;
113
114 a = sp->h[0]; b = sp->h[1]; c = sp->h[2]; d = sp->h[3]; e = sp->h[4];
115
116 SUBROUND1(a,b,c,d,e,w[ 0],k[0]);
117 SUBROUND1(e,a,b,c,d,w[ 1],k[0]);
118 SUBROUND1(d,e,a,b,c,w[ 2],k[0]);
119 SUBROUND1(c,d,e,a,b,w[ 3],k[0]);
120 SUBROUND1(b,c,d,e,a,w[ 4],k[0]);
121 SUBROUND1(a,b,c,d,e,w[ 5],k[0]);
122 SUBROUND1(e,a,b,c,d,w[ 6],k[0]);
123 SUBROUND1(d,e,a,b,c,w[ 7],k[0]);
124 SUBROUND1(c,d,e,a,b,w[ 8],k[0]);
125 SUBROUND1(b,c,d,e,a,w[ 9],k[0]);
126 SUBROUND1(a,b,c,d,e,w[10],k[0]);
127 SUBROUND1(e,a,b,c,d,w[11],k[0]);
128 SUBROUND1(d,e,a,b,c,w[12],k[0]);
129 SUBROUND1(c,d,e,a,b,w[13],k[0]);
130 SUBROUND1(b,c,d,e,a,w[14],k[0]);
131 SUBROUND1(a,b,c,d,e,w[15],k[0]);
132 SUBROUND1(e,a,b,c,d,w[16],k[0]);
133 SUBROUND1(d,e,a,b,c,w[17],k[0]);
134 SUBROUND1(c,d,e,a,b,w[18],k[0]);
135 SUBROUND1(b,c,d,e,a,w[19],k[0]);
136
137 SUBROUND2(a,b,c,d,e,w[20],k[1]);
138 SUBROUND2(e,a,b,c,d,w[21],k[1]);
139 SUBROUND2(d,e,a,b,c,w[22],k[1]);
140 SUBROUND2(c,d,e,a,b,w[23],k[1]);
141 SUBROUND2(b,c,d,e,a,w[24],k[1]);
142 SUBROUND2(a,b,c,d,e,w[25],k[1]);
143 SUBROUND2(e,a,b,c,d,w[26],k[1]);
144 SUBROUND2(d,e,a,b,c,w[27],k[1]);
145 SUBROUND2(c,d,e,a,b,w[28],k[1]);
146 SUBROUND2(b,c,d,e,a,w[29],k[1]);
147 SUBROUND2(a,b,c,d,e,w[30],k[1]);
148 SUBROUND2(e,a,b,c,d,w[31],k[1]);
149 SUBROUND2(d,e,a,b,c,w[32],k[1]);
150 SUBROUND2(c,d,e,a,b,w[33],k[1]);
151 SUBROUND2(b,c,d,e,a,w[34],k[1]);
152 SUBROUND2(a,b,c,d,e,w[35],k[1]);
153 SUBROUND2(e,a,b,c,d,w[36],k[1]);
154 SUBROUND2(d,e,a,b,c,w[37],k[1]);
155 SUBROUND2(c,d,e,a,b,w[38],k[1]);
156 SUBROUND2(b,c,d,e,a,w[39],k[1]);
157
158 SUBROUND3(a,b,c,d,e,w[40],k[2]);
159 SUBROUND3(e,a,b,c,d,w[41],k[2]);
160 SUBROUND3(d,e,a,b,c,w[42],k[2]);
161 SUBROUND3(c,d,e,a,b,w[43],k[2]);
162 SUBROUND3(b,c,d,e,a,w[44],k[2]);
163 SUBROUND3(a,b,c,d,e,w[45],k[2]);
164 SUBROUND3(e,a,b,c,d,w[46],k[2]);
165 SUBROUND3(d,e,a,b,c,w[47],k[2]);
166 SUBROUND3(c,d,e,a,b,w[48],k[2]);
167 SUBROUND3(b,c,d,e,a,w[49],k[2]);
168 SUBROUND3(a,b,c,d,e,w[50],k[2]);
169 SUBROUND3(e,a,b,c,d,w[51],k[2]);
170 SUBROUND3(d,e,a,b,c,w[52],k[2]);
171 SUBROUND3(c,d,e,a,b,w[53],k[2]);
172 SUBROUND3(b,c,d,e,a,w[54],k[2]);
173 SUBROUND3(a,b,c,d,e,w[55],k[2]);
174 SUBROUND3(e,a,b,c,d,w[56],k[2]);
175 SUBROUND3(d,e,a,b,c,w[57],k[2]);
176 SUBROUND3(c,d,e,a,b,w[58],k[2]);
177 SUBROUND3(b,c,d,e,a,w[59],k[2]);
178
179 SUBROUND4(a,b,c,d,e,w[60],k[3]);
180 SUBROUND4(e,a,b,c,d,w[61],k[3]);
181 SUBROUND4(d,e,a,b,c,w[62],k[3]);
182 SUBROUND4(c,d,e,a,b,w[63],k[3]);
183 SUBROUND4(b,c,d,e,a,w[64],k[3]);
184 SUBROUND4(a,b,c,d,e,w[65],k[3]);
185 SUBROUND4(e,a,b,c,d,w[66],k[3]);
186 SUBROUND4(d,e,a,b,c,w[67],k[3]);
187 SUBROUND4(c,d,e,a,b,w[68],k[3]);
188 SUBROUND4(b,c,d,e,a,w[69],k[3]);
189 SUBROUND4(a,b,c,d,e,w[70],k[3]);
190 SUBROUND4(e,a,b,c,d,w[71],k[3]);
191 SUBROUND4(d,e,a,b,c,w[72],k[3]);
192 SUBROUND4(c,d,e,a,b,w[73],k[3]);
193 SUBROUND4(b,c,d,e,a,w[74],k[3]);
194 SUBROUND4(a,b,c,d,e,w[75],k[3]);
195 SUBROUND4(e,a,b,c,d,w[76],k[3]);
196 SUBROUND4(d,e,a,b,c,w[77],k[3]);
197 SUBROUND4(c,d,e,a,b,w[78],k[3]);
198 SUBROUND4(b,c,d,e,a,w[79],k[3]);
199
200 sp->h[0] += a;
201 sp->h[1] += b;
202 sp->h[2] += c;
203 sp->h[3] += d;
204 sp->h[4] += e;
205}
206#endif
207
208int sha1Update(sha1Param* sp, const byte* data, size_t size)
209{
210 register uint32_t proclength;
211
212 #if (MP_WBITS == 64)
213 mpw add[1];
214 mpsetw(1, add, size);
215 mplshift(1, add, 3);
216 mpadd(1, sp->length, add);
217 #elif (MP_WBITS == 32)
218 mpw add[2];
219 mpsetw(2, add, size);
220 mplshift(2, add, 3);
221 mpadd(2, sp->length, add);
222 #else
223 # error
224 #endif
225
226 while (size > 0)
227 {
228 proclength = ((sp->offset + size) > 64U) ? (64U - sp->offset) : size;
229 memcpy(((byte *) sp->data) + sp->offset, data, proclength);
230 size -= proclength;
231 data += proclength;
232 sp->offset += proclength;
233
234 if (sp->offset == 64)
235 {
236 sha1Process(sp);
237 sp->offset = 0;
238 }
239 }
240 return 0;
241}
242
243static void sha1Finish(sha1Param* sp)
244{
245 register byte *ptr = ((byte *) sp->data) + sp->offset++;
246
247 *(ptr++) = 0x80;
248
249 if (sp->offset > 56)
250 {
251 while (sp->offset++ < 64)
252 *(ptr++) = 0;
253
254 sha1Process(sp);
255 sp->offset = 0;
256 }
257
258 ptr = ((byte*) sp->data) + sp->offset;
259 while (sp->offset++ < 56)
260 *(ptr++) = 0;
261
262 #if WORDS_BIGENDIAN
263 memcpy(ptr, sp->length, 8);
264 #else
265 # if (MP_WBITS == 64)
266 ptr[0] = (byte)(sp->length[0] >> 56);
267 ptr[1] = (byte)(sp->length[0] >> 48);
268 ptr[2] = (byte)(sp->length[0] >> 40);
269 ptr[3] = (byte)(sp->length[0] >> 32);
270 ptr[4] = (byte)(sp->length[0] >> 24);
271 ptr[5] = (byte)(sp->length[0] >> 16);
272 ptr[6] = (byte)(sp->length[0] >> 8);
273 ptr[7] = (byte)(sp->length[0] );
274 #elif (MP_WBITS == 32)
275 ptr[0] = (byte)(sp->length[0] >> 24);
276 ptr[1] = (byte)(sp->length[0] >> 16);
277 ptr[2] = (byte)(sp->length[0] >> 8);
278 ptr[3] = (byte)(sp->length[0] );
279 ptr[4] = (byte)(sp->length[1] >> 24);
280 ptr[5] = (byte)(sp->length[1] >> 16);
281 ptr[6] = (byte)(sp->length[1] >> 8);
282 ptr[7] = (byte)(sp->length[1] );
283 # else
284 # error
285 # endif
286 #endif
287
288 sha1Process(sp);
289
290 sp->offset = 0;
291}
292
293int sha1Digest(sha1Param* sp, byte* data)
294{
295 sha1Finish(sp);
296
297 #if WORDS_BIGENDIAN
298 memcpy(data, sp->h, 20);
299 #else
300 /* encode 5 integers big-endian style */
301 data[ 0] = (byte)(sp->h[0] >> 24);
302 data[ 1] = (byte)(sp->h[0] >> 16);
303 data[ 2] = (byte)(sp->h[0] >> 8);
304 data[ 3] = (byte)(sp->h[0] >> 0);
305 data[ 4] = (byte)(sp->h[1] >> 24);
306 data[ 5] = (byte)(sp->h[1] >> 16);
307 data[ 6] = (byte)(sp->h[1] >> 8);
308 data[ 7] = (byte)(sp->h[1] >> 0);
309 data[ 8] = (byte)(sp->h[2] >> 24);
310 data[ 9] = (byte)(sp->h[2] >> 16);
311 data[10] = (byte)(sp->h[2] >> 8);
312 data[11] = (byte)(sp->h[2] >> 0);
313 data[12] = (byte)(sp->h[3] >> 24);
314 data[13] = (byte)(sp->h[3] >> 16);
315 data[14] = (byte)(sp->h[3] >> 8);
316 data[15] = (byte)(sp->h[3] >> 0);
317 data[16] = (byte)(sp->h[4] >> 24);
318 data[17] = (byte)(sp->h[4] >> 16);
319 data[18] = (byte)(sp->h[4] >> 8);
320 data[19] = (byte)(sp->h[4] >> 0);
321 #endif
322
323 sha1Reset(sp);
324
325 return 0;
326}
327
328/*!\}
329 */