From 6aa5fdf9527620c46220050ccb2d6fe0e9c113ab Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Wed, 28 Jun 2017 18:22:12 +0200 Subject: [PATCH] rsaenh: Use bcrypt for SHA512 hashes. Signed-off-by: Jacek Caban Signed-off-by: Alexandre Julliard --- dlls/rsaenh/Makefile.in | 2 +- dlls/rsaenh/implglue.c | 32 +++++++++++-------- dlls/rsaenh/implglue.h | 5 ++- dlls/rsaenh/rsaenh.c | 1 - dlls/rsaenh/sha2.c | 71 ----------------------------------------- dlls/rsaenh/sha2.h | 6 ---- 6 files changed, 24 insertions(+), 93 deletions(-) diff --git a/dlls/rsaenh/Makefile.in b/dlls/rsaenh/Makefile.in index 5b6e2feedb7..0fdae10065c 100644 --- a/dlls/rsaenh/Makefile.in +++ b/dlls/rsaenh/Makefile.in @@ -1,6 +1,6 @@ MODULE = rsaenh.dll IMPORTLIB = rsaenh -IMPORTS = crypt32 advapi32 +IMPORTS = bcrypt crypt32 advapi32 C_SRCS = \ aes.c \ diff --git a/dlls/rsaenh/implglue.c b/dlls/rsaenh/implglue.c index 77a0085f82e..20ebddca855 100644 --- a/dlls/rsaenh/implglue.c +++ b/dlls/rsaenh/implglue.c @@ -50,6 +50,8 @@ BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen); BOOL init_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext) { + const WCHAR *algid = NULL; + switch (aiAlgid) { case CALG_MD2: @@ -77,10 +79,22 @@ BOOL init_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext) break; case CALG_SHA_512: - SHA512_Init(&pHashContext->sha512); + algid = BCRYPT_SHA512_ALGORITHM; break; } + if (algid) + { + BCRYPT_ALG_HANDLE provider; + NTSTATUS status; + + status = BCryptOpenAlgorithmProvider(&provider, algid, MS_PRIMITIVE_PROVIDER, 0); + if (status) return FALSE; + + status = BCryptCreateHash(provider, &pHashContext->bcrypt_hash, NULL, 0, NULL, 0, 0); + BCryptCloseAlgorithmProvider(provider, 0); + if (status) return FALSE; + } return TRUE; } @@ -113,13 +127,8 @@ BOOL update_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, const BYTE *pb SHA384_Update(&pHashContext->sha384, pbData, dwDataLen); break; - case CALG_SHA_512: - SHA512_Update(&pHashContext->sha512, pbData, dwDataLen); - break; - default: - SetLastError(NTE_BAD_ALGID); - return FALSE; + BCryptHashData(pHashContext->bcrypt_hash, (UCHAR*)pbData, dwDataLen, 0); } return TRUE; @@ -155,13 +164,10 @@ BOOL finalize_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, BYTE *pbHash SHA384_Final(pbHashValue, &pHashContext->sha384); break; - case CALG_SHA_512: - SHA512_Final(pbHashValue, &pHashContext->sha512); - break; - default: - SetLastError(NTE_BAD_ALGID); - return FALSE; + BCryptFinishHash(pHashContext->bcrypt_hash, pbHashValue, RSAENH_MAX_HASH_SIZE, 0); + BCryptDestroyHash(pHashContext->bcrypt_hash); + break; } return TRUE; diff --git a/dlls/rsaenh/implglue.h b/dlls/rsaenh/implglue.h index 3ac7eef62a1..4f53101fa33 100644 --- a/dlls/rsaenh/implglue.h +++ b/dlls/rsaenh/implglue.h @@ -24,9 +24,12 @@ #ifndef __WINE_IMPLGLUE_H #define __WINE_IMPLGLUE_H +#include "bcrypt.h" #include "tomcrypt.h" #include "sha2.h" +#define RSAENH_MAX_HASH_SIZE 104 + /* Next typedef copied from dlls/advapi32/crypt_md4.c */ typedef struct tagMD4_CTX { unsigned int buf[4]; @@ -60,7 +63,7 @@ typedef union tagHASH_CONTEXT { SHA_CTX sha; SHA256_CTX sha256; SHA384_CTX sha384; - SHA512_CTX sha512; + BCRYPT_HASH_HANDLE bcrypt_hash; } HASH_CONTEXT; typedef union tagKEY_CONTEXT { diff --git a/dlls/rsaenh/rsaenh.c b/dlls/rsaenh/rsaenh.c index 9af770dfc6e..98bba16680c 100644 --- a/dlls/rsaenh/rsaenh.c +++ b/dlls/rsaenh/rsaenh.c @@ -48,7 +48,6 @@ static HINSTANCE instance; * CRYPTHASH - hash objects */ #define RSAENH_MAGIC_HASH 0x85938417u -#define RSAENH_MAX_HASH_SIZE 104 #define RSAENH_HASHSTATE_HASHING 1 #define RSAENH_HASHSTATE_FINISHED 2 typedef struct _RSAENH_TLS1PRF_PARAMS diff --git a/dlls/rsaenh/sha2.c b/dlls/rsaenh/sha2.c index 76e7184a0c2..67933ce743f 100644 --- a/dlls/rsaenh/sha2.c +++ b/dlls/rsaenh/sha2.c @@ -599,16 +599,6 @@ char* SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_S } -/*** SHA-512: *********************************************************/ -void SHA512_Init(SHA512_CTX* context) { - if (context == NULL) { - return; - } - MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH); - MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH); - context->bitcount[0] = context->bitcount[1] = 0; -} - #ifdef SHA2_UNROLL_TRANSFORM /* Unrolled SHA-512 round macros: */ @@ -867,67 +857,6 @@ void SHA512_Last(SHA512_CTX* context) { SHA512_Transform(context, (sha2_word64*)context->buffer); } -void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) { - sha2_word64 *d = (sha2_word64*)digest; - - /* Sanity check: */ - assert(context != NULL); - - /* If no digest buffer is passed, we don't bother doing this: */ - if (digest != NULL) { - SHA512_Last(context); - - /* Save the hash data for output: */ -#ifndef WORDS_BIGENDIAN - { - /* Convert TO host byte order */ - int j; - for (j = 0; j < 8; j++) { - REVERSE64(context->state[j],context->state[j]); - *d++ = context->state[j]; - } - } -#else - MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH); -#endif - } - - /* Zero out state data */ - MEMSET_BZERO(context, sizeof(*context)); -} - -char *SHA512_End(SHA512_CTX* context, char buffer[]) { - sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest; - int i; - - /* Sanity check: */ - assert(context != NULL); - - if (buffer != NULL) { - SHA512_Final(digest, context); - - for (i = 0; i < SHA512_DIGEST_LENGTH; i++) { - *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4]; - *buffer++ = sha2_hex_digits[*d & 0x0f]; - d++; - } - *buffer = 0; - } else { - MEMSET_BZERO(context, sizeof(*context)); - } - MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH); - return buffer; -} - -char* SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) { - SHA512_CTX context; - - SHA512_Init(&context); - SHA512_Update(&context, data, len); - return SHA512_End(&context, digest); -} - - /*** SHA-384: *********************************************************/ void SHA384_Init(SHA384_CTX* context) { if (context == NULL) { diff --git a/dlls/rsaenh/sha2.h b/dlls/rsaenh/sha2.h index 2dda41230c1..342e8389688 100644 --- a/dlls/rsaenh/sha2.h +++ b/dlls/rsaenh/sha2.h @@ -80,10 +80,4 @@ void SHA384_Final(sha2_byte[SHA384_DIGEST_LENGTH], SHA384_CTX*); char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); char* SHA384_Data(const sha2_byte*, size_t, char[SHA384_DIGEST_STRING_LENGTH]); -void SHA512_Init(SHA512_CTX*); -void SHA512_Update(SHA512_CTX*, const sha2_byte*, size_t); -void SHA512_Final(sha2_byte[SHA512_DIGEST_LENGTH], SHA512_CTX*); -char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); -char* SHA512_Data(const sha2_byte*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); - #endif /* __SHA2_H__ */