diff options
| author | Gary Ching-Pang Lin <glin@suse.com> | 2015-07-28 11:46:38 -0400 |
|---|---|---|
| committer | Peter Jones <pjones@redhat.com> | 2015-07-28 11:46:38 -0400 |
| commit | 5ce38c90cf43ee79cd999716ea83a5a44eeb819e (patch) | |
| tree | 2fb3d9dd667c772fae5f87fa61e1501cf12da0ce /Cryptlib/OpenSSL/crypto/sha/sha512.c | |
| parent | 69ba24ff72921ecabbb47178de40dc5a79350040 (diff) | |
| download | efi-boot-shim-5ce38c90cf43ee79cd999716ea83a5a44eeb819e.tar.gz efi-boot-shim-5ce38c90cf43ee79cd999716ea83a5a44eeb819e.zip | |
Update openssl to 1.0.2d
Also update Cryptlib to edk2 r17731
Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
Diffstat (limited to 'Cryptlib/OpenSSL/crypto/sha/sha512.c')
| -rw-r--r-- | Cryptlib/OpenSSL/crypto/sha/sha512.c | 173 |
1 files changed, 118 insertions, 55 deletions
diff --git a/Cryptlib/OpenSSL/crypto/sha/sha512.c b/Cryptlib/OpenSSL/crypto/sha/sha512.c index abcbe53f..3bf66ae1 100644 --- a/Cryptlib/OpenSSL/crypto/sha/sha512.c +++ b/Cryptlib/OpenSSL/crypto/sha/sha512.c @@ -5,10 +5,6 @@ * ==================================================================== */ #include <openssl/opensslconf.h> -#ifdef OPENSSL_FIPS -# include <openssl/fips.h> -#endif - #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA512) /*- * IMPLEMENTATION NOTES. @@ -59,15 +55,13 @@ const char SHA512_version[] = "SHA-512" OPENSSL_VERSION_PTEXT; # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64) || \ defined(__s390__) || defined(__s390x__) || \ + defined(__aarch64__) || \ defined(SHA512_ASM) # define SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA # endif -int SHA384_Init(SHA512_CTX *c) +fips_md_init_ctx(SHA384, SHA512) { -# ifdef OPENSSL_FIPS - FIPS_selftest_check(); -# endif c->h[0] = U64(0xcbbb9d5dc1059ed8); c->h[1] = U64(0x629a292a367cd507); c->h[2] = U64(0x9159015a3070dd17); @@ -76,6 +70,7 @@ int SHA384_Init(SHA512_CTX *c) c->h[5] = U64(0x8eb44a8768581511); c->h[6] = U64(0xdb0c2e0d64f98fa7); c->h[7] = U64(0x47b5481dbefa4fa4); + c->Nl = 0; c->Nh = 0; c->num = 0; @@ -83,11 +78,8 @@ int SHA384_Init(SHA512_CTX *c) return 1; } -int SHA512_Init(SHA512_CTX *c) +fips_md_init(SHA512) { -# ifdef OPENSSL_FIPS - FIPS_selftest_check(); -# endif c->h[0] = U64(0x6a09e667f3bcc908); c->h[1] = U64(0xbb67ae8584caa73b); c->h[2] = U64(0x3c6ef372fe94f82b); @@ -96,6 +88,7 @@ int SHA512_Init(SHA512_CTX *c) c->h[5] = U64(0x9b05688c2b3e6c1f); c->h[6] = U64(0x1f83d9abfb41bd6b); c->h[7] = U64(0x5be0cd19137e2179); + c->Nl = 0; c->Nh = 0; c->num = 0; @@ -210,7 +203,7 @@ int SHA512_Update(SHA512_CTX *c, const void *_data, size_t len) size_t n = sizeof(c->u) - c->num; if (len < n) { - memcpy(p + c->num, data, len), c->num += len; + memcpy(p + c->num, data, len), c->num += (unsigned int)len; return 1; } else { memcpy(p + c->num, data, n), c->num = 0; @@ -245,6 +238,10 @@ int SHA384_Update(SHA512_CTX *c, const void *data, size_t len) void SHA512_Transform(SHA512_CTX *c, const unsigned char *data) { +# ifndef SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA + if ((size_t)data % sizeof(c->u.d[0]) != 0) + memcpy(c->u.p, data, sizeof(c->u.p)), data = c->u.p; +# endif sha512_block_data_order(c, data, 1); } @@ -323,7 +320,7 @@ static const SHA_LONG64 K512[80] = { # ifndef PEDANTIC # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) # if defined(__x86_64) || defined(__x86_64__) -# define ROTR(a,n) ({ unsigned long ret; \ +# define ROTR(a,n) ({ SHA_LONG64 ret; \ asm ("rorq %1,%0" \ : "=r"(ret) \ : "J"(n),"0"(a) \ @@ -346,20 +343,33 @@ static const SHA_LONG64 K512[80] = { ((SHA_LONG64)hi)<<32|lo; }) # else # define PULL64(x) ({ const unsigned int *p=(const unsigned int *)(&(x));\ - unsigned int hi=p[0],lo=p[1]; \ + unsigned int hi=p[0],lo=p[1]; \ asm ("bswapl %0; bswapl %1;" \ : "=r"(lo),"=r"(hi) \ : "0"(lo),"1"(hi)); \ ((SHA_LONG64)hi)<<32|lo; }) # endif # elif (defined(_ARCH_PPC) && defined(__64BIT__)) || defined(_ARCH_PPC64) -# define ROTR(a,n) ({ unsigned long ret; \ +# define ROTR(a,n) ({ SHA_LONG64 ret; \ asm ("rotrdi %0,%1,%2" \ : "=r"(ret) \ : "r"(a),"K"(n)); ret; }) +# elif defined(__aarch64__) +# define ROTR(a,n) ({ SHA_LONG64 ret; \ + asm ("ror %0,%1,%2" \ + : "=r"(ret) \ + : "r"(a),"I"(n)); ret; }) +# if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \ + __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__ +# define PULL64(x) ({ SHA_LONG64 ret; \ + asm ("rev %0,%1" \ + : "=r"(ret) \ + : "r"(*((const SHA_LONG64 *)(&(x))))); ret; }) +# endif # endif # elif defined(_MSC_VER) # if defined(_WIN64) /* applies to both IA-64 and AMD64 */ +# pragma intrinsic(_rotr64) # define ROTR(a,n) _rotr64((a),n) # endif # if defined(_M_IX86) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) @@ -398,14 +408,71 @@ _asm bswap edx _asm bswap eax} # define sigma1(x) (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6)) # define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) # define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) -# if defined(OPENSSL_IA32_SSE2) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY) -# define GO_FOR_SSE2(ctx,in,num) do { \ - void sha512_block_sse2(void *,const void *,size_t); \ - if (!(OPENSSL_ia32cap_P & (1<<26))) break; \ - sha512_block_sse2(ctx->h,in,num); return; \ - } while (0) -# endif -# ifdef OPENSSL_SMALL_FOOTPRINT +# if defined(__i386) || defined(__i386__) || defined(_M_IX86) +/* + * This code should give better results on 32-bit CPU with less than + * ~24 registers, both size and performance wise... + */ static void sha512_block_data_order(SHA512_CTX *ctx, const void *in, + size_t num) +{ + const SHA_LONG64 *W = in; + SHA_LONG64 A, E, T; + SHA_LONG64 X[9 + 80], *F; + int i; + + while (num--) { + + F = X + 80; + A = ctx->h[0]; + F[1] = ctx->h[1]; + F[2] = ctx->h[2]; + F[3] = ctx->h[3]; + E = ctx->h[4]; + F[5] = ctx->h[5]; + F[6] = ctx->h[6]; + F[7] = ctx->h[7]; + + for (i = 0; i < 16; i++, F--) { +# ifdef B_ENDIAN + T = W[i]; +# else + T = PULL64(W[i]); +# endif + F[0] = A; + F[4] = E; + F[8] = T; + T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i]; + E = F[3] + T; + A = T + Sigma0(A) + Maj(A, F[1], F[2]); + } + + for (; i < 80; i++, F--) { + T = sigma0(F[8 + 16 - 1]); + T += sigma1(F[8 + 16 - 14]); + T += F[8 + 16] + F[8 + 16 - 9]; + + F[0] = A; + F[4] = E; + F[8] = T; + T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i]; + E = F[3] + T; + A = T + Sigma0(A) + Maj(A, F[1], F[2]); + } + + ctx->h[0] += A; + ctx->h[1] += F[1]; + ctx->h[2] += F[2]; + ctx->h[3] += F[3]; + ctx->h[4] += E; + ctx->h[5] += F[5]; + ctx->h[6] += F[6]; + ctx->h[7] += F[7]; + + W += SHA_LBLOCK; + } +} + +# elif defined(OPENSSL_SMALL_FOOTPRINT) static void sha512_block_data_order(SHA512_CTX *ctx, const void *in, size_t num) { @@ -414,10 +481,6 @@ static void sha512_block_data_order(SHA512_CTX *ctx, const void *in, SHA_LONG64 X[16]; int i; -# ifdef GO_FOR_SSE2 - GO_FOR_SSE2(ctx, in, num); -# endif - while (num--) { a = ctx->h[0]; @@ -484,11 +547,11 @@ static void sha512_block_data_order(SHA512_CTX *ctx, const void *in, T1 += h + Sigma1(e) + Ch(e,f,g) + K512[i]; \ h = Sigma0(a) + Maj(a,b,c); \ d += T1; h += T1; } while (0) -# define ROUND_16_80(i,a,b,c,d,e,f,g,h,X) do { \ - s0 = X[(i+1)&0x0f]; s0 = sigma0(s0); \ - s1 = X[(i+14)&0x0f]; s1 = sigma1(s1); \ - T1 = X[(i)&0x0f] += s0 + s1 + X[(i+9)&0x0f]; \ - ROUND_00_15(i,a,b,c,d,e,f,g,h); } while (0) +# define ROUND_16_80(i,j,a,b,c,d,e,f,g,h,X) do { \ + s0 = X[(j+1)&0x0f]; s0 = sigma0(s0); \ + s1 = X[(j+14)&0x0f]; s1 = sigma1(s1); \ + T1 = X[(j)&0x0f] += s0 + s1 + X[(j+9)&0x0f]; \ + ROUND_00_15(i+j,a,b,c,d,e,f,g,h); } while (0) static void sha512_block_data_order(SHA512_CTX *ctx, const void *in, size_t num) { @@ -497,10 +560,6 @@ static void sha512_block_data_order(SHA512_CTX *ctx, const void *in, SHA_LONG64 X[16]; int i; -# ifdef GO_FOR_SSE2 - GO_FOR_SSE2(ctx, in, num); -# endif - while (num--) { a = ctx->h[0]; @@ -580,15 +639,23 @@ static void sha512_block_data_order(SHA512_CTX *ctx, const void *in, ROUND_00_15(15, b, c, d, e, f, g, h, a); # endif - for (i = 16; i < 80; i += 8) { - ROUND_16_80(i + 0, a, b, c, d, e, f, g, h, X); - ROUND_16_80(i + 1, h, a, b, c, d, e, f, g, X); - ROUND_16_80(i + 2, g, h, a, b, c, d, e, f, X); - ROUND_16_80(i + 3, f, g, h, a, b, c, d, e, X); - ROUND_16_80(i + 4, e, f, g, h, a, b, c, d, X); - ROUND_16_80(i + 5, d, e, f, g, h, a, b, c, X); - ROUND_16_80(i + 6, c, d, e, f, g, h, a, b, X); - ROUND_16_80(i + 7, b, c, d, e, f, g, h, a, X); + for (i = 16; i < 80; i += 16) { + ROUND_16_80(i, 0, a, b, c, d, e, f, g, h, X); + ROUND_16_80(i, 1, h, a, b, c, d, e, f, g, X); + ROUND_16_80(i, 2, g, h, a, b, c, d, e, f, X); + ROUND_16_80(i, 3, f, g, h, a, b, c, d, e, X); + ROUND_16_80(i, 4, e, f, g, h, a, b, c, d, X); + ROUND_16_80(i, 5, d, e, f, g, h, a, b, c, X); + ROUND_16_80(i, 6, c, d, e, f, g, h, a, b, X); + ROUND_16_80(i, 7, b, c, d, e, f, g, h, a, X); + ROUND_16_80(i, 8, a, b, c, d, e, f, g, h, X); + ROUND_16_80(i, 9, h, a, b, c, d, e, f, g, X); + ROUND_16_80(i, 10, g, h, a, b, c, d, e, f, X); + ROUND_16_80(i, 11, f, g, h, a, b, c, d, e, X); + ROUND_16_80(i, 12, e, f, g, h, a, b, c, d, X); + ROUND_16_80(i, 13, d, e, f, g, h, a, b, c, X); + ROUND_16_80(i, 14, c, d, e, f, g, h, a, b, X); + ROUND_16_80(i, 15, b, c, d, e, f, g, h, a, X); } ctx->h[0] += a; @@ -608,14 +675,10 @@ static void sha512_block_data_order(SHA512_CTX *ctx, const void *in, # endif /* SHA512_ASM */ -#else /* OPENSSL_NO_SHA512 */ +#else /* !OPENSSL_NO_SHA512 */ -/* - * Sensitive compilers ("Compaq C V6.4-005 on OpenVMS VAX V7.3", for example) - * dislike a statement-free file, complaining: "%CC-W-EMPTYFILE, Source file - * does not contain any declarations." - */ - -int sha512_dummy(); +# if defined(PEDANTIC) || defined(__DECC) || defined(OPENSSL_SYS_MACOSX) +static void *dummy = &dummy; +# endif -#endif /* OPENSSL_NO_SHA512 */ +#endif /* !OPENSSL_NO_SHA512 */ |
