blob: bd876ea32a7bcdb5940156295dffa19f7fcb210e [file] [log] [blame]
Antonio de Angelis7557e682022-11-30 15:37:51 +00001/*
2 * Copyright (c) 2022, Arm Limited. All rights reserved.
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 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 Angelisedbafb62022-12-01 13:52:15 +000019/*
Antonio de Angelisc4d8a562022-12-01 14:22:38 +000020 * \brief These includes are required to get the interface that TF-M crypto
21 * exposes on its client side, in particular regarding key attributes
22 */
23#include "psa/crypto_client_struct.h"
24
25/*
Antonio de Angelisedbafb62022-12-01 13:52:15 +000026 * \brief This Mbed TLS include is needed to initialise the memory allocator
27 * of the library used for internal allocations
28 */
29#include "mbedtls/memory_buffer_alloc.h"
30/*
31 * \brief This Mbed TLS include is needed to set the mbedtls_printf to the
32 * function required by the TF-M framework in order to be able to
33 * print to terminal through mbedtls_printf
34 */
35#include "mbedtls/platform.h"
36/*
37 * \brief This Mbed TLS include is needed to retrieve version information for
38 * display
39 */
Antonio de Angelis7557e682022-11-30 15:37:51 +000040#include "mbedtls/build_info.h"
41
42#ifndef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
43#error "MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER must be selected in Mbed TLS config file"
44#endif
45
Antonio de Angelisedbafb62022-12-01 13:52:15 +000046/**
47 * \brief Static buffer containing the string describing the mbed TLS version. mbed TLS
48 * guarantees that the string will never be greater than 18 bytes
49 */
Antonio de Angelis7557e682022-11-30 15:37:51 +000050static char mbedtls_version_full[18];
51
Antonio de Angelisedbafb62022-12-01 13:52:15 +000052/**
53 * \brief Static buffer to be used by Mbed Crypto for memory allocations
54 *
55 */
56static uint8_t mbedtls_mem_buf[CRYPTO_ENGINE_BUF_SIZE] = {0};
57
Antonio de Angelis7557e682022-11-30 15:37:51 +000058/*!
59 * \defgroup tfm_crypto_library Set of functions implementing the abstractions of the underlying cryptographic
60 * library that implements the PSA Crypto APIs to provide the PSA Crypto core
61 * functionality to the TF-M Crypto service. Currently it supports only an
62 * mbed TLS based abstraction.
63 */
64/*!@{*/
65tfm_crypto_library_key_id_t tfm_crypto_library_key_id_init(int32_t owner, psa_key_id_t key_id)
66{
67 return mbedtls_svc_key_id_make(owner, key_id);
68}
69
70char *tfm_crypto_library_get_info(void)
71{
72 memcpy(mbedtls_version_full, MBEDTLS_VERSION_STRING_FULL, sizeof(MBEDTLS_VERSION_STRING_FULL));
73 return mbedtls_version_full;
74}
Antonio de Angelisedbafb62022-12-01 13:52:15 +000075
76psa_status_t tfm_crypto_core_library_init(void)
77{
78 /* Initialise the Mbed Crypto memory allocator to use static memory
79 * allocation from the provided buffer instead of using the heap
80 */
81 mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf,
82 CRYPTO_ENGINE_BUF_SIZE);
83
84 /* mbedtls_printf is used to print messages including error information. */
85#if (TFM_PARTITION_LOG_LEVEL >= TFM_PARTITION_LOG_LEVEL_ERROR)
86 mbedtls_platform_set_printf(printf);
87#endif
88
89 return PSA_SUCCESS;
90}
Antonio de Angelisc4d8a562022-12-01 14:22:38 +000091
92psa_status_t tfm_crypto_core_library_key_attributes_from_client(
93 const struct psa_client_key_attributes_s *client_key_attr,
94 int32_t client_id,
95 psa_key_attributes_t *key_attributes)
96{
97 psa_core_key_attributes_t *core;
98
99 if (client_key_attr == NULL || key_attributes == NULL) {
100 return PSA_ERROR_PROGRAMMER_ERROR;
101 }
102
103 *key_attributes = psa_key_attributes_init();
104 core = &(key_attributes->MBEDTLS_PRIVATE(core));
105
106 /* Copy core key attributes from the client core key attributes */
107 core->MBEDTLS_PRIVATE(type) = client_key_attr->type;
108 core->MBEDTLS_PRIVATE(lifetime) = client_key_attr->lifetime;
109 core->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) =
110 client_key_attr->usage;
111 core->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) =
112 client_key_attr->alg;
113 core->MBEDTLS_PRIVATE(bits) = client_key_attr->bits;
114
115 /* Use the client key id as the key_id and its partition id as the owner */
116 core->MBEDTLS_PRIVATE(id) = mbedtls_svc_key_id_make(client_id, client_key_attr->id);
117
118 return PSA_SUCCESS;
119}
120
121psa_status_t tfm_crypto_core_library_key_attributes_to_client(
122 const psa_key_attributes_t *key_attributes,
123 struct psa_client_key_attributes_s *client_key_attr)
124{
125 if (client_key_attr == NULL || key_attributes == NULL) {
126 return PSA_ERROR_PROGRAMMER_ERROR;
127 }
128
129 struct psa_client_key_attributes_s v = PSA_CLIENT_KEY_ATTRIBUTES_INIT;
130 *client_key_attr = v;
131 psa_core_key_attributes_t core = key_attributes->MBEDTLS_PRIVATE(core);
132
133 /* Copy core key attributes from the client core key attributes */
134 client_key_attr->type = core.MBEDTLS_PRIVATE(type);
135 client_key_attr->lifetime = core.MBEDTLS_PRIVATE(lifetime);
136 client_key_attr->usage = core.MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
137 client_key_attr->alg = core.MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
138 client_key_attr->bits = core.MBEDTLS_PRIVATE(bits);
139
140 /* Return the key_id as the client key id, do not return the owner */
141 client_key_attr->id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(core.MBEDTLS_PRIVATE(id));
142
143 return PSA_SUCCESS;
144}
Antonio de Angelis7557e682022-11-30 15:37:51 +0000145/*!@}*/