From 59281c5a97b75f9289ebc659aad7cbb8b8aada3d Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 19 Feb 2018 21:09:53 -0800 Subject: [PATCH] Implement smcRsaOaep --- exosphere/smc_api.c | 4 ++++ exosphere/smc_user.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/exosphere/smc_api.c b/exosphere/smc_api.c index 903363b17..d6865d82c 100644 --- a/exosphere/smc_api.c +++ b/exosphere/smc_api.c @@ -301,6 +301,10 @@ uint32_t smc_compute_cmac(smc_args_t *args) { return smc_wrapper_sync(args, user_compute_cmac); } +uint32_t smc_rsa_oaep(smc_args_t *args) { + return smc_wrapper_async(args, user_rsa_oaep, smc_exp_mod_get_result); +} + uint32_t smc_cpu_on(smc_args_t *args) { diff --git a/exosphere/smc_user.c b/exosphere/smc_user.c index 25f82b9b5..dbae79ecb 100644 --- a/exosphere/smc_user.c +++ b/exosphere/smc_user.c @@ -11,6 +11,9 @@ int g_crypt_aes_done = 0; int g_exp_mod_done = 0; +uint8_t g_rsa_oaep_exponent[0x100]; +uint8_t g_rsa_private_exponent[0x100]; + void set_exp_mod_done(int done) { g_exp_mod_done = done & 1; @@ -176,5 +179,35 @@ uint32_t user_compute_cmac(smc_args_t *args) { args->X[1] = result_cmac[0]; args->X[2] = result_cmac[1]; + return 0; +} + +uint32_t user_rsa_oaep(smc_args_t *args) { + uint8_t modulus[0x100]; + uint8_t input[0x100]; + + upage_ref_t page_ref; + + size_t exponent_size = (size_t)args->X[4]; + + void *user_input = (void *)args->X[1]; + void *user_modulus = (void *)args->X[2]; + + /* 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; + } + + set_exp_mod_done(0); + /* Hardcode RSA keyslot 0. */ + set_rsa_keyslot(0, modulus, 0x100, g_rsa_oaep_exponent, 0x100); + se_exp_mod(0, input, 0x100, exp_mod_done_handler); + return 0; } \ No newline at end of file