Skip to content

Commit

Permalink
fix mbedtls_cipher_finish invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkb committed Dec 4, 2023
1 parent 1d64a83 commit 4f5b0b6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ void aes_encrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l) {
/* Prepare context */
mbedtls_cipher_reset(&ctx->cipher_enc);

int offset = 0;

/* XTS doesn't need per-block updating */
if (mbedtls_cipher_get_cipher_mode(&ctx->cipher_enc) == MBEDTLS_MODE_XTS || mbedtls_cipher_get_cipher_mode(&ctx->cipher_enc) == MBEDTLS_MODE_CBC)
mbedtls_cipher_update(&ctx->cipher_enc, (const unsigned char * )src, l, (unsigned char *)dst, &out_len);
Expand All @@ -77,15 +79,15 @@ void aes_encrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l) {
unsigned int blk_size = mbedtls_cipher_get_block_size(&ctx->cipher_enc);

/* Do per-block updating */
for (int offset = 0; (unsigned int)offset < l; offset += blk_size)
for (; (unsigned int)offset < l; offset += blk_size)
{
int len = ((unsigned int)(l - offset) > blk_size) ? blk_size : (unsigned int) (l - offset);
mbedtls_cipher_update(&ctx->cipher_enc, (const unsigned char * )src + offset, len, (unsigned char *)dst + offset, &out_len);
}
}

/* Flush all data */
mbedtls_cipher_finish(&ctx->cipher_enc, NULL, NULL);
mbedtls_cipher_finish(&ctx->cipher_enc, (unsigned char * )dst + offset + out_len, &out_len);
}

/* Decrypt with context. */
Expand All @@ -109,6 +111,8 @@ void aes_decrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l)
/* Prepare context */
mbedtls_cipher_reset(&ctx->cipher_dec);

int offset = 0;

/* XTS doesn't need per-block updating */
if (mbedtls_cipher_get_cipher_mode(&ctx->cipher_dec) == MBEDTLS_MODE_XTS || mbedtls_cipher_get_cipher_mode(&ctx->cipher_enc) == MBEDTLS_MODE_CBC)
mbedtls_cipher_update(&ctx->cipher_dec, (const unsigned char * )src, l, (unsigned char *)dst, &out_len);
Expand All @@ -117,15 +121,15 @@ void aes_decrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l)
unsigned int blk_size = mbedtls_cipher_get_block_size(&ctx->cipher_dec);

/* Do per-block updating */
for (int offset = 0; (unsigned int)offset < l; offset += blk_size)
for (; (unsigned int)offset < l; offset += blk_size)
{
int len = ((unsigned int)(l - offset) > blk_size) ? blk_size : (unsigned int) (l - offset);
mbedtls_cipher_update(&ctx->cipher_dec, (const unsigned char * )src + offset, len, (unsigned char *)dst + offset, &out_len);
}
}

/* Flush all data */
mbedtls_cipher_finish(&ctx->cipher_dec, NULL, NULL);
mbedtls_cipher_finish(&ctx->cipher_dec, (unsigned char * )dst + offset + out_len, &out_len);

if (src_equals_dst)
{
Expand Down

0 comments on commit 4f5b0b6

Please sign in to comment.