summaryrefslogtreecommitdiffabout
path: root/pwmanager/libcrypt/cipher/sha1.c
authorzautrix <zautrix>2004-10-19 20:16:14 (UTC)
committer zautrix <zautrix>2004-10-19 20:16:14 (UTC)
commiteca49bb06a71980ef61d078904573f25890fc7f2 (patch) (unidiff)
treec5338e3b12430248979a9ac2c1c7e6646ea9ecdf /pwmanager/libcrypt/cipher/sha1.c
parent53cc32b6e7b1f672bf91b2baf2df6c1e8baf3e0a (diff)
downloadkdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.zip
kdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.tar.gz
kdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.tar.bz2
Initial revision
Diffstat (limited to 'pwmanager/libcrypt/cipher/sha1.c') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/libcrypt/cipher/sha1.c368
1 files changed, 368 insertions, 0 deletions
diff --git a/pwmanager/libcrypt/cipher/sha1.c b/pwmanager/libcrypt/cipher/sha1.c
new file mode 100644
index 0000000..1f31443
--- a/dev/null
+++ b/pwmanager/libcrypt/cipher/sha1.c
@@ -0,0 +1,368 @@
1/* sha1.c - SHA1 hash function
2 *Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt 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 Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21
22/* Test vectors:
23 *
24 * "abc"
25 * A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
26 *
27 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
28 * 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
29 */
30
31
32#include <config.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <assert.h>
37#include "g10lib.h"
38#include "memory.h"
39#include "bithelp.h"
40#include "cipher.h"
41
42typedef struct {
43 u32 h0,h1,h2,h3,h4;
44 u32 nblocks;
45 byte buf[64];
46 int count;
47} SHA1_CONTEXT;
48
49
50static void
51sha1_init (void *context)
52{
53 SHA1_CONTEXT *hd = context;
54
55 hd->h0 = 0x67452301;
56 hd->h1 = 0xefcdab89;
57 hd->h2 = 0x98badcfe;
58 hd->h3 = 0x10325476;
59 hd->h4 = 0xc3d2e1f0;
60 hd->nblocks = 0;
61 hd->count = 0;
62}
63
64
65/****************
66 * Transform the message X which consists of 16 32-bit-words
67 */
68static void
69transform( SHA1_CONTEXT *hd, byte *data )
70{
71 register u32 a,b,c,d,e,tm;
72 u32 x[16];
73
74 /* Get values from the chaining vars. */
75 a = hd->h0;
76 b = hd->h1;
77 c = hd->h2;
78 d = hd->h3;
79 e = hd->h4;
80
81#ifdef WORDS_BIGENDIAN
82 memcpy( x, data, 64 );
83#else
84 {
85 int i;
86 byte *p2;
87 for(i=0, p2=(byte*)x; i < 16; i++, p2 += 4 )
88 {
89 p2[3] = *data++;
90 p2[2] = *data++;
91 p2[1] = *data++;
92 p2[0] = *data++;
93 }
94 }
95#endif
96
97
98#define K1 0x5A827999L
99#define K2 0x6ED9EBA1L
100#define K3 0x8F1BBCDCL
101#define K4 0xCA62C1D6L
102#define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
103#define F2(x,y,z) ( x ^ y ^ z )
104#define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
105#define F4(x,y,z) ( x ^ y ^ z )
106
107
108#define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \
109 ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
110 , (x[i&0x0f] = rol(tm, 1)) )
111
112#define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
113 + f( b, c, d ) \
114 + k \
115 + m; \
116 b = rol( b, 30 ); \
117 } while(0)
118 R( a, b, c, d, e, F1, K1, x[ 0] );
119 R( e, a, b, c, d, F1, K1, x[ 1] );
120 R( d, e, a, b, c, F1, K1, x[ 2] );
121 R( c, d, e, a, b, F1, K1, x[ 3] );
122 R( b, c, d, e, a, F1, K1, x[ 4] );
123 R( a, b, c, d, e, F1, K1, x[ 5] );
124 R( e, a, b, c, d, F1, K1, x[ 6] );
125 R( d, e, a, b, c, F1, K1, x[ 7] );
126 R( c, d, e, a, b, F1, K1, x[ 8] );
127 R( b, c, d, e, a, F1, K1, x[ 9] );
128 R( a, b, c, d, e, F1, K1, x[10] );
129 R( e, a, b, c, d, F1, K1, x[11] );
130 R( d, e, a, b, c, F1, K1, x[12] );
131 R( c, d, e, a, b, F1, K1, x[13] );
132 R( b, c, d, e, a, F1, K1, x[14] );
133 R( a, b, c, d, e, F1, K1, x[15] );
134 R( e, a, b, c, d, F1, K1, M(16) );
135 R( d, e, a, b, c, F1, K1, M(17) );
136 R( c, d, e, a, b, F1, K1, M(18) );
137 R( b, c, d, e, a, F1, K1, M(19) );
138 R( a, b, c, d, e, F2, K2, M(20) );
139 R( e, a, b, c, d, F2, K2, M(21) );
140 R( d, e, a, b, c, F2, K2, M(22) );
141 R( c, d, e, a, b, F2, K2, M(23) );
142 R( b, c, d, e, a, F2, K2, M(24) );
143 R( a, b, c, d, e, F2, K2, M(25) );
144 R( e, a, b, c, d, F2, K2, M(26) );
145 R( d, e, a, b, c, F2, K2, M(27) );
146 R( c, d, e, a, b, F2, K2, M(28) );
147 R( b, c, d, e, a, F2, K2, M(29) );
148 R( a, b, c, d, e, F2, K2, M(30) );
149 R( e, a, b, c, d, F2, K2, M(31) );
150 R( d, e, a, b, c, F2, K2, M(32) );
151 R( c, d, e, a, b, F2, K2, M(33) );
152 R( b, c, d, e, a, F2, K2, M(34) );
153 R( a, b, c, d, e, F2, K2, M(35) );
154 R( e, a, b, c, d, F2, K2, M(36) );
155 R( d, e, a, b, c, F2, K2, M(37) );
156 R( c, d, e, a, b, F2, K2, M(38) );
157 R( b, c, d, e, a, F2, K2, M(39) );
158 R( a, b, c, d, e, F3, K3, M(40) );
159 R( e, a, b, c, d, F3, K3, M(41) );
160 R( d, e, a, b, c, F3, K3, M(42) );
161 R( c, d, e, a, b, F3, K3, M(43) );
162 R( b, c, d, e, a, F3, K3, M(44) );
163 R( a, b, c, d, e, F3, K3, M(45) );
164 R( e, a, b, c, d, F3, K3, M(46) );
165 R( d, e, a, b, c, F3, K3, M(47) );
166 R( c, d, e, a, b, F3, K3, M(48) );
167 R( b, c, d, e, a, F3, K3, M(49) );
168 R( a, b, c, d, e, F3, K3, M(50) );
169 R( e, a, b, c, d, F3, K3, M(51) );
170 R( d, e, a, b, c, F3, K3, M(52) );
171 R( c, d, e, a, b, F3, K3, M(53) );
172 R( b, c, d, e, a, F3, K3, M(54) );
173 R( a, b, c, d, e, F3, K3, M(55) );
174 R( e, a, b, c, d, F3, K3, M(56) );
175 R( d, e, a, b, c, F3, K3, M(57) );
176 R( c, d, e, a, b, F3, K3, M(58) );
177 R( b, c, d, e, a, F3, K3, M(59) );
178 R( a, b, c, d, e, F4, K4, M(60) );
179 R( e, a, b, c, d, F4, K4, M(61) );
180 R( d, e, a, b, c, F4, K4, M(62) );
181 R( c, d, e, a, b, F4, K4, M(63) );
182 R( b, c, d, e, a, F4, K4, M(64) );
183 R( a, b, c, d, e, F4, K4, M(65) );
184 R( e, a, b, c, d, F4, K4, M(66) );
185 R( d, e, a, b, c, F4, K4, M(67) );
186 R( c, d, e, a, b, F4, K4, M(68) );
187 R( b, c, d, e, a, F4, K4, M(69) );
188 R( a, b, c, d, e, F4, K4, M(70) );
189 R( e, a, b, c, d, F4, K4, M(71) );
190 R( d, e, a, b, c, F4, K4, M(72) );
191 R( c, d, e, a, b, F4, K4, M(73) );
192 R( b, c, d, e, a, F4, K4, M(74) );
193 R( a, b, c, d, e, F4, K4, M(75) );
194 R( e, a, b, c, d, F4, K4, M(76) );
195 R( d, e, a, b, c, F4, K4, M(77) );
196 R( c, d, e, a, b, F4, K4, M(78) );
197 R( b, c, d, e, a, F4, K4, M(79) );
198
199 /* Update chaining vars. */
200 hd->h0 += a;
201 hd->h1 += b;
202 hd->h2 += c;
203 hd->h3 += d;
204 hd->h4 += e;
205}
206
207
208/* Update the message digest with the contents
209 * of INBUF with length INLEN.
210 */
211static void
212sha1_write( void *context, byte *inbuf, size_t inlen)
213{
214 SHA1_CONTEXT *hd = context;
215
216 if( hd->count == 64 ) /* flush the buffer */
217 {
218 transform( hd, hd->buf );
219 _gcry_burn_stack (88+4*sizeof(void*));
220 hd->count = 0;
221 hd->nblocks++;
222 }
223 if( !inbuf )
224 return;
225
226 if( hd->count )
227 {
228 for( ; inlen && hd->count < 64; inlen-- )
229 hd->buf[hd->count++] = *inbuf++;
230 sha1_write( hd, NULL, 0 );
231 if( !inlen )
232 return;
233 }
234
235 while( inlen >= 64 )
236 {
237 transform( hd, inbuf );
238 hd->count = 0;
239 hd->nblocks++;
240 inlen -= 64;
241 inbuf += 64;
242 }
243 _gcry_burn_stack (88+4*sizeof(void*));
244 for( ; inlen && hd->count < 64; inlen-- )
245 hd->buf[hd->count++] = *inbuf++;
246}
247
248
249/* The routine final terminates the computation and
250 * returns the digest.
251 * The handle is prepared for a new cycle, but adding bytes to the
252 * handle will the destroy the returned buffer.
253 * Returns: 20 bytes representing the digest.
254 */
255
256static void
257sha1_final(void *context)
258{
259 SHA1_CONTEXT *hd = context;
260
261 u32 t, msb, lsb;
262 byte *p;
263
264 sha1_write(hd, NULL, 0); /* flush */;
265
266 t = hd->nblocks;
267 /* multiply by 64 to make a byte count */
268 lsb = t << 6;
269 msb = t >> 26;
270 /* add the count */
271 t = lsb;
272 if( (lsb += hd->count) < t )
273 msb++;
274 /* multiply by 8 to make a bit count */
275 t = lsb;
276 lsb <<= 3;
277 msb <<= 3;
278 msb |= t >> 29;
279
280 if( hd->count < 56 ) /* enough room */
281 {
282 hd->buf[hd->count++] = 0x80; /* pad */
283 while( hd->count < 56 )
284 hd->buf[hd->count++] = 0; /* pad */
285 }
286 else /* need one extra block */
287 {
288 hd->buf[hd->count++] = 0x80; /* pad character */
289 while( hd->count < 64 )
290 hd->buf[hd->count++] = 0;
291 sha1_write(hd, NULL, 0); /* flush */;
292 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
293 }
294 /* append the 64 bit count */
295 hd->buf[56] = msb >> 24;
296 hd->buf[57] = msb >> 16;
297 hd->buf[58] = msb >> 8;
298 hd->buf[59] = msb ;
299 hd->buf[60] = lsb >> 24;
300 hd->buf[61] = lsb >> 16;
301 hd->buf[62] = lsb >> 8;
302 hd->buf[63] = lsb ;
303 transform( hd, hd->buf );
304 _gcry_burn_stack (88+4*sizeof(void*));
305
306 p = hd->buf;
307#ifdef WORDS_BIGENDIAN
308#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
309#else /* little endian */
310 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
311 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
312#endif
313 X(0);
314 X(1);
315 X(2);
316 X(3);
317 X(4);
318#undef X
319
320}
321
322static byte *
323sha1_read( void *context )
324{
325 SHA1_CONTEXT *hd = context;
326
327 return hd->buf;
328}
329
330/****************
331 * Shortcut functions which puts the hash value of the supplied buffer
332 * into outbuf which must have a size of 20 bytes.
333 */
334void
335_gcry_sha1_hash_buffer (char *outbuf, const char *buffer, size_t length)
336{
337 SHA1_CONTEXT hd;
338
339 sha1_init (&hd);
340 sha1_write (&hd, (byte*)buffer, length);
341 sha1_final (&hd);
342 memcpy (outbuf, hd.buf, 20);
343}
344
345
346static byte asn[15] = /* Object ID is 1.3.14.3.2.26 */
347 { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
348 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
349
350static gcry_md_oid_spec_t oid_spec_sha1[] =
351 {
352 /* iso.member-body.us.rsadsi.pkcs.pkcs-1.5 (sha1WithRSAEncryption) */
353 { "1.2.840.113549.1.1.5" },
354 /* iso.member-body.us.x9-57.x9cm.3 (dsaWithSha1)*/
355 { "1.2.840.10040.4.3" },
356 /* from NIST's OIW (sha1) */
357 { "1.3.14.3.2.26" },
358 /* from NIST OIW (sha-1WithRSAEncryption) */
359 { "1.3.14.3.2.29" },
360 { NULL },
361 };
362
363gcry_md_spec_t _gcry_digest_spec_sha1 =
364 {
365 "SHA1", asn, DIM (asn), oid_spec_sha1, 20,
366 sha1_init, sha1_write, sha1_final, sha1_read,
367 sizeof (SHA1_CONTEXT)
368 };