openssl: replace openssl_hash() with openssl_digest()

The openssl_hash() function was used only by string_hashnum(); change it to use
openssl_digest() instead.
This commit is contained in:
Dan Streetman
2023-09-28 16:44:42 -04:00
parent bed4831ce2
commit 11f7bc5e9c
3 changed files with 15 additions and 54 deletions
+8 -45
View File
@@ -50,43 +50,6 @@ int openssl_pkey_from_pem(const void *pem, size_t pem_size, EVP_PKEY **ret) {
return 0;
}
int openssl_hash(const EVP_MD *alg,
const void *msg,
size_t msg_len,
uint8_t *ret_hash,
size_t *ret_hash_len) {
_cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *ctx = NULL;
unsigned len;
int r;
ctx = EVP_MD_CTX_new();
if (!ctx)
/* This function just calls OPENSSL_zalloc, so failure
* here is almost certainly a failed allocation. */
return -ENOMEM;
/* The documentation claims EVP_DigestInit behaves just like
* EVP_DigestInit_ex if passed NULL, except it also calls
* EVP_MD_CTX_reset, which deinitializes the context. */
r = EVP_DigestInit_ex(ctx, alg, NULL);
if (r == 0)
return -EIO;
r = EVP_DigestUpdate(ctx, msg, msg_len);
if (r == 0)
return -EIO;
r = EVP_DigestFinal_ex(ctx, ret_hash, &len);
if (r == 0)
return -EIO;
if (ret_hash_len)
*ret_hash_len = len;
return 0;
}
/* Returns the number of bytes generated by the specified digest algorithm. This can be used only for
* fixed-size algorithms, e.g. md5, sha1, sha256, etc. Do not use this for variable-sized digest algorithms,
* e.g. shake128. Returns 0 on success, -EOPNOTSUPP if the algorithm is not supported, or < 0 for any other
@@ -681,18 +644,19 @@ int digest_and_sign(
int string_hashsum(
const char *s,
size_t len,
const EVP_MD *md_algorithm,
const char *md_algorithm,
char **ret) {
uint8_t hash[EVP_MAX_MD_SIZE];
_cleanup_free_ void *hash = NULL;
size_t hash_size;
char *enc;
_cleanup_free_ char *enc;
int r;
hash_size = EVP_MD_size(md_algorithm);
assert(hash_size > 0);
assert(s || len == 0);
assert(md_algorithm);
assert(ret);
r = openssl_hash(md_algorithm, s, len, hash, NULL);
r = openssl_digest(md_algorithm, s, len, &hash, &hash_size);
if (r < 0)
return r;
@@ -700,9 +664,8 @@ int string_hashsum(
if (!enc)
return -ENOMEM;
*ret = enc;
*ret = TAKE_PTR(enc);
return 0;
}
# endif
#endif
+3 -5
View File
@@ -57,8 +57,6 @@ static inline void sk_X509_free_allp(STACK_OF(X509) **sk) {
int openssl_pkey_from_pem(const void *pem, size_t pem_size, EVP_PKEY **ret);
int openssl_hash(const EVP_MD *alg, const void *msg, size_t msg_len, uint8_t *ret_hash, size_t *ret_hash_len);
int openssl_digest_size(const char *digest_alg, size_t *ret_digest_size);
int openssl_digest_many(const char *digest_alg, const struct iovec data[], size_t n_data, void **ret_digest, size_t *ret_digest_size);
@@ -130,13 +128,13 @@ typedef gcry_md_hd_t hash_context_t;
#endif
#if PREFER_OPENSSL
int string_hashsum(const char *s, size_t len, hash_algorithm_t md_algorithm, char **ret);
int string_hashsum(const char *s, size_t len, const char *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);
return string_hashsum(s, len, "SHA224", ret);
}
static inline int string_hashsum_sha256(const char *s, size_t len, char **ret) {
return string_hashsum(s, len, EVP_sha256(), ret);
return string_hashsum(s, len, "SHA256", ret);
}
#endif