Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1 | /** |
| 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 Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 27 | #ifndef __PSA_CRYPTO_DRIVER_H__ |
| 28 | #define __PSA_CRYPTO_DRIVER_H__ |
| 29 | |
| 30 | #include <stddef.h> |
| 31 | #include <stdint.h> |
| 32 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 33 | /** 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 Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 37 | typedef uint32_t psa_status_t; |
| 38 | typedef uint32_t psa_algorithm_t; |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 39 | typedef uint8_t encrypt_or_decrypt_t; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 40 | typedef uint32_t psa_key_slot_t; |
| 41 | typedef uint32_t psa_key_type_t; |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 42 | typedef uint32_t psa_key_usage_t; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 43 | |
| 44 | /** \defgroup opaque_mac Opaque Message Authentication Code |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 45 | * Generation and authentication of Message Authentication Codes (MACs) using |
| 46 | * opaque keys can be done either as a single function call (via the |
| 47 | * `pcd_mac_opaque_generate_t` or `psa_mac_opaque_verify_t` functions), or in |
| 48 | * parts using the following sequence: |
| 49 | * - `psa_mac_opaque_setup_t` |
| 50 | * - `psa_mac_opaque_update_t` |
| 51 | * - `psa_mac_opaque_update_t` |
| 52 | * - ... |
| 53 | * - `psa_mac_opaque_finish_t` or `psa_mac_opaque_finish_verify_t` |
| 54 | * |
| 55 | * If a previously started Opaque MAC operation needs to be terminated, it |
| 56 | * should be done so by the `psa_mac_opaque_abort_t`. Failure to do so may |
| 57 | * result in allocated resources not being freed or in other undefined |
| 58 | * behavior. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 59 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 60 | /**@{*/ |
| 61 | /** \brief A function that starts a MAC operation for a PSA Crypto Driver |
| 62 | * implementation using an opaque key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 63 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 64 | * \param[in,out] p_context A structure that will contain the |
| 65 | * hardware-specific MAC context |
| 66 | * \param[in] key_slot The slot of the key to be used for the |
| 67 | * operation |
| 68 | * \param[in] algorithm The algorithm to be used to underly the MAC |
| 69 | * operation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 70 | * |
| 71 | * \retval PSA_SUCCESS |
| 72 | * Success. |
| 73 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 74 | typedef psa_status_t (*pcd_mac_opaque_setup_t)(void *p_context, |
| 75 | psa_key_slot_t key_slot, |
| 76 | psa_algorithm_t algorithm); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 77 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 78 | /** \brief A function that continues a previously started MAC operation using |
| 79 | * an opaque key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 80 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 81 | * \param[in,out] p_context A hardware-specific structure for the |
| 82 | * previously-established MAC operation to be |
| 83 | * continued |
| 84 | * \param[in] p_input A buffer containing the message to be appended |
| 85 | * to the MAC operation |
| 86 | * \param[in] input_length The size in bytes of the input message buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 87 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 88 | typedef psa_status_t (*pcd_mac_opaque_update_t)(void *p_context, |
| 89 | const uint8_t *p_input, |
| 90 | size_t input_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 91 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 92 | /** \brief a function that completes a previously started MAC operation by |
| 93 | * returning the resulting MAC using an opaque key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 94 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 95 | * \param[in,out] p_context A hardware-specific structure for the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 96 | * previously started MAC operation to be |
| 97 | * finished |
| 98 | * \param[out] p_mac A buffer where the generated MAC will be |
| 99 | * placed |
| 100 | * \param[in] mac_size The size in bytes of the buffer that has been |
| 101 | * allocated for the `output` buffer |
| 102 | * \param[out] p_mac_length After completion, will contain the number of |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 103 | * bytes placed in the `p_mac` buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 104 | * |
| 105 | * \retval PSA_SUCCESS |
| 106 | * Success. |
| 107 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 108 | typedef psa_status_t (*pcd_mac_opaque_finish_t)(void *p_context, |
| 109 | uint8_t *p_mac, |
| 110 | size_t mac_size, |
| 111 | size_t *p_mac_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 112 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 113 | /** \brief A function that completes a previously started MAC operation by |
| 114 | * comparing the resulting MAC against a known value using an opaque key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 115 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 116 | * \param[in,out] p_context A hardware-specific structure for the previously |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 117 | * started MAC operation to be fiinished |
| 118 | * \param[in] p_mac The MAC value against which the resulting MAC will |
| 119 | * be compared against |
| 120 | * \param[in] mac_length The size in bytes of the value stored in `p_mac` |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 121 | * |
| 122 | * \retval PSA_SUCCESS |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 123 | * The operation completed successfully and the MACs matched each |
| 124 | * other |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 125 | * \retval PSA_ERROR_INVALID_SIGNATURE |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 126 | * The operation completed successfully, but the calculated MAC did |
| 127 | * not match the provided MAC |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 128 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 129 | typedef psa_status_t (*pcd_mac_opaque_finish_verify_t)(void *p_context, |
| 130 | const uint8_t *p_mac, |
| 131 | size_t mac_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 132 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 133 | /** \brief A function that aborts a previous started opaque-key MAC operation |
| 134 | |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 135 | * \param[in,out] p_context A hardware-specific structure for the previously |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 136 | * started MAC operation to be aborted |
| 137 | */ |
| 138 | typedef psa_status_t (*pcd_mac_opaque_abort_t)(void *p_context); |
| 139 | |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 140 | /** \brief A function that performs a MAC operation in one command and returns |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 141 | * the calculated MAC using an opaque key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 142 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 143 | * \param[in] p_input A buffer containing the message to be MACed |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 144 | * \param[in] input_length The size in bytes of `p_input` |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 145 | * \param[in] key_slot The slot of the key to be used |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 146 | * \param[in] alg The algorithm to be used to underlie the MAC |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 147 | * operation |
| 148 | * \param[out] p_mac A buffer where the generated MAC will be |
| 149 | * placed |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 150 | * \param[in] mac_size The size in bytes of the `p_mac` buffer |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 151 | * \param[out] p_mac_length After completion, will contain the number of |
| 152 | * bytes placed in the `output` buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 153 | * |
| 154 | * \retval PSA_SUCCESS |
| 155 | * Success. |
| 156 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 157 | typedef psa_status_t (*pcd_mac_opaque_generate_t)(const uint8_t *p_input, |
| 158 | size_t input_length, |
| 159 | psa_key_slot_t key_slot, |
| 160 | psa_algorithm_t alg, |
| 161 | uint8_t *p_mac, |
| 162 | size_t mac_size, |
| 163 | size_t *p_mac_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 164 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 165 | /** \brief A function that performs an MAC operation in one command and |
| 166 | * compare the resulting MAC against a known value using an opaque key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 167 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 168 | * \param[in] p_input A buffer containing the message to be MACed |
| 169 | * \param[in] input_length The size in bytes of `input` |
| 170 | * \param[in] key_slot The slot of the key to be used |
| 171 | * \param[in] alg The algorithm to be used to underlie the MAC |
| 172 | * operation |
| 173 | * \param[in] p_mac The MAC value against which the resulting MAC will |
| 174 | * be compared against |
| 175 | * \param[in] mac_length The size in bytes of `mac` |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 176 | * |
| 177 | * \retval PSA_SUCCESS |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 178 | * The operation completed successfully and the MACs matched each |
| 179 | * other |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 180 | * \retval PSA_ERROR_INVALID_SIGNATURE |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 181 | * The operation completed successfully, but the calculated MAC did |
| 182 | * not match the provided MAC |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 183 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 184 | typedef psa_status_t (*pcd_mac_opaque_verify_t)(const uint8_t *p_input, |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 185 | size_t input_length, |
| 186 | psa_key_slot_t key_slot, |
| 187 | psa_algorithm_t alg, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 188 | const uint8_t *p_mac, |
| 189 | size_t mac_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 190 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 191 | /** \brief A struct containing all of the function pointers needed to |
| 192 | * implement MAC operations using opaque keys. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 193 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 194 | * PSA Crypto API implementations should populate the table as appropriate |
| 195 | * upon startup. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 196 | * |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 197 | * If one of the functions is not implemented (such as |
| 198 | * `pcd_mac_opaque_generate_t`), it should be set to NULL. |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 199 | * |
| 200 | * Driver implementers should ensure that they implement all of the functions |
| 201 | * that make sense for their hardware, and that they provide a full solution |
| 202 | * (for example, if they support `p_setup`, they should also support |
| 203 | * `p_update` and at least one of `p_finish` or `p_finish_verify`). |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 204 | * |
| 205 | */ |
| 206 | struct pcd_mac_opaque_t { |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 207 | /**The size in bytes of the hardware-specific Opaque-MAC Context structure |
| 208 | */ |
| 209 | size_t context_size; |
| 210 | /** Function that performs the setup operation |
| 211 | */ |
| 212 | pcd_mac_opaque_setup_t *p_setup; |
| 213 | /** Function that performs the update operation |
| 214 | */ |
| 215 | pcd_mac_opaque_update_t *p_update; |
| 216 | /** Function that completes the operation |
| 217 | */ |
| 218 | pcd_mac_opaque_finish_t *p_finish; |
| 219 | /** Function that completed a MAC operation with a verify check |
| 220 | */ |
| 221 | pcd_mac_opaque_finish_verify_t *p_finish_verify; |
| 222 | /** Function that aborts a previoustly started operation |
| 223 | */ |
| 224 | pcd_mac_opaque_abort_t *p_abort; |
| 225 | /** Function that performs the MAC operation in one call |
| 226 | */ |
| 227 | pcd_mac_opaque_generate_t *p_mac; |
| 228 | /** Function that performs the MAC and verify operation in one call |
| 229 | */ |
| 230 | pcd_mac_opaque_verify_t *p_mac_verify; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 231 | }; |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 232 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 233 | |
| 234 | /** \defgroup transparent_mac Transparent Message Authentication Code |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 235 | * Generation and authentication of Message Authentication Codes (MACs) using |
| 236 | * transparent keys can be done either as a single function call (via the |
| 237 | * `pcd_mac_transparent_generate_t` or `psa_mac_transparent_verify_t` |
| 238 | * functions), or in parts using the following sequence: |
| 239 | * - `psa_mac_transparent_setup_t` |
| 240 | * - `psa_mac_transparent_update_t` |
| 241 | * - `psa_mac_transparent_update_t` |
| 242 | * - ... |
| 243 | * - `psa_mac_transparent_finish_t` or `psa_mac_transparent_finish_verify_t` |
| 244 | * |
| 245 | * If a previously started Transparent MAC operation needs to be terminated, it |
| 246 | * should be done so by the `psa_mac_transparent_abort_t`. Failure to do so may |
| 247 | * result in allocated resources not being freed or in other undefined |
| 248 | * behavior. |
| 249 | * |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 250 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 251 | /**@{*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 252 | |
| 253 | /** \brief The hardware-specific transparent-key MAC context structure |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 254 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 255 | * The contents of this structure are implementation dependent and are |
| 256 | * therefore not described here. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 257 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 258 | typedef struct pcd_mac_transparent_context_s pcd_mac_transparent_context_t; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 259 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 260 | /** \brief The function prototype for the setup operation of a |
| 261 | * transparent-key MAC operation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 262 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 263 | * Functions that implement the prototype should be named in the following |
| 264 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 265 | * ~~~~~~~~~~~~~{.c} |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 266 | * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_setup |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 267 | * ~~~~~~~~~~~~~ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 268 | * Where `ALGO` is the name of the underlying primitive, and `MAC_VARIANT` |
| 269 | * is the specific variant of a MAC operation (such as HMAC or CMAC) |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 270 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 271 | * \param[in,out] p_context A structure that will contain the |
| 272 | * hardware-specific MAC context |
| 273 | * \param[in] p_key A buffer containing the cleartext key material |
| 274 | * to be used in the operation |
| 275 | * \param[in] key_length The size in bytes of the key material |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 276 | * |
| 277 | * \retval PSA_SUCCESS |
| 278 | * Success. |
| 279 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 280 | typedef psa_status_t (*pcd_mac_transparent_setup_t)(pcd_mac_transparent_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 281 | const uint8_t *p_key, |
| 282 | size_t key_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 283 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 284 | /** \brief The function prototype for the update operation of a |
| 285 | * transparent-key MAC operation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 286 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 287 | * Functions that implement the prototype should be named in the following |
| 288 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 289 | * ~~~~~~~~~~~~~{.c} |
| 290 | * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_update |
| 291 | * ~~~~~~~~~~~~~ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 292 | * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` |
| 293 | * is the specific variant of a MAC operation (such as HMAC or CMAC) |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 294 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 295 | * \param[in,out] p_context A hardware-specific structure for the |
| 296 | * previously-established MAC operation to be |
| 297 | * continued |
| 298 | * \param[in] p_input A buffer containing the message to be appended |
| 299 | * to the MAC operation |
| 300 | * \param[in] input_length The size in bytes of the input message buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 301 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 302 | typedef psa_status_t (*pcd_mac_transparent_update_t)(pcd_mac_transparent_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 303 | const uint8_t *p_input, |
| 304 | size_t input_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 305 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 306 | /** \brief The function prototype for the finish operation of a |
| 307 | * transparent-key MAC operation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 308 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 309 | * Functions that implement the prototype should be named in the following |
| 310 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 311 | * ~~~~~~~~~~~~~{.c} |
| 312 | * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish |
| 313 | * ~~~~~~~~~~~~~ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 314 | * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is |
| 315 | * the specific variant of a MAC operation (such as HMAC or CMAC) |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 316 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 317 | * \param[in,out] p_context A hardware-specific structure for the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 318 | * previously started MAC operation to be |
| 319 | * finished |
| 320 | * \param[out] p_mac A buffer where the generated MAC will be placed |
| 321 | * \param[in] mac_length The size in bytes of the buffer that has been |
| 322 | * allocated for the `p_mac` buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 323 | * |
| 324 | * \retval PSA_SUCCESS |
| 325 | * Success. |
| 326 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 327 | typedef psa_status_t (*pcd_mac_transparent_finish_t)(pcd_mac_transparent_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 328 | uint8_t *p_mac, |
| 329 | size_t mac_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 330 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 331 | /** \brief The function prototype for the finish and verify operation of a |
| 332 | * transparent-key MAC operation |
| 333 | * |
| 334 | * Functions that implement the prototype should be named in the following |
| 335 | * convention: |
| 336 | * ~~~~~~~~~~~~~{.c} |
| 337 | * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_finish_verify |
| 338 | * ~~~~~~~~~~~~~ |
| 339 | * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is |
| 340 | * the specific variant of a MAC operation (such as HMAC or CMAC) |
| 341 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 342 | * \param[in,out] p_context A hardware-specific structure for the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 343 | * previously started MAC operation to be |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 344 | * verified and finished |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 345 | * \param[in] p_mac A buffer containing the MAC that will be used |
| 346 | * for verification |
| 347 | * \param[in] mac_length The size in bytes of the data in the `p_mac` |
| 348 | * buffer |
| 349 | * |
| 350 | * \retval PSA_SUCCESS |
| 351 | * The operation completed successfully and the comparison matched |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 352 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 353 | typedef psa_status_t (*pcd_mac_transparent_finish_verify_t)(pcd_mac_transparent_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 354 | const uint8_t *p_mac, |
| 355 | size_t mac_length); |
| 356 | |
| 357 | /** \brief The function prototype for the abort operation for a previously |
| 358 | * started transparent-key MAC operation |
| 359 | * |
| 360 | * Functions that implement the prototype should be named in the following |
| 361 | * convention: |
| 362 | * ~~~~~~~~~~~~~{.c} |
| 363 | * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_abort |
| 364 | * ~~~~~~~~~~~~~ |
| 365 | * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is |
| 366 | * the specific variant of a MAC operation (such as HMAC or CMAC) |
| 367 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 368 | * \param[in,out] p_context A hardware-specific structure for the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 369 | * previously started MAC operation to be |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 370 | * aborted |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 371 | * |
| 372 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 373 | typedef psa_status_t (*pcd_mac_transparent_abort_t)(pcd_mac_transparent_context_t *p_context); |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 374 | |
| 375 | /** \brief The function prototype for a one-shot operation of a transparent-key |
| 376 | * MAC operation |
| 377 | * |
| 378 | * Functions that implement the prototype should be named in the following |
| 379 | * convention: |
| 380 | * ~~~~~~~~~~~~~{.c} |
| 381 | * pcd_mac_transparent_<ALGO>_<MAC_VARIANT> |
| 382 | * ~~~~~~~~~~~~~ |
| 383 | * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is |
| 384 | * the specific variant of a MAC operation (such as HMAC or CMAC) |
| 385 | * |
| 386 | * \param[in] p_input A buffer containing the data to be MACed |
| 387 | * \param[in] input_length The length in bytes of the `p_input` data |
| 388 | * \param[in] p_key A buffer containing the key material to be used |
| 389 | * for the MAC operation |
| 390 | * \param[in] key_length The length in bytes of the `p_key` data |
| 391 | * \param[in] alg The algorithm to be performed |
| 392 | * \param[out] p_mac The buffer where the resulting MAC will be placed |
| 393 | * upon success |
| 394 | * \param[in] mac_length The length in bytes of the `p_mac` buffer |
| 395 | */ |
| 396 | typedef psa_status_t (*pcd_mac_transparent_t)(const uint8_t *p_input, |
| 397 | size_t input_length, |
| 398 | const uint8_t *p_key, |
| 399 | size_t key_length, |
| 400 | psa_algorithm_t alg, |
| 401 | uint8_t *p_mac, |
| 402 | size_t mac_length); |
| 403 | |
| 404 | /** \brief The function prototype for a one-shot operation of a transparent-key |
| 405 | * MAC Verify operation |
| 406 | * |
| 407 | * Functions that implement the prototype should be named in the following |
| 408 | * convention: |
| 409 | * ~~~~~~~~~~~~~{.c} |
| 410 | * pcd_mac_transparent_<ALGO>_<MAC_VARIANT>_verify |
| 411 | * ~~~~~~~~~~~~~ |
| 412 | * Where `ALGO` is the name of the underlying algorithm, and `MAC_VARIANT` is |
| 413 | * the specific variant of a MAC operation (such as HMAC or CMAC) |
| 414 | * |
| 415 | * \param[in] p_input A buffer containing the data to be MACed |
| 416 | * \param[in] input_length The length in bytes of the `p_input` data |
| 417 | * \param[in] p_key A buffer containing the key material to be used |
| 418 | * for the MAC operation |
| 419 | * \param[in] key_length The length in bytes of the `p_key` data |
| 420 | * \param[in] alg The algorithm to be performed |
| 421 | * \param[in] p_mac The MAC data to be compared |
| 422 | * \param[in] mac_length The length in bytes of the `p_mac` buffer |
| 423 | * |
| 424 | * \retval PSA_SUCCESS |
| 425 | * The operation completed successfully and the comparison matched |
| 426 | */ |
| 427 | typedef psa_status_t (*pcd_mac_transparent_verify_t)(const uint8_t *p_input, |
| 428 | size_t input_length, |
| 429 | const uint8_t *p_key, |
| 430 | size_t key_length, |
| 431 | psa_algorithm_t alg, |
| 432 | const uint8_t *p_mac, |
| 433 | size_t mac_length); |
| 434 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 435 | |
| 436 | /** \defgroup opaque_cipher Opaque Symmetric Ciphers |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 437 | * |
| 438 | * Encryption and Decryption using opaque keys in block modes other than ECB |
| 439 | * must be done in multiple parts, using the following flow: |
| 440 | * - `pcd_cipher_opaque_setup_t` |
| 441 | * - `pcd_cipher_opaque_set_iv_t` (optional depending upon block mode) |
| 442 | * - `pcd_cipher_opaque_update_t` |
| 443 | * - ... |
| 444 | * - `pcd_cipher_opaque_finish_t` |
| 445 | |
| 446 | * If a previously started Opaque Cipher operation needs to be terminated, it |
| 447 | * should be done so by the `psa_cipher_opaque_abort_t`. Failure to do so may |
| 448 | * result in allocated resources not being freed or in other undefined |
| 449 | * behavior. |
| 450 | * |
| 451 | * In situations where a PSA Cryptographic API implementation is using a block |
| 452 | * mode not-supported by the underlying hardware or driver, it can construct |
| 453 | * the block mode itself, while calling the `pcd_cipher_opaque_ecb_t` function |
| 454 | * pointer for the cipher operations. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 455 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 456 | /**@{*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 457 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 458 | /** \brief A function pointer that provides the cipher setup function for |
| 459 | * opaque-key operations |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 460 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 461 | * \param[in,out] p_context A structure that will contain the |
| 462 | * hardware-specific cipher context. |
| 463 | * \param[in] key_slot The slot of the key to be used for the |
| 464 | * operation |
| 465 | * \param[in] algorithm The algorithm to be used in the cipher |
| 466 | * operation |
| 467 | * \param[in] direction Indicates whether the operation is an encrypt |
| 468 | * or decrypt |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 469 | * |
| 470 | * \retval PSA_SUCCESS |
| 471 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 472 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 473 | typedef psa_status_t (*pcd_cipher_opaque_setup_t)(void *p_context, |
| 474 | psa_key_slot_t key_slot, |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 475 | psa_algorithm_t algorithm, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 476 | encrypt_or_decrypt_t direction); |
| 477 | |
| 478 | /** \brief A function pointer that sets the initialization vector (if |
| 479 | * necessary) for an opaque cipher operation |
| 480 | * |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 481 | * Rationale: The `psa_cipher_*` function in the PSA Cryptographic API has two |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 482 | * IV functions: one to set the IV, and one to generate it internally. The |
| 483 | * generate function is not necessary for the driver API as the PSA Crypto |
| 484 | * implementation can do the generation using its RNG features. |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 485 | * |
| 486 | * \param[in,out] p_context A structure that contains the previously set up |
| 487 | * hardware-specific cipher context |
| 488 | * \param[in] p_iv A buffer containing the initialization vector |
| 489 | * \param[in] iv_length The size (in bytes) of the `p_iv` buffer |
| 490 | * |
| 491 | * \retval PSA_SUCCESS |
| 492 | */ |
| 493 | typedef psa_status_t (*pcd_cipher_opaque_set_iv_t)(void *p_context, |
| 494 | const uint8_t *p_iv, |
| 495 | size_t iv_length); |
| 496 | |
| 497 | /** \brief A function that continues a previously started opaque-key cipher |
| 498 | * operation |
| 499 | * |
| 500 | * \param[in,out] p_context A hardware-specific structure for the |
| 501 | * previously started cipher operation |
| 502 | * \param[in] p_input A buffer containing the data to be |
| 503 | * encrypted/decrypted |
| 504 | * \param[in] input_size The size in bytes of the buffer pointed to |
| 505 | * by `p_input` |
| 506 | * \param[out] p_output The caller-allocated buffer where the |
| 507 | * output will be placed |
| 508 | * \param[in] output_size The allocated size in bytes of the |
| 509 | * `p_output` buffer |
| 510 | * \param[out] p_output_length After completion, will contain the number |
| 511 | * of bytes placed in the `p_output` buffer |
| 512 | * |
| 513 | * \retval PSA_SUCCESS |
| 514 | */ |
| 515 | typedef psa_status_t (*pcd_cipher_opaque_update_t)(void *p_context, |
| 516 | const uint8_t *p_input, |
| 517 | size_t input_size, |
| 518 | uint8_t *p_output, |
| 519 | size_t output_size, |
| 520 | size_t *p_output_length); |
| 521 | |
| 522 | /** \brief A function that completes a previously started opaque-key cipher |
| 523 | * operation |
| 524 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 525 | * \param[in,out] p_context A hardware-specific structure for the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 526 | * previously started cipher operation |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 527 | * \param[out] p_output The caller-allocated buffer where the output |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 528 | * will be placed |
| 529 | * \param[in] output_size The allocated size in bytes of the `p_output` |
| 530 | * buffer |
| 531 | * \param[out] p_output_length After completion, will contain the number of |
| 532 | * bytes placed in the `p_output` buffer |
| 533 | * |
| 534 | * \retval PSA_SUCCESS |
| 535 | */ |
| 536 | typedef psa_status_t (*pcd_cipher_opaque_finish_t)(void *p_context, |
| 537 | uint8_t *p_output, |
| 538 | size_t output_size, |
| 539 | size_t *p_output_length); |
| 540 | |
| 541 | /** \brief A function that aborts a previously started opaque-key cipher |
| 542 | * operation |
| 543 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 544 | * \param[in,out] p_context A hardware-specific structure for the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 545 | * previously started cipher operation |
| 546 | */ |
| 547 | typedef psa_status_t (*pcd_cipher_opaque_abort_t)(void *p_context); |
| 548 | |
| 549 | /** \brief A function that performs the ECB block mode for opaque-key cipher |
| 550 | * operations |
| 551 | * |
| 552 | * Note: this function should only be used with implementations that do not |
| 553 | * provide a needed higher-level operation. |
| 554 | * |
| 555 | * \param[in] key_slot The slot of the key to be used for the operation |
| 556 | * \param[in] algorithm The algorithm to be used in the cipher operation |
| 557 | * \param[in] direction Indicates whether the operation is an encrypt or |
| 558 | * decrypt |
| 559 | * \param[in] p_input A buffer containing the data to be |
| 560 | * encrypted/decrypted |
| 561 | * \param[in] input_size The size in bytes of the buffer pointed to by |
| 562 | * `p_input` |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 563 | * \param[out] p_output The caller-allocated buffer where the output will |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 564 | * be placed |
| 565 | * \param[in] output_size The allocated size in bytes of the `p_output` |
| 566 | * buffer |
| 567 | * |
| 568 | * \retval PSA_SUCCESS |
| 569 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 570 | */ |
| 571 | typedef psa_status_t (*pcd_cipher_opaque_ecb_t)(psa_key_slot_t key_slot, |
| 572 | psa_algorithm_t algorithm, |
| 573 | encrypt_or_decrypt_t direction, |
| 574 | const uint8_t *p_input, |
| 575 | size_t input_size, |
| 576 | uint8_t *p_output, |
| 577 | size_t output_size); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 578 | |
| 579 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 580 | * \brief A struct containing all of the function pointers needed to implement |
| 581 | * cipher operations using opaque keys. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 582 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 583 | * PSA Crypto API implementations should populate instances of the table as |
| 584 | * appropriate upon startup. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 585 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 586 | * If one of the functions is not implemented (such as |
| 587 | * `pcd_cipher_opaque_ecb_t`), it should be set to NULL. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 588 | */ |
| 589 | struct pcd_cipher_opaque_t { |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 590 | /** The size in bytes of the hardware-specific Opaque Cipher context |
| 591 | * structure |
| 592 | */ |
| 593 | size_t size; |
| 594 | /** Function that performs the setup operation */ |
| 595 | pcd_cipher_opaque_setup_t *p_setup; |
| 596 | /** Function that sets the IV (if necessary) */ |
| 597 | pcd_cipher_opaque_set_iv_t *p_set_iv; |
| 598 | /** Function that performs the update operation */ |
| 599 | pcd_cipher_opaque_update_t *p_update; |
| 600 | /** Function that completes the operation */ |
| 601 | pcd_cipher_opaque_finish_t *p_finish; |
| 602 | /** Function that aborts the operation */ |
| 603 | pcd_cipher_opaque_abort_t *p_abort; |
| 604 | /** Function that performs ECB mode for the cipher |
| 605 | * (Danger: ECB mode should not be used directly by clients of the PSA |
| 606 | * Crypto Client API) |
| 607 | */ |
| 608 | pcd_cipher_opaque_ecb_t *p_ecb; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 609 | }; |
| 610 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 611 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 612 | |
| 613 | /** \defgroup transparent_cipher Transparent Block Cipher |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 614 | * Encryption and Decryption using transparent keys in block modes other than |
| 615 | * ECB must be done in multiple parts, using the following flow: |
| 616 | * - `pcd_cipher_transparent_setup_t` |
| 617 | * - `pcd_cipher_transparent_set_iv_t` (optional depending upon block mode) |
| 618 | * - `pcd_cipher_transparent_update_t` |
| 619 | * - ... |
| 620 | * - `pcd_cipher_transparent_finish_t` |
| 621 | |
| 622 | * If a previously started Transparent Cipher operation needs to be terminated, |
| 623 | * it should be done so by the `psa_cipher_transparent_abort_t`. Failure to do |
| 624 | * so may result in allocated resources not being freed or in other undefined |
| 625 | * behavior. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 626 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 627 | /**@{*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 628 | |
| 629 | /** \brief The hardware-specific transparent-key Cipher context structure |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 630 | * |
| 631 | * The contents of this structure are implementation dependent and are |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 632 | * therefore not described here. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 633 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 634 | typedef struct pcd_cipher_transparent_context_s pcd_cipher_transparent_context_t; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 635 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 636 | /** \brief The function prototype for the setup operation of transparent-key |
| 637 | * block cipher operations. |
| 638 | * Functions that implement the prototype should be named in the following |
| 639 | * conventions: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 640 | * ~~~~~~~~~~~~~{.c} |
| 641 | * pcd_cipher_transparent_setup_<CIPHER_NAME>_<MODE> |
| 642 | * ~~~~~~~~~~~~~ |
| 643 | * Where |
| 644 | * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES) |
| 645 | * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR) |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 646 | * or for stream ciphers: |
| 647 | * ~~~~~~~~~~~~~{.c} |
| 648 | * pcd_cipher_transparent_setup_<CIPHER_NAME> |
| 649 | * ~~~~~~~~~~~~~ |
| 650 | * Where `CIPHER_NAME` is the name of a stream cipher (i.e. RC4) |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 651 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 652 | * \param[in,out] p_context A structure that will contain the |
| 653 | * hardware-specific cipher context |
| 654 | * \param[in] direction Indicates if the operation is an encrypt or a |
| 655 | * decrypt |
| 656 | * \param[in] p_key_data A buffer containing the cleartext key material |
| 657 | * to be used in the operation |
| 658 | * \param[in] key_data_size The size in bytes of the key material |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 659 | * |
| 660 | * \retval PSA_SUCCESS |
| 661 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 662 | typedef psa_status_t (*pcd_cipher_transparent_setup_t)(pcd_cipher_transparent_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 663 | encrypt_or_decrypt_t direction, |
| 664 | const uint8_t *p_key_data, |
| 665 | size_t key_data_size); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 666 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 667 | /** \brief The function prototype for the set initialization vector operation |
| 668 | * of transparent-key block cipher operations |
| 669 | * Functions that implement the prototype should be named in the following |
| 670 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 671 | * ~~~~~~~~~~~~~{.c} |
| 672 | * pcd_cipher_transparent_set_iv_<CIPHER_NAME>_<MODE> |
| 673 | * ~~~~~~~~~~~~~ |
| 674 | * Where |
| 675 | * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES) |
| 676 | * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR) |
| 677 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 678 | * \param[in,out] p_context A structure that contains the previously setup |
| 679 | * hardware-specific cipher context |
| 680 | * \param[in] p_iv A buffer containing the initialization vecotr |
| 681 | * \param[in] iv_length The size in bytes of the contents of `p_iv` |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 682 | * |
| 683 | * \retval PSA_SUCCESS |
| 684 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 685 | typedef psa_status_t (*pcd_cipher_transparent_set_iv_t)(pcd_cipher_transparent_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 686 | const uint8_t *p_iv, |
| 687 | size_t iv_length); |
| 688 | |
| 689 | /** \brief The function prototype for the update operation of transparent-key |
| 690 | * block cipher operations. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 691 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 692 | * Functions that implement the prototype should be named in the following |
| 693 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 694 | * ~~~~~~~~~~~~~{.c} |
| 695 | * pcd_cipher_transparent_update_<CIPHER_NAME>_<MODE> |
| 696 | * ~~~~~~~~~~~~~ |
| 697 | * Where |
| 698 | * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES) |
| 699 | * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR) |
| 700 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 701 | * \param[in,out] p_context A hardware-specific structure for the |
| 702 | * previously started cipher operation |
| 703 | * \param[in] p_input A buffer containing the data to be |
| 704 | * encrypted or decrypted |
| 705 | * \param[in] input_size The size in bytes of the `p_input` buffer |
| 706 | * \param[out] p_output A caller-allocated buffer where the |
| 707 | * generated output will be placed |
| 708 | * \param[in] output_size The size in bytes of the `p_output` buffer |
| 709 | * \param[out] p_output_length After completion, will contain the number |
| 710 | * of bytes placed in the `p_output` buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 711 | * |
| 712 | * \retval PSA_SUCCESS |
| 713 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 714 | typedef psa_status_t (*pcd_cipher_transparent_update_t)(pcd_cipher_transparent_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 715 | const uint8_t *p_input, |
| 716 | size_t input_size, |
| 717 | uint8_t *p_output, |
| 718 | size_t output_size, |
| 719 | size_t *p_output_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 720 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 721 | /** \brief The function prototype for the finish operation of transparent-key |
| 722 | * block cipher operations. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 723 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 724 | * Functions that implement the prototype should be named in the following |
| 725 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 726 | * ~~~~~~~~~~~~~{.c} |
| 727 | * pcd_cipher_transparent_finish_<CIPHER_NAME>_<MODE> |
| 728 | * ~~~~~~~~~~~~~ |
| 729 | * Where |
| 730 | * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES) |
| 731 | * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR) |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 732 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 733 | * \param[in,out] p_context A hardware-specific structure for the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 734 | * previously started cipher operation |
| 735 | * \param[out] p_output A caller-allocated buffer where the generated |
| 736 | * output will be placed |
| 737 | * \param[in] output_size The size in bytes of the `p_output` buffer |
| 738 | * \param[out] p_output_length After completion, will contain the number of |
| 739 | * bytes placed in the `p_output` buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 740 | * |
| 741 | * \retval PSA_SUCCESS |
| 742 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 743 | typedef psa_status_t (*pcd_cipher_transparent_finish_t)(pcd_cipher_transparent_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 744 | uint8_t *p_output, |
| 745 | size_t output_size, |
| 746 | size_t *p_output_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 747 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 748 | /** \brief The function prototype for the abort operation of transparent-key |
| 749 | * block cipher operations. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 750 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 751 | * Functions that implement the following prototype should be named in the |
| 752 | * following convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 753 | * ~~~~~~~~~~~~~{.c} |
| 754 | * pcd_cipher_transparent_abort_<CIPHER_NAME>_<MODE> |
| 755 | * ~~~~~~~~~~~~~ |
| 756 | * Where |
| 757 | * - `CIPHER_NAME` is the name of the underlying block cipher (i.e. AES or DES) |
| 758 | * - `MODE` is the block mode of the cipher operation (i.e. CBC or CTR) |
| 759 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 760 | * \param[in,out] p_context A hardware-specific structure for the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 761 | * previously started cipher operation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 762 | * |
| 763 | * \retval PSA_SUCCESS |
| 764 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 765 | typedef psa_status_t (*pcd_cipher_transparent_abort_t)(pcd_cipher_transparent_context_t *p_context); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 766 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 767 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 768 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 769 | /** \defgroup driver_digest Message Digests |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 770 | * |
| 771 | * Generation and authentication of Message Digests (aka hashes) must be done |
| 772 | * in parts using the following sequence: |
| 773 | * - `psa_hash_setup_t` |
| 774 | * - `psa_hash_update_t` |
| 775 | * - ... |
| 776 | * - `psa_hash_finish_t` |
| 777 | * |
| 778 | * If a previously started Message Digest operation needs to be terminated |
| 779 | * before the `psa_hash_finish_t` operation is complete, it should be aborted |
| 780 | * by the `psa_hash_abort_t`. Failure to do so may result in allocated |
| 781 | * resources not being freed or in other undefined behavior. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 782 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 783 | /**@{*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 784 | |
| 785 | /** \brief The hardware-specific hash context structure |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 786 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 787 | * The contents of this structure are implementation dependent and are |
| 788 | * therefore not described here |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 789 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 790 | typedef struct pcd_hash_context_s pcd_hash_context_t; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 791 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 792 | /** \brief The function prototype for the start operation of a hash (message |
| 793 | * digest) operation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 794 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 795 | * Functions that implement the prototype should be named in the following |
| 796 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 797 | * ~~~~~~~~~~~~~{.c} |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 798 | * pcd_hash_<ALGO>_setup |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 799 | * ~~~~~~~~~~~~~ |
| 800 | * Where `ALGO` is the name of the underlying hash function |
| 801 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 802 | * \param[in,out] p_context A structure that will contain the |
| 803 | * hardware-specific hash context |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 804 | * |
| 805 | * \retval PSA_SUCCESS Success. |
| 806 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 807 | typedef psa_status_t (*pcd_hash_setup_t)(pcd_hash_context_t *p_context); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 808 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 809 | /** \brief The function prototype for the update operation of a hash (message |
| 810 | * digest) operation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 811 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 812 | * Functions that implement the prototype should be named in the following |
| 813 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 814 | * ~~~~~~~~~~~~~{.c} |
| 815 | * pcd_hash_<ALGO>_update |
| 816 | * ~~~~~~~~~~~~~ |
| 817 | * Where `ALGO` is the name of the underlying algorithm |
| 818 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 819 | * \param[in,out] p_context A hardware-specific structure for the |
| 820 | * previously-established hash operation to be |
| 821 | * continued |
| 822 | * \param[in] p_input A buffer containing the message to be appended |
| 823 | * to the hash operation |
| 824 | * \param[in] input_length The size in bytes of the input message buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 825 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 826 | typedef psa_status_t (*pcd_hash_update_t)(pcd_hash_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 827 | const uint8_t *p_input, |
| 828 | size_t input_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 829 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 830 | /** \brief The prototype for the finish operation of a hash (message digest) |
| 831 | * operation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 832 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 833 | * Functions that implement the prototype should be named in the following |
| 834 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 835 | * ~~~~~~~~~~~~~{.c} |
| 836 | * pcd_hash_<ALGO>_finish |
| 837 | * ~~~~~~~~~~~~~ |
| 838 | * Where `ALGO` is the name of the underlying algorithm |
| 839 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 840 | * \param[in,out] p_context A hardware-specific structure for the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 841 | * previously started hash operation to be |
| 842 | * fiinished |
| 843 | * \param[out] p_output A buffer where the generated digest will be |
| 844 | * placed |
| 845 | * \param[in] output_size The size in bytes of the buffer that has been |
| 846 | * allocated for the `p_output` buffer |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 847 | * \param[out] p_output_length The number of bytes placed in `p_output` after |
| 848 | * success |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 849 | * |
| 850 | * \retval PSA_SUCCESS |
| 851 | * Success. |
| 852 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 853 | typedef psa_status_t (*pcd_hash_finish_t)(pcd_hash_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 854 | uint8_t *p_output, |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 855 | size_t output_size, |
| 856 | size_t *p_output_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 857 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 858 | /** \brief The function prototype for the abort operation of a hash (message |
| 859 | * digest) operation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 860 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 861 | * Functions that implement the prototype should be named in the following |
| 862 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 863 | * ~~~~~~~~~~~~~{.c} |
| 864 | * pcd_hash_<ALGO>_abort |
| 865 | * ~~~~~~~~~~~~~ |
| 866 | * Where `ALGO` is the name of the underlying algorithm |
| 867 | * |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 868 | * \param[in,out] p_context A hardware-specific structure for the previously |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 869 | * started hash operation to be aborted |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 870 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 871 | typedef void (*pcd_hash_abort_t)(pcd_hash_context_t *p_context); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 872 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 873 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 874 | |
| 875 | |
| 876 | /** \defgroup opaque_asymmetric Opaque Asymmetric Cryptography |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 877 | * |
| 878 | * Since the amount of data that can (or should) be encrypted or signed using |
| 879 | * asymmetric keys is limited by the key size, asymmetric key operations using |
| 880 | * opaque keys must be done in single function calls. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 881 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 882 | /**@{*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 883 | |
| 884 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 885 | * \brief A function that signs a hash or short message with a private key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 886 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 887 | * \param[in] key_slot Key slot of an asymmetric key pair |
| 888 | * \param[in] alg A signature algorithm that is compatible |
| 889 | * with the type of `key` |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 890 | * \param[in] p_hash The hash to sign |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 891 | * \param[in] hash_length Size of the `p_hash` buffer in bytes |
| 892 | * \param[out] p_signature Buffer where the signature is to be written |
Derek Miller | f3d0a56 | 2018-10-18 16:41:08 -0500 | [diff] [blame] | 893 | * \param[in] signature_size Size of the `p_signature` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 894 | * \param[out] p_signature_length On success, the number of bytes |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 895 | * that make up the returned signature value |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 896 | * |
| 897 | * \retval PSA_SUCCESS |
| 898 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 899 | typedef psa_status_t (*pcd_asymmetric_opaque_sign_t)(psa_key_slot_t key_slot, |
| 900 | psa_algorithm_t alg, |
| 901 | const uint8_t *p_hash, |
| 902 | size_t hash_length, |
| 903 | uint8_t *p_signature, |
| 904 | size_t signature_size, |
| 905 | size_t *p_signature_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 906 | |
| 907 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 908 | * \brief A function that verifies the signature a hash or short message using |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 909 | * an asymmetric public key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 910 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 911 | * \param[in] key_slot Key slot of a public key or an asymmetric key |
| 912 | * pair |
| 913 | * \param[in] alg A signature algorithm that is compatible with |
| 914 | * the type of `key` |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 915 | * \param[in] p_hash The hash whose signature is to be verified |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 916 | * \param[in] hash_length Size of the `p_hash` buffer in bytes |
| 917 | * \param[in] p_signature Buffer containing the signature to verify |
| 918 | * \param[in] signature_length Size of the `p_signature` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 919 | * |
| 920 | * \retval PSA_SUCCESS |
| 921 | * The signature is valid. |
| 922 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 923 | typedef psa_status_t (*pcd_asymmetric_opaque_verify_t)(psa_key_slot_t key_slot, |
| 924 | psa_algorithm_t alg, |
| 925 | const uint8_t *p_hash, |
| 926 | size_t hash_length, |
| 927 | const uint8_t *p_signature, |
| 928 | size_t signature_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 929 | |
| 930 | /** |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 931 | * \brief A function that encrypts a short message with an asymmetric public |
| 932 | * key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 933 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 934 | * \param[in] key_slot Key slot of a public key or an asymmetric key |
| 935 | * pair |
| 936 | * \param[in] alg An asymmetric encryption algorithm that is |
| 937 | * compatible with the type of `key` |
| 938 | * \param[in] p_input The message to encrypt |
| 939 | * \param[in] input_length Size of the `p_input` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 940 | * \param[in] p_salt A salt or label, if supported by the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 941 | * encryption algorithm |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 942 | * If the algorithm does not support a |
| 943 | * salt, pass `NULL`. |
| 944 | * If the algorithm supports an optional |
| 945 | * salt and you do not want to pass a salt, |
| 946 | * pass `NULL`. |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 947 | * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 948 | * supported. |
| 949 | * \param[in] salt_length Size of the `p_salt` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 950 | * If `p_salt` is `NULL`, pass 0. |
| 951 | * \param[out] p_output Buffer where the encrypted message is to |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 952 | * be written |
| 953 | * \param[in] output_size Size of the `p_output` buffer in bytes |
| 954 | * \param[out] p_output_length On success, the number of bytes that make up |
| 955 | * the returned output |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 956 | * |
| 957 | * \retval PSA_SUCCESS |
| 958 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 959 | typedef psa_status_t (*pcd_asymmetric_opaque_encrypt_t)(psa_key_slot_t key_slot, |
| 960 | psa_algorithm_t alg, |
| 961 | const uint8_t *p_input, |
| 962 | size_t input_length, |
| 963 | const uint8_t *p_salt, |
| 964 | size_t salt_length, |
| 965 | uint8_t *p_output, |
| 966 | size_t output_size, |
| 967 | size_t *p_output_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 968 | |
| 969 | /** |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 970 | * \brief Decrypt a short message with an asymmetric private key. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 971 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 972 | * \param[in] key_slot Key slot of an asymmetric key pair |
| 973 | * \param[in] alg An asymmetric encryption algorithm that is |
| 974 | * compatible with the type of `key` |
| 975 | * \param[in] p_input The message to decrypt |
| 976 | * \param[in] input_length Size of the `p_input` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 977 | * \param[in] p_salt A salt or label, if supported by the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 978 | * encryption algorithm |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 979 | * If the algorithm does not support a |
| 980 | * salt, pass `NULL`. |
| 981 | * If the algorithm supports an optional |
| 982 | * salt and you do not want to pass a salt, |
| 983 | * pass `NULL`. |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 984 | * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 985 | * supported. |
| 986 | * \param[in] salt_length Size of the `p_salt` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 987 | * If `p_salt` is `NULL`, pass 0. |
| 988 | * \param[out] p_output Buffer where the decrypted message is to |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 989 | * be written |
| 990 | * \param[in] output_size Size of the `p_output` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 991 | * \param[out] p_output_length On success, the number of bytes |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 992 | * that make up the returned output |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 993 | * |
| 994 | * \retval PSA_SUCCESS |
| 995 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 996 | typedef psa_status_t (*pcd_asymmetric_opaque_decrypt_t)(psa_key_slot_t key_slot, |
| 997 | psa_algorithm_t alg, |
| 998 | const uint8_t *p_input, |
| 999 | size_t input_length, |
| 1000 | const uint8_t *p_salt, |
| 1001 | size_t salt_length, |
| 1002 | uint8_t *p_output, |
| 1003 | size_t output_size, |
| 1004 | size_t *p_output_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1005 | |
| 1006 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1007 | * \brief A struct containing all of the function pointers needed to implement |
| 1008 | * asymmetric cryptographic operations using opaque keys. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1009 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1010 | * PSA Crypto API implementations should populate instances of the table as |
| 1011 | * appropriate upon startup. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1012 | * |
| 1013 | * If one of the functions is not implemented, it should be set to NULL. |
| 1014 | */ |
| 1015 | struct pcd_asymmetric_opaque_t { |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1016 | /** Function that performs the asymmetric sign operation */ |
| 1017 | pcd_asymmetric_opaque_sign_t *p_sign; |
| 1018 | /** Function that performs the asymmetric verify operation */ |
| 1019 | pcd_asymmetric_opaque_verify_t *p_verify; |
| 1020 | /** Function that performs the asymmetric encrypt operation */ |
| 1021 | pcd_asymmetric_opaque_encrypt_t *p_encrypt; |
| 1022 | /** Function that performs the asymmetric decrypt operation */ |
| 1023 | pcd_asymmetric_opaque_decrypt_t *p_decrypt; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1024 | }; |
| 1025 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1026 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1027 | |
| 1028 | /** \defgroup transparent_asymmetric Transparent Asymmetric Cryptography |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1029 | * |
| 1030 | * Since the amount of data that can (or should) be encrypted or signed using |
| 1031 | * asymmetric keys is limited by the key size, asymmetric key operations using |
| 1032 | * transparent keys must be done in single function calls. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1033 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1034 | /**@{*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1035 | |
| 1036 | |
| 1037 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1038 | * \brief A function that signs a hash or short message with a transparent |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1039 | * asymmetric private key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1040 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1041 | * Functions that implement the prototype should be named in the following |
| 1042 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1043 | * ~~~~~~~~~~~~~{.c} |
| 1044 | * pcd_asymmetric_<ALGO>_sign |
| 1045 | * ~~~~~~~~~~~~~ |
| 1046 | * Where `ALGO` is the name of the signing algorithm |
| 1047 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1048 | * \param[in] p_key A buffer containing the private key |
| 1049 | * material |
| 1050 | * \param[in] key_size The size in bytes of the `p_key` data |
| 1051 | * \param[in] alg A signature algorithm that is compatible |
| 1052 | * with the type of `p_key` |
| 1053 | * \param[in] p_hash The hash or message to sign |
| 1054 | * \param[in] hash_length Size of the `p_hash` buffer in bytes |
| 1055 | * \param[out] p_signature Buffer where the signature is to be written |
| 1056 | * \param[in] signature_size Size of the `p_signature` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1057 | * \param[out] p_signature_length On success, the number of bytes |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1058 | * that make up the returned signature value |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1059 | * |
| 1060 | * \retval PSA_SUCCESS |
| 1061 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1062 | typedef psa_status_t (*pcd_asymmetric_transparent_sign_t)(const uint8_t *p_key, |
| 1063 | size_t key_size, |
| 1064 | psa_algorithm_t alg, |
| 1065 | const uint8_t *p_hash, |
| 1066 | size_t hash_length, |
| 1067 | uint8_t *p_signature, |
| 1068 | size_t signature_size, |
| 1069 | size_t *p_signature_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1070 | |
| 1071 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1072 | * \brief A function that verifies the signature a hash or short message using |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1073 | * a transparent asymmetric public key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1074 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1075 | * Functions that implement the prototype should be named in the following |
| 1076 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1077 | * ~~~~~~~~~~~~~{.c} |
| 1078 | * pcd_asymmetric_<ALGO>_verify |
| 1079 | * ~~~~~~~~~~~~~ |
| 1080 | * Where `ALGO` is the name of the signing algorithm |
| 1081 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1082 | * \param[in] p_key A buffer containing the public key material |
| 1083 | * \param[in] key_size The size in bytes of the `p_key` data |
| 1084 | * \param[in] alg A signature algorithm that is compatible with |
| 1085 | * the type of `key` |
| 1086 | * \param[in] p_hash The hash or message whose signature is to be |
| 1087 | * verified |
| 1088 | * \param[in] hash_length Size of the `p_hash` buffer in bytes |
| 1089 | * \param[in] p_signature Buffer containing the signature to verify |
| 1090 | * \param[in] signature_length Size of the `p_signature` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1091 | * |
| 1092 | * \retval PSA_SUCCESS |
| 1093 | * The signature is valid. |
| 1094 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1095 | typedef psa_status_t (*pcd_asymmetric_transparent_verify_t)(const uint8_t *p_key, |
| 1096 | size_t key_size, |
| 1097 | psa_algorithm_t alg, |
| 1098 | const uint8_t *p_hash, |
| 1099 | size_t hash_length, |
| 1100 | const uint8_t *p_signature, |
| 1101 | size_t signature_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1102 | |
| 1103 | /** |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1104 | * \brief A function that encrypts a short message with a transparent |
| 1105 | * asymmetric public key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1106 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1107 | * Functions that implement the prototype should be named in the following |
| 1108 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1109 | * ~~~~~~~~~~~~~{.c} |
| 1110 | * pcd_asymmetric_<ALGO>_encrypt |
| 1111 | * ~~~~~~~~~~~~~ |
| 1112 | * Where `ALGO` is the name of the encryption algorithm |
| 1113 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1114 | * \param[in] p_key A buffer containing the public key material |
| 1115 | * \param[in] key_size The size in bytes of the `p_key` data |
| 1116 | * \param[in] alg An asymmetric encryption algorithm that is |
| 1117 | * compatible with the type of `key` |
| 1118 | * \param[in] p_input The message to encrypt |
| 1119 | * \param[in] input_length Size of the `p_input` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1120 | * \param[in] p_salt A salt or label, if supported by the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1121 | * encryption algorithm |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1122 | * If the algorithm does not support a |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1123 | * salt, pass `NULL` |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1124 | * If the algorithm supports an optional |
| 1125 | * salt and you do not want to pass a salt, |
| 1126 | * pass `NULL`. |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1127 | * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 1128 | * supported. |
| 1129 | * \param[in] salt_length Size of the `p_salt` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1130 | * If `p_salt` is `NULL`, pass 0. |
| 1131 | * \param[out] p_output Buffer where the encrypted message is to |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1132 | * be written |
| 1133 | * \param[in] output_size Size of the `p_output` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1134 | * \param[out] p_output_length On success, the number of bytes |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1135 | * that make up the returned output |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1136 | * |
| 1137 | * \retval PSA_SUCCESS |
| 1138 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1139 | typedef psa_status_t (*pcd_asymmetric_transparent_encrypt_t)(const uint8_t *p_key, |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1140 | size_t key_size, |
| 1141 | psa_algorithm_t alg, |
| 1142 | const uint8_t *p_input, |
| 1143 | size_t input_length, |
| 1144 | const uint8_t *p_salt, |
| 1145 | size_t salt_length, |
| 1146 | uint8_t *p_output, |
| 1147 | size_t output_size, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1148 | size_t *p_output_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1149 | |
| 1150 | /** |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1151 | * \brief Decrypt a short message with a transparent asymmetric private key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1152 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1153 | * Functions that implement the prototype should be named in the following |
| 1154 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1155 | * ~~~~~~~~~~~~~{.c} |
| 1156 | * pcd_asymmetric_<ALGO>_decrypt |
| 1157 | * ~~~~~~~~~~~~~ |
| 1158 | * Where `ALGO` is the name of the encryption algorithm |
| 1159 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1160 | * \param[in] p_key A buffer containing the private key material |
| 1161 | * \param[in] key_size The size in bytes of the `p_key` data |
| 1162 | * \param[in] alg An asymmetric encryption algorithm that is |
| 1163 | * compatible with the type of `key` |
| 1164 | * \param[in] p_input The message to decrypt |
| 1165 | * \param[in] input_length Size of the `p_input` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1166 | * \param[in] p_salt A salt or label, if supported by the |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1167 | * encryption algorithm |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1168 | * If the algorithm does not support a |
| 1169 | * salt, pass `NULL`. |
| 1170 | * If the algorithm supports an optional |
| 1171 | * salt and you do not want to pass a salt, |
| 1172 | * pass `NULL`. |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1173 | * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 1174 | * supported |
| 1175 | * \param[in] salt_length Size of the `p_salt` buffer in bytes |
| 1176 | * If `p_salt` is `NULL`, pass 0 |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1177 | * \param[out] p_output Buffer where the decrypted message is to |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1178 | * be written |
| 1179 | * \param[in] output_size Size of the `p_output` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1180 | * \param[out] p_output_length On success, the number of bytes |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1181 | * that make up the returned output |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1182 | * |
| 1183 | * \retval PSA_SUCCESS |
| 1184 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1185 | typedef psa_status_t (*pcd_asymmetric_transparent_decrypt_t)(const uint8_t *p_key, |
| 1186 | size_t key_size, |
| 1187 | psa_algorithm_t alg, |
| 1188 | const uint8_t *p_input, |
| 1189 | size_t input_length, |
| 1190 | const uint8_t *p_salt, |
| 1191 | size_t salt_length, |
| 1192 | uint8_t *p_output, |
| 1193 | size_t output_size, |
| 1194 | size_t *p_output_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1195 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1196 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1197 | |
| 1198 | /** \defgroup aead_opaque AEAD Opaque |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1199 | * Authenticated Encryption with Additional Data (AEAD) operations with opaque |
| 1200 | * keys must be done in one function call. While this creates a burden for |
| 1201 | * implementers as there must be sufficient space in memory for the entire |
| 1202 | * message, it prevents decrypted data from being made available before the |
| 1203 | * authentication operation is complete and the data is known to be authentic. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1204 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1205 | /**@{*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1206 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1207 | /** \brief Process an authenticated encryption operation using an opaque key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1208 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1209 | * \param[in] key_slot Slot containing the key to use. |
| 1210 | * \param[in] algorithm The AEAD algorithm to compute |
| 1211 | * (\c PSA_ALG_XXX value such that |
| 1212 | * #PSA_ALG_IS_AEAD(`alg`) is true) |
| 1213 | * \param[in] p_nonce Nonce or IV to use |
| 1214 | * \param[in] nonce_length Size of the `p_nonce` buffer in bytes |
| 1215 | * \param[in] p_additional_data Additional data that will be |
| 1216 | * authenticated but not encrypted |
| 1217 | * \param[in] additional_data_length Size of `p_additional_data` in bytes |
| 1218 | * \param[in] p_plaintext Data that will be authenticated and |
| 1219 | * encrypted |
| 1220 | * \param[in] plaintext_length Size of `p_plaintext` in bytes |
| 1221 | * \param[out] p_ciphertext Output buffer for the authenticated and |
| 1222 | * encrypted data. The additional data is |
| 1223 | * not part of this output. For algorithms |
| 1224 | * where the encrypted data and the |
| 1225 | * authentication tag are defined as |
| 1226 | * separate outputs, the authentication |
| 1227 | * tag is appended to the encrypted data. |
| 1228 | * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in |
| 1229 | * bytes |
| 1230 | * \param[out] p_ciphertext_length On success, the size of the output in |
| 1231 | * the `p_ciphertext` buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1232 | * |
| 1233 | * \retval #PSA_SUCCESS |
| 1234 | * Success. |
| 1235 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1236 | typedef psa_status_t (*psa_aead_opaque_encrypt_t)(psa_key_slot_t key_slot, |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1237 | psa_algorithm_t algorithm, |
| 1238 | const uint8_t *p_nonce, |
| 1239 | size_t nonce_length, |
| 1240 | const uint8_t *p_additional_data, |
| 1241 | size_t additional_data_length, |
| 1242 | const uint8_t *p_plaintext, |
| 1243 | size_t plaintext_length, |
| 1244 | uint8_t *p_ciphertext, |
| 1245 | size_t ciphertext_size, |
| 1246 | size_t *p_ciphertext_length); |
| 1247 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1248 | /** Process an authenticated decryption operation using an opaque key |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1249 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1250 | * \param[in] key_slot Slot containing the key to use |
| 1251 | * \param[in] algorithm The AEAD algorithm to compute |
| 1252 | * (\c PSA_ALG_XXX value such that |
| 1253 | * #PSA_ALG_IS_AEAD(`alg`) is true) |
| 1254 | * \param[in] p_nonce Nonce or IV to use |
| 1255 | * \param[in] nonce_length Size of the `p_nonce` buffer in bytes |
| 1256 | * \param[in] p_additional_data Additional data that has been |
| 1257 | * authenticated but not encrypted |
| 1258 | * \param[in] additional_data_length Size of `p_additional_data` in bytes |
| 1259 | * \param[in] p_ciphertext Data that has been authenticated and |
| 1260 | * encrypted. |
| 1261 | * For algorithms where the encrypted data |
| 1262 | * and the authentication tag are defined |
| 1263 | * as separate inputs, the buffer must |
| 1264 | * contain the encrypted data followed by |
| 1265 | * the authentication tag. |
| 1266 | * \param[in] ciphertext_length Size of `p_ciphertext` in bytes |
| 1267 | * \param[out] p_plaintext Output buffer for the decrypted data |
| 1268 | * \param[in] plaintext_size Size of the `p_plaintext` buffer in |
| 1269 | * bytes |
| 1270 | * \param[out] p_plaintext_length On success, the size of the output in |
| 1271 | * the `p_plaintext` buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1272 | * |
| 1273 | * \retval #PSA_SUCCESS |
| 1274 | * Success. |
| 1275 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1276 | typedef psa_status_t (*psa_aead_opaque_decrypt_t)(psa_key_slot_t key_slot, |
| 1277 | psa_algorithm_t algorithm, |
| 1278 | const uint8_t *p_nonce, |
| 1279 | size_t nonce_length, |
| 1280 | const uint8_t *p_additional_data, |
| 1281 | size_t additional_data_length, |
| 1282 | const uint8_t *p_ciphertext, |
| 1283 | size_t ciphertext_length, |
| 1284 | uint8_t *p_plaintext, |
| 1285 | size_t plaintext_size, |
| 1286 | size_t *p_plaintext_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1287 | |
| 1288 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1289 | * \brief A struct containing all of the function pointers needed to implement |
| 1290 | * Authenticated Encryption with Additional Data operations using opaque keys |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1291 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1292 | * PSA Crypto API implementations should populate instances of the table as |
| 1293 | * appropriate upon startup. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1294 | * |
| 1295 | * If one of the functions is not implemented, it should be set to NULL. |
| 1296 | */ |
| 1297 | struct psa_aead_opaque_t { |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1298 | /** Function that performs the AEAD encrypt operation */ |
| 1299 | psa_aead_opaque_encrypt_t *p_encrypt; |
| 1300 | /** Function that performs the AEAD decrypt operation */ |
| 1301 | psa_aead_opaque_decrypt_t *p_decrypt; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1302 | }; |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1303 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1304 | |
| 1305 | /** \defgroup aead_transparent AEAD Transparent |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1306 | * |
| 1307 | * Authenticated Encryption with Additional Data (AEAD) operations with |
| 1308 | * transparent keys must be done in one function call. While this creates a |
| 1309 | * burden for implementers as there must be sufficient space in memory for the |
| 1310 | * entire message, it prevents decrypted data from being made available before |
| 1311 | * the authentication operation is complete and the data is known to be |
| 1312 | * authentic. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1313 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1314 | /**@{*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1315 | |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1316 | /** Process an authenticated encryption operation using an opaque key. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1317 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1318 | * Functions that implement the prototype should be named in the following |
| 1319 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1320 | * ~~~~~~~~~~~~~{.c} |
| 1321 | * pcd_aead_<ALGO>_encrypt |
| 1322 | * ~~~~~~~~~~~~~ |
| 1323 | * Where `ALGO` is the name of the AEAD algorithm |
| 1324 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1325 | * \param[in] p_key A pointer to the key material |
| 1326 | * \param[in] key_length The size in bytes of the key material |
| 1327 | * \param[in] alg The AEAD algorithm to compute |
| 1328 | * (\c PSA_ALG_XXX value such that |
| 1329 | * #PSA_ALG_IS_AEAD(`alg`) is true) |
| 1330 | * \param[in] nonce Nonce or IV to use |
| 1331 | * \param[in] nonce_length Size of the `nonce` buffer in bytes |
| 1332 | * \param[in] additional_data Additional data that will be MACed |
| 1333 | * but not encrypted. |
| 1334 | * \param[in] additional_data_length Size of `additional_data` in bytes |
| 1335 | * \param[in] plaintext Data that will be MACed and |
| 1336 | * encrypted. |
| 1337 | * \param[in] plaintext_length Size of `plaintext` in bytes |
| 1338 | * \param[out] ciphertext Output buffer for the authenticated and |
| 1339 | * encrypted data. The additional data is |
| 1340 | * not part of this output. For algorithms |
| 1341 | * where the encrypted data and the |
| 1342 | * authentication tag are defined as |
| 1343 | * separate outputs, the authentication |
| 1344 | * tag is appended to the encrypted data. |
| 1345 | * \param[in] ciphertext_size Size of the `ciphertext` buffer in |
| 1346 | * bytes |
| 1347 | * This must be at least |
| 1348 | * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(`alg`, |
| 1349 | * `plaintext_length`). |
| 1350 | * \param[out] ciphertext_length On success, the size of the output in |
| 1351 | * the `ciphertext` buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1352 | * |
| 1353 | * \retval #PSA_SUCCESS |
| 1354 | |
| 1355 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1356 | typedef psa_status_t (*psa_aead_transparent_encrypt_t)(const uint8_t *p_key, |
| 1357 | size_t key_length, |
| 1358 | psa_algorithm_t alg, |
| 1359 | const uint8_t *nonce, |
| 1360 | size_t nonce_length, |
| 1361 | const uint8_t *additional_data, |
| 1362 | size_t additional_data_length, |
| 1363 | const uint8_t *plaintext, |
| 1364 | size_t plaintext_length, |
| 1365 | uint8_t *ciphertext, |
| 1366 | size_t ciphertext_size, |
| 1367 | size_t *ciphertext_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1368 | |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1369 | /** Process an authenticated decryption operation using an opaque key. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1370 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1371 | * Functions that implement the prototype should be named in the following |
| 1372 | * convention: |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1373 | * ~~~~~~~~~~~~~{.c} |
| 1374 | * pcd_aead_<ALGO>_decrypt |
| 1375 | * ~~~~~~~~~~~~~ |
| 1376 | * Where `ALGO` is the name of the AEAD algorithm |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1377 | * \param[in] p_key A pointer to the key material |
| 1378 | * \param[in] key_length The size in bytes of the key material |
| 1379 | * \param[in] alg The AEAD algorithm to compute |
| 1380 | * (\c PSA_ALG_XXX value such that |
| 1381 | * #PSA_ALG_IS_AEAD(`alg`) is true) |
| 1382 | * \param[in] nonce Nonce or IV to use |
| 1383 | * \param[in] nonce_length Size of the `nonce` buffer in bytes |
| 1384 | * \param[in] additional_data Additional data that has been MACed |
| 1385 | * but not encrypted |
| 1386 | * \param[in] additional_data_length Size of `additional_data` in bytes |
| 1387 | * \param[in] ciphertext Data that has been MACed and |
| 1388 | * encrypted |
| 1389 | * For algorithms where the encrypted data |
| 1390 | * and the authentication tag are defined |
| 1391 | * as separate inputs, the buffer must |
| 1392 | * contain the encrypted data followed by |
| 1393 | * the authentication tag. |
| 1394 | * \param[in] ciphertext_length Size of `ciphertext` in bytes |
| 1395 | * \param[out] plaintext Output buffer for the decrypted data |
| 1396 | * \param[in] plaintext_size Size of the `plaintext` buffer in |
| 1397 | * bytes |
| 1398 | * This must be at least |
| 1399 | * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(`alg`, |
| 1400 | * `ciphertext_length`). |
| 1401 | * \param[out] plaintext_length On success, the size of the output |
| 1402 | * in the \b plaintext buffer |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1403 | * |
| 1404 | * \retval #PSA_SUCCESS |
| 1405 | * Success. |
| 1406 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1407 | typedef psa_status_t (*psa_aead_transparent_decrypt_t)(const uint8_t *p_key, |
| 1408 | size_t key_length, |
| 1409 | psa_algorithm_t alg, |
| 1410 | const uint8_t *nonce, |
| 1411 | size_t nonce_length, |
| 1412 | const uint8_t *additional_data, |
| 1413 | size_t additional_data_length, |
| 1414 | const uint8_t *ciphertext, |
| 1415 | size_t ciphertext_length, |
| 1416 | uint8_t *plaintext, |
| 1417 | size_t plaintext_size, |
| 1418 | size_t *plaintext_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1419 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1420 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1421 | |
| 1422 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1423 | /** \defgroup driver_rng Entropy Generation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1424 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1425 | /**@{*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1426 | |
| 1427 | /** \brief A hardware-specific structure for a entropy providing hardware |
| 1428 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 1429 | typedef struct pcd_entropy_context_s pcd_entropy_context_t; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1430 | |
| 1431 | /** \brief Initialize an entropy driver |
| 1432 | * |
| 1433 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1434 | * \param[in,out] p_context A hardware-specific structure |
| 1435 | * containing any context information for |
| 1436 | * the implementation |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1437 | * |
| 1438 | * \retval PSA_SUCCESS |
| 1439 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 1440 | typedef psa_status_t (*pcd_entropy_init_t)(pcd_entropy_context_t *p_context); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1441 | |
| 1442 | /** \brief Get a specified number of bytes from the entropy source |
| 1443 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1444 | * It retrives `buffer_size` bytes of data from the entropy source. The entropy |
| 1445 | * source will always fill the provided buffer to its full size, however, most |
| 1446 | * entropy sources have biases, and the actual amount of entropy contained in |
| 1447 | * the buffer will be less than the number of bytes. |
| 1448 | * The driver will return the actual number of bytes of entropy placed in the |
| 1449 | * buffer in `p_received_entropy_bytes`. |
| 1450 | * A PSA Crypto API implementation will likely feed the output of this function |
| 1451 | * into a Digital Random Bit Generator (DRBG), and typically has a minimum |
| 1452 | * amount of entropy that it needs. |
| 1453 | * To accomplish this, the PSA Crypto implementation should be designed to call |
| 1454 | * this function multiple times until it has received the required amount of |
| 1455 | * entropy from the entropy source. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1456 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1457 | * \param[in,out] p_context A hardware-specific structure |
| 1458 | * containing any context information |
| 1459 | * for the implementation |
| 1460 | * \param[out] p_buffer A caller-allocated buffer for the |
| 1461 | * retrieved bytes to be placed in |
| 1462 | * \param[in] buffer_size The allocated size of `p_buffer` |
| 1463 | * \param[out] p_received_entropy_bytes The amount of entropy (in bytes) |
| 1464 | * actually provided in `p_buffer` |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1465 | * |
| 1466 | * \retval PSA_SUCCESS |
| 1467 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 1468 | typedef psa_status_t (*pcd_entropy_get_bytes_t)(pcd_entropy_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1469 | uint8_t *p_buffer, |
| 1470 | uint32_t buffer_size, |
| 1471 | uint32_t *p_received_entropy_bytes); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1472 | |
| 1473 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1474 | * \brief A struct containing all of the function pointers needed to interface |
| 1475 | * to an entropy source |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1476 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1477 | * PSA Crypto API implementations should populate instances of the table as |
| 1478 | * appropriate upon startup. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1479 | * |
| 1480 | * If one of the functions is not implemented, it should be set to NULL. |
| 1481 | */ |
| 1482 | struct pcd_entropy_t { |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1483 | /** Function that performs initialization for the entropy source */ |
| 1484 | pcd_entropy_init_t *p_init; |
| 1485 | /** Function that performs the get_bytes operation for the entropy source |
| 1486 | */ |
| 1487 | pcd_entropy_get_bytes_t *p_get_bytes; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1488 | }; |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1489 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1490 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1491 | /** \defgroup driver_key_management Key Management |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1492 | * Currently, key management is limited to importing keys in the clear, |
| 1493 | * destroying keys, and exporting keys in the clear. |
| 1494 | * Whether a key may be exported is determined by the key policies in place |
| 1495 | * on the key slot. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1496 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1497 | /**@{*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1498 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1499 | /** \brief Import a key in binary format |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1500 | * |
| 1501 | * This function can support any output from psa_export_key(). Refer to the |
| 1502 | * documentation of psa_export_key() for the format for each key type. |
| 1503 | * |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 1504 | * \param[in] key_slot Slot where the key will be stored |
| 1505 | * This must be a valid slot for a key of the chosen |
| 1506 | * type. It must be unoccupied. |
| 1507 | * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value) |
| 1508 | * \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value) |
| 1509 | * \param[in] usage The allowed uses of the key |
| 1510 | * \param[in] p_data Buffer containing the key data |
| 1511 | * \param[in] data_length Size of the `data` buffer in bytes |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1512 | * |
| 1513 | * \retval #PSA_SUCCESS |
| 1514 | * Success. |
| 1515 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1516 | typedef psa_status_t (*pcd_opaque_import_key_t)(psa_key_slot_t key_slot, |
| 1517 | psa_key_type_t type, |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 1518 | psa_algorithm_t algorithm, |
| 1519 | psa_key_usage_t usage, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1520 | const uint8_t *p_data, |
| 1521 | size_t data_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1522 | |
| 1523 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1524 | * \brief Destroy a key and restore the slot to its default state |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1525 | * |
| 1526 | * This function destroys the content of the key slot from both volatile |
| 1527 | * memory and, if applicable, non-volatile storage. Implementations shall |
| 1528 | * make a best effort to ensure that any previous content of the slot is |
| 1529 | * unrecoverable. |
| 1530 | * |
| 1531 | * This function also erases any metadata such as policies. It returns the |
| 1532 | * specified slot to its default state. |
| 1533 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1534 | * \param[in] key_slot The key slot to erase. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1535 | * |
| 1536 | * \retval #PSA_SUCCESS |
| 1537 | * The slot's content, if any, has been erased. |
| 1538 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1539 | typedef psa_status_t (*pcd_destroy_key_t)(psa_key_slot_t key); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1540 | |
| 1541 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1542 | * \brief Export a key in binary format |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1543 | * |
| 1544 | * The output of this function can be passed to psa_import_key() to |
| 1545 | * create an equivalent object. |
| 1546 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1547 | * If a key is created with `psa_import_key()` and then exported with |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1548 | * this function, it is not guaranteed that the resulting data is |
| 1549 | * identical: the implementation may choose a different representation |
| 1550 | * of the same key if the format permits it. |
| 1551 | * |
| 1552 | * For standard key types, the output format is as follows: |
| 1553 | * |
| 1554 | * - For symmetric keys (including MAC keys), the format is the |
| 1555 | * raw bytes of the key. |
| 1556 | * - For DES, the key data consists of 8 bytes. The parity bits must be |
| 1557 | * correct. |
| 1558 | * - For Triple-DES, the format is the concatenation of the |
| 1559 | * two or three DES keys. |
| 1560 | * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format |
| 1561 | * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017) |
| 1562 | * as RSAPrivateKey. |
| 1563 | * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format |
| 1564 | * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo. |
| 1565 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1566 | * \param[in] key Slot whose content is to be exported. This must |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1567 | * be an occupied key slot. |
| 1568 | * \param[out] p_data Buffer where the key data is to be written. |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1569 | * \param[in] data_size Size of the `p_data` buffer in bytes. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1570 | * \param[out] p_data_length On success, the number of bytes |
| 1571 | * that make up the key data. |
| 1572 | * |
| 1573 | * \retval #PSA_SUCCESS |
| 1574 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1575 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1576 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1577 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1578 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1579 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1580 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1581 | typedef psa_status_t (*pcd_export_key_t)(psa_key_slot_t key, |
| 1582 | uint8_t *p_data, |
| 1583 | size_t data_size, |
| 1584 | size_t *p_data_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1585 | |
| 1586 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1587 | * \brief Export a public key or the public part of a key pair in binary format |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1588 | * |
| 1589 | * The output of this function can be passed to psa_import_key() to |
| 1590 | * create an object that is equivalent to the public key. |
| 1591 | * |
| 1592 | * For standard key types, the output format is as follows: |
| 1593 | * |
| 1594 | * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY), |
| 1595 | * the format is the DER representation of the public key defined by RFC 5280 |
| 1596 | * as SubjectPublicKeyInfo. |
| 1597 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1598 | * \param[in] key_slot Slot whose content is to be exported. This must |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1599 | * be an occupied key slot. |
| 1600 | * \param[out] p_data Buffer where the key data is to be written. |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1601 | * \param[in] data_size Size of the `data` buffer in bytes. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1602 | * \param[out] p_data_length On success, the number of bytes |
| 1603 | * that make up the key data. |
| 1604 | * |
| 1605 | * \retval #PSA_SUCCESS |
| 1606 | */ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1607 | typedef psa_status_t (*pcd_export_public_key_t)(psa_key_slot_t key, |
| 1608 | uint8_t *p_data, |
| 1609 | size_t data_size, |
| 1610 | size_t *p_data_length); |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1611 | |
| 1612 | /** |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1613 | * \brief A struct containing all of the function pointers needed to for key |
| 1614 | * management using opaque keys |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1615 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1616 | * PSA Crypto API implementations should populate instances of the table as |
| 1617 | * appropriate upon startup. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1618 | * |
| 1619 | * If one of the functions is not implemented, it should be set to NULL. |
| 1620 | */ |
| 1621 | struct pcd_key_management_t { |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1622 | /** Function that performs the key import operation */ |
| 1623 | pcd_opaque_import_key_t *p_import; |
| 1624 | /** Function that performs the key destroy operation */ |
| 1625 | pcd_destroy_key_t *p_destroy; |
| 1626 | /** Function that performs the key export operation */ |
| 1627 | pcd_export_key_t *p_export; |
| 1628 | /** Function that perforsm the public key export operation */ |
| 1629 | pcd_export_public_key_t *p_export_public; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1630 | }; |
| 1631 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1632 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1633 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1634 | /** \defgroup driver_derivation Key Derivation and Agreement |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1635 | * Key derivation is the process of generating new key material using an |
| 1636 | * existing key and additional parameters, iterating through a basic |
| 1637 | * cryptographic function, such as a hash. |
| 1638 | * Key agreement is a part of cryptographic protocols that allows two parties |
| 1639 | * to agree on the same key value, but starting from different original key |
| 1640 | * material. |
| 1641 | * The flows are similar, and the PSA Crypto Driver API uses the same functions |
| 1642 | * for both of the flows. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1643 | * |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1644 | * There are two different final functions for the flows, |
| 1645 | * `pcd_key_derivation_derive` and `pcd_key_derivation_export`. |
| 1646 | * `pcd_key_derivation_derive` is used when the key material should be placed |
| 1647 | * in a slot on the hardware and not exposed to the caller. |
| 1648 | * `pcd_key_derivation_export` is used when the key material should be returned |
| 1649 | * to the PSA Cryptographic API implementation. |
| 1650 | * |
| 1651 | * Different key derivation algorithms require a different number of inputs. |
| 1652 | * Instead of having an API that takes as input variable length arrays, which |
| 1653 | * can be problemmatic to manage on embedded platforms, the inputs are passed |
| 1654 | * to the driver via a function, `pcd_key_derivation_collateral`, that is |
| 1655 | * called multiple times with different `collateral_id`s. Thus, for a key |
| 1656 | * derivation algorithm that required 3 paramter inputs, the flow would look |
| 1657 | * something like: |
| 1658 | * ~~~~~~~~~~~~~{.c} |
| 1659 | * pcd_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes); |
| 1660 | * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_0, |
| 1661 | * p_collateral_0, |
| 1662 | * collateral_0_size); |
| 1663 | * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_1, |
| 1664 | * p_collateral_1, |
| 1665 | * collateral_1_size); |
| 1666 | * pcd_key_derivation_collateral(kdf_algorithm_collateral_id_2, |
| 1667 | * p_collateral_2, |
| 1668 | * collateral_2_size); |
| 1669 | * pcd_key_derivation_derive(); |
| 1670 | * ~~~~~~~~~~~~~ |
| 1671 | * |
| 1672 | * key agreement example: |
| 1673 | * ~~~~~~~~~~~~~{.c} |
| 1674 | * pcd_key_derivation_setup(alg, source_key. dest_key_size_bytes); |
| 1675 | * pcd_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size); |
| 1676 | * pcd_key_derivation_export(p_session_key, |
| 1677 | * session_key_size, |
| 1678 | * &session_key_length); |
| 1679 | * ~~~~~~~~~~~~~ |
| 1680 | */ |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1681 | /**@{*/ |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1682 | |
Derek Miller | 765682c | 2018-10-22 15:27:27 -0500 | [diff] [blame] | 1683 | /** \brief The hardware-specific key derivation context structure |
| 1684 | * |
| 1685 | * The contents of this structure are implementation dependent and are |
| 1686 | * therefore not described here |
| 1687 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 1688 | typedef struct pcd_key_derivation_context_s pcd_key_derivation_context_t; |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1689 | |
| 1690 | /** \brief Set up a key derivation operation by specifying the algorithm and |
| 1691 | * the source key sot |
| 1692 | * |
| 1693 | * \param[in,out] p_context A hardware-specific structure containing any |
| 1694 | * context information for the implementation |
| 1695 | * \param[in] kdf_alg The algorithm to be used for the key derivation |
| 1696 | * \param[in] souce_key The key to be used as the source material for the |
| 1697 | * key derivation |
| 1698 | * |
| 1699 | * \retval PSA_SUCCESS |
| 1700 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 1701 | typedef psa_status_t (*pcd_key_derivation_setup_t)(pcd_key_derivation_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1702 | psa_algorithm_t kdf_alg, |
| 1703 | psa_key_slot_t source_key); |
| 1704 | |
| 1705 | /** \brief Provide collateral (parameters) needed for a key derivation or key |
| 1706 | * agreement operation |
| 1707 | * |
| 1708 | * Since many key derivation algorithms require multiple parameters, it is |
| 1709 | * expeced that this function may be called multiple times for the same |
| 1710 | * operation, each with a different algorithm-specific `collateral_id` |
| 1711 | * |
| 1712 | * \param[in,out] p_context A hardware-specific structure containing any |
| 1713 | * context information for the implementation |
| 1714 | * \param[in] collateral_id An ID for the collateral being provided |
| 1715 | * \param[in] p_collateral A buffer containing the collateral data |
| 1716 | * \param[in] collateral_size The size in bytes of the collateral |
| 1717 | * |
| 1718 | * \retval PSA_SUCCESS |
| 1719 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 1720 | typedef psa_status_t (*pcd_key_derivation_collateral_t)(pcd_key_derivation_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1721 | uint32_t collateral_id, |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 1722 | const uint8_t *p_collateral, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1723 | size_t collateral_size); |
| 1724 | |
| 1725 | /** \brief Perform the final key derivation step and place the generated key |
| 1726 | * material in a slot |
| 1727 | * \param[in,out] p_context A hardware-specific structure containing any |
| 1728 | * context information for the implementation |
| 1729 | * \param[in] dest_key The slot where the generated key material |
| 1730 | * should be placed |
| 1731 | * |
| 1732 | * \retval PSA_SUCCESS |
| 1733 | */ |
Derek Miller | 81133a6 | 2018-10-23 14:55:32 -0500 | [diff] [blame^] | 1734 | typedef psa_status_t (*pcd_key_derivation_derive_t)(pcd_key_derivation_context_t *p_context, |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1735 | psa_key_slot_t dest_key); |
| 1736 | |
| 1737 | /** \brief Perform the final step of a key agreement and place the generated |
| 1738 | * key material in a buffer |
| 1739 | * |
| 1740 | * \param[out] p_output Buffer in which to place the generated key |
| 1741 | * material |
| 1742 | * \param[in] output_size The size in bytes of `p_output` |
| 1743 | * \param[out] p_output_length Upon success, contains the number of bytes of |
| 1744 | * key material placed in `p_output` |
| 1745 | * |
| 1746 | * \retval PSA_SUCCESS |
| 1747 | */ |
| 1748 | typedef psa_status_t (*pcd_key_derivation_export_t)(uint8_t *p_output, |
| 1749 | size_t output_size, |
| 1750 | size_t *p_output_length); |
| 1751 | |
| 1752 | /** |
| 1753 | * \brief A struct containing all of the function pointers needed to for key |
| 1754 | * derivation and agreement |
| 1755 | * |
| 1756 | * PSA Crypto API implementations should populate instances of the table as |
| 1757 | * appropriate upon startup. |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1758 | * |
| 1759 | * If one of the functions is not implemented, it should be set to NULL. |
| 1760 | */ |
| 1761 | struct pcd_key_derivation_t { |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1762 | /** Function that performs the key derivation setup */ |
| 1763 | pcd_key_derivation_setup_t *p_setup; |
| 1764 | /** Function that sets the key derivation collateral */ |
| 1765 | pcd_key_derivation_collateral_t *p_collateral; |
| 1766 | /** Function that performs the final key derivation step */ |
| 1767 | pcd_key_derivation_derive_t *p_derive; |
| 1768 | /** Function that perforsm the final key derivation or agreement and |
| 1769 | * exports the key */ |
| 1770 | pcd_key_derivation_export_t *p_export; |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1771 | }; |
| 1772 | |
Derek Miller | 16e7229 | 2018-10-15 16:14:24 -0500 | [diff] [blame] | 1773 | /**@}*/ |
Derek Miller | 5b3417a | 2018-10-10 17:55:03 -0500 | [diff] [blame] | 1774 | |
| 1775 | #endif // __PSA_CRYPTO_DRIVER_H__ |