crypto: handle gcrypt ciphers being disabled
PATCH 1/4 crypto: factor out conversion of QAPI to gcrypt constants
In reply to Daniel P. BerrangéThe conversion of cipher mode will shortly be required in more than one place.
--- crypto/cipher-gcrypt.c.inc | 116 +++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 56 deletions(-)
crypto/cipher-gcrypt.c.inc +60 -56
@@ -20,6 +20,56 @@ #include <gcrypt.h> +static int qcrypto_cipher_alg_to_gcry_alg(QCryptoCipherAlgorithm alg) +{ + switch (alg) { + case QCRYPTO_CIPHER_ALG_DES: + return GCRY_CIPHER_DES; + case QCRYPTO_CIPHER_ALG_3DES: + return GCRY_CIPHER_3DES; + case QCRYPTO_CIPHER_ALG_AES_128: + return GCRY_CIPHER_AES128; + case QCRYPTO_CIPHER_ALG_AES_192: + return GCRY_CIPHER_AES192; + case QCRYPTO_CIPHER_ALG_AES_256: + return GCRY_CIPHER_AES256; + case QCRYPTO_CIPHER_ALG_CAST5_128: + return GCRY_CIPHER_CAST5; + case QCRYPTO_CIPHER_ALG_SERPENT_128: + return GCRY_CIPHER_SERPENT128; + case QCRYPTO_CIPHER_ALG_SERPENT_192: + return GCRY_CIPHER_SERPENT192; + case QCRYPTO_CIPHER_ALG_SERPENT_256: + return GCRY_CIPHER_SERPENT256; + case QCRYPTO_CIPHER_ALG_TWOFISH_128: + return GCRY_CIPHER_TWOFISH128; + case QCRYPTO_CIPHER_ALG_TWOFISH_256: + return GCRY_CIPHER_TWOFISH; +#ifdef CONFIG_CRYPTO_SM4 + case QCRYPTO_CIPHER_ALG_SM4: + return GCRY_CIPHER_SM4; +#endif + default: + return GCRY_CIPHER_NONE; + } +} + +static int qcrypto_cipher_mode_to_gcry_mode(QCryptoCipherMode mode) +{ + switch (mode) { + case QCRYPTO_CIPHER_MODE_ECB: + return GCRY_CIPHER_MODE_ECB; + case QCRYPTO_CIPHER_MODE_XTS: + return GCRY_CIPHER_MODE_XTS; + case QCRYPTO_CIPHER_MODE_CBC: + return GCRY_CIPHER_MODE_CBC; + case QCRYPTO_CIPHER_MODE_CTR: + return GCRY_CIPHER_MODE_CTR; + default: + return GCRY_CIPHER_MODE_NONE; + } +} + bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, QCryptoCipherMode mode) {
@@ -188,72 +238,26 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, return NULL; } - switch (alg) { - case QCRYPTO_CIPHER_ALG_DES: - gcryalg = GCRY_CIPHER_DES; - break; - case QCRYPTO_CIPHER_ALG_3DES: - gcryalg = GCRY_CIPHER_3DES; - break; - case QCRYPTO_CIPHER_ALG_AES_128: - gcryalg = GCRY_CIPHER_AES128; - break; - case QCRYPTO_CIPHER_ALG_AES_192: - gcryalg = GCRY_CIPHER_AES192; - break; - case QCRYPTO_CIPHER_ALG_AES_256: - gcryalg = GCRY_CIPHER_AES256; - break; - case QCRYPTO_CIPHER_ALG_CAST5_128: - gcryalg = GCRY_CIPHER_CAST5; - break; - case QCRYPTO_CIPHER_ALG_SERPENT_128: - gcryalg = GCRY_CIPHER_SERPENT128; - break; - case QCRYPTO_CIPHER_ALG_SERPENT_192: - gcryalg = GCRY_CIPHER_SERPENT192; - break; - case QCRYPTO_CIPHER_ALG_SERPENT_256: - gcryalg = GCRY_CIPHER_SERPENT256; - break; - case QCRYPTO_CIPHER_ALG_TWOFISH_128: - gcryalg = GCRY_CIPHER_TWOFISH128; - break; - case QCRYPTO_CIPHER_ALG_TWOFISH_256: - gcryalg = GCRY_CIPHER_TWOFISH; - break; -#ifdef CONFIG_CRYPTO_SM4 - case QCRYPTO_CIPHER_ALG_SM4: - gcryalg = GCRY_CIPHER_SM4; - break; -#endif - default: + gcryalg = qcrypto_cipher_alg_to_gcry_alg(alg); + if (gcryalg == GCRY_CIPHER_NONE) { error_setg(errp, "Unsupported cipher algorithm %s", QCryptoCipherAlgorithm_str(alg)); return NULL; } - drv = &qcrypto_gcrypt_driver; - switch (mode) { - case QCRYPTO_CIPHER_MODE_ECB: - gcrymode = GCRY_CIPHER_MODE_ECB; - break; - case QCRYPTO_CIPHER_MODE_XTS: - gcrymode = GCRY_CIPHER_MODE_XTS; - break; - case QCRYPTO_CIPHER_MODE_CBC: - gcrymode = GCRY_CIPHER_MODE_CBC; - break; - case QCRYPTO_CIPHER_MODE_CTR: - drv = &qcrypto_gcrypt_ctr_driver; - gcrymode = GCRY_CIPHER_MODE_CTR; - break; - default: + gcrymode = qcrypto_cipher_mode_to_gcry_mode(mode); + if (gcrymode == GCRY_CIPHER_MODE_NONE) { error_setg(errp, "Unsupported cipher mode %s", QCryptoCipherMode_str(mode)); return NULL; } + if (mode == QCRYPTO_CIPHER_MODE_CTR) { + drv = &qcrypto_gcrypt_ctr_driver; + } else { + drv = &qcrypto_gcrypt_driver; + } + ctx = g_new0(QCryptoCipherGcrypt, 1); ctx->base.driver = drv;
Signature · 1 line
2.43.0
PATCH 2/4 crypto: query gcrypt for cipher availability
In reply to Daniel P. BerrangéJust because a cipher is defined in the gcrypt header file, does not imply that it can be used. Distros can filter the list of ciphers when building gcrypt. For example, RHEL-9 disables the SM4 cipher. It is also possible that running in FIPS mode might dynamically change what ciphers are available at runtime.
qcrypto_cipher_supports must therefore query gcrypt directly to check for cipher availability.
--- crypto/cipher-gcrypt.c.inc | 5 +++++ 1 file changed, 5 insertions(+)
crypto/cipher-gcrypt.c.inc +5 -0
@@ -93,6 +93,11 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, return false; } + if (gcry_cipher_algo_info(qcrypto_cipher_alg_to_gcry_alg(alg), + GCRYCTL_TEST_ALGO, NULL, NULL) != 0) { + return false; + } + switch (mode) { case QCRYPTO_CIPHER_MODE_ECB: case QCRYPTO_CIPHER_MODE_CBC:
Signature · 1 line
2.43.0
PATCH 3/4 crypto: use error_abort for unexpected failures
In reply to Daniel P. BerrangéThis improves the error diagnosis from the unit test when a cipher is unexpected not available from
ERROR:../tests/unit/test-crypto-cipher.c:683:test_cipher: assertion failed: (err == NULL) Bail out! ERROR:../tests/unit/test-crypto-cipher.c:683:test_cipher: assertion failed: (err == NULL) Aborted (core dumped)
to
Unexpected error in qcrypto_cipher_ctx_new() at ../crypto/cipher-gcrypt.c.inc:262:
./build//tests/unit/test-crypto-cipher: Cannot initialize cipher: Invalid cipher algorithm Aborted (core dumped)
--- tests/unit/test-crypto-cipher.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tests/unit/test-crypto-cipher.c +1 -2
@@ -676,9 +676,8 @@ static void test_cipher(const void *opaque) cipher =qcrypto_cipher_new ( data->alg, data->mode, key, nkey, - &err); + data->plaintext ? &error_abort : &err);if (data->plaintext) { -g_assert (err ==NULL );g_assert (cipher !=NULL ); }else {error_free_or_abort (&err);
Signature · 1 line
2.43.0
cover crypto: handle gcrypt ciphers being disabled
This fixes testing on RHEL with libgcrypt cipher backends
Daniel P. Berrangé (4):
crypto: factor out conversion of QAPI to gcrypt constants
crypto: query gcrypt for cipher availability
crypto: use error_abort for unexpected failures
crypto: report which ciphers are being skipped during tests
crypto/cipher-gcrypt.c.inc | 121 +++++++++++++++++--------------- tests/unit/test-crypto-cipher.c | 7 +- 2 files changed, 70 insertions(+), 58 deletions(-)
Signature · 1 line
2.43.0
PATCH 4/4 crypto: report which ciphers are being skipped during tests
Since the ciphers can be dynamically disabled at runtime, when running unit tests it is helpful to report which ciphers we can skipped for testing.
--- tests/unit/test-crypto-cipher.c | 4 ++++ 1 file changed, 4 insertions(+)
tests/unit/test-crypto-cipher.c +4 -0
@@ -821,6 +821,10 @@ int main(int argc, char **argv)for (i =0 ; i <G_N_ELEMENTS (test_data); i++) {if (qcrypto_cipher_supports (test_data[i].alg, test_data[i].mode)) {g_test_add_data_func (test_data[i].path, &test_data[i], test_cipher); + }else { +g_printerr ("# skip unsupported %s:%s\n" , +QCryptoCipherAlgorithm_str (test_data[i].alg), +QCryptoCipherMode_str (test_data[i].mode)); } }
Signature · 1 line
2.43.0
Quoted 7 lines · Daniel P. Berrangé
This fixes testing on RHEL with libgcrypt cipher backends
Daniel P. Berrangé (4):
crypto: factor out conversion of QAPI to gcrypt constants
crypto: query gcrypt for cipher availability
crypto: useerror_abortfor unexpected failures
crypto: report which ciphers are being skipped during tests
Series:
Re: [PATCH 1/4] crypto: factor out conversion of QAPI to gcrypt constants
In reply to Daniel P. BerrangéQuoted 7 lines · Daniel P. Berrangé
The conversion of cipher mode will shortly be required in more than one place.
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/cipher-gcrypt.c.inc | 116 +++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 56 deletions(-)
Re: [PATCH 2/4] crypto: query gcrypt for cipher availability
In reply to Daniel P. BerrangéQuoted 20 lines · Daniel P. Berrangé
Just because a cipher is defined in the gcrypt header file, does not imply that it can be used. Distros can filter the list of ciphers when building gcrypt. For example, RHEL-9 disables the SM4 cipher. It is also possible that running in FIPS mode might dynamically change what ciphers are available at runtime.
qcrypto_cipher_supportsmust therefore query gcrypt directly to check for cipher availability.Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/cipher-gcrypt.c.inc | 5 +++++ 1 file changed, 5 insertions(+)crypto/cipher-gcrypt.c.inc +0
-0@@ -93,6 +93,11 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, return false;
crypto/cipher-gcrypt.c.inc bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, · line 103
Quoted 10 lines
crypto/cipher-gcrypt.c.inc +5
-0} + if (gcry_cipher_algo_info(qcrypto_cipher_alg_to_gcry_alg(alg), + GCRYCTL_TEST_ALGO, NULL, NULL) != 0) { + return false; + } + switch (mode) { case QCRYPTO_CIPHER_MODE_ECB: case QCRYPTO_CIPHER_MODE_CBC:
Re: [PATCH 3/4] crypto: use error_abort for unexpected failures
In reply to Daniel P. BerrangéQuoted 23 lines · Daniel P. Berrangé
This improves the error diagnosis from the unit test when a cipher is unexpected not available from
ERROR:../tests/unit/test-crypto-cipher.c:683:test_cipher: assertion failed: (err == NULL) Bail out! ERROR:../tests/unit/test-crypto-cipher.c:683:test_cipher: assertion failed: (err == NULL) Aborted (core dumped)
to
Unexpected error in
qcrypto_cipher_ctx_new()at ../crypto/cipher-gcrypt.c.inc:262:
./build//tests/unit/test-crypto-cipher: Cannot initialize cipher: Invalid cipher algorithm Aborted (core dumped)Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/unit/test-crypto-cipher.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)tests/unit/test-crypto-cipher.c +0
-0
tests/unit/test-crypto-cipher.c static void test_cipher(const void *opaque) · line 683
Quoted 10 lines
tests/unit/test-crypto-cipher.c +1
-2@@ -676,9 +676,8 @@ static void test_cipher(const void *opaque) cipher =qcrypto_cipher_new ( data->alg, data->mode, key, nkey, - &err); + data->plaintext ? &error_abort : &err);if (data->plaintext) { -g_assert (err ==NULL );g_assert (cipher !=NULL ); }else {error_free_or_abort (&err);
Re: [PATCH 4/4] crypto: report which ciphers are being skipped during tests
In reply to Daniel P. BerrangéQuoted 14 lines · Daniel P. Berrangé
Since the ciphers can be dynamically disabled at runtime, when running unit tests it is helpful to report which ciphers we can skipped for testing.
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/unit/test-crypto-cipher.c | 4 ++++ 1 file changed, 4 insertions(+)tests/unit/test-crypto-cipher.c +0
-0
tests/unit/test-crypto-cipher.c int main(int argc, char **argv) · line 830
Quoted 10 lines
tests/unit/test-crypto-cipher.c +4
-0@@ -821,6 +821,10 @@ int main(int argc, char **argv)for (i =0 ; i <G_N_ELEMENTS (test_data); i++) {if (qcrypto_cipher_supports (test_data[i].alg, test_data[i].mode)) {g_test_add_data_func (test_data[i].path, &test_data[i], test_cipher); + }else { +g_printerr ("# skip unsupported %s:%s\n" , +QCryptoCipherAlgorithm_str (test_data[i].alg), +QCryptoCipherMode_str (test_data[i].mode)); } }