1
0
Fork 0
mirror of https://github.com/s1204IT/Lockpick_RCM.git synced 2024-11-22 17:56:39 +00:00

Support firmware 11.0.0, continue refactor

This commit is contained in:
shchmue 2020-12-03 18:16:55 -07:00
parent c4172d77e1
commit bd134cf670
13 changed files with 567 additions and 493 deletions

View file

@ -60,6 +60,7 @@
// SDMMC DMA buffers 2 // SDMMC DMA buffers 2
#define SDXC_BUF_ALIGNED 0xEF000000 #define SDXC_BUF_ALIGNED 0xEF000000
#define MIXD_BUF_ALIGNED 0xF0000000 #define MIXD_BUF_ALIGNED 0xF0000000
#define TITLEKEY_BUF_ADR MIXD_BUF_ALIGNED
#define EMMC_BUF_ALIGNED MIXD_BUF_ALIGNED #define EMMC_BUF_ALIGNED MIXD_BUF_ALIGNED
#define SDMMC_DMA_BUF_SZ 0x1000000 // 16MB (4MB currently used). #define SDMMC_DMA_BUF_SZ 0x1000000 // 16MB (4MB currently used).

View file

@ -344,6 +344,11 @@ int se_aes_crypt_ctr(u32 ks, void *dst, u32 dst_size, const void *src, u32 src_s
// random calls were derived from Atmosphère's // random calls were derived from Atmosphère's
int se_initialize_rng() int se_initialize_rng()
{ {
static bool initialized = false;
if (initialized)
return 1;
u8 *output_buf = (u8 *)malloc(0x10); u8 *output_buf = (u8 *)malloc(0x10);
SE(SE_CONFIG_REG_OFFSET) = SE_CONFIG_ENC_ALG(ALG_RNG) | SE_CONFIG_DST(DST_MEMORY); SE(SE_CONFIG_REG_OFFSET) = SE_CONFIG_ENC_ALG(ALG_RNG) | SE_CONFIG_DST(DST_MEMORY);
@ -357,6 +362,8 @@ int se_initialize_rng()
int res =_se_execute(OP_START, output_buf, 0x10, NULL, 0); int res =_se_execute(OP_START, output_buf, 0x10, NULL, 0);
free(output_buf); free(output_buf);
if (res)
initialized = true;
return res; return res;
} }
@ -600,3 +607,74 @@ out:;
free(opad); free(opad);
return res; return res;
} }
// _mgf1_xor() and rsa_oaep_decode were derived from Atmosphère
static void _mgf1_xor(void *masked, u32 masked_size, const void *seed, u32 seed_size)
{
u8 cur_hash[0x20];
u8 hash_buf[0xe4];
u32 hash_buf_size = seed_size + 4;
memcpy(hash_buf, seed, seed_size);
u32 round_num = 0;
u8 *p_out = (u8 *)masked;
while (masked_size) {
u32 cur_size = MIN(masked_size, 0x20);
for (u32 i = 0; i < 4; i++)
hash_buf[seed_size + 3 - i] = (round_num >> (8 * i)) & 0xff;
round_num++;
se_calc_sha256(cur_hash, hash_buf, hash_buf_size);
for (unsigned int i = 0; i < cur_size; i++) {
*p_out ^= cur_hash[i];
p_out++;
}
masked_size -= cur_size;
}
}
u32 se_rsa_oaep_decode(void *dst, u32 dst_size, const void *label_digest, u32 label_digest_size, u8 *buf, u32 buf_size)
{
if (dst_size <= 0 || buf_size < 0x43 || label_digest_size != 0x20)
return 0;
bool is_valid = buf[0] == 0;
u32 db_len = buf_size - 0x21;
u8 *seed = buf + 1;
u8 *db = seed + 0x20;
_mgf1_xor(seed, 0x20, db, db_len);
_mgf1_xor(db, db_len, seed, 0x20);
is_valid &= memcmp(label_digest, db, 0x20) ? 0 : 1;
db += 0x20;
db_len -= 0x20;
int msg_ofs = 0;
int looking_for_one = 1;
int invalid_db_padding = 0;
int is_zero;
int is_one;
for (int i = 0; i < db_len; )
{
is_zero = (db[i] == 0);
is_one = (db[i] == 1);
msg_ofs += (looking_for_one & is_one) * (++i);
looking_for_one &= ~is_one;
invalid_db_padding |= (looking_for_one & ~is_zero);
}
is_valid &= (invalid_db_padding == 0);
const u32 msg_size = MIN(dst_size, is_valid * (db_len - msg_ofs));
memcpy(dst, db + msg_ofs, msg_size);
return msg_size;
}

View file

@ -42,5 +42,6 @@ int se_aes_xts_crypt(u32 tweak_ks, u32 crypt_ks, u32 enc, u64 sec, void *dst, co
int se_aes_cmac(u32 ks, void *dst, u32 dst_size, const void *src, u32 src_size); int se_aes_cmac(u32 ks, void *dst, u32 dst_size, const void *src, u32 src_size);
int se_calc_sha256(void *dst, const void *src, u32 src_size); int se_calc_sha256(void *dst, const void *src, u32 src_size);
int se_calc_hmac_sha256(void *dst, const void *src, u32 src_size, const void *key, u32 key_size); int se_calc_hmac_sha256(void *dst, const void *src, u32 src_size, const void *key, u32 key_size);
u32 se_rsa_oaep_decode(void *dst, u32 dst_size, const void *label_digest, u32 label_digest_size, u8 *buf, u32 buf_size);
#endif #endif

View file

@ -19,10 +19,15 @@
#define NULL ((void *)0) #define NULL ((void *)0)
#define ALWAYS_INLINE inline __attribute__((always_inline))
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define ALIGN_DOWN(x, a) (((x) - ((a) - 1)) & ~((a) - 1)) #define ALIGN_DOWN(x, a) (((x) - ((a) - 1)) & ~((a) - 1))
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#define DIV_ROUND_UP(a, b) ((a + b - 1) / b)
#define LOG2(n) (32 - __builtin_clz(n) - 1)
#define OFFSET_OF(t, m) ((u32)&((t *)NULL)->m) #define OFFSET_OF(t, m) ((u32)&((t *)NULL)->m)
#define CONTAINER_OF(mp, t, mn) ((t *)((u32)mp - OFFSET_OF(t, mn))) #define CONTAINER_OF(mp, t, mn) ((t *)((u32)mp - OFFSET_OF(t, mn)))
@ -91,4 +96,20 @@ typedef struct __attribute__((__packed__)) _reloc_meta_t
u32 ep; u32 ep;
} reloc_meta_t; } reloc_meta_t;
typedef enum
{
VALIDITY_UNCHECKED = 0,
VALIDITY_INVALID,
VALIDITY_VALID
} validity_t;
typedef enum
{
OPEN_MODE_READ = 1,
OPEN_MODE_WRITE = 2,
OPEN_MODE_ALLOW_APPEND = 4,
OPEN_MODE_READ_WRITE = OPEN_MODE_READ | OPEN_MODE_WRITE,
OPEN_MODE_ALL = OPEN_MODE_READ | OPEN_MODE_WRITE | OPEN_MODE_ALLOW_APPEND
} open_mode_t;
#endif #endif

View file

@ -457,6 +457,50 @@ void gfx_hexdump(u32 base, const u8 *buf, u32 len)
gfx_con.fntsz = prevFontSize; gfx_con.fntsz = prevFontSize;
} }
void gfx_hexdiff(u32 base, const u8 *buf1, const u8 *buf2, u32 len)
{
if (gfx_con.mute)
return;
if (memcmp(buf1, buf2, len) == 0)
{
gfx_printf("Diff: No differences found.\n");
return;
}
u8 prevFontSize = gfx_con.fntsz;
gfx_con.fntsz = 8;
for(u32 i = 0; i < len; i+=0x10)
{
u32 bytes_left = len - i < 0x10 ? len - i : 0x10;
if (memcmp(buf1 + i, buf2 + i, bytes_left) == 0)
continue;
gfx_printf("Diff 1: %08x: ", base + i);
for (u32 j = 0; j < bytes_left; j++)
{
if (buf1[i+j] != buf2[i+j])
gfx_con.fgcol = COLOR_ORANGE;
gfx_printf("%02x ", buf1[i+j]);
gfx_con.fgcol = 0xFFCCCCCC;
}
gfx_puts("| ");
gfx_putc('\n');
gfx_printf("Diff 2: %08x: ", base + i);
for (u32 j = 0; j < bytes_left; j++)
{
if (buf1[i+j] != buf2[i+j])
gfx_con.fgcol = COLOR_ORANGE;
gfx_printf("%02x ", buf2[i+j]);
gfx_con.fgcol = 0xFFCCCCCC;
}
gfx_puts("| ");
gfx_putc('\n');
gfx_putc('\n');
}
gfx_putc('\n');
gfx_con.fntsz = prevFontSize;
}
static int abs(int x) static int abs(int x)
{ {
if (x < 0) if (x < 0)

View file

@ -64,6 +64,7 @@ void gfx_putc(char c);
void gfx_puts(const char *s); void gfx_puts(const char *s);
void gfx_printf(const char *fmt, ...); void gfx_printf(const char *fmt, ...);
void gfx_hexdump(u32 base, const u8 *buf, u32 len); void gfx_hexdump(u32 base, const u8 *buf, u32 len);
void gfx_hexdiff(u32 base, const u8 *buf1, const u8 *buf2, u32 len);
void gfx_set_pixel(u32 x, u32 y, u32 color); void gfx_set_pixel(u32 x, u32 y, u32 color);
void gfx_line(int x0, int y0, int x1, int y1, u32 color); void gfx_line(int x0, int y0, int x1, int y1, u32 color);

View file

@ -22,78 +22,23 @@
#include "pkg1.h" #include "pkg1.h"
#include <sec/se.h> #include <sec/se.h>
#define HASH_ORDER_100_100 { \
FS_KEY_AREA_KEY_APPLI_SOURCE, \
FS_KEY_AREA_KEY_OCEAN_SOURCE, \
FS_KEY_AREA_KEY_SYSTE_SOURCE, \
FS_HEADER_KEK_SOURCE, \
FS_SAVE_MAC_KEK_SOURCE, \
FS_SAVE_MAC_KEY_SOURCE, \
FS_HEADER_KEY_SOURCE \
}
#define HASH_ORDER_200_510 { \
FS_KEY_AREA_KEY_APPLI_SOURCE, \
FS_KEY_AREA_KEY_OCEAN_SOURCE, \
FS_KEY_AREA_KEY_SYSTE_SOURCE, \
FS_HEADER_KEK_SOURCE, \
FS_SAVE_MAC_KEK_SOURCE, \
FS_SAVE_MAC_SD_KEK_SOURCE, \
FS_SD_KEK_SOURCE, \
FS_SD_SAVE_KEY_SOURCE, \
FS_SD_NCA_KEY_SOURCE, \
FS_SAVE_MAC_KEY_SOURCE, \
FS_SAVE_MAC_SD_KEY_SOURCE, \
FS_HEADER_KEY_SOURCE \
}
#define HASH_ORDER_600_620 { \
FS_SAVE_MAC_KEY_SOURCE, \
FS_SAVE_MAC_KEK_SOURCE, \
FS_SD_KEK_SOURCE, \
FS_SAVE_MAC_SD_KEK_SOURCE, \
FS_SAVE_MAC_SD_KEY_SOURCE, \
FS_KEY_AREA_KEY_APPLI_SOURCE, \
FS_KEY_AREA_KEY_OCEAN_SOURCE, \
FS_KEY_AREA_KEY_SYSTE_SOURCE, \
FS_HEADER_KEK_SOURCE, \
FS_SD_SAVE_KEY_SOURCE, \
FS_SD_NCA_KEY_SOURCE, \
FS_HEADER_KEY_SOURCE \
}
#define HASH_ORDER_700_10x { \
FS_SAVE_MAC_KEY_SOURCE, \
FS_SAVE_MAC_KEK_SOURCE, \
FS_SD_KEK_SOURCE, \
FS_SAVE_MAC_SD_KEK_SOURCE, \
FS_SAVE_MAC_SD_KEY_SOURCE, \
FS_KEY_AREA_KEY_APPLI_SOURCE, \
FS_KEY_AREA_KEY_OCEAN_SOURCE, \
FS_KEY_AREA_KEY_SYSTE_SOURCE, \
FS_HEADER_KEK_SOURCE, \
FS_SD_SAVE_KEY_SOURCE, \
FS_SD_NCA_KEY_SOURCE, \
FS_SD_CUSTOM_KEY_SOURCE, \
FS_HEADER_KEY_SOURCE \
}
static const pkg1_id_t _pkg1_ids[] = { static const pkg1_id_t _pkg1_ids[] = {
{ "20161121183008", 0, {0x1b517, 0x125bc2, 1, 16, 6, HASH_ORDER_100_100, 0, 0x449dc} }, //1.0.0 { "20161121183008", 0 }, //1.0.0
{ "20170210155124", 0, {0x1d226, 0x26fe, 0, 16, 11, HASH_ORDER_200_510, 0x557b, 0x3d41a} }, //2.0.0 - 2.3.0 { "20170210155124", 0 }, //2.0.0 - 2.3.0
{ "20170519101410", 1, {0x1ffa6, 0x298b, 0, 16, 11, HASH_ORDER_200_510, 0x552d, 0x3cb81} }, //3.0.0 { "20170519101410", 1 }, //3.0.0
{ "20170710161758", 2, {0x20026, 0x29ab, 0, 16, 11, HASH_ORDER_200_510, 0x552d, 0x3cb81} }, //3.0.1 - 3.0.2 { "20170710161758", 2 }, //3.0.1 - 3.0.2
{ "20170921172629", 3, {0x1c64c, 0x37eb, 0, 16, 11, HASH_ORDER_200_510, 0x5382, 0x3711c} }, //4.0.0 - 4.1.0 { "20170921172629", 3 }, //4.0.0 - 4.1.0
{ "20180220163747", 4, {0x1f3b4, 0x465b, 0, 16, 11, HASH_ORDER_200_510, 0x5a63, 0x37901} }, //5.0.0 - 5.1.0 { "20180220163747", 4 }, //5.0.0 - 5.1.0
{ "20180802162753", 5, {0x27350, 0x17ff5, 1, 8, 11, HASH_ORDER_600_620, 0x5674, 0x1d5be} }, //6.0.0 - 6.1.0 { "20180802162753", 5 }, //6.0.0 - 6.1.0
{ "20181107105733", 6, {0x27350, 0x17ff5, 1, 8, 11, HASH_ORDER_600_620, 0x5674, 0x1d5be} }, //6.2.0 { "20181107105733", 6 }, //6.2.0
{ "20181218175730", 7, {0x29c50, 0x6a73, 0, 8, 12, HASH_ORDER_700_10x, 0x5563, 0x1d437} }, //7.0.0 { "20181218175730", 7 }, //7.0.0
{ "20190208150037", 7, {0x29c50, 0x6a73, 0, 8, 12, HASH_ORDER_700_10x, 0x5563, 0x1d437} }, //7.0.1 { "20190208150037", 7 }, //7.0.1
{ "20190314172056", 7, {0x29c50, 0x6a73, 0, 8, 12, HASH_ORDER_700_10x, 0x5563, 0x1d437} }, //8.0.0 - 8.0.1 { "20190314172056", 7 }, //8.0.0 - 8.0.1
{ "20190531152432", 8, {0x29c50, 0x6a73, 0, 8, 12, HASH_ORDER_700_10x, 0x5563, 0x1d437} }, //8.1.0 { "20190531152432", 8 }, //8.1.0
{ "20190809135709", 9, {0x2ec10, 0x5573, 0, 1, 12, HASH_ORDER_700_10x, 0x6495, 0x1d807} }, //9.0.0 - 9.0.1 { "20190809135709", 9 }, //9.0.0 - 9.0.1
{ "20191021113848", 10,{0x2ec10, 0x5573, 0, 1, 12, HASH_ORDER_700_10x, 0x6495, 0x1d807} }, //9.1.0 { "20191021113848", 10}, //9.1.0
{ "20200303104606", 10,{0x30ea0, 0x5e4b, 0, 1, 12, HASH_ORDER_700_10x, 0x663c, 0x1d9a4} }, //10.0.0+ { "20200303104606", 10}, //10.0.0 - 10.2.0
{ "20201030110855", 10}, //11.0.0
{ NULL } //End. { NULL } //End.
}; };

View file

@ -19,41 +19,14 @@
#include <utils/types.h> #include <utils/types.h>
#define FS_HEADER_KEK_SOURCE 0
#define FS_HEADER_KEY_SOURCE 1
#define FS_KEY_AREA_KEY_APPLI_SOURCE 2
#define FS_KEY_AREA_KEY_OCEAN_SOURCE 3
#define FS_KEY_AREA_KEY_SYSTE_SOURCE 4
#define FS_SAVE_MAC_KEK_SOURCE 5
#define FS_SAVE_MAC_KEY_SOURCE 6
#define FS_SAVE_MAC_SD_KEK_SOURCE 7
#define FS_SAVE_MAC_SD_KEY_SOURCE 8
#define FS_SD_CUSTOM_KEY_SOURCE 9
#define FS_SD_KEK_SOURCE 10
#define FS_SD_NCA_KEY_SOURCE 11
#define FS_SD_SAVE_KEY_SOURCE 12
#define PKG1_MAX_SIZE 0x40000 #define PKG1_MAX_SIZE 0x40000
#define PKG1_OFFSET 0x100000 #define PKG1_OFFSET 0x100000
#define KEYBLOB_OFFSET 0x180000 #define KEYBLOB_OFFSET 0x180000
typedef struct _key_info_t
{
u32 start_offset;
u32 hks_offset;
bool hks_offset_is_from_end;
u32 alignment;
u32 hash_max;
u8 hash_order[13];
u32 es_offset;
u32 ssl_offset;
} key_info_t;
typedef struct _pkg1_id_t typedef struct _pkg1_id_t
{ {
const char *id; const char *id;
u32 kb; u32 kb;
key_info_t key_info;
} pkg1_id_t; } pkg1_id_t;
const pkg1_id_t *pkg1_identify(u8 *pkg1); const pkg1_id_t *pkg1_identify(u8 *pkg1);

View file

@ -16,6 +16,10 @@
static u8 zeros[0x10] = {0}; static u8 zeros[0x10] = {0};
static u8 null_hash[0x20] = {
0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55};
static const u8 keyblob_key_source[][0x10] = { static const u8 keyblob_key_source[][0x10] = {
{0xDF, 0x20, 0x6F, 0x59, 0x44, 0x54, 0xEF, 0xDC, 0x70, 0x74, 0x48, 0x3B, 0x0D, 0xED, 0x9F, 0xD3}, //1.0.0 {0xDF, 0x20, 0x6F, 0x59, 0x44, 0x54, 0xEF, 0xDC, 0x70, 0x74, 0x48, 0x3B, 0x0D, 0xED, 0x9F, 0xD3}, //1.0.0
{0x0C, 0x25, 0x61, 0x5D, 0x68, 0x4C, 0xEB, 0x42, 0x1C, 0x23, 0x79, 0xEA, 0x82, 0x25, 0x12, 0xAC}, //3.0.0 {0x0C, 0x25, 0x61, 0x5D, 0x68, 0x4C, 0xEB, 0x42, 0x1C, 0x23, 0x79, 0xEA, 0x82, 0x25, 0x12, 0xAC}, //3.0.0
@ -33,8 +37,7 @@ static const u8 master_kek_sources[KB_FIRMWARE_VERSION_MAX - KB_FIRMWARE_VERSION
{0x30, 0x3F, 0x02, 0x7E, 0xD8, 0x38, 0xEC, 0xD7, 0x93, 0x25, 0x34, 0xB5, 0x30, 0xEB, 0xCA, 0x7A}, //9.1.0 {0x30, 0x3F, 0x02, 0x7E, 0xD8, 0x38, 0xEC, 0xD7, 0x93, 0x25, 0x34, 0xB5, 0x30, 0xEB, 0xCA, 0x7A}, //9.1.0
}; };
static const u8 master_key_vectors[KB_FIRMWARE_VERSION_MAX+1][0x10] = static const u8 master_key_vectors[KB_FIRMWARE_VERSION_MAX+1][0x10] = {
{
{0x0C, 0xF0, 0x59, 0xAC, 0x85, 0xF6, 0x26, 0x65, 0xE1, 0xE9, 0x19, 0x55, 0xE6, 0xF2, 0x67, 0x3D}, /* Zeroes encrypted with Master Key 00. */ {0x0C, 0xF0, 0x59, 0xAC, 0x85, 0xF6, 0x26, 0x65, 0xE1, 0xE9, 0x19, 0x55, 0xE6, 0xF2, 0x67, 0x3D}, /* Zeroes encrypted with Master Key 00. */
{0x29, 0x4C, 0x04, 0xC8, 0xEB, 0x10, 0xED, 0x9D, 0x51, 0x64, 0x97, 0xFB, 0xF3, 0x4D, 0x50, 0xDD}, /* Master key 00 encrypted with Master key 01. */ {0x29, 0x4C, 0x04, 0xC8, 0xEB, 0x10, 0xED, 0x9D, 0x51, 0x64, 0x97, 0xFB, 0xF3, 0x4D, 0x50, 0xDD}, /* Master key 00 encrypted with Master key 01. */
{0xDE, 0xCF, 0xEB, 0xEB, 0x10, 0xAE, 0x74, 0xD8, 0xAD, 0x7C, 0xF4, 0x9E, 0x62, 0xE0, 0xE8, 0x72}, /* Master key 01 encrypted with Master key 02. */ {0xDE, 0xCF, 0xEB, 0xEB, 0x10, 0xAE, 0x74, 0xD8, 0xAD, 0x7C, 0xF4, 0x9E, 0x62, 0xE0, 0xE8, 0x72}, /* Master key 01 encrypted with Master key 02. */
@ -86,14 +89,17 @@ static const u8 device_master_key_source_sources[KB_FIRMWARE_VERSION_MAX - KB_FI
{0x14, 0xB8, 0x74, 0x12, 0xCB, 0xBD, 0x0B, 0x8F, 0x20, 0xFB, 0x30, 0xDA, 0x27, 0xE4, 0x58, 0x94}, /* 9.1.0 Device Master Key Source Source. */ {0x14, 0xB8, 0x74, 0x12, 0xCB, 0xBD, 0x0B, 0x8F, 0x20, 0xFB, 0x30, 0xDA, 0x27, 0xE4, 0x58, 0x94}, /* 9.1.0 Device Master Key Source Source. */
}; };
static const u8 es_keys[3][0x10] = { // from ES
{0xdb, 0xa4, 0x51, 0x12, 0x4c, 0xa0, 0xa9, 0x83, 0x68, 0x14, 0xf5, 0xed, 0x95, 0xe3, 0x12, 0x5b}, // eticket_rsa_kek_source static const u8 eticket_rsa_kek_source[0x10] = {
{0x46, 0x6e, 0x57, 0xb7, 0x4a, 0x44, 0x7f, 0x02, 0xf3, 0x21, 0xcd, 0xe5, 0x8f, 0x2f, 0x55, 0x35}, // eticket_rsa_kekek_source 0XDB, 0XA4, 0X51, 0X12, 0X4C, 0XA0, 0XA9, 0X83, 0X68, 0X14, 0XF5, 0XED, 0X95, 0XE3, 0X12, 0X5B};
{0x7f, 0x5b, 0xb0, 0x84, 0x7b, 0x25, 0xaa, 0x67, 0xfa, 0xc8, 0x4b, 0xe2, 0x3d, 0x7b, 0x69, 0x03}, // ssl_rsa_kek_source_x static const u8 eticket_rsa_kekek_source[0x10] = {
}; 0X46, 0X6E, 0X57, 0XB7, 0X4A, 0X44, 0X7F, 0X02, 0XF3, 0X21, 0XCD, 0XE5, 0X8F, 0X2F, 0X55, 0X35};
static const u8 ssl_keys[0x10] = { // from SSL
0x9a, 0x38, 0x3b, 0xf4, 0x31, 0xd0, 0xbd, 0x81, 0x32, 0x53, 0x4b, 0xa9, 0x64, 0x39, 0x7d, 0xe3}; // ssl_rsa_kek_source_y static const u8 ssl_rsa_kek_source_x[0x10] = {
0X7F, 0X5B, 0XB0, 0X84, 0X7B, 0X25, 0XAA, 0X67, 0XFA, 0XC8, 0X4B, 0XE2, 0X3D, 0X7B, 0X69, 0X03};
static const u8 ssl_rsa_kek_source_y[0x10] = {
0X9A, 0X38, 0X3B, 0XF4, 0X31, 0XD0, 0XBD, 0X81, 0X32, 0X53, 0X4B, 0XA9, 0X64, 0X39, 0X7D, 0XE3};
static const u8 device_master_kek_sources[KB_FIRMWARE_VERSION_MAX - KB_FIRMWARE_VERSION_400 + 1][0x10] = { static const u8 device_master_kek_sources[KB_FIRMWARE_VERSION_MAX - KB_FIRMWARE_VERSION_400 + 1][0x10] = {
{0x88, 0x62, 0x34, 0x6E, 0xFA, 0xF7, 0xD8, 0x3F, 0xE1, 0x30, 0x39, 0x50, 0xF0, 0xB7, 0x5D, 0x5D}, /* 4.0.0 Device Master Kek Source. */ {0x88, 0x62, 0x34, 0x6E, 0xFA, 0xF7, 0xD8, 0x3F, 0xE1, 0x30, 0x39, 0x50, 0xF0, 0xB7, 0x5D, 0x5D}, /* 4.0.0 Device Master Kek Source. */
@ -124,22 +130,35 @@ static const u8 bis_key_source[3][0x20] = {
0x52, 0xC2, 0xE9, 0xEB, 0x09, 0xE3, 0xEE, 0x29, 0x32, 0xA1, 0x0C, 0x1F, 0xB6, 0xA0, 0x92, 0x6C, 0x52, 0xC2, 0xE9, 0xEB, 0x09, 0xE3, 0xEE, 0x29, 0x32, 0xA1, 0x0C, 0x1F, 0xB6, 0xA0, 0x92, 0x6C,
0x4D, 0x12, 0xE1, 0x4B, 0x2A, 0x47, 0x4C, 0x1C, 0x09, 0xCB, 0x03, 0x59, 0xF0, 0x15, 0xF4, 0xE4} 0x4D, 0x12, 0xE1, 0x4B, 0x2A, 0x47, 0x4C, 0x1C, 0x09, 0xCB, 0x03, 0x59, 0xF0, 0x15, 0xF4, 0xE4}
}; };
static const u8 fs_keys[13][0x20] = { static const u8 header_kek_source[0x10] = {
{0x1F, 0x12, 0x91, 0x3A, 0x4A, 0xCB, 0xF0, 0x0D, 0x4C, 0xDE, 0x3A, 0xF6, 0xD5, 0x23, 0x88, 0x2A}, // header key source 0x1F, 0x12, 0x91, 0x3A, 0x4A, 0xCB, 0xF0, 0x0D, 0x4C, 0xDE, 0x3A, 0xF6, 0xD5, 0x23, 0x88, 0x2A};
{0x5A, 0x3E, 0xD8, 0x4F, 0xDE, 0xC0, 0xD8, 0x26, 0x31, 0xF7, 0xE2, 0x5D, 0x19, 0x7B, 0xF5, 0xD0, static const u8 header_key_source[0x20] = {
0x1C, 0x9B, 0x7B, 0xFA, 0xF6, 0x28, 0x18, 0x3D, 0x71, 0xF6, 0x4D, 0x73, 0xF1, 0x50, 0xB9, 0xD2}, // encrypted header key 0x5A, 0x3E, 0xD8, 0x4F, 0xDE, 0xC0, 0xD8, 0x26, 0x31, 0xF7, 0xE2, 0x5D, 0x19, 0x7B, 0xF5, 0xD0,
{0x7F, 0x59, 0x97, 0x1E, 0x62, 0x9F, 0x36, 0xA1, 0x30, 0x98, 0x06, 0x6F, 0x21, 0x44, 0xC3, 0x0D}, // kak appli 0x1C, 0x9B, 0x7B, 0xFA, 0xF6, 0x28, 0x18, 0x3D, 0x71, 0xF6, 0x4D, 0x73, 0xF1, 0x50, 0xB9, 0xD2};
{0x32, 0x7D, 0x36, 0x08, 0x5A, 0xD1, 0x75, 0x8D, 0xAB, 0x4E, 0x6F, 0xBA, 0xA5, 0x55, 0xD8, 0x82}, // kak ocean static const u8 key_area_key_sources[3][0x10] = {
{0x87, 0x45, 0xF1, 0xBB, 0xA6, 0xBE, 0x79, 0x64, 0x7D, 0x04, 0x8B, 0xA6, 0x7B, 0x5F, 0xDA, 0x4A}, // kak system {
{0xd8, 0x9c, 0x23, 0x6e, 0xc9, 0x12, 0x4e, 0x43, 0xc8, 0x2b, 0x03, 0x87, 0x43, 0xf9, 0xcf, 0x1b}, // save mac kek source 0x7F, 0x59, 0x97, 0x1E, 0x62, 0x9F, 0x36, 0xA1, 0x30, 0x98, 0x06, 0x6F, 0x21, 0x44, 0xC3, 0x0D}, // application
{0xe4, 0xcd, 0x3d, 0x4a, 0xd5, 0x0f, 0x74, 0x28, 0x45, 0xa4, 0x87, 0xe5, 0xa0, 0x63, 0xea, 0x1f}, // save mac key source {
{0x04, 0x89, 0xef, 0x5d, 0x32, 0x6e, 0x1a, 0x59, 0xc4, 0xb7, 0xab, 0x8c, 0x36, 0x7a, 0xab, 0x17}, // save mac sd kek source 0x32, 0x7D, 0x36, 0x08, 0x5A, 0xD1, 0x75, 0x8D, 0xAB, 0x4E, 0x6F, 0xBA, 0xA5, 0x55, 0xD8, 0x82}, // ocean
{0x6f, 0x64, 0x59, 0x47, 0xc5, 0x61, 0x46, 0xf9, 0xff, 0xa0, 0x45, 0xd5, 0x95, 0x33, 0x29, 0x18}, // save mac sd key source {
{0x37, 0x0c, 0x34, 0x5e, 0x12, 0xe4, 0xce, 0xfe, 0x21, 0xb5, 0x8e, 0x64, 0xdb, 0x52, 0xaf, 0x35, 0x87, 0x45, 0xF1, 0xBB, 0xA6, 0xBE, 0x79, 0x64, 0x7D, 0x04, 0x8B, 0xA6, 0x7B, 0x5F, 0xDA, 0x4A}, // system
0x4f, 0x2c, 0xa5, 0xa3, 0xfc, 0x99, 0x9a, 0x47, 0xc0, 0x3e, 0xe0, 0x04, 0x48, 0x5b, 0x2f, 0xd0}, // sd custom key source
{0x88, 0x35, 0x8d, 0x9c, 0x62, 0x9b, 0xa1, 0xa0, 0x01, 0x47, 0xdb, 0xe0, 0x62, 0x1b, 0x54, 0x32}, // sd card kek source
{0x58, 0x41, 0xa2, 0x84, 0x93, 0x5b, 0x56, 0x27, 0x8b, 0x8e, 0x1f, 0xc5, 0x18, 0xe9, 0x9f, 0x2b,
0x67, 0xc7, 0x93, 0xf0, 0xf2, 0x4f, 0xde, 0xd0, 0x75, 0x49, 0x5d, 0xca, 0x00, 0x6d, 0x99, 0xc2}, // sd nca key source
{0x24, 0x49, 0xb7, 0x22, 0x72, 0x67, 0x03, 0xa8, 0x19, 0x65, 0xe6, 0xe3, 0xea, 0x58, 0x2f, 0xdd,
0x9a, 0x95, 0x15, 0x17, 0xb1, 0x6e, 0x8f, 0x7f, 0x1f, 0x68, 0x26, 0x31, 0x52, 0xea, 0x29, 0x6a}, // sd nca save source
}; };
static const u8 save_mac_kek_source[0x10] = {
0XD8, 0X9C, 0X23, 0X6E, 0XC9, 0X12, 0X4E, 0X43, 0XC8, 0X2B, 0X03, 0X87, 0X43, 0XF9, 0XCF, 0X1B};
static const u8 save_mac_key_source[0x10] = {
0XE4, 0XCD, 0X3D, 0X4A, 0XD5, 0X0F, 0X74, 0X28, 0X45, 0XA4, 0X87, 0XE5, 0XA0, 0X63, 0XEA, 0X1F};
static const u8 save_mac_sd_card_kek_source[0x10] = {
0X04, 0X89, 0XEF, 0X5D, 0X32, 0X6E, 0X1A, 0X59, 0XC4, 0XB7, 0XAB, 0X8C, 0X36, 0X7A, 0XAB, 0X17};
static const u8 save_mac_sd_card_key_source[0x10] = {
0X6F, 0X64, 0X59, 0X47, 0XC5, 0X61, 0X46, 0XF9, 0XFF, 0XA0, 0X45, 0XD5, 0X95, 0X33, 0X29, 0X18};
static const u8 sd_card_custom_storage_key_source[0x20] = {
0X37, 0X0C, 0X34, 0X5E, 0X12, 0XE4, 0XCE, 0XFE, 0X21, 0XB5, 0X8E, 0X64, 0XDB, 0X52, 0XAF, 0X35,
0X4F, 0X2C, 0XA5, 0XA3, 0XFC, 0X99, 0X9A, 0X47, 0XC0, 0X3E, 0XE0, 0X04, 0X48, 0X5B, 0X2F, 0XD0};
static const u8 sd_card_kek_source[0x10] = {
0X88, 0X35, 0X8D, 0X9C, 0X62, 0X9B, 0XA1, 0XA0, 0X01, 0X47, 0XDB, 0XE0, 0X62, 0X1B, 0X54, 0X32};
static const u8 sd_card_nca_key_source[0x20] = {
0X58, 0X41, 0XA2, 0X84, 0X93, 0X5B, 0X56, 0X27, 0X8B, 0X8E, 0X1F, 0XC5, 0X18, 0XE9, 0X9F, 0X2B,
0X67, 0XC7, 0X93, 0XF0, 0XF2, 0X4F, 0XDE, 0XD0, 0X75, 0X49, 0X5D, 0XCA, 0X00, 0X6D, 0X99, 0XC2};
static const u8 sd_card_save_key_source[0x20] = {
0X24, 0X49, 0XB7, 0X22, 0X72, 0X67, 0X03, 0XA8, 0X19, 0X65, 0XE6, 0XE3, 0XEA, 0X58, 0X2F, 0XDD,
0X9A, 0X95, 0X15, 0X17, 0XB1, 0X6E, 0X8F, 0X7F, 0X1F, 0X68, 0X26, 0X31, 0X52, 0XEA, 0X29, 0X6A};

View file

@ -56,28 +56,20 @@ static u32 _key_count = 0, _titlekey_count = 0;
static u32 start_time, end_time; static u32 start_time, end_time;
u32 color_idx = 0; u32 color_idx = 0;
#define TPRINTF(text) \ static ALWAYS_INLINE u32 _read_le_u32(const void *buffer, u32 offset) {
end_time = get_tmr_us(); \
gfx_printf(text" done in %d us\n", end_time - start_time); \
start_time = get_tmr_us(); \
minerva_periodic_training()
#define TPRINTFARGS(text, args...) \
end_time = get_tmr_us(); \
gfx_printf(text" done in %d us\n", args, end_time - start_time); \
start_time = get_tmr_us(); \
minerva_periodic_training()
#define SAVE_KEY(name, src, len) _save_key(name, src, len, text_buffer)
#define SAVE_KEY_FAMILY(name, src, start, count, len) _save_key_family(name, src, start, count, len, text_buffer)
static inline u32 _read_le_u32(const void *buffer, u32 offset) {
return (*(u8*)(buffer + offset + 0) ) | return (*(u8*)(buffer + offset + 0) ) |
(*(u8*)(buffer + offset + 1) << 0x08) | (*(u8*)(buffer + offset + 1) << 0x08) |
(*(u8*)(buffer + offset + 2) << 0x10) | (*(u8*)(buffer + offset + 2) << 0x10) |
(*(u8*)(buffer + offset + 3) << 0x18); (*(u8*)(buffer + offset + 3) << 0x18);
} }
static ALWAYS_INLINE u32 _read_be_u32(const void *buffer, u32 offset) {
return (*(u8*)(buffer + offset + 3) ) |
(*(u8*)(buffer + offset + 2) << 0x08) |
(*(u8*)(buffer + offset + 1) << 0x10) |
(*(u8*)(buffer + offset + 0) << 0x18);
}
// key functions // key functions
static int _key_exists(const void *data) { return memcmp(data, zeros, 0x10) != 0; }; static int _key_exists(const void *data) { return memcmp(data, zeros, 0x10) != 0; };
static void _save_key(const char *name, const void *data, u32 len, char *outbuf); static void _save_key(const char *name, const void *data, u32 len, char *outbuf);
@ -86,9 +78,8 @@ static void _generate_kek(u32 ks, const void *key_source, void *master_key, con
static void _get_device_key(u32 ks, void *out_device_key, u32 revision, const void *device_key, const void *master_key); static void _get_device_key(u32 ks, void *out_device_key, u32 revision, const void *device_key, const void *master_key);
// titlekey functions // titlekey functions
static bool _test_key_pair(const void *E, const void *D, const void *N); static bool _test_key_pair(const void *E, const void *D, const void *N);
static void _mgf1_xor(void *masked, u32 masked_size, const void *seed, u32 seed_size);
static inline u8 *_find_tsec_fw(const u8 *pkg1) { static ALWAYS_INLINE u8 *_find_tsec_fw(const u8 *pkg1) {
const u32 tsec_fw_align = 0x100; const u32 tsec_fw_align = 0x100;
const u32 tsec_fw_first_instruction = 0xCF42004D; const u32 tsec_fw_first_instruction = 0xCF42004D;
@ -99,10 +90,137 @@ static inline u8 *_find_tsec_fw(const u8 *pkg1) {
return NULL; return NULL;
} }
static inline u32 _get_tsec_fw_size(tsec_key_data_t *key_data) { static ALWAYS_INLINE u32 _get_tsec_fw_size(tsec_key_data_t *key_data) {
return key_data->blob0_size + sizeof(tsec_key_data_t) + key_data->blob1_size + key_data->blob2_size + key_data->blob3_size + key_data->blob4_size; return key_data->blob0_size + sizeof(tsec_key_data_t) + key_data->blob1_size + key_data->blob2_size + key_data->blob3_size + key_data->blob4_size;
} }
static bool _get_titlekeys_from_save(u32 buf_size, const u8 *save_mac_key, titlekey_buffer_t *titlekey_buffer, rsa_keypair_t *rsa_keypair) {
FIL fp;
u64 br = buf_size;
u64 offset = 0;
u32 file_tkey_count = 0;
u32 save_x = gfx_con.x, save_y = gfx_con.y;
bool is_personalized = rsa_keypair != NULL;
u32 start_titlekey_count = _titlekey_count;
char titlekey_save_path[32] = "bis:/save/80000000000000E1";
if (is_personalized) {
titlekey_save_path[25] = '2';
gfx_printf("\n%kPersonalized... ", colors[color_idx % 6]);
} else {
gfx_printf("\n%kCommon... ", colors[color_idx % 6]);
}
if (f_open(&fp, titlekey_save_path, FA_READ | FA_OPEN_EXISTING)) {
EPRINTF("Unable to open e1 save. Skipping.");
return false;
}
save_ctx_t *save_ctx = calloc(1, sizeof(save_ctx_t));
save_init(save_ctx, &fp, save_mac_key, 0);
bool save_process_success = save_process(save_ctx);
TPRINTF("\n Save process...");
if (!save_process_success) {
EPRINTF("Failed to process es save.");
f_close(&fp);
save_free_contexts(save_ctx);
free(save_ctx);
return false;
}
char ticket_bin_path[0x40] = "/ticket.bin";
char ticket_list_bin_path[0x40] = "/ticket_list.bin";
save_data_file_ctx_t ticket_file;
if (!save_open_file(save_ctx, &ticket_file, ticket_list_bin_path, OPEN_MODE_READ)) {
EPRINTF("Unable to locate ticket_list.bin in save.");
f_close(&fp);
save_free_contexts(save_ctx);
free(save_ctx);
return false;
}
bool terminator_reached = false;
while (offset < ticket_file.size && !terminator_reached) {
if (!save_data_file_read(&ticket_file, &br, offset, titlekey_buffer->read_buffer, buf_size) || titlekey_buffer->read_buffer[0] == 0 || br != buf_size)
break;
offset += br;
minerva_periodic_training();
ticket_record_t *curr_ticket_record = (ticket_record_t *)titlekey_buffer->read_buffer;
for (u32 i = 0; i < buf_size; i += sizeof(ticket_record_t), curr_ticket_record++) {
if (curr_ticket_record->rights_id[0] == 0xFF) {
terminator_reached = true;
break;
}
file_tkey_count++;
}
}
TPRINTF(" Count keys...");
if (!save_open_file(save_ctx, &ticket_file, ticket_bin_path, OPEN_MODE_READ)) {
EPRINTF("Unable to locate ticket.bin in save.");
f_close(&fp);
save_free_contexts(save_ctx);
free(save_ctx);
return false;
}
const u32 ticket_sig_type_rsa2048_sha256 = 0x10004;
offset = 0;
terminator_reached = false;
u32 pct = 0, last_pct = 0;
while (offset < ticket_file.size && !terminator_reached) {
if (!save_data_file_read(&ticket_file, &br, offset, titlekey_buffer->read_buffer, buf_size) || titlekey_buffer->read_buffer[0] == 0 || br != buf_size)
break;
offset += br;
ticket_t *curr_ticket = (ticket_t *)titlekey_buffer->read_buffer;
for (u32 j = 0; j < buf_size; j += sizeof(ticket_t), curr_ticket++) {
minerva_periodic_training();
pct = (_titlekey_count - start_titlekey_count) * 100 / file_tkey_count;
if (pct > last_pct && pct <= 100) {
last_pct = pct;
tui_pbar(save_x, save_y, pct, COLOR_GREEN, 0xFF155500);
}
if (curr_ticket->signature_type != ticket_sig_type_rsa2048_sha256) {
terminator_reached = true;
break;
}
if (is_personalized) {
se_rsa_exp_mod(0, curr_ticket->titlekey_block, sizeof(curr_ticket->titlekey_block), curr_ticket->titlekey_block, sizeof(curr_ticket->titlekey_block));
if (se_rsa_oaep_decode(
curr_ticket->titlekey_block, sizeof(titlekey_buffer->titlekeys[0]),
null_hash, sizeof(null_hash),
curr_ticket->titlekey_block, sizeof(curr_ticket->titlekey_block)
) != sizeof(titlekey_buffer->titlekeys[0])
)
continue;
}
memcpy(titlekey_buffer->rights_ids[_titlekey_count], curr_ticket->rights_id, sizeof(titlekey_buffer->rights_ids[0]));
memcpy(titlekey_buffer->titlekeys[_titlekey_count], curr_ticket->titlekey_block, sizeof(titlekey_buffer->titlekeys[0]));
_titlekey_count++;
}
}
tui_pbar(save_x, save_y, 100, COLOR_GREEN, 0xFF155500);
f_close(&fp);
save_free_contexts(save_ctx);
free(save_ctx);
gfx_con_setpos(0, save_y);
if (is_personalized) {
TPRINTFARGS("\n%kPersonalized... ", colors[(color_idx++) % 6]);
} else {
TPRINTFARGS("\n%kCommon... ", colors[(color_idx++) % 6]);
}
gfx_printf("\n\n\n");
return true;
}
#define RELOC_META_OFF 0x7C #define RELOC_META_OFF 0x7C
void dump_keys() { void dump_keys() {
@ -119,7 +237,6 @@ void dump_keys() {
eticket_rsa_kek_personalized[0x10] = {0}, eticket_rsa_kek_personalized[0x10] = {0},
ssl_rsa_kek[0x10] = {0}, ssl_rsa_kek[0x10] = {0},
// keyblob-derived families // keyblob-derived families
keyblob[KB_FIRMWARE_VERSION_600+1][0x90] = {0},
keyblob_key[KB_FIRMWARE_VERSION_600+1][0x10] = {0}, keyblob_key[KB_FIRMWARE_VERSION_600+1][0x10] = {0},
keyblob_mac_key[KB_FIRMWARE_VERSION_600+1][0x10] = {0}, keyblob_mac_key[KB_FIRMWARE_VERSION_600+1][0x10] = {0},
package1_key[KB_FIRMWARE_VERSION_600+1][0x10] = {0}, package1_key[KB_FIRMWARE_VERSION_600+1][0x10] = {0},
@ -130,6 +247,8 @@ void dump_keys() {
package2_key[KB_FIRMWARE_VERSION_MAX+1][0x10] = {0}, package2_key[KB_FIRMWARE_VERSION_MAX+1][0x10] = {0},
titlekek[KB_FIRMWARE_VERSION_MAX+1][0x10] = {0}; titlekek[KB_FIRMWARE_VERSION_MAX+1][0x10] = {0};
keyblob_t keyblob[KB_FIRMWARE_VERSION_600+1] = {0};
sd_mount(); sd_mount();
display_backlight_brightness(h_cfg.backlight, 1000); display_backlight_brightness(h_cfg.backlight, 1000);
@ -318,22 +437,22 @@ get_tsec: ;
} }
u8 *keyblob_block = (u8 *)calloc(KB_FIRMWARE_VERSION_600 + 1, NX_EMMC_BLOCKSIZE); u8 *keyblob_block = (u8 *)calloc(KB_FIRMWARE_VERSION_600 + 1, NX_EMMC_BLOCKSIZE);
u8 *current_keyblob = keyblob_block; encrypted_keyblob_t *current_keyblob = (encrypted_keyblob_t *)keyblob_block;
u8 keyblob_mac[0x10] = {0}; u8 keyblob_mac[0x10] = {0};
u32 sbk[4] = {FUSE(FUSE_PRIVATE_KEY0), FUSE(FUSE_PRIVATE_KEY1), u32 sbk[4] = {FUSE(FUSE_PRIVATE_KEY0), FUSE(FUSE_PRIVATE_KEY1),
FUSE(FUSE_PRIVATE_KEY2), FUSE(FUSE_PRIVATE_KEY3)}; FUSE(FUSE_PRIVATE_KEY2), FUSE(FUSE_PRIVATE_KEY3)};
se_aes_key_set(8, tsec_keys, 0x10); se_aes_key_set(8, tsec_keys, sizeof(tsec_keys) / 2);
se_aes_key_set(9, sbk, 0x10); se_aes_key_set(9, sbk, sizeof(sbk));
if (!emummc_storage_read(&emmc_storage, KEYBLOB_OFFSET / NX_EMMC_BLOCKSIZE, KB_FIRMWARE_VERSION_600 + 1, keyblob_block)) { if (!emummc_storage_read(&emmc_storage, KEYBLOB_OFFSET / NX_EMMC_BLOCKSIZE, KB_FIRMWARE_VERSION_600 + 1, keyblob_block)) {
EPRINTF("Unable to read keyblob."); EPRINTF("Unable to read keyblob.");
} }
for (u32 i = 0; i <= KB_FIRMWARE_VERSION_600; i++, current_keyblob += NX_EMMC_BLOCKSIZE) { for (u32 i = 0; i <= KB_FIRMWARE_VERSION_600; i++, current_keyblob++) {
minerva_periodic_training(); minerva_periodic_training();
se_aes_crypt_block_ecb(8, 0, keyblob_key[i], keyblob_key_source[i]); // temp = unwrap(kbks, tsec) se_aes_crypt_block_ecb(8, 0, keyblob_key[i], keyblob_key_source[i]); // temp = unwrap(kbks, tsec)
se_aes_crypt_block_ecb(9, 0, keyblob_key[i], keyblob_key[i]); // kbk = unwrap(temp, sbk) se_aes_crypt_block_ecb(9, 0, keyblob_key[i], keyblob_key[i]); // kbk = unwrap(temp, sbk)
se_aes_key_set(7, keyblob_key[i], 0x10); se_aes_key_set(7, keyblob_key[i], sizeof(keyblob_key[i]));
se_aes_crypt_block_ecb(7, 0, keyblob_mac_key[i], keyblob_mac_key_source); // kbm = unwrap(kbms, kbk) se_aes_crypt_block_ecb(7, 0, keyblob_mac_key[i], keyblob_mac_key_source); // kbm = unwrap(kbms, kbk)
if (i == 0) { if (i == 0) {
se_aes_crypt_block_ecb(7, 0, device_key, per_console_key_source); // devkey = unwrap(pcks, kbk0) se_aes_crypt_block_ecb(7, 0, device_key, per_console_key_source); // devkey = unwrap(pcks, kbk0)
@ -341,22 +460,20 @@ get_tsec: ;
} }
// verify keyblob is not corrupt // verify keyblob is not corrupt
se_aes_key_set(10, keyblob_mac_key[i], 0x10); se_aes_key_set(10, keyblob_mac_key[i], sizeof(keyblob_mac_key[i]));
se_aes_cmac(10, keyblob_mac, 0x10, current_keyblob + 0x10, 0xa0); se_aes_cmac(10, keyblob_mac, sizeof(keyblob_mac), current_keyblob->iv, sizeof(current_keyblob->iv) + sizeof(keyblob_t));
if (memcmp(current_keyblob, keyblob_mac, 0x10) != 0) { if (memcmp(current_keyblob, keyblob_mac, sizeof(keyblob_mac)) != 0) {
EPRINTFARGS("Keyblob %x corrupt.", i); EPRINTFARGS("Keyblob %x corrupt.", i);
gfx_hexdump(i, current_keyblob, 0x10);
gfx_hexdump(i, keyblob_mac, 0x10);
continue; continue;
} }
// decrypt keyblobs // decrypt keyblobs
se_aes_key_set(6, keyblob_key[i], 0x10); se_aes_key_set(6, keyblob_key[i], sizeof(keyblob_key[i]));
se_aes_crypt_ctr(6, keyblob[i], 0x90, current_keyblob + 0x20, 0x90, current_keyblob + 0x10); se_aes_crypt_ctr(6, &keyblob[i], sizeof(keyblob_t), &current_keyblob->key_data, sizeof(keyblob_t), current_keyblob->iv);
memcpy(package1_key[i], keyblob[i] + 0x80, 0x10); memcpy(package1_key[i], keyblob[i].package1_key, sizeof(package1_key[i]));
memcpy(master_kek[i], keyblob[i], 0x10); memcpy(master_kek[i], keyblob[i].master_kek, sizeof(master_kek[i]));
se_aes_key_set(7, master_kek[i], 0x10); se_aes_key_set(7, master_kek[i], sizeof(master_kek[i]));
se_aes_crypt_block_ecb(7, 0, master_key[i], master_key_source); se_aes_crypt_block_ecb(7, 0, master_key[i], master_key_source);
} }
free(keyblob_block); free(keyblob_block);
@ -389,19 +506,17 @@ get_tsec: ;
memcpy(bis_key[3], bis_key[2], 0x20); memcpy(bis_key[3], bis_key[2], 0x20);
} }
u8 *rights_ids = NULL, *titlekeys = NULL;
TPRINTFARGS("%kFS keys... ", colors[(color_idx++) % 6]); TPRINTFARGS("%kFS keys... ", colors[(color_idx++) % 6]);
if (_key_exists(fs_keys[FS_HEADER_KEK_SOURCE]) && _key_exists(fs_keys[FS_HEADER_KEY_SOURCE]) && _key_exists(master_key[0])) { if (_key_exists(master_key[0])) {
_generate_kek(8, fs_keys[FS_HEADER_KEK_SOURCE], master_key[0], aes_kek_generation_source, aes_key_generation_source); _generate_kek(8, header_kek_source, master_key[0], aes_kek_generation_source, aes_key_generation_source);
se_aes_crypt_block_ecb(8, 0, header_key + 0x00, fs_keys[FS_HEADER_KEY_SOURCE] + 0x00); se_aes_crypt_block_ecb(8, 0, header_key + 0x00, header_key_source + 0x00);
se_aes_crypt_block_ecb(8, 0, header_key + 0x10, fs_keys[FS_HEADER_KEY_SOURCE] + 0x10); se_aes_crypt_block_ecb(8, 0, header_key + 0x10, header_key_source + 0x10);
} }
if (_key_exists(fs_keys[FS_SAVE_MAC_KEK_SOURCE]) && _key_exists(fs_keys[FS_SAVE_MAC_KEY_SOURCE]) && _key_exists(device_key)) { if (_key_exists(device_key)) {
_generate_kek(8, fs_keys[FS_SAVE_MAC_KEK_SOURCE], device_key, aes_kek_generation_source, NULL); _generate_kek(8, save_mac_kek_source, device_key, aes_kek_generation_source, NULL);
se_aes_crypt_block_ecb(8, 0, save_mac_key, fs_keys[FS_SAVE_MAC_KEY_SOURCE]); se_aes_crypt_block_ecb(8, 0, save_mac_key, save_mac_key_source);
} }
if (_key_exists(master_key[derivable_key_count])) { if (_key_exists(master_key[derivable_key_count])) {
@ -410,12 +525,10 @@ get_tsec: ;
for (u32 i = 0; i < derivable_key_count; i++) { for (u32 i = 0; i < derivable_key_count; i++) {
if (!_key_exists(master_key[i])) if (!_key_exists(master_key[i]))
continue; continue;
if (_key_exists(fs_keys[FS_KEY_AREA_KEY_APPLI_SOURCE]) && _key_exists(fs_keys[FS_KEY_AREA_KEY_OCEAN_SOURCE]) && _key_exists(fs_keys[FS_KEY_AREA_KEY_SYSTE_SOURCE])) {
for (u32 j = 0; j < 3; j++) { for (u32 j = 0; j < 3; j++) {
_generate_kek(8, fs_keys[FS_KEY_AREA_KEY_APPLI_SOURCE + j], master_key[i], aes_kek_generation_source, NULL); _generate_kek(8, key_area_key_sources[j], master_key[i], aes_kek_generation_source, NULL);
se_aes_crypt_block_ecb(8, 0, key_area_key[j][i], aes_key_generation_source); se_aes_crypt_block_ecb(8, 0, key_area_key[j][i], aes_key_generation_source);
} }
}
se_aes_key_set(8, master_key[i], 0x10); se_aes_key_set(8, master_key[i], 0x10);
se_aes_crypt_block_ecb(8, 0, package2_key[i], package2_key_source); se_aes_crypt_block_ecb(8, 0, package2_key[i], package2_key_source);
se_aes_crypt_block_ecb(8, 0, titlekek[i], titlekek_source); se_aes_crypt_block_ecb(8, 0, titlekek[i], titlekek_source);
@ -429,21 +542,19 @@ get_tsec: ;
FILINFO fno; FILINFO fno;
FIL fp; FIL fp;
save_ctx_t *save_ctx = NULL;
bool save_process_success = false;
u32 read_bytes = 0; u32 read_bytes = 0;
// derive eticket_rsa_kek and ssl_rsa_kek // derive eticket_rsa_kek and ssl_rsa_kek
if (_key_exists(master_key[0])) { if (_key_exists(master_key[0])) {
for (u32 i = 0; i < 0x10; i++) for (u32 i = 0; i < 0x10; i++)
temp_key[i] = aes_kek_generation_source[i] ^ aes_kek_seed_03[i]; temp_key[i] = aes_kek_generation_source[i] ^ aes_kek_seed_03[i];
_generate_kek(7, es_keys[1], master_key[0], temp_key, NULL); _generate_kek(7, eticket_rsa_kekek_source, master_key[0], temp_key, NULL);
se_aes_crypt_block_ecb(7, 0, eticket_rsa_kek, es_keys[0]); se_aes_crypt_block_ecb(7, 0, eticket_rsa_kek, eticket_rsa_kek_source);
for (u32 i = 0; i < 0x10; i++) for (u32 i = 0; i < 0x10; i++)
temp_key[i] = aes_kek_generation_source[i] ^ aes_kek_seed_01[i]; temp_key[i] = aes_kek_generation_source[i] ^ aes_kek_seed_01[i];
_generate_kek(7, es_keys[2], master_key[0], temp_key, NULL); _generate_kek(7, ssl_rsa_kek_source_x, master_key[0], temp_key, NULL);
se_aes_crypt_block_ecb(7, 0, ssl_rsa_kek, ssl_keys); se_aes_crypt_block_ecb(7, 0, ssl_rsa_kek, ssl_rsa_kek_source_y);
} }
// Set BIS keys. // Set BIS keys.
@ -511,8 +622,8 @@ get_tsec: ;
for (u32 i = 0x8000; i < f_size(&fp); i += 0x4000) { for (u32 i = 0x8000; i < f_size(&fp); i += 0x4000) {
if (f_lseek(&fp, i) || f_read(&fp, read_buf, 0x20, &read_bytes) || read_bytes != 0x20) if (f_lseek(&fp, i) || f_read(&fp, read_buf, 0x20, &read_bytes) || read_bytes != 0x20)
break; break;
if (!memcmp(temp_key, read_buf, 0x10)) { if (!memcmp(temp_key, read_buf, sizeof(temp_key))) {
memcpy(sd_seed, read_buf + 0x10, 0x10); memcpy(sd_seed, read_buf + 0x10, sizeof(sd_seed));
break; break;
} }
} }
@ -524,35 +635,28 @@ get_titlekeys:
if (!_key_exists(eticket_rsa_kek)) if (!_key_exists(eticket_rsa_kek))
goto dismount; goto dismount;
gfx_printf("%kTitlekeys... ", colors[(color_idx++) % 6]); gfx_printf("%kTitlekeys... \n", colors[(color_idx++) % 6]);
u32 save_x = gfx_con.x, save_y = gfx_con.y;
gfx_printf("\n%kCommon... ", colors[color_idx % 6]);
u8 null_hash[0x20] = {
0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55};
u32 buf_size = 0x4000; u32 buf_size = 0x4000;
u8 *buffer = (u8 *)MIXD_BUF_ALIGNED; rsa_keypair_t rsa_keypair = {0};
u8 keypair[0x230] = {0}; titlekey_buffer_t *titlekey_buffer = (titlekey_buffer_t *)TITLEKEY_BUF_ADR;
if (!emummc_storage_read(&emmc_storage, NX_EMMC_CALIBRATION_OFFSET / NX_EMMC_BLOCKSIZE, NX_EMMC_CALIBRATION_SIZE / NX_EMMC_BLOCKSIZE, buffer)) { if (!emummc_storage_read(&emmc_storage, NX_EMMC_CALIBRATION_OFFSET / NX_EMMC_BLOCKSIZE, NX_EMMC_CALIBRATION_SIZE / NX_EMMC_BLOCKSIZE, titlekey_buffer->read_buffer)) {
EPRINTF("Unable to read PRODINFO."); EPRINTF("Unable to read PRODINFO.");
goto dismount; goto dismount;
} }
se_aes_xts_crypt(1, 0, 0, 0, buffer, buffer, 0x4000, NX_EMMC_CALIBRATION_SIZE / 0x4000); se_aes_xts_crypt(1, 0, 0, 0, titlekey_buffer->read_buffer, titlekey_buffer->read_buffer, XTS_CLUSTER_SIZE, NX_EMMC_CALIBRATION_SIZE / XTS_CLUSTER_SIZE);
nx_emmc_cal0_t *cal0 = (nx_emmc_cal0_t *)buffer; nx_emmc_cal0_t *cal0 = (nx_emmc_cal0_t *)titlekey_buffer->read_buffer;
if (cal0->magic != 0x304C4143) { if (cal0->magic != 0x304C4143) {
EPRINTF("CAL0 magic not found. Check BIS key 0."); EPRINTF("Invalid CAL0 magic. Check BIS key 0.");
goto dismount; goto dismount;
} }
u32 keypair_generation = cal0->ext_ecc_rsa2048_eticket_key_ver; // settings sysmodule manually zeroes this out below cal version 9
if (cal0->version <= 8) u32 keypair_generation = cal0->version <= 8 ? 0 : cal0->ext_ecc_rsa2048_eticket_key_ver;
keypair_generation = 0; // settings zeroes this out below cal version 9
if (keypair_generation) { if (keypair_generation) {
keypair_generation--; keypair_generation--;
@ -560,216 +664,35 @@ get_titlekeys:
temp_key[i] = aes_kek_generation_source[i] ^ aes_kek_seed_03[i]; temp_key[i] = aes_kek_generation_source[i] ^ aes_kek_seed_03[i];
u8 temp_device_key[0x10] = {0}; u8 temp_device_key[0x10] = {0};
_get_device_key(7, temp_device_key, keypair_generation, device_key_4x, master_key[0]); _get_device_key(7, temp_device_key, keypair_generation, device_key_4x, master_key[0]);
_generate_kek(7, es_keys[1], temp_device_key, temp_key, NULL); _generate_kek(7, eticket_rsa_kekek_source, temp_device_key, temp_key, NULL);
se_aes_crypt_block_ecb(7, 0, eticket_rsa_kek_personalized, es_keys[0]); se_aes_crypt_block_ecb(7, 0, eticket_rsa_kek_personalized, eticket_rsa_kek_source);
memcpy(temp_key, eticket_rsa_kek_personalized, 0x10); memcpy(temp_key, eticket_rsa_kek_personalized, sizeof(temp_key));
} else { } else {
memcpy(temp_key, eticket_rsa_kek, 0x10); memcpy(temp_key, eticket_rsa_kek, sizeof(temp_key));
} }
se_aes_key_set(6, temp_key, 0x10); se_aes_key_set(6, temp_key, sizeof(temp_key));
se_aes_crypt_ctr(6, keypair, 0x230, cal0->ext_ecc_rsa2048_eticket_key + 0x10, 0x230, cal0->ext_ecc_rsa2048_eticket_key); se_aes_crypt_ctr(6, &rsa_keypair, sizeof(rsa_keypair), cal0->ext_ecc_rsa2048_eticket_key, sizeof(cal0->ext_ecc_rsa2048_eticket_key), cal0->ext_ecc_rsa2048_eticket_key_iv);
u8 *D = keypair, *N = keypair + 0x100, *E = keypair + 0x200; // Check public exponent is 65537 big endian
if (_read_be_u32(rsa_keypair.public_exponent, 0) != 65537) {
// Check public exponent is 0x10001 big endian
if (E[0] != 0 || E[1] != 1 || E[2] != 0 || E[3] != 1) {
EPRINTF("Invalid public exponent."); EPRINTF("Invalid public exponent.");
goto dismount; goto dismount;
} }
if (!_test_key_pair(E, D, N)) { if (!_test_key_pair(rsa_keypair.public_exponent, rsa_keypair.private_exponent, rsa_keypair.modulus)) {
EPRINTF("Invalid keypair. Check eticket_rsa_kek."); EPRINTF("Invalid keypair. Check eticket_rsa_kek.");
goto dismount; goto dismount;
} }
se_rsa_key_set(0, N, 0x100, D, 0x100); se_rsa_key_set(0, rsa_keypair.modulus, sizeof(rsa_keypair.modulus), rsa_keypair.private_exponent, sizeof(rsa_keypair.private_exponent));
u64 br = buf_size; _get_titlekeys_from_save(buf_size, save_mac_key, titlekey_buffer, NULL);
u32 file_tkey_count = 0; _get_titlekeys_from_save(buf_size, save_mac_key, titlekey_buffer, &rsa_keypair);
u64 offset = 0;
rights_ids = (u8 *)(MIXD_BUF_ALIGNED + 0x40000);
titlekeys = (u8 *)(MIXD_BUF_ALIGNED + 0x80000);
save_ctx = calloc(1, sizeof(save_ctx_t));
u8 M[0x100];
if (f_open(&fp, "bis:/save/80000000000000E1", FA_READ | FA_OPEN_EXISTING)) {
EPRINTF("Unable to open e1 save. Skipping.");
goto dismount;
}
u32 pct = 0, last_pct = 0;
save_ctx->file = &fp;
save_ctx->action = 0;
memcpy(save_ctx->save_mac_key, save_mac_key, 0x10);
save_process_success = save_process(save_ctx);
if (!save_process_success) {
EPRINTF("Failed to process e1 save.");
f_close(&fp);
goto dismount;
}
char ticket_bin_path[0x40] = "/ticket.bin";
char ticket_list_bin_path[0x40] = "/ticket_list.bin";
save_data_file_ctx_t ticket_file;
if (!save_open_file(save_ctx, &ticket_file, ticket_list_bin_path, OPEN_MODE_READ)) {
EPRINTF("Unable to locate ticket_list.bin in e1.");
f_close(&fp);
goto dismount;
}
bool terminator_reached = false;
while (offset < ticket_file.size && !terminator_reached) {
if (!save_data_file_read(&ticket_file, &br, offset, buffer, buf_size) || buffer[0] == 0 || br != buf_size)
break;
offset += br;
minerva_periodic_training();
for (u32 j = 0; j < buf_size; j += 0x20) {
if (buffer[j] == 0xff && buffer[j+1] == 0xff && buffer[j+2] == 0xff && buffer[j+3] == 0xff) {
terminator_reached = true;
break;
}
file_tkey_count++;
}
}
if (!save_open_file(save_ctx, &ticket_file, ticket_bin_path, OPEN_MODE_READ)) {
EPRINTF("Unable to locate ticket.bin in e1 save.");
f_close(&fp);
goto dismount;
}
offset = 0;
terminator_reached = false;
while (offset < ticket_file.size && !terminator_reached) {
if (!save_data_file_read(&ticket_file, &br, offset, buffer, buf_size) || buffer[0] == 0 || br != buf_size)
break;
offset += br;
for (u32 j = 0; j < buf_size; j += 0x400) {
pct = _titlekey_count * 100 / file_tkey_count;
if (pct > last_pct && pct <= 100) {
last_pct = pct;
tui_pbar(save_x, save_y, pct, COLOR_GREEN, 0xFF155500);
}
minerva_periodic_training();
if (buffer[j] == 4 && buffer[j+1] == 0 && buffer[j+2] == 1 && buffer[j+3] == 0) {
memcpy(rights_ids + 0x10 * _titlekey_count, buffer + j + 0x2a0, 0x10);
memcpy(titlekeys + 0x10 * _titlekey_count, buffer + j + 0x180, 0x10);
_titlekey_count++;
} else {
terminator_reached = true;
break;
}
}
}
tui_pbar(save_x, save_y, 100, COLOR_GREEN, 0xFF155500);
f_close(&fp);
save_free_contexts(save_ctx);
save_process_success = false;
memset(save_ctx, 0, sizeof(save_ctx_t));
gfx_con_setpos(0, save_y);
TPRINTFARGS("\n%kCommon... ", colors[(color_idx++) % 6]);
save_x = gfx_con.x + 16 * 17;
save_y = gfx_con.y;
gfx_printf("\n%kPersonalized... ", colors[color_idx % 6]);
u32 common_titlekey_count = _titlekey_count;
if (f_open(&fp, "bis:/save/80000000000000E2", FA_READ | FA_OPEN_EXISTING)) {
EPRINTF("Unable to open e2 save. Skipping.");
goto dismount;
}
save_ctx->file = &fp;
save_ctx->action = 0;
memcpy(save_ctx->save_mac_key, save_mac_key, 0x10);
save_process_success = save_process(save_ctx);
if (!save_process_success) {
EPRINTF("Failed to process e2 save.");
f_close(&fp);
goto dismount;
}
if (!save_open_file(save_ctx, &ticket_file, ticket_list_bin_path, OPEN_MODE_READ)) {
EPRINTF("Unable to locate ticket_list.bin in e2 save.");
f_close(&fp);
goto dismount;
}
offset = 0;
file_tkey_count = 0;
terminator_reached = false;
while (offset < ticket_file.size && !terminator_reached) {
if (!save_data_file_read(&ticket_file, &br, offset, buffer, buf_size) || buffer[0] == 0 || br != buf_size)
break;
offset += br;
minerva_periodic_training();
for (u32 j = 0; j < buf_size; j += 0x20) {
if (buffer[j] == 0xff && buffer[j+1] == 0xff && buffer[j+2] == 0xff && buffer[j+3] == 0xff) {
terminator_reached = true;
break;
}
file_tkey_count++;
}
}
if (!save_open_file(save_ctx, &ticket_file, ticket_bin_path, OPEN_MODE_READ)) {
EPRINTF("Unable to locate ticket.bin in e2 save.");
f_close(&fp);
goto dismount;
}
offset = 0;
pct = 0;
last_pct = 0;
terminator_reached = false;
while (offset < ticket_file.size && !terminator_reached) {
if (!save_data_file_read(&ticket_file, &br, offset, buffer, buf_size) || buffer[0] == 0 || br != buf_size)
break;
offset += br;
for (u32 j = 0; j < buf_size; j += 0x400) {
pct = (_titlekey_count - common_titlekey_count) * 100 / file_tkey_count;
if (pct > last_pct && pct <= 100) {
last_pct = pct;
tui_pbar(save_x, save_y, pct, COLOR_GREEN, 0xFF155500);
}
minerva_periodic_training();
if (buffer[j] == 4 && buffer[j+1] == 0 && buffer[j+2] == 1 && buffer[j+3] == 0) {
memcpy(rights_ids + 0x10 * _titlekey_count, buffer + j + 0x2a0, 0x10);
u8 *titlekey_block = buffer + j + 0x180;
se_rsa_exp_mod(0, M, 0x100, titlekey_block, 0x100);
u8 *salt = M + 1;
u8 *db = M + 0x21;
_mgf1_xor(salt, 0x20, db, 0xdf);
_mgf1_xor(db, 0xdf, salt, 0x20);
if (memcmp(db, null_hash, 0x20) != 0)
continue;
memcpy(titlekeys + 0x10 * _titlekey_count, db + 0xcf, 0x10);
_titlekey_count++;
} else {
terminator_reached = true;
break;
}
}
}
tui_pbar(save_x, save_y, 100, COLOR_GREEN, 0xFF155500);
f_close(&fp);
gfx_con_setpos(0, save_y);
TPRINTFARGS("\n%kPersonalized... ", colors[(color_idx++) % 6]);
gfx_printf("\n%k Found %d titlekeys.\n", colors[(color_idx++) % 6], _titlekey_count); gfx_printf("\n%k Found %d titlekeys.\n", colors[(color_idx++) % 6], _titlekey_count);
dismount: ; dismount: ;
if (save_process_success)
save_free_contexts(save_ctx);
if (save_ctx)
free(save_ctx);
f_mount(NULL, "bis:", 1); f_mount(NULL, "bis:", 1);
nx_emmc_gpt_free(&gpt); nx_emmc_gpt_free(&gpt);
@ -780,70 +703,76 @@ key_output: ;
EPRINTF("Unable to mount SD."); EPRINTF("Unable to mount SD.");
goto free_buffers; goto free_buffers;
} }
u32 text_buffer_size = MAX(_titlekey_count * 68 + 1, 0x4000);
typedef struct {
char rights_id[0x20];
char equals[3];
char titlekey[0x20];
char newline[1];
} titlekey_text_buffer_t;
u32 text_buffer_size = MAX(_titlekey_count * sizeof(titlekey_text_buffer_t) + 1, 0x4000);
text_buffer = (char *)calloc(1, text_buffer_size); text_buffer = (char *)calloc(1, text_buffer_size);
SAVE_KEY("aes_kek_generation_source", aes_kek_generation_source, 0x10); SAVE_KEY(aes_kek_generation_source);
SAVE_KEY("aes_key_generation_source", aes_key_generation_source, 0x10); SAVE_KEY(aes_key_generation_source);
SAVE_KEY("bis_kek_source", bis_kek_source, 0x10); SAVE_KEY(bis_kek_source);
SAVE_KEY_FAMILY("bis_key", bis_key, 0, 4, 0x20); SAVE_KEY_FAMILY(bis_key, 0);
SAVE_KEY_FAMILY("bis_key_source", bis_key_source, 0, 3, 0x20); SAVE_KEY_FAMILY(bis_key_source, 0);
SAVE_KEY("device_key", device_key, 0x10); SAVE_KEY(device_key);
SAVE_KEY("device_key_4x", device_key_4x, 0x10); SAVE_KEY(device_key_4x);
SAVE_KEY("eticket_rsa_kek", eticket_rsa_kek, 0x10); SAVE_KEY(eticket_rsa_kek);
SAVE_KEY("eticket_rsa_kek_personalized", eticket_rsa_kek_personalized, 0x10); SAVE_KEY(eticket_rsa_kek_personalized);
SAVE_KEY("eticket_rsa_kek_source", es_keys[0], 0x10); SAVE_KEY(eticket_rsa_kek_source);
SAVE_KEY("eticket_rsa_kekek_source", es_keys[1], 0x10); SAVE_KEY(eticket_rsa_kekek_source);
SAVE_KEY("header_kek_source", fs_keys[FS_HEADER_KEK_SOURCE], 0x10); SAVE_KEY(header_kek_source);
SAVE_KEY("header_key", header_key, 0x20); SAVE_KEY(header_key);
SAVE_KEY("header_key_source", fs_keys[FS_HEADER_KEY_SOURCE], 0x20); SAVE_KEY(header_key_source);
SAVE_KEY_FAMILY("key_area_key_application", key_area_key[0], 0, derivable_key_count, 0x10); SAVE_KEY_FAMILY_VAR(key_area_key_application, key_area_key[0], 0);
SAVE_KEY("key_area_key_application_source", fs_keys[FS_KEY_AREA_KEY_APPLI_SOURCE], 0x10); SAVE_KEY_VAR(key_area_key_application_source, key_area_key_sources[0]);
SAVE_KEY_FAMILY("key_area_key_ocean", key_area_key[1], 0, derivable_key_count, 0x10); SAVE_KEY_FAMILY_VAR(key_area_key_ocean, key_area_key[1], 0);
SAVE_KEY("key_area_key_ocean_source", fs_keys[FS_KEY_AREA_KEY_OCEAN_SOURCE], 0x10); SAVE_KEY_VAR(key_area_key_ocean_source, key_area_key_sources[1]);
SAVE_KEY_FAMILY("key_area_key_system", key_area_key[2], 0, derivable_key_count, 0x10); SAVE_KEY_FAMILY_VAR(key_area_key_system, key_area_key[2], 0);
SAVE_KEY("key_area_key_system_source", fs_keys[FS_KEY_AREA_KEY_SYSTE_SOURCE], 0x10); SAVE_KEY_VAR(key_area_key_system_source, key_area_key_sources[2]);
SAVE_KEY_FAMILY("keyblob", keyblob, 0, 6, 0x90); SAVE_KEY_FAMILY(keyblob, 0);
SAVE_KEY_FAMILY("keyblob_key", keyblob_key, 0, 6, 0x10); SAVE_KEY_FAMILY(keyblob_key, 0);
SAVE_KEY_FAMILY("keyblob_key_source", keyblob_key_source, 0, 6, 0x10); SAVE_KEY_FAMILY(keyblob_key_source, 0);
SAVE_KEY_FAMILY("keyblob_mac_key", keyblob_mac_key, 0, 6, 0x10); SAVE_KEY_FAMILY(keyblob_mac_key, 0);
SAVE_KEY("keyblob_mac_key_source", keyblob_mac_key_source, 0x10); SAVE_KEY(keyblob_mac_key_source);
SAVE_KEY_FAMILY("master_kek", master_kek, 0, derivable_key_count, 0x10); SAVE_KEY_FAMILY(master_kek, 0);
SAVE_KEY_FAMILY("master_kek_source", master_kek_sources, KB_FIRMWARE_VERSION_620, sizeof(master_kek_sources) / 0x10, 0x10); SAVE_KEY_FAMILY_VAR(master_kek_source, master_kek_sources, KB_FIRMWARE_VERSION_620);
SAVE_KEY_FAMILY("master_key", master_key, 0, derivable_key_count, 0x10); SAVE_KEY_FAMILY(master_key, 0);
SAVE_KEY("master_key_source", master_key_source, 0x10); SAVE_KEY(master_key_source);
SAVE_KEY_FAMILY("package1_key", package1_key, 0, 6, 0x10); SAVE_KEY_FAMILY(package1_key, 0);
SAVE_KEY_FAMILY("package2_key", package2_key, 0, derivable_key_count, 0x10); SAVE_KEY_FAMILY(package2_key, 0);
SAVE_KEY("package2_key_source", package2_key_source, 0x10); SAVE_KEY(package2_key_source);
SAVE_KEY("per_console_key_source", per_console_key_source, 0x10); SAVE_KEY(per_console_key_source);
SAVE_KEY("retail_specific_aes_key_source", retail_specific_aes_key_source, 0x10); SAVE_KEY(retail_specific_aes_key_source);
for (u32 i = 0; i < 0x10; i++) for (u32 i = 0; i < 0x10; i++)
temp_key[i] = aes_kek_generation_source[i] ^ aes_kek_seed_03[i]; temp_key[i] = aes_kek_generation_source[i] ^ aes_kek_seed_03[i];
SAVE_KEY("rsa_oaep_kek_generation_source", temp_key, 0x10); SAVE_KEY_VAR(rsa_oaep_kek_generation_source, temp_key);
for (u32 i = 0; i < 0x10; i++) for (u32 i = 0; i < 0x10; i++)
temp_key[i] = aes_kek_generation_source[i] ^ aes_kek_seed_01[i]; temp_key[i] = aes_kek_generation_source[i] ^ aes_kek_seed_01[i];
SAVE_KEY("rsa_private_kek_generation_source", temp_key, 0x10); SAVE_KEY_VAR(rsa_private_kek_generation_source, temp_key);
SAVE_KEY("save_mac_kek_source", fs_keys[FS_SAVE_MAC_KEK_SOURCE], 0x10); SAVE_KEY(save_mac_kek_source);
SAVE_KEY("save_mac_key", save_mac_key, 0x10); SAVE_KEY(save_mac_key);
SAVE_KEY("save_mac_key_source", fs_keys[FS_SAVE_MAC_KEY_SOURCE], 0x10); SAVE_KEY(save_mac_key_source);
SAVE_KEY("save_mac_sd_card_kek_source", fs_keys[FS_SAVE_MAC_SD_KEK_SOURCE], 0x10); SAVE_KEY(save_mac_sd_card_kek_source);
SAVE_KEY("save_mac_sd_card_key_source", fs_keys[FS_SAVE_MAC_SD_KEY_SOURCE], 0x10); SAVE_KEY(save_mac_sd_card_key_source);
SAVE_KEY("sd_card_custom_storage_key_source", fs_keys[FS_SD_CUSTOM_KEY_SOURCE], 0x20); SAVE_KEY(sd_card_custom_storage_key_source);
SAVE_KEY("sd_card_kek_source", fs_keys[FS_SD_KEK_SOURCE], 0x10); SAVE_KEY(sd_card_kek_source);
SAVE_KEY("sd_card_nca_key_source", fs_keys[FS_SD_NCA_KEY_SOURCE], 0x20); SAVE_KEY(sd_card_nca_key_source);
SAVE_KEY("sd_card_save_key_source", fs_keys[FS_SD_SAVE_KEY_SOURCE], 0x20); SAVE_KEY(sd_card_save_key_source);
SAVE_KEY("sd_seed", sd_seed, 0x10); SAVE_KEY(sd_seed);
SAVE_KEY("secure_boot_key", sbk, 0x10); SAVE_KEY_VAR(secure_boot_key, sbk);
SAVE_KEY("ssl_rsa_kek", ssl_rsa_kek, 0x10); SAVE_KEY(ssl_rsa_kek);
SAVE_KEY("ssl_rsa_kek_source_x", es_keys[2], 0x10); SAVE_KEY(ssl_rsa_kek_source_x);
SAVE_KEY("ssl_rsa_kek_source_y", ssl_keys, 0x10); SAVE_KEY(ssl_rsa_kek_source_y);
SAVE_KEY_FAMILY("titlekek", titlekek, 0, derivable_key_count, 0x10); SAVE_KEY_FAMILY(titlekek, 0);
SAVE_KEY("titlekek_source", titlekek_source, 0x10); SAVE_KEY(titlekek_source);
SAVE_KEY("tsec_key", tsec_keys, 0x10); _save_key("tsec_key", tsec_keys, 0x10, text_buffer);
if (pkg1_id->kb == KB_FIRMWARE_VERSION_620) if (pkg1_id->kb == KB_FIRMWARE_VERSION_620)
SAVE_KEY("tsec_root_key", tsec_keys + 0x10, 0x10); _save_key("tsec_root_key", tsec_keys + 0x10, 0x10, text_buffer);
//gfx_con.fntsz = 8; gfx_puts(text_buffer); gfx_con.fntsz = 16;
end_time = get_tmr_us(); end_time = get_tmr_us();
gfx_printf("\n%k Found %d keys.\n\n", colors[(color_idx++) % 6], _key_count); gfx_printf("\n%k Found %d keys.\n\n", colors[(color_idx++) % 6], _key_count);
@ -864,13 +793,16 @@ key_output: ;
if (_titlekey_count == 0) if (_titlekey_count == 0)
goto free_buffers; goto free_buffers;
memset(text_buffer, 0, text_buffer_size); memset(text_buffer, 0, text_buffer_size);
titlekey_text_buffer_t *titlekey_text = (titlekey_text_buffer_t *)text_buffer;
for (u32 i = 0; i < _titlekey_count; i++) { for (u32 i = 0; i < _titlekey_count; i++) {
for (u32 j = 0; j < 0x10; j++) for (u32 j = 0; j < 0x10; j++)
sprintf(&text_buffer[i * 68 + j * 2], "%02x", rights_ids[i * 0x10 + j]); sprintf(&titlekey_text[i].rights_id[j * 2], "%02x", titlekey_buffer->rights_ids[i][j]);
sprintf(&text_buffer[i * 68 + 0x20], " = "); sprintf(titlekey_text[i].equals, " = ");
for (u32 j = 0; j < 0x10; j++) for (u32 j = 0; j < 0x10; j++)
sprintf(&text_buffer[i * 68 + 0x23 + j * 2], "%02x", titlekeys[i * 0x10 + j]); sprintf(&titlekey_text[i].titlekey[j * 2], "%02x", titlekey_buffer->titlekeys[i][j]);
sprintf(&text_buffer[i * 68 + 0x43], "\n"); sprintf(titlekey_text[i].newline, "\n");
} }
sprintf(&keyfile_path[11], "title.keys"); sprintf(&keyfile_path[11], "title.keys");
if (!sd_save_to_file(text_buffer, strlen(text_buffer), keyfile_path) && !f_stat(keyfile_path, &fno)) { if (!sd_save_to_file(text_buffer, strlen(text_buffer), keyfile_path) && !f_stat(keyfile_path, &fno)) {
@ -935,44 +867,17 @@ static void _get_device_key(u32 ks, void *out_device_key, u32 revision, const vo
se_aes_crypt_ecb(ks, 0, out_device_key, 0x10, temp_key, 0x10); se_aes_crypt_ecb(ks, 0, out_device_key, 0x10, temp_key, 0x10);
} }
static bool _test_key_pair(const void *E, const void *D, const void *N) { static bool _test_key_pair(const void *public_exponent, const void *private_exponent, const void *modulus) {
u8 X[0x100] = {0}, Y[0x100] = {0}, Z[0x100] = {0}; u8 plaintext[0x100] = {0}, ciphertext[0x100] = {0}, work[0x100] = {0};
// 0xCAFEBABE // 0xCAFEBABE
X[0xfc] = 0xca; X[0xfd] = 0xfe; X[0xfe] = 0xba; X[0xff] = 0xbe; plaintext[0xfc] = 0xca; plaintext[0xfd] = 0xfe; plaintext[0xfe] = 0xba; plaintext[0xff] = 0xbe;
se_rsa_key_set(0, N, 0x100, D, 0x100);
se_rsa_exp_mod(0, Y, 0x100, X, 0x100);
se_rsa_key_set(0, N, 0x100, E, 4);
se_rsa_exp_mod(0, Z, 0x100, Y, 0x100);
return !memcmp(X, Z, 0x100); se_rsa_key_set(0, modulus, 0x100, private_exponent, 0x100);
} se_rsa_exp_mod(0, ciphertext, 0x100, plaintext, 0x100);
// _mgf1_xor() was derived from Atmosphère's calculate_mgf1_and_xor se_rsa_key_set(0, modulus, 0x100, public_exponent, 4);
static void _mgf1_xor(void *masked, u32 masked_size, const void *seed, u32 seed_size) { se_rsa_exp_mod(0, work, 0x100, ciphertext, 0x100);
u8 cur_hash[0x20];
u8 hash_buf[0xe4]; return !memcmp(plaintext, work, 0x100);
u32 hash_buf_size = seed_size + 4;
memcpy(hash_buf, seed, seed_size);
u32 round_num = 0;
u8 *p_out = (u8 *)masked;
while (masked_size) {
u32 cur_size = MIN(masked_size, 0x20);
for (u32 i = 0; i < 4; i++)
hash_buf[seed_size + 3 - i] = (round_num >> (8 * i)) & 0xff;
round_num++;
se_calc_sha256(cur_hash, hash_buf, hash_buf_size);
for (unsigned int i = 0; i < cur_size; i++) {
*p_out ^= cur_hash[i];
p_out++;
}
masked_size -= cur_size;
}
} }

View file

@ -17,6 +17,90 @@
#ifndef _KEYS_H_ #ifndef _KEYS_H_
#define _KEYS_H_ #define _KEYS_H_
#include <utils/types.h>
// only tickets of type Rsa2048Sha256 are expected
typedef struct {
u32 signature_type; // always 0x10004
u8 signature[0x100];
u8 sig_padding[0x3C];
char issuer[0x40];
u8 titlekey_block[0x100];
u8 format_version;
u8 titlekey_type;
u16 ticket_version;
u8 license_type;
u8 common_key_id;
u16 property_mask;
u64 reserved;
u64 ticket_id;
u64 device_id;
u8 rights_id[0x10];
u32 account_id;
u32 sect_total_size;
u32 sect_hdr_offset;
u16 sect_hdr_count;
u16 sect_hdr_entry_size;
u8 padding[0x140];
} ticket_t;
typedef struct {
u8 rights_id[0x10];
u64 ticket_id;
u32 account_id;
u16 property_mask;
u16 reserved;
} ticket_record_t;
typedef struct {
u8 read_buffer[0x40000];
u8 rights_ids[0x40000 / 0x10][0x10];
u8 titlekeys[0x40000 / 0x10][0x10];
} titlekey_buffer_t;
typedef struct {
u8 private_exponent[0x100];
u8 modulus[0x100];
u8 public_exponent[4];
u8 reserved[0x14];
u64 device_id;
u8 gmac[0x10];
} rsa_keypair_t;
typedef struct {
u8 master_kek[0x10];
u8 data[0x70];
u8 package1_key[0x10];
} keyblob_t;
typedef struct {
u8 cmac[0x10];
u8 iv[0x10];
keyblob_t key_data;
u8 unused[0x150];
} encrypted_keyblob_t;
#define TPRINTF(text) \
end_time = get_tmr_us(); \
gfx_printf(text" done in %d us\n", end_time - start_time); \
start_time = get_tmr_us(); \
minerva_periodic_training()
#define TPRINTFARGS(text, args...) \
end_time = get_tmr_us(); \
gfx_printf(text" done in %d us\n", args, end_time - start_time); \
start_time = get_tmr_us(); \
minerva_periodic_training()
// save key wrapper
#define SAVE_KEY(name) _save_key(#name, name, sizeof(name), text_buffer)
// save key with different name than variable
#define SAVE_KEY_VAR(name, varname) _save_key(#name, varname, sizeof(varname), text_buffer)
// save key family wrapper
#define SAVE_KEY_FAMILY(name, start) _save_key_family(#name, name, start, sizeof(name) / sizeof(name[0]), sizeof(name[0]), text_buffer)
// save key family with different name than variable
#define SAVE_KEY_FAMILY_VAR(name, varname, start) _save_key_family(#name, varname, start, sizeof(varname) / sizeof(varname[0]), sizeof(varname[0]), text_buffer)
void dump_keys(); void dump_keys();
#endif #endif

View file

@ -24,12 +24,12 @@
#include <mem/heap.h> #include <mem/heap.h>
#include <sec/se.h> #include <sec/se.h>
#include "../storage/nx_emmc.h" #include "../storage/nx_emmc.h"
#include "nx_emmc_bis.h"
#include <storage/sdmmc.h> #include <storage/sdmmc.h>
#include <utils/types.h> #include <utils/types.h>
#define MAX_CLUSTER_CACHE_ENTRIES 32768 #define MAX_CLUSTER_CACHE_ENTRIES 32768
#define CLUSTER_LOOKUP_EMPTY_ENTRY 0xFFFFFFFF #define CLUSTER_LOOKUP_EMPTY_ENTRY 0xFFFFFFFF
#define XTS_CLUSTER_SIZE 0x4000
#define SECTORS_PER_CLUSTER 0x20 #define SECTORS_PER_CLUSTER 0x20
typedef struct typedef struct

View file

@ -163,7 +163,8 @@ typedef struct _nx_emmc_cal0_t
u8 crc16_pad36[0x10]; u8 crc16_pad36[0x10];
u8 ext_ecc_b233_eticket_key[0x50]; u8 ext_ecc_b233_eticket_key[0x50];
u8 crc16_pad37[0x10]; u8 crc16_pad37[0x10];
u8 ext_ecc_rsa2048_eticket_key[0x240]; u8 ext_ecc_rsa2048_eticket_key_iv[0x10];
u8 ext_ecc_rsa2048_eticket_key[0x230];
u32 ext_ecc_rsa2048_eticket_key_ver; u32 ext_ecc_rsa2048_eticket_key_ver;
u8 crc16_pad38[0xC]; u8 crc16_pad38[0xC];
u8 ext_ssl_key[0x130]; u8 ext_ssl_key[0x130];
@ -226,6 +227,7 @@ typedef struct _nx_emmc_cal0_t
#define NX_EMMC_CALIBRATION_OFFSET 0x4400 #define NX_EMMC_CALIBRATION_OFFSET 0x4400
#define NX_EMMC_CALIBRATION_SIZE 0x8000 #define NX_EMMC_CALIBRATION_SIZE 0x8000
#define XTS_CLUSTER_SIZE 0x4000
int nx_emmc_bis_read(u32 sector, u32 count, void *buff); int nx_emmc_bis_read(u32 sector, u32 count, void *buff);
int nx_emmc_bis_write(u32 sector, u32 count, void *buff); int nx_emmc_bis_write(u32 sector, u32 count, void *buff);