port string_hashsum from libgcrypt to openssl^gcrypt
This allows resolved and importd to be built without libgcrypt. Note that we now say either 'cryptographic library' or 'cryptolib'. Co-authored-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "openssl-util.h"
|
||||
#include "alloc-util.h"
|
||||
#include "hexdecoct.h"
|
||||
|
||||
#if HAVE_OPENSSL
|
||||
int openssl_hash(const EVP_MD *alg,
|
||||
@@ -107,4 +108,33 @@ int rsa_pkey_to_suitable_key_size(
|
||||
*ret_suitable_key_size = suitable_key_size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
# if PREFER_OPENSSL
|
||||
int string_hashsum(
|
||||
const char *s,
|
||||
size_t len,
|
||||
const EVP_MD *md_algorithm,
|
||||
char **ret) {
|
||||
|
||||
uint8_t hash[EVP_MAX_MD_SIZE];
|
||||
size_t hash_size;
|
||||
char *enc;
|
||||
int r;
|
||||
|
||||
hash_size = EVP_MD_size(md_algorithm);
|
||||
assert(hash_size > 0);
|
||||
|
||||
r = openssl_hash(md_algorithm, s, len, hash, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
enc = hexmem(hash, hash_size);
|
||||
if (!enc)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = enc;
|
||||
return 0;
|
||||
|
||||
}
|
||||
# endif
|
||||
#endif
|
||||
|
||||
@@ -62,3 +62,15 @@ typedef const char* elliptic_curve_t;
|
||||
typedef gcry_md_hd_t hash_context_t;
|
||||
# define OPENSSL_OR_GCRYPT(a, b) (b)
|
||||
#endif
|
||||
|
||||
#if PREFER_OPENSSL
|
||||
int string_hashsum(const char *s, size_t len, hash_algorithm_t md_algorithm, char **ret);
|
||||
|
||||
static inline int string_hashsum_sha224(const char *s, size_t len, char **ret) {
|
||||
return string_hashsum(s, len, EVP_sha224(), ret);
|
||||
}
|
||||
|
||||
static inline int string_hashsum_sha256(const char *s, size_t len, char **ret) {
|
||||
return string_hashsum(s, len, EVP_sha256(), ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user