Skip to content

Commit

Permalink
Fix aes buffer passing error (closes #59)
Browse files Browse the repository at this point in the history
  • Loading branch information
SciresM committed Nov 24, 2018
1 parent 499f06e commit 3b399f2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,21 @@ void aes_encrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l) {
}

/* Decrypt with context. */
void aes_decrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l) {
void aes_decrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l)
{
bool src_equals_dst = false;

if (src == dst)
{
src_equals_dst = true;

dst = malloc(l);
if (dst == NULL) {
fprintf(stderr, "Error: AES buffer allocation failure!\n");
exit(EXIT_FAILURE);
}
}

size_t out_len = 0;

/* Prepare context */
Expand All @@ -111,8 +125,15 @@ void aes_decrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l) {

/* Flush all data */
mbedtls_cipher_finish(&ctx->cipher_dec, NULL, NULL);

if (src_equals_dst)
{
memcpy((void*)src, dst, l);
free(dst);
}
}


static void get_tweak(unsigned char *tweak, size_t sector) {
for (int i = 0xF; i >= 0; i--) { /* Nintendo LE custom tweak... */
tweak[i] = (unsigned char)(sector & 0xFF);
Expand Down

0 comments on commit 3b399f2

Please sign in to comment.