Fix aes buffer passing error (closes #59)

This commit is contained in:
Michael Scire 2018-11-24 00:34:42 -08:00
parent 499f06ed9c
commit 3b399f2197

23
aes.c
View file

@ -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 */
@ -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);