blob: 14784bb72d9f2bb1f7725b1eb40c4e4780ce06e7 [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;
42
43/** \defgroup opaque_mac Opaque Message Authentication Code
Derek Miller765682c2018-10-22 15:27:27 -050044 * Generation and authentication of Message Authentication Codes (MACs) using
45 * opaque keys can be done either as a single function call (via the
46 * `pcd_mac_opaque_generate_t` or `psa_mac_opaque_verify_t` functions), or in
47 * parts using the following sequence:
48 * - `psa_mac_opaque_setup_t`
49 * - `psa_mac_opaque_update_t`
50 * - `psa_mac_opaque_update_t`
51 * - ...
52 * - `psa_mac_opaque_finish_t` or `psa_mac_opaque_finish_verify_t`
53 *
54 * If a previously started Opaque MAC operation needs to be terminated, it
55 * should be done so by the `psa_mac_opaque_abort_t`. Failure to do so may
56 * result in allocated resources not being freed or in other undefined
57 * behavior.
Derek Miller5b3417a2018-10-10 17:55:03 -050058 */
Derek Miller16e72292018-10-15 16:14:24 -050059/**@{*/
60/** \brief A function that starts a MAC operation for a PSA Crypto Driver
61 * implementation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -050062 *
Derek Miller16e72292018-10-15 16:14:24 -050063 * \param[in,out] p_context A structure that will contain the
64 * hardware-specific MAC context
65 * \param[in] key_slot The slot of the key to be used for the
66 * operation
67 * \param[in] algorithm The algorithm to be used to underly the MAC
68 * operation
Derek Miller5b3417a2018-10-10 17:55:03 -050069 *
70 * \retval PSA_SUCCESS
71 * Success.
72 */
Derek Miller16e72292018-10-15 16:14:24 -050073typedef psa_status_t (*pcd_mac_opaque_setup_t)(void *p_context,
74 psa_key_slot_t key_slot,
75 psa_algorithm_t algorithm);
Derek Miller5b3417a2018-10-10 17:55:03 -050076
Derek Miller16e72292018-10-15 16:14:24 -050077/** \brief A function that continues a previously started MAC operation using
78 * an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -050079 *
Derek Miller16e72292018-10-15 16:14:24 -050080 * \param[in,out] p_context A hardware-specific structure for the
81 * previously-established MAC operation to be
82 * continued
83 * \param[in] p_input A buffer containing the message to be appended
84 * to the MAC operation
85 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -050086 */
Derek Miller16e72292018-10-15 16:14:24 -050087typedef psa_status_t (*pcd_mac_opaque_update_t)(void *p_context,
88 const uint8_t *p_input,
89 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -050090
Derek Miller16e72292018-10-15 16:14:24 -050091/** \brief a function that completes a previously started MAC operation by
92 * returning the resulting MAC using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -050093 *
Derek Millerf3d0a562018-10-18 16:41:08 -050094 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -050095 * previously started MAC operation to be
96 * finished
97 * \param[out] p_mac A buffer where the generated MAC will be
98 * placed
99 * \param[in] mac_size The size in bytes of the buffer that has been
100 * allocated for the `output` buffer
101 * \param[out] p_mac_length After completion, will contain the number of
Derek Millerf3d0a562018-10-18 16:41:08 -0500102 * bytes placed in the `p_mac` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500103 *
104 * \retval PSA_SUCCESS
105 * Success.
106 */
Derek Miller16e72292018-10-15 16:14:24 -0500107typedef psa_status_t (*pcd_mac_opaque_finish_t)(void *p_context,
108 uint8_t *p_mac,
109 size_t mac_size,
110 size_t *p_mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500111
Derek Miller16e72292018-10-15 16:14:24 -0500112/** \brief A function that completes a previously started MAC operation by
113 * comparing the resulting MAC against a known value using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -0500114 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500115 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500116 * started MAC operation to be fiinished
117 * \param[in] p_mac The MAC value against which the resulting MAC will
118 * be compared against
119 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
Derek Miller5b3417a2018-10-10 17:55:03 -0500120 *
121 * \retval PSA_SUCCESS
Derek Miller16e72292018-10-15 16:14:24 -0500122 * The operation completed successfully and the MACs matched each
123 * other
Derek Miller5b3417a2018-10-10 17:55:03 -0500124 * \retval PSA_ERROR_INVALID_SIGNATURE
Derek Miller16e72292018-10-15 16:14:24 -0500125 * The operation completed successfully, but the calculated MAC did
126 * not match the provided MAC
Derek Miller5b3417a2018-10-10 17:55:03 -0500127 */
Derek Miller16e72292018-10-15 16:14:24 -0500128typedef psa_status_t (*pcd_mac_opaque_finish_verify_t)(void *p_context,
129 const uint8_t *p_mac,
130 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500131
Derek Miller16e72292018-10-15 16:14:24 -0500132/** \brief A function that aborts a previous started opaque-key MAC operation
133
Derek Millerf3d0a562018-10-18 16:41:08 -0500134 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500135 * started MAC operation to be aborted
136 */
137typedef psa_status_t (*pcd_mac_opaque_abort_t)(void *p_context);
138
139/** \brief A funciton that performs a MAC operation in one command and return
140 * the calculated MAC using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -0500141 *
Derek Miller16e72292018-10-15 16:14:24 -0500142 * \param[in] p_input A buffer containing the message to be MACed
Derek Millerf3d0a562018-10-18 16:41:08 -0500143 * \param[in] input_length The size in bytes of `p_input`
Derek Miller16e72292018-10-15 16:14:24 -0500144 * \param[in] key_slot The slot of the key to be used
Derek Millerf3d0a562018-10-18 16:41:08 -0500145 * \param[in] alg The algorithm to be used to underlie the MAC
Derek Miller16e72292018-10-15 16:14:24 -0500146 * operation
147 * \param[out] p_mac A buffer where the generated MAC will be
148 * placed
149 * \param[in] mac_size The size in bytes of the `output` buffer
150 * \param[out] p_mac_length After completion, will contain the number of
151 * bytes placed in the `output` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500152 *
153 * \retval PSA_SUCCESS
154 * Success.
155 */
Derek Miller16e72292018-10-15 16:14:24 -0500156typedef psa_status_t (*pcd_mac_opaque_generate_t)(const uint8_t *p_input,
157 size_t input_length,
158 psa_key_slot_t key_slot,
159 psa_algorithm_t alg,
160 uint8_t *p_mac,
161 size_t mac_size,
162 size_t *p_mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500163
Derek Miller16e72292018-10-15 16:14:24 -0500164/** \brief A function that performs an MAC operation in one command and
165 * compare the resulting MAC against a known value using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -0500166 *
Derek Miller16e72292018-10-15 16:14:24 -0500167 * \param[in] p_input A buffer containing the message to be MACed
168 * \param[in] input_length The size in bytes of `input`
169 * \param[in] key_slot The slot of the key to be used
170 * \param[in] alg The algorithm to be used to underlie the MAC
171 * operation
172 * \param[in] p_mac The MAC value against which the resulting MAC will
173 * be compared against
174 * \param[in] mac_length The size in bytes of `mac`
Derek Miller5b3417a2018-10-10 17:55:03 -0500175 *
176 * \retval PSA_SUCCESS
Derek Miller16e72292018-10-15 16:14:24 -0500177 * The operation completed successfully and the MACs matched each
178 * other
Derek Miller5b3417a2018-10-10 17:55:03 -0500179 * \retval PSA_ERROR_INVALID_SIGNATURE
Derek Miller16e72292018-10-15 16:14:24 -0500180 * The operation completed successfully, but the calculated MAC did
181 * not match the provided MAC
Derek Miller5b3417a2018-10-10 17:55:03 -0500182 */
Derek Miller16e72292018-10-15 16:14:24 -0500183typedef psa_status_t (*pcd_mac_opaque_verify_t)(const uint8_t *p_input,
Derek Miller5b3417a2018-10-10 17:55:03 -0500184 size_t input_length,
185 psa_key_slot_t key_slot,
186 psa_algorithm_t alg,
Derek Miller16e72292018-10-15 16:14:24 -0500187 const uint8_t *p_mac,
188 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500189
Derek Miller16e72292018-10-15 16:14:24 -0500190/** \brief A struct containing all of the function pointers needed to
191 * implement MAC operations using opaque keys.
Derek Miller5b3417a2018-10-10 17:55:03 -0500192 *
Derek Miller16e72292018-10-15 16:14:24 -0500193 * PSA Crypto API implementations should populate the table as appropriate
194 * upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -0500195 *
Derek Miller765682c2018-10-22 15:27:27 -0500196 * If one of the functions is not implemented (such as
197 * `pcd_mac_opaque_generate_t`), it should be set to NULL.
Derek Miller16e72292018-10-15 16:14:24 -0500198 *
199 * Driver implementers should ensure that they implement all of the functions
200 * that make sense for their hardware, and that they provide a full solution
201 * (for example, if they support `p_setup`, they should also support
202 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
Derek Miller5b3417a2018-10-10 17:55:03 -0500203 *
204 */
205struct pcd_mac_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -0500206 /**The size in bytes of the hardware-specific Opaque-MAC Context structure
207 */
208 size_t context_size;
209 /** Function that performs the setup operation
210 */
211 pcd_mac_opaque_setup_t *p_setup;
212 /** Function that performs the update operation
213 */
214 pcd_mac_opaque_update_t *p_update;
215 /** Function that completes the operation
216 */
217 pcd_mac_opaque_finish_t *p_finish;
218 /** Function that completed a MAC operation with a verify check
219 */
220 pcd_mac_opaque_finish_verify_t *p_finish_verify;
221 /** Function that aborts a previoustly started operation
222 */
223 pcd_mac_opaque_abort_t *p_abort;
224 /** Function that performs the MAC operation in one call
225 */
226 pcd_mac_opaque_generate_t *p_mac;
227 /** Function that performs the MAC and verify operation in one call
228 */
229 pcd_mac_opaque_verify_t *p_mac_verify;
Derek Miller5b3417a2018-10-10 17:55:03 -0500230};
Derek Miller16e72292018-10-15 16:14:24 -0500231/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500232
233/** \defgroup transparent_mac Transparent Message Authentication Code
Derek Miller765682c2018-10-22 15:27:27 -0500234 * Generation and authentication of Message Authentication Codes (MACs) using
235 * transparent keys can be done either as a single function call (via the
236 * `pcd_mac_transparent_generate_t` or `psa_mac_transparent_verify_t`
237 * functions), or in parts using the following sequence:
238 * - `psa_mac_transparent_setup_t`
239 * - `psa_mac_transparent_update_t`
240 * - `psa_mac_transparent_update_t`
241 * - ...
242 * - `psa_mac_transparent_finish_t` or `psa_mac_transparent_finish_verify_t`
243 *
244 * If a previously started Transparent MAC operation needs to be terminated, it
245 * should be done so by the `psa_mac_transparent_abort_t`. Failure to do so may
246 * result in allocated resources not being freed or in other undefined
247 * behavior.
248 *
Derek Miller5b3417a2018-10-10 17:55:03 -0500249 */
Derek Miller16e72292018-10-15 16:14:24 -0500250/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500251
252/** \brief The hardware-specific transparent-key MAC context structure
Derek Miller765682c2018-10-22 15:27:27 -0500253 *
Derek Miller16e72292018-10-15 16:14:24 -0500254 * The contents of this structure are implementation dependent and are
255 * therefore not described here.
Derek Miller5b3417a2018-10-10 17:55:03 -0500256 */
257struct pcd_mac_transparent_context_t {
258 // Implementation specific
259};
260
Derek Miller16e72292018-10-15 16:14:24 -0500261/** \brief The function prototype for the setup operation of a
262 * transparent-key MAC operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500263 *
Derek Miller16e72292018-10-15 16:14:24 -0500264 * Functions that implement the prototype should be named in the following
265 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500266 * ~~~~~~~~~~~~~{.c}
Derek Miller16e72292018-10-15 16:14:24 -0500267 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_setup
Derek Miller5b3417a2018-10-10 17:55:03 -0500268 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500269 * Where `ALGO` is the name of the underlying primitive, and `MAC_VARIANT`
270 * is the specific variant of a MAC operation (such as HMAC or CMAC)
Derek Miller5b3417a2018-10-10 17:55:03 -0500271 *
Derek Miller16e72292018-10-15 16:14:24 -0500272 * \param[in,out] p_context A structure that will contain the
273 * hardware-specific MAC context
274 * \param[in] p_key A buffer containing the cleartext key material
275 * to be used in the operation
276 * \param[in] key_length The size in bytes of the key material
Derek Miller5b3417a2018-10-10 17:55:03 -0500277 *
278 * \retval PSA_SUCCESS
279 * Success.
280 */
Derek Miller16e72292018-10-15 16:14:24 -0500281typedef psa_status_t (*pcd_mac_transparent_setup_t)(struct pcd_mac_transparent_context_t *p_context,
282 const uint8_t *p_key,
283 size_t key_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500284
Derek Miller16e72292018-10-15 16:14:24 -0500285/** \brief The function prototype for the update operation of a
286 * transparent-key MAC operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500287 *
Derek Miller16e72292018-10-15 16:14:24 -0500288 * Functions that implement the prototype should be named in the following
289 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500290 * ~~~~~~~~~~~~~{.c}
291 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_update
292 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500293 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT`
294 * is the specific variant of a MAC operation (such as HMAC or CMAC)
Derek Miller5b3417a2018-10-10 17:55:03 -0500295 *
Derek Miller16e72292018-10-15 16:14:24 -0500296 * \param[in,out] p_context A hardware-specific structure for the
297 * previously-established MAC operation to be
298 * continued
299 * \param[in] p_input A buffer containing the message to be appended
300 * to the MAC operation
301 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500302 */
Derek Miller16e72292018-10-15 16:14:24 -0500303typedef psa_status_t (*pcd_mac_transparent_update_t)(struct pcd_mac_transparent_context_t *p_context,
304 const uint8_t *p_input,
305 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500306
Derek Miller16e72292018-10-15 16:14:24 -0500307/** \brief The function prototype for the finish operation of a
308 * transparent-key MAC operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500309 *
Derek Miller16e72292018-10-15 16:14:24 -0500310 * Functions that implement the prototype should be named in the following
311 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500312 * ~~~~~~~~~~~~~{.c}
313 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish
314 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500315 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
316 * the specific variant of a MAC operation (such as HMAC or CMAC)
Derek Miller5b3417a2018-10-10 17:55:03 -0500317 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500318 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500319 * previously started MAC operation to be
320 * finished
321 * \param[out] p_mac A buffer where the generated MAC will be placed
322 * \param[in] mac_length The size in bytes of the buffer that has been
323 * allocated for the `p_mac` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500324 *
325 * \retval PSA_SUCCESS
326 * Success.
327 */
Derek Miller16e72292018-10-15 16:14:24 -0500328typedef psa_status_t (*pcd_mac_transparent_finish_t)(struct pcd_mac_transparent_context_t *p_context,
329 uint8_t *p_mac,
330 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500331
Derek Miller16e72292018-10-15 16:14:24 -0500332/** \brief The function prototype for the finish and verify operation of a
333 * transparent-key MAC operation
334 *
335 * Functions that implement the prototype should be named in the following
336 * convention:
337 * ~~~~~~~~~~~~~{.c}
338 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish_verify
339 * ~~~~~~~~~~~~~
340 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
341 * the specific variant of a MAC operation (such as HMAC or CMAC)
342 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500343 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500344 * previously started MAC operation to be
Derek Millerf3d0a562018-10-18 16:41:08 -0500345 * verified and finished
Derek Miller16e72292018-10-15 16:14:24 -0500346 * \param[in] p_mac A buffer containing the MAC that will be used
347 * for verification
348 * \param[in] mac_length The size in bytes of the data in the `p_mac`
349 * buffer
350 *
351 * \retval PSA_SUCCESS
352 * The operation completed successfully and the comparison matched
Derek Miller5b3417a2018-10-10 17:55:03 -0500353 */
Derek Miller765682c2018-10-22 15:27:27 -0500354typedef psa_status_t (*pcd_mac_transparent_finish_verify_t)(struct pcd_mac_transparent_context_t *p_context,
Derek Miller16e72292018-10-15 16:14:24 -0500355 const uint8_t *p_mac,
356 size_t mac_length);
357
358/** \brief The function prototype for the abort operation for a previously
359 * started transparent-key MAC operation
360 *
361 * Functions that implement the prototype should be named in the following
362 * convention:
363 * ~~~~~~~~~~~~~{.c}
364 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_abort
365 * ~~~~~~~~~~~~~
366 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
367 * the specific variant of a MAC operation (such as HMAC or CMAC)
368 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500369 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500370 * previously started MAC operation to be
Derek Millerf3d0a562018-10-18 16:41:08 -0500371 * aborted
Derek Miller16e72292018-10-15 16:14:24 -0500372 *
373 */
374typedef psa_status_t (*pcd_mac_transparent_abort_t)(struct pcd_mac_transparent_context_t *p_context);
375
376/** \brief The function prototype for a one-shot operation of a transparent-key
377 * MAC operation
378 *
379 * Functions that implement the prototype should be named in the following
380 * convention:
381 * ~~~~~~~~~~~~~{.c}
382 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>
383 * ~~~~~~~~~~~~~
384 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
385 * the specific variant of a MAC operation (such as HMAC or CMAC)
386 *
387 * \param[in] p_input A buffer containing the data to be MACed
388 * \param[in] input_length The length in bytes of the `p_input` data
389 * \param[in] p_key A buffer containing the key material to be used
390 * for the MAC operation
391 * \param[in] key_length The length in bytes of the `p_key` data
392 * \param[in] alg The algorithm to be performed
393 * \param[out] p_mac The buffer where the resulting MAC will be placed
394 * upon success
395 * \param[in] mac_length The length in bytes of the `p_mac` buffer
396 */
397typedef psa_status_t (*pcd_mac_transparent_t)(const uint8_t *p_input,
398 size_t input_length,
399 const uint8_t *p_key,
400 size_t key_length,
401 psa_algorithm_t alg,
402 uint8_t *p_mac,
403 size_t mac_length);
404
405/** \brief The function prototype for a one-shot operation of a transparent-key
406 * MAC Verify operation
407 *
408 * Functions that implement the prototype should be named in the following
409 * convention:
410 * ~~~~~~~~~~~~~{.c}
411 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_verify
412 * ~~~~~~~~~~~~~
413 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
414 * the specific variant of a MAC operation (such as HMAC or CMAC)
415 *
416 * \param[in] p_input A buffer containing the data to be MACed
417 * \param[in] input_length The length in bytes of the `p_input` data
418 * \param[in] p_key A buffer containing the key material to be used
419 * for the MAC operation
420 * \param[in] key_length The length in bytes of the `p_key` data
421 * \param[in] alg The algorithm to be performed
422 * \param[in] p_mac The MAC data to be compared
423 * \param[in] mac_length The length in bytes of the `p_mac` buffer
424 *
425 * \retval PSA_SUCCESS
426 * The operation completed successfully and the comparison matched
427 */
428typedef psa_status_t (*pcd_mac_transparent_verify_t)(const uint8_t *p_input,
429 size_t input_length,
430 const uint8_t *p_key,
431 size_t key_length,
432 psa_algorithm_t alg,
433 const uint8_t *p_mac,
434 size_t mac_length);
435/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500436
437/** \defgroup opaque_cipher Opaque Symmetric Ciphers
Derek Miller765682c2018-10-22 15:27:27 -0500438 *
439 * Encryption and Decryption using opaque keys in block modes other than ECB
440 * must be done in multiple parts, using the following flow:
441 * - `pcd_cipher_opaque_setup_t`
442 * - `pcd_cipher_opaque_set_iv_t` (optional depending upon block mode)
443 * - `pcd_cipher_opaque_update_t`
444 * - ...
445 * - `pcd_cipher_opaque_finish_t`
446
447 * If a previously started Opaque Cipher operation needs to be terminated, it
448 * should be done so by the `psa_cipher_opaque_abort_t`. Failure to do so may
449 * result in allocated resources not being freed or in other undefined
450 * behavior.
451 *
452 * In situations where a PSA Cryptographic API implementation is using a block
453 * mode not-supported by the underlying hardware or driver, it can construct
454 * the block mode itself, while calling the `pcd_cipher_opaque_ecb_t` function
455 * pointer for the cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500456 */
Derek Miller16e72292018-10-15 16:14:24 -0500457/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500458
Derek Miller16e72292018-10-15 16:14:24 -0500459/** \brief A function pointer that provides the cipher setup function for
460 * opaque-key operations
Derek Miller5b3417a2018-10-10 17:55:03 -0500461 *
Derek Miller16e72292018-10-15 16:14:24 -0500462 * \param[in,out] p_context A structure that will contain the
463 * hardware-specific cipher context.
464 * \param[in] key_slot The slot of the key to be used for the
465 * operation
466 * \param[in] algorithm The algorithm to be used in the cipher
467 * operation
468 * \param[in] direction Indicates whether the operation is an encrypt
469 * or decrypt
Derek Miller5b3417a2018-10-10 17:55:03 -0500470 *
471 * \retval PSA_SUCCESS
472 * \retval PSA_ERROR_NOT_SUPPORTED
473 */
Derek Miller16e72292018-10-15 16:14:24 -0500474typedef psa_status_t (*pcd_cipher_opaque_setup_t)(void *p_context,
475 psa_key_slot_t key_slot,
Derek Miller5b3417a2018-10-10 17:55:03 -0500476 psa_algorithm_t algorithm,
Derek Miller16e72292018-10-15 16:14:24 -0500477 encrypt_or_decrypt_t direction);
478
479/** \brief A function pointer that sets the initialization vector (if
480 * necessary) for an opaque cipher operation
481 *
Derek Miller765682c2018-10-22 15:27:27 -0500482 * Rationale: The `psa_cipher_*` function in the PSA Cryptographif API has two
483 * IV functions: one to set the IV, and one to generate it internally. The
484 * generate function is not necessary for the driver API as the PSA Crypto
485 * implementation can do the generation using its RNG features.
Derek Miller16e72292018-10-15 16:14:24 -0500486 *
487 * \param[in,out] p_context A structure that contains the previously set up
488 * hardware-specific cipher context
489 * \param[in] p_iv A buffer containing the initialization vector
490 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
491 *
492 * \retval PSA_SUCCESS
493 */
494typedef psa_status_t (*pcd_cipher_opaque_set_iv_t)(void *p_context,
495 const uint8_t *p_iv,
496 size_t iv_length);
497
498/** \brief A function that continues a previously started opaque-key cipher
499 * operation
500 *
501 * \param[in,out] p_context A hardware-specific structure for the
502 * previously started cipher operation
503 * \param[in] p_input A buffer containing the data to be
504 * encrypted/decrypted
505 * \param[in] input_size The size in bytes of the buffer pointed to
506 * by `p_input`
507 * \param[out] p_output The caller-allocated buffer where the
508 * output will be placed
509 * \param[in] output_size The allocated size in bytes of the
510 * `p_output` buffer
511 * \param[out] p_output_length After completion, will contain the number
512 * of bytes placed in the `p_output` buffer
513 *
514 * \retval PSA_SUCCESS
515 */
516typedef psa_status_t (*pcd_cipher_opaque_update_t)(void *p_context,
517 const uint8_t *p_input,
518 size_t input_size,
519 uint8_t *p_output,
520 size_t output_size,
521 size_t *p_output_length);
522
523/** \brief A function that completes a previously started opaque-key cipher
524 * operation
525 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500526 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500527 * previously started cipher operation
Derek Millerf3d0a562018-10-18 16:41:08 -0500528 * \param[out] p_output The caller-allocated buffer where the output
Derek Miller16e72292018-10-15 16:14:24 -0500529 * will be placed
530 * \param[in] output_size The allocated size in bytes of the `p_output`
531 * buffer
532 * \param[out] p_output_length After completion, will contain the number of
533 * bytes placed in the `p_output` buffer
534 *
535 * \retval PSA_SUCCESS
536 */
537typedef psa_status_t (*pcd_cipher_opaque_finish_t)(void *p_context,
538 uint8_t *p_output,
539 size_t output_size,
540 size_t *p_output_length);
541
542/** \brief A function that aborts a previously started opaque-key cipher
543 * operation
544 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500545 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500546 * previously started cipher operation
547 */
548typedef psa_status_t (*pcd_cipher_opaque_abort_t)(void *p_context);
549
550/** \brief A function that performs the ECB block mode for opaque-key cipher
551 * operations
552 *
553 * Note: this function should only be used with implementations that do not
554 * provide a needed higher-level operation.
555 *
556 * \param[in] key_slot The slot of the key to be used for the operation
557 * \param[in] algorithm The algorithm to be used in the cipher operation
558 * \param[in] direction Indicates whether the operation is an encrypt or
559 * decrypt
560 * \param[in] p_input A buffer containing the data to be
561 * encrypted/decrypted
562 * \param[in] input_size The size in bytes of the buffer pointed to by
563 * `p_input`
Derek Millerf3d0a562018-10-18 16:41:08 -0500564 * \param[out] p_output The caller-allocated buffer where the output will
Derek Miller16e72292018-10-15 16:14:24 -0500565 * be placed
566 * \param[in] output_size The allocated size in bytes of the `p_output`
567 * buffer
568 *
569 * \retval PSA_SUCCESS
570 * \retval PSA_ERROR_NOT_SUPPORTED
571 */
572typedef psa_status_t (*pcd_cipher_opaque_ecb_t)(psa_key_slot_t key_slot,
573 psa_algorithm_t algorithm,
574 encrypt_or_decrypt_t direction,
575 const uint8_t *p_input,
576 size_t input_size,
577 uint8_t *p_output,
578 size_t output_size);
Derek Miller5b3417a2018-10-10 17:55:03 -0500579
580/**
Derek Miller16e72292018-10-15 16:14:24 -0500581 * \brief A struct containing all of the function pointers needed to implement
582 * cipher operations using opaque keys.
Derek Miller5b3417a2018-10-10 17:55:03 -0500583 *
Derek Miller16e72292018-10-15 16:14:24 -0500584 * PSA Crypto API implementations should populate instances of the table as
585 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -0500586 *
Derek Miller16e72292018-10-15 16:14:24 -0500587 * If one of the functions is not implemented (such as
588 * `pcd_cipher_opaque_ecb_t`), it should be set to NULL.
Derek Miller5b3417a2018-10-10 17:55:03 -0500589 */
590struct pcd_cipher_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -0500591 /** The size in bytes of the hardware-specific Opaque Cipher context
592 * structure
593 */
594 size_t size;
595 /** Function that performs the setup operation */
596 pcd_cipher_opaque_setup_t *p_setup;
597 /** Function that sets the IV (if necessary) */
598 pcd_cipher_opaque_set_iv_t *p_set_iv;
599 /** Function that performs the update operation */
600 pcd_cipher_opaque_update_t *p_update;
601 /** Function that completes the operation */
602 pcd_cipher_opaque_finish_t *p_finish;
603 /** Function that aborts the operation */
604 pcd_cipher_opaque_abort_t *p_abort;
605 /** Function that performs ECB mode for the cipher
606 * (Danger: ECB mode should not be used directly by clients of the PSA
607 * Crypto Client API)
608 */
609 pcd_cipher_opaque_ecb_t *p_ecb;
Derek Miller5b3417a2018-10-10 17:55:03 -0500610};
611
Derek Miller16e72292018-10-15 16:14:24 -0500612/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500613
614/** \defgroup transparent_cipher Transparent Block Cipher
Derek Miller765682c2018-10-22 15:27:27 -0500615 * Encryption and Decryption using transparent keys in block modes other than
616 * ECB must be done in multiple parts, using the following flow:
617 * - `pcd_cipher_transparent_setup_t`
618 * - `pcd_cipher_transparent_set_iv_t` (optional depending upon block mode)
619 * - `pcd_cipher_transparent_update_t`
620 * - ...
621 * - `pcd_cipher_transparent_finish_t`
622
623 * If a previously started Transparent Cipher operation needs to be terminated,
624 * it should be done so by the `psa_cipher_transparent_abort_t`. Failure to do
625 * so may result in allocated resources not being freed or in other undefined
626 * behavior.
Derek Miller5b3417a2018-10-10 17:55:03 -0500627 */
Derek Miller16e72292018-10-15 16:14:24 -0500628/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500629
630/** \brief The hardware-specific transparent-key Cipher context structure
Derek Miller765682c2018-10-22 15:27:27 -0500631 *
632 * The contents of this structure are implementation dependent and are
Derek Miller16e72292018-10-15 16:14:24 -0500633 * therefore not described here.
Derek Miller5b3417a2018-10-10 17:55:03 -0500634 */
635struct pcd_cipher_transparent_context_t {
636 // Implementation specific
637};
638
Derek Miller16e72292018-10-15 16:14:24 -0500639/** \brief The function prototype for the setup operation of transparent-key
640 * block cipher operations.
641 * Functions that implement the prototype should be named in the following
642 * conventions:
Derek Miller5b3417a2018-10-10 17:55:03 -0500643 * ~~~~~~~~~~~~~{.c}
644 * pcd_cipher_transparent_setup_<CIPHER_NAME>_<MODE>
645 * ~~~~~~~~~~~~~
646 * Where
647 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
648 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Derek Miller16e72292018-10-15 16:14:24 -0500649 * or for stream ciphers:
650 * ~~~~~~~~~~~~~{.c}
651 * pcd_cipher_transparent_setup_<CIPHER_NAME>
652 * ~~~~~~~~~~~~~
653 * Where `CIPHER_NAME` is the name of a stream cipher (i.e. RC4)
Derek Miller5b3417a2018-10-10 17:55:03 -0500654 *
Derek Miller16e72292018-10-15 16:14:24 -0500655 * \param[in,out] p_context A structure that will contain the
656 * hardware-specific cipher context
657 * \param[in] direction Indicates if the operation is an encrypt or a
658 * decrypt
659 * \param[in] p_key_data A buffer containing the cleartext key material
660 * to be used in the operation
661 * \param[in] key_data_size The size in bytes of the key material
Derek Miller5b3417a2018-10-10 17:55:03 -0500662 *
663 * \retval PSA_SUCCESS
664 */
Derek Miller16e72292018-10-15 16:14:24 -0500665typedef psa_status_t (*pcd_cipher_transparent_setup_t)(struct pcd_cipher_transparent_context_t *p_context,
666 encrypt_or_decrypt_t direction,
667 const uint8_t *p_key_data,
668 size_t key_data_size);
Derek Miller5b3417a2018-10-10 17:55:03 -0500669
Derek Miller16e72292018-10-15 16:14:24 -0500670/** \brief The function prototype for the set initialization vector operation
671 * of transparent-key block cipher operations
672 * Functions that implement the prototype should be named in the following
673 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500674 * ~~~~~~~~~~~~~{.c}
675 * pcd_cipher_transparent_set_iv_<CIPHER_NAME>_<MODE>
676 * ~~~~~~~~~~~~~
677 * Where
678 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
679 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
680 *
Derek Miller16e72292018-10-15 16:14:24 -0500681 * \param[in,out] p_context A structure that contains the previously setup
682 * hardware-specific cipher context
683 * \param[in] p_iv A buffer containing the initialization vecotr
684 * \param[in] iv_length The size in bytes of the contents of `p_iv`
Derek Miller5b3417a2018-10-10 17:55:03 -0500685 *
686 * \retval PSA_SUCCESS
687*/
Derek Miller16e72292018-10-15 16:14:24 -0500688typedef psa_status_t (*pcd_cipher_transparent_set_iv_t)(struct pcd_cipher_transparent_context_t *p_context,
689 const uint8_t *p_iv,
690 size_t iv_length);
691
692/** \brief The function prototype for the update operation of transparent-key
693 * block cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500694 *
Derek Miller16e72292018-10-15 16:14:24 -0500695 * Functions that implement the prototype should be named in the following
696 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500697 * ~~~~~~~~~~~~~{.c}
698 * pcd_cipher_transparent_update_<CIPHER_NAME>_<MODE>
699 * ~~~~~~~~~~~~~
700 * Where
701 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
702 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
703 *
Derek Miller16e72292018-10-15 16:14:24 -0500704 * \param[in,out] p_context A hardware-specific structure for the
705 * previously started cipher operation
706 * \param[in] p_input A buffer containing the data to be
707 * encrypted or decrypted
708 * \param[in] input_size The size in bytes of the `p_input` buffer
709 * \param[out] p_output A caller-allocated buffer where the
710 * generated output will be placed
711 * \param[in] output_size The size in bytes of the `p_output` buffer
712 * \param[out] p_output_length After completion, will contain the number
713 * of bytes placed in the `p_output` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500714 *
715 * \retval PSA_SUCCESS
716 */
Derek Miller16e72292018-10-15 16:14:24 -0500717typedef psa_status_t (*pcd_cipher_transparent_update_t)(struct pcd_cipher_transparent_context_t *p_context,
718 const uint8_t *p_input,
719 size_t input_size,
720 uint8_t *p_output,
721 size_t output_size,
722 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500723
Derek Miller16e72292018-10-15 16:14:24 -0500724/** \brief The function prototype for the finish operation of transparent-key
725 * block cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500726*
Derek Miller16e72292018-10-15 16:14:24 -0500727 * Functions that implement the prototype should be named in the following
728 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500729 * ~~~~~~~~~~~~~{.c}
730 * pcd_cipher_transparent_finish_<CIPHER_NAME>_<MODE>
731 * ~~~~~~~~~~~~~
732 * Where
733 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
734 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Derek Miller16e72292018-10-15 16:14:24 -0500735 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500736 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500737 * previously started cipher operation
738 * \param[out] p_output A caller-allocated buffer where the generated
739 * output will be placed
740 * \param[in] output_size The size in bytes of the `p_output` buffer
741 * \param[out] p_output_length After completion, will contain the number of
742 * bytes placed in the `p_output` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500743 *
744 * \retval PSA_SUCCESS
745 */
Derek Miller16e72292018-10-15 16:14:24 -0500746typedef psa_status_t (*pcd_cipher_transparent_finish_t)(struct pcd_cipher_transparent_context_t *p_context,
747 uint8_t *p_output,
748 size_t output_size,
749 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500750
Derek Miller16e72292018-10-15 16:14:24 -0500751/** \brief The function prototype for the abort operation of transparent-key
752 * block cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500753 *
Derek Miller16e72292018-10-15 16:14:24 -0500754 * Functions that implement the following prototype should be named in the
755 * following convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500756 * ~~~~~~~~~~~~~{.c}
757 * pcd_cipher_transparent_abort_<CIPHER_NAME>_<MODE>
758 * ~~~~~~~~~~~~~
759 * Where
760 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
761 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
762 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500763 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500764 * previously started cipher operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500765 *
766 * \retval PSA_SUCCESS
767 */
Derek Miller16e72292018-10-15 16:14:24 -0500768typedef psa_status_t (*pcd_cipher_transparent_abort_t)(struct pcd_cipher_transparent_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -0500769
Derek Miller16e72292018-10-15 16:14:24 -0500770/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500771
Derek Miller16e72292018-10-15 16:14:24 -0500772/** \defgroup driver_digest Message Digests
Derek Miller765682c2018-10-22 15:27:27 -0500773 *
774 * Generation and authentication of Message Digests (aka hashes) must be done
775 * in parts using the following sequence:
776 * - `psa_hash_setup_t`
777 * - `psa_hash_update_t`
778 * - ...
779 * - `psa_hash_finish_t`
780 *
781 * If a previously started Message Digest operation needs to be terminated
782 * before the `psa_hash_finish_t` operation is complete, it should be aborted
783 * by the `psa_hash_abort_t`. Failure to do so may result in allocated
784 * resources not being freed or in other undefined behavior.
Derek Miller5b3417a2018-10-10 17:55:03 -0500785 */
Derek Miller16e72292018-10-15 16:14:24 -0500786/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500787
788/** \brief The hardware-specific hash context structure
Derek Miller765682c2018-10-22 15:27:27 -0500789 *
Derek Miller16e72292018-10-15 16:14:24 -0500790 * The contents of this structure are implementation dependent and are
791 * therefore not described here
Derek Miller5b3417a2018-10-10 17:55:03 -0500792 */
793struct pcd_hash_context_t {
794 // Implementation specific
795};
796
Derek Miller16e72292018-10-15 16:14:24 -0500797/** \brief The function prototype for the start operation of a hash (message
798 * digest) operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500799 *
Derek Miller16e72292018-10-15 16:14:24 -0500800 * Functions that implement the prototype should be named in the following
801 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500802 * ~~~~~~~~~~~~~{.c}
Derek Miller16e72292018-10-15 16:14:24 -0500803 * pcd_hash_<ALGO>_setup
Derek Miller5b3417a2018-10-10 17:55:03 -0500804 * ~~~~~~~~~~~~~
805 * Where `ALGO` is the name of the underlying hash function
806 *
Derek Miller16e72292018-10-15 16:14:24 -0500807 * \param[in,out] p_context A structure that will contain the
808 * hardware-specific hash context
Derek Miller5b3417a2018-10-10 17:55:03 -0500809 *
810 * \retval PSA_SUCCESS Success.
811 */
Derek Miller16e72292018-10-15 16:14:24 -0500812typedef psa_status_t (*pcd_hash_setup_t)(struct pcd_hash_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -0500813
Derek Miller16e72292018-10-15 16:14:24 -0500814/** \brief The function prototype for the update operation of a hash (message
815 * digest) operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500816 *
Derek Miller16e72292018-10-15 16:14:24 -0500817 * Functions that implement the prototype should be named in the following
818 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500819 * ~~~~~~~~~~~~~{.c}
820 * pcd_hash_<ALGO>_update
821 * ~~~~~~~~~~~~~
822 * Where `ALGO` is the name of the underlying algorithm
823 *
Derek Miller16e72292018-10-15 16:14:24 -0500824 * \param[in,out] p_context A hardware-specific structure for the
825 * previously-established hash operation to be
826 * continued
827 * \param[in] p_input A buffer containing the message to be appended
828 * to the hash operation
829 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500830 */
Derek Miller16e72292018-10-15 16:14:24 -0500831typedef psa_status_t (*pcd_hash_update_t)(struct pcd_hash_context_t *p_context,
832 const uint8_t *p_input,
833 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500834
Derek Miller16e72292018-10-15 16:14:24 -0500835/** \brief The prototype for the finish operation of a hash (message digest)
836 * operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500837 *
Derek Miller16e72292018-10-15 16:14:24 -0500838 * Functions that implement the prototype should be named in the following
839 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500840 * ~~~~~~~~~~~~~{.c}
841 * pcd_hash_<ALGO>_finish
842 * ~~~~~~~~~~~~~
843 * Where `ALGO` is the name of the underlying algorithm
844 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500845 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500846 * previously started hash operation to be
847 * fiinished
848 * \param[out] p_output A buffer where the generated digest will be
849 * placed
850 * \param[in] output_size The size in bytes of the buffer that has been
851 * allocated for the `p_output` buffer
Derek Millerf3d0a562018-10-18 16:41:08 -0500852 * \param[out] p_output_length The number of bytes placed in `p_output` after
853 * success
Derek Miller5b3417a2018-10-10 17:55:03 -0500854 *
855 * \retval PSA_SUCCESS
856 * Success.
857 */
Derek Miller16e72292018-10-15 16:14:24 -0500858typedef psa_status_t (*pcd_hash_finish_t)(struct pcd_hash_context_t *p_context,
859 uint8_t *p_output,
Derek Millerf3d0a562018-10-18 16:41:08 -0500860 size_t output_size,
861 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500862
Derek Miller16e72292018-10-15 16:14:24 -0500863/** \brief The function prototype for the abort operation of a hash (message
864 * digest) operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500865 *
Derek Miller16e72292018-10-15 16:14:24 -0500866 * Functions that implement the prototype should be named in the following
867 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500868 * ~~~~~~~~~~~~~{.c}
869 * pcd_hash_<ALGO>_abort
870 * ~~~~~~~~~~~~~
871 * Where `ALGO` is the name of the underlying algorithm
872 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500873 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500874 * started hash operation to be aborted
Derek Miller5b3417a2018-10-10 17:55:03 -0500875 */
876typedef void (*pcd_hash_abort_t)(struct pcd_hash_context_t *p_context);
877
Derek Miller16e72292018-10-15 16:14:24 -0500878/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500879
880
881/** \defgroup opaque_asymmetric Opaque Asymmetric Cryptography
Derek Miller765682c2018-10-22 15:27:27 -0500882 *
883 * Since the amount of data that can (or should) be encrypted or signed using
884 * asymmetric keys is limited by the key size, asymmetric key operations using
885 * opaque keys must be done in single function calls.
Derek Miller5b3417a2018-10-10 17:55:03 -0500886 */
Derek Miller16e72292018-10-15 16:14:24 -0500887/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500888
889/**
Derek Miller16e72292018-10-15 16:14:24 -0500890 * \brief A function that signs a hash or short message with a private key
Derek Miller5b3417a2018-10-10 17:55:03 -0500891 *
Derek Miller16e72292018-10-15 16:14:24 -0500892 * \param[in] key_slot Key slot of an asymmetric key pair
893 * \param[in] alg A signature algorithm that is compatible
894 * with the type of `key`
Derek Miller765682c2018-10-22 15:27:27 -0500895 * \param[in] p_hash The hash to sign
Derek Miller16e72292018-10-15 16:14:24 -0500896 * \param[in] hash_length Size of the `p_hash` buffer in bytes
897 * \param[out] p_signature Buffer where the signature is to be written
Derek Millerf3d0a562018-10-18 16:41:08 -0500898 * \param[in] signature_size Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500899 * \param[out] p_signature_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -0500900 * that make up the returned signature value
Derek Miller5b3417a2018-10-10 17:55:03 -0500901 *
902 * \retval PSA_SUCCESS
903 */
Derek Miller16e72292018-10-15 16:14:24 -0500904typedef psa_status_t (*pcd_asymmetric_opaque_sign_t)(psa_key_slot_t key_slot,
905 psa_algorithm_t alg,
906 const uint8_t *p_hash,
907 size_t hash_length,
908 uint8_t *p_signature,
909 size_t signature_size,
910 size_t *p_signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500911
912/**
Derek Miller16e72292018-10-15 16:14:24 -0500913 * \brief A function that verifies the signature a hash or short message using
Derek Miller765682c2018-10-22 15:27:27 -0500914 * an asymmetric public key
Derek Miller5b3417a2018-10-10 17:55:03 -0500915 *
Derek Miller16e72292018-10-15 16:14:24 -0500916 * \param[in] key_slot Key slot of a public key or an asymmetric key
917 * pair
918 * \param[in] alg A signature algorithm that is compatible with
919 * the type of `key`
Derek Miller765682c2018-10-22 15:27:27 -0500920 * \param[in] p_hash The hash whose signature is to be verified
Derek Miller16e72292018-10-15 16:14:24 -0500921 * \param[in] hash_length Size of the `p_hash` buffer in bytes
922 * \param[in] p_signature Buffer containing the signature to verify
923 * \param[in] signature_length Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500924 *
925 * \retval PSA_SUCCESS
926 * The signature is valid.
927 */
Derek Miller16e72292018-10-15 16:14:24 -0500928typedef psa_status_t (*pcd_asymmetric_opaque_verify_t)(psa_key_slot_t key_slot,
929 psa_algorithm_t alg,
930 const uint8_t *p_hash,
931 size_t hash_length,
932 const uint8_t *p_signature,
933 size_t signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500934
935/**
Derek Miller765682c2018-10-22 15:27:27 -0500936 * \brief A function that encrypts a short message with an asymmetric public
937 * key
Derek Miller5b3417a2018-10-10 17:55:03 -0500938 *
Derek Miller16e72292018-10-15 16:14:24 -0500939 * \param[in] key_slot Key slot of a public key or an asymmetric key
940 * pair
941 * \param[in] alg An asymmetric encryption algorithm that is
942 * compatible with the type of `key`
943 * \param[in] p_input The message to encrypt
944 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500945 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -0500946 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -0500947 * If the algorithm does not support a
948 * salt, pass `NULL`.
949 * If the algorithm supports an optional
950 * salt and you do not want to pass a salt,
951 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -0500952 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
953 * supported.
954 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500955 * If `p_salt` is `NULL`, pass 0.
956 * \param[out] p_output Buffer where the encrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -0500957 * be written
958 * \param[in] output_size Size of the `p_output` buffer in bytes
959 * \param[out] p_output_length On success, the number of bytes that make up
960 * the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -0500961 *
962 * \retval PSA_SUCCESS
963 */
Derek Miller16e72292018-10-15 16:14:24 -0500964typedef psa_status_t (*pcd_asymmetric_opaque_encrypt_t)(psa_key_slot_t key_slot,
965 psa_algorithm_t alg,
966 const uint8_t *p_input,
967 size_t input_length,
968 const uint8_t *p_salt,
969 size_t salt_length,
970 uint8_t *p_output,
971 size_t output_size,
972 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500973
974/**
Derek Miller765682c2018-10-22 15:27:27 -0500975 * \brief Decrypt a short message with an asymmetric private key.
Derek Miller5b3417a2018-10-10 17:55:03 -0500976 *
Derek Miller16e72292018-10-15 16:14:24 -0500977 * \param[in] key_slot Key slot of an asymmetric key pair
978 * \param[in] alg An asymmetric encryption algorithm that is
979 * compatible with the type of `key`
980 * \param[in] p_input The message to decrypt
981 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500982 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -0500983 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -0500984 * If the algorithm does not support a
985 * salt, pass `NULL`.
986 * If the algorithm supports an optional
987 * salt and you do not want to pass a salt,
988 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -0500989 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
990 * supported.
991 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500992 * If `p_salt` is `NULL`, pass 0.
993 * \param[out] p_output Buffer where the decrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -0500994 * be written
995 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500996 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -0500997 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -0500998 *
999 * \retval PSA_SUCCESS
1000 */
Derek Miller16e72292018-10-15 16:14:24 -05001001typedef psa_status_t (*pcd_asymmetric_opaque_decrypt_t)(psa_key_slot_t key_slot,
1002 psa_algorithm_t alg,
1003 const uint8_t *p_input,
1004 size_t input_length,
1005 const uint8_t *p_salt,
1006 size_t salt_length,
1007 uint8_t *p_output,
1008 size_t output_size,
1009 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001010
1011/**
Derek Miller16e72292018-10-15 16:14:24 -05001012 * \brief A struct containing all of the function pointers needed to implement
1013 * asymmetric cryptographic operations using opaque keys.
Derek Miller5b3417a2018-10-10 17:55:03 -05001014 *
Derek Miller16e72292018-10-15 16:14:24 -05001015 * PSA Crypto API implementations should populate instances of the table as
1016 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001017 *
1018 * If one of the functions is not implemented, it should be set to NULL.
1019 */
1020struct pcd_asymmetric_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -05001021 /** Function that performs the asymmetric sign operation */
1022 pcd_asymmetric_opaque_sign_t *p_sign;
1023 /** Function that performs the asymmetric verify operation */
1024 pcd_asymmetric_opaque_verify_t *p_verify;
1025 /** Function that performs the asymmetric encrypt operation */
1026 pcd_asymmetric_opaque_encrypt_t *p_encrypt;
1027 /** Function that performs the asymmetric decrypt operation */
1028 pcd_asymmetric_opaque_decrypt_t *p_decrypt;
Derek Miller5b3417a2018-10-10 17:55:03 -05001029};
1030
Derek Miller16e72292018-10-15 16:14:24 -05001031/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001032
1033/** \defgroup transparent_asymmetric Transparent Asymmetric Cryptography
Derek Miller765682c2018-10-22 15:27:27 -05001034 *
1035 * Since the amount of data that can (or should) be encrypted or signed using
1036 * asymmetric keys is limited by the key size, asymmetric key operations using
1037 * transparent keys must be done in single function calls.
Derek Miller5b3417a2018-10-10 17:55:03 -05001038 */
Derek Miller16e72292018-10-15 16:14:24 -05001039/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001040
1041
1042/**
Derek Miller16e72292018-10-15 16:14:24 -05001043 * \brief A function that signs a hash or short message with a transparent
Derek Miller765682c2018-10-22 15:27:27 -05001044 * asymmetric private key
Derek Miller5b3417a2018-10-10 17:55:03 -05001045 *
Derek Miller16e72292018-10-15 16:14:24 -05001046 * Functions that implement the prototype should be named in the following
1047 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001048 * ~~~~~~~~~~~~~{.c}
1049 * pcd_asymmetric_<ALGO>_sign
1050 * ~~~~~~~~~~~~~
1051 * Where `ALGO` is the name of the signing algorithm
1052 *
Derek Miller16e72292018-10-15 16:14:24 -05001053 * \param[in] p_key A buffer containing the private key
1054 * material
1055 * \param[in] key_size The size in bytes of the `p_key` data
1056 * \param[in] alg A signature algorithm that is compatible
1057 * with the type of `p_key`
1058 * \param[in] p_hash The hash or message to sign
1059 * \param[in] hash_length Size of the `p_hash` buffer in bytes
1060 * \param[out] p_signature Buffer where the signature is to be written
1061 * \param[in] signature_size Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001062 * \param[out] p_signature_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001063 * that make up the returned signature value
Derek Miller5b3417a2018-10-10 17:55:03 -05001064 *
1065 * \retval PSA_SUCCESS
1066 */
Derek Miller16e72292018-10-15 16:14:24 -05001067typedef psa_status_t (*pcd_asymmetric_transparent_sign_t)(const uint8_t *p_key,
1068 size_t key_size,
1069 psa_algorithm_t alg,
1070 const uint8_t *p_hash,
1071 size_t hash_length,
1072 uint8_t *p_signature,
1073 size_t signature_size,
1074 size_t *p_signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001075
1076/**
Derek Miller16e72292018-10-15 16:14:24 -05001077 * \brief A function that verifies the signature a hash or short message using
Derek Miller765682c2018-10-22 15:27:27 -05001078 * a transparent asymmetric public key
Derek Miller5b3417a2018-10-10 17:55:03 -05001079 *
Derek Miller16e72292018-10-15 16:14:24 -05001080 * Functions that implement the prototype should be named in the following
1081 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001082 * ~~~~~~~~~~~~~{.c}
1083 * pcd_asymmetric_<ALGO>_verify
1084 * ~~~~~~~~~~~~~
1085 * Where `ALGO` is the name of the signing algorithm
1086 *
Derek Miller16e72292018-10-15 16:14:24 -05001087 * \param[in] p_key A buffer containing the public key material
1088 * \param[in] key_size The size in bytes of the `p_key` data
1089 * \param[in] alg A signature algorithm that is compatible with
1090 * the type of `key`
1091 * \param[in] p_hash The hash or message whose signature is to be
1092 * verified
1093 * \param[in] hash_length Size of the `p_hash` buffer in bytes
1094 * \param[in] p_signature Buffer containing the signature to verify
1095 * \param[in] signature_length Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001096 *
1097 * \retval PSA_SUCCESS
1098 * The signature is valid.
1099 */
Derek Miller16e72292018-10-15 16:14:24 -05001100typedef psa_status_t (*pcd_asymmetric_transparent_verify_t)(const uint8_t *p_key,
1101 size_t key_size,
1102 psa_algorithm_t alg,
1103 const uint8_t *p_hash,
1104 size_t hash_length,
1105 const uint8_t *p_signature,
1106 size_t signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001107
1108/**
Derek Miller765682c2018-10-22 15:27:27 -05001109 * \brief A function that encrypts a short message with a transparent
1110 * asymmetric public key
Derek Miller5b3417a2018-10-10 17:55:03 -05001111 *
Derek Miller16e72292018-10-15 16:14:24 -05001112 * Functions that implement the prototype should be named in the following
1113 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001114 * ~~~~~~~~~~~~~{.c}
1115 * pcd_asymmetric_<ALGO>_encrypt
1116 * ~~~~~~~~~~~~~
1117 * Where `ALGO` is the name of the encryption algorithm
1118 *
Derek Miller16e72292018-10-15 16:14:24 -05001119 * \param[in] p_key A buffer containing the public key material
1120 * \param[in] key_size The size in bytes of the `p_key` data
1121 * \param[in] alg An asymmetric encryption algorithm that is
1122 * compatible with the type of `key`
1123 * \param[in] p_input The message to encrypt
1124 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001125 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -05001126 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -05001127 * If the algorithm does not support a
Derek Miller16e72292018-10-15 16:14:24 -05001128 * salt, pass `NULL`
Derek Miller5b3417a2018-10-10 17:55:03 -05001129 * If the algorithm supports an optional
1130 * salt and you do not want to pass a salt,
1131 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -05001132 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1133 * supported.
1134 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001135 * If `p_salt` is `NULL`, pass 0.
1136 * \param[out] p_output Buffer where the encrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -05001137 * be written
1138 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001139 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001140 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -05001141 *
1142 * \retval PSA_SUCCESS
1143 */
Derek Miller16e72292018-10-15 16:14:24 -05001144typedef psa_status_t (*pcd_asymmetric_transparent_encrypt_t)(const uint8_t *p_key,
Derek Miller5b3417a2018-10-10 17:55:03 -05001145 size_t key_size,
1146 psa_algorithm_t alg,
1147 const uint8_t *p_input,
1148 size_t input_length,
1149 const uint8_t *p_salt,
1150 size_t salt_length,
1151 uint8_t *p_output,
1152 size_t output_size,
Derek Miller16e72292018-10-15 16:14:24 -05001153 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001154
1155/**
Derek Miller765682c2018-10-22 15:27:27 -05001156 * \brief Decrypt a short message with a transparent asymmetric private key
Derek Miller5b3417a2018-10-10 17:55:03 -05001157 *
Derek Miller16e72292018-10-15 16:14:24 -05001158 * Functions that implement the prototype should be named in the following
1159 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001160 * ~~~~~~~~~~~~~{.c}
1161 * pcd_asymmetric_<ALGO>_decrypt
1162 * ~~~~~~~~~~~~~
1163 * Where `ALGO` is the name of the encryption algorithm
1164 *
Derek Miller16e72292018-10-15 16:14:24 -05001165 * \param[in] p_key A buffer containing the private key material
1166 * \param[in] key_size The size in bytes of the `p_key` data
1167 * \param[in] alg An asymmetric encryption algorithm that is
1168 * compatible with the type of `key`
1169 * \param[in] p_input The message to decrypt
1170 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001171 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -05001172 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -05001173 * If the algorithm does not support a
1174 * salt, pass `NULL`.
1175 * If the algorithm supports an optional
1176 * salt and you do not want to pass a salt,
1177 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -05001178 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1179 * supported
1180 * \param[in] salt_length Size of the `p_salt` buffer in bytes
1181 * If `p_salt` is `NULL`, pass 0
Derek Miller5b3417a2018-10-10 17:55:03 -05001182 * \param[out] p_output Buffer where the decrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -05001183 * be written
1184 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001185 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001186 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -05001187 *
1188 * \retval PSA_SUCCESS
1189 */
Derek Miller16e72292018-10-15 16:14:24 -05001190typedef psa_status_t (*pcd_asymmetric_transparent_decrypt_t)(const uint8_t *p_key,
1191 size_t key_size,
1192 psa_algorithm_t alg,
1193 const uint8_t *p_input,
1194 size_t input_length,
1195 const uint8_t *p_salt,
1196 size_t salt_length,
1197 uint8_t *p_output,
1198 size_t output_size,
1199 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001200
Derek Miller16e72292018-10-15 16:14:24 -05001201/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001202
1203/** \defgroup aead_opaque AEAD Opaque
Derek Miller765682c2018-10-22 15:27:27 -05001204 * Authenticated Encryption with Additional Data (AEAD) operations with opaque
1205 * keys must be done in one function call. While this creates a burden for
1206 * implementers as there must be sufficient space in memory for the entire
1207 * message, it prevents decrypted data from being made available before the
1208 * authentication operation is complete and the data is known to be authentic.
Derek Miller5b3417a2018-10-10 17:55:03 -05001209 */
Derek Miller16e72292018-10-15 16:14:24 -05001210/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001211
Derek Miller16e72292018-10-15 16:14:24 -05001212/** \brief Process an authenticated encryption operation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -05001213 *
Derek Miller16e72292018-10-15 16:14:24 -05001214 * \param[in] key_slot Slot containing the key to use.
1215 * \param[in] algorithm The AEAD algorithm to compute
1216 * (\c PSA_ALG_XXX value such that
1217 * #PSA_ALG_IS_AEAD(`alg`) is true)
1218 * \param[in] p_nonce Nonce or IV to use
1219 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
1220 * \param[in] p_additional_data Additional data that will be
1221 * authenticated but not encrypted
1222 * \param[in] additional_data_length Size of `p_additional_data` in bytes
1223 * \param[in] p_plaintext Data that will be authenticated and
1224 * encrypted
1225 * \param[in] plaintext_length Size of `p_plaintext` in bytes
1226 * \param[out] p_ciphertext Output buffer for the authenticated and
1227 * encrypted data. The additional data is
1228 * not part of this output. For algorithms
1229 * where the encrypted data and the
1230 * authentication tag are defined as
1231 * separate outputs, the authentication
1232 * tag is appended to the encrypted data.
1233 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
1234 * bytes
1235 * \param[out] p_ciphertext_length On success, the size of the output in
1236 * the `p_ciphertext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001237 *
1238 * \retval #PSA_SUCCESS
1239 * Success.
1240 */
Derek Miller16e72292018-10-15 16:14:24 -05001241typedef psa_status_t (*psa_aead_opaque_encrypt_t)(psa_key_slot_t key_slot,
Derek Miller5b3417a2018-10-10 17:55:03 -05001242 psa_algorithm_t algorithm,
1243 const uint8_t *p_nonce,
1244 size_t nonce_length,
1245 const uint8_t *p_additional_data,
1246 size_t additional_data_length,
1247 const uint8_t *p_plaintext,
1248 size_t plaintext_length,
1249 uint8_t *p_ciphertext,
1250 size_t ciphertext_size,
1251 size_t *p_ciphertext_length);
1252
Derek Miller16e72292018-10-15 16:14:24 -05001253/** Process an authenticated decryption operation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -05001254 *
Derek Miller16e72292018-10-15 16:14:24 -05001255 * \param[in] key_slot Slot containing the key to use
1256 * \param[in] algorithm The AEAD algorithm to compute
1257 * (\c PSA_ALG_XXX value such that
1258 * #PSA_ALG_IS_AEAD(`alg`) is true)
1259 * \param[in] p_nonce Nonce or IV to use
1260 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
1261 * \param[in] p_additional_data Additional data that has been
1262 * authenticated but not encrypted
1263 * \param[in] additional_data_length Size of `p_additional_data` in bytes
1264 * \param[in] p_ciphertext Data that has been authenticated and
1265 * encrypted.
1266 * For algorithms where the encrypted data
1267 * and the authentication tag are defined
1268 * as separate inputs, the buffer must
1269 * contain the encrypted data followed by
1270 * the authentication tag.
1271 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
1272 * \param[out] p_plaintext Output buffer for the decrypted data
1273 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
1274 * bytes
1275 * \param[out] p_plaintext_length On success, the size of the output in
1276 * the `p_plaintext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001277 *
1278 * \retval #PSA_SUCCESS
1279 * Success.
1280 */
Derek Miller16e72292018-10-15 16:14:24 -05001281typedef psa_status_t (*psa_aead_opaque_decrypt_t)(psa_key_slot_t key_slot,
1282 psa_algorithm_t algorithm,
1283 const uint8_t *p_nonce,
1284 size_t nonce_length,
1285 const uint8_t *p_additional_data,
1286 size_t additional_data_length,
1287 const uint8_t *p_ciphertext,
1288 size_t ciphertext_length,
1289 uint8_t *p_plaintext,
1290 size_t plaintext_size,
1291 size_t *p_plaintext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001292
1293/**
Derek Miller16e72292018-10-15 16:14:24 -05001294 * \brief A struct containing all of the function pointers needed to implement
1295 * Authenticated Encryption with Additional Data operations using opaque keys
Derek Miller5b3417a2018-10-10 17:55:03 -05001296 *
Derek Miller16e72292018-10-15 16:14:24 -05001297 * PSA Crypto API implementations should populate instances of the table as
1298 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001299 *
1300 * If one of the functions is not implemented, it should be set to NULL.
1301 */
1302struct psa_aead_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -05001303 /** Function that performs the AEAD encrypt operation */
1304 psa_aead_opaque_encrypt_t *p_encrypt;
1305 /** Function that performs the AEAD decrypt operation */
1306 psa_aead_opaque_decrypt_t *p_decrypt;
Derek Miller5b3417a2018-10-10 17:55:03 -05001307};
Derek Miller16e72292018-10-15 16:14:24 -05001308/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001309
1310/** \defgroup aead_transparent AEAD Transparent
Derek Miller765682c2018-10-22 15:27:27 -05001311 *
1312 * Authenticated Encryption with Additional Data (AEAD) operations with
1313 * transparent keys must be done in one function call. While this creates a
1314 * burden for implementers as there must be sufficient space in memory for the
1315 * entire message, it prevents decrypted data from being made available before
1316 * the authentication operation is complete and the data is known to be
1317 * authentic.
Derek Miller5b3417a2018-10-10 17:55:03 -05001318 */
Derek Miller16e72292018-10-15 16:14:24 -05001319/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001320
Derek Miller765682c2018-10-22 15:27:27 -05001321/** Process an authenticated encryption operation using an opaque key.
Derek Miller5b3417a2018-10-10 17:55:03 -05001322 *
Derek Miller16e72292018-10-15 16:14:24 -05001323 * Functions that implement the prototype should be named in the following
1324 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001325 * ~~~~~~~~~~~~~{.c}
1326 * pcd_aead_<ALGO>_encrypt
1327 * ~~~~~~~~~~~~~
1328 * Where `ALGO` is the name of the AEAD algorithm
1329 *
Derek Miller16e72292018-10-15 16:14:24 -05001330 * \param[in] p_key A pointer to the key material
1331 * \param[in] key_length The size in bytes of the key material
1332 * \param[in] alg The AEAD algorithm to compute
1333 * (\c PSA_ALG_XXX value such that
1334 * #PSA_ALG_IS_AEAD(`alg`) is true)
1335 * \param[in] nonce Nonce or IV to use
1336 * \param[in] nonce_length Size of the `nonce` buffer in bytes
1337 * \param[in] additional_data Additional data that will be MACed
1338 * but not encrypted.
1339 * \param[in] additional_data_length Size of `additional_data` in bytes
1340 * \param[in] plaintext Data that will be MACed and
1341 * encrypted.
1342 * \param[in] plaintext_length Size of `plaintext` in bytes
1343 * \param[out] ciphertext Output buffer for the authenticated and
1344 * encrypted data. The additional data is
1345 * not part of this output. For algorithms
1346 * where the encrypted data and the
1347 * authentication tag are defined as
1348 * separate outputs, the authentication
1349 * tag is appended to the encrypted data.
1350 * \param[in] ciphertext_size Size of the `ciphertext` buffer in
1351 * bytes
1352 * This must be at least
1353 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(`alg`,
1354 * `plaintext_length`).
1355 * \param[out] ciphertext_length On success, the size of the output in
1356 * the `ciphertext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001357 *
1358 * \retval #PSA_SUCCESS
1359
1360 */
Derek Miller16e72292018-10-15 16:14:24 -05001361typedef psa_status_t (*psa_aead_transparent_encrypt_t)(const uint8_t *p_key,
1362 size_t key_length,
1363 psa_algorithm_t alg,
1364 const uint8_t *nonce,
1365 size_t nonce_length,
1366 const uint8_t *additional_data,
1367 size_t additional_data_length,
1368 const uint8_t *plaintext,
1369 size_t plaintext_length,
1370 uint8_t *ciphertext,
1371 size_t ciphertext_size,
1372 size_t *ciphertext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001373
Derek Miller765682c2018-10-22 15:27:27 -05001374/** Process an authenticated decryption operation using an opaque key.
Derek Miller5b3417a2018-10-10 17:55:03 -05001375 *
Derek Miller16e72292018-10-15 16:14:24 -05001376 * Functions that implement the prototype should be named in the following
1377 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001378 * ~~~~~~~~~~~~~{.c}
1379 * pcd_aead_<ALGO>_decrypt
1380 * ~~~~~~~~~~~~~
1381 * Where `ALGO` is the name of the AEAD algorithm
Derek Miller16e72292018-10-15 16:14:24 -05001382 * \param[in] p_key A pointer to the key material
1383 * \param[in] key_length The size in bytes of the key material
1384 * \param[in] alg The AEAD algorithm to compute
1385 * (\c PSA_ALG_XXX value such that
1386 * #PSA_ALG_IS_AEAD(`alg`) is true)
1387 * \param[in] nonce Nonce or IV to use
1388 * \param[in] nonce_length Size of the `nonce` buffer in bytes
1389 * \param[in] additional_data Additional data that has been MACed
1390 * but not encrypted
1391 * \param[in] additional_data_length Size of `additional_data` in bytes
1392 * \param[in] ciphertext Data that has been MACed and
1393 * encrypted
1394 * For algorithms where the encrypted data
1395 * and the authentication tag are defined
1396 * as separate inputs, the buffer must
1397 * contain the encrypted data followed by
1398 * the authentication tag.
1399 * \param[in] ciphertext_length Size of `ciphertext` in bytes
1400 * \param[out] plaintext Output buffer for the decrypted data
1401 * \param[in] plaintext_size Size of the `plaintext` buffer in
1402 * bytes
1403 * This must be at least
1404 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(`alg`,
1405 * `ciphertext_length`).
1406 * \param[out] plaintext_length On success, the size of the output
1407 * in the \b plaintext buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001408 *
1409 * \retval #PSA_SUCCESS
1410 * Success.
1411 */
Derek Miller16e72292018-10-15 16:14:24 -05001412typedef psa_status_t (*psa_aead_transparent_decrypt_t)(const uint8_t *p_key,
1413 size_t key_length,
1414 psa_algorithm_t alg,
1415 const uint8_t *nonce,
1416 size_t nonce_length,
1417 const uint8_t *additional_data,
1418 size_t additional_data_length,
1419 const uint8_t *ciphertext,
1420 size_t ciphertext_length,
1421 uint8_t *plaintext,
1422 size_t plaintext_size,
1423 size_t *plaintext_length);
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
Derek Miller16e72292018-10-15 16:14:24 -05001428/** \defgroup driver_rng Entropy Generation
Derek Miller5b3417a2018-10-10 17:55:03 -05001429 */
Derek Miller16e72292018-10-15 16:14:24 -05001430/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001431
1432/** \brief A hardware-specific structure for a entropy providing hardware
1433 */
1434struct pcd_entropy_context_t {
1435 // Implementation specific
1436};
1437
1438/** \brief Initialize an entropy driver
1439 *
1440 *
Derek Miller16e72292018-10-15 16:14:24 -05001441 * \param[in,out] p_context A hardware-specific structure
1442 * containing any context information for
1443 * the implementation
Derek Miller5b3417a2018-10-10 17:55:03 -05001444 *
1445 * \retval PSA_SUCCESS
1446 */
Derek Miller16e72292018-10-15 16:14:24 -05001447typedef psa_status_t (*pcd_entropy_init_t)(struct pcd_entropy_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -05001448
1449/** \brief Get a specified number of bytes from the entropy source
1450 *
Derek Miller16e72292018-10-15 16:14:24 -05001451 * It retrives `buffer_size` bytes of data from the entropy source. The entropy
1452 * source will always fill the provided buffer to its full size, however, most
1453 * entropy sources have biases, and the actual amount of entropy contained in
1454 * the buffer will be less than the number of bytes.
1455 * The driver will return the actual number of bytes of entropy placed in the
1456 * buffer in `p_received_entropy_bytes`.
1457 * A PSA Crypto API implementation will likely feed the output of this function
1458 * into a Digital Random Bit Generator (DRBG), and typically has a minimum
1459 * amount of entropy that it needs.
1460 * To accomplish this, the PSA Crypto implementation should be designed to call
1461 * this function multiple times until it has received the required amount of
1462 * entropy from the entropy source.
Derek Miller5b3417a2018-10-10 17:55:03 -05001463 *
Derek Miller16e72292018-10-15 16:14:24 -05001464 * \param[in,out] p_context A hardware-specific structure
1465 * containing any context information
1466 * for the implementation
1467 * \param[out] p_buffer A caller-allocated buffer for the
1468 * retrieved bytes to be placed in
1469 * \param[in] buffer_size The allocated size of `p_buffer`
1470 * \param[out] p_received_entropy_bytes The amount of entropy (in bytes)
1471 * actually provided in `p_buffer`
Derek Miller5b3417a2018-10-10 17:55:03 -05001472 *
1473 * \retval PSA_SUCCESS
1474 */
Derek Miller16e72292018-10-15 16:14:24 -05001475typedef psa_status_t (*pcd_entropy_get_bytes_t)(struct pcd_entropy_context_t *p_context,
1476 uint8_t *p_buffer,
1477 uint32_t buffer_size,
1478 uint32_t *p_received_entropy_bytes);
Derek Miller5b3417a2018-10-10 17:55:03 -05001479
1480/**
Derek Miller16e72292018-10-15 16:14:24 -05001481 * \brief A struct containing all of the function pointers needed to interface
1482 * to an entropy source
Derek Miller5b3417a2018-10-10 17:55:03 -05001483 *
Derek Miller16e72292018-10-15 16:14:24 -05001484 * PSA Crypto API implementations should populate instances of the table as
1485 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001486 *
1487 * If one of the functions is not implemented, it should be set to NULL.
1488 */
1489struct pcd_entropy_t {
Derek Miller16e72292018-10-15 16:14:24 -05001490 /** Function that performs initialization for the entropy source */
1491 pcd_entropy_init_t *p_init;
1492 /** Function that performs the get_bytes operation for the entropy source
1493 */
1494 pcd_entropy_get_bytes_t *p_get_bytes;
Derek Miller5b3417a2018-10-10 17:55:03 -05001495};
Derek Miller16e72292018-10-15 16:14:24 -05001496/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001497
Derek Miller16e72292018-10-15 16:14:24 -05001498/** \defgroup driver_key_management Key Management
Derek Miller765682c2018-10-22 15:27:27 -05001499 * Currently, key management is limited to importing keys in the clear,
1500 * destroying keys, and exporting keys in the clear.
1501 * Whether a key may be exported is determined by the key policies in place
1502 * on the key slot.
Derek Miller5b3417a2018-10-10 17:55:03 -05001503 */
Derek Miller16e72292018-10-15 16:14:24 -05001504/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001505
Derek Miller16e72292018-10-15 16:14:24 -05001506/** \brief Import a key in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001507 *
1508 * This function can support any output from psa_export_key(). Refer to the
1509 * documentation of psa_export_key() for the format for each key type.
1510 *
Derek Miller16e72292018-10-15 16:14:24 -05001511 * \param[in] key_slot Slot where the key will be stored. This must be a
1512 * valid slot for a key of the chosen type. It must
1513 * be unoccupied.
1514 * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value).
1515 * \param[in] p_data Buffer containing the key data.
1516 * \param[in] data_length Size of the `data` buffer in bytes.
Derek Miller5b3417a2018-10-10 17:55:03 -05001517 *
1518 * \retval #PSA_SUCCESS
1519 * Success.
1520 */
Derek Miller16e72292018-10-15 16:14:24 -05001521typedef psa_status_t (*pcd_opaque_import_key_t)(psa_key_slot_t key_slot,
1522 psa_key_type_t type,
1523 const uint8_t *p_data,
1524 size_t data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001525
1526/**
Derek Miller16e72292018-10-15 16:14:24 -05001527 * \brief Destroy a key and restore the slot to its default state
Derek Miller5b3417a2018-10-10 17:55:03 -05001528 *
1529 * This function destroys the content of the key slot from both volatile
1530 * memory and, if applicable, non-volatile storage. Implementations shall
1531 * make a best effort to ensure that any previous content of the slot is
1532 * unrecoverable.
1533 *
1534 * This function also erases any metadata such as policies. It returns the
1535 * specified slot to its default state.
1536 *
Derek Miller16e72292018-10-15 16:14:24 -05001537 * \param[in] key_slot The key slot to erase.
Derek Miller5b3417a2018-10-10 17:55:03 -05001538 *
1539 * \retval #PSA_SUCCESS
1540 * The slot's content, if any, has been erased.
1541 */
Derek Miller16e72292018-10-15 16:14:24 -05001542typedef psa_status_t (*pcd_destroy_key_t)(psa_key_slot_t key);
Derek Miller5b3417a2018-10-10 17:55:03 -05001543
1544/**
Derek Miller16e72292018-10-15 16:14:24 -05001545 * \brief Export a key in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001546 *
1547 * The output of this function can be passed to psa_import_key() to
1548 * create an equivalent object.
1549 *
Derek Miller16e72292018-10-15 16:14:24 -05001550 * If a key is created with `psa_import_key()` and then exported with
Derek Miller5b3417a2018-10-10 17:55:03 -05001551 * this function, it is not guaranteed that the resulting data is
1552 * identical: the implementation may choose a different representation
1553 * of the same key if the format permits it.
1554 *
1555 * For standard key types, the output format is as follows:
1556 *
1557 * - For symmetric keys (including MAC keys), the format is the
1558 * raw bytes of the key.
1559 * - For DES, the key data consists of 8 bytes. The parity bits must be
1560 * correct.
1561 * - For Triple-DES, the format is the concatenation of the
1562 * two or three DES keys.
1563 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
1564 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1565 * as RSAPrivateKey.
1566 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
1567 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
1568 *
Derek Miller16e72292018-10-15 16:14:24 -05001569 * \param[in] key Slot whose content is to be exported. This must
Derek Miller5b3417a2018-10-10 17:55:03 -05001570 * be an occupied key slot.
1571 * \param[out] p_data Buffer where the key data is to be written.
Derek Miller16e72292018-10-15 16:14:24 -05001572 * \param[in] data_size Size of the `p_data` buffer in bytes.
Derek Miller5b3417a2018-10-10 17:55:03 -05001573 * \param[out] p_data_length On success, the number of bytes
1574 * that make up the key data.
1575 *
1576 * \retval #PSA_SUCCESS
1577 * \retval #PSA_ERROR_EMPTY_SLOT
1578 * \retval #PSA_ERROR_NOT_PERMITTED
1579 * \retval #PSA_ERROR_NOT_SUPPORTED
1580 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1581 * \retval #PSA_ERROR_HARDWARE_FAILURE
1582 * \retval #PSA_ERROR_TAMPERING_DETECTED
1583 */
Derek Miller16e72292018-10-15 16:14:24 -05001584typedef psa_status_t (*pcd_export_key_t)(psa_key_slot_t key,
1585 uint8_t *p_data,
1586 size_t data_size,
1587 size_t *p_data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001588
1589/**
Derek Miller16e72292018-10-15 16:14:24 -05001590 * \brief Export a public key or the public part of a key pair in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001591 *
1592 * The output of this function can be passed to psa_import_key() to
1593 * create an object that is equivalent to the public key.
1594 *
1595 * For standard key types, the output format is as follows:
1596 *
1597 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1598 * the format is the DER representation of the public key defined by RFC 5280
1599 * as SubjectPublicKeyInfo.
1600 *
Derek Miller16e72292018-10-15 16:14:24 -05001601 * \param[in] key_slot Slot whose content is to be exported. This must
Derek Miller5b3417a2018-10-10 17:55:03 -05001602 * be an occupied key slot.
1603 * \param[out] p_data Buffer where the key data is to be written.
Derek Miller16e72292018-10-15 16:14:24 -05001604 * \param[in] data_size Size of the `data` buffer in bytes.
Derek Miller5b3417a2018-10-10 17:55:03 -05001605 * \param[out] p_data_length On success, the number of bytes
1606 * that make up the key data.
1607 *
1608 * \retval #PSA_SUCCESS
1609 */
Derek Miller16e72292018-10-15 16:14:24 -05001610typedef psa_status_t (*pcd_export_public_key_t)(psa_key_slot_t key,
1611 uint8_t *p_data,
1612 size_t data_size,
1613 size_t *p_data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001614
1615/**
Derek Miller16e72292018-10-15 16:14:24 -05001616 * \brief A struct containing all of the function pointers needed to for key
1617 * management using opaque keys
Derek Miller5b3417a2018-10-10 17:55:03 -05001618 *
Derek Miller16e72292018-10-15 16:14:24 -05001619 * PSA Crypto API implementations should populate instances of the table as
1620 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001621 *
1622 * If one of the functions is not implemented, it should be set to NULL.
1623 */
1624struct pcd_key_management_t {
Derek Miller16e72292018-10-15 16:14:24 -05001625 /** Function that performs the key import operation */
1626 pcd_opaque_import_key_t *p_import;
1627 /** Function that performs the key destroy operation */
1628 pcd_destroy_key_t *p_destroy;
1629 /** Function that performs the key export operation */
1630 pcd_export_key_t *p_export;
1631 /** Function that perforsm the public key export operation */
1632 pcd_export_public_key_t *p_export_public;
Derek Miller5b3417a2018-10-10 17:55:03 -05001633};
1634
Derek Miller16e72292018-10-15 16:14:24 -05001635/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001636
Derek Miller16e72292018-10-15 16:14:24 -05001637/** \defgroup driver_derivation Key Derivation and Agreement
Derek Miller16e72292018-10-15 16:14:24 -05001638 * Key derivation is the process of generating new key material using an
1639 * existing key and additional parameters, iterating through a basic
1640 * cryptographic function, such as a hash.
1641 * Key agreement is a part of cryptographic protocols that allows two parties
1642 * to agree on the same key value, but starting from different original key
1643 * material.
1644 * The flows are similar, and the PSA Crypto Driver API uses the same functions
1645 * for both of the flows.
Derek Miller5b3417a2018-10-10 17:55:03 -05001646 *
Derek Miller16e72292018-10-15 16:14:24 -05001647 * There are two different final functions for the flows,
1648 * `pcd_key_derivation_derive` and `pcd_key_derivation_export`.
1649 * `pcd_key_derivation_derive` is used when the key material should be placed
1650 * in a slot on the hardware and not exposed to the caller.
1651 * `pcd_key_derivation_export` is used when the key material should be returned
1652 * to the PSA Cryptographic API implementation.
1653 *
1654 * Different key derivation algorithms require a different number of inputs.
1655 * Instead of having an API that takes as input variable length arrays, which
1656 * can be problemmatic to manage on embedded platforms, the inputs are passed
1657 * to the driver via a function, `pcd_key_derivation_collateral`, that is
1658 * called multiple times with different `collateral_id`s. Thus, for a key
1659 * derivation algorithm that required 3 paramter inputs, the flow would look
1660 * something like:
1661 * ~~~~~~~~~~~~~{.c}
1662 * pcd_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1663 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_0,
1664 * p_collateral_0,
1665 * collateral_0_size);
1666 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_1,
1667 * p_collateral_1,
1668 * collateral_1_size);
1669 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_2,
1670 * p_collateral_2,
1671 * collateral_2_size);
1672 * pcd_key_derivation_derive();
1673 * ~~~~~~~~~~~~~
1674 *
1675 * key agreement example:
1676 * ~~~~~~~~~~~~~{.c}
1677 * pcd_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1678 * pcd_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1679 * pcd_key_derivation_export(p_session_key,
1680 * session_key_size,
1681 * &session_key_length);
1682 * ~~~~~~~~~~~~~
1683 */
Derek Miller765682c2018-10-22 15:27:27 -05001684/**@{*/
Derek Miller16e72292018-10-15 16:14:24 -05001685
Derek Miller765682c2018-10-22 15:27:27 -05001686/** \brief The hardware-specific key derivation context structure
1687 *
1688 * The contents of this structure are implementation dependent and are
1689 * therefore not described here
1690 */
Derek Miller16e72292018-10-15 16:14:24 -05001691struct pcd_key_derivation_context_t {
1692 // Implementation specific
1693};
1694
1695/** \brief Set up a key derivation operation by specifying the algorithm and
1696 * the source key sot
1697 *
1698 * \param[in,out] p_context A hardware-specific structure containing any
1699 * context information for the implementation
1700 * \param[in] kdf_alg The algorithm to be used for the key derivation
1701 * \param[in] souce_key The key to be used as the source material for the
1702 * key derivation
1703 *
1704 * \retval PSA_SUCCESS
1705 */
1706typedef psa_status_t (*pcd_key_derivation_setup_t)(struct pcd_key_derivation_context_t *p_context,
1707 psa_algorithm_t kdf_alg,
1708 psa_key_slot_t source_key);
1709
1710/** \brief Provide collateral (parameters) needed for a key derivation or key
1711 * agreement operation
1712 *
1713 * Since many key derivation algorithms require multiple parameters, it is
1714 * expeced that this function may be called multiple times for the same
1715 * operation, each with a different algorithm-specific `collateral_id`
1716 *
1717 * \param[in,out] p_context A hardware-specific structure containing any
1718 * context information for the implementation
1719 * \param[in] collateral_id An ID for the collateral being provided
1720 * \param[in] p_collateral A buffer containing the collateral data
1721 * \param[in] collateral_size The size in bytes of the collateral
1722 *
1723 * \retval PSA_SUCCESS
1724 */
1725typedef psa_status_t (*pcd_key_derivation_collateral_t)(struct pcd_key_derivation_context_t *p_context,
1726 uint32_t collateral_id,
1727 const uint8_t p_collateral,
1728 size_t collateral_size);
1729
1730/** \brief Perform the final key derivation step and place the generated key
1731 * material in a slot
1732 * \param[in,out] p_context A hardware-specific structure containing any
1733 * context information for the implementation
1734 * \param[in] dest_key The slot where the generated key material
1735 * should be placed
1736 *
1737 * \retval PSA_SUCCESS
1738 */
1739typedef psa_status_t (*pcd_key_derivation_derive_t)(struct pcd_key_derivation_context_t *p_context,
1740 psa_key_slot_t dest_key);
1741
1742/** \brief Perform the final step of a key agreement and place the generated
1743 * key material in a buffer
1744 *
1745 * \param[out] p_output Buffer in which to place the generated key
1746 * material
1747 * \param[in] output_size The size in bytes of `p_output`
1748 * \param[out] p_output_length Upon success, contains the number of bytes of
1749 * key material placed in `p_output`
1750 *
1751 * \retval PSA_SUCCESS
1752 */
1753typedef psa_status_t (*pcd_key_derivation_export_t)(uint8_t *p_output,
1754 size_t output_size,
1755 size_t *p_output_length);
1756
1757/**
1758 * \brief A struct containing all of the function pointers needed to for key
1759 * derivation and agreement
1760 *
1761 * PSA Crypto API implementations should populate instances of the table as
1762 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001763 *
1764 * If one of the functions is not implemented, it should be set to NULL.
1765 */
1766struct pcd_key_derivation_t {
Derek Miller16e72292018-10-15 16:14:24 -05001767 /** Function that performs the key derivation setup */
1768 pcd_key_derivation_setup_t *p_setup;
1769 /** Function that sets the key derivation collateral */
1770 pcd_key_derivation_collateral_t *p_collateral;
1771 /** Function that performs the final key derivation step */
1772 pcd_key_derivation_derive_t *p_derive;
1773 /** Function that perforsm the final key derivation or agreement and
1774 * exports the key */
1775 pcd_key_derivation_export_t *p_export;
Derek Miller5b3417a2018-10-10 17:55:03 -05001776};
1777
Derek Miller16e72292018-10-15 16:14:24 -05001778/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001779
1780#endif // __PSA_CRYPTO_DRIVER_H__