hactool/aes.c

176 lines
5.5 KiB
C
Raw Normal View History

2018-01-24 17:52:25 +00:00
#include <stdlib.h>
#include <stdio.h>
2018-11-24 09:01:30 +00:00
#include <string.h>
2018-01-24 17:52:25 +00:00
#include "aes.h"
#include "types.h"
#include "utils.h"
/* Allocate a new context. */
2018-01-29 18:39:30 +00:00
aes_ctx_t *new_aes_ctx(const void *key, unsigned int key_size, aes_mode_t mode) {
2018-01-24 17:52:25 +00:00
aes_ctx_t *ctx;
2020-01-15 10:07:22 +00:00
2018-01-24 17:52:25 +00:00
if ((ctx = malloc(sizeof(*ctx))) == NULL) {
FATAL_ERROR("Failed to allocate aes_ctx_t!");
}
2018-01-29 18:39:30 +00:00
mbedtls_cipher_init(&ctx->cipher_dec);
mbedtls_cipher_init(&ctx->cipher_enc);
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
if (mbedtls_cipher_setup(&ctx->cipher_dec, mbedtls_cipher_info_from_type(mode))
|| mbedtls_cipher_setup(&ctx->cipher_enc, mbedtls_cipher_info_from_type(mode))) {
FATAL_ERROR("Failed to set up AES context!");
2018-01-24 17:52:25 +00:00
}
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
if (mbedtls_cipher_setkey(&ctx->cipher_dec, key, key_size * 8, AES_DECRYPT)
|| mbedtls_cipher_setkey(&ctx->cipher_enc, key, key_size * 8, AES_ENCRYPT)) {
FATAL_ERROR("Failed to set key for AES context!");
2018-01-24 17:52:25 +00:00
}
2020-01-15 10:07:22 +00:00
2018-01-24 17:52:25 +00:00
return ctx;
}
/* Free an allocated context. */
void free_aes_ctx(aes_ctx_t *ctx) {
/* Explicitly allow NULL. */
if (ctx == NULL) {
return;
}
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
mbedtls_cipher_free(&ctx->cipher_dec);
mbedtls_cipher_free(&ctx->cipher_enc);
2018-01-24 17:52:25 +00:00
free(ctx);
}
/* Set AES CTR or IV for a context. */
void aes_setiv(aes_ctx_t *ctx, const void *iv, size_t l) {
2018-01-29 18:39:30 +00:00
if (mbedtls_cipher_set_iv(&ctx->cipher_dec, iv, l)
|| mbedtls_cipher_set_iv(&ctx->cipher_enc, iv, l)) {
FATAL_ERROR("Failed to set IV for AES context!");
2018-01-24 17:52:25 +00:00
}
}
/* Calculate CMAC. */
void aes_calculate_cmac(void *dst, void *src, size_t size, const void *key) {
mbedtls_cipher_context_t m_ctx;
mbedtls_cipher_init(&m_ctx);
if (mbedtls_cipher_setup(&m_ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB))
|| mbedtls_cipher_cmac_starts(&m_ctx, key, 0x80)
|| mbedtls_cipher_cmac_update(&m_ctx, src, size)
|| mbedtls_cipher_cmac_finish(&m_ctx, dst)) {
FATAL_ERROR("Failed to calculate CMAC!");
}
}
2018-01-24 17:52:25 +00:00
/* Encrypt with context. */
void aes_encrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l) {
2018-01-29 18:39:30 +00:00
size_t out_len = 0;
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
/* Prepare context */
mbedtls_cipher_reset(&ctx->cipher_enc);
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
/* XTS doesn't need per-block updating */
if (mbedtls_cipher_get_cipher_mode(&ctx->cipher_enc) == MBEDTLS_MODE_XTS)
mbedtls_cipher_update(&ctx->cipher_enc, (const unsigned char * )src, l, (unsigned char *)dst, &out_len);
else
{
unsigned int blk_size = mbedtls_cipher_get_block_size(&ctx->cipher_enc);
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
/* Do per-block updating */
for (int offset = 0; (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);
}
2018-01-24 17:52:25 +00:00
}
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
/* Flush all data */
mbedtls_cipher_finish(&ctx->cipher_enc, NULL, NULL);
2018-01-24 17:52:25 +00:00
}
/* Decrypt with context. */
2020-01-15 10:07:22 +00:00
void aes_decrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l)
{
2020-01-15 10:07:22 +00:00
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);
}
}
2018-01-29 18:39:30 +00:00
size_t out_len = 0;
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
/* Prepare context */
mbedtls_cipher_reset(&ctx->cipher_dec);
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
/* XTS doesn't need per-block updating */
if (mbedtls_cipher_get_cipher_mode(&ctx->cipher_dec) == MBEDTLS_MODE_XTS)
mbedtls_cipher_update(&ctx->cipher_dec, (const unsigned char * )src, l, (unsigned char *)dst, &out_len);
else
{
unsigned int blk_size = mbedtls_cipher_get_block_size(&ctx->cipher_dec);
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
/* Do per-block updating */
for (int offset = 0; (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);
}
}
2020-01-15 10:07:22 +00:00
2018-01-29 18:39:30 +00:00
/* Flush all data */
mbedtls_cipher_finish(&ctx->cipher_dec, NULL, NULL);
if (src_equals_dst)
{
memcpy((void*)src, dst, l);
free(dst);
}
2018-01-24 17:52:25 +00:00
}
static void get_tweak(unsigned char *tweak, size_t sector) {
2018-01-24 17:52:25 +00:00
for (int i = 0xF; i >= 0; i--) { /* Nintendo LE custom tweak... */
tweak[i] = (unsigned char)(sector & 0xFF);
sector >>= 8;
}
}
2018-01-29 18:39:30 +00:00
/* Encrypt with context for XTS. */
void aes_xts_encrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l, size_t sector, size_t sector_size) {
2018-01-24 17:52:25 +00:00
unsigned char tweak[0x10];
if (l % sector_size != 0) {
FATAL_ERROR("Length must be multiple of sectors!");
}
for (size_t i = 0; i < l; i += sector_size) {
/* Workaround for Nintendo's custom sector...manually generate the tweak. */
get_tweak(tweak, sector++);
2018-01-29 18:39:30 +00:00
aes_setiv(ctx, tweak, 16);
aes_encrypt(ctx, (char *)dst + i, (const char *)src + i, sector_size);
2018-01-24 17:52:25 +00:00
}
}
2018-01-29 18:39:30 +00:00
/* Decrypt with context for XTS. */
void aes_xts_decrypt(aes_ctx_t *ctx, void *dst, const void *src, size_t l, size_t sector, size_t sector_size) {
2018-01-24 17:52:25 +00:00
unsigned char tweak[0x10];
if (l % sector_size != 0) {
FATAL_ERROR("Length must be multiple of sectors!");
}
for (size_t i = 0; i < l; i += sector_size) {
/* Workaround for Nintendo's custom sector...manually generate the tweak. */
get_tweak(tweak, sector++);
2018-01-29 18:39:30 +00:00
aes_setiv(ctx, tweak, 16);
aes_decrypt(ctx, (char *)dst + i, (const char *)src + i, sector_size);
2018-01-24 17:52:25 +00:00
}
}