crypto: ensure XTS is only used with ciphers with 16 byte blocks
PATCH v2 1/1 crypto: ensure XTS is only used with ciphers with 16 byte blocks
The XTS cipher mode needs to be used with a cipher which has a block size of 16 bytes. If a mis-matching block size is used, the code will either corrupt memory beyond the IV array, or not fully encrypt/decrypt the IV.
This fixes a memory corruption crash when attempting to use cast5-128 with xts, since the former has an 8 byte block size.
A test case is added to ensure the cipher creation fails with such an invalid combination.
Reviewed-by Eric Blake <[email protected]>
Signed-off-by Daniel P. Berrange <[email protected]>
--- crypto/cipher-gcrypt.c | 6 ++++++ crypto/cipher-nettle.c | 12 +++++++----- tests/test-crypto-cipher.c | 43 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 48 insertions(+), 13 deletions(-)
crypto/cipher-gcrypt.c +6 -0
@@ -192,6 +192,12 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, }if (cipher->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; + } ctx->iv =g_new0 (uint8_t, ctx->blocksize); }
crypto/cipher-nettle.c +7 -5
@@ -361,6 +361,13 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, goto error; } +if (mode ==QCRYPTO_CIPHER_MODE_XTS && + 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; + } + ctx->iv =g_new0 (uint8_t, ctx->blocksize); cipher->opaque = ctx;
@@ -456,11 +463,6 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher,break ;case QCRYPTO_CIPHER_MODE_XTS : -if (ctx->blocksize !=XTS_BLOCK_SIZE ) { -error_setg (errp,"Block size must be %d not %zu" , -XTS_BLOCK_SIZE , ctx->blocksize); -return -1 ; - }xts_decrypt (ctx->ctx, ctx->ctx_tweak, ctx->alg_encrypt_wrapper, ctx->alg_decrypt_wrapper, ctx->iv, len, out, in);
tests/test-crypto-cipher.c +35 -8
70 lines not on this page · raw message
Signature · 1 line
2.7.4