diff --git a/include/arm11/hardware.h b/include/arm11/hardware.h index 9fdf988..10d362c 100644 --- a/include/arm11/hardware.h +++ b/include/arm11/hardware.h @@ -3,3 +3,4 @@ void hardwareInit(void); +//void hardwareDeinit(void); diff --git a/include/arm9/crypto.h b/include/arm9/crypto.h index cc2ceca..629a207 100644 --- a/include/arm9/crypto.h +++ b/include/arm9/crypto.h @@ -16,7 +16,7 @@ #define AES_MAX_BLOCKS (0xFFFC) #define AES_WRITE_FIFO_COUNT (REG_AESCNT & 0x1F) -#define AES_READ_FIFO_COUNT (REG_AESCNT>>5 & 0x1F) +#define AES_READ_FIFO_COUNT (REG_AESCNT & 0x3E0) #define AES_FLUSH_READ_FIFO (1u<<10) #define AES_FLUSH_WRITE_FIFO (1u<<11) @@ -46,6 +46,13 @@ #define AES_MODE_ECB_ENCRYPT (7u<<27) +typedef enum +{ + AES_KEY_NORMAL = 0, + AES_KEY_X = 1, + AES_KEY_Y = 2, +} AesKeyType; + typedef struct { u32 ctrIvNonceParams; @@ -60,33 +67,15 @@ void AES_init(void); /** - * @brief Sets a normal key. + * @brief Sets a AES key in the specified keyslot. * * @param[in] keyslot The keyslot this key will be set for. + * @param[in] type The key type. Can be AES_KEY_NORMAL/X/Y. * @param[in] orderEndianess Word order and endianess bitmask. + * @param[in] twlScrambler Set to true to use the TWL keyscrambler for keyslots > 0x03. * @param[in] key Pointer to 128-bit AES key data. */ -void AES_setNormalKey(u8 keyslot, u8 orderEndianess, const u32 key[4]); - -/** - * @brief Sets a keyX. - * - * @param[in] keyslot The keyslot this key will be set for. - * @param[in] orderEndianess Word order and endianess bitmask. - * @param[in] useTwlScrambler Set to true to use the TWL keyscrambler for keyslots > 0x03. - * @param[in] keyX Pointer to 128-bit AES keyX data. - */ -void AES_setKeyX(u8 keyslot, u8 orderEndianess, bool useTwlScrambler, const u32 keyX[4]); - -/** - * @brief Sets a keyY. - * - * @param[in] keyslot The keyslot this key will be set for. - * @param[in] orderEndianess Word order and endianess bitmask. - * @param[in] useTwlScrambler Set to true to use the TWL keyscrambler for keyslots > 0x03. - * @param[in] keyY Pointer to 128-bit AES keyY data. - */ -void AES_setKeyY(u8 keyslot, u8 orderEndianess, bool useTwlScrambler, const u32 keyY[4]); +void AES_setKey(u8 keyslot, AesKeyType type, u8 orderEndianess, bool twlScrambler, const u32 key[4]); /** * @brief Selects the given keyslot for all following crypto operations. diff --git a/include/arm9/hardware.h b/include/arm9/hardware.h index 9fdf988..d9dd1e5 100644 --- a/include/arm9/hardware.h +++ b/include/arm9/hardware.h @@ -3,3 +3,4 @@ void hardwareInit(void); +void hardwareDeinit(void); diff --git a/source/arm11/hardware.c b/source/arm11/hardware.c index 8cab4ff..884a537 100644 --- a/source/arm11/hardware.c +++ b/source/arm11/hardware.c @@ -7,3 +7,7 @@ { PXI_init(); } + +/*void hardwareDeinit(void) +{ +}*/ diff --git a/source/arm9/crypto.c b/source/arm9/crypto.c index 53493e6..6634b7d 100644 --- a/source/arm9/crypto.c +++ b/source/arm9/crypto.c @@ -43,9 +43,9 @@ #define REG_AESKEYX3 ((vu32*)(AES_REGS_BASE + 0x0E0)) #define REG_AESKEYY3 ((vu32*)(AES_REGS_BASE + 0x0F0)) -#define REG_AESKEYFIFO *((vu32*)(AES_REGS_BASE + 0x100)) -#define REG_AESKEYXFIFO *((vu32*)(AES_REGS_BASE + 0x104)) -#define REG_AESKEYYFIFO *((vu32*)(AES_REGS_BASE + 0x108)) +#define REG_AESKEYFIFO ((vu32*)(AES_REGS_BASE + 0x100)) +#define REG_AESKEYXFIFO ((vu32*)(AES_REGS_BASE + 0x104)) +#define REG_AESKEYYFIFO ((vu32*)(AES_REGS_BASE + 0x108)) void AES_init(void) @@ -66,7 +66,7 @@ REG_IRQ_IE |= 1u< 3) { - REG_AESKEYCNT = keyslot | 0x80u; - REG_AESKEYFIFO = key[0]; - REG_AESKEYFIFO = key[1]; - REG_AESKEYFIFO = key[2]; - REG_AESKEYFIFO = key[3]; + REG_AESKEYCNT = 0x80u | (type > AES_KEY_NORMAL && twlScrambler ? 1u : 0u)<<6 | keyslot; + REG_AESKEYFIFO[type] = key[0]; + REG_AESKEYFIFO[type] = key[1]; + REG_AESKEYFIFO[type] = key[2]; + REG_AESKEYFIFO[type] = key[3]; } else { u32 lastu32; - vu32 *twlKeyNReg = ®_AESKEY0[12 * keyslot]; - if(orderEndianess & 4) + vu32 *twlKeyNReg = ®_AESKEY0[12u * keyslot + type * 4u]; + if(orderEndianess & AES_INPUT_NORMAL) { twlKeyNReg[0] = key[3]; twlKeyNReg[1] = key[2]; @@ -103,80 +103,6 @@ } } -void AES_setKeyX(u8 keyslot, u8 orderEndianess, bool useTwlScrambler, const u32 keyX[4]) -{ - assert(keyslot < 0x40); - assert(keyX != NULL); - - - REG_AESCNT = (u32)orderEndianess<<23; - if(keyslot > 3) - { - REG_AESKEYCNT = keyslot | (u8)useTwlScrambler<<6 | 0x80u; - REG_AESKEYXFIFO = keyX[0]; - REG_AESKEYXFIFO = keyX[1]; - REG_AESKEYXFIFO = keyX[2]; - REG_AESKEYXFIFO = keyX[3]; - } - else - { - u32 lastu32; - vu32 *twlKeyNReg = ®_AESKEY0[12 * keyslot]; - if(orderEndianess & 4) - { - twlKeyNReg[4] = keyX[3]; - twlKeyNReg[5] = keyX[2]; - twlKeyNReg[6] = keyX[1]; - lastu32 = keyX[0]; - } - else - { - twlKeyNReg[4] = keyX[0]; - twlKeyNReg[5] = keyX[1]; - twlKeyNReg[6] = keyX[2]; - lastu32 = keyX[3]; - } - twlKeyNReg[7] = lastu32; - } -} - -void AES_setKeyY(u8 keyslot, u8 orderEndianess, bool useTwlScrambler, const u32 keyY[4]) -{ - assert(keyslot < 0x40); - assert(keyY != NULL); - - - REG_AESCNT = (u32)orderEndianess<<23; - if(keyslot > 3) - { - REG_AESKEYCNT = keyslot | (u8)useTwlScrambler<<6 | 0x80u; - REG_AESKEYYFIFO = keyY[0]; - REG_AESKEYYFIFO = keyY[1]; - REG_AESKEYYFIFO = keyY[2]; - REG_AESKEYYFIFO = keyY[3]; - } - else - { - u32 lastu32; - vu32 *twlKeyNReg = ®_AESKEY0[12 * keyslot]; - if(orderEndianess & 4) - { - twlKeyNReg[8] = keyY[3]; - twlKeyNReg[9] = keyY[2]; - twlKeyNReg[10] = keyY[1]; - lastu32 = keyY[0]; - } - else - { - twlKeyNReg[8] = keyY[0]; - twlKeyNReg[9] = keyY[1]; - twlKeyNReg[10] = keyY[2]; - lastu32 = keyY[3]; - } - twlKeyNReg[11] = lastu32; - } -} - void AES_selectKeyslot(u8 keyslot) { assert(keyslot < 0x40); @@ -195,7 +121,7 @@ ctx->ctrIvNonceParams = (u32)orderEndianess<<23; u32 *const ctrIvNonce = ctx->ctrIvNonce; u32 lastu32; - if(orderEndianess & 4) + if(orderEndianess & AES_INPUT_NORMAL) { ctrIvNonce[0] = nonce[2]; ctrIvNonce[1] = nonce[1]; @@ -219,7 +145,7 @@ ctx->ctrIvNonceParams = (u32)orderEndianess<<23; u32 *const ctrIvNonce = ctx->ctrIvNonce; u32 lastu32; - if(orderEndianess & 4) + if(orderEndianess & AES_INPUT_NORMAL) { ctrIvNonce[0] = ctrIv[3]; ctrIvNonce[1] = ctrIv[2]; @@ -282,7 +208,7 @@ ctx->aesParams = (u32)inEndianessOrder<<23 | (u32)outEndianessOrder<<22; } -static void processBlocksCpu(const u32 *in, u32 *out, u32 blocks) +static void aesProcessBlocksCpu(const u32 *in, u32 *out, u32 blocks) { REG_AES_BLKCNT_HIGH = blocks; REG_AESCNT |= AES_ENABLE | 3<<12 | AES_FLUSH_READ_FIFO | AES_FLUSH_WRITE_FIFO; @@ -304,7 +230,7 @@ } // AES_init() must be called before this works -static void processBlocksDma(const u32 *in, u32 *out, u32 blocks) +static void aesProcessBlocksDma(const u32 *in, u32 *out, u32 blocks) { // DMA can't reach TCMs assert(((u32)in >= ITCM_BOOT9_MIRROR + ITCM_SIZE) && (((u32)in < DTCM_BASE) || ((u32)in >= DTCM_BASE + DTCM_SIZE))); @@ -371,8 +297,8 @@ REG_AESCNT = aesParams; u32 blockNum = ((blocks > AES_MAX_BLOCKS) ? AES_MAX_BLOCKS : blocks); - if(dma) processBlocksDma(in, out, blockNum); - else processBlocksCpu(in, out, blockNum); + if(dma) aesProcessBlocksDma(in, out, blockNum); + else aesProcessBlocksCpu(in, out, blockNum); AES_addCounter(ctr, blockNum<<4); in += blockNum<<2; @@ -396,7 +322,7 @@ void SHA_start(u8 params) { - REG_SHA_CNT = SHA_ENABLE | (u32)params; + REG_SHA_CNT = (u32)params | SHA_ENABLE; } void SHA_update(const u32 *data, u32 size) diff --git a/source/arm9/dev.c b/source/arm9/dev.c index 8233a79..073c096 100644 --- a/source/arm9/dev.c +++ b/source/arm9/dev.c @@ -234,7 +234,7 @@ pad[2] ^= ((u32*)BOOT9_BASE)[2]; pad[3] ^= ((u32*)BOOT9_BASE)[3]; - AES_setKeyY(0x05, AES_INPUT_BIG | AES_INPUT_NORMAL, false, pad); + AES_setKey(0x05, AES_KEY_Y, AES_INPUT_BIG | AES_INPUT_NORMAL, false, pad); } @@ -319,13 +319,13 @@ twlKeyX[1] = 0x544E494E; // "NINT" twlKeyX[2] = 0x4F444E45; // "ENDO" twlKeyX[3] = *((u32*)0x01FFB80C) ^ 0x08C267B7; - AES_setKeyX(0x03, AES_INPUT_LITTLE | AES_INPUT_REVERSED, false, twlKeyX); + AES_setKey(0x03, AES_KEY_X, AES_INPUT_LITTLE | AES_INPUT_REVERSED, false, twlKeyX); // TWL keyslot 0x03 keyY u32 twlKeyY[4]; for(int i = 0; i < 3; i++) twlKeyY[i] = ((u32*)0x01FFD3C8)[i]; twlKeyY[3] = 0xE1A00005; - AES_setKeyY(0x03, AES_INPUT_LITTLE | AES_INPUT_REVERSED, false, twlKeyY); + AES_setKey(0x03, AES_KEY_Y, AES_INPUT_LITTLE | AES_INPUT_REVERSED, false, twlKeyY); // Crypt settings AES_setCryptParams(&dev_dnand.twlAesCtx, AES_INPUT_LITTLE | AES_INPUT_REVERSED,