diff --git a/makerom/aes_keygen.c b/makerom/aes_keygen.c index e5a35d6..ffaa4d9 100644 --- a/makerom/aes_keygen.c +++ b/makerom/aes_keygen.c @@ -106,6 +106,7 @@ void n128_xor(const uint8_t *a, const uint8_t *b, uint8_t *out) } // keygen algorithm +/* void n_aes_keygen(const uint8_t *x, uint8_t x_shift, const uint8_t *y, uint8_t y_shift, const uint8_t *keygen_constant, uint8_t *key) { // overall algo: @@ -121,4 +122,25 @@ void n_aes_keygen(const uint8_t *x, uint8_t x_shift, const uint8_t *y, uint8_t y // Add secret n128_add(key_xy, keygen_constant, key); +} +*/ + +void ctr_aes_keygen(const uint8_t *x, const uint8_t *y, uint8_t *key) +{ + static const uint8_t KEYGEN_CONST[16] = { 0x1F, 0xF9, 0xE9, 0xAA, 0xC5, 0xFE, 0x04, 0x08, 0x02, 0x45, 0x91, 0xDC, 0x5D, 0x52, 0x76, 0x8A }; + + // overall algo: + // key = (((x <<< 2) ^ y) + KEYGEN_CONST) >>> 41 + uint8_t x_rot[16], key_xy[16], key_xyc[16]; + + // x_rot = x <<< 2 + n128_lrot(x, 2, x_rot); + + // key_xy = x_rot ^ y + n128_xor(x_rot, y, key_xy); + + // key_xyc = key_xy + KEYGEN_CONST + n128_add(key_xy, KEYGEN_CONST, key); + + n128_rrot(key_xyc, 41, key); } \ No newline at end of file diff --git a/makerom/aes_keygen.h b/makerom/aes_keygen.h index 0314744..dae87a3 100644 --- a/makerom/aes_keygen.h +++ b/makerom/aes_keygen.h @@ -2,11 +2,7 @@ #include /* - AES Key generator for the Nintendo (Handheld) Consoles - - BYO keygen constants, and input >>> parameters - - key = ((x >>> x_shift) ^ (y >>> y_shift)) + keygen_constant + AES Key generator for the Nintendo 3DS (CTR) Consoles */ -void n_aes_keygen(const uint8_t *x, uint8_t x_shift, const uint8_t *y, uint8_t y_shift, const uint8_t *keygen_constant, uint8_t *key); +void ctr_aes_keygen(const uint8_t *x, const uint8_t *y, uint8_t *key); diff --git a/makerom/keyset.c b/makerom/keyset.c index d468471..3710d92 100644 --- a/makerom/keyset.c +++ b/makerom/keyset.c @@ -48,15 +48,6 @@ void PrintBadKeySize(char *path, u32 size) fprintf(stderr,"[KEYSET ERROR] %s has invalid size (0x%x)\n",path,size); } -u8* AesKeyScrambler(u8 *key, const u8 *keyX, const u8 *keyY) -{ - static const uint8_t CTR_KEYGEN_CONST[16] = { 0xEE, 0x2E, 0xA9, 0x3B, 0x45, 0x0F, 0xFC, 0xF4, 0xD5, 0x62, 0xFF, 0x02, 0x04, 0x01, 0x22, 0xC8 }; - static const uint8_t CTR_KEYX_SHIFT = 39; - static const uint8_t CTR_KEYY_SHIFT = 41; - n_aes_keygen(keyX, CTR_KEYX_SHIFT, keyY, CTR_KEYY_SHIFT, CTR_KEYGEN_CONST, key); - return key; -} - int SetKeys(keys_struct *keys) { int result = 0; @@ -135,7 +126,7 @@ int LoadKeysFromResources(keys_struct *keys) // CIA //for(int i = 0; i < 6; i++){ // keys->aes.commonKey[i] = malloc(16); - // AesKeyScrambler(keys->aes.commonKey[i], ctr_common_etd_keyX_ppki, ctr_common_etd_keyY_ppki[i]); + // ctr_aes_keygen(ctr_common_etd_keyX_ppki, ctr_common_etd_keyY_ppki[i], keys->aes.commonKey[i]); //} if(keys->aes.currentCommonKey > 0xff) SetCurrentCommonKey(keys,0); diff --git a/makerom/keyset.h b/makerom/keyset.h index 1e1db78..4ab18ff 100644 --- a/makerom/keyset.h +++ b/makerom/keyset.h @@ -96,6 +96,4 @@ void FreeKeys(keys_struct *keys); int SetCommonKey(keys_struct *keys, const u8 *key, u8 Index); int SetCurrentCommonKey(keys_struct *keys, u8 Index); int SetNormalKey(keys_struct *keys, const u8 *key); -int SetSystemFixedKey(keys_struct *keys, const u8 *key); - -u8* AesKeyScrambler(u8 *key, const u8 *keyX, const u8 *keyY); +int SetSystemFixedKey(keys_struct *keys, const u8 *key); \ No newline at end of file diff --git a/makerom/ncch.c b/makerom/ncch.c index 577843b..e8d6412 100644 --- a/makerom/ncch.c +++ b/makerom/ncch.c @@ -1,4 +1,5 @@ #include "lib.h" +#include "aes_keygen.h" #include "ncch_build.h" #include "exheader_build.h" #include "exheader_read.h" @@ -1043,12 +1044,12 @@ bool SetNcchKeys(keys_struct *keys, ncch_hdr *hdr) } if(keys->aes.ncchKeyX[0]) - AesKeyScrambler(keys->aes.ncchKey0,keys->aes.ncchKeyX[0],hdr->signature); + ctr_aes_keygen(keys->aes.ncchKeyX[0],hdr->signature,keys->aes.ncchKey0); else return false; if(keys->aes.ncchKeyX[hdr->flags[ncchflag_CONTENT_KEYX]]) - AesKeyScrambler(keys->aes.ncchKey1,keys->aes.ncchKeyX[hdr->flags[ncchflag_CONTENT_KEYX]],hdr->signature); + ctr_aes_keygen(keys->aes.ncchKeyX[ncchflag_CONTENT_KEYX], hdr->signature, keys->aes.ncchKey0); else return false;