2018-02-23 03:58:39 +00:00
|
|
|
#include <stdbool.h>
|
2018-02-18 00:34:31 +00:00
|
|
|
#include <stdint.h>
|
2018-02-22 23:32:47 +00:00
|
|
|
#include <string.h>
|
2018-02-18 00:34:31 +00:00
|
|
|
|
|
|
|
#include "utils.h"
|
2018-02-27 21:29:47 +00:00
|
|
|
#include "arm.h"
|
2018-02-22 22:18:34 +00:00
|
|
|
#include "configitem.h"
|
2018-02-24 00:09:55 +00:00
|
|
|
#include "gcm.h"
|
2018-02-20 07:41:27 +00:00
|
|
|
#include "masterkey.h"
|
2018-02-18 00:34:31 +00:00
|
|
|
#include "smc_api.h"
|
|
|
|
#include "smc_user.h"
|
|
|
|
#include "se.h"
|
2018-02-25 19:00:50 +00:00
|
|
|
#include "fuse.h"
|
2018-02-20 09:02:01 +00:00
|
|
|
#include "sealedkeys.h"
|
2018-02-20 04:28:37 +00:00
|
|
|
#include "userpage.h"
|
2018-02-20 05:47:11 +00:00
|
|
|
#include "titlekey.h"
|
2018-03-25 22:05:08 +01:00
|
|
|
#include "exocfg.h"
|
2018-02-18 00:34:31 +00:00
|
|
|
|
2018-02-19 21:26:37 +00:00
|
|
|
/* Globals. */
|
2018-02-25 19:00:50 +00:00
|
|
|
static bool g_crypt_aes_done = false;
|
|
|
|
static bool g_exp_mod_done = false;
|
2018-02-20 04:56:12 +00:00
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
static uint8_t g_imported_exponents[4][0x100];
|
2018-02-20 05:09:53 +00:00
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
static uint8_t g_rsausecase_to_cryptousecase[5] = {1, 2, 3, 5, 6};
|
2018-02-20 04:56:12 +00:00
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
void set_exp_mod_done(bool done) {
|
|
|
|
g_exp_mod_done = done;
|
2018-02-20 04:56:12 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
bool get_exp_mod_done(void) {
|
2018-02-20 04:56:12 +00:00
|
|
|
return g_exp_mod_done;
|
|
|
|
}
|
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
uint32_t exp_mod_done_handler(void) {
|
|
|
|
set_exp_mod_done(true);
|
|
|
|
|
2018-02-20 04:56:12 +00:00
|
|
|
se_trigger_interrupt();
|
2018-02-23 03:58:39 +00:00
|
|
|
|
2018-02-20 04:56:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t user_exp_mod(smc_args_t *args) {
|
|
|
|
uint8_t modulus[0x100];
|
|
|
|
uint8_t exponent[0x100];
|
|
|
|
uint8_t input[0x100];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 04:56:12 +00:00
|
|
|
upage_ref_t page_ref;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 04:56:12 +00:00
|
|
|
/* Validate size. */
|
|
|
|
if (args->X[4] == 0 || args->X[4] > 0x100 || (args->X[4] & 3) != 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 04:56:12 +00:00
|
|
|
size_t exponent_size = (size_t)args->X[4];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 04:56:12 +00:00
|
|
|
void *user_input = (void *)args->X[1];
|
|
|
|
void *user_exponent = (void *)args->X[2];
|
|
|
|
void *user_modulus = (void *)args->X[3];
|
|
|
|
|
|
|
|
/* Copy user data into secure memory. */
|
|
|
|
if (upage_init(&page_ref, user_input) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (user_copy_to_secure(&page_ref, input, user_input, 0x100) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (user_copy_to_secure(&page_ref, exponent, user_exponent, exponent_size) == 0) {
|
|
|
|
return 2;
|
2018-03-07 14:11:53 +00:00
|
|
|
}
|
2018-02-20 04:56:12 +00:00
|
|
|
if (user_copy_to_secure(&page_ref, modulus, user_modulus, 0x100) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
set_exp_mod_done(false);
|
2018-02-20 04:56:12 +00:00
|
|
|
/* Hardcode RSA keyslot 0. */
|
|
|
|
set_rsa_keyslot(0, modulus, 0x100, exponent, exponent_size);
|
|
|
|
se_exp_mod(0, input, 0x100, exp_mod_done_handler);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 04:56:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-02-19 21:26:37 +00:00
|
|
|
|
2018-02-20 11:19:35 +00:00
|
|
|
uint32_t user_get_random_bytes(smc_args_t *args) {
|
|
|
|
uint8_t random_bytes[0x40];
|
|
|
|
if (args->X[1] > 0x38) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 11:19:35 +00:00
|
|
|
size_t size = (size_t)args->X[1];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 11:19:35 +00:00
|
|
|
flush_dcache_range(random_bytes, random_bytes + size);
|
|
|
|
se_generate_random(KEYSLOT_SWITCH_RNGKEY, random_bytes, size);
|
|
|
|
flush_dcache_range(random_bytes, random_bytes + size);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-03-08 10:17:46 +00:00
|
|
|
if (size != 0) {
|
|
|
|
memcpy(&args->X[1], random_bytes, size);
|
|
|
|
}
|
2018-02-20 11:19:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
uint32_t user_generate_aes_kek(smc_args_t *args) {
|
|
|
|
uint64_t wrapped_kek[2];
|
|
|
|
uint8_t kek_source[0x10];
|
|
|
|
uint64_t kek[2];
|
|
|
|
uint64_t sealed_kek[2];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
wrapped_kek[0] = args->X[1];
|
|
|
|
wrapped_kek[1] = args->X[2];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
unsigned int master_key_rev = (unsigned int)args->X[3];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
if (master_key_rev > 0) {
|
|
|
|
master_key_rev -= 1; /* GenerateAesKek offsets by one. */
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
if (master_key_rev >= MASTERKEY_REVISION_MAX) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
uint64_t packed_options = args->X[4];
|
|
|
|
if (packed_options > 0xFF) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
/* Switched the output based on how the system was booted. */
|
|
|
|
uint8_t mask_id = (uint8_t)((packed_options >> 1) & 3);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
/* Switches the output based on how it will be used. */
|
2018-03-25 22:05:08 +01:00
|
|
|
uint8_t usecase = (uint8_t)((packed_options >> 5) & (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_500 ? 7 : 3));
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
/* Switched the output based on whether it should be console unique. */
|
2018-02-23 03:58:39 +00:00
|
|
|
bool is_personalized = (int)(packed_options & 1);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
bool is_recovery_boot = configitem_is_recovery_boot();
|
2018-03-25 22:05:08 +01:00
|
|
|
|
|
|
|
/* 5.0.0+ Bounds checking. */
|
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_500) {
|
|
|
|
if (is_personalized) {
|
|
|
|
if (master_key_rev > MASTERKEY_REVISION_500_CURRENT || ((1 << (master_key_rev + 1)) & 0x33) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (mask_id > 3 || usecase >= CRYPTOUSECASE_MAX_5X) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (usecase >= CRYPTOUSECASE_UNK6) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (usecase == CRYPTOUSECASE_UNK5 && mask_id >= 4) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Mask 2 is only allowed when booted from recovery. */
|
2018-02-23 03:58:39 +00:00
|
|
|
if (mask_id == 2 && !is_recovery_boot) {
|
2018-02-20 09:02:01 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-02-20 17:39:50 +00:00
|
|
|
/* Mask 1 is only allowed when booted normally. */
|
2018-02-23 03:58:39 +00:00
|
|
|
if (mask_id == 1 && is_recovery_boot) {
|
2018-02-20 09:02:01 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
/* Masks 0, 3 are allowed all the time. */
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
const uint8_t kek_seeds[4][0x10] = {
|
|
|
|
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
|
|
|
{0xA2, 0xAB, 0xBF, 0x9C, 0x92, 0x2F, 0xBB, 0xE3, 0x78, 0x79, 0x9B, 0xC0, 0xCC, 0xEA, 0xA5, 0x74},
|
|
|
|
{0x57, 0xE2, 0xD9, 0x45, 0xE4, 0x92, 0xF4, 0xFD, 0xC3, 0xF9, 0x86, 0x38, 0x89, 0x78, 0x9F, 0x3C},
|
|
|
|
{0xE5, 0x4D, 0x9A, 0x02, 0xF0, 0x4F, 0x5F, 0xA8, 0xAD, 0x76, 0x0A, 0xF6, 0x32, 0x95, 0x59, 0xBB}
|
|
|
|
};
|
|
|
|
const uint8_t kek_masks[4][0x10] = {
|
|
|
|
{0x4D, 0x87, 0x09, 0x86, 0xC4, 0x5D, 0x20, 0x72, 0x2F, 0xBA, 0x10, 0x53, 0xDA, 0x92, 0xE8, 0xA9},
|
|
|
|
{0x25, 0x03, 0x31, 0xFB, 0x25, 0x26, 0x0B, 0x79, 0x8C, 0x80, 0xD2, 0x69, 0x98, 0xE2, 0x22, 0x77},
|
|
|
|
{0x76, 0x14, 0x1D, 0x34, 0x93, 0x2D, 0xE1, 0x84, 0x24, 0x7B, 0x66, 0x65, 0x55, 0x04, 0x65, 0x81},
|
|
|
|
{0xAF, 0x3D, 0xB7, 0xF3, 0x08, 0xA2, 0xD8, 0xA2, 0x08, 0xCA, 0x18, 0xA8, 0x69, 0x46, 0xC9, 0x0B}
|
|
|
|
};
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
/* Create kek source. */
|
|
|
|
for (unsigned int i = 0; i < 0x10; i++) {
|
|
|
|
kek_source[i] = kek_seeds[usecase][i] ^ kek_masks[mask_id][i];
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
unsigned int keyslot;
|
|
|
|
if (is_personalized) {
|
2018-03-25 22:05:08 +01:00
|
|
|
/* Behavior changed in 4.0.0, and in 5.0.0. */
|
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_500) {
|
|
|
|
keyslot = devkey_get_keyslot(master_key_rev);
|
|
|
|
} else if (exosphere_get_target_firmware() == EXOSPHERE_TARGET_FIRMWARE_400) {
|
2018-02-20 09:02:01 +00:00
|
|
|
if (master_key_rev >= 1) {
|
|
|
|
keyslot = KEYSLOT_SWITCH_DEVICEKEY; /* New device key, 4.x. */
|
|
|
|
} else {
|
|
|
|
keyslot = KEYSLOT_SWITCH_4XOLDDEVICEKEY; /* Old device key, 4.x. */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
keyslot = KEYSLOT_SWITCH_DEVICEKEY;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
keyslot = mkey_get_keyslot(master_key_rev);
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
/* Derive kek. */
|
|
|
|
decrypt_data_into_keyslot(KEYSLOT_SWITCH_TEMPKEY, keyslot, kek_source, 0x10);
|
|
|
|
se_aes_ecb_decrypt_block(KEYSLOT_SWITCH_TEMPKEY, kek, 0x10, wrapped_kek, 0x10);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
/* Seal kek. */
|
|
|
|
seal_key(sealed_kek, 0x10, kek, 0x10, usecase);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
args->X[1] = sealed_kek[0];
|
|
|
|
args->X[2] = sealed_kek[1];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 09:02:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-18 00:34:31 +00:00
|
|
|
uint32_t user_load_aes_key(smc_args_t *args) {
|
2018-02-18 23:02:37 +00:00
|
|
|
uint64_t sealed_kek[2];
|
|
|
|
uint64_t wrapped_key[2];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-18 00:34:31 +00:00
|
|
|
uint32_t keyslot = (uint32_t)args->X[1];
|
|
|
|
if (keyslot > 3) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-18 00:34:31 +00:00
|
|
|
/* Copy keydata */
|
2018-02-18 23:02:37 +00:00
|
|
|
sealed_kek[0] = args->X[2];
|
|
|
|
sealed_kek[1] = args->X[3];
|
|
|
|
wrapped_key[0] = args->X[4];
|
|
|
|
wrapped_key[1] = args->X[5];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-25 09:21:52 +00:00
|
|
|
/* Unseal the kek. */
|
2018-02-20 09:02:01 +00:00
|
|
|
unseal_key(KEYSLOT_SWITCH_TEMPKEY, sealed_kek, 0x10, CRYPTOUSECASE_AES);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-18 00:34:31 +00:00
|
|
|
/* Unwrap the key. */
|
2018-02-20 07:41:27 +00:00
|
|
|
decrypt_data_into_keyslot(keyslot, KEYSLOT_SWITCH_TEMPKEY, wrapped_key, 0x10);
|
2018-02-18 00:34:31 +00:00
|
|
|
return 0;
|
2018-02-18 23:02:37 +00:00
|
|
|
}
|
2018-02-19 21:26:37 +00:00
|
|
|
|
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
void set_crypt_aes_done(bool done) {
|
|
|
|
g_crypt_aes_done = done;
|
2018-02-19 21:26:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
bool get_crypt_aes_done(void) {
|
2018-02-19 21:26:37 +00:00
|
|
|
return g_crypt_aes_done;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t crypt_aes_done_handler(void) {
|
|
|
|
se_check_for_error();
|
2018-02-23 03:58:39 +00:00
|
|
|
|
|
|
|
set_crypt_aes_done(true);
|
|
|
|
|
2018-02-20 04:56:12 +00:00
|
|
|
se_trigger_interrupt();
|
2018-02-23 03:58:39 +00:00
|
|
|
|
2018-02-20 04:56:12 +00:00
|
|
|
return 0;
|
2018-02-19 21:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t user_crypt_aes(smc_args_t *args) {
|
|
|
|
uint32_t keyslot = args->X[1] & 3;
|
|
|
|
uint32_t mode = (args->X[1] >> 4) & 3;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-19 21:26:37 +00:00
|
|
|
uint64_t iv_ctr[2];
|
|
|
|
iv_ctr[0] = args->X[2];
|
|
|
|
iv_ctr[1] = args->X[3];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-19 21:26:37 +00:00
|
|
|
uint32_t in_ll_paddr = (uint32_t)(args->X[4]);
|
|
|
|
uint32_t out_ll_paddr = (uint32_t)(args->X[5]);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-19 21:26:37 +00:00
|
|
|
size_t size = args->X[6];
|
|
|
|
if (size & 0xF) {
|
2018-03-25 22:05:08 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_500) {
|
|
|
|
/* Disallow dma lists outside of safe range. */
|
|
|
|
if (in_ll_paddr - 0x80000000 >= 0x3FF7F5) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (out_ll_paddr - 0x80000000 >= 0x3FF7F5) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-02-19 21:26:37 +00:00
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
set_crypt_aes_done(false);
|
2018-02-19 21:26:37 +00:00
|
|
|
|
|
|
|
uint64_t result = 0;
|
|
|
|
switch (mode) {
|
|
|
|
case 0: /* CBC Encryption */
|
|
|
|
se_aes_cbc_encrypt_insecure(keyslot, out_ll_paddr, in_ll_paddr, size, iv_ctr, crypt_aes_done_handler);
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
case 1: /* CBC Decryption */
|
|
|
|
se_aes_cbc_decrypt_insecure(keyslot, out_ll_paddr, in_ll_paddr, size, iv_ctr, crypt_aes_done_handler);
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
case 2: /* CTR "Encryption" */
|
|
|
|
se_aes_ctr_crypt_insecure(keyslot, out_ll_paddr, in_ll_paddr, size, iv_ctr, crypt_aes_done_handler);
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
default:
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-19 21:26:37 +00:00
|
|
|
return result;
|
2018-02-20 04:28:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
uint32_t user_generate_specific_aes_key(smc_args_t *args) {
|
|
|
|
uint64_t wrapped_key[2];
|
|
|
|
uint8_t key[0x10];
|
|
|
|
unsigned int master_key_rev;
|
2018-02-23 03:58:39 +00:00
|
|
|
bool should_mask;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
wrapped_key[0] = args->X[1];
|
|
|
|
wrapped_key[1] = args->X[2];
|
2018-03-07 14:50:50 +00:00
|
|
|
|
2018-03-14 15:03:16 +00:00
|
|
|
master_key_rev = (unsigned int)args->X[3];
|
|
|
|
if (master_key_rev > 0) {
|
|
|
|
master_key_rev -= 1;
|
2018-03-07 14:50:50 +00:00
|
|
|
}
|
2018-03-25 22:05:08 +01:00
|
|
|
if (exosphere_get_target_firmware() < EXOSPHERE_TARGET_FIRMWARE_400) {
|
|
|
|
master_key_rev = 0;
|
|
|
|
}
|
2018-03-14 15:03:16 +00:00
|
|
|
|
|
|
|
if (master_key_rev >= MASTERKEY_REVISION_MAX) {
|
2018-02-20 21:25:57 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:50:50 +00:00
|
|
|
|
|
|
|
if (args->X[4] > 1) {
|
2018-02-20 21:25:57 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:50:50 +00:00
|
|
|
should_mask = args->X[4] != 0;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
unsigned int keyslot;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
/* Behavior changed in 5.0.0. */
|
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_500) {
|
|
|
|
keyslot = devkey_get_keyslot(master_key_rev);
|
|
|
|
} else if (exosphere_get_target_firmware() == EXOSPHERE_TARGET_FIRMWARE_400) {
|
|
|
|
if (master_key_rev >= 1) {
|
2018-02-20 21:25:57 +00:00
|
|
|
keyslot = KEYSLOT_SWITCH_DEVICEKEY; /* New device key, 4.x. */
|
|
|
|
} else {
|
|
|
|
keyslot = KEYSLOT_SWITCH_4XOLDDEVICEKEY; /* Old device key, 4.x. */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
keyslot = KEYSLOT_SWITCH_DEVICEKEY;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-25 09:21:52 +00:00
|
|
|
if (fuse_get_bootrom_patch_version() < 0x7F) {
|
2018-02-20 21:25:57 +00:00
|
|
|
/* On dev units, use a fixed "all-zeroes" seed. */
|
|
|
|
/* Yes, this data really is all-zero in actual TrustZone .rodata. */
|
|
|
|
uint8_t dev_specific_aes_key_source[0x10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
|
|
|
uint8_t dev_specific_aes_key_ctr[0x10] = {0x3C, 0xD5, 0x92, 0xEC, 0x68, 0x31, 0x4A, 0x06, 0xD4, 0x1B, 0x0C, 0xD9, 0xF6, 0x2E, 0xD9, 0xE9};
|
|
|
|
uint8_t dev_specific_aes_key_mask[0x10] = {0xAC, 0xCA, 0x9A, 0xCA, 0xFF, 0x2E, 0xB9, 0x22, 0xCC, 0x1F, 0x4F, 0xAD, 0xDD, 0x77, 0x21, 0x1E};
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-24 04:38:21 +00:00
|
|
|
flush_dcache_range(key, key + 0x10);
|
2018-02-20 21:25:57 +00:00
|
|
|
se_aes_ctr_crypt(keyslot, key, 0x10, dev_specific_aes_key_source, 0x10, dev_specific_aes_key_ctr, 0x10);
|
2018-02-24 04:38:21 +00:00
|
|
|
flush_dcache_range(key, key + 0x10);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
if (should_mask) {
|
|
|
|
for (unsigned int i = 0; i < 0x10; i++) {
|
|
|
|
key[i] ^= dev_specific_aes_key_mask[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* On retail, standard kek->key decryption. */
|
|
|
|
uint8_t retail_specific_aes_key_source[0x10] = {0xE2, 0xD6, 0xB8, 0x7A, 0x11, 0x9C, 0xB8, 0x80, 0xE8, 0x22, 0x88, 0x8A, 0x46, 0xFB, 0xA1, 0x95};
|
|
|
|
decrypt_data_into_keyslot(KEYSLOT_SWITCH_TEMPKEY, keyslot, retail_specific_aes_key_source, 0x10);
|
|
|
|
se_aes_ecb_decrypt_block(KEYSLOT_SWITCH_TEMPKEY, key, 0x10, wrapped_key, 0x10);
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
args->X[1] = read64le(key, 0);
|
|
|
|
args->X[2] = read64le(key, 8);
|
2018-02-20 21:25:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-20 04:28:37 +00:00
|
|
|
uint32_t user_compute_cmac(smc_args_t *args) {
|
|
|
|
uint32_t keyslot = (uint32_t)args->X[1];
|
|
|
|
void *user_address = (void *)args->X[2];
|
|
|
|
size_t size = (size_t)args->X[3];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 04:28:37 +00:00
|
|
|
uint8_t user_data[0x400];
|
|
|
|
uint64_t result_cmac[2];
|
|
|
|
upage_ref_t page_ref;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 04:28:37 +00:00
|
|
|
/* Validate keyslot and size. */
|
|
|
|
if (keyslot > 3 || args->X[3] > 0x400) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 04:56:12 +00:00
|
|
|
if (upage_init(&page_ref, user_address) == 0 || user_copy_to_secure(&page_ref, user_data, user_address, size) == 0) {
|
2018-02-20 04:28:37 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-03-14 15:03:16 +00:00
|
|
|
|
2018-02-20 04:28:37 +00:00
|
|
|
flush_dcache_range(user_data, user_data + size);
|
2018-03-08 12:59:00 +00:00
|
|
|
|
2018-02-20 04:28:37 +00:00
|
|
|
se_compute_aes_128_cmac(keyslot, result_cmac, 0x10, user_data, size);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 04:28:37 +00:00
|
|
|
/* Copy CMAC out. */
|
|
|
|
args->X[1] = result_cmac[0];
|
|
|
|
args->X[2] = result_cmac[1];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:09:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:47:58 +00:00
|
|
|
uint32_t user_load_rsa_oaep_key(smc_args_t *args) {
|
2018-02-20 21:25:57 +00:00
|
|
|
uint64_t sealed_kek[2];
|
|
|
|
uint64_t wrapped_key[2];
|
2018-02-23 03:58:39 +00:00
|
|
|
bool is_personalized;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
uint8_t user_data[0x400];
|
|
|
|
void *user_address;
|
|
|
|
size_t size;
|
|
|
|
upage_ref_t page_ref;
|
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
/* This function no longer exists in 5.x+. */
|
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_500) {
|
|
|
|
generic_panic();
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
/* Copy keydata */
|
|
|
|
sealed_kek[0] = args->X[1];
|
|
|
|
sealed_kek[1] = args->X[2];
|
|
|
|
if (args->X[3] > 1) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-02-23 03:58:39 +00:00
|
|
|
is_personalized = args->X[3] != 0;
|
2018-02-20 21:25:57 +00:00
|
|
|
user_address = (void *)args->X[4];
|
2018-02-22 22:18:34 +00:00
|
|
|
size = (size_t)args->X[5];
|
2018-02-20 21:25:57 +00:00
|
|
|
wrapped_key[0] = args->X[6];
|
|
|
|
wrapped_key[1] = args->X[7];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
if (is_personalized && size != 0x240) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-02-25 09:21:52 +00:00
|
|
|
if (!is_personalized && (size != 0x220 || fuse_get_bootrom_patch_version() >= 0x7F)) {
|
2018-02-20 21:25:57 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
if (upage_init(&page_ref, user_address) == 0 || user_copy_to_secure(&page_ref, user_data, user_address, size) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-14 15:03:16 +00:00
|
|
|
|
2018-03-08 12:59:00 +00:00
|
|
|
flush_dcache_range(user_data, user_data + size);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
/* Ensure that our private key is 0x100 bytes. */
|
2018-03-25 22:05:08 +01:00
|
|
|
if (gcm_decrypt_key(user_data, size, user_data, size, sealed_kek, 0x10, wrapped_key, 0x10, CRYPTOUSECASE_RSAOAEP, is_personalized, NULL) < 0x100) {
|
2018-02-20 21:25:57 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
memcpy(g_imported_exponents[0], user_data, 0x100);
|
2018-02-20 21:25:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-20 20:56:41 +00:00
|
|
|
uint32_t user_decrypt_rsa_private_key(smc_args_t *args) {
|
|
|
|
uint64_t sealed_kek[2];
|
|
|
|
uint64_t wrapped_key[2];
|
2018-02-23 03:58:39 +00:00
|
|
|
bool is_personalized;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 20:56:41 +00:00
|
|
|
uint8_t user_data[0x400];
|
|
|
|
void *user_address;
|
|
|
|
size_t size;
|
|
|
|
upage_ref_t page_ref;
|
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
/* This function no longer exists in 5.x+. */
|
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_500) {
|
|
|
|
generic_panic();
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 20:56:41 +00:00
|
|
|
/* Copy keydata */
|
|
|
|
sealed_kek[0] = args->X[1];
|
|
|
|
sealed_kek[1] = args->X[2];
|
|
|
|
if (args->X[3] > 1) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-02-23 03:58:39 +00:00
|
|
|
is_personalized = args->X[3] != 0;
|
2018-02-20 20:56:41 +00:00
|
|
|
user_address = (void *)args->X[4];
|
2018-02-20 23:53:23 +00:00
|
|
|
size = (size_t)args->X[5];
|
2018-02-20 20:56:41 +00:00
|
|
|
wrapped_key[0] = args->X[6];
|
|
|
|
wrapped_key[1] = args->X[7];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 20:56:41 +00:00
|
|
|
if (size > 0x240) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 20:56:41 +00:00
|
|
|
if (is_personalized && size < 0x31) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-02-25 09:21:52 +00:00
|
|
|
if (!is_personalized && (size < 0x11 || fuse_get_bootrom_patch_version() >= 0x7F)) {
|
2018-02-20 20:56:41 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 20:56:41 +00:00
|
|
|
if (upage_init(&page_ref, user_address) == 0 || user_copy_to_secure(&page_ref, user_data, user_address, size) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-14 15:03:16 +00:00
|
|
|
|
2018-03-08 12:59:00 +00:00
|
|
|
flush_dcache_range(user_data, user_data + size);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 20:56:41 +00:00
|
|
|
size_t out_size;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
if ((out_size = gcm_decrypt_key(user_data, size, user_data, size, sealed_kek, 0x10, wrapped_key, 0x10, CRYPTOUSECASE_RSAPRIVATE, is_personalized, NULL)) == 0) {
|
2018-02-20 20:56:41 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 20:56:41 +00:00
|
|
|
if (secure_copy_to_user(&page_ref, user_address, user_data, size) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
args->X[1] = out_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:47:58 +00:00
|
|
|
uint32_t user_load_secure_exp_mod_key(smc_args_t *args) {
|
2018-02-20 21:25:57 +00:00
|
|
|
uint64_t sealed_kek[2];
|
|
|
|
uint64_t wrapped_key[2];
|
2018-02-23 03:58:39 +00:00
|
|
|
bool is_personalized;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
uint8_t user_data[0x400];
|
|
|
|
void *user_address;
|
|
|
|
size_t size;
|
|
|
|
upage_ref_t page_ref;
|
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
/* This function no longer exists in 5.x+. */
|
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_500) {
|
|
|
|
generic_panic();
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
/* Copy keydata */
|
|
|
|
sealed_kek[0] = args->X[1];
|
|
|
|
sealed_kek[1] = args->X[2];
|
|
|
|
if (args->X[3] > 1) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-02-23 03:58:39 +00:00
|
|
|
is_personalized = args->X[3] != 0;
|
2018-02-20 21:25:57 +00:00
|
|
|
user_address = (void *)args->X[4];
|
2018-02-22 22:18:34 +00:00
|
|
|
size = (size_t)args->X[5];
|
2018-02-20 21:25:57 +00:00
|
|
|
wrapped_key[0] = args->X[6];
|
|
|
|
wrapped_key[1] = args->X[7];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
if (is_personalized && size != 0x130) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-02-25 09:21:52 +00:00
|
|
|
if (!is_personalized && (size != 0x110 || fuse_get_bootrom_patch_version() >= 0x7F)) {
|
2018-02-20 21:25:57 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
if (upage_init(&page_ref, user_address) == 0 || user_copy_to_secure(&page_ref, user_data, user_address, size) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-14 15:03:16 +00:00
|
|
|
|
2018-03-08 12:59:00 +00:00
|
|
|
flush_dcache_range(user_data, user_data + size);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
size_t out_size;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
/* Ensure that our key is non-zero bytes. */
|
2018-03-25 22:05:08 +01:00
|
|
|
if ((out_size = gcm_decrypt_key(user_data, size, user_data, size, sealed_kek, 0x10, wrapped_key, 0x10, CRYPTOUSECASE_SECUREEXPMOD, is_personalized, NULL)) == 0) {
|
2018-02-20 21:25:57 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
/* Copy key to global. */
|
|
|
|
if (out_size <= 0x100) {
|
2018-03-25 22:05:08 +01:00
|
|
|
memcpy(g_imported_exponents[1], user_data, out_size);
|
2018-02-20 21:25:57 +00:00
|
|
|
} else {
|
2018-03-25 22:05:08 +01:00
|
|
|
memcpy(g_imported_exponents[1], user_data, 0x100);
|
2018-02-20 21:25:57 +00:00
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 21:25:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:47:58 +00:00
|
|
|
uint32_t user_secure_exp_mod(smc_args_t *args) {
|
2018-02-20 05:09:53 +00:00
|
|
|
uint8_t modulus[0x100];
|
|
|
|
uint8_t input[0x100];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:09:53 +00:00
|
|
|
upage_ref_t page_ref;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:09:53 +00:00
|
|
|
void *user_input = (void *)args->X[1];
|
|
|
|
void *user_modulus = (void *)args->X[2];
|
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
unsigned int exponent_id = 1;
|
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_500) {
|
|
|
|
switch (args->X[3]) {
|
|
|
|
case 0:
|
|
|
|
exponent_id = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
exponent_id = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
exponent_id = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-20 05:09:53 +00:00
|
|
|
/* Copy user data into secure memory. */
|
|
|
|
if (upage_init(&page_ref, user_input) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (user_copy_to_secure(&page_ref, input, user_input, 0x100) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (user_copy_to_secure(&page_ref, modulus, user_modulus, 0x100) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
set_exp_mod_done(false);
|
2018-02-20 05:09:53 +00:00
|
|
|
/* Hardcode RSA keyslot 0. */
|
2018-03-25 22:05:08 +01:00
|
|
|
set_rsa_keyslot(0, modulus, 0x100, g_imported_exponents[exponent_id], 0x100);
|
2018-02-20 05:09:53 +00:00
|
|
|
se_exp_mod(0, input, 0x100, exp_mod_done_handler);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 04:28:37 +00:00
|
|
|
return 0;
|
2018-02-20 05:47:11 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 02:47:58 +00:00
|
|
|
uint32_t user_unwrap_rsa_oaep_wrapped_titlekey(smc_args_t *args) {
|
2018-02-20 05:47:11 +00:00
|
|
|
uint8_t modulus[0x100];
|
|
|
|
uint8_t wrapped_key[0x100];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
upage_ref_t page_ref;
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
void *user_wrapped_key = (void *)args->X[1];
|
|
|
|
void *user_modulus = (void *)args->X[2];
|
|
|
|
unsigned int master_key_rev = (unsigned int)args->X[7];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-03-14 15:03:16 +00:00
|
|
|
if(master_key_rev > 0) {
|
|
|
|
master_key_rev -= 1;
|
|
|
|
}
|
|
|
|
|
2018-03-25 22:05:08 +01:00
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_300 && master_key_rev >= MASTERKEY_REVISION_MAX) {
|
2018-02-20 07:41:27 +00:00
|
|
|
return 2;
|
2018-03-09 03:48:35 +00:00
|
|
|
} else {
|
|
|
|
master_key_rev = 0;
|
2018-02-20 07:41:27 +00:00
|
|
|
}
|
2018-02-20 05:47:11 +00:00
|
|
|
|
|
|
|
/* Copy user data into secure memory. */
|
|
|
|
if (upage_init(&page_ref, user_wrapped_key) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (user_copy_to_secure(&page_ref, wrapped_key, user_wrapped_key, 0x100) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (user_copy_to_secure(&page_ref, modulus, user_modulus, 0x100) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-23 03:58:39 +00:00
|
|
|
set_exp_mod_done(false);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-21 02:47:58 +00:00
|
|
|
/* Expected label_hash occupies args->X[3] to args->X[6]. */
|
|
|
|
tkey_set_expected_label_hash(&args->X[3]);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
tkey_set_master_key_rev(master_key_rev);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
/* Hardcode RSA keyslot 0. */
|
2018-03-25 22:05:08 +01:00
|
|
|
set_rsa_keyslot(0, modulus, 0x100, g_imported_exponents[0], 0x100);
|
2018-02-20 05:47:11 +00:00
|
|
|
se_exp_mod(0, wrapped_key, 0x100, exp_mod_done_handler);
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t user_load_titlekey(smc_args_t *args) {
|
|
|
|
uint64_t sealed_titlekey[2];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
uint32_t keyslot = (uint32_t)args->X[1];
|
|
|
|
if (keyslot > 3) {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
/* Copy keydata */
|
|
|
|
sealed_titlekey[0] = args->X[2];
|
|
|
|
sealed_titlekey[1] = args->X[3];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
/* Unseal the key. */
|
2018-02-20 09:02:01 +00:00
|
|
|
unseal_titlekey(keyslot, sealed_titlekey, 0x10);
|
2018-02-20 05:47:11 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t user_unwrap_aes_wrapped_titlekey(smc_args_t *args) {
|
|
|
|
uint64_t aes_wrapped_titlekey[2];
|
|
|
|
uint8_t titlekey[0x10];
|
|
|
|
uint64_t sealed_titlekey[2];
|
2018-03-07 14:11:53 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
aes_wrapped_titlekey[0] = args->X[1];
|
|
|
|
aes_wrapped_titlekey[1] = args->X[2];
|
|
|
|
unsigned int master_key_rev = (unsigned int)args->X[3];
|
2018-02-25 19:00:50 +00:00
|
|
|
|
2018-03-14 15:03:16 +00:00
|
|
|
if (master_key_rev > 0) {
|
|
|
|
master_key_rev -= 1;
|
|
|
|
}
|
2018-03-25 22:05:08 +01:00
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_300 && master_key_rev >= MASTERKEY_REVISION_MAX) {
|
2018-02-20 07:41:27 +00:00
|
|
|
return 2;
|
2018-03-09 03:48:35 +00:00
|
|
|
} else {
|
|
|
|
master_key_rev = 0;
|
2018-02-20 07:41:27 +00:00
|
|
|
}
|
2018-02-25 19:00:50 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
tkey_set_master_key_rev(master_key_rev);
|
2018-02-25 19:00:50 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
tkey_aes_unwrap(titlekey, 0x10, aes_wrapped_titlekey, 0x10);
|
2018-02-20 09:02:01 +00:00
|
|
|
seal_titlekey(sealed_titlekey, 0x10, titlekey, 0x10);
|
2018-02-25 19:00:50 +00:00
|
|
|
|
2018-02-20 05:47:11 +00:00
|
|
|
args->X[1] = sealed_titlekey[0];
|
|
|
|
args->X[2] = sealed_titlekey[1];
|
2018-02-25 19:00:50 +00:00
|
|
|
|
|
|
|
return 0; /* FIXME: what should we return there */
|
2018-02-20 23:53:23 +00:00
|
|
|
}
|
2018-03-25 22:05:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
uint32_t user_encrypt_rsa_key_for_import(smc_args_t *args) {
|
|
|
|
uint64_t in_sealed_kek[2];
|
|
|
|
uint64_t out_sealed_kek[2];
|
|
|
|
uint64_t in_wrapped_key[2];
|
|
|
|
uint64_t out_wrapped_key[2];
|
|
|
|
uint8_t usecase;
|
|
|
|
|
|
|
|
uint8_t user_data[0x400];
|
|
|
|
void *user_address;
|
|
|
|
void *user_in_kek;
|
|
|
|
void *user_out_kek;
|
|
|
|
void *user_in_key;
|
|
|
|
void *user_out_key;
|
|
|
|
size_t size;
|
|
|
|
upage_ref_t page_ref;
|
|
|
|
|
|
|
|
/* Copy keydata */
|
|
|
|
user_in_kek = (void *)args->X[1];
|
|
|
|
user_out_kek = (void *)args->X[2];
|
|
|
|
usecase = args->X[3] & 7;
|
|
|
|
user_address = (void *)args->X[4];
|
|
|
|
size = (size_t)args->X[5];
|
|
|
|
user_in_key = (void *)args->X[6];
|
|
|
|
user_out_key = (void *)args->X[7];
|
|
|
|
|
|
|
|
if (usecase > CRYPTOUSECASE_RSAIMPORT) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (usecase == 0) {
|
|
|
|
if (size < 0x31 || size > 0x240) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
} else if (size < 0x130 || size > 0x240) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upage_init(&page_ref, user_address) == 0
|
|
|
|
|| user_copy_to_secure(&page_ref, user_data, user_address, size) == 0
|
|
|
|
|| user_copy_to_secure(&page_ref, in_sealed_kek, user_in_kek, 0x10) == 0
|
|
|
|
|| user_copy_to_secure(&page_ref, out_sealed_kek, user_out_kek, 0x10) == 0
|
|
|
|
|| user_copy_to_secure(&page_ref, in_wrapped_key, user_in_key, 0x10) == 0
|
|
|
|
|| user_copy_to_secure(&page_ref, out_wrapped_key, user_out_key, 0x10) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
flush_dcache_range(user_data, user_data + size);
|
|
|
|
|
|
|
|
size_t out_size;
|
|
|
|
|
|
|
|
uint8_t device_id_high;
|
|
|
|
|
|
|
|
if ((out_size = gcm_decrypt_key(user_data, size, user_data, size, in_sealed_kek, 0x10, in_wrapped_key, 0x10, CRYPTOUSECASE_RSAIMPORT, true, &device_id_high)) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcm_encrypt_key(user_data, size, user_data, size - 0x30, out_sealed_kek, 0x10, out_wrapped_key, 0x10, g_rsausecase_to_cryptousecase[usecase], device_id_high);
|
|
|
|
|
|
|
|
if (secure_copy_to_user(&page_ref, user_address, user_data, size) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t user_decrypt_or_import_rsa_key(smc_args_t *args) {
|
|
|
|
uint64_t sealed_kek[2];
|
|
|
|
uint64_t wrapped_key[2];
|
|
|
|
uint8_t usecase;
|
|
|
|
|
|
|
|
uint8_t user_data[0x400];
|
|
|
|
void *user_address;
|
|
|
|
size_t size;
|
|
|
|
upage_ref_t page_ref;
|
|
|
|
|
|
|
|
/* This function no longer exists in 5.x+. */
|
|
|
|
if (exosphere_get_target_firmware() >= EXOSPHERE_TARGET_FIRMWARE_500) {
|
|
|
|
generic_panic();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy keydata */
|
|
|
|
sealed_kek[0] = args->X[1];
|
|
|
|
sealed_kek[1] = args->X[2];
|
|
|
|
usecase = args->X[3] & 7;
|
|
|
|
user_address = (void *)args->X[4];
|
|
|
|
size = (size_t)args->X[5];
|
|
|
|
wrapped_key[0] = args->X[6];
|
|
|
|
wrapped_key[1] = args->X[7];
|
|
|
|
|
|
|
|
if (usecase > CRYPTOUSECASE_RSAIMPORT) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (usecase == 0) {
|
|
|
|
if (size < 0x31 || size > 0x240) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
} else if (size < 0x130 || size > 0x240) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upage_init(&page_ref, user_address) == 0 || user_copy_to_secure(&page_ref, user_data, user_address, size) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
flush_dcache_range(user_data, user_data + size);
|
|
|
|
|
|
|
|
size_t out_size;
|
|
|
|
|
|
|
|
if ((out_size = gcm_decrypt_key(user_data, size, user_data, size, sealed_kek, 0x10, wrapped_key, 0x10, g_rsausecase_to_cryptousecase[usecase], true, NULL)) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int exponent_id;
|
|
|
|
|
|
|
|
switch (usecase) {
|
|
|
|
case 0:
|
|
|
|
if (secure_copy_to_user(&page_ref, user_address, user_data, size) == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case 1:
|
|
|
|
exponent_id = 1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
exponent_id = 0;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
exponent_id = 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
exponent_id = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
generic_panic();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy key to global. */
|
|
|
|
memcpy(g_imported_exponents[exponent_id], user_data, 0x100);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|