mirror of https://github.com/xqemu/xqemu.git
crypto: don't let builtin aes crash if no IV is provided
If no IV is provided, then use a default IV of all-zeros instead of crashing. This gives parity with gcrypt and nettle backends. Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
parent
91bfcdb01d
commit
eb2a770b17
|
@ -25,8 +25,7 @@ typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES;
|
||||||
struct QCryptoCipherBuiltinAES {
|
struct QCryptoCipherBuiltinAES {
|
||||||
AES_KEY encrypt_key;
|
AES_KEY encrypt_key;
|
||||||
AES_KEY decrypt_key;
|
AES_KEY decrypt_key;
|
||||||
uint8_t *iv;
|
uint8_t iv[AES_BLOCK_SIZE];
|
||||||
size_t niv;
|
|
||||||
};
|
};
|
||||||
typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB;
|
typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB;
|
||||||
struct QCryptoCipherBuiltinDESRFB {
|
struct QCryptoCipherBuiltinDESRFB {
|
||||||
|
@ -61,7 +60,6 @@ static void qcrypto_cipher_free_aes(QCryptoCipher *cipher)
|
||||||
{
|
{
|
||||||
QCryptoCipherBuiltin *ctxt = cipher->opaque;
|
QCryptoCipherBuiltin *ctxt = cipher->opaque;
|
||||||
|
|
||||||
g_free(ctxt->state.aes.iv);
|
|
||||||
g_free(ctxt);
|
g_free(ctxt);
|
||||||
cipher->opaque = NULL;
|
cipher->opaque = NULL;
|
||||||
}
|
}
|
||||||
|
@ -145,15 +143,13 @@ static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
QCryptoCipherBuiltin *ctxt = cipher->opaque;
|
QCryptoCipherBuiltin *ctxt = cipher->opaque;
|
||||||
if (niv != 16) {
|
if (niv != AES_BLOCK_SIZE) {
|
||||||
error_setg(errp, "IV must be 16 bytes not %zu", niv);
|
error_setg(errp, "IV must be %d bytes not %zu",
|
||||||
|
AES_BLOCK_SIZE, niv);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free(ctxt->state.aes.iv);
|
memcpy(ctxt->state.aes.iv, iv, AES_BLOCK_SIZE);
|
||||||
ctxt->state.aes.iv = g_new0(uint8_t, niv);
|
|
||||||
memcpy(ctxt->state.aes.iv, iv, niv);
|
|
||||||
ctxt->state.aes.niv = niv;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -287,6 +287,32 @@ static void test_cipher(const void *opaque)
|
||||||
qcrypto_cipher_free(cipher);
|
qcrypto_cipher_free(cipher);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_cipher_null_iv(void)
|
||||||
|
{
|
||||||
|
QCryptoCipher *cipher;
|
||||||
|
uint8_t key[32] = { 0 };
|
||||||
|
uint8_t plaintext[32] = { 0 };
|
||||||
|
uint8_t ciphertext[32] = { 0 };
|
||||||
|
|
||||||
|
cipher = qcrypto_cipher_new(
|
||||||
|
QCRYPTO_CIPHER_ALG_AES_256,
|
||||||
|
QCRYPTO_CIPHER_MODE_CBC,
|
||||||
|
key, sizeof(key),
|
||||||
|
&error_abort);
|
||||||
|
g_assert(cipher != NULL);
|
||||||
|
|
||||||
|
/* Don't call qcrypto_cipher_setiv */
|
||||||
|
|
||||||
|
qcrypto_cipher_encrypt(cipher,
|
||||||
|
plaintext,
|
||||||
|
ciphertext,
|
||||||
|
sizeof(plaintext),
|
||||||
|
&error_abort);
|
||||||
|
|
||||||
|
qcrypto_cipher_free(cipher);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
@ -298,5 +324,9 @@ int main(int argc, char **argv)
|
||||||
for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
|
for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
|
||||||
g_test_add_data_func(test_data[i].path, &test_data[i], test_cipher);
|
g_test_add_data_func(test_data[i].path, &test_data[i], test_cipher);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g_test_add_func("/crypto/cipher/null-iv",
|
||||||
|
test_cipher_null_iv);
|
||||||
|
|
||||||
return g_test_run();
|
return g_test_run();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue