1a8e1175bSopenharmony_ci/**
2a8e1175bSopenharmony_ci * \file psa/crypto_se_driver.h
3a8e1175bSopenharmony_ci * \brief PSA external cryptoprocessor driver module
4a8e1175bSopenharmony_ci *
5a8e1175bSopenharmony_ci * This header declares types and function signatures for cryptography
6a8e1175bSopenharmony_ci * drivers that access key material via opaque references.
7a8e1175bSopenharmony_ci * This is meant for cryptoprocessors that have a separate key storage from the
8a8e1175bSopenharmony_ci * space in which the PSA Crypto implementation runs, typically secure
9a8e1175bSopenharmony_ci * elements (SEs).
10a8e1175bSopenharmony_ci *
11a8e1175bSopenharmony_ci * This file is part of the PSA Crypto Driver HAL (hardware abstraction layer),
12a8e1175bSopenharmony_ci * containing functions for driver developers to implement to enable hardware
13a8e1175bSopenharmony_ci * to be called in a standardized way by a PSA Cryptography API
14a8e1175bSopenharmony_ci * implementation. The functions comprising the driver HAL, which driver
15a8e1175bSopenharmony_ci * authors implement, are not intended to be called by application developers.
16a8e1175bSopenharmony_ci */
17a8e1175bSopenharmony_ci
18a8e1175bSopenharmony_ci/*
19a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
20a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0
21a8e1175bSopenharmony_ci *
22a8e1175bSopenharmony_ci *  Licensed under the Apache License, Version 2.0 (the "License"); you may
23a8e1175bSopenharmony_ci *  not use this file except in compliance with the License.
24a8e1175bSopenharmony_ci *  You may obtain a copy of the License at
25a8e1175bSopenharmony_ci *
26a8e1175bSopenharmony_ci *  http://www.apache.org/licenses/LICENSE-2.0
27a8e1175bSopenharmony_ci *
28a8e1175bSopenharmony_ci *  Unless required by applicable law or agreed to in writing, software
29a8e1175bSopenharmony_ci *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30a8e1175bSopenharmony_ci *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31a8e1175bSopenharmony_ci *  See the License for the specific language governing permissions and
32a8e1175bSopenharmony_ci *  limitations under the License.
33a8e1175bSopenharmony_ci */
34a8e1175bSopenharmony_ci#ifndef PSA_CRYPTO_SE_DRIVER_H
35a8e1175bSopenharmony_ci#define PSA_CRYPTO_SE_DRIVER_H
36a8e1175bSopenharmony_ci#include "mbedtls/private_access.h"
37a8e1175bSopenharmony_ci
38a8e1175bSopenharmony_ci#include "crypto_driver_common.h"
39a8e1175bSopenharmony_ci
40a8e1175bSopenharmony_ci#ifdef __cplusplus
41a8e1175bSopenharmony_ciextern "C" {
42a8e1175bSopenharmony_ci#endif
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_ci/** \defgroup se_init Secure element driver initialization
45a8e1175bSopenharmony_ci */
46a8e1175bSopenharmony_ci/**@{*/
47a8e1175bSopenharmony_ci
48a8e1175bSopenharmony_ci/** \brief Driver context structure
49a8e1175bSopenharmony_ci *
50a8e1175bSopenharmony_ci * Driver functions receive a pointer to this structure.
51a8e1175bSopenharmony_ci * Each registered driver has one instance of this structure.
52a8e1175bSopenharmony_ci *
53a8e1175bSopenharmony_ci * Implementations must include the fields specified here and
54a8e1175bSopenharmony_ci * may include other fields.
55a8e1175bSopenharmony_ci */
56a8e1175bSopenharmony_citypedef struct {
57a8e1175bSopenharmony_ci    /** A read-only pointer to the driver's persistent data.
58a8e1175bSopenharmony_ci     *
59a8e1175bSopenharmony_ci     * Drivers typically use this persistent data to keep track of
60a8e1175bSopenharmony_ci     * which slot numbers are available. This is only a guideline:
61a8e1175bSopenharmony_ci     * drivers may use the persistent data for any purpose, keeping
62a8e1175bSopenharmony_ci     * in mind the restrictions on when the persistent data is saved
63a8e1175bSopenharmony_ci     * to storage: the persistent data is only saved after calling
64a8e1175bSopenharmony_ci     * certain functions that receive a writable pointer to the
65a8e1175bSopenharmony_ci     * persistent data.
66a8e1175bSopenharmony_ci     *
67a8e1175bSopenharmony_ci     * The core allocates a memory buffer for the persistent data.
68a8e1175bSopenharmony_ci     * The pointer is guaranteed to be suitably aligned for any data type,
69a8e1175bSopenharmony_ci     * like a pointer returned by `malloc` (but the core can use any
70a8e1175bSopenharmony_ci     * method to allocate the buffer, not necessarily `malloc`).
71a8e1175bSopenharmony_ci     *
72a8e1175bSopenharmony_ci     * The size of this buffer is in the \c persistent_data_size field of
73a8e1175bSopenharmony_ci     * this structure.
74a8e1175bSopenharmony_ci     *
75a8e1175bSopenharmony_ci     * Before the driver is initialized for the first time, the content of
76a8e1175bSopenharmony_ci     * the persistent data is all-bits-zero. After a driver upgrade, if the
77a8e1175bSopenharmony_ci     * size of the persistent data has increased, the original data is padded
78a8e1175bSopenharmony_ci     * on the right with zeros; if the size has decreased, the original data
79a8e1175bSopenharmony_ci     * is truncated to the new size.
80a8e1175bSopenharmony_ci     *
81a8e1175bSopenharmony_ci     * This pointer is to read-only data. Only a few driver functions are
82a8e1175bSopenharmony_ci     * allowed to modify the persistent data. These functions receive a
83a8e1175bSopenharmony_ci     * writable pointer. These functions are:
84a8e1175bSopenharmony_ci     * - psa_drv_se_t::p_init
85a8e1175bSopenharmony_ci     * - psa_drv_se_key_management_t::p_allocate
86a8e1175bSopenharmony_ci     * - psa_drv_se_key_management_t::p_destroy
87a8e1175bSopenharmony_ci     *
88a8e1175bSopenharmony_ci     * The PSA Cryptography core saves the persistent data from one
89a8e1175bSopenharmony_ci     * session to the next. It does this before returning from API functions
90a8e1175bSopenharmony_ci     * that call a driver method that is allowed to modify the persistent
91a8e1175bSopenharmony_ci     * data, specifically:
92a8e1175bSopenharmony_ci     * - psa_crypto_init() causes a call to psa_drv_se_t::p_init, and may call
93a8e1175bSopenharmony_ci     *   psa_drv_se_key_management_t::p_destroy to complete an action
94a8e1175bSopenharmony_ci     *   that was interrupted by a power failure.
95a8e1175bSopenharmony_ci     * - Key creation functions cause a call to
96a8e1175bSopenharmony_ci     *   psa_drv_se_key_management_t::p_allocate, and may cause a call to
97a8e1175bSopenharmony_ci     *   psa_drv_se_key_management_t::p_destroy in case an error occurs.
98a8e1175bSopenharmony_ci     * - psa_destroy_key() causes a call to
99a8e1175bSopenharmony_ci     *   psa_drv_se_key_management_t::p_destroy.
100a8e1175bSopenharmony_ci     */
101a8e1175bSopenharmony_ci    const void *const MBEDTLS_PRIVATE(persistent_data);
102a8e1175bSopenharmony_ci
103a8e1175bSopenharmony_ci    /** The size of \c persistent_data in bytes.
104a8e1175bSopenharmony_ci     *
105a8e1175bSopenharmony_ci     * This is always equal to the value of the `persistent_data_size` field
106a8e1175bSopenharmony_ci     * of the ::psa_drv_se_t structure when the driver is registered.
107a8e1175bSopenharmony_ci     */
108a8e1175bSopenharmony_ci    const size_t MBEDTLS_PRIVATE(persistent_data_size);
109a8e1175bSopenharmony_ci
110a8e1175bSopenharmony_ci    /** Driver transient data.
111a8e1175bSopenharmony_ci     *
112a8e1175bSopenharmony_ci     * The core initializes this value to 0 and does not read or modify it
113a8e1175bSopenharmony_ci     * afterwards. The driver may store whatever it wants in this field.
114a8e1175bSopenharmony_ci     */
115a8e1175bSopenharmony_ci    uintptr_t MBEDTLS_PRIVATE(transient_data);
116a8e1175bSopenharmony_ci} psa_drv_se_context_t;
117a8e1175bSopenharmony_ci
118a8e1175bSopenharmony_ci/** \brief A driver initialization function.
119a8e1175bSopenharmony_ci *
120a8e1175bSopenharmony_ci * \param[in,out] drv_context       The driver context structure.
121a8e1175bSopenharmony_ci * \param[in,out] persistent_data   A pointer to the persistent data
122a8e1175bSopenharmony_ci *                                  that allows writing.
123a8e1175bSopenharmony_ci * \param location                  The location value for which this driver
124a8e1175bSopenharmony_ci *                                  is registered. The driver will be invoked
125a8e1175bSopenharmony_ci *                                  for all keys whose lifetime is in this
126a8e1175bSopenharmony_ci *                                  location.
127a8e1175bSopenharmony_ci *
128a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
129a8e1175bSopenharmony_ci *         The driver is operational.
130a8e1175bSopenharmony_ci *         The core will update the persistent data in storage.
131a8e1175bSopenharmony_ci * \return
132a8e1175bSopenharmony_ci *         Any other return value prevents the driver from being used in
133a8e1175bSopenharmony_ci *         this session.
134a8e1175bSopenharmony_ci *         The core will NOT update the persistent data in storage.
135a8e1175bSopenharmony_ci */
136a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context,
137a8e1175bSopenharmony_ci                                          void *persistent_data,
138a8e1175bSopenharmony_ci                                          psa_key_location_t location);
139a8e1175bSopenharmony_ci
140a8e1175bSopenharmony_ci#if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C)
141a8e1175bSopenharmony_ci/* Mbed Crypto with secure element support enabled defines this type in
142a8e1175bSopenharmony_ci * crypto_types.h because it is also visible to applications through an
143a8e1175bSopenharmony_ci * implementation-specific extension.
144a8e1175bSopenharmony_ci * For the PSA Cryptography specification, this type is only visible
145a8e1175bSopenharmony_ci * via crypto_se_driver.h. */
146a8e1175bSopenharmony_ci/** An internal designation of a key slot between the core part of the
147a8e1175bSopenharmony_ci * PSA Crypto implementation and the driver. The meaning of this value
148a8e1175bSopenharmony_ci * is driver-dependent. */
149a8e1175bSopenharmony_citypedef uint64_t psa_key_slot_number_t;
150a8e1175bSopenharmony_ci#endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */
151a8e1175bSopenharmony_ci
152a8e1175bSopenharmony_ci/**@}*/
153a8e1175bSopenharmony_ci
154a8e1175bSopenharmony_ci/** \defgroup se_mac Secure Element Message Authentication Codes
155a8e1175bSopenharmony_ci * Generation and authentication of Message Authentication Codes (MACs) using
156a8e1175bSopenharmony_ci * a secure element can be done either as a single function call (via the
157a8e1175bSopenharmony_ci * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in
158a8e1175bSopenharmony_ci * parts using the following sequence:
159a8e1175bSopenharmony_ci * - `psa_drv_se_mac_setup_t`
160a8e1175bSopenharmony_ci * - `psa_drv_se_mac_update_t`
161a8e1175bSopenharmony_ci * - `psa_drv_se_mac_update_t`
162a8e1175bSopenharmony_ci * - ...
163a8e1175bSopenharmony_ci * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t`
164a8e1175bSopenharmony_ci *
165a8e1175bSopenharmony_ci * If a previously started secure element MAC operation needs to be terminated,
166a8e1175bSopenharmony_ci * it should be done so by the `psa_drv_se_mac_abort_t`. Failure to do so may
167a8e1175bSopenharmony_ci * result in allocated resources not being freed or in other undefined
168a8e1175bSopenharmony_ci * behavior.
169a8e1175bSopenharmony_ci */
170a8e1175bSopenharmony_ci/**@{*/
171a8e1175bSopenharmony_ci/** \brief A function that starts a secure element  MAC operation for a PSA
172a8e1175bSopenharmony_ci * Crypto Driver implementation
173a8e1175bSopenharmony_ci *
174a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
175a8e1175bSopenharmony_ci * \param[in,out] op_context    A structure that will contain the
176a8e1175bSopenharmony_ci *                              hardware-specific MAC context
177a8e1175bSopenharmony_ci * \param[in] key_slot          The slot of the key to be used for the
178a8e1175bSopenharmony_ci *                              operation
179a8e1175bSopenharmony_ci * \param[in] algorithm         The algorithm to be used to underly the MAC
180a8e1175bSopenharmony_ci *                              operation
181a8e1175bSopenharmony_ci *
182a8e1175bSopenharmony_ci * \retval  #PSA_SUCCESS
183a8e1175bSopenharmony_ci *          Success.
184a8e1175bSopenharmony_ci */
185a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context,
186a8e1175bSopenharmony_ci                                               void *op_context,
187a8e1175bSopenharmony_ci                                               psa_key_slot_number_t key_slot,
188a8e1175bSopenharmony_ci                                               psa_algorithm_t algorithm);
189a8e1175bSopenharmony_ci
190a8e1175bSopenharmony_ci/** \brief A function that continues a previously started secure element MAC
191a8e1175bSopenharmony_ci * operation
192a8e1175bSopenharmony_ci *
193a8e1175bSopenharmony_ci * \param[in,out] op_context    A hardware-specific structure for the
194a8e1175bSopenharmony_ci *                              previously-established MAC operation to be
195a8e1175bSopenharmony_ci *                              updated
196a8e1175bSopenharmony_ci * \param[in] p_input           A buffer containing the message to be appended
197a8e1175bSopenharmony_ci *                              to the MAC operation
198a8e1175bSopenharmony_ci * \param[in] input_length      The size in bytes of the input message buffer
199a8e1175bSopenharmony_ci */
200a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context,
201a8e1175bSopenharmony_ci                                                const uint8_t *p_input,
202a8e1175bSopenharmony_ci                                                size_t input_length);
203a8e1175bSopenharmony_ci
204a8e1175bSopenharmony_ci/** \brief a function that completes a previously started secure element MAC
205a8e1175bSopenharmony_ci * operation by returning the resulting MAC.
206a8e1175bSopenharmony_ci *
207a8e1175bSopenharmony_ci * \param[in,out] op_context    A hardware-specific structure for the
208a8e1175bSopenharmony_ci *                              previously started MAC operation to be
209a8e1175bSopenharmony_ci *                              finished
210a8e1175bSopenharmony_ci * \param[out] p_mac            A buffer where the generated MAC will be
211a8e1175bSopenharmony_ci *                              placed
212a8e1175bSopenharmony_ci * \param[in] mac_size          The size in bytes of the buffer that has been
213a8e1175bSopenharmony_ci *                              allocated for the `output` buffer
214a8e1175bSopenharmony_ci * \param[out] p_mac_length     After completion, will contain the number of
215a8e1175bSopenharmony_ci *                              bytes placed in the `p_mac` buffer
216a8e1175bSopenharmony_ci *
217a8e1175bSopenharmony_ci * \retval  #PSA_SUCCESS
218a8e1175bSopenharmony_ci *          Success.
219a8e1175bSopenharmony_ci */
220a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context,
221a8e1175bSopenharmony_ci                                                uint8_t *p_mac,
222a8e1175bSopenharmony_ci                                                size_t mac_size,
223a8e1175bSopenharmony_ci                                                size_t *p_mac_length);
224a8e1175bSopenharmony_ci
225a8e1175bSopenharmony_ci/** \brief A function that completes a previously started secure element MAC
226a8e1175bSopenharmony_ci * operation by comparing the resulting MAC against a provided value
227a8e1175bSopenharmony_ci *
228a8e1175bSopenharmony_ci * \param[in,out] op_context    A hardware-specific structure for the previously
229a8e1175bSopenharmony_ci *                              started MAC operation to be finished
230a8e1175bSopenharmony_ci * \param[in] p_mac             The MAC value against which the resulting MAC
231a8e1175bSopenharmony_ci *                              will be compared against
232a8e1175bSopenharmony_ci * \param[in] mac_length        The size in bytes of the value stored in `p_mac`
233a8e1175bSopenharmony_ci *
234a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
235a8e1175bSopenharmony_ci *         The operation completed successfully and the MACs matched each
236a8e1175bSopenharmony_ci *         other
237a8e1175bSopenharmony_ci * \retval #PSA_ERROR_INVALID_SIGNATURE
238a8e1175bSopenharmony_ci *         The operation completed successfully, but the calculated MAC did
239a8e1175bSopenharmony_ci *         not match the provided MAC
240a8e1175bSopenharmony_ci */
241a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context,
242a8e1175bSopenharmony_ci                                                       const uint8_t *p_mac,
243a8e1175bSopenharmony_ci                                                       size_t mac_length);
244a8e1175bSopenharmony_ci
245a8e1175bSopenharmony_ci/** \brief A function that aborts a previous started secure element MAC
246a8e1175bSopenharmony_ci * operation
247a8e1175bSopenharmony_ci *
248a8e1175bSopenharmony_ci * \param[in,out] op_context    A hardware-specific structure for the previously
249a8e1175bSopenharmony_ci *                              started MAC operation to be aborted
250a8e1175bSopenharmony_ci */
251a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context);
252a8e1175bSopenharmony_ci
253a8e1175bSopenharmony_ci/** \brief A function that performs a secure element MAC operation in one
254a8e1175bSopenharmony_ci * command and returns the calculated MAC
255a8e1175bSopenharmony_ci *
256a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
257a8e1175bSopenharmony_ci * \param[in] p_input           A buffer containing the message to be MACed
258a8e1175bSopenharmony_ci * \param[in] input_length      The size in bytes of `p_input`
259a8e1175bSopenharmony_ci * \param[in] key_slot          The slot of the key to be used
260a8e1175bSopenharmony_ci * \param[in] alg               The algorithm to be used to underlie the MAC
261a8e1175bSopenharmony_ci *                              operation
262a8e1175bSopenharmony_ci * \param[out] p_mac            A buffer where the generated MAC will be
263a8e1175bSopenharmony_ci *                              placed
264a8e1175bSopenharmony_ci * \param[in] mac_size          The size in bytes of the `p_mac` buffer
265a8e1175bSopenharmony_ci * \param[out] p_mac_length     After completion, will contain the number of
266a8e1175bSopenharmony_ci *                              bytes placed in the `output` buffer
267a8e1175bSopenharmony_ci *
268a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
269a8e1175bSopenharmony_ci *         Success.
270a8e1175bSopenharmony_ci */
271a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context,
272a8e1175bSopenharmony_ci                                                  const uint8_t *p_input,
273a8e1175bSopenharmony_ci                                                  size_t input_length,
274a8e1175bSopenharmony_ci                                                  psa_key_slot_number_t key_slot,
275a8e1175bSopenharmony_ci                                                  psa_algorithm_t alg,
276a8e1175bSopenharmony_ci                                                  uint8_t *p_mac,
277a8e1175bSopenharmony_ci                                                  size_t mac_size,
278a8e1175bSopenharmony_ci                                                  size_t *p_mac_length);
279a8e1175bSopenharmony_ci
280a8e1175bSopenharmony_ci/** \brief A function that performs a secure element MAC operation in one
281a8e1175bSopenharmony_ci * command and compares the resulting MAC against a provided value
282a8e1175bSopenharmony_ci *
283a8e1175bSopenharmony_ci * \param[in,out] drv_context       The driver context structure.
284a8e1175bSopenharmony_ci * \param[in] p_input       A buffer containing the message to be MACed
285a8e1175bSopenharmony_ci * \param[in] input_length  The size in bytes of `input`
286a8e1175bSopenharmony_ci * \param[in] key_slot      The slot of the key to be used
287a8e1175bSopenharmony_ci * \param[in] alg           The algorithm to be used to underlie the MAC
288a8e1175bSopenharmony_ci *                          operation
289a8e1175bSopenharmony_ci * \param[in] p_mac         The MAC value against which the resulting MAC will
290a8e1175bSopenharmony_ci *                          be compared against
291a8e1175bSopenharmony_ci * \param[in] mac_length   The size in bytes of `mac`
292a8e1175bSopenharmony_ci *
293a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
294a8e1175bSopenharmony_ci *         The operation completed successfully and the MACs matched each
295a8e1175bSopenharmony_ci *         other
296a8e1175bSopenharmony_ci * \retval #PSA_ERROR_INVALID_SIGNATURE
297a8e1175bSopenharmony_ci *         The operation completed successfully, but the calculated MAC did
298a8e1175bSopenharmony_ci *         not match the provided MAC
299a8e1175bSopenharmony_ci */
300a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context,
301a8e1175bSopenharmony_ci                                                const uint8_t *p_input,
302a8e1175bSopenharmony_ci                                                size_t input_length,
303a8e1175bSopenharmony_ci                                                psa_key_slot_number_t key_slot,
304a8e1175bSopenharmony_ci                                                psa_algorithm_t alg,
305a8e1175bSopenharmony_ci                                                const uint8_t *p_mac,
306a8e1175bSopenharmony_ci                                                size_t mac_length);
307a8e1175bSopenharmony_ci
308a8e1175bSopenharmony_ci/** \brief A struct containing all of the function pointers needed to
309a8e1175bSopenharmony_ci * perform secure element MAC operations
310a8e1175bSopenharmony_ci *
311a8e1175bSopenharmony_ci * PSA Crypto API implementations should populate the table as appropriate
312a8e1175bSopenharmony_ci * upon startup.
313a8e1175bSopenharmony_ci *
314a8e1175bSopenharmony_ci * If one of the functions is not implemented (such as
315a8e1175bSopenharmony_ci * `psa_drv_se_mac_generate_t`), it should be set to NULL.
316a8e1175bSopenharmony_ci *
317a8e1175bSopenharmony_ci * Driver implementers should ensure that they implement all of the functions
318a8e1175bSopenharmony_ci * that make sense for their hardware, and that they provide a full solution
319a8e1175bSopenharmony_ci * (for example, if they support `p_setup`, they should also support
320a8e1175bSopenharmony_ci * `p_update` and at least one of `p_finish` or `p_finish_verify`).
321a8e1175bSopenharmony_ci *
322a8e1175bSopenharmony_ci */
323a8e1175bSopenharmony_citypedef struct {
324a8e1175bSopenharmony_ci    /**The size in bytes of the hardware-specific secure element MAC context
325a8e1175bSopenharmony_ci     * structure
326a8e1175bSopenharmony_ci     */
327a8e1175bSopenharmony_ci    size_t                    MBEDTLS_PRIVATE(context_size);
328a8e1175bSopenharmony_ci    /** Function that performs a MAC setup operation
329a8e1175bSopenharmony_ci     */
330a8e1175bSopenharmony_ci    psa_drv_se_mac_setup_t          MBEDTLS_PRIVATE(p_setup);
331a8e1175bSopenharmony_ci    /** Function that performs a MAC update operation
332a8e1175bSopenharmony_ci     */
333a8e1175bSopenharmony_ci    psa_drv_se_mac_update_t         MBEDTLS_PRIVATE(p_update);
334a8e1175bSopenharmony_ci    /** Function that completes a MAC operation
335a8e1175bSopenharmony_ci     */
336a8e1175bSopenharmony_ci    psa_drv_se_mac_finish_t         MBEDTLS_PRIVATE(p_finish);
337a8e1175bSopenharmony_ci    /** Function that completes a MAC operation with a verify check
338a8e1175bSopenharmony_ci     */
339a8e1175bSopenharmony_ci    psa_drv_se_mac_finish_verify_t  MBEDTLS_PRIVATE(p_finish_verify);
340a8e1175bSopenharmony_ci    /** Function that aborts a previously started MAC operation
341a8e1175bSopenharmony_ci     */
342a8e1175bSopenharmony_ci    psa_drv_se_mac_abort_t          MBEDTLS_PRIVATE(p_abort);
343a8e1175bSopenharmony_ci    /** Function that performs a MAC operation in one call
344a8e1175bSopenharmony_ci     */
345a8e1175bSopenharmony_ci    psa_drv_se_mac_generate_t       MBEDTLS_PRIVATE(p_mac);
346a8e1175bSopenharmony_ci    /** Function that performs a MAC and verify operation in one call
347a8e1175bSopenharmony_ci     */
348a8e1175bSopenharmony_ci    psa_drv_se_mac_verify_t         MBEDTLS_PRIVATE(p_mac_verify);
349a8e1175bSopenharmony_ci} psa_drv_se_mac_t;
350a8e1175bSopenharmony_ci/**@}*/
351a8e1175bSopenharmony_ci
352a8e1175bSopenharmony_ci/** \defgroup se_cipher Secure Element Symmetric Ciphers
353a8e1175bSopenharmony_ci *
354a8e1175bSopenharmony_ci * Encryption and Decryption using secure element keys in block modes other
355a8e1175bSopenharmony_ci * than ECB must be done in multiple parts, using the following flow:
356a8e1175bSopenharmony_ci * - `psa_drv_se_cipher_setup_t`
357a8e1175bSopenharmony_ci * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
358a8e1175bSopenharmony_ci * - `psa_drv_se_cipher_update_t`
359a8e1175bSopenharmony_ci * - `psa_drv_se_cipher_update_t`
360a8e1175bSopenharmony_ci * - ...
361a8e1175bSopenharmony_ci * - `psa_drv_se_cipher_finish_t`
362a8e1175bSopenharmony_ci *
363a8e1175bSopenharmony_ci * If a previously started secure element Cipher operation needs to be
364a8e1175bSopenharmony_ci * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
365a8e1175bSopenharmony_ci * to do so may result in allocated resources not being freed or in other
366a8e1175bSopenharmony_ci * undefined behavior.
367a8e1175bSopenharmony_ci *
368a8e1175bSopenharmony_ci * In situations where a PSA Cryptographic API implementation is using a block
369a8e1175bSopenharmony_ci * mode not-supported by the underlying hardware or driver, it can construct
370a8e1175bSopenharmony_ci * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
371a8e1175bSopenharmony_ci * for the cipher operations.
372a8e1175bSopenharmony_ci */
373a8e1175bSopenharmony_ci/**@{*/
374a8e1175bSopenharmony_ci
375a8e1175bSopenharmony_ci/** \brief A function that provides the cipher setup function for a
376a8e1175bSopenharmony_ci * secure element driver
377a8e1175bSopenharmony_ci *
378a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
379a8e1175bSopenharmony_ci * \param[in,out] op_context    A structure that will contain the
380a8e1175bSopenharmony_ci *                              hardware-specific cipher context.
381a8e1175bSopenharmony_ci * \param[in] key_slot          The slot of the key to be used for the
382a8e1175bSopenharmony_ci *                              operation
383a8e1175bSopenharmony_ci * \param[in] algorithm         The algorithm to be used in the cipher
384a8e1175bSopenharmony_ci *                              operation
385a8e1175bSopenharmony_ci * \param[in] direction         Indicates whether the operation is an encrypt
386a8e1175bSopenharmony_ci *                              or decrypt
387a8e1175bSopenharmony_ci *
388a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
389a8e1175bSopenharmony_ci * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
390a8e1175bSopenharmony_ci */
391a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context,
392a8e1175bSopenharmony_ci                                                  void *op_context,
393a8e1175bSopenharmony_ci                                                  psa_key_slot_number_t key_slot,
394a8e1175bSopenharmony_ci                                                  psa_algorithm_t algorithm,
395a8e1175bSopenharmony_ci                                                  psa_encrypt_or_decrypt_t direction);
396a8e1175bSopenharmony_ci
397a8e1175bSopenharmony_ci/** \brief A function that sets the initialization vector (if
398a8e1175bSopenharmony_ci * necessary) for a secure element cipher operation
399a8e1175bSopenharmony_ci *
400a8e1175bSopenharmony_ci * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
401a8e1175bSopenharmony_ci * two IV functions: one to set the IV, and one to generate it internally. The
402a8e1175bSopenharmony_ci * generate function is not necessary for the drivers to implement as the PSA
403a8e1175bSopenharmony_ci * Crypto implementation can do the generation using its RNG features.
404a8e1175bSopenharmony_ci *
405a8e1175bSopenharmony_ci * \param[in,out] op_context    A structure that contains the previously set up
406a8e1175bSopenharmony_ci *                              hardware-specific cipher context
407a8e1175bSopenharmony_ci * \param[in] p_iv              A buffer containing the initialization vector
408a8e1175bSopenharmony_ci * \param[in] iv_length         The size (in bytes) of the `p_iv` buffer
409a8e1175bSopenharmony_ci *
410a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
411a8e1175bSopenharmony_ci */
412a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
413a8e1175bSopenharmony_ci                                                   const uint8_t *p_iv,
414a8e1175bSopenharmony_ci                                                   size_t iv_length);
415a8e1175bSopenharmony_ci
416a8e1175bSopenharmony_ci/** \brief A function that continues a previously started secure element cipher
417a8e1175bSopenharmony_ci * operation
418a8e1175bSopenharmony_ci *
419a8e1175bSopenharmony_ci * \param[in,out] op_context        A hardware-specific structure for the
420a8e1175bSopenharmony_ci *                                  previously started cipher operation
421a8e1175bSopenharmony_ci * \param[in] p_input               A buffer containing the data to be
422a8e1175bSopenharmony_ci *                                  encrypted/decrypted
423a8e1175bSopenharmony_ci * \param[in] input_size            The size in bytes of the buffer pointed to
424a8e1175bSopenharmony_ci *                                  by `p_input`
425a8e1175bSopenharmony_ci * \param[out] p_output             The caller-allocated buffer where the
426a8e1175bSopenharmony_ci *                                  output will be placed
427a8e1175bSopenharmony_ci * \param[in] output_size           The allocated size in bytes of the
428a8e1175bSopenharmony_ci *                                  `p_output` buffer
429a8e1175bSopenharmony_ci * \param[out] p_output_length      After completion, will contain the number
430a8e1175bSopenharmony_ci *                                  of bytes placed in the `p_output` buffer
431a8e1175bSopenharmony_ci *
432a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
433a8e1175bSopenharmony_ci */
434a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
435a8e1175bSopenharmony_ci                                                   const uint8_t *p_input,
436a8e1175bSopenharmony_ci                                                   size_t input_size,
437a8e1175bSopenharmony_ci                                                   uint8_t *p_output,
438a8e1175bSopenharmony_ci                                                   size_t output_size,
439a8e1175bSopenharmony_ci                                                   size_t *p_output_length);
440a8e1175bSopenharmony_ci
441a8e1175bSopenharmony_ci/** \brief A function that completes a previously started secure element cipher
442a8e1175bSopenharmony_ci * operation
443a8e1175bSopenharmony_ci *
444a8e1175bSopenharmony_ci * \param[in,out] op_context    A hardware-specific structure for the
445a8e1175bSopenharmony_ci *                              previously started cipher operation
446a8e1175bSopenharmony_ci * \param[out] p_output         The caller-allocated buffer where the output
447a8e1175bSopenharmony_ci *                              will be placed
448a8e1175bSopenharmony_ci * \param[in] output_size       The allocated size in bytes of the `p_output`
449a8e1175bSopenharmony_ci *                              buffer
450a8e1175bSopenharmony_ci * \param[out] p_output_length  After completion, will contain the number of
451a8e1175bSopenharmony_ci *                              bytes placed in the `p_output` buffer
452a8e1175bSopenharmony_ci *
453a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
454a8e1175bSopenharmony_ci */
455a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context,
456a8e1175bSopenharmony_ci                                                   uint8_t *p_output,
457a8e1175bSopenharmony_ci                                                   size_t output_size,
458a8e1175bSopenharmony_ci                                                   size_t *p_output_length);
459a8e1175bSopenharmony_ci
460a8e1175bSopenharmony_ci/** \brief A function that aborts a previously started secure element cipher
461a8e1175bSopenharmony_ci * operation
462a8e1175bSopenharmony_ci *
463a8e1175bSopenharmony_ci * \param[in,out] op_context    A hardware-specific structure for the
464a8e1175bSopenharmony_ci *                              previously started cipher operation
465a8e1175bSopenharmony_ci */
466a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context);
467a8e1175bSopenharmony_ci
468a8e1175bSopenharmony_ci/** \brief A function that performs the ECB block mode for secure element
469a8e1175bSopenharmony_ci * cipher operations
470a8e1175bSopenharmony_ci *
471a8e1175bSopenharmony_ci * Note: this function should only be used with implementations that do not
472a8e1175bSopenharmony_ci * provide a needed higher-level operation.
473a8e1175bSopenharmony_ci *
474a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
475a8e1175bSopenharmony_ci * \param[in] key_slot          The slot of the key to be used for the operation
476a8e1175bSopenharmony_ci * \param[in] algorithm         The algorithm to be used in the cipher operation
477a8e1175bSopenharmony_ci * \param[in] direction         Indicates whether the operation is an encrypt or
478a8e1175bSopenharmony_ci *                              decrypt
479a8e1175bSopenharmony_ci * \param[in] p_input           A buffer containing the data to be
480a8e1175bSopenharmony_ci *                              encrypted/decrypted
481a8e1175bSopenharmony_ci * \param[in] input_size        The size in bytes of the buffer pointed to by
482a8e1175bSopenharmony_ci *                              `p_input`
483a8e1175bSopenharmony_ci * \param[out] p_output         The caller-allocated buffer where the output
484a8e1175bSopenharmony_ci *                              will be placed
485a8e1175bSopenharmony_ci * \param[in] output_size       The allocated size in bytes of the `p_output`
486a8e1175bSopenharmony_ci *                              buffer
487a8e1175bSopenharmony_ci *
488a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
489a8e1175bSopenharmony_ci * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
490a8e1175bSopenharmony_ci */
491a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context,
492a8e1175bSopenharmony_ci                                                psa_key_slot_number_t key_slot,
493a8e1175bSopenharmony_ci                                                psa_algorithm_t algorithm,
494a8e1175bSopenharmony_ci                                                psa_encrypt_or_decrypt_t direction,
495a8e1175bSopenharmony_ci                                                const uint8_t *p_input,
496a8e1175bSopenharmony_ci                                                size_t input_size,
497a8e1175bSopenharmony_ci                                                uint8_t *p_output,
498a8e1175bSopenharmony_ci                                                size_t output_size);
499a8e1175bSopenharmony_ci
500a8e1175bSopenharmony_ci/**
501a8e1175bSopenharmony_ci * \brief A struct containing all of the function pointers needed to implement
502a8e1175bSopenharmony_ci * cipher operations using secure elements.
503a8e1175bSopenharmony_ci *
504a8e1175bSopenharmony_ci * PSA Crypto API implementations should populate instances of the table as
505a8e1175bSopenharmony_ci * appropriate upon startup or at build time.
506a8e1175bSopenharmony_ci *
507a8e1175bSopenharmony_ci * If one of the functions is not implemented (such as
508a8e1175bSopenharmony_ci * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
509a8e1175bSopenharmony_ci */
510a8e1175bSopenharmony_citypedef struct {
511a8e1175bSopenharmony_ci    /** The size in bytes of the hardware-specific secure element cipher
512a8e1175bSopenharmony_ci     * context structure
513a8e1175bSopenharmony_ci     */
514a8e1175bSopenharmony_ci    size_t               MBEDTLS_PRIVATE(context_size);
515a8e1175bSopenharmony_ci    /** Function that performs a cipher setup operation */
516a8e1175bSopenharmony_ci    psa_drv_se_cipher_setup_t  MBEDTLS_PRIVATE(p_setup);
517a8e1175bSopenharmony_ci    /** Function that sets a cipher IV (if necessary) */
518a8e1175bSopenharmony_ci    psa_drv_se_cipher_set_iv_t MBEDTLS_PRIVATE(p_set_iv);
519a8e1175bSopenharmony_ci    /** Function that performs a cipher update operation */
520a8e1175bSopenharmony_ci    psa_drv_se_cipher_update_t MBEDTLS_PRIVATE(p_update);
521a8e1175bSopenharmony_ci    /** Function that completes a cipher operation */
522a8e1175bSopenharmony_ci    psa_drv_se_cipher_finish_t MBEDTLS_PRIVATE(p_finish);
523a8e1175bSopenharmony_ci    /** Function that aborts a cipher operation */
524a8e1175bSopenharmony_ci    psa_drv_se_cipher_abort_t  MBEDTLS_PRIVATE(p_abort);
525a8e1175bSopenharmony_ci    /** Function that performs ECB mode for a cipher operation
526a8e1175bSopenharmony_ci     * (Danger: ECB mode should not be used directly by clients of the PSA
527a8e1175bSopenharmony_ci     * Crypto Client API)
528a8e1175bSopenharmony_ci     */
529a8e1175bSopenharmony_ci    psa_drv_se_cipher_ecb_t    MBEDTLS_PRIVATE(p_ecb);
530a8e1175bSopenharmony_ci} psa_drv_se_cipher_t;
531a8e1175bSopenharmony_ci
532a8e1175bSopenharmony_ci/**@}*/
533a8e1175bSopenharmony_ci
534a8e1175bSopenharmony_ci/** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
535a8e1175bSopenharmony_ci *
536a8e1175bSopenharmony_ci * Since the amount of data that can (or should) be encrypted or signed using
537a8e1175bSopenharmony_ci * asymmetric keys is limited by the key size, asymmetric key operations using
538a8e1175bSopenharmony_ci * keys in a secure element must be done in single function calls.
539a8e1175bSopenharmony_ci */
540a8e1175bSopenharmony_ci/**@{*/
541a8e1175bSopenharmony_ci
542a8e1175bSopenharmony_ci/**
543a8e1175bSopenharmony_ci * \brief A function that signs a hash or short message with a private key in
544a8e1175bSopenharmony_ci * a secure element
545a8e1175bSopenharmony_ci *
546a8e1175bSopenharmony_ci * \param[in,out] drv_context       The driver context structure.
547a8e1175bSopenharmony_ci * \param[in] key_slot              Key slot of an asymmetric key pair
548a8e1175bSopenharmony_ci * \param[in] alg                   A signature algorithm that is compatible
549a8e1175bSopenharmony_ci *                                  with the type of `key`
550a8e1175bSopenharmony_ci * \param[in] p_hash                The hash to sign
551a8e1175bSopenharmony_ci * \param[in] hash_length           Size of the `p_hash` buffer in bytes
552a8e1175bSopenharmony_ci * \param[out] p_signature          Buffer where the signature is to be written
553a8e1175bSopenharmony_ci * \param[in] signature_size        Size of the `p_signature` buffer in bytes
554a8e1175bSopenharmony_ci * \param[out] p_signature_length   On success, the number of bytes
555a8e1175bSopenharmony_ci *                                  that make up the returned signature value
556a8e1175bSopenharmony_ci *
557a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
558a8e1175bSopenharmony_ci */
559a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context,
560a8e1175bSopenharmony_ci                                                     psa_key_slot_number_t key_slot,
561a8e1175bSopenharmony_ci                                                     psa_algorithm_t alg,
562a8e1175bSopenharmony_ci                                                     const uint8_t *p_hash,
563a8e1175bSopenharmony_ci                                                     size_t hash_length,
564a8e1175bSopenharmony_ci                                                     uint8_t *p_signature,
565a8e1175bSopenharmony_ci                                                     size_t signature_size,
566a8e1175bSopenharmony_ci                                                     size_t *p_signature_length);
567a8e1175bSopenharmony_ci
568a8e1175bSopenharmony_ci/**
569a8e1175bSopenharmony_ci * \brief A function that verifies the signature a hash or short message using
570a8e1175bSopenharmony_ci * an asymmetric public key in a secure element
571a8e1175bSopenharmony_ci *
572a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
573a8e1175bSopenharmony_ci * \param[in] key_slot          Key slot of a public key or an asymmetric key
574a8e1175bSopenharmony_ci *                              pair
575a8e1175bSopenharmony_ci * \param[in] alg               A signature algorithm that is compatible with
576a8e1175bSopenharmony_ci *                              the type of `key`
577a8e1175bSopenharmony_ci * \param[in] p_hash            The hash whose signature is to be verified
578a8e1175bSopenharmony_ci * \param[in] hash_length       Size of the `p_hash` buffer in bytes
579a8e1175bSopenharmony_ci * \param[in] p_signature       Buffer containing the signature to verify
580a8e1175bSopenharmony_ci * \param[in] signature_length  Size of the `p_signature` buffer in bytes
581a8e1175bSopenharmony_ci *
582a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
583a8e1175bSopenharmony_ci *         The signature is valid.
584a8e1175bSopenharmony_ci */
585a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context,
586a8e1175bSopenharmony_ci                                                       psa_key_slot_number_t key_slot,
587a8e1175bSopenharmony_ci                                                       psa_algorithm_t alg,
588a8e1175bSopenharmony_ci                                                       const uint8_t *p_hash,
589a8e1175bSopenharmony_ci                                                       size_t hash_length,
590a8e1175bSopenharmony_ci                                                       const uint8_t *p_signature,
591a8e1175bSopenharmony_ci                                                       size_t signature_length);
592a8e1175bSopenharmony_ci
593a8e1175bSopenharmony_ci/**
594a8e1175bSopenharmony_ci * \brief A function that encrypts a short message with an asymmetric public
595a8e1175bSopenharmony_ci * key in a secure element
596a8e1175bSopenharmony_ci *
597a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
598a8e1175bSopenharmony_ci * \param[in] key_slot          Key slot of a public key or an asymmetric key
599a8e1175bSopenharmony_ci *                              pair
600a8e1175bSopenharmony_ci * \param[in] alg               An asymmetric encryption algorithm that is
601a8e1175bSopenharmony_ci *                              compatible with the type of `key`
602a8e1175bSopenharmony_ci * \param[in] p_input           The message to encrypt
603a8e1175bSopenharmony_ci * \param[in] input_length      Size of the `p_input` buffer in bytes
604a8e1175bSopenharmony_ci * \param[in] p_salt            A salt or label, if supported by the
605a8e1175bSopenharmony_ci *                              encryption algorithm
606a8e1175bSopenharmony_ci *                              If the algorithm does not support a
607a8e1175bSopenharmony_ci *                              salt, pass `NULL`.
608a8e1175bSopenharmony_ci *                              If the algorithm supports an optional
609a8e1175bSopenharmony_ci *                              salt and you do not want to pass a salt,
610a8e1175bSopenharmony_ci *                              pass `NULL`.
611a8e1175bSopenharmony_ci *                              For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
612a8e1175bSopenharmony_ci *                              supported.
613a8e1175bSopenharmony_ci * \param[in] salt_length       Size of the `p_salt` buffer in bytes
614a8e1175bSopenharmony_ci *                              If `p_salt` is `NULL`, pass 0.
615a8e1175bSopenharmony_ci * \param[out] p_output         Buffer where the encrypted message is to
616a8e1175bSopenharmony_ci *                              be written
617a8e1175bSopenharmony_ci * \param[in] output_size       Size of the `p_output` buffer in bytes
618a8e1175bSopenharmony_ci * \param[out] p_output_length  On success, the number of bytes that make up
619a8e1175bSopenharmony_ci *                              the returned output
620a8e1175bSopenharmony_ci *
621a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
622a8e1175bSopenharmony_ci */
623a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context,
624a8e1175bSopenharmony_ci                                                        psa_key_slot_number_t key_slot,
625a8e1175bSopenharmony_ci                                                        psa_algorithm_t alg,
626a8e1175bSopenharmony_ci                                                        const uint8_t *p_input,
627a8e1175bSopenharmony_ci                                                        size_t input_length,
628a8e1175bSopenharmony_ci                                                        const uint8_t *p_salt,
629a8e1175bSopenharmony_ci                                                        size_t salt_length,
630a8e1175bSopenharmony_ci                                                        uint8_t *p_output,
631a8e1175bSopenharmony_ci                                                        size_t output_size,
632a8e1175bSopenharmony_ci                                                        size_t *p_output_length);
633a8e1175bSopenharmony_ci
634a8e1175bSopenharmony_ci/**
635a8e1175bSopenharmony_ci * \brief A function that decrypts a short message with an asymmetric private
636a8e1175bSopenharmony_ci * key in a secure element.
637a8e1175bSopenharmony_ci *
638a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
639a8e1175bSopenharmony_ci * \param[in] key_slot          Key slot of an asymmetric key pair
640a8e1175bSopenharmony_ci * \param[in] alg               An asymmetric encryption algorithm that is
641a8e1175bSopenharmony_ci *                              compatible with the type of `key`
642a8e1175bSopenharmony_ci * \param[in] p_input           The message to decrypt
643a8e1175bSopenharmony_ci * \param[in] input_length      Size of the `p_input` buffer in bytes
644a8e1175bSopenharmony_ci * \param[in] p_salt            A salt or label, if supported by the
645a8e1175bSopenharmony_ci *                              encryption algorithm
646a8e1175bSopenharmony_ci *                              If the algorithm does not support a
647a8e1175bSopenharmony_ci *                              salt, pass `NULL`.
648a8e1175bSopenharmony_ci *                              If the algorithm supports an optional
649a8e1175bSopenharmony_ci *                              salt and you do not want to pass a salt,
650a8e1175bSopenharmony_ci *                              pass `NULL`.
651a8e1175bSopenharmony_ci *                              For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
652a8e1175bSopenharmony_ci *                              supported.
653a8e1175bSopenharmony_ci * \param[in] salt_length       Size of the `p_salt` buffer in bytes
654a8e1175bSopenharmony_ci *                              If `p_salt` is `NULL`, pass 0.
655a8e1175bSopenharmony_ci * \param[out] p_output         Buffer where the decrypted message is to
656a8e1175bSopenharmony_ci *                              be written
657a8e1175bSopenharmony_ci * \param[in] output_size       Size of the `p_output` buffer in bytes
658a8e1175bSopenharmony_ci * \param[out] p_output_length  On success, the number of bytes
659a8e1175bSopenharmony_ci *                              that make up the returned output
660a8e1175bSopenharmony_ci *
661a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
662a8e1175bSopenharmony_ci */
663a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context,
664a8e1175bSopenharmony_ci                                                        psa_key_slot_number_t key_slot,
665a8e1175bSopenharmony_ci                                                        psa_algorithm_t alg,
666a8e1175bSopenharmony_ci                                                        const uint8_t *p_input,
667a8e1175bSopenharmony_ci                                                        size_t input_length,
668a8e1175bSopenharmony_ci                                                        const uint8_t *p_salt,
669a8e1175bSopenharmony_ci                                                        size_t salt_length,
670a8e1175bSopenharmony_ci                                                        uint8_t *p_output,
671a8e1175bSopenharmony_ci                                                        size_t output_size,
672a8e1175bSopenharmony_ci                                                        size_t *p_output_length);
673a8e1175bSopenharmony_ci
674a8e1175bSopenharmony_ci/**
675a8e1175bSopenharmony_ci * \brief A struct containing all of the function pointers needed to implement
676a8e1175bSopenharmony_ci * asymmetric cryptographic operations using secure elements.
677a8e1175bSopenharmony_ci *
678a8e1175bSopenharmony_ci * PSA Crypto API implementations should populate instances of the table as
679a8e1175bSopenharmony_ci * appropriate upon startup or at build time.
680a8e1175bSopenharmony_ci *
681a8e1175bSopenharmony_ci * If one of the functions is not implemented, it should be set to NULL.
682a8e1175bSopenharmony_ci */
683a8e1175bSopenharmony_citypedef struct {
684a8e1175bSopenharmony_ci    /** Function that performs an asymmetric sign operation */
685a8e1175bSopenharmony_ci    psa_drv_se_asymmetric_sign_t    MBEDTLS_PRIVATE(p_sign);
686a8e1175bSopenharmony_ci    /** Function that performs an asymmetric verify operation */
687a8e1175bSopenharmony_ci    psa_drv_se_asymmetric_verify_t  MBEDTLS_PRIVATE(p_verify);
688a8e1175bSopenharmony_ci    /** Function that performs an asymmetric encrypt operation */
689a8e1175bSopenharmony_ci    psa_drv_se_asymmetric_encrypt_t MBEDTLS_PRIVATE(p_encrypt);
690a8e1175bSopenharmony_ci    /** Function that performs an asymmetric decrypt operation */
691a8e1175bSopenharmony_ci    psa_drv_se_asymmetric_decrypt_t MBEDTLS_PRIVATE(p_decrypt);
692a8e1175bSopenharmony_ci} psa_drv_se_asymmetric_t;
693a8e1175bSopenharmony_ci
694a8e1175bSopenharmony_ci/**@}*/
695a8e1175bSopenharmony_ci
696a8e1175bSopenharmony_ci/** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
697a8e1175bSopenharmony_ci * Authenticated Encryption with Additional Data (AEAD) operations with secure
698a8e1175bSopenharmony_ci * elements must be done in one function call. While this creates a burden for
699a8e1175bSopenharmony_ci * implementers as there must be sufficient space in memory for the entire
700a8e1175bSopenharmony_ci * message, it prevents decrypted data from being made available before the
701a8e1175bSopenharmony_ci * authentication operation is complete and the data is known to be authentic.
702a8e1175bSopenharmony_ci */
703a8e1175bSopenharmony_ci/**@{*/
704a8e1175bSopenharmony_ci
705a8e1175bSopenharmony_ci/** \brief A function that performs a secure element authenticated encryption
706a8e1175bSopenharmony_ci * operation
707a8e1175bSopenharmony_ci *
708a8e1175bSopenharmony_ci * \param[in,out] drv_context           The driver context structure.
709a8e1175bSopenharmony_ci * \param[in] key_slot                  Slot containing the key to use.
710a8e1175bSopenharmony_ci * \param[in] algorithm                 The AEAD algorithm to compute
711a8e1175bSopenharmony_ci *                                      (\c PSA_ALG_XXX value such that
712a8e1175bSopenharmony_ci *                                      #PSA_ALG_IS_AEAD(`alg`) is true)
713a8e1175bSopenharmony_ci * \param[in] p_nonce                   Nonce or IV to use
714a8e1175bSopenharmony_ci * \param[in] nonce_length              Size of the `p_nonce` buffer in bytes
715a8e1175bSopenharmony_ci * \param[in] p_additional_data         Additional data that will be
716a8e1175bSopenharmony_ci *                                      authenticated but not encrypted
717a8e1175bSopenharmony_ci * \param[in] additional_data_length    Size of `p_additional_data` in bytes
718a8e1175bSopenharmony_ci * \param[in] p_plaintext               Data that will be authenticated and
719a8e1175bSopenharmony_ci *                                      encrypted
720a8e1175bSopenharmony_ci * \param[in] plaintext_length          Size of `p_plaintext` in bytes
721a8e1175bSopenharmony_ci * \param[out] p_ciphertext             Output buffer for the authenticated and
722a8e1175bSopenharmony_ci *                                      encrypted data. The additional data is
723a8e1175bSopenharmony_ci *                                      not part of this output. For algorithms
724a8e1175bSopenharmony_ci *                                      where the encrypted data and the
725a8e1175bSopenharmony_ci *                                      authentication tag are defined as
726a8e1175bSopenharmony_ci *                                      separate outputs, the authentication
727a8e1175bSopenharmony_ci *                                      tag is appended to the encrypted data.
728a8e1175bSopenharmony_ci * \param[in] ciphertext_size           Size of the `p_ciphertext` buffer in
729a8e1175bSopenharmony_ci *                                      bytes
730a8e1175bSopenharmony_ci * \param[out] p_ciphertext_length      On success, the size of the output in
731a8e1175bSopenharmony_ci *                                      the `p_ciphertext` buffer
732a8e1175bSopenharmony_ci *
733a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
734a8e1175bSopenharmony_ci *         Success.
735a8e1175bSopenharmony_ci */
736a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context,
737a8e1175bSopenharmony_ci                                                  psa_key_slot_number_t key_slot,
738a8e1175bSopenharmony_ci                                                  psa_algorithm_t algorithm,
739a8e1175bSopenharmony_ci                                                  const uint8_t *p_nonce,
740a8e1175bSopenharmony_ci                                                  size_t nonce_length,
741a8e1175bSopenharmony_ci                                                  const uint8_t *p_additional_data,
742a8e1175bSopenharmony_ci                                                  size_t additional_data_length,
743a8e1175bSopenharmony_ci                                                  const uint8_t *p_plaintext,
744a8e1175bSopenharmony_ci                                                  size_t plaintext_length,
745a8e1175bSopenharmony_ci                                                  uint8_t *p_ciphertext,
746a8e1175bSopenharmony_ci                                                  size_t ciphertext_size,
747a8e1175bSopenharmony_ci                                                  size_t *p_ciphertext_length);
748a8e1175bSopenharmony_ci
749a8e1175bSopenharmony_ci/** A function that performs a secure element authenticated decryption operation
750a8e1175bSopenharmony_ci *
751a8e1175bSopenharmony_ci * \param[in,out] drv_context           The driver context structure.
752a8e1175bSopenharmony_ci * \param[in] key_slot                  Slot containing the key to use
753a8e1175bSopenharmony_ci * \param[in] algorithm                 The AEAD algorithm to compute
754a8e1175bSopenharmony_ci *                                      (\c PSA_ALG_XXX value such that
755a8e1175bSopenharmony_ci *                                      #PSA_ALG_IS_AEAD(`alg`) is true)
756a8e1175bSopenharmony_ci * \param[in] p_nonce                   Nonce or IV to use
757a8e1175bSopenharmony_ci * \param[in] nonce_length              Size of the `p_nonce` buffer in bytes
758a8e1175bSopenharmony_ci * \param[in] p_additional_data         Additional data that has been
759a8e1175bSopenharmony_ci *                                      authenticated but not encrypted
760a8e1175bSopenharmony_ci * \param[in] additional_data_length    Size of `p_additional_data` in bytes
761a8e1175bSopenharmony_ci * \param[in] p_ciphertext              Data that has been authenticated and
762a8e1175bSopenharmony_ci *                                      encrypted.
763a8e1175bSopenharmony_ci *                                      For algorithms where the encrypted data
764a8e1175bSopenharmony_ci *                                      and the authentication tag are defined
765a8e1175bSopenharmony_ci *                                      as separate inputs, the buffer must
766a8e1175bSopenharmony_ci *                                      contain the encrypted data followed by
767a8e1175bSopenharmony_ci *                                      the authentication tag.
768a8e1175bSopenharmony_ci * \param[in] ciphertext_length         Size of `p_ciphertext` in bytes
769a8e1175bSopenharmony_ci * \param[out] p_plaintext              Output buffer for the decrypted data
770a8e1175bSopenharmony_ci * \param[in] plaintext_size            Size of the `p_plaintext` buffer in
771a8e1175bSopenharmony_ci *                                      bytes
772a8e1175bSopenharmony_ci * \param[out] p_plaintext_length       On success, the size of the output in
773a8e1175bSopenharmony_ci *                                      the `p_plaintext` buffer
774a8e1175bSopenharmony_ci *
775a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
776a8e1175bSopenharmony_ci *         Success.
777a8e1175bSopenharmony_ci */
778a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context,
779a8e1175bSopenharmony_ci                                                  psa_key_slot_number_t key_slot,
780a8e1175bSopenharmony_ci                                                  psa_algorithm_t algorithm,
781a8e1175bSopenharmony_ci                                                  const uint8_t *p_nonce,
782a8e1175bSopenharmony_ci                                                  size_t nonce_length,
783a8e1175bSopenharmony_ci                                                  const uint8_t *p_additional_data,
784a8e1175bSopenharmony_ci                                                  size_t additional_data_length,
785a8e1175bSopenharmony_ci                                                  const uint8_t *p_ciphertext,
786a8e1175bSopenharmony_ci                                                  size_t ciphertext_length,
787a8e1175bSopenharmony_ci                                                  uint8_t *p_plaintext,
788a8e1175bSopenharmony_ci                                                  size_t plaintext_size,
789a8e1175bSopenharmony_ci                                                  size_t *p_plaintext_length);
790a8e1175bSopenharmony_ci
791a8e1175bSopenharmony_ci/**
792a8e1175bSopenharmony_ci * \brief A struct containing all of the function pointers needed to implement
793a8e1175bSopenharmony_ci * secure element Authenticated Encryption with Additional Data operations
794a8e1175bSopenharmony_ci *
795a8e1175bSopenharmony_ci * PSA Crypto API implementations should populate instances of the table as
796a8e1175bSopenharmony_ci * appropriate upon startup.
797a8e1175bSopenharmony_ci *
798a8e1175bSopenharmony_ci * If one of the functions is not implemented, it should be set to NULL.
799a8e1175bSopenharmony_ci */
800a8e1175bSopenharmony_citypedef struct {
801a8e1175bSopenharmony_ci    /** Function that performs the AEAD encrypt operation */
802a8e1175bSopenharmony_ci    psa_drv_se_aead_encrypt_t MBEDTLS_PRIVATE(p_encrypt);
803a8e1175bSopenharmony_ci    /** Function that performs the AEAD decrypt operation */
804a8e1175bSopenharmony_ci    psa_drv_se_aead_decrypt_t MBEDTLS_PRIVATE(p_decrypt);
805a8e1175bSopenharmony_ci} psa_drv_se_aead_t;
806a8e1175bSopenharmony_ci/**@}*/
807a8e1175bSopenharmony_ci
808a8e1175bSopenharmony_ci/** \defgroup se_key_management Secure Element Key Management
809a8e1175bSopenharmony_ci * Currently, key management is limited to importing keys in the clear,
810a8e1175bSopenharmony_ci * destroying keys, and exporting keys in the clear.
811a8e1175bSopenharmony_ci * Whether a key may be exported is determined by the key policies in place
812a8e1175bSopenharmony_ci * on the key slot.
813a8e1175bSopenharmony_ci */
814a8e1175bSopenharmony_ci/**@{*/
815a8e1175bSopenharmony_ci
816a8e1175bSopenharmony_ci/** An enumeration indicating how a key is created.
817a8e1175bSopenharmony_ci */
818a8e1175bSopenharmony_citypedef enum {
819a8e1175bSopenharmony_ci    PSA_KEY_CREATION_IMPORT, /**< During psa_import_key() */
820a8e1175bSopenharmony_ci    PSA_KEY_CREATION_GENERATE, /**< During psa_generate_key() */
821a8e1175bSopenharmony_ci    PSA_KEY_CREATION_DERIVE, /**< During psa_key_derivation_output_key() */
822a8e1175bSopenharmony_ci    PSA_KEY_CREATION_COPY, /**< During psa_copy_key() */
823a8e1175bSopenharmony_ci
824a8e1175bSopenharmony_ci#ifndef __DOXYGEN_ONLY__
825a8e1175bSopenharmony_ci    /** A key is being registered with mbedtls_psa_register_se_key().
826a8e1175bSopenharmony_ci     *
827a8e1175bSopenharmony_ci     * The core only passes this value to
828a8e1175bSopenharmony_ci     * psa_drv_se_key_management_t::p_validate_slot_number, not to
829a8e1175bSopenharmony_ci     * psa_drv_se_key_management_t::p_allocate. The call to
830a8e1175bSopenharmony_ci     * `p_validate_slot_number` is not followed by any other call to the
831a8e1175bSopenharmony_ci     * driver: the key is considered successfully registered if the call to
832a8e1175bSopenharmony_ci     * `p_validate_slot_number` succeeds, or if `p_validate_slot_number` is
833a8e1175bSopenharmony_ci     * null.
834a8e1175bSopenharmony_ci     *
835a8e1175bSopenharmony_ci     * With this creation method, the driver must return #PSA_SUCCESS if
836a8e1175bSopenharmony_ci     * the given attributes are compatible with the existing key in the slot,
837a8e1175bSopenharmony_ci     * and #PSA_ERROR_DOES_NOT_EXIST if the driver can determine that there
838a8e1175bSopenharmony_ci     * is no key with the specified slot number.
839a8e1175bSopenharmony_ci     *
840a8e1175bSopenharmony_ci     * This is an Mbed Crypto extension.
841a8e1175bSopenharmony_ci     */
842a8e1175bSopenharmony_ci    PSA_KEY_CREATION_REGISTER,
843a8e1175bSopenharmony_ci#endif
844a8e1175bSopenharmony_ci} psa_key_creation_method_t;
845a8e1175bSopenharmony_ci
846a8e1175bSopenharmony_ci/** \brief A function that allocates a slot for a key.
847a8e1175bSopenharmony_ci *
848a8e1175bSopenharmony_ci * To create a key in a specific slot in a secure element, the core
849a8e1175bSopenharmony_ci * first calls this function to determine a valid slot number,
850a8e1175bSopenharmony_ci * then calls a function to create the key material in that slot.
851a8e1175bSopenharmony_ci * In nominal conditions (that is, if no error occurs),
852a8e1175bSopenharmony_ci * the effect of a call to a key creation function in the PSA Cryptography
853a8e1175bSopenharmony_ci * API with a lifetime that places the key in a secure element is the
854a8e1175bSopenharmony_ci * following:
855a8e1175bSopenharmony_ci * -# The core calls psa_drv_se_key_management_t::p_allocate
856a8e1175bSopenharmony_ci *    (or in some implementations
857a8e1175bSopenharmony_ci *    psa_drv_se_key_management_t::p_validate_slot_number). The driver
858a8e1175bSopenharmony_ci *    selects (or validates) a suitable slot number given the key attributes
859a8e1175bSopenharmony_ci *    and the state of the secure element.
860a8e1175bSopenharmony_ci * -# The core calls a key creation function in the driver.
861a8e1175bSopenharmony_ci *
862a8e1175bSopenharmony_ci * The key creation functions in the PSA Cryptography API are:
863a8e1175bSopenharmony_ci * - psa_import_key(), which causes
864a8e1175bSopenharmony_ci *   a call to `p_allocate` with \p method = #PSA_KEY_CREATION_IMPORT
865a8e1175bSopenharmony_ci *   then a call to psa_drv_se_key_management_t::p_import.
866a8e1175bSopenharmony_ci * - psa_generate_key(), which causes
867a8e1175bSopenharmony_ci *   a call to `p_allocate` with \p method = #PSA_KEY_CREATION_GENERATE
868a8e1175bSopenharmony_ci *   then a call to psa_drv_se_key_management_t::p_import.
869a8e1175bSopenharmony_ci * - psa_key_derivation_output_key(), which causes
870a8e1175bSopenharmony_ci *   a call to `p_allocate` with \p method = #PSA_KEY_CREATION_DERIVE
871a8e1175bSopenharmony_ci *   then a call to psa_drv_se_key_derivation_t::p_derive.
872a8e1175bSopenharmony_ci * - psa_copy_key(), which causes
873a8e1175bSopenharmony_ci *   a call to `p_allocate` with \p method = #PSA_KEY_CREATION_COPY
874a8e1175bSopenharmony_ci *   then a call to psa_drv_se_key_management_t::p_export.
875a8e1175bSopenharmony_ci *
876a8e1175bSopenharmony_ci * In case of errors, other behaviors are possible.
877a8e1175bSopenharmony_ci * - If the PSA Cryptography subsystem dies after the first step,
878a8e1175bSopenharmony_ci *   for example because the device has lost power abruptly,
879a8e1175bSopenharmony_ci *   the second step may never happen, or may happen after a reset
880a8e1175bSopenharmony_ci *   and re-initialization. Alternatively, after a reset and
881a8e1175bSopenharmony_ci *   re-initialization, the core may call
882a8e1175bSopenharmony_ci *   psa_drv_se_key_management_t::p_destroy on the slot number that
883a8e1175bSopenharmony_ci *   was allocated (or validated) instead of calling a key creation function.
884a8e1175bSopenharmony_ci * - If an error occurs, the core may call
885a8e1175bSopenharmony_ci *   psa_drv_se_key_management_t::p_destroy on the slot number that
886a8e1175bSopenharmony_ci *   was allocated (or validated) instead of calling a key creation function.
887a8e1175bSopenharmony_ci *
888a8e1175bSopenharmony_ci * Errors and system resets also have an impact on the driver's persistent
889a8e1175bSopenharmony_ci * data. If a reset happens before the overall key creation process is
890a8e1175bSopenharmony_ci * completed (before or after the second step above), it is unspecified
891a8e1175bSopenharmony_ci * whether the persistent data after the reset is identical to what it
892a8e1175bSopenharmony_ci * was before or after the call to `p_allocate` (or `p_validate_slot_number`).
893a8e1175bSopenharmony_ci *
894a8e1175bSopenharmony_ci * \param[in,out] drv_context       The driver context structure.
895a8e1175bSopenharmony_ci * \param[in,out] persistent_data   A pointer to the persistent data
896a8e1175bSopenharmony_ci *                                  that allows writing.
897a8e1175bSopenharmony_ci * \param[in] attributes            Attributes of the key.
898a8e1175bSopenharmony_ci * \param method                    The way in which the key is being created.
899a8e1175bSopenharmony_ci * \param[out] key_slot             Slot where the key will be stored.
900a8e1175bSopenharmony_ci *                                  This must be a valid slot for a key of the
901a8e1175bSopenharmony_ci *                                  chosen type. It must be unoccupied.
902a8e1175bSopenharmony_ci *
903a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
904a8e1175bSopenharmony_ci *         Success.
905a8e1175bSopenharmony_ci *         The core will record \c *key_slot as the key slot where the key
906a8e1175bSopenharmony_ci *         is stored and will update the persistent data in storage.
907a8e1175bSopenharmony_ci * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
908a8e1175bSopenharmony_ci * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
909a8e1175bSopenharmony_ci */
910a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_allocate_key_t)(
911a8e1175bSopenharmony_ci    psa_drv_se_context_t *drv_context,
912a8e1175bSopenharmony_ci    void *persistent_data,
913a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes,
914a8e1175bSopenharmony_ci    psa_key_creation_method_t method,
915a8e1175bSopenharmony_ci    psa_key_slot_number_t *key_slot);
916a8e1175bSopenharmony_ci
917a8e1175bSopenharmony_ci/** \brief A function that determines whether a slot number is valid
918a8e1175bSopenharmony_ci * for a key.
919a8e1175bSopenharmony_ci *
920a8e1175bSopenharmony_ci * To create a key in a specific slot in a secure element, the core
921a8e1175bSopenharmony_ci * first calls this function to validate the choice of slot number,
922a8e1175bSopenharmony_ci * then calls a function to create the key material in that slot.
923a8e1175bSopenharmony_ci * See the documentation of #psa_drv_se_allocate_key_t for more details.
924a8e1175bSopenharmony_ci *
925a8e1175bSopenharmony_ci * As of the PSA Cryptography API specification version 1.0, there is no way
926a8e1175bSopenharmony_ci * for applications to trigger a call to this function. However some
927a8e1175bSopenharmony_ci * implementations offer the capability to create or declare a key in
928a8e1175bSopenharmony_ci * a specific slot via implementation-specific means, generally for the
929a8e1175bSopenharmony_ci * sake of initial device provisioning or onboarding. Such a mechanism may
930a8e1175bSopenharmony_ci * be added to a future version of the PSA Cryptography API specification.
931a8e1175bSopenharmony_ci *
932a8e1175bSopenharmony_ci * This function may update the driver's persistent data through
933a8e1175bSopenharmony_ci * \p persistent_data. The core will save the updated persistent data at the
934a8e1175bSopenharmony_ci * end of the key creation process. See the description of
935a8e1175bSopenharmony_ci * ::psa_drv_se_allocate_key_t for more information.
936a8e1175bSopenharmony_ci *
937a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
938a8e1175bSopenharmony_ci * \param[in,out] persistent_data   A pointer to the persistent data
939a8e1175bSopenharmony_ci *                                  that allows writing.
940a8e1175bSopenharmony_ci * \param[in] attributes        Attributes of the key.
941a8e1175bSopenharmony_ci * \param method                The way in which the key is being created.
942a8e1175bSopenharmony_ci * \param[in] key_slot          Slot where the key is to be stored.
943a8e1175bSopenharmony_ci *
944a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
945a8e1175bSopenharmony_ci *         The given slot number is valid for a key with the given
946a8e1175bSopenharmony_ci *         attributes.
947a8e1175bSopenharmony_ci * \retval #PSA_ERROR_INVALID_ARGUMENT
948a8e1175bSopenharmony_ci *         The given slot number is not valid for a key with the
949a8e1175bSopenharmony_ci *         given attributes. This includes the case where the slot
950a8e1175bSopenharmony_ci *         number is not valid at all.
951a8e1175bSopenharmony_ci * \retval #PSA_ERROR_ALREADY_EXISTS
952a8e1175bSopenharmony_ci *         There is already a key with the specified slot number.
953a8e1175bSopenharmony_ci *         Drivers may choose to return this error from the key
954a8e1175bSopenharmony_ci *         creation function instead.
955a8e1175bSopenharmony_ci */
956a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_validate_slot_number_t)(
957a8e1175bSopenharmony_ci    psa_drv_se_context_t *drv_context,
958a8e1175bSopenharmony_ci    void *persistent_data,
959a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes,
960a8e1175bSopenharmony_ci    psa_key_creation_method_t method,
961a8e1175bSopenharmony_ci    psa_key_slot_number_t key_slot);
962a8e1175bSopenharmony_ci
963a8e1175bSopenharmony_ci/** \brief A function that imports a key into a secure element in binary format
964a8e1175bSopenharmony_ci *
965a8e1175bSopenharmony_ci * This function can support any output from psa_export_key(). Refer to the
966a8e1175bSopenharmony_ci * documentation of psa_export_key() for the format for each key type.
967a8e1175bSopenharmony_ci *
968a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
969a8e1175bSopenharmony_ci * \param key_slot              Slot where the key will be stored.
970a8e1175bSopenharmony_ci *                              This must be a valid slot for a key of the
971a8e1175bSopenharmony_ci *                              chosen type. It must be unoccupied.
972a8e1175bSopenharmony_ci * \param[in] attributes        The key attributes, including the lifetime,
973a8e1175bSopenharmony_ci *                              the key type and the usage policy.
974a8e1175bSopenharmony_ci *                              Drivers should not access the key size stored
975a8e1175bSopenharmony_ci *                              in the attributes: it may not match the
976a8e1175bSopenharmony_ci *                              data passed in \p data.
977a8e1175bSopenharmony_ci *                              Drivers can call psa_get_key_lifetime(),
978a8e1175bSopenharmony_ci *                              psa_get_key_type(),
979a8e1175bSopenharmony_ci *                              psa_get_key_usage_flags() and
980a8e1175bSopenharmony_ci *                              psa_get_key_algorithm() to access this
981a8e1175bSopenharmony_ci *                              information.
982a8e1175bSopenharmony_ci * \param[in] data              Buffer containing the key data.
983a8e1175bSopenharmony_ci * \param[in] data_length       Size of the \p data buffer in bytes.
984a8e1175bSopenharmony_ci * \param[out] bits             On success, the key size in bits. The driver
985a8e1175bSopenharmony_ci *                              must determine this value after parsing the
986a8e1175bSopenharmony_ci *                              key according to the key type.
987a8e1175bSopenharmony_ci *                              This value is not used if the function fails.
988a8e1175bSopenharmony_ci *
989a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
990a8e1175bSopenharmony_ci *         Success.
991a8e1175bSopenharmony_ci */
992a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_import_key_t)(
993a8e1175bSopenharmony_ci    psa_drv_se_context_t *drv_context,
994a8e1175bSopenharmony_ci    psa_key_slot_number_t key_slot,
995a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes,
996a8e1175bSopenharmony_ci    const uint8_t *data,
997a8e1175bSopenharmony_ci    size_t data_length,
998a8e1175bSopenharmony_ci    size_t *bits);
999a8e1175bSopenharmony_ci
1000a8e1175bSopenharmony_ci/**
1001a8e1175bSopenharmony_ci * \brief A function that destroys a secure element key and restore the slot to
1002a8e1175bSopenharmony_ci * its default state
1003a8e1175bSopenharmony_ci *
1004a8e1175bSopenharmony_ci * This function destroys the content of the key from a secure element.
1005a8e1175bSopenharmony_ci * Implementations shall make a best effort to ensure that any previous content
1006a8e1175bSopenharmony_ci * of the slot is unrecoverable.
1007a8e1175bSopenharmony_ci *
1008a8e1175bSopenharmony_ci * This function returns the specified slot to its default state.
1009a8e1175bSopenharmony_ci *
1010a8e1175bSopenharmony_ci * \param[in,out] drv_context       The driver context structure.
1011a8e1175bSopenharmony_ci * \param[in,out] persistent_data   A pointer to the persistent data
1012a8e1175bSopenharmony_ci *                                  that allows writing.
1013a8e1175bSopenharmony_ci * \param key_slot                  The key slot to erase.
1014a8e1175bSopenharmony_ci *
1015a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
1016a8e1175bSopenharmony_ci *         The slot's content, if any, has been erased.
1017a8e1175bSopenharmony_ci */
1018a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_destroy_key_t)(
1019a8e1175bSopenharmony_ci    psa_drv_se_context_t *drv_context,
1020a8e1175bSopenharmony_ci    void *persistent_data,
1021a8e1175bSopenharmony_ci    psa_key_slot_number_t key_slot);
1022a8e1175bSopenharmony_ci
1023a8e1175bSopenharmony_ci/**
1024a8e1175bSopenharmony_ci * \brief A function that exports a secure element key in binary format
1025a8e1175bSopenharmony_ci *
1026a8e1175bSopenharmony_ci * The output of this function can be passed to psa_import_key() to
1027a8e1175bSopenharmony_ci * create an equivalent object.
1028a8e1175bSopenharmony_ci *
1029a8e1175bSopenharmony_ci * If a key is created with `psa_import_key()` and then exported with
1030a8e1175bSopenharmony_ci * this function, it is not guaranteed that the resulting data is
1031a8e1175bSopenharmony_ci * identical: the implementation may choose a different representation
1032a8e1175bSopenharmony_ci * of the same key if the format permits it.
1033a8e1175bSopenharmony_ci *
1034a8e1175bSopenharmony_ci * This function should generate output in the same format that
1035a8e1175bSopenharmony_ci * `psa_export_key()` does. Refer to the
1036a8e1175bSopenharmony_ci * documentation of `psa_export_key()` for the format for each key type.
1037a8e1175bSopenharmony_ci *
1038a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
1039a8e1175bSopenharmony_ci * \param[in] key               Slot whose content is to be exported. This must
1040a8e1175bSopenharmony_ci *                              be an occupied key slot.
1041a8e1175bSopenharmony_ci * \param[out] p_data           Buffer where the key data is to be written.
1042a8e1175bSopenharmony_ci * \param[in] data_size         Size of the `p_data` buffer in bytes.
1043a8e1175bSopenharmony_ci * \param[out] p_data_length    On success, the number of bytes
1044a8e1175bSopenharmony_ci *                              that make up the key data.
1045a8e1175bSopenharmony_ci *
1046a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
1047a8e1175bSopenharmony_ci * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
1048a8e1175bSopenharmony_ci * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
1049a8e1175bSopenharmony_ci * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
1050a8e1175bSopenharmony_ci * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1051a8e1175bSopenharmony_ci * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
1052a8e1175bSopenharmony_ci * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1053a8e1175bSopenharmony_ci */
1054a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context,
1055a8e1175bSopenharmony_ci                                                psa_key_slot_number_t key,
1056a8e1175bSopenharmony_ci                                                uint8_t *p_data,
1057a8e1175bSopenharmony_ci                                                size_t data_size,
1058a8e1175bSopenharmony_ci                                                size_t *p_data_length);
1059a8e1175bSopenharmony_ci
1060a8e1175bSopenharmony_ci/**
1061a8e1175bSopenharmony_ci * \brief A function that generates a symmetric or asymmetric key on a secure
1062a8e1175bSopenharmony_ci * element
1063a8e1175bSopenharmony_ci *
1064a8e1175bSopenharmony_ci * If the key type \c type recorded in \p attributes
1065a8e1175bSopenharmony_ci * is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\c type) = 1),
1066a8e1175bSopenharmony_ci * the driver may export the public key at the time of generation,
1067a8e1175bSopenharmony_ci * in the format documented for psa_export_public_key() by writing it
1068a8e1175bSopenharmony_ci * to the \p pubkey buffer.
1069a8e1175bSopenharmony_ci * This is optional, intended for secure elements that output the
1070a8e1175bSopenharmony_ci * public key at generation time and that cannot export the public key
1071a8e1175bSopenharmony_ci * later. Drivers that do not need this feature should leave
1072a8e1175bSopenharmony_ci * \p *pubkey_length set to 0 and should
1073a8e1175bSopenharmony_ci * implement the psa_drv_key_management_t::p_export_public function.
1074a8e1175bSopenharmony_ci * Some implementations do not support this feature, in which case
1075a8e1175bSopenharmony_ci * \p pubkey is \c NULL and \p pubkey_size is 0.
1076a8e1175bSopenharmony_ci *
1077a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
1078a8e1175bSopenharmony_ci * \param key_slot              Slot where the key will be stored.
1079a8e1175bSopenharmony_ci *                              This must be a valid slot for a key of the
1080a8e1175bSopenharmony_ci *                              chosen type. It must be unoccupied.
1081a8e1175bSopenharmony_ci * \param[in] attributes        The key attributes, including the lifetime,
1082a8e1175bSopenharmony_ci *                              the key type and size, and the usage policy.
1083a8e1175bSopenharmony_ci *                              Drivers can call psa_get_key_lifetime(),
1084a8e1175bSopenharmony_ci *                              psa_get_key_type(), psa_get_key_bits(),
1085a8e1175bSopenharmony_ci *                              psa_get_key_usage_flags() and
1086a8e1175bSopenharmony_ci *                              psa_get_key_algorithm() to access this
1087a8e1175bSopenharmony_ci *                              information.
1088a8e1175bSopenharmony_ci * \param[out] pubkey           A buffer where the driver can write the
1089a8e1175bSopenharmony_ci *                              public key, when generating an asymmetric
1090a8e1175bSopenharmony_ci *                              key pair.
1091a8e1175bSopenharmony_ci *                              This is \c NULL when generating a symmetric
1092a8e1175bSopenharmony_ci *                              key or if the core does not support
1093a8e1175bSopenharmony_ci *                              exporting the public key at generation time.
1094a8e1175bSopenharmony_ci * \param pubkey_size           The size of the `pubkey` buffer in bytes.
1095a8e1175bSopenharmony_ci *                              This is 0 when generating a symmetric
1096a8e1175bSopenharmony_ci *                              key or if the core does not support
1097a8e1175bSopenharmony_ci *                              exporting the public key at generation time.
1098a8e1175bSopenharmony_ci * \param[out] pubkey_length    On entry, this is always 0.
1099a8e1175bSopenharmony_ci *                              On success, the number of bytes written to
1100a8e1175bSopenharmony_ci *                              \p pubkey. If this is 0 or unchanged on return,
1101a8e1175bSopenharmony_ci *                              the core will not read the \p pubkey buffer,
1102a8e1175bSopenharmony_ci *                              and will instead call the driver's
1103a8e1175bSopenharmony_ci *                              psa_drv_key_management_t::p_export_public
1104a8e1175bSopenharmony_ci *                              function to export the public key when needed.
1105a8e1175bSopenharmony_ci */
1106a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_generate_key_t)(
1107a8e1175bSopenharmony_ci    psa_drv_se_context_t *drv_context,
1108a8e1175bSopenharmony_ci    psa_key_slot_number_t key_slot,
1109a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes,
1110a8e1175bSopenharmony_ci    uint8_t *pubkey, size_t pubkey_size, size_t *pubkey_length);
1111a8e1175bSopenharmony_ci
1112a8e1175bSopenharmony_ci/**
1113a8e1175bSopenharmony_ci * \brief A struct containing all of the function pointers needed to for secure
1114a8e1175bSopenharmony_ci * element key management
1115a8e1175bSopenharmony_ci *
1116a8e1175bSopenharmony_ci * PSA Crypto API implementations should populate instances of the table as
1117a8e1175bSopenharmony_ci * appropriate upon startup or at build time.
1118a8e1175bSopenharmony_ci *
1119a8e1175bSopenharmony_ci * If one of the functions is not implemented, it should be set to NULL.
1120a8e1175bSopenharmony_ci */
1121a8e1175bSopenharmony_citypedef struct {
1122a8e1175bSopenharmony_ci    /** Function that allocates a slot for a key. */
1123a8e1175bSopenharmony_ci    psa_drv_se_allocate_key_t   MBEDTLS_PRIVATE(p_allocate);
1124a8e1175bSopenharmony_ci    /** Function that checks the validity of a slot for a key. */
1125a8e1175bSopenharmony_ci    psa_drv_se_validate_slot_number_t MBEDTLS_PRIVATE(p_validate_slot_number);
1126a8e1175bSopenharmony_ci    /** Function that performs a key import operation */
1127a8e1175bSopenharmony_ci    psa_drv_se_import_key_t     MBEDTLS_PRIVATE(p_import);
1128a8e1175bSopenharmony_ci    /** Function that performs a generation */
1129a8e1175bSopenharmony_ci    psa_drv_se_generate_key_t   MBEDTLS_PRIVATE(p_generate);
1130a8e1175bSopenharmony_ci    /** Function that performs a key destroy operation */
1131a8e1175bSopenharmony_ci    psa_drv_se_destroy_key_t    MBEDTLS_PRIVATE(p_destroy);
1132a8e1175bSopenharmony_ci    /** Function that performs a key export operation */
1133a8e1175bSopenharmony_ci    psa_drv_se_export_key_t     MBEDTLS_PRIVATE(p_export);
1134a8e1175bSopenharmony_ci    /** Function that performs a public key export operation */
1135a8e1175bSopenharmony_ci    psa_drv_se_export_key_t     MBEDTLS_PRIVATE(p_export_public);
1136a8e1175bSopenharmony_ci} psa_drv_se_key_management_t;
1137a8e1175bSopenharmony_ci
1138a8e1175bSopenharmony_ci/**@}*/
1139a8e1175bSopenharmony_ci
1140a8e1175bSopenharmony_ci/** \defgroup driver_derivation Secure Element Key Derivation and Agreement
1141a8e1175bSopenharmony_ci * Key derivation is the process of generating new key material using an
1142a8e1175bSopenharmony_ci * existing key and additional parameters, iterating through a basic
1143a8e1175bSopenharmony_ci * cryptographic function, such as a hash.
1144a8e1175bSopenharmony_ci * Key agreement is a part of cryptographic protocols that allows two parties
1145a8e1175bSopenharmony_ci * to agree on the same key value, but starting from different original key
1146a8e1175bSopenharmony_ci * material.
1147a8e1175bSopenharmony_ci * The flows are similar, and the PSA Crypto Driver Model uses the same functions
1148a8e1175bSopenharmony_ci * for both of the flows.
1149a8e1175bSopenharmony_ci *
1150a8e1175bSopenharmony_ci * There are two different final functions for the flows,
1151a8e1175bSopenharmony_ci * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`.
1152a8e1175bSopenharmony_ci * `psa_drv_se_key_derivation_derive` is used when the key material should be
1153a8e1175bSopenharmony_ci * placed in a slot on the hardware and not exposed to the caller.
1154a8e1175bSopenharmony_ci * `psa_drv_se_key_derivation_export` is used when the key material should be
1155a8e1175bSopenharmony_ci * returned to the PSA Cryptographic API implementation.
1156a8e1175bSopenharmony_ci *
1157a8e1175bSopenharmony_ci * Different key derivation algorithms require a different number of inputs.
1158a8e1175bSopenharmony_ci * Instead of having an API that takes as input variable length arrays, which
1159a8e1175bSopenharmony_ci * can be problematic to manage on embedded platforms, the inputs are passed
1160a8e1175bSopenharmony_ci * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that
1161a8e1175bSopenharmony_ci * is called multiple times with different `collateral_id`s. Thus, for a key
1162a8e1175bSopenharmony_ci * derivation algorithm that required 3 parameter inputs, the flow would look
1163a8e1175bSopenharmony_ci * something like:
1164a8e1175bSopenharmony_ci * ~~~~~~~~~~~~~{.c}
1165a8e1175bSopenharmony_ci * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1166a8e1175bSopenharmony_ci * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0,
1167a8e1175bSopenharmony_ci *                                      p_collateral_0,
1168a8e1175bSopenharmony_ci *                                      collateral_0_size);
1169a8e1175bSopenharmony_ci * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1,
1170a8e1175bSopenharmony_ci *                                      p_collateral_1,
1171a8e1175bSopenharmony_ci *                                      collateral_1_size);
1172a8e1175bSopenharmony_ci * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2,
1173a8e1175bSopenharmony_ci *                                      p_collateral_2,
1174a8e1175bSopenharmony_ci *                                      collateral_2_size);
1175a8e1175bSopenharmony_ci * psa_drv_se_key_derivation_derive();
1176a8e1175bSopenharmony_ci * ~~~~~~~~~~~~~
1177a8e1175bSopenharmony_ci *
1178a8e1175bSopenharmony_ci * key agreement example:
1179a8e1175bSopenharmony_ci * ~~~~~~~~~~~~~{.c}
1180a8e1175bSopenharmony_ci * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1181a8e1175bSopenharmony_ci * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1182a8e1175bSopenharmony_ci * psa_drv_se_key_derivation_export(p_session_key,
1183a8e1175bSopenharmony_ci *                                  session_key_size,
1184a8e1175bSopenharmony_ci *                                  &session_key_length);
1185a8e1175bSopenharmony_ci * ~~~~~~~~~~~~~
1186a8e1175bSopenharmony_ci */
1187a8e1175bSopenharmony_ci/**@{*/
1188a8e1175bSopenharmony_ci
1189a8e1175bSopenharmony_ci/** \brief A function that Sets up a secure element key derivation operation by
1190a8e1175bSopenharmony_ci * specifying the algorithm and the source key sot
1191a8e1175bSopenharmony_ci *
1192a8e1175bSopenharmony_ci * \param[in,out] drv_context   The driver context structure.
1193a8e1175bSopenharmony_ci * \param[in,out] op_context    A hardware-specific structure containing any
1194a8e1175bSopenharmony_ci *                              context information for the implementation
1195a8e1175bSopenharmony_ci * \param[in] kdf_alg           The algorithm to be used for the key derivation
1196a8e1175bSopenharmony_ci * \param[in] source_key        The key to be used as the source material for
1197a8e1175bSopenharmony_ci *                              the key derivation
1198a8e1175bSopenharmony_ci *
1199a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
1200a8e1175bSopenharmony_ci */
1201a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context,
1202a8e1175bSopenharmony_ci                                                          void *op_context,
1203a8e1175bSopenharmony_ci                                                          psa_algorithm_t kdf_alg,
1204a8e1175bSopenharmony_ci                                                          psa_key_slot_number_t source_key);
1205a8e1175bSopenharmony_ci
1206a8e1175bSopenharmony_ci/** \brief A function that provides collateral (parameters) needed for a secure
1207a8e1175bSopenharmony_ci * element key derivation or key agreement operation
1208a8e1175bSopenharmony_ci *
1209a8e1175bSopenharmony_ci * Since many key derivation algorithms require multiple parameters, it is
1210a8e1175bSopenharmony_ci * expected that this function may be called multiple times for the same
1211a8e1175bSopenharmony_ci * operation, each with a different algorithm-specific `collateral_id`
1212a8e1175bSopenharmony_ci *
1213a8e1175bSopenharmony_ci * \param[in,out] op_context    A hardware-specific structure containing any
1214a8e1175bSopenharmony_ci *                              context information for the implementation
1215a8e1175bSopenharmony_ci * \param[in] collateral_id     An ID for the collateral being provided
1216a8e1175bSopenharmony_ci * \param[in] p_collateral      A buffer containing the collateral data
1217a8e1175bSopenharmony_ci * \param[in] collateral_size   The size in bytes of the collateral
1218a8e1175bSopenharmony_ci *
1219a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
1220a8e1175bSopenharmony_ci */
1221a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context,
1222a8e1175bSopenharmony_ci                                                               uint32_t collateral_id,
1223a8e1175bSopenharmony_ci                                                               const uint8_t *p_collateral,
1224a8e1175bSopenharmony_ci                                                               size_t collateral_size);
1225a8e1175bSopenharmony_ci
1226a8e1175bSopenharmony_ci/** \brief A function that performs the final secure element key derivation
1227a8e1175bSopenharmony_ci * step and place the generated key material in a slot
1228a8e1175bSopenharmony_ci *
1229a8e1175bSopenharmony_ci * \param[in,out] op_context    A hardware-specific structure containing any
1230a8e1175bSopenharmony_ci *                              context information for the implementation
1231a8e1175bSopenharmony_ci * \param[in] dest_key          The slot where the generated key material
1232a8e1175bSopenharmony_ci *                              should be placed
1233a8e1175bSopenharmony_ci *
1234a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
1235a8e1175bSopenharmony_ci */
1236a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context,
1237a8e1175bSopenharmony_ci                                                           psa_key_slot_number_t dest_key);
1238a8e1175bSopenharmony_ci
1239a8e1175bSopenharmony_ci/** \brief A function that performs the final step of a secure element key
1240a8e1175bSopenharmony_ci * agreement and place the generated key material in a buffer
1241a8e1175bSopenharmony_ci *
1242a8e1175bSopenharmony_ci * \param[out] p_output         Buffer in which to place the generated key
1243a8e1175bSopenharmony_ci *                              material
1244a8e1175bSopenharmony_ci * \param[in] output_size       The size in bytes of `p_output`
1245a8e1175bSopenharmony_ci * \param[out] p_output_length  Upon success, contains the number of bytes of
1246a8e1175bSopenharmony_ci *                              key material placed in `p_output`
1247a8e1175bSopenharmony_ci *
1248a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS \emptydescription
1249a8e1175bSopenharmony_ci */
1250a8e1175bSopenharmony_citypedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context,
1251a8e1175bSopenharmony_ci                                                           uint8_t *p_output,
1252a8e1175bSopenharmony_ci                                                           size_t output_size,
1253a8e1175bSopenharmony_ci                                                           size_t *p_output_length);
1254a8e1175bSopenharmony_ci
1255a8e1175bSopenharmony_ci/**
1256a8e1175bSopenharmony_ci * \brief A struct containing all of the function pointers needed to for secure
1257a8e1175bSopenharmony_ci * element key derivation and agreement
1258a8e1175bSopenharmony_ci *
1259a8e1175bSopenharmony_ci * PSA Crypto API implementations should populate instances of the table as
1260a8e1175bSopenharmony_ci * appropriate upon startup.
1261a8e1175bSopenharmony_ci *
1262a8e1175bSopenharmony_ci * If one of the functions is not implemented, it should be set to NULL.
1263a8e1175bSopenharmony_ci */
1264a8e1175bSopenharmony_citypedef struct {
1265a8e1175bSopenharmony_ci    /** The driver-specific size of the key derivation context */
1266a8e1175bSopenharmony_ci    size_t                           MBEDTLS_PRIVATE(context_size);
1267a8e1175bSopenharmony_ci    /** Function that performs a key derivation setup */
1268a8e1175bSopenharmony_ci    psa_drv_se_key_derivation_setup_t      MBEDTLS_PRIVATE(p_setup);
1269a8e1175bSopenharmony_ci    /** Function that sets key derivation collateral */
1270a8e1175bSopenharmony_ci    psa_drv_se_key_derivation_collateral_t MBEDTLS_PRIVATE(p_collateral);
1271a8e1175bSopenharmony_ci    /** Function that performs a final key derivation step */
1272a8e1175bSopenharmony_ci    psa_drv_se_key_derivation_derive_t     MBEDTLS_PRIVATE(p_derive);
1273a8e1175bSopenharmony_ci    /** Function that performs a final key derivation or agreement and
1274a8e1175bSopenharmony_ci     * exports the key */
1275a8e1175bSopenharmony_ci    psa_drv_se_key_derivation_export_t     MBEDTLS_PRIVATE(p_export);
1276a8e1175bSopenharmony_ci} psa_drv_se_key_derivation_t;
1277a8e1175bSopenharmony_ci
1278a8e1175bSopenharmony_ci/**@}*/
1279a8e1175bSopenharmony_ci
1280a8e1175bSopenharmony_ci/** \defgroup se_registration Secure element driver registration
1281a8e1175bSopenharmony_ci */
1282a8e1175bSopenharmony_ci/**@{*/
1283a8e1175bSopenharmony_ci
1284a8e1175bSopenharmony_ci/** A structure containing pointers to all the entry points of a
1285a8e1175bSopenharmony_ci * secure element driver.
1286a8e1175bSopenharmony_ci *
1287a8e1175bSopenharmony_ci * Future versions of this specification may add extra substructures at
1288a8e1175bSopenharmony_ci * the end of this structure.
1289a8e1175bSopenharmony_ci */
1290a8e1175bSopenharmony_citypedef struct {
1291a8e1175bSopenharmony_ci    /** The version of the driver HAL that this driver implements.
1292a8e1175bSopenharmony_ci     * This is a protection against loading driver binaries built against
1293a8e1175bSopenharmony_ci     * a different version of this specification.
1294a8e1175bSopenharmony_ci     * Use #PSA_DRV_SE_HAL_VERSION.
1295a8e1175bSopenharmony_ci     */
1296a8e1175bSopenharmony_ci    uint32_t MBEDTLS_PRIVATE(hal_version);
1297a8e1175bSopenharmony_ci
1298a8e1175bSopenharmony_ci    /** The size of the driver's persistent data in bytes.
1299a8e1175bSopenharmony_ci     *
1300a8e1175bSopenharmony_ci     * This can be 0 if the driver does not need persistent data.
1301a8e1175bSopenharmony_ci     *
1302a8e1175bSopenharmony_ci     * See the documentation of psa_drv_se_context_t::persistent_data
1303a8e1175bSopenharmony_ci     * for more information about why and how a driver can use
1304a8e1175bSopenharmony_ci     * persistent data.
1305a8e1175bSopenharmony_ci     */
1306a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(persistent_data_size);
1307a8e1175bSopenharmony_ci
1308a8e1175bSopenharmony_ci    /** The driver initialization function.
1309a8e1175bSopenharmony_ci     *
1310a8e1175bSopenharmony_ci     * This function is called once during the initialization of the
1311a8e1175bSopenharmony_ci     * PSA Cryptography subsystem, before any other function of the
1312a8e1175bSopenharmony_ci     * driver is called. If this function returns a failure status,
1313a8e1175bSopenharmony_ci     * the driver will be unusable, at least until the next system reset.
1314a8e1175bSopenharmony_ci     *
1315a8e1175bSopenharmony_ci     * If this field is \c NULL, it is equivalent to a function that does
1316a8e1175bSopenharmony_ci     * nothing and returns #PSA_SUCCESS.
1317a8e1175bSopenharmony_ci     */
1318a8e1175bSopenharmony_ci    psa_drv_se_init_t MBEDTLS_PRIVATE(p_init);
1319a8e1175bSopenharmony_ci
1320a8e1175bSopenharmony_ci    const psa_drv_se_key_management_t *MBEDTLS_PRIVATE(key_management);
1321a8e1175bSopenharmony_ci    const psa_drv_se_mac_t *MBEDTLS_PRIVATE(mac);
1322a8e1175bSopenharmony_ci    const psa_drv_se_cipher_t *MBEDTLS_PRIVATE(cipher);
1323a8e1175bSopenharmony_ci    const psa_drv_se_aead_t *MBEDTLS_PRIVATE(aead);
1324a8e1175bSopenharmony_ci    const psa_drv_se_asymmetric_t *MBEDTLS_PRIVATE(asymmetric);
1325a8e1175bSopenharmony_ci    const psa_drv_se_key_derivation_t *MBEDTLS_PRIVATE(derivation);
1326a8e1175bSopenharmony_ci} psa_drv_se_t;
1327a8e1175bSopenharmony_ci
1328a8e1175bSopenharmony_ci/** The current version of the secure element driver HAL.
1329a8e1175bSopenharmony_ci */
1330a8e1175bSopenharmony_ci/* 0.0.0 patchlevel 5 */
1331a8e1175bSopenharmony_ci#define PSA_DRV_SE_HAL_VERSION 0x00000005
1332a8e1175bSopenharmony_ci
1333a8e1175bSopenharmony_ci/** Register an external cryptoprocessor (secure element) driver.
1334a8e1175bSopenharmony_ci *
1335a8e1175bSopenharmony_ci * This function is only intended to be used by driver code, not by
1336a8e1175bSopenharmony_ci * application code. In implementations with separation between the
1337a8e1175bSopenharmony_ci * PSA cryptography module and applications, this function should
1338a8e1175bSopenharmony_ci * only be available to callers that run in the same memory space as
1339a8e1175bSopenharmony_ci * the cryptography module, and should not be exposed to applications
1340a8e1175bSopenharmony_ci * running in a different memory space.
1341a8e1175bSopenharmony_ci *
1342a8e1175bSopenharmony_ci * This function may be called before psa_crypto_init(). It is
1343a8e1175bSopenharmony_ci * implementation-defined whether this function may be called
1344a8e1175bSopenharmony_ci * after psa_crypto_init().
1345a8e1175bSopenharmony_ci *
1346a8e1175bSopenharmony_ci * \note Implementations store metadata about keys including the lifetime
1347a8e1175bSopenharmony_ci *       value, which contains the driver's location indicator. Therefore,
1348a8e1175bSopenharmony_ci *       from one instantiation of the PSA Cryptography
1349a8e1175bSopenharmony_ci *       library to the next one, if there is a key in storage with a certain
1350a8e1175bSopenharmony_ci *       lifetime value, you must always register the same driver (or an
1351a8e1175bSopenharmony_ci *       updated version that communicates with the same secure element)
1352a8e1175bSopenharmony_ci *       with the same location value.
1353a8e1175bSopenharmony_ci *
1354a8e1175bSopenharmony_ci * \param location      The location value through which this driver will
1355a8e1175bSopenharmony_ci *                      be exposed to applications.
1356a8e1175bSopenharmony_ci *                      This driver will be used for all keys such that
1357a8e1175bSopenharmony_ci *                      `location == #PSA_KEY_LIFETIME_GET_LOCATION( lifetime )`.
1358a8e1175bSopenharmony_ci *                      The value #PSA_KEY_LOCATION_LOCAL_STORAGE is reserved
1359a8e1175bSopenharmony_ci *                      and may not be used for drivers. Implementations
1360a8e1175bSopenharmony_ci *                      may reserve other values.
1361a8e1175bSopenharmony_ci * \param[in] methods   The method table of the driver. This structure must
1362a8e1175bSopenharmony_ci *                      remain valid for as long as the cryptography
1363a8e1175bSopenharmony_ci *                      module keeps running. It is typically a global
1364a8e1175bSopenharmony_ci *                      constant.
1365a8e1175bSopenharmony_ci *
1366a8e1175bSopenharmony_ci * \return #PSA_SUCCESS
1367a8e1175bSopenharmony_ci *         The driver was successfully registered. Applications can now
1368a8e1175bSopenharmony_ci *         use \p location to access keys through the methods passed to
1369a8e1175bSopenharmony_ci *         this function.
1370a8e1175bSopenharmony_ci * \return #PSA_ERROR_BAD_STATE
1371a8e1175bSopenharmony_ci *         This function was called after the initialization of the
1372a8e1175bSopenharmony_ci *         cryptography module, and this implementation does not support
1373a8e1175bSopenharmony_ci *         driver registration at this stage.
1374a8e1175bSopenharmony_ci * \return #PSA_ERROR_ALREADY_EXISTS
1375a8e1175bSopenharmony_ci *         There is already a registered driver for this value of \p location.
1376a8e1175bSopenharmony_ci * \return #PSA_ERROR_INVALID_ARGUMENT
1377a8e1175bSopenharmony_ci *         \p location is a reserved value.
1378a8e1175bSopenharmony_ci * \return #PSA_ERROR_NOT_SUPPORTED
1379a8e1175bSopenharmony_ci *         `methods->hal_version` is not supported by this implementation.
1380a8e1175bSopenharmony_ci * \return #PSA_ERROR_INSUFFICIENT_MEMORY
1381a8e1175bSopenharmony_ci * \return #PSA_ERROR_NOT_PERMITTED
1382a8e1175bSopenharmony_ci * \return #PSA_ERROR_STORAGE_FAILURE
1383a8e1175bSopenharmony_ci * \return #PSA_ERROR_DATA_CORRUPT
1384a8e1175bSopenharmony_ci */
1385a8e1175bSopenharmony_cipsa_status_t psa_register_se_driver(
1386a8e1175bSopenharmony_ci    psa_key_location_t location,
1387a8e1175bSopenharmony_ci    const psa_drv_se_t *methods);
1388a8e1175bSopenharmony_ci
1389a8e1175bSopenharmony_ci/**@}*/
1390a8e1175bSopenharmony_ci
1391a8e1175bSopenharmony_ci#ifdef __cplusplus
1392a8e1175bSopenharmony_ci}
1393a8e1175bSopenharmony_ci#endif
1394a8e1175bSopenharmony_ci
1395a8e1175bSopenharmony_ci#endif /* PSA_CRYPTO_SE_DRIVER_H */
1396