crypto: misc cleanup and introduce gnutls backend driver
Subsystems (2)
cover crypto: misc cleanup and introduce gnutls backend driver
Currently the crypto layer has a choice of backend drivers
* builtin - AES/DES for ciphers using in-tree impl, glib for hash / hmac
* gcrypt - all ciphers and al hash/hmac algs
* nettle - all ciphers and al hash/hmac algs
We currently default to nettle because that minimizes the deps from QEMU, as gnutls already pulls in nettle.
In retrospect, however, this was the wrong metric to optimize for. Instead we should have picked backend based on the performance of the drivers.
The nettle impls have some limited CPU hardware acceleration, but aside from in ECB mode, nettle is slower than gcrypt in every case. In the most important AES-XTS case used for luks disk encryption, nettle is achieves just 15% of the performance of gcrypt. It is clear we should prefer gcrypt over nettle.
gnutls uses nettle internally and also exposes many of the ciphers for direct usage. Unexpectedly, gnutls is actually faster than nettle, despite using nettle. The reason for this is that gnutls provides CPU accelerated code for handling CBC and XTS modes. This lets gnutls get in the same ballpark as gcrypt for the most important encryption modes. It is also good for hash impls.
This series thus does a number of things
- Introduce gnutls as a backe driver
- Change priority order gnutls > gcrypt > nettle > builtin
- Cleanup cruft from older versions of crypto libraries
- Make some tests more robust and easier to debug
- Drop support for built-in XTS impl, as it is too slow to be useful for LUKS
- Drop support for built-in DES impl, to minize amount of custom crypto code carried. VNC password auth will require use of an grypt/nettle/gnutls
Daniel P. Berrangé (18):
crypto: remove conditional around 3DES crypto test cases
crypto: remove obsolete crypto test condition
crypto: skip essiv ivgen tests if AES+ECB isn't available
crypto: use &error_fatal in crypto tests
crypto: fix gcrypt min version 1.8 regression
crypto: drop gcrypt thread initialization code
crypto: drop custom XTS support in gcrypt driver
crypto: add crypto tests for single block DES-ECB and DES-CBC
crypto: delete built-in DES implementation
crypto: delete built-in XTS cipher mode support
crypto: rename des-rfb cipher to just des
crypto: flip priority of backends to prefer gcrypt
crypto: introduce build system for gnutls crypto backend
crypto: add gnutls cipher provider
crypto: add gnutls hash provider
crypto: add gnutls hmac provider
crypto: add gnutls pbkdf provider
crypto: prefer gnutls as the crypto backend if new enough
crypto/cipher-builtin.c.inc | 132 ---------- crypto/cipher-gcrypt.c.inc | 143 +---------- crypto/cipher-gnutls.c.inc | 325 +++++++++++++++++++++++++ crypto/cipher-nettle.c.inc | 26 +- crypto/cipher.c | 30 +-- crypto/desrfb.c | 416 -------------------------------- crypto/hash-gnutls.c | 104 ++++++++ crypto/hmac-gnutls.c | 136 +++++++++++ crypto/init.c | 62 ----- crypto/meson.build | 9 +- crypto/pbkdf-gnutls.c | 90 +++++++ meson.build | 102 +++++--- qapi/crypto.json | 4 +- tests/unit/test-crypto-cipher.c | 31 ++- tests/unit/test-crypto-hash.c | 12 +- tests/unit/test-crypto-hmac.c | 28 +-- tests/unit/test-crypto-ivgen.c | 14 +- tests/unit/test-crypto-pbkdf.c | 5 +- ui/vnc.c | 20 +- 19 files changed, 814 insertions(+), 875 deletions(-) create mode 100644 crypto/cipher-gnutls.c.inc delete mode 100644 crypto/desrfb.c create mode 100644 crypto/hash-gnutls.c create mode 100644 crypto/hmac-gnutls.c create mode 100644 crypto/pbkdf-gnutls.c
Signature · 1 line
2.31.1
PATCH 1/18 crypto: remove conditional around 3DES crypto test cases
The main method checks whether the cipher choice is supported at runtime, so there is no need for compile time conditions.
--- tests/unit/test-crypto-cipher.c | 2 -- 1 file changed, 2 deletions(-)
tests/unit/test-crypto-cipher.c +0 -2
@@ -165,7 +165,6 @@ static QCryptoCipherTestData test_data[] = {"ffd29f1bb5596ad94ea2d8e6196b7f09" "30d8ed0bf2773af36dd82a6280c20926", }, -#if defined(CONFIG_NETTLE ) || defined(CONFIG_GCRYPT ) {/* Borrowed from linux-kernel crypto/testmgr.h */ .path = "/crypto/cipher/3des-cbc",
@@ -283,7 +282,6 @@ static QCryptoCipherTestData test_data[] = {"407772c2ea0e3a7846b991b6e73d5142" "fd51b0c62c6313785ceefccfc4700034", }, -#endif {/* RFC 2144, Appendix B.1 */ .path = "/crypto/cipher/cast5-128",
Signature · 1 line
2.31.1
Re: [PATCH 01/18] crypto: remove conditional around 3DES crypto test cases
Quoted 7 lines · Daniel P. Berrangé
The main method checks whether the cipher choice is supported at runtime, so there is no need for compile time conditions.
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/unit/test-crypto-cipher.c | 2 -- 1 file changed, 2 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 2/18 crypto: remove obsolete crypto test condition
In reply to Daniel P. BerrangéSince we now require gcrypt >= 1.8.0, there is no need to exclude the pbkdf test case.
--- tests/unit/test-crypto-pbkdf.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
tests/unit/test-crypto-pbkdf.c +1 -4
@@ -229,10 +229,8 @@ static QCryptoPbkdfTestData test_data[] = { },/* non-RFC misc test data */ -#ifdef CONFIG_NETTLE { -/* empty password test. -* Broken with libgcrypt <= 1.5.0, hence CONFIG_NETTLE */ +/* empty password test. */ .path = "/crypto/pbkdf/nonrfc/sha1/iter2", .hash = QCRYPTO_HASH_ALG_SHA1, .iterations = 2,
@@ -244,7 +242,6 @@ static QCryptoPbkdfTestData test_data[] = { "\xbf\x03\xe1\x1c\x71\xca\x79\x4e\x07\x97", .nout =20 }, -#endif {/* Password exceeds block size test */ .path = "/crypto/pbkdf/nonrfc/sha256/iter1200",
Signature · 1 line
2.31.1
Re: [PATCH 02/18] crypto: remove obsolete crypto test condition
Quoted 7 lines · Daniel P. Berrangé
Since we now require gcrypt >= 1.8.0, there is no need to exclude the pbkdf test case.
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/unit/test-crypto-pbkdf.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 3/18 crypto: skip essiv ivgen tests if AES+ECB isn't available
In reply to Daniel P. Berrangé--- tests/unit/test-crypto-ivgen.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
tests/unit/test-crypto-ivgen.c +9 -5
@@ -136,8 +136,15 @@ struct QCryptoIVGenTestData {static void test_ivgen (const void *opaque) {const struct QCryptoIVGenTestData *data = opaque; -uint8_t *iv =g_new0 (uint8_t, data->niv); - QCryptoIVGen *ivgen = qcrypto_ivgen_new( + g_autofree uint8_t *iv = g_new0(uint8_t, data->niv); +g_autoptr (QCryptoIVGen) ivgen =NULL ; + +if (!qcrypto_cipher_supports (data->cipheralg, +QCRYPTO_CIPHER_MODE_ECB )) { +return ; + } + + ivgen = qcrypto_ivgen_new( data->ivalg, data->cipheralg, data->hashalg,
@@ -152,9 +159,6 @@ static void test_ivgen(const void *opaque) &error_abort);g_assert (memcmp (iv, data->iv, data->niv) ==0 ); - -qcrypto_ivgen_free (ivgen); -g_free (iv); }int main (int argc,char **argv)
Signature · 1 line
2.31.1
Re: [PATCH 03/18] crypto: skip essiv ivgen tests if AES+ECB isn't available
Quoted 5 lines · Daniel P. Berrangé
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/unit/test-crypto-ivgen.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 4/18 crypto: use &error_fatal in crypto tests
In reply to Daniel P. BerrangéUsing error_fatal provides better diagnostics when tests failed, than using asserts, because we see the text of the error message.
--- tests/unit/test-crypto-hash.c | 12 ++++++------ tests/unit/test-crypto-hmac.c | 28 ++++++++-------------------- 2 files changed, 14 insertions(+), 26 deletions(-)
tests/unit/test-crypto-hash.c +6 -6
@@ -104,7 +104,7 @@ static void test_hash_alloc(void)strlen (INPUT_TEXT ), &result, &resultlen, - NULL); + &error_fatal);g_assert (ret ==0 );g_assert (resultlen == expected_lens[i]);
@@ -139,7 +139,7 @@ static void test_hash_prealloc(void)strlen (INPUT_TEXT ), &result, &resultlen, - NULL); + &error_fatal);g_assert (ret ==0 );g_assert (resultlen == expected_lens[i]);
@@ -176,7 +176,7 @@ static void test_hash_iov(void) iov,3 , &result, &resultlen, - NULL); + &error_fatal);g_assert (ret ==0 );g_assert (resultlen == expected_lens[i]);for (j =0 ; j < resultlen; j++) {
@@ -210,7 +210,7 @@ static void test_hash_digest(void)INPUT_TEXT ,strlen (INPUT_TEXT ), &digest, - NULL); + &error_fatal);g_assert (ret ==0 ); g_assert_cmpstr(digest, ==, expected_outputs[i]);g_free (digest);
@@ -234,7 +234,7 @@ static void test_hash_base64(void)INPUT_TEXT ,strlen (INPUT_TEXT ), &digest, - NULL); + &error_fatal);g_assert (ret ==0 ); g_assert_cmpstr(digest, ==, expected_outputs_b64[i]);g_free (digest);
@@ -243,7 +243,7 @@ static void test_hash_base64(void)int main (int argc,char **argv) { -g_assert (qcrypto_init (NULL ) ==0 ); +g_assert (qcrypto_init (&error_fatal) ==0 );g_test_init (&argc, &argv,NULL );g_test_add_func ("/crypto/hash/iov" , test_hash_iov);
tests/unit/test-crypto-hmac.c +8 -20
@@ -89,7 +89,6 @@ static void test_hmac_alloc(void)QCryptoHmac *hmac =NULL ;uint8_t *result =NULL ;size_t resultlen =0 ; -Error *err =NULL ;const char *exp_output =NULL ;int ret;size_t j;
@@ -101,14 +100,12 @@ static void test_hmac_alloc(void) exp_output = data->hex_digest; hmac =qcrypto_hmac_new (data->alg, (const uint8_t *)KEY , -strlen (KEY ), &err); -g_assert (err ==NULL ); +strlen (KEY ), &error_fatal);g_assert (hmac !=NULL ); ret =qcrypto_hmac_bytes (hmac, (const char *)INPUT_TEXT ,strlen (INPUT_TEXT ), &result, - &resultlen, &err); -g_assert (err ==NULL ); + &resultlen, &error_fatal);g_assert (ret ==0 );for (j =0 ; j < resultlen; j++) {
@@ -131,7 +128,6 @@ static void test_hmac_prealloc(void)QCryptoHmac *hmac =NULL ;uint8_t *result =NULL ;size_t resultlen =0 ; -Error *err =NULL ;const char *exp_output =NULL ;int ret;size_t j;
@@ -146,14 +142,12 @@ static void test_hmac_prealloc(void) result =g_new0 (uint8_t, resultlen); hmac =qcrypto_hmac_new (data->alg, (const uint8_t *)KEY , -strlen (KEY ), &err); -g_assert (err ==NULL ); +strlen (KEY ), &error_fatal);g_assert (hmac !=NULL ); ret =qcrypto_hmac_bytes (hmac, (const char *)INPUT_TEXT ,strlen (INPUT_TEXT ), &result, - &resultlen, &err); -g_assert (err ==NULL ); + &resultlen, &error_fatal);g_assert (ret ==0 ); exp_output = data->hex_digest;
@@ -177,7 +171,6 @@ static void test_hmac_iov(void)QCryptoHmac *hmac =NULL ;uint8_t *result =NULL ;size_t resultlen =0 ; -Error *err =NULL ;const char *exp_output =NULL ;int ret;size_t j;
@@ -194,13 +187,11 @@ static void test_hmac_iov(void) exp_output = data->hex_digest; hmac =qcrypto_hmac_new (data->alg, (const uint8_t *)KEY , -strlen (KEY ), &err); -g_assert (err ==NULL ); +strlen (KEY ), &error_fatal);g_assert (hmac !=NULL ); ret =qcrypto_hmac_bytesv (hmac, iov,3 , &result, - &resultlen, &err); -g_assert (err ==NULL ); + &resultlen, &error_fatal);g_assert (ret ==0 );for (j =0 ; j < resultlen; j++) {
@@ -222,7 +213,6 @@ static void test_hmac_digest(void)QCryptoHmacTestData *data = &test_data[i];QCryptoHmac *hmac =NULL ;uint8_t *result =NULL ; -Error *err =NULL ;const char *exp_output =NULL ;int ret;
@@ -233,14 +223,12 @@ static void test_hmac_digest(void) exp_output = data->hex_digest; hmac =qcrypto_hmac_new (data->alg, (const uint8_t *)KEY , -strlen (KEY ), &err); -g_assert (err ==NULL ); +strlen (KEY ), &error_fatal);g_assert (hmac !=NULL ); ret =qcrypto_hmac_digest (hmac, (const char *)INPUT_TEXT ,strlen (INPUT_TEXT ), (char **)&result, - &err); -g_assert (err ==NULL ); + &error_fatal);g_assert (ret ==0 ); g_assert_cmpstr((const char *)result, ==, exp_output);
Signature · 1 line
2.31.1
Re: [PATCH 04/18] crypto: use &error_fatal in crypto tests
Quoted 15 lines · Daniel P. Berrangé
Using
error_fatalprovides better diagnostics when tests failed, than using asserts, because we see the text of the error message.Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/unit/test-crypto-hash.c | 12 ++++++------ tests/unit/test-crypto-hmac.c | 28 ++++++++-------------------- 2 files changed, 14 insertions(+), 26 deletions(-)tests/unit/test-crypto-hash.c +0
-1--- a/tests/unit/test-crypto-hash.c
tests/unit/test-crypto-hash.c static void test_hash_base64(void) · line 246
Quoted 5 lines
tests/unit/test-crypto-hash.c +1
-1@@ -243,7 +243,7 @@ static void test_hash_base64(void)int main (int argc,char **argv) { -g_assert (qcrypto_init (NULL ) ==0 ); +g_assert (qcrypto_init (&error_fatal) ==0 );
This is a side effect inside a g_assert(). It might be worth cleaning it up while you are touching here. But since it is pre-existing, it doesn't affect my:
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 5/18 crypto: fix gcrypt min version 1.8 regression
In reply to Daniel P. BerrangéThe min gcrypt was bumped:
commit b33a84632a3759c00320fd80923aa963c11207fc
Author: Daniel P. Berrangé <[email protected]>
Date: Fri May 14 13:04:08 2021 +0100
crypto: bump min gcrypt to 1.8.0, dropping RHEL-7 support
but this was accidentally lost in conflict resolution for
commit 5761251138cb69c310e9df7dfc82c4c6fd2444e4
Author: Paolo Bonzini <[email protected]>
Date: Thu Jun 3 11:15:26 2021 +0200
configure, meson: convert crypto detection to meson
--- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
meson.build +1 -1
@@ -834,7 +834,7 @@ elif (not get_option('nettle').auto() or have_system) and not get_option('gcrypt endif endif if (not get_option('gcrypt').auto() or have_system) and not nettle.found() - gcrypt = dependency('libgcrypt', version: '>=1.5', + gcrypt = dependency('libgcrypt', version: '>=1.8', method: 'config-tool', required: get_option('gcrypt'), kwargs: static_kwargs)
Signature · 1 line
2.31.1
Re: [PATCH 05/18] crypto: fix gcrypt min version 1.8 regression
Quoted 14 lines · Daniel P. Berrangé
The min gcrypt was bumped:
commit
b33a84632a3759c00320fd80923aa963c11207fc
Author: Daniel P. Berrangé <[email protected]>
Date: Fri May 14 13:04:08 2021 +0100crypto: bump min gcrypt to 1.8.0, dropping RHEL-7 support
but this was accidentally lost in conflict resolution for
commit
5761251138cb69c310e9df7dfc82c4c6fd2444e4
Author: Paolo Bonzini <[email protected]>
Date: Thu Jun 3 11:15:26 2021 +0200
Quoted 6 lines
configure, meson: convert crypto detection to meson
Signed-off-by Daniel P. Berrangé <[email protected]>--- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 6/18 crypto: drop gcrypt thread initialization code
In reply to Daniel P. BerrangéThis is only required on gcrypt < 1.6.0, and is thus obsolete since
commit b33a84632a3759c00320fd80923aa963c11207fc
Author: Daniel P. Berrangé <[email protected]>
Date: Fri May 14 13:04:08 2021 +0100
crypto: bump min gcrypt to 1.8.0, dropping RHEL-7 support
--- crypto/init.c | 62 --------------------------------------------------- 1 file changed, 62 deletions(-)
crypto/init.c +0 -62
@@ -35,21 +35,6 @@#include "crypto/random.h" /* #define DEBUG_GNUTLS */ - -/* -* We need to init gcrypt threading if -* -* - gcrypt < 1.6.0 -* -*/ - -#if (defined(CONFIG_GCRYPT ) && \ - (GCRYPT_VERSION_NUMBER <0x010600 )) -#define QCRYPTO_INIT_GCRYPT_THREADS -#else -#undef QCRYPTO_INIT_GCRYPT_THREADS -#endif -#ifdef DEBUG_GNUTLS static void qcrypto_gnutls_log (int level,const char *str) {
@@ -57,55 +42,8 @@ static void qcrypto_gnutls_log(int level, const char *str) }#endif -#ifdef QCRYPTO_INIT_GCRYPT_THREADS -static int qcrypto_gcrypt_mutex_init (void **priv) -{ \ -QemuMutex *lock =NULL ; - lock =g_new0 (QemuMutex,1 ); -qemu_mutex_init (lock); - *priv = lock; -return 0 ; -} - -static int qcrypto_gcrypt_mutex_destroy (void **priv) -{ -QemuMutex *lock = *priv; -qemu_mutex_destroy (lock); -g_free (lock); -return 0 ; -} - -static int qcrypto_gcrypt_mutex_lock (void **priv) -{ -QemuMutex *lock = *priv; -qemu_mutex_lock (lock); -return 0 ; -} - -static int qcrypto_gcrypt_mutex_unlock (void **priv) -{ -QemuMutex *lock = *priv; -qemu_mutex_unlock (lock); -return 0 ; -} - -static struct gcry_thread_cbs qcrypto_gcrypt_thread_impl = { - (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION <<8 )), -NULL , - qcrypto_gcrypt_mutex_init, - qcrypto_gcrypt_mutex_destroy, - qcrypto_gcrypt_mutex_lock, - qcrypto_gcrypt_mutex_unlock, -NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL ,NULL -}; -#endif /* QCRYPTO_INIT_GCRYPT */ -int qcrypto_init (Error **errp) { -#ifdef QCRYPTO_INIT_GCRYPT_THREADS -gcry_control (GCRYCTL_SET_THREAD_CBS , &qcrypto_gcrypt_thread_impl); -#endif /* QCRYPTO_INIT_GCRYPT_THREADS */ -#ifdef CONFIG_GNUTLS int ret; ret =gnutls_global_init ();
Signature · 1 line
2.31.1
Re: [PATCH 06/18] crypto: drop gcrypt thread initialization code
Quoted 7 lines · Daniel P. Berrangé
This is only required on gcrypt < 1.6.0, and is thus obsolete since
commit
b33a84632a3759c00320fd80923aa963c11207fc
Author: Daniel P. Berrangé <[email protected]>
Date: Fri May 14 13:04:08 2021 +0100
Quoted 6 lines
crypto: bump min gcrypt to 1.8.0, dropping RHEL-7 support
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/init.c | 62 --------------------------------------------------- 1 file changed, 62 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 7/18 crypto: drop custom XTS support in gcrypt driver
In reply to Daniel P. BerrangéThe XTS cipher mode was introduced in gcrypt 1.8.0, which matches QEMU's current minimum version.
--- crypto/cipher-gcrypt.c.inc | 127 ------------------------------------- meson.build | 14 +--- 2 files changed, 1 insertion(+), 140 deletions(-)
crypto/cipher-gcrypt.c.inc +0 -127
@@ -18,10 +18,6 @@ * */ -#ifdef CONFIG_QEMU_PRIVATE_XTS -#include "crypto/xts.h" -#endif - #include <gcrypt.h> bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
@@ -59,10 +55,6 @@ typedef struct QCryptoCipherGcrypt { QCryptoCipher base; gcry_cipher_hd_t handle; size_t blocksize; -#ifdef CONFIG_QEMU_PRIVATE_XTS - gcry_cipher_hd_t tweakhandle; - uint8_t iv[XTS_BLOCK_SIZE]; -#endif } QCryptoCipherGcrypt;
@@ -178,90 +170,6 @@ static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = { .cipher_free = qcrypto_gcrypt_ctx_free, }; -#ifdef CONFIG_QEMU_PRIVATE_XTS -static void qcrypto_gcrypt_xts_ctx_free(QCryptoCipher *cipher) -{ - QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); - - gcry_cipher_close(ctx->tweakhandle); - qcrypto_gcrypt_ctx_free(cipher); -} - -static void qcrypto_gcrypt_xts_wrape(const void *ctx, size_t length, - uint8_t *dst, const uint8_t *src) -{ - gcry_error_t err; - err = gcry_cipher_encrypt((gcry_cipher_hd_t)ctx, dst, length, src, length); - g_assert(err == 0); -} - -static void qcrypto_gcrypt_xts_wrapd(const void *ctx, size_t length, - uint8_t *dst, const uint8_t *src) -{ - gcry_error_t err; - err = gcry_cipher_decrypt((gcry_cipher_hd_t)ctx, dst, length, src, length); - g_assert(err == 0); -} - -static int qcrypto_gcrypt_xts_encrypt(QCryptoCipher *cipher, const void *in, - void *out, size_t len, Error **errp) -{ - QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); - - if (len & (ctx->blocksize - 1)) { - error_setg(errp, "Length %zu must be a multiple of block size %zu", - len, ctx->blocksize); - return -1; - } - - xts_encrypt(ctx->handle, ctx->tweakhandle, - qcrypto_gcrypt_xts_wrape, qcrypto_gcrypt_xts_wrapd, - ctx->iv, len, out, in); - return 0; -} - -static int qcrypto_gcrypt_xts_decrypt(QCryptoCipher *cipher, const void *in, - void *out, size_t len, Error **errp) -{ - QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); - - if (len & (ctx->blocksize - 1)) { - error_setg(errp, "Length %zu must be a multiple of block size %zu", - len, ctx->blocksize); - return -1; - } - - xts_decrypt(ctx->handle, ctx->tweakhandle, - qcrypto_gcrypt_xts_wrape, qcrypto_gcrypt_xts_wrapd, - ctx->iv, len, out, in); - return 0; -} - -static int qcrypto_gcrypt_xts_setiv(QCryptoCipher *cipher, - const uint8_t *iv, size_t niv, - Error **errp) -{ - QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base); - - if (niv != ctx->blocksize) { - error_setg(errp, "Expected IV size %zu not %zu", - ctx->blocksize, niv); - return -1; - } - - memcpy(ctx->iv, iv, niv); - return 0; -} - -static const struct QCryptoCipherDriver qcrypto_gcrypt_xts_driver = { - .cipher_encrypt = qcrypto_gcrypt_xts_encrypt, - .cipher_decrypt = qcrypto_gcrypt_xts_decrypt, - .cipher_setiv = qcrypto_gcrypt_xts_setiv, - .cipher_free = qcrypto_gcrypt_xts_ctx_free, -}; -#endif /* CONFIG_QEMU_PRIVATE_XTS */ - - static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, QCryptoCipherMode mode, const uint8_t *key,
@@ -323,12 +231,7 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, gcrymode = GCRY_CIPHER_MODE_ECB; break; case QCRYPTO_CIPHER_MODE_XTS: -#ifdef CONFIG_QEMU_PRIVATE_XTS - drv = &qcrypto_gcrypt_xts_driver; - gcrymode = GCRY_CIPHER_MODE_ECB; -#else gcrymode = GCRY_CIPHER_MODE_XTS; -#endif break; case QCRYPTO_CIPHER_MODE_CBC: gcrymode = GCRY_CIPHER_MODE_CBC;
@@ -354,23 +257,6 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, } ctx->blocksize = gcry_cipher_get_algo_blklen(gcryalg); -#ifdef CONFIG_QEMU_PRIVATE_XTS - if (mode == QCRYPTO_CIPHER_MODE_XTS) { - if (ctx->blocksize != XTS_BLOCK_SIZE) { - error_setg(errp, - "Cipher block size %zu must equal XTS block size %d", - ctx->blocksize, XTS_BLOCK_SIZE); - goto error; - } - err = gcry_cipher_open(&ctx->tweakhandle, gcryalg, gcrymode, 0); - if (err != 0) { - error_setg(errp, "Cannot initialize cipher: %s", - gcry_strerror(err)); - goto error; - } - } -#endif - if (alg == QCRYPTO_CIPHER_ALG_DES_RFB) { /* We're using standard DES cipher from gcrypt, so we need * to munge the key so that the results are the same as the
@@ -380,16 +266,6 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, err = gcry_cipher_setkey(ctx->handle, rfbkey, nkey); g_free(rfbkey); } else { -#ifdef CONFIG_QEMU_PRIVATE_XTS - if (mode == QCRYPTO_CIPHER_MODE_XTS) { - nkey /= 2; - err = gcry_cipher_setkey(ctx->tweakhandle, key + nkey, nkey); - if (err != 0) { - error_setg(errp, "Cannot set key: %s", gcry_strerror(err)); - goto error; - } - } -#endif err = gcry_cipher_setkey(ctx->handle, key, nkey); } if (err != 0) {
@@ -400,9 +276,6 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, return &ctx->base; error: -#ifdef CONFIG_QEMU_PRIVATE_XTS - gcry_cipher_close(ctx->tweakhandle); -#endif gcry_cipher_close(ctx->handle); g_free(ctx); return NULL;
meson.build +1 -13
@@ -838,16 +838,7 @@ if (not get_option('gcrypt').auto() or have_system) and not nettle.found() method: 'config-tool', required: get_option('gcrypt'), kwargs: static_kwargs) - if gcrypt.found() and cc.compiles(''' - #include <gcrypt.h> - int main(void) { - gcry_cipher_hd_t handle; - gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_XTS, 0); - return 0; - } - ''', dependencies: gcrypt) - xts = 'gcrypt' - endif + xts = 'gcrypt' # Debian has removed -lgpg-error from libgcrypt-config # as it "spreads unnecessary dependencies" which in # turn breaks static builds...
@@ -2731,9 +2722,6 @@ summary_info += {'TLS priority': config_host['CONFIG_TLS_PRIORITY']} summary_info += {'GNUTLS support': gnutls.found()} # TODO: add back version summary_info += {'libgcrypt': gcrypt.found()} -if gcrypt.found() - summary_info += {' XTS': xts != 'private'} -endif # TODO: add back version summary_info += {'nettle': nettle.found()} if nettle.found()
Signature · 1 line
2.31.1
Re: [PATCH 07/18] crypto: drop custom XTS support in gcrypt driver
Quoted 8 lines · Daniel P. Berrangé
The XTS cipher mode was introduced in gcrypt 1.8.0, which matches QEMU's current minimum version.
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/cipher-gcrypt.c.inc | 127 ------------------------------------- meson.build | 14 +--- 2 files changed, 1 insertion(+), 140 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 8/18 crypto: add crypto tests for single block DES-ECB and DES-CBC
In reply to Daniel P. BerrangéThe GNUTLS crypto provider doesn't support DES-ECB, only DES-CBC. We can use the latter to simulate the former, if we encrypt only 1 block (8 bytes) of data at a time, using a all-zeros IV. This is a very inefficient way to use the QCryptoCipher APIs, but since the VNC authentication challenge is only 16 bytes, this is acceptable. No other part of QEMU should be using DES. This test case demonstrates the equivalence of ECB and CBC for the single-block case.
--- tests/unit/test-crypto-cipher.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
tests/unit/test-crypto-cipher.c +23 -0
@@ -149,6 +149,29 @@ static QCryptoCipherTestData test_data[] = {"39f23369a9d9bacfa530e26304231461" "b2eb05e2c39be9fcda6c19078c6a9d1b", }, + { +/* +* Testing 'password' as plaintext fits +* in single AES block, and gives identical +* ciphertext in ECB and CBC modes +*/ + .path = "/crypto/cipher/des-rfb-ecb-56-one-block", + .alg = QCRYPTO_CIPHER_ALG_DES_RFB, + .mode = QCRYPTO_CIPHER_MODE_ECB, + .key = "0123456789abcdef", + .plaintext = "70617373776f7264", + .ciphertext = "73fa80b66134e403", + }, + { +/* See previous comment */ + .path = "/crypto/cipher/des-rfb-cbc-56-one-block", + .alg = QCRYPTO_CIPHER_ALG_DES_RFB, + .mode = QCRYPTO_CIPHER_MODE_CBC, + .key = "0123456789abcdef", + .iv = "0000000000000000", + .plaintext = "70617373776f7264", + .ciphertext = "73fa80b66134e403", + }, { .path = "/crypto/cipher/des-rfb-ecb-56", .alg = QCRYPTO_CIPHER_ALG_DES_RFB,
Signature · 1 line
2.31.1
Re: [PATCH 08/18] crypto: add crypto tests for single block DES-ECB and DES-CBC
Quoted 1 line · Daniel P. Berrangé
The GNUTLS crypto provider doesn't support DES-ECB, only DES-CBC.
I had to go research these terms; DES-ECB is weaker (each block encrypted on its own), DES-CBC is stronger (the encryption of later blocks depend on the earlier text). Makes sense that GNUTLS has dropped support for the weaker form.
Quoted 2 lines
We can use the latter to simulate the former, if we encrypt only 1 block (8 bytes) of data at a time, using a all-zeros IV. This
using an all-zeros
Quoted 5 lines
is a very inefficient way to use the QCryptoCipher APIs, but since the VNC authentication challenge is only 16 bytes, this is acceptable. No other part of QEMU should be using DES. This test case demonstrates the equivalence of ECB and CBC for the single-block case.
Agreed - both on the inefficiency (we're throwing away all the work spent on chaining the later blocks - thankfully there is only one such block in our 16-byte challenge), and on the fact that DES should be avoided where possible (our sole use is due to VNC's less-than-stellar "security").
Quoted 5 lines
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/unit/test-crypto-cipher.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH 08/18] crypto: add crypto tests for single block DES-ECB and DES-CBC
Quoted 19 lines · Eric Blake
Quoted 1 line · Daniel P. Berrangé
The GNUTLS crypto provider doesn't support DES-ECB, only DES-CBC.
I had to go research these terms; DES-ECB is weaker (each block encrypted on its own), DES-CBC is stronger (the encryption of later blocks depend on the earlier text). Makes sense that GNUTLS has dropped support for the weaker form.
Quoted 2 lines
We can use the latter to simulate the former, if we encrypt only 1 block (8 bytes) of data at a time, using a all-zeros IV. This
using an all-zeros
Quoted 5 lines
is a very inefficient way to use the QCryptoCipher APIs, but since the VNC authentication challenge is only 16 bytes, this is acceptable. No other part of QEMU should be using DES. This test case demonstrates the equivalence of ECB and CBC for the single-block case.
Quoted 5 lines
Agreed - both on the inefficiency (we're throwing away all the work spent on chaining the later blocks - thankfully there is only one such block in our 16-byte challenge), and on the fact that DES should be avoided where possible (our sole use is due to VNC's less-than-stellar "security").
Actually there isn't any work wasted chaining blocks, because we're only writing one block of data.
The inefficiency is because we have to constantly re-create the cipher context object after every 8 bytes. This massively dominates over the cipher speed.
Quoted 8 lines
Quoted 6 lines
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/unit/test-crypto-cipher.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)Reviewed-by Eric Blake <[email protected]>
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH 9/18 crypto: delete built-in DES implementation
In reply to Daniel P. BerrangéThe built-in DES implementation is used for the VNC server password authentication scheme. When building system emulators it is reasonable to expect that an external crypto library is being used. It is thus not worth keeping a home grown DES implementation in tree.
--- crypto/cipher-builtin.c.inc | 72 ------- crypto/desrfb.c | 416 ------------------------------------ crypto/meson.build | 1 - 3 files changed, 489 deletions(-) delete mode 100644 crypto/desrfb.c
crypto/cipher-builtin.c.inc +0 -72
@@ -19,7 +19,6 @@ */ #include "crypto/aes.h" -#include "crypto/desrfb.h" #include "crypto/xts.h" typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext;
@@ -265,69 +264,10 @@ static const struct QCryptoCipherDriver qcrypto_cipher_aes_driver_xts = { }; -typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB; -struct QCryptoCipherBuiltinDESRFB { - QCryptoCipher base; - - /* C.f. alg_key_len[QCRYPTO_CIPHER_ALG_DES_RFB] */ - uint8_t key[8]; -}; - -static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher, - const void *in, void *out, - size_t len, Error **errp) -{ - QCryptoCipherBuiltinDESRFB *ctx - = container_of(cipher, QCryptoCipherBuiltinDESRFB, base); - size_t i; - - if (!qcrypto_length_check(len, 8, errp)) { - return -1; - } - - deskey(ctx->key, EN0); - - for (i = 0; i < len; i += 8) { - des((void *)in + i, out + i); - } - - return 0; -} - -static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher, - const void *in, void *out, - size_t len, Error **errp) -{ - QCryptoCipherBuiltinDESRFB *ctx - = container_of(cipher, QCryptoCipherBuiltinDESRFB, base); - size_t i; - - if (!qcrypto_length_check(len, 8, errp)) { - return -1; - } - - deskey(ctx->key, DE1); - - for (i = 0; i < len; i += 8) { - des((void *)in + i, out + i); - } - - return 0; -} - -static const struct QCryptoCipherDriver qcrypto_cipher_des_rfb_driver = { - .cipher_encrypt = qcrypto_cipher_encrypt_des_rfb, - .cipher_decrypt = qcrypto_cipher_decrypt_des_rfb, - .cipher_setiv = qcrypto_cipher_no_setiv, - .cipher_free = qcrypto_cipher_ctx_free, -}; - bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, QCryptoCipherMode mode) { switch (alg) { - case QCRYPTO_CIPHER_ALG_DES_RFB: - return mode == QCRYPTO_CIPHER_MODE_ECB; case QCRYPTO_CIPHER_ALG_AES_128: case QCRYPTO_CIPHER_ALG_AES_192: case QCRYPTO_CIPHER_ALG_AES_256:
@@ -356,18 +296,6 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, } switch (alg) { - case QCRYPTO_CIPHER_ALG_DES_RFB: - if (mode == QCRYPTO_CIPHER_MODE_ECB) { - QCryptoCipherBuiltinDESRFB *ctx; - - ctx = g_new0(QCryptoCipherBuiltinDESRFB, 1); - ctx->base.driver = &qcrypto_cipher_des_rfb_driver; - memcpy(ctx->key, key, sizeof(ctx->key)); - - return &ctx->base; - } - goto bad_mode; - case QCRYPTO_CIPHER_ALG_AES_128: case QCRYPTO_CIPHER_ALG_AES_192: case QCRYPTO_CIPHER_ALG_AES_256:
crypto/desrfb.c deleted +0 -416
416 lines not on this page · raw message
crypto/meson.build +0 -1
@@ -5,7 +5,6 @@ crypto_ss.add(files( 'block-qcow.c', 'block.c', 'cipher.c', - 'desrfb.c', 'hash.c', 'hmac.c', 'ivgen-essiv.c',
Signature · 1 line
2.31.1
Re: [PATCH 09/18] crypto: delete built-in DES implementation
Quoted 5 lines · Daniel P. Berrangé
The built-in DES implementation is used for the VNC server password authentication scheme. When building system emulators it is reasonable to expect that an external crypto library is being used. It is thus not worth keeping a home grown DES implementation in tree.
Quoted 7 lines
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/cipher-builtin.c.inc | 72 ------- crypto/desrfb.c | 416 ------------------------------------ crypto/meson.build | 1 - 3 files changed, 489 deletions(-) delete mode 100644 crypto/desrfb.c
Nice diffstat!
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 10/18 crypto: delete built-in XTS cipher mode support
In reply to Daniel P. BerrangéThe built-in AES+XTS implementation is used for the LUKS encryption When building system emulators it is reasonable to expect that an external crypto library is being used instead. The performance of the builtin XTS implementation is terrible as it has no CPU acceleration support. It is thus not worth keeping a home grown XTS implementation for the built-in cipher backend.
--- crypto/cipher-builtin.c.inc | 60 ------------------------------------- crypto/meson.build | 6 ++-- meson.build | 7 ++--- 3 files changed, 6 insertions(+), 67 deletions(-)
crypto/cipher-builtin.c.inc +0 -60
@@ -19,7 +19,6 @@ */ #include "crypto/aes.h" -#include "crypto/xts.h" typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext; struct QCryptoCipherBuiltinAESContext {
@@ -31,7 +30,6 @@ typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES; struct QCryptoCipherBuiltinAES { QCryptoCipher base; QCryptoCipherBuiltinAESContext key; - QCryptoCipherBuiltinAESContext key_tweak; uint8_t iv[AES_BLOCK_SIZE]; };
@@ -193,39 +191,6 @@ static int qcrypto_cipher_aes_decrypt_cbc(QCryptoCipher *cipher, return 0; } -static int qcrypto_cipher_aes_encrypt_xts(QCryptoCipher *cipher, - const void *in, void *out, - size_t len, Error **errp) -{ - QCryptoCipherBuiltinAES *ctx - = container_of(cipher, QCryptoCipherBuiltinAES, base); - - if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) { - return -1; - } - xts_encrypt(&ctx->key, &ctx->key_tweak, - do_aes_encrypt_ecb, do_aes_decrypt_ecb, - ctx->iv, len, out, in); - return 0; -} - -static int qcrypto_cipher_aes_decrypt_xts(QCryptoCipher *cipher, - const void *in, void *out, - size_t len, Error **errp) -{ - QCryptoCipherBuiltinAES *ctx - = container_of(cipher, QCryptoCipherBuiltinAES, base); - - if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) { - return -1; - } - xts_decrypt(&ctx->key, &ctx->key_tweak, - do_aes_encrypt_ecb, do_aes_decrypt_ecb, - ctx->iv, len, out, in); - return 0; -} - - static int qcrypto_cipher_aes_setiv(QCryptoCipher *cipher, const uint8_t *iv, size_t niv, Error **errp) {
@@ -256,14 +221,6 @@ static const struct QCryptoCipherDriver qcrypto_cipher_aes_driver_cbc = { .cipher_free = qcrypto_cipher_ctx_free, }; -static const struct QCryptoCipherDriver qcrypto_cipher_aes_driver_xts = { - .cipher_encrypt = qcrypto_cipher_aes_encrypt_xts, - .cipher_decrypt = qcrypto_cipher_aes_decrypt_xts, - .cipher_setiv = qcrypto_cipher_aes_setiv, - .cipher_free = qcrypto_cipher_ctx_free, -}; - - bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, QCryptoCipherMode mode) {
@@ -274,7 +231,6 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, switch (mode) { case QCRYPTO_CIPHER_MODE_ECB: case QCRYPTO_CIPHER_MODE_CBC: - case QCRYPTO_CIPHER_MODE_XTS: return true; default: return false;
@@ -310,9 +266,6 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, case QCRYPTO_CIPHER_MODE_CBC: drv = &qcrypto_cipher_aes_driver_cbc; break; - case QCRYPTO_CIPHER_MODE_XTS: - drv = &qcrypto_cipher_aes_driver_xts; - break; default: goto bad_mode; }
@@ -320,19 +273,6 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, ctx = g_new0(QCryptoCipherBuiltinAES, 1); ctx->base.driver = drv; - if (mode == QCRYPTO_CIPHER_MODE_XTS) { - nkey /= 2; - if (AES_set_encrypt_key(key + nkey, nkey * 8, - &ctx->key_tweak.enc)) { - error_setg(errp, "Failed to set encryption key"); - goto error; - } - if (AES_set_decrypt_key(key + nkey, nkey * 8, - &ctx->key_tweak.dec)) { - error_setg(errp, "Failed to set decryption key"); - goto error; - } - } if (AES_set_encrypt_key(key, nkey * 8, &ctx->key.enc)) { error_setg(errp, "Failed to set encryption key"); goto error;
crypto/meson.build +3 -3
@@ -23,14 +23,14 @@ crypto_ss.add(files( if nettle.found() crypto_ss.add(nettle, files('hash-nettle.c', 'hmac-nettle.c', 'pbkdf-nettle.c')) + if xts == 'private' + crypto_ss.add(files('xts.c')) + endif elif gcrypt.found() crypto_ss.add(gcrypt, files('hash-gcrypt.c', 'hmac-gcrypt.c', 'pbkdf-gcrypt.c')) else crypto_ss.add(files('hash-glib.c', 'hmac-glib.c', 'pbkdf-stub.c')) endif -if xts == 'private' - crypto_ss.add(files('xts.c')) -endif crypto_ss.add(when: 'CONFIG_SECRET_KEYRING', if_true: files('secret_keyring.c')) crypto_ss.add(when: 'CONFIG_AF_ALG', if_true: files('afalg.c', 'cipher-afalg.c', 'hash-afalg.c'))
meson.build +3 -4
@@ -821,7 +821,7 @@ endif # Nettle has priority over gcrypt gcrypt = not_found nettle = not_found -xts = 'private' +xts = 'none' if get_option('nettle').enabled() and get_option('gcrypt').enabled() error('Only one of gcrypt & nettle can be enabled') elif (not get_option('nettle').auto() or have_system) and not get_option('gcrypt').enabled()
@@ -829,8 +829,8 @@ elif (not get_option('nettle').auto() or have_system) and not get_option('gcrypt method: 'pkg-config', required: get_option('nettle'), kwargs: static_kwargs) - if nettle.found() and cc.has_header('nettle/xts.h', dependencies: nettle) - xts = 'nettle' + if nettle.found() and not cc.has_header('nettle/xts.h', dependencies: nettle) + xts = 'private' endif endif if (not get_option('gcrypt').auto() or have_system) and not nettle.found()
@@ -838,7 +838,6 @@ if (not get_option('gcrypt').auto() or have_system) and not nettle.found() method: 'config-tool', required: get_option('gcrypt'), kwargs: static_kwargs) - xts = 'gcrypt' # Debian has removed -lgpg-error from libgcrypt-config # as it "spreads unnecessary dependencies" which in # turn breaks static builds...
Signature · 1 line
2.31.1
Re: [PATCH 10/18] crypto: delete built-in XTS cipher mode support
Quoted 7 lines · Daniel P. Berrangé
The built-in AES+XTS implementation is used for the LUKS encryption When building system emulators it is reasonable to expect that an external crypto library is being used instead. The performance of the builtin XTS implementation is terrible as it has no CPU acceleration support. It is thus not worth keeping a home grown XTS implementation for the built-in cipher backend.
Quoted 6 lines
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/cipher-builtin.c.inc | 60 ------------------------------------- crypto/meson.build | 6 ++-- meson.build | 7 ++--- 3 files changed, 6 insertions(+), 67 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 11/18 crypto: rename des-rfb cipher to just des
In reply to Daniel P. BerrangéCurrently the crypto layer exposes support for a 'des-rfb' algorithm which is just normal single-DES, with the bits in each key byte reversed. This special key munging is required by the RFB protocol password authentication mechanism.
Since the crypto layer is generic shared code, it makes more sense to do the key byte munging in the VNC server code, and expose normal single-DES support.
--- crypto/cipher-gcrypt.c.inc | 16 +++------------- crypto/cipher-nettle.c.inc | 26 +++++++++++--------------- crypto/cipher.c | 28 +++++----------------------- qapi/crypto.json | 4 ++-- tests/unit/test-crypto-cipher.c | 18 +++++++++--------- ui/vnc.c | 20 +++++++++++++++++--- 6 files changed, 47 insertions(+), 65 deletions(-)
crypto/cipher-gcrypt.c.inc +3 -13
@@ -24,7 +24,7 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, QCryptoCipherMode mode) { switch (alg) { - case QCRYPTO_CIPHER_ALG_DES_RFB: + case QCRYPTO_CIPHER_ALG_DES: case QCRYPTO_CIPHER_ALG_3DES: case QCRYPTO_CIPHER_ALG_AES_128: case QCRYPTO_CIPHER_ALG_AES_192:
@@ -186,7 +186,7 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, } switch (alg) { - case QCRYPTO_CIPHER_ALG_DES_RFB: + case QCRYPTO_CIPHER_ALG_DES: gcryalg = GCRY_CIPHER_DES; break; case QCRYPTO_CIPHER_ALG_3DES:
@@ -257,17 +257,7 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, } ctx->blocksize = gcry_cipher_get_algo_blklen(gcryalg); - if (alg == QCRYPTO_CIPHER_ALG_DES_RFB) { - /* We're using standard DES cipher from gcrypt, so we need - * to munge the key so that the results are the same as the - * bizarre RFB variant of DES :-) - */ - uint8_t *rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey); - err = gcry_cipher_setkey(ctx->handle, rfbkey, nkey); - g_free(rfbkey); - } else { - err = gcry_cipher_setkey(ctx->handle, key, nkey); - } + err = gcry_cipher_setkey(ctx->handle, key, nkey); if (err != 0) { error_setg(errp, "Cannot set key: %s", gcry_strerror(err)); goto error;
crypto/cipher-nettle.c.inc +11 -15
@@ -235,11 +235,11 @@ static const struct QCryptoCipherDriver NAME##_driver_xts = { \ DEFINE_XTS(NAME, TYPE, BLEN, ENCRYPT, DECRYPT) -typedef struct QCryptoNettleDESRFB { +typedef struct QCryptoNettleDES { QCryptoCipher base; struct des_ctx key; uint8_t iv[DES_BLOCK_SIZE]; -} QCryptoNettleDESRFB; +} QCryptoNettleDES; static void des_encrypt_native(const void *ctx, size_t length, uint8_t *dst, const uint8_t *src)
@@ -253,7 +253,7 @@ static void des_decrypt_native(const void *ctx, size_t length, des_decrypt(ctx, length, dst, src); } -DEFINE_ECB_CBC_CTR(qcrypto_nettle_des_rfb, QCryptoNettleDESRFB, +DEFINE_ECB_CBC_CTR(qcrypto_nettle_des, QCryptoNettleDES, DES_BLOCK_SIZE, des_encrypt_native, des_decrypt_native)
@@ -431,7 +431,7 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, QCryptoCipherMode mode) { switch (alg) { - case QCRYPTO_CIPHER_ALG_DES_RFB: + case QCRYPTO_CIPHER_ALG_DES: case QCRYPTO_CIPHER_ALG_3DES: case QCRYPTO_CIPHER_ALG_AES_128: case QCRYPTO_CIPHER_ALG_AES_192:
@@ -480,32 +480,28 @@ static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, } switch (alg) { - case QCRYPTO_CIPHER_ALG_DES_RFB: + case QCRYPTO_CIPHER_ALG_DES: { - QCryptoNettleDESRFB *ctx; + QCryptoNettleDES *ctx; const QCryptoCipherDriver *drv; - uint8_t *rfbkey; switch (mode) { case QCRYPTO_CIPHER_MODE_ECB: - drv = &qcrypto_nettle_des_rfb_driver_ecb; + drv = &qcrypto_nettle_des_driver_ecb; break; case QCRYPTO_CIPHER_MODE_CBC: - drv = &qcrypto_nettle_des_rfb_driver_cbc; + drv = &qcrypto_nettle_des_driver_cbc; break; case QCRYPTO_CIPHER_MODE_CTR: - drv = &qcrypto_nettle_des_rfb_driver_ctr; + drv = &qcrypto_nettle_des_driver_ctr; break; default: goto bad_cipher_mode; } - ctx = g_new0(QCryptoNettleDESRFB, 1); + ctx = g_new0(QCryptoNettleDES, 1); ctx->base.driver = drv; - - rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey); - des_set_key(&ctx->key, rfbkey); - g_free(rfbkey); + des_set_key(&ctx->key, key); return &ctx->base; }
crypto/cipher.c +5 -23
@@ -29,7 +29,7 @@ static const size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = { [QCRYPTO_CIPHER_ALG_AES_128] = 16, [QCRYPTO_CIPHER_ALG_AES_192] = 24, [QCRYPTO_CIPHER_ALG_AES_256] = 32, - [QCRYPTO_CIPHER_ALG_DES_RFB] = 8, + [QCRYPTO_CIPHER_ALG_DES] = 8, [QCRYPTO_CIPHER_ALG_3DES] = 24, [QCRYPTO_CIPHER_ALG_CAST5_128] = 16, [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
@@ -44,7 +44,7 @@ static const size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = { [QCRYPTO_CIPHER_ALG_AES_128] = 16, [QCRYPTO_CIPHER_ALG_AES_192] = 16, [QCRYPTO_CIPHER_ALG_AES_256] = 16, - [QCRYPTO_CIPHER_ALG_DES_RFB] = 8, + [QCRYPTO_CIPHER_ALG_DES] = 8, [QCRYPTO_CIPHER_ALG_3DES] = 8, [QCRYPTO_CIPHER_ALG_CAST5_128] = 8, [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
@@ -107,9 +107,9 @@ qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg, }if (mode ==QCRYPTO_CIPHER_MODE_XTS ) { -if (alg ==QCRYPTO_CIPHER_ALG_DES_RFB - || alg ==QCRYPTO_CIPHER_ALG_3DES ) { -error_setg (errp,"XTS mode not compatible with DES-RFB/3DES" ); +if (alg ==QCRYPTO_CIPHER_ALG_DES || + alg ==QCRYPTO_CIPHER_ALG_3DES ) { +error_setg (errp,"XTS mode not compatible with DES/3DES" );return false; }if (nkey %2 ) {
@@ -132,24 +132,6 @@ qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,return true; } -#if defined(CONFIG_GCRYPT ) || defined(CONFIG_NETTLE ) -static uint8_t * -qcrypto_cipher_munge_des_rfb_key (const uint8_t *key, -size_t nkey) -{ -uint8_t *ret =g_new0 (uint8_t, nkey); -size_t i; -for (i =0 ; i < nkey; i++) { -uint8_t r = key[i]; - r = (r &0xf0 ) >>4 | (r &0x0f ) <<4 ; - r = (r &0xcc ) >>2 | (r &0x33 ) <<2 ; - r = (r &0xaa ) >>1 | (r &0x55 ) <<1 ; - ret[i] = r; - } -return ret; -} -#endif /* CONFIG_GCRYPT || CONFIG_NETTLE */ -#ifdef CONFIG_GCRYPT #include "cipher-gcrypt.c.inc" #elif definedCONFIG_NETTLE
qapi/crypto.json +2 -2
@@ -66,7 +66,7 @@ # @aes-128: AES with 128 bit / 16 byte keys # @aes-192: AES with 192 bit / 24 byte keys # @aes-256: AES with 256 bit / 32 byte keys -# @des-rfb: RFB specific variant of single DES. Do not use except in VNC. +# @des: DES with 56 bit / 8 byte keys. Do not use except in VNC. # @3des: 3DES(EDE) with 192 bit / 24 byte keys (since 2.9) # @cast5-128: Cast5 with 128 bit / 16 byte keys # @serpent-128: Serpent with 128 bit / 16 byte keys
@@ -80,7 +80,7 @@ { 'enum': 'QCryptoCipherAlgorithm', 'prefix': 'QCRYPTO_CIPHER_ALG', 'data': ['aes-128', 'aes-192', 'aes-256', - 'des-rfb', '3des', + 'des', '3des', 'cast5-128', 'serpent-128', 'serpent-192', 'serpent-256', 'twofish-128', 'twofish-192', 'twofish-256']}
tests/unit/test-crypto-cipher.c +9 -9
@@ -155,28 +155,28 @@ static QCryptoCipherTestData test_data[] = {* in single AES block, and gives identical * ciphertext in ECB and CBC modes */ - .path = "/crypto/cipher/des-rfb-ecb-56-one-block", - .alg = QCRYPTO_CIPHER_ALG_DES_RFB, + .path = "/crypto/cipher/des-ecb-56-one-block", + .alg = QCRYPTO_CIPHER_ALG_DES, .mode = QCRYPTO_CIPHER_MODE_ECB, - .key = "0123456789abcdef", + .key = "80c4a2e691d5b3f7", .plaintext = "70617373776f7264", .ciphertext = "73fa80b66134e403", }, {/* See previous comment */ - .path = "/crypto/cipher/des-rfb-cbc-56-one-block", - .alg = QCRYPTO_CIPHER_ALG_DES_RFB, + .path = "/crypto/cipher/des-cbc-56-one-block", + .alg = QCRYPTO_CIPHER_ALG_DES, .mode = QCRYPTO_CIPHER_MODE_CBC, - .key = "0123456789abcdef", + .key = "80c4a2e691d5b3f7", .iv = "0000000000000000", .plaintext = "70617373776f7264", .ciphertext = "73fa80b66134e403", }, { - .path = "/crypto/cipher/des-rfb-ecb-56", - .alg = QCRYPTO_CIPHER_ALG_DES_RFB, + .path = "/crypto/cipher/des-ecb-56", + .alg = QCRYPTO_CIPHER_ALG_DES, .mode = QCRYPTO_CIPHER_MODE_ECB, - .key ="0123456789abcdef" , + .key ="80c4a2e691d5b3f7" , .plaintext ="6bc1bee22e409f96e93d7e117393172a" "ae2d8a571e03ac9c9eb76fac45af8e51"
ui/vnc.c +17 -3
@@ -2733,6 +2733,19 @@ static void authentication_failed(VncState *vs)vnc_client_error (vs); } +static void +vnc_munge_des_rfb_key (unsigned char *key,size_t nkey) +{ +size_t i; +for (i =0 ; i < nkey; i++) { +uint8_t r = key[i]; + r = (r &0xf0 ) >>4 | (r &0x0f ) <<4 ; + r = (r &0xcc ) >>2 | (r &0x33 ) <<2 ; + r = (r &0xaa ) >>1 | (r &0x55 ) <<1 ; + key[i] = r; + } +} +static int protocol_client_auth_vnc (VncState *vs,uint8_t *data,size_t len) {unsigned char response[VNC_AUTH_CHALLENGE_SIZE ];
@@ -2757,9 +2770,10 @@ static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) pwlen =strlen (vs->vd->password);for (i=0 ; i<sizeof (key); i++) key[i] = i<pwlen ? vs->vd->password[i] :0 ; +vnc_munge_des_rfb_key (key,sizeof (key)); cipher =qcrypto_cipher_new ( -QCRYPTO_CIPHER_ALG_DES_RFB , +QCRYPTO_CIPHER_ALG_DES ,QCRYPTO_CIPHER_MODE_ECB , key,G_N_ELEMENTS (key), &err);
@@ -4045,9 +4059,9 @@ void vnc_display_open(const char *id, Error **errp) goto fail; }if (!qcrypto_cipher_supports ( -QCRYPTO_CIPHER_ALG_DES_RFB ,QCRYPTO_CIPHER_MODE_ECB )) { +QCRYPTO_CIPHER_ALG_DES ,QCRYPTO_CIPHER_MODE_ECB )) {error_setg (errp, -"Cipher backend does not support DES RFB algorithm" ); +"Cipher backend does not support DES algorithm" ); goto fail; } }
Signature · 1 line
2.31.1
Re: [PATCH 11/18] crypto: rename des-rfb cipher to just des
Quoted 12 lines · Daniel P. Berrangé
Currently the crypto layer exposes support for a 'des-rfb' algorithm which is just normal single-DES, with the bits in each key byte reversed. This special key munging is required by the RFB protocol password authentication mechanism.
Since the crypto layer is generic shared code, it makes more sense to do the key byte munging in the VNC server code, and expose normal single-DES support.
Signed-off-by Daniel P. Berrangé <[email protected]>---
[...]
Quoted 14 lines
qapi/crypto.json +1
-1@@ -66,7 +66,7 @@ # @aes-128: AES with 128 bit / 16 byte keys # @aes-192: AES with 192 bit / 24 byte keys # @aes-256: AES with 256 bit / 32 byte keys -# @des-rfb: RFB specific variant of single DES. Do not use except in VNC. +# @des: DES with 56 bit / 8 byte keys. Do not use except in VNC. # @3des: 3DES(EDE) with 192 bit / 24 byte keys (since 2.9) # @cast5-128: Cast5 with 128 bit / 16 byte keys # @serpent-128: Serpent with 128 bit / 16 byte keys
qapi/crypto.json line 86
Quoted 8 lines
qapi/crypto.json +1
-1@@ -80,7 +80,7 @@ { 'enum': 'QCryptoCipherAlgorithm', 'prefix': 'QCRYPTO_CIPHER_ALG', 'data': ['aes-128', 'aes-192', 'aes-256', - 'des-rfb', '3des', + 'des', '3des', 'cast5-128', 'serpent-128', 'serpent-192', 'serpent-256', 'twofish-128', 'twofish-192', 'twofish-256']}
Is enum value "des-rfb" part of any external interface?
[...]
Re: [PATCH 11/18] crypto: rename des-rfb cipher to just des
Quoted 33 lines · Markus Armbruster
Quoted 12 lines · Daniel P. Berrangé
Currently the crypto layer exposes support for a 'des-rfb' algorithm which is just normal single-DES, with the bits in each key byte reversed. This special key munging is required by the RFB protocol password authentication mechanism.
Since the crypto layer is generic shared code, it makes more sense to do the key byte munging in the VNC server code, and expose normal single-DES support.
Signed-off-by Daniel P. Berrangé <[email protected]>---
[...]
Quoted 16 lines
qapi/crypto.json +1
-1@@ -66,7 +66,7 @@ # @aes-128: AES with 128 bit / 16 byte keys # @aes-192: AES with 192 bit / 24 byte keys # @aes-256: AES with 256 bit / 32 byte keys -# @des-rfb: RFB specific variant of single DES. Do not use except in VNC. +# @des: DES with 56 bit / 8 byte keys. Do not use except in VNC. # @3des: 3DES(EDE) with 192 bit / 24 byte keys (since 2.9) # @cast5-128: Cast5 with 128 bit / 16 byte keys # @serpent-128: Serpent with 128 bit / 16 byte keys@@ -80,7 +80,7 @@ { 'enum': 'QCryptoCipherAlgorithm', 'prefix': 'QCRYPTO_CIPHER_ALG',
Quoted 8 lines
qapi/crypto.json line 86
Quoted 6 lines
qapi/crypto.json +1
-1'data': ['aes-128', 'aes-192', 'aes-256', - 'des-rfb', '3des', + 'des', '3des', 'cast5-128', 'serpent-128', 'serpent-192', 'serpent-256', 'twofish-128', 'twofish-192', 'twofish-256']}Is enum value "des-rfb" part of any external interface?
Strictly speaking, yes, but in reality it doesn't matter.
The only place in QEMU that actually uses DES-RFB is the VNC server code. That is an indirect usage when the user sets the "password" option flag in QemuOpts. The fact that it uses DES-RFB is an internal impl detail.
The one place that does publically expose ability to set a field using the QCryptoCipherAlgorithm enum type is the LUKS support in the block layer:
{ 'struct': 'QCryptoBlockCreateOptionsLUKS',
'base': 'QCryptoBlockOptionsLUKS',
'data': { '*cipher-alg': 'QCryptoCipherAlgorithm',
'*cipher-mode': 'QCryptoCipherMode',
'*ivgen-alg': 'QCryptoIVGenAlgorithm',
'*ivgen-hash-alg': 'QCryptoHashAlgorithm',
'*hash-alg': 'QCryptoHashAlgorithm',
'*iter-time': 'int'}}eg exposed on CLI as:
$ qemu-img create -f luks -o cipher-alg=NNN foo.luks 1G
or equivalant with QMP blockdev-create
While the QMP schema allows any valid QCryptoCipherAlgorithm string to be set, the actual implementation does not.
The crypto/block-luks.c code has a map between cipher algs and LUKS format algoritm names:
static const QCryptoBlockLUKSCipherNameMap qcrypto_block_luks_cipher_name_map[] = {
{ "aes", qcrypto_block_luks_cipher_size_map_aes }, { "cast5", qcrypto_block_luks_cipher_size_map_cast5 }, { "serpent", qcrypto_block_luks_cipher_size_map_serpent }, { "twofish", qcrypto_block_luks_cipher_size_map_twofish },
};
If it isn't in that table, it can't be used. IOW, the only scenario we're affecting in this rename is one which would already result in an error condition
Original behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-rfb qemu-img: demo.luks: Algorithm 'des-rfb' not supported
New behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-fish qemu-img: demo.luks: Invalid parameter 'des-rfb'
I considered this incompatibility to be acceptable, and thus not worth going through a deprecation dance.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Markus Armbruster
Reviewed-byRe: [PATCH 11/18] crypto: rename des-rfb cipher to just des
Quoted 95 lines · Daniel P. Berrangé
Quoted 33 lines · Markus Armbruster
Quoted 12 lines · Daniel P. Berrangé
Currently the crypto layer exposes support for a 'des-rfb' algorithm which is just normal single-DES, with the bits in each key byte reversed. This special key munging is required by the RFB protocol password authentication mechanism.
Since the crypto layer is generic shared code, it makes more sense to do the key byte munging in the VNC server code, and expose normal single-DES support.
Signed-off-by Daniel P. Berrangé <[email protected]>---
[...]
Quoted 16 lines
qapi/crypto.json +1
-1@@ -66,7 +66,7 @@ # @aes-128: AES with 128 bit / 16 byte keys # @aes-192: AES with 192 bit / 24 byte keys # @aes-256: AES with 256 bit / 32 byte keys -# @des-rfb: RFB specific variant of single DES. Do not use except in VNC. +# @des: DES with 56 bit / 8 byte keys. Do not use except in VNC. # @3des: 3DES(EDE) with 192 bit / 24 byte keys (since 2.9) # @cast5-128: Cast5 with 128 bit / 16 byte keys # @serpent-128: Serpent with 128 bit / 16 byte keys@@ -80,7 +80,7 @@ { 'enum': 'QCryptoCipherAlgorithm', 'prefix': 'QCRYPTO_CIPHER_ALG',Quoted 8 lines
qapi/crypto.json line 86
Quoted 6 lines
qapi/crypto.json +1
-1'data': ['aes-128', 'aes-192', 'aes-256', - 'des-rfb', '3des', + 'des', '3des', 'cast5-128', 'serpent-128', 'serpent-192', 'serpent-256', 'twofish-128', 'twofish-192', 'twofish-256']}Is enum value "des-rfb" part of any external interface?
Strictly speaking, yes, but in reality it doesn't matter.
The only place in QEMU that actually uses DES-RFB is the VNC server code. That is an indirect usage when the user sets the "password" option flag in QemuOpts. The fact that it uses DES-RFB is an internal impl detail.
The one place that does publically expose ability to set a field using the QCryptoCipherAlgorithm enum type is the LUKS support in the block layer:
{ 'struct': 'QCryptoBlockCreateOptionsLUKS',
'base': 'QCryptoBlockOptionsLUKS',
'data': { '*cipher-alg': 'QCryptoCipherAlgorithm','*cipher-mode': 'QCryptoCipherMode', '*ivgen-alg': 'QCryptoIVGenAlgorithm', '*ivgen-hash-alg': 'QCryptoHashAlgorithm', '*hash-alg': 'QCryptoHashAlgorithm', '*iter-time': 'int'}}eg exposed on CLI as:
$ qemu-img create -f luks -o cipher-alg=NNN foo.luks 1Gor equivalant with QMP blockdev-create
While the QMP schema allows any valid QCryptoCipherAlgorithm string to be set, the actual implementation does not.
The
crypto/block-luks.ccode has a map between cipher algs and LUKS format algoritm names:static const QCryptoBlockLUKSCipherNameMap qcrypto_block_luks_cipher_name_map[] = {
{ "aes",qcrypto_block_luks_cipher_size_map_aes}, { "cast5",qcrypto_block_luks_cipher_size_map_cast5}, { "serpent",qcrypto_block_luks_cipher_size_map_serpent}, { "twofish",qcrypto_block_luks_cipher_size_map_twofish},
};If it isn't in that table, it can't be used. IOW, the only scenario we're affecting in this rename is one which would already result in an error condition
Original behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-rfb qemu-img: demo.luks: Algorithm 'des-rfb' not supported
Quoted 8 lines
New behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-fish qemu-img: demo.luks: Invalid parameter 'des-rfb'I considered this incompatibility to be acceptable, and thus not worth going through a deprecation dance.
Thanks for the explanation. I agree the deprecation dance is not necessary here.
Please consider explaining this in your commit message. Suggest to append a variation of the tail of your explanation:
Replacing cipher 'des-rfb' by 'des' looks like an incompatible interface change, but it doesn't matter. While the QMP schema allows
...
qemu-img: demo.luks: Invalid parameter 'des-rfb'
Also consider tweaking the title to
crypto: Replace 'des-rfb' cipher by 'des'
because it's not actually just a rename.
Re: [PATCH 11/18] crypto: rename des-rfb cipher to just des
Quoted 117 lines · Markus Armbruster
Quoted 95 lines · Daniel P. Berrangé
Quoted 33 lines · Markus Armbruster
Quoted 12 lines · Daniel P. Berrangé
Currently the crypto layer exposes support for a 'des-rfb' algorithm which is just normal single-DES, with the bits in each key byte reversed. This special key munging is required by the RFB protocol password authentication mechanism.
Since the crypto layer is generic shared code, it makes more sense to do the key byte munging in the VNC server code, and expose normal single-DES support.
Signed-off-by Daniel P. Berrangé <[email protected]>---
[...]
Quoted 16 lines
qapi/crypto.json +1
-1@@ -66,7 +66,7 @@ # @aes-128: AES with 128 bit / 16 byte keys # @aes-192: AES with 192 bit / 24 byte keys # @aes-256: AES with 256 bit / 32 byte keys -# @des-rfb: RFB specific variant of single DES. Do not use except in VNC. +# @des: DES with 56 bit / 8 byte keys. Do not use except in VNC. # @3des: 3DES(EDE) with 192 bit / 24 byte keys (since 2.9) # @cast5-128: Cast5 with 128 bit / 16 byte keys # @serpent-128: Serpent with 128 bit / 16 byte keys@@ -80,7 +80,7 @@ { 'enum': 'QCryptoCipherAlgorithm', 'prefix': 'QCRYPTO_CIPHER_ALG',Quoted 8 lines
qapi/crypto.json line 86
Quoted 6 lines
qapi/crypto.json +1
-1'data': ['aes-128', 'aes-192', 'aes-256', - 'des-rfb', '3des', + 'des', '3des', 'cast5-128', 'serpent-128', 'serpent-192', 'serpent-256', 'twofish-128', 'twofish-192', 'twofish-256']}Is enum value "des-rfb" part of any external interface?
Strictly speaking, yes, but in reality it doesn't matter.
The only place in QEMU that actually uses DES-RFB is the VNC server code. That is an indirect usage when the user sets the "password" option flag in QemuOpts. The fact that it uses DES-RFB is an internal impl detail.
The one place that does publically expose ability to set a field using the QCryptoCipherAlgorithm enum type is the LUKS support in the block layer:
{ 'struct': 'QCryptoBlockCreateOptionsLUKS',
'base': 'QCryptoBlockOptionsLUKS',
'data': { '*cipher-alg': 'QCryptoCipherAlgorithm','*cipher-mode': 'QCryptoCipherMode', '*ivgen-alg': 'QCryptoIVGenAlgorithm', '*ivgen-hash-alg': 'QCryptoHashAlgorithm', '*hash-alg': 'QCryptoHashAlgorithm', '*iter-time': 'int'}}eg exposed on CLI as:
$ qemu-img create -f luks -o cipher-alg=NNN foo.luks 1Gor equivalant with QMP blockdev-create
While the QMP schema allows any valid QCryptoCipherAlgorithm string to be set, the actual implementation does not.
The
crypto/block-luks.ccode has a map between cipher algs and LUKS format algoritm names:static const QCryptoBlockLUKSCipherNameMap qcrypto_block_luks_cipher_name_map[] = {
{ "aes",qcrypto_block_luks_cipher_size_map_aes}, { "cast5",qcrypto_block_luks_cipher_size_map_cast5}, { "serpent",qcrypto_block_luks_cipher_size_map_serpent}, { "twofish",qcrypto_block_luks_cipher_size_map_twofish},
};If it isn't in that table, it can't be used. IOW, the only scenario we're affecting in this rename is one which would already result in an error condition
Original behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-rfb qemu-img: demo.luks: Algorithm 'des-rfb' not supportedQuoted 8 lines
New behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-fish qemu-img: demo.luks: Invalid parameter 'des-rfb'I considered this incompatibility to be acceptable, and thus not worth going through a deprecation dance.
Thanks for the explanation. I agree the deprecation dance is not necessary here.
Please consider explaining this in your commit message. Suggest to append a variation of the tail of your explanation:
Replacing cipher 'des-rfb' by 'des' looks like an incompatible interface change, but it doesn't matter. While the QMP schema allows
...
qemu-img: demo.luks: Invalid parameter 'des-rfb'
Quoted 7 lines
Also consider tweaking the title to
crypto: Replace 'des-rfb' cipher by 'des'
because it's not actually just a rename.
Reviewed-by Markus Armbruster <[email protected]>
Thanks, I've updated the commit msg as suggested
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 11/18] crypto: rename des-rfb cipher to just des
In reply to Daniel P. BerrangéQuoted 11 lines · Daniel P. Berrangé
Currently the crypto layer exposes support for a 'des-rfb' algorithm which is just normal single-DES, with the bits in each key byte reversed. This special key munging is required by the RFB protocol password authentication mechanism.
Since the crypto layer is generic shared code, it makes more sense to do the key byte munging in the VNC server code, and expose normal single-DES support.
Signed-off-by Daniel P. Berrangé <[email protected]>
I agree with Markus' suggestion to enhance the commit message.
Quoted 5 lines
+++
b/tests/unit/test-crypto-cipher.ctests/unit/test-crypto-cipher.c +0
-0@@ -155,28 +155,28 @@ static QCryptoCipherTestData test_data[] = {* in single AES block, and gives identical * ciphertext in ECB and CBC modes */
tests/unit/test-crypto-cipher.c static QCryptoCipherTestData test_data[] = { · line 164
Quoted 10 lines
tests/unit/test-crypto-cipher.c +3
-3- .path = "/crypto/cipher/des-rfb-ecb-56-one-block", - .alg = QCRYPTO_CIPHER_ALG_DES_RFB, + .path = "/crypto/cipher/des-ecb-56-one-block", + .alg = QCRYPTO_CIPHER_ALG_DES, .mode = QCRYPTO_CIPHER_MODE_ECB, - .key = "0123456789abcdef", + .key = "80c4a2e691d5b3f7", .plaintext = "70617373776f7264", .ciphertext = "73fa80b66134e403", },
This is a rather cute way to avoid recomputing the canonical .ciphertext due to the change in bit ordering.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 12/18 crypto: flip priority of backends to prefer gcrypt
In reply to Daniel P. BerrangéOriginally we preferred to use nettle, over gcrypt because gnutls already links to nettle and thus it minimizes the dependencies. In retrospect this was the wrong criteria to optimize for.
Currently shipping versions of gcrypt have cipher impls that are massively faster than those in nettle and this is way more important. The nettle library is also not capable of enforcing FIPS compliance, since it considers that out of scope. It merely aims to provide general purpose impls of algorithms, and usage policy is left upto the layer above, such as GNUTTLS.
--- meson.build | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
meson.build +11 -11
@@ -818,22 +818,13 @@ if not get_option('gnutls').auto() or have_system kwargs: static_kwargs) endif -# Nettle has priority over gcrypt +# Gcrypt has priority over nettle gcrypt = not_found nettle = not_found xts = 'none' if get_option('nettle').enabled() and get_option('gcrypt').enabled() error('Only one of gcrypt & nettle can be enabled') -elif (not get_option('nettle').auto() or have_system) and not get_option('gcrypt').enabled() - nettle = dependency('nettle', version: '>=3.4', - method: 'pkg-config', - required: get_option('nettle'), - kwargs: static_kwargs) - if nettle.found() and not cc.has_header('nettle/xts.h', dependencies: nettle) - xts = 'private' - endif -endif -if (not get_option('gcrypt').auto() or have_system) and not nettle.found() +elif (not get_option('gcrypt').auto() or have_system) and not get_option('nettle').enabled() gcrypt = dependency('libgcrypt', version: '>=1.8', method: 'config-tool', required: get_option('gcrypt'),
@@ -847,6 +838,15 @@ if (not get_option('gcrypt').auto() or have_system) and not nettle.found() cc.find_library('gpg-error', required: true, kwargs: static_kwargs)]) endif endif +if (not get_option('nettle').auto() or have_system) and not gcrypt.found() + nettle = dependency('nettle', version: '>=3.4', + method: 'pkg-config', + required: get_option('nettle'), + kwargs: static_kwargs) + if nettle.found() and not cc.has_header('nettle/xts.h', dependencies: nettle) + xts = 'private' + endif +endif gtk = not_found gtkx11 = not_found
Signature · 1 line
2.31.1
Re: [PATCH 12/18] crypto: flip priority of backends to prefer gcrypt
Quoted 1 line · Daniel P. Berrangé
Originally we preferred to use nettle, over gcrypt because
s/nettle, over gcrypt/nettle over gcrypt,/
Quoted 11 lines
gnutls already links to nettle and thus it minimizes the dependencies. In retrospect this was the wrong criteria to optimize for.
Currently shipping versions of gcrypt have cipher impls that are massively faster than those in nettle and this is way more important. The nettle library is also not capable of enforcing FIPS compliance, since it considers that out of scope. It merely aims to provide general purpose impls of algorithms, and usage policy is left upto the layer above, such as GNUTTLS.
GNUTLS
Quoted 6 lines
Signed-off-by Daniel P. Berrangé <[email protected]>--- meson.build | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
My meson skills are weak, but the change looks reasonable, and the proof is in building things.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 13/18 crypto: introduce build system for gnutls crypto backend
In reply to Daniel P. BerrangéThis introduces the build logic needed to decide whether we can use gnutls as a crypto driver backend. The actual implementations will be introduced in following patches. We only wish to use gnutls if it has version 3.6.14 or newer, because that is what finally brings HW accelerated AES-XTS mode for x86_64.
--- meson.build | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-)
meson.build +32 -4
@@ -811,11 +811,34 @@ if 'CONFIG_OPENGL' in config_host endif gnutls = not_found +gnutls_crypto = not_found if not get_option('gnutls').auto() or have_system - gnutls = dependency('gnutls', version: '>=3.5.18', - method: 'pkg-config', - required: get_option('gnutls'), - kwargs: static_kwargs) + # For general TLS support our min gnutls matches + # that implied by our platform support matrix + # + # For the crypto backends, we look for a newer + # gnutls: + # + # Version 3.6.8 is needed to get XTS + # Version 3.6.13 is needed to get PBKDF + # Version 3.6.14 is needed to get HW accelerated XTS + # + # If newer enough gnutls isn't available, we can + # still use a different crypto backend to satisfy + # the platform support requirements + gnutls_crypto = dependency('gnutls', version: '>=3.6.14', + method: 'pkg-config', + required: get_option('gnutls'), + kwargs: static_kwargs) + if gnutls_crypto.found() + gnutls = gnutls_crypto + else + # Our min version if all we need is TLS + gnutls = dependency('gnutls', version: '>=3.5.18', + method: 'pkg-config', + required: get_option('gnutls'), + kwargs: static_kwargs) + endif endif # Gcrypt has priority over nettle
@@ -847,6 +870,9 @@ if (not get_option('nettle').auto() or have_system) and not gcrypt.found() xts = 'private' endif endif +if gcrypt.found() or nettle.found() + gnutls_crypto = not_found +endif gtk = not_found gtkx11 = not_found
@@ -1219,6 +1245,7 @@ config_host_data.set('CONFIG_XKBCOMMON', xkbcommon.found()) config_host_data.set('CONFIG_KEYUTILS', keyutils.found()) config_host_data.set('CONFIG_GETTID', has_gettid) config_host_data.set('CONFIG_GNUTLS', gnutls.found()) +config_host_data.set('CONFIG_GNUTLS_CRYPTO', gnutls_crypto.found()) config_host_data.set('CONFIG_GCRYPT', gcrypt.found()) config_host_data.set('CONFIG_NETTLE', nettle.found()) config_host_data.set('CONFIG_QEMU_PRIVATE_XTS', xts == 'private')
@@ -2719,6 +2746,7 @@ summary(summary_info, bool_yn: true, section: 'Block layer support') summary_info = {} summary_info += {'TLS priority': config_host['CONFIG_TLS_PRIORITY']} summary_info += {'GNUTLS support': gnutls.found()} +summary_info += {'GNUTLS crypto': gnutls_crypto.found()} # TODO: add back version summary_info += {'libgcrypt': gcrypt.found()} # TODO: add back version
Signature · 1 line
2.31.1
Re: [PATCH 13/18] crypto: introduce build system for gnutls crypto backend
Quoted 10 lines · Daniel P. Berrangé
This introduces the build logic needed to decide whether we can use gnutls as a crypto driver backend. The actual implementations will be introduced in following patches. We only wish to use gnutls if it has version 3.6.14 or newer, because that is what finally brings HW accelerated AES-XTS mode for
x86_64.Signed-off-by Daniel P. Berrangé <[email protected]>--- meson.build | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-)
Again, take this with a grain of salt, since my meson skills are near zero. But the comments do a good job, and it looks sane.
Quoted 29 lines
meson.build +16
-4@@ -811,11 +811,34 @@ if 'CONFIG_OPENGL' in config_host endif gnutls = not_found +gnutls_crypto = not_found if not get_option('gnutls').auto() or have_system - gnutls = dependency('gnutls', version: '>=3.5.18', - method: 'pkg-config', - required: get_option('gnutls'), - kwargs: static_kwargs) + # For general TLS support our min gnutls matches + # that implied by our platform support matrix + # + # For the crypto backends, we look for a newer + # gnutls: + # + # Version 3.6.8 is needed to get XTS + # Version 3.6.13 is needed to get PBKDF + # Version 3.6.14 is needed to get HW accelerated XTS + # + # If newer enough gnutls isn't available, we can + # still use a different crypto backend to satisfy + # the platform support requirements + gnutls_crypto = dependency('gnutls', version: '>=3.6.14', + method: 'pkg-config',
meson.build if 'CONFIG_OPENGL' in config_host · line 840
Quoted 10 lines
meson.build +10
-0+ required: get_option('gnutls'), + kwargs: static_kwargs) + if gnutls_crypto.found() + gnutls = gnutls_crypto + else + # Our min version if all we need is TLS + gnutls = dependency('gnutls', version: '>=3.5.18', + method: 'pkg-config', + required: get_option('gnutls'), + kwargs: static_kwargs)
TAB damage.
With that fixed,
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 14/18 crypto: add gnutls cipher provider
In reply to Daniel P. BerrangéAdd an implementation of the QEMU cipher APIs to the gnutls crypto backend. XTS support is only available for gnutls version >= 3.6.8. Since ECB mode is not exposed by gnutls APIs, we can't use the private XTS code for compatibility.
--- crypto/cipher-gnutls.c.inc | 325 +++++++++++++++++++++++++++++++++++++ crypto/cipher.c | 2 + 2 files changed, 327 insertions(+) create mode 100644 crypto/cipher-gnutls.c.inc
crypto/cipher-gnutls.c.inc new file +325 -0
@@ -0,0 +1,325 @@ +/* + * QEMU Crypto cipher gnutls algorithms + * + * Copyright (c) 2021 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see <http://www.gnu.org/licenses/>. + * + */ + +#include "qemu/osdep.h" +#include "cipherpriv.h" + +#include <gnutls/crypto.h> + +#if GNUTLS_VERSION_NUMBER >= 0x030608 +#define QEMU_GNUTLS_XTS +#endif + +bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode) +{ + + switch (mode) { + case QCRYPTO_CIPHER_MODE_ECB: + case QCRYPTO_CIPHER_MODE_CBC: + switch (alg) { + case QCRYPTO_CIPHER_ALG_AES_128: + case QCRYPTO_CIPHER_ALG_AES_192: + case QCRYPTO_CIPHER_ALG_AES_256: + case QCRYPTO_CIPHER_ALG_DES: + case QCRYPTO_CIPHER_ALG_3DES: + return true; + default: + return false; + } +#ifdef QEMU_GNUTLS_XTS + case QCRYPTO_CIPHER_MODE_XTS: + switch (alg) { + case QCRYPTO_CIPHER_ALG_AES_128: + case QCRYPTO_CIPHER_ALG_AES_256: + return true; + default: + return false; + } + return true; +#endif + default: + return false; + } +} + +typedef struct QCryptoCipherGnutls QCryptoCipherGnutls; +struct QCryptoCipherGnutls { + QCryptoCipher base; + gnutls_cipher_hd_t handle; /* XTS & CBC mode */ + gnutls_cipher_algorithm_t galg; /* ECB mode */ + guint8 *key; /* ECB mode */ + size_t nkey; /* ECB mode */ + size_t blocksize; +}; + + +static void +qcrypto_gnutls_cipher_free(QCryptoCipher *cipher) +{ + QCryptoCipherGnutls *ctx = container_of(cipher, QCryptoCipherGnutls, base); + + g_free(ctx->key); + if (ctx->handle) { + gnutls_cipher_deinit(ctx->handle); + } + g_free(ctx); +} + + +static int +qcrypto_gnutls_cipher_encrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) +{ + QCryptoCipherGnutls *ctx = container_of(cipher, QCryptoCipherGnutls, base); + int err; + + if (len % ctx->blocksize) { + error_setg(errp, "Length %zu must be a multiple of block size %zu", + len, ctx->blocksize); + return -1; + } + + if (ctx->handle) { /* CBC / XTS mode */ + err = gnutls_cipher_encrypt2(ctx->handle, + in, len, + out, len); + if (err != 0) { + error_setg(errp, "Cannot encrypt data: %s", + gnutls_strerror(err)); + return -1; + } + } else { /* ECB mode very inefficiently faked with CBC */ + g_autofree unsigned char *iv = g_new0(unsigned char, ctx->blocksize); + while (len) { + gnutls_cipher_hd_t handle; + gnutls_datum_t gkey = { (unsigned char *)ctx->key, ctx->nkey }; + int err = gnutls_cipher_init(&handle, ctx->galg, &gkey, NULL); + if (err != 0) { + error_setg(errp, "Cannot initialize cipher: %s", + gnutls_strerror(err)); + return -1; + } + + gnutls_cipher_set_iv(handle, iv, ctx->blocksize); + + err = gnutls_cipher_encrypt2(handle, + in, ctx->blocksize, + out, ctx->blocksize); + if (err != 0) { + gnutls_cipher_deinit(handle); + error_setg(errp, "Cannot encrypt data: %s", + gnutls_strerror(err)); + return -1; + } + gnutls_cipher_deinit(handle); + + len -= ctx->blocksize; + in += ctx->blocksize; + out += ctx->blocksize; + } + } + + return 0; +} + + +static int +qcrypto_gnutls_cipher_decrypt(QCryptoCipher *cipher, + const void *in, + void *out, + size_t len, + Error **errp) +{ + QCryptoCipherGnutls *ctx = container_of(cipher, QCryptoCipherGnutls, base); + int err; + + if (len % ctx->blocksize) { + error_setg(errp, "Length %zu must be a multiple of block size %zu", + len, ctx->blocksize); + return -1; + } + + if (ctx->handle) { /* CBC / XTS mode */ + err = gnutls_cipher_decrypt2(ctx->handle, + in, len, + out, len); + + if (err != 0) { + error_setg(errp, "Cannot decrypt data: %s", + gnutls_strerror(err)); + return -1; + } + } else { /* ECB mode very inefficiently faked with CBC */ + g_autofree unsigned char *iv = g_new0(unsigned char, ctx->blocksize); + while (len) { + gnutls_cipher_hd_t handle; + gnutls_datum_t gkey = { (unsigned char *)ctx->key, ctx->nkey }; + int err = gnutls_cipher_init(&handle, ctx->galg, &gkey, NULL); + if (err != 0) { + error_setg(errp, "Cannot initialize cipher: %s", + gnutls_strerror(err)); + return -1; + } + + gnutls_cipher_set_iv(handle, iv, ctx->blocksize); + + err = gnutls_cipher_decrypt2(handle, + in, ctx->blocksize, + out, ctx->blocksize); + if (err != 0) { + gnutls_cipher_deinit(handle); + error_setg(errp, "Cannot encrypt data: %s", + gnutls_strerror(err)); + return -1; + } + gnutls_cipher_deinit(handle); + + len -= ctx->blocksize; + in += ctx->blocksize; + out += ctx->blocksize; + } + } + + return 0; +} + +static int +qcrypto_gnutls_cipher_setiv(QCryptoCipher *cipher, + const uint8_t *iv, size_t niv, + Error **errp) +{ + QCryptoCipherGnutls *ctx = container_of(cipher, QCryptoCipherGnutls, base); + + if (niv != ctx->blocksize) { + error_setg(errp, "Expected IV size %zu not %zu", + ctx->blocksize, niv); + return -1; + } + + gnutls_cipher_set_iv(ctx->handle, (unsigned char *)iv, niv); + + return 0; +} + + +static struct QCryptoCipherDriver gnutls_driver = { + .cipher_encrypt = qcrypto_gnutls_cipher_encrypt, + .cipher_decrypt = qcrypto_gnutls_cipher_decrypt, + .cipher_setiv = qcrypto_gnutls_cipher_setiv, + .cipher_free = qcrypto_gnutls_cipher_free, +}; + +static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode, + const uint8_t *key, + size_t nkey, + Error **errp) +{ + QCryptoCipherGnutls *ctx; + gnutls_datum_t gkey = { (unsigned char *)key, nkey }; + gnutls_cipher_algorithm_t galg = GNUTLS_CIPHER_UNKNOWN; + int err; + + switch (mode) { +#ifdef QEMU_GNUTLS_XTS + case QCRYPTO_CIPHER_MODE_XTS: + switch (alg) { + case QCRYPTO_CIPHER_ALG_AES_128: + galg = GNUTLS_CIPHER_AES_128_XTS; + break; + case QCRYPTO_CIPHER_ALG_AES_256: + galg = GNUTLS_CIPHER_AES_256_XTS; + break; + default: + break; + } + break; +#endif + + case QCRYPTO_CIPHER_MODE_ECB: + case QCRYPTO_CIPHER_MODE_CBC: + switch (alg) { + case QCRYPTO_CIPHER_ALG_AES_128: + galg = GNUTLS_CIPHER_AES_128_CBC; + break; + case QCRYPTO_CIPHER_ALG_AES_192: + galg = GNUTLS_CIPHER_AES_192_CBC; + break; + case QCRYPTO_CIPHER_ALG_AES_256: + galg = GNUTLS_CIPHER_AES_256_CBC; + break; + case QCRYPTO_CIPHER_ALG_DES: + galg = GNUTLS_CIPHER_DES_CBC; + break; + case QCRYPTO_CIPHER_ALG_3DES: + galg = GNUTLS_CIPHER_3DES_CBC; + break; + default: + break; + } + break; + default: + break; + } + + if (galg == GNUTLS_CIPHER_UNKNOWN) { + error_setg(errp, "Unsupported cipher algorithm %s with %s mode", + QCryptoCipherAlgorithm_str(alg), + QCryptoCipherMode_str(mode)); + return NULL; + } + + if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) { + return NULL; + } + + ctx = g_new0(QCryptoCipherGnutls, 1); + ctx->base.driver = &gnutls_driver; + + if (mode == QCRYPTO_CIPHER_MODE_ECB) { + ctx->key = g_new0(guint8, nkey); + memcpy(ctx->key, key, nkey); + ctx->nkey = nkey; + ctx->galg = galg; + } else { + err = gnutls_cipher_init(&ctx->handle, galg, &gkey, NULL); + if (err != 0) { + error_setg(errp, "Cannot initialize cipher: %s", + gnutls_strerror(err)); + goto error; + } + } + + if (alg == QCRYPTO_CIPHER_ALG_DES || + alg == QCRYPTO_CIPHER_ALG_3DES) + ctx->blocksize = 8; + else + ctx->blocksize = 16; + + return &ctx->base; + + error: + qcrypto_gnutls_cipher_free(&ctx->base); + return NULL; +}
crypto/cipher.c +2 -0
@@ -136,6 +136,8 @@ qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,#include "cipher-gcrypt.c.inc" #elif definedCONFIG_NETTLE #include "cipher-nettle.c.inc" +#elif definedCONFIG_GNUTLS_CRYPTO +#include "cipher-gnutls.c.inc" #else #include "cipher-builtin.c.inc" #endif
Signature · 1 line
2.31.1
Re: [PATCH 14/18] crypto: add gnutls cipher provider
Quoted 36 lines · Daniel P. Berrangé
Add an implementation of the QEMU cipher APIs to the gnutls crypto backend. XTS support is only available for gnutls version >= 3.6.8. Since ECB mode is not exposed by gnutls APIs, we can't use the private XTS code for compatibility.
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/cipher-gnutls.c.inc | 325 +++++++++++++++++++++++++++++++++++++ crypto/cipher.c | 2 + 2 files changed, 327 insertions(+) create mode 100644 crypto/cipher-gnutls.c.inccrypto/cipher-gnutls.c.inc new file +18
-0+ +bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg, + QCryptoCipherMode mode) +{ + + switch (mode) { + case QCRYPTO_CIPHER_MODE_ECB: + case QCRYPTO_CIPHER_MODE_CBC: + switch (alg) { + case QCRYPTO_CIPHER_ALG_AES_128: + case QCRYPTO_CIPHER_ALG_AES_192: + case QCRYPTO_CIPHER_ALG_AES_256: + case QCRYPTO_CIPHER_ALG_DES: + case QCRYPTO_CIPHER_ALG_3DES: + return true; + default: + return false; + }
crypto/cipher-gnutls.c.inc line 56
Quoted 10 lines
crypto/cipher-gnutls.c.inc new file +10
-0+#ifdef QEMU_GNUTLS_XTS + case QCRYPTO_CIPHER_MODE_XTS: + switch (alg) { + case QCRYPTO_CIPHER_ALG_AES_128: + case QCRYPTO_CIPHER_ALG_AES_256: + return true; + default: + return false; + } + return true;
This line is dead code.
crypto/cipher-gnutls.c.inc line 62
Quoted 6 lines
crypto/cipher-gnutls.c.inc new file +6
-0+#endif + default: + return false; + } +} +
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 15/18 crypto: add gnutls hash provider
In reply to Daniel P. BerrangéThis adds support for using gnutls as a provider of the crypto hash APIs.
--- crypto/hash-gnutls.c | 104 +++++++++++++++++++++++++++++++++++++++++++ crypto/meson.build | 2 + 2 files changed, 106 insertions(+) create mode 100644 crypto/hash-gnutls.c
crypto/hash-gnutls.c new file +104 -0
@@ -0,0 +1,104 @@ +/* +* QEMU Crypto hash algorithms +* +* Copyright (c) 2016 Red Hat, Inc. +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>. +* +*/ + +#include "qemu/osdep.h" +#include <gnutls/crypto.h> +#include "qapi/error.h" +#include "crypto/hash.h" +#include "hashpriv.h" + + +static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX ] = { + [QCRYPTO_HASH_ALG_MD5 ] =GNUTLS_DIG_MD5 , + [QCRYPTO_HASH_ALG_SHA1 ] =GNUTLS_DIG_SHA1 , + [QCRYPTO_HASH_ALG_SHA224 ] =GNUTLS_DIG_SHA224 , + [QCRYPTO_HASH_ALG_SHA256 ] =GNUTLS_DIG_SHA256 , + [QCRYPTO_HASH_ALG_SHA384 ] =GNUTLS_DIG_SHA384 , + [QCRYPTO_HASH_ALG_SHA512 ] =GNUTLS_DIG_SHA512 , + [QCRYPTO_HASH_ALG_RIPEMD160 ] =GNUTLS_DIG_RMD160 , +}; + +gboolean qcrypto_hash_supports (QCryptoHashAlgorithm alg) +{ +size_t i; +const gnutls_digest_algorithm_t *algs; +if (alg >=G_N_ELEMENTS (qcrypto_hash_alg_map) || + qcrypto_hash_alg_map[alg] ==GNUTLS_DIG_UNKNOWN ) { +return false; + } + algs =gnutls_digest_list (); +for (i =0 ; algs[i] !=GNUTLS_DIG_UNKNOWN ; i++) { +if (algs[i] == qcrypto_hash_alg_map[alg]) { +return true; + } + } +return false; +} + + +static int +qcrypto_gnutls_hash_bytesv (QCryptoHashAlgorithm alg, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +int i, ret; +gnutls_hash_hd_t hash; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + + ret =gnutls_hash_get_len (qcrypto_hash_alg_map[alg]); +if (*resultlen ==0 ) { + *resultlen = ret; + *result =g_new0 (uint8_t, *resultlen); + }else if (*resultlen != ret) { +error_setg (errp, +"Result buffer size %zu is smaller than hash %d" , + *resultlen, ret); +return -1 ; + } + + ret =gnutls_hash_init (&hash, qcrypto_hash_alg_map[alg]); +if (ret <0 ) { +error_setg (errp, +"Unable to initialize hash algorithm: %s" , +gnutls_strerror (ret)); +return -1 ; + } + +for (i =0 ; i < niov; i++) { +gnutls_hash (hash, iov[i].iov_base, iov[i].iov_len); + } + +gnutls_hash_deinit (hash, *result); +return 0 ; +} + + +QCryptoHashDriver qcrypto_hash_lib_driver = { + .hash_bytesv = qcrypto_gnutls_hash_bytesv, +};
crypto/meson.build +2 -0
@@ -28,6 +28,8 @@ if nettle.found() endif elif gcrypt.found() crypto_ss.add(gcrypt, files('hash-gcrypt.c', 'hmac-gcrypt.c', 'pbkdf-gcrypt.c')) +elif gnutls_crypto.found() + crypto_ss.add(gnutls, files('hash-gnutls.c', 'hmac-glib.c', 'pbkdf-stub.c') else crypto_ss.add(files('hash-glib.c', 'hmac-glib.c', 'pbkdf-stub.c')) endif
Signature · 1 line
2.31.1
Re: [PATCH 15/18] crypto: add gnutls hash provider
Quoted 16 lines · Daniel P. Berrangé
This adds support for using gnutls as a provider of the crypto hash APIs.
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/hash-gnutls.c | 104 +++++++++++++++++++++++++++++++++++++++++++ crypto/meson.build | 2 + 2 files changed, 106 insertions(+) create mode 100644 crypto/hash-gnutls.ccrypto/hash-gnutls.c new file +0
-0
crypto/hash-gnutls.c line 4
Quoted 4 lines
crypto/hash-gnutls.c new file +4
-0@@ -0,0 +1,104 @@ +/* +* QEMU Crypto hash algorithms +* +* Copyright (c) 2016 Red Hat, Inc.
Want to add 2021 here?
Quoted 13 lines
crypto/hash-gnutls.c new file +12
-0+static int +qcrypto_gnutls_hash_bytesv (QCryptoHashAlgorithm alg, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ + + ret =gnutls_hash_init (&hash, qcrypto_hash_alg_map[alg]); +if (ret <0 ) { + error_setg(errp,
crypto/hash-gnutls.c line 97
Quoted 10 lines
crypto/hash-gnutls.c new file +10
-0+"Unable to initialize hash algorithm: %s" , + gnutls_strerror(ret)); +return -1 ; + } + +for (i =0 ; i < niov; i++) { +gnutls_hash (hash, iov[i].iov_base, iov[i].iov_len); + } + +gnutls_hash_deinit (hash, *result);
Is there any speed penalty for creating a new gnutls_hash object for each call to qcrypto_gnutls_hash_bytesv(), compared to creating a hash object just once and using gnutls_hash_output() to grab results and reset it for reuse? But that can be a later patch (if it even works - I may have mis-read gnutls docs), so it doesn't affect review of this patch.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 16/18 crypto: add gnutls hmac provider
In reply to Daniel P. BerrangéThis adds support for using gnutls as a provider of the crypto hmac APIs.
--- crypto/hmac-gnutls.c | 136 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 crypto/hmac-gnutls.c
crypto/hmac-gnutls.c new file +136 -0
@@ -0,0 +1,136 @@ +/* +* QEMU Crypto hmac algorithms +* +* Derived from hmac-gcrypt.c: +* +* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* (at your option) any later version. See the COPYING file in the +* top-level directory. +* +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "crypto/hmac.h" +#include "hmacpriv.h" +#include <gnutls/crypto.h> + +static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX ] = { + [QCRYPTO_HASH_ALG_MD5 ] =GNUTLS_MAC_MD5 , + [QCRYPTO_HASH_ALG_SHA1 ] =GNUTLS_MAC_SHA1 , + [QCRYPTO_HASH_ALG_SHA224 ] =GNUTLS_MAC_SHA224 , + [QCRYPTO_HASH_ALG_SHA256 ] =GNUTLS_MAC_SHA256 , + [QCRYPTO_HASH_ALG_SHA384 ] =GNUTLS_MAC_SHA384 , + [QCRYPTO_HASH_ALG_SHA512 ] =GNUTLS_MAC_SHA512 , + [QCRYPTO_HASH_ALG_RIPEMD160 ] =GNUTLS_MAC_RMD160 , +}; + +typedef struct QCryptoHmacGnutls QCryptoHmacGnutls ; +struct QCryptoHmacGnutls { +gnutls_hmac_hd_t handle; +}; + +bool qcrypto_hmac_supports (QCryptoHashAlgorithm alg) +{ +size_t i; +const gnutls_digest_algorithm_t *algs; +if (alg >=G_N_ELEMENTS (qcrypto_hmac_alg_map) || + qcrypto_hmac_alg_map[alg] ==GNUTLS_DIG_UNKNOWN ) { +return false; + } + algs =gnutls_digest_list (); +for (i =0 ; algs[i] !=GNUTLS_DIG_UNKNOWN ; i++) { +if (algs[i] == qcrypto_hmac_alg_map[alg]) { +return true; + } + } +return false; +} + +void *qcrypto_hmac_ctx_new (QCryptoHashAlgorithm alg, +const uint8_t *key,size_t nkey, +Error **errp) +{ +QCryptoHmacGnutls *ctx; +int err; + +if (!qcrypto_hmac_supports (alg)) { +error_setg (errp,"Unsupported hmac algorithm %s" , +QCryptoHashAlgorithm_str (alg)); +return NULL ; + } + + ctx =g_new0 (QCryptoHmacGnutls,1 ); + + err =gnutls_hmac_init (&ctx->handle, + qcrypto_hmac_alg_map[alg], + (const void *)key, nkey); +if (err !=0 ) { +error_setg (errp,"Cannot initialize hmac: %s" , +gnutls_strerror (err)); + goto error; + } + +return ctx; + +error: +g_free (ctx); +return NULL ; +} + +static void +qcrypto_gnutls_hmac_ctx_free (QCryptoHmac *hmac) +{ +QCryptoHmacGnutls *ctx; + + ctx = hmac->opaque; +gnutls_hmac_deinit (ctx->handle,NULL ); + +g_free (ctx); +} + +static int +qcrypto_gnutls_hmac_bytesv (QCryptoHmac *hmac, +const struct iovec *iov, +size_t niov, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +QCryptoHmacGnutls *ctx; +uint32_t ret; +int i; + + ctx = hmac->opaque; + +for (i =0 ; i < niov; i++) { +gnutls_hmac (ctx->handle, iov[i].iov_base, iov[i].iov_len); + } + + ret =gnutls_hmac_get_len (qcrypto_hmac_alg_map[hmac->alg]); +if (ret <=0 ) { +error_setg (errp,"Unable to get hmac length: %s" , +gnutls_strerror (ret)); +return -1 ; + } + +if (*resultlen ==0 ) { + *resultlen = ret; + *result =g_new0 (uint8_t, *resultlen); + }else if (*resultlen != ret) { +error_setg (errp,"Result buffer size %zu is smaller than hmac %d" , + *resultlen, ret); +return -1 ; + } + +gnutls_hmac_output (ctx->handle, *result); + +return 0 ; +} + +QCryptoHmacDriver qcrypto_hmac_lib_driver = { + .hmac_bytesv = qcrypto_gnutls_hmac_bytesv, + .hmac_free = qcrypto_gnutls_hmac_ctx_free, +};
Signature · 1 line
2.31.1
Re: [PATCH 16/18] crypto: add gnutls hmac provider
Quoted 15 lines · Daniel P. Berrangé
This adds support for using gnutls as a provider of the crypto hmac APIs.
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/hmac-gnutls.c | 136 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 crypto/hmac-gnutls.ccrypto/hmac-gnutls.c new file +0
-0
crypto/hmac-gnutls.c line 6
Quoted 6 lines
crypto/hmac-gnutls.c new file +6
-0@@ -0,0 +1,136 @@ +/* +* QEMU Crypto hmac algorithms +* +* Derived from hmac-gcrypt.c: +* +* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
Is this line correct?
crypto/hmac-gnutls.c line 18
Quoted 12 lines
crypto/hmac-gnutls.c new file +12
-0+* +* This work is licensed under the terms of the GNU GPL, version 2 or +* (at your option) any later version. See the COPYING file in the +* top-level directory. +* +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "crypto/hmac.h" +#include "hmacpriv.h" +#include <gnutls/crypto.h>
Should you favor floating this to be right after "qemu/osdep.h"?
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH 16/18] crypto: add gnutls hmac provider
Quoted 10 lines · Eric Blake
Quoted 9 lines · Daniel P. Berrangé
This adds support for using gnutls as a provider of the crypto hmac APIs.
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/hmac-gnutls.c | 136 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 crypto/hmac-gnutls.c
Quoted 14 lines
crypto/hmac-gnutls.c line 6
Quoted 12 lines
crypto/hmac-gnutls.c new file +6
-0@@ -0,0 +1,136 @@ +/* +* QEMU Crypto hmac algorithms +* +* Derived from hmac-gcrypt.c: +* +* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. Is this line correct?
This is referring to the statement above "Derived from..."
I should have /also/ added a Red Hat copyright before though
Quoted 15 lines
crypto/hmac-gnutls.c line 18
Quoted 13 lines
crypto/hmac-gnutls.c new file +12
-0+* +* This work is licensed under the terms of the GNU GPL, version 2 or +* (at your option) any later version. See the COPYING file in the +* top-level directory. +* +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "crypto/hmac.h" +#include "hmacpriv.h" +#include <gnutls/crypto.h> Should you favor floating this to be right after "
qemu/osdep.h"?
yeah good idea
Quoted 8 lines
Reviewed-by Eric Blake <[email protected]>Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH 17/18 crypto: add gnutls pbkdf provider
In reply to Daniel P. BerrangéThis adds support for using gnutls as a provider of the crypto pbkdf APIs.
--- crypto/meson.build | 2 +- crypto/pbkdf-gnutls.c | 90 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 crypto/pbkdf-gnutls.c
crypto/meson.build +1 -1
@@ -29,7 +29,7 @@ if nettle.found() elif gcrypt.found() crypto_ss.add(gcrypt, files('hash-gcrypt.c', 'hmac-gcrypt.c', 'pbkdf-gcrypt.c')) elif gnutls_crypto.found() - crypto_ss.add(gnutls, files('hash-gnutls.c', 'hmac-glib.c', 'pbkdf-stub.c') + crypto_ss.add(gnutls, files('hash-gnutls.c', 'hmac-gnutls.c', 'pbkdf-gnutls.c')) else crypto_ss.add(files('hash-glib.c', 'hmac-glib.c', 'pbkdf-stub.c')) endif
crypto/pbkdf-gnutls.c new file +90 -0
90 lines not on this page · raw message
Signature · 1 line
2.31.1
Re: [PATCH 17/18] crypto: add gnutls pbkdf provider
Quoted 13 lines · Daniel P. Berrangé
This adds support for using gnutls as a provider of the crypto pbkdf APIs.
Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/meson.build | 2 +- crypto/pbkdf-gnutls.c | 90 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 crypto/pbkdf-gnutls.c+++
b/crypto/pbkdf-gnutls.c
crypto/pbkdf-gnutls.c line 4
Quoted 4 lines
crypto/pbkdf-gnutls.c new file +4
-0@@ -0,0 +1,90 @@ +/* +* QEMU Crypto PBKDF support (Password-Based Key Derivation Function) +* +* Copyright (c) 2015-2016 Red Hat, Inc.
Want to add 2021?
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 18/18 crypto: prefer gnutls as the crypto backend if new enough
In reply to Daniel P. BerrangéIf we have gnutls >= 3.6.13, then it has enough functionality and performance that we can use it as the preferred crypto backend.
--- meson.build | 59 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 24 deletions(-)
meson.build +35 -24
@@ -841,39 +841,50 @@ if not get_option('gnutls').auto() or have_system endif endif -# Gcrypt has priority over nettle +# We prefer use of gnutls for crypto, unless the options +# explicitly asked for nettle or gcrypt. +# +# If gnutls isn't available for crypto, then we'll prefer +# gcrypt over nettle for performance reasons. gcrypt = not_found nettle = not_found xts = 'none' + if get_option('nettle').enabled() and get_option('gcrypt').enabled() error('Only one of gcrypt & nettle can be enabled') -elif (not get_option('gcrypt').auto() or have_system) and not get_option('nettle').enabled() - gcrypt = dependency('libgcrypt', version: '>=1.8', - method: 'config-tool', - required: get_option('gcrypt'), - kwargs: static_kwargs) - # Debian has removed -lgpg-error from libgcrypt-config - # as it "spreads unnecessary dependencies" which in - # turn breaks static builds... - if gcrypt.found() and enable_static - gcrypt = declare_dependency(dependencies: [ - gcrypt, - cc.find_library('gpg-error', required: true, kwargs: static_kwargs)]) - endif -endif -if (not get_option('nettle').auto() or have_system) and not gcrypt.found() - nettle = dependency('nettle', version: '>=3.4', - method: 'pkg-config', - required: get_option('nettle'), - kwargs: static_kwargs) - if nettle.found() and not cc.has_header('nettle/xts.h', dependencies: nettle) - xts = 'private' - endif endif -if gcrypt.found() or nettle.found() + +# Explicit nettle/gcrypt request, so ignore gnutls for crypto +if get_option('nettle').enabled() or get_option('gcrypt').enabled() gnutls_crypto = not_found endif +if not gnutls_crypto.found() + if (not get_option('gcrypt').auto() or have_system) and not get_option('nettle').enabled() + gcrypt = dependency('libgcrypt', version: '>=1.8', + method: 'config-tool', + required: get_option('gcrypt'), + kwargs: static_kwargs) + # Debian has removed -lgpg-error from libgcrypt-config + # as it "spreads unnecessary dependencies" which in + # turn breaks static builds... + if gcrypt.found() and enable_static + gcrypt = declare_dependency(dependencies: [ + gcrypt, + cc.find_library('gpg-error', required: true, kwargs: static_kwargs)]) + endif + endif + if (not get_option('nettle').auto() or have_system) and not gcrypt.found() + nettle = dependency('nettle', version: '>=3.4', + method: 'pkg-config', + required: get_option('nettle'), + kwargs: static_kwargs) + if nettle.found() and not cc.has_header('nettle/xts.h', dependencies: nettle) + xts = 'private' + endif + endif +endif + gtk = not_found gtkx11 = not_found if not get_option('gtk').auto() or (have_system and not cocoa.found())
Signature · 1 line
2.31.1
Re: [PATCH 18/18] crypto: prefer gnutls as the crypto backend if new enough
Quoted 8 lines · Daniel P. Berrangé
If we have gnutls >= 3.6.13, then it has enough functionality and performance that we can use it as the preferred crypto backend.
Signed-off-by Daniel P. Berrangé <[email protected]>--- meson.build | 59 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 24 deletions(-)
Once again, take my meson review with a grain of salt.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org