GRUB-1.98 changes
[grub2/jjazz.git] / lib / libgcrypt-grub / cipher / sha1.c
blob36d902f41e80aa95eeb6a3b08174842f97c4ec7f
1 /* This file was automatically imported with
2 import_gcry.py. Please don't modify it */
3 /* sha1.c - SHA1 hash function
4 * Copyright (C) 1998, 2001, 2002, 2003, 2008 Free Software Foundation, Inc.
6 * This file is part of Libgcrypt.
8 * Libgcrypt is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * Libgcrypt is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
23 /* Test vectors:
25 * "abc"
26 * A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
28 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
29 * 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
33 #ifdef HAVE_STDINT_H
34 # include <stdint.h>
35 #endif
37 #include "g10lib.h"
38 #include "memory.h"
39 #include "bithelp.h"
40 #include "cipher.h"
41 #include "hash-common.h"
44 /* A macro to test whether P is properly aligned for an u32 type.
45 Note that config.h provides a suitable replacement for uintptr_t if
46 it does not exist in stdint.h. */
47 /* #if __GNUC__ >= 2 */
48 /* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % __alignof__ (u32))) */
49 /* #else */
50 /* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % sizeof (u32))) */
51 /* #endif */
53 #define TRANSFORM(x,d,n) transform ((x), (d), (n))
56 typedef struct
58 u32 h0,h1,h2,h3,h4;
59 u32 nblocks;
60 unsigned char buf[64];
61 int count;
62 } SHA1_CONTEXT;
66 static void
67 sha1_init (void *context)
69 SHA1_CONTEXT *hd = context;
71 hd->h0 = 0x67452301;
72 hd->h1 = 0xefcdab89;
73 hd->h2 = 0x98badcfe;
74 hd->h3 = 0x10325476;
75 hd->h4 = 0xc3d2e1f0;
76 hd->nblocks = 0;
77 hd->count = 0;
81 /* Round function macros. */
82 #define K1 0x5A827999L
83 #define K2 0x6ED9EBA1L
84 #define K3 0x8F1BBCDCL
85 #define K4 0xCA62C1D6L
86 #define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
87 #define F2(x,y,z) ( x ^ y ^ z )
88 #define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
89 #define F4(x,y,z) ( x ^ y ^ z )
90 #define M(i) ( tm = x[ i &0x0f] \
91 ^ x[(i-14)&0x0f] \
92 ^ x[(i-8) &0x0f] \
93 ^ x[(i-3) &0x0f], \
94 (x[i&0x0f] = rol(tm, 1)))
95 #define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
96 + f( b, c, d ) \
97 + k \
98 + m; \
99 b = rol( b, 30 ); \
100 } while(0)
104 * Transform NBLOCKS of each 64 bytes (16 32-bit words) at DATA.
106 static void
107 transform (SHA1_CONTEXT *hd, const unsigned char *data, size_t nblocks)
109 register u32 a, b, c, d, e; /* Local copies of the chaining variables. */
110 register u32 tm; /* Helper. */
111 u32 x[16]; /* The array we work on. */
113 /* Loop over all blocks. */
114 for ( ;nblocks; nblocks--)
116 #ifdef WORDS_BIGENDIAN
117 memcpy (x, data, 64);
118 data += 64;
119 #else
121 int i;
122 unsigned char *p;
124 for(i=0, p=(unsigned char*)x; i < 16; i++, p += 4 )
126 p[3] = *data++;
127 p[2] = *data++;
128 p[1] = *data++;
129 p[0] = *data++;
132 #endif
133 /* Get the values of the chaining variables. */
134 a = hd->h0;
135 b = hd->h1;
136 c = hd->h2;
137 d = hd->h3;
138 e = hd->h4;
140 /* Transform. */
141 R( a, b, c, d, e, F1, K1, x[ 0] );
142 R( e, a, b, c, d, F1, K1, x[ 1] );
143 R( d, e, a, b, c, F1, K1, x[ 2] );
144 R( c, d, e, a, b, F1, K1, x[ 3] );
145 R( b, c, d, e, a, F1, K1, x[ 4] );
146 R( a, b, c, d, e, F1, K1, x[ 5] );
147 R( e, a, b, c, d, F1, K1, x[ 6] );
148 R( d, e, a, b, c, F1, K1, x[ 7] );
149 R( c, d, e, a, b, F1, K1, x[ 8] );
150 R( b, c, d, e, a, F1, K1, x[ 9] );
151 R( a, b, c, d, e, F1, K1, x[10] );
152 R( e, a, b, c, d, F1, K1, x[11] );
153 R( d, e, a, b, c, F1, K1, x[12] );
154 R( c, d, e, a, b, F1, K1, x[13] );
155 R( b, c, d, e, a, F1, K1, x[14] );
156 R( a, b, c, d, e, F1, K1, x[15] );
157 R( e, a, b, c, d, F1, K1, M(16) );
158 R( d, e, a, b, c, F1, K1, M(17) );
159 R( c, d, e, a, b, F1, K1, M(18) );
160 R( b, c, d, e, a, F1, K1, M(19) );
161 R( a, b, c, d, e, F2, K2, M(20) );
162 R( e, a, b, c, d, F2, K2, M(21) );
163 R( d, e, a, b, c, F2, K2, M(22) );
164 R( c, d, e, a, b, F2, K2, M(23) );
165 R( b, c, d, e, a, F2, K2, M(24) );
166 R( a, b, c, d, e, F2, K2, M(25) );
167 R( e, a, b, c, d, F2, K2, M(26) );
168 R( d, e, a, b, c, F2, K2, M(27) );
169 R( c, d, e, a, b, F2, K2, M(28) );
170 R( b, c, d, e, a, F2, K2, M(29) );
171 R( a, b, c, d, e, F2, K2, M(30) );
172 R( e, a, b, c, d, F2, K2, M(31) );
173 R( d, e, a, b, c, F2, K2, M(32) );
174 R( c, d, e, a, b, F2, K2, M(33) );
175 R( b, c, d, e, a, F2, K2, M(34) );
176 R( a, b, c, d, e, F2, K2, M(35) );
177 R( e, a, b, c, d, F2, K2, M(36) );
178 R( d, e, a, b, c, F2, K2, M(37) );
179 R( c, d, e, a, b, F2, K2, M(38) );
180 R( b, c, d, e, a, F2, K2, M(39) );
181 R( a, b, c, d, e, F3, K3, M(40) );
182 R( e, a, b, c, d, F3, K3, M(41) );
183 R( d, e, a, b, c, F3, K3, M(42) );
184 R( c, d, e, a, b, F3, K3, M(43) );
185 R( b, c, d, e, a, F3, K3, M(44) );
186 R( a, b, c, d, e, F3, K3, M(45) );
187 R( e, a, b, c, d, F3, K3, M(46) );
188 R( d, e, a, b, c, F3, K3, M(47) );
189 R( c, d, e, a, b, F3, K3, M(48) );
190 R( b, c, d, e, a, F3, K3, M(49) );
191 R( a, b, c, d, e, F3, K3, M(50) );
192 R( e, a, b, c, d, F3, K3, M(51) );
193 R( d, e, a, b, c, F3, K3, M(52) );
194 R( c, d, e, a, b, F3, K3, M(53) );
195 R( b, c, d, e, a, F3, K3, M(54) );
196 R( a, b, c, d, e, F3, K3, M(55) );
197 R( e, a, b, c, d, F3, K3, M(56) );
198 R( d, e, a, b, c, F3, K3, M(57) );
199 R( c, d, e, a, b, F3, K3, M(58) );
200 R( b, c, d, e, a, F3, K3, M(59) );
201 R( a, b, c, d, e, F4, K4, M(60) );
202 R( e, a, b, c, d, F4, K4, M(61) );
203 R( d, e, a, b, c, F4, K4, M(62) );
204 R( c, d, e, a, b, F4, K4, M(63) );
205 R( b, c, d, e, a, F4, K4, M(64) );
206 R( a, b, c, d, e, F4, K4, M(65) );
207 R( e, a, b, c, d, F4, K4, M(66) );
208 R( d, e, a, b, c, F4, K4, M(67) );
209 R( c, d, e, a, b, F4, K4, M(68) );
210 R( b, c, d, e, a, F4, K4, M(69) );
211 R( a, b, c, d, e, F4, K4, M(70) );
212 R( e, a, b, c, d, F4, K4, M(71) );
213 R( d, e, a, b, c, F4, K4, M(72) );
214 R( c, d, e, a, b, F4, K4, M(73) );
215 R( b, c, d, e, a, F4, K4, M(74) );
216 R( a, b, c, d, e, F4, K4, M(75) );
217 R( e, a, b, c, d, F4, K4, M(76) );
218 R( d, e, a, b, c, F4, K4, M(77) );
219 R( c, d, e, a, b, F4, K4, M(78) );
220 R( b, c, d, e, a, F4, K4, M(79) );
222 /* Update the chaining variables. */
223 hd->h0 += a;
224 hd->h1 += b;
225 hd->h2 += c;
226 hd->h3 += d;
227 hd->h4 += e;
232 /* Update the message digest with the contents
233 * of INBUF with length INLEN.
235 static void
236 sha1_write( void *context, const void *inbuf_arg, size_t inlen)
238 const unsigned char *inbuf = inbuf_arg;
239 SHA1_CONTEXT *hd = context;
240 size_t nblocks;
242 if (hd->count == 64) /* Flush the buffer. */
244 TRANSFORM( hd, hd->buf, 1 );
245 _gcry_burn_stack (88+4*sizeof(void*));
246 hd->count = 0;
247 hd->nblocks++;
249 if (!inbuf)
250 return;
252 if (hd->count)
254 for (; inlen && hd->count < 64; inlen--)
255 hd->buf[hd->count++] = *inbuf++;
256 sha1_write (hd, NULL, 0);
257 if (!inlen)
258 return;
261 nblocks = inlen / 64;
262 if (nblocks)
264 TRANSFORM (hd, inbuf, nblocks);
265 hd->count = 0;
266 hd->nblocks += nblocks;
267 inlen -= nblocks * 64;
268 inbuf += nblocks * 64;
270 _gcry_burn_stack (88+4*sizeof(void*));
272 /* Save remaining bytes. */
273 for (; inlen && hd->count < 64; inlen--)
274 hd->buf[hd->count++] = *inbuf++;
278 /* The routine final terminates the computation and
279 * returns the digest.
280 * The handle is prepared for a new cycle, but adding bytes to the
281 * handle will the destroy the returned buffer.
282 * Returns: 20 bytes representing the digest.
285 static void
286 sha1_final(void *context)
288 SHA1_CONTEXT *hd = context;
290 u32 t, msb, lsb;
291 unsigned char *p;
293 sha1_write(hd, NULL, 0); /* flush */;
295 t = hd->nblocks;
296 /* multiply by 64 to make a byte count */
297 lsb = t << 6;
298 msb = t >> 26;
299 /* add the count */
300 t = lsb;
301 if( (lsb += hd->count) < t )
302 msb++;
303 /* multiply by 8 to make a bit count */
304 t = lsb;
305 lsb <<= 3;
306 msb <<= 3;
307 msb |= t >> 29;
309 if( hd->count < 56 ) /* enough room */
311 hd->buf[hd->count++] = 0x80; /* pad */
312 while( hd->count < 56 )
313 hd->buf[hd->count++] = 0; /* pad */
315 else /* need one extra block */
317 hd->buf[hd->count++] = 0x80; /* pad character */
318 while( hd->count < 64 )
319 hd->buf[hd->count++] = 0;
320 sha1_write(hd, NULL, 0); /* flush */;
321 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
323 /* append the 64 bit count */
324 hd->buf[56] = msb >> 24;
325 hd->buf[57] = msb >> 16;
326 hd->buf[58] = msb >> 8;
327 hd->buf[59] = msb ;
328 hd->buf[60] = lsb >> 24;
329 hd->buf[61] = lsb >> 16;
330 hd->buf[62] = lsb >> 8;
331 hd->buf[63] = lsb ;
332 TRANSFORM( hd, hd->buf, 1 );
333 _gcry_burn_stack (88+4*sizeof(void*));
335 p = hd->buf;
336 #ifdef WORDS_BIGENDIAN
337 #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
338 #else /* little endian */
339 #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; \
340 *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
341 #endif
342 X(0);
343 X(1);
344 X(2);
345 X(3);
346 X(4);
347 #undef X
351 static unsigned char *
352 sha1_read( void *context )
354 SHA1_CONTEXT *hd = context;
356 return hd->buf;
359 /****************
360 * Shortcut functions which puts the hash value of the supplied buffer
361 * into outbuf which must have a size of 20 bytes.
367 Self-test section.
373 /* Run a full self-test for ALGO and return 0 on success. */
378 static unsigned char asn[15] = /* Object ID is 1.3.14.3.2.26 */
379 { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
380 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
382 static gcry_md_oid_spec_t oid_spec_sha1[] =
384 /* iso.member-body.us.rsadsi.pkcs.pkcs-1.5 (sha1WithRSAEncryption) */
385 { "1.2.840.113549.1.1.5" },
386 /* iso.member-body.us.x9-57.x9cm.3 (dsaWithSha1)*/
387 { "1.2.840.10040.4.3" },
388 /* from NIST's OIW (sha1) */
389 { "1.3.14.3.2.26" },
390 /* from NIST OIW (sha-1WithRSAEncryption) */
391 { "1.3.14.3.2.29" },
392 /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha1 */
393 { "1.2.840.10045.4.1" },
394 { NULL },
397 gcry_md_spec_t _gcry_digest_spec_sha1 =
399 "SHA1", asn, DIM (asn), oid_spec_sha1, 20,
400 sha1_init, sha1_write, sha1_final, sha1_read,
401 sizeof (SHA1_CONTEXT)
403 .blocksize = 64
408 GRUB_MOD_INIT(gcry_sha1)
410 grub_md_register (&_gcry_digest_spec_sha1);
413 GRUB_MOD_FINI(gcry_sha1)
415 grub_md_unregister (&_gcry_digest_spec_sha1);