blob: 19f5adaacc99eb0eef68c052a1c8fdc90dc0d92e [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
4 *
5 * 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.
34 * It is intended that these will be moved to a new common header file to
35 * 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
44/** \defgroup opaque_mac Opaque Message Authentication Code
Derek Miller765682c2018-10-22 15:27:27 -050045 * Generation and authentication of Message Authentication Codes (MACs) using
46 * opaque keys can be done either as a single function call (via the
47 * `pcd_mac_opaque_generate_t` or `psa_mac_opaque_verify_t` functions), or in
48 * parts using the following sequence:
49 * - `psa_mac_opaque_setup_t`
50 * - `psa_mac_opaque_update_t`
51 * - `psa_mac_opaque_update_t`
52 * - ...
53 * - `psa_mac_opaque_finish_t` or `psa_mac_opaque_finish_verify_t`
54 *
55 * If a previously started Opaque MAC operation needs to be terminated, it
56 * should be done so by the `psa_mac_opaque_abort_t`. Failure to do so may
57 * result in allocated resources not being freed or in other undefined
58 * behavior.
Derek Miller5b3417a2018-10-10 17:55:03 -050059 */
Derek Miller16e72292018-10-15 16:14:24 -050060/**@{*/
61/** \brief A function that starts a MAC operation for a PSA Crypto Driver
62 * implementation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -050063 *
Derek Miller16e72292018-10-15 16:14:24 -050064 * \param[in,out] p_context A structure that will contain the
65 * hardware-specific MAC context
66 * \param[in] key_slot The slot of the key to be used for the
67 * operation
68 * \param[in] algorithm The algorithm to be used to underly the MAC
69 * operation
Derek Miller5b3417a2018-10-10 17:55:03 -050070 *
71 * \retval PSA_SUCCESS
72 * Success.
73 */
Derek Miller16e72292018-10-15 16:14:24 -050074typedef psa_status_t (*pcd_mac_opaque_setup_t)(void *p_context,
75 psa_key_slot_t key_slot,
76 psa_algorithm_t algorithm);
Derek Miller5b3417a2018-10-10 17:55:03 -050077
Derek Miller16e72292018-10-15 16:14:24 -050078/** \brief A function that continues a previously started MAC operation using
79 * an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -050080 *
Derek Miller16e72292018-10-15 16:14:24 -050081 * \param[in,out] p_context A hardware-specific structure for the
82 * previously-established MAC operation to be
83 * continued
84 * \param[in] p_input A buffer containing the message to be appended
85 * to the MAC operation
86 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -050087 */
Derek Miller16e72292018-10-15 16:14:24 -050088typedef psa_status_t (*pcd_mac_opaque_update_t)(void *p_context,
89 const uint8_t *p_input,
90 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -050091
Derek Miller16e72292018-10-15 16:14:24 -050092/** \brief a function that completes a previously started MAC operation by
93 * returning the resulting MAC using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -050094 *
Derek Millerf3d0a562018-10-18 16:41:08 -050095 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -050096 * previously started MAC operation to be
97 * finished
98 * \param[out] p_mac A buffer where the generated MAC will be
99 * placed
100 * \param[in] mac_size The size in bytes of the buffer that has been
101 * allocated for the `output` buffer
102 * \param[out] p_mac_length After completion, will contain the number of
Derek Millerf3d0a562018-10-18 16:41:08 -0500103 * bytes placed in the `p_mac` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500104 *
105 * \retval PSA_SUCCESS
106 * Success.
107 */
Derek Miller16e72292018-10-15 16:14:24 -0500108typedef psa_status_t (*pcd_mac_opaque_finish_t)(void *p_context,
109 uint8_t *p_mac,
110 size_t mac_size,
111 size_t *p_mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500112
Derek Miller16e72292018-10-15 16:14:24 -0500113/** \brief A function that completes a previously started MAC operation by
114 * comparing the resulting MAC against a known value using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -0500115 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500116 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500117 * started MAC operation to be fiinished
118 * \param[in] p_mac The MAC value against which the resulting MAC will
119 * be compared against
120 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
Derek Miller5b3417a2018-10-10 17:55:03 -0500121 *
122 * \retval PSA_SUCCESS
Derek Miller16e72292018-10-15 16:14:24 -0500123 * The operation completed successfully and the MACs matched each
124 * other
Derek Miller5b3417a2018-10-10 17:55:03 -0500125 * \retval PSA_ERROR_INVALID_SIGNATURE
Derek Miller16e72292018-10-15 16:14:24 -0500126 * The operation completed successfully, but the calculated MAC did
127 * not match the provided MAC
Derek Miller5b3417a2018-10-10 17:55:03 -0500128 */
Derek Miller16e72292018-10-15 16:14:24 -0500129typedef psa_status_t (*pcd_mac_opaque_finish_verify_t)(void *p_context,
130 const uint8_t *p_mac,
131 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500132
Derek Miller16e72292018-10-15 16:14:24 -0500133/** \brief A function that aborts a previous started opaque-key MAC operation
134
Derek Millerf3d0a562018-10-18 16:41:08 -0500135 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500136 * started MAC operation to be aborted
137 */
138typedef psa_status_t (*pcd_mac_opaque_abort_t)(void *p_context);
139
Derek Miller81133a62018-10-23 14:55:32 -0500140/** \brief A function that performs a MAC operation in one command and returns
Derek Miller16e72292018-10-15 16:14:24 -0500141 * the calculated MAC using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -0500142 *
Derek Miller16e72292018-10-15 16:14:24 -0500143 * \param[in] p_input A buffer containing the message to be MACed
Derek Millerf3d0a562018-10-18 16:41:08 -0500144 * \param[in] input_length The size in bytes of `p_input`
Derek Miller16e72292018-10-15 16:14:24 -0500145 * \param[in] key_slot The slot of the key to be used
Derek Millerf3d0a562018-10-18 16:41:08 -0500146 * \param[in] alg The algorithm to be used to underlie the MAC
Derek Miller16e72292018-10-15 16:14:24 -0500147 * operation
148 * \param[out] p_mac A buffer where the generated MAC will be
149 * placed
Derek Miller81133a62018-10-23 14:55:32 -0500150 * \param[in] mac_size The size in bytes of the `p_mac` buffer
Derek Miller16e72292018-10-15 16:14:24 -0500151 * \param[out] p_mac_length After completion, will contain the number of
152 * bytes placed in the `output` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500153 *
154 * \retval PSA_SUCCESS
155 * Success.
156 */
Derek Miller16e72292018-10-15 16:14:24 -0500157typedef psa_status_t (*pcd_mac_opaque_generate_t)(const uint8_t *p_input,
158 size_t input_length,
159 psa_key_slot_t key_slot,
160 psa_algorithm_t alg,
161 uint8_t *p_mac,
162 size_t mac_size,
163 size_t *p_mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500164
Derek Miller16e72292018-10-15 16:14:24 -0500165/** \brief A function that performs an MAC operation in one command and
166 * compare the resulting MAC against a known value using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -0500167 *
Derek Miller16e72292018-10-15 16:14:24 -0500168 * \param[in] p_input A buffer containing the message to be MACed
169 * \param[in] input_length The size in bytes of `input`
170 * \param[in] key_slot The slot of the key to be used
171 * \param[in] alg The algorithm to be used to underlie the MAC
172 * operation
173 * \param[in] p_mac The MAC value against which the resulting MAC will
174 * be compared against
175 * \param[in] mac_length The size in bytes of `mac`
Derek Miller5b3417a2018-10-10 17:55:03 -0500176 *
177 * \retval PSA_SUCCESS
Derek Miller16e72292018-10-15 16:14:24 -0500178 * The operation completed successfully and the MACs matched each
179 * other
Derek Miller5b3417a2018-10-10 17:55:03 -0500180 * \retval PSA_ERROR_INVALID_SIGNATURE
Derek Miller16e72292018-10-15 16:14:24 -0500181 * The operation completed successfully, but the calculated MAC did
182 * not match the provided MAC
Derek Miller5b3417a2018-10-10 17:55:03 -0500183 */
Derek Miller16e72292018-10-15 16:14:24 -0500184typedef psa_status_t (*pcd_mac_opaque_verify_t)(const uint8_t *p_input,
Derek Miller5b3417a2018-10-10 17:55:03 -0500185 size_t input_length,
186 psa_key_slot_t key_slot,
187 psa_algorithm_t alg,
Derek Miller16e72292018-10-15 16:14:24 -0500188 const uint8_t *p_mac,
189 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500190
Derek Miller16e72292018-10-15 16:14:24 -0500191/** \brief A struct containing all of the function pointers needed to
192 * implement MAC operations using opaque keys.
Derek Miller5b3417a2018-10-10 17:55:03 -0500193 *
Derek Miller16e72292018-10-15 16:14:24 -0500194 * PSA Crypto API implementations should populate the table as appropriate
195 * upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -0500196 *
Derek Miller765682c2018-10-22 15:27:27 -0500197 * If one of the functions is not implemented (such as
198 * `pcd_mac_opaque_generate_t`), it should be set to NULL.
Derek Miller16e72292018-10-15 16:14:24 -0500199 *
200 * Driver implementers should ensure that they implement all of the functions
201 * that make sense for their hardware, and that they provide a full solution
202 * (for example, if they support `p_setup`, they should also support
203 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
Derek Miller5b3417a2018-10-10 17:55:03 -0500204 *
205 */
206struct pcd_mac_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -0500207 /**The size in bytes of the hardware-specific Opaque-MAC Context structure
208 */
209 size_t context_size;
210 /** Function that performs the setup operation
211 */
212 pcd_mac_opaque_setup_t *p_setup;
213 /** Function that performs the update operation
214 */
215 pcd_mac_opaque_update_t *p_update;
216 /** Function that completes the operation
217 */
218 pcd_mac_opaque_finish_t *p_finish;
219 /** Function that completed a MAC operation with a verify check
220 */
221 pcd_mac_opaque_finish_verify_t *p_finish_verify;
222 /** Function that aborts a previoustly started operation
223 */
224 pcd_mac_opaque_abort_t *p_abort;
225 /** Function that performs the MAC operation in one call
226 */
227 pcd_mac_opaque_generate_t *p_mac;
228 /** Function that performs the MAC and verify operation in one call
229 */
230 pcd_mac_opaque_verify_t *p_mac_verify;
Derek Miller5b3417a2018-10-10 17:55:03 -0500231};
Derek Miller16e72292018-10-15 16:14:24 -0500232/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500233
234/** \defgroup transparent_mac Transparent Message Authentication Code
Derek Miller765682c2018-10-22 15:27:27 -0500235 * Generation and authentication of Message Authentication Codes (MACs) using
236 * transparent keys can be done either as a single function call (via the
237 * `pcd_mac_transparent_generate_t` or `psa_mac_transparent_verify_t`
238 * functions), or in parts using the following sequence:
239 * - `psa_mac_transparent_setup_t`
240 * - `psa_mac_transparent_update_t`
241 * - `psa_mac_transparent_update_t`
242 * - ...
243 * - `psa_mac_transparent_finish_t` or `psa_mac_transparent_finish_verify_t`
244 *
245 * If a previously started Transparent MAC operation needs to be terminated, it
246 * should be done so by the `psa_mac_transparent_abort_t`. Failure to do so may
247 * result in allocated resources not being freed or in other undefined
248 * behavior.
249 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500250 */
Derek Miller16e72292018-10-15 16:14:24 -0500251/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500252
253/** \brief The hardware-specific transparent-key MAC context structure
Derek Miller765682c2018-10-22 15:27:27 -0500254 *
Derek Miller16e72292018-10-15 16:14:24 -0500255 * The contents of this structure are implementation dependent and are
256 * therefore not described here.
Derek Miller5b3417a2018-10-10 17:55:03 -0500257 */
Derek Miller81133a62018-10-23 14:55:32 -0500258typedef struct pcd_mac_transparent_context_s pcd_mac_transparent_context_t;
Derek Miller5b3417a2018-10-10 17:55:03 -0500259
Derek Miller16e72292018-10-15 16:14:24 -0500260/** \brief The function prototype for the setup operation of a
261 * transparent-key MAC operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500262 *
Derek Miller16e72292018-10-15 16:14:24 -0500263 * Functions that implement the prototype should be named in the following
264 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500265 * ~~~~~~~~~~~~~{.c}
Derek Miller16e72292018-10-15 16:14:24 -0500266 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_setup
Derek Miller5b3417a2018-10-10 17:55:03 -0500267 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500268 * Where `ALGO` is the name of the underlying primitive, and `MAC_VARIANT`
269 * is the specific variant of a MAC operation (such as HMAC or CMAC)
Derek Miller5b3417a2018-10-10 17:55:03 -0500270 *
Derek Miller16e72292018-10-15 16:14:24 -0500271 * \param[in,out] p_context A structure that will contain the
272 * hardware-specific MAC context
273 * \param[in] p_key A buffer containing the cleartext key material
274 * to be used in the operation
275 * \param[in] key_length The size in bytes of the key material
Derek Miller5b3417a2018-10-10 17:55:03 -0500276 *
277 * \retval PSA_SUCCESS
278 * Success.
279 */
Derek Miller81133a62018-10-23 14:55:32 -0500280typedef psa_status_t (*pcd_mac_transparent_setup_t)(pcd_mac_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500281 const uint8_t *p_key,
282 size_t key_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500283
Derek Miller16e72292018-10-15 16:14:24 -0500284/** \brief The function prototype for the update operation of a
285 * transparent-key MAC operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500286 *
Derek Miller16e72292018-10-15 16:14:24 -0500287 * Functions that implement the prototype should be named in the following
288 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500289 * ~~~~~~~~~~~~~{.c}
290 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_update
291 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500292 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT`
293 * is the specific variant of a MAC operation (such as HMAC or CMAC)
Derek Miller5b3417a2018-10-10 17:55:03 -0500294 *
Derek Miller16e72292018-10-15 16:14:24 -0500295 * \param[in,out] p_context A hardware-specific structure for the
296 * previously-established MAC operation to be
297 * continued
298 * \param[in] p_input A buffer containing the message to be appended
299 * to the MAC operation
300 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500301 */
Derek Miller81133a62018-10-23 14:55:32 -0500302typedef psa_status_t (*pcd_mac_transparent_update_t)(pcd_mac_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500303 const uint8_t *p_input,
304 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500305
Derek Miller16e72292018-10-15 16:14:24 -0500306/** \brief The function prototype for the finish operation of a
307 * transparent-key MAC operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500308 *
Derek Miller16e72292018-10-15 16:14:24 -0500309 * Functions that implement the prototype should be named in the following
310 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500311 * ~~~~~~~~~~~~~{.c}
312 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish
313 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500314 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
315 * the specific variant of a MAC operation (such as HMAC or CMAC)
Derek Miller5b3417a2018-10-10 17:55:03 -0500316 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500317 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500318 * previously started MAC operation to be
319 * finished
320 * \param[out] p_mac A buffer where the generated MAC will be placed
321 * \param[in] mac_length The size in bytes of the buffer that has been
322 * allocated for the `p_mac` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500323 *
324 * \retval PSA_SUCCESS
325 * Success.
326 */
Derek Miller81133a62018-10-23 14:55:32 -0500327typedef psa_status_t (*pcd_mac_transparent_finish_t)(pcd_mac_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500328 uint8_t *p_mac,
329 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500330
Derek Miller16e72292018-10-15 16:14:24 -0500331/** \brief The function prototype for the finish and verify operation of a
332 * transparent-key MAC operation
333 *
334 * Functions that implement the prototype should be named in the following
335 * convention:
336 * ~~~~~~~~~~~~~{.c}
337 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish_verify
338 * ~~~~~~~~~~~~~
339 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
340 * the specific variant of a MAC operation (such as HMAC or CMAC)
341 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500342 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500343 * previously started MAC operation to be
Derek Millerf3d0a562018-10-18 16:41:08 -0500344 * verified and finished
Derek Miller16e72292018-10-15 16:14:24 -0500345 * \param[in] p_mac A buffer containing the MAC that will be used
346 * for verification
347 * \param[in] mac_length The size in bytes of the data in the `p_mac`
348 * buffer
349 *
350 * \retval PSA_SUCCESS
351 * The operation completed successfully and the comparison matched
Derek Miller5b3417a2018-10-10 17:55:03 -0500352 */
Derek Miller81133a62018-10-23 14:55:32 -0500353typedef psa_status_t (*pcd_mac_transparent_finish_verify_t)(pcd_mac_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500354 const uint8_t *p_mac,
355 size_t mac_length);
356
357/** \brief The function prototype for the abort operation for a previously
358 * started transparent-key MAC operation
359 *
360 * Functions that implement the prototype should be named in the following
361 * convention:
362 * ~~~~~~~~~~~~~{.c}
363 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_abort
364 * ~~~~~~~~~~~~~
365 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
366 * the specific variant of a MAC operation (such as HMAC or CMAC)
367 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500368 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500369 * previously started MAC operation to be
Derek Millerf3d0a562018-10-18 16:41:08 -0500370 * aborted
Derek Miller16e72292018-10-15 16:14:24 -0500371 *
372 */
Derek Miller81133a62018-10-23 14:55:32 -0500373typedef psa_status_t (*pcd_mac_transparent_abort_t)(pcd_mac_transparent_context_t *p_context);
Derek Miller16e72292018-10-15 16:14:24 -0500374
375/** \brief The function prototype for a one-shot operation of a transparent-key
376 * MAC operation
377 *
378 * Functions that implement the prototype should be named in the following
379 * convention:
380 * ~~~~~~~~~~~~~{.c}
381 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>
382 * ~~~~~~~~~~~~~
383 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
384 * the specific variant of a MAC operation (such as HMAC or CMAC)
385 *
386 * \param[in] p_input A buffer containing the data to be MACed
387 * \param[in] input_length The length in bytes of the `p_input` data
388 * \param[in] p_key A buffer containing the key material to be used
389 * for the MAC operation
390 * \param[in] key_length The length in bytes of the `p_key` data
391 * \param[in] alg The algorithm to be performed
392 * \param[out] p_mac The buffer where the resulting MAC will be placed
393 * upon success
394 * \param[in] mac_length The length in bytes of the `p_mac` buffer
395 */
396typedef psa_status_t (*pcd_mac_transparent_t)(const uint8_t *p_input,
397 size_t input_length,
398 const uint8_t *p_key,
399 size_t key_length,
400 psa_algorithm_t alg,
401 uint8_t *p_mac,
402 size_t mac_length);
403
404/** \brief The function prototype for a one-shot operation of a transparent-key
405 * MAC Verify operation
406 *
407 * Functions that implement the prototype should be named in the following
408 * convention:
409 * ~~~~~~~~~~~~~{.c}
410 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_verify
411 * ~~~~~~~~~~~~~
412 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
413 * the specific variant of a MAC operation (such as HMAC or CMAC)
414 *
415 * \param[in] p_input A buffer containing the data to be MACed
416 * \param[in] input_length The length in bytes of the `p_input` data
417 * \param[in] p_key A buffer containing the key material to be used
418 * for the MAC operation
419 * \param[in] key_length The length in bytes of the `p_key` data
420 * \param[in] alg The algorithm to be performed
421 * \param[in] p_mac The MAC data to be compared
422 * \param[in] mac_length The length in bytes of the `p_mac` buffer
423 *
424 * \retval PSA_SUCCESS
425 * The operation completed successfully and the comparison matched
426 */
427typedef psa_status_t (*pcd_mac_transparent_verify_t)(const uint8_t *p_input,
428 size_t input_length,
429 const uint8_t *p_key,
430 size_t key_length,
431 psa_algorithm_t alg,
432 const uint8_t *p_mac,
433 size_t mac_length);
434/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500435
436/** \defgroup opaque_cipher Opaque Symmetric Ciphers
Derek Miller765682c2018-10-22 15:27:27 -0500437 *
438 * Encryption and Decryption using opaque keys in block modes other than ECB
439 * must be done in multiple parts, using the following flow:
440 * - `pcd_cipher_opaque_setup_t`
441 * - `pcd_cipher_opaque_set_iv_t` (optional depending upon block mode)
442 * - `pcd_cipher_opaque_update_t`
443 * - ...
444 * - `pcd_cipher_opaque_finish_t`
445
446 * If a previously started Opaque Cipher operation needs to be terminated, it
447 * should be done so by the `psa_cipher_opaque_abort_t`. Failure to do so may
448 * result in allocated resources not being freed or in other undefined
449 * behavior.
450 *
451 * In situations where a PSA Cryptographic API implementation is using a block
452 * mode not-supported by the underlying hardware or driver, it can construct
453 * the block mode itself, while calling the `pcd_cipher_opaque_ecb_t` function
454 * pointer for the cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500455 */
Derek Miller16e72292018-10-15 16:14:24 -0500456/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500457
Derek Miller16e72292018-10-15 16:14:24 -0500458/** \brief A function pointer that provides the cipher setup function for
459 * opaque-key operations
Derek Miller5b3417a2018-10-10 17:55:03 -0500460 *
Derek Miller16e72292018-10-15 16:14:24 -0500461 * \param[in,out] p_context A structure that will contain the
462 * hardware-specific cipher context.
463 * \param[in] key_slot The slot of the key to be used for the
464 * operation
465 * \param[in] algorithm The algorithm to be used in the cipher
466 * operation
467 * \param[in] direction Indicates whether the operation is an encrypt
468 * or decrypt
Derek Miller5b3417a2018-10-10 17:55:03 -0500469 *
470 * \retval PSA_SUCCESS
471 * \retval PSA_ERROR_NOT_SUPPORTED
472 */
Derek Miller16e72292018-10-15 16:14:24 -0500473typedef psa_status_t (*pcd_cipher_opaque_setup_t)(void *p_context,
474 psa_key_slot_t key_slot,
Derek Miller5b3417a2018-10-10 17:55:03 -0500475 psa_algorithm_t algorithm,
Derek Miller16e72292018-10-15 16:14:24 -0500476 encrypt_or_decrypt_t direction);
477
478/** \brief A function pointer that sets the initialization vector (if
479 * necessary) for an opaque cipher operation
480 *
Derek Miller81133a62018-10-23 14:55:32 -0500481 * Rationale: The `psa_cipher_*` function in the PSA Cryptographic API has two
Derek Miller765682c2018-10-22 15:27:27 -0500482 * IV functions: one to set the IV, and one to generate it internally. The
483 * generate function is not necessary for the driver API as the PSA Crypto
484 * implementation can do the generation using its RNG features.
Derek Miller16e72292018-10-15 16:14:24 -0500485 *
486 * \param[in,out] p_context A structure that contains the previously set up
487 * hardware-specific cipher context
488 * \param[in] p_iv A buffer containing the initialization vector
489 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
490 *
491 * \retval PSA_SUCCESS
492 */
493typedef psa_status_t (*pcd_cipher_opaque_set_iv_t)(void *p_context,
494 const uint8_t *p_iv,
495 size_t iv_length);
496
497/** \brief A function that continues a previously started opaque-key cipher
498 * operation
499 *
500 * \param[in,out] p_context A hardware-specific structure for the
501 * previously started cipher operation
502 * \param[in] p_input A buffer containing the data to be
503 * encrypted/decrypted
504 * \param[in] input_size The size in bytes of the buffer pointed to
505 * by `p_input`
506 * \param[out] p_output The caller-allocated buffer where the
507 * output will be placed
508 * \param[in] output_size The allocated size in bytes of the
509 * `p_output` buffer
510 * \param[out] p_output_length After completion, will contain the number
511 * of bytes placed in the `p_output` buffer
512 *
513 * \retval PSA_SUCCESS
514 */
515typedef psa_status_t (*pcd_cipher_opaque_update_t)(void *p_context,
516 const uint8_t *p_input,
517 size_t input_size,
518 uint8_t *p_output,
519 size_t output_size,
520 size_t *p_output_length);
521
522/** \brief A function that completes a previously started opaque-key cipher
523 * operation
524 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500525 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500526 * previously started cipher operation
Derek Millerf3d0a562018-10-18 16:41:08 -0500527 * \param[out] p_output The caller-allocated buffer where the output
Derek Miller16e72292018-10-15 16:14:24 -0500528 * will be placed
529 * \param[in] output_size The allocated size in bytes of the `p_output`
530 * buffer
531 * \param[out] p_output_length After completion, will contain the number of
532 * bytes placed in the `p_output` buffer
533 *
534 * \retval PSA_SUCCESS
535 */
536typedef psa_status_t (*pcd_cipher_opaque_finish_t)(void *p_context,
537 uint8_t *p_output,
538 size_t output_size,
539 size_t *p_output_length);
540
541/** \brief A function that aborts a previously started opaque-key cipher
542 * operation
543 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500544 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500545 * previously started cipher operation
546 */
547typedef psa_status_t (*pcd_cipher_opaque_abort_t)(void *p_context);
548
549/** \brief A function that performs the ECB block mode for opaque-key cipher
550 * operations
551 *
552 * Note: this function should only be used with implementations that do not
553 * provide a needed higher-level operation.
554 *
555 * \param[in] key_slot The slot of the key to be used for the operation
556 * \param[in] algorithm The algorithm to be used in the cipher operation
557 * \param[in] direction Indicates whether the operation is an encrypt or
558 * decrypt
559 * \param[in] p_input A buffer containing the data to be
560 * encrypted/decrypted
561 * \param[in] input_size The size in bytes of the buffer pointed to by
562 * `p_input`
Derek Millerf3d0a562018-10-18 16:41:08 -0500563 * \param[out] p_output The caller-allocated buffer where the output will
Derek Miller16e72292018-10-15 16:14:24 -0500564 * be placed
565 * \param[in] output_size The allocated size in bytes of the `p_output`
566 * buffer
567 *
568 * \retval PSA_SUCCESS
569 * \retval PSA_ERROR_NOT_SUPPORTED
570 */
571typedef psa_status_t (*pcd_cipher_opaque_ecb_t)(psa_key_slot_t key_slot,
572 psa_algorithm_t algorithm,
573 encrypt_or_decrypt_t direction,
574 const uint8_t *p_input,
575 size_t input_size,
576 uint8_t *p_output,
577 size_t output_size);
Derek Miller5b3417a2018-10-10 17:55:03 -0500578
579/**
Derek Miller16e72292018-10-15 16:14:24 -0500580 * \brief A struct containing all of the function pointers needed to implement
581 * cipher operations using opaque keys.
Derek Miller5b3417a2018-10-10 17:55:03 -0500582 *
Derek Miller16e72292018-10-15 16:14:24 -0500583 * PSA Crypto API implementations should populate instances of the table as
584 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -0500585 *
Derek Miller16e72292018-10-15 16:14:24 -0500586 * If one of the functions is not implemented (such as
587 * `pcd_cipher_opaque_ecb_t`), it should be set to NULL.
Derek Miller5b3417a2018-10-10 17:55:03 -0500588 */
589struct pcd_cipher_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -0500590 /** The size in bytes of the hardware-specific Opaque Cipher context
591 * structure
592 */
593 size_t size;
594 /** Function that performs the setup operation */
595 pcd_cipher_opaque_setup_t *p_setup;
596 /** Function that sets the IV (if necessary) */
597 pcd_cipher_opaque_set_iv_t *p_set_iv;
598 /** Function that performs the update operation */
599 pcd_cipher_opaque_update_t *p_update;
600 /** Function that completes the operation */
601 pcd_cipher_opaque_finish_t *p_finish;
602 /** Function that aborts the operation */
603 pcd_cipher_opaque_abort_t *p_abort;
604 /** Function that performs ECB mode for the cipher
605 * (Danger: ECB mode should not be used directly by clients of the PSA
606 * Crypto Client API)
607 */
608 pcd_cipher_opaque_ecb_t *p_ecb;
Derek Miller5b3417a2018-10-10 17:55:03 -0500609};
610
Derek Miller16e72292018-10-15 16:14:24 -0500611/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500612
613/** \defgroup transparent_cipher Transparent Block Cipher
Derek Miller765682c2018-10-22 15:27:27 -0500614 * Encryption and Decryption using transparent keys in block modes other than
615 * ECB must be done in multiple parts, using the following flow:
616 * - `pcd_cipher_transparent_setup_t`
617 * - `pcd_cipher_transparent_set_iv_t` (optional depending upon block mode)
618 * - `pcd_cipher_transparent_update_t`
619 * - ...
620 * - `pcd_cipher_transparent_finish_t`
621
622 * If a previously started Transparent Cipher operation needs to be terminated,
623 * it should be done so by the `psa_cipher_transparent_abort_t`. Failure to do
624 * so may result in allocated resources not being freed or in other undefined
625 * behavior.
Derek Miller5b3417a2018-10-10 17:55:03 -0500626 */
Derek Miller16e72292018-10-15 16:14:24 -0500627/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500628
629/** \brief The hardware-specific transparent-key Cipher context structure
Derek Miller765682c2018-10-22 15:27:27 -0500630 *
631 * The contents of this structure are implementation dependent and are
Derek Miller16e72292018-10-15 16:14:24 -0500632 * therefore not described here.
Derek Miller5b3417a2018-10-10 17:55:03 -0500633 */
Derek Miller81133a62018-10-23 14:55:32 -0500634typedef struct pcd_cipher_transparent_context_s pcd_cipher_transparent_context_t;
Derek Miller5b3417a2018-10-10 17:55:03 -0500635
Derek Miller16e72292018-10-15 16:14:24 -0500636/** \brief The function prototype for the setup operation of transparent-key
637 * block cipher operations.
638 * Functions that implement the prototype should be named in the following
639 * conventions:
Derek Miller5b3417a2018-10-10 17:55:03 -0500640 * ~~~~~~~~~~~~~{.c}
641 * pcd_cipher_transparent_setup_<CIPHER_NAME>_<MODE>
642 * ~~~~~~~~~~~~~
643 * Where
644 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
645 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Derek Miller16e72292018-10-15 16:14:24 -0500646 * or for stream ciphers:
647 * ~~~~~~~~~~~~~{.c}
648 * pcd_cipher_transparent_setup_<CIPHER_NAME>
649 * ~~~~~~~~~~~~~
650 * Where `CIPHER_NAME` is the name of a stream cipher (i.e. RC4)
Derek Miller5b3417a2018-10-10 17:55:03 -0500651 *
Derek Miller16e72292018-10-15 16:14:24 -0500652 * \param[in,out] p_context A structure that will contain the
653 * hardware-specific cipher context
654 * \param[in] direction Indicates if the operation is an encrypt or a
655 * decrypt
656 * \param[in] p_key_data A buffer containing the cleartext key material
657 * to be used in the operation
658 * \param[in] key_data_size The size in bytes of the key material
Derek Miller5b3417a2018-10-10 17:55:03 -0500659 *
660 * \retval PSA_SUCCESS
661 */
Derek Miller81133a62018-10-23 14:55:32 -0500662typedef psa_status_t (*pcd_cipher_transparent_setup_t)(pcd_cipher_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500663 encrypt_or_decrypt_t direction,
664 const uint8_t *p_key_data,
665 size_t key_data_size);
Derek Miller5b3417a2018-10-10 17:55:03 -0500666
Derek Miller16e72292018-10-15 16:14:24 -0500667/** \brief The function prototype for the set initialization vector operation
668 * of transparent-key block cipher operations
669 * Functions that implement the prototype should be named in the following
670 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500671 * ~~~~~~~~~~~~~{.c}
672 * pcd_cipher_transparent_set_iv_<CIPHER_NAME>_<MODE>
673 * ~~~~~~~~~~~~~
674 * Where
675 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
676 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
677 *
Derek Miller16e72292018-10-15 16:14:24 -0500678 * \param[in,out] p_context A structure that contains the previously setup
679 * hardware-specific cipher context
680 * \param[in] p_iv A buffer containing the initialization vecotr
681 * \param[in] iv_length The size in bytes of the contents of `p_iv`
Derek Miller5b3417a2018-10-10 17:55:03 -0500682 *
683 * \retval PSA_SUCCESS
684*/
Derek Miller81133a62018-10-23 14:55:32 -0500685typedef psa_status_t (*pcd_cipher_transparent_set_iv_t)(pcd_cipher_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500686 const uint8_t *p_iv,
687 size_t iv_length);
688
689/** \brief The function prototype for the update operation of transparent-key
690 * block cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500691 *
Derek Miller16e72292018-10-15 16:14:24 -0500692 * Functions that implement the prototype should be named in the following
693 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500694 * ~~~~~~~~~~~~~{.c}
695 * pcd_cipher_transparent_update_<CIPHER_NAME>_<MODE>
696 * ~~~~~~~~~~~~~
697 * Where
698 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
699 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
700 *
Derek Miller16e72292018-10-15 16:14:24 -0500701 * \param[in,out] p_context A hardware-specific structure for the
702 * previously started cipher operation
703 * \param[in] p_input A buffer containing the data to be
704 * encrypted or decrypted
705 * \param[in] input_size The size in bytes of the `p_input` buffer
706 * \param[out] p_output A caller-allocated buffer where the
707 * generated output will be placed
708 * \param[in] output_size The size in bytes of the `p_output` buffer
709 * \param[out] p_output_length After completion, will contain the number
710 * of bytes placed in the `p_output` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500711 *
712 * \retval PSA_SUCCESS
713 */
Derek Miller81133a62018-10-23 14:55:32 -0500714typedef psa_status_t (*pcd_cipher_transparent_update_t)(pcd_cipher_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500715 const uint8_t *p_input,
716 size_t input_size,
717 uint8_t *p_output,
718 size_t output_size,
719 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500720
Derek Miller16e72292018-10-15 16:14:24 -0500721/** \brief The function prototype for the finish operation of transparent-key
722 * block cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500723*
Derek Miller16e72292018-10-15 16:14:24 -0500724 * Functions that implement the prototype should be named in the following
725 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500726 * ~~~~~~~~~~~~~{.c}
727 * pcd_cipher_transparent_finish_<CIPHER_NAME>_<MODE>
728 * ~~~~~~~~~~~~~
729 * Where
730 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
731 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Derek Miller16e72292018-10-15 16:14:24 -0500732 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500733 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500734 * previously started cipher operation
735 * \param[out] p_output A caller-allocated buffer where the generated
736 * output will be placed
737 * \param[in] output_size The size in bytes of the `p_output` buffer
738 * \param[out] p_output_length After completion, will contain the number of
739 * bytes placed in the `p_output` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500740 *
741 * \retval PSA_SUCCESS
742 */
Derek Miller81133a62018-10-23 14:55:32 -0500743typedef psa_status_t (*pcd_cipher_transparent_finish_t)(pcd_cipher_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500744 uint8_t *p_output,
745 size_t output_size,
746 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500747
Derek Miller16e72292018-10-15 16:14:24 -0500748/** \brief The function prototype for the abort operation of transparent-key
749 * block cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500750 *
Derek Miller16e72292018-10-15 16:14:24 -0500751 * Functions that implement the following prototype should be named in the
752 * following convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500753 * ~~~~~~~~~~~~~{.c}
754 * pcd_cipher_transparent_abort_<CIPHER_NAME>_<MODE>
755 * ~~~~~~~~~~~~~
756 * Where
757 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
758 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
759 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500760 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500761 * previously started cipher operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500762 *
763 * \retval PSA_SUCCESS
764 */
Derek Miller81133a62018-10-23 14:55:32 -0500765typedef psa_status_t (*pcd_cipher_transparent_abort_t)(pcd_cipher_transparent_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -0500766
Derek Miller16e72292018-10-15 16:14:24 -0500767/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500768
Derek Miller16e72292018-10-15 16:14:24 -0500769/** \defgroup driver_digest Message Digests
Derek Miller765682c2018-10-22 15:27:27 -0500770 *
771 * Generation and authentication of Message Digests (aka hashes) must be done
772 * in parts using the following sequence:
773 * - `psa_hash_setup_t`
774 * - `psa_hash_update_t`
775 * - ...
776 * - `psa_hash_finish_t`
777 *
778 * If a previously started Message Digest operation needs to be terminated
779 * before the `psa_hash_finish_t` operation is complete, it should be aborted
780 * by the `psa_hash_abort_t`. Failure to do so may result in allocated
781 * resources not being freed or in other undefined behavior.
Derek Miller5b3417a2018-10-10 17:55:03 -0500782 */
Derek Miller16e72292018-10-15 16:14:24 -0500783/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500784
785/** \brief The hardware-specific hash context structure
Derek Miller765682c2018-10-22 15:27:27 -0500786 *
Derek Miller16e72292018-10-15 16:14:24 -0500787 * The contents of this structure are implementation dependent and are
788 * therefore not described here
Derek Miller5b3417a2018-10-10 17:55:03 -0500789 */
Derek Miller81133a62018-10-23 14:55:32 -0500790typedef struct pcd_hash_context_s pcd_hash_context_t;
Derek Miller5b3417a2018-10-10 17:55:03 -0500791
Derek Miller16e72292018-10-15 16:14:24 -0500792/** \brief The function prototype for the start operation of a hash (message
793 * digest) operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500794 *
Derek Miller16e72292018-10-15 16:14:24 -0500795 * Functions that implement the prototype should be named in the following
796 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500797 * ~~~~~~~~~~~~~{.c}
Derek Miller16e72292018-10-15 16:14:24 -0500798 * pcd_hash_<ALGO>_setup
Derek Miller5b3417a2018-10-10 17:55:03 -0500799 * ~~~~~~~~~~~~~
800 * Where `ALGO` is the name of the underlying hash function
801 *
Derek Miller16e72292018-10-15 16:14:24 -0500802 * \param[in,out] p_context A structure that will contain the
803 * hardware-specific hash context
Derek Miller5b3417a2018-10-10 17:55:03 -0500804 *
805 * \retval PSA_SUCCESS Success.
806 */
Derek Miller81133a62018-10-23 14:55:32 -0500807typedef psa_status_t (*pcd_hash_setup_t)(pcd_hash_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -0500808
Derek Miller16e72292018-10-15 16:14:24 -0500809/** \brief The function prototype for the update operation of a hash (message
810 * digest) operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500811 *
Derek Miller16e72292018-10-15 16:14:24 -0500812 * Functions that implement the prototype should be named in the following
813 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500814 * ~~~~~~~~~~~~~{.c}
815 * pcd_hash_<ALGO>_update
816 * ~~~~~~~~~~~~~
817 * Where `ALGO` is the name of the underlying algorithm
818 *
Derek Miller16e72292018-10-15 16:14:24 -0500819 * \param[in,out] p_context A hardware-specific structure for the
820 * previously-established hash operation to be
821 * continued
822 * \param[in] p_input A buffer containing the message to be appended
823 * to the hash operation
824 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500825 */
Derek Miller81133a62018-10-23 14:55:32 -0500826typedef psa_status_t (*pcd_hash_update_t)(pcd_hash_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500827 const uint8_t *p_input,
828 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500829
Derek Miller16e72292018-10-15 16:14:24 -0500830/** \brief The prototype for the finish operation of a hash (message digest)
831 * operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500832 *
Derek Miller16e72292018-10-15 16:14:24 -0500833 * Functions that implement the prototype should be named in the following
834 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500835 * ~~~~~~~~~~~~~{.c}
836 * pcd_hash_<ALGO>_finish
837 * ~~~~~~~~~~~~~
838 * Where `ALGO` is the name of the underlying algorithm
839 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500840 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500841 * previously started hash operation to be
842 * fiinished
843 * \param[out] p_output A buffer where the generated digest will be
844 * placed
845 * \param[in] output_size The size in bytes of the buffer that has been
846 * allocated for the `p_output` buffer
Derek Millerf3d0a562018-10-18 16:41:08 -0500847 * \param[out] p_output_length The number of bytes placed in `p_output` after
848 * success
Derek Miller5b3417a2018-10-10 17:55:03 -0500849 *
850 * \retval PSA_SUCCESS
851 * Success.
852 */
Derek Miller81133a62018-10-23 14:55:32 -0500853typedef psa_status_t (*pcd_hash_finish_t)(pcd_hash_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500854 uint8_t *p_output,
Derek Millerf3d0a562018-10-18 16:41:08 -0500855 size_t output_size,
856 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500857
Derek Miller16e72292018-10-15 16:14:24 -0500858/** \brief The function prototype for the abort operation of a hash (message
859 * digest) operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500860 *
Derek Miller16e72292018-10-15 16:14:24 -0500861 * Functions that implement the prototype should be named in the following
862 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500863 * ~~~~~~~~~~~~~{.c}
864 * pcd_hash_<ALGO>_abort
865 * ~~~~~~~~~~~~~
866 * Where `ALGO` is the name of the underlying algorithm
867 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500868 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500869 * started hash operation to be aborted
Derek Miller5b3417a2018-10-10 17:55:03 -0500870 */
Derek Miller81133a62018-10-23 14:55:32 -0500871typedef void (*pcd_hash_abort_t)(pcd_hash_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -0500872
Derek Miller16e72292018-10-15 16:14:24 -0500873/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500874
875
876/** \defgroup opaque_asymmetric Opaque Asymmetric Cryptography
Derek Miller765682c2018-10-22 15:27:27 -0500877 *
878 * Since the amount of data that can (or should) be encrypted or signed using
879 * asymmetric keys is limited by the key size, asymmetric key operations using
880 * opaque keys must be done in single function calls.
Derek Miller5b3417a2018-10-10 17:55:03 -0500881 */
Derek Miller16e72292018-10-15 16:14:24 -0500882/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500883
884/**
Derek Miller16e72292018-10-15 16:14:24 -0500885 * \brief A function that signs a hash or short message with a private key
Derek Miller5b3417a2018-10-10 17:55:03 -0500886 *
Derek Miller16e72292018-10-15 16:14:24 -0500887 * \param[in] key_slot Key slot of an asymmetric key pair
888 * \param[in] alg A signature algorithm that is compatible
889 * with the type of `key`
Derek Miller765682c2018-10-22 15:27:27 -0500890 * \param[in] p_hash The hash to sign
Derek Miller16e72292018-10-15 16:14:24 -0500891 * \param[in] hash_length Size of the `p_hash` buffer in bytes
892 * \param[out] p_signature Buffer where the signature is to be written
Derek Millerf3d0a562018-10-18 16:41:08 -0500893 * \param[in] signature_size Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500894 * \param[out] p_signature_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -0500895 * that make up the returned signature value
Derek Miller5b3417a2018-10-10 17:55:03 -0500896 *
897 * \retval PSA_SUCCESS
898 */
Derek Miller16e72292018-10-15 16:14:24 -0500899typedef psa_status_t (*pcd_asymmetric_opaque_sign_t)(psa_key_slot_t key_slot,
900 psa_algorithm_t alg,
901 const uint8_t *p_hash,
902 size_t hash_length,
903 uint8_t *p_signature,
904 size_t signature_size,
905 size_t *p_signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500906
907/**
Derek Miller16e72292018-10-15 16:14:24 -0500908 * \brief A function that verifies the signature a hash or short message using
Derek Miller765682c2018-10-22 15:27:27 -0500909 * an asymmetric public key
Derek Miller5b3417a2018-10-10 17:55:03 -0500910 *
Derek Miller16e72292018-10-15 16:14:24 -0500911 * \param[in] key_slot Key slot of a public key or an asymmetric key
912 * pair
913 * \param[in] alg A signature algorithm that is compatible with
914 * the type of `key`
Derek Miller765682c2018-10-22 15:27:27 -0500915 * \param[in] p_hash The hash whose signature is to be verified
Derek Miller16e72292018-10-15 16:14:24 -0500916 * \param[in] hash_length Size of the `p_hash` buffer in bytes
917 * \param[in] p_signature Buffer containing the signature to verify
918 * \param[in] signature_length Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500919 *
920 * \retval PSA_SUCCESS
921 * The signature is valid.
922 */
Derek Miller16e72292018-10-15 16:14:24 -0500923typedef psa_status_t (*pcd_asymmetric_opaque_verify_t)(psa_key_slot_t key_slot,
924 psa_algorithm_t alg,
925 const uint8_t *p_hash,
926 size_t hash_length,
927 const uint8_t *p_signature,
928 size_t signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500929
930/**
Derek Miller765682c2018-10-22 15:27:27 -0500931 * \brief A function that encrypts a short message with an asymmetric public
932 * key
Derek Miller5b3417a2018-10-10 17:55:03 -0500933 *
Derek Miller16e72292018-10-15 16:14:24 -0500934 * \param[in] key_slot Key slot of a public key or an asymmetric key
935 * pair
936 * \param[in] alg An asymmetric encryption algorithm that is
937 * compatible with the type of `key`
938 * \param[in] p_input The message to encrypt
939 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500940 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -0500941 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -0500942 * If the algorithm does not support a
943 * salt, pass `NULL`.
944 * If the algorithm supports an optional
945 * salt and you do not want to pass a salt,
946 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -0500947 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
948 * supported.
949 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500950 * If `p_salt` is `NULL`, pass 0.
951 * \param[out] p_output Buffer where the encrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -0500952 * be written
953 * \param[in] output_size Size of the `p_output` buffer in bytes
954 * \param[out] p_output_length On success, the number of bytes that make up
955 * the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -0500956 *
957 * \retval PSA_SUCCESS
958 */
Derek Miller16e72292018-10-15 16:14:24 -0500959typedef psa_status_t (*pcd_asymmetric_opaque_encrypt_t)(psa_key_slot_t key_slot,
960 psa_algorithm_t alg,
961 const uint8_t *p_input,
962 size_t input_length,
963 const uint8_t *p_salt,
964 size_t salt_length,
965 uint8_t *p_output,
966 size_t output_size,
967 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500968
969/**
Derek Miller765682c2018-10-22 15:27:27 -0500970 * \brief Decrypt a short message with an asymmetric private key.
Derek Miller5b3417a2018-10-10 17:55:03 -0500971 *
Derek Miller16e72292018-10-15 16:14:24 -0500972 * \param[in] key_slot Key slot of an asymmetric key pair
973 * \param[in] alg An asymmetric encryption algorithm that is
974 * compatible with the type of `key`
975 * \param[in] p_input The message to decrypt
976 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500977 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -0500978 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -0500979 * If the algorithm does not support a
980 * salt, pass `NULL`.
981 * If the algorithm supports an optional
982 * salt and you do not want to pass a salt,
983 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -0500984 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
985 * supported.
986 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500987 * If `p_salt` is `NULL`, pass 0.
988 * \param[out] p_output Buffer where the decrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -0500989 * be written
990 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500991 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -0500992 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -0500993 *
994 * \retval PSA_SUCCESS
995 */
Derek Miller16e72292018-10-15 16:14:24 -0500996typedef psa_status_t (*pcd_asymmetric_opaque_decrypt_t)(psa_key_slot_t key_slot,
997 psa_algorithm_t alg,
998 const uint8_t *p_input,
999 size_t input_length,
1000 const uint8_t *p_salt,
1001 size_t salt_length,
1002 uint8_t *p_output,
1003 size_t output_size,
1004 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001005
1006/**
Derek Miller16e72292018-10-15 16:14:24 -05001007 * \brief A struct containing all of the function pointers needed to implement
1008 * asymmetric cryptographic operations using opaque keys.
Derek Miller5b3417a2018-10-10 17:55:03 -05001009 *
Derek Miller16e72292018-10-15 16:14:24 -05001010 * PSA Crypto API implementations should populate instances of the table as
1011 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001012 *
1013 * If one of the functions is not implemented, it should be set to NULL.
1014 */
1015struct pcd_asymmetric_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -05001016 /** Function that performs the asymmetric sign operation */
1017 pcd_asymmetric_opaque_sign_t *p_sign;
1018 /** Function that performs the asymmetric verify operation */
1019 pcd_asymmetric_opaque_verify_t *p_verify;
1020 /** Function that performs the asymmetric encrypt operation */
1021 pcd_asymmetric_opaque_encrypt_t *p_encrypt;
1022 /** Function that performs the asymmetric decrypt operation */
1023 pcd_asymmetric_opaque_decrypt_t *p_decrypt;
Derek Miller5b3417a2018-10-10 17:55:03 -05001024};
1025
Derek Miller16e72292018-10-15 16:14:24 -05001026/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001027
1028/** \defgroup transparent_asymmetric Transparent Asymmetric Cryptography
Derek Miller765682c2018-10-22 15:27:27 -05001029 *
1030 * Since the amount of data that can (or should) be encrypted or signed using
1031 * asymmetric keys is limited by the key size, asymmetric key operations using
1032 * transparent keys must be done in single function calls.
Derek Miller5b3417a2018-10-10 17:55:03 -05001033 */
Derek Miller16e72292018-10-15 16:14:24 -05001034/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001035
1036
1037/**
Derek Miller16e72292018-10-15 16:14:24 -05001038 * \brief A function that signs a hash or short message with a transparent
Derek Miller765682c2018-10-22 15:27:27 -05001039 * asymmetric private key
Derek Miller5b3417a2018-10-10 17:55:03 -05001040 *
Derek Miller16e72292018-10-15 16:14:24 -05001041 * Functions that implement the prototype should be named in the following
1042 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001043 * ~~~~~~~~~~~~~{.c}
1044 * pcd_asymmetric_<ALGO>_sign
1045 * ~~~~~~~~~~~~~
1046 * Where `ALGO` is the name of the signing algorithm
1047 *
Derek Miller16e72292018-10-15 16:14:24 -05001048 * \param[in] p_key A buffer containing the private key
1049 * material
1050 * \param[in] key_size The size in bytes of the `p_key` data
1051 * \param[in] alg A signature algorithm that is compatible
1052 * with the type of `p_key`
1053 * \param[in] p_hash The hash or message to sign
1054 * \param[in] hash_length Size of the `p_hash` buffer in bytes
1055 * \param[out] p_signature Buffer where the signature is to be written
1056 * \param[in] signature_size Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001057 * \param[out] p_signature_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001058 * that make up the returned signature value
Derek Miller5b3417a2018-10-10 17:55:03 -05001059 *
1060 * \retval PSA_SUCCESS
1061 */
Derek Miller16e72292018-10-15 16:14:24 -05001062typedef psa_status_t (*pcd_asymmetric_transparent_sign_t)(const uint8_t *p_key,
1063 size_t key_size,
1064 psa_algorithm_t alg,
1065 const uint8_t *p_hash,
1066 size_t hash_length,
1067 uint8_t *p_signature,
1068 size_t signature_size,
1069 size_t *p_signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001070
1071/**
Derek Miller16e72292018-10-15 16:14:24 -05001072 * \brief A function that verifies the signature a hash or short message using
Derek Miller765682c2018-10-22 15:27:27 -05001073 * a transparent asymmetric public key
Derek Miller5b3417a2018-10-10 17:55:03 -05001074 *
Derek Miller16e72292018-10-15 16:14:24 -05001075 * Functions that implement the prototype should be named in the following
1076 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001077 * ~~~~~~~~~~~~~{.c}
1078 * pcd_asymmetric_<ALGO>_verify
1079 * ~~~~~~~~~~~~~
1080 * Where `ALGO` is the name of the signing algorithm
1081 *
Derek Miller16e72292018-10-15 16:14:24 -05001082 * \param[in] p_key A buffer containing the public key material
1083 * \param[in] key_size The size in bytes of the `p_key` data
1084 * \param[in] alg A signature algorithm that is compatible with
1085 * the type of `key`
1086 * \param[in] p_hash The hash or message whose signature is to be
1087 * verified
1088 * \param[in] hash_length Size of the `p_hash` buffer in bytes
1089 * \param[in] p_signature Buffer containing the signature to verify
1090 * \param[in] signature_length Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001091 *
1092 * \retval PSA_SUCCESS
1093 * The signature is valid.
1094 */
Derek Miller16e72292018-10-15 16:14:24 -05001095typedef psa_status_t (*pcd_asymmetric_transparent_verify_t)(const uint8_t *p_key,
1096 size_t key_size,
1097 psa_algorithm_t alg,
1098 const uint8_t *p_hash,
1099 size_t hash_length,
1100 const uint8_t *p_signature,
1101 size_t signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001102
1103/**
Derek Miller765682c2018-10-22 15:27:27 -05001104 * \brief A function that encrypts a short message with a transparent
1105 * asymmetric public key
Derek Miller5b3417a2018-10-10 17:55:03 -05001106 *
Derek Miller16e72292018-10-15 16:14:24 -05001107 * Functions that implement the prototype should be named in the following
1108 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001109 * ~~~~~~~~~~~~~{.c}
1110 * pcd_asymmetric_<ALGO>_encrypt
1111 * ~~~~~~~~~~~~~
1112 * Where `ALGO` is the name of the encryption algorithm
1113 *
Derek Miller16e72292018-10-15 16:14:24 -05001114 * \param[in] p_key A buffer containing the public key material
1115 * \param[in] key_size The size in bytes of the `p_key` data
1116 * \param[in] alg An asymmetric encryption algorithm that is
1117 * compatible with the type of `key`
1118 * \param[in] p_input The message to encrypt
1119 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001120 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -05001121 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -05001122 * If the algorithm does not support a
Derek Miller16e72292018-10-15 16:14:24 -05001123 * salt, pass `NULL`
Derek Miller5b3417a2018-10-10 17:55:03 -05001124 * If the algorithm supports an optional
1125 * salt and you do not want to pass a salt,
1126 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -05001127 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1128 * supported.
1129 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001130 * If `p_salt` is `NULL`, pass 0.
1131 * \param[out] p_output Buffer where the encrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -05001132 * be written
1133 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001134 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001135 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -05001136 *
1137 * \retval PSA_SUCCESS
1138 */
Derek Miller16e72292018-10-15 16:14:24 -05001139typedef psa_status_t (*pcd_asymmetric_transparent_encrypt_t)(const uint8_t *p_key,
Derek Miller5b3417a2018-10-10 17:55:03 -05001140 size_t key_size,
1141 psa_algorithm_t alg,
1142 const uint8_t *p_input,
1143 size_t input_length,
1144 const uint8_t *p_salt,
1145 size_t salt_length,
1146 uint8_t *p_output,
1147 size_t output_size,
Derek Miller16e72292018-10-15 16:14:24 -05001148 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001149
1150/**
Derek Miller765682c2018-10-22 15:27:27 -05001151 * \brief Decrypt a short message with a transparent asymmetric private key
Derek Miller5b3417a2018-10-10 17:55:03 -05001152 *
Derek Miller16e72292018-10-15 16:14:24 -05001153 * Functions that implement the prototype should be named in the following
1154 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001155 * ~~~~~~~~~~~~~{.c}
1156 * pcd_asymmetric_<ALGO>_decrypt
1157 * ~~~~~~~~~~~~~
1158 * Where `ALGO` is the name of the encryption algorithm
1159 *
Derek Miller16e72292018-10-15 16:14:24 -05001160 * \param[in] p_key A buffer containing the private key material
1161 * \param[in] key_size The size in bytes of the `p_key` data
1162 * \param[in] alg An asymmetric encryption algorithm that is
1163 * compatible with the type of `key`
1164 * \param[in] p_input The message to decrypt
1165 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001166 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -05001167 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -05001168 * If the algorithm does not support a
1169 * salt, pass `NULL`.
1170 * If the algorithm supports an optional
1171 * salt and you do not want to pass a salt,
1172 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -05001173 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1174 * supported
1175 * \param[in] salt_length Size of the `p_salt` buffer in bytes
1176 * If `p_salt` is `NULL`, pass 0
Derek Miller5b3417a2018-10-10 17:55:03 -05001177 * \param[out] p_output Buffer where the decrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -05001178 * be written
1179 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001180 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001181 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -05001182 *
1183 * \retval PSA_SUCCESS
1184 */
Derek Miller16e72292018-10-15 16:14:24 -05001185typedef psa_status_t (*pcd_asymmetric_transparent_decrypt_t)(const uint8_t *p_key,
1186 size_t key_size,
1187 psa_algorithm_t alg,
1188 const uint8_t *p_input,
1189 size_t input_length,
1190 const uint8_t *p_salt,
1191 size_t salt_length,
1192 uint8_t *p_output,
1193 size_t output_size,
1194 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001195
Derek Miller16e72292018-10-15 16:14:24 -05001196/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001197
1198/** \defgroup aead_opaque AEAD Opaque
Derek Miller765682c2018-10-22 15:27:27 -05001199 * Authenticated Encryption with Additional Data (AEAD) operations with opaque
1200 * keys must be done in one function call. While this creates a burden for
1201 * implementers as there must be sufficient space in memory for the entire
1202 * message, it prevents decrypted data from being made available before the
1203 * authentication operation is complete and the data is known to be authentic.
Derek Miller5b3417a2018-10-10 17:55:03 -05001204 */
Derek Miller16e72292018-10-15 16:14:24 -05001205/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001206
Derek Miller16e72292018-10-15 16:14:24 -05001207/** \brief Process an authenticated encryption operation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -05001208 *
Derek Miller16e72292018-10-15 16:14:24 -05001209 * \param[in] key_slot Slot containing the key to use.
1210 * \param[in] algorithm The AEAD algorithm to compute
1211 * (\c PSA_ALG_XXX value such that
1212 * #PSA_ALG_IS_AEAD(`alg`) is true)
1213 * \param[in] p_nonce Nonce or IV to use
1214 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
1215 * \param[in] p_additional_data Additional data that will be
1216 * authenticated but not encrypted
1217 * \param[in] additional_data_length Size of `p_additional_data` in bytes
1218 * \param[in] p_plaintext Data that will be authenticated and
1219 * encrypted
1220 * \param[in] plaintext_length Size of `p_plaintext` in bytes
1221 * \param[out] p_ciphertext Output buffer for the authenticated and
1222 * encrypted data. The additional data is
1223 * not part of this output. For algorithms
1224 * where the encrypted data and the
1225 * authentication tag are defined as
1226 * separate outputs, the authentication
1227 * tag is appended to the encrypted data.
1228 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
1229 * bytes
1230 * \param[out] p_ciphertext_length On success, the size of the output in
1231 * the `p_ciphertext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001232 *
1233 * \retval #PSA_SUCCESS
1234 * Success.
1235 */
Derek Miller16e72292018-10-15 16:14:24 -05001236typedef psa_status_t (*psa_aead_opaque_encrypt_t)(psa_key_slot_t key_slot,
Derek Miller5b3417a2018-10-10 17:55:03 -05001237 psa_algorithm_t algorithm,
1238 const uint8_t *p_nonce,
1239 size_t nonce_length,
1240 const uint8_t *p_additional_data,
1241 size_t additional_data_length,
1242 const uint8_t *p_plaintext,
1243 size_t plaintext_length,
1244 uint8_t *p_ciphertext,
1245 size_t ciphertext_size,
1246 size_t *p_ciphertext_length);
1247
Derek Miller16e72292018-10-15 16:14:24 -05001248/** Process an authenticated decryption operation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -05001249 *
Derek Miller16e72292018-10-15 16:14:24 -05001250 * \param[in] key_slot Slot containing the key to use
1251 * \param[in] algorithm The AEAD algorithm to compute
1252 * (\c PSA_ALG_XXX value such that
1253 * #PSA_ALG_IS_AEAD(`alg`) is true)
1254 * \param[in] p_nonce Nonce or IV to use
1255 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
1256 * \param[in] p_additional_data Additional data that has been
1257 * authenticated but not encrypted
1258 * \param[in] additional_data_length Size of `p_additional_data` in bytes
1259 * \param[in] p_ciphertext Data that has been authenticated and
1260 * encrypted.
1261 * For algorithms where the encrypted data
1262 * and the authentication tag are defined
1263 * as separate inputs, the buffer must
1264 * contain the encrypted data followed by
1265 * the authentication tag.
1266 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
1267 * \param[out] p_plaintext Output buffer for the decrypted data
1268 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
1269 * bytes
1270 * \param[out] p_plaintext_length On success, the size of the output in
1271 * the `p_plaintext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001272 *
1273 * \retval #PSA_SUCCESS
1274 * Success.
1275 */
Derek Miller16e72292018-10-15 16:14:24 -05001276typedef psa_status_t (*psa_aead_opaque_decrypt_t)(psa_key_slot_t key_slot,
1277 psa_algorithm_t algorithm,
1278 const uint8_t *p_nonce,
1279 size_t nonce_length,
1280 const uint8_t *p_additional_data,
1281 size_t additional_data_length,
1282 const uint8_t *p_ciphertext,
1283 size_t ciphertext_length,
1284 uint8_t *p_plaintext,
1285 size_t plaintext_size,
1286 size_t *p_plaintext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001287
1288/**
Derek Miller16e72292018-10-15 16:14:24 -05001289 * \brief A struct containing all of the function pointers needed to implement
1290 * Authenticated Encryption with Additional Data operations using opaque keys
Derek Miller5b3417a2018-10-10 17:55:03 -05001291 *
Derek Miller16e72292018-10-15 16:14:24 -05001292 * PSA Crypto API implementations should populate instances of the table as
1293 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001294 *
1295 * If one of the functions is not implemented, it should be set to NULL.
1296 */
1297struct psa_aead_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -05001298 /** Function that performs the AEAD encrypt operation */
1299 psa_aead_opaque_encrypt_t *p_encrypt;
1300 /** Function that performs the AEAD decrypt operation */
1301 psa_aead_opaque_decrypt_t *p_decrypt;
Derek Miller5b3417a2018-10-10 17:55:03 -05001302};
Derek Miller16e72292018-10-15 16:14:24 -05001303/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001304
1305/** \defgroup aead_transparent AEAD Transparent
Derek Miller765682c2018-10-22 15:27:27 -05001306 *
1307 * Authenticated Encryption with Additional Data (AEAD) operations with
1308 * transparent keys must be done in one function call. While this creates a
1309 * burden for implementers as there must be sufficient space in memory for the
1310 * entire message, it prevents decrypted data from being made available before
1311 * the authentication operation is complete and the data is known to be
1312 * authentic.
Derek Miller5b3417a2018-10-10 17:55:03 -05001313 */
Derek Miller16e72292018-10-15 16:14:24 -05001314/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001315
Derek Miller765682c2018-10-22 15:27:27 -05001316/** Process an authenticated encryption operation using an opaque key.
Derek Miller5b3417a2018-10-10 17:55:03 -05001317 *
Derek Miller16e72292018-10-15 16:14:24 -05001318 * Functions that implement the prototype should be named in the following
1319 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001320 * ~~~~~~~~~~~~~{.c}
1321 * pcd_aead_<ALGO>_encrypt
1322 * ~~~~~~~~~~~~~
1323 * Where `ALGO` is the name of the AEAD algorithm
1324 *
Derek Miller16e72292018-10-15 16:14:24 -05001325 * \param[in] p_key A pointer to the key material
1326 * \param[in] key_length The size in bytes of the key material
1327 * \param[in] alg The AEAD algorithm to compute
1328 * (\c PSA_ALG_XXX value such that
1329 * #PSA_ALG_IS_AEAD(`alg`) is true)
1330 * \param[in] nonce Nonce or IV to use
1331 * \param[in] nonce_length Size of the `nonce` buffer in bytes
1332 * \param[in] additional_data Additional data that will be MACed
1333 * but not encrypted.
1334 * \param[in] additional_data_length Size of `additional_data` in bytes
1335 * \param[in] plaintext Data that will be MACed and
1336 * encrypted.
1337 * \param[in] plaintext_length Size of `plaintext` in bytes
1338 * \param[out] ciphertext Output buffer for the authenticated and
1339 * encrypted data. The additional data is
1340 * not part of this output. For algorithms
1341 * where the encrypted data and the
1342 * authentication tag are defined as
1343 * separate outputs, the authentication
1344 * tag is appended to the encrypted data.
1345 * \param[in] ciphertext_size Size of the `ciphertext` buffer in
1346 * bytes
1347 * This must be at least
1348 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(`alg`,
1349 * `plaintext_length`).
1350 * \param[out] ciphertext_length On success, the size of the output in
1351 * the `ciphertext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001352 *
1353 * \retval #PSA_SUCCESS
1354
1355 */
Derek Miller16e72292018-10-15 16:14:24 -05001356typedef psa_status_t (*psa_aead_transparent_encrypt_t)(const uint8_t *p_key,
1357 size_t key_length,
1358 psa_algorithm_t alg,
1359 const uint8_t *nonce,
1360 size_t nonce_length,
1361 const uint8_t *additional_data,
1362 size_t additional_data_length,
1363 const uint8_t *plaintext,
1364 size_t plaintext_length,
1365 uint8_t *ciphertext,
1366 size_t ciphertext_size,
1367 size_t *ciphertext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001368
Derek Miller765682c2018-10-22 15:27:27 -05001369/** Process an authenticated decryption operation using an opaque key.
Derek Miller5b3417a2018-10-10 17:55:03 -05001370 *
Derek Miller16e72292018-10-15 16:14:24 -05001371 * Functions that implement the prototype should be named in the following
1372 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001373 * ~~~~~~~~~~~~~{.c}
1374 * pcd_aead_<ALGO>_decrypt
1375 * ~~~~~~~~~~~~~
1376 * Where `ALGO` is the name of the AEAD algorithm
Derek Miller16e72292018-10-15 16:14:24 -05001377 * \param[in] p_key A pointer to the key material
1378 * \param[in] key_length The size in bytes of the key material
1379 * \param[in] alg The AEAD algorithm to compute
1380 * (\c PSA_ALG_XXX value such that
1381 * #PSA_ALG_IS_AEAD(`alg`) is true)
1382 * \param[in] nonce Nonce or IV to use
1383 * \param[in] nonce_length Size of the `nonce` buffer in bytes
1384 * \param[in] additional_data Additional data that has been MACed
1385 * but not encrypted
1386 * \param[in] additional_data_length Size of `additional_data` in bytes
1387 * \param[in] ciphertext Data that has been MACed and
1388 * encrypted
1389 * For algorithms where the encrypted data
1390 * and the authentication tag are defined
1391 * as separate inputs, the buffer must
1392 * contain the encrypted data followed by
1393 * the authentication tag.
1394 * \param[in] ciphertext_length Size of `ciphertext` in bytes
1395 * \param[out] plaintext Output buffer for the decrypted data
1396 * \param[in] plaintext_size Size of the `plaintext` buffer in
1397 * bytes
1398 * This must be at least
1399 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(`alg`,
1400 * `ciphertext_length`).
1401 * \param[out] plaintext_length On success, the size of the output
1402 * in the \b plaintext buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001403 *
1404 * \retval #PSA_SUCCESS
1405 * Success.
1406 */
Derek Miller16e72292018-10-15 16:14:24 -05001407typedef psa_status_t (*psa_aead_transparent_decrypt_t)(const uint8_t *p_key,
1408 size_t key_length,
1409 psa_algorithm_t alg,
1410 const uint8_t *nonce,
1411 size_t nonce_length,
1412 const uint8_t *additional_data,
1413 size_t additional_data_length,
1414 const uint8_t *ciphertext,
1415 size_t ciphertext_length,
1416 uint8_t *plaintext,
1417 size_t plaintext_size,
1418 size_t *plaintext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001419
Derek Miller16e72292018-10-15 16:14:24 -05001420/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001421
1422
Derek Miller16e72292018-10-15 16:14:24 -05001423/** \defgroup driver_rng Entropy Generation
Derek Miller5b3417a2018-10-10 17:55:03 -05001424 */
Derek Miller16e72292018-10-15 16:14:24 -05001425/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001426
1427/** \brief A hardware-specific structure for a entropy providing hardware
1428 */
Derek Miller81133a62018-10-23 14:55:32 -05001429typedef struct pcd_entropy_context_s pcd_entropy_context_t;
Derek Miller5b3417a2018-10-10 17:55:03 -05001430
1431/** \brief Initialize an entropy driver
1432 *
1433 *
Derek Miller16e72292018-10-15 16:14:24 -05001434 * \param[in,out] p_context A hardware-specific structure
1435 * containing any context information for
1436 * the implementation
Derek Miller5b3417a2018-10-10 17:55:03 -05001437 *
1438 * \retval PSA_SUCCESS
1439 */
Derek Miller81133a62018-10-23 14:55:32 -05001440typedef psa_status_t (*pcd_entropy_init_t)(pcd_entropy_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -05001441
1442/** \brief Get a specified number of bytes from the entropy source
1443 *
Derek Miller16e72292018-10-15 16:14:24 -05001444 * It retrives `buffer_size` bytes of data from the entropy source. The entropy
1445 * source will always fill the provided buffer to its full size, however, most
1446 * entropy sources have biases, and the actual amount of entropy contained in
1447 * the buffer will be less than the number of bytes.
1448 * The driver will return the actual number of bytes of entropy placed in the
1449 * buffer in `p_received_entropy_bytes`.
1450 * A PSA Crypto API implementation will likely feed the output of this function
1451 * into a Digital Random Bit Generator (DRBG), and typically has a minimum
1452 * amount of entropy that it needs.
1453 * To accomplish this, the PSA Crypto implementation should be designed to call
1454 * this function multiple times until it has received the required amount of
1455 * entropy from the entropy source.
Derek Miller5b3417a2018-10-10 17:55:03 -05001456 *
Derek Miller16e72292018-10-15 16:14:24 -05001457 * \param[in,out] p_context A hardware-specific structure
1458 * containing any context information
1459 * for the implementation
1460 * \param[out] p_buffer A caller-allocated buffer for the
1461 * retrieved bytes to be placed in
1462 * \param[in] buffer_size The allocated size of `p_buffer`
1463 * \param[out] p_received_entropy_bytes The amount of entropy (in bytes)
1464 * actually provided in `p_buffer`
Derek Miller5b3417a2018-10-10 17:55:03 -05001465 *
1466 * \retval PSA_SUCCESS
1467 */
Derek Miller81133a62018-10-23 14:55:32 -05001468typedef psa_status_t (*pcd_entropy_get_bytes_t)(pcd_entropy_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -05001469 uint8_t *p_buffer,
1470 uint32_t buffer_size,
1471 uint32_t *p_received_entropy_bytes);
Derek Miller5b3417a2018-10-10 17:55:03 -05001472
1473/**
Derek Miller16e72292018-10-15 16:14:24 -05001474 * \brief A struct containing all of the function pointers needed to interface
1475 * to an entropy source
Derek Miller5b3417a2018-10-10 17:55:03 -05001476 *
Derek Miller16e72292018-10-15 16:14:24 -05001477 * PSA Crypto API implementations should populate instances of the table as
1478 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001479 *
1480 * If one of the functions is not implemented, it should be set to NULL.
1481 */
1482struct pcd_entropy_t {
Derek Miller16e72292018-10-15 16:14:24 -05001483 /** Function that performs initialization for the entropy source */
1484 pcd_entropy_init_t *p_init;
1485 /** Function that performs the get_bytes operation for the entropy source
1486 */
1487 pcd_entropy_get_bytes_t *p_get_bytes;
Derek Miller5b3417a2018-10-10 17:55:03 -05001488};
Derek Miller16e72292018-10-15 16:14:24 -05001489/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001490
Derek Miller16e72292018-10-15 16:14:24 -05001491/** \defgroup driver_key_management Key Management
Derek Miller765682c2018-10-22 15:27:27 -05001492 * Currently, key management is limited to importing keys in the clear,
1493 * destroying keys, and exporting keys in the clear.
1494 * Whether a key may be exported is determined by the key policies in place
1495 * on the key slot.
Derek Miller5b3417a2018-10-10 17:55:03 -05001496 */
Derek Miller16e72292018-10-15 16:14:24 -05001497/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001498
Derek Miller16e72292018-10-15 16:14:24 -05001499/** \brief Import a key in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001500 *
1501 * This function can support any output from psa_export_key(). Refer to the
1502 * documentation of psa_export_key() for the format for each key type.
1503 *
Derek Miller81133a62018-10-23 14:55:32 -05001504 * \param[in] key_slot Slot where the key will be stored
1505 * This must be a valid slot for a key of the chosen
1506 * type. It must be unoccupied.
1507 * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value)
1508 * \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value)
1509 * \param[in] usage The allowed uses of the key
1510 * \param[in] p_data Buffer containing the key data
1511 * \param[in] data_length Size of the `data` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001512 *
1513 * \retval #PSA_SUCCESS
1514 * Success.
1515 */
Derek Miller16e72292018-10-15 16:14:24 -05001516typedef psa_status_t (*pcd_opaque_import_key_t)(psa_key_slot_t key_slot,
1517 psa_key_type_t type,
Derek Miller81133a62018-10-23 14:55:32 -05001518 psa_algorithm_t algorithm,
1519 psa_key_usage_t usage,
Derek Miller16e72292018-10-15 16:14:24 -05001520 const uint8_t *p_data,
1521 size_t data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001522
1523/**
Derek Miller16e72292018-10-15 16:14:24 -05001524 * \brief Destroy a key and restore the slot to its default state
Derek Miller5b3417a2018-10-10 17:55:03 -05001525 *
1526 * This function destroys the content of the key slot from both volatile
1527 * memory and, if applicable, non-volatile storage. Implementations shall
1528 * make a best effort to ensure that any previous content of the slot is
1529 * unrecoverable.
1530 *
1531 * This function also erases any metadata such as policies. It returns the
1532 * specified slot to its default state.
1533 *
Derek Miller16e72292018-10-15 16:14:24 -05001534 * \param[in] key_slot The key slot to erase.
Derek Miller5b3417a2018-10-10 17:55:03 -05001535 *
1536 * \retval #PSA_SUCCESS
1537 * The slot's content, if any, has been erased.
1538 */
Derek Miller16e72292018-10-15 16:14:24 -05001539typedef psa_status_t (*pcd_destroy_key_t)(psa_key_slot_t key);
Derek Miller5b3417a2018-10-10 17:55:03 -05001540
1541/**
Derek Miller16e72292018-10-15 16:14:24 -05001542 * \brief Export a key in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001543 *
1544 * The output of this function can be passed to psa_import_key() to
1545 * create an equivalent object.
1546 *
Derek Miller16e72292018-10-15 16:14:24 -05001547 * If a key is created with `psa_import_key()` and then exported with
Derek Miller5b3417a2018-10-10 17:55:03 -05001548 * this function, it is not guaranteed that the resulting data is
1549 * identical: the implementation may choose a different representation
1550 * of the same key if the format permits it.
1551 *
1552 * For standard key types, the output format is as follows:
1553 *
1554 * - For symmetric keys (including MAC keys), the format is the
1555 * raw bytes of the key.
1556 * - For DES, the key data consists of 8 bytes. The parity bits must be
1557 * correct.
1558 * - For Triple-DES, the format is the concatenation of the
1559 * two or three DES keys.
1560 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
1561 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1562 * as RSAPrivateKey.
1563 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
1564 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
1565 *
Derek Miller16e72292018-10-15 16:14:24 -05001566 * \param[in] key Slot whose content is to be exported. This must
Derek Miller5b3417a2018-10-10 17:55:03 -05001567 * be an occupied key slot.
1568 * \param[out] p_data Buffer where the key data is to be written.
Derek Miller16e72292018-10-15 16:14:24 -05001569 * \param[in] data_size Size of the `p_data` buffer in bytes.
Derek Miller5b3417a2018-10-10 17:55:03 -05001570 * \param[out] p_data_length On success, the number of bytes
1571 * that make up the key data.
1572 *
1573 * \retval #PSA_SUCCESS
1574 * \retval #PSA_ERROR_EMPTY_SLOT
1575 * \retval #PSA_ERROR_NOT_PERMITTED
1576 * \retval #PSA_ERROR_NOT_SUPPORTED
1577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1578 * \retval #PSA_ERROR_HARDWARE_FAILURE
1579 * \retval #PSA_ERROR_TAMPERING_DETECTED
1580 */
Derek Miller16e72292018-10-15 16:14:24 -05001581typedef psa_status_t (*pcd_export_key_t)(psa_key_slot_t key,
1582 uint8_t *p_data,
1583 size_t data_size,
1584 size_t *p_data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001585
1586/**
Derek Miller16e72292018-10-15 16:14:24 -05001587 * \brief Export a public key or the public part of a key pair in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001588 *
1589 * The output of this function can be passed to psa_import_key() to
1590 * create an object that is equivalent to the public key.
1591 *
1592 * For standard key types, the output format is as follows:
1593 *
1594 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1595 * the format is the DER representation of the public key defined by RFC 5280
1596 * as SubjectPublicKeyInfo.
1597 *
Derek Miller16e72292018-10-15 16:14:24 -05001598 * \param[in] key_slot Slot whose content is to be exported. This must
Derek Miller5b3417a2018-10-10 17:55:03 -05001599 * be an occupied key slot.
1600 * \param[out] p_data Buffer where the key data is to be written.
Derek Miller16e72292018-10-15 16:14:24 -05001601 * \param[in] data_size Size of the `data` buffer in bytes.
Derek Miller5b3417a2018-10-10 17:55:03 -05001602 * \param[out] p_data_length On success, the number of bytes
1603 * that make up the key data.
1604 *
1605 * \retval #PSA_SUCCESS
1606 */
Derek Miller16e72292018-10-15 16:14:24 -05001607typedef psa_status_t (*pcd_export_public_key_t)(psa_key_slot_t key,
1608 uint8_t *p_data,
1609 size_t data_size,
1610 size_t *p_data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001611
1612/**
Derek Miller16e72292018-10-15 16:14:24 -05001613 * \brief A struct containing all of the function pointers needed to for key
1614 * management using opaque keys
Derek Miller5b3417a2018-10-10 17:55:03 -05001615 *
Derek Miller16e72292018-10-15 16:14:24 -05001616 * PSA Crypto API implementations should populate instances of the table as
1617 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001618 *
1619 * If one of the functions is not implemented, it should be set to NULL.
1620 */
1621struct pcd_key_management_t {
Derek Miller16e72292018-10-15 16:14:24 -05001622 /** Function that performs the key import operation */
1623 pcd_opaque_import_key_t *p_import;
1624 /** Function that performs the key destroy operation */
1625 pcd_destroy_key_t *p_destroy;
1626 /** Function that performs the key export operation */
1627 pcd_export_key_t *p_export;
1628 /** Function that perforsm the public key export operation */
1629 pcd_export_public_key_t *p_export_public;
Derek Miller5b3417a2018-10-10 17:55:03 -05001630};
1631
Derek Miller16e72292018-10-15 16:14:24 -05001632/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001633
Derek Miller16e72292018-10-15 16:14:24 -05001634/** \defgroup driver_derivation Key Derivation and Agreement
Derek Miller16e72292018-10-15 16:14:24 -05001635 * Key derivation is the process of generating new key material using an
1636 * existing key and additional parameters, iterating through a basic
1637 * cryptographic function, such as a hash.
1638 * Key agreement is a part of cryptographic protocols that allows two parties
1639 * to agree on the same key value, but starting from different original key
1640 * material.
1641 * The flows are similar, and the PSA Crypto Driver API uses the same functions
1642 * for both of the flows.
Derek Miller5b3417a2018-10-10 17:55:03 -05001643 *
Derek Miller16e72292018-10-15 16:14:24 -05001644 * There are two different final functions for the flows,
1645 * `pcd_key_derivation_derive` and `pcd_key_derivation_export`.
1646 * `pcd_key_derivation_derive` is used when the key material should be placed
1647 * in a slot on the hardware and not exposed to the caller.
1648 * `pcd_key_derivation_export` is used when the key material should be returned
1649 * to the PSA Cryptographic API implementation.
1650 *
1651 * Different key derivation algorithms require a different number of inputs.
1652 * Instead of having an API that takes as input variable length arrays, which
1653 * can be problemmatic to manage on embedded platforms, the inputs are passed
1654 * to the driver via a function, `pcd_key_derivation_collateral`, that is
1655 * called multiple times with different `collateral_id`s. Thus, for a key
1656 * derivation algorithm that required 3 paramter inputs, the flow would look
1657 * something like:
1658 * ~~~~~~~~~~~~~{.c}
1659 * pcd_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1660 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_0,
1661 * p_collateral_0,
1662 * collateral_0_size);
1663 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_1,
1664 * p_collateral_1,
1665 * collateral_1_size);
1666 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_2,
1667 * p_collateral_2,
1668 * collateral_2_size);
1669 * pcd_key_derivation_derive();
1670 * ~~~~~~~~~~~~~
1671 *
1672 * key agreement example:
1673 * ~~~~~~~~~~~~~{.c}
1674 * pcd_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1675 * pcd_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1676 * pcd_key_derivation_export(p_session_key,
1677 * session_key_size,
1678 * &session_key_length);
1679 * ~~~~~~~~~~~~~
1680 */
Derek Miller765682c2018-10-22 15:27:27 -05001681/**@{*/
Derek Miller16e72292018-10-15 16:14:24 -05001682
Derek Miller765682c2018-10-22 15:27:27 -05001683/** \brief The hardware-specific key derivation context structure
1684 *
1685 * The contents of this structure are implementation dependent and are
1686 * therefore not described here
1687 */
Derek Miller81133a62018-10-23 14:55:32 -05001688typedef struct pcd_key_derivation_context_s pcd_key_derivation_context_t;
Derek Miller16e72292018-10-15 16:14:24 -05001689
1690/** \brief Set up a key derivation operation by specifying the algorithm and
1691 * the source key sot
1692 *
1693 * \param[in,out] p_context A hardware-specific structure containing any
1694 * context information for the implementation
1695 * \param[in] kdf_alg The algorithm to be used for the key derivation
1696 * \param[in] souce_key The key to be used as the source material for the
1697 * key derivation
1698 *
1699 * \retval PSA_SUCCESS
1700 */
Derek Miller81133a62018-10-23 14:55:32 -05001701typedef psa_status_t (*pcd_key_derivation_setup_t)(pcd_key_derivation_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -05001702 psa_algorithm_t kdf_alg,
1703 psa_key_slot_t source_key);
1704
1705/** \brief Provide collateral (parameters) needed for a key derivation or key
1706 * agreement operation
1707 *
1708 * Since many key derivation algorithms require multiple parameters, it is
1709 * expeced that this function may be called multiple times for the same
1710 * operation, each with a different algorithm-specific `collateral_id`
1711 *
1712 * \param[in,out] p_context A hardware-specific structure containing any
1713 * context information for the implementation
1714 * \param[in] collateral_id An ID for the collateral being provided
1715 * \param[in] p_collateral A buffer containing the collateral data
1716 * \param[in] collateral_size The size in bytes of the collateral
1717 *
1718 * \retval PSA_SUCCESS
1719 */
Derek Miller81133a62018-10-23 14:55:32 -05001720typedef psa_status_t (*pcd_key_derivation_collateral_t)(pcd_key_derivation_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -05001721 uint32_t collateral_id,
Derek Miller81133a62018-10-23 14:55:32 -05001722 const uint8_t *p_collateral,
Derek Miller16e72292018-10-15 16:14:24 -05001723 size_t collateral_size);
1724
1725/** \brief Perform the final key derivation step and place the generated key
1726 * material in a slot
1727 * \param[in,out] p_context A hardware-specific structure containing any
1728 * context information for the implementation
1729 * \param[in] dest_key The slot where the generated key material
1730 * should be placed
1731 *
1732 * \retval PSA_SUCCESS
1733 */
Derek Miller81133a62018-10-23 14:55:32 -05001734typedef psa_status_t (*pcd_key_derivation_derive_t)(pcd_key_derivation_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -05001735 psa_key_slot_t dest_key);
1736
1737/** \brief Perform the final step of a key agreement and place the generated
1738 * key material in a buffer
1739 *
1740 * \param[out] p_output Buffer in which to place the generated key
1741 * material
1742 * \param[in] output_size The size in bytes of `p_output`
1743 * \param[out] p_output_length Upon success, contains the number of bytes of
1744 * key material placed in `p_output`
1745 *
1746 * \retval PSA_SUCCESS
1747 */
1748typedef psa_status_t (*pcd_key_derivation_export_t)(uint8_t *p_output,
1749 size_t output_size,
1750 size_t *p_output_length);
1751
1752/**
1753 * \brief A struct containing all of the function pointers needed to for key
1754 * derivation and agreement
1755 *
1756 * PSA Crypto API implementations should populate instances of the table as
1757 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001758 *
1759 * If one of the functions is not implemented, it should be set to NULL.
1760 */
1761struct pcd_key_derivation_t {
Derek Miller16e72292018-10-15 16:14:24 -05001762 /** Function that performs the key derivation setup */
1763 pcd_key_derivation_setup_t *p_setup;
1764 /** Function that sets the key derivation collateral */
1765 pcd_key_derivation_collateral_t *p_collateral;
1766 /** Function that performs the final key derivation step */
1767 pcd_key_derivation_derive_t *p_derive;
1768 /** Function that perforsm the final key derivation or agreement and
1769 * exports the key */
1770 pcd_key_derivation_export_t *p_export;
Derek Miller5b3417a2018-10-10 17:55:03 -05001771};
1772
Derek Miller16e72292018-10-15 16:14:24 -05001773/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001774
1775#endif // __PSA_CRYPTO_DRIVER_H__