diff options
Diffstat (limited to 'nix')
-rw-r--r-- | nix/libutil/gcrypt-hash.cc | 51 | ||||
-rw-r--r-- | nix/libutil/gcrypt-hash.hh | 50 | ||||
-rw-r--r-- | nix/libutil/hash.cc | 67 | ||||
-rw-r--r-- | nix/libutil/hash.hh | 20 | ||||
-rw-r--r-- | nix/libutil/md5.h | 35 | ||||
-rw-r--r-- | nix/libutil/sha1.h | 35 | ||||
-rw-r--r-- | nix/libutil/sha256.h | 35 | ||||
-rw-r--r-- | nix/libutil/sha512.h | 35 | ||||
-rw-r--r-- | nix/local.mk | 12 |
9 files changed, 48 insertions, 292 deletions
diff --git a/nix/libutil/gcrypt-hash.cc b/nix/libutil/gcrypt-hash.cc deleted file mode 100644 index c4ae7bfcc2..0000000000 --- a/nix/libutil/gcrypt-hash.cc +++ /dev/null @@ -1,51 +0,0 @@ -/* GNU Guix --- Functional package management for GNU - Copyright (C) 2012, 2013 Ludovic Courtès <ludo@gnu.org> - - This file is part of GNU Guix. - - GNU Guix is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or (at - your option) any later version. - - GNU Guix is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */ - -#include <config.h> - -#include <gcrypt-hash.hh> -#include <assert.h> - -extern "C" { - -void -guix_hash_init (struct guix_hash_context *ctx, int algo) -{ - gcry_error_t err; - - err = gcry_md_open (&ctx->md_handle, algo, 0); - assert (err == GPG_ERR_NO_ERROR); -} - -void -guix_hash_update (struct guix_hash_context *ctx, const void *buffer, size_t len) -{ - gcry_md_write (ctx->md_handle, buffer, len); -} - -void -guix_hash_final (void *resbuf, struct guix_hash_context *ctx, - int algo) -{ - memcpy (resbuf, gcry_md_read (ctx->md_handle, algo), - gcry_md_get_algo_dlen (algo)); - gcry_md_close (ctx->md_handle); - ctx->md_handle = NULL; -} - -} diff --git a/nix/libutil/gcrypt-hash.hh b/nix/libutil/gcrypt-hash.hh deleted file mode 100644 index 11f061159f..0000000000 --- a/nix/libutil/gcrypt-hash.hh +++ /dev/null @@ -1,50 +0,0 @@ -/* GNU Guix --- Functional package management for GNU - Copyright (C) 2012, 2013 Ludovic Courtès <ludo@gnu.org> - - This file is part of GNU Guix. - - GNU Guix is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or (at - your option) any later version. - - GNU Guix is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */ - -/* An OpenSSL-like interface to GNU libgcrypt cryptographic hash - functions. */ - -#pragma once -#include <gcrypt.h> -#include <unistd.h> - -struct guix_hash_context -{ - /* This copy constructor is needed in 'HashSink::currentHash()' where we - expect the copy of a 'Ctx' object to yield a truly different context. */ - guix_hash_context (guix_hash_context &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. */ - guix_hash_context (): md_handle (NULL) { }; - - gcry_md_hd_t md_handle; -}; - -extern "C" { -extern void guix_hash_init (struct guix_hash_context *ctx, int algo); -extern void guix_hash_update (struct guix_hash_context *ctx, const void *buffer, - size_t len); -extern void guix_hash_final (void *resbuf, struct guix_hash_context *ctx, - int algo); -} diff --git a/nix/libutil/hash.cc b/nix/libutil/hash.cc index ea69aa64f9..7853acdd49 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" @@ -38,11 +26,9 @@ Hash::Hash() Hash::Hash(HashType type) { this->type = type; - if (type == htMD5) hashSize = md5HashSize; - else if (type == htSHA1) hashSize = sha1HashSize; - else if (type == htSHA256) hashSize = sha256HashSize; - else if (type == htSHA512) hashSize = sha512HashSize; - else throw Error("unknown hash type"); + hashSize = gcry_md_get_algo_dlen(type); + + if (hashSize == 0) throw Error("unknown hash type"); assert(hashSize <= maxHashSize); memset(hash, 0, maxHashSize); } @@ -195,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; } @@ -328,6 +321,9 @@ HashType parseHashType(const string & s) else if (s == "sha1") return htSHA1; else if (s == "sha256") return htSHA256; else if (s == "sha512") return htSHA512; + else if (s == "sha3-256") return htSHA3_256; + else if (s == "sha3-512") return htSHA3_512; + else if (s == "blake2s-256") return htBLAKE2s_256; else return htUnknown; } @@ -338,6 +334,9 @@ string printHashType(HashType ht) else if (ht == htSHA1) return "sha1"; else if (ht == htSHA256) return "sha256"; else if (ht == htSHA512) return "sha512"; + else if (ht == htSHA3_256) return "sha3-256"; + else if (ht == htSHA3_512) return "sha3-512"; + else if (ht == htBLAKE2s_256) return "blake2s-256"; else throw Error("cannot print unknown hash type"); } diff --git a/nix/libutil/hash.hh b/nix/libutil/hash.hh index 6b5e47cd8a..ac58651a02 100644 --- a/nix/libutil/hash.hh +++ b/nix/libutil/hash.hh @@ -1,5 +1,7 @@ #pragma once +#include <gcrypt.h> + #include "types.hh" #include "serialise.hh" @@ -7,16 +9,18 @@ namespace nix { -typedef enum { htUnknown, htMD5, htSHA1, htSHA256, htSHA512 } HashType; - - -const int md5HashSize = 16; -const int sha1HashSize = 20; -const int sha256HashSize = 32; -const int sha512HashSize = 64; - extern const string base32Chars; +typedef enum { + htUnknown = 0, + htMD5 = GCRY_MD_MD5, + htSHA1 = GCRY_MD_SHA1, + htSHA256 = GCRY_MD_SHA256, + htSHA512 = GCRY_MD_SHA512, + htSHA3_256 = GCRY_MD_SHA3_256, + htSHA3_512 = GCRY_MD_SHA3_512, + htBLAKE2s_256 = GCRY_MD_BLAKE2S_256 +} HashType; struct Hash { diff --git a/nix/libutil/md5.h b/nix/libutil/md5.h deleted file mode 100644 index 4583a458b3..0000000000 --- a/nix/libutil/md5.h +++ /dev/null @@ -1,35 +0,0 @@ -/* GNU Guix --- Functional package management for GNU - Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org> - - This file is part of GNU Guix. - - GNU Guix is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or (at - your option) any later version. - - GNU Guix is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */ - -#include <gcrypt-hash.hh> - -#define MD5_CTX guix_hash_context - -static inline void -MD5_Init (struct MD5_CTX *ctx) -{ - guix_hash_init (ctx, GCRY_MD_MD5); -} - -#define MD5_Update guix_hash_update - -static inline void -MD5_Final (void *resbuf, struct MD5_CTX *ctx) -{ - guix_hash_final (resbuf, ctx, GCRY_MD_MD5); -} diff --git a/nix/libutil/sha1.h b/nix/libutil/sha1.h deleted file mode 100644 index d2d071e058..0000000000 --- a/nix/libutil/sha1.h +++ /dev/null @@ -1,35 +0,0 @@ -/* GNU Guix --- Functional package management for GNU - Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org> - - This file is part of GNU Guix. - - GNU Guix is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or (at - your option) any later version. - - GNU Guix is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */ - -#include <gcrypt-hash.hh> - -#define SHA_CTX guix_hash_context - -static inline void -SHA1_Init (struct SHA_CTX *ctx) -{ - guix_hash_init (ctx, GCRY_MD_SHA1); -} - -#define SHA1_Update guix_hash_update - -static inline void -SHA1_Final (void *resbuf, struct SHA_CTX *ctx) -{ - guix_hash_final (resbuf, ctx, GCRY_MD_SHA1); -} diff --git a/nix/libutil/sha256.h b/nix/libutil/sha256.h deleted file mode 100644 index ca95d7fea8..0000000000 --- a/nix/libutil/sha256.h +++ /dev/null @@ -1,35 +0,0 @@ -/* GNU Guix --- Functional package management for GNU - Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org> - - This file is part of GNU Guix. - - GNU Guix is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or (at - your option) any later version. - - GNU Guix is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */ - -#include <gcrypt-hash.hh> - -#define SHA256_CTX guix_hash_context - -static inline void -SHA256_Init (struct SHA256_CTX *ctx) -{ - guix_hash_init (ctx, GCRY_MD_SHA256); -} - -#define SHA256_Update guix_hash_update - -static inline void -SHA256_Final (void *resbuf, struct SHA256_CTX *ctx) -{ - guix_hash_final (resbuf, ctx, GCRY_MD_SHA256); -} diff --git a/nix/libutil/sha512.h b/nix/libutil/sha512.h deleted file mode 100644 index d2abab4c5f..0000000000 --- a/nix/libutil/sha512.h +++ /dev/null @@ -1,35 +0,0 @@ -/* GNU Guix --- Functional package management for GNU - Copyright (C) 2012, 2015 Ludovic Courtès <ludo@gnu.org> - - This file is part of GNU Guix. - - GNU Guix is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or (at - your option) any later version. - - GNU Guix is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */ - -#include <gcrypt-hash.hh> - -#define SHA512_CTX guix_hash_context - -static inline void -SHA512_Init (struct SHA512_CTX *ctx) -{ - guix_hash_init (ctx, GCRY_MD_SHA512); -} - -#define SHA512_Update guix_hash_update - -static inline void -SHA512_Final (void *resbuf, struct SHA512_CTX *ctx) -{ - guix_hash_final (resbuf, ctx, GCRY_MD_SHA512); -} diff --git a/nix/local.mk b/nix/local.mk index c136fb7202..005cde5563 100644 --- a/nix/local.mk +++ b/nix/local.mk @@ -1,5 +1,5 @@ # GNU Guix --- Functional package management for GNU -# Copyright © 2012, 2013, 2014, 2015, 2016, 2018, 2019 Ludovic Courtès <ludo@gnu.org> +# Copyright © 2012, 2013, 2014, 2015, 2016, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> # Copyright © 2016 Mathieu Lirzin <mthl@gnu.org> # Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr> # @@ -56,8 +56,7 @@ libutil_a_SOURCES = \ %D%/libutil/affinity.cc \ %D%/libutil/serialise.cc \ %D%/libutil/util.cc \ - %D%/libutil/hash.cc \ - %D%/libutil/gcrypt-hash.cc + %D%/libutil/hash.cc libutil_headers = \ %D%/libutil/affinity.hh \ @@ -65,12 +64,7 @@ libutil_headers = \ %D%/libutil/serialise.hh \ %D%/libutil/util.hh \ %D%/libutil/archive.hh \ - %D%/libutil/types.hh \ - %D%/libutil/gcrypt-hash.hh \ - %D%/libutil/md5.h \ - %D%/libutil/sha1.h \ - %D%/libutil/sha256.h \ - %D%/libutil/sha512.h + %D%/libutil/types.hh libutil_a_CPPFLAGS = \ -I$(top_builddir)/nix \ |