hactool/pki.c
2020-06-01 17:14:12 -07:00

610 lines
37 KiB
C

#include <stdio.h>
#include <string.h>
#include "aes.h"
#include "pki.h"
/* Keydata for very early beta NCA0 archives' RSA-OAEP. */
static const unsigned char beta_nca0_modulus[0x100] = {
0xAD, 0x58, 0xEE, 0x97, 0xF9, 0x47, 0x90, 0x7D, 0xF9, 0x29, 0x5F, 0x1F, 0x39, 0x68, 0xEE, 0x49,
0x4C, 0x1E, 0x8D, 0x84, 0x91, 0x31, 0x5D, 0xE5, 0x96, 0x27, 0xB2, 0xB3, 0x59, 0x7B, 0xDE, 0xFD,
0xB7, 0xEB, 0x40, 0xA1, 0xE7, 0xEB, 0xDC, 0x60, 0xD0, 0x3D, 0xC5, 0x50, 0x92, 0xAD, 0x3D, 0xC4,
0x8C, 0x17, 0xD2, 0x37, 0x66, 0xE3, 0xF7, 0x14, 0x34, 0x38, 0x6B, 0xA7, 0x2B, 0x21, 0x10, 0x9B,
0x73, 0x49, 0x15, 0xD9, 0x2A, 0x90, 0x86, 0x76, 0x81, 0x6A, 0x10, 0xBD, 0x74, 0xC4, 0x20, 0x55,
0x25, 0xA8, 0x02, 0xC5, 0xA0, 0x34, 0x36, 0x7B, 0x66, 0x47, 0x2C, 0x7E, 0x47, 0x82, 0xA5, 0xD4,
0xA3, 0x42, 0x45, 0xE8, 0xFD, 0x65, 0x72, 0x48, 0xA1, 0xB0, 0x44, 0x10, 0xEF, 0xAC, 0x1D, 0x0F,
0xB5, 0x12, 0x19, 0xA8, 0x41, 0x0B, 0x76, 0x3B, 0xBC, 0xF1, 0x4A, 0x10, 0x46, 0x22, 0xB8, 0xF1,
0xBC, 0x21, 0x81, 0x69, 0x9B, 0x63, 0x6F, 0xD7, 0xB9, 0x60, 0x2A, 0x9A, 0xE5, 0x2C, 0x47, 0x72,
0x59, 0x65, 0xA2, 0x21, 0x60, 0xC4, 0xFC, 0xB0, 0xD7, 0x6F, 0x42, 0xC9, 0x0C, 0xF5, 0x76, 0x7D,
0xF2, 0x5C, 0xE0, 0x80, 0x0F, 0xEE, 0x45, 0x7E, 0x4E, 0x3A, 0x8D, 0x9C, 0x5B, 0x5B, 0xD9, 0xD1,
0x43, 0x94, 0x2C, 0xC7, 0x2E, 0xB9, 0x4A, 0xE5, 0x3E, 0x15, 0xDD, 0x43, 0x00, 0xF7, 0x78, 0xE7,
0x7C, 0x39, 0xB0, 0x4D, 0xC5, 0xD1, 0x1C, 0xF2, 0xB4, 0x7A, 0x2A, 0xEA, 0x0A, 0x8E, 0xB9, 0x13,
0xB4, 0x4F, 0xD7, 0x5B, 0x4D, 0x7B, 0x43, 0xB0, 0x3A, 0x9A, 0x60, 0x22, 0x47, 0x91, 0x78, 0xC7,
0x10, 0x64, 0xE0, 0x2C, 0x69, 0xD1, 0x66, 0x3C, 0x42, 0x2E, 0xEF, 0x19, 0x21, 0x89, 0x8E, 0xE1,
0xB0, 0xB4, 0xD0, 0x17, 0xA1, 0x0F, 0x73, 0x98, 0x5A, 0xF6, 0xEE, 0xC0, 0x2F, 0x9E, 0xCE, 0xC5
};
static unsigned char beta_nca0_exponent[0x100] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
};
static const unsigned char beta_nca0_label_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
};
const unsigned char *pki_get_beta_nca0_modulus(void) {
return beta_nca0_modulus;
}
void pki_set_beta_nca0_exponent(void *exponent) {
memcpy(beta_nca0_exponent, exponent, sizeof(beta_nca0_exponent));
}
const unsigned char *pki_get_beta_nca0_exponent(void) {
return beta_nca0_exponent;
}
const unsigned char *pki_get_beta_nca0_label_hash(void) {
return beta_nca0_label_hash;
}
static const nca_keyset_t nca_keys_retail = {
.nca_hdr_fixed_key_moduli = { /* Fixed RSA key used to validate NCA signature 0. */
{
0xBF, 0xBE, 0x40, 0x6C, 0xF4, 0xA7, 0x80, 0xE9, 0xF0, 0x7D, 0x0C, 0x99, 0x61, 0x1D, 0x77, 0x2F,
0x96, 0xBC, 0x4B, 0x9E, 0x58, 0x38, 0x1B, 0x03, 0xAB, 0xB1, 0x75, 0x49, 0x9F, 0x2B, 0x4D, 0x58,
0x34, 0xB0, 0x05, 0xA3, 0x75, 0x22, 0xBE, 0x1A, 0x3F, 0x03, 0x73, 0xAC, 0x70, 0x68, 0xD1, 0x16,
0xB9, 0x04, 0x46, 0x5E, 0xB7, 0x07, 0x91, 0x2F, 0x07, 0x8B, 0x26, 0xDE, 0xF6, 0x00, 0x07, 0xB2,
0xB4, 0x51, 0xF8, 0x0D, 0x0A, 0x5E, 0x58, 0xAD, 0xEB, 0xBC, 0x9A, 0xD6, 0x49, 0xB9, 0x64, 0xEF,
0xA7, 0x82, 0xB5, 0xCF, 0x6D, 0x70, 0x13, 0xB0, 0x0F, 0x85, 0xF6, 0xA9, 0x08, 0xAA, 0x4D, 0x67,
0x66, 0x87, 0xFA, 0x89, 0xFF, 0x75, 0x90, 0x18, 0x1E, 0x6B, 0x3D, 0xE9, 0x8A, 0x68, 0xC9, 0x26,
0x04, 0xD9, 0x80, 0xCE, 0x3F, 0x5E, 0x92, 0xCE, 0x01, 0xFF, 0x06, 0x3B, 0xF2, 0xC1, 0xA9, 0x0C,
0xCE, 0x02, 0x6F, 0x16, 0xBC, 0x92, 0x42, 0x0A,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98, 0x1F, 0x0D, 0xF8, 0xB3, 0x34, 0xE6, 0x98, 0x68, 0x5A, 0xAD,
}
},
.acid_fixed_key_moduli = { /* Fixed RSA keys used to validate ACID signatures. */
{
0xDD, 0xC8, 0xDD, 0xF2, 0x4E, 0x6D, 0xF0, 0xCA, 0x9E, 0xC7, 0x5D, 0xC7, 0x7B, 0xAD, 0xFE, 0x7D,
0x23, 0x89, 0x69, 0xB6, 0xF2, 0x06, 0xA2, 0x02, 0x88, 0xE1, 0x55, 0x91, 0xAB, 0xCB, 0x4D, 0x50,
0x2E, 0xFC, 0x9D, 0x94, 0x76, 0xD6, 0x4C, 0xD8, 0xFF, 0x10, 0xFA, 0x5E, 0x93, 0x0A, 0xB4, 0x57,
0xAC, 0x51, 0xC7, 0x16, 0x66, 0xF4, 0x1A, 0x54, 0xC2, 0xC5, 0x04, 0x3D, 0x1B, 0xFE, 0x30, 0x20,
0x8A, 0xAC, 0x6F, 0x6F, 0xF5, 0xC7, 0xB6, 0x68, 0xB8, 0xC9, 0x40, 0x6B, 0x42, 0xAD, 0x11, 0x21,
0xE7, 0x8B, 0xE9, 0x75, 0x01, 0x86, 0xE4, 0x48, 0x9B, 0x0A, 0x0A, 0xF8, 0x7F, 0xE8, 0x87, 0xF2,
0x82, 0x01, 0xE6, 0xA3, 0x0F, 0xE4, 0x66, 0xAE, 0x83, 0x3F, 0x4E, 0x9F, 0x5E, 0x01, 0x30, 0xA4,
0x00, 0xB9, 0x9A, 0xAE, 0x5F, 0x03, 0xCC, 0x18, 0x60, 0xE5, 0xEF, 0x3B, 0x5E, 0x15, 0x16, 0xFE,
0x1C, 0x82, 0x78, 0xB5, 0x2F, 0x47, 0x7C, 0x06, 0x66, 0x88, 0x5D, 0x35, 0xA2, 0x67, 0x20, 0x10,
0xE7, 0x6C, 0x43, 0x68, 0xD3, 0xE4, 0x5A, 0x68, 0x2A, 0x5A, 0xE2, 0x6D, 0x73, 0xB0, 0x31, 0x53,
0x1C, 0x20, 0x09, 0x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, 0xAC, 0xA5, 0x7B, 0xDB, 0x42, 0x5D,
0x59, 0x35, 0x45, 0x5D, 0x8A, 0x02, 0xB5, 0x70, 0xC0, 0x72, 0x35, 0x46, 0xD0, 0x1D, 0x60, 0x01,
0x4A, 0xCC, 0x1C, 0x46, 0xD3, 0xD6, 0x35, 0x52, 0xD6, 0xE1, 0xF8, 0x3B, 0x5D, 0xEA, 0xDD, 0xB8,
0xFE, 0x7D, 0x50, 0xCB, 0x35, 0x23, 0x67, 0x8B, 0xB6, 0xE4, 0x74, 0xD2, 0x60, 0xFC, 0xFD, 0x43,
0xBF, 0x91, 0x08, 0x81, 0xC5, 0x4F, 0x5D, 0x16, 0x9A, 0xC4, 0x9A, 0xC6, 0xF6, 0xF3, 0xE1, 0xF6,
0x5C, 0x07, 0xAA, 0x71, 0x6C, 0x13, 0xA4, 0xB1, 0xB3, 0x66, 0xBF, 0x90, 0x4C, 0x3D, 0xA2, 0xC4,
0x0B, 0xB8, 0x3D, 0x7A, 0x8C, 0x19, 0xFA, 0xFF, 0x6B, 0xB9, 0x1F, 0x02, 0xCC, 0xB6, 0xD3, 0x0C,
0x7D, 0x19, 0x1F, 0x47, 0xF9, 0xC7, 0x40, 0x01, 0xFA, 0x46, 0xEA, 0x0B, 0xD4, 0x02, 0xE0, 0x3D,
0x30, 0x9A, 0x1A, 0x0F, 0xEA, 0xA7, 0x66, 0x55, 0xF7, 0xCB, 0x28, 0xE2, 0xBB, 0x99, 0xE4, 0x83,
0xC3, 0x43, 0x03, 0xEE, 0xDC, 0x1F, 0x02, 0x23, 0xDD, 0xD1, 0x2D, 0x39, 0xA4, 0x65, 0x75, 0x03,
0xEF, 0x37, 0x9C, 0x06, 0xD6, 0xFA, 0xA1, 0x15, 0xF0, 0xDB, 0x17, 0x47, 0x26, 0x4F, 0x49, 0x03
}
},
.package2_fixed_key_modulus = { /* Fixed RSA key used to validate PK21 signatures. */
0x8D, 0x13, 0xA7, 0x77, 0x6A, 0xE5, 0xDC, 0xC0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, 0x47,
0xFD, 0xFA, 0x4C, 0x6C, 0x6F, 0x81, 0x87, 0x3B, 0xCA, 0xB8, 0x50, 0xF6, 0x3E, 0x39, 0x5D, 0x4D,
0x97, 0x3F, 0x0F, 0x35, 0x39, 0x53, 0xFB, 0xFA, 0xCD, 0xAB, 0xA8, 0x7A, 0x62, 0x9A, 0x3F, 0xF2,
0x09, 0x27, 0x96, 0x3F, 0x07, 0x9A, 0x91, 0xF7, 0x16, 0xBF, 0xC6, 0x3A, 0x82, 0x5A, 0x4B, 0xCF,
0x49, 0x50, 0x95, 0x8C, 0x55, 0x80, 0x7E, 0x39, 0xB1, 0x48, 0x05, 0x1E, 0x21, 0xC7, 0x24, 0x4F
}
};
static const nca_keyset_t nca_keys_dev = {
.nca_hdr_fixed_key_moduli = { /* Fixed RSA key used to validate NCA signature 0. */
{
0xD8, 0xF1, 0x18, 0xEF, 0x32, 0x72, 0x4C, 0xA7, 0x47, 0x4C, 0xB9, 0xEA, 0xB3, 0x04, 0xA8, 0xA4,
0xAC, 0x99, 0x08, 0x08, 0x04, 0xBF, 0x68, 0x57, 0xB8, 0x43, 0x94, 0x2B, 0xC7, 0xB9, 0x66, 0x49,
0x85, 0xE5, 0x8A, 0x9B, 0xC1, 0x00, 0x9A, 0x6A, 0x8D, 0xD0, 0xEF, 0xCE, 0xFF, 0x86, 0xC8, 0x5C,
0x5D, 0xE9, 0x53, 0x7B, 0x19, 0x2A, 0xA8, 0xC0, 0x22, 0xD1, 0xF3, 0x22, 0x0A, 0x50, 0xF2, 0x2B,
0x65, 0x05, 0x1B, 0x9E, 0xEC, 0x61, 0xB5, 0x63, 0xA3, 0x6F, 0x3B, 0xBA, 0x63, 0x3A, 0x53, 0xF4,
0x49, 0x2F, 0xCF, 0x03, 0xCC, 0xD7, 0x50, 0x82, 0x1B, 0x29, 0x4F, 0x08, 0xDE, 0x1B, 0x6D, 0x47,
0x4F, 0xA8, 0xB6, 0x6A, 0x26, 0xA0, 0x83, 0x3F, 0x1A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A3, 0xBE, 0x10, 0x06, 0x02, 0xE9, 0xFB, 0x85, 0xFA, 0x5F, 0x01,
0xAC, 0x60, 0xE0, 0xED, 0x7D, 0xB9, 0x49, 0xA8, 0x9E, 0x98, 0x7D, 0x91, 0x40, 0x05, 0xCF, 0xF9,
0x1A, 0xFC, 0x40, 0x22, 0xA8, 0x96, 0x5B, 0xB0, 0xDC, 0x7A, 0xF5, 0xB7, 0xE9, 0x91, 0x4C, 0x49,
}
},
.acid_fixed_key_moduli = { /* Fixed RSA keys used to validate ACID signatures. */
{
0xD6, 0x34, 0xA5, 0x78, 0x6C, 0x68, 0xCE, 0x5A, 0xC2, 0x37, 0x17, 0xF3, 0x82, 0x45, 0xC6, 0x89,
0xE1, 0x2D, 0x06, 0x67, 0xBF, 0xB4, 0x06, 0x19, 0x55, 0x6B, 0x27, 0x66, 0x0C, 0xA4, 0xB5, 0x87,
0x81, 0x25, 0xF4, 0x30, 0xBC, 0x53, 0x08, 0x68, 0xA2, 0x48, 0x49, 0x8C, 0x3F, 0x38, 0x40, 0x9C,
0xC4, 0x26, 0xF4, 0x79, 0xE2, 0xA1, 0x85, 0xF5, 0x5C, 0x7F, 0x58, 0xBA, 0xA6, 0x1C, 0xA0, 0x8B,
0x84, 0x16, 0x14, 0x6F, 0x85, 0xD9, 0x7C, 0xE1, 0x3C, 0x67, 0x22, 0x1E, 0xFB, 0xD8, 0xA7, 0xA5,
0x9A, 0xBF, 0xEC, 0x0E, 0xCF, 0x96, 0x7E, 0x85, 0xC2, 0x1D, 0x49, 0x5D, 0x54, 0x26, 0xCB, 0x32,
0x7C, 0xF6, 0xBB, 0x58, 0x03, 0x80, 0x2B, 0x5D, 0xF7, 0xFB, 0xD1, 0x9D, 0xC7, 0xC6, 0x2E, 0x53,
0xC0, 0x6F, 0x39, 0x2C, 0x1F, 0xA9, 0x92, 0xF2, 0x4D, 0x7D, 0x4E, 0x74, 0xFF, 0xE4, 0xEF, 0xE4,
0x7C, 0x3D, 0x34, 0x2A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, 0x4A, 0x36,
0x3C, 0x60, 0x91, 0x9F, 0x6E, 0x1C, 0x71, 0xEA, 0xD0, 0x78, 0x78, 0xA0, 0x2E, 0xC6, 0x32, 0x6B
}
},
.package2_fixed_key_modulus = { /* Fixed RSA key used to validate PK21 signatures. */
0xB3, 0x65, 0x54, 0xFB, 0x0A, 0xB0, 0x1E, 0x85, 0xA7, 0xF6, 0xCF, 0x91, 0x8E, 0xBA, 0x96, 0x99,
0x0D, 0x8B, 0x91, 0x69, 0x2A, 0xEE, 0x01, 0x20, 0x4F, 0x34, 0x5C, 0x2C, 0x4F, 0x4E, 0x37, 0xC7,
0xF1, 0x0B, 0xD4, 0xCD, 0xA1, 0x7F, 0x93, 0xF1, 0x33, 0x59, 0xCE, 0xB1, 0xE9, 0xDD, 0x26, 0xE6,
0xF3, 0xBB, 0x77, 0x87, 0x46, 0x7A, 0xD6, 0x4E, 0x47, 0x4A, 0xD1, 0x41, 0xB7, 0x79, 0x4A, 0x38,
0x06, 0x6E, 0xCF, 0x61, 0x8F, 0xCD, 0xC1, 0x40, 0x0B, 0xFA, 0x26, 0xDC, 0xC0, 0x34, 0x51, 0x83,
0xD9, 0x3B, 0x11, 0x54, 0x3B, 0x96, 0x27, 0x32, 0x9A, 0x95, 0xBE, 0x1E, 0x68, 0x11, 0x50, 0xA0,
0x6B, 0x10, 0xA8, 0x83, 0x8B, 0xF5, 0xFC, 0xBC, 0x90, 0x84, 0x7A, 0x5A, 0x5C, 0x43, 0x52, 0xE6,
0xC8, 0x26, 0xE9, 0xFE, 0x06, 0xA0, 0x8B, 0x53, 0x0F, 0xAF, 0x1E, 0xC4, 0x1C, 0x0B, 0xCF, 0x50,
0x1A, 0xA4, 0xF3, 0x5C, 0xFB, 0xF0, 0x97, 0xE4, 0xDE, 0x32, 0x0A, 0x9F, 0xE3, 0x5A, 0xAA, 0xB7,
0x44, 0x7F, 0x5C, 0x33, 0x60, 0xB9, 0x0F, 0x22, 0x2D, 0x33, 0x2A, 0xE9, 0x69, 0x79, 0x31, 0x42,
0x8F, 0xE4, 0x3A, 0x13, 0x8B, 0xE7, 0x26, 0xBD, 0x08, 0x87, 0x6C, 0xA6, 0xF2, 0x73, 0xF6, 0x8E,
0xA7, 0xF2, 0xFE, 0xFB, 0x6C, 0x28, 0x66, 0x0D, 0xBD, 0xD7, 0xEB, 0x42, 0xA8, 0x78, 0xE6, 0xB8,
0x6B, 0xAE, 0xC7, 0xA9, 0xE2, 0x40, 0x6E, 0x89, 0x20, 0x82, 0x25, 0x8E, 0x3C, 0x6A, 0x60, 0xD7,
0xF3, 0x56, 0x8E, 0xEC, 0x8D, 0x51, 0x8A, 0x63, 0x3C, 0x04, 0x78, 0x23, 0x0E, 0x90, 0x0C, 0xB4,
0xE7, 0x86, 0x3B, 0x4F, 0x8E, 0x13, 0x09, 0x47, 0x32, 0x0E, 0x04, 0xB8, 0x4D, 0x5B, 0xB0, 0x46,
0x71, 0xB0, 0x5C, 0xF4, 0xAD, 0x63, 0x4F, 0xC5, 0xE2, 0xAC, 0x1E, 0xC4, 0x33, 0x96, 0x09, 0x7B
}
};
static void generate_kek(unsigned char *dst, const unsigned char *src, const unsigned char *master_key, const unsigned char *kek_seed, const unsigned char *key_seed) {
unsigned char kek[0x10];
unsigned char src_kek[0x10];
aes_ctx_t *master_ctx = new_aes_ctx(master_key, 0x10, AES_MODE_ECB);
aes_decrypt(master_ctx, kek, kek_seed, 0x10);
free_aes_ctx(master_ctx);
aes_ctx_t *kek_ctx = new_aes_ctx(kek, 0x10, AES_MODE_ECB);
aes_decrypt(kek_ctx, src_kek, src, 0x10);
free_aes_ctx(kek_ctx);
if (key_seed != NULL) {
aes_ctx_t *key_ctx = new_aes_ctx(src_kek, 0x10, AES_MODE_ECB);
aes_decrypt(key_ctx, dst, key_seed, 0x10);
free_aes_ctx(key_ctx);
} else {
memcpy(dst, src_kek, 0x10);
}
}
void pki_derive_keys(nca_keyset_t *keyset) {
unsigned char zeroes[0x100];
unsigned char cmac[0x10];
memset(zeroes, 0, 0x100);
memset(cmac, 0, 0x10);
/* Derive keys as necessary. */
for (unsigned int i = 0; i < 0x6; i++) {
/* Start by deriving keyblob keys. */
if (memcmp(&keyset->secure_boot_key, zeroes, 0x10) == 0) {
continue;
}
if (memcmp(&keyset->tsec_key, zeroes, 0x10) == 0) {
continue;
}
if (memcmp(&keyset->keyblob_key_sources[i], zeroes, 0x10) == 0) {
continue;
}
aes_ctx_t *sbk_ctx = new_aes_ctx(keyset->secure_boot_key, 0x10, AES_MODE_ECB);
aes_ctx_t *tsec_ctx = new_aes_ctx(keyset->tsec_key, 0x10, AES_MODE_ECB);
aes_decrypt(tsec_ctx, &keyset->keyblob_keys[i], &keyset->keyblob_key_sources[i], 0x10);
aes_decrypt(sbk_ctx, &keyset->keyblob_keys[i], &keyset->keyblob_keys[i], 0x10);
free_aes_ctx(tsec_ctx);
free_aes_ctx(sbk_ctx);
if (memcmp(keyset->keyblob_mac_key_source, zeroes, 0x10) == 0) {
continue;
}
aes_ctx_t *mac_gen_ctx = new_aes_ctx(&keyset->keyblob_keys[i], 0x10, AES_MODE_ECB);
aes_decrypt(mac_gen_ctx, &keyset->keyblob_mac_keys[i], keyset->keyblob_mac_key_source, 0x10);
/* Derive Device key */
if (i == 0 && memcmp(keyset->per_console_key_source, zeroes, 0x10) != 0) {
aes_decrypt(mac_gen_ctx, keyset->device_key, keyset->per_console_key_source, 0x10);
}
free_aes_ctx(mac_gen_ctx);
}
for (unsigned int i = 0; i < 0x6; i++) {
/* Then we decrypt keyblobs. */
if (memcmp(&keyset->keyblob_keys[i], zeroes, 0x10) == 0) {
continue;
}
if (memcmp(&keyset->keyblob_mac_keys[i], zeroes, 0x10) == 0) {
continue;
}
if (memcmp(&keyset->encrypted_keyblobs[i], zeroes, 0xB0) == 0) {
continue;
}
aes_calculate_cmac(cmac, &keyset->encrypted_keyblobs[i][0x10], 0xA0, keyset->keyblob_mac_keys[i]);
if (memcmp(cmac, &keyset->encrypted_keyblobs[i][0], 0x10) != 0) {
fprintf(stderr, "[ WARN ] Keyblob MAC %02x is invalid. Are SBK/TSEC key correct?\n", i);
continue;
}
aes_ctx_t *keyblob_ctx = new_aes_ctx(&keyset->keyblob_keys[i], 0x10, AES_MODE_CTR);
aes_setiv(keyblob_ctx, &keyset->encrypted_keyblobs[i][0x10], 0x10);
aes_decrypt(keyblob_ctx, &keyset->keyblobs[i], &keyset->encrypted_keyblobs[i][0x20], sizeof(keyset->keyblobs[i]));
free_aes_ctx(keyblob_ctx);
}
for (unsigned int i = 0; i < 0x6; i++) {
/* Set package1 key as relevant. */
if (memcmp(keyset->keyblobs[i] + 0x80, zeroes, 0x10) != 0) {
memcpy(&keyset->package1_keys[i], &keyset->keyblobs[i][0x80], 0x10);
}
/* Set master kek as relevant. */
if (memcmp(keyset->keyblobs[i] + 0x00, zeroes, 0x10) != 0) {
memcpy(&keyset->master_keks[i], &keyset->keyblobs[i][0x00], 0x10);
}
}
for (unsigned int i = 0x6; i < 0x20; i++) {
/* Derive new 6.2.0+ keks. */
if (memcmp(keyset->tsec_auth_signatures[i-6], zeroes, 0x10) == 0) {
continue;
}
/* Derive TSEC root key. */
if (memcmp(keyset->tsec_root_kek, zeroes, 0x10) != 0) {
aes_ctx_t *tsec_root_ctx = new_aes_ctx(keyset->tsec_root_kek, 0x10, AES_MODE_ECB);
aes_encrypt(tsec_root_ctx, keyset->tsec_root_keys[i-6], keyset->tsec_auth_signatures[i-6], 0x10);
free_aes_ctx(tsec_root_ctx);
}
/* Derive package1 MAC key */
if (memcmp(keyset->package1_mac_kek, zeroes, 0x10) != 0) {
aes_ctx_t *pk11_mac_ctx = new_aes_ctx(keyset->package1_mac_kek, 0x10, AES_MODE_ECB);
aes_encrypt(pk11_mac_ctx, keyset->package1_mac_keys[i], keyset->tsec_auth_signatures[i-6], 0x10);
free_aes_ctx(pk11_mac_ctx);
}
/* Derive package1 key */
if (memcmp(keyset->package1_kek, zeroes, 0x10) != 0) {
aes_ctx_t *pk11_ctx = new_aes_ctx(keyset->package1_kek, 0x10, AES_MODE_ECB);
aes_encrypt(pk11_ctx, keyset->package1_keys[i], keyset->tsec_auth_signatures[i-6], 0x10);
free_aes_ctx(pk11_ctx);
}
}
for (unsigned int i = 0x6; i < 0x20; i++) {
/* Do new keygen for 6.2.0+. */
const unsigned int which_tsec_root_key = (i >= 0x8) ? (0x8 - 6) : (i - 6);
if (memcmp(keyset->tsec_root_keys[which_tsec_root_key], zeroes, 0x10) == 0) {
continue;
}
if (memcmp(keyset->master_kek_sources[i], zeroes, 0x10) == 0) {
continue;
}
aes_ctx_t *tsec_root_ctx = new_aes_ctx(keyset->tsec_root_keys[which_tsec_root_key], 0x10, AES_MODE_ECB);
aes_decrypt(tsec_root_ctx, keyset->master_keks[i], keyset->master_kek_sources[i], 0x10);
free_aes_ctx(tsec_root_ctx);
}
/* Derive master keks with mariko keydata -- these are always preferred to other sources. */
for (unsigned int i = 0; i < 0x20; i++) {
if (memcmp(keyset->mariko_kek, zeroes, 0x10) == 0) {
continue;
}
if (memcmp(keyset->mariko_master_kek_sources[i], zeroes, 0x10) == 0) {
continue;
}
aes_ctx_t *mariko_kek_ctx = new_aes_ctx(keyset->mariko_kek, 0x10, AES_MODE_ECB);
aes_decrypt(mariko_kek_ctx, keyset->master_keks[i], keyset->mariko_master_kek_sources[i], 0x10);
free_aes_ctx(mariko_kek_ctx);
}
for (unsigned int i = 0; i < 0x20; i++) {
/* Then we derive master keys. */
if (memcmp(keyset->master_key_source, zeroes, 0x10) == 0) {
continue;
}
/* We need a non-zero master kek. */
if (memcmp(keyset->master_keks[i], zeroes, 0x10) == 0) {
continue;
}
/* Derive Master Keys. */
aes_ctx_t *master_gen_ctx = new_aes_ctx(&keyset->master_keks[i], 0x10, AES_MODE_ECB);
aes_decrypt(master_gen_ctx, &keyset->master_keys[i], keyset->master_key_source, 0x10);
free_aes_ctx(master_gen_ctx);
}
for (unsigned int i = 0; i < 0x20; i++) {
if (memcmp(&keyset->master_keys[i], zeroes, 0x10) == 0) {
continue;
}
aes_ctx_t *master_ctx = new_aes_ctx(&keyset->master_keys[i], 0x10, AES_MODE_ECB);
/* Derive Key Area Encryption Keys */
if (memcmp(keyset->key_area_key_application_source, zeroes, 0x10) != 0) {
generate_kek(keyset->key_area_keys[i][0], keyset->key_area_key_application_source, keyset->master_keys[i], keyset->aes_kek_generation_source, keyset->aes_key_generation_source);
}
if (memcmp(keyset->key_area_key_ocean_source, zeroes, 0x10) != 0) {
generate_kek(keyset->key_area_keys[i][1], keyset->key_area_key_ocean_source, keyset->master_keys[i], keyset->aes_kek_generation_source, keyset->aes_key_generation_source);
}
if (memcmp(keyset->key_area_key_system_source, zeroes, 0x10) != 0) {
generate_kek(keyset->key_area_keys[i][2], keyset->key_area_key_system_source, keyset->master_keys[i], keyset->aes_kek_generation_source, keyset->aes_key_generation_source);
}
/* Derive Titlekek */
if (memcmp(keyset->titlekek_source, zeroes, 0x10) != 0) {
aes_decrypt(master_ctx, &keyset->titlekeks[i], keyset->titlekek_source, 0x10);
}
/* Derive Package2 Key */
if (memcmp(keyset->package2_key_source, zeroes, 0x10) != 0) {
aes_decrypt(master_ctx, &keyset->package2_keys[i], keyset->package2_key_source, 0x10);
}
/* Derive Header Key */
if (i == 0 && memcmp(keyset->header_kek_source, zeroes, 0x10) != 0 && memcmp(keyset->header_key_source, zeroes, 0x20) != 0) {
unsigned char header_kek[0x10];
generate_kek(header_kek, keyset->header_kek_source, keyset->master_keys[i], keyset->aes_kek_generation_source, keyset->aes_key_generation_source);
aes_ctx_t *header_ctx = new_aes_ctx(header_kek, 0x10, AES_MODE_ECB);
aes_decrypt(header_ctx, keyset->header_key, keyset->header_key_source, 0x20);
free_aes_ctx(header_ctx);
}
/* Derive SD Card Key */
if (i == 0 && memcmp(keyset->sd_card_kek_source, zeroes, 0x10) != 0) {
unsigned char sd_kek[0x10];
generate_kek(sd_kek, keyset->sd_card_kek_source, keyset->master_keys[i], keyset->aes_kek_generation_source, keyset->aes_key_generation_source);
aes_ctx_t *sd_ctx = new_aes_ctx(sd_kek, 0x10, AES_MODE_ECB);
for (unsigned int k = 0; k < 2; k++) {
if (memcmp(keyset->sd_card_key_sources[k], zeroes, 0x20) != 0) {
aes_decrypt(sd_ctx, keyset->sd_card_keys[k], keyset->sd_card_key_sources[k], 0x20);
}
}
free_aes_ctx(sd_ctx);
}
/* Derive Save MAC Key */
if (i == 0 && memcmp(keyset->save_mac_kek_source, zeroes, 0x10) != 0 && memcmp(keyset->save_mac_key_source, zeroes, 0x10) != 0 && memcmp(keyset->device_key, zeroes, 0x10) != 0) {
generate_kek(keyset->save_mac_key, keyset->save_mac_kek_source, keyset->device_key, keyset->aes_kek_generation_source, keyset->save_mac_key_source);
}
free_aes_ctx(master_ctx);
}
}
void pki_print_keys(nca_keyset_t *keyset) {
unsigned char zeroes[0x100] = {0};
#define PRINT_KEY_WITH_NAME(ky, kn) do { if (memcmp(ky, zeroes, sizeof(ky)) != 0) { printf("%-32s= ", #kn); for (unsigned int k_i = 0; k_i < sizeof(ky); k_i++) { printf("%02X", ky[k_i]); } printf("\n"); } } while (0)
#define PRINT_KEY_WITH_NAME_IDX(ky, kn, idx) do { if (memcmp(ky, zeroes, sizeof(ky)) != 0) { char KEY_NAME[32]; snprintf(KEY_NAME, sizeof(KEY_NAME), "%s_%02"PRIx32, #kn, idx); printf("%-32s= ", KEY_NAME); for (unsigned int k_i = 0; k_i < sizeof(ky); k_i++) { printf("%02X", ky[k_i]); } printf("\n"); } } while (0)
PRINT_KEY_WITH_NAME(keyset->secure_boot_key, secure_boot_key);
PRINT_KEY_WITH_NAME(keyset->tsec_key, tsec_key);
PRINT_KEY_WITH_NAME(keyset->device_key, device_key);
PRINT_KEY_WITH_NAME(keyset->tsec_root_kek, tsec_root_kek);
PRINT_KEY_WITH_NAME(keyset->package1_mac_kek, package1_mac_kek);
PRINT_KEY_WITH_NAME(keyset->package1_kek, package1_kek);
printf("\n");
for (unsigned int i = 0x6; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->tsec_auth_signatures[i-6], tsec_auth_signature, i-6);
}
printf("\n");
for (unsigned int i = 0x6; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->tsec_root_keys[i-6], tsec_root_key, i-6);
}
printf("\n");
PRINT_KEY_WITH_NAME(keyset->keyblob_mac_key_source, keyblob_mac_key_source);
for (unsigned int i = 0; i < 0x6; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->keyblob_key_sources[i], keyblob_key_source, i);
}
printf("\n");
for (unsigned int i = 0; i < 0x6; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->keyblob_keys[i], keyblob_key, i);
}
printf("\n");
for (unsigned int i = 0; i < 0x6; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->keyblob_mac_keys[i], keyblob_mac_key, i);
}
printf("\n");
for (unsigned int i = 0; i < 0x6; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->encrypted_keyblobs[i], encrypted_keyblob, i);
}
printf("\n");
for (unsigned int i = 0; i < 0x6; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->keyblobs[i], keyblob, i);
}
printf("\n");
for (unsigned int i = 0x6; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->master_kek_sources[i], master_kek_source, i);
}
printf("\n");
PRINT_KEY_WITH_NAME(keyset->mariko_kek, mariko_kek);
PRINT_KEY_WITH_NAME(keyset->mariko_bek, mariko_bek);
for (unsigned int i = 0x0; i < 0xC; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->mariko_aes_class_keys[i], mariko_aes_class_key, i);
}
printf("\n");
for (unsigned int i = 0x0; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->mariko_master_kek_sources[i], mariko_master_kek_source, i);
}
printf("\n");
for (unsigned int i = 0x0; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->master_keks[i], master_kek, i);
}
printf("\n");
PRINT_KEY_WITH_NAME(keyset->master_key_source, master_key_source);
printf("\n");
for (unsigned int i = 0; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->master_keys[i], master_key, i);
}
printf("\n");
for (unsigned int i = 0; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->package1_keys[i], package1_key, i);
}
printf("\n");
for (unsigned int i = 0x6; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->package1_mac_keys[i], package1_mac_key, i);
}
printf("\n");
PRINT_KEY_WITH_NAME(keyset->package2_key_source, package2_key_source);
printf("\n");
for (unsigned int i = 0; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->package2_keys[i], package2_key, i);
}
printf("\n");
PRINT_KEY_WITH_NAME(keyset->per_console_key_source, per_console_key_source);
PRINT_KEY_WITH_NAME(keyset->aes_kek_generation_source, aes_kek_generation_source);
PRINT_KEY_WITH_NAME(keyset->aes_key_generation_source, aes_key_generation_source);
PRINT_KEY_WITH_NAME(keyset->titlekek_source, titlekek_source);
printf("\n");
for (unsigned int i = 0; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->titlekeks[i], titlekek, i);
}
printf("\n");
PRINT_KEY_WITH_NAME(keyset->key_area_key_application_source, key_area_key_application_source);
PRINT_KEY_WITH_NAME(keyset->key_area_key_ocean_source, key_area_key_ocean_source);
PRINT_KEY_WITH_NAME(keyset->key_area_key_system_source, key_area_key_system_source);
PRINT_KEY_WITH_NAME(keyset->sd_card_kek_source, sd_card_kek_source);
PRINT_KEY_WITH_NAME(keyset->sd_card_key_sources[0], sd_card_save_key_source);
PRINT_KEY_WITH_NAME(keyset->sd_card_key_sources[1], sd_card_nca_key_source);
PRINT_KEY_WITH_NAME(keyset->save_mac_kek_source, save_mac_kek_source);
PRINT_KEY_WITH_NAME(keyset->save_mac_key_source, save_mac_key_source);
PRINT_KEY_WITH_NAME(keyset->save_mac_key, save_mac_key);
printf("\n");
PRINT_KEY_WITH_NAME(keyset->header_key_source, header_key_source);
PRINT_KEY_WITH_NAME(keyset->header_key, header_key);
printf("\n");
for (unsigned int i = 0; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->key_area_keys[i][0], key_area_key_application, i);
}
printf("\n");
for (unsigned int i = 0; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->key_area_keys[i][1], key_area_key_ocean, i);
}
printf("\n");
for (unsigned int i = 0; i < 0x20; i++) {
PRINT_KEY_WITH_NAME_IDX(keyset->key_area_keys[i][2], key_area_key_system, i);
}
printf("\n");
#undef PRINT_KEY_WITH_NAME_IDX
#undef PRINT_KEY_WITH_NAME
}
void pki_initialize_keyset(nca_keyset_t *keyset, keyset_variant_t variant) {
switch (variant) {
case KEYSET_DEV:
memcpy(keyset, &nca_keys_dev, sizeof(*keyset));
break;
case KEYSET_RETAIL:
memcpy(keyset, &nca_keys_retail, sizeof(*keyset));
break;
default:
memset(keyset, 0, sizeof(*keyset));
break;
}
pki_derive_keys(keyset);
}