From d0eebc1f94cd2dd9f1543e46463ed416261b6fca Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Mon, 20 Nov 2023 15:17:53 +0100 Subject: [PATCH] ccm/gcm: improve code maintainability Signed-off-by: Valerio Setti --- library/ccm.c | 23 ++++++++--------------- library/gcm.c | 36 +++++++++++++----------------------- 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/library/ccm.c b/library/ccm.c index 444351df0e..6700dc743d 100644 --- a/library/ccm.c +++ b/library/ccm.c @@ -130,20 +130,15 @@ static int mbedtls_ccm_crypt(mbedtls_ccm_context *ctx, #if defined(MBEDTLS_CIPHER_C) size_t olen = 0; - - if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->ctr, 16, tmp_buf, - &olen)) != 0) { - ctx->state |= CCM_STATE__ERROR; - mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf)); - return ret; - } + ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->ctr, 16, tmp_buf, &olen); #else - if ((ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->ctr, tmp_buf)) != 0) { + ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->ctr, tmp_buf); +#endif + if (ret != 0) { ctx->state |= CCM_STATE__ERROR; mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf)); return ret; } -#endif mbedtls_xor(output, input, tmp_buf + offset, use_len); @@ -212,16 +207,14 @@ static int ccm_calculate_first_block_if_ready(mbedtls_ccm_context *ctx) /* Start CBC-MAC with first block*/ #if defined(MBEDTLS_CIPHER_C) - if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen)) != 0) { - ctx->state |= CCM_STATE__ERROR; - return ret; - } + ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen); #else - if ((ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y)) != 0) { + ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y); +#endif + if (ret != 0) { ctx->state |= CCM_STATE__ERROR; return ret; } -#endif return 0; } diff --git a/library/gcm.c b/library/gcm.c index 390bb3e156..8181ec88aa 100644 --- a/library/gcm.c +++ b/library/gcm.c @@ -68,15 +68,13 @@ static int gcm_gen_table(mbedtls_gcm_context *ctx) #if defined(MBEDTLS_CIPHER_C) size_t olen = 0; - - if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, h, 16, h, &olen)) != 0) { - return ret; - } + ret = mbedtls_cipher_update(&ctx->cipher_ctx, h, 16, h, &olen); #else - if ((ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, h, h)) != 0) { + ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, h, h); +#endif + if (ret != 0) { return ret; } -#endif /* pack h as two 64-bits ints, big-endian */ hi = MBEDTLS_GET_UINT32_BE(h, 0); @@ -323,16 +321,13 @@ int mbedtls_gcm_starts(mbedtls_gcm_context *ctx, #if defined(MBEDTLS_CIPHER_C) - if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, - ctx->base_ectr, &olen)) != 0) { - return ret; - } + ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->base_ectr, &olen); #else - if ((ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, - ctx->base_ectr)) != 0) { + ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->base_ectr); +#endif + if (ret != 0) { return ret; } -#endif return 0; } @@ -423,22 +418,17 @@ static int gcm_mask(mbedtls_gcm_context *ctx, unsigned char *output) { int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + #if defined(MBEDTLS_CIPHER_C) size_t olen = 0; - - if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ectr, - &olen)) != 0) { - mbedtls_platform_zeroize(ectr, 16); - return ret; - } - + ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ectr, &olen); #else - - if ((ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ectr)) != 0) { + ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ectr); +#endif + if (ret != 0) { mbedtls_platform_zeroize(ectr, 16); return ret; } -#endif if (ctx->mode == MBEDTLS_GCM_DECRYPT) { mbedtls_xor(ctx->buf + offset, ctx->buf + offset, input, use_len);