Skip to contentSkip to conversation

crypto: misc gcrypt hash/hmac fixes

PATCH In review Daniel P. Berrangé 2 participants 5 messages Oct 30, 2024 – Oct 31, 2024 Crypto API

lore
Share & tools
lore
#0 lore raw

cover crypto: misc gcrypt hash/hmac fixes

This fixes the test suite when gcrypt disables some algorithms at runtime.

Daniel P. Berrangé (2):
crypto: fix error check on gcry_md_open
crypto: perform runtime check for hash/hmac support in gcrypt

 crypto/hash-gcrypt.c | 6 +++---
 crypto/hmac-gcrypt.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH 1/2 crypto: fix error check on gcry_md_open

Gcrypt does not return negative values on error, it returns non-zero values. This caused QEMU not to detect failure to open an unsupported hash, resulting in a later crash trying to use a NULL context.

---
 crypto/hash-gcrypt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
crypto/hash-gcrypt.c +2 -2
index 73533a4949..22ddf394ec 100644
@@ -49,7 +49,7 @@ static
 QCryptoHash *qcrypto_gcrypt_hash_new(QCryptoHashAlgo alg, Error **errp)
 {
     QCryptoHash *hash;
-    int ret;
+    gcry_error_t ret;
 
     hash = g_new(QCryptoHash, 1);
     hash->alg = alg;
@@ -57,7 +57,7 @@ QCryptoHash *qcrypto_gcrypt_hash_new(QCryptoHashAlgo alg, Error **errp)
 
     ret = gcry_md_open((gcry_md_hd_t *) hash->opaque,
                        qcrypto_hash_alg_map[alg], 0);
-    if (ret < 0) {
+    if (ret != 0) {
         error_setg(errp,
                    "Unable to initialize hash algorithm: %s",
                    gcry_strerror(ret));
Signature · 1 line
2.46.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] crypto: fix error check on gcry_md_open

Quoted 8 lines · Daniel P. Berrangé

Gcrypt does not return negative values on error, it returns non-zero values. This caused QEMU not to detect failure to open an unsupported hash, resulting in a later crash trying to use a NULL context.

---
  crypto/hash-gcrypt.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
#3 lore raw

PATCH 2/2 crypto: perform runtime check for hash/hmac support in gcrypt

In reply to Daniel P. Berrangé

gcrypto has the ability to dynamically disable hash/hmac algorithms at runtime, so QEMU must perform a runtime check.

---
 crypto/hash-gcrypt.c | 2 +-
 crypto/hmac-gcrypt.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
crypto/hash-gcrypt.c +1 -1
index 22ddf394ec..2c8325869a 100644
@@ -40,7 +40,7 @@ gboolean qcrypto_hash_supports(QCryptoHashAlgo alg)
 {
     if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) &&
         qcrypto_hash_alg_map[alg] != GCRY_MD_NONE) {
-        return true;
+        return gcry_md_test_algo(qcrypto_hash_alg_map[alg]) == 0;
     }
     return false;
 }
crypto/hmac-gcrypt.c +1 -1
index 19990cb6ed..181b376572 100644
@@ -37,7 +37,7 @@ bool qcrypto_hmac_supports(QCryptoHashAlgo alg)
 {
     if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
         qcrypto_hmac_alg_map[alg] != GCRY_MAC_NONE) {
-        return true;
+        return gcry_mac_test_algo(qcrypto_hmac_alg_map[alg]) == 0;
     }
 
     return false;
Signature · 1 line
2.46.0
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] crypto: perform runtime check for hash/hmac support in gcrypt

Quoted 8 lines · Daniel P. Berrangé

gcrypto has the ability to dynamically disable hash/hmac algorithms at runtime, so QEMU must perform a runtime check.

---
  crypto/hash-gcrypt.c | 2 +-
  crypto/hmac-gcrypt.c | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)