2020-04-16 01:06:41 +01:00
|
|
|
/*
|
2020-07-03 10:31:22 +01:00
|
|
|
* rsa.c
|
2020-04-16 01:06:41 +01:00
|
|
|
*
|
2020-07-03 10:31:22 +01:00
|
|
|
* Copyright (c) 2018-2019, SciresM.
|
2022-03-17 12:58:40 +00:00
|
|
|
* Copyright (c) 2020-2022, DarkMatterCore <pabloacurielz@gmail.com>.
|
2020-07-03 10:31:22 +01:00
|
|
|
*
|
|
|
|
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
|
|
|
|
*
|
2021-03-25 19:26:58 +00:00
|
|
|
* nxdumptool is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2020-04-16 01:06:41 +01:00
|
|
|
*
|
2021-03-25 19:26:58 +00:00
|
|
|
* nxdumptool is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2020-04-16 01:06:41 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-03-25 19:26:58 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-04-16 01:06:41 +01:00
|
|
|
*/
|
|
|
|
|
2021-03-26 04:35:14 +00:00
|
|
|
#include "nxdt_utils.h"
|
2020-07-03 10:31:22 +01:00
|
|
|
#include "rsa.h"
|
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
#include <mbedtls/rsa.h>
|
2020-04-11 06:28:26 +01:00
|
|
|
#include <mbedtls/entropy.h>
|
|
|
|
#include <mbedtls/ctr_drbg.h>
|
2021-05-21 14:34:43 +01:00
|
|
|
#include <mbedtls/pk.h>
|
2020-04-11 06:28:26 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
bool rsa2048VerifySha256BasedPssSignature(const void *data, size_t data_size, const void *signature, const void *modulus, const void *public_exponent, size_t public_exponent_size)
|
2020-04-11 06:28:26 +01:00
|
|
|
{
|
2021-05-21 14:34:43 +01:00
|
|
|
if (!data || !data_size || !signature || !modulus || !public_exponent || !public_exponent_size)
|
2020-04-11 06:28:26 +01:00
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid parameters!");
|
2020-04-11 06:28:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
int mbedtls_ret = 0;
|
|
|
|
mbedtls_rsa_context rsa;
|
2020-10-22 05:38:14 +01:00
|
|
|
u8 hash[SHA256_HASH_SIZE] = {0};
|
2021-05-21 14:34:43 +01:00
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
/* Initialize RSA context. */
|
|
|
|
mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
/* Import RSA parameters. */
|
|
|
|
mbedtls_ret = mbedtls_rsa_import_raw(&rsa, (const u8*)modulus, RSA2048_BYTES, NULL, 0, NULL, 0, NULL, 0, (const u8*)public_exponent, public_exponent_size);
|
|
|
|
if (mbedtls_ret != 0)
|
|
|
|
{
|
|
|
|
LOG_MSG("mbedtls_rsa_import_raw failed! (%d).", mbedtls_ret);
|
|
|
|
goto end;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
/* Calculate SHA-256 checksum for the input data. */
|
|
|
|
sha256CalculateHash(hash, data, data_size);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
/* Verify signature. */
|
|
|
|
mbedtls_ret = mbedtls_rsa_rsassa_pss_verify(&rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA256, SHA256_HASH_SIZE, hash, (const u8*)signature);
|
|
|
|
if (mbedtls_ret != 0)
|
|
|
|
{
|
|
|
|
LOG_MSG("mbedtls_rsa_rsassa_pss_verify failed! (%d).", mbedtls_ret);
|
|
|
|
goto end;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
ret = true;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
end:
|
|
|
|
mbedtls_rsa_free(&rsa);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rsa2048OaepDecrypt(void *dst, size_t dst_size, const void *signature, const void *modulus, const void *public_exponent, size_t public_exponent_size, const void *private_exponent, \
|
|
|
|
size_t private_exponent_size, const void *label, size_t label_size, size_t *out_size)
|
2020-04-11 06:28:26 +01:00
|
|
|
{
|
2021-05-21 14:34:43 +01:00
|
|
|
if (!dst || !dst_size || !signature || !modulus || !public_exponent || !public_exponent_size || !private_exponent || !private_exponent_size || (!label && label_size) || (label && !label_size) || \
|
|
|
|
!out_size)
|
2020-04-11 06:28:26 +01:00
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid parameters!");
|
2020-04-11 06:28:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
mbedtls_entropy_context entropy;
|
|
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
|
|
|
mbedtls_rsa_context rsa;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
const char *pers = __func__;
|
|
|
|
int mbedtls_ret = 0;
|
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
/* Initialize contexts. */
|
|
|
|
mbedtls_entropy_init(&entropy);
|
|
|
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
|
|
mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
/* Seed the random number generator. */
|
|
|
|
mbedtls_ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const u8*)pers, strlen(pers));
|
|
|
|
if (mbedtls_ret != 0)
|
2020-04-11 06:28:26 +01:00
|
|
|
{
|
2021-05-21 14:34:43 +01:00
|
|
|
LOG_MSG("mbedtls_ctr_drbg_seed failed! (%d).", mbedtls_ret);
|
|
|
|
goto end;
|
2020-04-11 06:28:26 +01:00
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
/* Import RSA parameters. */
|
|
|
|
mbedtls_ret = mbedtls_rsa_import_raw(&rsa, (const u8*)modulus, RSA2048_BYTES, NULL, 0, NULL, 0, (const u8*)private_exponent, private_exponent_size, (const u8*)public_exponent, public_exponent_size);
|
|
|
|
if (mbedtls_ret != 0)
|
2020-04-11 06:28:26 +01:00
|
|
|
{
|
2021-05-21 14:34:43 +01:00
|
|
|
LOG_MSG("mbedtls_rsa_import_raw failed! (%d).", mbedtls_ret);
|
|
|
|
goto end;
|
2020-04-11 06:28:26 +01:00
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
/* Derive RSA prime factors. */
|
|
|
|
mbedtls_ret = mbedtls_rsa_complete(&rsa);
|
|
|
|
if (mbedtls_ret != 0)
|
2020-04-11 06:28:26 +01:00
|
|
|
{
|
2021-05-21 14:34:43 +01:00
|
|
|
LOG_MSG("mbedtls_rsa_complete failed! (%d).", mbedtls_ret);
|
|
|
|
goto end;
|
2020-04-11 06:28:26 +01:00
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
/* Perform RSA-OAEP decryption. */
|
|
|
|
mbedtls_ret = mbedtls_rsa_rsaes_oaep_decrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, MBEDTLS_RSA_PRIVATE, (const u8*)label, label_size, out_size, (const u8*)signature, (u8*)dst, dst_size);
|
|
|
|
if (mbedtls_ret != 0)
|
2020-04-11 06:28:26 +01:00
|
|
|
{
|
2021-05-21 14:34:43 +01:00
|
|
|
LOG_MSG("mbedtls_rsa_rsaes_oaep_decrypt failed! (%d).", mbedtls_ret);
|
|
|
|
goto end;
|
2020-04-11 06:28:26 +01:00
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
ret = true;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
end:
|
|
|
|
mbedtls_rsa_free(&rsa);
|
|
|
|
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
|
|
mbedtls_entropy_free(&entropy);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-05-21 14:34:43 +01:00
|
|
|
return ret;
|
2020-04-11 06:28:26 +01:00
|
|
|
}
|