blob: 79bf703ab1fda0ef44bd2b6fdc0a6804a7d0bac0 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2018-2021, 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
Soby Mathewd7b79f22020-05-21 15:06:54 +010027#include "psa/crypto_client_struct.h"
28
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000029#define UNIFORM_SIGNATURE_API(api_name) \
30 psa_status_t api_name(psa_invec[], size_t, psa_outvec[], size_t)
31
Antonio de Angelis8908f472018-08-31 15:44:25 +010032/**
33 * \brief List of possible operation types supported by the TFM based
34 * implementation. This type is needed by the operation allocation,
35 * lookup and release functions.
36 *
37 */
38enum tfm_crypto_operation_type {
39 TFM_CRYPTO_OPERATION_NONE = 0,
40 TFM_CRYPTO_CIPHER_OPERATION = 1,
41 TFM_CRYPTO_MAC_OPERATION = 2,
42 TFM_CRYPTO_HASH_OPERATION = 3,
Antonio de Angelis04debbd2019-10-14 12:12:52 +010043 TFM_CRYPTO_KEY_DERIVATION_OPERATION = 4,
Antonio de Angelis8908f472018-08-31 15:44:25 +010044
45 /* Used to force the enum size */
46 TFM_CRYPTO_OPERATION_TYPE_MAX = INT_MAX
47};
48
49/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010050 * \brief Initialise the service
Antonio de Angelis8908f472018-08-31 15:44:25 +010051 *
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000052 * \return Return values as described in \ref psa_status_t
Antonio de Angelis8908f472018-08-31 15:44:25 +010053 */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000054psa_status_t tfm_crypto_init(void);
Antonio de Angelis8908f472018-08-31 15:44:25 +010055
56/**
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010057 * \brief Initialise the Alloc module
58 *
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000059 * \return Return values as described in \ref psa_status_t
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010060 */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000061psa_status_t tfm_crypto_init_alloc(void);
Antonio de Angeliscf85ba22018-10-09 13:29:40 +010062
63/**
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010064 * \brief Returns the ID of the caller
65 *
66 * \param[out] id Pointer to hold the ID of the caller
67 *
68 * \return Return values as described in \ref psa_status_t
69 */
70psa_status_t tfm_crypto_get_caller_id(int32_t *id);
71
72/**
Jamie Fox98ab4412020-01-17 17:12:30 +000073 * \brief Gets key attributes from client key attributes.
74 *
75 * \param[in] client_key_attr Client key attributes
76 * \param[in] client_id Partition ID of the calling client
77 * \param[out] key_attributes Key attributes
78 *
79 * \return Return values as described in \ref psa_status_t
80 */
81psa_status_t tfm_crypto_key_attributes_from_client(
Maulik Patel28659c42021-01-06 14:09:22 +000082 const struct psa_client_key_attributes_s *client_key_attr,
83 int32_t client_id,
84 psa_key_attributes_t *key_attributes);
Jamie Fox98ab4412020-01-17 17:12:30 +000085
86/**
87 * \brief Converts key attributes to client key attributes.
88 *
89 * \param[in] key_attributes Key attributes
90 * \param[out] client_key_attr Client key attributes
91 *
92 * \return Return values as described in \ref psa_status_t
93 */
94psa_status_t tfm_crypto_key_attributes_to_client(
Maulik Patel28659c42021-01-06 14:09:22 +000095 const psa_key_attributes_t *key_attributes,
96 struct psa_client_key_attributes_s *client_key_attr);
Jamie Fox98ab4412020-01-17 17:12:30 +000097
98/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +000099 * \brief Allocate an operation context in the backend
Antonio de Angelis8908f472018-08-31 15:44:25 +0100100 *
Antonio de Angelis4743e672019-04-11 11:38:48 +0100101 * \param[in] type Type of the operation context to allocate
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100102 * \param[out] handle Pointer to hold the allocated handle
Antonio de Angelis4743e672019-04-11 11:38:48 +0100103 * \param[out ctx Double pointer to the corresponding context
Antonio de Angelis8908f472018-08-31 15:44:25 +0100104 *
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000105 * \return Return values as described in \ref psa_status_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100106 */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000107psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100108 uint32_t *handle,
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000109 void **ctx);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100110/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000111 * \brief Release an operation context in the backend
Antonio de Angelis8908f472018-08-31 15:44:25 +0100112 *
Antonio de Angelis4743e672019-04-11 11:38:48 +0100113 * \param[in] handle Pointer to the handle of the context to release
Antonio de Angelis8908f472018-08-31 15:44:25 +0100114 *
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000115 * \return Return values as described in \ref psa_status_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100116 */
Antonio de Angelis4743e672019-04-11 11:38:48 +0100117psa_status_t tfm_crypto_operation_release(uint32_t *handle);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100118/**
Antonio de Angelis819c2f32019-02-06 14:32:02 +0000119 * \brief Look up an operation context in the backend for the corresponding
120 * frontend operation
Antonio de Angelis8908f472018-08-31 15:44:25 +0100121 *
Antonio de Angelis4743e672019-04-11 11:38:48 +0100122 * \param[in] type Type of the operation context to look up
123 * \param[in] handle Handle of the context to lookup
124 * \param[out] ctx Double pointer to the corresponding context
Antonio de Angelis8908f472018-08-31 15:44:25 +0100125 *
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000126 * \return Return values as described in \ref psa_status_t
Antonio de Angelis8908f472018-08-31 15:44:25 +0100127 */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000128psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100129 uint32_t handle,
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000130 void **ctx);
Maulik Patel28659c42021-01-06 14:09:22 +0000131/**
132 * \brief Encodes the input key id and owner to output key
133 *
134 * \param[in] key_id Id of the key to encode
135 * \param[out] enc_key_ptr Pointer to encoded key with id and owner
136 *
137 * \return Return values as described in \ref psa_status_t
138 */
139psa_status_t tfm_crypto_encode_id_and_owner(psa_key_id_t key_id,
140 mbedtls_svc_key_id_t *enc_key_ptr);
Antonio de Angelis8908f472018-08-31 15:44:25 +0100141
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000142#define LIST_TFM_CRYPTO_UNIFORM_SIGNATURE_API \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100143 X(tfm_crypto_get_key_attributes) \
144 X(tfm_crypto_reset_key_attributes) \
Jamie Foxdadb4e82019-09-03 17:59:41 +0100145 X(tfm_crypto_open_key) \
146 X(tfm_crypto_close_key) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100147 X(tfm_crypto_import_key) \
148 X(tfm_crypto_destroy_key) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100149 X(tfm_crypto_export_key) \
150 X(tfm_crypto_export_public_key) \
Maulik Patel28659c42021-01-06 14:09:22 +0000151 X(tfm_crypto_purge_key) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100152 X(tfm_crypto_copy_key) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100153 X(tfm_crypto_hash_compute) \
154 X(tfm_crypto_hash_compare) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100155 X(tfm_crypto_hash_setup) \
156 X(tfm_crypto_hash_update) \
157 X(tfm_crypto_hash_finish) \
158 X(tfm_crypto_hash_verify) \
159 X(tfm_crypto_hash_abort) \
160 X(tfm_crypto_hash_clone) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100161 X(tfm_crypto_mac_compute) \
162 X(tfm_crypto_mac_verify) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100163 X(tfm_crypto_mac_sign_setup) \
164 X(tfm_crypto_mac_verify_setup) \
165 X(tfm_crypto_mac_update) \
166 X(tfm_crypto_mac_sign_finish) \
167 X(tfm_crypto_mac_verify_finish) \
168 X(tfm_crypto_mac_abort) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100169 X(tfm_crypto_cipher_encrypt) \
170 X(tfm_crypto_cipher_decrypt) \
171 X(tfm_crypto_cipher_encrypt_setup) \
172 X(tfm_crypto_cipher_decrypt_setup) \
173 X(tfm_crypto_cipher_generate_iv) \
174 X(tfm_crypto_cipher_set_iv) \
175 X(tfm_crypto_cipher_update) \
176 X(tfm_crypto_cipher_finish) \
177 X(tfm_crypto_cipher_abort) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100178 X(tfm_crypto_aead_encrypt) \
179 X(tfm_crypto_aead_decrypt) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100180 X(tfm_crypto_aead_encrypt_setup) \
181 X(tfm_crypto_aead_decrypt_setup) \
182 X(tfm_crypto_aead_generate_nonce) \
183 X(tfm_crypto_aead_set_nonce) \
184 X(tfm_crypto_aead_set_lengths) \
185 X(tfm_crypto_aead_update_ad) \
186 X(tfm_crypto_aead_update) \
187 X(tfm_crypto_aead_finish) \
188 X(tfm_crypto_aead_verify) \
189 X(tfm_crypto_aead_abort) \
Summer Qinb9492d22021-06-22 18:00:54 +0800190 X(tfm_crypto_sign_message) \
191 X(tfm_crypto_verify_message) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100192 X(tfm_crypto_sign_hash) \
193 X(tfm_crypto_verify_hash) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100194 X(tfm_crypto_asymmetric_encrypt) \
195 X(tfm_crypto_asymmetric_decrypt) \
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100196 X(tfm_crypto_key_derivation_setup) \
197 X(tfm_crypto_key_derivation_get_capacity) \
198 X(tfm_crypto_key_derivation_set_capacity) \
199 X(tfm_crypto_key_derivation_input_bytes) \
200 X(tfm_crypto_key_derivation_input_key) \
201 X(tfm_crypto_key_derivation_key_agreement)\
202 X(tfm_crypto_key_derivation_output_bytes) \
203 X(tfm_crypto_key_derivation_output_key) \
204 X(tfm_crypto_key_derivation_abort) \
205 X(tfm_crypto_raw_key_agreement) \
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100206 X(tfm_crypto_generate_random) \
207 X(tfm_crypto_generate_key) \
Jamie Foxefd82732018-11-26 10:34:32 +0000208
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100209#define X(api_name) UNIFORM_SIGNATURE_API(api_name);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000210LIST_TFM_CRYPTO_UNIFORM_SIGNATURE_API
211#undef X
Jamie Foxefd82732018-11-26 10:34:32 +0000212
Antonio de Angelis8908f472018-08-31 15:44:25 +0100213#ifdef __cplusplus
214}
215#endif
216
217#endif /* __TFM_CRYPTO_API_H__ */