Even more clean-up
This commit is contained in:
parent
899ad1887a
commit
bf81ec252e
128
source/crypto.c
128
source/crypto.c
@ -5,7 +5,6 @@
|
||||
#include <stddef.h>
|
||||
#include "memory.h"
|
||||
#include "fatfs/sdmmc/sdmmc.h"
|
||||
#include "fatfs/ff.h"
|
||||
|
||||
//Nand key#2 (0x12C10)
|
||||
u8 key2[0x10] = {
|
||||
@ -231,133 +230,6 @@ void aes(void* dst, const void* src, u32 blockCount, void* iv, u32 mode, u32 ivM
|
||||
}
|
||||
}
|
||||
|
||||
void sha_wait_idle()
|
||||
{
|
||||
while(*REG_SHA_CNT & 1);
|
||||
}
|
||||
|
||||
void sha(void* res, const void* src, u32 size, u32 mode)
|
||||
{
|
||||
sha_wait_idle();
|
||||
*REG_SHA_CNT = mode | SHA_CNT_OUTPUT_ENDIAN | SHA_NORMAL_ROUND;
|
||||
|
||||
const u32* src32 = (const u32*)src;
|
||||
int i;
|
||||
while(size >= 0x40)
|
||||
{
|
||||
sha_wait_idle();
|
||||
for(i = 0; i < 4; ++i)
|
||||
{
|
||||
*REG_SHA_INFIFO = *src32++;
|
||||
*REG_SHA_INFIFO = *src32++;
|
||||
*REG_SHA_INFIFO = *src32++;
|
||||
*REG_SHA_INFIFO = *src32++;
|
||||
}
|
||||
|
||||
size -= 0x40;
|
||||
}
|
||||
|
||||
sha_wait_idle();
|
||||
memcpy((void*)REG_SHA_INFIFO, src32, size);
|
||||
|
||||
*REG_SHA_CNT = (*REG_SHA_CNT & ~SHA_NORMAL_ROUND) | SHA_FINAL_ROUND;
|
||||
|
||||
while(*REG_SHA_CNT & SHA_FINAL_ROUND);
|
||||
sha_wait_idle();
|
||||
|
||||
u32 hashSize = SHA_256_HASH_SIZE;
|
||||
if(mode == SHA_224_MODE)
|
||||
hashSize = SHA_224_HASH_SIZE;
|
||||
else if(mode == SHA_1_MODE)
|
||||
hashSize = SHA_1_HASH_SIZE;
|
||||
|
||||
memcpy(res, (void*)REG_SHA_HASH, hashSize);
|
||||
}
|
||||
|
||||
void rsa_wait_idle()
|
||||
{
|
||||
while(*REG_RSA_CNT & 1);
|
||||
}
|
||||
|
||||
void rsa_use_keyslot(u32 keyslot)
|
||||
{
|
||||
*REG_RSA_CNT = (*REG_RSA_CNT & ~RSA_CNT_KEYSLOTS) | (keyslot << 4);
|
||||
}
|
||||
|
||||
void rsa_setkey(u32 keyslot, const void* mod, const void* exp, u32 mode)
|
||||
{
|
||||
rsa_wait_idle();
|
||||
*REG_RSA_CNT = (*REG_RSA_CNT & ~RSA_CNT_KEYSLOTS) | (keyslot << 4) | RSA_IO_BE | RSA_IO_NORMAL;
|
||||
|
||||
u32 size = mode * 4;
|
||||
|
||||
volatile u32* keyslotCnt = REG_RSA_SLOT0 + (keyslot << 4);
|
||||
keyslotCnt[0] &= ~(RSA_SLOTCNT_KEY_SET | RSA_SLOTCNT_WPROTECT);
|
||||
keyslotCnt[1] = mode;
|
||||
|
||||
memcpy((void*)REG_RSA_MOD_END - size, mod, size);
|
||||
|
||||
if(exp == NULL)
|
||||
{
|
||||
size -= 4;
|
||||
while(size)
|
||||
{
|
||||
*REG_RSA_EXPFIFO = 0;
|
||||
size -= 4;
|
||||
}
|
||||
*REG_RSA_EXPFIFO = 0x01000100; // 0x00010001 byteswapped
|
||||
}
|
||||
else
|
||||
{
|
||||
const u32* exp32 = (const u32*)exp;
|
||||
while(size)
|
||||
{
|
||||
*REG_RSA_EXPFIFO = *exp32++;
|
||||
size -= 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int rsa_iskeyset(u32 keyslot)
|
||||
{
|
||||
return *(REG_RSA_SLOT0 + (keyslot << 4)) & 1;
|
||||
}
|
||||
|
||||
void rsa(void* dst, const void* src, u32 size)
|
||||
{
|
||||
u32 keyslot = (*REG_RSA_CNT & RSA_CNT_KEYSLOTS) >> 4;
|
||||
if(rsa_iskeyset(keyslot) == 0)
|
||||
return;
|
||||
|
||||
rsa_wait_idle();
|
||||
*REG_RSA_CNT |= RSA_IO_BE | RSA_IO_NORMAL;
|
||||
|
||||
// Pad the message with zeroes so that it's a multiple of 8
|
||||
// and write the message with the end aligned with the register
|
||||
u32 padSize = ((size + 7) & ~7) - size;
|
||||
memset((void*)REG_RSA_TXT_END - (size + padSize), 0, padSize);
|
||||
memcpy((void*)REG_RSA_TXT_END - size, src, size);
|
||||
|
||||
// Start
|
||||
*REG_RSA_CNT |= RSA_CNT_START;
|
||||
|
||||
rsa_wait_idle();
|
||||
memcpy(dst, (void*)REG_RSA_TXT_END - size, size);
|
||||
}
|
||||
|
||||
int rsa_verify(const void* data, u32 size, const void* sig, u32 mode)
|
||||
{
|
||||
u8 dataHash[SHA_256_HASH_SIZE];
|
||||
sha(dataHash, data, size, SHA_256_MODE);
|
||||
|
||||
u8 decSig[0x100]; // Way too big, need to request a work area
|
||||
|
||||
u32 sigSize = mode * 4;
|
||||
rsa(decSig, sig, sigSize);
|
||||
|
||||
return memcmp(dataHash, decSig + (sigSize - SHA_256_HASH_SIZE), SHA_256_HASH_SIZE) == 0;
|
||||
}
|
||||
|
||||
/****************************************************************
|
||||
* Nand/FIRM Crypto stuff
|
||||
****************************************************************/
|
||||
|
@ -6,16 +6,6 @@
|
||||
#include <stdint.h>
|
||||
#include "types.h"
|
||||
|
||||
#define FIRM_TYPE_ARM9 0
|
||||
#define FIRM_TYPE_ARM11 1
|
||||
|
||||
#define MEDIA_UNITS 0x200
|
||||
|
||||
#define NCCH_MAGIC (0x4843434E)
|
||||
#define NCSD_MAGIC (0x4453434E)
|
||||
#define FIRM_MAGIC (0x4D524946)
|
||||
#define ARM9BIN_MAGIC (0x47704770)
|
||||
|
||||
/**************************AES****************************/
|
||||
#define REG_AESCNT ((volatile u32*)0x10009000)
|
||||
#define REG_AESBLKCNT ((volatile u32*)0x10009004)
|
||||
@ -52,8 +42,6 @@
|
||||
#define AES_INPUT_NORMAL (AES_CNT_INPUT_ORDER)
|
||||
#define AES_INPUT_REVERSED 0
|
||||
|
||||
#define AES_TEMP_KEYSLOT 0x11
|
||||
|
||||
#define AES_BLOCK_SIZE 0x10
|
||||
|
||||
#define AES_KEYCNT_WRITE (1 << 0x7)
|
||||
@ -61,75 +49,6 @@
|
||||
#define AES_KEYX 1
|
||||
#define AES_KEYY 2
|
||||
|
||||
/**************************SHA****************************/
|
||||
#define REG_SHA_CNT ((volatile u32*)0x1000A000)
|
||||
#define REG_SHA_BLKCNT ((volatile u32*)0x1000A004)
|
||||
#define REG_SHA_HASH ((volatile u32*)0x1000A040)
|
||||
#define REG_SHA_INFIFO ((volatile u32*)0x1000A080)
|
||||
|
||||
#define SHA_CNT_STATE 0x00000003
|
||||
#define SHA_CNT_UNK2 0x00000004
|
||||
#define SHA_CNT_OUTPUT_ENDIAN 0x00000008
|
||||
#define SHA_CNT_MODE 0x00000030
|
||||
#define SHA_CNT_ENABLE 0x00010000
|
||||
#define SHA_CNT_ACTIVE 0x00020000
|
||||
|
||||
#define SHA_HASH_READY 0x00000000
|
||||
#define SHA_NORMAL_ROUND 0x00000001
|
||||
#define SHA_FINAL_ROUND 0x00000002
|
||||
|
||||
#define SHA_OUTPUT_BE SHA_CNT_OUTPUT_ENDIAN
|
||||
#define SHA_OUTPUT_LE 0
|
||||
|
||||
#define SHA_256_MODE 0
|
||||
#define SHA_224_MODE 0x00000010
|
||||
#define SHA_1_MODE 0x00000020
|
||||
|
||||
#define SHA_256_HASH_SIZE (256 / 8)
|
||||
#define SHA_224_HASH_SIZE (224 / 8)
|
||||
#define SHA_1_HASH_SIZE (160 / 8)
|
||||
|
||||
/**************************RSA****************************/
|
||||
#define REG_RSA_CNT ((volatile u32*)0x1000B000)
|
||||
#define REG_RSA_SLOT0 ((volatile u32*)0x1000B100)
|
||||
#define REG_RSA_SLOT1 ((volatile u32*)0x1000B110)
|
||||
#define REG_RSA_SLOT2 ((volatile u32*)0x1000B120)
|
||||
#define REG_RSA_SLOT3 ((volatile u32*)0x1000B130)
|
||||
#define REG_RSA_EXPFIFO ((volatile u32*)0x1000B200)
|
||||
#define REG_RSA_MOD_END ((volatile u32*)0x1000B500)
|
||||
#define REG_RSA_TXT_END ((volatile u32*)0x1000B900)
|
||||
|
||||
#define RSA_CNT_START 0x00000001
|
||||
#define RSA_CNT_KEYSLOTS 0x000000F0
|
||||
#define RSA_CNT_IO_ENDIAN 0x00000100
|
||||
#define RSA_CNT_IO_ORDER 0x00000200
|
||||
|
||||
#define RSA_SLOTCNT_KEY_SET 0x00000001
|
||||
#define RSA_SLOTCNT_WPROTECT 0x00000002 // Write protect
|
||||
|
||||
#define RSA_IO_BE RSA_CNT_IO_ENDIAN
|
||||
#define RSA_IO_LE 0
|
||||
#define RSA_IO_NORMAL RSA_CNT_IO_ORDER
|
||||
#define RSA_IO_REVERSED 0
|
||||
|
||||
#define RSA_TEMP_KEYSLOT 0
|
||||
|
||||
#define RSA_1024_MODE 0x20
|
||||
#define RSA_2048_MODE 0x40
|
||||
|
||||
//Crypto Libs
|
||||
void aes_setkey(u8 keyslot, const void* key, u32 keyType, u32 mode);
|
||||
void aes_use_keyslot(u8 keyslot);
|
||||
void aes(void* dst, const void* src, u32 blockCount, void* iv, u32 mode, u32 ivMode);
|
||||
void aes_setiv(const void* iv, u32 mode);
|
||||
void aes_advctr(void* ctr, u32 val, u32 mode);
|
||||
void aes_change_ctrmode(void* ctr, u32 fromMode, u32 toMode);
|
||||
void aes_batch(void* dst, const void* src, u32 blockCount);
|
||||
void sha(void* res, const void* src, u32 size, u32 mode);
|
||||
void rsa_setkey(u32 keyslot, const void* mod, const void* exp, u32 mode);
|
||||
void rsa_use_keyslot(u32 keyslot);
|
||||
int rsa_verify(const void* data, u32 size, const void* sig, u32 mode);
|
||||
|
||||
//NAND/FIRM stuff
|
||||
void nandFirm0(u8 *outbuf, const u32 size, u8 console);
|
||||
void decArm9Bin(void *armHdr, u8 mode);
|
||||
|
@ -5,10 +5,6 @@
|
||||
*/
|
||||
#include "memory.h"
|
||||
|
||||
void memcpy32(u32 *dest, u32 *src, u32 size){
|
||||
for (u32 i = 0; i < size; i++) dest[i] = src[i];
|
||||
}
|
||||
|
||||
void memcpy(void *dest, const void *src, u32 size){
|
||||
char *destc = (char *)dest;
|
||||
const char *srcc = (const char *)src;
|
||||
|
@ -9,7 +9,6 @@
|
||||
#include "types.h"
|
||||
|
||||
void memcpy(void *dest, const void *src, u32 size);
|
||||
void memcpy32(u32 *dest, u32 *src, u32 size);
|
||||
void memset(void *dest, int filler, u32 size);
|
||||
int memcmp(const void *buf1, const void *buf2, u32 size);
|
||||
void *memsearch(void *start_pos, void *search, u32 size, u32 size_search);
|
||||
|
Reference in New Issue
Block a user