Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mbedtls_cipher_finish invocations #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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