Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file psa/crypto.h |
| 3 | * \brief Platform Security Architecture cryptography module |
| 4 | */ |
Jaeden Amero | cab5494 | 2018-07-25 13:26:13 +0100 | [diff] [blame] | 5 | /* |
| 6 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 21 | |
| 22 | #ifndef PSA_CRYPTO_H |
| 23 | #define PSA_CRYPTO_H |
| 24 | |
| 25 | #include "crypto_platform.h" |
| 26 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 27 | #include <stddef.h> |
| 28 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 29 | #ifdef __DOXYGEN_ONLY__ |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 30 | /* This __DOXYGEN_ONLY__ block contains mock definitions for things that |
| 31 | * must be defined in the crypto_platform.h header. These mock definitions |
| 32 | * are present in this file as a convenience to generate pretty-printed |
| 33 | * documentation that includes those definitions. */ |
| 34 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 35 | /** \defgroup platform Implementation-specific definitions |
| 36 | * @{ |
| 37 | */ |
| 38 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 39 | /** \brief Key slot number. |
| 40 | * |
| 41 | * This type represents key slots. It must be an unsigned integral |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 42 | * type. The choice of type is implementation-dependent. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 43 | * 0 is not a valid key slot number. The meaning of other values is |
| 44 | * implementation dependent. |
| 45 | * |
| 46 | * At any given point in time, each key slot either contains a |
| 47 | * cryptographic object, or is empty. Key slots are persistent: |
| 48 | * once set, the cryptographic object remains in the key slot until |
| 49 | * explicitly destroyed. |
| 50 | */ |
| 51 | typedef _unsigned_integral_type_ psa_key_slot_t; |
| 52 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 53 | /**@}*/ |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 54 | #endif /* __DOXYGEN_ONLY__ */ |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 55 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 56 | #ifdef __cplusplus |
| 57 | extern "C" { |
| 58 | #endif |
| 59 | |
| 60 | /** \defgroup basic Basic definitions |
| 61 | * @{ |
| 62 | */ |
| 63 | |
Gilles Peskine | e9a0a9d | 2018-06-20 13:59:04 +0200 | [diff] [blame] | 64 | #if defined(PSA_SUCCESS) |
| 65 | /* If PSA_SUCCESS is defined, assume that PSA crypto is being used |
| 66 | * together with PSA IPC, which also defines the identifier |
| 67 | * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case; |
| 68 | * the other error code names don't clash. Also define psa_status_t as |
| 69 | * an alias for the type used by PSA IPC. This is a temporary hack |
mohammad1603 | 13f4394 | 2018-08-05 12:09:44 +0300 | [diff] [blame] | 70 | * until we unify error reporting in PSA IPC and PSA crypto. |
Gilles Peskine | e9a0a9d | 2018-06-20 13:59:04 +0200 | [diff] [blame] | 71 | * |
| 72 | * Note that psa_defs.h must be included before this header! |
| 73 | */ |
| 74 | typedef psa_error_t psa_status_t; |
| 75 | |
| 76 | #else /* defined(PSA_SUCCESS) */ |
| 77 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 78 | /** |
| 79 | * \brief Function return status. |
| 80 | * |
Gilles Peskine | e9a0a9d | 2018-06-20 13:59:04 +0200 | [diff] [blame] | 81 | * This is either #PSA_SUCCESS (which is zero), indicating success, |
| 82 | * or a nonzero value indicating that an error occurred. Errors are |
| 83 | * encoded as one of the \c PSA_ERROR_xxx values defined here. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 84 | */ |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 85 | typedef int32_t psa_status_t; |
Gilles Peskine | e9a0a9d | 2018-06-20 13:59:04 +0200 | [diff] [blame] | 86 | |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 87 | /** The action was completed successfully. */ |
| 88 | #define PSA_SUCCESS ((psa_status_t)0) |
Gilles Peskine | e9a0a9d | 2018-06-20 13:59:04 +0200 | [diff] [blame] | 89 | |
| 90 | #endif /* !defined(PSA_SUCCESS) */ |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 91 | |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 92 | /** An error occurred that does not correspond to any defined |
| 93 | * failure cause. |
| 94 | * |
| 95 | * Implementations may use this error code if none of the other standard |
| 96 | * error codes are applicable. */ |
| 97 | #define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1) |
| 98 | |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 99 | /** The requested operation or a parameter is not supported |
| 100 | * by this implementation. |
| 101 | * |
| 102 | * Implementations should return this error code when an enumeration |
| 103 | * parameter such as a key type, algorithm, etc. is not recognized. |
| 104 | * If a combination of parameters is recognized and identified as |
| 105 | * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 106 | #define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 107 | |
| 108 | /** The requested action is denied by a policy. |
| 109 | * |
| 110 | * Implementations should return this error code when the parameters |
| 111 | * are recognized as valid and supported, and a policy explicitly |
| 112 | * denies the requested operation. |
| 113 | * |
| 114 | * If a subset of the parameters of a function call identify a |
| 115 | * forbidden operation, and another subset of the parameters are |
| 116 | * not valid or not supported, it is unspecified whether the function |
| 117 | * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or |
| 118 | * #PSA_ERROR_INVALID_ARGUMENT. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 119 | #define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 120 | |
| 121 | /** An output buffer is too small. |
| 122 | * |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 123 | * Applications can call the \c PSA_xxx_SIZE macro listed in the function |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 124 | * description to determine a sufficient buffer size. |
| 125 | * |
| 126 | * Implementations should preferably return this error code only |
| 127 | * in cases when performing the operation with a larger output |
| 128 | * buffer would succeed. However implementations may return this |
| 129 | * error if a function has invalid or unsupported parameters in addition |
| 130 | * to the parameters that determine the necessary output buffer size. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 131 | #define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 132 | |
| 133 | /** A slot is occupied, but must be empty to carry out the |
| 134 | * requested action. |
| 135 | * |
| 136 | * If the slot number is invalid (i.e. the requested action could |
| 137 | * not be performed even after erasing the slot's content), |
| 138 | * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 139 | #define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 140 | |
| 141 | /** A slot is empty, but must be occupied to carry out the |
| 142 | * requested action. |
| 143 | * |
| 144 | * If the slot number is invalid (i.e. the requested action could |
| 145 | * not be performed even after creating appropriate content in the slot), |
| 146 | * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 147 | #define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 148 | |
| 149 | /** The requested action cannot be performed in the current state. |
| 150 | * |
| 151 | * Multipart operations return this error when one of the |
| 152 | * functions is called out of sequence. Refer to the function |
| 153 | * descriptions for permitted sequencing of functions. |
| 154 | * |
| 155 | * Implementations shall not return this error code to indicate |
| 156 | * that a key slot is occupied when it needs to be free or vice versa, |
| 157 | * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT |
| 158 | * as applicable. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 159 | #define PSA_ERROR_BAD_STATE ((psa_status_t)7) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 160 | |
| 161 | /** The parameters passed to the function are invalid. |
| 162 | * |
| 163 | * Implementations may return this error any time a parameter or |
| 164 | * combination of parameters are recognized as invalid. |
| 165 | * |
| 166 | * Implementations shall not return this error code to indicate |
| 167 | * that a key slot is occupied when it needs to be free or vice versa, |
| 168 | * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT |
| 169 | * as applicable. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 170 | #define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 171 | |
| 172 | /** There is not enough runtime memory. |
| 173 | * |
| 174 | * If the action is carried out across multiple security realms, this |
| 175 | * error can refer to available memory in any of the security realms. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 176 | #define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 177 | |
| 178 | /** There is not enough persistent storage. |
| 179 | * |
| 180 | * Functions that modify the key storage return this error code if |
| 181 | * there is insufficient storage space on the host media. In addition, |
| 182 | * many functions that do not otherwise access storage may return this |
| 183 | * error code if the implementation requires a mandatory log entry for |
| 184 | * the requested action and the log storage space is full. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 185 | #define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 186 | |
| 187 | /** There was a communication failure inside the implementation. |
| 188 | * |
| 189 | * This can indicate a communication failure between the application |
| 190 | * and an external cryptoprocessor or between the cryptoprocessor and |
| 191 | * an external volatile or persistent memory. A communication failure |
| 192 | * may be transient or permanent depending on the cause. |
| 193 | * |
| 194 | * \warning If a function returns this error, it is undetermined |
| 195 | * whether the requested action has completed or not. Implementations |
| 196 | * should return #PSA_SUCCESS on successful completion whenver |
| 197 | * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE |
| 198 | * if the requested action was completed successfully in an external |
| 199 | * cryptoprocessor but there was a breakdown of communication before |
| 200 | * the cryptoprocessor could report the status to the application. |
| 201 | */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 202 | #define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 203 | |
| 204 | /** There was a storage failure that may have led to data loss. |
| 205 | * |
| 206 | * This error indicates that some persistent storage is corrupted. |
| 207 | * It should not be used for a corruption of volatile memory |
| 208 | * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error |
| 209 | * between the cryptoprocessor and its external storage (use |
| 210 | * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is |
| 211 | * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE). |
| 212 | * |
| 213 | * Note that a storage failure does not indicate that any data that was |
| 214 | * previously read is invalid. However this previously read data may no |
| 215 | * longer be readable from storage. |
| 216 | * |
| 217 | * When a storage failure occurs, it is no longer possible to ensure |
| 218 | * the global integrity of the keystore. Depending on the global |
| 219 | * integrity guarantees offered by the implementation, access to other |
| 220 | * data may or may not fail even if the data is still readable but |
| 221 | * its integrity canont be guaranteed. |
| 222 | * |
| 223 | * Implementations should only use this error code to report a |
| 224 | * permanent storage corruption. However application writers should |
| 225 | * keep in mind that transient errors while reading the storage may be |
| 226 | * reported using this error code. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 227 | #define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 228 | |
| 229 | /** A hardware failure was detected. |
| 230 | * |
| 231 | * A hardware failure may be transient or permanent depending on the |
| 232 | * cause. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 233 | #define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 234 | |
| 235 | /** A tampering attempt was detected. |
| 236 | * |
| 237 | * If an application receives this error code, there is no guarantee |
| 238 | * that previously accessed or computed data was correct and remains |
| 239 | * confidential. Applications should not perform any security function |
| 240 | * and should enter a safe failure state. |
| 241 | * |
| 242 | * Implementations may return this error code if they detect an invalid |
| 243 | * state that cannot happen during normal operation and that indicates |
| 244 | * that the implementation's security guarantees no longer hold. Depending |
| 245 | * on the implementation architecture and on its security and safety goals, |
| 246 | * the implementation may forcibly terminate the application. |
| 247 | * |
| 248 | * This error code is intended as a last resort when a security breach |
| 249 | * is detected and it is unsure whether the keystore data is still |
| 250 | * protected. Implementations shall only return this error code |
| 251 | * to report an alarm from a tampering detector, to indicate that |
| 252 | * the confidentiality of stored data can no longer be guaranteed, |
| 253 | * or to indicate that the integrity of previously returned data is now |
| 254 | * considered compromised. Implementations shall not use this error code |
| 255 | * to indicate a hardware failure that merely makes it impossible to |
| 256 | * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE, |
| 257 | * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE, |
| 258 | * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code |
| 259 | * instead). |
| 260 | * |
| 261 | * This error indicates an attack against the application. Implementations |
| 262 | * shall not return this error code as a consequence of the behavior of |
| 263 | * the application itself. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 264 | #define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 265 | |
| 266 | /** There is not enough entropy to generate random data needed |
| 267 | * for the requested action. |
| 268 | * |
| 269 | * This error indicates a failure of a hardware random generator. |
| 270 | * Application writers should note that this error can be returned not |
| 271 | * only by functions whose purpose is to generate random data, such |
| 272 | * as key, IV or nonce generation, but also by functions that execute |
| 273 | * an algorithm with a randomized result, as well as functions that |
| 274 | * use randomization of intermediate computations as a countermeasure |
| 275 | * to certain attacks. |
| 276 | * |
| 277 | * Implementations should avoid returning this error after psa_crypto_init() |
| 278 | * has succeeded. Implementations should generate sufficient |
| 279 | * entropy during initialization and subsequently use a cryptographically |
| 280 | * secure pseudorandom generator (PRNG). However implementations may return |
| 281 | * this error at any time if a policy requires the PRNG to be reseeded |
| 282 | * during normal operation. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 283 | #define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 284 | |
| 285 | /** The signature, MAC or hash is incorrect. |
| 286 | * |
| 287 | * Verification functions return this error if the verification |
| 288 | * calculations completed successfully, and the value to be verified |
| 289 | * was determined to be incorrect. |
| 290 | * |
| 291 | * If the value to verify has an invalid size, implementations may return |
| 292 | * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 293 | #define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 294 | |
| 295 | /** The decrypted padding is incorrect. |
| 296 | * |
| 297 | * \warning In some protocols, when decrypting data, it is essential that |
| 298 | * the behavior of the application does not depend on whether the padding |
| 299 | * is correct, down to precise timing. Applications should prefer |
| 300 | * protocols that use authenticated encryption rather than plain |
| 301 | * encryption. If the application must perform a decryption of |
| 302 | * unauthenticated data, the application writer should take care not |
| 303 | * to reveal whether the padding is invalid. |
| 304 | * |
| 305 | * Implementations should strive to make valid and invalid padding |
| 306 | * as close as possible to indistinguishable to an external observer. |
| 307 | * In particular, the timing of a decryption operation should not |
| 308 | * depend on the validity of the padding. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 309 | #define PSA_ERROR_INVALID_PADDING ((psa_status_t)17) |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 310 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 311 | /** The generator has insufficient capacity left. |
| 312 | * |
| 313 | * Once a function returns this error, attempts to read from the |
| 314 | * generator will always return this error. */ |
itayzafrir | f26dbfc | 2018-08-01 16:09:08 +0300 | [diff] [blame] | 315 | #define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * \brief Library initialization. |
| 319 | * |
| 320 | * Applications must call this function before calling any other |
| 321 | * function in this module. |
| 322 | * |
| 323 | * Applications may call this function more than once. Once a call |
| 324 | * succeeds, subsequent calls are guaranteed to succeed. |
| 325 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 326 | * \retval #PSA_SUCCESS |
| 327 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 328 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 329 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 330 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 331 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 332 | */ |
| 333 | psa_status_t psa_crypto_init(void); |
| 334 | |
Gilles Peskine | 2905a7a | 2018-03-07 16:39:31 +0100 | [diff] [blame] | 335 | #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8) |
| 336 | #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 337 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 338 | /**@}*/ |
| 339 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 340 | /** \defgroup crypto_types Key and algorithm types |
| 341 | * @{ |
| 342 | */ |
| 343 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 344 | /** \brief Encoding of a key type. |
| 345 | */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 346 | typedef uint32_t psa_key_type_t; |
| 347 | |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 348 | /** An invalid key type value. |
| 349 | * |
| 350 | * Zero is not the encoding of any key type. |
| 351 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 352 | #define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000) |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 353 | |
| 354 | /** Vendor-defined flag |
| 355 | * |
| 356 | * Key types defined by this standard will never have the |
| 357 | * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types |
| 358 | * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should |
| 359 | * respect the bitwise structure used by standard encodings whenever practical. |
| 360 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 361 | #define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 362 | |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 363 | #define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000) |
| 364 | #define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000) |
| 365 | #define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000) |
| 366 | #define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000) |
| 367 | #define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000) |
| 368 | |
| 369 | #define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 370 | |
Gilles Peskine | e877974 | 2018-08-10 16:10:56 +0200 | [diff] [blame] | 371 | /** Whether a key type is vendor-defined. */ |
| 372 | #define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \ |
| 373 | (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0) |
| 374 | |
| 375 | /** Whether a key type is an unstructured array of bytes. |
| 376 | * |
| 377 | * This encompasses both symmetric keys and non-key data. |
| 378 | */ |
| 379 | #define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \ |
| 380 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \ |
| 381 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) |
| 382 | |
| 383 | /** Whether a key type is asymmetric: either a key pair or a public key. */ |
| 384 | #define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \ |
| 385 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK \ |
| 386 | & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \ |
| 387 | PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY) |
| 388 | /** Whether a key type is the public part of a key pair. */ |
| 389 | #define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \ |
| 390 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY) |
| 391 | /** Whether a key type is a key pair containing a private part and a public |
| 392 | * part. */ |
| 393 | #define PSA_KEY_TYPE_IS_KEYPAIR(type) \ |
| 394 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR) |
| 395 | /** The key pair type corresponding to a public key type. |
| 396 | * |
| 397 | * You may also pass a key pair type as \p type, it will be left unchanged. |
| 398 | * |
| 399 | * \param type A public key type or key pair type. |
| 400 | * |
| 401 | * \return The corresponding key pair type. |
| 402 | * If \p type is not a public key or a key pair, |
| 403 | * the return value is undefined. |
| 404 | */ |
| 405 | #define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \ |
| 406 | ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) |
| 407 | /** The public key type corresponding to a key pair type. |
| 408 | * |
| 409 | * You may also pass a key pair type as \p type, it will be left unchanged. |
| 410 | * |
| 411 | * \param type A public key type or key pair type. |
| 412 | * |
| 413 | * \return The corresponding public key type. |
| 414 | * If \p type is not a public key or a key pair, |
| 415 | * the return value is undefined. |
| 416 | */ |
| 417 | #define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \ |
| 418 | ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) |
| 419 | /** Whether a key type is an RSA key (pair or public-only). */ |
| 420 | #define PSA_KEY_TYPE_IS_RSA(type) \ |
| 421 | (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY) |
| 422 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 423 | /** Raw data. |
| 424 | * |
| 425 | * A "key" of this type cannot be used for any cryptographic operation. |
| 426 | * Applications may use this type to store arbitrary data in the keystore. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 427 | #define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 428 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 429 | /** HMAC key. |
| 430 | * |
| 431 | * The key policy determines which underlying hash algorithm the key can be |
| 432 | * used for. |
| 433 | * |
| 434 | * HMAC keys should generally have the same size as the underlying hash. |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 435 | * This size can be calculated with #PSA_HASH_SIZE(\c alg) where |
| 436 | * \c alg is the HMAC algorithm or the underlying hash algorithm. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 437 | #define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 438 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 439 | /** A secret for key derivation. |
| 440 | * |
| 441 | * The key policy determines which key derivation algorithm the key |
| 442 | * can be used for. |
| 443 | */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 444 | #define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 445 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 446 | /** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher. |
| 447 | * |
| 448 | * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or |
| 449 | * 32 bytes (AES-256). |
| 450 | */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 451 | #define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 452 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 453 | /** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES). |
| 454 | * |
| 455 | * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or |
| 456 | * 24 bytes (3-key 3DES). |
| 457 | * |
| 458 | * Note that single DES and 2-key 3DES are weak and strongly |
| 459 | * deprecated and should only be used to decrypt legacy data. 3-key 3DES |
| 460 | * is weak and deprecated and should only be used in legacy protocols. |
| 461 | */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 462 | #define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 463 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 464 | /** Key for an cipher, AEAD or MAC algorithm based on the |
| 465 | * Camellia block cipher. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 466 | #define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 467 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 468 | /** Key for the RC4 stream cipher. |
| 469 | * |
| 470 | * Note that RC4 is weak and deprecated and should only be used in |
| 471 | * legacy protocols. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 472 | #define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 473 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 474 | /** RSA public key. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 475 | #define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 476 | /** RSA key pair (private and public key). */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 477 | #define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 478 | |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 479 | /** DSA public key. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 480 | #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 481 | /** DSA key pair (private and public key). */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 482 | #define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 483 | |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 484 | #define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000) |
| 485 | #define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 486 | #define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 487 | /** Elliptic curve key pair. */ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 488 | #define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \ |
| 489 | (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve)) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 490 | /** Elliptic curve public key. */ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 491 | #define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \ |
| 492 | (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve)) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 493 | |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 494 | /** Whether a key type is an elliptic curve key (pair or public-only). */ |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 495 | #define PSA_KEY_TYPE_IS_ECC(type) \ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 496 | ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \ |
| 497 | ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE) |
Gilles Peskine | 55728b0 | 2018-07-16 23:08:16 +0200 | [diff] [blame] | 498 | #define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \ |
| 499 | (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \ |
| 500 | PSA_KEY_TYPE_ECC_KEYPAIR_BASE) |
| 501 | #define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \ |
| 502 | (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \ |
| 503 | PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 504 | |
Gilles Peskine | e1fed0d | 2018-06-18 20:45:45 +0200 | [diff] [blame] | 505 | /** The type of PSA elliptic curve identifiers. */ |
| 506 | typedef uint16_t psa_ecc_curve_t; |
| 507 | /** Extract the curve from an elliptic curve key type. */ |
| 508 | #define PSA_KEY_TYPE_GET_CURVE(type) \ |
| 509 | ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \ |
| 510 | ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \ |
| 511 | 0)) |
| 512 | |
| 513 | /* The encoding of curve identifiers is currently aligned with the |
| 514 | * TLS Supported Groups Registry (formerly known as the |
| 515 | * TLS EC Named Curve Registry) |
| 516 | * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8 |
| 517 | * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */ |
| 518 | #define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001) |
| 519 | #define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002) |
| 520 | #define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003) |
| 521 | #define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004) |
| 522 | #define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005) |
| 523 | #define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006) |
| 524 | #define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007) |
| 525 | #define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008) |
| 526 | #define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009) |
| 527 | #define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a) |
| 528 | #define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b) |
| 529 | #define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c) |
| 530 | #define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d) |
| 531 | #define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e) |
| 532 | #define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f) |
| 533 | #define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010) |
| 534 | #define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011) |
| 535 | #define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012) |
| 536 | #define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013) |
| 537 | #define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014) |
| 538 | #define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015) |
| 539 | #define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016) |
| 540 | #define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017) |
| 541 | #define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018) |
| 542 | #define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019) |
| 543 | #define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a) |
| 544 | #define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b) |
| 545 | #define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c) |
| 546 | #define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d) |
| 547 | #define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e) |
| 548 | #define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100) |
| 549 | #define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101) |
| 550 | #define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102) |
| 551 | #define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103) |
| 552 | #define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104) |
| 553 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 554 | /** The block size of a block cipher. |
| 555 | * |
| 556 | * \param type A cipher key type (value of type #psa_key_type_t). |
| 557 | * |
| 558 | * \return The block size for a block cipher, or 1 for a stream cipher. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 559 | * The return value is undefined if \p type is not a supported |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 560 | * cipher key type. |
| 561 | * |
| 562 | * \note It is possible to build stream cipher algorithms on top of a block |
| 563 | * cipher, for example CTR mode (#PSA_ALG_CTR). |
| 564 | * This macro only takes the key type into account, so it cannot be |
| 565 | * used to determine the size of the data that #psa_cipher_update() |
| 566 | * might buffer for future processing in general. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 567 | * |
| 568 | * \note This macro returns a compile-time constant if its argument is one. |
| 569 | * |
| 570 | * \warning This macro may evaluate its argument multiple times. |
| 571 | */ |
Gilles Peskine | 03182e9 | 2018-03-07 16:40:52 +0100 | [diff] [blame] | 572 | #define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 573 | ( \ |
| 574 | (type) == PSA_KEY_TYPE_AES ? 16 : \ |
| 575 | (type) == PSA_KEY_TYPE_DES ? 8 : \ |
| 576 | (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \ |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 577 | (type) == PSA_KEY_TYPE_ARC4 ? 1 : \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 578 | 0) |
| 579 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 580 | /** \brief Encoding of a cryptographic algorithm. |
| 581 | * |
| 582 | * For algorithms that can be applied to multiple key types, this type |
| 583 | * does not encode the key type. For example, for symmetric ciphers |
| 584 | * based on a block cipher, #psa_algorithm_t encodes the block cipher |
| 585 | * mode and the padding mode while the block cipher itself is encoded |
| 586 | * via #psa_key_type_t. |
| 587 | */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 588 | typedef uint32_t psa_algorithm_t; |
| 589 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 590 | #define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000) |
| 591 | #define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000) |
| 592 | #define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000) |
| 593 | #define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000) |
| 594 | #define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000) |
| 595 | #define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000) |
| 596 | #define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000) |
| 597 | #define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000) |
| 598 | #define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000) |
| 599 | #define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 600 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 601 | #define PSA_ALG_IS_VENDOR_DEFINED(alg) \ |
| 602 | (((alg) & PSA_ALG_VENDOR_FLAG) != 0) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 603 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 604 | /** Whether the specified algorithm is a hash algorithm. |
| 605 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 606 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 607 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 608 | * \return 1 if \p alg is a hash algorithm, 0 otherwise. |
| 609 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 610 | * algorithm identifier. |
| 611 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 612 | #define PSA_ALG_IS_HASH(alg) \ |
| 613 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 614 | |
| 615 | /** Whether the specified algorithm is a MAC algorithm. |
| 616 | * |
| 617 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 618 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 619 | * \return 1 if \p alg is a MAC algorithm, 0 otherwise. |
| 620 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 621 | * algorithm identifier. |
| 622 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 623 | #define PSA_ALG_IS_MAC(alg) \ |
| 624 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 625 | |
| 626 | /** Whether the specified algorithm is a symmetric cipher algorithm. |
| 627 | * |
| 628 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 629 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 630 | * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise. |
| 631 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 632 | * algorithm identifier. |
| 633 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 634 | #define PSA_ALG_IS_CIPHER(alg) \ |
| 635 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 636 | |
| 637 | /** Whether the specified algorithm is an authenticated encryption |
| 638 | * with associated data (AEAD) algorithm. |
| 639 | * |
| 640 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 641 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 642 | * \return 1 if \p alg is an AEAD algorithm, 0 otherwise. |
| 643 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 644 | * algorithm identifier. |
| 645 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 646 | #define PSA_ALG_IS_AEAD(alg) \ |
| 647 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 648 | |
| 649 | /** Whether the specified algorithm is a public-key signature algorithm. |
| 650 | * |
| 651 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 652 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 653 | * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise. |
| 654 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 655 | * algorithm identifier. |
| 656 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 657 | #define PSA_ALG_IS_SIGN(alg) \ |
| 658 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 659 | |
| 660 | /** Whether the specified algorithm is a public-key encryption algorithm. |
| 661 | * |
| 662 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 663 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 664 | * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise. |
| 665 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 666 | * algorithm identifier. |
| 667 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 668 | #define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \ |
| 669 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 670 | |
| 671 | /** Whether the specified algorithm is a key agreement algorithm. |
| 672 | * |
| 673 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 674 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 675 | * \return 1 if \p alg is a key agreement algorithm, 0 otherwise. |
| 676 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 677 | * algorithm identifier. |
| 678 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 679 | #define PSA_ALG_IS_KEY_AGREEMENT(alg) \ |
| 680 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 681 | |
| 682 | /** Whether the specified algorithm is a key derivation algorithm. |
| 683 | * |
| 684 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 685 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 686 | * \return 1 if \p alg is a key derivation algorithm, 0 otherwise. |
| 687 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 688 | * algorithm identifier. |
| 689 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 690 | #define PSA_ALG_IS_KEY_DERIVATION(alg) \ |
| 691 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION) |
| 692 | |
| 693 | #define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff) |
| 694 | #define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001) |
| 695 | #define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002) |
| 696 | #define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003) |
Gilles Peskine | e3f694f | 2018-03-08 07:48:40 +0100 | [diff] [blame] | 697 | #define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004) |
| 698 | #define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 699 | /** SHA2-224 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 700 | #define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 701 | /** SHA2-256 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 702 | #define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 703 | /** SHA2-384 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 704 | #define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 705 | /** SHA2-512 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 706 | #define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 707 | /** SHA2-512/224 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 708 | #define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 709 | /** SHA2-512/256 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 710 | #define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 711 | /** SHA3-224 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 712 | #define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 713 | /** SHA3-256 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 714 | #define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 715 | /** SHA3-384 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 716 | #define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 717 | /** SHA3-512 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 718 | #define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013) |
| 719 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 720 | #define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 721 | #define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000) |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 722 | /** Macro to build an HMAC algorithm. |
| 723 | * |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 724 | * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256. |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 725 | * |
Gilles Peskine | ea4469f | 2018-06-28 13:57:23 +0200 | [diff] [blame] | 726 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 727 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 728 | * |
Gilles Peskine | ea4469f | 2018-06-28 13:57:23 +0200 | [diff] [blame] | 729 | * \return The corresponding HMAC algorithm. |
| 730 | * \return Unspecified if \p alg is not a supported |
| 731 | * hash algorithm. |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 732 | */ |
| 733 | #define PSA_ALG_HMAC(hash_alg) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 734 | (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 735 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 736 | #define PSA_ALG_HMAC_HASH(hmac_alg) \ |
| 737 | (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK)) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 738 | |
| 739 | /** Whether the specified algorithm is an HMAC algorithm. |
| 740 | * |
| 741 | * HMAC is a family of MAC algorithms that are based on a hash function. |
| 742 | * |
| 743 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 744 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 745 | * \return 1 if \p alg is an HMAC algorithm, 0 otherwise. |
| 746 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 747 | * algorithm identifier. |
| 748 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 749 | #define PSA_ALG_IS_HMAC(alg) \ |
| 750 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \ |
| 751 | PSA_ALG_HMAC_BASE) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 752 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 753 | #define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000) |
| 754 | #define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001) |
| 755 | #define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002) |
| 756 | #define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 757 | |
| 758 | /** Whether the specified algorithm is a MAC algorithm based on a block cipher. |
| 759 | * |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 760 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 761 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 762 | * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise. |
| 763 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 764 | * algorithm identifier. |
| 765 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 766 | #define PSA_ALG_IS_CIPHER_MAC(alg) \ |
| 767 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \ |
| 768 | PSA_ALG_CIPHER_MAC_BASE) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 769 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 770 | #define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000) |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 771 | #define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 772 | #define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff) |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 773 | #define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 774 | |
| 775 | /** Use a block cipher mode without padding. |
| 776 | * |
| 777 | * This padding mode may only be used with messages whose lengths are a |
| 778 | * whole number of blocks for the chosen block cipher. |
| 779 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 780 | #define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000) |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 781 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 782 | #define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 783 | |
| 784 | /** Whether the specified algorithm is a block cipher. |
| 785 | * |
| 786 | * A block cipher is a symmetric cipher that encrypts or decrypts messages |
| 787 | * by chopping them into fixed-size blocks. Processing a message requires |
| 788 | * applying a _padding mode_ to transform the message into one whose |
| 789 | * length is a whole number of blocks. To construct an algorithm |
| 790 | * identifier for a block cipher, apply a bitwise-or between the block |
| 791 | * cipher mode and the padding mode. For example, CBC with PKCS#7 padding |
| 792 | * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`. |
| 793 | * |
| 794 | * The transformation applied to each block is determined by the key type. |
| 795 | * For example, to use AES-128-CBC-PKCS7, use the algorithm above with |
| 796 | * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes). |
| 797 | * |
| 798 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 799 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 800 | * \return 1 if \p alg is a block cipher algorithm, 0 otherwise. |
| 801 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 802 | * algorithm identifier or if it is not a symmetric cipher algorithm. |
| 803 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 804 | #define PSA_ALG_IS_BLOCK_CIPHER(alg) \ |
| 805 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \ |
| 806 | PSA_ALG_BLOCK_CIPHER_BASE) |
| 807 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 808 | /** The CBC block cipher mode. |
| 809 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 810 | #define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 811 | #define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002) |
| 812 | #define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003) |
| 813 | #define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004) |
Gilles Peskine | 5d1888e | 2018-07-12 00:32:42 +0200 | [diff] [blame] | 814 | |
| 815 | #define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000) |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 816 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 817 | /** The CTR stream cipher mode. |
| 818 | * |
| 819 | * CTR is a stream cipher which is built from a block cipher. The |
| 820 | * underlying block cipher is determined by the key type. For example, |
| 821 | * to use AES-128-CTR, use this algorithm with |
| 822 | * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes). |
| 823 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 824 | #define PSA_ALG_CTR ((psa_algorithm_t)0x04800001) |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 825 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 826 | /** The ARC4 stream cipher algorithm. |
| 827 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 828 | #define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 829 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 830 | /** Whether the specified algorithm is a stream cipher. |
| 831 | * |
| 832 | * A stream cipher is a symmetric cipher that encrypts or decrypts messages |
| 833 | * by applying a bitwise-xor with a stream of bytes that is generated |
| 834 | * from a key. |
| 835 | * |
| 836 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 837 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 838 | * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise. |
| 839 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 840 | * algorithm identifier or if it is not a symmetric cipher algorithm. |
| 841 | */ |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 842 | #define PSA_ALG_IS_STREAM_CIPHER(alg) \ |
| 843 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \ |
Gilles Peskine | 5d1888e | 2018-07-12 00:32:42 +0200 | [diff] [blame] | 844 | PSA_ALG_STREAM_CIPHER_BASE) |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 845 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 846 | #define PSA_ALG_CCM ((psa_algorithm_t)0x06000001) |
| 847 | #define PSA_ALG_GCM ((psa_algorithm_t)0x06000002) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 848 | |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 849 | #define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000) |
| 850 | /** RSA PKCS#1 v1.5 signature with hashing. |
| 851 | * |
| 852 | * This is the signature scheme defined by RFC 8017 |
| 853 | * (PKCS#1: RSA Cryptography Specifications) under the name |
| 854 | * RSASSA-PKCS1-v1_5. |
| 855 | * |
| 856 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 857 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 858 | * |
| 859 | * \return The corresponding RSA PKCS#1 v1.5 signature algorithm. |
| 860 | * \return Unspecified if \p alg is not a supported |
| 861 | * hash algorithm. |
| 862 | */ |
Gilles Peskine | a592623 | 2018-03-28 14:16:50 +0200 | [diff] [blame] | 863 | #define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 864 | (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 865 | /** Raw PKCS#1 v1.5 signature. |
| 866 | * |
| 867 | * The input to this algorithm is the DigestInfo structure used by |
| 868 | * RFC 8017 (PKCS#1: RSA Cryptography Specifications), §9.2 |
| 869 | * steps 3–6. |
| 870 | */ |
| 871 | #define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE |
Gilles Peskine | a592623 | 2018-03-28 14:16:50 +0200 | [diff] [blame] | 872 | #define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 873 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 874 | |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 875 | #define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000) |
| 876 | /** RSA PSS signature with hashing. |
| 877 | * |
| 878 | * This is the signature scheme defined by RFC 8017 |
| 879 | * (PKCS#1: RSA Cryptography Specifications) under the name |
Gilles Peskine | a4d20bd | 2018-06-29 23:35:02 +0200 | [diff] [blame] | 880 | * RSASSA-PSS, with the message generation function MGF1, and with |
| 881 | * a salt length equal to the length of the hash. The specified |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 882 | * hash algorithm is used to hash the input message, to create the |
| 883 | * salted hash, and for the mask generation. |
| 884 | * |
| 885 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 886 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 887 | * |
| 888 | * \return The corresponding RSA PSS signature algorithm. |
| 889 | * \return Unspecified if \p alg is not a supported |
| 890 | * hash algorithm. |
| 891 | */ |
| 892 | #define PSA_ALG_RSA_PSS(hash_alg) \ |
| 893 | (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 894 | #define PSA_ALG_IS_RSA_PSS(alg) \ |
| 895 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE) |
| 896 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 897 | #define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000) |
| 898 | /** DSA signature with hashing. |
| 899 | * |
| 900 | * This is the signature scheme defined by FIPS 186-4, |
| 901 | * with a random per-message secret number (*k*). |
| 902 | * |
| 903 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 904 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 905 | * |
| 906 | * \return The corresponding DSA signature algorithm. |
| 907 | * \return Unspecified if \p alg is not a supported |
| 908 | * hash algorithm. |
| 909 | */ |
| 910 | #define PSA_ALG_DSA(hash_alg) \ |
| 911 | (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 912 | #define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000) |
| 913 | #define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000) |
| 914 | #define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \ |
| 915 | (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 916 | #define PSA_ALG_IS_DSA(alg) \ |
| 917 | (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \ |
| 918 | PSA_ALG_DSA_BASE) |
| 919 | #define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \ |
| 920 | (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0) |
Gilles Peskine | 55728b0 | 2018-07-16 23:08:16 +0200 | [diff] [blame] | 921 | #define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \ |
| 922 | (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg)) |
| 923 | #define PSA_ALG_IS_RANDOMIZED_DSA(alg) \ |
| 924 | (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg)) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 925 | |
| 926 | #define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000) |
| 927 | /** ECDSA signature with hashing. |
| 928 | * |
| 929 | * This is the ECDSA signature scheme defined by ANSI X9.62, |
| 930 | * with a random per-message secret number (*k*). |
| 931 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 932 | * The representation of the signature as a byte string consists of |
| 933 | * the concatentation of the signature values *r* and *s*. Each of |
| 934 | * *r* and *s* is encoded as an *N*-octet string, where *N* is the length |
| 935 | * of the base point of the curve in octets. Each value is represented |
| 936 | * in big-endian order (most significant octet first). |
| 937 | * |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 938 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 939 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 940 | * |
| 941 | * \return The corresponding ECDSA signature algorithm. |
| 942 | * \return Unspecified if \p alg is not a supported |
| 943 | * hash algorithm. |
| 944 | */ |
| 945 | #define PSA_ALG_ECDSA(hash_alg) \ |
| 946 | (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 947 | /** ECDSA signature without hashing. |
| 948 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 949 | * This is the same signature scheme as #PSA_ALG_ECDSA(), but |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 950 | * without specifying a hash algorithm. This algorithm may only be |
| 951 | * used to sign or verify a sequence of bytes that should be an |
| 952 | * already-calculated hash. Note that the input is padded with |
| 953 | * zeros on the left or truncated on the left as required to fit |
| 954 | * the curve size. |
| 955 | */ |
| 956 | #define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE |
| 957 | #define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000) |
| 958 | /** Deterministic ECDSA signature with hashing. |
| 959 | * |
| 960 | * This is the deterministic ECDSA signature scheme defined by RFC 6979. |
| 961 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 962 | * The representation of a signature is the same as with #PSA_ALG_ECDSA(). |
| 963 | * |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 964 | * Note that when this algorithm is used for verification, signatures |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 965 | * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 966 | * same private key are accepted. In other words, |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 967 | * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from |
| 968 | * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification. |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 969 | * |
| 970 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 971 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 972 | * |
| 973 | * \return The corresponding deterministic ECDSA signature |
| 974 | * algorithm. |
| 975 | * \return Unspecified if \p alg is not a supported |
| 976 | * hash algorithm. |
| 977 | */ |
| 978 | #define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \ |
| 979 | (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 980 | #define PSA_ALG_IS_ECDSA(alg) \ |
| 981 | (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \ |
| 982 | PSA_ALG_ECDSA_BASE) |
| 983 | #define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \ |
| 984 | (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0) |
Gilles Peskine | 55728b0 | 2018-07-16 23:08:16 +0200 | [diff] [blame] | 985 | #define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \ |
| 986 | (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) |
| 987 | #define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \ |
| 988 | (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 989 | |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 990 | /** Get the hash used by a hash-and-sign signature algorithm. |
| 991 | * |
| 992 | * A hash-and-sign algorithm is a signature algorithm which is |
| 993 | * composed of two phases: first a hashing phase which does not use |
| 994 | * the key and produces a hash of the input message, then a signing |
| 995 | * phase which only uses the hash and the key and not the message |
| 996 | * itself. |
| 997 | * |
| 998 | * \param alg A signature algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 999 | * #PSA_ALG_IS_SIGN(\p alg) is true). |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 1000 | * |
| 1001 | * \return The underlying hash algorithm if \p alg is a hash-and-sign |
| 1002 | * algorithm. |
| 1003 | * \return 0 if \p alg is a signature algorithm that does not |
| 1004 | * follow the hash-and-sign structure. |
| 1005 | * \return Unspecified if \p alg is not a signature algorithm or |
| 1006 | * if it is not supported by the implementation. |
| 1007 | */ |
| 1008 | #define PSA_ALG_SIGN_GET_HASH(alg) \ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1009 | (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \ |
| 1010 | PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \ |
Gilles Peskine | 54622ae | 2018-06-29 22:24:24 +0200 | [diff] [blame] | 1011 | ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \ |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 1012 | ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \ |
| 1013 | 0) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1014 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1015 | /** RSA PKCS#1 v1.5 encryption. |
| 1016 | */ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 1017 | #define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1018 | |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 1019 | #define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1020 | /** RSA OAEP encryption. |
| 1021 | * |
| 1022 | * This is the encryption scheme defined by RFC 8017 |
| 1023 | * (PKCS#1: RSA Cryptography Specifications) under the name |
| 1024 | * RSAES-OAEP, with the message generation function MGF1. |
| 1025 | * |
| 1026 | * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that |
| 1027 | * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use |
| 1028 | * for MGF1. |
| 1029 | * |
| 1030 | * \return The corresponding RSA OAEP signature algorithm. |
| 1031 | * \return Unspecified if \p alg is not a supported |
| 1032 | * hash algorithm. |
| 1033 | */ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 1034 | #define PSA_ALG_RSA_OAEP(hash_alg) \ |
| 1035 | (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1036 | #define PSA_ALG_IS_RSA_OAEP(alg) \ |
| 1037 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 1038 | #define PSA_ALG_RSA_OAEP_GET_HASH(alg) \ |
| 1039 | (PSA_ALG_IS_RSA_OAEP(alg) ? \ |
| 1040 | ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \ |
| 1041 | 0) |
Gilles Peskine | d1e8e41 | 2018-06-07 09:49:39 +0200 | [diff] [blame] | 1042 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 1043 | #define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100) |
| 1044 | /** Macro to build an HKDF algorithm. |
| 1045 | * |
| 1046 | * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256. |
| 1047 | * |
| 1048 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 1049 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 1050 | * |
| 1051 | * \return The corresponding HKDF algorithm. |
| 1052 | * \return Unspecified if \p alg is not a supported |
| 1053 | * hash algorithm. |
| 1054 | */ |
| 1055 | #define PSA_ALG_HKDF(hash_alg) \ |
| 1056 | (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1057 | /** Whether the specified algorithm is an HKDF algorithm. |
| 1058 | * |
| 1059 | * HKDF is a family of key derivation algorithms that are based on a hash |
| 1060 | * function and the HMAC construction. |
| 1061 | * |
| 1062 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 1063 | * |
| 1064 | * \return 1 if \c alg is an HKDF algorithm, 0 otherwise. |
| 1065 | * This macro may return either 0 or 1 if \c alg is not a supported |
| 1066 | * key derivation algorithm identifier. |
| 1067 | */ |
| 1068 | #define PSA_ALG_IS_HKDF(alg) \ |
| 1069 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE) |
| 1070 | #define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \ |
| 1071 | (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK)) |
| 1072 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1073 | /**@}*/ |
| 1074 | |
| 1075 | /** \defgroup key_management Key management |
| 1076 | * @{ |
| 1077 | */ |
| 1078 | |
| 1079 | /** |
| 1080 | * \brief Import a key in binary format. |
| 1081 | * |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 1082 | * This function supports any output from psa_export_key(). Refer to the |
| 1083 | * documentation of psa_export_key() for the format for each key type. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1084 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1085 | * \param key Slot where the key will be stored. This must be a |
| 1086 | * valid slot for a key of the chosen type. It must |
| 1087 | * be unoccupied. |
| 1088 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1089 | * \param[in] data Buffer containing the key data. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1090 | * \param data_length Size of the \p data buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1091 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1092 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1093 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1094 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1095 | * The key type or key size is not supported, either by the |
| 1096 | * implementation in general or in this particular slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1097 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1098 | * The key slot is invalid, |
| 1099 | * or the key data is not correctly formatted. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1100 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1101 | * There is already a key in the specified slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1102 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1103 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1104 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1105 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1106 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1107 | */ |
| 1108 | psa_status_t psa_import_key(psa_key_slot_t key, |
| 1109 | psa_key_type_t type, |
| 1110 | const uint8_t *data, |
| 1111 | size_t data_length); |
| 1112 | |
| 1113 | /** |
Gilles Peskine | 154bd95 | 2018-04-19 08:38:16 +0200 | [diff] [blame] | 1114 | * \brief Destroy a key and restore the slot to its default state. |
| 1115 | * |
| 1116 | * This function destroys the content of the key slot from both volatile |
| 1117 | * memory and, if applicable, non-volatile storage. Implementations shall |
| 1118 | * make a best effort to ensure that any previous content of the slot is |
| 1119 | * unrecoverable. |
| 1120 | * |
| 1121 | * This function also erases any metadata such as policies. It returns the |
| 1122 | * specified slot to its default state. |
| 1123 | * |
| 1124 | * \param key The key slot to erase. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1125 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1126 | * \retval #PSA_SUCCESS |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1127 | * The slot's content, if any, has been erased. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1128 | * \retval #PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1129 | * The slot holds content and cannot be erased because it is |
| 1130 | * read-only, either due to a policy or due to physical restrictions. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1131 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1132 | * The specified slot number does not designate a valid slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1133 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1134 | * There was an failure in communication with the cryptoprocessor. |
| 1135 | * The key material may still be present in the cryptoprocessor. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1136 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1137 | * The storage is corrupted. Implementations shall make a best effort |
| 1138 | * to erase key material even in this stage, however applications |
| 1139 | * should be aware that it may be impossible to guarantee that the |
| 1140 | * key material is not recoverable in such cases. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1141 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1142 | * An unexpected condition which is not a storage corruption or |
| 1143 | * a communication failure occurred. The cryptoprocessor may have |
| 1144 | * been compromised. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1145 | */ |
| 1146 | psa_status_t psa_destroy_key(psa_key_slot_t key); |
| 1147 | |
| 1148 | /** |
| 1149 | * \brief Get basic metadata about a key. |
| 1150 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1151 | * \param key Slot whose content is queried. This must |
| 1152 | * be an occupied key slot. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1153 | * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1154 | * This may be a null pointer, in which case the key type |
| 1155 | * is not written. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1156 | * \param[out] bits On success, the key size in bits. |
Gilles Peskine | 9a1ba0d | 2018-03-21 20:49:16 +0100 | [diff] [blame] | 1157 | * This may be a null pointer, in which case the key size |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1158 | * is not written. |
| 1159 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1160 | * \retval #PSA_SUCCESS |
| 1161 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1162 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1163 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1164 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1165 | */ |
| 1166 | psa_status_t psa_get_key_information(psa_key_slot_t key, |
| 1167 | psa_key_type_t *type, |
| 1168 | size_t *bits); |
| 1169 | |
| 1170 | /** |
| 1171 | * \brief Export a key in binary format. |
| 1172 | * |
| 1173 | * The output of this function can be passed to psa_import_key() to |
| 1174 | * create an equivalent object. |
| 1175 | * |
| 1176 | * If a key is created with psa_import_key() and then exported with |
| 1177 | * this function, it is not guaranteed that the resulting data is |
| 1178 | * identical: the implementation may choose a different representation |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1179 | * of the same key if the format permits it. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1180 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1181 | * For standard key types, the output format is as follows: |
| 1182 | * |
| 1183 | * - For symmetric keys (including MAC keys), the format is the |
| 1184 | * raw bytes of the key. |
| 1185 | * - For DES, the key data consists of 8 bytes. The parity bits must be |
| 1186 | * correct. |
| 1187 | * - For Triple-DES, the format is the concatenation of the |
| 1188 | * two or three DES keys. |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1189 | * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1190 | * is the non-encrypted DER encoding of the representation defined by |
| 1191 | * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0. |
| 1192 | * ``` |
| 1193 | * RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1194 | * version INTEGER, -- must be 0 |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1195 | * modulus INTEGER, -- n |
| 1196 | * publicExponent INTEGER, -- e |
| 1197 | * privateExponent INTEGER, -- d |
| 1198 | * prime1 INTEGER, -- p |
| 1199 | * prime2 INTEGER, -- q |
| 1200 | * exponent1 INTEGER, -- d mod (p-1) |
| 1201 | * exponent2 INTEGER, -- d mod (q-1) |
| 1202 | * coefficient INTEGER, -- (inverse of q) mod p |
| 1203 | * } |
| 1204 | * ``` |
| 1205 | * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format |
| 1206 | * is the non-encrypted DER encoding of the representation used by |
Gilles Peskine | c6290c0 | 2018-08-13 17:24:59 +0200 | [diff] [blame^] | 1207 | * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows: |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1208 | * ``` |
| 1209 | * DSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1210 | * version INTEGER, -- must be 0 |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1211 | * prime INTEGER, -- p |
| 1212 | * subprime INTEGER, -- q |
| 1213 | * generator INTEGER, -- g |
| 1214 | * public INTEGER, -- y |
| 1215 | * private INTEGER, -- x |
| 1216 | * } |
| 1217 | * ``` |
| 1218 | * - For elliptic curve key pairs (key types for which |
| 1219 | * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is the |
| 1220 | * non-encrypted DER encoding of the representation defined by RFC 5915 as |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1221 | * `ECPrivateKey`, version 1. The `ECParameters` field must be a |
| 1222 | * `namedCurve` OID as specified in RFC 5480 §2.1.1.1. The public key |
| 1223 | * must be present and must be an `ECPoint` in the same format |
| 1224 | * (uncompressed variant) an ECC public key of the |
| 1225 | * corresponding type exported with psa_export_public_key(). |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1226 | * ``` |
| 1227 | * ECPrivateKey ::= SEQUENCE { |
| 1228 | * version INTEGER, -- must be 1 |
| 1229 | * privateKey OCTET STRING, |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1230 | * -- `ceiling(log2(n)/8)`-byte string, big endian, |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1231 | * -- where n is the order of the curve. |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1232 | * parameters [0] IMPLICIT ECParameters {{ namedCurve }}, -- mandatory |
| 1233 | * publicKey [1] IMPLICIT BIT STRING -- mandatory |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1234 | * } |
| 1235 | * ``` |
| 1236 | * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is |
| 1237 | * true), the format is the same as for psa_export_public_key(). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1238 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1239 | * \param key Slot whose content is to be exported. This must |
| 1240 | * be an occupied key slot. |
| 1241 | * \param[out] data Buffer where the key data is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1242 | * \param data_size Size of the \p data buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1243 | * \param[out] data_length On success, the number of bytes |
| 1244 | * that make up the key data. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1245 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1246 | * \retval #PSA_SUCCESS |
| 1247 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1248 | * \retval #PSA_ERROR_NOT_PERMITTED |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1249 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 1250 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1251 | * The size of the \p data buffer is too small. You can determine a |
| 1252 | * sufficient buffer size by calling |
| 1253 | * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits) |
| 1254 | * where \c type is the key type |
| 1255 | * and \c bits is the key size in bits. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1256 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1257 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1258 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1259 | */ |
| 1260 | psa_status_t psa_export_key(psa_key_slot_t key, |
| 1261 | uint8_t *data, |
| 1262 | size_t data_size, |
| 1263 | size_t *data_length); |
| 1264 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1265 | /** |
| 1266 | * \brief Export a public key or the public part of a key pair in binary format. |
| 1267 | * |
| 1268 | * The output of this function can be passed to psa_import_key() to |
| 1269 | * create an object that is equivalent to the public key. |
| 1270 | * |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1271 | * The format is the DER representation defined by RFC 5280 as |
| 1272 | * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format |
| 1273 | * specified below. |
| 1274 | * ``` |
| 1275 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 1276 | * algorithm AlgorithmIdentifier, |
| 1277 | * subjectPublicKey BIT STRING } |
| 1278 | * AlgorithmIdentifier ::= SEQUENCE { |
| 1279 | * algorithm OBJECT IDENTIFIER, |
| 1280 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 1281 | * ``` |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1282 | * |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1283 | * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), |
| 1284 | * the `subjectPublicKey` format is defined by RFC 3279 §2.3.1 as |
| 1285 | * `RSAPublicKey`, |
| 1286 | * with the OID `rsaEncryption`, |
| 1287 | * and with the parameters `NULL`. |
| 1288 | * ``` |
| 1289 | * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) |
| 1290 | * rsadsi(113549) pkcs(1) 1 } |
| 1291 | * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 } |
| 1292 | * |
| 1293 | * RSAPublicKey ::= SEQUENCE { |
| 1294 | * modulus INTEGER, -- n |
| 1295 | * publicExponent INTEGER } -- e |
| 1296 | * ``` |
| 1297 | * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), |
| 1298 | * the `subjectPublicKey` format is defined by RFC 3279 §2.3.2 as |
| 1299 | * `DSAPublicKey`, |
| 1300 | * with the OID `id-dsa`, |
| 1301 | * and with the parameters `DSS-Parms`. |
| 1302 | * ``` |
| 1303 | * id-dsa OBJECT IDENTIFIER ::= { |
| 1304 | * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 } |
| 1305 | * |
| 1306 | * Dss-Parms ::= SEQUENCE { |
| 1307 | * p INTEGER, |
| 1308 | * q INTEGER, |
| 1309 | * g INTEGER } |
| 1310 | * DSAPublicKey ::= INTEGER -- public key, Y |
| 1311 | * ``` |
| 1312 | * - For elliptic curve public keys (key types for which |
| 1313 | * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), |
| 1314 | * the `subjectPublicKey` format is defined by RFC 3279 §2.3.5 as |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1315 | * `ECPoint`, which contains the uncompressed |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1316 | * representation defined by SEC1 §2.3.3. |
| 1317 | * The OID is `id-ecPublicKey`, |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1318 | * and the parameters must be given as a `namedCurve` OID as specified in |
Gilles Peskine | c6290c0 | 2018-08-13 17:24:59 +0200 | [diff] [blame^] | 1319 | * RFC 5480 §2.1.1.1 or other applicable standards. |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1320 | * ``` |
| 1321 | * ansi-X9-62 OBJECT IDENTIFIER ::= |
| 1322 | * { iso(1) member-body(2) us(840) 10045 } |
| 1323 | * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 } |
| 1324 | * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 } |
| 1325 | * |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1326 | * ECPoint ::= ... |
| 1327 | * -- first 8 bits: 0x04; |
| 1328 | * -- then x_P as an n-bit string, big endian; |
| 1329 | * -- then y_P as a n-bit string, big endian, |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1330 | * -- where n is the order of the curve. |
| 1331 | * |
| 1332 | * EcpkParameters ::= CHOICE { -- other choices are not allowed |
| 1333 | * namedCurve OBJECT IDENTIFIER } |
| 1334 | * ``` |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1335 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1336 | * \param key Slot whose content is to be exported. This must |
| 1337 | * be an occupied key slot. |
| 1338 | * \param[out] data Buffer where the key data is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1339 | * \param data_size Size of the \p data buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1340 | * \param[out] data_length On success, the number of bytes |
| 1341 | * that make up the key data. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1342 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1343 | * \retval #PSA_SUCCESS |
| 1344 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1345 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 1346 | * The key is neither a public key nor a key pair. |
| 1347 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1348 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1349 | * The size of the \p data buffer is too small. You can determine a |
| 1350 | * sufficient buffer size by calling |
| 1351 | * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits) |
| 1352 | * where \c type is the key type |
| 1353 | * and \c bits is the key size in bits. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1354 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1355 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1356 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1357 | */ |
| 1358 | psa_status_t psa_export_public_key(psa_key_slot_t key, |
| 1359 | uint8_t *data, |
| 1360 | size_t data_size, |
| 1361 | size_t *data_length); |
| 1362 | |
| 1363 | /**@}*/ |
| 1364 | |
| 1365 | /** \defgroup policy Key policies |
| 1366 | * @{ |
| 1367 | */ |
| 1368 | |
| 1369 | /** \brief Encoding of permitted usage on a key. */ |
| 1370 | typedef uint32_t psa_key_usage_t; |
| 1371 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1372 | /** Whether the key may be exported. |
| 1373 | * |
| 1374 | * A public key or the public part of a key pair may always be exported |
| 1375 | * regardless of the value of this permission flag. |
| 1376 | * |
| 1377 | * If a key does not have export permission, implementations shall not |
| 1378 | * allow the key to be exported in plain form from the cryptoprocessor, |
| 1379 | * whether through psa_export_key() or through a proprietary interface. |
| 1380 | * The key may however be exportable in a wrapped form, i.e. in a form |
| 1381 | * where it is encrypted by another key. |
| 1382 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1383 | #define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001) |
| 1384 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1385 | /** Whether the key may be used to encrypt a message. |
| 1386 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1387 | * This flag allows the key to be used for a symmetric encryption operation, |
| 1388 | * for an AEAD encryption-and-authentication operation, |
| 1389 | * or for an asymmetric encryption operation, |
| 1390 | * if otherwise permitted by the key's type and policy. |
| 1391 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1392 | * For a key pair, this concerns the public key. |
| 1393 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1394 | #define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1395 | |
| 1396 | /** Whether the key may be used to decrypt a message. |
| 1397 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1398 | * This flag allows the key to be used for a symmetric decryption operation, |
| 1399 | * for an AEAD decryption-and-verification operation, |
| 1400 | * or for an asymmetric decryption operation, |
| 1401 | * if otherwise permitted by the key's type and policy. |
| 1402 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1403 | * For a key pair, this concerns the private key. |
| 1404 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1405 | #define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1406 | |
| 1407 | /** Whether the key may be used to sign a message. |
| 1408 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1409 | * This flag allows the key to be used for a MAC calculation operation |
| 1410 | * or for an asymmetric signature operation, |
| 1411 | * if otherwise permitted by the key's type and policy. |
| 1412 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1413 | * For a key pair, this concerns the private key. |
| 1414 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1415 | #define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1416 | |
| 1417 | /** Whether the key may be used to verify a message signature. |
| 1418 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1419 | * This flag allows the key to be used for a MAC verification operation |
| 1420 | * or for an asymmetric signature verification operation, |
| 1421 | * if otherwise permitted by by the key's type and policy. |
| 1422 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1423 | * For a key pair, this concerns the public key. |
| 1424 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1425 | #define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800) |
| 1426 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1427 | /** Whether the key may be used to derive other keys. |
| 1428 | */ |
| 1429 | #define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000) |
| 1430 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1431 | /** The type of the key policy data structure. |
| 1432 | * |
| 1433 | * This is an implementation-defined \c struct. Applications should not |
| 1434 | * make any assumptions about the content of this structure except |
| 1435 | * as directed by the documentation of a specific implementation. */ |
| 1436 | typedef struct psa_key_policy_s psa_key_policy_t; |
| 1437 | |
| 1438 | /** \brief Initialize a key policy structure to a default that forbids all |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1439 | * usage of the key. |
| 1440 | * |
| 1441 | * \param[out] policy The policy object to initialize. |
| 1442 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1443 | void psa_key_policy_init(psa_key_policy_t *policy); |
| 1444 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1445 | /** \brief Set the standard fields of a policy structure. |
| 1446 | * |
| 1447 | * Note that this function does not make any consistency check of the |
| 1448 | * parameters. The values are only checked when applying the policy to |
| 1449 | * a key slot with psa_set_key_policy(). |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1450 | * |
| 1451 | * \param[out] policy The policy object to modify. |
| 1452 | * \param usage The permitted uses for the key. |
| 1453 | * \param alg The algorithm that the key may be used for. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1454 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1455 | void psa_key_policy_set_usage(psa_key_policy_t *policy, |
| 1456 | psa_key_usage_t usage, |
| 1457 | psa_algorithm_t alg); |
| 1458 | |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1459 | /** \brief Retrieve the usage field of a policy structure. |
| 1460 | * |
| 1461 | * \param[in] policy The policy object to query. |
| 1462 | * |
| 1463 | * \return The permitted uses for a key with this policy. |
| 1464 | */ |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 1465 | psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1466 | |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1467 | /** \brief Retrieve the algorithm field of a policy structure. |
| 1468 | * |
| 1469 | * \param[in] policy The policy object to query. |
| 1470 | * |
| 1471 | * \return The permitted algorithm for a key with this policy. |
| 1472 | */ |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 1473 | psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1474 | |
| 1475 | /** \brief Set the usage policy on a key slot. |
| 1476 | * |
| 1477 | * This function must be called on an empty key slot, before importing, |
| 1478 | * generating or creating a key in the slot. Changing the policy of an |
| 1479 | * existing key is not permitted. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1480 | * |
| 1481 | * Implementations may set restrictions on supported key policies |
| 1482 | * depending on the key type and the key slot. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1483 | * |
| 1484 | * \param key The key slot whose policy is to be changed. |
| 1485 | * \param[in] policy The policy object to query. |
| 1486 | * |
| 1487 | * \retval #PSA_SUCCESS |
| 1488 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 1489 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1490 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1491 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1492 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1493 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1494 | */ |
| 1495 | psa_status_t psa_set_key_policy(psa_key_slot_t key, |
| 1496 | const psa_key_policy_t *policy); |
| 1497 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1498 | /** \brief Get the usage policy for a key slot. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1499 | * |
| 1500 | * \param key The key slot whose policy is being queried. |
| 1501 | * \param[out] policy On success, the key's policy. |
| 1502 | * |
| 1503 | * \retval #PSA_SUCCESS |
| 1504 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1505 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1506 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1507 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1508 | psa_status_t psa_get_key_policy(psa_key_slot_t key, |
| 1509 | psa_key_policy_t *policy); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1510 | |
| 1511 | /**@}*/ |
| 1512 | |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 1513 | /** \defgroup persistence Key lifetime |
| 1514 | * @{ |
| 1515 | */ |
| 1516 | |
| 1517 | /** Encoding of key lifetimes. |
| 1518 | */ |
| 1519 | typedef uint32_t psa_key_lifetime_t; |
| 1520 | |
| 1521 | /** A volatile key slot retains its content as long as the application is |
| 1522 | * running. It is guaranteed to be erased on a power reset. |
| 1523 | */ |
| 1524 | #define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000) |
| 1525 | |
| 1526 | /** A persistent key slot retains its content as long as it is not explicitly |
| 1527 | * destroyed. |
| 1528 | */ |
| 1529 | #define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001) |
| 1530 | |
| 1531 | /** A write-once key slot may not be modified once a key has been set. |
| 1532 | * It will retain its content as long as the device remains operational. |
| 1533 | */ |
| 1534 | #define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff) |
| 1535 | |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1536 | /** \brief Retrieve the lifetime of a key slot. |
| 1537 | * |
| 1538 | * The assignment of lifetimes to slots is implementation-dependent. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 1539 | * |
Gilles Peskine | 9bb53d7 | 2018-04-17 14:09:24 +0200 | [diff] [blame] | 1540 | * \param key Slot to query. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1541 | * \param[out] lifetime On success, the lifetime value. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 1542 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1543 | * \retval #PSA_SUCCESS |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1544 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1545 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
mohammad1603 | a7d245a | 2018-04-17 00:40:08 -0700 | [diff] [blame] | 1546 | * The key slot is invalid. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1547 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1548 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1549 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1550 | */ |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 1551 | psa_status_t psa_get_key_lifetime(psa_key_slot_t key, |
| 1552 | psa_key_lifetime_t *lifetime); |
| 1553 | |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1554 | /** \brief Change the lifetime of a key slot. |
| 1555 | * |
| 1556 | * Whether the lifetime of a key slot can be changed at all, and if so |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1557 | * whether the lifetime of an occupied key slot can be changed, is |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1558 | * implementation-dependent. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 1559 | * |
Gilles Peskine | 9bb53d7 | 2018-04-17 14:09:24 +0200 | [diff] [blame] | 1560 | * \param key Slot whose lifetime is to be changed. |
| 1561 | * \param lifetime The lifetime value to set for the given key slot. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 1562 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1563 | * \retval #PSA_SUCCESS |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1564 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1565 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1566 | * The key slot is invalid, |
mohammad1603 | a7d245a | 2018-04-17 00:40:08 -0700 | [diff] [blame] | 1567 | * or the lifetime value is invalid. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1568 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | f0c9dd3 | 2018-04-17 14:11:07 +0200 | [diff] [blame] | 1569 | * The implementation does not support the specified lifetime value, |
| 1570 | * at least for the specified key slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1571 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | f0c9dd3 | 2018-04-17 14:11:07 +0200 | [diff] [blame] | 1572 | * The slot contains a key, and the implementation does not support |
| 1573 | * changing the lifetime of an occupied slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1574 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1575 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1576 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1577 | */ |
| 1578 | psa_status_t psa_set_key_lifetime(psa_key_slot_t key, |
mohammad1603 | ea05009 | 2018-04-17 00:31:34 -0700 | [diff] [blame] | 1579 | psa_key_lifetime_t lifetime); |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1580 | |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 1581 | /**@}*/ |
| 1582 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1583 | /** \defgroup hash Message digests |
| 1584 | * @{ |
| 1585 | */ |
| 1586 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1587 | /** The type of the state data structure for multipart hash operations. |
| 1588 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1589 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1590 | * make any assumptions about the content of this structure except |
| 1591 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1592 | typedef struct psa_hash_operation_s psa_hash_operation_t; |
| 1593 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1594 | /** The size of the output of psa_hash_finish(), in bytes. |
| 1595 | * |
| 1596 | * This is also the hash size that psa_hash_verify() expects. |
| 1597 | * |
| 1598 | * \param alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1599 | * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 1600 | * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 1601 | * hash algorithm). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1602 | * |
| 1603 | * \return The hash size for the specified hash algorithm. |
| 1604 | * If the hash algorithm is not recognized, return 0. |
| 1605 | * An implementation may return either 0 or the correct size |
| 1606 | * for a hash algorithm that it recognizes, but does not support. |
| 1607 | */ |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 1608 | #define PSA_HASH_SIZE(alg) \ |
| 1609 | ( \ |
| 1610 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \ |
| 1611 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \ |
| 1612 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \ |
| 1613 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \ |
| 1614 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \ |
| 1615 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \ |
| 1616 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \ |
| 1617 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \ |
| 1618 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \ |
| 1619 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \ |
| 1620 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \ |
| 1621 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \ |
| 1622 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \ |
| 1623 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \ |
| 1624 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1625 | 0) |
| 1626 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1627 | /** Start a multipart hash operation. |
| 1628 | * |
| 1629 | * The sequence of operations to calculate a hash (message digest) |
| 1630 | * is as follows: |
| 1631 | * -# Allocate an operation object which will be passed to all the functions |
| 1632 | * listed here. |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1633 | * -# Call psa_hash_setup() to specify the algorithm. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1634 | * -# Call psa_hash_update() zero, one or more times, passing a fragment |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1635 | * of the message each time. The hash that is calculated is the hash |
| 1636 | * of the concatenation of these messages in order. |
| 1637 | * -# To calculate the hash, call psa_hash_finish(). |
| 1638 | * To compare the hash with an expected value, call psa_hash_verify(). |
| 1639 | * |
| 1640 | * The application may call psa_hash_abort() at any time after the operation |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1641 | * has been initialized with psa_hash_setup(). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1642 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1643 | * After a successful call to psa_hash_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1644 | * eventually terminate the operation. The following events terminate an |
| 1645 | * operation: |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1646 | * - A failed call to psa_hash_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1647 | * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort(). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1648 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1649 | * \param[out] operation The operation object to use. |
| 1650 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 1651 | * such that #PSA_ALG_IS_HASH(\p alg) is true). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1652 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1653 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1654 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1655 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1656 | * \p alg is not supported or is not a hash algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1657 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1658 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1659 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1660 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1661 | */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1662 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1663 | psa_algorithm_t alg); |
| 1664 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1665 | /** Add a message fragment to a multipart hash operation. |
| 1666 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1667 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1668 | * |
| 1669 | * If this function returns an error status, the operation becomes inactive. |
| 1670 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1671 | * \param[in,out] operation Active hash operation. |
| 1672 | * \param[in] input Buffer containing the message fragment to hash. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1673 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1674 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1675 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1676 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1677 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1678 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1679 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1680 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1681 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1682 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1683 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1684 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 1685 | const uint8_t *input, |
| 1686 | size_t input_length); |
| 1687 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1688 | /** Finish the calculation of the hash of a message. |
| 1689 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1690 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1691 | * This function calculates the hash of the message formed by concatenating |
| 1692 | * the inputs passed to preceding calls to psa_hash_update(). |
| 1693 | * |
| 1694 | * When this function returns, the operation becomes inactive. |
| 1695 | * |
| 1696 | * \warning Applications should not call this function if they expect |
| 1697 | * a specific value for the hash. Call psa_hash_verify() instead. |
| 1698 | * Beware that comparing integrity or authenticity data such as |
| 1699 | * hash values with a function such as \c memcmp is risky |
| 1700 | * because the time taken by the comparison may leak information |
| 1701 | * about the hashed data which could allow an attacker to guess |
| 1702 | * a valid hash and thereby bypass security controls. |
| 1703 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1704 | * \param[in,out] operation Active hash operation. |
| 1705 | * \param[out] hash Buffer where the hash is to be written. |
| 1706 | * \param hash_size Size of the \p hash buffer in bytes. |
| 1707 | * \param[out] hash_length On success, the number of bytes |
| 1708 | * that make up the hash value. This is always |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 1709 | * #PSA_HASH_SIZE(\c alg) where \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1710 | * hash algorithm that is calculated. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1711 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1712 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1713 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1714 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1715 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1716 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1717 | * The size of the \p hash buffer is too small. You can determine a |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1718 | * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1719 | * where \c alg is the hash algorithm that is calculated. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1720 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1721 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1722 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1723 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1724 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1725 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 1726 | uint8_t *hash, |
| 1727 | size_t hash_size, |
| 1728 | size_t *hash_length); |
| 1729 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1730 | /** Finish the calculation of the hash of a message and compare it with |
| 1731 | * an expected value. |
| 1732 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1733 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1734 | * This function calculates the hash of the message formed by concatenating |
| 1735 | * the inputs passed to preceding calls to psa_hash_update(). It then |
| 1736 | * compares the calculated hash with the expected hash passed as a |
| 1737 | * parameter to this function. |
| 1738 | * |
| 1739 | * When this function returns, the operation becomes inactive. |
| 1740 | * |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1741 | * \note Implementations shall make the best effort to ensure that the |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1742 | * comparison between the actual hash and the expected hash is performed |
| 1743 | * in constant time. |
| 1744 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1745 | * \param[in,out] operation Active hash operation. |
| 1746 | * \param[in] hash Buffer containing the expected hash value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1747 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1748 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1749 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1750 | * The expected hash is identical to the actual hash of the message. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1751 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1752 | * The hash of the message was calculated successfully, but it |
| 1753 | * differs from the expected hash. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1754 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1755 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1756 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1757 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1758 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1759 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1760 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1761 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 1762 | const uint8_t *hash, |
| 1763 | size_t hash_length); |
| 1764 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1765 | /** Abort a hash operation. |
| 1766 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1767 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1768 | * \p operation structure itself. Once aborted, the operation object |
| 1769 | * can be reused for another operation by calling |
| 1770 | * psa_hash_setup() again. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1771 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1772 | * You may call this function any time after the operation object has |
| 1773 | * been initialized by any of the following methods: |
| 1774 | * - A call to psa_hash_setup(), whether it succeeds or not. |
| 1775 | * - Initializing the \c struct to all-bits-zero. |
| 1776 | * - Initializing the \c struct to logical zeros, e.g. |
| 1777 | * `psa_hash_operation_t operation = {0}`. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1778 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1779 | * In particular, calling psa_hash_abort() after the operation has been |
| 1780 | * terminated by a call to psa_hash_abort(), psa_hash_finish() or |
| 1781 | * psa_hash_verify() is safe and has no effect. |
| 1782 | * |
| 1783 | * \param[in,out] operation Initialized hash operation. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1784 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1785 | * \retval #PSA_SUCCESS |
| 1786 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1787 | * \p operation is not an active hash operation. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1788 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1789 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1790 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1791 | */ |
| 1792 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1793 | |
| 1794 | /**@}*/ |
| 1795 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1796 | /** \defgroup MAC Message authentication codes |
| 1797 | * @{ |
| 1798 | */ |
| 1799 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1800 | /** The type of the state data structure for multipart MAC operations. |
| 1801 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1802 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1803 | * make any assumptions about the content of this structure except |
| 1804 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1805 | typedef struct psa_mac_operation_s psa_mac_operation_t; |
| 1806 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1807 | /** Start a multipart MAC calculation operation. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1808 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1809 | * This function sets up the calculation of the MAC |
| 1810 | * (message authentication code) of a byte string. |
| 1811 | * To verify the MAC of a message against an |
| 1812 | * expected value, use psa_mac_verify_setup() instead. |
| 1813 | * |
| 1814 | * The sequence of operations to calculate a MAC is as follows: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1815 | * -# Allocate an operation object which will be passed to all the functions |
| 1816 | * listed here. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1817 | * -# Call psa_mac_sign_setup() to specify the algorithm and key. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1818 | * The key remains associated with the operation even if the content |
| 1819 | * of the key slot changes. |
| 1820 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 1821 | * of the message each time. The MAC that is calculated is the MAC |
| 1822 | * of the concatenation of these messages in order. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1823 | * -# At the end of the message, call psa_mac_sign_finish() to finish |
| 1824 | * calculating the MAC value and retrieve it. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1825 | * |
| 1826 | * The application may call psa_mac_abort() at any time after the operation |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1827 | * has been initialized with psa_mac_sign_setup(). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1828 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1829 | * After a successful call to psa_mac_sign_setup(), the application must |
| 1830 | * eventually terminate the operation through one of the following methods: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1831 | * - A failed call to psa_mac_update(). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1832 | * - A call to psa_mac_sign_finish() or psa_mac_abort(). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1833 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1834 | * \param[out] operation The operation object to use. |
| 1835 | * \param key Slot containing the key to use for the operation. |
| 1836 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1837 | * such that #PSA_ALG_IS_MAC(alg) is true). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1838 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1839 | * \retval #PSA_SUCCESS |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1840 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1841 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1842 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1843 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1844 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1845 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1846 | * \p alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1847 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1848 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1849 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1850 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1851 | */ |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1852 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 1853 | psa_key_slot_t key, |
| 1854 | psa_algorithm_t alg); |
| 1855 | |
| 1856 | /** Start a multipart MAC verification operation. |
| 1857 | * |
| 1858 | * This function sets up the verification of the MAC |
| 1859 | * (message authentication code) of a byte string against an expected value. |
| 1860 | * |
| 1861 | * The sequence of operations to verify a MAC is as follows: |
| 1862 | * -# Allocate an operation object which will be passed to all the functions |
| 1863 | * listed here. |
| 1864 | * -# Call psa_mac_verify_setup() to specify the algorithm and key. |
| 1865 | * The key remains associated with the operation even if the content |
| 1866 | * of the key slot changes. |
| 1867 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 1868 | * of the message each time. The MAC that is calculated is the MAC |
| 1869 | * of the concatenation of these messages in order. |
| 1870 | * -# At the end of the message, call psa_mac_verify_finish() to finish |
| 1871 | * calculating the actual MAC of the message and verify it against |
| 1872 | * the expected value. |
| 1873 | * |
| 1874 | * The application may call psa_mac_abort() at any time after the operation |
| 1875 | * has been initialized with psa_mac_verify_setup(). |
| 1876 | * |
| 1877 | * After a successful call to psa_mac_verify_setup(), the application must |
| 1878 | * eventually terminate the operation through one of the following methods: |
| 1879 | * - A failed call to psa_mac_update(). |
| 1880 | * - A call to psa_mac_verify_finish() or psa_mac_abort(). |
| 1881 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1882 | * \param[out] operation The operation object to use. |
| 1883 | * \param key Slot containing the key to use for the operation. |
| 1884 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1885 | * such that #PSA_ALG_IS_MAC(\p alg) is true). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1886 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1887 | * \retval #PSA_SUCCESS |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1888 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1889 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1890 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1891 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1892 | * \c key is not compatible with \c alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1893 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1894 | * \c alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1895 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1896 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1897 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1898 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1899 | */ |
| 1900 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 1901 | psa_key_slot_t key, |
| 1902 | psa_algorithm_t alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1903 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1904 | /** Add a message fragment to a multipart MAC operation. |
| 1905 | * |
| 1906 | * The application must call psa_mac_sign_setup() or psa_mac_verify_setup() |
| 1907 | * before calling this function. |
| 1908 | * |
| 1909 | * If this function returns an error status, the operation becomes inactive. |
| 1910 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1911 | * \param[in,out] operation Active MAC operation. |
| 1912 | * \param[in] input Buffer containing the message fragment to add to |
| 1913 | * the MAC calculation. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1914 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1915 | * |
| 1916 | * \retval #PSA_SUCCESS |
| 1917 | * Success. |
| 1918 | * \retval #PSA_ERROR_BAD_STATE |
| 1919 | * The operation state is not valid (not started, or already completed). |
| 1920 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1921 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1922 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1923 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1924 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1925 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 1926 | const uint8_t *input, |
| 1927 | size_t input_length); |
| 1928 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1929 | /** Finish the calculation of the MAC of a message. |
| 1930 | * |
| 1931 | * The application must call psa_mac_sign_setup() before calling this function. |
| 1932 | * This function calculates the MAC of the message formed by concatenating |
| 1933 | * the inputs passed to preceding calls to psa_mac_update(). |
| 1934 | * |
| 1935 | * When this function returns, the operation becomes inactive. |
| 1936 | * |
| 1937 | * \warning Applications should not call this function if they expect |
| 1938 | * a specific value for the MAC. Call psa_mac_verify_finish() instead. |
| 1939 | * Beware that comparing integrity or authenticity data such as |
| 1940 | * MAC values with a function such as \c memcmp is risky |
| 1941 | * because the time taken by the comparison may leak information |
| 1942 | * about the MAC value which could allow an attacker to guess |
| 1943 | * a valid MAC and thereby bypass security controls. |
| 1944 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1945 | * \param[in,out] operation Active MAC operation. |
| 1946 | * \param[out] mac Buffer where the MAC value is to be written. |
| 1947 | * \param mac_size Size of the \p mac buffer in bytes. |
| 1948 | * \param[out] mac_length On success, the number of bytes |
| 1949 | * that make up the MAC value. This is always |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1950 | * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg) |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1951 | * where \c key_type and \c key_bits are the type and |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1952 | * bit-size respectively of the key and \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1953 | * MAC algorithm that is calculated. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1954 | * |
| 1955 | * \retval #PSA_SUCCESS |
| 1956 | * Success. |
| 1957 | * \retval #PSA_ERROR_BAD_STATE |
| 1958 | * The operation state is not valid (not started, or already completed). |
| 1959 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1960 | * The size of the \p mac buffer is too small. You can determine a |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1961 | * sufficient buffer size by calling PSA_MAC_FINAL_SIZE(). |
| 1962 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1963 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1964 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1965 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1966 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 1967 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 1968 | uint8_t *mac, |
| 1969 | size_t mac_size, |
| 1970 | size_t *mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1971 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1972 | /** Finish the calculation of the MAC of a message and compare it with |
| 1973 | * an expected value. |
| 1974 | * |
| 1975 | * The application must call psa_mac_verify_setup() before calling this function. |
| 1976 | * This function calculates the MAC of the message formed by concatenating |
| 1977 | * the inputs passed to preceding calls to psa_mac_update(). It then |
| 1978 | * compares the calculated MAC with the expected MAC passed as a |
| 1979 | * parameter to this function. |
| 1980 | * |
| 1981 | * When this function returns, the operation becomes inactive. |
| 1982 | * |
| 1983 | * \note Implementations shall make the best effort to ensure that the |
| 1984 | * comparison between the actual MAC and the expected MAC is performed |
| 1985 | * in constant time. |
| 1986 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1987 | * \param[in,out] operation Active MAC operation. |
| 1988 | * \param[in] mac Buffer containing the expected MAC value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1989 | * \param mac_length Size of the \p mac buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1990 | * |
| 1991 | * \retval #PSA_SUCCESS |
| 1992 | * The expected MAC is identical to the actual MAC of the message. |
| 1993 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 1994 | * The MAC of the message was calculated successfully, but it |
| 1995 | * differs from the expected MAC. |
| 1996 | * \retval #PSA_ERROR_BAD_STATE |
| 1997 | * The operation state is not valid (not started, or already completed). |
| 1998 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1999 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2000 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2001 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2002 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2003 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2004 | const uint8_t *mac, |
| 2005 | size_t mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2006 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2007 | /** Abort a MAC operation. |
| 2008 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2009 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2010 | * \p operation structure itself. Once aborted, the operation object |
| 2011 | * can be reused for another operation by calling |
| 2012 | * psa_mac_sign_setup() or psa_mac_verify_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2013 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2014 | * You may call this function any time after the operation object has |
| 2015 | * been initialized by any of the following methods: |
| 2016 | * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether |
| 2017 | * it succeeds or not. |
| 2018 | * - Initializing the \c struct to all-bits-zero. |
| 2019 | * - Initializing the \c struct to logical zeros, e.g. |
| 2020 | * `psa_mac_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2021 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2022 | * In particular, calling psa_mac_abort() after the operation has been |
| 2023 | * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or |
| 2024 | * psa_mac_verify_finish() is safe and has no effect. |
| 2025 | * |
| 2026 | * \param[in,out] operation Initialized MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2027 | * |
| 2028 | * \retval #PSA_SUCCESS |
| 2029 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2030 | * \p operation is not an active MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2031 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2032 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2033 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2034 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2035 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation); |
| 2036 | |
| 2037 | /**@}*/ |
| 2038 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2039 | /** \defgroup cipher Symmetric ciphers |
| 2040 | * @{ |
| 2041 | */ |
| 2042 | |
| 2043 | /** The type of the state data structure for multipart cipher operations. |
| 2044 | * |
| 2045 | * This is an implementation-defined \c struct. Applications should not |
| 2046 | * make any assumptions about the content of this structure except |
| 2047 | * as directed by the documentation of a specific implementation. */ |
| 2048 | typedef struct psa_cipher_operation_s psa_cipher_operation_t; |
| 2049 | |
| 2050 | /** Set the key for a multipart symmetric encryption operation. |
| 2051 | * |
| 2052 | * The sequence of operations to encrypt a message with a symmetric cipher |
| 2053 | * is as follows: |
| 2054 | * -# Allocate an operation object which will be passed to all the functions |
| 2055 | * listed here. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2056 | * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2057 | * The key remains associated with the operation even if the content |
| 2058 | * of the key slot changes. |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 2059 | * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2060 | * generate or set the IV (initialization vector). You should use |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 2061 | * psa_cipher_generate_iv() unless the protocol you are implementing |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2062 | * requires a specific IV value. |
| 2063 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 2064 | * of the message each time. |
| 2065 | * -# Call psa_cipher_finish(). |
| 2066 | * |
| 2067 | * The application may call psa_cipher_abort() at any time after the operation |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2068 | * has been initialized with psa_cipher_encrypt_setup(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2069 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2070 | * After a successful call to psa_cipher_encrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 2071 | * eventually terminate the operation. The following events terminate an |
| 2072 | * operation: |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 2073 | * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv() |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2074 | * or psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 2075 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2076 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2077 | * \param[out] operation The operation object to use. |
| 2078 | * \param key Slot containing the key to use for the operation. |
| 2079 | * \param alg The cipher algorithm to compute |
| 2080 | * (\c PSA_ALG_XXX value such that |
| 2081 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2082 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2083 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2084 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2085 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2086 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2087 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2088 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2089 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2090 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2091 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2092 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2093 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2094 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2095 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2096 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 2097 | psa_key_slot_t key, |
| 2098 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2099 | |
| 2100 | /** Set the key for a multipart symmetric decryption operation. |
| 2101 | * |
| 2102 | * The sequence of operations to decrypt a message with a symmetric cipher |
| 2103 | * is as follows: |
| 2104 | * -# Allocate an operation object which will be passed to all the functions |
| 2105 | * listed here. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2106 | * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2107 | * The key remains associated with the operation even if the content |
| 2108 | * of the key slot changes. |
| 2109 | * -# Call psa_cipher_update() with the IV (initialization vector) for the |
| 2110 | * decryption. If the IV is prepended to the ciphertext, you can call |
| 2111 | * psa_cipher_update() on a buffer containing the IV followed by the |
| 2112 | * beginning of the message. |
| 2113 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 2114 | * of the message each time. |
| 2115 | * -# Call psa_cipher_finish(). |
| 2116 | * |
| 2117 | * The application may call psa_cipher_abort() at any time after the operation |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2118 | * has been initialized with psa_cipher_decrypt_setup(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2119 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2120 | * After a successful call to psa_cipher_decrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 2121 | * eventually terminate the operation. The following events terminate an |
| 2122 | * operation: |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2123 | * - A failed call to psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 2124 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2125 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2126 | * \param[out] operation The operation object to use. |
| 2127 | * \param key Slot containing the key to use for the operation. |
| 2128 | * \param alg The cipher algorithm to compute |
| 2129 | * (\c PSA_ALG_XXX value such that |
| 2130 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2131 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2132 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2133 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2134 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2135 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2136 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2137 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2138 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2139 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2140 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2141 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2142 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2143 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2144 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2145 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 2146 | psa_key_slot_t key, |
| 2147 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2148 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2149 | /** Generate an IV for a symmetric encryption operation. |
| 2150 | * |
| 2151 | * This function generates a random IV (initialization vector), nonce |
| 2152 | * or initial counter value for the encryption operation as appropriate |
| 2153 | * for the chosen algorithm, key type and key size. |
| 2154 | * |
| 2155 | * The application must call psa_cipher_encrypt_setup() before |
| 2156 | * calling this function. |
| 2157 | * |
| 2158 | * If this function returns an error status, the operation becomes inactive. |
| 2159 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2160 | * \param[in,out] operation Active cipher operation. |
| 2161 | * \param[out] iv Buffer where the generated IV is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2162 | * \param iv_size Size of the \p iv buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2163 | * \param[out] iv_length On success, the number of bytes of the |
| 2164 | * generated IV. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2165 | * |
| 2166 | * \retval #PSA_SUCCESS |
| 2167 | * Success. |
| 2168 | * \retval #PSA_ERROR_BAD_STATE |
| 2169 | * The operation state is not valid (not started, or IV already set). |
| 2170 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2171 | * The size of the \p iv buffer is too small. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2172 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2173 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2174 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2175 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2176 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2177 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 2178 | unsigned char *iv, |
| 2179 | size_t iv_size, |
| 2180 | size_t *iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2181 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2182 | /** Set the IV for a symmetric encryption or decryption operation. |
| 2183 | * |
| 2184 | * This function sets the random IV (initialization vector), nonce |
| 2185 | * or initial counter value for the encryption or decryption operation. |
| 2186 | * |
| 2187 | * The application must call psa_cipher_encrypt_setup() before |
| 2188 | * calling this function. |
| 2189 | * |
| 2190 | * If this function returns an error status, the operation becomes inactive. |
| 2191 | * |
| 2192 | * \note When encrypting, applications should use psa_cipher_generate_iv() |
| 2193 | * instead of this function, unless implementing a protocol that requires |
| 2194 | * a non-random IV. |
| 2195 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2196 | * \param[in,out] operation Active cipher operation. |
| 2197 | * \param[in] iv Buffer containing the IV to use. |
| 2198 | * \param iv_length Size of the IV in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2199 | * |
| 2200 | * \retval #PSA_SUCCESS |
| 2201 | * Success. |
| 2202 | * \retval #PSA_ERROR_BAD_STATE |
| 2203 | * The operation state is not valid (not started, or IV already set). |
| 2204 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2205 | * The size of \p iv is not acceptable for the chosen algorithm, |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2206 | * or the chosen algorithm does not use an IV. |
| 2207 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2208 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2209 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2210 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2211 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2212 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 2213 | const unsigned char *iv, |
| 2214 | size_t iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2215 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2216 | /** Encrypt or decrypt a message fragment in an active cipher operation. |
| 2217 | * |
Gilles Peskine | 9ac9426 | 2018-07-12 20:15:32 +0200 | [diff] [blame] | 2218 | * Before calling this function, you must: |
| 2219 | * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(). |
| 2220 | * The choice of setup function determines whether this function |
| 2221 | * encrypts or decrypts its input. |
| 2222 | * 2. If the algorithm requires an IV, call psa_cipher_generate_iv() |
| 2223 | * (recommended when encrypting) or psa_cipher_set_iv(). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2224 | * |
| 2225 | * If this function returns an error status, the operation becomes inactive. |
| 2226 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2227 | * \param[in,out] operation Active cipher operation. |
| 2228 | * \param[in] input Buffer containing the message fragment to |
| 2229 | * encrypt or decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2230 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2231 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2232 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2233 | * \param[out] output_length On success, the number of bytes |
| 2234 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2235 | * |
| 2236 | * \retval #PSA_SUCCESS |
| 2237 | * Success. |
| 2238 | * \retval #PSA_ERROR_BAD_STATE |
| 2239 | * The operation state is not valid (not started, IV required but |
| 2240 | * not set, or already completed). |
| 2241 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 2242 | * The size of the \p output buffer is too small. |
| 2243 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2244 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2245 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2246 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2247 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2248 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 2249 | const uint8_t *input, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2250 | size_t input_length, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2251 | unsigned char *output, |
| 2252 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2253 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2254 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2255 | /** Finish encrypting or decrypting a message in a cipher operation. |
| 2256 | * |
| 2257 | * The application must call psa_cipher_encrypt_setup() or |
| 2258 | * psa_cipher_decrypt_setup() before calling this function. The choice |
| 2259 | * of setup function determines whether this function encrypts or |
| 2260 | * decrypts its input. |
| 2261 | * |
| 2262 | * This function finishes the encryption or decryption of the message |
| 2263 | * formed by concatenating the inputs passed to preceding calls to |
| 2264 | * psa_cipher_update(). |
| 2265 | * |
| 2266 | * When this function returns, the operation becomes inactive. |
| 2267 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2268 | * \param[in,out] operation Active cipher operation. |
| 2269 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2270 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2271 | * \param[out] output_length On success, the number of bytes |
| 2272 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2273 | * |
| 2274 | * \retval #PSA_SUCCESS |
| 2275 | * Success. |
| 2276 | * \retval #PSA_ERROR_BAD_STATE |
| 2277 | * The operation state is not valid (not started, IV required but |
| 2278 | * not set, or already completed). |
| 2279 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 2280 | * The size of the \p output buffer is too small. |
| 2281 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2282 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2283 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2284 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2285 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2286 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2287 | uint8_t *output, |
Moran Peker | 0071b87 | 2018-04-22 20:16:58 +0300 | [diff] [blame] | 2288 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2289 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2290 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2291 | /** Abort a cipher operation. |
| 2292 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2293 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2294 | * \p operation structure itself. Once aborted, the operation object |
| 2295 | * can be reused for another operation by calling |
| 2296 | * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2297 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2298 | * You may call this function any time after the operation object has |
| 2299 | * been initialized by any of the following methods: |
| 2300 | * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(), |
| 2301 | * whether it succeeds or not. |
| 2302 | * - Initializing the \c struct to all-bits-zero. |
| 2303 | * - Initializing the \c struct to logical zeros, e.g. |
| 2304 | * `psa_cipher_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2305 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2306 | * In particular, calling psa_cipher_abort() after the operation has been |
| 2307 | * terminated by a call to psa_cipher_abort() or psa_cipher_finish() |
| 2308 | * is safe and has no effect. |
| 2309 | * |
| 2310 | * \param[in,out] operation Initialized cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2311 | * |
| 2312 | * \retval #PSA_SUCCESS |
| 2313 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2314 | * \p operation is not an active cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2315 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2316 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2317 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2318 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2319 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); |
| 2320 | |
| 2321 | /**@}*/ |
| 2322 | |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2323 | /** \defgroup aead Authenticated encryption with associated data (AEAD) |
| 2324 | * @{ |
| 2325 | */ |
| 2326 | |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 2327 | /** The tag size for an AEAD algorithm, in bytes. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2328 | * |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 2329 | * \param alg An AEAD algorithm |
| 2330 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2331 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 2332 | * |
| 2333 | * \return The tag size for the specified algorithm. |
| 2334 | * If the AEAD algorithm does not have an identified |
| 2335 | * tag that can be distinguished from the rest of |
| 2336 | * the ciphertext, return 0. |
| 2337 | * If the AEAD algorithm is not recognized, return 0. |
| 2338 | * An implementation may return either 0 or a |
| 2339 | * correct size for an AEAD algorithm that it |
| 2340 | * recognizes, but does not support. |
| 2341 | */ |
| 2342 | #define PSA_AEAD_TAG_SIZE(alg) \ |
| 2343 | ((alg) == PSA_ALG_GCM ? 16 : \ |
| 2344 | (alg) == PSA_ALG_CCM ? 16 : \ |
| 2345 | 0) |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2346 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2347 | /** Process an authenticated encryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2348 | * |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2349 | * \param key Slot containing the key to use. |
| 2350 | * \param alg The AEAD algorithm to compute |
| 2351 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2352 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2353 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2354 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2355 | * \param[in] additional_data Additional data that will be authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2356 | * but not encrypted. |
| 2357 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2358 | * \param[in] plaintext Data that will be authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2359 | * encrypted. |
| 2360 | * \param plaintext_length Size of \p plaintext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2361 | * \param[out] ciphertext Output buffer for the authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2362 | * encrypted data. The additional data is not |
| 2363 | * part of this output. For algorithms where the |
| 2364 | * encrypted data and the authentication tag |
| 2365 | * are defined as separate outputs, the |
| 2366 | * authentication tag is appended to the |
| 2367 | * encrypted data. |
| 2368 | * \param ciphertext_size Size of the \p ciphertext buffer in bytes. |
| 2369 | * This must be at least |
| 2370 | * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, |
| 2371 | * \p plaintext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2372 | * \param[out] ciphertext_length On success, the size of the output |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2373 | * in the \b ciphertext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2374 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2375 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2376 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2377 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2378 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2379 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2380 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2381 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2382 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2383 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2384 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2385 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2386 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2387 | */ |
Gilles Peskine | 9fb0e01 | 2018-07-19 15:51:49 +0200 | [diff] [blame] | 2388 | psa_status_t psa_aead_encrypt(psa_key_slot_t key, |
| 2389 | psa_algorithm_t alg, |
| 2390 | const uint8_t *nonce, |
| 2391 | size_t nonce_length, |
| 2392 | const uint8_t *additional_data, |
| 2393 | size_t additional_data_length, |
| 2394 | const uint8_t *plaintext, |
| 2395 | size_t plaintext_length, |
| 2396 | uint8_t *ciphertext, |
| 2397 | size_t ciphertext_size, |
| 2398 | size_t *ciphertext_length); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2399 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2400 | /** Process an authenticated decryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2401 | * |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2402 | * \param key Slot containing the key to use. |
| 2403 | * \param alg The AEAD algorithm to compute |
| 2404 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2405 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2406 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2407 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2408 | * \param[in] additional_data Additional data that has been authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2409 | * but not encrypted. |
| 2410 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2411 | * \param[in] ciphertext Data that has been authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2412 | * encrypted. For algorithms where the |
| 2413 | * encrypted data and the authentication tag |
| 2414 | * are defined as separate inputs, the buffer |
| 2415 | * must contain the encrypted data followed |
| 2416 | * by the authentication tag. |
| 2417 | * \param ciphertext_length Size of \p ciphertext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2418 | * \param[out] plaintext Output buffer for the decrypted data. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2419 | * \param plaintext_size Size of the \p plaintext buffer in bytes. |
| 2420 | * This must be at least |
| 2421 | * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, |
| 2422 | * \p ciphertext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2423 | * \param[out] plaintext_length On success, the size of the output |
mohammad1603 | fb5b9cb | 2018-06-06 13:44:27 +0300 | [diff] [blame] | 2424 | * in the \b plaintext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2425 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2426 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2427 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2428 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2429 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2430 | * The ciphertext is not authentic. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2431 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2432 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2433 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2434 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2435 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2436 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2437 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2438 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2439 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2440 | */ |
Gilles Peskine | 9fb0e01 | 2018-07-19 15:51:49 +0200 | [diff] [blame] | 2441 | psa_status_t psa_aead_decrypt(psa_key_slot_t key, |
| 2442 | psa_algorithm_t alg, |
| 2443 | const uint8_t *nonce, |
| 2444 | size_t nonce_length, |
| 2445 | const uint8_t *additional_data, |
| 2446 | size_t additional_data_length, |
| 2447 | const uint8_t *ciphertext, |
| 2448 | size_t ciphertext_length, |
| 2449 | uint8_t *plaintext, |
| 2450 | size_t plaintext_size, |
| 2451 | size_t *plaintext_length); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2452 | |
| 2453 | /**@}*/ |
| 2454 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2455 | /** \defgroup asymmetric Asymmetric cryptography |
| 2456 | * @{ |
| 2457 | */ |
| 2458 | |
| 2459 | /** |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2460 | * \brief ECDSA signature size for a given curve bit size |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2461 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2462 | * \param curve_bits Curve size in bits. |
| 2463 | * \return Signature size in bytes. |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2464 | * |
| 2465 | * \note This macro returns a compile-time constant if its argument is one. |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2466 | */ |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2467 | #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ |
| 2468 | (PSA_BITS_TO_BYTES(curve_bits) * 2) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2469 | |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2470 | /** |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2471 | * \brief Sign a hash or short message with a private key. |
| 2472 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2473 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2474 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2475 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 2476 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 2477 | * to determine the hash algorithm to use. |
| 2478 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2479 | * \param key Key slot containing an asymmetric key pair. |
| 2480 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2481 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2482 | * \param[in] hash The hash or message to sign. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2483 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2484 | * \param[out] signature Buffer where the signature is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2485 | * \param signature_size Size of the \p signature buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2486 | * \param[out] signature_length On success, the number of bytes |
| 2487 | * that make up the returned signature value. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2488 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2489 | * \retval #PSA_SUCCESS |
| 2490 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2491 | * The size of the \p signature buffer is too small. You can |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2492 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2493 | * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2494 | * where \c key_type and \c key_bits are the type and bit-size |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2495 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2496 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2497 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2498 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2499 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2500 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2501 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2502 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2503 | */ |
| 2504 | psa_status_t psa_asymmetric_sign(psa_key_slot_t key, |
| 2505 | psa_algorithm_t alg, |
| 2506 | const uint8_t *hash, |
| 2507 | size_t hash_length, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2508 | uint8_t *signature, |
| 2509 | size_t signature_size, |
| 2510 | size_t *signature_length); |
| 2511 | |
| 2512 | /** |
| 2513 | * \brief Verify the signature a hash or short message using a public key. |
| 2514 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2515 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2516 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2517 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 2518 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 2519 | * to determine the hash algorithm to use. |
| 2520 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2521 | * \param key Key slot containing a public key or an |
| 2522 | * asymmetric key pair. |
| 2523 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2524 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2525 | * \param[in] hash The hash or message whose signature is to be |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2526 | * verified. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2527 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2528 | * \param[in] signature Buffer containing the signature to verify. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2529 | * \param signature_length Size of the \p signature buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2530 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2531 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2532 | * The signature is valid. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2533 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2534 | * The calculation was perfomed successfully, but the passed |
| 2535 | * signature is not a valid signature. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2536 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2537 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2538 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2539 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2540 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2541 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2542 | */ |
| 2543 | psa_status_t psa_asymmetric_verify(psa_key_slot_t key, |
| 2544 | psa_algorithm_t alg, |
| 2545 | const uint8_t *hash, |
| 2546 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 2547 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 2548 | size_t signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2549 | |
Gilles Peskine | 723feff | 2018-05-31 20:08:13 +0200 | [diff] [blame] | 2550 | #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 2551 | (PSA_ALG_IS_RSA_OAEP(alg) ? \ |
| 2552 | 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \ |
Gilles Peskine | 723feff | 2018-05-31 20:08:13 +0200 | [diff] [blame] | 2553 | 11 /*PKCS#1v1.5*/) |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2554 | |
| 2555 | /** |
| 2556 | * \brief Encrypt a short message with a public key. |
| 2557 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2558 | * \param key Key slot containing a public key or an |
| 2559 | * asymmetric key pair. |
| 2560 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2561 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2562 | * \param[in] input The message to encrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2563 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2564 | * \param[in] salt A salt or label, if supported by the |
| 2565 | * encryption algorithm. |
| 2566 | * If the algorithm does not support a |
| 2567 | * salt, pass \c NULL. |
| 2568 | * If the algorithm supports an optional |
| 2569 | * salt and you do not want to pass a salt, |
| 2570 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2571 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2572 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 2573 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2574 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2575 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2576 | * \param[out] output Buffer where the encrypted message is to |
| 2577 | * be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2578 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2579 | * \param[out] output_length On success, the number of bytes |
| 2580 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2581 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2582 | * \retval #PSA_SUCCESS |
| 2583 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2584 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2585 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2586 | * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2587 | * where \c key_type and \c key_bits are the type and bit-size |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2588 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2589 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2590 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2591 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2592 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2593 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2594 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2595 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2596 | */ |
| 2597 | psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key, |
| 2598 | psa_algorithm_t alg, |
| 2599 | const uint8_t *input, |
| 2600 | size_t input_length, |
| 2601 | const uint8_t *salt, |
| 2602 | size_t salt_length, |
| 2603 | uint8_t *output, |
| 2604 | size_t output_size, |
| 2605 | size_t *output_length); |
| 2606 | |
| 2607 | /** |
| 2608 | * \brief Decrypt a short message with a private key. |
| 2609 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2610 | * \param key Key slot containing an asymmetric key pair. |
| 2611 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2612 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2613 | * \param[in] input The message to decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2614 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2615 | * \param[in] salt A salt or label, if supported by the |
| 2616 | * encryption algorithm. |
| 2617 | * If the algorithm does not support a |
| 2618 | * salt, pass \c NULL. |
| 2619 | * If the algorithm supports an optional |
| 2620 | * salt and you do not want to pass a salt, |
| 2621 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2622 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2623 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 2624 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2625 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2626 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2627 | * \param[out] output Buffer where the decrypted message is to |
| 2628 | * be written. |
| 2629 | * \param output_size Size of the \c output buffer in bytes. |
| 2630 | * \param[out] output_length On success, the number of bytes |
| 2631 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2632 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2633 | * \retval #PSA_SUCCESS |
| 2634 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2635 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2636 | * determine a sufficient buffer size by calling |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2637 | * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2638 | * where \c key_type and \c key_bits are the type and bit-size |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2639 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2640 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2641 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2642 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2643 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2644 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2645 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2646 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2647 | * \retval #PSA_ERROR_INVALID_PADDING |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2648 | */ |
| 2649 | psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key, |
| 2650 | psa_algorithm_t alg, |
| 2651 | const uint8_t *input, |
| 2652 | size_t input_length, |
| 2653 | const uint8_t *salt, |
| 2654 | size_t salt_length, |
| 2655 | uint8_t *output, |
| 2656 | size_t output_size, |
| 2657 | size_t *output_length); |
| 2658 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2659 | /**@}*/ |
| 2660 | |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 2661 | /** \defgroup generators Generators |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2662 | * @{ |
| 2663 | */ |
| 2664 | |
| 2665 | /** The type of the state data structure for generators. |
| 2666 | * |
| 2667 | * Before calling any function on a generator, the application must |
| 2668 | * initialize it by any of the following means: |
| 2669 | * - Set the structure to all-bits-zero, for example: |
| 2670 | * \code |
| 2671 | * psa_crypto_generator_t generator; |
| 2672 | * memset(&generator, 0, sizeof(generator)); |
| 2673 | * \endcode |
| 2674 | * - Initialize the structure to logical zero values, for example: |
| 2675 | * \code |
| 2676 | * psa_crypto_generator_t generator = {0}; |
| 2677 | * \endcode |
| 2678 | * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT, |
| 2679 | * for example: |
| 2680 | * \code |
| 2681 | * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 2682 | * \endcode |
| 2683 | * - Assign the result of the function psa_crypto_generator_init() |
| 2684 | * to the structure, for example: |
| 2685 | * \code |
| 2686 | * psa_crypto_generator_t generator; |
| 2687 | * generator = psa_crypto_generator_init(); |
| 2688 | * \endcode |
| 2689 | * |
| 2690 | * This is an implementation-defined \c struct. Applications should not |
| 2691 | * make any assumptions about the content of this structure except |
| 2692 | * as directed by the documentation of a specific implementation. |
| 2693 | */ |
| 2694 | typedef struct psa_crypto_generator_s psa_crypto_generator_t; |
| 2695 | |
| 2696 | /** \def PSA_CRYPTO_GENERATOR_INIT |
| 2697 | * |
| 2698 | * This macro returns a suitable initializer for a generator object |
| 2699 | * of type #psa_crypto_generator_t. |
| 2700 | */ |
| 2701 | #ifdef __DOXYGEN_ONLY__ |
| 2702 | /* This is an example definition for documentation purposes. |
| 2703 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 2704 | */ |
| 2705 | #define PSA_CRYPTO_GENERATOR_INIT {0} |
| 2706 | #endif |
| 2707 | |
| 2708 | /** Return an initial value for a generator object. |
| 2709 | */ |
| 2710 | static psa_crypto_generator_t psa_crypto_generator_init(void); |
| 2711 | |
| 2712 | /** Retrieve the current capacity of a generator. |
| 2713 | * |
| 2714 | * The capacity of a generator is the maximum number of bytes that it can |
| 2715 | * return. Reading *N* bytes from a generator reduces its capacity by *N*. |
| 2716 | * |
| 2717 | * \param[in] generator The generator to query. |
| 2718 | * \param[out] capacity On success, the capacity of the generator. |
| 2719 | * |
| 2720 | * \retval PSA_SUCCESS |
| 2721 | * \retval PSA_ERROR_BAD_STATE |
| 2722 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 2723 | */ |
| 2724 | psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator, |
| 2725 | size_t *capacity); |
| 2726 | |
| 2727 | /** Read some data from a generator. |
| 2728 | * |
| 2729 | * This function reads and returns a sequence of bytes from a generator. |
| 2730 | * The data that is read is discarded from the generator. The generator's |
| 2731 | * capacity is decreased by the number of bytes read. |
| 2732 | * |
| 2733 | * \param[in,out] generator The generator object to read from. |
| 2734 | * \param[out] output Buffer where the generator output will be |
| 2735 | * written. |
| 2736 | * \param output_length Number of bytes to output. |
| 2737 | * |
| 2738 | * \retval PSA_SUCCESS |
| 2739 | * \retval PSA_ERROR_INSUFFICIENT_CAPACITY |
| 2740 | * There were fewer than \p output_length bytes |
| 2741 | * in the generator. Note that in this case, no |
| 2742 | * output is written to the output buffer. |
| 2743 | * The generator's capacity is set to 0, thus |
| 2744 | * subsequent calls to this function will not |
| 2745 | * succeed, even with a smaller output buffer. |
| 2746 | * \retval PSA_ERROR_BAD_STATE |
| 2747 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 2748 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 2749 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 2750 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 2751 | */ |
| 2752 | psa_status_t psa_generator_read(psa_crypto_generator_t *generator, |
| 2753 | uint8_t *output, |
| 2754 | size_t output_length); |
| 2755 | |
| 2756 | /** Create a symmetric key from data read from a generator. |
| 2757 | * |
| 2758 | * This function reads a sequence of bytes from a generator and imports |
| 2759 | * these bytes as a key. |
| 2760 | * The data that is read is discarded from the generator. The generator's |
| 2761 | * capacity is decreased by the number of bytes read. |
| 2762 | * |
| 2763 | * This function is equivalent to calling #psa_generator_read and |
| 2764 | * passing the resulting output to #psa_import_key, but |
| 2765 | * if the implementation provides an isolation boundary then |
| 2766 | * the key material is not exposed outside the isolation boundary. |
| 2767 | * |
| 2768 | * \param key Slot where the key will be stored. This must be a |
| 2769 | * valid slot for a key of the chosen type. It must |
| 2770 | * be unoccupied. |
| 2771 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 2772 | * This must be a symmetric key type. |
| 2773 | * \param bits Key size in bits. |
| 2774 | * \param[in,out] generator The generator object to read from. |
| 2775 | * |
| 2776 | * \retval PSA_SUCCESS |
| 2777 | * Success. |
| 2778 | * \retval PSA_ERROR_INSUFFICIENT_CAPACITY |
| 2779 | * There were fewer than \p output_length bytes |
| 2780 | * in the generator. Note that in this case, no |
| 2781 | * output is written to the output buffer. |
| 2782 | * The generator's capacity is set to 0, thus |
| 2783 | * subsequent calls to this function will not |
| 2784 | * succeed, even with a smaller output buffer. |
| 2785 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 2786 | * The key type or key size is not supported, either by the |
| 2787 | * implementation in general or in this particular slot. |
| 2788 | * \retval PSA_ERROR_BAD_STATE |
| 2789 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 2790 | * The key slot is invalid. |
| 2791 | * \retval PSA_ERROR_OCCUPIED_SLOT |
| 2792 | * There is already a key in the specified slot. |
| 2793 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 2794 | * \retval PSA_ERROR_INSUFFICIENT_STORAGE |
| 2795 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 2796 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 2797 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 2798 | */ |
| 2799 | psa_status_t psa_generator_import_key(psa_key_slot_t key, |
| 2800 | psa_key_type_t type, |
| 2801 | size_t bits, |
| 2802 | psa_crypto_generator_t *generator); |
| 2803 | |
| 2804 | /** Abort a generator. |
| 2805 | * |
| 2806 | * Once a generator has been aborted, its capacity is zero. |
| 2807 | * Aborting a generator frees all associated resources except for the |
| 2808 | * \c generator structure itself. |
| 2809 | * |
| 2810 | * This function may be called at any time as long as the generator |
| 2811 | * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to |
| 2812 | * psa_crypto_generator_init() or a zero value. In particular, it is valid |
| 2813 | * to call psa_generator_abort() twice, or to call psa_generator_abort() |
| 2814 | * on a generator that has not been set up. |
| 2815 | * |
| 2816 | * Once aborted, the generator object may be called. |
| 2817 | * |
| 2818 | * \param[in,out] generator The generator to abort. |
| 2819 | * |
| 2820 | * \retval PSA_SUCCESS |
| 2821 | * \retval PSA_ERROR_BAD_STATE |
| 2822 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 2823 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 2824 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 2825 | */ |
| 2826 | psa_status_t psa_generator_abort(psa_crypto_generator_t *generator); |
| 2827 | |
| 2828 | /**@}*/ |
| 2829 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2830 | /** \defgroup derivation Key derivation |
| 2831 | * @{ |
| 2832 | */ |
| 2833 | |
| 2834 | /** Set up a key derivation operation. |
| 2835 | * |
| 2836 | * A key derivation algorithm takes three inputs: a secret input \p key and |
| 2837 | * two non-secret inputs \p label and p salt. |
| 2838 | * The result of this function is a byte generator which can |
| 2839 | * be used to produce keys and other cryptographic material. |
| 2840 | * |
| 2841 | * The role of \p label and \p salt is as follows: |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 2842 | * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step |
| 2843 | * and \p label is the info string used in the "expand" step. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2844 | * |
| 2845 | * \param[in,out] generator The generator object to set up. It must |
| 2846 | * have been initialized to . |
| 2847 | * \param key Slot containing the secret key to use. |
| 2848 | * \param alg The key derivation algorithm to compute |
| 2849 | * (\c PSA_ALG_XXX value such that |
| 2850 | * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true). |
| 2851 | * \param[in] salt Salt to use. |
| 2852 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2853 | * \param[in] label Label to use. |
| 2854 | * \param label_length Size of the \p label buffer in bytes. |
| 2855 | * \param capacity The maximum number of bytes that the |
| 2856 | * generator will be able to provide. |
| 2857 | * |
| 2858 | * \retval #PSA_SUCCESS |
| 2859 | * Success. |
| 2860 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2861 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2862 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2863 | * \c key is not compatible with \c alg, |
| 2864 | * or \p capacity is too large for the specified algorithm and key. |
| 2865 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2866 | * \c alg is not supported or is not a key derivation algorithm. |
| 2867 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2868 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2869 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2870 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2871 | */ |
| 2872 | psa_status_t psa_key_derivation(psa_crypto_generator_t *generator, |
Darryl Green | 8800136 | 2018-07-26 13:59:04 +0100 | [diff] [blame] | 2873 | psa_key_slot_t key, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2874 | psa_algorithm_t alg, |
| 2875 | const uint8_t *salt, |
| 2876 | size_t salt_length, |
| 2877 | const uint8_t *label, |
| 2878 | size_t label_length, |
| 2879 | size_t capacity); |
| 2880 | |
| 2881 | /**@}*/ |
| 2882 | |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 2883 | /** \defgroup random Random generation |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2884 | * @{ |
| 2885 | */ |
| 2886 | |
| 2887 | /** |
| 2888 | * \brief Generate random bytes. |
| 2889 | * |
| 2890 | * \warning This function **can** fail! Callers MUST check the return status |
| 2891 | * and MUST NOT use the content of the output buffer if the return |
| 2892 | * status is not #PSA_SUCCESS. |
| 2893 | * |
| 2894 | * \note To generate a key, use psa_generate_key() instead. |
| 2895 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2896 | * \param[out] output Output buffer for the generated data. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2897 | * \param output_size Number of bytes to generate and output. |
| 2898 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2899 | * \retval #PSA_SUCCESS |
| 2900 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2901 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2902 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2903 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2904 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2905 | */ |
| 2906 | psa_status_t psa_generate_random(uint8_t *output, |
| 2907 | size_t output_size); |
| 2908 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2909 | /** Extra parameters for RSA key generation. |
| 2910 | * |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 2911 | * You may pass a pointer to a structure of this type as the \c extra |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2912 | * parameter to psa_generate_key(). |
| 2913 | */ |
| 2914 | typedef struct { |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 2915 | uint32_t e; /**< Public exponent value. Default: 65537. */ |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2916 | } psa_generate_key_extra_rsa; |
| 2917 | |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2918 | /** |
| 2919 | * \brief Generate a key or key pair. |
| 2920 | * |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 2921 | * \param key Slot where the key will be stored. This must be a |
| 2922 | * valid slot for a key of the chosen type. It must |
| 2923 | * be unoccupied. |
| 2924 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 2925 | * \param bits Key size in bits. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2926 | * \param[in] extra Extra parameters for key generation. The |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 2927 | * interpretation of this parameter depends on |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2928 | * \p type. All types support \c NULL to use |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2929 | * default parameters. Implementation that support |
| 2930 | * the generation of vendor-specific key types |
| 2931 | * that allow extra parameters shall document |
| 2932 | * the format of these extra parameters and |
| 2933 | * the default values. For standard parameters, |
| 2934 | * the meaning of \p extra is as follows: |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2935 | * - For a symmetric key type (a type such |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2936 | * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is |
| 2937 | * false), \p extra must be \c NULL. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2938 | * - For an elliptic curve key type (a type |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2939 | * such that #PSA_KEY_TYPE_IS_ECC(\p type) is |
| 2940 | * false), \p extra must be \c NULL. |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2941 | * - For an RSA key (\p type is |
| 2942 | * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an |
| 2943 | * optional #psa_generate_key_extra_rsa structure |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2944 | * specifying the public exponent. The |
| 2945 | * default public exponent used when \p extra |
| 2946 | * is \c NULL is 65537. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2947 | * \param extra_size Size of the buffer that \p extra |
| 2948 | * points to, in bytes. Note that if \p extra is |
| 2949 | * \c NULL then \p extra_size must be zero. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2950 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2951 | * \retval #PSA_SUCCESS |
| 2952 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2953 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2954 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2955 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2956 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2957 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2958 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2959 | */ |
| 2960 | psa_status_t psa_generate_key(psa_key_slot_t key, |
| 2961 | psa_key_type_t type, |
| 2962 | size_t bits, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2963 | const void *extra, |
| 2964 | size_t extra_size); |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2965 | |
| 2966 | /**@}*/ |
| 2967 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2968 | #ifdef __cplusplus |
| 2969 | } |
| 2970 | #endif |
| 2971 | |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 2972 | /* The file "crypto_sizes.h" contains definitions for size calculation |
| 2973 | * macros whose definitions are implementation-specific. */ |
| 2974 | #include "crypto_sizes.h" |
| 2975 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2976 | /* The file "crypto_struct.h" contains definitions for |
| 2977 | * implementation-specific structs that are declared above. */ |
| 2978 | #include "crypto_struct.h" |
| 2979 | |
| 2980 | /* The file "crypto_extra.h" contains vendor-specific definitions. This |
| 2981 | * can include vendor-defined algorithms, extra functions, etc. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2982 | #include "crypto_extra.h" |
| 2983 | |
| 2984 | #endif /* PSA_CRYPTO_H */ |