blob: 737476a9dfc212e8f12d49b457d19e45e3e81198 [file] [log] [blame]
Derek Miller16e72292018-10-15 16:14:24 -05001/**
2 * \file psa/crypto_driver.h
3 * \brief Platform Security Architecture cryptographic driver module
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01004 *
Derek Miller16e72292018-10-15 16:14:24 -05005 * This file describes an API for driver developers to implement to enable
6 * hardware to be called in a standardized way by a PSA Cryptographic API
7 * implementation. The API described is not intended to be called by
8 * application developers.
9 */
10
11/*
12 * Copyright (C) 2018, ARM Limited, All Rights Reserved
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License"); you may
16 * not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 */
Derek Miller5b3417a2018-10-10 17:55:03 -050027#ifndef __PSA_CRYPTO_DRIVER_H__
28#define __PSA_CRYPTO_DRIVER_H__
29
30#include <stddef.h>
31#include <stdint.h>
32
Derek Miller16e72292018-10-15 16:14:24 -050033/** The following types are redefinitions from the psa/crypto.h file.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +010034 * It is intended that these will be moved to a new common header file to
Derek Miller16e72292018-10-15 16:14:24 -050035 * avoid duplication. They are included here for expediency in publication.
36 */
Derek Miller5b3417a2018-10-10 17:55:03 -050037typedef uint32_t psa_status_t;
38typedef uint32_t psa_algorithm_t;
Derek Miller16e72292018-10-15 16:14:24 -050039typedef uint8_t encrypt_or_decrypt_t;
Derek Miller5b3417a2018-10-10 17:55:03 -050040typedef uint32_t psa_key_slot_t;
41typedef uint32_t psa_key_type_t;
Derek Miller81133a62018-10-23 14:55:32 -050042typedef uint32_t psa_key_usage_t;
Derek Miller5b3417a2018-10-10 17:55:03 -050043
Derek Miller6f960ab2018-10-23 15:58:06 -050044#define PSA_CRYPTO_DRIVER_ENCRYPT 1
45#define PSA_CRYPTO_DRIVER_DECRYPT 0
46
Derek Miller5b3417a2018-10-10 17:55:03 -050047/** \defgroup opaque_mac Opaque Message Authentication Code
Derek Miller765682c2018-10-22 15:27:27 -050048 * Generation and authentication of Message Authentication Codes (MACs) using
Jaeden Amerod3d26aa2018-10-26 10:07:32 +010049 * opaque keys can be done either as a single function call (via the
Derek Miller765682c2018-10-22 15:27:27 -050050 * `pcd_mac_opaque_generate_t` or `psa_mac_opaque_verify_t` functions), or in
51 * parts using the following sequence:
52 * - `psa_mac_opaque_setup_t`
53 * - `psa_mac_opaque_update_t`
54 * - `psa_mac_opaque_update_t`
55 * - ...
56 * - `psa_mac_opaque_finish_t` or `psa_mac_opaque_finish_verify_t`
Jaeden Amerod3d26aa2018-10-26 10:07:32 +010057 *
58 * If a previously started Opaque MAC operation needs to be terminated, it
Derek Miller765682c2018-10-22 15:27:27 -050059 * should be done so by the `psa_mac_opaque_abort_t`. Failure to do so may
60 * result in allocated resources not being freed or in other undefined
61 * behavior.
Derek Miller5b3417a2018-10-10 17:55:03 -050062 */
Derek Miller16e72292018-10-15 16:14:24 -050063/**@{*/
Jaeden Amerod3d26aa2018-10-26 10:07:32 +010064/** \brief A function that starts a MAC operation for a PSA Crypto Driver
Derek Miller16e72292018-10-15 16:14:24 -050065 * implementation using an opaque key
Jaeden Amerod3d26aa2018-10-26 10:07:32 +010066 *
67 * \param[in,out] p_context A structure that will contain the
Derek Miller16e72292018-10-15 16:14:24 -050068 * hardware-specific MAC context
69 * \param[in] key_slot The slot of the key to be used for the
70 * operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +010071 * \param[in] algorithm The algorithm to be used to underly the MAC
Derek Miller16e72292018-10-15 16:14:24 -050072 * operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +010073 *
Derek Miller5b3417a2018-10-10 17:55:03 -050074 * \retval PSA_SUCCESS
75 * Success.
76 */
Derek Miller16e72292018-10-15 16:14:24 -050077typedef psa_status_t (*pcd_mac_opaque_setup_t)(void *p_context,
78 psa_key_slot_t key_slot,
79 psa_algorithm_t algorithm);
Derek Miller5b3417a2018-10-10 17:55:03 -050080
Derek Miller16e72292018-10-15 16:14:24 -050081/** \brief A function that continues a previously started MAC operation using
82 * an opaque key
Jaeden Amerod3d26aa2018-10-26 10:07:32 +010083 *
Derek Miller16e72292018-10-15 16:14:24 -050084 * \param[in,out] p_context A hardware-specific structure for the
85 * previously-established MAC operation to be
86 * continued
87 * \param[in] p_input A buffer containing the message to be appended
88 * to the MAC operation
89 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -050090 */
Derek Miller16e72292018-10-15 16:14:24 -050091typedef psa_status_t (*pcd_mac_opaque_update_t)(void *p_context,
92 const uint8_t *p_input,
93 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -050094
Derek Miller16e72292018-10-15 16:14:24 -050095/** \brief a function that completes a previously started MAC operation by
96 * returning the resulting MAC using an opaque key
Jaeden Amerod3d26aa2018-10-26 10:07:32 +010097 *
98 * \param[in,out] p_context A hardware-specific structure for the
99 * previously started MAC operation to be
Derek Miller16e72292018-10-15 16:14:24 -0500100 * finished
101 * \param[out] p_mac A buffer where the generated MAC will be
102 * placed
103 * \param[in] mac_size The size in bytes of the buffer that has been
104 * allocated for the `output` buffer
105 * \param[out] p_mac_length After completion, will contain the number of
Derek Millerf3d0a562018-10-18 16:41:08 -0500106 * bytes placed in the `p_mac` buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100107 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500108 * \retval PSA_SUCCESS
109 * Success.
110 */
Derek Miller16e72292018-10-15 16:14:24 -0500111typedef psa_status_t (*pcd_mac_opaque_finish_t)(void *p_context,
112 uint8_t *p_mac,
113 size_t mac_size,
114 size_t *p_mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500115
Derek Miller16e72292018-10-15 16:14:24 -0500116/** \brief A function that completes a previously started MAC operation by
117 * comparing the resulting MAC against a known value using an opaque key
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100118 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500119 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500120 * started MAC operation to be fiinished
121 * \param[in] p_mac The MAC value against which the resulting MAC will
122 * be compared against
123 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100124 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500125 * \retval PSA_SUCCESS
Derek Miller16e72292018-10-15 16:14:24 -0500126 * The operation completed successfully and the MACs matched each
127 * other
Derek Miller5b3417a2018-10-10 17:55:03 -0500128 * \retval PSA_ERROR_INVALID_SIGNATURE
Derek Miller16e72292018-10-15 16:14:24 -0500129 * The operation completed successfully, but the calculated MAC did
130 * not match the provided MAC
Derek Miller5b3417a2018-10-10 17:55:03 -0500131 */
Derek Miller16e72292018-10-15 16:14:24 -0500132typedef psa_status_t (*pcd_mac_opaque_finish_verify_t)(void *p_context,
133 const uint8_t *p_mac,
134 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500135
Derek Miller16e72292018-10-15 16:14:24 -0500136/** \brief A function that aborts a previous started opaque-key MAC operation
137
Derek Millerf3d0a562018-10-18 16:41:08 -0500138 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500139 * started MAC operation to be aborted
140 */
141typedef psa_status_t (*pcd_mac_opaque_abort_t)(void *p_context);
142
Derek Miller81133a62018-10-23 14:55:32 -0500143/** \brief A function that performs a MAC operation in one command and returns
Derek Miller16e72292018-10-15 16:14:24 -0500144 * the calculated MAC using an opaque key
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100145 *
Derek Miller16e72292018-10-15 16:14:24 -0500146 * \param[in] p_input A buffer containing the message to be MACed
Derek Millerf3d0a562018-10-18 16:41:08 -0500147 * \param[in] input_length The size in bytes of `p_input`
Derek Miller16e72292018-10-15 16:14:24 -0500148 * \param[in] key_slot The slot of the key to be used
Derek Millerf3d0a562018-10-18 16:41:08 -0500149 * \param[in] alg The algorithm to be used to underlie the MAC
Derek Miller16e72292018-10-15 16:14:24 -0500150 * operation
151 * \param[out] p_mac A buffer where the generated MAC will be
152 * placed
Derek Miller81133a62018-10-23 14:55:32 -0500153 * \param[in] mac_size The size in bytes of the `p_mac` buffer
Derek Miller16e72292018-10-15 16:14:24 -0500154 * \param[out] p_mac_length After completion, will contain the number of
155 * bytes placed in the `output` buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100156 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500157 * \retval PSA_SUCCESS
158 * Success.
159 */
Derek Miller16e72292018-10-15 16:14:24 -0500160typedef psa_status_t (*pcd_mac_opaque_generate_t)(const uint8_t *p_input,
161 size_t input_length,
162 psa_key_slot_t key_slot,
163 psa_algorithm_t alg,
164 uint8_t *p_mac,
165 size_t mac_size,
166 size_t *p_mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500167
Derek Miller16e72292018-10-15 16:14:24 -0500168/** \brief A function that performs an MAC operation in one command and
169 * compare the resulting MAC against a known value using an opaque key
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100170 *
Derek Miller16e72292018-10-15 16:14:24 -0500171 * \param[in] p_input A buffer containing the message to be MACed
172 * \param[in] input_length The size in bytes of `input`
173 * \param[in] key_slot The slot of the key to be used
174 * \param[in] alg The algorithm to be used to underlie the MAC
175 * operation
176 * \param[in] p_mac The MAC value against which the resulting MAC will
177 * be compared against
178 * \param[in] mac_length The size in bytes of `mac`
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100179 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500180 * \retval PSA_SUCCESS
Derek Miller16e72292018-10-15 16:14:24 -0500181 * The operation completed successfully and the MACs matched each
182 * other
Derek Miller5b3417a2018-10-10 17:55:03 -0500183 * \retval PSA_ERROR_INVALID_SIGNATURE
Derek Miller16e72292018-10-15 16:14:24 -0500184 * The operation completed successfully, but the calculated MAC did
185 * not match the provided MAC
Derek Miller5b3417a2018-10-10 17:55:03 -0500186 */
Derek Miller16e72292018-10-15 16:14:24 -0500187typedef psa_status_t (*pcd_mac_opaque_verify_t)(const uint8_t *p_input,
Derek Miller5b3417a2018-10-10 17:55:03 -0500188 size_t input_length,
189 psa_key_slot_t key_slot,
190 psa_algorithm_t alg,
Derek Miller16e72292018-10-15 16:14:24 -0500191 const uint8_t *p_mac,
192 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500193
Derek Miller16e72292018-10-15 16:14:24 -0500194/** \brief A struct containing all of the function pointers needed to
195 * implement MAC operations using opaque keys.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100196 *
Derek Miller16e72292018-10-15 16:14:24 -0500197 * PSA Crypto API implementations should populate the table as appropriate
198 * upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -0500199 *
Derek Miller765682c2018-10-22 15:27:27 -0500200 * If one of the functions is not implemented (such as
201 * `pcd_mac_opaque_generate_t`), it should be set to NULL.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100202 *
Derek Miller16e72292018-10-15 16:14:24 -0500203 * Driver implementers should ensure that they implement all of the functions
204 * that make sense for their hardware, and that they provide a full solution
205 * (for example, if they support `p_setup`, they should also support
206 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100207 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500208 */
209struct pcd_mac_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -0500210 /**The size in bytes of the hardware-specific Opaque-MAC Context structure
211 */
212 size_t context_size;
213 /** Function that performs the setup operation
214 */
215 pcd_mac_opaque_setup_t *p_setup;
216 /** Function that performs the update operation
217 */
218 pcd_mac_opaque_update_t *p_update;
219 /** Function that completes the operation
220 */
221 pcd_mac_opaque_finish_t *p_finish;
222 /** Function that completed a MAC operation with a verify check
223 */
224 pcd_mac_opaque_finish_verify_t *p_finish_verify;
225 /** Function that aborts a previoustly started operation
226 */
227 pcd_mac_opaque_abort_t *p_abort;
228 /** Function that performs the MAC operation in one call
229 */
230 pcd_mac_opaque_generate_t *p_mac;
231 /** Function that performs the MAC and verify operation in one call
232 */
233 pcd_mac_opaque_verify_t *p_mac_verify;
Derek Miller5b3417a2018-10-10 17:55:03 -0500234};
Derek Miller16e72292018-10-15 16:14:24 -0500235/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500236
237/** \defgroup transparent_mac Transparent Message Authentication Code
Derek Miller765682c2018-10-22 15:27:27 -0500238 * Generation and authentication of Message Authentication Codes (MACs) using
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100239 * transparent keys can be done either as a single function call (via the
Derek Miller765682c2018-10-22 15:27:27 -0500240 * `pcd_mac_transparent_generate_t` or `psa_mac_transparent_verify_t`
241 * functions), or in parts using the following sequence:
242 * - `psa_mac_transparent_setup_t`
243 * - `psa_mac_transparent_update_t`
244 * - `psa_mac_transparent_update_t`
245 * - ...
246 * - `psa_mac_transparent_finish_t` or `psa_mac_transparent_finish_verify_t`
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100247 *
248 * If a previously started Transparent MAC operation needs to be terminated, it
Derek Miller765682c2018-10-22 15:27:27 -0500249 * should be done so by the `psa_mac_transparent_abort_t`. Failure to do so may
250 * result in allocated resources not being freed or in other undefined
251 * behavior.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100252 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500253 */
Derek Miller16e72292018-10-15 16:14:24 -0500254/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500255
256/** \brief The hardware-specific transparent-key MAC context structure
Derek Miller765682c2018-10-22 15:27:27 -0500257 *
Derek Miller16e72292018-10-15 16:14:24 -0500258 * The contents of this structure are implementation dependent and are
259 * therefore not described here.
Derek Miller5b3417a2018-10-10 17:55:03 -0500260 */
Derek Miller81133a62018-10-23 14:55:32 -0500261typedef struct pcd_mac_transparent_context_s pcd_mac_transparent_context_t;
Derek Miller5b3417a2018-10-10 17:55:03 -0500262
Derek Miller16e72292018-10-15 16:14:24 -0500263/** \brief The function prototype for the setup operation of a
264 * transparent-key MAC operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100265 *
Derek Miller16e72292018-10-15 16:14:24 -0500266 * Functions that implement the prototype should be named in the following
267 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500268 * ~~~~~~~~~~~~~{.c}
Derek Miller16e72292018-10-15 16:14:24 -0500269 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_setup
Derek Miller5b3417a2018-10-10 17:55:03 -0500270 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500271 * Where `ALGO` is the name of the underlying primitive, and `MAC_VARIANT`
272 * is the specific variant of a MAC operation (such as HMAC or CMAC)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100273 *
274 * \param[in,out] p_context A structure that will contain the
Derek Miller16e72292018-10-15 16:14:24 -0500275 * hardware-specific MAC context
276 * \param[in] p_key A buffer containing the cleartext key material
277 * to be used in the operation
278 * \param[in] key_length The size in bytes of the key material
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100279 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500280 * \retval PSA_SUCCESS
281 * Success.
282 */
Derek Miller81133a62018-10-23 14:55:32 -0500283typedef psa_status_t (*pcd_mac_transparent_setup_t)(pcd_mac_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500284 const uint8_t *p_key,
285 size_t key_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500286
Derek Miller16e72292018-10-15 16:14:24 -0500287/** \brief The function prototype for the update operation of a
288 * transparent-key MAC operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500289 *
Derek Miller16e72292018-10-15 16:14:24 -0500290 * Functions that implement the prototype should be named in the following
291 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500292 * ~~~~~~~~~~~~~{.c}
293 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_update
294 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500295 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT`
296 * is the specific variant of a MAC operation (such as HMAC or CMAC)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100297 *
Derek Miller16e72292018-10-15 16:14:24 -0500298 * \param[in,out] p_context A hardware-specific structure for the
299 * previously-established MAC operation to be
300 * continued
301 * \param[in] p_input A buffer containing the message to be appended
302 * to the MAC operation
303 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500304 */
Derek Miller81133a62018-10-23 14:55:32 -0500305typedef psa_status_t (*pcd_mac_transparent_update_t)(pcd_mac_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500306 const uint8_t *p_input,
307 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500308
Derek Miller16e72292018-10-15 16:14:24 -0500309/** \brief The function prototype for the finish operation of a
310 * transparent-key MAC operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100311 *
Derek Miller16e72292018-10-15 16:14:24 -0500312 * Functions that implement the prototype should be named in the following
313 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500314 * ~~~~~~~~~~~~~{.c}
315 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish
316 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500317 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
318 * the specific variant of a MAC operation (such as HMAC or CMAC)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100319 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500320 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500321 * previously started MAC operation to be
322 * finished
323 * \param[out] p_mac A buffer where the generated MAC will be placed
324 * \param[in] mac_length The size in bytes of the buffer that has been
325 * allocated for the `p_mac` buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100326 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500327 * \retval PSA_SUCCESS
328 * Success.
329 */
Derek Miller81133a62018-10-23 14:55:32 -0500330typedef psa_status_t (*pcd_mac_transparent_finish_t)(pcd_mac_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500331 uint8_t *p_mac,
332 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500333
Derek Miller16e72292018-10-15 16:14:24 -0500334/** \brief The function prototype for the finish and verify operation of a
335 * transparent-key MAC operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100336 *
Derek Miller16e72292018-10-15 16:14:24 -0500337 * Functions that implement the prototype should be named in the following
338 * convention:
339 * ~~~~~~~~~~~~~{.c}
340 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish_verify
341 * ~~~~~~~~~~~~~
342 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
343 * the specific variant of a MAC operation (such as HMAC or CMAC)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100344 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500345 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500346 * previously started MAC operation to be
Derek Millerf3d0a562018-10-18 16:41:08 -0500347 * verified and finished
Derek Miller16e72292018-10-15 16:14:24 -0500348 * \param[in] p_mac A buffer containing the MAC that will be used
349 * for verification
350 * \param[in] mac_length The size in bytes of the data in the `p_mac`
351 * buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100352 *
Derek Miller16e72292018-10-15 16:14:24 -0500353 * \retval PSA_SUCCESS
354 * The operation completed successfully and the comparison matched
Derek Miller5b3417a2018-10-10 17:55:03 -0500355 */
Derek Miller81133a62018-10-23 14:55:32 -0500356typedef psa_status_t (*pcd_mac_transparent_finish_verify_t)(pcd_mac_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500357 const uint8_t *p_mac,
358 size_t mac_length);
359
360/** \brief The function prototype for the abort operation for a previously
361 * started transparent-key MAC operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100362 *
Derek Miller16e72292018-10-15 16:14:24 -0500363 * Functions that implement the prototype should be named in the following
364 * convention:
365 * ~~~~~~~~~~~~~{.c}
366 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_abort
367 * ~~~~~~~~~~~~~
368 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
369 * the specific variant of a MAC operation (such as HMAC or CMAC)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100370 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500371 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500372 * previously started MAC operation to be
Derek Millerf3d0a562018-10-18 16:41:08 -0500373 * aborted
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100374 *
Derek Miller16e72292018-10-15 16:14:24 -0500375 */
Derek Miller81133a62018-10-23 14:55:32 -0500376typedef psa_status_t (*pcd_mac_transparent_abort_t)(pcd_mac_transparent_context_t *p_context);
Derek Miller16e72292018-10-15 16:14:24 -0500377
378/** \brief The function prototype for a one-shot operation of a transparent-key
379 * MAC operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100380 *
Derek Miller16e72292018-10-15 16:14:24 -0500381 * Functions that implement the prototype should be named in the following
382 * convention:
383 * ~~~~~~~~~~~~~{.c}
384 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>
385 * ~~~~~~~~~~~~~
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100386 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
Derek Miller16e72292018-10-15 16:14:24 -0500387 * the specific variant of a MAC operation (such as HMAC or CMAC)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100388 *
Derek Miller16e72292018-10-15 16:14:24 -0500389 * \param[in] p_input A buffer containing the data to be MACed
390 * \param[in] input_length The length in bytes of the `p_input` data
391 * \param[in] p_key A buffer containing the key material to be used
392 * for the MAC operation
393 * \param[in] key_length The length in bytes of the `p_key` data
394 * \param[in] alg The algorithm to be performed
395 * \param[out] p_mac The buffer where the resulting MAC will be placed
396 * upon success
397 * \param[in] mac_length The length in bytes of the `p_mac` buffer
398 */
399typedef psa_status_t (*pcd_mac_transparent_t)(const uint8_t *p_input,
400 size_t input_length,
401 const uint8_t *p_key,
402 size_t key_length,
403 psa_algorithm_t alg,
404 uint8_t *p_mac,
405 size_t mac_length);
406
407/** \brief The function prototype for a one-shot operation of a transparent-key
408 * MAC Verify operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100409 *
Derek Miller16e72292018-10-15 16:14:24 -0500410 * Functions that implement the prototype should be named in the following
411 * convention:
412 * ~~~~~~~~~~~~~{.c}
413 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_verify
414 * ~~~~~~~~~~~~~
415 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
416 * the specific variant of a MAC operation (such as HMAC or CMAC)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100417 *
Derek Miller16e72292018-10-15 16:14:24 -0500418 * \param[in] p_input A buffer containing the data to be MACed
419 * \param[in] input_length The length in bytes of the `p_input` data
420 * \param[in] p_key A buffer containing the key material to be used
421 * for the MAC operation
422 * \param[in] key_length The length in bytes of the `p_key` data
423 * \param[in] alg The algorithm to be performed
424 * \param[in] p_mac The MAC data to be compared
425 * \param[in] mac_length The length in bytes of the `p_mac` buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100426 *
Derek Miller16e72292018-10-15 16:14:24 -0500427 * \retval PSA_SUCCESS
428 * The operation completed successfully and the comparison matched
429 */
430typedef psa_status_t (*pcd_mac_transparent_verify_t)(const uint8_t *p_input,
431 size_t input_length,
432 const uint8_t *p_key,
433 size_t key_length,
434 psa_algorithm_t alg,
435 const uint8_t *p_mac,
436 size_t mac_length);
437/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500438
439/** \defgroup opaque_cipher Opaque Symmetric Ciphers
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100440 *
Derek Miller765682c2018-10-22 15:27:27 -0500441 * Encryption and Decryption using opaque keys in block modes other than ECB
442 * must be done in multiple parts, using the following flow:
443 * - `pcd_cipher_opaque_setup_t`
444 * - `pcd_cipher_opaque_set_iv_t` (optional depending upon block mode)
445 * - `pcd_cipher_opaque_update_t`
446 * - ...
447 * - `pcd_cipher_opaque_finish_t`
448
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100449 * If a previously started Opaque Cipher operation needs to be terminated, it
Derek Miller765682c2018-10-22 15:27:27 -0500450 * should be done so by the `psa_cipher_opaque_abort_t`. Failure to do so may
451 * result in allocated resources not being freed or in other undefined
452 * behavior.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100453 *
Derek Miller765682c2018-10-22 15:27:27 -0500454 * In situations where a PSA Cryptographic API implementation is using a block
455 * mode not-supported by the underlying hardware or driver, it can construct
456 * the block mode itself, while calling the `pcd_cipher_opaque_ecb_t` function
457 * pointer for the cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500458 */
Derek Miller16e72292018-10-15 16:14:24 -0500459/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500460
Derek Miller16e72292018-10-15 16:14:24 -0500461/** \brief A function pointer that provides the cipher setup function for
462 * opaque-key operations
Derek Miller5b3417a2018-10-10 17:55:03 -0500463 *
Derek Miller16e72292018-10-15 16:14:24 -0500464 * \param[in,out] p_context A structure that will contain the
465 * hardware-specific cipher context.
466 * \param[in] key_slot The slot of the key to be used for the
467 * operation
468 * \param[in] algorithm The algorithm to be used in the cipher
469 * operation
470 * \param[in] direction Indicates whether the operation is an encrypt
471 * or decrypt
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100472 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500473 * \retval PSA_SUCCESS
474 * \retval PSA_ERROR_NOT_SUPPORTED
475 */
Derek Miller16e72292018-10-15 16:14:24 -0500476typedef psa_status_t (*pcd_cipher_opaque_setup_t)(void *p_context,
477 psa_key_slot_t key_slot,
Derek Miller5b3417a2018-10-10 17:55:03 -0500478 psa_algorithm_t algorithm,
Derek Miller16e72292018-10-15 16:14:24 -0500479 encrypt_or_decrypt_t direction);
480
481/** \brief A function pointer that sets the initialization vector (if
482 * necessary) for an opaque cipher operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100483 *
Derek Miller81133a62018-10-23 14:55:32 -0500484 * Rationale: The `psa_cipher_*` function in the PSA Cryptographic API has two
Derek Miller765682c2018-10-22 15:27:27 -0500485 * IV functions: one to set the IV, and one to generate it internally. The
486 * generate function is not necessary for the driver API as the PSA Crypto
487 * implementation can do the generation using its RNG features.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100488 *
Derek Miller16e72292018-10-15 16:14:24 -0500489 * \param[in,out] p_context A structure that contains the previously set up
490 * hardware-specific cipher context
491 * \param[in] p_iv A buffer containing the initialization vector
492 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100493 *
Derek Miller16e72292018-10-15 16:14:24 -0500494 * \retval PSA_SUCCESS
495 */
496typedef psa_status_t (*pcd_cipher_opaque_set_iv_t)(void *p_context,
497 const uint8_t *p_iv,
498 size_t iv_length);
499
500/** \brief A function that continues a previously started opaque-key cipher
501 * operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100502 *
Derek Miller16e72292018-10-15 16:14:24 -0500503 * \param[in,out] p_context A hardware-specific structure for the
504 * previously started cipher operation
505 * \param[in] p_input A buffer containing the data to be
506 * encrypted/decrypted
507 * \param[in] input_size The size in bytes of the buffer pointed to
508 * by `p_input`
509 * \param[out] p_output The caller-allocated buffer where the
510 * output will be placed
511 * \param[in] output_size The allocated size in bytes of the
512 * `p_output` buffer
513 * \param[out] p_output_length After completion, will contain the number
514 * of bytes placed in the `p_output` buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100515 *
Derek Miller16e72292018-10-15 16:14:24 -0500516 * \retval PSA_SUCCESS
517 */
518typedef psa_status_t (*pcd_cipher_opaque_update_t)(void *p_context,
519 const uint8_t *p_input,
520 size_t input_size,
521 uint8_t *p_output,
522 size_t output_size,
523 size_t *p_output_length);
524
525/** \brief A function that completes a previously started opaque-key cipher
526 * operation
527 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500528 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500529 * previously started cipher operation
Derek Millerf3d0a562018-10-18 16:41:08 -0500530 * \param[out] p_output The caller-allocated buffer where the output
Derek Miller16e72292018-10-15 16:14:24 -0500531 * will be placed
532 * \param[in] output_size The allocated size in bytes of the `p_output`
533 * buffer
534 * \param[out] p_output_length After completion, will contain the number of
535 * bytes placed in the `p_output` buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100536 *
Derek Miller16e72292018-10-15 16:14:24 -0500537 * \retval PSA_SUCCESS
538 */
539typedef psa_status_t (*pcd_cipher_opaque_finish_t)(void *p_context,
540 uint8_t *p_output,
541 size_t output_size,
542 size_t *p_output_length);
543
544/** \brief A function that aborts a previously started opaque-key cipher
545 * operation
546 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500547 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500548 * previously started cipher operation
549 */
550typedef psa_status_t (*pcd_cipher_opaque_abort_t)(void *p_context);
551
552/** \brief A function that performs the ECB block mode for opaque-key cipher
553 * operations
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100554 *
Derek Miller16e72292018-10-15 16:14:24 -0500555 * Note: this function should only be used with implementations that do not
556 * provide a needed higher-level operation.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100557 *
Derek Miller16e72292018-10-15 16:14:24 -0500558 * \param[in] key_slot The slot of the key to be used for the operation
559 * \param[in] algorithm The algorithm to be used in the cipher operation
560 * \param[in] direction Indicates whether the operation is an encrypt or
561 * decrypt
562 * \param[in] p_input A buffer containing the data to be
563 * encrypted/decrypted
564 * \param[in] input_size The size in bytes of the buffer pointed to by
565 * `p_input`
Derek Millerf3d0a562018-10-18 16:41:08 -0500566 * \param[out] p_output The caller-allocated buffer where the output will
Derek Miller16e72292018-10-15 16:14:24 -0500567 * be placed
568 * \param[in] output_size The allocated size in bytes of the `p_output`
569 * buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100570 *
Derek Miller16e72292018-10-15 16:14:24 -0500571 * \retval PSA_SUCCESS
572 * \retval PSA_ERROR_NOT_SUPPORTED
573 */
574typedef psa_status_t (*pcd_cipher_opaque_ecb_t)(psa_key_slot_t key_slot,
575 psa_algorithm_t algorithm,
576 encrypt_or_decrypt_t direction,
577 const uint8_t *p_input,
578 size_t input_size,
579 uint8_t *p_output,
580 size_t output_size);
Derek Miller5b3417a2018-10-10 17:55:03 -0500581
582/**
Derek Miller16e72292018-10-15 16:14:24 -0500583 * \brief A struct containing all of the function pointers needed to implement
584 * cipher operations using opaque keys.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100585 *
Derek Miller16e72292018-10-15 16:14:24 -0500586 * PSA Crypto API implementations should populate instances of the table as
587 * appropriate upon startup.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100588 *
Derek Miller16e72292018-10-15 16:14:24 -0500589 * If one of the functions is not implemented (such as
590 * `pcd_cipher_opaque_ecb_t`), it should be set to NULL.
Derek Miller5b3417a2018-10-10 17:55:03 -0500591 */
592struct pcd_cipher_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -0500593 /** The size in bytes of the hardware-specific Opaque Cipher context
594 * structure
595 */
596 size_t size;
597 /** Function that performs the setup operation */
598 pcd_cipher_opaque_setup_t *p_setup;
599 /** Function that sets the IV (if necessary) */
600 pcd_cipher_opaque_set_iv_t *p_set_iv;
601 /** Function that performs the update operation */
602 pcd_cipher_opaque_update_t *p_update;
603 /** Function that completes the operation */
604 pcd_cipher_opaque_finish_t *p_finish;
605 /** Function that aborts the operation */
606 pcd_cipher_opaque_abort_t *p_abort;
607 /** Function that performs ECB mode for the cipher
608 * (Danger: ECB mode should not be used directly by clients of the PSA
609 * Crypto Client API)
610 */
611 pcd_cipher_opaque_ecb_t *p_ecb;
Derek Miller5b3417a2018-10-10 17:55:03 -0500612};
613
Derek Miller16e72292018-10-15 16:14:24 -0500614/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500615
616/** \defgroup transparent_cipher Transparent Block Cipher
Derek Miller765682c2018-10-22 15:27:27 -0500617 * Encryption and Decryption using transparent keys in block modes other than
618 * ECB must be done in multiple parts, using the following flow:
619 * - `pcd_cipher_transparent_setup_t`
620 * - `pcd_cipher_transparent_set_iv_t` (optional depending upon block mode)
621 * - `pcd_cipher_transparent_update_t`
622 * - ...
623 * - `pcd_cipher_transparent_finish_t`
624
625 * If a previously started Transparent Cipher operation needs to be terminated,
626 * it should be done so by the `psa_cipher_transparent_abort_t`. Failure to do
627 * so may result in allocated resources not being freed or in other undefined
628 * behavior.
Derek Miller5b3417a2018-10-10 17:55:03 -0500629 */
Derek Miller16e72292018-10-15 16:14:24 -0500630/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500631
632/** \brief The hardware-specific transparent-key Cipher context structure
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100633 *
Derek Miller765682c2018-10-22 15:27:27 -0500634 * The contents of this structure are implementation dependent and are
Derek Miller16e72292018-10-15 16:14:24 -0500635 * therefore not described here.
Derek Miller5b3417a2018-10-10 17:55:03 -0500636 */
Derek Miller81133a62018-10-23 14:55:32 -0500637typedef struct pcd_cipher_transparent_context_s pcd_cipher_transparent_context_t;
Derek Miller5b3417a2018-10-10 17:55:03 -0500638
Derek Miller16e72292018-10-15 16:14:24 -0500639/** \brief The function prototype for the setup operation of transparent-key
640 * block cipher operations.
641 * Functions that implement the prototype should be named in the following
642 * conventions:
Derek Miller5b3417a2018-10-10 17:55:03 -0500643 * ~~~~~~~~~~~~~{.c}
644 * pcd_cipher_transparent_setup_<CIPHER_NAME>_<MODE>
645 * ~~~~~~~~~~~~~
646 * Where
647 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
648 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Derek Miller16e72292018-10-15 16:14:24 -0500649 * or for stream ciphers:
650 * ~~~~~~~~~~~~~{.c}
651 * pcd_cipher_transparent_setup_<CIPHER_NAME>
652 * ~~~~~~~~~~~~~
653 * Where `CIPHER_NAME` is the name of a stream cipher (i.e. RC4)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100654 *
Derek Miller16e72292018-10-15 16:14:24 -0500655 * \param[in,out] p_context A structure that will contain the
656 * hardware-specific cipher context
657 * \param[in] direction Indicates if the operation is an encrypt or a
658 * decrypt
659 * \param[in] p_key_data A buffer containing the cleartext key material
660 * to be used in the operation
661 * \param[in] key_data_size The size in bytes of the key material
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100662 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500663 * \retval PSA_SUCCESS
664 */
Derek Miller81133a62018-10-23 14:55:32 -0500665typedef psa_status_t (*pcd_cipher_transparent_setup_t)(pcd_cipher_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500666 encrypt_or_decrypt_t direction,
667 const uint8_t *p_key_data,
668 size_t key_data_size);
Derek Miller5b3417a2018-10-10 17:55:03 -0500669
Derek Miller16e72292018-10-15 16:14:24 -0500670/** \brief The function prototype for the set initialization vector operation
671 * of transparent-key block cipher operations
672 * Functions that implement the prototype should be named in the following
673 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500674 * ~~~~~~~~~~~~~{.c}
675 * pcd_cipher_transparent_set_iv_<CIPHER_NAME>_<MODE>
676 * ~~~~~~~~~~~~~
677 * Where
678 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
679 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100680 *
Derek Miller16e72292018-10-15 16:14:24 -0500681 * \param[in,out] p_context A structure that contains the previously setup
682 * hardware-specific cipher context
683 * \param[in] p_iv A buffer containing the initialization vecotr
684 * \param[in] iv_length The size in bytes of the contents of `p_iv`
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100685 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500686 * \retval PSA_SUCCESS
Jaeden Amero0a09f772018-10-26 11:42:32 +0100687 */
Derek Miller81133a62018-10-23 14:55:32 -0500688typedef psa_status_t (*pcd_cipher_transparent_set_iv_t)(pcd_cipher_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500689 const uint8_t *p_iv,
690 size_t iv_length);
691
692/** \brief The function prototype for the update operation of transparent-key
693 * block cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500694 *
Derek Miller16e72292018-10-15 16:14:24 -0500695 * Functions that implement the prototype should be named in the following
696 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500697 * ~~~~~~~~~~~~~{.c}
698 * pcd_cipher_transparent_update_<CIPHER_NAME>_<MODE>
699 * ~~~~~~~~~~~~~
700 * Where
701 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
702 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100703 *
Derek Miller16e72292018-10-15 16:14:24 -0500704 * \param[in,out] p_context A hardware-specific structure for the
705 * previously started cipher operation
706 * \param[in] p_input A buffer containing the data to be
707 * encrypted or decrypted
708 * \param[in] input_size The size in bytes of the `p_input` buffer
709 * \param[out] p_output A caller-allocated buffer where the
710 * generated output will be placed
711 * \param[in] output_size The size in bytes of the `p_output` buffer
712 * \param[out] p_output_length After completion, will contain the number
713 * of bytes placed in the `p_output` buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100714 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500715 * \retval PSA_SUCCESS
716 */
Derek Miller81133a62018-10-23 14:55:32 -0500717typedef psa_status_t (*pcd_cipher_transparent_update_t)(pcd_cipher_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500718 const uint8_t *p_input,
719 size_t input_size,
720 uint8_t *p_output,
721 size_t output_size,
722 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500723
Derek Miller16e72292018-10-15 16:14:24 -0500724/** \brief The function prototype for the finish operation of transparent-key
725 * block cipher operations.
Jaeden Amero0a09f772018-10-26 11:42:32 +0100726 *
Derek Miller16e72292018-10-15 16:14:24 -0500727 * Functions that implement the prototype should be named in the following
728 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500729 * ~~~~~~~~~~~~~{.c}
730 * pcd_cipher_transparent_finish_<CIPHER_NAME>_<MODE>
731 * ~~~~~~~~~~~~~
732 * Where
733 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
734 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Derek Miller16e72292018-10-15 16:14:24 -0500735 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500736 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500737 * previously started cipher operation
738 * \param[out] p_output A caller-allocated buffer where the generated
739 * output will be placed
740 * \param[in] output_size The size in bytes of the `p_output` buffer
741 * \param[out] p_output_length After completion, will contain the number of
742 * bytes placed in the `p_output` buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100743 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500744 * \retval PSA_SUCCESS
745 */
Derek Miller81133a62018-10-23 14:55:32 -0500746typedef psa_status_t (*pcd_cipher_transparent_finish_t)(pcd_cipher_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500747 uint8_t *p_output,
748 size_t output_size,
749 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500750
Derek Miller16e72292018-10-15 16:14:24 -0500751/** \brief The function prototype for the abort operation of transparent-key
752 * block cipher operations.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100753 *
Derek Miller16e72292018-10-15 16:14:24 -0500754 * Functions that implement the following prototype should be named in the
755 * following convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500756 * ~~~~~~~~~~~~~{.c}
757 * pcd_cipher_transparent_abort_<CIPHER_NAME>_<MODE>
758 * ~~~~~~~~~~~~~
759 * Where
760 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
761 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100762 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500763 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500764 * previously started cipher operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100765 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500766 * \retval PSA_SUCCESS
767 */
Derek Miller81133a62018-10-23 14:55:32 -0500768typedef psa_status_t (*pcd_cipher_transparent_abort_t)(pcd_cipher_transparent_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -0500769
Derek Miller16e72292018-10-15 16:14:24 -0500770/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500771
Derek Miller16e72292018-10-15 16:14:24 -0500772/** \defgroup driver_digest Message Digests
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100773 *
Derek Miller765682c2018-10-22 15:27:27 -0500774 * Generation and authentication of Message Digests (aka hashes) must be done
775 * in parts using the following sequence:
776 * - `psa_hash_setup_t`
777 * - `psa_hash_update_t`
778 * - ...
779 * - `psa_hash_finish_t`
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100780 *
Derek Miller765682c2018-10-22 15:27:27 -0500781 * If a previously started Message Digest operation needs to be terminated
782 * before the `psa_hash_finish_t` operation is complete, it should be aborted
783 * by the `psa_hash_abort_t`. Failure to do so may result in allocated
784 * resources not being freed or in other undefined behavior.
Derek Miller5b3417a2018-10-10 17:55:03 -0500785 */
Derek Miller16e72292018-10-15 16:14:24 -0500786/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500787
788/** \brief The hardware-specific hash context structure
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100789 *
Derek Miller16e72292018-10-15 16:14:24 -0500790 * The contents of this structure are implementation dependent and are
791 * therefore not described here
Derek Miller5b3417a2018-10-10 17:55:03 -0500792 */
Derek Miller81133a62018-10-23 14:55:32 -0500793typedef struct pcd_hash_context_s pcd_hash_context_t;
Derek Miller5b3417a2018-10-10 17:55:03 -0500794
Derek Miller16e72292018-10-15 16:14:24 -0500795/** \brief The function prototype for the start operation of a hash (message
796 * digest) operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100797 *
Derek Miller16e72292018-10-15 16:14:24 -0500798 * Functions that implement the prototype should be named in the following
799 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500800 * ~~~~~~~~~~~~~{.c}
Derek Miller16e72292018-10-15 16:14:24 -0500801 * pcd_hash_<ALGO>_setup
Derek Miller5b3417a2018-10-10 17:55:03 -0500802 * ~~~~~~~~~~~~~
803 * Where `ALGO` is the name of the underlying hash function
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100804 *
Derek Miller16e72292018-10-15 16:14:24 -0500805 * \param[in,out] p_context A structure that will contain the
806 * hardware-specific hash context
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100807 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500808 * \retval PSA_SUCCESS Success.
809 */
Derek Miller81133a62018-10-23 14:55:32 -0500810typedef psa_status_t (*pcd_hash_setup_t)(pcd_hash_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -0500811
Derek Miller16e72292018-10-15 16:14:24 -0500812/** \brief The function prototype for the update operation of a hash (message
813 * digest) operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500814 *
Derek Miller16e72292018-10-15 16:14:24 -0500815 * Functions that implement the prototype should be named in the following
816 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500817 * ~~~~~~~~~~~~~{.c}
818 * pcd_hash_<ALGO>_update
819 * ~~~~~~~~~~~~~
820 * Where `ALGO` is the name of the underlying algorithm
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100821 *
Derek Miller16e72292018-10-15 16:14:24 -0500822 * \param[in,out] p_context A hardware-specific structure for the
823 * previously-established hash operation to be
824 * continued
825 * \param[in] p_input A buffer containing the message to be appended
826 * to the hash operation
827 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500828 */
Derek Miller81133a62018-10-23 14:55:32 -0500829typedef psa_status_t (*pcd_hash_update_t)(pcd_hash_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500830 const uint8_t *p_input,
831 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500832
Derek Miller16e72292018-10-15 16:14:24 -0500833/** \brief The prototype for the finish operation of a hash (message digest)
834 * operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100835 *
Derek Miller16e72292018-10-15 16:14:24 -0500836 * Functions that implement the prototype should be named in the following
837 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500838 * ~~~~~~~~~~~~~{.c}
839 * pcd_hash_<ALGO>_finish
840 * ~~~~~~~~~~~~~
841 * Where `ALGO` is the name of the underlying algorithm
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100842 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500843 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500844 * previously started hash operation to be
845 * fiinished
846 * \param[out] p_output A buffer where the generated digest will be
847 * placed
848 * \param[in] output_size The size in bytes of the buffer that has been
849 * allocated for the `p_output` buffer
Derek Millerf3d0a562018-10-18 16:41:08 -0500850 * \param[out] p_output_length The number of bytes placed in `p_output` after
851 * success
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100852 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500853 * \retval PSA_SUCCESS
854 * Success.
855 */
Derek Miller81133a62018-10-23 14:55:32 -0500856typedef psa_status_t (*pcd_hash_finish_t)(pcd_hash_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500857 uint8_t *p_output,
Derek Millerf3d0a562018-10-18 16:41:08 -0500858 size_t output_size,
859 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500860
Derek Miller16e72292018-10-15 16:14:24 -0500861/** \brief The function prototype for the abort operation of a hash (message
862 * digest) operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100863 *
Derek Miller16e72292018-10-15 16:14:24 -0500864 * Functions that implement the prototype should be named in the following
865 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500866 * ~~~~~~~~~~~~~{.c}
867 * pcd_hash_<ALGO>_abort
868 * ~~~~~~~~~~~~~
869 * Where `ALGO` is the name of the underlying algorithm
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100870 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500871 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500872 * started hash operation to be aborted
Derek Miller5b3417a2018-10-10 17:55:03 -0500873 */
Derek Miller81133a62018-10-23 14:55:32 -0500874typedef void (*pcd_hash_abort_t)(pcd_hash_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -0500875
Derek Miller16e72292018-10-15 16:14:24 -0500876/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500877
878
879/** \defgroup opaque_asymmetric Opaque Asymmetric Cryptography
Jaeden Amerod3d26aa2018-10-26 10:07:32 +0100880 *
Derek Miller765682c2018-10-22 15:27:27 -0500881 * Since the amount of data that can (or should) be encrypted or signed using
882 * asymmetric keys is limited by the key size, asymmetric key operations using
883 * opaque keys must be done in single function calls.
Derek Miller5b3417a2018-10-10 17:55:03 -0500884 */
Derek Miller16e72292018-10-15 16:14:24 -0500885/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500886
887/**
Derek Miller16e72292018-10-15 16:14:24 -0500888 * \brief A function that signs a hash or short message with a private key
Derek Miller5b3417a2018-10-10 17:55:03 -0500889 *
Derek Miller16e72292018-10-15 16:14:24 -0500890 * \param[in] key_slot Key slot of an asymmetric key pair
891 * \param[in] alg A signature algorithm that is compatible
892 * with the type of `key`
Derek Miller765682c2018-10-22 15:27:27 -0500893 * \param[in] p_hash The hash to sign
Derek Miller16e72292018-10-15 16:14:24 -0500894 * \param[in] hash_length Size of the `p_hash` buffer in bytes
895 * \param[out] p_signature Buffer where the signature is to be written
Derek Millerf3d0a562018-10-18 16:41:08 -0500896 * \param[in] signature_size Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500897 * \param[out] p_signature_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -0500898 * that make up the returned signature value
Derek Miller5b3417a2018-10-10 17:55:03 -0500899 *
900 * \retval PSA_SUCCESS
901 */
Derek Miller16e72292018-10-15 16:14:24 -0500902typedef psa_status_t (*pcd_asymmetric_opaque_sign_t)(psa_key_slot_t key_slot,
903 psa_algorithm_t alg,
904 const uint8_t *p_hash,
905 size_t hash_length,
906 uint8_t *p_signature,
907 size_t signature_size,
908 size_t *p_signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500909
910/**
Derek Miller16e72292018-10-15 16:14:24 -0500911 * \brief A function that verifies the signature a hash or short message using
Derek Miller765682c2018-10-22 15:27:27 -0500912 * an asymmetric public key
Derek Miller5b3417a2018-10-10 17:55:03 -0500913 *
Derek Miller16e72292018-10-15 16:14:24 -0500914 * \param[in] key_slot Key slot of a public key or an asymmetric key
915 * pair
916 * \param[in] alg A signature algorithm that is compatible with
917 * the type of `key`
Derek Miller765682c2018-10-22 15:27:27 -0500918 * \param[in] p_hash The hash whose signature is to be verified
Derek Miller16e72292018-10-15 16:14:24 -0500919 * \param[in] hash_length Size of the `p_hash` buffer in bytes
920 * \param[in] p_signature Buffer containing the signature to verify
921 * \param[in] signature_length Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500922 *
923 * \retval PSA_SUCCESS
924 * The signature is valid.
925 */
Derek Miller16e72292018-10-15 16:14:24 -0500926typedef psa_status_t (*pcd_asymmetric_opaque_verify_t)(psa_key_slot_t key_slot,
927 psa_algorithm_t alg,
928 const uint8_t *p_hash,
929 size_t hash_length,
930 const uint8_t *p_signature,
931 size_t signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500932
933/**
Derek Miller765682c2018-10-22 15:27:27 -0500934 * \brief A function that encrypts a short message with an asymmetric public
935 * key
Derek Miller5b3417a2018-10-10 17:55:03 -0500936 *
Derek Miller16e72292018-10-15 16:14:24 -0500937 * \param[in] key_slot Key slot of a public key or an asymmetric key
938 * pair
939 * \param[in] alg An asymmetric encryption algorithm that is
940 * compatible with the type of `key`
941 * \param[in] p_input The message to encrypt
942 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500943 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -0500944 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -0500945 * If the algorithm does not support a
946 * salt, pass `NULL`.
947 * If the algorithm supports an optional
948 * salt and you do not want to pass a salt,
949 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -0500950 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
951 * supported.
952 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500953 * If `p_salt` is `NULL`, pass 0.
954 * \param[out] p_output Buffer where the encrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -0500955 * be written
956 * \param[in] output_size Size of the `p_output` buffer in bytes
957 * \param[out] p_output_length On success, the number of bytes that make up
958 * the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -0500959 *
960 * \retval PSA_SUCCESS
961 */
Derek Miller16e72292018-10-15 16:14:24 -0500962typedef psa_status_t (*pcd_asymmetric_opaque_encrypt_t)(psa_key_slot_t key_slot,
963 psa_algorithm_t alg,
964 const uint8_t *p_input,
965 size_t input_length,
966 const uint8_t *p_salt,
967 size_t salt_length,
968 uint8_t *p_output,
969 size_t output_size,
970 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500971
972/**
Derek Miller765682c2018-10-22 15:27:27 -0500973 * \brief Decrypt a short message with an asymmetric private key.
Derek Miller5b3417a2018-10-10 17:55:03 -0500974 *
Derek Miller16e72292018-10-15 16:14:24 -0500975 * \param[in] key_slot Key slot of an asymmetric key pair
976 * \param[in] alg An asymmetric encryption algorithm that is
977 * compatible with the type of `key`
978 * \param[in] p_input The message to decrypt
979 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500980 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -0500981 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -0500982 * If the algorithm does not support a
983 * salt, pass `NULL`.
984 * If the algorithm supports an optional
985 * salt and you do not want to pass a salt,
986 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -0500987 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
988 * supported.
989 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500990 * If `p_salt` is `NULL`, pass 0.
991 * \param[out] p_output Buffer where the decrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -0500992 * be written
993 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500994 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -0500995 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -0500996 *
997 * \retval PSA_SUCCESS
998 */
Derek Miller16e72292018-10-15 16:14:24 -0500999typedef psa_status_t (*pcd_asymmetric_opaque_decrypt_t)(psa_key_slot_t key_slot,
1000 psa_algorithm_t alg,
1001 const uint8_t *p_input,
1002 size_t input_length,
1003 const uint8_t *p_salt,
1004 size_t salt_length,
1005 uint8_t *p_output,
1006 size_t output_size,
1007 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001008
1009/**
Derek Miller16e72292018-10-15 16:14:24 -05001010 * \brief A struct containing all of the function pointers needed to implement
1011 * asymmetric cryptographic operations using opaque keys.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001012 *
Derek Miller16e72292018-10-15 16:14:24 -05001013 * PSA Crypto API implementations should populate instances of the table as
1014 * appropriate upon startup.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001015 *
Derek Miller5b3417a2018-10-10 17:55:03 -05001016 * If one of the functions is not implemented, it should be set to NULL.
1017 */
1018struct pcd_asymmetric_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -05001019 /** Function that performs the asymmetric sign operation */
1020 pcd_asymmetric_opaque_sign_t *p_sign;
1021 /** Function that performs the asymmetric verify operation */
1022 pcd_asymmetric_opaque_verify_t *p_verify;
1023 /** Function that performs the asymmetric encrypt operation */
1024 pcd_asymmetric_opaque_encrypt_t *p_encrypt;
1025 /** Function that performs the asymmetric decrypt operation */
1026 pcd_asymmetric_opaque_decrypt_t *p_decrypt;
Derek Miller5b3417a2018-10-10 17:55:03 -05001027};
1028
Derek Miller16e72292018-10-15 16:14:24 -05001029/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001030
1031/** \defgroup transparent_asymmetric Transparent Asymmetric Cryptography
Derek Miller765682c2018-10-22 15:27:27 -05001032 *
1033 * Since the amount of data that can (or should) be encrypted or signed using
1034 * asymmetric keys is limited by the key size, asymmetric key operations using
1035 * transparent keys must be done in single function calls.
Derek Miller5b3417a2018-10-10 17:55:03 -05001036 */
Derek Miller16e72292018-10-15 16:14:24 -05001037/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001038
1039
1040/**
Derek Miller16e72292018-10-15 16:14:24 -05001041 * \brief A function that signs a hash or short message with a transparent
Derek Miller765682c2018-10-22 15:27:27 -05001042 * asymmetric private key
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001043 *
Derek Miller16e72292018-10-15 16:14:24 -05001044 * Functions that implement the prototype should be named in the following
1045 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001046 * ~~~~~~~~~~~~~{.c}
1047 * pcd_asymmetric_<ALGO>_sign
1048 * ~~~~~~~~~~~~~
1049 * Where `ALGO` is the name of the signing algorithm
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001050 *
Derek Miller16e72292018-10-15 16:14:24 -05001051 * \param[in] p_key A buffer containing the private key
1052 * material
1053 * \param[in] key_size The size in bytes of the `p_key` data
1054 * \param[in] alg A signature algorithm that is compatible
1055 * with the type of `p_key`
1056 * \param[in] p_hash The hash or message to sign
1057 * \param[in] hash_length Size of the `p_hash` buffer in bytes
1058 * \param[out] p_signature Buffer where the signature is to be written
1059 * \param[in] signature_size Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001060 * \param[out] p_signature_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001061 * that make up the returned signature value
Derek Miller5b3417a2018-10-10 17:55:03 -05001062 *
1063 * \retval PSA_SUCCESS
1064 */
Derek Miller16e72292018-10-15 16:14:24 -05001065typedef psa_status_t (*pcd_asymmetric_transparent_sign_t)(const uint8_t *p_key,
1066 size_t key_size,
1067 psa_algorithm_t alg,
1068 const uint8_t *p_hash,
1069 size_t hash_length,
1070 uint8_t *p_signature,
1071 size_t signature_size,
1072 size_t *p_signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001073
1074/**
Derek Miller16e72292018-10-15 16:14:24 -05001075 * \brief A function that verifies the signature a hash or short message using
Derek Miller765682c2018-10-22 15:27:27 -05001076 * a transparent asymmetric public key
Derek Miller5b3417a2018-10-10 17:55:03 -05001077 *
Derek Miller16e72292018-10-15 16:14:24 -05001078 * Functions that implement the prototype should be named in the following
1079 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001080 * ~~~~~~~~~~~~~{.c}
1081 * pcd_asymmetric_<ALGO>_verify
1082 * ~~~~~~~~~~~~~
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001083 * Where `ALGO` is the name of the signing algorithm
1084 *
Derek Miller16e72292018-10-15 16:14:24 -05001085 * \param[in] p_key A buffer containing the public key material
1086 * \param[in] key_size The size in bytes of the `p_key` data
1087 * \param[in] alg A signature algorithm that is compatible with
1088 * the type of `key`
1089 * \param[in] p_hash The hash or message whose signature is to be
1090 * verified
1091 * \param[in] hash_length Size of the `p_hash` buffer in bytes
1092 * \param[in] p_signature Buffer containing the signature to verify
1093 * \param[in] signature_length Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001094 *
1095 * \retval PSA_SUCCESS
1096 * The signature is valid.
1097 */
Derek Miller16e72292018-10-15 16:14:24 -05001098typedef psa_status_t (*pcd_asymmetric_transparent_verify_t)(const uint8_t *p_key,
1099 size_t key_size,
1100 psa_algorithm_t alg,
1101 const uint8_t *p_hash,
1102 size_t hash_length,
1103 const uint8_t *p_signature,
1104 size_t signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001105
1106/**
Derek Miller765682c2018-10-22 15:27:27 -05001107 * \brief A function that encrypts a short message with a transparent
1108 * asymmetric public key
Derek Miller5b3417a2018-10-10 17:55:03 -05001109 *
Derek Miller16e72292018-10-15 16:14:24 -05001110 * Functions that implement the prototype should be named in the following
1111 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001112 * ~~~~~~~~~~~~~{.c}
1113 * pcd_asymmetric_<ALGO>_encrypt
1114 * ~~~~~~~~~~~~~
1115 * Where `ALGO` is the name of the encryption algorithm
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001116 *
Derek Miller16e72292018-10-15 16:14:24 -05001117 * \param[in] p_key A buffer containing the public key material
1118 * \param[in] key_size The size in bytes of the `p_key` data
1119 * \param[in] alg An asymmetric encryption algorithm that is
1120 * compatible with the type of `key`
1121 * \param[in] p_input The message to encrypt
1122 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001123 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -05001124 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -05001125 * If the algorithm does not support a
Derek Miller16e72292018-10-15 16:14:24 -05001126 * salt, pass `NULL`
Derek Miller5b3417a2018-10-10 17:55:03 -05001127 * If the algorithm supports an optional
1128 * salt and you do not want to pass a salt,
1129 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -05001130 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1131 * supported.
1132 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001133 * If `p_salt` is `NULL`, pass 0.
1134 * \param[out] p_output Buffer where the encrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -05001135 * be written
1136 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001137 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001138 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -05001139 *
1140 * \retval PSA_SUCCESS
1141 */
Derek Miller16e72292018-10-15 16:14:24 -05001142typedef psa_status_t (*pcd_asymmetric_transparent_encrypt_t)(const uint8_t *p_key,
Derek Miller5b3417a2018-10-10 17:55:03 -05001143 size_t key_size,
1144 psa_algorithm_t alg,
1145 const uint8_t *p_input,
1146 size_t input_length,
1147 const uint8_t *p_salt,
1148 size_t salt_length,
1149 uint8_t *p_output,
1150 size_t output_size,
Derek Miller16e72292018-10-15 16:14:24 -05001151 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001152
1153/**
Derek Miller765682c2018-10-22 15:27:27 -05001154 * \brief Decrypt a short message with a transparent asymmetric private key
Derek Miller5b3417a2018-10-10 17:55:03 -05001155 *
Derek Miller16e72292018-10-15 16:14:24 -05001156 * Functions that implement the prototype should be named in the following
1157 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001158 * ~~~~~~~~~~~~~{.c}
1159 * pcd_asymmetric_<ALGO>_decrypt
1160 * ~~~~~~~~~~~~~
1161 * Where `ALGO` is the name of the encryption algorithm
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001162 *
Derek Miller16e72292018-10-15 16:14:24 -05001163 * \param[in] p_key A buffer containing the private key material
1164 * \param[in] key_size The size in bytes of the `p_key` data
1165 * \param[in] alg An asymmetric encryption algorithm that is
1166 * compatible with the type of `key`
1167 * \param[in] p_input The message to decrypt
1168 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001169 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -05001170 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -05001171 * If the algorithm does not support a
1172 * salt, pass `NULL`.
1173 * If the algorithm supports an optional
1174 * salt and you do not want to pass a salt,
1175 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -05001176 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1177 * supported
1178 * \param[in] salt_length Size of the `p_salt` buffer in bytes
1179 * If `p_salt` is `NULL`, pass 0
Derek Miller5b3417a2018-10-10 17:55:03 -05001180 * \param[out] p_output Buffer where the decrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -05001181 * be written
1182 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001183 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001184 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -05001185 *
1186 * \retval PSA_SUCCESS
1187 */
Derek Miller16e72292018-10-15 16:14:24 -05001188typedef psa_status_t (*pcd_asymmetric_transparent_decrypt_t)(const uint8_t *p_key,
1189 size_t key_size,
1190 psa_algorithm_t alg,
1191 const uint8_t *p_input,
1192 size_t input_length,
1193 const uint8_t *p_salt,
1194 size_t salt_length,
1195 uint8_t *p_output,
1196 size_t output_size,
1197 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001198
Derek Miller16e72292018-10-15 16:14:24 -05001199/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001200
1201/** \defgroup aead_opaque AEAD Opaque
Derek Miller765682c2018-10-22 15:27:27 -05001202 * Authenticated Encryption with Additional Data (AEAD) operations with opaque
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001203 * keys must be done in one function call. While this creates a burden for
Derek Miller765682c2018-10-22 15:27:27 -05001204 * implementers as there must be sufficient space in memory for the entire
1205 * message, it prevents decrypted data from being made available before the
1206 * authentication operation is complete and the data is known to be authentic.
Derek Miller5b3417a2018-10-10 17:55:03 -05001207 */
Derek Miller16e72292018-10-15 16:14:24 -05001208/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001209
Derek Miller16e72292018-10-15 16:14:24 -05001210/** \brief Process an authenticated encryption operation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -05001211 *
Derek Miller16e72292018-10-15 16:14:24 -05001212 * \param[in] key_slot Slot containing the key to use.
1213 * \param[in] algorithm The AEAD algorithm to compute
1214 * (\c PSA_ALG_XXX value such that
1215 * #PSA_ALG_IS_AEAD(`alg`) is true)
1216 * \param[in] p_nonce Nonce or IV to use
1217 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
1218 * \param[in] p_additional_data Additional data that will be
1219 * authenticated but not encrypted
1220 * \param[in] additional_data_length Size of `p_additional_data` in bytes
1221 * \param[in] p_plaintext Data that will be authenticated and
1222 * encrypted
1223 * \param[in] plaintext_length Size of `p_plaintext` in bytes
1224 * \param[out] p_ciphertext Output buffer for the authenticated and
1225 * encrypted data. The additional data is
1226 * not part of this output. For algorithms
1227 * where the encrypted data and the
1228 * authentication tag are defined as
1229 * separate outputs, the authentication
1230 * tag is appended to the encrypted data.
1231 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
1232 * bytes
1233 * \param[out] p_ciphertext_length On success, the size of the output in
1234 * the `p_ciphertext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001235 *
1236 * \retval #PSA_SUCCESS
1237 * Success.
1238 */
Derek Miller16e72292018-10-15 16:14:24 -05001239typedef psa_status_t (*psa_aead_opaque_encrypt_t)(psa_key_slot_t key_slot,
Derek Miller5b3417a2018-10-10 17:55:03 -05001240 psa_algorithm_t algorithm,
1241 const uint8_t *p_nonce,
1242 size_t nonce_length,
1243 const uint8_t *p_additional_data,
1244 size_t additional_data_length,
1245 const uint8_t *p_plaintext,
1246 size_t plaintext_length,
1247 uint8_t *p_ciphertext,
1248 size_t ciphertext_size,
1249 size_t *p_ciphertext_length);
1250
Derek Miller16e72292018-10-15 16:14:24 -05001251/** Process an authenticated decryption operation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -05001252 *
Derek Miller16e72292018-10-15 16:14:24 -05001253 * \param[in] key_slot Slot containing the key to use
1254 * \param[in] algorithm The AEAD algorithm to compute
1255 * (\c PSA_ALG_XXX value such that
1256 * #PSA_ALG_IS_AEAD(`alg`) is true)
1257 * \param[in] p_nonce Nonce or IV to use
1258 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
1259 * \param[in] p_additional_data Additional data that has been
1260 * authenticated but not encrypted
1261 * \param[in] additional_data_length Size of `p_additional_data` in bytes
1262 * \param[in] p_ciphertext Data that has been authenticated and
1263 * encrypted.
1264 * For algorithms where the encrypted data
1265 * and the authentication tag are defined
1266 * as separate inputs, the buffer must
1267 * contain the encrypted data followed by
1268 * the authentication tag.
1269 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
1270 * \param[out] p_plaintext Output buffer for the decrypted data
1271 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
1272 * bytes
1273 * \param[out] p_plaintext_length On success, the size of the output in
1274 * the `p_plaintext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001275 *
1276 * \retval #PSA_SUCCESS
1277 * Success.
1278 */
Derek Miller16e72292018-10-15 16:14:24 -05001279typedef psa_status_t (*psa_aead_opaque_decrypt_t)(psa_key_slot_t key_slot,
1280 psa_algorithm_t algorithm,
1281 const uint8_t *p_nonce,
1282 size_t nonce_length,
1283 const uint8_t *p_additional_data,
1284 size_t additional_data_length,
1285 const uint8_t *p_ciphertext,
1286 size_t ciphertext_length,
1287 uint8_t *p_plaintext,
1288 size_t plaintext_size,
1289 size_t *p_plaintext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001290
1291/**
Derek Miller16e72292018-10-15 16:14:24 -05001292 * \brief A struct containing all of the function pointers needed to implement
1293 * Authenticated Encryption with Additional Data operations using opaque keys
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001294 *
Derek Miller16e72292018-10-15 16:14:24 -05001295 * PSA Crypto API implementations should populate instances of the table as
1296 * appropriate upon startup.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001297 *
Derek Miller5b3417a2018-10-10 17:55:03 -05001298 * If one of the functions is not implemented, it should be set to NULL.
1299 */
1300struct psa_aead_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -05001301 /** Function that performs the AEAD encrypt operation */
1302 psa_aead_opaque_encrypt_t *p_encrypt;
1303 /** Function that performs the AEAD decrypt operation */
1304 psa_aead_opaque_decrypt_t *p_decrypt;
Derek Miller5b3417a2018-10-10 17:55:03 -05001305};
Derek Miller16e72292018-10-15 16:14:24 -05001306/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001307
1308/** \defgroup aead_transparent AEAD Transparent
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001309 *
Derek Miller765682c2018-10-22 15:27:27 -05001310 * Authenticated Encryption with Additional Data (AEAD) operations with
1311 * transparent keys must be done in one function call. While this creates a
1312 * burden for implementers as there must be sufficient space in memory for the
1313 * entire message, it prevents decrypted data from being made available before
1314 * the authentication operation is complete and the data is known to be
1315 * authentic.
Derek Miller5b3417a2018-10-10 17:55:03 -05001316 */
Derek Miller16e72292018-10-15 16:14:24 -05001317/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001318
Derek Miller765682c2018-10-22 15:27:27 -05001319/** Process an authenticated encryption operation using an opaque key.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001320 *
Derek Miller16e72292018-10-15 16:14:24 -05001321 * Functions that implement the prototype should be named in the following
1322 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001323 * ~~~~~~~~~~~~~{.c}
1324 * pcd_aead_<ALGO>_encrypt
1325 * ~~~~~~~~~~~~~
1326 * Where `ALGO` is the name of the AEAD algorithm
1327 *
Derek Miller16e72292018-10-15 16:14:24 -05001328 * \param[in] p_key A pointer to the key material
1329 * \param[in] key_length The size in bytes of the key material
1330 * \param[in] alg The AEAD algorithm to compute
1331 * (\c PSA_ALG_XXX value such that
1332 * #PSA_ALG_IS_AEAD(`alg`) is true)
1333 * \param[in] nonce Nonce or IV to use
1334 * \param[in] nonce_length Size of the `nonce` buffer in bytes
1335 * \param[in] additional_data Additional data that will be MACed
1336 * but not encrypted.
1337 * \param[in] additional_data_length Size of `additional_data` in bytes
1338 * \param[in] plaintext Data that will be MACed and
1339 * encrypted.
1340 * \param[in] plaintext_length Size of `plaintext` in bytes
1341 * \param[out] ciphertext Output buffer for the authenticated and
1342 * encrypted data. The additional data is
1343 * not part of this output. For algorithms
1344 * where the encrypted data and the
1345 * authentication tag are defined as
1346 * separate outputs, the authentication
1347 * tag is appended to the encrypted data.
1348 * \param[in] ciphertext_size Size of the `ciphertext` buffer in
1349 * bytes
1350 * This must be at least
1351 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(`alg`,
1352 * `plaintext_length`).
1353 * \param[out] ciphertext_length On success, the size of the output in
1354 * the `ciphertext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001355 *
1356 * \retval #PSA_SUCCESS
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001357
Derek Miller5b3417a2018-10-10 17:55:03 -05001358 */
Derek Miller16e72292018-10-15 16:14:24 -05001359typedef psa_status_t (*psa_aead_transparent_encrypt_t)(const uint8_t *p_key,
1360 size_t key_length,
1361 psa_algorithm_t alg,
1362 const uint8_t *nonce,
1363 size_t nonce_length,
1364 const uint8_t *additional_data,
1365 size_t additional_data_length,
1366 const uint8_t *plaintext,
1367 size_t plaintext_length,
1368 uint8_t *ciphertext,
1369 size_t ciphertext_size,
1370 size_t *ciphertext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001371
Derek Miller765682c2018-10-22 15:27:27 -05001372/** Process an authenticated decryption operation using an opaque key.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001373 *
Derek Miller16e72292018-10-15 16:14:24 -05001374 * Functions that implement the prototype should be named in the following
1375 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001376 * ~~~~~~~~~~~~~{.c}
1377 * pcd_aead_<ALGO>_decrypt
1378 * ~~~~~~~~~~~~~
1379 * Where `ALGO` is the name of the AEAD algorithm
Derek Miller16e72292018-10-15 16:14:24 -05001380 * \param[in] p_key A pointer to the key material
1381 * \param[in] key_length The size in bytes of the key material
1382 * \param[in] alg The AEAD algorithm to compute
1383 * (\c PSA_ALG_XXX value such that
1384 * #PSA_ALG_IS_AEAD(`alg`) is true)
1385 * \param[in] nonce Nonce or IV to use
1386 * \param[in] nonce_length Size of the `nonce` buffer in bytes
1387 * \param[in] additional_data Additional data that has been MACed
1388 * but not encrypted
1389 * \param[in] additional_data_length Size of `additional_data` in bytes
1390 * \param[in] ciphertext Data that has been MACed and
1391 * encrypted
1392 * For algorithms where the encrypted data
1393 * and the authentication tag are defined
1394 * as separate inputs, the buffer must
1395 * contain the encrypted data followed by
1396 * the authentication tag.
1397 * \param[in] ciphertext_length Size of `ciphertext` in bytes
1398 * \param[out] plaintext Output buffer for the decrypted data
1399 * \param[in] plaintext_size Size of the `plaintext` buffer in
1400 * bytes
1401 * This must be at least
1402 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(`alg`,
1403 * `ciphertext_length`).
1404 * \param[out] plaintext_length On success, the size of the output
1405 * in the \b plaintext buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001406 *
1407 * \retval #PSA_SUCCESS
1408 * Success.
1409 */
Derek Miller16e72292018-10-15 16:14:24 -05001410typedef psa_status_t (*psa_aead_transparent_decrypt_t)(const uint8_t *p_key,
1411 size_t key_length,
1412 psa_algorithm_t alg,
1413 const uint8_t *nonce,
1414 size_t nonce_length,
1415 const uint8_t *additional_data,
1416 size_t additional_data_length,
1417 const uint8_t *ciphertext,
1418 size_t ciphertext_length,
1419 uint8_t *plaintext,
1420 size_t plaintext_size,
1421 size_t *plaintext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001422
Derek Miller16e72292018-10-15 16:14:24 -05001423/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001424
1425
Derek Miller16e72292018-10-15 16:14:24 -05001426/** \defgroup driver_rng Entropy Generation
Derek Miller5b3417a2018-10-10 17:55:03 -05001427 */
Derek Miller16e72292018-10-15 16:14:24 -05001428/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001429
1430/** \brief A hardware-specific structure for a entropy providing hardware
1431 */
Derek Miller81133a62018-10-23 14:55:32 -05001432typedef struct pcd_entropy_context_s pcd_entropy_context_t;
Derek Miller5b3417a2018-10-10 17:55:03 -05001433
1434/** \brief Initialize an entropy driver
1435 *
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001436 *
Derek Miller16e72292018-10-15 16:14:24 -05001437 * \param[in,out] p_context A hardware-specific structure
1438 * containing any context information for
1439 * the implementation
Derek Miller5b3417a2018-10-10 17:55:03 -05001440 *
1441 * \retval PSA_SUCCESS
1442 */
Derek Miller81133a62018-10-23 14:55:32 -05001443typedef psa_status_t (*pcd_entropy_init_t)(pcd_entropy_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -05001444
Derek Miller6f960ab2018-10-23 15:58:06 -05001445/** \brief Get a specified number of bits from the entropy source
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001446 *
Derek Miller16e72292018-10-15 16:14:24 -05001447 * It retrives `buffer_size` bytes of data from the entropy source. The entropy
1448 * source will always fill the provided buffer to its full size, however, most
1449 * entropy sources have biases, and the actual amount of entropy contained in
1450 * the buffer will be less than the number of bytes.
1451 * The driver will return the actual number of bytes of entropy placed in the
1452 * buffer in `p_received_entropy_bytes`.
1453 * A PSA Crypto API implementation will likely feed the output of this function
1454 * into a Digital Random Bit Generator (DRBG), and typically has a minimum
1455 * amount of entropy that it needs.
1456 * To accomplish this, the PSA Crypto implementation should be designed to call
1457 * this function multiple times until it has received the required amount of
1458 * entropy from the entropy source.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001459 *
Derek Miller16e72292018-10-15 16:14:24 -05001460 * \param[in,out] p_context A hardware-specific structure
1461 * containing any context information
1462 * for the implementation
1463 * \param[out] p_buffer A caller-allocated buffer for the
Derek Miller6f960ab2018-10-23 15:58:06 -05001464 * retrieved entropy to be placed in
Derek Miller16e72292018-10-15 16:14:24 -05001465 * \param[in] buffer_size The allocated size of `p_buffer`
Derek Miller6f960ab2018-10-23 15:58:06 -05001466 * \param[out] p_received_entropy_bits The amount of entropy (in bits)
Derek Miller16e72292018-10-15 16:14:24 -05001467 * actually provided in `p_buffer`
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001468 *
Derek Miller5b3417a2018-10-10 17:55:03 -05001469 * \retval PSA_SUCCESS
1470 */
Derek Miller6f960ab2018-10-23 15:58:06 -05001471typedef psa_status_t (*pcd_entropy_get_bits_t)(pcd_entropy_context_t *p_context,
1472 uint8_t *p_buffer,
1473 uint32_t buffer_size,
1474 uint32_t *p_received_entropy_bits);
Derek Miller5b3417a2018-10-10 17:55:03 -05001475
1476/**
Derek Miller16e72292018-10-15 16:14:24 -05001477 * \brief A struct containing all of the function pointers needed to interface
1478 * to an entropy source
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001479 *
Derek Miller16e72292018-10-15 16:14:24 -05001480 * PSA Crypto API implementations should populate instances of the table as
1481 * appropriate upon startup.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001482 *
Derek Miller5b3417a2018-10-10 17:55:03 -05001483 * If one of the functions is not implemented, it should be set to NULL.
1484 */
1485struct pcd_entropy_t {
Derek Miller16e72292018-10-15 16:14:24 -05001486 /** Function that performs initialization for the entropy source */
1487 pcd_entropy_init_t *p_init;
Derek Miller6f960ab2018-10-23 15:58:06 -05001488 /** Function that performs the get_bits operation for the entropy source
Derek Miller16e72292018-10-15 16:14:24 -05001489 */
Derek Miller6f960ab2018-10-23 15:58:06 -05001490 pcd_entropy_get_bits_t *p_get_bits;
Derek Miller5b3417a2018-10-10 17:55:03 -05001491};
Derek Miller16e72292018-10-15 16:14:24 -05001492/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001493
Derek Miller16e72292018-10-15 16:14:24 -05001494/** \defgroup driver_key_management Key Management
Derek Miller765682c2018-10-22 15:27:27 -05001495 * Currently, key management is limited to importing keys in the clear,
1496 * destroying keys, and exporting keys in the clear.
1497 * Whether a key may be exported is determined by the key policies in place
1498 * on the key slot.
Derek Miller5b3417a2018-10-10 17:55:03 -05001499 */
Derek Miller16e72292018-10-15 16:14:24 -05001500/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001501
Derek Miller16e72292018-10-15 16:14:24 -05001502/** \brief Import a key in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001503 *
1504 * This function can support any output from psa_export_key(). Refer to the
1505 * documentation of psa_export_key() for the format for each key type.
1506 *
Derek Miller81133a62018-10-23 14:55:32 -05001507 * \param[in] key_slot Slot where the key will be stored
1508 * This must be a valid slot for a key of the chosen
1509 * type. It must be unoccupied.
1510 * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value)
1511 * \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value)
1512 * \param[in] usage The allowed uses of the key
1513 * \param[in] p_data Buffer containing the key data
1514 * \param[in] data_length Size of the `data` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001515 *
1516 * \retval #PSA_SUCCESS
1517 * Success.
1518 */
Derek Miller16e72292018-10-15 16:14:24 -05001519typedef psa_status_t (*pcd_opaque_import_key_t)(psa_key_slot_t key_slot,
1520 psa_key_type_t type,
Derek Miller81133a62018-10-23 14:55:32 -05001521 psa_algorithm_t algorithm,
1522 psa_key_usage_t usage,
Derek Miller16e72292018-10-15 16:14:24 -05001523 const uint8_t *p_data,
1524 size_t data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001525
1526/**
Derek Miller16e72292018-10-15 16:14:24 -05001527 * \brief Destroy a key and restore the slot to its default state
Derek Miller5b3417a2018-10-10 17:55:03 -05001528 *
1529 * This function destroys the content of the key slot from both volatile
1530 * memory and, if applicable, non-volatile storage. Implementations shall
1531 * make a best effort to ensure that any previous content of the slot is
1532 * unrecoverable.
1533 *
1534 * This function also erases any metadata such as policies. It returns the
1535 * specified slot to its default state.
1536 *
Derek Miller16e72292018-10-15 16:14:24 -05001537 * \param[in] key_slot The key slot to erase.
Derek Miller5b3417a2018-10-10 17:55:03 -05001538 *
1539 * \retval #PSA_SUCCESS
1540 * The slot's content, if any, has been erased.
1541 */
Derek Miller16e72292018-10-15 16:14:24 -05001542typedef psa_status_t (*pcd_destroy_key_t)(psa_key_slot_t key);
Derek Miller5b3417a2018-10-10 17:55:03 -05001543
1544/**
Derek Miller16e72292018-10-15 16:14:24 -05001545 * \brief Export a key in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001546 *
1547 * The output of this function can be passed to psa_import_key() to
1548 * create an equivalent object.
1549 *
Derek Miller16e72292018-10-15 16:14:24 -05001550 * If a key is created with `psa_import_key()` and then exported with
Derek Miller5b3417a2018-10-10 17:55:03 -05001551 * this function, it is not guaranteed that the resulting data is
1552 * identical: the implementation may choose a different representation
1553 * of the same key if the format permits it.
1554 *
1555 * For standard key types, the output format is as follows:
1556 *
1557 * - For symmetric keys (including MAC keys), the format is the
1558 * raw bytes of the key.
1559 * - For DES, the key data consists of 8 bytes. The parity bits must be
1560 * correct.
1561 * - For Triple-DES, the format is the concatenation of the
1562 * two or three DES keys.
1563 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
1564 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1565 * as RSAPrivateKey.
1566 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
1567 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
1568 *
Derek Miller16e72292018-10-15 16:14:24 -05001569 * \param[in] key Slot whose content is to be exported. This must
Derek Miller5b3417a2018-10-10 17:55:03 -05001570 * be an occupied key slot.
1571 * \param[out] p_data Buffer where the key data is to be written.
Derek Miller16e72292018-10-15 16:14:24 -05001572 * \param[in] data_size Size of the `p_data` buffer in bytes.
Derek Miller5b3417a2018-10-10 17:55:03 -05001573 * \param[out] p_data_length On success, the number of bytes
1574 * that make up the key data.
1575 *
1576 * \retval #PSA_SUCCESS
1577 * \retval #PSA_ERROR_EMPTY_SLOT
1578 * \retval #PSA_ERROR_NOT_PERMITTED
1579 * \retval #PSA_ERROR_NOT_SUPPORTED
1580 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1581 * \retval #PSA_ERROR_HARDWARE_FAILURE
1582 * \retval #PSA_ERROR_TAMPERING_DETECTED
1583 */
Derek Miller16e72292018-10-15 16:14:24 -05001584typedef psa_status_t (*pcd_export_key_t)(psa_key_slot_t key,
1585 uint8_t *p_data,
1586 size_t data_size,
1587 size_t *p_data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001588
1589/**
Derek Miller16e72292018-10-15 16:14:24 -05001590 * \brief Export a public key or the public part of a key pair in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001591 *
1592 * The output of this function can be passed to psa_import_key() to
1593 * create an object that is equivalent to the public key.
1594 *
1595 * For standard key types, the output format is as follows:
1596 *
1597 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1598 * the format is the DER representation of the public key defined by RFC 5280
1599 * as SubjectPublicKeyInfo.
1600 *
Derek Miller16e72292018-10-15 16:14:24 -05001601 * \param[in] key_slot Slot whose content is to be exported. This must
Derek Miller5b3417a2018-10-10 17:55:03 -05001602 * be an occupied key slot.
1603 * \param[out] p_data Buffer where the key data is to be written.
Derek Miller16e72292018-10-15 16:14:24 -05001604 * \param[in] data_size Size of the `data` buffer in bytes.
Derek Miller5b3417a2018-10-10 17:55:03 -05001605 * \param[out] p_data_length On success, the number of bytes
1606 * that make up the key data.
1607 *
1608 * \retval #PSA_SUCCESS
1609 */
Derek Miller16e72292018-10-15 16:14:24 -05001610typedef psa_status_t (*pcd_export_public_key_t)(psa_key_slot_t key,
1611 uint8_t *p_data,
1612 size_t data_size,
1613 size_t *p_data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001614
1615/**
Derek Miller16e72292018-10-15 16:14:24 -05001616 * \brief A struct containing all of the function pointers needed to for key
1617 * management using opaque keys
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001618 *
Derek Miller16e72292018-10-15 16:14:24 -05001619 * PSA Crypto API implementations should populate instances of the table as
1620 * appropriate upon startup.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001621 *
Derek Miller5b3417a2018-10-10 17:55:03 -05001622 * If one of the functions is not implemented, it should be set to NULL.
1623 */
1624struct pcd_key_management_t {
Derek Miller16e72292018-10-15 16:14:24 -05001625 /** Function that performs the key import operation */
1626 pcd_opaque_import_key_t *p_import;
1627 /** Function that performs the key destroy operation */
1628 pcd_destroy_key_t *p_destroy;
1629 /** Function that performs the key export operation */
1630 pcd_export_key_t *p_export;
1631 /** Function that perforsm the public key export operation */
1632 pcd_export_public_key_t *p_export_public;
Derek Miller5b3417a2018-10-10 17:55:03 -05001633};
1634
Derek Miller16e72292018-10-15 16:14:24 -05001635/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001636
Derek Miller16e72292018-10-15 16:14:24 -05001637/** \defgroup driver_derivation Key Derivation and Agreement
Derek Miller16e72292018-10-15 16:14:24 -05001638 * Key derivation is the process of generating new key material using an
1639 * existing key and additional parameters, iterating through a basic
1640 * cryptographic function, such as a hash.
1641 * Key agreement is a part of cryptographic protocols that allows two parties
1642 * to agree on the same key value, but starting from different original key
1643 * material.
1644 * The flows are similar, and the PSA Crypto Driver API uses the same functions
1645 * for both of the flows.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001646 *
Derek Miller16e72292018-10-15 16:14:24 -05001647 * There are two different final functions for the flows,
1648 * `pcd_key_derivation_derive` and `pcd_key_derivation_export`.
1649 * `pcd_key_derivation_derive` is used when the key material should be placed
1650 * in a slot on the hardware and not exposed to the caller.
1651 * `pcd_key_derivation_export` is used when the key material should be returned
1652 * to the PSA Cryptographic API implementation.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001653 *
Derek Miller16e72292018-10-15 16:14:24 -05001654 * Different key derivation algorithms require a different number of inputs.
1655 * Instead of having an API that takes as input variable length arrays, which
1656 * can be problemmatic to manage on embedded platforms, the inputs are passed
1657 * to the driver via a function, `pcd_key_derivation_collateral`, that is
1658 * called multiple times with different `collateral_id`s. Thus, for a key
1659 * derivation algorithm that required 3 paramter inputs, the flow would look
1660 * something like:
1661 * ~~~~~~~~~~~~~{.c}
1662 * pcd_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1663 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_0,
1664 * p_collateral_0,
1665 * collateral_0_size);
1666 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_1,
1667 * p_collateral_1,
1668 * collateral_1_size);
1669 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_2,
1670 * p_collateral_2,
1671 * collateral_2_size);
1672 * pcd_key_derivation_derive();
1673 * ~~~~~~~~~~~~~
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001674 *
Derek Miller16e72292018-10-15 16:14:24 -05001675 * key agreement example:
1676 * ~~~~~~~~~~~~~{.c}
1677 * pcd_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1678 * pcd_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1679 * pcd_key_derivation_export(p_session_key,
1680 * session_key_size,
1681 * &session_key_length);
1682 * ~~~~~~~~~~~~~
1683 */
Derek Miller765682c2018-10-22 15:27:27 -05001684/**@{*/
Derek Miller16e72292018-10-15 16:14:24 -05001685
Derek Miller765682c2018-10-22 15:27:27 -05001686/** \brief The hardware-specific key derivation context structure
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001687 *
Derek Miller765682c2018-10-22 15:27:27 -05001688 * The contents of this structure are implementation dependent and are
1689 * therefore not described here
1690 */
Derek Miller81133a62018-10-23 14:55:32 -05001691typedef struct pcd_key_derivation_context_s pcd_key_derivation_context_t;
Derek Miller16e72292018-10-15 16:14:24 -05001692
1693/** \brief Set up a key derivation operation by specifying the algorithm and
1694 * the source key sot
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001695 *
Derek Miller16e72292018-10-15 16:14:24 -05001696 * \param[in,out] p_context A hardware-specific structure containing any
1697 * context information for the implementation
1698 * \param[in] kdf_alg The algorithm to be used for the key derivation
1699 * \param[in] souce_key The key to be used as the source material for the
1700 * key derivation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001701 *
Derek Miller16e72292018-10-15 16:14:24 -05001702 * \retval PSA_SUCCESS
1703 */
Derek Miller81133a62018-10-23 14:55:32 -05001704typedef psa_status_t (*pcd_key_derivation_setup_t)(pcd_key_derivation_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -05001705 psa_algorithm_t kdf_alg,
1706 psa_key_slot_t source_key);
1707
1708/** \brief Provide collateral (parameters) needed for a key derivation or key
1709 * agreement operation
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001710 *
Derek Miller16e72292018-10-15 16:14:24 -05001711 * Since many key derivation algorithms require multiple parameters, it is
1712 * expeced that this function may be called multiple times for the same
1713 * operation, each with a different algorithm-specific `collateral_id`
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001714 *
Derek Miller16e72292018-10-15 16:14:24 -05001715 * \param[in,out] p_context A hardware-specific structure containing any
1716 * context information for the implementation
1717 * \param[in] collateral_id An ID for the collateral being provided
1718 * \param[in] p_collateral A buffer containing the collateral data
1719 * \param[in] collateral_size The size in bytes of the collateral
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001720 *
Derek Miller16e72292018-10-15 16:14:24 -05001721 * \retval PSA_SUCCESS
1722 */
Derek Miller81133a62018-10-23 14:55:32 -05001723typedef psa_status_t (*pcd_key_derivation_collateral_t)(pcd_key_derivation_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -05001724 uint32_t collateral_id,
Derek Miller81133a62018-10-23 14:55:32 -05001725 const uint8_t *p_collateral,
Derek Miller16e72292018-10-15 16:14:24 -05001726 size_t collateral_size);
1727
1728/** \brief Perform the final key derivation step and place the generated key
1729 * material in a slot
1730 * \param[in,out] p_context A hardware-specific structure containing any
1731 * context information for the implementation
1732 * \param[in] dest_key The slot where the generated key material
1733 * should be placed
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001734 *
Derek Miller16e72292018-10-15 16:14:24 -05001735 * \retval PSA_SUCCESS
1736 */
Derek Miller81133a62018-10-23 14:55:32 -05001737typedef psa_status_t (*pcd_key_derivation_derive_t)(pcd_key_derivation_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -05001738 psa_key_slot_t dest_key);
1739
1740/** \brief Perform the final step of a key agreement and place the generated
1741 * key material in a buffer
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001742 *
Derek Miller16e72292018-10-15 16:14:24 -05001743 * \param[out] p_output Buffer in which to place the generated key
1744 * material
1745 * \param[in] output_size The size in bytes of `p_output`
1746 * \param[out] p_output_length Upon success, contains the number of bytes of
1747 * key material placed in `p_output`
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001748 *
Derek Miller16e72292018-10-15 16:14:24 -05001749 * \retval PSA_SUCCESS
1750 */
1751typedef psa_status_t (*pcd_key_derivation_export_t)(uint8_t *p_output,
1752 size_t output_size,
1753 size_t *p_output_length);
1754
1755/**
1756 * \brief A struct containing all of the function pointers needed to for key
1757 * derivation and agreement
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001758 *
Derek Miller16e72292018-10-15 16:14:24 -05001759 * PSA Crypto API implementations should populate instances of the table as
1760 * appropriate upon startup.
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001761 *
Derek Miller5b3417a2018-10-10 17:55:03 -05001762 * If one of the functions is not implemented, it should be set to NULL.
1763 */
1764struct pcd_key_derivation_t {
Derek Miller16e72292018-10-15 16:14:24 -05001765 /** Function that performs the key derivation setup */
1766 pcd_key_derivation_setup_t *p_setup;
1767 /** Function that sets the key derivation collateral */
1768 pcd_key_derivation_collateral_t *p_collateral;
1769 /** Function that performs the final key derivation step */
1770 pcd_key_derivation_derive_t *p_derive;
1771 /** Function that perforsm the final key derivation or agreement and
1772 * exports the key */
1773 pcd_key_derivation_export_t *p_export;
Derek Miller5b3417a2018-10-10 17:55:03 -05001774};
1775
Derek Miller16e72292018-10-15 16:14:24 -05001776/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001777
Jaeden Amerod3d26aa2018-10-26 10:07:32 +01001778#endif // __PSA_CRYPTO_DRIVER_H__