summary refs log tree commit diff
path: root/nix/libutil/hash.cc
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2020-06-23 12:11:00 +0200
committerLudovic Courtès <ludo@gnu.org>2020-06-27 23:42:20 +0200
commit8dc6c387852bb9505be44567a5f56913633cc23a (patch)
tree872914ede2ea33f72f8c09b2c3517fdf92ee85f5 /nix/libutil/hash.cc
parent3fb6b8f30444d41963ba5bdd441123a6d2df17bd (diff)
downloadguix-8dc6c387852bb9505be44567a5f56913633cc23a.tar.gz
daemon: Remove OpenSSL hash compatibility wrappers.
* nix/libutil/hash.cc (struct Ctx): Copy from gcrypt-hash.hh.
(start, update, finish): Use gcrypt functions directly instead of
OpenSSL-like wrappers.
* nix/libutil/gcrypt-hash.cc, nix/libutil/gcrypt-hash.hh,
nix/libutil/md5.h, nix/libutil/sha1.h, nix/libutil/sha256.h,
nix/libutil/sha512.h: Remove.
* nix/local.mk (libutil_a_SOURCES, libutil_headers): Adjust
accordingly.
Diffstat (limited to 'nix/libutil/hash.cc')
-rw-r--r--nix/libutil/hash.cc53
1 files changed, 24 insertions, 29 deletions
diff --git a/nix/libutil/hash.cc b/nix/libutil/hash.cc
index 251f18f60e..20d2e4b724 100644
--- a/nix/libutil/hash.cc
+++ b/nix/libutil/hash.cc
@@ -3,18 +3,6 @@
 #include <iostream>
 #include <cstring>
 
-#ifdef HAVE_OPENSSL
-#include <openssl/md5.h>
-#include <openssl/sha.h>
-#else
-extern "C" {
-#include "md5.h"
-#include "sha1.h"
-#include "sha256.h"
-#include "sha512.h"
-}
-#endif
-
 #include "hash.hh"
 #include "archive.hh"
 #include "util.hh"
@@ -193,41 +181,48 @@ bool isHash(const string & s)
     return true;
 }
 
-
+/* The "hash context".  */
 struct Ctx
 {
-    MD5_CTX md5;
-    SHA_CTX sha1;
-    SHA256_CTX sha256;
-    SHA512_CTX sha512;
+  /* This copy constructor is needed in 'HashSink::currentHash()' where we
+     expect the copy of a 'Ctx' object to yield a truly different context.  */
+  Ctx(Ctx &ref)
+  {
+    if (ref.md_handle == NULL)
+      md_handle = NULL;
+    else
+      gcry_md_copy (&md_handle, ref.md_handle);
+  }
+
+  /* Make sure 'md_handle' is always initialized.  */
+  Ctx(): md_handle (NULL) { };
+
+  gcry_md_hd_t md_handle;
 };
 
 
 static void start(HashType ht, Ctx & ctx)
 {
-    if (ht == htMD5) MD5_Init(&ctx.md5);
-    else if (ht == htSHA1) SHA1_Init(&ctx.sha1);
-    else if (ht == htSHA256) SHA256_Init(&ctx.sha256);
-    else if (ht == htSHA512) SHA512_Init(&ctx.sha512);
+    gcry_error_t err;
+
+    err = gcry_md_open (&ctx.md_handle, ht, 0);
+    assert (err == GPG_ERR_NO_ERROR);
 }
 
 
 static void update(HashType ht, Ctx & ctx,
     const unsigned char * bytes, unsigned int len)
 {
-    if (ht == htMD5) MD5_Update(&ctx.md5, bytes, len);
-    else if (ht == htSHA1) SHA1_Update(&ctx.sha1, bytes, len);
-    else if (ht == htSHA256) SHA256_Update(&ctx.sha256, bytes, len);
-    else if (ht == htSHA512) SHA512_Update(&ctx.sha512, bytes, len);
+    gcry_md_write (ctx.md_handle, bytes, len);
 }
 
 
 static void finish(HashType ht, Ctx & ctx, unsigned char * hash)
 {
-    if (ht == htMD5) MD5_Final(hash, &ctx.md5);
-    else if (ht == htSHA1) SHA1_Final(hash, &ctx.sha1);
-    else if (ht == htSHA256) SHA256_Final(hash, &ctx.sha256);
-    else if (ht == htSHA512) SHA512_Final(hash, &ctx.sha512);
+    memcpy (hash, gcry_md_read (ctx.md_handle, ht),
+	    gcry_md_get_algo_dlen (ht));
+    gcry_md_close (ctx.md_handle);
+    ctx.md_handle = NULL;
 }