Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file psa/crypto_extra.h |
| 3 | * |
| 4 | * \brief PSA cryptography module: Mbed TLS vendor extensions |
Gilles Peskine | 07c91f5 | 2018-06-28 18:02:53 +0200 | [diff] [blame] | 5 | * |
| 6 | * \note This file may not be included directly. Applications must |
| 7 | * include psa/crypto.h. |
| 8 | * |
| 9 | * This file is reserved for vendor-specific definitions. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 10 | */ |
| 11 | /* |
| 12 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 13 | * SPDX-License-Identifier: Apache-2.0 |
| 14 | * |
| 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 16 | * not use this file except in compliance with the License. |
| 17 | * You may obtain a copy of the License at |
| 18 | * |
| 19 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | * |
| 21 | * Unless required by applicable law or agreed to in writing, software |
| 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 24 | * See the License for the specific language governing permissions and |
| 25 | * limitations under the License. |
| 26 | * |
| 27 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 28 | */ |
| 29 | |
| 30 | #ifndef PSA_CRYPTO_EXTRA_H |
| 31 | #define PSA_CRYPTO_EXTRA_H |
| 32 | |
Jaeden Amero | 81cefed | 2019-02-25 08:51:27 +0000 | [diff] [blame] | 33 | #include "mbedtls/platform_util.h" |
| 34 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 39 | /* UID for secure storage seed */ |
avolinski | 0d2c266 | 2018-11-21 17:31:07 +0200 | [diff] [blame] | 40 | #define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52 |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 41 | |
Jaeden Amero | 5e6d24c | 2019-02-21 10:41:29 +0000 | [diff] [blame] | 42 | /* |
| 43 | * Deprecated PSA Crypto error code definitions |
| 44 | */ |
| 45 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 46 | #define PSA_ERROR_UNKNOWN_ERROR \ |
| 47 | MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_GENERIC_ERROR ) |
| 48 | #endif |
| 49 | |
| 50 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 51 | #define PSA_ERROR_OCCUPIED_SLOT \ |
| 52 | MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_ALREADY_EXISTS ) |
| 53 | #endif |
| 54 | |
| 55 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 56 | #define PSA_ERROR_EMPTY_SLOT \ |
| 57 | MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_DOES_NOT_EXIST ) |
| 58 | #endif |
| 59 | |
| 60 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 61 | #define PSA_ERROR_INSUFFICIENT_CAPACITY \ |
| 62 | MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_INSUFFICIENT_DATA ) |
| 63 | #endif |
| 64 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 65 | /** |
| 66 | * \brief Library deinitialization. |
| 67 | * |
| 68 | * This function clears all data associated with the PSA layer, |
| 69 | * including the whole key store. |
| 70 | * |
| 71 | * This is an Mbed TLS extension. |
| 72 | */ |
| 73 | void mbedtls_psa_crypto_free( void ); |
| 74 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 75 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 76 | /** |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 77 | * \brief Inject an initial entropy seed for the random generator into |
| 78 | * secure storage. |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 79 | * |
| 80 | * This function injects data to be used as a seed for the random generator |
| 81 | * used by the PSA Crypto implementation. On devices that lack a trusted |
| 82 | * entropy source (preferably a hardware random number generator), |
| 83 | * the Mbed PSA Crypto implementation uses this value to seed its |
| 84 | * random generator. |
| 85 | * |
| 86 | * On devices without a trusted entropy source, this function must be |
| 87 | * called exactly once in the lifetime of the device. On devices with |
| 88 | * a trusted entropy source, calling this function is optional. |
| 89 | * In all cases, this function may only be called before calling any |
| 90 | * other function in the PSA Crypto API, including psa_crypto_init(). |
| 91 | * |
| 92 | * When this function returns successfully, it populates a file in |
| 93 | * persistent storage. Once the file has been created, this function |
| 94 | * can no longer succeed. |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 95 | * |
| 96 | * If any error occurs, this function does not change the system state. |
| 97 | * You can call this function again after correcting the reason for the |
| 98 | * error if possible. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 99 | * |
| 100 | * \warning This function **can** fail! Callers MUST check the return status. |
| 101 | * |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 102 | * \warning If you use this function, you should use it as part of a |
| 103 | * factory provisioning process. The value of the injected seed |
| 104 | * is critical to the security of the device. It must be |
| 105 | * *secret*, *unpredictable* and (statistically) *unique per device*. |
| 106 | * You should be generate it randomly using a cryptographically |
| 107 | * secure random generator seeded from trusted entropy sources. |
| 108 | * You should transmit it securely to the device and ensure |
| 109 | * that its value is not leaked or stored anywhere beyond the |
| 110 | * needs of transmitting it from the point of generation to |
| 111 | * the call of this function, and erase all copies of the value |
| 112 | * once this function returns. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 113 | * |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 114 | * This is an Mbed TLS extension. |
| 115 | * |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 116 | * \note This function is only available on the following platforms: |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 117 | * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled. |
| 118 | * Note that you must provide compatible implementations of |
| 119 | * mbedtls_nv_seed_read and mbedtls_nv_seed_write. |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 120 | * * In a client-server integration of PSA Cryptography, on the client side, |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 121 | * if the server supports this feature. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 122 | * \param[in] seed Buffer containing the seed value to inject. |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 123 | * \param[in] seed_size Size of the \p seed buffer. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 124 | * The size of the seed in bytes must be greater |
| 125 | * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM |
| 126 | * and #MBEDTLS_ENTROPY_BLOCK_SIZE. |
| 127 | * It must be less or equal to |
| 128 | * #MBEDTLS_ENTROPY_MAX_SEED_SIZE. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 129 | * |
| 130 | * \retval #PSA_SUCCESS |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 131 | * The seed value was injected successfully. The random generator |
| 132 | * of the PSA Crypto implementation is now ready for use. |
| 133 | * You may now call psa_crypto_init() and use the PSA Crypto |
| 134 | * implementation. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 135 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 136 | * \p seed_size is out of range. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 137 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 138 | * There was a failure reading or writing from storage. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 139 | * \retval #PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 140 | * The library has already been initialized. It is no longer |
| 141 | * possible to call this function. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 142 | */ |
| 143 | psa_status_t mbedtls_psa_inject_entropy(const unsigned char *seed, |
| 144 | size_t seed_size); |
| 145 | |
Gilles Peskine | 5dcd3ce | 2019-01-18 16:41:31 +0100 | [diff] [blame] | 146 | /** Set up a key derivation operation. |
| 147 | * |
| 148 | * FIMXE This function is no longer part of the official API. Its prototype |
| 149 | * is only kept around for the sake of tests that haven't been updated yet. |
| 150 | * |
Gilles Peskine | 3be6b7f | 2019-03-05 19:32:26 +0100 | [diff] [blame] | 151 | * A key derivation algorithm takes three inputs: a secret input \p handle and |
Gilles Peskine | 5dcd3ce | 2019-01-18 16:41:31 +0100 | [diff] [blame] | 152 | * two non-secret inputs \p label and p salt. |
| 153 | * The result of this function is a byte generator which can |
| 154 | * be used to produce keys and other cryptographic material. |
| 155 | * |
| 156 | * The role of \p label and \p salt is as follows: |
| 157 | * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step |
| 158 | * and \p label is the info string used in the "expand" step. |
| 159 | * |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 160 | * \param[in,out] operation The key derivation object to set up. It must |
| 161 | * have been initialized as per the documentation |
| 162 | * for #psa_key_derivation_operation_t and not |
| 163 | * yet be in use. |
Gilles Peskine | 5dcd3ce | 2019-01-18 16:41:31 +0100 | [diff] [blame] | 164 | * \param handle Handle to the secret key. |
| 165 | * \param alg The key derivation algorithm to compute |
| 166 | * (\c PSA_ALG_XXX value such that |
| 167 | * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true). |
| 168 | * \param[in] salt Salt to use. |
| 169 | * \param salt_length Size of the \p salt buffer in bytes. |
| 170 | * \param[in] label Label to use. |
| 171 | * \param label_length Size of the \p label buffer in bytes. |
| 172 | * \param capacity The maximum number of bytes that the |
Gilles Peskine | 35675b6 | 2019-05-16 17:26:11 +0200 | [diff] [blame] | 173 | * operation will be able to provide. |
Gilles Peskine | 5dcd3ce | 2019-01-18 16:41:31 +0100 | [diff] [blame] | 174 | * |
| 175 | * \retval #PSA_SUCCESS |
| 176 | * Success. |
| 177 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 178 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 179 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 180 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 181 | * \c key is not compatible with \c alg, |
| 182 | * or \p capacity is too large for the specified algorithm and key. |
| 183 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 184 | * \c alg is not supported or is not a key derivation algorithm. |
| 185 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 186 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 187 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 188 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 189 | * \retval #PSA_ERROR_BAD_STATE |
| 190 | * The library has not been previously initialized by psa_crypto_init(). |
| 191 | * It is implementation-dependent whether a failure to initialize |
| 192 | * results in this error code. |
| 193 | */ |
Gilles Peskine | 35675b6 | 2019-05-16 17:26:11 +0200 | [diff] [blame] | 194 | psa_status_t psa_key_derivation(psa_key_derivation_operation_t *operation, |
Gilles Peskine | 5dcd3ce | 2019-01-18 16:41:31 +0100 | [diff] [blame] | 195 | psa_key_handle_t handle, |
| 196 | psa_algorithm_t alg, |
| 197 | const uint8_t *salt, |
| 198 | size_t salt_length, |
| 199 | const uint8_t *label, |
| 200 | size_t label_length, |
| 201 | size_t capacity); |
| 202 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 203 | /* FIXME Deprecated. Remove this as soon as all the tests are updated. */ |
| 204 | #define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 205 | |
Gilles Peskine | a3dd737 | 2019-04-19 19:42:26 +0200 | [diff] [blame] | 206 | /** \defgroup policy Key policies |
| 207 | * @{ |
| 208 | * |
| 209 | * The functions in this section are legacy interfaces where the properties |
| 210 | * of a key object are set after allocating a handle, in constrast with the |
| 211 | * preferred interface where key objects are created atomically from |
| 212 | * a structure that represents the properties. |
| 213 | */ |
| 214 | |
| 215 | /** \def PSA_KEY_POLICY_INIT |
| 216 | * |
| 217 | * This macro returns a suitable initializer for a key policy object of type |
| 218 | * #psa_key_policy_t. |
| 219 | */ |
| 220 | #ifdef __DOXYGEN_ONLY__ |
| 221 | /* This is an example definition for documentation purposes. |
| 222 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 223 | */ |
| 224 | #define PSA_KEY_POLICY_INIT {0} |
| 225 | #endif |
| 226 | |
| 227 | /** Return an initial value for a key policy that forbids all usage of the key. |
| 228 | */ |
| 229 | static psa_key_policy_t psa_key_policy_init(void); |
| 230 | |
| 231 | /** \brief Set the standard fields of a policy structure. |
| 232 | * |
| 233 | * Note that this function does not make any consistency check of the |
| 234 | * parameters. The values are only checked when applying the policy to |
| 235 | * a key slot with psa_set_key_policy(). |
| 236 | * |
| 237 | * \param[in,out] policy The key policy to modify. It must have been |
| 238 | * initialized as per the documentation for |
| 239 | * #psa_key_policy_t. |
| 240 | * \param usage The permitted uses for the key. |
| 241 | * \param alg The algorithm that the key may be used for. |
| 242 | */ |
| 243 | void psa_key_policy_set_usage(psa_key_policy_t *policy, |
| 244 | psa_key_usage_t usage, |
| 245 | psa_algorithm_t alg); |
| 246 | |
| 247 | /** \brief Retrieve the usage field of a policy structure. |
| 248 | * |
| 249 | * \param[in] policy The policy object to query. |
| 250 | * |
| 251 | * \return The permitted uses for a key with this policy. |
| 252 | */ |
| 253 | psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy); |
| 254 | |
| 255 | /** \brief Retrieve the algorithm field of a policy structure. |
| 256 | * |
| 257 | * \param[in] policy The policy object to query. |
| 258 | * |
| 259 | * \return The permitted algorithm for a key with this policy. |
| 260 | */ |
| 261 | psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy); |
| 262 | |
| 263 | /** \brief Set the usage policy on a key slot. |
| 264 | * |
| 265 | * This function must be called on an empty key slot, before importing, |
| 266 | * generating or creating a key in the slot. Changing the policy of an |
| 267 | * existing key is not permitted. |
| 268 | * |
| 269 | * Implementations may set restrictions on supported key policies |
| 270 | * depending on the key type and the key slot. |
| 271 | * |
| 272 | * \param handle Handle to the key whose policy is to be changed. |
| 273 | * \param[in] policy The policy object to query. |
| 274 | * |
| 275 | * \retval #PSA_SUCCESS |
| 276 | * Success. |
| 277 | * If the key is persistent, it is implementation-defined whether |
| 278 | * the policy has been saved to persistent storage. Implementations |
| 279 | * may defer saving the policy until the key material is created. |
| 280 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 281 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 282 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 283 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 284 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 285 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 286 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 287 | * \retval #PSA_ERROR_BAD_STATE |
| 288 | * The library has not been previously initialized by psa_crypto_init(). |
| 289 | * It is implementation-dependent whether a failure to initialize |
| 290 | * results in this error code. |
| 291 | */ |
| 292 | psa_status_t psa_set_key_policy(psa_key_handle_t handle, |
| 293 | const psa_key_policy_t *policy); |
| 294 | |
| 295 | /** \brief Get the usage policy for a key slot. |
| 296 | * |
| 297 | * \param handle Handle to the key slot whose policy is being queried. |
| 298 | * \param[out] policy On success, the key's policy. |
| 299 | * |
| 300 | * \retval #PSA_SUCCESS |
| 301 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 302 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 303 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 304 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 305 | * \retval #PSA_ERROR_BAD_STATE |
| 306 | * The library has not been previously initialized by psa_crypto_init(). |
| 307 | * It is implementation-dependent whether a failure to initialize |
| 308 | * results in this error code. |
| 309 | */ |
| 310 | psa_status_t psa_get_key_policy(psa_key_handle_t handle, |
| 311 | psa_key_policy_t *policy); |
| 312 | |
| 313 | /**@}*/ |
| 314 | |
Gilles Peskine | 87a5e56 | 2019-04-17 12:28:25 +0200 | [diff] [blame] | 315 | /** \defgroup to_handle Key creation to allocated handle |
| 316 | * @{ |
| 317 | * |
| 318 | * The functions in this section are legacy interfaces where the properties |
| 319 | * of a key object are set after allocating a handle, in constrast with the |
| 320 | * preferred interface where key objects are created atomically from |
| 321 | * a structure that represents the properties. |
| 322 | */ |
| 323 | |
| 324 | /** Create a new persistent key slot. |
| 325 | * |
| 326 | * Create a new persistent key slot and return a handle to it. The handle |
| 327 | * remains valid until the application calls psa_close_key() or terminates. |
| 328 | * The application can open the key again with psa_open_key() until it |
| 329 | * removes the key by calling psa_destroy_key(). |
| 330 | * |
| 331 | * \param lifetime The lifetime of the key. This designates a storage |
| 332 | * area where the key material is stored. This must not |
| 333 | * be #PSA_KEY_LIFETIME_VOLATILE. |
| 334 | * \param id The persistent identifier of the key. |
| 335 | * \param[out] handle On success, a handle to the newly created key slot. |
| 336 | * When key material is later created in this key slot, |
| 337 | * it will be saved to the specified persistent location. |
| 338 | * |
| 339 | * \retval #PSA_SUCCESS |
| 340 | * Success. The application can now use the value of `*handle` |
| 341 | * to access the newly allocated key slot. |
| 342 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 343 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 344 | * \retval #PSA_ERROR_ALREADY_EXISTS |
| 345 | * There is already a key with the identifier \p id in the storage |
| 346 | * area designated by \p lifetime. |
| 347 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 348 | * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE. |
| 349 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 350 | * \p id is invalid for the specified lifetime. |
| 351 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 352 | * \p lifetime is not supported. |
| 353 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 354 | * \p lifetime is valid, but the application does not have the |
| 355 | * permission to create a key there. |
| 356 | */ |
| 357 | psa_status_t psa_create_key(psa_key_lifetime_t lifetime, |
| 358 | psa_key_id_t id, |
| 359 | psa_key_handle_t *handle); |
| 360 | |
Gilles Peskine | a3dd737 | 2019-04-19 19:42:26 +0200 | [diff] [blame] | 361 | /** Allocate a key slot for a transient key, i.e. a key which is only stored |
| 362 | * in volatile memory. |
| 363 | * |
| 364 | * The allocated key slot and its handle remain valid until the |
| 365 | * application calls psa_close_key() or psa_destroy_key() or until the |
| 366 | * application terminates. |
| 367 | * |
| 368 | * \param[out] handle On success, a handle to a volatile key slot. |
| 369 | * |
| 370 | * \retval #PSA_SUCCESS |
| 371 | * Success. The application can now use the value of `*handle` |
| 372 | * to access the newly allocated key slot. |
| 373 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 374 | * There was not enough memory, or the maximum number of key slots |
| 375 | * has been reached. |
| 376 | */ |
| 377 | psa_status_t psa_allocate_key(psa_key_handle_t *handle); |
| 378 | |
| 379 | /** |
| 380 | * \brief Get basic metadata about a key. |
| 381 | * |
| 382 | * \param handle Handle to the key slot to query. |
| 383 | * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value). |
| 384 | * This may be a null pointer, in which case the key type |
| 385 | * is not written. |
| 386 | * \param[out] bits On success, the key size in bits. |
| 387 | * This may be a null pointer, in which case the key size |
| 388 | * is not written. |
| 389 | * |
| 390 | * \retval #PSA_SUCCESS |
| 391 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 392 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
| 393 | * The handle is to a key slot which does not contain key material yet. |
| 394 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 395 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 396 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 397 | * \retval #PSA_ERROR_BAD_STATE |
| 398 | * The library has not been previously initialized by psa_crypto_init(). |
| 399 | * It is implementation-dependent whether a failure to initialize |
| 400 | * results in this error code. |
| 401 | */ |
| 402 | psa_status_t psa_get_key_information(psa_key_handle_t handle, |
| 403 | psa_key_type_t *type, |
| 404 | size_t *bits); |
| 405 | |
Gilles Peskine | 87a5e56 | 2019-04-17 12:28:25 +0200 | [diff] [blame] | 406 | /** \brief Retrieve the lifetime of an open key. |
| 407 | * |
| 408 | * \param handle Handle to query. |
| 409 | * \param[out] lifetime On success, the lifetime value. |
| 410 | * |
| 411 | * \retval #PSA_SUCCESS |
| 412 | * Success. |
| 413 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 414 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 415 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 416 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 417 | * \retval #PSA_ERROR_BAD_STATE |
| 418 | * The library has not been previously initialized by psa_crypto_init(). |
| 419 | * It is implementation-dependent whether a failure to initialize |
| 420 | * results in this error code. |
| 421 | */ |
| 422 | psa_status_t psa_get_key_lifetime_from_handle(psa_key_handle_t handle, |
| 423 | psa_key_lifetime_t *lifetime); |
| 424 | |
| 425 | psa_status_t psa_import_key_to_handle(psa_key_handle_t handle, |
| 426 | psa_key_type_t type, |
| 427 | const uint8_t *data, |
| 428 | size_t data_length); |
| 429 | |
| 430 | psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle, |
| 431 | psa_key_handle_t target_handle, |
| 432 | const psa_key_policy_t *constraint); |
| 433 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 434 | psa_status_t psa_generate_derived_key_to_handle(psa_key_handle_t handle, |
Gilles Peskine | 87a5e56 | 2019-04-17 12:28:25 +0200 | [diff] [blame] | 435 | psa_key_type_t type, |
| 436 | size_t bits, |
Gilles Peskine | 35675b6 | 2019-05-16 17:26:11 +0200 | [diff] [blame] | 437 | psa_key_derivation_operation_t *operation); |
Gilles Peskine | 87a5e56 | 2019-04-17 12:28:25 +0200 | [diff] [blame] | 438 | |
Adrian L. Shaw | 5a5a79a | 2019-05-03 15:44:28 +0100 | [diff] [blame] | 439 | psa_status_t psa_generate_random_key_to_handle(psa_key_handle_t handle, |
Gilles Peskine | 87a5e56 | 2019-04-17 12:28:25 +0200 | [diff] [blame] | 440 | psa_key_type_t type, |
| 441 | size_t bits, |
| 442 | const void *extra, |
| 443 | size_t extra_size); |
| 444 | |
| 445 | /**@}*/ |
| 446 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 447 | |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 448 | /** \addtogroup crypto_types |
| 449 | * @{ |
| 450 | */ |
| 451 | |
Gilles Peskine | a130219 | 2019-05-16 13:58:24 +0200 | [diff] [blame] | 452 | /** DSA public key. |
| 453 | * |
| 454 | * The import and export format is the |
| 455 | * representation of the public key `y = g^x mod p` as a big-endian byte |
| 456 | * string. The length of the byte string is the length of the base prime `p` |
| 457 | * in bytes. |
| 458 | */ |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 459 | #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000) |
Gilles Peskine | a130219 | 2019-05-16 13:58:24 +0200 | [diff] [blame] | 460 | |
| 461 | /** DSA key pair (private and public key). |
| 462 | * |
| 463 | * The import and export format is the |
| 464 | * representation of the private key `x` as a big-endian byte string. The |
| 465 | * length of the byte string is the private key size in bytes (leading zeroes |
| 466 | * are not stripped). |
| 467 | * |
| 468 | * Determinstic DSA key derivation with psa_generate_derived_key follows |
| 469 | * FIPS 186-4 §B.1.2: interpret the byte string as integer |
| 470 | * in big-endian order. Discard it if it is not in the range |
| 471 | * [0, *N* - 2] where *N* is the boundary of the private key domain |
| 472 | * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA, |
| 473 | * or the order of the curve's base point for ECC). |
| 474 | * Add 1 to the resulting integer and use this as the private key *x*. |
| 475 | * |
| 476 | */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame^] | 477 | #define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x70020000) |
Gilles Peskine | a130219 | 2019-05-16 13:58:24 +0200 | [diff] [blame] | 478 | |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 479 | /** Whether a key type is an DSA key (pair or public-only). */ |
| 480 | #define PSA_KEY_TYPE_IS_DSA(type) \ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame^] | 481 | (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY) |
Gilles Peskine | e38ab1a | 2019-05-16 13:51:50 +0200 | [diff] [blame] | 482 | |
| 483 | #define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000) |
| 484 | /** DSA signature with hashing. |
| 485 | * |
| 486 | * This is the signature scheme defined by FIPS 186-4, |
| 487 | * with a random per-message secret number (*k*). |
| 488 | * |
| 489 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 490 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 491 | * This includes #PSA_ALG_ANY_HASH |
| 492 | * when specifying the algorithm in a usage policy. |
| 493 | * |
| 494 | * \return The corresponding DSA signature algorithm. |
| 495 | * \return Unspecified if \p hash_alg is not a supported |
| 496 | * hash algorithm. |
| 497 | */ |
| 498 | #define PSA_ALG_DSA(hash_alg) \ |
| 499 | (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 500 | #define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000) |
| 501 | #define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000) |
| 502 | /** Deterministic DSA signature with hashing. |
| 503 | * |
| 504 | * This is the deterministic variant defined by RFC 6979 of |
| 505 | * the signature scheme defined by FIPS 186-4. |
| 506 | * |
| 507 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 508 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 509 | * This includes #PSA_ALG_ANY_HASH |
| 510 | * when specifying the algorithm in a usage policy. |
| 511 | * |
| 512 | * \return The corresponding DSA signature algorithm. |
| 513 | * \return Unspecified if \p hash_alg is not a supported |
| 514 | * hash algorithm. |
| 515 | */ |
| 516 | #define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \ |
| 517 | (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 518 | #define PSA_ALG_IS_DSA(alg) \ |
| 519 | (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \ |
| 520 | PSA_ALG_DSA_BASE) |
| 521 | #define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \ |
| 522 | (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0) |
| 523 | #define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \ |
| 524 | (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg)) |
| 525 | #define PSA_ALG_IS_RANDOMIZED_DSA(alg) \ |
| 526 | (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg)) |
| 527 | |
| 528 | |
| 529 | /* We need to expand the sample definition of this macro from |
| 530 | * the API definition. */ |
| 531 | #undef PSA_ALG_IS_HASH_AND_SIGN |
| 532 | #define PSA_ALG_IS_HASH_AND_SIGN(alg) \ |
| 533 | (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \ |
| 534 | PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg)) |
| 535 | |
| 536 | /**@}*/ |
| 537 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 538 | /** \addtogroup attributes |
| 539 | * @{ |
| 540 | */ |
| 541 | |
Gilles Peskine | dcaefae | 2019-05-16 12:55:35 +0200 | [diff] [blame] | 542 | /** Custom Diffie-Hellman group. |
| 543 | * |
| 544 | * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_GROUP_CUSTOM) or |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame^] | 545 | * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_GROUP_CUSTOM), the group data comes |
Gilles Peskine | dcaefae | 2019-05-16 12:55:35 +0200 | [diff] [blame] | 546 | * from domain parameters set by psa_set_key_domain_parameters(). |
| 547 | */ |
| 548 | /* This value is reserved for private use in the TLS named group registry. */ |
| 549 | #define PSA_DH_GROUP_CUSTOM ((psa_dh_group_t) 0x01fc) |
| 550 | |
| 551 | |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 552 | /** |
| 553 | * \brief Set domain parameters for a key. |
| 554 | * |
| 555 | * Some key types require additional domain parameters in addition to |
| 556 | * the key type identifier and the key size. Use this function instead |
| 557 | * of psa_set_key_type() when you need to specify domain parameters. |
| 558 | * |
| 559 | * The format for the required domain parameters varies based on the key type. |
| 560 | * |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame^] | 561 | * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR), |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 562 | * the domain parameter data consists of the public exponent, |
| 563 | * represented as a big-endian integer with no leading zeros. |
| 564 | * This information is used when generating an RSA key pair. |
| 565 | * When importing a key, the public exponent is read from the imported |
| 566 | * key data and the exponent recorded in the attribute structure is ignored. |
| 567 | * As an exception, the public exponent 65537 is represented by an empty |
| 568 | * byte string. |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame^] | 569 | * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR), |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 570 | * the `Dss-Parms` format as defined by RFC 3279 §2.3.2. |
| 571 | * ``` |
| 572 | * Dss-Parms ::= SEQUENCE { |
| 573 | * p INTEGER, |
| 574 | * q INTEGER, |
| 575 | * g INTEGER |
| 576 | * } |
| 577 | * ``` |
Gilles Peskine | dcaefae | 2019-05-16 12:55:35 +0200 | [diff] [blame] | 578 | * - For Diffie-Hellman key exchange keys |
| 579 | * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_GROUP_CUSTOM) or |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame^] | 580 | * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_GROUP_CUSTOM)), the |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 581 | * `DomainParameters` format as defined by RFC 3279 §2.3.3. |
| 582 | * ``` |
| 583 | * DomainParameters ::= SEQUENCE { |
| 584 | * p INTEGER, -- odd prime, p=jq +1 |
| 585 | * g INTEGER, -- generator, g |
| 586 | * q INTEGER, -- factor of p-1 |
| 587 | * j INTEGER OPTIONAL, -- subgroup factor |
| 588 | * validationParms ValidationParms OPTIONAL |
| 589 | * } |
| 590 | * ValidationParms ::= SEQUENCE { |
| 591 | * seed BIT STRING, |
| 592 | * pgenCounter INTEGER |
| 593 | * } |
| 594 | * ``` |
| 595 | * |
| 596 | * \note This function may allocate memory or other resources. |
| 597 | * Once you have called this function on an attribute structure, |
| 598 | * you must call psa_reset_key_attributes() to free these resources. |
| 599 | * |
| 600 | * \note This is an experimental extension to the interface. It may change |
| 601 | * in future versions of the library. |
| 602 | * |
| 603 | * \param[in,out] attributes Attribute structure where the specified domain |
| 604 | * parameters will be stored. |
| 605 | * If this function fails, the content of |
| 606 | * \p attributes is not modified. |
| 607 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 608 | * \param[in] data Buffer containing the key domain parameters. |
| 609 | * The content of this buffer is interpreted |
| 610 | * according to \p type as described above. |
| 611 | * \param data_length Size of the \p data buffer in bytes. |
| 612 | * |
| 613 | * \retval #PSA_SUCCESS |
| 614 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 615 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 616 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 617 | */ |
| 618 | psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes, |
| 619 | psa_key_type_t type, |
| 620 | const uint8_t *data, |
| 621 | size_t data_length); |
| 622 | |
| 623 | /** |
| 624 | * \brief Get domain parameters for a key. |
| 625 | * |
| 626 | * Get the domain parameters for a key with this function, if any. The format |
| 627 | * of the domain parameters written to \p data is specified in the |
| 628 | * documentation for psa_set_key_domain_parameters(). |
| 629 | * |
| 630 | * \note This is an experimental extension to the interface. It may change |
| 631 | * in future versions of the library. |
| 632 | * |
| 633 | * \param[in] attributes The key attribute structure to query. |
| 634 | * \param[out] data On success, the key domain parameters. |
| 635 | * \param data_size Size of the \p data buffer in bytes. |
| 636 | * The buffer is guaranteed to be large |
| 637 | * enough if its size in bytes is at least |
| 638 | * the value given by |
| 639 | * PSA_KEY_DOMAIN_PARAMETERS_SIZE(). |
| 640 | * \param[out] data_length On success, the number of bytes |
| 641 | * that make up the key domain parameters data. |
| 642 | * |
| 643 | * \retval #PSA_SUCCESS |
| 644 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 645 | */ |
| 646 | psa_status_t psa_get_key_domain_parameters( |
| 647 | const psa_key_attributes_t *attributes, |
| 648 | uint8_t *data, |
| 649 | size_t data_size, |
| 650 | size_t *data_length); |
| 651 | |
| 652 | /** Safe output buffer size for psa_get_key_domain_parameters(). |
| 653 | * |
| 654 | * This macro returns a compile-time constant if its arguments are |
| 655 | * compile-time constants. |
| 656 | * |
| 657 | * \warning This function may call its arguments multiple times or |
| 658 | * zero times, so you should not pass arguments that contain |
| 659 | * side effects. |
| 660 | * |
| 661 | * \note This is an experimental extension to the interface. It may change |
| 662 | * in future versions of the library. |
| 663 | * |
| 664 | * \param key_type A supported key type. |
| 665 | * \param key_bits The size of the key in bits. |
| 666 | * |
| 667 | * \return If the parameters are valid and supported, return |
| 668 | * a buffer size in bytes that guarantees that |
| 669 | * psa_get_key_domain_parameters() will not fail with |
| 670 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 671 | * If the parameters are a valid combination that is not supported |
Gilles Peskine | 27a983d | 2019-05-16 17:24:53 +0200 | [diff] [blame] | 672 | * by the implementation, this macro shall return either a |
Gilles Peskine | 24f10f8 | 2019-05-16 12:18:32 +0200 | [diff] [blame] | 673 | * sensible size or 0. |
| 674 | * If the parameters are not valid, the |
| 675 | * return value is unspecified. |
| 676 | */ |
| 677 | #define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \ |
| 678 | (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \ |
| 679 | PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \ |
| 680 | PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \ |
| 681 | 0) |
| 682 | #define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \ |
| 683 | (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/) |
| 684 | #define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \ |
| 685 | (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/) |
| 686 | |
| 687 | /**@}*/ |
| 688 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 689 | #ifdef __cplusplus |
| 690 | } |
| 691 | #endif |
| 692 | |
| 693 | #endif /* PSA_CRYPTO_EXTRA_H */ |