basic/openssl-util: Add sha256 hash wrapper
This commit is contained in:
committed by
Zbigniew Jędrzejewski-Szmek
parent
1736344e9e
commit
fc169a6fb2
@@ -4,6 +4,43 @@
|
||||
#include "alloc-util.h"
|
||||
|
||||
#if HAVE_OPENSSL
|
||||
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;
|
||||
}
|
||||
|
||||
int rsa_encrypt_bytes(
|
||||
EVP_PKEY *pkey,
|
||||
const void *decrypted_key,
|
||||
|
||||
Reference in New Issue
Block a user