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 | |
Gilles Peskine | f535eb2 | 2018-11-30 14:08:36 +0100 | [diff] [blame] | 317 | /** The key handle is not valid. |
| 318 | */ |
| 319 | #define PSA_ERROR_INVALID_HANDLE ((psa_status_t)19) |
| 320 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 321 | /** |
| 322 | * \brief Library initialization. |
| 323 | * |
| 324 | * Applications must call this function before calling any other |
| 325 | * function in this module. |
| 326 | * |
| 327 | * Applications may call this function more than once. Once a call |
| 328 | * succeeds, subsequent calls are guaranteed to succeed. |
| 329 | * |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 330 | * If the application calls other functions before calling psa_crypto_init(), |
| 331 | * the behavior is undefined. Implementations are encouraged to either perform |
| 332 | * the operation as if the library had been initialized or to return |
| 333 | * #PSA_ERROR_BAD_STATE or some other applicable error. In particular, |
| 334 | * implementations should not return a success status if the lack of |
| 335 | * initialization may have security implications, for example due to improper |
| 336 | * seeding of the random number generator. |
| 337 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 338 | * \retval #PSA_SUCCESS |
| 339 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 340 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 341 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 342 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 343 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 344 | */ |
| 345 | psa_status_t psa_crypto_init(void); |
| 346 | |
Gilles Peskine | 2905a7a | 2018-03-07 16:39:31 +0100 | [diff] [blame] | 347 | #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8) |
| 348 | #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 349 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 350 | /**@}*/ |
| 351 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 352 | /** \defgroup crypto_types Key and algorithm types |
| 353 | * @{ |
| 354 | */ |
| 355 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 356 | /** \brief Encoding of a key type. |
| 357 | */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 358 | typedef uint32_t psa_key_type_t; |
| 359 | |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 360 | /** An invalid key type value. |
| 361 | * |
| 362 | * Zero is not the encoding of any key type. |
| 363 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 364 | #define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000) |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 365 | |
| 366 | /** Vendor-defined flag |
| 367 | * |
| 368 | * Key types defined by this standard will never have the |
| 369 | * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types |
| 370 | * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should |
| 371 | * respect the bitwise structure used by standard encodings whenever practical. |
| 372 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 373 | #define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 374 | |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 375 | #define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000) |
| 376 | #define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000) |
| 377 | #define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000) |
| 378 | #define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000) |
| 379 | #define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000) |
| 380 | |
| 381 | #define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 382 | |
Gilles Peskine | e877974 | 2018-08-10 16:10:56 +0200 | [diff] [blame] | 383 | /** Whether a key type is vendor-defined. */ |
| 384 | #define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \ |
| 385 | (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0) |
| 386 | |
| 387 | /** Whether a key type is an unstructured array of bytes. |
| 388 | * |
| 389 | * This encompasses both symmetric keys and non-key data. |
| 390 | */ |
| 391 | #define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \ |
| 392 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \ |
| 393 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) |
| 394 | |
| 395 | /** Whether a key type is asymmetric: either a key pair or a public key. */ |
| 396 | #define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \ |
| 397 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK \ |
| 398 | & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \ |
| 399 | PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY) |
| 400 | /** Whether a key type is the public part of a key pair. */ |
| 401 | #define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \ |
| 402 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY) |
| 403 | /** Whether a key type is a key pair containing a private part and a public |
| 404 | * part. */ |
| 405 | #define PSA_KEY_TYPE_IS_KEYPAIR(type) \ |
| 406 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR) |
| 407 | /** The key pair type corresponding to a public key 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 key pair 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_KEYPAIR_OF_PUBLIC_KEY(type) \ |
| 418 | ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) |
| 419 | /** The public key type corresponding to a key pair type. |
| 420 | * |
| 421 | * You may also pass a key pair type as \p type, it will be left unchanged. |
| 422 | * |
| 423 | * \param type A public key type or key pair type. |
| 424 | * |
| 425 | * \return The corresponding public key type. |
| 426 | * If \p type is not a public key or a key pair, |
| 427 | * the return value is undefined. |
| 428 | */ |
| 429 | #define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \ |
| 430 | ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) |
Gilles Peskine | e877974 | 2018-08-10 16:10:56 +0200 | [diff] [blame] | 431 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 432 | /** Raw data. |
| 433 | * |
| 434 | * A "key" of this type cannot be used for any cryptographic operation. |
| 435 | * Applications may use this type to store arbitrary data in the keystore. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 436 | #define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 437 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 438 | /** HMAC key. |
| 439 | * |
| 440 | * The key policy determines which underlying hash algorithm the key can be |
| 441 | * used for. |
| 442 | * |
| 443 | * HMAC keys should generally have the same size as the underlying hash. |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 444 | * This size can be calculated with #PSA_HASH_SIZE(\c alg) where |
| 445 | * \c alg is the HMAC algorithm or the underlying hash algorithm. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 446 | #define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 447 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 448 | /** A secret for key derivation. |
| 449 | * |
| 450 | * The key policy determines which key derivation algorithm the key |
| 451 | * can be used for. |
| 452 | */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 453 | #define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 454 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 455 | /** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher. |
| 456 | * |
| 457 | * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or |
| 458 | * 32 bytes (AES-256). |
| 459 | */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 460 | #define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 461 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 462 | /** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES). |
| 463 | * |
| 464 | * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or |
| 465 | * 24 bytes (3-key 3DES). |
| 466 | * |
| 467 | * Note that single DES and 2-key 3DES are weak and strongly |
| 468 | * deprecated and should only be used to decrypt legacy data. 3-key 3DES |
| 469 | * is weak and deprecated and should only be used in legacy protocols. |
| 470 | */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 471 | #define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 472 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 473 | /** Key for an cipher, AEAD or MAC algorithm based on the |
| 474 | * Camellia block cipher. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 475 | #define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 476 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 477 | /** Key for the RC4 stream cipher. |
| 478 | * |
| 479 | * Note that RC4 is weak and deprecated and should only be used in |
| 480 | * legacy protocols. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 481 | #define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 482 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 483 | /** RSA public key. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 484 | #define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 485 | /** RSA key pair (private and public key). */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 486 | #define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000) |
Gilles Peskine | 583b55d | 2018-08-22 18:21:32 +0200 | [diff] [blame] | 487 | /** Whether a key type is an RSA key (pair or public-only). */ |
| 488 | #define PSA_KEY_TYPE_IS_RSA(type) \ |
| 489 | (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 490 | |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 491 | /** DSA public key. */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 492 | #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 493 | /** DSA key pair (private and public key). */ |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 494 | #define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000) |
Gilles Peskine | 583b55d | 2018-08-22 18:21:32 +0200 | [diff] [blame] | 495 | /** Whether a key type is an DSA key (pair or public-only). */ |
| 496 | #define PSA_KEY_TYPE_IS_DSA(type) \ |
| 497 | (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 498 | |
Gilles Peskine | 78b3bb6 | 2018-08-10 16:03:41 +0200 | [diff] [blame] | 499 | #define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000) |
| 500 | #define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 501 | #define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 502 | /** Elliptic curve key pair. */ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 503 | #define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \ |
| 504 | (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve)) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 505 | /** Elliptic curve public key. */ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 506 | #define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \ |
| 507 | (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve)) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 508 | |
Gilles Peskine | d8008d6 | 2018-06-29 19:51:51 +0200 | [diff] [blame] | 509 | /** 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] | 510 | #define PSA_KEY_TYPE_IS_ECC(type) \ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 511 | ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \ |
| 512 | ~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] | 513 | #define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \ |
| 514 | (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \ |
| 515 | PSA_KEY_TYPE_ECC_KEYPAIR_BASE) |
| 516 | #define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \ |
| 517 | (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \ |
| 518 | PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 519 | |
Gilles Peskine | e1fed0d | 2018-06-18 20:45:45 +0200 | [diff] [blame] | 520 | /** The type of PSA elliptic curve identifiers. */ |
| 521 | typedef uint16_t psa_ecc_curve_t; |
| 522 | /** Extract the curve from an elliptic curve key type. */ |
| 523 | #define PSA_KEY_TYPE_GET_CURVE(type) \ |
| 524 | ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \ |
| 525 | ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \ |
| 526 | 0)) |
| 527 | |
| 528 | /* The encoding of curve identifiers is currently aligned with the |
| 529 | * TLS Supported Groups Registry (formerly known as the |
| 530 | * TLS EC Named Curve Registry) |
| 531 | * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8 |
Gilles Peskine | 70ce2c6 | 2018-08-22 18:21:57 +0200 | [diff] [blame] | 532 | * The values are defined by RFC 8422 and RFC 7027. */ |
Gilles Peskine | e1fed0d | 2018-06-18 20:45:45 +0200 | [diff] [blame] | 533 | #define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001) |
| 534 | #define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002) |
| 535 | #define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003) |
| 536 | #define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004) |
| 537 | #define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005) |
| 538 | #define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006) |
| 539 | #define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007) |
| 540 | #define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008) |
| 541 | #define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009) |
| 542 | #define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a) |
| 543 | #define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b) |
| 544 | #define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c) |
| 545 | #define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d) |
| 546 | #define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e) |
| 547 | #define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f) |
| 548 | #define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010) |
| 549 | #define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011) |
| 550 | #define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012) |
| 551 | #define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013) |
| 552 | #define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014) |
| 553 | #define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015) |
| 554 | #define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016) |
| 555 | #define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017) |
| 556 | #define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018) |
| 557 | #define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019) |
| 558 | #define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a) |
| 559 | #define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b) |
| 560 | #define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c) |
| 561 | #define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d) |
| 562 | #define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e) |
Gilles Peskine | e1fed0d | 2018-06-18 20:45:45 +0200 | [diff] [blame] | 563 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 564 | /** The block size of a block cipher. |
| 565 | * |
| 566 | * \param type A cipher key type (value of type #psa_key_type_t). |
| 567 | * |
| 568 | * \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] | 569 | * The return value is undefined if \p type is not a supported |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 570 | * cipher key type. |
| 571 | * |
| 572 | * \note It is possible to build stream cipher algorithms on top of a block |
| 573 | * cipher, for example CTR mode (#PSA_ALG_CTR). |
| 574 | * This macro only takes the key type into account, so it cannot be |
| 575 | * used to determine the size of the data that #psa_cipher_update() |
| 576 | * might buffer for future processing in general. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 577 | * |
| 578 | * \note This macro returns a compile-time constant if its argument is one. |
| 579 | * |
| 580 | * \warning This macro may evaluate its argument multiple times. |
| 581 | */ |
Gilles Peskine | 03182e9 | 2018-03-07 16:40:52 +0100 | [diff] [blame] | 582 | #define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 583 | ( \ |
| 584 | (type) == PSA_KEY_TYPE_AES ? 16 : \ |
| 585 | (type) == PSA_KEY_TYPE_DES ? 8 : \ |
| 586 | (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \ |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 587 | (type) == PSA_KEY_TYPE_ARC4 ? 1 : \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 588 | 0) |
| 589 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 590 | /** \brief Encoding of a cryptographic algorithm. |
| 591 | * |
| 592 | * For algorithms that can be applied to multiple key types, this type |
| 593 | * does not encode the key type. For example, for symmetric ciphers |
| 594 | * based on a block cipher, #psa_algorithm_t encodes the block cipher |
| 595 | * mode and the padding mode while the block cipher itself is encoded |
| 596 | * via #psa_key_type_t. |
| 597 | */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 598 | typedef uint32_t psa_algorithm_t; |
| 599 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 600 | #define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000) |
| 601 | #define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000) |
| 602 | #define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000) |
| 603 | #define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000) |
| 604 | #define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000) |
| 605 | #define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000) |
| 606 | #define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000) |
| 607 | #define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000) |
| 608 | #define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000) |
| 609 | #define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000) |
Gilles Peskine | e8f0e3d | 2018-09-18 11:52:10 +0200 | [diff] [blame] | 610 | #define PSA_ALG_CATEGORY_KEY_SELECTION ((psa_algorithm_t)0x31000000) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 611 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 612 | #define PSA_ALG_IS_VENDOR_DEFINED(alg) \ |
| 613 | (((alg) & PSA_ALG_VENDOR_FLAG) != 0) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 614 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 615 | /** Whether the specified algorithm is a hash algorithm. |
| 616 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 617 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 618 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 619 | * \return 1 if \p alg is a hash algorithm, 0 otherwise. |
| 620 | * 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] | 621 | * algorithm identifier. |
| 622 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 623 | #define PSA_ALG_IS_HASH(alg) \ |
| 624 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 625 | |
| 626 | /** Whether the specified algorithm is a MAC 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 MAC 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_MAC(alg) \ |
| 635 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 636 | |
| 637 | /** Whether the specified algorithm is a symmetric cipher algorithm. |
| 638 | * |
| 639 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 640 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 641 | * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise. |
| 642 | * 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] | 643 | * algorithm identifier. |
| 644 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 645 | #define PSA_ALG_IS_CIPHER(alg) \ |
| 646 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 647 | |
| 648 | /** Whether the specified algorithm is an authenticated encryption |
| 649 | * with associated data (AEAD) 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 an AEAD 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_AEAD(alg) \ |
| 658 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 659 | |
| 660 | /** Whether the specified algorithm is a public-key signature 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 signature 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_SIGN(alg) \ |
| 669 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 670 | |
| 671 | /** Whether the specified algorithm is a public-key encryption 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 public-key encryption 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_ASYMMETRIC_ENCRYPTION(alg) \ |
| 680 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 681 | |
Gilles Peskine | e8f0e3d | 2018-09-18 11:52:10 +0200 | [diff] [blame] | 682 | #define PSA_ALG_KEY_SELECTION_FLAG ((psa_algorithm_t)0x01000000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 683 | /** Whether the specified algorithm is a key agreement algorithm. |
| 684 | * |
| 685 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 686 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 687 | * \return 1 if \p alg is a key agreement algorithm, 0 otherwise. |
| 688 | * 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] | 689 | * algorithm identifier. |
| 690 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 691 | #define PSA_ALG_IS_KEY_AGREEMENT(alg) \ |
Gilles Peskine | e8f0e3d | 2018-09-18 11:52:10 +0200 | [diff] [blame] | 692 | (((alg) & PSA_ALG_CATEGORY_MASK & ~PSA_ALG_KEY_SELECTION_FLAG) == \ |
| 693 | PSA_ALG_CATEGORY_KEY_AGREEMENT) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 694 | |
| 695 | /** Whether the specified algorithm is a key derivation algorithm. |
| 696 | * |
| 697 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 698 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 699 | * \return 1 if \p alg is a key derivation algorithm, 0 otherwise. |
| 700 | * 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] | 701 | * algorithm identifier. |
| 702 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 703 | #define PSA_ALG_IS_KEY_DERIVATION(alg) \ |
| 704 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION) |
| 705 | |
Gilles Peskine | e8f0e3d | 2018-09-18 11:52:10 +0200 | [diff] [blame] | 706 | /** Whether the specified algorithm is a key selection algorithm. |
| 707 | * |
| 708 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 709 | * |
| 710 | * \return 1 if \p alg is a key selection algorithm, 0 otherwise. |
| 711 | * This macro may return either 0 or 1 if \p alg is not a supported |
| 712 | * algorithm identifier. |
| 713 | */ |
| 714 | #define PSA_ALG_IS_KEY_SELECTION(alg) \ |
| 715 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_SELECTION) |
| 716 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 717 | #define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff) |
| 718 | #define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001) |
| 719 | #define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002) |
| 720 | #define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003) |
Gilles Peskine | e3f694f | 2018-03-08 07:48:40 +0100 | [diff] [blame] | 721 | #define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004) |
| 722 | #define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 723 | /** SHA2-224 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 724 | #define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 725 | /** SHA2-256 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 726 | #define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 727 | /** SHA2-384 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 728 | #define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 729 | /** SHA2-512 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 730 | #define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 731 | /** SHA2-512/224 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 732 | #define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 733 | /** SHA2-512/256 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 734 | #define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 735 | /** SHA3-224 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 736 | #define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 737 | /** SHA3-256 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 738 | #define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 739 | /** SHA3-384 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 740 | #define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012) |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 741 | /** SHA3-512 */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 742 | #define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013) |
| 743 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 744 | #define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 745 | #define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000) |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 746 | /** Macro to build an HMAC algorithm. |
| 747 | * |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 748 | * 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] | 749 | * |
Gilles Peskine | ea4469f | 2018-06-28 13:57:23 +0200 | [diff] [blame] | 750 | * \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] | 751 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 752 | * |
Gilles Peskine | ea4469f | 2018-06-28 13:57:23 +0200 | [diff] [blame] | 753 | * \return The corresponding HMAC algorithm. |
| 754 | * \return Unspecified if \p alg is not a supported |
| 755 | * hash algorithm. |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 756 | */ |
| 757 | #define PSA_ALG_HMAC(hash_alg) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 758 | (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 759 | |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 760 | #define PSA_ALG_HMAC_GET_HASH(hmac_alg) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 761 | (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK)) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 762 | |
| 763 | /** Whether the specified algorithm is an HMAC algorithm. |
| 764 | * |
| 765 | * HMAC is a family of MAC algorithms that are based on a hash function. |
| 766 | * |
| 767 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 768 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 769 | * \return 1 if \p alg is an HMAC algorithm, 0 otherwise. |
| 770 | * 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] | 771 | * algorithm identifier. |
| 772 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 773 | #define PSA_ALG_IS_HMAC(alg) \ |
| 774 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \ |
| 775 | PSA_ALG_HMAC_BASE) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 776 | |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 777 | /* In the encoding of a MAC algorithm, the bits corresponding to |
| 778 | * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is |
| 779 | * truncated. As an exception, the value 0 means the untruncated algorithm, |
| 780 | * whatever its length is. The length is encoded in 6 bits, so it can |
| 781 | * reach up to 63; the largest MAC is 64 bytes so its trivial truncation |
| 782 | * to full length is correctly encoded as 0 and any non-trivial truncation |
| 783 | * is correctly encoded as a value between 1 and 63. */ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 784 | #define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x00003f00) |
| 785 | #define PSA_MAC_TRUNCATION_OFFSET 8 |
| 786 | |
| 787 | /** Macro to build a truncated MAC algorithm. |
| 788 | * |
| 789 | * A truncated MAC algorithm is identical to the corresponding MAC |
| 790 | * algorithm except that the MAC value for the truncated algorithm |
| 791 | * consists of only the first \p mac_length bytes of the MAC value |
| 792 | * for the untruncated algorithm. |
| 793 | * |
| 794 | * \note This macro may allow constructing algorithm identifiers that |
| 795 | * are not valid, either because the specified length is larger |
| 796 | * than the untruncated MAC or because the specified length is |
| 797 | * smaller than permitted by the implementation. |
| 798 | * |
| 799 | * \note It is implementation-defined whether a truncated MAC that |
| 800 | * is truncated to the same length as the MAC of the untruncated |
| 801 | * algorithm is considered identical to the untruncated algorithm |
| 802 | * for policy comparison purposes. |
| 803 | * |
| 804 | * \param alg A MAC algorithm identifier (value of type |
| 805 | * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg) |
| 806 | * is true). This may be a truncated or untruncated |
| 807 | * MAC algorithm. |
| 808 | * \param mac_length Desired length of the truncated MAC in bytes. |
Gilles Peskine | 6d72ff9 | 2018-08-21 14:55:08 +0200 | [diff] [blame] | 809 | * This must be at most the full length of the MAC |
| 810 | * and must be at least an implementation-specified |
| 811 | * minimum. The implementation-specified minimum |
| 812 | * shall not be zero. |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 813 | * |
| 814 | * \return The corresponding MAC algorithm with the specified |
| 815 | * length. |
| 816 | * \return Unspecified if \p alg is not a supported |
| 817 | * MAC algorithm or if \p mac_length is too small or |
| 818 | * too large for the specified MAC algorithm. |
| 819 | */ |
| 820 | #define PSA_ALG_TRUNCATED_MAC(alg, mac_length) \ |
| 821 | (((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \ |
| 822 | ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK)) |
| 823 | |
Gilles Peskine | e0e9c7c | 2018-10-17 18:28:05 +0200 | [diff] [blame] | 824 | /** Macro to build the base MAC algorithm corresponding to a truncated |
| 825 | * MAC algorithm. |
| 826 | * |
| 827 | * \param alg A MAC algorithm identifier (value of type |
| 828 | * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg) |
| 829 | * is true). This may be a truncated or untruncated |
| 830 | * MAC algorithm. |
| 831 | * |
| 832 | * \return The corresponding base MAC algorithm. |
| 833 | * \return Unspecified if \p alg is not a supported |
| 834 | * MAC algorithm. |
| 835 | */ |
| 836 | #define PSA_ALG_FULL_LENGTH_MAC(alg) \ |
| 837 | ((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) |
| 838 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 839 | /** Length to which a MAC algorithm is truncated. |
| 840 | * |
| 841 | * \param alg A MAC algorithm identifier (value of type |
| 842 | * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg) |
| 843 | * is true). |
| 844 | * |
| 845 | * \return Length of the truncated MAC in bytes. |
| 846 | * \return 0 if \p alg is a non-truncated MAC algorithm. |
| 847 | * \return Unspecified if \p alg is not a supported |
| 848 | * MAC algorithm. |
| 849 | */ |
| 850 | #define PSA_MAC_TRUNCATED_LENGTH(alg) \ |
| 851 | (((alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET) |
| 852 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 853 | #define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000) |
| 854 | #define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001) |
| 855 | #define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002) |
| 856 | #define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 857 | |
| 858 | /** Whether the specified algorithm is a MAC algorithm based on a block cipher. |
| 859 | * |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 860 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 861 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 862 | * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise. |
| 863 | * 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] | 864 | * algorithm identifier. |
| 865 | */ |
Gilles Peskine | 9df2dc8 | 2018-08-22 18:24:17 +0200 | [diff] [blame] | 866 | #define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 867 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \ |
| 868 | PSA_ALG_CIPHER_MAC_BASE) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 869 | |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 870 | #define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000) |
| 871 | #define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 872 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 873 | /** Whether the specified algorithm is a stream cipher. |
| 874 | * |
| 875 | * A stream cipher is a symmetric cipher that encrypts or decrypts messages |
| 876 | * by applying a bitwise-xor with a stream of bytes that is generated |
| 877 | * from a key. |
| 878 | * |
| 879 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 880 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 881 | * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise. |
| 882 | * 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] | 883 | * algorithm identifier or if it is not a symmetric cipher algorithm. |
| 884 | */ |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 885 | #define PSA_ALG_IS_STREAM_CIPHER(alg) \ |
Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 886 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \ |
| 887 | (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG)) |
| 888 | |
| 889 | /** The ARC4 stream cipher algorithm. |
| 890 | */ |
| 891 | #define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001) |
| 892 | |
| 893 | /** The CTR stream cipher mode. |
| 894 | * |
| 895 | * CTR is a stream cipher which is built from a block cipher. |
| 896 | * The underlying block cipher is determined by the key type. |
| 897 | * For example, to use AES-128-CTR, use this algorithm with |
| 898 | * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes). |
| 899 | */ |
| 900 | #define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001) |
| 901 | |
| 902 | #define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002) |
| 903 | |
| 904 | #define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003) |
| 905 | |
| 906 | /** The XTS cipher mode. |
| 907 | * |
| 908 | * XTS is a cipher mode which is built from a block cipher. It requires at |
| 909 | * least one full block of input, but beyond this minimum the input |
| 910 | * does not need to be a whole number of blocks. |
| 911 | */ |
| 912 | #define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff) |
| 913 | |
| 914 | /** The CBC block cipher chaining mode, with no padding. |
| 915 | * |
| 916 | * The underlying block cipher is determined by the key type. |
| 917 | * |
| 918 | * This symmetric cipher mode can only be used with messages whose lengths |
| 919 | * are whole number of blocks for the chosen block cipher. |
| 920 | */ |
| 921 | #define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100) |
| 922 | |
| 923 | /** The CBC block cipher chaining mode with PKCS#7 padding. |
| 924 | * |
| 925 | * The underlying block cipher is determined by the key type. |
| 926 | * |
| 927 | * This is the padding method defined by PKCS#7 (RFC 2315) §10.3. |
| 928 | */ |
| 929 | #define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101) |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 930 | |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 931 | #define PSA_ALG_CCM ((psa_algorithm_t)0x06001001) |
| 932 | #define PSA_ALG_GCM ((psa_algorithm_t)0x06001002) |
| 933 | |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 934 | /* In the encoding of a AEAD algorithm, the bits corresponding to |
| 935 | * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag. |
| 936 | * The constants for default lengths follow this encoding. |
| 937 | */ |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 938 | #define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x00003f00) |
| 939 | #define PSA_AEAD_TAG_LENGTH_OFFSET 8 |
| 940 | |
| 941 | /** Macro to build a shortened AEAD algorithm. |
| 942 | * |
| 943 | * A shortened AEAD algorithm is similar to the corresponding AEAD |
| 944 | * algorithm, but has an authentication tag that consists of fewer bytes. |
| 945 | * Depending on the algorithm, the tag length may affect the calculation |
| 946 | * of the ciphertext. |
| 947 | * |
| 948 | * \param alg A AEAD algorithm identifier (value of type |
| 949 | * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg) |
| 950 | * is true). |
Gilles Peskine | 3111981 | 2018-08-21 14:47:48 +0200 | [diff] [blame] | 951 | * \param tag_length Desired length of the authentication tag in bytes. |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 952 | * |
| 953 | * \return The corresponding AEAD algorithm with the specified |
| 954 | * length. |
| 955 | * \return Unspecified if \p alg is not a supported |
| 956 | * AEAD algorithm or if \p tag_length is not valid |
| 957 | * for the specified AEAD algorithm. |
| 958 | */ |
| 959 | #define PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, tag_length) \ |
| 960 | (((alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \ |
| 961 | ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \ |
| 962 | PSA_ALG_AEAD_TAG_LENGTH_MASK)) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 963 | |
Gilles Peskine | 70f46e1 | 2018-08-20 15:07:53 +0200 | [diff] [blame] | 964 | /** Calculate the corresponding AEAD algorithm with the default tag length. |
| 965 | * |
| 966 | * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that |
| 967 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 968 | * |
| 969 | * \return The corresponding AEAD algorithm with the default tag length |
| 970 | * for that algorithm. |
| 971 | */ |
| 972 | #define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) \ |
| 973 | ( \ |
| 974 | PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_CCM) \ |
| 975 | PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_GCM) \ |
| 976 | 0) |
| 977 | #define PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, ref) \ |
| 978 | PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, 0) == \ |
| 979 | PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \ |
| 980 | ref : |
| 981 | |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 982 | #define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000) |
| 983 | /** RSA PKCS#1 v1.5 signature with hashing. |
| 984 | * |
| 985 | * This is the signature scheme defined by RFC 8017 |
| 986 | * (PKCS#1: RSA Cryptography Specifications) under the name |
| 987 | * RSASSA-PKCS1-v1_5. |
| 988 | * |
| 989 | * \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] | 990 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 991 | * |
| 992 | * \return The corresponding RSA PKCS#1 v1.5 signature algorithm. |
| 993 | * \return Unspecified if \p alg is not a supported |
| 994 | * hash algorithm. |
| 995 | */ |
Gilles Peskine | a592623 | 2018-03-28 14:16:50 +0200 | [diff] [blame] | 996 | #define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 997 | (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 998 | /** Raw PKCS#1 v1.5 signature. |
| 999 | * |
| 1000 | * The input to this algorithm is the DigestInfo structure used by |
| 1001 | * RFC 8017 (PKCS#1: RSA Cryptography Specifications), §9.2 |
| 1002 | * steps 3–6. |
| 1003 | */ |
| 1004 | #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] | 1005 | #define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 1006 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1007 | |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 1008 | #define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000) |
| 1009 | /** RSA PSS signature with hashing. |
| 1010 | * |
| 1011 | * This is the signature scheme defined by RFC 8017 |
| 1012 | * (PKCS#1: RSA Cryptography Specifications) under the name |
Gilles Peskine | a4d20bd | 2018-06-29 23:35:02 +0200 | [diff] [blame] | 1013 | * RSASSA-PSS, with the message generation function MGF1, and with |
| 1014 | * a salt length equal to the length of the hash. The specified |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 1015 | * hash algorithm is used to hash the input message, to create the |
| 1016 | * salted hash, and for the mask generation. |
| 1017 | * |
| 1018 | * \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] | 1019 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 1020 | * |
| 1021 | * \return The corresponding RSA PSS signature algorithm. |
| 1022 | * \return Unspecified if \p alg is not a supported |
| 1023 | * hash algorithm. |
| 1024 | */ |
| 1025 | #define PSA_ALG_RSA_PSS(hash_alg) \ |
| 1026 | (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1027 | #define PSA_ALG_IS_RSA_PSS(alg) \ |
| 1028 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE) |
| 1029 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1030 | #define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000) |
| 1031 | /** DSA signature with hashing. |
| 1032 | * |
| 1033 | * This is the signature scheme defined by FIPS 186-4, |
| 1034 | * with a random per-message secret number (*k*). |
| 1035 | * |
| 1036 | * \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] | 1037 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1038 | * |
| 1039 | * \return The corresponding DSA signature algorithm. |
| 1040 | * \return Unspecified if \p alg is not a supported |
| 1041 | * hash algorithm. |
| 1042 | */ |
| 1043 | #define PSA_ALG_DSA(hash_alg) \ |
| 1044 | (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1045 | #define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000) |
| 1046 | #define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000) |
| 1047 | #define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \ |
| 1048 | (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1049 | #define PSA_ALG_IS_DSA(alg) \ |
| 1050 | (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \ |
| 1051 | PSA_ALG_DSA_BASE) |
| 1052 | #define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \ |
| 1053 | (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0) |
Gilles Peskine | 55728b0 | 2018-07-16 23:08:16 +0200 | [diff] [blame] | 1054 | #define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \ |
| 1055 | (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg)) |
| 1056 | #define PSA_ALG_IS_RANDOMIZED_DSA(alg) \ |
| 1057 | (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg)) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1058 | |
| 1059 | #define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000) |
| 1060 | /** ECDSA signature with hashing. |
| 1061 | * |
| 1062 | * This is the ECDSA signature scheme defined by ANSI X9.62, |
| 1063 | * with a random per-message secret number (*k*). |
| 1064 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 1065 | * The representation of the signature as a byte string consists of |
| 1066 | * the concatentation of the signature values *r* and *s*. Each of |
| 1067 | * *r* and *s* is encoded as an *N*-octet string, where *N* is the length |
| 1068 | * of the base point of the curve in octets. Each value is represented |
| 1069 | * in big-endian order (most significant octet first). |
| 1070 | * |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1071 | * \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] | 1072 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1073 | * |
| 1074 | * \return The corresponding ECDSA signature algorithm. |
| 1075 | * \return Unspecified if \p alg is not a supported |
| 1076 | * hash algorithm. |
| 1077 | */ |
| 1078 | #define PSA_ALG_ECDSA(hash_alg) \ |
| 1079 | (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1080 | /** ECDSA signature without hashing. |
| 1081 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 1082 | * This is the same signature scheme as #PSA_ALG_ECDSA(), but |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1083 | * without specifying a hash algorithm. This algorithm may only be |
| 1084 | * used to sign or verify a sequence of bytes that should be an |
| 1085 | * already-calculated hash. Note that the input is padded with |
| 1086 | * zeros on the left or truncated on the left as required to fit |
| 1087 | * the curve size. |
| 1088 | */ |
| 1089 | #define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE |
| 1090 | #define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000) |
| 1091 | /** Deterministic ECDSA signature with hashing. |
| 1092 | * |
| 1093 | * This is the deterministic ECDSA signature scheme defined by RFC 6979. |
| 1094 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 1095 | * The representation of a signature is the same as with #PSA_ALG_ECDSA(). |
| 1096 | * |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1097 | * Note that when this algorithm is used for verification, signatures |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1098 | * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1099 | * same private key are accepted. In other words, |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1100 | * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from |
| 1101 | * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification. |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1102 | * |
| 1103 | * \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] | 1104 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1105 | * |
| 1106 | * \return The corresponding deterministic ECDSA signature |
| 1107 | * algorithm. |
| 1108 | * \return Unspecified if \p alg is not a supported |
| 1109 | * hash algorithm. |
| 1110 | */ |
| 1111 | #define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \ |
| 1112 | (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1113 | #define PSA_ALG_IS_ECDSA(alg) \ |
| 1114 | (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \ |
| 1115 | PSA_ALG_ECDSA_BASE) |
| 1116 | #define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \ |
| 1117 | (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0) |
Gilles Peskine | 55728b0 | 2018-07-16 23:08:16 +0200 | [diff] [blame] | 1118 | #define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \ |
| 1119 | (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) |
| 1120 | #define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \ |
| 1121 | (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1122 | |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 1123 | /** Get the hash used by a hash-and-sign signature algorithm. |
| 1124 | * |
| 1125 | * A hash-and-sign algorithm is a signature algorithm which is |
| 1126 | * composed of two phases: first a hashing phase which does not use |
| 1127 | * the key and produces a hash of the input message, then a signing |
| 1128 | * phase which only uses the hash and the key and not the message |
| 1129 | * itself. |
| 1130 | * |
| 1131 | * \param alg A signature algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1132 | * #PSA_ALG_IS_SIGN(\p alg) is true). |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 1133 | * |
| 1134 | * \return The underlying hash algorithm if \p alg is a hash-and-sign |
| 1135 | * algorithm. |
| 1136 | * \return 0 if \p alg is a signature algorithm that does not |
| 1137 | * follow the hash-and-sign structure. |
| 1138 | * \return Unspecified if \p alg is not a signature algorithm or |
| 1139 | * if it is not supported by the implementation. |
| 1140 | */ |
| 1141 | #define PSA_ALG_SIGN_GET_HASH(alg) \ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 1142 | (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \ |
| 1143 | PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \ |
Gilles Peskine | 54622ae | 2018-06-29 22:24:24 +0200 | [diff] [blame] | 1144 | ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \ |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 1145 | ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \ |
| 1146 | 0) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1147 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1148 | /** RSA PKCS#1 v1.5 encryption. |
| 1149 | */ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 1150 | #define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1151 | |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 1152 | #define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1153 | /** RSA OAEP encryption. |
| 1154 | * |
| 1155 | * This is the encryption scheme defined by RFC 8017 |
| 1156 | * (PKCS#1: RSA Cryptography Specifications) under the name |
| 1157 | * RSAES-OAEP, with the message generation function MGF1. |
| 1158 | * |
| 1159 | * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that |
| 1160 | * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use |
| 1161 | * for MGF1. |
| 1162 | * |
| 1163 | * \return The corresponding RSA OAEP signature algorithm. |
| 1164 | * \return Unspecified if \p alg is not a supported |
| 1165 | * hash algorithm. |
| 1166 | */ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 1167 | #define PSA_ALG_RSA_OAEP(hash_alg) \ |
| 1168 | (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1169 | #define PSA_ALG_IS_RSA_OAEP(alg) \ |
| 1170 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE) |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 1171 | #define PSA_ALG_RSA_OAEP_GET_HASH(alg) \ |
| 1172 | (PSA_ALG_IS_RSA_OAEP(alg) ? \ |
| 1173 | ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \ |
| 1174 | 0) |
Gilles Peskine | d1e8e41 | 2018-06-07 09:49:39 +0200 | [diff] [blame] | 1175 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 1176 | #define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100) |
| 1177 | /** Macro to build an HKDF algorithm. |
| 1178 | * |
| 1179 | * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256. |
| 1180 | * |
| 1181 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 1182 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 1183 | * |
| 1184 | * \return The corresponding HKDF algorithm. |
| 1185 | * \return Unspecified if \p alg is not a supported |
| 1186 | * hash algorithm. |
| 1187 | */ |
| 1188 | #define PSA_ALG_HKDF(hash_alg) \ |
| 1189 | (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1190 | /** Whether the specified algorithm is an HKDF algorithm. |
| 1191 | * |
| 1192 | * HKDF is a family of key derivation algorithms that are based on a hash |
| 1193 | * function and the HMAC construction. |
| 1194 | * |
| 1195 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 1196 | * |
| 1197 | * \return 1 if \c alg is an HKDF algorithm, 0 otherwise. |
| 1198 | * This macro may return either 0 or 1 if \c alg is not a supported |
| 1199 | * key derivation algorithm identifier. |
| 1200 | */ |
| 1201 | #define PSA_ALG_IS_HKDF(alg) \ |
| 1202 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE) |
| 1203 | #define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \ |
| 1204 | (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK)) |
| 1205 | |
Hanno Becker | 79250c2 | 2018-10-09 17:32:46 +0100 | [diff] [blame] | 1206 | #define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x30000200) |
| 1207 | /** Macro to build a TLS-1.2 PRF algorithm. |
| 1208 | * |
Hanno Becker | 2255a36 | 2018-11-16 16:05:13 +0000 | [diff] [blame] | 1209 | * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule, |
| 1210 | * specified in Section 5 of RFC 5246. It is based on HMAC and can be |
| 1211 | * used with either SHA-256 or SHA-384. |
| 1212 | * |
| 1213 | * For the application to TLS-1.2, the salt and label arguments passed |
| 1214 | * to psa_key_derivation() are what's called 'seed' and 'label' in RFC 5246, |
| 1215 | * respectively. For example, for TLS key expansion, the salt is the |
| 1216 | * concatenation of ServerHello.Random + ClientHello.Random, |
| 1217 | * while the label is "key expansion". |
| 1218 | * |
Hanno Becker | 79250c2 | 2018-10-09 17:32:46 +0100 | [diff] [blame] | 1219 | * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the |
| 1220 | * TLS 1.2 PRF using HMAC-SHA-256. |
| 1221 | * |
| 1222 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 1223 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 1224 | * |
| 1225 | * \return The corresponding TLS-1.2 PRF algorithm. |
| 1226 | * \return Unspecified if \p alg is not a supported |
| 1227 | * hash algorithm. |
| 1228 | */ |
| 1229 | #define PSA_ALG_TLS12_PRF(hash_alg) \ |
| 1230 | (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1231 | |
| 1232 | /** Whether the specified algorithm is a TLS-1.2 PRF algorithm. |
| 1233 | * |
Hanno Becker | 79250c2 | 2018-10-09 17:32:46 +0100 | [diff] [blame] | 1234 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 1235 | * |
| 1236 | * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise. |
| 1237 | * This macro may return either 0 or 1 if \c alg is not a supported |
| 1238 | * key derivation algorithm identifier. |
| 1239 | */ |
| 1240 | #define PSA_ALG_IS_TLS12_PRF(alg) \ |
| 1241 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE) |
| 1242 | #define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \ |
| 1243 | (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK)) |
| 1244 | |
Hanno Becker | 8dbfca4 | 2018-10-12 11:56:55 +0100 | [diff] [blame] | 1245 | #define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x30000300) |
| 1246 | /** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm. |
| 1247 | * |
Hanno Becker | 2255a36 | 2018-11-16 16:05:13 +0000 | [diff] [blame] | 1248 | * In a pure-PSK handshake in TLS 1.2, the master secret is derived |
| 1249 | * from the PreSharedKey (PSK) through the application of padding |
| 1250 | * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5). |
| 1251 | * The latter is based on HMAC and can be used with either SHA-256 |
| 1252 | * or SHA-384. |
| 1253 | * |
| 1254 | * For the application to TLS-1.2, the salt passed to psa_key_derivation() |
| 1255 | * (and forwarded to the TLS-1.2 PRF) is the concatenation of the |
| 1256 | * ClientHello.Random + ServerHello.Random, while the label is "master secret" |
| 1257 | * or "extended master secret". |
| 1258 | * |
Hanno Becker | 8dbfca4 | 2018-10-12 11:56:55 +0100 | [diff] [blame] | 1259 | * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the |
| 1260 | * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256. |
| 1261 | * |
| 1262 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 1263 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 1264 | * |
| 1265 | * \return The corresponding TLS-1.2 PSK to MS algorithm. |
| 1266 | * \return Unspecified if \p alg is not a supported |
| 1267 | * hash algorithm. |
| 1268 | */ |
| 1269 | #define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \ |
| 1270 | (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 1271 | |
| 1272 | /** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm. |
| 1273 | * |
Hanno Becker | 8dbfca4 | 2018-10-12 11:56:55 +0100 | [diff] [blame] | 1274 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 1275 | * |
| 1276 | * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise. |
| 1277 | * This macro may return either 0 or 1 if \c alg is not a supported |
| 1278 | * key derivation algorithm identifier. |
| 1279 | */ |
| 1280 | #define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \ |
| 1281 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE) |
| 1282 | #define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \ |
| 1283 | (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK)) |
| 1284 | |
Gilles Peskine | e8f0e3d | 2018-09-18 11:52:10 +0200 | [diff] [blame] | 1285 | #define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0x010fffff) |
| 1286 | |
| 1287 | /** Use a shared secret as is. |
| 1288 | * |
| 1289 | * Specify this algorithm as the selection component of a key agreement |
| 1290 | * to use the raw result of the key agreement as key material. |
| 1291 | * |
| 1292 | * \warning The raw result of a key agreement algorithm such as finite-field |
| 1293 | * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should |
| 1294 | * not be used directly as key material. It can however be used as the secret |
| 1295 | * input in a key derivation algorithm. |
| 1296 | */ |
| 1297 | #define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001) |
| 1298 | |
| 1299 | #define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \ |
| 1300 | (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION) |
| 1301 | |
| 1302 | #define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \ |
| 1303 | ((alg) & ~PSA_ALG_KEY_DERIVATION_MASK) |
Gilles Peskine | 93098fd | 2018-09-18 11:54:43 +0200 | [diff] [blame] | 1304 | |
| 1305 | #define PSA_ALG_FFDH_BASE ((psa_algorithm_t)0x22100000) |
| 1306 | /** The Diffie-Hellman key agreement algorithm. |
| 1307 | * |
Gilles Peskine | 2607bca | 2018-10-25 22:21:03 +0200 | [diff] [blame] | 1308 | * This algorithm combines the finite-field Diffie-Hellman (DH) key |
| 1309 | * agreement, also known as Diffie-Hellman-Merkle (DHM) key agreement, |
| 1310 | * to produce a shared secret from a private key and the peer's |
Gilles Peskine | 93098fd | 2018-09-18 11:54:43 +0200 | [diff] [blame] | 1311 | * public key, with a key selection or key derivation algorithm to produce |
| 1312 | * one or more shared keys and other shared cryptographic material. |
| 1313 | * |
Gilles Peskine | 99d0259 | 2018-11-15 17:47:25 +0100 | [diff] [blame] | 1314 | * The shared secret produced by key agreement and passed as input to the |
| 1315 | * derivation or selection algorithm \p kdf_alg is the shared secret |
| 1316 | * `g^{ab}` in big-endian format. |
| 1317 | * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p` |
| 1318 | * in bits. |
Gilles Peskine | 79dd622 | 2018-10-25 22:22:11 +0200 | [diff] [blame] | 1319 | * |
Gilles Peskine | 93098fd | 2018-09-18 11:54:43 +0200 | [diff] [blame] | 1320 | * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such |
| 1321 | * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true) |
| 1322 | * or a key selection algorithm (\c PSA_ALG_XXX value such |
Gilles Peskine | 19643c5 | 2018-11-16 16:45:02 +0100 | [diff] [blame] | 1323 | * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true). |
Gilles Peskine | 93098fd | 2018-09-18 11:54:43 +0200 | [diff] [blame] | 1324 | * |
| 1325 | * \return The Diffie-Hellman algorithm with the specified |
| 1326 | * selection or derivation algorithm. |
| 1327 | */ |
| 1328 | #define PSA_ALG_FFDH(kdf_alg) \ |
| 1329 | (PSA_ALG_FFDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK)) |
| 1330 | /** Whether the specified algorithm is a finite field Diffie-Hellman algorithm. |
| 1331 | * |
| 1332 | * This includes every supported key selection or key agreement algorithm |
| 1333 | * for the output of the Diffie-Hellman calculation. |
| 1334 | * |
| 1335 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 1336 | * |
| 1337 | * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise. |
| 1338 | * This macro may return either 0 or 1 if \c alg is not a supported |
| 1339 | * key agreement algorithm identifier. |
| 1340 | */ |
| 1341 | #define PSA_ALG_IS_FFDH(alg) \ |
| 1342 | (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH_BASE) |
| 1343 | |
| 1344 | #define PSA_ALG_ECDH_BASE ((psa_algorithm_t)0x22200000) |
Gilles Peskine | 2607bca | 2018-10-25 22:21:03 +0200 | [diff] [blame] | 1345 | /** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm. |
Gilles Peskine | 93098fd | 2018-09-18 11:54:43 +0200 | [diff] [blame] | 1346 | * |
| 1347 | * This algorithm combines the elliptic curve Diffie-Hellman key |
| 1348 | * agreement to produce a shared secret from a private key and the peer's |
| 1349 | * public key, with a key selection or key derivation algorithm to produce |
| 1350 | * one or more shared keys and other shared cryptographic material. |
| 1351 | * |
Gilles Peskine | 7b5b4a0 | 2018-11-14 21:05:10 +0100 | [diff] [blame] | 1352 | * The shared secret produced by key agreement and passed as input to the |
| 1353 | * derivation or selection algorithm \p kdf_alg is the x-coordinate of |
Gilles Peskine | 6c6a023 | 2018-11-15 17:44:43 +0100 | [diff] [blame] | 1354 | * the shared secret point. It is always `ceiling(m / 8)` bytes long where |
| 1355 | * `m` is the bit size associated with the curve, i.e. the bit size of the |
| 1356 | * order of the curve's coordinate field. When `m` is not a multiple of 8, |
Gilles Peskine | 7b5b4a0 | 2018-11-14 21:05:10 +0100 | [diff] [blame] | 1357 | * the byte containing the most significant bit of the shared secret |
| 1358 | * is padded with zero bits. The byte order is either little-endian |
| 1359 | * or big-endian depending on the curve type. |
| 1360 | * |
| 1361 | * - For Montgomery curves (curve types `PSA_ECC_CURVE_CURVEXXX`), |
| 1362 | * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A` |
| 1363 | * in little-endian byte order. |
| 1364 | * The bit size is 448 for Curve448 and 255 for Curve25519. |
| 1365 | * - For Weierstrass curves over prime fields (curve types |
| 1366 | * `PSA_ECC_CURVE_SECPXXX` and `PSA_ECC_CURVE_BRAINPOOL_PXXX`), |
| 1367 | * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A` |
| 1368 | * in big-endian byte order. |
Gilles Peskine | 6c6a023 | 2018-11-15 17:44:43 +0100 | [diff] [blame] | 1369 | * The bit size is `m = ceiling(log_2(p))` for the field `F_p`. |
Gilles Peskine | 7b5b4a0 | 2018-11-14 21:05:10 +0100 | [diff] [blame] | 1370 | * - For Weierstrass curves over binary fields (curve types |
| 1371 | * `PSA_ECC_CURVE_SECTXXX`), |
| 1372 | * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A` |
| 1373 | * in big-endian byte order. |
Gilles Peskine | 6c6a023 | 2018-11-15 17:44:43 +0100 | [diff] [blame] | 1374 | * The bit size is `m` for the field `F_{2^m}`. |
Gilles Peskine | 79dd622 | 2018-10-25 22:22:11 +0200 | [diff] [blame] | 1375 | * |
Gilles Peskine | 93098fd | 2018-09-18 11:54:43 +0200 | [diff] [blame] | 1376 | * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such |
| 1377 | * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true) |
| 1378 | * or a selection algorithm (\c PSA_ALG_XXX value such |
| 1379 | * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true). |
| 1380 | * |
| 1381 | * \return The Diffie-Hellman algorithm with the specified |
| 1382 | * selection or derivation algorithm. |
| 1383 | */ |
| 1384 | #define PSA_ALG_ECDH(kdf_alg) \ |
| 1385 | (PSA_ALG_ECDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK)) |
| 1386 | /** Whether the specified algorithm is an elliptic curve Diffie-Hellman |
| 1387 | * algorithm. |
| 1388 | * |
| 1389 | * This includes every supported key selection or key agreement algorithm |
| 1390 | * for the output of the Diffie-Hellman calculation. |
| 1391 | * |
| 1392 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 1393 | * |
| 1394 | * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm, |
| 1395 | * 0 otherwise. |
| 1396 | * This macro may return either 0 or 1 if \c alg is not a supported |
| 1397 | * key agreement algorithm identifier. |
| 1398 | */ |
| 1399 | #define PSA_ALG_IS_ECDH(alg) \ |
| 1400 | (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH_BASE) |
| 1401 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1402 | /**@}*/ |
| 1403 | |
| 1404 | /** \defgroup key_management Key management |
| 1405 | * @{ |
| 1406 | */ |
| 1407 | |
Gilles Peskine | 3cac8c4 | 2018-11-30 14:07:45 +0100 | [diff] [blame] | 1408 | /** Encoding of key lifetimes. |
| 1409 | */ |
| 1410 | typedef uint32_t psa_key_lifetime_t; |
| 1411 | |
| 1412 | /** Encoding of identifiers of persistent keys. |
| 1413 | */ |
| 1414 | typedef uint32_t psa_key_id_t; |
| 1415 | |
| 1416 | /** A volatile key slot retains its content as long as the application is |
| 1417 | * running. It is guaranteed to be erased on a power reset. |
| 1418 | */ |
| 1419 | #define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000) |
| 1420 | |
| 1421 | /** A persistent key slot retains its content as long as it is not explicitly |
| 1422 | * destroyed. |
| 1423 | */ |
| 1424 | #define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001) |
| 1425 | |
| 1426 | /** A write-once key slot may not be modified once a key has been set. |
| 1427 | * It will retain its content as long as the device remains operational. |
| 1428 | */ |
| 1429 | #define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff) |
| 1430 | |
| 1431 | /** \brief Retrieve the lifetime of a key slot. |
| 1432 | * |
| 1433 | * The assignment of lifetimes to slots is implementation-dependent. |
| 1434 | * |
| 1435 | * \param key Slot to query. |
| 1436 | * \param[out] lifetime On success, the lifetime value. |
| 1437 | * |
| 1438 | * \retval #PSA_SUCCESS |
| 1439 | * Success. |
| 1440 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1441 | * The key slot is invalid. |
| 1442 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1443 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1444 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1445 | * \retval #PSA_ERROR_BAD_STATE |
| 1446 | * The library has not been previously initialized by psa_crypto_init(). |
| 1447 | * It is implementation-dependent whether a failure to initialize |
| 1448 | * results in this error code. |
| 1449 | */ |
| 1450 | psa_status_t psa_get_key_lifetime(psa_key_slot_t key, |
| 1451 | psa_key_lifetime_t *lifetime); |
| 1452 | |
| 1453 | /** \brief Change the lifetime of a key slot. |
| 1454 | * |
| 1455 | * Whether the lifetime of a key slot can be changed at all, and if so |
| 1456 | * whether the lifetime of an occupied key slot can be changed, is |
| 1457 | * implementation-dependent. |
| 1458 | * |
| 1459 | * When creating a persistent key, you must call this function before creating |
| 1460 | * the key material with psa_import_key(), psa_generate_key() or |
| 1461 | * psa_generator_import_key(). To open an existing persistent key, you must |
| 1462 | * call this function with the correct lifetime value before using the slot |
| 1463 | * for a cryptographic operation. Once a slot's lifetime has been set, |
| 1464 | * the lifetime remains associated with the slot until a subsequent call to |
| 1465 | * psa_set_key_lifetime(), until the key is wiped with psa_destroy_key or |
| 1466 | * until the application terminates (or disconnects from the cryptography |
| 1467 | * service, if the implementation offers such a possibility). |
| 1468 | * |
| 1469 | * \param key Slot whose lifetime is to be changed. |
| 1470 | * \param lifetime The lifetime value to set for the given key slot. |
| 1471 | * |
| 1472 | * \retval #PSA_SUCCESS |
| 1473 | * Success. |
| 1474 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1475 | * The key slot is invalid, |
| 1476 | * or the lifetime value is invalid. |
| 1477 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1478 | * The implementation does not support the specified lifetime value, |
| 1479 | * at least for the specified key slot. |
| 1480 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 1481 | * The slot contains a key, and the implementation does not support |
| 1482 | * changing the lifetime of an occupied slot. |
| 1483 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1484 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1485 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1486 | * \retval #PSA_ERROR_BAD_STATE |
| 1487 | * The library has not been previously initialized by psa_crypto_init(). |
| 1488 | * It is implementation-dependent whether a failure to initialize |
| 1489 | * results in this error code. |
| 1490 | */ |
| 1491 | psa_status_t psa_set_key_lifetime(psa_key_slot_t key, |
| 1492 | psa_key_lifetime_t lifetime); |
| 1493 | |
Gilles Peskine | f535eb2 | 2018-11-30 14:08:36 +0100 | [diff] [blame] | 1494 | /** Allocate a key slot for a transient key, i.e. a key which is only stored |
| 1495 | * in volatile memory. |
| 1496 | * |
| 1497 | * The allocated key slot and its handle remain valid until the |
| 1498 | * application calls psa_close_key() or psa_destroy_key() or until the |
| 1499 | * application terminates. |
| 1500 | * |
| 1501 | * This function takes a key type and maximum size as arguments so that |
| 1502 | * the implementation can reserve a corresponding amount of memory. |
| 1503 | * Implementations are not required to enforce this limit: if the application |
| 1504 | * later tries to create a larger key or a key of a different type, it |
| 1505 | * is implementation-defined whether this may succeed. |
| 1506 | * |
| 1507 | * \param type The type of key that the slot will contain. |
| 1508 | * \param max_bits The maximum key size that the slot will contain. |
| 1509 | * \param[out] handle On success, a handle to a volatile key slot. |
| 1510 | * |
| 1511 | * \retval #PSA_SUCCESS |
| 1512 | * Success. The application can now use the value of `*handle` |
| 1513 | * to access the newly allocated key slot. |
| 1514 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1515 | * There was not enough memory, or the maximum number of key slots |
| 1516 | * has been reached. |
| 1517 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1518 | * This implementation does not support this key type. |
| 1519 | */ |
| 1520 | |
| 1521 | psa_status_t psa_allocate_key(psa_key_type_t type, |
| 1522 | size_t max_bits, |
| 1523 | psa_key_handle_t *handle); |
| 1524 | |
| 1525 | /** Open a handle to an existing persistent key. |
| 1526 | * |
| 1527 | * Open a handle to a key which was previously created with psa_create_key(). |
| 1528 | * |
| 1529 | * \param lifetime The lifetime of the key. This designates a storage |
| 1530 | * area where the key material is stored. This must not |
| 1531 | * be #PSA_KEY_LIFETIME_VOLATILE. |
| 1532 | * \param id The persistent identifier of the key. |
| 1533 | * \param[out] handle On success, a handle to a key slot which contains |
| 1534 | * the data and metadata loaded from the specified |
| 1535 | * persistent location. |
| 1536 | * |
| 1537 | * \retval #PSA_SUCCESS |
| 1538 | * Success. The application can now use the value of `*handle` |
| 1539 | * to access the newly allocated key slot. |
| 1540 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1541 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1542 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1543 | * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE. |
| 1544 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1545 | * \p id is invalid for the specified lifetime. |
| 1546 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1547 | * \p lifetime is not supported. |
| 1548 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1549 | * The specified key exists, but the application does not have the |
| 1550 | * permission to access it. Note that this specification does not |
| 1551 | * define any way to create such a key, but it may be possible |
| 1552 | * through implementation-specific means. |
| 1553 | */ |
| 1554 | psa_status_t psa_open_key(psa_key_lifetime_t lifetime, |
| 1555 | psa_key_id_t id, |
| 1556 | psa_key_handle_t *handle); |
| 1557 | |
| 1558 | /** Create a new persistent key slot. |
| 1559 | * |
| 1560 | * Create a new persistent key slot and return a handle to it. The handle |
| 1561 | * remains valid until the application calls psa_close_key() or terminates. |
| 1562 | * The application can open the key again with psa_open_key() until it |
| 1563 | * removes the key by calling psa_destroy_key(). |
| 1564 | * |
| 1565 | * \param lifetime The lifetime of the key. This designates a storage |
| 1566 | * area where the key material is stored. This must not |
| 1567 | * be #PSA_KEY_LIFETIME_VOLATILE. |
| 1568 | * \param id The persistent identifier of the key. |
| 1569 | * \param type The type of key that the slot will contain. |
| 1570 | * \param max_bits The maximum key size that the slot will contain. |
| 1571 | * \param[out] handle On success, a handle to the newly created key slot. |
| 1572 | * When key material is later created in this key slot, |
| 1573 | * it will be saved to the specified persistent location. |
| 1574 | * |
| 1575 | * \retval #PSA_SUCCESS |
| 1576 | * Success. The application can now use the value of `*handle` |
| 1577 | * to access the newly allocated key slot. |
| 1578 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1579 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1580 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 1581 | * There is already a key with the identifier \p id in the storage |
| 1582 | * area designated by \p lifetime. |
| 1583 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1584 | * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE. |
| 1585 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1586 | * \p id is invalid for the specified lifetime. |
| 1587 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1588 | * \p lifetime is not supported. |
| 1589 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1590 | * \p lifetime is valid, but the application does not have the |
| 1591 | * permission to create a key there. |
| 1592 | */ |
| 1593 | psa_status_t psa_create_key(psa_key_lifetime_t lifetime, |
| 1594 | psa_key_id_t id, |
| 1595 | psa_key_type_t type, |
| 1596 | size_t max_bits, |
| 1597 | psa_key_handle_t *handle); |
| 1598 | |
| 1599 | /** Close a key handle. |
| 1600 | * |
| 1601 | * If the handle designates a volatile key, destroy the key material and |
| 1602 | * free all associated resources, just like psa_destroy_key(). |
| 1603 | * |
| 1604 | * If the handle designates a persistent key, free all resources associated |
| 1605 | * with the key in volatile memory. The key slot in persistent storage is |
| 1606 | * not affected and can be opened again later with psa_open_key(). |
| 1607 | * |
| 1608 | * \param handle The key handle to close. |
| 1609 | * |
| 1610 | * \retval #PSA_SUCCESS |
| 1611 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 1612 | */ |
| 1613 | psa_status_t psa_close_key(psa_key_handle_t handle); |
| 1614 | |
Gilles Peskine | 3cac8c4 | 2018-11-30 14:07:45 +0100 | [diff] [blame] | 1615 | /**@}*/ |
| 1616 | |
| 1617 | /** \defgroup import_export Key import and export |
| 1618 | * @{ |
| 1619 | */ |
| 1620 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1621 | /** |
| 1622 | * \brief Import a key in binary format. |
| 1623 | * |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 1624 | * This function supports any output from psa_export_key(). Refer to the |
Gilles Peskine | f793393 | 2018-10-31 14:07:52 +0100 | [diff] [blame] | 1625 | * documentation of psa_export_public_key() for the format of public keys |
| 1626 | * and to the documentation of psa_export_key() for the format for |
| 1627 | * other key types. |
| 1628 | * |
| 1629 | * This specification supports a single format for each key type. |
| 1630 | * Implementations may support other formats as long as the standard |
| 1631 | * format is supported. Implementations that support other formats |
| 1632 | * should ensure that the formats are clearly unambiguous so as to |
| 1633 | * minimize the risk that an invalid input is accidentally interpreted |
| 1634 | * according to a different format. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1635 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1636 | * \param key Slot where the key will be stored. This must be a |
| 1637 | * valid slot for a key of the chosen type. It must |
| 1638 | * be unoccupied. |
Gilles Peskine | f793393 | 2018-10-31 14:07:52 +0100 | [diff] [blame] | 1639 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful |
| 1640 | * import, the key slot will contain a key of this type. |
| 1641 | * \param[in] data Buffer containing the key data. The content of this |
| 1642 | * buffer is interpreted according to \p type. It must |
| 1643 | * contain the format described in the documentation |
| 1644 | * of psa_export_key() or psa_export_public_key() for |
| 1645 | * the chosen type. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1646 | * \param data_length Size of the \p data buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1647 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1648 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1649 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1650 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1651 | * The key type or key size is not supported, either by the |
| 1652 | * implementation in general or in this particular slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1653 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1654 | * The key slot is invalid, |
| 1655 | * or the key data is not correctly formatted. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1656 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1657 | * There is already a key in the specified slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1658 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1659 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1660 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 1661 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1662 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1663 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1664 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1665 | * The library has not been previously initialized by psa_crypto_init(). |
| 1666 | * It is implementation-dependent whether a failure to initialize |
| 1667 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1668 | */ |
| 1669 | psa_status_t psa_import_key(psa_key_slot_t key, |
| 1670 | psa_key_type_t type, |
| 1671 | const uint8_t *data, |
| 1672 | size_t data_length); |
| 1673 | |
| 1674 | /** |
Gilles Peskine | 154bd95 | 2018-04-19 08:38:16 +0200 | [diff] [blame] | 1675 | * \brief Destroy a key and restore the slot to its default state. |
| 1676 | * |
| 1677 | * This function destroys the content of the key slot from both volatile |
| 1678 | * memory and, if applicable, non-volatile storage. Implementations shall |
| 1679 | * make a best effort to ensure that any previous content of the slot is |
| 1680 | * unrecoverable. |
| 1681 | * |
| 1682 | * This function also erases any metadata such as policies. It returns the |
| 1683 | * specified slot to its default state. |
| 1684 | * |
| 1685 | * \param key The key slot to erase. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1686 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1687 | * \retval #PSA_SUCCESS |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1688 | * The slot's content, if any, has been erased. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1689 | * \retval #PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1690 | * The slot holds content and cannot be erased because it is |
| 1691 | * read-only, either due to a policy or due to physical restrictions. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1692 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1693 | * The specified slot number does not designate a valid slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1694 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1695 | * There was an failure in communication with the cryptoprocessor. |
| 1696 | * The key material may still be present in the cryptoprocessor. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1697 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1698 | * The storage is corrupted. Implementations shall make a best effort |
| 1699 | * to erase key material even in this stage, however applications |
| 1700 | * should be aware that it may be impossible to guarantee that the |
| 1701 | * key material is not recoverable in such cases. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1702 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1703 | * An unexpected condition which is not a storage corruption or |
| 1704 | * a communication failure occurred. The cryptoprocessor may have |
| 1705 | * been compromised. |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1706 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1707 | * The library has not been previously initialized by psa_crypto_init(). |
| 1708 | * It is implementation-dependent whether a failure to initialize |
| 1709 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1710 | */ |
| 1711 | psa_status_t psa_destroy_key(psa_key_slot_t key); |
| 1712 | |
| 1713 | /** |
| 1714 | * \brief Get basic metadata about a key. |
| 1715 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1716 | * \param key Slot whose content is queried. This must |
| 1717 | * be an occupied key slot. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1718 | * \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] | 1719 | * This may be a null pointer, in which case the key type |
| 1720 | * is not written. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1721 | * \param[out] bits On success, the key size in bits. |
Gilles Peskine | 9a1ba0d | 2018-03-21 20:49:16 +0100 | [diff] [blame] | 1722 | * This may be a null pointer, in which case the key size |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1723 | * is not written. |
| 1724 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1725 | * \retval #PSA_SUCCESS |
| 1726 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1727 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1728 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1729 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1730 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1731 | * The library has not been previously initialized by psa_crypto_init(). |
| 1732 | * It is implementation-dependent whether a failure to initialize |
| 1733 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1734 | */ |
| 1735 | psa_status_t psa_get_key_information(psa_key_slot_t key, |
| 1736 | psa_key_type_t *type, |
| 1737 | size_t *bits); |
| 1738 | |
| 1739 | /** |
| 1740 | * \brief Export a key in binary format. |
| 1741 | * |
| 1742 | * The output of this function can be passed to psa_import_key() to |
| 1743 | * create an equivalent object. |
| 1744 | * |
Gilles Peskine | f793393 | 2018-10-31 14:07:52 +0100 | [diff] [blame] | 1745 | * If the implementation of psa_import_key() supports other formats |
| 1746 | * beyond the format specified here, the output from psa_export_key() |
| 1747 | * must use the representation specified here, not the original |
| 1748 | * representation. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1749 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1750 | * For standard key types, the output format is as follows: |
| 1751 | * |
| 1752 | * - For symmetric keys (including MAC keys), the format is the |
| 1753 | * raw bytes of the key. |
| 1754 | * - For DES, the key data consists of 8 bytes. The parity bits must be |
| 1755 | * correct. |
| 1756 | * - For Triple-DES, the format is the concatenation of the |
| 1757 | * two or three DES keys. |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1758 | * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1759 | * is the non-encrypted DER encoding of the representation defined by |
| 1760 | * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0. |
| 1761 | * ``` |
| 1762 | * RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1763 | * version INTEGER, -- must be 0 |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1764 | * modulus INTEGER, -- n |
| 1765 | * publicExponent INTEGER, -- e |
| 1766 | * privateExponent INTEGER, -- d |
| 1767 | * prime1 INTEGER, -- p |
| 1768 | * prime2 INTEGER, -- q |
| 1769 | * exponent1 INTEGER, -- d mod (p-1) |
| 1770 | * exponent2 INTEGER, -- d mod (q-1) |
| 1771 | * coefficient INTEGER, -- (inverse of q) mod p |
| 1772 | * } |
| 1773 | * ``` |
| 1774 | * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format |
| 1775 | * is the non-encrypted DER encoding of the representation used by |
Gilles Peskine | c6290c0 | 2018-08-13 17:24:59 +0200 | [diff] [blame] | 1776 | * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows: |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1777 | * ``` |
| 1778 | * DSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1779 | * version INTEGER, -- must be 0 |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1780 | * prime INTEGER, -- p |
| 1781 | * subprime INTEGER, -- q |
| 1782 | * generator INTEGER, -- g |
| 1783 | * public INTEGER, -- y |
| 1784 | * private INTEGER, -- x |
| 1785 | * } |
| 1786 | * ``` |
| 1787 | * - For elliptic curve key pairs (key types for which |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 1788 | * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is |
Gilles Peskine | 6c6a023 | 2018-11-15 17:44:43 +0100 | [diff] [blame] | 1789 | * a representation of the private value as a `ceiling(m/8)`-byte string |
| 1790 | * where `m` is the bit size associated with the curve, i.e. the bit size |
| 1791 | * of the order of the curve's coordinate field. This byte string is |
| 1792 | * in little-endian order for Montgomery curves (curve types |
| 1793 | * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass |
| 1794 | * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX` |
| 1795 | * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`). |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 1796 | * This is the content of the `privateKey` field of the `ECPrivateKey` |
| 1797 | * format defined by RFC 5915. |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1798 | * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is |
| 1799 | * true), the format is the same as for psa_export_public_key(). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1800 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1801 | * \param key Slot whose content is to be exported. This must |
| 1802 | * be an occupied key slot. |
| 1803 | * \param[out] data Buffer where the key data is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1804 | * \param data_size Size of the \p data buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1805 | * \param[out] data_length On success, the number of bytes |
| 1806 | * that make up the key data. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1807 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1808 | * \retval #PSA_SUCCESS |
| 1809 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1810 | * \retval #PSA_ERROR_NOT_PERMITTED |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 1811 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 1812 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1813 | * The size of the \p data buffer is too small. You can determine a |
| 1814 | * sufficient buffer size by calling |
| 1815 | * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits) |
| 1816 | * where \c type is the key type |
| 1817 | * and \c bits is the key size in bits. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1818 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1819 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1820 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1821 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1822 | * The library has not been previously initialized by psa_crypto_init(). |
| 1823 | * It is implementation-dependent whether a failure to initialize |
| 1824 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1825 | */ |
| 1826 | psa_status_t psa_export_key(psa_key_slot_t key, |
| 1827 | uint8_t *data, |
| 1828 | size_t data_size, |
| 1829 | size_t *data_length); |
| 1830 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1831 | /** |
| 1832 | * \brief Export a public key or the public part of a key pair in binary format. |
| 1833 | * |
| 1834 | * The output of this function can be passed to psa_import_key() to |
| 1835 | * create an object that is equivalent to the public key. |
| 1836 | * |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1837 | * The format is the DER representation defined by RFC 5280 as |
| 1838 | * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format |
| 1839 | * specified below. |
| 1840 | * ``` |
| 1841 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 1842 | * algorithm AlgorithmIdentifier, |
| 1843 | * subjectPublicKey BIT STRING } |
| 1844 | * AlgorithmIdentifier ::= SEQUENCE { |
| 1845 | * algorithm OBJECT IDENTIFIER, |
| 1846 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 1847 | * ``` |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1848 | * |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1849 | * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), |
| 1850 | * the `subjectPublicKey` format is defined by RFC 3279 §2.3.1 as |
| 1851 | * `RSAPublicKey`, |
| 1852 | * with the OID `rsaEncryption`, |
| 1853 | * and with the parameters `NULL`. |
| 1854 | * ``` |
| 1855 | * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) |
| 1856 | * rsadsi(113549) pkcs(1) 1 } |
| 1857 | * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 } |
| 1858 | * |
| 1859 | * RSAPublicKey ::= SEQUENCE { |
| 1860 | * modulus INTEGER, -- n |
| 1861 | * publicExponent INTEGER } -- e |
| 1862 | * ``` |
| 1863 | * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), |
| 1864 | * the `subjectPublicKey` format is defined by RFC 3279 §2.3.2 as |
| 1865 | * `DSAPublicKey`, |
| 1866 | * with the OID `id-dsa`, |
| 1867 | * and with the parameters `DSS-Parms`. |
| 1868 | * ``` |
| 1869 | * id-dsa OBJECT IDENTIFIER ::= { |
| 1870 | * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 } |
| 1871 | * |
| 1872 | * Dss-Parms ::= SEQUENCE { |
| 1873 | * p INTEGER, |
| 1874 | * q INTEGER, |
| 1875 | * g INTEGER } |
| 1876 | * DSAPublicKey ::= INTEGER -- public key, Y |
| 1877 | * ``` |
| 1878 | * - For elliptic curve public keys (key types for which |
| 1879 | * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), |
| 1880 | * the `subjectPublicKey` format is defined by RFC 3279 §2.3.5 as |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1881 | * `ECPoint`, which contains the uncompressed |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1882 | * representation defined by SEC1 §2.3.3. |
| 1883 | * The OID is `id-ecPublicKey`, |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1884 | * 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] | 1885 | * RFC 5480 §2.1.1.1 or other applicable standards. |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1886 | * ``` |
| 1887 | * ansi-X9-62 OBJECT IDENTIFIER ::= |
| 1888 | * { iso(1) member-body(2) us(840) 10045 } |
| 1889 | * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 } |
| 1890 | * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 } |
| 1891 | * |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 1892 | * ECPoint ::= ... |
| 1893 | * -- first 8 bits: 0x04; |
Gilles Peskine | 6c6a023 | 2018-11-15 17:44:43 +0100 | [diff] [blame] | 1894 | * -- then x_P as a `ceiling(m/8)`-byte string, big endian; |
| 1895 | * -- then y_P as a `ceiling(m/8)`-byte string, big endian; |
| 1896 | * -- where `m` is the bit size associated with the curve, |
Gilles Peskine | 7b5b4a0 | 2018-11-14 21:05:10 +0100 | [diff] [blame] | 1897 | * -- i.e. the bit size of `q` for a curve over `F_q`. |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 1898 | * |
| 1899 | * EcpkParameters ::= CHOICE { -- other choices are not allowed |
| 1900 | * namedCurve OBJECT IDENTIFIER } |
| 1901 | * ``` |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1902 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1903 | * \param key Slot whose content is to be exported. This must |
| 1904 | * be an occupied key slot. |
| 1905 | * \param[out] data Buffer where the key data is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1906 | * \param data_size Size of the \p data buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1907 | * \param[out] data_length On success, the number of bytes |
| 1908 | * that make up the key data. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1909 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1910 | * \retval #PSA_SUCCESS |
| 1911 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1912 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 1913 | * The key is neither a public key nor a key pair. |
| 1914 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1915 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1916 | * The size of the \p data buffer is too small. You can determine a |
| 1917 | * sufficient buffer size by calling |
| 1918 | * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits) |
| 1919 | * where \c type is the key type |
| 1920 | * and \c bits is the key size in bits. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1921 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1922 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1923 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1924 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1925 | * The library has not been previously initialized by psa_crypto_init(). |
| 1926 | * It is implementation-dependent whether a failure to initialize |
| 1927 | * results in this error code. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1928 | */ |
| 1929 | psa_status_t psa_export_public_key(psa_key_slot_t key, |
| 1930 | uint8_t *data, |
| 1931 | size_t data_size, |
| 1932 | size_t *data_length); |
| 1933 | |
| 1934 | /**@}*/ |
| 1935 | |
| 1936 | /** \defgroup policy Key policies |
| 1937 | * @{ |
| 1938 | */ |
| 1939 | |
| 1940 | /** \brief Encoding of permitted usage on a key. */ |
| 1941 | typedef uint32_t psa_key_usage_t; |
| 1942 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1943 | /** Whether the key may be exported. |
| 1944 | * |
| 1945 | * A public key or the public part of a key pair may always be exported |
| 1946 | * regardless of the value of this permission flag. |
| 1947 | * |
| 1948 | * If a key does not have export permission, implementations shall not |
| 1949 | * allow the key to be exported in plain form from the cryptoprocessor, |
| 1950 | * whether through psa_export_key() or through a proprietary interface. |
| 1951 | * The key may however be exportable in a wrapped form, i.e. in a form |
| 1952 | * where it is encrypted by another key. |
| 1953 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1954 | #define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001) |
| 1955 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1956 | /** Whether the key may be used to encrypt a message. |
| 1957 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1958 | * This flag allows the key to be used for a symmetric encryption operation, |
| 1959 | * for an AEAD encryption-and-authentication operation, |
| 1960 | * or for an asymmetric encryption operation, |
| 1961 | * if otherwise permitted by the key's type and policy. |
| 1962 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1963 | * For a key pair, this concerns the public key. |
| 1964 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1965 | #define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1966 | |
| 1967 | /** Whether the key may be used to decrypt a message. |
| 1968 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1969 | * This flag allows the key to be used for a symmetric decryption operation, |
| 1970 | * for an AEAD decryption-and-verification operation, |
| 1971 | * or for an asymmetric decryption operation, |
| 1972 | * if otherwise permitted by the key's type and policy. |
| 1973 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1974 | * For a key pair, this concerns the private key. |
| 1975 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1976 | #define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1977 | |
| 1978 | /** Whether the key may be used to sign a message. |
| 1979 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1980 | * This flag allows the key to be used for a MAC calculation operation |
| 1981 | * or for an asymmetric signature operation, |
| 1982 | * if otherwise permitted by the key's type and policy. |
| 1983 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1984 | * For a key pair, this concerns the private key. |
| 1985 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1986 | #define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1987 | |
| 1988 | /** Whether the key may be used to verify a message signature. |
| 1989 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1990 | * This flag allows the key to be used for a MAC verification operation |
| 1991 | * or for an asymmetric signature verification operation, |
| 1992 | * if otherwise permitted by by the key's type and policy. |
| 1993 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1994 | * For a key pair, this concerns the public key. |
| 1995 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1996 | #define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800) |
| 1997 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1998 | /** Whether the key may be used to derive other keys. |
| 1999 | */ |
| 2000 | #define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000) |
| 2001 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 2002 | /** The type of the key policy data structure. |
| 2003 | * |
| 2004 | * This is an implementation-defined \c struct. Applications should not |
| 2005 | * make any assumptions about the content of this structure except |
| 2006 | * as directed by the documentation of a specific implementation. */ |
| 2007 | typedef struct psa_key_policy_s psa_key_policy_t; |
| 2008 | |
| 2009 | /** \brief Initialize a key policy structure to a default that forbids all |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 2010 | * usage of the key. |
| 2011 | * |
| 2012 | * \param[out] policy The policy object to initialize. |
| 2013 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 2014 | void psa_key_policy_init(psa_key_policy_t *policy); |
| 2015 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 2016 | /** \brief Set the standard fields of a policy structure. |
| 2017 | * |
| 2018 | * Note that this function does not make any consistency check of the |
| 2019 | * parameters. The values are only checked when applying the policy to |
| 2020 | * a key slot with psa_set_key_policy(). |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 2021 | * |
| 2022 | * \param[out] policy The policy object to modify. |
| 2023 | * \param usage The permitted uses for the key. |
| 2024 | * \param alg The algorithm that the key may be used for. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 2025 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 2026 | void psa_key_policy_set_usage(psa_key_policy_t *policy, |
| 2027 | psa_key_usage_t usage, |
| 2028 | psa_algorithm_t alg); |
| 2029 | |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 2030 | /** \brief Retrieve the usage field of a policy structure. |
| 2031 | * |
| 2032 | * \param[in] policy The policy object to query. |
| 2033 | * |
| 2034 | * \return The permitted uses for a key with this policy. |
| 2035 | */ |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 2036 | 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] | 2037 | |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 2038 | /** \brief Retrieve the algorithm field of a policy structure. |
| 2039 | * |
| 2040 | * \param[in] policy The policy object to query. |
| 2041 | * |
| 2042 | * \return The permitted algorithm for a key with this policy. |
| 2043 | */ |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 2044 | 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] | 2045 | |
| 2046 | /** \brief Set the usage policy on a key slot. |
| 2047 | * |
| 2048 | * This function must be called on an empty key slot, before importing, |
| 2049 | * generating or creating a key in the slot. Changing the policy of an |
| 2050 | * existing key is not permitted. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 2051 | * |
| 2052 | * Implementations may set restrictions on supported key policies |
| 2053 | * depending on the key type and the key slot. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 2054 | * |
| 2055 | * \param key The key slot whose policy is to be changed. |
| 2056 | * \param[in] policy The policy object to query. |
| 2057 | * |
| 2058 | * \retval #PSA_SUCCESS |
| 2059 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 2060 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2061 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2062 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2063 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2064 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2065 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2066 | * The library has not been previously initialized by psa_crypto_init(). |
| 2067 | * It is implementation-dependent whether a failure to initialize |
| 2068 | * results in this error code. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 2069 | */ |
| 2070 | psa_status_t psa_set_key_policy(psa_key_slot_t key, |
| 2071 | const psa_key_policy_t *policy); |
| 2072 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 2073 | /** \brief Get the usage policy for a key slot. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 2074 | * |
| 2075 | * \param key The key slot whose policy is being queried. |
| 2076 | * \param[out] policy On success, the key's policy. |
| 2077 | * |
| 2078 | * \retval #PSA_SUCCESS |
| 2079 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2080 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2081 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2082 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2083 | * The library has not been previously initialized by psa_crypto_init(). |
| 2084 | * It is implementation-dependent whether a failure to initialize |
| 2085 | * results in this error code. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 2086 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 2087 | psa_status_t psa_get_key_policy(psa_key_slot_t key, |
| 2088 | psa_key_policy_t *policy); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2089 | |
| 2090 | /**@}*/ |
| 2091 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2092 | /** \defgroup hash Message digests |
| 2093 | * @{ |
| 2094 | */ |
| 2095 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2096 | /** The type of the state data structure for multipart hash operations. |
| 2097 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 2098 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2099 | * make any assumptions about the content of this structure except |
| 2100 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2101 | typedef struct psa_hash_operation_s psa_hash_operation_t; |
| 2102 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2103 | /** The size of the output of psa_hash_finish(), in bytes. |
| 2104 | * |
| 2105 | * This is also the hash size that psa_hash_verify() expects. |
| 2106 | * |
| 2107 | * \param alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2108 | * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 2109 | * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 2110 | * hash algorithm). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2111 | * |
| 2112 | * \return The hash size for the specified hash algorithm. |
| 2113 | * If the hash algorithm is not recognized, return 0. |
| 2114 | * An implementation may return either 0 or the correct size |
| 2115 | * for a hash algorithm that it recognizes, but does not support. |
| 2116 | */ |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 2117 | #define PSA_HASH_SIZE(alg) \ |
| 2118 | ( \ |
Gilles Peskine | 00709fa | 2018-08-22 18:25:41 +0200 | [diff] [blame] | 2119 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \ |
| 2120 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \ |
| 2121 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \ |
| 2122 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \ |
| 2123 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \ |
| 2124 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \ |
| 2125 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \ |
| 2126 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \ |
| 2127 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \ |
| 2128 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \ |
| 2129 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \ |
| 2130 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \ |
| 2131 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \ |
| 2132 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \ |
| 2133 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2134 | 0) |
| 2135 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2136 | /** Start a multipart hash operation. |
| 2137 | * |
| 2138 | * The sequence of operations to calculate a hash (message digest) |
| 2139 | * is as follows: |
| 2140 | * -# Allocate an operation object which will be passed to all the functions |
| 2141 | * listed here. |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2142 | * -# Call psa_hash_setup() to specify the algorithm. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2143 | * -# Call psa_hash_update() zero, one or more times, passing a fragment |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2144 | * of the message each time. The hash that is calculated is the hash |
| 2145 | * of the concatenation of these messages in order. |
| 2146 | * -# To calculate the hash, call psa_hash_finish(). |
| 2147 | * To compare the hash with an expected value, call psa_hash_verify(). |
| 2148 | * |
| 2149 | * 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] | 2150 | * has been initialized with psa_hash_setup(). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2151 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2152 | * After a successful call to psa_hash_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 2153 | * eventually terminate the operation. The following events terminate an |
| 2154 | * operation: |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2155 | * - A failed call to psa_hash_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 2156 | * - 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] | 2157 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2158 | * \param[out] operation The operation object to use. |
| 2159 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 2160 | * such that #PSA_ALG_IS_HASH(\p alg) is true). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2161 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2162 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2163 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2164 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2165 | * \p alg is not supported or is not a hash algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2166 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2167 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2168 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2169 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2170 | */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2171 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2172 | psa_algorithm_t alg); |
| 2173 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2174 | /** Add a message fragment to a multipart hash operation. |
| 2175 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2176 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2177 | * |
| 2178 | * If this function returns an error status, the operation becomes inactive. |
| 2179 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2180 | * \param[in,out] operation Active hash operation. |
| 2181 | * \param[in] input Buffer containing the message fragment to hash. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2182 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2183 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2184 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2185 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2186 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2187 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2188 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2189 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2190 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2191 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2192 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2193 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 2194 | const uint8_t *input, |
| 2195 | size_t input_length); |
| 2196 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2197 | /** Finish the calculation of the hash of a message. |
| 2198 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2199 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2200 | * This function calculates the hash of the message formed by concatenating |
| 2201 | * the inputs passed to preceding calls to psa_hash_update(). |
| 2202 | * |
| 2203 | * When this function returns, the operation becomes inactive. |
| 2204 | * |
| 2205 | * \warning Applications should not call this function if they expect |
| 2206 | * a specific value for the hash. Call psa_hash_verify() instead. |
| 2207 | * Beware that comparing integrity or authenticity data such as |
| 2208 | * hash values with a function such as \c memcmp is risky |
| 2209 | * because the time taken by the comparison may leak information |
| 2210 | * about the hashed data which could allow an attacker to guess |
| 2211 | * a valid hash and thereby bypass security controls. |
| 2212 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2213 | * \param[in,out] operation Active hash operation. |
| 2214 | * \param[out] hash Buffer where the hash is to be written. |
| 2215 | * \param hash_size Size of the \p hash buffer in bytes. |
| 2216 | * \param[out] hash_length On success, the number of bytes |
| 2217 | * that make up the hash value. This is always |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 2218 | * #PSA_HASH_SIZE(\c alg) where \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2219 | * hash algorithm that is calculated. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2220 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2221 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2222 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2223 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2224 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2225 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2226 | * 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] | 2227 | * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2228 | * where \c alg is the hash algorithm that is calculated. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2229 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2230 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2231 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2232 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2233 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2234 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2235 | uint8_t *hash, |
| 2236 | size_t hash_size, |
| 2237 | size_t *hash_length); |
| 2238 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2239 | /** Finish the calculation of the hash of a message and compare it with |
| 2240 | * an expected value. |
| 2241 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2242 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2243 | * This function calculates the hash of the message formed by concatenating |
| 2244 | * the inputs passed to preceding calls to psa_hash_update(). It then |
| 2245 | * compares the calculated hash with the expected hash passed as a |
| 2246 | * parameter to this function. |
| 2247 | * |
| 2248 | * When this function returns, the operation becomes inactive. |
| 2249 | * |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 2250 | * \note Implementations shall make the best effort to ensure that the |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2251 | * comparison between the actual hash and the expected hash is performed |
| 2252 | * in constant time. |
| 2253 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2254 | * \param[in,out] operation Active hash operation. |
| 2255 | * \param[in] hash Buffer containing the expected hash value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2256 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2257 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2258 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2259 | * The expected hash is identical to the actual hash of the message. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2260 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2261 | * The hash of the message was calculated successfully, but it |
| 2262 | * differs from the expected hash. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2263 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2264 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2265 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2266 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2267 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2268 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2269 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2270 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 2271 | const uint8_t *hash, |
| 2272 | size_t hash_length); |
| 2273 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2274 | /** Abort a hash operation. |
| 2275 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2276 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2277 | * \p operation structure itself. Once aborted, the operation object |
| 2278 | * can be reused for another operation by calling |
| 2279 | * psa_hash_setup() again. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2280 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2281 | * You may call this function any time after the operation object has |
| 2282 | * been initialized by any of the following methods: |
| 2283 | * - A call to psa_hash_setup(), whether it succeeds or not. |
| 2284 | * - Initializing the \c struct to all-bits-zero. |
| 2285 | * - Initializing the \c struct to logical zeros, e.g. |
| 2286 | * `psa_hash_operation_t operation = {0}`. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2287 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2288 | * In particular, calling psa_hash_abort() after the operation has been |
| 2289 | * terminated by a call to psa_hash_abort(), psa_hash_finish() or |
| 2290 | * psa_hash_verify() is safe and has no effect. |
| 2291 | * |
| 2292 | * \param[in,out] operation Initialized hash operation. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2293 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2294 | * \retval #PSA_SUCCESS |
| 2295 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2296 | * \p operation is not an active hash operation. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2297 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2298 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2299 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2300 | */ |
| 2301 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2302 | |
| 2303 | /**@}*/ |
| 2304 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2305 | /** \defgroup MAC Message authentication codes |
| 2306 | * @{ |
| 2307 | */ |
| 2308 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2309 | /** The type of the state data structure for multipart MAC operations. |
| 2310 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 2311 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2312 | * make any assumptions about the content of this structure except |
| 2313 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2314 | typedef struct psa_mac_operation_s psa_mac_operation_t; |
| 2315 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2316 | /** Start a multipart MAC calculation operation. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2317 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2318 | * This function sets up the calculation of the MAC |
| 2319 | * (message authentication code) of a byte string. |
| 2320 | * To verify the MAC of a message against an |
| 2321 | * expected value, use psa_mac_verify_setup() instead. |
| 2322 | * |
| 2323 | * The sequence of operations to calculate a MAC is as follows: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2324 | * -# Allocate an operation object which will be passed to all the functions |
| 2325 | * listed here. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2326 | * -# Call psa_mac_sign_setup() to specify the algorithm and key. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2327 | * The key remains associated with the operation even if the content |
| 2328 | * of the key slot changes. |
| 2329 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 2330 | * of the message each time. The MAC that is calculated is the MAC |
| 2331 | * of the concatenation of these messages in order. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2332 | * -# At the end of the message, call psa_mac_sign_finish() to finish |
| 2333 | * calculating the MAC value and retrieve it. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2334 | * |
| 2335 | * 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] | 2336 | * has been initialized with psa_mac_sign_setup(). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2337 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2338 | * After a successful call to psa_mac_sign_setup(), the application must |
| 2339 | * eventually terminate the operation through one of the following methods: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2340 | * - A failed call to psa_mac_update(). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2341 | * - A call to psa_mac_sign_finish() or psa_mac_abort(). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2342 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2343 | * \param[out] operation The operation object to use. |
| 2344 | * \param key Slot containing the key to use for the operation. |
| 2345 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 2346 | * such that #PSA_ALG_IS_MAC(alg) is true). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2347 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2348 | * \retval #PSA_SUCCESS |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2349 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2350 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2351 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2352 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2353 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2354 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2355 | * \p alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2356 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2357 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2358 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2359 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2360 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2361 | * The library has not been previously initialized by psa_crypto_init(). |
| 2362 | * It is implementation-dependent whether a failure to initialize |
| 2363 | * results in this error code. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 2364 | */ |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2365 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2366 | psa_key_slot_t key, |
| 2367 | psa_algorithm_t alg); |
| 2368 | |
| 2369 | /** Start a multipart MAC verification operation. |
| 2370 | * |
| 2371 | * This function sets up the verification of the MAC |
| 2372 | * (message authentication code) of a byte string against an expected value. |
| 2373 | * |
| 2374 | * The sequence of operations to verify a MAC is as follows: |
| 2375 | * -# Allocate an operation object which will be passed to all the functions |
| 2376 | * listed here. |
| 2377 | * -# Call psa_mac_verify_setup() to specify the algorithm and key. |
| 2378 | * The key remains associated with the operation even if the content |
| 2379 | * of the key slot changes. |
| 2380 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 2381 | * of the message each time. The MAC that is calculated is the MAC |
| 2382 | * of the concatenation of these messages in order. |
| 2383 | * -# At the end of the message, call psa_mac_verify_finish() to finish |
| 2384 | * calculating the actual MAC of the message and verify it against |
| 2385 | * the expected value. |
| 2386 | * |
| 2387 | * The application may call psa_mac_abort() at any time after the operation |
| 2388 | * has been initialized with psa_mac_verify_setup(). |
| 2389 | * |
| 2390 | * After a successful call to psa_mac_verify_setup(), the application must |
| 2391 | * eventually terminate the operation through one of the following methods: |
| 2392 | * - A failed call to psa_mac_update(). |
| 2393 | * - A call to psa_mac_verify_finish() or psa_mac_abort(). |
| 2394 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2395 | * \param[out] operation The operation object to use. |
| 2396 | * \param key Slot containing the key to use for the operation. |
| 2397 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 2398 | * such that #PSA_ALG_IS_MAC(\p alg) is true). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2399 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2400 | * \retval #PSA_SUCCESS |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2401 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2402 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2403 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2404 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2405 | * \c key is not compatible with \c alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2406 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2407 | * \c alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2408 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2409 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2410 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2411 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2412 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2413 | * The library has not been previously initialized by psa_crypto_init(). |
| 2414 | * It is implementation-dependent whether a failure to initialize |
| 2415 | * results in this error code. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2416 | */ |
| 2417 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2418 | psa_key_slot_t key, |
| 2419 | psa_algorithm_t alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2420 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2421 | /** Add a message fragment to a multipart MAC operation. |
| 2422 | * |
| 2423 | * The application must call psa_mac_sign_setup() or psa_mac_verify_setup() |
| 2424 | * before calling this function. |
| 2425 | * |
| 2426 | * If this function returns an error status, the operation becomes inactive. |
| 2427 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2428 | * \param[in,out] operation Active MAC operation. |
| 2429 | * \param[in] input Buffer containing the message fragment to add to |
| 2430 | * the MAC calculation. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2431 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2432 | * |
| 2433 | * \retval #PSA_SUCCESS |
| 2434 | * Success. |
| 2435 | * \retval #PSA_ERROR_BAD_STATE |
| 2436 | * The operation state is not valid (not started, or already completed). |
| 2437 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2438 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2439 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2440 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2441 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2442 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 2443 | const uint8_t *input, |
| 2444 | size_t input_length); |
| 2445 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2446 | /** Finish the calculation of the MAC of a message. |
| 2447 | * |
| 2448 | * The application must call psa_mac_sign_setup() before calling this function. |
| 2449 | * This function calculates the MAC of the message formed by concatenating |
| 2450 | * the inputs passed to preceding calls to psa_mac_update(). |
| 2451 | * |
| 2452 | * When this function returns, the operation becomes inactive. |
| 2453 | * |
| 2454 | * \warning Applications should not call this function if they expect |
| 2455 | * a specific value for the MAC. Call psa_mac_verify_finish() instead. |
| 2456 | * Beware that comparing integrity or authenticity data such as |
| 2457 | * MAC values with a function such as \c memcmp is risky |
| 2458 | * because the time taken by the comparison may leak information |
| 2459 | * about the MAC value which could allow an attacker to guess |
| 2460 | * a valid MAC and thereby bypass security controls. |
| 2461 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2462 | * \param[in,out] operation Active MAC operation. |
| 2463 | * \param[out] mac Buffer where the MAC value is to be written. |
| 2464 | * \param mac_size Size of the \p mac buffer in bytes. |
| 2465 | * \param[out] mac_length On success, the number of bytes |
| 2466 | * that make up the MAC value. This is always |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2467 | * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg) |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2468 | * where \c key_type and \c key_bits are the type and |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2469 | * bit-size respectively of the key and \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2470 | * MAC algorithm that is calculated. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2471 | * |
| 2472 | * \retval #PSA_SUCCESS |
| 2473 | * Success. |
| 2474 | * \retval #PSA_ERROR_BAD_STATE |
| 2475 | * The operation state is not valid (not started, or already completed). |
| 2476 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2477 | * 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] | 2478 | * sufficient buffer size by calling PSA_MAC_FINAL_SIZE(). |
| 2479 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2480 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2481 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2482 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2483 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2484 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 2485 | uint8_t *mac, |
| 2486 | size_t mac_size, |
| 2487 | size_t *mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2488 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2489 | /** Finish the calculation of the MAC of a message and compare it with |
| 2490 | * an expected value. |
| 2491 | * |
| 2492 | * The application must call psa_mac_verify_setup() before calling this function. |
| 2493 | * This function calculates the MAC of the message formed by concatenating |
| 2494 | * the inputs passed to preceding calls to psa_mac_update(). It then |
| 2495 | * compares the calculated MAC with the expected MAC passed as a |
| 2496 | * parameter to this function. |
| 2497 | * |
| 2498 | * When this function returns, the operation becomes inactive. |
| 2499 | * |
| 2500 | * \note Implementations shall make the best effort to ensure that the |
| 2501 | * comparison between the actual MAC and the expected MAC is performed |
| 2502 | * in constant time. |
| 2503 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2504 | * \param[in,out] operation Active MAC operation. |
| 2505 | * \param[in] mac Buffer containing the expected MAC value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2506 | * \param mac_length Size of the \p mac buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2507 | * |
| 2508 | * \retval #PSA_SUCCESS |
| 2509 | * The expected MAC is identical to the actual MAC of the message. |
| 2510 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 2511 | * The MAC of the message was calculated successfully, but it |
| 2512 | * differs from the expected MAC. |
| 2513 | * \retval #PSA_ERROR_BAD_STATE |
| 2514 | * The operation state is not valid (not started, or already completed). |
| 2515 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2516 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2517 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2518 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2519 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2520 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2521 | const uint8_t *mac, |
| 2522 | size_t mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2523 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2524 | /** Abort a MAC operation. |
| 2525 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2526 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2527 | * \p operation structure itself. Once aborted, the operation object |
| 2528 | * can be reused for another operation by calling |
| 2529 | * psa_mac_sign_setup() or psa_mac_verify_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2530 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2531 | * You may call this function any time after the operation object has |
| 2532 | * been initialized by any of the following methods: |
| 2533 | * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether |
| 2534 | * it succeeds or not. |
| 2535 | * - Initializing the \c struct to all-bits-zero. |
| 2536 | * - Initializing the \c struct to logical zeros, e.g. |
| 2537 | * `psa_mac_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2538 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2539 | * In particular, calling psa_mac_abort() after the operation has been |
| 2540 | * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or |
| 2541 | * psa_mac_verify_finish() is safe and has no effect. |
| 2542 | * |
| 2543 | * \param[in,out] operation Initialized MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2544 | * |
| 2545 | * \retval #PSA_SUCCESS |
| 2546 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2547 | * \p operation is not an active MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2548 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2549 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2550 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2551 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2552 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation); |
| 2553 | |
| 2554 | /**@}*/ |
| 2555 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2556 | /** \defgroup cipher Symmetric ciphers |
| 2557 | * @{ |
| 2558 | */ |
| 2559 | |
| 2560 | /** The type of the state data structure for multipart cipher operations. |
| 2561 | * |
| 2562 | * This is an implementation-defined \c struct. Applications should not |
| 2563 | * make any assumptions about the content of this structure except |
| 2564 | * as directed by the documentation of a specific implementation. */ |
| 2565 | typedef struct psa_cipher_operation_s psa_cipher_operation_t; |
| 2566 | |
| 2567 | /** Set the key for a multipart symmetric encryption operation. |
| 2568 | * |
| 2569 | * The sequence of operations to encrypt a message with a symmetric cipher |
| 2570 | * is as follows: |
| 2571 | * -# Allocate an operation object which will be passed to all the functions |
| 2572 | * listed here. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2573 | * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2574 | * The key remains associated with the operation even if the content |
| 2575 | * of the key slot changes. |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 2576 | * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2577 | * generate or set the IV (initialization vector). You should use |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 2578 | * psa_cipher_generate_iv() unless the protocol you are implementing |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2579 | * requires a specific IV value. |
| 2580 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 2581 | * of the message each time. |
| 2582 | * -# Call psa_cipher_finish(). |
| 2583 | * |
| 2584 | * 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] | 2585 | * has been initialized with psa_cipher_encrypt_setup(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2586 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2587 | * After a successful call to psa_cipher_encrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 2588 | * eventually terminate the operation. The following events terminate an |
| 2589 | * operation: |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 2590 | * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv() |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2591 | * or psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 2592 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2593 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2594 | * \param[out] operation The operation object to use. |
| 2595 | * \param key Slot containing the key to use for the operation. |
| 2596 | * \param alg The cipher algorithm to compute |
| 2597 | * (\c PSA_ALG_XXX value such that |
| 2598 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2599 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2600 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2601 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2602 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2603 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2604 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2605 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2606 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2607 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2608 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2609 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2610 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2611 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2612 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2613 | * The library has not been previously initialized by psa_crypto_init(). |
| 2614 | * It is implementation-dependent whether a failure to initialize |
| 2615 | * results in this error code. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2616 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2617 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 2618 | psa_key_slot_t key, |
| 2619 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2620 | |
| 2621 | /** Set the key for a multipart symmetric decryption operation. |
| 2622 | * |
| 2623 | * The sequence of operations to decrypt a message with a symmetric cipher |
| 2624 | * is as follows: |
| 2625 | * -# Allocate an operation object which will be passed to all the functions |
| 2626 | * listed here. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2627 | * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2628 | * The key remains associated with the operation even if the content |
| 2629 | * of the key slot changes. |
| 2630 | * -# Call psa_cipher_update() with the IV (initialization vector) for the |
| 2631 | * decryption. If the IV is prepended to the ciphertext, you can call |
| 2632 | * psa_cipher_update() on a buffer containing the IV followed by the |
| 2633 | * beginning of the message. |
| 2634 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 2635 | * of the message each time. |
| 2636 | * -# Call psa_cipher_finish(). |
| 2637 | * |
| 2638 | * 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] | 2639 | * has been initialized with psa_cipher_decrypt_setup(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2640 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2641 | * After a successful call to psa_cipher_decrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 2642 | * eventually terminate the operation. The following events terminate an |
| 2643 | * operation: |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2644 | * - A failed call to psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 2645 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2646 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2647 | * \param[out] operation The operation object to use. |
| 2648 | * \param key Slot containing the key to use for the operation. |
| 2649 | * \param alg The cipher algorithm to compute |
| 2650 | * (\c PSA_ALG_XXX value such that |
| 2651 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2652 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2653 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2654 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2655 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2656 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2657 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2658 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2659 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2660 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2661 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2662 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2663 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2664 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2665 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2666 | * The library has not been previously initialized by psa_crypto_init(). |
| 2667 | * It is implementation-dependent whether a failure to initialize |
| 2668 | * results in this error code. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2669 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2670 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 2671 | psa_key_slot_t key, |
| 2672 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2673 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2674 | /** Generate an IV for a symmetric encryption operation. |
| 2675 | * |
| 2676 | * This function generates a random IV (initialization vector), nonce |
| 2677 | * or initial counter value for the encryption operation as appropriate |
| 2678 | * for the chosen algorithm, key type and key size. |
| 2679 | * |
| 2680 | * The application must call psa_cipher_encrypt_setup() before |
| 2681 | * calling this function. |
| 2682 | * |
| 2683 | * If this function returns an error status, the operation becomes inactive. |
| 2684 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2685 | * \param[in,out] operation Active cipher operation. |
| 2686 | * \param[out] iv Buffer where the generated IV is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2687 | * \param iv_size Size of the \p iv buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2688 | * \param[out] iv_length On success, the number of bytes of the |
| 2689 | * generated IV. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2690 | * |
| 2691 | * \retval #PSA_SUCCESS |
| 2692 | * Success. |
| 2693 | * \retval #PSA_ERROR_BAD_STATE |
| 2694 | * The operation state is not valid (not started, or IV already set). |
| 2695 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2696 | * The size of the \p iv buffer is too small. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2697 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2698 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2699 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2700 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2701 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2702 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 2703 | unsigned char *iv, |
| 2704 | size_t iv_size, |
| 2705 | size_t *iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2706 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2707 | /** Set the IV for a symmetric encryption or decryption operation. |
| 2708 | * |
| 2709 | * This function sets the random IV (initialization vector), nonce |
| 2710 | * or initial counter value for the encryption or decryption operation. |
| 2711 | * |
| 2712 | * The application must call psa_cipher_encrypt_setup() before |
| 2713 | * calling this function. |
| 2714 | * |
| 2715 | * If this function returns an error status, the operation becomes inactive. |
| 2716 | * |
| 2717 | * \note When encrypting, applications should use psa_cipher_generate_iv() |
| 2718 | * instead of this function, unless implementing a protocol that requires |
| 2719 | * a non-random IV. |
| 2720 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2721 | * \param[in,out] operation Active cipher operation. |
| 2722 | * \param[in] iv Buffer containing the IV to use. |
| 2723 | * \param iv_length Size of the IV in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2724 | * |
| 2725 | * \retval #PSA_SUCCESS |
| 2726 | * Success. |
| 2727 | * \retval #PSA_ERROR_BAD_STATE |
| 2728 | * The operation state is not valid (not started, or IV already set). |
| 2729 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2730 | * The size of \p iv is not acceptable for the chosen algorithm, |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2731 | * or the chosen algorithm does not use an IV. |
| 2732 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2733 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2734 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2735 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2736 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2737 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 2738 | const unsigned char *iv, |
| 2739 | size_t iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2740 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2741 | /** Encrypt or decrypt a message fragment in an active cipher operation. |
| 2742 | * |
Gilles Peskine | 9ac9426 | 2018-07-12 20:15:32 +0200 | [diff] [blame] | 2743 | * Before calling this function, you must: |
| 2744 | * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(). |
| 2745 | * The choice of setup function determines whether this function |
| 2746 | * encrypts or decrypts its input. |
| 2747 | * 2. If the algorithm requires an IV, call psa_cipher_generate_iv() |
| 2748 | * (recommended when encrypting) or psa_cipher_set_iv(). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2749 | * |
| 2750 | * If this function returns an error status, the operation becomes inactive. |
| 2751 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2752 | * \param[in,out] operation Active cipher operation. |
| 2753 | * \param[in] input Buffer containing the message fragment to |
| 2754 | * encrypt or decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2755 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2756 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2757 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2758 | * \param[out] output_length On success, the number of bytes |
| 2759 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2760 | * |
| 2761 | * \retval #PSA_SUCCESS |
| 2762 | * Success. |
| 2763 | * \retval #PSA_ERROR_BAD_STATE |
| 2764 | * The operation state is not valid (not started, IV required but |
| 2765 | * not set, or already completed). |
| 2766 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 2767 | * The size of the \p output buffer is too small. |
| 2768 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2769 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2770 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2771 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2772 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2773 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 2774 | const uint8_t *input, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2775 | size_t input_length, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2776 | unsigned char *output, |
| 2777 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2778 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2779 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2780 | /** Finish encrypting or decrypting a message in a cipher operation. |
| 2781 | * |
| 2782 | * The application must call psa_cipher_encrypt_setup() or |
| 2783 | * psa_cipher_decrypt_setup() before calling this function. The choice |
| 2784 | * of setup function determines whether this function encrypts or |
| 2785 | * decrypts its input. |
| 2786 | * |
| 2787 | * This function finishes the encryption or decryption of the message |
| 2788 | * formed by concatenating the inputs passed to preceding calls to |
| 2789 | * psa_cipher_update(). |
| 2790 | * |
| 2791 | * When this function returns, the operation becomes inactive. |
| 2792 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2793 | * \param[in,out] operation Active cipher operation. |
| 2794 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2795 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2796 | * \param[out] output_length On success, the number of bytes |
| 2797 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2798 | * |
| 2799 | * \retval #PSA_SUCCESS |
| 2800 | * Success. |
| 2801 | * \retval #PSA_ERROR_BAD_STATE |
| 2802 | * The operation state is not valid (not started, IV required but |
| 2803 | * not set, or already completed). |
| 2804 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 2805 | * The size of the \p output buffer is too small. |
| 2806 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2807 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2808 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2809 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2810 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2811 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2812 | uint8_t *output, |
Moran Peker | 0071b87 | 2018-04-22 20:16:58 +0300 | [diff] [blame] | 2813 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2814 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2815 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2816 | /** Abort a cipher operation. |
| 2817 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2818 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2819 | * \p operation structure itself. Once aborted, the operation object |
| 2820 | * can be reused for another operation by calling |
| 2821 | * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2822 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2823 | * You may call this function any time after the operation object has |
| 2824 | * been initialized by any of the following methods: |
| 2825 | * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(), |
| 2826 | * whether it succeeds or not. |
| 2827 | * - Initializing the \c struct to all-bits-zero. |
| 2828 | * - Initializing the \c struct to logical zeros, e.g. |
| 2829 | * `psa_cipher_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2830 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2831 | * In particular, calling psa_cipher_abort() after the operation has been |
| 2832 | * terminated by a call to psa_cipher_abort() or psa_cipher_finish() |
| 2833 | * is safe and has no effect. |
| 2834 | * |
| 2835 | * \param[in,out] operation Initialized cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2836 | * |
| 2837 | * \retval #PSA_SUCCESS |
| 2838 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2839 | * \p operation is not an active cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2840 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2841 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2842 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2843 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2844 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); |
| 2845 | |
| 2846 | /**@}*/ |
| 2847 | |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2848 | /** \defgroup aead Authenticated encryption with associated data (AEAD) |
| 2849 | * @{ |
| 2850 | */ |
| 2851 | |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 2852 | /** The tag size for an AEAD algorithm, in bytes. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2853 | * |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 2854 | * \param alg An AEAD algorithm |
| 2855 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2856 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 2857 | * |
| 2858 | * \return The tag size for the specified algorithm. |
| 2859 | * If the AEAD algorithm does not have an identified |
| 2860 | * tag that can be distinguished from the rest of |
| 2861 | * the ciphertext, return 0. |
| 2862 | * If the AEAD algorithm is not recognized, return 0. |
| 2863 | * An implementation may return either 0 or a |
| 2864 | * correct size for an AEAD algorithm that it |
| 2865 | * recognizes, but does not support. |
| 2866 | */ |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 2867 | #define PSA_AEAD_TAG_LENGTH(alg) \ |
| 2868 | (PSA_ALG_IS_AEAD(alg) ? \ |
| 2869 | (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \ |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 2870 | 0) |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2871 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2872 | /** Process an authenticated encryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2873 | * |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2874 | * \param key Slot containing the key to use. |
| 2875 | * \param alg The AEAD algorithm to compute |
| 2876 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2877 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2878 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2879 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2880 | * \param[in] additional_data Additional data that will be authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2881 | * but not encrypted. |
| 2882 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2883 | * \param[in] plaintext Data that will be authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2884 | * encrypted. |
| 2885 | * \param plaintext_length Size of \p plaintext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2886 | * \param[out] ciphertext Output buffer for the authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2887 | * encrypted data. The additional data is not |
| 2888 | * part of this output. For algorithms where the |
| 2889 | * encrypted data and the authentication tag |
| 2890 | * are defined as separate outputs, the |
| 2891 | * authentication tag is appended to the |
| 2892 | * encrypted data. |
| 2893 | * \param ciphertext_size Size of the \p ciphertext buffer in bytes. |
| 2894 | * This must be at least |
| 2895 | * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, |
| 2896 | * \p plaintext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2897 | * \param[out] ciphertext_length On success, the size of the output |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2898 | * in the \b ciphertext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2899 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2900 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2901 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2902 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2903 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2904 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2905 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2906 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2907 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2908 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2909 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2910 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2911 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2912 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2913 | * The library has not been previously initialized by psa_crypto_init(). |
| 2914 | * It is implementation-dependent whether a failure to initialize |
| 2915 | * results in this error code. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2916 | */ |
Gilles Peskine | 9fb0e01 | 2018-07-19 15:51:49 +0200 | [diff] [blame] | 2917 | psa_status_t psa_aead_encrypt(psa_key_slot_t key, |
| 2918 | psa_algorithm_t alg, |
| 2919 | const uint8_t *nonce, |
| 2920 | size_t nonce_length, |
| 2921 | const uint8_t *additional_data, |
| 2922 | size_t additional_data_length, |
| 2923 | const uint8_t *plaintext, |
| 2924 | size_t plaintext_length, |
| 2925 | uint8_t *ciphertext, |
| 2926 | size_t ciphertext_size, |
| 2927 | size_t *ciphertext_length); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2928 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2929 | /** Process an authenticated decryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2930 | * |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2931 | * \param key Slot containing the key to use. |
| 2932 | * \param alg The AEAD algorithm to compute |
| 2933 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2934 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2935 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2936 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2937 | * \param[in] additional_data Additional data that has been authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2938 | * but not encrypted. |
| 2939 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2940 | * \param[in] ciphertext Data that has been authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2941 | * encrypted. For algorithms where the |
| 2942 | * encrypted data and the authentication tag |
| 2943 | * are defined as separate inputs, the buffer |
| 2944 | * must contain the encrypted data followed |
| 2945 | * by the authentication tag. |
| 2946 | * \param ciphertext_length Size of \p ciphertext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2947 | * \param[out] plaintext Output buffer for the decrypted data. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2948 | * \param plaintext_size Size of the \p plaintext buffer in bytes. |
| 2949 | * This must be at least |
| 2950 | * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, |
| 2951 | * \p ciphertext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2952 | * \param[out] plaintext_length On success, the size of the output |
mohammad1603 | fb5b9cb | 2018-06-06 13:44:27 +0300 | [diff] [blame] | 2953 | * in the \b plaintext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2954 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2955 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2956 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2957 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2958 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2959 | * The ciphertext is not authentic. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2960 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2961 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2962 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2963 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2964 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2965 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2966 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2967 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2968 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2969 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2970 | * The library has not been previously initialized by psa_crypto_init(). |
| 2971 | * It is implementation-dependent whether a failure to initialize |
| 2972 | * results in this error code. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2973 | */ |
Gilles Peskine | 9fb0e01 | 2018-07-19 15:51:49 +0200 | [diff] [blame] | 2974 | psa_status_t psa_aead_decrypt(psa_key_slot_t key, |
| 2975 | psa_algorithm_t alg, |
| 2976 | const uint8_t *nonce, |
| 2977 | size_t nonce_length, |
| 2978 | const uint8_t *additional_data, |
| 2979 | size_t additional_data_length, |
| 2980 | const uint8_t *ciphertext, |
| 2981 | size_t ciphertext_length, |
| 2982 | uint8_t *plaintext, |
| 2983 | size_t plaintext_size, |
| 2984 | size_t *plaintext_length); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2985 | |
| 2986 | /**@}*/ |
| 2987 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2988 | /** \defgroup asymmetric Asymmetric cryptography |
| 2989 | * @{ |
| 2990 | */ |
| 2991 | |
| 2992 | /** |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2993 | * \brief ECDSA signature size for a given curve bit size |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2994 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2995 | * \param curve_bits Curve size in bits. |
| 2996 | * \return Signature size in bytes. |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2997 | * |
| 2998 | * \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] | 2999 | */ |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 3000 | #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ |
| 3001 | (PSA_BITS_TO_BYTES(curve_bits) * 2) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3002 | |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3003 | /** |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3004 | * \brief Sign a hash or short message with a private key. |
| 3005 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 3006 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 3007 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 3008 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 3009 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 3010 | * to determine the hash algorithm to use. |
| 3011 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3012 | * \param key Key slot containing an asymmetric key pair. |
| 3013 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3014 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3015 | * \param[in] hash The hash or message to sign. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3016 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3017 | * \param[out] signature Buffer where the signature is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3018 | * \param signature_size Size of the \p signature buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3019 | * \param[out] signature_length On success, the number of bytes |
| 3020 | * that make up the returned signature value. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 3021 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3022 | * \retval #PSA_SUCCESS |
| 3023 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3024 | * The size of the \p signature buffer is too small. You can |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 3025 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 3026 | * #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] | 3027 | * 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] | 3028 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3029 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 3030 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 3031 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 3032 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3033 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3034 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 3035 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 3036 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 3037 | * The library has not been previously initialized by psa_crypto_init(). |
| 3038 | * It is implementation-dependent whether a failure to initialize |
| 3039 | * results in this error code. |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3040 | */ |
| 3041 | psa_status_t psa_asymmetric_sign(psa_key_slot_t key, |
| 3042 | psa_algorithm_t alg, |
| 3043 | const uint8_t *hash, |
| 3044 | size_t hash_length, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3045 | uint8_t *signature, |
| 3046 | size_t signature_size, |
| 3047 | size_t *signature_length); |
| 3048 | |
| 3049 | /** |
| 3050 | * \brief Verify the signature a hash or short message using a public key. |
| 3051 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 3052 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 3053 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 3054 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 3055 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 3056 | * to determine the hash algorithm to use. |
| 3057 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 3058 | * \param key Key slot containing a public key or an |
| 3059 | * asymmetric key pair. |
| 3060 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3061 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3062 | * \param[in] hash The hash or message whose signature is to be |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 3063 | * verified. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3064 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3065 | * \param[in] signature Buffer containing the signature to verify. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3066 | * \param signature_length Size of the \p signature buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 3067 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3068 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 3069 | * The signature is valid. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3070 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 3071 | * The calculation was perfomed successfully, but the passed |
| 3072 | * signature is not a valid signature. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3073 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 3074 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 3075 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 3076 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3077 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3078 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 3079 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 3080 | * The library has not been previously initialized by psa_crypto_init(). |
| 3081 | * It is implementation-dependent whether a failure to initialize |
| 3082 | * results in this error code. |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3083 | */ |
| 3084 | psa_status_t psa_asymmetric_verify(psa_key_slot_t key, |
| 3085 | psa_algorithm_t alg, |
| 3086 | const uint8_t *hash, |
| 3087 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 3088 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 3089 | size_t signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3090 | |
Gilles Peskine | 723feff | 2018-05-31 20:08:13 +0200 | [diff] [blame] | 3091 | #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \ |
Gilles Peskine | 072ac56 | 2018-06-30 00:21:29 +0200 | [diff] [blame] | 3092 | (PSA_ALG_IS_RSA_OAEP(alg) ? \ |
| 3093 | 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] | 3094 | 11 /*PKCS#1v1.5*/) |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 3095 | |
| 3096 | /** |
| 3097 | * \brief Encrypt a short message with a public key. |
| 3098 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3099 | * \param key Key slot containing a public key or an |
| 3100 | * asymmetric key pair. |
| 3101 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3102 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3103 | * \param[in] input The message to encrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3104 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3105 | * \param[in] salt A salt or label, if supported by the |
| 3106 | * encryption algorithm. |
| 3107 | * If the algorithm does not support a |
| 3108 | * salt, pass \c NULL. |
| 3109 | * If the algorithm supports an optional |
| 3110 | * salt and you do not want to pass a salt, |
| 3111 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 3112 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3113 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 3114 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3115 | * \param salt_length Size of the \p salt buffer in bytes. |
| 3116 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3117 | * \param[out] output Buffer where the encrypted message is to |
| 3118 | * be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3119 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3120 | * \param[out] output_length On success, the number of bytes |
| 3121 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 3122 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3123 | * \retval #PSA_SUCCESS |
| 3124 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3125 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 3126 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 3127 | * #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] | 3128 | * 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] | 3129 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3130 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 3131 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 3132 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 3133 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3134 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3135 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 3136 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 3137 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 3138 | * The library has not been previously initialized by psa_crypto_init(). |
| 3139 | * It is implementation-dependent whether a failure to initialize |
| 3140 | * results in this error code. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 3141 | */ |
| 3142 | psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key, |
| 3143 | psa_algorithm_t alg, |
| 3144 | const uint8_t *input, |
| 3145 | size_t input_length, |
| 3146 | const uint8_t *salt, |
| 3147 | size_t salt_length, |
| 3148 | uint8_t *output, |
| 3149 | size_t output_size, |
| 3150 | size_t *output_length); |
| 3151 | |
| 3152 | /** |
| 3153 | * \brief Decrypt a short message with a private key. |
| 3154 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3155 | * \param key Key slot containing an asymmetric key pair. |
| 3156 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3157 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3158 | * \param[in] input The message to decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3159 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3160 | * \param[in] salt A salt or label, if supported by the |
| 3161 | * encryption algorithm. |
| 3162 | * If the algorithm does not support a |
| 3163 | * salt, pass \c NULL. |
| 3164 | * If the algorithm supports an optional |
| 3165 | * salt and you do not want to pass a salt, |
| 3166 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 3167 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3168 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 3169 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3170 | * \param salt_length Size of the \p salt buffer in bytes. |
| 3171 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3172 | * \param[out] output Buffer where the decrypted message is to |
| 3173 | * be written. |
| 3174 | * \param output_size Size of the \c output buffer in bytes. |
| 3175 | * \param[out] output_length On success, the number of bytes |
| 3176 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 3177 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3178 | * \retval #PSA_SUCCESS |
| 3179 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3180 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 3181 | * determine a sufficient buffer size by calling |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 3182 | * #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] | 3183 | * 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] | 3184 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3185 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 3186 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 3187 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 3188 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3189 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3190 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 3191 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 3192 | * \retval #PSA_ERROR_INVALID_PADDING |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 3193 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 3194 | * The library has not been previously initialized by psa_crypto_init(). |
| 3195 | * It is implementation-dependent whether a failure to initialize |
| 3196 | * results in this error code. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 3197 | */ |
| 3198 | psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key, |
| 3199 | psa_algorithm_t alg, |
| 3200 | const uint8_t *input, |
| 3201 | size_t input_length, |
| 3202 | const uint8_t *salt, |
| 3203 | size_t salt_length, |
| 3204 | uint8_t *output, |
| 3205 | size_t output_size, |
| 3206 | size_t *output_length); |
| 3207 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 3208 | /**@}*/ |
| 3209 | |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 3210 | /** \defgroup generators Generators |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3211 | * @{ |
| 3212 | */ |
| 3213 | |
| 3214 | /** The type of the state data structure for generators. |
| 3215 | * |
| 3216 | * Before calling any function on a generator, the application must |
| 3217 | * initialize it by any of the following means: |
| 3218 | * - Set the structure to all-bits-zero, for example: |
| 3219 | * \code |
| 3220 | * psa_crypto_generator_t generator; |
| 3221 | * memset(&generator, 0, sizeof(generator)); |
| 3222 | * \endcode |
| 3223 | * - Initialize the structure to logical zero values, for example: |
| 3224 | * \code |
| 3225 | * psa_crypto_generator_t generator = {0}; |
| 3226 | * \endcode |
| 3227 | * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT, |
| 3228 | * for example: |
| 3229 | * \code |
| 3230 | * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3231 | * \endcode |
| 3232 | * - Assign the result of the function psa_crypto_generator_init() |
| 3233 | * to the structure, for example: |
| 3234 | * \code |
| 3235 | * psa_crypto_generator_t generator; |
| 3236 | * generator = psa_crypto_generator_init(); |
| 3237 | * \endcode |
| 3238 | * |
| 3239 | * This is an implementation-defined \c struct. Applications should not |
| 3240 | * make any assumptions about the content of this structure except |
| 3241 | * as directed by the documentation of a specific implementation. |
| 3242 | */ |
| 3243 | typedef struct psa_crypto_generator_s psa_crypto_generator_t; |
| 3244 | |
| 3245 | /** \def PSA_CRYPTO_GENERATOR_INIT |
| 3246 | * |
| 3247 | * This macro returns a suitable initializer for a generator object |
| 3248 | * of type #psa_crypto_generator_t. |
| 3249 | */ |
| 3250 | #ifdef __DOXYGEN_ONLY__ |
| 3251 | /* This is an example definition for documentation purposes. |
| 3252 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 3253 | */ |
| 3254 | #define PSA_CRYPTO_GENERATOR_INIT {0} |
| 3255 | #endif |
| 3256 | |
| 3257 | /** Return an initial value for a generator object. |
| 3258 | */ |
| 3259 | static psa_crypto_generator_t psa_crypto_generator_init(void); |
| 3260 | |
| 3261 | /** Retrieve the current capacity of a generator. |
| 3262 | * |
| 3263 | * The capacity of a generator is the maximum number of bytes that it can |
| 3264 | * return. Reading *N* bytes from a generator reduces its capacity by *N*. |
| 3265 | * |
| 3266 | * \param[in] generator The generator to query. |
| 3267 | * \param[out] capacity On success, the capacity of the generator. |
| 3268 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame^] | 3269 | * \retval #PSA_SUCCESS |
| 3270 | * \retval #PSA_ERROR_BAD_STATE |
| 3271 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3272 | */ |
| 3273 | psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator, |
| 3274 | size_t *capacity); |
| 3275 | |
| 3276 | /** Read some data from a generator. |
| 3277 | * |
| 3278 | * This function reads and returns a sequence of bytes from a generator. |
| 3279 | * The data that is read is discarded from the generator. The generator's |
| 3280 | * capacity is decreased by the number of bytes read. |
| 3281 | * |
| 3282 | * \param[in,out] generator The generator object to read from. |
| 3283 | * \param[out] output Buffer where the generator output will be |
| 3284 | * written. |
| 3285 | * \param output_length Number of bytes to output. |
| 3286 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame^] | 3287 | * \retval #PSA_SUCCESS |
| 3288 | * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3289 | * There were fewer than \p output_length bytes |
| 3290 | * in the generator. Note that in this case, no |
| 3291 | * output is written to the output buffer. |
| 3292 | * The generator's capacity is set to 0, thus |
| 3293 | * subsequent calls to this function will not |
| 3294 | * succeed, even with a smaller output buffer. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame^] | 3295 | * \retval #PSA_ERROR_BAD_STATE |
| 3296 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 3297 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3298 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3299 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3300 | */ |
| 3301 | psa_status_t psa_generator_read(psa_crypto_generator_t *generator, |
| 3302 | uint8_t *output, |
| 3303 | size_t output_length); |
| 3304 | |
| 3305 | /** Create a symmetric key from data read from a generator. |
| 3306 | * |
| 3307 | * This function reads a sequence of bytes from a generator and imports |
| 3308 | * these bytes as a key. |
| 3309 | * The data that is read is discarded from the generator. The generator's |
| 3310 | * capacity is decreased by the number of bytes read. |
| 3311 | * |
| 3312 | * This function is equivalent to calling #psa_generator_read and |
| 3313 | * passing the resulting output to #psa_import_key, but |
| 3314 | * if the implementation provides an isolation boundary then |
| 3315 | * the key material is not exposed outside the isolation boundary. |
| 3316 | * |
| 3317 | * \param key Slot where the key will be stored. This must be a |
| 3318 | * valid slot for a key of the chosen type. It must |
| 3319 | * be unoccupied. |
| 3320 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 3321 | * This must be a symmetric key type. |
| 3322 | * \param bits Key size in bits. |
| 3323 | * \param[in,out] generator The generator object to read from. |
| 3324 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame^] | 3325 | * \retval #PSA_SUCCESS |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3326 | * Success. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame^] | 3327 | * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3328 | * There were fewer than \p output_length bytes |
| 3329 | * in the generator. Note that in this case, no |
| 3330 | * output is written to the output buffer. |
| 3331 | * The generator's capacity is set to 0, thus |
| 3332 | * subsequent calls to this function will not |
| 3333 | * succeed, even with a smaller output buffer. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame^] | 3334 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3335 | * The key type or key size is not supported, either by the |
| 3336 | * implementation in general or in this particular slot. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame^] | 3337 | * \retval #PSA_ERROR_BAD_STATE |
| 3338 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3339 | * The key slot is invalid. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame^] | 3340 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3341 | * There is already a key in the specified slot. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame^] | 3342 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 3343 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 3344 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3345 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3346 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 3347 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 3348 | * The library has not been previously initialized by psa_crypto_init(). |
| 3349 | * It is implementation-dependent whether a failure to initialize |
| 3350 | * results in this error code. |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3351 | */ |
| 3352 | psa_status_t psa_generator_import_key(psa_key_slot_t key, |
| 3353 | psa_key_type_t type, |
| 3354 | size_t bits, |
| 3355 | psa_crypto_generator_t *generator); |
| 3356 | |
| 3357 | /** Abort a generator. |
| 3358 | * |
| 3359 | * Once a generator has been aborted, its capacity is zero. |
| 3360 | * Aborting a generator frees all associated resources except for the |
| 3361 | * \c generator structure itself. |
| 3362 | * |
| 3363 | * This function may be called at any time as long as the generator |
| 3364 | * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to |
| 3365 | * psa_crypto_generator_init() or a zero value. In particular, it is valid |
| 3366 | * to call psa_generator_abort() twice, or to call psa_generator_abort() |
| 3367 | * on a generator that has not been set up. |
| 3368 | * |
| 3369 | * Once aborted, the generator object may be called. |
| 3370 | * |
| 3371 | * \param[in,out] generator The generator to abort. |
| 3372 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame^] | 3373 | * \retval #PSA_SUCCESS |
| 3374 | * \retval #PSA_ERROR_BAD_STATE |
| 3375 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3376 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3377 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3378 | */ |
| 3379 | psa_status_t psa_generator_abort(psa_crypto_generator_t *generator); |
| 3380 | |
Gilles Peskine | 8feb3a8 | 2018-09-18 12:06:11 +0200 | [diff] [blame] | 3381 | /** Use the maximum possible capacity for a generator. |
| 3382 | * |
| 3383 | * Use this value as the capacity argument when setting up a generator |
| 3384 | * to indicate that the generator should have the maximum possible capacity. |
| 3385 | * The value of the maximum possible capacity depends on the generator |
| 3386 | * algorithm. |
| 3387 | */ |
| 3388 | #define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1)) |
| 3389 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 3390 | /**@}*/ |
| 3391 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3392 | /** \defgroup derivation Key derivation |
| 3393 | * @{ |
| 3394 | */ |
| 3395 | |
| 3396 | /** Set up a key derivation operation. |
| 3397 | * |
| 3398 | * A key derivation algorithm takes three inputs: a secret input \p key and |
| 3399 | * two non-secret inputs \p label and p salt. |
| 3400 | * The result of this function is a byte generator which can |
| 3401 | * be used to produce keys and other cryptographic material. |
| 3402 | * |
| 3403 | * The role of \p label and \p salt is as follows: |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 3404 | * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step |
| 3405 | * and \p label is the info string used in the "expand" step. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3406 | * |
| 3407 | * \param[in,out] generator The generator object to set up. It must |
Gilles Peskine | 92587db | 2018-09-18 12:12:42 +0200 | [diff] [blame] | 3408 | * have been initialized to all-bits-zero, |
| 3409 | * a logical zero (`{0}`), |
| 3410 | * \c PSA_CRYPTO_GENERATOR_INIT or |
| 3411 | * psa_crypto_generator_init(). |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3412 | * \param key Slot containing the secret key to use. |
| 3413 | * \param alg The key derivation algorithm to compute |
| 3414 | * (\c PSA_ALG_XXX value such that |
| 3415 | * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true). |
| 3416 | * \param[in] salt Salt to use. |
| 3417 | * \param salt_length Size of the \p salt buffer in bytes. |
| 3418 | * \param[in] label Label to use. |
| 3419 | * \param label_length Size of the \p label buffer in bytes. |
| 3420 | * \param capacity The maximum number of bytes that the |
| 3421 | * generator will be able to provide. |
| 3422 | * |
| 3423 | * \retval #PSA_SUCCESS |
| 3424 | * Success. |
| 3425 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 3426 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 3427 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 3428 | * \c key is not compatible with \c alg, |
| 3429 | * or \p capacity is too large for the specified algorithm and key. |
| 3430 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 3431 | * \c alg is not supported or is not a key derivation algorithm. |
| 3432 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 3433 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3434 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3435 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 3436 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 3437 | * The library has not been previously initialized by psa_crypto_init(). |
| 3438 | * It is implementation-dependent whether a failure to initialize |
| 3439 | * results in this error code. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3440 | */ |
| 3441 | psa_status_t psa_key_derivation(psa_crypto_generator_t *generator, |
Darryl Green | 8800136 | 2018-07-26 13:59:04 +0100 | [diff] [blame] | 3442 | psa_key_slot_t key, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3443 | psa_algorithm_t alg, |
| 3444 | const uint8_t *salt, |
| 3445 | size_t salt_length, |
| 3446 | const uint8_t *label, |
| 3447 | size_t label_length, |
| 3448 | size_t capacity); |
| 3449 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 3450 | /** Set up a key agreement operation. |
| 3451 | * |
| 3452 | * A key agreement algorithm takes two inputs: a private key \p private_key |
| 3453 | * a public key \p peer_key. |
| 3454 | * The result of this function is a byte generator which can |
| 3455 | * be used to produce keys and other cryptographic material. |
| 3456 | * |
Gilles Peskine | 211a436 | 2018-10-25 22:22:31 +0200 | [diff] [blame] | 3457 | * The resulting generator always has the maximum capacity permitted by |
| 3458 | * the algorithm. |
| 3459 | * |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 3460 | * \param[in,out] generator The generator object to set up. It must |
| 3461 | * have been initialized to all-bits-zero, |
| 3462 | * a logical zero (`{0}`), |
| 3463 | * \c PSA_CRYPTO_GENERATOR_INIT or |
| 3464 | * psa_crypto_generator_init(). |
| 3465 | * \param private_key Slot containing the private key to use. |
Gilles Peskine | d171e78 | 2018-11-15 17:46:21 +0100 | [diff] [blame] | 3466 | * \param[in] peer_key Public key of the peer. It must be |
| 3467 | * in the same format that psa_import_key() |
| 3468 | * accepts. The standard formats for public |
| 3469 | * keys are documented in the documentation |
| 3470 | * of psa_export_public_key(). |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 3471 | * \param peer_key_length Size of \p peer_key in bytes. |
| 3472 | * \param alg The key agreement algorithm to compute |
| 3473 | * (\c PSA_ALG_XXX value such that |
| 3474 | * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true). |
| 3475 | * |
| 3476 | * \retval #PSA_SUCCESS |
| 3477 | * Success. |
| 3478 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 3479 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 3480 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 3481 | * \c private_key is not compatible with \c alg, |
| 3482 | * or \p peer_key is not valid for \c alg or not compatible with |
| 3483 | * \c private_key. |
| 3484 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 3485 | * \c alg is not supported or is not a key derivation algorithm. |
| 3486 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 3487 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3488 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3489 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 3490 | */ |
| 3491 | psa_status_t psa_key_agreement(psa_crypto_generator_t *generator, |
| 3492 | psa_key_slot_t private_key, |
| 3493 | const uint8_t *peer_key, |
| 3494 | size_t peer_key_length, |
| 3495 | psa_algorithm_t alg); |
| 3496 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3497 | /**@}*/ |
| 3498 | |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 3499 | /** \defgroup random Random generation |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 3500 | * @{ |
| 3501 | */ |
| 3502 | |
| 3503 | /** |
| 3504 | * \brief Generate random bytes. |
| 3505 | * |
| 3506 | * \warning This function **can** fail! Callers MUST check the return status |
| 3507 | * and MUST NOT use the content of the output buffer if the return |
| 3508 | * status is not #PSA_SUCCESS. |
| 3509 | * |
| 3510 | * \note To generate a key, use psa_generate_key() instead. |
| 3511 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 3512 | * \param[out] output Output buffer for the generated data. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 3513 | * \param output_size Number of bytes to generate and output. |
| 3514 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3515 | * \retval #PSA_SUCCESS |
| 3516 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 3517 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 3518 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3519 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3520 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 3521 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 3522 | * The library has not been previously initialized by psa_crypto_init(). |
| 3523 | * It is implementation-dependent whether a failure to initialize |
| 3524 | * results in this error code. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 3525 | */ |
| 3526 | psa_status_t psa_generate_random(uint8_t *output, |
| 3527 | size_t output_size); |
| 3528 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 3529 | /** Extra parameters for RSA key generation. |
| 3530 | * |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 3531 | * 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] | 3532 | * parameter to psa_generate_key(). |
| 3533 | */ |
| 3534 | typedef struct { |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 3535 | uint32_t e; /**< Public exponent value. Default: 65537. */ |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 3536 | } psa_generate_key_extra_rsa; |
| 3537 | |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 3538 | /** |
| 3539 | * \brief Generate a key or key pair. |
| 3540 | * |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 3541 | * \param key Slot where the key will be stored. This must be a |
| 3542 | * valid slot for a key of the chosen type. It must |
| 3543 | * be unoccupied. |
| 3544 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 3545 | * \param bits Key size in bits. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 3546 | * \param[in] extra Extra parameters for key generation. The |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 3547 | * interpretation of this parameter depends on |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3548 | * \p type. All types support \c NULL to use |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 3549 | * default parameters. Implementation that support |
| 3550 | * the generation of vendor-specific key types |
| 3551 | * that allow extra parameters shall document |
| 3552 | * the format of these extra parameters and |
| 3553 | * the default values. For standard parameters, |
| 3554 | * the meaning of \p extra is as follows: |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3555 | * - For a symmetric key type (a type such |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 3556 | * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is |
| 3557 | * false), \p extra must be \c NULL. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 3558 | * - For an elliptic curve key type (a type |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 3559 | * such that #PSA_KEY_TYPE_IS_ECC(\p type) is |
| 3560 | * false), \p extra must be \c NULL. |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 3561 | * - For an RSA key (\p type is |
| 3562 | * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an |
| 3563 | * optional #psa_generate_key_extra_rsa structure |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 3564 | * specifying the public exponent. The |
| 3565 | * default public exponent used when \p extra |
| 3566 | * is \c NULL is 65537. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 3567 | * \param extra_size Size of the buffer that \p extra |
| 3568 | * points to, in bytes. Note that if \p extra is |
| 3569 | * \c NULL then \p extra_size must be zero. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 3570 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 3571 | * \retval #PSA_SUCCESS |
| 3572 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 3573 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 3574 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 3575 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 3576 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 3577 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 3578 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 3579 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 3580 | * The library has not been previously initialized by psa_crypto_init(). |
| 3581 | * It is implementation-dependent whether a failure to initialize |
| 3582 | * results in this error code. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 3583 | */ |
| 3584 | psa_status_t psa_generate_key(psa_key_slot_t key, |
| 3585 | psa_key_type_t type, |
| 3586 | size_t bits, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 3587 | const void *extra, |
| 3588 | size_t extra_size); |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 3589 | |
| 3590 | /**@}*/ |
| 3591 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3592 | #ifdef __cplusplus |
| 3593 | } |
| 3594 | #endif |
| 3595 | |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 3596 | /* The file "crypto_sizes.h" contains definitions for size calculation |
| 3597 | * macros whose definitions are implementation-specific. */ |
| 3598 | #include "crypto_sizes.h" |
| 3599 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 3600 | /* The file "crypto_struct.h" contains definitions for |
| 3601 | * implementation-specific structs that are declared above. */ |
| 3602 | #include "crypto_struct.h" |
| 3603 | |
| 3604 | /* The file "crypto_extra.h" contains vendor-specific definitions. This |
| 3605 | * can include vendor-defined algorithms, extra functions, etc. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3606 | #include "crypto_extra.h" |
| 3607 | |
| 3608 | #endif /* PSA_CRYPTO_H */ |