]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
sha256: Added WinCrypt implementation
authorSteve Holme <steve_holme@hotmail.com>
Thu, 20 Feb 2020 01:49:31 +0000 (01:49 +0000)
committerSteve Holme <steve_holme@hotmail.com>
Sun, 8 Mar 2020 11:45:28 +0000 (11:45 +0000)
Closed #5030

lib/sha256.c

index a14f42aa30520a338b9e05e9af9100da1d294aa6..352d577e8df35b4430d5629da3f3d1bed4c51213 100644 (file)
@@ -192,6 +192,49 @@ static void SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
   (void) CC_SHA256_Final(digest, ctx);
 }
 
+#elif defined(USE_WIN32_CRYPTO)
+
+#include <wincrypt.h>
+
+typedef struct {
+  HCRYPTPROV hCryptProv;
+  HCRYPTHASH hHash;
+} SHA256_CTX;
+
+#if !defined(CALG_SHA_256)
+#define CALG_SHA_256 0x0000800c
+#endif
+
+static void SHA256_Init(SHA256_CTX *ctx)
+{
+  if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL,
+                         PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
+    CryptCreateHash(ctx->hCryptProv, CALG_SHA_256, 0, 0, &ctx->hHash);
+  }
+}
+
+static void SHA256_Update(SHA256_CTX *ctx,
+                          const unsigned char *data,
+                          unsigned int length)
+{
+  CryptHashData(ctx->hHash, (unsigned char *) data, length, 0);
+}
+
+static void SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
+{
+  unsigned long length;
+
+  CryptGetHashParam(ctx->hHash, HP_HASHVAL, NULL, &length, 0);
+  if(length == SHA256_DIGEST_LENGTH)
+    CryptGetHashParam(ctx->hHash, HP_HASHVAL, digest, &length, 0);
+
+  if(ctx->hHash)
+    CryptDestroyHash(ctx->hHash);
+
+  if(ctx->hCryptProv)
+    CryptReleaseContext(ctx->hCryptProv, 0);
+}
+
 #else
 
 /* When no other crypto library is available we use this code segment */