|
| 1 | + |
| 2 | +#include "rsa.h" |
| 3 | +#include "jade_assert.h" |
| 4 | +#include "keychain.h" |
| 5 | +#include "sensitive.h" |
| 6 | +#include "utils/shake256.h" |
| 7 | +#include "wallet.h" |
| 8 | + |
| 9 | +#include <mbedtls/pk.h> |
| 10 | +#include <mbedtls/rsa.h> |
| 11 | + |
| 12 | +// Function to get bip85-generated rsa context |
| 13 | +// See: https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki |
| 14 | +static bool get_bip85_rsa_ctx(const size_t key_bits, const size_t index, mbedtls_rsa_context* output) |
| 15 | +{ |
| 16 | + JADE_ASSERT(key_bits <= MAX_RSA_GEN_KEY_LEN); |
| 17 | + JADE_ASSERT(RSA_KEY_SIZE_VALID(key_bits)); |
| 18 | + // index can be 0 |
| 19 | + JADE_ASSERT(output); |
| 20 | + |
| 21 | + JADE_ASSERT(keychain_get()); |
| 22 | + JADE_LOGI("Deriving BIP85 RSA context for index %u, key length: %u", index, key_bits); |
| 23 | + |
| 24 | + uint8_t entropy[HMAC_SHA512_LEN]; |
| 25 | + size_t entropy_len = 0; |
| 26 | + wallet_get_bip85_rsa_entropy(key_bits, index, entropy, sizeof(entropy), &entropy_len); |
| 27 | + JADE_ASSERT(entropy_len == 64); |
| 28 | + |
| 29 | + struct shake256_ctx sctx = {}; |
| 30 | + shake256_init(&sctx, entropy, entropy_len); |
| 31 | + if (mbedtls_rsa_gen_key(output, shake256_mbedtls_rnd_cb, &sctx, key_bits, 65537) != 0) { |
| 32 | + JADE_LOGE("Failed to create/setup key from rsa context"); |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + return true; |
| 37 | +} |
| 38 | + |
| 39 | +// Function to get bip85-generated rsa key pem |
| 40 | +bool rsa_get_bip85_pubkey_pem(const size_t key_bits, const size_t index, char* output, const size_t output_len) |
| 41 | +{ |
| 42 | + JADE_ASSERT(key_bits <= MAX_RSA_GEN_KEY_LEN); |
| 43 | + JADE_ASSERT(RSA_KEY_SIZE_VALID(key_bits)); |
| 44 | + // index can be 0 |
| 45 | + JADE_ASSERT(output); |
| 46 | + JADE_ASSERT(output_len); |
| 47 | + |
| 48 | + JADE_ASSERT(keychain_get()); |
| 49 | + bool retval = false; |
| 50 | + |
| 51 | + mbedtls_rsa_context rsa = {}; |
| 52 | + mbedtls_rsa_init(&rsa); |
| 53 | + SENSITIVE_PUSH(&rsa, sizeof(rsa)); |
| 54 | + |
| 55 | + mbedtls_pk_context pk; |
| 56 | + mbedtls_pk_init(&pk); |
| 57 | + SENSITIVE_PUSH(&pk, sizeof(pk)); |
| 58 | + |
| 59 | + if (mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) != 0) { |
| 60 | + JADE_LOGE("Failed to create/setup key from rsa context"); |
| 61 | + goto cleanup; |
| 62 | + } |
| 63 | + |
| 64 | + if (!get_bip85_rsa_ctx(key_bits, index, &rsa)) { |
| 65 | + JADE_LOGE("Failed to generate bip85 rsa ctx"); |
| 66 | + goto cleanup; |
| 67 | + } |
| 68 | + |
| 69 | + if (mbedtls_rsa_copy(mbedtls_pk_rsa(pk), &rsa) != 0) { |
| 70 | + JADE_LOGE("Failed to copy key from rsa context"); |
| 71 | + goto cleanup; |
| 72 | + } |
| 73 | + |
| 74 | + if (mbedtls_pk_write_pubkey_pem(&pk, (uint8_t*)output, output_len) != 0) { |
| 75 | + JADE_LOGE("Failed to write pubkey to buffer of length %u", output_len); |
| 76 | + goto cleanup; |
| 77 | + } |
| 78 | + |
| 79 | + retval = true; |
| 80 | + |
| 81 | +cleanup: |
| 82 | + mbedtls_pk_free(&pk); |
| 83 | + SENSITIVE_POP(&pk); |
| 84 | + |
| 85 | + mbedtls_rsa_free(&rsa); |
| 86 | + SENSITIVE_POP(&rsa); |
| 87 | + |
| 88 | + return retval; |
| 89 | +} |
0 commit comments