Commit 056e8236 authored by Olaf Bergmann's avatar Olaf Bergmann

dcaf_crypt_mbedtls.c: normalize AES keys to block length

MbedTLS requires AES keys to have the exact block length in function
mbedtls_cipher_setkey(). The key passed to the dcaf
encryption/decryption functions therefore are filled with zero bytes
when to short, or truncated when too long.
parent 012a3a70
......@@ -75,6 +75,9 @@ setup_cipher_context(mbedtls_cipher_context_t *ctx,
mbedtls_operation_t mode) {
const mbedtls_cipher_info_t *cipher_info;
int tmp;
uint8_t key[DCAF_MAX_KEY_SIZE]; /* buffer for normalizing the key according to its key length */
int klen;
memset(key, 0, sizeof(key));
tmp = get_alg(dcaf_alg);
......@@ -87,7 +90,13 @@ setup_cipher_context(mbedtls_cipher_context_t *ctx,
C(mbedtls_cipher_setup(ctx, cipher_info));
C(mbedtls_cipher_setkey(ctx, key_data, 8 * key_length, mode));
klen = mbedtls_cipher_get_key_bitlen(ctx);
if ((klen > (int)(sizeof(key) * 8)) || (key_length > sizeof(key))) {
dcaf_log(DCAF_LOG_CRIT, "dcaf_crypto: cannot set key\n");
goto error;
memcpy(key, key_data, key_length);
C(mbedtls_cipher_setkey(ctx, key, klen, mode));
/* On success, the cipher context is released by the caller. */
return true;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment