blob: 014f6e5c9b8aafc893a6efbd64871e3bf35ce607 [file] [log] [blame]
Antonio de Angelis7557e682022-11-30 15:37:51 +00001/*
Antonio de Angelis01a93bc2023-01-20 11:17:14 +00002 * Copyright (c) 2022-2023, Arm Limited. All rights reserved.
Antonio de Angelis7557e682022-11-30 15:37:51 +00003 *
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 Angelisedbafb62022-12-01 13:52:15 +000012#include "tfm_sp_log.h"
Antonio de Angelis7557e682022-11-30 15:37:51 +000013
Antonio de Angelisedbafb62022-12-01 13:52:15 +000014#include "config_crypto.h"
15#include "psa/crypto.h"
Antonio de Angelisc4d8a562022-12-01 14:22:38 +000016#include "psa/error.h"
Antonio de Angelis7557e682022-11-30 15:37:51 +000017#include "crypto_library.h"
18
Antonio de Angelis41189122022-12-01 14:45:38 +000019/**
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 Angelisc4d8a562022-12-01 14:22:38 +000027 * \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 Angelis41189122022-12-01 14:45:38 +000032/**
Antonio de Angelisedbafb62022-12-01 13:52:15 +000033 * \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 Angelis41189122022-12-01 14:45:38 +000037
38/**
Antonio de Angelisedbafb62022-12-01 13:52:15 +000039 * \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 Angelis41189122022-12-01 14:45:38 +000044
45/**
Antonio de Angelisedbafb62022-12-01 13:52:15 +000046 * \brief This Mbed TLS include is needed to retrieve version information for
47 * display
48 */
Antonio de Angelis7557e682022-11-30 15:37:51 +000049#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 Angelisedbafb62022-12-01 13:52:15 +000055/**
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 Angelis7557e682022-11-30 15:37:51 +000059static char mbedtls_version_full[18];
60
Antonio de Angelisedbafb62022-12-01 13:52:15 +000061/**
62 * \brief Static buffer to be used by Mbed Crypto for memory allocations
63 *
64 */
65static uint8_t mbedtls_mem_buf[CRYPTO_ENGINE_BUF_SIZE] = {0};
66
Antonio de Angelis7557e682022-11-30 15:37:51 +000067/*!
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/*!@{*/
74tfm_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
79char *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 Angelisedbafb62022-12-01 13:52:15 +000084
85psa_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 Angelisc4d8a562022-12-01 14:22:38 +0000100
101psa_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
130psa_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 Angelis41189122022-12-01 14:45:38 +0000154
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 */
165psa_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 Angelis01a93bc2023-01-20 11:17:14 +0000170 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 Angelis41189122022-12-01 14:45:38 +0000172
Antonio de Angelis01a93bc2023-01-20 11:17:14 +0000173 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 Angelis41189122022-12-01 14:45:38 +0000179 }
180
Antonio de Angelis01a93bc2023-01-20 11:17:14 +0000181 return PSA_ERROR_DOES_NOT_EXIST;
Antonio de Angelis41189122022-12-01 14:45:38 +0000182}
Antonio de Angelis7557e682022-11-30 15:37:51 +0000183/*!@}*/