4
4
GRUB_MOD_LICENSE ("GPLv3+");
5
5
/* sha512.c - SHA384 and SHA512 hash functions
6
* Copyright (C) 2003, 2008 Free Software Foundation, Inc.
6
* Copyright (C) 2003, 2008, 2009 Free Software Foundation, Inc.
8
8
* This file is part of Libgcrypt.
106
return ((x >> n) | (x << (64 - n)));
110
Ch (u64 x, u64 y, u64 z)
112
return ((x & y) ^ ( ~x & z));
116
Maj (u64 x, u64 y, u64 z)
118
return ((x & y) ^ (x & z) ^ (y & z));
124
return (ROTR (x, 28) ^ ROTR (x, 34) ^ ROTR (x, 39));
130
return (ROTR (x, 14) ^ ROTR (x, 18) ^ ROTR (x, 41));
103
133
/****************
104
134
* Transform the message W which consists of 16 64-bit-words
187
#define ROTR(x,n) (((x)>>(n)) | ((x)<<(64-(n))))
188
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
189
#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
190
#define Sum0(x) (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
191
#define Sum1(x) (ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41))
192
217
#define S0(x) (ROTR((x),1) ^ ROTR((x),8) ^ ((x)>>7))
193
218
#define S1(x) (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6))
195
220
for (t = 16; t < 80; t++)
196
221
w[t] = S1 (w[t - 2]) + w[t - 7] + S0 (w[t - 15]) + w[t - 16];
198
for (t = 0; t < 80; t++)
224
for (t = 0; t < 80; )
228
/* Performance on a AMD Athlon(tm) Dual Core Processor 4050e
229
with gcc 4.3.3 using gcry_md_hash_buffer of each 10000 bytes
230
initialized to 0,1,2,3...255,0,... and 1000 iterations:
232
Not unrolled with macros: 440ms
233
Unrolled with macros: 350ms
234
Unrolled with inline: 330ms
236
#if 0 /* Not unrolled. */
202
237
t1 = h + Sum1 (e) + Ch (e, f, g) + k[t] + w[t];
203
238
t2 = Sum0 (a) + Maj (a, b, c);
213
/* printf("t=%d a=%016llX b=%016llX c=%016llX d=%016llX "
214
"e=%016llX f=%016llX g=%016llX h=%016llX\n",t,a,b,c,d,e,f,g,h); */
248
#else /* Unrolled to interweave the chain variables. */
249
t1 = h + Sum1 (e) + Ch (e, f, g) + k[t] + w[t];
250
t2 = Sum0 (a) + Maj (a, b, c);
254
t1 = g + Sum1 (d) + Ch (d, e, f) + k[t+1] + w[t+1];
255
t2 = Sum0 (h) + Maj (h, a, b);
259
t1 = f + Sum1 (c) + Ch (c, d, e) + k[t+2] + w[t+2];
260
t2 = Sum0 (g) + Maj (g, h, a);
264
t1 = e + Sum1 (b) + Ch (b, c, d) + k[t+3] + w[t+3];
265
t2 = Sum0 (f) + Maj (f, g, h);
269
t1 = d + Sum1 (a) + Ch (a, b, c) + k[t+4] + w[t+4];
270
t2 = Sum0 (e) + Maj (e, f, g);
274
t1 = c + Sum1 (h) + Ch (h, a, b) + k[t+5] + w[t+5];
275
t2 = Sum0 (d) + Maj (d, e, f);
279
t1 = b + Sum1 (g) + Ch (g, h, a) + k[t+6] + w[t+6];
280
t2 = Sum0 (c) + Maj (c, d, e);
284
t1 = a + Sum1 (f) + Ch (f, g, h) + k[t+7] + w[t+7];
285
t2 = Sum0 (b) + Maj (b, c, d);
217
/* update chaining vars */
293
/* Update chaining vars. */
397
gcry_md_spec_t _gcry_digest_spec_sha512 =
473
gcry_md_spec_t _gcry_digest_spec_sha512 =
399
475
"SHA512", sha512_asn, DIM (sha512_asn), oid_spec_sha512, 64,
400
476
sha512_init, sha512_write, sha512_final, sha512_read,
415
491
static gcry_md_oid_spec_t oid_spec_sha384[] =
417
{ "2.16.840.1.101.3.4.2.2" },
493
{ "2.16.840.1.101.3.4.2.2" },
419
495
/* PKCS#1 sha384WithRSAEncryption */
420
496
{ "1.2.840.113549.1.1.12" },
425
gcry_md_spec_t _gcry_digest_spec_sha384 =
501
gcry_md_spec_t _gcry_digest_spec_sha384 =
427
503
"SHA384", sha384_asn, DIM (sha384_asn), oid_spec_sha384, 48,
428
504
sha384_init, sha512_write, sha512_final, sha512_read,
437
513
GRUB_MOD_INIT(gcry_sha512)
515
COMPILE_TIME_ASSERT(sizeof (SHA512_CONTEXT) <= GRUB_CRYPTO_MAX_MD_CONTEXT_SIZE);
516
COMPILE_TIME_ASSERT(sizeof (SHA512_CONTEXT) <= GRUB_CRYPTO_MAX_MD_CONTEXT_SIZE);
439
517
grub_md_register (&_gcry_digest_spec_sha512);
440
518
grub_md_register (&_gcry_digest_spec_sha384);