1
/* sha1.c - SHA1 hash function
2
* Copyright (C) 1998, 2001, 2002, 2003, 2008 Free Software Foundation, Inc.
4
* This file is part of Libgcrypt.
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.
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.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
24
* A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
26
* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
27
* 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
43
#include "hash-common.h"
46
/* A macro to test whether P is properly aligned for an u32 type.
47
Note that config.h provides a suitable replacement for uintptr_t if
48
it does not exist in stdint.h. */
49
/* #if __GNUC__ >= 2 */
50
/* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % __alignof__ (u32))) */
52
/* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % sizeof (u32))) */
55
#define TRANSFORM(x,d,n) transform ((x), (d), (n))
62
unsigned char buf[64];
69
sha1_init (void *context)
71
SHA1_CONTEXT *hd = context;
83
/* Round function macros. */
84
#define K1 0x5A827999L
85
#define K2 0x6ED9EBA1L
86
#define K3 0x8F1BBCDCL
87
#define K4 0xCA62C1D6L
88
#define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
89
#define F2(x,y,z) ( x ^ y ^ z )
90
#define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
91
#define F4(x,y,z) ( x ^ y ^ z )
92
#define M(i) ( tm = x[ i &0x0f] \
96
(x[i&0x0f] = rol(tm, 1)))
97
#define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
106
* Transform NBLOCKS of each 64 bytes (16 32-bit words) at DATA.
109
transform (SHA1_CONTEXT *hd, const unsigned char *data, size_t nblocks)
111
register u32 a, b, c, d, e; /* Local copies of the chaining variables. */
112
register u32 tm; /* Helper. */
113
u32 x[16]; /* The array we work on. */
115
/* Loop over all blocks. */
116
for ( ;nblocks; nblocks--)
118
#ifdef WORDS_BIGENDIAN
119
memcpy (x, data, 64);
126
for(i=0, p=(unsigned char*)x; i < 16; i++, p += 4 )
135
/* Get the values of the chaining variables. */
143
R( a, b, c, d, e, F1, K1, x[ 0] );
144
R( e, a, b, c, d, F1, K1, x[ 1] );
145
R( d, e, a, b, c, F1, K1, x[ 2] );
146
R( c, d, e, a, b, F1, K1, x[ 3] );
147
R( b, c, d, e, a, F1, K1, x[ 4] );
148
R( a, b, c, d, e, F1, K1, x[ 5] );
149
R( e, a, b, c, d, F1, K1, x[ 6] );
150
R( d, e, a, b, c, F1, K1, x[ 7] );
151
R( c, d, e, a, b, F1, K1, x[ 8] );
152
R( b, c, d, e, a, F1, K1, x[ 9] );
153
R( a, b, c, d, e, F1, K1, x[10] );
154
R( e, a, b, c, d, F1, K1, x[11] );
155
R( d, e, a, b, c, F1, K1, x[12] );
156
R( c, d, e, a, b, F1, K1, x[13] );
157
R( b, c, d, e, a, F1, K1, x[14] );
158
R( a, b, c, d, e, F1, K1, x[15] );
159
R( e, a, b, c, d, F1, K1, M(16) );
160
R( d, e, a, b, c, F1, K1, M(17) );
161
R( c, d, e, a, b, F1, K1, M(18) );
162
R( b, c, d, e, a, F1, K1, M(19) );
163
R( a, b, c, d, e, F2, K2, M(20) );
164
R( e, a, b, c, d, F2, K2, M(21) );
165
R( d, e, a, b, c, F2, K2, M(22) );
166
R( c, d, e, a, b, F2, K2, M(23) );
167
R( b, c, d, e, a, F2, K2, M(24) );
168
R( a, b, c, d, e, F2, K2, M(25) );
169
R( e, a, b, c, d, F2, K2, M(26) );
170
R( d, e, a, b, c, F2, K2, M(27) );
171
R( c, d, e, a, b, F2, K2, M(28) );
172
R( b, c, d, e, a, F2, K2, M(29) );
173
R( a, b, c, d, e, F2, K2, M(30) );
174
R( e, a, b, c, d, F2, K2, M(31) );
175
R( d, e, a, b, c, F2, K2, M(32) );
176
R( c, d, e, a, b, F2, K2, M(33) );
177
R( b, c, d, e, a, F2, K2, M(34) );
178
R( a, b, c, d, e, F2, K2, M(35) );
179
R( e, a, b, c, d, F2, K2, M(36) );
180
R( d, e, a, b, c, F2, K2, M(37) );
181
R( c, d, e, a, b, F2, K2, M(38) );
182
R( b, c, d, e, a, F2, K2, M(39) );
183
R( a, b, c, d, e, F3, K3, M(40) );
184
R( e, a, b, c, d, F3, K3, M(41) );
185
R( d, e, a, b, c, F3, K3, M(42) );
186
R( c, d, e, a, b, F3, K3, M(43) );
187
R( b, c, d, e, a, F3, K3, M(44) );
188
R( a, b, c, d, e, F3, K3, M(45) );
189
R( e, a, b, c, d, F3, K3, M(46) );
190
R( d, e, a, b, c, F3, K3, M(47) );
191
R( c, d, e, a, b, F3, K3, M(48) );
192
R( b, c, d, e, a, F3, K3, M(49) );
193
R( a, b, c, d, e, F3, K3, M(50) );
194
R( e, a, b, c, d, F3, K3, M(51) );
195
R( d, e, a, b, c, F3, K3, M(52) );
196
R( c, d, e, a, b, F3, K3, M(53) );
197
R( b, c, d, e, a, F3, K3, M(54) );
198
R( a, b, c, d, e, F3, K3, M(55) );
199
R( e, a, b, c, d, F3, K3, M(56) );
200
R( d, e, a, b, c, F3, K3, M(57) );
201
R( c, d, e, a, b, F3, K3, M(58) );
202
R( b, c, d, e, a, F3, K3, M(59) );
203
R( a, b, c, d, e, F4, K4, M(60) );
204
R( e, a, b, c, d, F4, K4, M(61) );
205
R( d, e, a, b, c, F4, K4, M(62) );
206
R( c, d, e, a, b, F4, K4, M(63) );
207
R( b, c, d, e, a, F4, K4, M(64) );
208
R( a, b, c, d, e, F4, K4, M(65) );
209
R( e, a, b, c, d, F4, K4, M(66) );
210
R( d, e, a, b, c, F4, K4, M(67) );
211
R( c, d, e, a, b, F4, K4, M(68) );
212
R( b, c, d, e, a, F4, K4, M(69) );
213
R( a, b, c, d, e, F4, K4, M(70) );
214
R( e, a, b, c, d, F4, K4, M(71) );
215
R( d, e, a, b, c, F4, K4, M(72) );
216
R( c, d, e, a, b, F4, K4, M(73) );
217
R( b, c, d, e, a, F4, K4, M(74) );
218
R( a, b, c, d, e, F4, K4, M(75) );
219
R( e, a, b, c, d, F4, K4, M(76) );
220
R( d, e, a, b, c, F4, K4, M(77) );
221
R( c, d, e, a, b, F4, K4, M(78) );
222
R( b, c, d, e, a, F4, K4, M(79) );
224
/* Update the chaining variables. */
234
/* Update the message digest with the contents
235
* of INBUF with length INLEN.
238
sha1_write( void *context, const void *inbuf_arg, size_t inlen)
240
const unsigned char *inbuf = inbuf_arg;
241
SHA1_CONTEXT *hd = context;
244
if (hd->count == 64) /* Flush the buffer. */
246
TRANSFORM( hd, hd->buf, 1 );
247
_gcry_burn_stack (88+4*sizeof(void*));
256
for (; inlen && hd->count < 64; inlen--)
257
hd->buf[hd->count++] = *inbuf++;
258
sha1_write (hd, NULL, 0);
263
nblocks = inlen / 64;
266
TRANSFORM (hd, inbuf, nblocks);
268
hd->nblocks += nblocks;
269
inlen -= nblocks * 64;
270
inbuf += nblocks * 64;
272
_gcry_burn_stack (88+4*sizeof(void*));
274
/* Save remaining bytes. */
275
for (; inlen && hd->count < 64; inlen--)
276
hd->buf[hd->count++] = *inbuf++;
280
/* The routine final terminates the computation and
281
* returns the digest.
282
* The handle is prepared for a new cycle, but adding bytes to the
283
* handle will the destroy the returned buffer.
284
* Returns: 20 bytes representing the digest.
288
sha1_final(void *context)
290
SHA1_CONTEXT *hd = context;
295
sha1_write(hd, NULL, 0); /* flush */;
298
/* multiply by 64 to make a byte count */
303
if( (lsb += hd->count) < t )
305
/* multiply by 8 to make a bit count */
311
if( hd->count < 56 ) /* enough room */
313
hd->buf[hd->count++] = 0x80; /* pad */
314
while( hd->count < 56 )
315
hd->buf[hd->count++] = 0; /* pad */
317
else /* need one extra block */
319
hd->buf[hd->count++] = 0x80; /* pad character */
320
while( hd->count < 64 )
321
hd->buf[hd->count++] = 0;
322
sha1_write(hd, NULL, 0); /* flush */;
323
memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
325
/* append the 64 bit count */
326
hd->buf[56] = msb >> 24;
327
hd->buf[57] = msb >> 16;
328
hd->buf[58] = msb >> 8;
330
hd->buf[60] = lsb >> 24;
331
hd->buf[61] = lsb >> 16;
332
hd->buf[62] = lsb >> 8;
334
TRANSFORM( hd, hd->buf, 1 );
335
_gcry_burn_stack (88+4*sizeof(void*));
338
#ifdef WORDS_BIGENDIAN
339
#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
340
#else /* little endian */
341
#define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
342
*p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
353
static unsigned char *
354
sha1_read( void *context )
356
SHA1_CONTEXT *hd = context;
362
* Shortcut functions which puts the hash value of the supplied buffer
363
* into outbuf which must have a size of 20 bytes.
366
_gcry_sha1_hash_buffer (void *outbuf, const void *buffer, size_t length)
371
sha1_write (&hd, buffer, length);
373
memcpy (outbuf, hd.buf, 20);
383
static gpg_err_code_t
384
selftests_sha1 (int extended, selftest_report_func_t report)
389
what = "short string";
390
errtxt = _gcry_hash_selftest_check_one
393
"\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
394
"\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
400
what = "long string";
401
errtxt = _gcry_hash_selftest_check_one
403
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
404
"\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE"
405
"\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1", 20);
409
what = "one million \"a\"";
410
errtxt = _gcry_hash_selftest_check_one
413
"\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E"
414
"\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F", 20);
419
return 0; /* Succeeded. */
423
report ("digest", GCRY_MD_SHA1, what, errtxt);
424
return GPG_ERR_SELFTEST_FAILED;
428
/* Run a full self-test for ALGO and return 0 on success. */
429
static gpg_err_code_t
430
run_selftests (int algo, int extended, selftest_report_func_t report)
437
ec = selftests_sha1 (extended, report);
440
ec = GPG_ERR_DIGEST_ALGO;
450
static unsigned char asn[15] = /* Object ID is 1.3.14.3.2.26 */
451
{ 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
452
0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
454
static gcry_md_oid_spec_t oid_spec_sha1[] =
456
/* iso.member-body.us.rsadsi.pkcs.pkcs-1.5 (sha1WithRSAEncryption) */
457
{ "1.2.840.113549.1.1.5" },
458
/* iso.member-body.us.x9-57.x9cm.3 (dsaWithSha1)*/
459
{ "1.2.840.10040.4.3" },
460
/* from NIST's OIW (sha1) */
462
/* from NIST OIW (sha-1WithRSAEncryption) */
464
/* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha1 */
465
{ "1.2.840.10045.4.1" },
469
gcry_md_spec_t _gcry_digest_spec_sha1 =
471
"SHA1", asn, DIM (asn), oid_spec_sha1, 20,
472
sha1_init, sha1_write, sha1_final, sha1_read,
473
sizeof (SHA1_CONTEXT)
475
md_extra_spec_t _gcry_digest_extraspec_sha1 =