blob: 98e615a407b06bdd7f61f59c845fc024342d0222 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Antonio de Angelis8908f472018-08-31 15:44:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __TFM_CRYPTO_API_H__
9#define __TFM_CRYPTO_API_H__
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15#include <stdint.h>
16#include "tfm_crypto_defs.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010017#ifdef TFM_PSA_API
Jamie Foxcc31d402019-01-28 17:13:52 +000018#include "psa/service.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010019
20/**
21 * \brief This define is a function pointer type to the Uniform Signature API
22 * prototype.
23 */
24typedef psa_status_t (*tfm_crypto_us_t)(psa_invec[],size_t,psa_outvec[],size_t);
25#endif
Antonio de Angelis8908f472018-08-31 15:44:25 +010026
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000027#define UNIFORM_SIGNATURE_API(api_name) \
28 psa_status_t api_name(psa_invec[], size_t, psa_outvec[], size_t)
29
Antonio de Angelis8908f472018-08-31 15:44:25 +010030/**
31 * \brief List of possible operation types supported by the TFM based
32 * implementation. This type is needed by the operation allocation,
33 * lookup and release functions.
34 *
35 */
36enum tfm_crypto_operation_type {
37 TFM_CRYPTO_OPERATION_NONE = 0,
38 TFM_CRYPTO_CIPHER_OPERATION = 1,
39 TFM_CRYPTO_MAC_OPERATION = 2,
40 TFM_CRYPTO_HASH_OPERATION = 3,
Antonio de Angelis04debbd2019-10-14 12:12:52 +010041 TFM_CRYPTO_KEY_DERIVATION_OPERATION = 4,
Antonio de Angelis8908f472018-08-31 15:44:25 +010042
43 /* Used to force the enum size */
44 TFM_CRYPTO_OPERATION_TYPE_MAX = INT_MAX
45};
46
47/**
Jamie Fox98ab4412020-01-17 17:12:30 +000048 * \brief Core key attributes struct as seen by the client, with
49 * psa_app_key_id_t as the key ID type.
50 */
51typedef struct {
52 psa_key_type_t type;
53 psa_key_lifetime_t lifetime;
54 psa_app_key_id_t id; /* Client key ID */
55 psa_key_policy_t policy;
56 psa_key_bits_t bits;
57 psa_key_attributes_flag_t flags;
58} psa_client_core_key_attributes_t;
59
60/**
61 * \brief This struct represents the psa_key_attributes_t struct exposed to the
62 * client. The Mbed Crypto library needs a different abstraction for
63 * psa_key_attributes_t, so this intermediate struct is defined.
64 *
65 * TODO: Cleanup crypto implementation details from client-side PSA abstraction.
66 */
67typedef struct {
68 psa_client_core_key_attributes_t core; /* Client core key attributes */
69 void *domain_parameters;
70 size_t domain_parameters_size;
71} psa_client_key_attributes_t;
72
73/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010074 * \brief Initialise the service
Antonio de Angelis8908f472018-08-31 15:44:25 +010075 *
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000076 * \return Return values as described in \ref psa_status_t
Antonio de Angelis8908f472018-08-31 15:44:25 +010077 */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000078psa_status_t tfm_crypto_init(void);
Antonio de Angelis8908f472018-08-31 15:44:25 +010079
80/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010081 * \brief Initialise the Alloc module
82 *
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000083 * \return Return values as described in \ref psa_status_t
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010084 */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000085psa_status_t tfm_crypto_init_alloc(void);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010086
87/**
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010088 * \brief Returns the ID of the caller
89 *
90 * \param[out] id Pointer to hold the ID of the caller
91 *
92 * \return Return values as described in \ref psa_status_t
93 */
94psa_status_t tfm_crypto_get_caller_id(int32_t *id);
95
96/**
Jamie Fox98ab4412020-01-17 17:12:30 +000097 * \brief Gets key attributes from client key attributes.
98 *
99 * \param[in] client_key_attr Client key attributes
100 * \param[in] client_id Partition ID of the calling client
101 * \param[out] key_attributes Key attributes
102 *
103 * \return Return values as described in \ref psa_status_t
104 */
105psa_status_t tfm_crypto_key_attributes_from_client(
106 const psa_client_key_attributes_t *client_key_attr,
107 int32_t client_id,
108 psa_key_attributes_t *key_attributes);
109
110/**
111 * \brief Converts key attributes to client key attributes.
112 *
113 * \param[in] key_attributes Key attributes
114 * \param[out] client_key_attr Client key attributes
115 *
116 * \return Return values as described in \ref psa_status_t
117 */
118psa_status_t tfm_crypto_key_attributes_to_client(
119 const psa_key_attributes_t *key_attributes,
120 psa_client_key_attributes_t *client_key_attr);
121
122/**
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100123 * \brief Checks that the requested handle belongs to the requesting
124 * partition
125 *
126 * \param[in] handle Handle given as input
127 * \param[out] index Optionally, pointer to hold the internal index
128 * corresponding to the input handle. Valid only
129 * on PSA_SUCCESS, it's returned only if the input
130 * parameter is not NULL.
131 *
132 * \return Return values as described in \ref psa_status_t
133 */
134psa_status_t tfm_crypto_check_handle_owner(psa_key_handle_t handle,
135 uint32_t *index);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100136
137/**
Jamie Fox99360e82020-02-20 16:00:09 +0000138 * \brief Checks that there is enough local storage in RAM to keep another key,
139 * and returns the index of the storage to use.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100140 *
Jamie Fox99360e82020-02-20 16:00:09 +0000141 * \param[out] index Index of the local storage to use
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100142 *
143 * \return Return values as described in \ref psa_status_t
144 */
Jamie Fox99360e82020-02-20 16:00:09 +0000145psa_status_t tfm_crypto_check_key_storage(uint32_t *index);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100146
147/**
Jamie Fox99360e82020-02-20 16:00:09 +0000148 * \brief Sets the index of the local storage in use with a key requested by the
149 * calling partition, and stores the corresponding key_handle.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100150 *
Jamie Fox99360e82020-02-20 16:00:09 +0000151 * \param[in] index Index of the local storage to use
152 * \param[in] key_handle Corresponding key handle to associate
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100153 *
154 * \return Return values as described in \ref psa_status_t
155 */
Jamie Fox99360e82020-02-20 16:00:09 +0000156psa_status_t tfm_crypto_set_key_storage(uint32_t index,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100157 psa_key_handle_t key_handle);
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100158/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000159 * \brief Allocate an operation context in the backend
Antonio de Angelis8908f472018-08-31 15:44:25 +0100160 *
Antonio de Angelis4743e672019-04-11 11:38:48 +0100161 * \param[in] type Type of the operation context to allocate
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100162 * \param[out] handle Pointer to hold the allocated handle
Antonio de Angelis4743e672019-04-11 11:38:48 +0100163 * \param[out ctx Double pointer to the corresponding context
Antonio de Angelis8908f472018-08-31 15:44:25 +0100164 *
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000165 * \return Return values as described in \ref psa_status_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100166 */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000167psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100168 uint32_t *handle,
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000169 void **ctx);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100170/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000171 * \brief Release an operation context in the backend
Antonio de Angelis8908f472018-08-31 15:44:25 +0100172 *
Antonio de Angelis4743e672019-04-11 11:38:48 +0100173 * \param[in] handle Pointer to the handle of the context to release
Antonio de Angelis8908f472018-08-31 15:44:25 +0100174 *
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000175 * \return Return values as described in \ref psa_status_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100176 */
Antonio de Angelis4743e672019-04-11 11:38:48 +0100177psa_status_t tfm_crypto_operation_release(uint32_t *handle);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100178/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000179 * \brief Look up an operation context in the backend for the corresponding
180 * frontend operation
Antonio de Angelis8908f472018-08-31 15:44:25 +0100181 *
Antonio de Angelis4743e672019-04-11 11:38:48 +0100182 * \param[in] type Type of the operation context to look up
183 * \param[in] handle Handle of the context to lookup
184 * \param[out] ctx Double pointer to the corresponding context
Antonio de Angelis8908f472018-08-31 15:44:25 +0100185 *
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000186 * \return Return values as described in \ref psa_status_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100187 */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000188psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100189 uint32_t handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000190 void **ctx);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100191
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000192#define LIST_TFM_CRYPTO_UNIFORM_SIGNATURE_API \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100193 X(tfm_crypto_get_key_attributes) \
194 X(tfm_crypto_reset_key_attributes) \
Jamie Foxdadb4e82019-09-03 17:59:41 +0100195 X(tfm_crypto_open_key) \
196 X(tfm_crypto_close_key) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100197 X(tfm_crypto_import_key) \
198 X(tfm_crypto_destroy_key) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100199 X(tfm_crypto_export_key) \
200 X(tfm_crypto_export_public_key) \
201 X(tfm_crypto_copy_key) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100202 X(tfm_crypto_hash_compute) \
203 X(tfm_crypto_hash_compare) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100204 X(tfm_crypto_hash_setup) \
205 X(tfm_crypto_hash_update) \
206 X(tfm_crypto_hash_finish) \
207 X(tfm_crypto_hash_verify) \
208 X(tfm_crypto_hash_abort) \
209 X(tfm_crypto_hash_clone) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100210 X(tfm_crypto_mac_compute) \
211 X(tfm_crypto_mac_verify) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100212 X(tfm_crypto_mac_sign_setup) \
213 X(tfm_crypto_mac_verify_setup) \
214 X(tfm_crypto_mac_update) \
215 X(tfm_crypto_mac_sign_finish) \
216 X(tfm_crypto_mac_verify_finish) \
217 X(tfm_crypto_mac_abort) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100218 X(tfm_crypto_cipher_encrypt) \
219 X(tfm_crypto_cipher_decrypt) \
220 X(tfm_crypto_cipher_encrypt_setup) \
221 X(tfm_crypto_cipher_decrypt_setup) \
222 X(tfm_crypto_cipher_generate_iv) \
223 X(tfm_crypto_cipher_set_iv) \
224 X(tfm_crypto_cipher_update) \
225 X(tfm_crypto_cipher_finish) \
226 X(tfm_crypto_cipher_abort) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100227 X(tfm_crypto_aead_encrypt) \
228 X(tfm_crypto_aead_decrypt) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100229 X(tfm_crypto_aead_encrypt_setup) \
230 X(tfm_crypto_aead_decrypt_setup) \
231 X(tfm_crypto_aead_generate_nonce) \
232 X(tfm_crypto_aead_set_nonce) \
233 X(tfm_crypto_aead_set_lengths) \
234 X(tfm_crypto_aead_update_ad) \
235 X(tfm_crypto_aead_update) \
236 X(tfm_crypto_aead_finish) \
237 X(tfm_crypto_aead_verify) \
238 X(tfm_crypto_aead_abort) \
239 X(tfm_crypto_sign_hash) \
240 X(tfm_crypto_verify_hash) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100241 X(tfm_crypto_asymmetric_encrypt) \
242 X(tfm_crypto_asymmetric_decrypt) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100243 X(tfm_crypto_key_derivation_setup) \
244 X(tfm_crypto_key_derivation_get_capacity) \
245 X(tfm_crypto_key_derivation_set_capacity) \
246 X(tfm_crypto_key_derivation_input_bytes) \
247 X(tfm_crypto_key_derivation_input_key) \
248 X(tfm_crypto_key_derivation_key_agreement)\
249 X(tfm_crypto_key_derivation_output_bytes) \
250 X(tfm_crypto_key_derivation_output_key) \
251 X(tfm_crypto_key_derivation_abort) \
252 X(tfm_crypto_raw_key_agreement) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100253 X(tfm_crypto_generate_random) \
254 X(tfm_crypto_generate_key) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100255 X(tfm_crypto_set_key_domain_parameters) \
256 X(tfm_crypto_get_key_domain_parameters) \
Jamie Foxefd82732018-11-26 10:34:32 +0000257
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100258#define X(api_name) UNIFORM_SIGNATURE_API(api_name);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000259LIST_TFM_CRYPTO_UNIFORM_SIGNATURE_API
260#undef X
Jamie Foxefd82732018-11-26 10:34:32 +0000261
Antonio de Angelis8908f472018-08-31 15:44:25 +0100262#ifdef __cplusplus
263}
264#endif
265
266#endif /* __TFM_CRYPTO_API_H__ */