From 0f87b92f702c83c1cc18798d1b05b9aacd8ce186 Mon Sep 17 00:00:00 2001 From: Dmitry Baryshkov Date: Fri, 17 Apr 2020 23:07:07 +0300 Subject: [PATCH 36/87] magma: fix IV handling for CTR mode magma-ctr uses half length IV per the specification, which is handled correctly. However we still have to zero the second half of IV. Do so in ctr_init_key() callback. Signed-off-by: Dmitry Baryshkov --- src/lib/libcrypto/evp/e_magma.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib/libcrypto/evp/e_magma.c b/src/lib/libcrypto/evp/e_magma.c index 712f79278..c88b25827 100644 --- a/src/lib/libcrypto/evp/e_magma.c +++ b/src/lib/libcrypto/evp/e_magma.c @@ -96,6 +96,19 @@ Magma_ofb64_encrypt(const unsigned char *in, unsigned char *out, size_t length, (block64_f)Magma_encrypt); } +static int +magma_ctr_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, + const unsigned char *iv, int enc) +{ + if (iv) + memset(ctx->iv + 4, 0, 4); + + if (!key) + return 1; + + return magma_init_key(ctx, key, iv, enc); +} + static int magma_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t len) @@ -114,8 +127,8 @@ IMPLEMENT_BLOCK_CIPHER(magma, ks, Magma, EVP_MAGMA_CTX, magma_ctl) BLOCK_CIPHER_def1(magma, ctr, ctr, CTR, EVP_MAGMA_CTX, - NID_magma, 1, 32, 4, 0, - magma_init_key, NULL, + NID_magma, 1, 32, 4, EVP_CIPH_ALWAYS_CALL_INIT, + magma_ctr_init_key, NULL, EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, magma_ctl) -- 2.17.1