hactool/pki.c

562 lines
28 KiB
C
Raw Normal View History

#include <stdio.h>
#include <string.h>
#include "aes.h"
#include "pki.h"
const nca_keyset_t nca_keys_retail = {
{
ZEROES_KEY, /* Master Key 00 */
ZEROES_KEY, /* Master Key 01 */
ZEROES_KEY, /* Master Key 02 */
ZEROES_KEY, /* Master Key 03 */
ZEROES_KEY, /* Master Key 04 */
ZEROES_KEY, /* Master Key 05 */
ZEROES_KEY, /* Master Key 06 */
ZEROES_KEY, /* Master Key 07 */
ZEROES_KEY, /* Master Key 08 */
ZEROES_KEY, /* Master Key 09 */
ZEROES_KEY, /* Master Key 10 */
ZEROES_KEY, /* Master Key 11 */
ZEROES_KEY, /* Master Key 12 */
ZEROES_KEY, /* Master Key 13 */
ZEROES_KEY, /* Master Key 14 */
ZEROES_KEY, /* Master Key 15 */
ZEROES_KEY, /* Master Key 16 */
ZEROES_KEY, /* Master Key 17 */
ZEROES_KEY, /* Master Key 18 */
ZEROES_KEY, /* Master Key 19 */
ZEROES_KEY, /* Master Key 20 */
ZEROES_KEY, /* Master Key 21 */
ZEROES_KEY, /* Master Key 22 */
ZEROES_KEY, /* Master Key 23 */
ZEROES_KEY, /* Master Key 24 */
ZEROES_KEY, /* Master Key 25 */
ZEROES_KEY, /* Master Key 26 */
ZEROES_KEY, /* Master Key 27 */
ZEROES_KEY, /* Master Key 28 */
ZEROES_KEY, /* Master Key 29 */
ZEROES_KEY, /* Master Key 30 */
ZEROES_KEY /* Master Key 31 */
},
2018-02-06 23:52:48 +00:00
{
ZEROES_KEY, /* Package1 Key 00 */
ZEROES_KEY, /* Package1 Key 01 */
ZEROES_KEY, /* Package1 Key 02 */
ZEROES_KEY, /* Package1 Key 03 */
ZEROES_KEY, /* Package1 Key 04 */
ZEROES_KEY, /* Package1 Key 05 */
ZEROES_KEY, /* Package1 Key 06 */
ZEROES_KEY, /* Package1 Key 07 */
ZEROES_KEY, /* Package1 Key 08 */
ZEROES_KEY, /* Package1 Key 09 */
ZEROES_KEY, /* Package1 Key 10 */
ZEROES_KEY, /* Package1 Key 11 */
ZEROES_KEY, /* Package1 Key 12 */
ZEROES_KEY, /* Package1 Key 13 */
ZEROES_KEY, /* Package1 Key 14 */
ZEROES_KEY, /* Package1 Key 15 */
ZEROES_KEY, /* Package1 Key 16 */
ZEROES_KEY, /* Package1 Key 17 */
ZEROES_KEY, /* Package1 Key 18 */
ZEROES_KEY, /* Package1 Key 19 */
ZEROES_KEY, /* Package1 Key 20 */
ZEROES_KEY, /* Package1 Key 21 */
ZEROES_KEY, /* Package1 Key 22 */
ZEROES_KEY, /* Package1 Key 23 */
ZEROES_KEY, /* Package1 Key 24 */
ZEROES_KEY, /* Package1 Key 25 */
ZEROES_KEY, /* Package1 Key 26 */
ZEROES_KEY, /* Package1 Key 27 */
ZEROES_KEY, /* Package1 Key 28 */
ZEROES_KEY, /* Package1 Key 29 */
ZEROES_KEY, /* Package1 Key 30 */
ZEROES_KEY /* Package1 Key 31 */
},
{
ZEROES_KEY, /* Package2 Key 00 */
ZEROES_KEY, /* Package2 Key 01 */
ZEROES_KEY, /* Package2 Key 02 */
ZEROES_KEY, /* Package2 Key 03 */
ZEROES_KEY, /* Package2 Key 04 */
ZEROES_KEY, /* Package2 Key 05 */
ZEROES_KEY, /* Package2 Key 06 */
ZEROES_KEY, /* Package2 Key 07 */
ZEROES_KEY, /* Package2 Key 08 */
ZEROES_KEY, /* Package2 Key 09 */
ZEROES_KEY, /* Package2 Key 10 */
ZEROES_KEY, /* Package2 Key 11 */
ZEROES_KEY, /* Package2 Key 12 */
ZEROES_KEY, /* Package2 Key 13 */
ZEROES_KEY, /* Package2 Key 14 */
ZEROES_KEY, /* Package2 Key 15 */
ZEROES_KEY, /* Package2 Key 16 */
ZEROES_KEY, /* Package2 Key 17 */
ZEROES_KEY, /* Package2 Key 18 */
ZEROES_KEY, /* Package2 Key 19 */
ZEROES_KEY, /* Package2 Key 20 */
ZEROES_KEY, /* Package2 Key 21 */
ZEROES_KEY, /* Package2 Key 22 */
ZEROES_KEY, /* Package2 Key 23 */
ZEROES_KEY, /* Package2 Key 24 */
ZEROES_KEY, /* Package2 Key 25 */
ZEROES_KEY, /* Package2 Key 26 */
ZEROES_KEY, /* Package2 Key 27 */
ZEROES_KEY, /* Package2 Key 28 */
ZEROES_KEY, /* Package2 Key 29 */
ZEROES_KEY, /* Package2 Key 30 */
ZEROES_KEY /* Package2 Key 31 */
},
ZEROES_KEY, /* Package2 Key Source */
ZEROES_KEY, /* Generate Aes Kek Source */
ZEROES_KEY, /* Generate Aes Key Source */
ZEROES_KEY, /* Key Area Encryption Key Source Application */
ZEROES_KEY, /* Key Area Encryption Key Source Ocean */
ZEROES_KEY, /* Key Area Encryption Key Source System */
ZEROES_KEY, /* Titlekek Source */
ZEROES_KEY, /* Headerkek Source */
ZEROES_XTS_KEY, /* Encrypted Header Key */
ZEROES_XTS_KEY, /* Header key */
{
ZEROES_KEY, /* Titlekek 00 */
ZEROES_KEY, /* Titlekek 01 */
ZEROES_KEY, /* Titlekek 02 */
ZEROES_KEY, /* Titlekek 03 */
ZEROES_KEY, /* Titlekek 04 */
ZEROES_KEY, /* Titlekek 05 */
ZEROES_KEY, /* Titlekek 06 */
ZEROES_KEY, /* Titlekek 07 */
ZEROES_KEY, /* Titlekek 08 */
ZEROES_KEY, /* Titlekek 09 */
ZEROES_KEY, /* Titlekek 10 */
ZEROES_KEY, /* Titlekek 11 */
ZEROES_KEY, /* Titlekek 12 */
ZEROES_KEY, /* Titlekek 13 */
ZEROES_KEY, /* Titlekek 14 */
ZEROES_KEY, /* Titlekek 15 */
ZEROES_KEY, /* Titlekek 16 */
ZEROES_KEY, /* Titlekek 17 */
ZEROES_KEY, /* Titlekek 18 */
ZEROES_KEY, /* Titlekek 19 */
ZEROES_KEY, /* Titlekek 20 */
ZEROES_KEY, /* Titlekek 21 */
ZEROES_KEY, /* Titlekek 22 */
ZEROES_KEY, /* Titlekek 23 */
ZEROES_KEY, /* Titlekek 24 */
ZEROES_KEY, /* Titlekek 25 */
ZEROES_KEY, /* Titlekek 26 */
ZEROES_KEY, /* Titlekek 27 */
ZEROES_KEY, /* Titlekek 28 */
ZEROES_KEY, /* Titlekek 29 */
ZEROES_KEY, /* Titlekek 30 */
ZEROES_KEY /* Titlekek 31 */
},
{
ZEROES_KAEKS, /* Key Area Encryption Keyset 00 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 01 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 02 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 03 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 04 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 05 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 06 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 07 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 08 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 09 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 10 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 11 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 12 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 13 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 14 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 15 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 16 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 17 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 18 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 19 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 20 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 21 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 22 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 23 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 24 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 25 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 26 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 27 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 28 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 29 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 30 */
ZEROES_KAEKS /* Key Area Encryption Keyset 31 */
},
{ /* 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, 0x41, 0x64, 0xCD, 0x52, 0xB6, 0x34, 0x4D, 0xAE,
0xC0, 0x2E, 0xDE, 0xA4, 0xDF, 0x27, 0x68, 0x3C, 0xC1, 0xA0, 0x60, 0xAD, 0x43, 0xF3, 0xFC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
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, 0x44, 0xF5, 0x1A, 0x9D, 0x22, 0xBE, 0x12, 0xA1, 0x77, 0x11, 0xE2, 0xA1, 0xCD,
0x40, 0x9A, 0xA2, 0x8B, 0x60, 0x9B, 0xEF, 0xA0, 0xD3, 0x48, 0x63, 0xA2, 0xF8, 0xA3, 0x2C, 0x08,
0x56, 0x52, 0x2E, 0x60, 0x19, 0x67, 0x5A, 0xA7, 0x9F, 0xDC, 0x3F, 0x3F, 0x69, 0x2B, 0x31, 0x6A,
0xB7, 0x88, 0x4A, 0x14, 0x84, 0x80, 0x33, 0x3C, 0x9D, 0x44, 0xB7, 0x3F, 0x4C, 0xE1, 0x75, 0xEA,
0x37, 0xEA, 0xE8, 0x1E, 0x7C, 0x77, 0xB7, 0xC6, 0x1A, 0xA2, 0xF0, 0x9F, 0x10, 0x61, 0xCD, 0x7B,
0x5B, 0x32, 0x4C, 0x37, 0xEF, 0xB1, 0x71, 0x68, 0x53, 0x0A, 0xED, 0x51, 0x7D, 0x35, 0x22, 0xFD
},
{ /* Fixed RSA key used to validate PK21 signatures. */
0x8D, 0x13, 0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0xCC, 0xCF, 0x90, 0xA7, 0xB9, 0x98, 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
}
};
const nca_keyset_t nca_keys_dev = {
{
ZEROES_KEY, /* Master Key 00 */
ZEROES_KEY, /* Master Key 01 */
ZEROES_KEY, /* Master Key 02 */
ZEROES_KEY, /* Master Key 03 */
ZEROES_KEY, /* Master Key 04 */
ZEROES_KEY, /* Master Key 05 */
ZEROES_KEY, /* Master Key 06 */
ZEROES_KEY, /* Master Key 07 */
ZEROES_KEY, /* Master Key 08 */
ZEROES_KEY, /* Master Key 09 */
ZEROES_KEY, /* Master Key 10 */
ZEROES_KEY, /* Master Key 11 */
ZEROES_KEY, /* Master Key 12 */
ZEROES_KEY, /* Master Key 13 */
ZEROES_KEY, /* Master Key 14 */
ZEROES_KEY, /* Master Key 15 */
ZEROES_KEY, /* Master Key 16 */
ZEROES_KEY, /* Master Key 17 */
ZEROES_KEY, /* Master Key 18 */
ZEROES_KEY, /* Master Key 19 */
ZEROES_KEY, /* Master Key 20 */
ZEROES_KEY, /* Master Key 21 */
ZEROES_KEY, /* Master Key 22 */
ZEROES_KEY, /* Master Key 23 */
ZEROES_KEY, /* Master Key 24 */
ZEROES_KEY, /* Master Key 25 */
ZEROES_KEY, /* Master Key 26 */
ZEROES_KEY, /* Master Key 27 */
ZEROES_KEY, /* Master Key 28 */
ZEROES_KEY, /* Master Key 29 */
ZEROES_KEY, /* Master Key 30 */
ZEROES_KEY /* Master Key 31 */
},
2018-02-06 23:52:48 +00:00
{
ZEROES_KEY, /* Package1 Key 00 */
ZEROES_KEY, /* Package1 Key 01 */
ZEROES_KEY, /* Package1 Key 02 */
ZEROES_KEY, /* Package1 Key 03 */
ZEROES_KEY, /* Package1 Key 04 */
ZEROES_KEY, /* Package1 Key 05 */
ZEROES_KEY, /* Package1 Key 06 */
ZEROES_KEY, /* Package1 Key 07 */
ZEROES_KEY, /* Package1 Key 08 */
ZEROES_KEY, /* Package1 Key 09 */
ZEROES_KEY, /* Package1 Key 10 */
ZEROES_KEY, /* Package1 Key 11 */
ZEROES_KEY, /* Package1 Key 12 */
ZEROES_KEY, /* Package1 Key 13 */
ZEROES_KEY, /* Package1 Key 14 */
ZEROES_KEY, /* Package1 Key 15 */
ZEROES_KEY, /* Package1 Key 16 */
ZEROES_KEY, /* Package1 Key 17 */
ZEROES_KEY, /* Package1 Key 18 */
ZEROES_KEY, /* Package1 Key 19 */
ZEROES_KEY, /* Package1 Key 20 */
ZEROES_KEY, /* Package1 Key 21 */
ZEROES_KEY, /* Package1 Key 22 */
ZEROES_KEY, /* Package1 Key 23 */
ZEROES_KEY, /* Package1 Key 24 */
ZEROES_KEY, /* Package1 Key 25 */
ZEROES_KEY, /* Package1 Key 26 */
ZEROES_KEY, /* Package1 Key 27 */
ZEROES_KEY, /* Package1 Key 28 */
ZEROES_KEY, /* Package1 Key 29 */
ZEROES_KEY, /* Package1 Key 30 */
ZEROES_KEY /* Package1 Key 31 */
},
{
ZEROES_KEY, /* Package2 Key 00 */
ZEROES_KEY, /* Package2 Key 01 */
ZEROES_KEY, /* Package2 Key 02 */
ZEROES_KEY, /* Package2 Key 03 */
ZEROES_KEY, /* Package2 Key 04 */
ZEROES_KEY, /* Package2 Key 05 */
ZEROES_KEY, /* Package2 Key 06 */
ZEROES_KEY, /* Package2 Key 07 */
ZEROES_KEY, /* Package2 Key 08 */
ZEROES_KEY, /* Package2 Key 09 */
ZEROES_KEY, /* Package2 Key 10 */
ZEROES_KEY, /* Package2 Key 11 */
ZEROES_KEY, /* Package2 Key 12 */
ZEROES_KEY, /* Package2 Key 13 */
ZEROES_KEY, /* Package2 Key 14 */
ZEROES_KEY, /* Package2 Key 15 */
ZEROES_KEY, /* Package2 Key 16 */
ZEROES_KEY, /* Package2 Key 17 */
ZEROES_KEY, /* Package2 Key 18 */
ZEROES_KEY, /* Package2 Key 19 */
ZEROES_KEY, /* Package2 Key 20 */
ZEROES_KEY, /* Package2 Key 21 */
ZEROES_KEY, /* Package2 Key 22 */
ZEROES_KEY, /* Package2 Key 23 */
ZEROES_KEY, /* Package2 Key 24 */
ZEROES_KEY, /* Package2 Key 25 */
ZEROES_KEY, /* Package2 Key 26 */
ZEROES_KEY, /* Package2 Key 27 */
ZEROES_KEY, /* Package2 Key 28 */
ZEROES_KEY, /* Package2 Key 29 */
ZEROES_KEY, /* Package2 Key 30 */
ZEROES_KEY /* Package2 Key 31 */
},
ZEROES_KEY, /* Package2 Key Source */
ZEROES_KEY, /* Generate Aes Kek Source */
ZEROES_KEY, /* Generate Aes Key Source */
ZEROES_KEY, /* Key Area Encryption Key Source Application */
ZEROES_KEY, /* Key Area Encryption Key Source Ocean */
ZEROES_KEY, /* Key Area Encryption Key Source System */
ZEROES_KEY, /* Titlekek Source */
ZEROES_KEY, /* Headerkek Source */
ZEROES_XTS_KEY, /* Encrypted Header Key */
ZEROES_XTS_KEY, /* Header key */
{
ZEROES_KEY, /* Titlekek 00 */
ZEROES_KEY, /* Titlekek 01 */
ZEROES_KEY, /* Titlekek 02 */
ZEROES_KEY, /* Titlekek 03 */
ZEROES_KEY, /* Titlekek 04 */
ZEROES_KEY, /* Titlekek 05 */
ZEROES_KEY, /* Titlekek 06 */
ZEROES_KEY, /* Titlekek 07 */
ZEROES_KEY, /* Titlekek 08 */
ZEROES_KEY, /* Titlekek 09 */
ZEROES_KEY, /* Titlekek 10 */
ZEROES_KEY, /* Titlekek 11 */
ZEROES_KEY, /* Titlekek 12 */
ZEROES_KEY, /* Titlekek 13 */
ZEROES_KEY, /* Titlekek 14 */
ZEROES_KEY, /* Titlekek 15 */
ZEROES_KEY, /* Titlekek 16 */
ZEROES_KEY, /* Titlekek 17 */
ZEROES_KEY, /* Titlekek 18 */
ZEROES_KEY, /* Titlekek 19 */
ZEROES_KEY, /* Titlekek 20 */
ZEROES_KEY, /* Titlekek 21 */
ZEROES_KEY, /* Titlekek 22 */
ZEROES_KEY, /* Titlekek 23 */
ZEROES_KEY, /* Titlekek 24 */
ZEROES_KEY, /* Titlekek 25 */
ZEROES_KEY, /* Titlekek 26 */
ZEROES_KEY, /* Titlekek 27 */
ZEROES_KEY, /* Titlekek 28 */
ZEROES_KEY, /* Titlekek 29 */
ZEROES_KEY, /* Titlekek 30 */
ZEROES_KEY /* Titlekek 31 */
},
{
ZEROES_KAEKS, /* Key Area Encryption Keyset 00 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 01 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 02 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 03 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 04 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 05 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 06 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 07 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 08 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 09 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 10 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 11 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 12 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 13 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 14 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 15 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 16 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 17 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 18 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 19 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 20 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 21 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 22 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 23 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 24 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 25 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 26 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 27 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 28 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 29 */
ZEROES_KAEKS, /* Key Area Encryption Keyset 30 */
ZEROES_KAEKS /* Key Area Encryption Keyset 31 */
},
{
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,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x50,
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
}
};
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];
memset(zeroes, 0, 0x100);
/* Derive keys as necessary. */
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);
}
2018-02-06 23:52:48 +00:00
/* 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->encrypted_header_key, 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->encrypted_header_key, 0x20);
free_aes_ctx(header_ctx);
}
free_aes_ctx(master_ctx);
}
}
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);
}