Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 01a93bc | 2023-01-20 11:17:14 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2022-2023, Arm Limited. All rights reserved. |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | #include <string.h> |
| 11 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 12 | #include "tfm_sp_log.h" |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 13 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 14 | #include "config_crypto.h" |
| 15 | #include "psa/crypto.h" |
Antonio de Angelis | c4d8a56 | 2022-12-01 14:22:38 +0000 | [diff] [blame] | 16 | #include "psa/error.h" |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 17 | #include "crypto_library.h" |
| 18 | |
Antonio de Angelis | 4118912 | 2022-12-01 14:45:38 +0000 | [diff] [blame] | 19 | /** |
| 20 | * \brief This include is required to get the underlying platform function |
| 21 | * to allow the builtin keys support in mbed TLS to map slots to key |
| 22 | * IDs. |
| 23 | */ |
| 24 | #include "tfm_plat_crypto_keys.h" |
| 25 | |
| 26 | /** |
Antonio de Angelis | c4d8a56 | 2022-12-01 14:22:38 +0000 | [diff] [blame] | 27 | * \brief These includes are required to get the interface that TF-M crypto |
| 28 | * exposes on its client side, in particular regarding key attributes |
| 29 | */ |
| 30 | #include "psa/crypto_client_struct.h" |
| 31 | |
Antonio de Angelis | 4118912 | 2022-12-01 14:45:38 +0000 | [diff] [blame] | 32 | /** |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 33 | * \brief This Mbed TLS include is needed to initialise the memory allocator |
| 34 | * of the library used for internal allocations |
| 35 | */ |
| 36 | #include "mbedtls/memory_buffer_alloc.h" |
Antonio de Angelis | 4118912 | 2022-12-01 14:45:38 +0000 | [diff] [blame] | 37 | |
| 38 | /** |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 39 | * \brief This Mbed TLS include is needed to set the mbedtls_printf to the |
| 40 | * function required by the TF-M framework in order to be able to |
| 41 | * print to terminal through mbedtls_printf |
| 42 | */ |
| 43 | #include "mbedtls/platform.h" |
Antonio de Angelis | 4118912 | 2022-12-01 14:45:38 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 46 | * \brief This Mbed TLS include is needed to retrieve version information for |
| 47 | * display |
| 48 | */ |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 49 | #include "mbedtls/build_info.h" |
| 50 | |
| 51 | #ifndef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
| 52 | #error "MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER must be selected in Mbed TLS config file" |
| 53 | #endif |
| 54 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 55 | /** |
| 56 | * \brief Static buffer containing the string describing the mbed TLS version. mbed TLS |
| 57 | * guarantees that the string will never be greater than 18 bytes |
| 58 | */ |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 59 | static char mbedtls_version_full[18]; |
| 60 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 61 | /** |
| 62 | * \brief Static buffer to be used by Mbed Crypto for memory allocations |
| 63 | * |
| 64 | */ |
| 65 | static uint8_t mbedtls_mem_buf[CRYPTO_ENGINE_BUF_SIZE] = {0}; |
| 66 | |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 67 | /*! |
| 68 | * \defgroup tfm_crypto_library Set of functions implementing the abstractions of the underlying cryptographic |
| 69 | * library that implements the PSA Crypto APIs to provide the PSA Crypto core |
| 70 | * functionality to the TF-M Crypto service. Currently it supports only an |
| 71 | * mbed TLS based abstraction. |
| 72 | */ |
| 73 | /*!@{*/ |
| 74 | tfm_crypto_library_key_id_t tfm_crypto_library_key_id_init(int32_t owner, psa_key_id_t key_id) |
| 75 | { |
| 76 | return mbedtls_svc_key_id_make(owner, key_id); |
| 77 | } |
| 78 | |
| 79 | char *tfm_crypto_library_get_info(void) |
| 80 | { |
| 81 | memcpy(mbedtls_version_full, MBEDTLS_VERSION_STRING_FULL, sizeof(MBEDTLS_VERSION_STRING_FULL)); |
| 82 | return mbedtls_version_full; |
| 83 | } |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 84 | |
| 85 | psa_status_t tfm_crypto_core_library_init(void) |
| 86 | { |
| 87 | /* Initialise the Mbed Crypto memory allocator to use static memory |
| 88 | * allocation from the provided buffer instead of using the heap |
| 89 | */ |
| 90 | mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, |
| 91 | CRYPTO_ENGINE_BUF_SIZE); |
| 92 | |
| 93 | /* mbedtls_printf is used to print messages including error information. */ |
| 94 | #if (TFM_PARTITION_LOG_LEVEL >= TFM_PARTITION_LOG_LEVEL_ERROR) |
| 95 | mbedtls_platform_set_printf(printf); |
| 96 | #endif |
| 97 | |
| 98 | return PSA_SUCCESS; |
| 99 | } |
Antonio de Angelis | c4d8a56 | 2022-12-01 14:22:38 +0000 | [diff] [blame] | 100 | |
| 101 | psa_status_t tfm_crypto_core_library_key_attributes_from_client( |
| 102 | const struct psa_client_key_attributes_s *client_key_attr, |
| 103 | int32_t client_id, |
| 104 | psa_key_attributes_t *key_attributes) |
| 105 | { |
| 106 | psa_core_key_attributes_t *core; |
| 107 | |
| 108 | if (client_key_attr == NULL || key_attributes == NULL) { |
| 109 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 110 | } |
| 111 | |
| 112 | *key_attributes = psa_key_attributes_init(); |
| 113 | core = &(key_attributes->MBEDTLS_PRIVATE(core)); |
| 114 | |
| 115 | /* Copy core key attributes from the client core key attributes */ |
| 116 | core->MBEDTLS_PRIVATE(type) = client_key_attr->type; |
| 117 | core->MBEDTLS_PRIVATE(lifetime) = client_key_attr->lifetime; |
| 118 | core->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = |
| 119 | client_key_attr->usage; |
| 120 | core->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = |
| 121 | client_key_attr->alg; |
| 122 | core->MBEDTLS_PRIVATE(bits) = client_key_attr->bits; |
| 123 | |
| 124 | /* Use the client key id as the key_id and its partition id as the owner */ |
| 125 | core->MBEDTLS_PRIVATE(id) = mbedtls_svc_key_id_make(client_id, client_key_attr->id); |
| 126 | |
| 127 | return PSA_SUCCESS; |
| 128 | } |
| 129 | |
| 130 | psa_status_t tfm_crypto_core_library_key_attributes_to_client( |
| 131 | const psa_key_attributes_t *key_attributes, |
| 132 | struct psa_client_key_attributes_s *client_key_attr) |
| 133 | { |
| 134 | if (client_key_attr == NULL || key_attributes == NULL) { |
| 135 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 136 | } |
| 137 | |
| 138 | struct psa_client_key_attributes_s v = PSA_CLIENT_KEY_ATTRIBUTES_INIT; |
| 139 | *client_key_attr = v; |
| 140 | psa_core_key_attributes_t core = key_attributes->MBEDTLS_PRIVATE(core); |
| 141 | |
| 142 | /* Copy core key attributes from the client core key attributes */ |
| 143 | client_key_attr->type = core.MBEDTLS_PRIVATE(type); |
| 144 | client_key_attr->lifetime = core.MBEDTLS_PRIVATE(lifetime); |
| 145 | client_key_attr->usage = core.MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage); |
| 146 | client_key_attr->alg = core.MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg); |
| 147 | client_key_attr->bits = core.MBEDTLS_PRIVATE(bits); |
| 148 | |
| 149 | /* Return the key_id as the client key id, do not return the owner */ |
| 150 | client_key_attr->id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(core.MBEDTLS_PRIVATE(id)); |
| 151 | |
| 152 | return PSA_SUCCESS; |
| 153 | } |
Antonio de Angelis | 4118912 | 2022-12-01 14:45:38 +0000 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * \brief This function is required by mbed TLS to enable support for |
| 157 | * platform builtin keys in the PSA Crypto core layer implemented |
| 158 | * by mbed TLS. This function is not standardized by the API hence |
| 159 | * this layer directly provides the symbol required by the library |
| 160 | * |
| 161 | * \note It maps builtin key IDs to cryptographic drivers and slots. The |
| 162 | * actual data is deferred to a platform function, as different |
| 163 | * platforms may have different key storage capabilities. |
| 164 | */ |
| 165 | psa_status_t mbedtls_psa_platform_get_builtin_key( |
| 166 | mbedtls_svc_key_id_t key_id, |
| 167 | psa_key_lifetime_t *lifetime, |
| 168 | psa_drv_slot_number_t *slot_number) |
| 169 | { |
Antonio de Angelis | 01a93bc | 2023-01-20 11:17:14 +0000 | [diff] [blame^] | 170 | const tfm_plat_builtin_key_descriptor_t *desc_table = NULL; |
| 171 | size_t number_of_keys = tfm_plat_builtin_key_get_desc_table_ptr(&desc_table); |
Antonio de Angelis | 4118912 | 2022-12-01 14:45:38 +0000 | [diff] [blame] | 172 | |
Antonio de Angelis | 01a93bc | 2023-01-20 11:17:14 +0000 | [diff] [blame^] | 173 | for (size_t idx = 0; idx < number_of_keys; idx++) { |
| 174 | if (desc_table[idx].key_id == MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key_id)) { |
| 175 | *lifetime = desc_table[idx].lifetime; |
| 176 | *slot_number = desc_table[idx].slot_number; |
| 177 | return PSA_SUCCESS; |
| 178 | } |
Antonio de Angelis | 4118912 | 2022-12-01 14:45:38 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Antonio de Angelis | 01a93bc | 2023-01-20 11:17:14 +0000 | [diff] [blame^] | 181 | return PSA_ERROR_DOES_NOT_EXIST; |
Antonio de Angelis | 4118912 | 2022-12-01 14:45:38 +0000 | [diff] [blame] | 182 | } |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 183 | /*!@}*/ |