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