blob: c571764cea9e916cfa8f9b2481bdd542bf2101c5 [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 Miller5b3417a2018-10-10 17:55:03 -050044 */
Derek Miller16e72292018-10-15 16:14:24 -050045/**@{*/
46/** \brief A function that starts a MAC operation for a PSA Crypto Driver
47 * implementation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -050048 *
Derek Miller16e72292018-10-15 16:14:24 -050049 * \param[in,out] p_context A structure that will contain the
50 * hardware-specific MAC context
51 * \param[in] key_slot The slot of the key to be used for the
52 * operation
53 * \param[in] algorithm The algorithm to be used to underly the MAC
54 * operation
Derek Miller5b3417a2018-10-10 17:55:03 -050055 *
56 * \retval PSA_SUCCESS
57 * Success.
58 */
Derek Miller16e72292018-10-15 16:14:24 -050059typedef psa_status_t (*pcd_mac_opaque_setup_t)(void *p_context,
60 psa_key_slot_t key_slot,
61 psa_algorithm_t algorithm);
Derek Miller5b3417a2018-10-10 17:55:03 -050062
Derek Miller16e72292018-10-15 16:14:24 -050063/** \brief A function that continues a previously started MAC operation using
64 * an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -050065 *
Derek Miller16e72292018-10-15 16:14:24 -050066 * \param[in,out] p_context A hardware-specific structure for the
67 * previously-established MAC operation to be
68 * continued
69 * \param[in] p_input A buffer containing the message to be appended
70 * to the MAC operation
71 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -050072 */
Derek Miller16e72292018-10-15 16:14:24 -050073typedef psa_status_t (*pcd_mac_opaque_update_t)(void *p_context,
74 const uint8_t *p_input,
75 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -050076
Derek Miller16e72292018-10-15 16:14:24 -050077/** \brief a function that completes a previously started MAC operation by
78 * returning the resulting MAC using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -050079 *
Derek Millerf3d0a562018-10-18 16:41:08 -050080 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -050081 * previously started MAC operation to be
82 * finished
83 * \param[out] p_mac A buffer where the generated MAC will be
84 * placed
85 * \param[in] mac_size The size in bytes of the buffer that has been
86 * allocated for the `output` buffer
87 * \param[out] p_mac_length After completion, will contain the number of
Derek Millerf3d0a562018-10-18 16:41:08 -050088 * bytes placed in the `p_mac` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -050089 *
90 * \retval PSA_SUCCESS
91 * Success.
92 */
Derek Miller16e72292018-10-15 16:14:24 -050093typedef psa_status_t (*pcd_mac_opaque_finish_t)(void *p_context,
94 uint8_t *p_mac,
95 size_t mac_size,
96 size_t *p_mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -050097
Derek Miller16e72292018-10-15 16:14:24 -050098/** \brief A function that completes a previously started MAC operation by
99 * comparing the resulting MAC against a known value using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -0500100 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500101 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500102 * started MAC operation to be fiinished
103 * \param[in] p_mac The MAC value against which the resulting MAC will
104 * be compared against
105 * \param[in] mac_length The size in bytes of the value stored in `p_mac`
Derek Miller5b3417a2018-10-10 17:55:03 -0500106 *
107 * \retval PSA_SUCCESS
Derek Miller16e72292018-10-15 16:14:24 -0500108 * The operation completed successfully and the MACs matched each
109 * other
Derek Miller5b3417a2018-10-10 17:55:03 -0500110 * \retval PSA_ERROR_INVALID_SIGNATURE
Derek Miller16e72292018-10-15 16:14:24 -0500111 * The operation completed successfully, but the calculated MAC did
112 * not match the provided MAC
Derek Miller5b3417a2018-10-10 17:55:03 -0500113 */
Derek Miller16e72292018-10-15 16:14:24 -0500114typedef psa_status_t (*pcd_mac_opaque_finish_verify_t)(void *p_context,
115 const uint8_t *p_mac,
116 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500117
Derek Miller16e72292018-10-15 16:14:24 -0500118/** \brief A function that aborts a previous started opaque-key MAC operation
119
Derek Millerf3d0a562018-10-18 16:41:08 -0500120 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500121 * started MAC operation to be aborted
122 */
123typedef psa_status_t (*pcd_mac_opaque_abort_t)(void *p_context);
124
125/** \brief A funciton that performs a MAC operation in one command and return
126 * the calculated MAC using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -0500127 *
Derek Miller16e72292018-10-15 16:14:24 -0500128 * \param[in] p_input A buffer containing the message to be MACed
Derek Millerf3d0a562018-10-18 16:41:08 -0500129 * \param[in] input_length The size in bytes of `p_input`
Derek Miller16e72292018-10-15 16:14:24 -0500130 * \param[in] key_slot The slot of the key to be used
Derek Millerf3d0a562018-10-18 16:41:08 -0500131 * \param[in] alg The algorithm to be used to underlie the MAC
Derek Miller16e72292018-10-15 16:14:24 -0500132 * operation
133 * \param[out] p_mac A buffer where the generated MAC will be
134 * placed
135 * \param[in] mac_size The size in bytes of the `output` buffer
136 * \param[out] p_mac_length After completion, will contain the number of
137 * bytes placed in the `output` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500138 *
139 * \retval PSA_SUCCESS
140 * Success.
141 */
Derek Miller16e72292018-10-15 16:14:24 -0500142typedef psa_status_t (*pcd_mac_opaque_generate_t)(const uint8_t *p_input,
143 size_t input_length,
144 psa_key_slot_t key_slot,
145 psa_algorithm_t alg,
146 uint8_t *p_mac,
147 size_t mac_size,
148 size_t *p_mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500149
Derek Miller16e72292018-10-15 16:14:24 -0500150/** \brief A function that performs an MAC operation in one command and
151 * compare the resulting MAC against a known value using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -0500152 *
Derek Miller16e72292018-10-15 16:14:24 -0500153 * \param[in] p_input A buffer containing the message to be MACed
154 * \param[in] input_length The size in bytes of `input`
155 * \param[in] key_slot The slot of the key to be used
156 * \param[in] alg The algorithm to be used to underlie the MAC
157 * operation
158 * \param[in] p_mac The MAC value against which the resulting MAC will
159 * be compared against
160 * \param[in] mac_length The size in bytes of `mac`
Derek Miller5b3417a2018-10-10 17:55:03 -0500161 *
162 * \retval PSA_SUCCESS
Derek Miller16e72292018-10-15 16:14:24 -0500163 * The operation completed successfully and the MACs matched each
164 * other
Derek Miller5b3417a2018-10-10 17:55:03 -0500165 * \retval PSA_ERROR_INVALID_SIGNATURE
Derek Miller16e72292018-10-15 16:14:24 -0500166 * The operation completed successfully, but the calculated MAC did
167 * not match the provided MAC
Derek Miller5b3417a2018-10-10 17:55:03 -0500168 */
Derek Miller16e72292018-10-15 16:14:24 -0500169typedef psa_status_t (*pcd_mac_opaque_verify_t)(const uint8_t *p_input,
Derek Miller5b3417a2018-10-10 17:55:03 -0500170 size_t input_length,
171 psa_key_slot_t key_slot,
172 psa_algorithm_t alg,
Derek Miller16e72292018-10-15 16:14:24 -0500173 const uint8_t *p_mac,
174 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500175
Derek Miller16e72292018-10-15 16:14:24 -0500176/** \brief A struct containing all of the function pointers needed to
177 * implement MAC operations using opaque keys.
Derek Miller5b3417a2018-10-10 17:55:03 -0500178 *
Derek Miller16e72292018-10-15 16:14:24 -0500179 * PSA Crypto API implementations should populate the table as appropriate
180 * upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -0500181 *
Derek Miller16e72292018-10-15 16:14:24 -0500182 * If one of the functions is not implemented (such as `pcd_mac_opaque_t`),
183 * it should be set to NULL.
184 *
185 * Driver implementers should ensure that they implement all of the functions
186 * that make sense for their hardware, and that they provide a full solution
187 * (for example, if they support `p_setup`, they should also support
188 * `p_update` and at least one of `p_finish` or `p_finish_verify`).
Derek Miller5b3417a2018-10-10 17:55:03 -0500189 *
190 */
191struct pcd_mac_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -0500192 /**The size in bytes of the hardware-specific Opaque-MAC Context structure
193 */
194 size_t context_size;
195 /** Function that performs the setup operation
196 */
197 pcd_mac_opaque_setup_t *p_setup;
198 /** Function that performs the update operation
199 */
200 pcd_mac_opaque_update_t *p_update;
201 /** Function that completes the operation
202 */
203 pcd_mac_opaque_finish_t *p_finish;
204 /** Function that completed a MAC operation with a verify check
205 */
206 pcd_mac_opaque_finish_verify_t *p_finish_verify;
207 /** Function that aborts a previoustly started operation
208 */
209 pcd_mac_opaque_abort_t *p_abort;
210 /** Function that performs the MAC operation in one call
211 */
212 pcd_mac_opaque_generate_t *p_mac;
213 /** Function that performs the MAC and verify operation in one call
214 */
215 pcd_mac_opaque_verify_t *p_mac_verify;
Derek Miller5b3417a2018-10-10 17:55:03 -0500216};
Derek Miller16e72292018-10-15 16:14:24 -0500217/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500218
219/** \defgroup transparent_mac Transparent Message Authentication Code
Derek Miller5b3417a2018-10-10 17:55:03 -0500220 */
Derek Miller16e72292018-10-15 16:14:24 -0500221/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500222
223/** \brief The hardware-specific transparent-key MAC context structure
Derek Miller16e72292018-10-15 16:14:24 -0500224 * The contents of this structure are implementation dependent and are
225 * therefore not described here.
Derek Miller5b3417a2018-10-10 17:55:03 -0500226 */
227struct pcd_mac_transparent_context_t {
228 // Implementation specific
229};
230
Derek Miller16e72292018-10-15 16:14:24 -0500231/** \brief The function prototype for the setup operation of a
232 * transparent-key MAC operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500233 *
Derek Miller16e72292018-10-15 16:14:24 -0500234 * Functions that implement the prototype should be named in the following
235 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500236 * ~~~~~~~~~~~~~{.c}
Derek Miller16e72292018-10-15 16:14:24 -0500237 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_setup
Derek Miller5b3417a2018-10-10 17:55:03 -0500238 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500239 * Where `ALGO` is the name of the underlying primitive, and `MAC_VARIANT`
240 * is the specific variant of a MAC operation (such as HMAC or CMAC)
Derek Miller5b3417a2018-10-10 17:55:03 -0500241 *
Derek Miller16e72292018-10-15 16:14:24 -0500242 * \param[in,out] p_context A structure that will contain the
243 * hardware-specific MAC context
244 * \param[in] p_key A buffer containing the cleartext key material
245 * to be used in the operation
246 * \param[in] key_length The size in bytes of the key material
Derek Miller5b3417a2018-10-10 17:55:03 -0500247 *
248 * \retval PSA_SUCCESS
249 * Success.
250 */
Derek Miller16e72292018-10-15 16:14:24 -0500251typedef psa_status_t (*pcd_mac_transparent_setup_t)(struct pcd_mac_transparent_context_t *p_context,
252 const uint8_t *p_key,
253 size_t key_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500254
Derek Miller16e72292018-10-15 16:14:24 -0500255/** \brief The function prototype for the update operation of a
256 * transparent-key MAC operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500257 *
Derek Miller16e72292018-10-15 16:14:24 -0500258 * Functions that implement the prototype should be named in the following
259 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500260 * ~~~~~~~~~~~~~{.c}
261 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_update
262 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500263 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT`
264 * is the specific variant of a MAC operation (such as HMAC or CMAC)
Derek Miller5b3417a2018-10-10 17:55:03 -0500265 *
Derek Miller16e72292018-10-15 16:14:24 -0500266 * \param[in,out] p_context A hardware-specific structure for the
267 * previously-established MAC operation to be
268 * continued
269 * \param[in] p_input A buffer containing the message to be appended
270 * to the MAC operation
271 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500272 */
Derek Miller16e72292018-10-15 16:14:24 -0500273typedef psa_status_t (*pcd_mac_transparent_update_t)(struct pcd_mac_transparent_context_t *p_context,
274 const uint8_t *p_input,
275 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500276
Derek Miller16e72292018-10-15 16:14:24 -0500277/** \brief The function prototype for the finish operation of a
278 * transparent-key MAC operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500279 *
Derek Miller16e72292018-10-15 16:14:24 -0500280 * Functions that implement the prototype should be named in the following
281 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500282 * ~~~~~~~~~~~~~{.c}
283 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish
284 * ~~~~~~~~~~~~~
Derek Miller16e72292018-10-15 16:14:24 -0500285 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
286 * the specific variant of a MAC operation (such as HMAC or CMAC)
Derek Miller5b3417a2018-10-10 17:55:03 -0500287 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500288 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500289 * previously started MAC operation to be
290 * finished
291 * \param[out] p_mac A buffer where the generated MAC will be placed
292 * \param[in] mac_length The size in bytes of the buffer that has been
293 * allocated for the `p_mac` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500294 *
295 * \retval PSA_SUCCESS
296 * Success.
297 */
Derek Miller16e72292018-10-15 16:14:24 -0500298typedef psa_status_t (*pcd_mac_transparent_finish_t)(struct pcd_mac_transparent_context_t *p_context,
299 uint8_t *p_mac,
300 size_t mac_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500301
Derek Miller16e72292018-10-15 16:14:24 -0500302/** \brief The function prototype for the finish and verify operation of a
303 * transparent-key MAC operation
304 *
305 * Functions that implement the prototype should be named in the following
306 * convention:
307 * ~~~~~~~~~~~~~{.c}
308 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish_verify
309 * ~~~~~~~~~~~~~
310 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
311 * the specific variant of a MAC operation (such as HMAC or CMAC)
312 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500313 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500314 * previously started MAC operation to be
Derek Millerf3d0a562018-10-18 16:41:08 -0500315 * verified and finished
Derek Miller16e72292018-10-15 16:14:24 -0500316 * \param[in] p_mac A buffer containing the MAC that will be used
317 * for verification
318 * \param[in] mac_length The size in bytes of the data in the `p_mac`
319 * buffer
320 *
321 * \retval PSA_SUCCESS
322 * The operation completed successfully and the comparison matched
Derek Miller5b3417a2018-10-10 17:55:03 -0500323 */
Derek Miller16e72292018-10-15 16:14:24 -0500324typedef psa_status_t (*pcd_mac_transparent_verify_finish_t)(struct pcd_mac_transparent_context_t *p_context,
325 const uint8_t *p_mac,
326 size_t mac_length);
327
328/** \brief The function prototype for the abort operation for a previously
329 * started transparent-key MAC operation
330 *
331 * Functions that implement the prototype should be named in the following
332 * convention:
333 * ~~~~~~~~~~~~~{.c}
334 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_abort
335 * ~~~~~~~~~~~~~
336 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
337 * the specific variant of a MAC operation (such as HMAC or CMAC)
338 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500339 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500340 * previously started MAC operation to be
Derek Millerf3d0a562018-10-18 16:41:08 -0500341 * aborted
Derek Miller16e72292018-10-15 16:14:24 -0500342 *
343 */
344typedef psa_status_t (*pcd_mac_transparent_abort_t)(struct pcd_mac_transparent_context_t *p_context);
345
346/** \brief The function prototype for a one-shot operation of a transparent-key
347 * MAC operation
348 *
349 * Functions that implement the prototype should be named in the following
350 * convention:
351 * ~~~~~~~~~~~~~{.c}
352 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>
353 * ~~~~~~~~~~~~~
354 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
355 * the specific variant of a MAC operation (such as HMAC or CMAC)
356 *
357 * \param[in] p_input A buffer containing the data to be MACed
358 * \param[in] input_length The length in bytes of the `p_input` data
359 * \param[in] p_key A buffer containing the key material to be used
360 * for the MAC operation
361 * \param[in] key_length The length in bytes of the `p_key` data
362 * \param[in] alg The algorithm to be performed
363 * \param[out] p_mac The buffer where the resulting MAC will be placed
364 * upon success
365 * \param[in] mac_length The length in bytes of the `p_mac` buffer
366 */
367typedef psa_status_t (*pcd_mac_transparent_t)(const uint8_t *p_input,
368 size_t input_length,
369 const uint8_t *p_key,
370 size_t key_length,
371 psa_algorithm_t alg,
372 uint8_t *p_mac,
373 size_t mac_length);
374
375/** \brief The function prototype for a one-shot operation of a transparent-key
376 * MAC Verify operation
377 *
378 * Functions that implement the prototype should be named in the following
379 * convention:
380 * ~~~~~~~~~~~~~{.c}
381 * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_verify
382 * ~~~~~~~~~~~~~
383 * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is
384 * the specific variant of a MAC operation (such as HMAC or CMAC)
385 *
386 * \param[in] p_input A buffer containing the data to be MACed
387 * \param[in] input_length The length in bytes of the `p_input` data
388 * \param[in] p_key A buffer containing the key material to be used
389 * for the MAC operation
390 * \param[in] key_length The length in bytes of the `p_key` data
391 * \param[in] alg The algorithm to be performed
392 * \param[in] p_mac The MAC data to be compared
393 * \param[in] mac_length The length in bytes of the `p_mac` buffer
394 *
395 * \retval PSA_SUCCESS
396 * The operation completed successfully and the comparison matched
397 */
398typedef psa_status_t (*pcd_mac_transparent_verify_t)(const uint8_t *p_input,
399 size_t input_length,
400 const uint8_t *p_key,
401 size_t key_length,
402 psa_algorithm_t alg,
403 const uint8_t *p_mac,
404 size_t mac_length);
405/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500406
407/** \defgroup opaque_cipher Opaque Symmetric Ciphers
Derek Miller5b3417a2018-10-10 17:55:03 -0500408 */
Derek Miller16e72292018-10-15 16:14:24 -0500409/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500410
Derek Miller16e72292018-10-15 16:14:24 -0500411/** \brief A function pointer that provides the cipher setup function for
412 * opaque-key operations
Derek Miller5b3417a2018-10-10 17:55:03 -0500413 *
Derek Miller16e72292018-10-15 16:14:24 -0500414 * \param[in,out] p_context A structure that will contain the
415 * hardware-specific cipher context.
416 * \param[in] key_slot The slot of the key to be used for the
417 * operation
418 * \param[in] algorithm The algorithm to be used in the cipher
419 * operation
420 * \param[in] direction Indicates whether the operation is an encrypt
421 * or decrypt
Derek Miller5b3417a2018-10-10 17:55:03 -0500422 *
423 * \retval PSA_SUCCESS
424 * \retval PSA_ERROR_NOT_SUPPORTED
425 */
Derek Miller16e72292018-10-15 16:14:24 -0500426typedef psa_status_t (*pcd_cipher_opaque_setup_t)(void *p_context,
427 psa_key_slot_t key_slot,
Derek Miller5b3417a2018-10-10 17:55:03 -0500428 psa_algorithm_t algorithm,
Derek Miller16e72292018-10-15 16:14:24 -0500429 encrypt_or_decrypt_t direction);
430
431/** \brief A function pointer that sets the initialization vector (if
432 * necessary) for an opaque cipher operation
433 *
434 * Rationale: that the psa_cipher_* function set has two IV functions: one to
435 * set the IV, and one to generate it internally. the generate function is not
436 * necessary for the driver API as the PSA Crypto implementation can do the
437 * generation using its RNG features.
438 *
439 * \param[in,out] p_context A structure that contains the previously set up
440 * hardware-specific cipher context
441 * \param[in] p_iv A buffer containing the initialization vector
442 * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
443 *
444 * \retval PSA_SUCCESS
445 */
446typedef psa_status_t (*pcd_cipher_opaque_set_iv_t)(void *p_context,
447 const uint8_t *p_iv,
448 size_t iv_length);
449
450/** \brief A function that continues a previously started opaque-key cipher
451 * operation
452 *
453 * \param[in,out] p_context A hardware-specific structure for the
454 * previously started cipher operation
455 * \param[in] p_input A buffer containing the data to be
456 * encrypted/decrypted
457 * \param[in] input_size The size in bytes of the buffer pointed to
458 * by `p_input`
459 * \param[out] p_output The caller-allocated buffer where the
460 * output will be placed
461 * \param[in] output_size The allocated size in bytes of the
462 * `p_output` buffer
463 * \param[out] p_output_length After completion, will contain the number
464 * of bytes placed in the `p_output` buffer
465 *
466 * \retval PSA_SUCCESS
467 */
468typedef psa_status_t (*pcd_cipher_opaque_update_t)(void *p_context,
469 const uint8_t *p_input,
470 size_t input_size,
471 uint8_t *p_output,
472 size_t output_size,
473 size_t *p_output_length);
474
475/** \brief A function that completes a previously started opaque-key cipher
476 * operation
477 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500478 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500479 * previously started cipher operation
Derek Millerf3d0a562018-10-18 16:41:08 -0500480 * \param[out] p_output The caller-allocated buffer where the output
Derek Miller16e72292018-10-15 16:14:24 -0500481 * will be placed
482 * \param[in] output_size The allocated size in bytes of the `p_output`
483 * buffer
484 * \param[out] p_output_length After completion, will contain the number of
485 * bytes placed in the `p_output` buffer
486 *
487 * \retval PSA_SUCCESS
488 */
489typedef psa_status_t (*pcd_cipher_opaque_finish_t)(void *p_context,
490 uint8_t *p_output,
491 size_t output_size,
492 size_t *p_output_length);
493
494/** \brief A function that aborts a previously started opaque-key cipher
495 * operation
496 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500497 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500498 * previously started cipher operation
499 */
500typedef psa_status_t (*pcd_cipher_opaque_abort_t)(void *p_context);
501
502/** \brief A function that performs the ECB block mode for opaque-key cipher
503 * operations
504 *
505 * Note: this function should only be used with implementations that do not
506 * provide a needed higher-level operation.
507 *
508 * \param[in] key_slot The slot of the key to be used for the operation
509 * \param[in] algorithm The algorithm to be used in the cipher operation
510 * \param[in] direction Indicates whether the operation is an encrypt or
511 * decrypt
512 * \param[in] p_input A buffer containing the data to be
513 * encrypted/decrypted
514 * \param[in] input_size The size in bytes of the buffer pointed to by
515 * `p_input`
Derek Millerf3d0a562018-10-18 16:41:08 -0500516 * \param[out] p_output The caller-allocated buffer where the output will
Derek Miller16e72292018-10-15 16:14:24 -0500517 * be placed
518 * \param[in] output_size The allocated size in bytes of the `p_output`
519 * buffer
520 *
521 * \retval PSA_SUCCESS
522 * \retval PSA_ERROR_NOT_SUPPORTED
523 */
524typedef psa_status_t (*pcd_cipher_opaque_ecb_t)(psa_key_slot_t key_slot,
525 psa_algorithm_t algorithm,
526 encrypt_or_decrypt_t direction,
527 const uint8_t *p_input,
528 size_t input_size,
529 uint8_t *p_output,
530 size_t output_size);
Derek Miller5b3417a2018-10-10 17:55:03 -0500531
532/**
Derek Miller16e72292018-10-15 16:14:24 -0500533 * \brief A struct containing all of the function pointers needed to implement
534 * cipher operations using opaque keys.
Derek Miller5b3417a2018-10-10 17:55:03 -0500535 *
Derek Miller16e72292018-10-15 16:14:24 -0500536 * PSA Crypto API implementations should populate instances of the table as
537 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -0500538 *
Derek Miller16e72292018-10-15 16:14:24 -0500539 * If one of the functions is not implemented (such as
540 * `pcd_cipher_opaque_ecb_t`), it should be set to NULL.
Derek Miller5b3417a2018-10-10 17:55:03 -0500541 */
542struct pcd_cipher_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -0500543 /** The size in bytes of the hardware-specific Opaque Cipher context
544 * structure
545 */
546 size_t size;
547 /** Function that performs the setup operation */
548 pcd_cipher_opaque_setup_t *p_setup;
549 /** Function that sets the IV (if necessary) */
550 pcd_cipher_opaque_set_iv_t *p_set_iv;
551 /** Function that performs the update operation */
552 pcd_cipher_opaque_update_t *p_update;
553 /** Function that completes the operation */
554 pcd_cipher_opaque_finish_t *p_finish;
555 /** Function that aborts the operation */
556 pcd_cipher_opaque_abort_t *p_abort;
557 /** Function that performs ECB mode for the cipher
558 * (Danger: ECB mode should not be used directly by clients of the PSA
559 * Crypto Client API)
560 */
561 pcd_cipher_opaque_ecb_t *p_ecb;
Derek Miller5b3417a2018-10-10 17:55:03 -0500562};
563
Derek Miller16e72292018-10-15 16:14:24 -0500564/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500565
566/** \defgroup transparent_cipher Transparent Block Cipher
Derek Miller5b3417a2018-10-10 17:55:03 -0500567 */
Derek Miller16e72292018-10-15 16:14:24 -0500568/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500569
570/** \brief The hardware-specific transparent-key Cipher context structure
Derek Miller16e72292018-10-15 16:14:24 -0500571 * The contents of this structure are implementation dependent and are
572 * therefore not described here.
Derek Miller5b3417a2018-10-10 17:55:03 -0500573 */
574struct pcd_cipher_transparent_context_t {
575 // Implementation specific
576};
577
Derek Miller16e72292018-10-15 16:14:24 -0500578/** \brief The function prototype for the setup operation of transparent-key
579 * block cipher operations.
580 * Functions that implement the prototype should be named in the following
581 * conventions:
Derek Miller5b3417a2018-10-10 17:55:03 -0500582 * ~~~~~~~~~~~~~{.c}
583 * pcd_cipher_transparent_setup_<CIPHER_NAME>_<MODE>
584 * ~~~~~~~~~~~~~
585 * Where
586 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
587 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Derek Miller16e72292018-10-15 16:14:24 -0500588 * or for stream ciphers:
589 * ~~~~~~~~~~~~~{.c}
590 * pcd_cipher_transparent_setup_<CIPHER_NAME>
591 * ~~~~~~~~~~~~~
592 * Where `CIPHER_NAME` is the name of a stream cipher (i.e. RC4)
Derek Miller5b3417a2018-10-10 17:55:03 -0500593 *
Derek Miller16e72292018-10-15 16:14:24 -0500594 * \param[in,out] p_context A structure that will contain the
595 * hardware-specific cipher context
596 * \param[in] direction Indicates if the operation is an encrypt or a
597 * decrypt
598 * \param[in] p_key_data A buffer containing the cleartext key material
599 * to be used in the operation
600 * \param[in] key_data_size The size in bytes of the key material
Derek Miller5b3417a2018-10-10 17:55:03 -0500601 *
602 * \retval PSA_SUCCESS
603 */
Derek Miller16e72292018-10-15 16:14:24 -0500604typedef psa_status_t (*pcd_cipher_transparent_setup_t)(struct pcd_cipher_transparent_context_t *p_context,
605 encrypt_or_decrypt_t direction,
606 const uint8_t *p_key_data,
607 size_t key_data_size);
Derek Miller5b3417a2018-10-10 17:55:03 -0500608
Derek Miller16e72292018-10-15 16:14:24 -0500609/** \brief The function prototype for the set initialization vector operation
610 * of transparent-key block cipher operations
611 * Functions that implement the prototype should be named in the following
612 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500613 * ~~~~~~~~~~~~~{.c}
614 * pcd_cipher_transparent_set_iv_<CIPHER_NAME>_<MODE>
615 * ~~~~~~~~~~~~~
616 * Where
617 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
618 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
619 *
Derek Miller16e72292018-10-15 16:14:24 -0500620 * \param[in,out] p_context A structure that contains the previously setup
621 * hardware-specific cipher context
622 * \param[in] p_iv A buffer containing the initialization vecotr
623 * \param[in] iv_length The size in bytes of the contents of `p_iv`
Derek Miller5b3417a2018-10-10 17:55:03 -0500624 *
625 * \retval PSA_SUCCESS
626*/
Derek Miller16e72292018-10-15 16:14:24 -0500627typedef psa_status_t (*pcd_cipher_transparent_set_iv_t)(struct pcd_cipher_transparent_context_t *p_context,
628 const uint8_t *p_iv,
629 size_t iv_length);
630
631/** \brief The function prototype for the update operation of transparent-key
632 * block cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500633 *
Derek Miller16e72292018-10-15 16:14:24 -0500634 * Functions that implement the prototype should be named in the following
635 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500636 * ~~~~~~~~~~~~~{.c}
637 * pcd_cipher_transparent_update_<CIPHER_NAME>_<MODE>
638 * ~~~~~~~~~~~~~
639 * Where
640 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
641 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
642 *
Derek Miller16e72292018-10-15 16:14:24 -0500643 * \param[in,out] p_context A hardware-specific structure for the
644 * previously started cipher operation
645 * \param[in] p_input A buffer containing the data to be
646 * encrypted or decrypted
647 * \param[in] input_size The size in bytes of the `p_input` buffer
648 * \param[out] p_output A caller-allocated buffer where the
649 * generated output will be placed
650 * \param[in] output_size The size in bytes of the `p_output` buffer
651 * \param[out] p_output_length After completion, will contain the number
652 * of bytes placed in the `p_output` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500653 *
654 * \retval PSA_SUCCESS
655 */
Derek Miller16e72292018-10-15 16:14:24 -0500656typedef psa_status_t (*pcd_cipher_transparent_update_t)(struct pcd_cipher_transparent_context_t *p_context,
657 const uint8_t *p_input,
658 size_t input_size,
659 uint8_t *p_output,
660 size_t output_size,
661 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500662
Derek Miller16e72292018-10-15 16:14:24 -0500663/** \brief The function prototype for the finish operation of transparent-key
664 * block cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500665*
Derek Miller16e72292018-10-15 16:14:24 -0500666 * Functions that implement the prototype should be named in the following
667 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500668 * ~~~~~~~~~~~~~{.c}
669 * pcd_cipher_transparent_finish_<CIPHER_NAME>_<MODE>
670 * ~~~~~~~~~~~~~
671 * Where
672 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
673 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
Derek Miller16e72292018-10-15 16:14:24 -0500674 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500675 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500676 * previously started cipher operation
677 * \param[out] p_output A caller-allocated buffer where the generated
678 * output will be placed
679 * \param[in] output_size The size in bytes of the `p_output` buffer
680 * \param[out] p_output_length After completion, will contain the number of
681 * bytes placed in the `p_output` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500682 *
683 * \retval PSA_SUCCESS
684 */
Derek Miller16e72292018-10-15 16:14:24 -0500685typedef psa_status_t (*pcd_cipher_transparent_finish_t)(struct pcd_cipher_transparent_context_t *p_context,
686 uint8_t *p_output,
687 size_t output_size,
688 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500689
Derek Miller16e72292018-10-15 16:14:24 -0500690/** \brief The function prototype for the abort operation of transparent-key
691 * block cipher operations.
Derek Miller5b3417a2018-10-10 17:55:03 -0500692 *
Derek Miller16e72292018-10-15 16:14:24 -0500693 * Functions that implement the following prototype should be named in the
694 * following convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500695 * ~~~~~~~~~~~~~{.c}
696 * pcd_cipher_transparent_abort_<CIPHER_NAME>_<MODE>
697 * ~~~~~~~~~~~~~
698 * Where
699 * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES)
700 * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR)
701 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500702 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500703 * previously started cipher operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500704 *
705 * \retval PSA_SUCCESS
706 */
Derek Miller16e72292018-10-15 16:14:24 -0500707typedef psa_status_t (*pcd_cipher_transparent_abort_t)(struct pcd_cipher_transparent_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -0500708
Derek Miller16e72292018-10-15 16:14:24 -0500709/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500710
Derek Miller16e72292018-10-15 16:14:24 -0500711/** \defgroup driver_digest Message Digests
Derek Miller5b3417a2018-10-10 17:55:03 -0500712 */
Derek Miller16e72292018-10-15 16:14:24 -0500713/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500714
715/** \brief The hardware-specific hash context structure
Derek Miller16e72292018-10-15 16:14:24 -0500716 * The contents of this structure are implementation dependent and are
717 * therefore not described here
Derek Miller5b3417a2018-10-10 17:55:03 -0500718 */
719struct pcd_hash_context_t {
720 // Implementation specific
721};
722
Derek Miller16e72292018-10-15 16:14:24 -0500723/** \brief The function prototype for the start operation of a hash (message
724 * digest) operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500725 *
Derek Miller16e72292018-10-15 16:14:24 -0500726 * Functions that implement the prototype should be named in the following
727 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500728 * ~~~~~~~~~~~~~{.c}
Derek Miller16e72292018-10-15 16:14:24 -0500729 * pcd_hash_<ALGO>_setup
Derek Miller5b3417a2018-10-10 17:55:03 -0500730 * ~~~~~~~~~~~~~
731 * Where `ALGO` is the name of the underlying hash function
732 *
Derek Miller16e72292018-10-15 16:14:24 -0500733 * \param[in,out] p_context A structure that will contain the
734 * hardware-specific hash context
Derek Miller5b3417a2018-10-10 17:55:03 -0500735 *
736 * \retval PSA_SUCCESS Success.
737 */
Derek Miller16e72292018-10-15 16:14:24 -0500738typedef psa_status_t (*pcd_hash_setup_t)(struct pcd_hash_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -0500739
Derek Miller16e72292018-10-15 16:14:24 -0500740/** \brief The function prototype for the update operation of a hash (message
741 * digest) operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500742 *
Derek Miller16e72292018-10-15 16:14:24 -0500743 * Functions that implement the prototype should be named in the following
744 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500745 * ~~~~~~~~~~~~~{.c}
746 * pcd_hash_<ALGO>_update
747 * ~~~~~~~~~~~~~
748 * Where `ALGO` is the name of the underlying algorithm
749 *
Derek Miller16e72292018-10-15 16:14:24 -0500750 * \param[in,out] p_context A hardware-specific structure for the
751 * previously-established hash operation to be
752 * continued
753 * \param[in] p_input A buffer containing the message to be appended
754 * to the hash operation
755 * \param[in] input_length The size in bytes of the input message buffer
Derek Miller5b3417a2018-10-10 17:55:03 -0500756 */
Derek Miller16e72292018-10-15 16:14:24 -0500757typedef psa_status_t (*pcd_hash_update_t)(struct pcd_hash_context_t *p_context,
758 const uint8_t *p_input,
759 size_t input_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500760
Derek Miller16e72292018-10-15 16:14:24 -0500761/** \brief The prototype for the finish operation of a hash (message digest)
762 * operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500763 *
Derek Miller16e72292018-10-15 16:14:24 -0500764 * Functions that implement the prototype should be named in the following
765 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500766 * ~~~~~~~~~~~~~{.c}
767 * pcd_hash_<ALGO>_finish
768 * ~~~~~~~~~~~~~
769 * Where `ALGO` is the name of the underlying algorithm
770 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500771 * \param[in,out] p_context A hardware-specific structure for the
Derek Miller16e72292018-10-15 16:14:24 -0500772 * previously started hash operation to be
773 * fiinished
774 * \param[out] p_output A buffer where the generated digest will be
775 * placed
776 * \param[in] output_size The size in bytes of the buffer that has been
777 * allocated for the `p_output` buffer
Derek Millerf3d0a562018-10-18 16:41:08 -0500778 * \param[out] p_output_length The number of bytes placed in `p_output` after
779 * success
Derek Miller5b3417a2018-10-10 17:55:03 -0500780 *
781 * \retval PSA_SUCCESS
782 * Success.
783 */
Derek Miller16e72292018-10-15 16:14:24 -0500784typedef psa_status_t (*pcd_hash_finish_t)(struct pcd_hash_context_t *p_context,
785 uint8_t *p_output,
Derek Millerf3d0a562018-10-18 16:41:08 -0500786 size_t output_size,
787 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500788
Derek Miller16e72292018-10-15 16:14:24 -0500789/** \brief The function prototype for the abort operation of a hash (message
790 * digest) operation
Derek Miller5b3417a2018-10-10 17:55:03 -0500791 *
Derek Miller16e72292018-10-15 16:14:24 -0500792 * Functions that implement the prototype should be named in the following
793 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500794 * ~~~~~~~~~~~~~{.c}
795 * pcd_hash_<ALGO>_abort
796 * ~~~~~~~~~~~~~
797 * Where `ALGO` is the name of the underlying algorithm
798 *
Derek Millerf3d0a562018-10-18 16:41:08 -0500799 * \param[in,out] p_context A hardware-specific structure for the previously
Derek Miller16e72292018-10-15 16:14:24 -0500800 * started hash operation to be aborted
Derek Miller5b3417a2018-10-10 17:55:03 -0500801 */
802typedef void (*pcd_hash_abort_t)(struct pcd_hash_context_t *p_context);
803
Derek Miller16e72292018-10-15 16:14:24 -0500804/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500805
806
807/** \defgroup opaque_asymmetric Opaque Asymmetric Cryptography
Derek Miller5b3417a2018-10-10 17:55:03 -0500808 */
Derek Miller16e72292018-10-15 16:14:24 -0500809/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500810
811/**
Derek Miller16e72292018-10-15 16:14:24 -0500812 * \brief A function that signs a hash or short message with a private key
Derek Miller5b3417a2018-10-10 17:55:03 -0500813 *
Derek Miller16e72292018-10-15 16:14:24 -0500814 * \param[in] key_slot Key slot of an asymmetric key pair
815 * \param[in] alg A signature algorithm that is compatible
816 * with the type of `key`
817 * \param[in] p_hash The hash or message to sign
818 * \param[in] hash_length Size of the `p_hash` buffer in bytes
819 * \param[out] p_signature Buffer where the signature is to be written
Derek Millerf3d0a562018-10-18 16:41:08 -0500820 * \param[in] signature_size Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500821 * \param[out] p_signature_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -0500822 * that make up the returned signature value
Derek Miller5b3417a2018-10-10 17:55:03 -0500823 *
824 * \retval PSA_SUCCESS
825 */
Derek Miller16e72292018-10-15 16:14:24 -0500826typedef psa_status_t (*pcd_asymmetric_opaque_sign_t)(psa_key_slot_t key_slot,
827 psa_algorithm_t alg,
828 const uint8_t *p_hash,
829 size_t hash_length,
830 uint8_t *p_signature,
831 size_t signature_size,
832 size_t *p_signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500833
834/**
Derek Miller16e72292018-10-15 16:14:24 -0500835 * \brief A function that verifies the signature a hash or short message using
836 * a public key
Derek Miller5b3417a2018-10-10 17:55:03 -0500837 *
Derek Miller16e72292018-10-15 16:14:24 -0500838 * \param[in] key_slot Key slot of a public key or an asymmetric key
839 * pair
840 * \param[in] alg A signature algorithm that is compatible with
841 * the type of `key`
842 * \param[in] p_hash The hash or message whose signature is to be
843 * verified
844 * \param[in] hash_length Size of the `p_hash` buffer in bytes
845 * \param[in] p_signature Buffer containing the signature to verify
846 * \param[in] signature_length Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500847 *
848 * \retval PSA_SUCCESS
849 * The signature is valid.
850 */
Derek Miller16e72292018-10-15 16:14:24 -0500851typedef psa_status_t (*pcd_asymmetric_opaque_verify_t)(psa_key_slot_t key_slot,
852 psa_algorithm_t alg,
853 const uint8_t *p_hash,
854 size_t hash_length,
855 const uint8_t *p_signature,
856 size_t signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500857
858/**
Derek Miller16e72292018-10-15 16:14:24 -0500859 * \brief A function that encrypts a short message with a public key
Derek Miller5b3417a2018-10-10 17:55:03 -0500860 *
Derek Miller16e72292018-10-15 16:14:24 -0500861 * \param[in] key_slot Key slot of a public key or an asymmetric key
862 * pair
863 * \param[in] alg An asymmetric encryption algorithm that is
864 * compatible with the type of `key`
865 * \param[in] p_input The message to encrypt
866 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500867 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -0500868 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -0500869 * If the algorithm does not support a
870 * salt, pass `NULL`.
871 * If the algorithm supports an optional
872 * salt and you do not want to pass a salt,
873 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -0500874 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
875 * supported.
876 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500877 * If `p_salt` is `NULL`, pass 0.
878 * \param[out] p_output Buffer where the encrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -0500879 * be written
880 * \param[in] output_size Size of the `p_output` buffer in bytes
881 * \param[out] p_output_length On success, the number of bytes that make up
882 * the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -0500883 *
884 * \retval PSA_SUCCESS
885 */
Derek Miller16e72292018-10-15 16:14:24 -0500886typedef psa_status_t (*pcd_asymmetric_opaque_encrypt_t)(psa_key_slot_t key_slot,
887 psa_algorithm_t alg,
888 const uint8_t *p_input,
889 size_t input_length,
890 const uint8_t *p_salt,
891 size_t salt_length,
892 uint8_t *p_output,
893 size_t output_size,
894 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500895
896/**
897 * \brief Decrypt a short message with a private key.
898 *
Derek Miller16e72292018-10-15 16:14:24 -0500899 * \param[in] key_slot Key slot of an asymmetric key pair
900 * \param[in] alg An asymmetric encryption algorithm that is
901 * compatible with the type of `key`
902 * \param[in] p_input The message to decrypt
903 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500904 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -0500905 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -0500906 * If the algorithm does not support a
907 * salt, pass `NULL`.
908 * If the algorithm supports an optional
909 * salt and you do not want to pass a salt,
910 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -0500911 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
912 * supported.
913 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500914 * If `p_salt` is `NULL`, pass 0.
915 * \param[out] p_output Buffer where the decrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -0500916 * be written
917 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500918 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -0500919 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -0500920 *
921 * \retval PSA_SUCCESS
922 */
Derek Miller16e72292018-10-15 16:14:24 -0500923typedef psa_status_t (*pcd_asymmetric_opaque_decrypt_t)(psa_key_slot_t key_slot,
924 psa_algorithm_t alg,
925 const uint8_t *p_input,
926 size_t input_length,
927 const uint8_t *p_salt,
928 size_t salt_length,
929 uint8_t *p_output,
930 size_t output_size,
931 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500932
933/**
Derek Miller16e72292018-10-15 16:14:24 -0500934 * \brief A struct containing all of the function pointers needed to implement
935 * asymmetric cryptographic operations using opaque keys.
Derek Miller5b3417a2018-10-10 17:55:03 -0500936 *
Derek Miller16e72292018-10-15 16:14:24 -0500937 * PSA Crypto API implementations should populate instances of the table as
938 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -0500939 *
940 * If one of the functions is not implemented, it should be set to NULL.
941 */
942struct pcd_asymmetric_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -0500943 /** Function that performs the asymmetric sign operation */
944 pcd_asymmetric_opaque_sign_t *p_sign;
945 /** Function that performs the asymmetric verify operation */
946 pcd_asymmetric_opaque_verify_t *p_verify;
947 /** Function that performs the asymmetric encrypt operation */
948 pcd_asymmetric_opaque_encrypt_t *p_encrypt;
949 /** Function that performs the asymmetric decrypt operation */
950 pcd_asymmetric_opaque_decrypt_t *p_decrypt;
Derek Miller5b3417a2018-10-10 17:55:03 -0500951};
952
Derek Miller16e72292018-10-15 16:14:24 -0500953/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500954
955/** \defgroup transparent_asymmetric Transparent Asymmetric Cryptography
Derek Miller5b3417a2018-10-10 17:55:03 -0500956 */
Derek Miller16e72292018-10-15 16:14:24 -0500957/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -0500958
959
960/**
Derek Miller16e72292018-10-15 16:14:24 -0500961 * \brief A function that signs a hash or short message with a transparent
962 * private key
Derek Miller5b3417a2018-10-10 17:55:03 -0500963 *
Derek Miller16e72292018-10-15 16:14:24 -0500964 * Functions that implement the prototype should be named in the following
965 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -0500966 * ~~~~~~~~~~~~~{.c}
967 * pcd_asymmetric_<ALGO>_sign
968 * ~~~~~~~~~~~~~
969 * Where `ALGO` is the name of the signing algorithm
970 *
Derek Miller16e72292018-10-15 16:14:24 -0500971 * \param[in] p_key A buffer containing the private key
972 * material
973 * \param[in] key_size The size in bytes of the `p_key` data
974 * \param[in] alg A signature algorithm that is compatible
975 * with the type of `p_key`
976 * \param[in] p_hash The hash or message to sign
977 * \param[in] hash_length Size of the `p_hash` buffer in bytes
978 * \param[out] p_signature Buffer where the signature is to be written
979 * \param[in] signature_size Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -0500980 * \param[out] p_signature_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -0500981 * that make up the returned signature value
Derek Miller5b3417a2018-10-10 17:55:03 -0500982 *
983 * \retval PSA_SUCCESS
984 */
Derek Miller16e72292018-10-15 16:14:24 -0500985typedef psa_status_t (*pcd_asymmetric_transparent_sign_t)(const uint8_t *p_key,
986 size_t key_size,
987 psa_algorithm_t alg,
988 const uint8_t *p_hash,
989 size_t hash_length,
990 uint8_t *p_signature,
991 size_t signature_size,
992 size_t *p_signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -0500993
994/**
Derek Miller16e72292018-10-15 16:14:24 -0500995 * \brief A function that verifies the signature a hash or short message using
996 * a transparent public key
Derek Miller5b3417a2018-10-10 17:55:03 -0500997 *
Derek Miller16e72292018-10-15 16:14:24 -0500998 * Functions that implement the prototype should be named in the following
999 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001000 * ~~~~~~~~~~~~~{.c}
1001 * pcd_asymmetric_<ALGO>_verify
1002 * ~~~~~~~~~~~~~
1003 * Where `ALGO` is the name of the signing algorithm
1004 *
Derek Miller16e72292018-10-15 16:14:24 -05001005 * \param[in] p_key A buffer containing the public key material
1006 * \param[in] key_size The size in bytes of the `p_key` data
1007 * \param[in] alg A signature algorithm that is compatible with
1008 * the type of `key`
1009 * \param[in] p_hash The hash or message whose signature is to be
1010 * verified
1011 * \param[in] hash_length Size of the `p_hash` buffer in bytes
1012 * \param[in] p_signature Buffer containing the signature to verify
1013 * \param[in] signature_length Size of the `p_signature` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001014 *
1015 * \retval PSA_SUCCESS
1016 * The signature is valid.
1017 */
Derek Miller16e72292018-10-15 16:14:24 -05001018typedef psa_status_t (*pcd_asymmetric_transparent_verify_t)(const uint8_t *p_key,
1019 size_t key_size,
1020 psa_algorithm_t alg,
1021 const uint8_t *p_hash,
1022 size_t hash_length,
1023 const uint8_t *p_signature,
1024 size_t signature_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001025
1026/**
Derek Miller16e72292018-10-15 16:14:24 -05001027 * \brief A function that encrypts a short message with a transparent public
1028 * key
Derek Miller5b3417a2018-10-10 17:55:03 -05001029 *
Derek Miller16e72292018-10-15 16:14:24 -05001030 * Functions that implement the prototype should be named in the following
1031 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001032 * ~~~~~~~~~~~~~{.c}
1033 * pcd_asymmetric_<ALGO>_encrypt
1034 * ~~~~~~~~~~~~~
1035 * Where `ALGO` is the name of the encryption algorithm
1036 *
Derek Miller16e72292018-10-15 16:14:24 -05001037 * \param[in] p_key A buffer containing the public key material
1038 * \param[in] key_size The size in bytes of the `p_key` data
1039 * \param[in] alg An asymmetric encryption algorithm that is
1040 * compatible with the type of `key`
1041 * \param[in] p_input The message to encrypt
1042 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001043 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -05001044 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -05001045 * If the algorithm does not support a
Derek Miller16e72292018-10-15 16:14:24 -05001046 * salt, pass `NULL`
Derek Miller5b3417a2018-10-10 17:55:03 -05001047 * If the algorithm supports an optional
1048 * salt and you do not want to pass a salt,
1049 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -05001050 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1051 * supported.
1052 * \param[in] salt_length Size of the `p_salt` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001053 * If `p_salt` is `NULL`, pass 0.
1054 * \param[out] p_output Buffer where the encrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -05001055 * be written
1056 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001057 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001058 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -05001059 *
1060 * \retval PSA_SUCCESS
1061 */
Derek Miller16e72292018-10-15 16:14:24 -05001062typedef psa_status_t (*pcd_asymmetric_transparent_encrypt_t)(const uint8_t *p_key,
Derek Miller5b3417a2018-10-10 17:55:03 -05001063 size_t key_size,
1064 psa_algorithm_t alg,
1065 const uint8_t *p_input,
1066 size_t input_length,
1067 const uint8_t *p_salt,
1068 size_t salt_length,
1069 uint8_t *p_output,
1070 size_t output_size,
Derek Miller16e72292018-10-15 16:14:24 -05001071 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001072
1073/**
Derek Miller16e72292018-10-15 16:14:24 -05001074 * \brief Decrypt a short message with a transparent private key
Derek Miller5b3417a2018-10-10 17:55:03 -05001075 *
Derek Miller16e72292018-10-15 16:14:24 -05001076 * Functions that implement the prototype should be named in the following
1077 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001078 * ~~~~~~~~~~~~~{.c}
1079 * pcd_asymmetric_<ALGO>_decrypt
1080 * ~~~~~~~~~~~~~
1081 * Where `ALGO` is the name of the encryption algorithm
1082 *
Derek Miller16e72292018-10-15 16:14:24 -05001083 * \param[in] p_key A buffer containing the private key material
1084 * \param[in] key_size The size in bytes of the `p_key` data
1085 * \param[in] alg An asymmetric encryption algorithm that is
1086 * compatible with the type of `key`
1087 * \param[in] p_input The message to decrypt
1088 * \param[in] input_length Size of the `p_input` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001089 * \param[in] p_salt A salt or label, if supported by the
Derek Miller16e72292018-10-15 16:14:24 -05001090 * encryption algorithm
Derek Miller5b3417a2018-10-10 17:55:03 -05001091 * If the algorithm does not support a
1092 * salt, pass `NULL`.
1093 * If the algorithm supports an optional
1094 * salt and you do not want to pass a salt,
1095 * pass `NULL`.
Derek Miller16e72292018-10-15 16:14:24 -05001096 * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1097 * supported
1098 * \param[in] salt_length Size of the `p_salt` buffer in bytes
1099 * If `p_salt` is `NULL`, pass 0
Derek Miller5b3417a2018-10-10 17:55:03 -05001100 * \param[out] p_output Buffer where the decrypted message is to
Derek Miller16e72292018-10-15 16:14:24 -05001101 * be written
1102 * \param[in] output_size Size of the `p_output` buffer in bytes
Derek Miller5b3417a2018-10-10 17:55:03 -05001103 * \param[out] p_output_length On success, the number of bytes
Derek Miller16e72292018-10-15 16:14:24 -05001104 * that make up the returned output
Derek Miller5b3417a2018-10-10 17:55:03 -05001105 *
1106 * \retval PSA_SUCCESS
1107 */
Derek Miller16e72292018-10-15 16:14:24 -05001108typedef psa_status_t (*pcd_asymmetric_transparent_decrypt_t)(const uint8_t *p_key,
1109 size_t key_size,
1110 psa_algorithm_t alg,
1111 const uint8_t *p_input,
1112 size_t input_length,
1113 const uint8_t *p_salt,
1114 size_t salt_length,
1115 uint8_t *p_output,
1116 size_t output_size,
1117 size_t *p_output_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001118
Derek Miller16e72292018-10-15 16:14:24 -05001119/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001120
1121/** \defgroup aead_opaque AEAD Opaque
Derek Miller5b3417a2018-10-10 17:55:03 -05001122 */
Derek Miller16e72292018-10-15 16:14:24 -05001123/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001124
Derek Miller16e72292018-10-15 16:14:24 -05001125/** \brief Process an authenticated encryption operation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -05001126 *
Derek Miller16e72292018-10-15 16:14:24 -05001127 * \param[in] key_slot Slot containing the key to use.
1128 * \param[in] algorithm The AEAD algorithm to compute
1129 * (\c PSA_ALG_XXX value such that
1130 * #PSA_ALG_IS_AEAD(`alg`) is true)
1131 * \param[in] p_nonce Nonce or IV to use
1132 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
1133 * \param[in] p_additional_data Additional data that will be
1134 * authenticated but not encrypted
1135 * \param[in] additional_data_length Size of `p_additional_data` in bytes
1136 * \param[in] p_plaintext Data that will be authenticated and
1137 * encrypted
1138 * \param[in] plaintext_length Size of `p_plaintext` in bytes
1139 * \param[out] p_ciphertext Output buffer for the authenticated and
1140 * encrypted data. The additional data is
1141 * not part of this output. For algorithms
1142 * where the encrypted data and the
1143 * authentication tag are defined as
1144 * separate outputs, the authentication
1145 * tag is appended to the encrypted data.
1146 * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
1147 * bytes
1148 * \param[out] p_ciphertext_length On success, the size of the output in
1149 * the `p_ciphertext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001150 *
1151 * \retval #PSA_SUCCESS
1152 * Success.
1153 */
Derek Miller16e72292018-10-15 16:14:24 -05001154typedef psa_status_t (*psa_aead_opaque_encrypt_t)(psa_key_slot_t key_slot,
Derek Miller5b3417a2018-10-10 17:55:03 -05001155 psa_algorithm_t algorithm,
1156 const uint8_t *p_nonce,
1157 size_t nonce_length,
1158 const uint8_t *p_additional_data,
1159 size_t additional_data_length,
1160 const uint8_t *p_plaintext,
1161 size_t plaintext_length,
1162 uint8_t *p_ciphertext,
1163 size_t ciphertext_size,
1164 size_t *p_ciphertext_length);
1165
Derek Miller16e72292018-10-15 16:14:24 -05001166/** Process an authenticated decryption operation using an opaque key
Derek Miller5b3417a2018-10-10 17:55:03 -05001167 *
Derek Miller16e72292018-10-15 16:14:24 -05001168 * \param[in] key_slot Slot containing the key to use
1169 * \param[in] algorithm The AEAD algorithm to compute
1170 * (\c PSA_ALG_XXX value such that
1171 * #PSA_ALG_IS_AEAD(`alg`) is true)
1172 * \param[in] p_nonce Nonce or IV to use
1173 * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
1174 * \param[in] p_additional_data Additional data that has been
1175 * authenticated but not encrypted
1176 * \param[in] additional_data_length Size of `p_additional_data` in bytes
1177 * \param[in] p_ciphertext Data that has been authenticated and
1178 * encrypted.
1179 * For algorithms where the encrypted data
1180 * and the authentication tag are defined
1181 * as separate inputs, the buffer must
1182 * contain the encrypted data followed by
1183 * the authentication tag.
1184 * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
1185 * \param[out] p_plaintext Output buffer for the decrypted data
1186 * \param[in] plaintext_size Size of the `p_plaintext` buffer in
1187 * bytes
1188 * \param[out] p_plaintext_length On success, the size of the output in
1189 * the `p_plaintext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001190 *
1191 * \retval #PSA_SUCCESS
1192 * Success.
1193 */
Derek Miller16e72292018-10-15 16:14:24 -05001194typedef psa_status_t (*psa_aead_opaque_decrypt_t)(psa_key_slot_t key_slot,
1195 psa_algorithm_t algorithm,
1196 const uint8_t *p_nonce,
1197 size_t nonce_length,
1198 const uint8_t *p_additional_data,
1199 size_t additional_data_length,
1200 const uint8_t *p_ciphertext,
1201 size_t ciphertext_length,
1202 uint8_t *p_plaintext,
1203 size_t plaintext_size,
1204 size_t *p_plaintext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001205
1206/**
Derek Miller16e72292018-10-15 16:14:24 -05001207 * \brief A struct containing all of the function pointers needed to implement
1208 * Authenticated Encryption with Additional Data operations using opaque keys
Derek Miller5b3417a2018-10-10 17:55:03 -05001209 *
Derek Miller16e72292018-10-15 16:14:24 -05001210 * PSA Crypto API implementations should populate instances of the table as
1211 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001212 *
1213 * If one of the functions is not implemented, it should be set to NULL.
1214 */
1215struct psa_aead_opaque_t {
Derek Miller16e72292018-10-15 16:14:24 -05001216 /** Function that performs the AEAD encrypt operation */
1217 psa_aead_opaque_encrypt_t *p_encrypt;
1218 /** Function that performs the AEAD decrypt operation */
1219 psa_aead_opaque_decrypt_t *p_decrypt;
Derek Miller5b3417a2018-10-10 17:55:03 -05001220};
Derek Miller16e72292018-10-15 16:14:24 -05001221/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001222
1223/** \defgroup aead_transparent AEAD Transparent
1224 */
Derek Miller16e72292018-10-15 16:14:24 -05001225/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001226
1227/** Process an authenticated encryption operation.
1228 *
Derek Miller16e72292018-10-15 16:14:24 -05001229 * Functions that implement the prototype should be named in the following
1230 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001231 * ~~~~~~~~~~~~~{.c}
1232 * pcd_aead_<ALGO>_encrypt
1233 * ~~~~~~~~~~~~~
1234 * Where `ALGO` is the name of the AEAD algorithm
1235 *
Derek Miller16e72292018-10-15 16:14:24 -05001236 * \param[in] p_key A pointer to the key material
1237 * \param[in] key_length The size in bytes of the key material
1238 * \param[in] alg The AEAD algorithm to compute
1239 * (\c PSA_ALG_XXX value such that
1240 * #PSA_ALG_IS_AEAD(`alg`) is true)
1241 * \param[in] nonce Nonce or IV to use
1242 * \param[in] nonce_length Size of the `nonce` buffer in bytes
1243 * \param[in] additional_data Additional data that will be MACed
1244 * but not encrypted.
1245 * \param[in] additional_data_length Size of `additional_data` in bytes
1246 * \param[in] plaintext Data that will be MACed and
1247 * encrypted.
1248 * \param[in] plaintext_length Size of `plaintext` in bytes
1249 * \param[out] ciphertext Output buffer for the authenticated and
1250 * encrypted data. The additional data is
1251 * not part of this output. For algorithms
1252 * where the encrypted data and the
1253 * authentication tag are defined as
1254 * separate outputs, the authentication
1255 * tag is appended to the encrypted data.
1256 * \param[in] ciphertext_size Size of the `ciphertext` buffer in
1257 * bytes
1258 * This must be at least
1259 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(`alg`,
1260 * `plaintext_length`).
1261 * \param[out] ciphertext_length On success, the size of the output in
1262 * the `ciphertext` buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001263 *
1264 * \retval #PSA_SUCCESS
1265
1266 */
Derek Miller16e72292018-10-15 16:14:24 -05001267typedef psa_status_t (*psa_aead_transparent_encrypt_t)(const uint8_t *p_key,
1268 size_t key_length,
1269 psa_algorithm_t alg,
1270 const uint8_t *nonce,
1271 size_t nonce_length,
1272 const uint8_t *additional_data,
1273 size_t additional_data_length,
1274 const uint8_t *plaintext,
1275 size_t plaintext_length,
1276 uint8_t *ciphertext,
1277 size_t ciphertext_size,
1278 size_t *ciphertext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001279
1280/** Process an authenticated decryption operation.
1281 *
Derek Miller16e72292018-10-15 16:14:24 -05001282 * Functions that implement the prototype should be named in the following
1283 * convention:
Derek Miller5b3417a2018-10-10 17:55:03 -05001284 * ~~~~~~~~~~~~~{.c}
1285 * pcd_aead_<ALGO>_decrypt
1286 * ~~~~~~~~~~~~~
1287 * Where `ALGO` is the name of the AEAD algorithm
Derek Miller16e72292018-10-15 16:14:24 -05001288 * \param[in] p_key A pointer to the key material
1289 * \param[in] key_length The size in bytes of the key material
1290 * \param[in] alg The AEAD algorithm to compute
1291 * (\c PSA_ALG_XXX value such that
1292 * #PSA_ALG_IS_AEAD(`alg`) is true)
1293 * \param[in] nonce Nonce or IV to use
1294 * \param[in] nonce_length Size of the `nonce` buffer in bytes
1295 * \param[in] additional_data Additional data that has been MACed
1296 * but not encrypted
1297 * \param[in] additional_data_length Size of `additional_data` in bytes
1298 * \param[in] ciphertext Data that has been MACed and
1299 * encrypted
1300 * For algorithms where the encrypted data
1301 * and the authentication tag are defined
1302 * as separate inputs, the buffer must
1303 * contain the encrypted data followed by
1304 * the authentication tag.
1305 * \param[in] ciphertext_length Size of `ciphertext` in bytes
1306 * \param[out] plaintext Output buffer for the decrypted data
1307 * \param[in] plaintext_size Size of the `plaintext` buffer in
1308 * bytes
1309 * This must be at least
1310 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(`alg`,
1311 * `ciphertext_length`).
1312 * \param[out] plaintext_length On success, the size of the output
1313 * in the \b plaintext buffer
Derek Miller5b3417a2018-10-10 17:55:03 -05001314 *
1315 * \retval #PSA_SUCCESS
1316 * Success.
1317 */
Derek Miller16e72292018-10-15 16:14:24 -05001318typedef psa_status_t (*psa_aead_transparent_decrypt_t)(const uint8_t *p_key,
1319 size_t key_length,
1320 psa_algorithm_t alg,
1321 const uint8_t *nonce,
1322 size_t nonce_length,
1323 const uint8_t *additional_data,
1324 size_t additional_data_length,
1325 const uint8_t *ciphertext,
1326 size_t ciphertext_length,
1327 uint8_t *plaintext,
1328 size_t plaintext_size,
1329 size_t *plaintext_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001330
Derek Miller16e72292018-10-15 16:14:24 -05001331/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001332
1333
Derek Miller16e72292018-10-15 16:14:24 -05001334/** \defgroup driver_rng Entropy Generation
Derek Miller5b3417a2018-10-10 17:55:03 -05001335 */
Derek Miller16e72292018-10-15 16:14:24 -05001336/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001337
1338/** \brief A hardware-specific structure for a entropy providing hardware
1339 */
1340struct pcd_entropy_context_t {
1341 // Implementation specific
1342};
1343
1344/** \brief Initialize an entropy driver
1345 *
1346 *
Derek Miller16e72292018-10-15 16:14:24 -05001347 * \param[in,out] p_context A hardware-specific structure
1348 * containing any context information for
1349 * the implementation
Derek Miller5b3417a2018-10-10 17:55:03 -05001350 *
1351 * \retval PSA_SUCCESS
1352 */
Derek Miller16e72292018-10-15 16:14:24 -05001353typedef psa_status_t (*pcd_entropy_init_t)(struct pcd_entropy_context_t *p_context);
Derek Miller5b3417a2018-10-10 17:55:03 -05001354
1355/** \brief Get a specified number of bytes from the entropy source
1356 *
Derek Miller16e72292018-10-15 16:14:24 -05001357 * It retrives `buffer_size` bytes of data from the entropy source. The entropy
1358 * source will always fill the provided buffer to its full size, however, most
1359 * entropy sources have biases, and the actual amount of entropy contained in
1360 * the buffer will be less than the number of bytes.
1361 * The driver will return the actual number of bytes of entropy placed in the
1362 * buffer in `p_received_entropy_bytes`.
1363 * A PSA Crypto API implementation will likely feed the output of this function
1364 * into a Digital Random Bit Generator (DRBG), and typically has a minimum
1365 * amount of entropy that it needs.
1366 * To accomplish this, the PSA Crypto implementation should be designed to call
1367 * this function multiple times until it has received the required amount of
1368 * entropy from the entropy source.
Derek Miller5b3417a2018-10-10 17:55:03 -05001369 *
Derek Miller16e72292018-10-15 16:14:24 -05001370 * \param[in,out] p_context A hardware-specific structure
1371 * containing any context information
1372 * for the implementation
1373 * \param[out] p_buffer A caller-allocated buffer for the
1374 * retrieved bytes to be placed in
1375 * \param[in] buffer_size The allocated size of `p_buffer`
1376 * \param[out] p_received_entropy_bytes The amount of entropy (in bytes)
1377 * actually provided in `p_buffer`
Derek Miller5b3417a2018-10-10 17:55:03 -05001378 *
1379 * \retval PSA_SUCCESS
1380 */
Derek Miller16e72292018-10-15 16:14:24 -05001381typedef psa_status_t (*pcd_entropy_get_bytes_t)(struct pcd_entropy_context_t *p_context,
1382 uint8_t *p_buffer,
1383 uint32_t buffer_size,
1384 uint32_t *p_received_entropy_bytes);
Derek Miller5b3417a2018-10-10 17:55:03 -05001385
1386/**
Derek Miller16e72292018-10-15 16:14:24 -05001387 * \brief A struct containing all of the function pointers needed to interface
1388 * to an entropy source
Derek Miller5b3417a2018-10-10 17:55:03 -05001389 *
Derek Miller16e72292018-10-15 16:14:24 -05001390 * PSA Crypto API implementations should populate instances of the table as
1391 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001392 *
1393 * If one of the functions is not implemented, it should be set to NULL.
1394 */
1395struct pcd_entropy_t {
Derek Miller16e72292018-10-15 16:14:24 -05001396 /** Function that performs initialization for the entropy source */
1397 pcd_entropy_init_t *p_init;
1398 /** Function that performs the get_bytes operation for the entropy source
1399 */
1400 pcd_entropy_get_bytes_t *p_get_bytes;
Derek Miller5b3417a2018-10-10 17:55:03 -05001401};
Derek Miller16e72292018-10-15 16:14:24 -05001402/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001403
Derek Miller16e72292018-10-15 16:14:24 -05001404/** \defgroup driver_key_management Key Management
Derek Miller5b3417a2018-10-10 17:55:03 -05001405 */
Derek Miller16e72292018-10-15 16:14:24 -05001406/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001407
Derek Miller16e72292018-10-15 16:14:24 -05001408/** \brief Import a key in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001409 *
1410 * This function can support any output from psa_export_key(). Refer to the
1411 * documentation of psa_export_key() for the format for each key type.
1412 *
Derek Miller16e72292018-10-15 16:14:24 -05001413 * \param[in] key_slot Slot where the key will be stored. This must be a
1414 * valid slot for a key of the chosen type. It must
1415 * be unoccupied.
1416 * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value).
1417 * \param[in] p_data Buffer containing the key data.
1418 * \param[in] data_length Size of the `data` buffer in bytes.
Derek Miller5b3417a2018-10-10 17:55:03 -05001419 *
1420 * \retval #PSA_SUCCESS
1421 * Success.
1422 */
Derek Miller16e72292018-10-15 16:14:24 -05001423typedef psa_status_t (*pcd_opaque_import_key_t)(psa_key_slot_t key_slot,
1424 psa_key_type_t type,
1425 const uint8_t *p_data,
1426 size_t data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001427
1428/**
Derek Miller16e72292018-10-15 16:14:24 -05001429 * \brief Destroy a key and restore the slot to its default state
Derek Miller5b3417a2018-10-10 17:55:03 -05001430 *
1431 * This function destroys the content of the key slot from both volatile
1432 * memory and, if applicable, non-volatile storage. Implementations shall
1433 * make a best effort to ensure that any previous content of the slot is
1434 * unrecoverable.
1435 *
1436 * This function also erases any metadata such as policies. It returns the
1437 * specified slot to its default state.
1438 *
Derek Miller16e72292018-10-15 16:14:24 -05001439 * \param[in] key_slot The key slot to erase.
Derek Miller5b3417a2018-10-10 17:55:03 -05001440 *
1441 * \retval #PSA_SUCCESS
1442 * The slot's content, if any, has been erased.
1443 */
Derek Miller16e72292018-10-15 16:14:24 -05001444typedef psa_status_t (*pcd_destroy_key_t)(psa_key_slot_t key);
Derek Miller5b3417a2018-10-10 17:55:03 -05001445
1446/**
Derek Miller16e72292018-10-15 16:14:24 -05001447 * \brief Export a key in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001448 *
1449 * The output of this function can be passed to psa_import_key() to
1450 * create an equivalent object.
1451 *
Derek Miller16e72292018-10-15 16:14:24 -05001452 * If a key is created with `psa_import_key()` and then exported with
Derek Miller5b3417a2018-10-10 17:55:03 -05001453 * this function, it is not guaranteed that the resulting data is
1454 * identical: the implementation may choose a different representation
1455 * of the same key if the format permits it.
1456 *
1457 * For standard key types, the output format is as follows:
1458 *
1459 * - For symmetric keys (including MAC keys), the format is the
1460 * raw bytes of the key.
1461 * - For DES, the key data consists of 8 bytes. The parity bits must be
1462 * correct.
1463 * - For Triple-DES, the format is the concatenation of the
1464 * two or three DES keys.
1465 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
1466 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1467 * as RSAPrivateKey.
1468 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
1469 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
1470 *
Derek Miller16e72292018-10-15 16:14:24 -05001471 * \param[in] key Slot whose content is to be exported. This must
Derek Miller5b3417a2018-10-10 17:55:03 -05001472 * be an occupied key slot.
1473 * \param[out] p_data Buffer where the key data is to be written.
Derek Miller16e72292018-10-15 16:14:24 -05001474 * \param[in] data_size Size of the `p_data` buffer in bytes.
Derek Miller5b3417a2018-10-10 17:55:03 -05001475 * \param[out] p_data_length On success, the number of bytes
1476 * that make up the key data.
1477 *
1478 * \retval #PSA_SUCCESS
1479 * \retval #PSA_ERROR_EMPTY_SLOT
1480 * \retval #PSA_ERROR_NOT_PERMITTED
1481 * \retval #PSA_ERROR_NOT_SUPPORTED
1482 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1483 * \retval #PSA_ERROR_HARDWARE_FAILURE
1484 * \retval #PSA_ERROR_TAMPERING_DETECTED
1485 */
Derek Miller16e72292018-10-15 16:14:24 -05001486typedef psa_status_t (*pcd_export_key_t)(psa_key_slot_t key,
1487 uint8_t *p_data,
1488 size_t data_size,
1489 size_t *p_data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001490
1491/**
Derek Miller16e72292018-10-15 16:14:24 -05001492 * \brief Export a public key or the public part of a key pair in binary format
Derek Miller5b3417a2018-10-10 17:55:03 -05001493 *
1494 * The output of this function can be passed to psa_import_key() to
1495 * create an object that is equivalent to the public key.
1496 *
1497 * For standard key types, the output format is as follows:
1498 *
1499 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1500 * the format is the DER representation of the public key defined by RFC 5280
1501 * as SubjectPublicKeyInfo.
1502 *
Derek Miller16e72292018-10-15 16:14:24 -05001503 * \param[in] key_slot Slot whose content is to be exported. This must
Derek Miller5b3417a2018-10-10 17:55:03 -05001504 * be an occupied key slot.
1505 * \param[out] p_data Buffer where the key data is to be written.
Derek Miller16e72292018-10-15 16:14:24 -05001506 * \param[in] data_size Size of the `data` buffer in bytes.
Derek Miller5b3417a2018-10-10 17:55:03 -05001507 * \param[out] p_data_length On success, the number of bytes
1508 * that make up the key data.
1509 *
1510 * \retval #PSA_SUCCESS
1511 */
Derek Miller16e72292018-10-15 16:14:24 -05001512typedef psa_status_t (*pcd_export_public_key_t)(psa_key_slot_t key,
1513 uint8_t *p_data,
1514 size_t data_size,
1515 size_t *p_data_length);
Derek Miller5b3417a2018-10-10 17:55:03 -05001516
1517/**
Derek Miller16e72292018-10-15 16:14:24 -05001518 * \brief A struct containing all of the function pointers needed to for key
1519 * management using opaque keys
Derek Miller5b3417a2018-10-10 17:55:03 -05001520 *
Derek Miller16e72292018-10-15 16:14:24 -05001521 * PSA Crypto API implementations should populate instances of the table as
1522 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001523 *
1524 * If one of the functions is not implemented, it should be set to NULL.
1525 */
1526struct pcd_key_management_t {
Derek Miller16e72292018-10-15 16:14:24 -05001527 /** Function that performs the key import operation */
1528 pcd_opaque_import_key_t *p_import;
1529 /** Function that performs the key destroy operation */
1530 pcd_destroy_key_t *p_destroy;
1531 /** Function that performs the key export operation */
1532 pcd_export_key_t *p_export;
1533 /** Function that perforsm the public key export operation */
1534 pcd_export_public_key_t *p_export_public;
Derek Miller5b3417a2018-10-10 17:55:03 -05001535};
1536
Derek Miller16e72292018-10-15 16:14:24 -05001537/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001538
Derek Miller16e72292018-10-15 16:14:24 -05001539/** \defgroup driver_derivation Key Derivation and Agreement
Derek Miller5b3417a2018-10-10 17:55:03 -05001540 */
Derek Miller16e72292018-10-15 16:14:24 -05001541/**@{*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001542
1543/**
Derek Miller16e72292018-10-15 16:14:24 -05001544 * Key derivation is the process of generating new key material using an
1545 * existing key and additional parameters, iterating through a basic
1546 * cryptographic function, such as a hash.
1547 * Key agreement is a part of cryptographic protocols that allows two parties
1548 * to agree on the same key value, but starting from different original key
1549 * material.
1550 * The flows are similar, and the PSA Crypto Driver API uses the same functions
1551 * for both of the flows.
Derek Miller5b3417a2018-10-10 17:55:03 -05001552 *
Derek Miller16e72292018-10-15 16:14:24 -05001553 * There are two different final functions for the flows,
1554 * `pcd_key_derivation_derive` and `pcd_key_derivation_export`.
1555 * `pcd_key_derivation_derive` is used when the key material should be placed
1556 * in a slot on the hardware and not exposed to the caller.
1557 * `pcd_key_derivation_export` is used when the key material should be returned
1558 * to the PSA Cryptographic API implementation.
1559 *
1560 * Different key derivation algorithms require a different number of inputs.
1561 * Instead of having an API that takes as input variable length arrays, which
1562 * can be problemmatic to manage on embedded platforms, the inputs are passed
1563 * to the driver via a function, `pcd_key_derivation_collateral`, that is
1564 * called multiple times with different `collateral_id`s. Thus, for a key
1565 * derivation algorithm that required 3 paramter inputs, the flow would look
1566 * something like:
1567 * ~~~~~~~~~~~~~{.c}
1568 * pcd_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
1569 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_0,
1570 * p_collateral_0,
1571 * collateral_0_size);
1572 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_1,
1573 * p_collateral_1,
1574 * collateral_1_size);
1575 * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_2,
1576 * p_collateral_2,
1577 * collateral_2_size);
1578 * pcd_key_derivation_derive();
1579 * ~~~~~~~~~~~~~
1580 *
1581 * key agreement example:
1582 * ~~~~~~~~~~~~~{.c}
1583 * pcd_key_derivation_setup(alg, source_key. dest_key_size_bytes);
1584 * pcd_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
1585 * pcd_key_derivation_export(p_session_key,
1586 * session_key_size,
1587 * &session_key_length);
1588 * ~~~~~~~~~~~~~
1589 */
1590
1591struct pcd_key_derivation_context_t {
1592 // Implementation specific
1593};
1594
1595/** \brief Set up a key derivation operation by specifying the algorithm and
1596 * the source key sot
1597 *
1598 * \param[in,out] p_context A hardware-specific structure containing any
1599 * context information for the implementation
1600 * \param[in] kdf_alg The algorithm to be used for the key derivation
1601 * \param[in] souce_key The key to be used as the source material for the
1602 * key derivation
1603 *
1604 * \retval PSA_SUCCESS
1605 */
1606typedef psa_status_t (*pcd_key_derivation_setup_t)(struct pcd_key_derivation_context_t *p_context,
1607 psa_algorithm_t kdf_alg,
1608 psa_key_slot_t source_key);
1609
1610/** \brief Provide collateral (parameters) needed for a key derivation or key
1611 * agreement operation
1612 *
1613 * Since many key derivation algorithms require multiple parameters, it is
1614 * expeced that this function may be called multiple times for the same
1615 * operation, each with a different algorithm-specific `collateral_id`
1616 *
1617 * \param[in,out] p_context A hardware-specific structure containing any
1618 * context information for the implementation
1619 * \param[in] collateral_id An ID for the collateral being provided
1620 * \param[in] p_collateral A buffer containing the collateral data
1621 * \param[in] collateral_size The size in bytes of the collateral
1622 *
1623 * \retval PSA_SUCCESS
1624 */
1625typedef psa_status_t (*pcd_key_derivation_collateral_t)(struct pcd_key_derivation_context_t *p_context,
1626 uint32_t collateral_id,
1627 const uint8_t p_collateral,
1628 size_t collateral_size);
1629
1630/** \brief Perform the final key derivation step and place the generated key
1631 * material in a slot
1632 * \param[in,out] p_context A hardware-specific structure containing any
1633 * context information for the implementation
1634 * \param[in] dest_key The slot where the generated key material
1635 * should be placed
1636 *
1637 * \retval PSA_SUCCESS
1638 */
1639typedef psa_status_t (*pcd_key_derivation_derive_t)(struct pcd_key_derivation_context_t *p_context,
1640 psa_key_slot_t dest_key);
1641
1642/** \brief Perform the final step of a key agreement and place the generated
1643 * key material in a buffer
1644 *
1645 * \param[out] p_output Buffer in which to place the generated key
1646 * material
1647 * \param[in] output_size The size in bytes of `p_output`
1648 * \param[out] p_output_length Upon success, contains the number of bytes of
1649 * key material placed in `p_output`
1650 *
1651 * \retval PSA_SUCCESS
1652 */
1653typedef psa_status_t (*pcd_key_derivation_export_t)(uint8_t *p_output,
1654 size_t output_size,
1655 size_t *p_output_length);
1656
1657/**
1658 * \brief A struct containing all of the function pointers needed to for key
1659 * derivation and agreement
1660 *
1661 * PSA Crypto API implementations should populate instances of the table as
1662 * appropriate upon startup.
Derek Miller5b3417a2018-10-10 17:55:03 -05001663 *
1664 * If one of the functions is not implemented, it should be set to NULL.
1665 */
1666struct pcd_key_derivation_t {
Derek Miller16e72292018-10-15 16:14:24 -05001667 /** Function that performs the key derivation setup */
1668 pcd_key_derivation_setup_t *p_setup;
1669 /** Function that sets the key derivation collateral */
1670 pcd_key_derivation_collateral_t *p_collateral;
1671 /** Function that performs the final key derivation step */
1672 pcd_key_derivation_derive_t *p_derive;
1673 /** Function that perforsm the final key derivation or agreement and
1674 * exports the key */
1675 pcd_key_derivation_export_t *p_export;
Derek Miller5b3417a2018-10-10 17:55:03 -05001676};
1677
Derek Miller16e72292018-10-15 16:14:24 -05001678/**@}*/
Derek Miller5b3417a2018-10-10 17:55:03 -05001679
1680#endif // __PSA_CRYPTO_DRIVER_H__