0.2.0-dev Download ZIP
106 lines ·3.4 KB Raw
| 1 | /* |
| 2 | * RSA setters for PK. |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "tf_psa_crypto_common.h" |
| 9 | |
| 10 | #include "mbedtls/pk.h" |
| 11 | #include "mbedtls/private/error_common.h" |
| 12 | #include "pk_internal.h" |
| 13 | |
| 14 | #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) |
| 15 | |
| 16 | int mbedtls_pk_rsa_set_key(mbedtls_pk_context *pk, const unsigned char *key, size_t key_len) |
| 17 | { |
| 18 | psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT; |
| 19 | psa_status_t status; |
| 20 | size_t key_bits = 0; |
| 21 | |
| 22 | pk->psa_type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
| 23 | psa_set_key_type(&attr, PSA_KEY_TYPE_RSA_KEY_PAIR); |
| 24 | psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 25 | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 26 | PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY); |
| 27 | psa_set_key_algorithm(&attr, PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH)); |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 29 | psa_set_key_enrollment_algorithm(&attr, PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH)); |
| 30 | #endif |
| 31 | |
| 32 | status = psa_import_key(&attr, key, key_len, &pk->priv_id); |
| 33 | if (status != PSA_SUCCESS) { |
| 34 | return psa_pk_status_to_mbedtls(status); |
| 35 | } |
| 36 | |
| 37 | /* psa_import_key() will also determine the size of the key in bits during import. |
| 38 | * We use this to update the PK context structure as well. */ |
| 39 | status = psa_get_key_attributes(pk->priv_id, &attr); |
| 40 | if (status != PSA_SUCCESS) { |
| 41 | psa_destroy_key(pk->priv_id); |
| 42 | return psa_pk_status_to_mbedtls(status); |
| 43 | } |
| 44 | |
| 45 | key_bits = psa_get_key_bits(&attr); |
| 46 | /* If "bits" was already setup previously its value must be correct. */ |
| 47 | if ((pk->bits != 0) && (pk->bits != key_bits)) { |
| 48 | psa_destroy_key(pk->priv_id); |
| 49 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 50 | } |
| 51 | pk->bits = key_bits; |
| 52 | |
| 53 | psa_reset_key_attributes(&attr); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | int mbedtls_pk_rsa_set_pubkey(mbedtls_pk_context *pk, const unsigned char *key, size_t key_len) |
| 59 | { |
| 60 | psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT; |
| 61 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 62 | psa_status_t status; |
| 63 | size_t key_bits; |
| 64 | |
| 65 | if (key_len > sizeof(pk->pub_raw)) { |
| 66 | return MBEDTLS_ERR_PK_INVALID_PUBKEY; |
| 67 | } |
| 68 | |
| 69 | /* Try importing the key to check if it's valid. */ |
| 70 | psa_set_key_type(&attr, PSA_KEY_TYPE_RSA_PUBLIC_KEY); |
| 71 | psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 72 | PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY); |
| 73 | psa_set_key_algorithm(&attr, PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH)); |
| 74 | |
| 75 | status = psa_import_key(&attr, key, key_len, &key_id); |
| 76 | if (status != PSA_SUCCESS) { |
| 77 | return MBEDTLS_ERR_PK_INVALID_PUBKEY; |
| 78 | } |
| 79 | |
| 80 | status = psa_get_key_attributes(key_id, &attr); |
| 81 | if (status != PSA_SUCCESS) { |
| 82 | psa_destroy_key(key_id); |
| 83 | return psa_pk_status_to_mbedtls(status); |
| 84 | } |
| 85 | |
| 86 | status = psa_destroy_key(key_id); |
| 87 | if (status != PSA_SUCCESS) { |
| 88 | psa_reset_key_attributes(&attr); |
| 89 | return psa_pk_status_to_mbedtls(status); |
| 90 | } |
| 91 | |
| 92 | key_bits = psa_get_key_bits(&attr); |
| 93 | psa_reset_key_attributes(&attr); |
| 94 | if ((pk->bits != 0) && (pk->bits != key_bits)) { |
| 95 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 96 | } |
| 97 | pk->bits = key_bits; |
| 98 | |
| 99 | memcpy(pk->pub_raw, key, key_len); |
| 100 | pk->pub_raw_len = key_len; |
| 101 | |
| 102 | pk->psa_type = PSA_KEY_TYPE_RSA_PUBLIC_KEY; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */ |