From: chaoskagami Date: Sun, 10 Jul 2016 00:01:52 +0000 (-0400) Subject: Still utterly broken crypto port X-Git-Tag: v0.2.0~25 X-Git-Url: https://chaos.moe/g/?a=commitdiff_plain;h=d8a611dc263ff46c0bd07b403bbd7cf2744cb1f9;p=corbenik%2Fcorbenik.git Still utterly broken crypto port --- diff --git a/source/firm/firm.c b/source/firm/firm.c index b33e4bd..19abe4b 100644 --- a/source/firm/firm.c +++ b/source/firm/firm.c @@ -66,7 +66,34 @@ ncch_getctr(const ncch_h *ncch, uint8_t *ctr, uint8_t type) void aes(void *dst, const void *src, uint32_t blockCount, void *iv, uint32_t mode, uint32_t ivMode) { - set_ctr(iv); + aes_setmode(mode); + uint32_t blocks; + while (blockCount != 0) { + if ((mode & AES_ALL_MODES) != AES_ECB_ENCRYPT_MODE && (mode & AES_ALL_MODES) != AES_ECB_DECRYPT_MODE) + aes_setiv(iv, ivMode); + blocks = (blockCount >= 0xFFFF) ? 0xFFFF : blockCount; + + // Save the last block for the next decryption CBC batch's iv + if ((mode & AES_ALL_MODES) == AES_CBC_DECRYPT_MODE) { + memcpy(iv, src + (blocks - 1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE); + aes_change_ctrmode(iv, AES_INPUT_BE | AES_INPUT_NORMAL, ivMode); + } + + // Process the current batch + aes_batch(dst, src, blocks); + + // Save the last block for the next encryption CBC batch's iv + if ((mode & AES_ALL_MODES) == AES_CBC_ENCRYPT_MODE) { + memcpy(iv, dst + (blocks - 1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE); + aes_change_ctrmode(iv, AES_INPUT_BE | AES_INPUT_NORMAL, ivMode); + } else if ((mode & AES_ALL_MODES) == AES_CTR_MODE) { // Advance counter for CTR mode + aes_advctr(iv, blocks, ivMode); + } + + src += blocks * AES_BLOCK_SIZE; + dst += blocks * AES_BLOCK_SIZE; + blockCount -= blocks; + } } void