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 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 37 | /* UID for secure storage seed */ |
avolinski | 0d2c266 | 2018-11-21 17:31:07 +0200 | [diff] [blame] | 38 | #define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52 |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 39 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 40 | /** |
| 41 | * \brief Library deinitialization. |
| 42 | * |
| 43 | * This function clears all data associated with the PSA layer, |
| 44 | * including the whole key store. |
| 45 | * |
| 46 | * This is an Mbed TLS extension. |
| 47 | */ |
| 48 | void mbedtls_psa_crypto_free( void ); |
| 49 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 50 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 51 | /** |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 52 | * \brief Inject an initial entropy seed for the random generator into |
| 53 | * secure storage. |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 54 | * |
| 55 | * This function injects data to be used as a seed for the random generator |
| 56 | * used by the PSA Crypto implementation. On devices that lack a trusted |
| 57 | * entropy source (preferably a hardware random number generator), |
| 58 | * the Mbed PSA Crypto implementation uses this value to seed its |
| 59 | * random generator. |
| 60 | * |
| 61 | * On devices without a trusted entropy source, this function must be |
| 62 | * called exactly once in the lifetime of the device. On devices with |
| 63 | * a trusted entropy source, calling this function is optional. |
| 64 | * In all cases, this function may only be called before calling any |
| 65 | * other function in the PSA Crypto API, including psa_crypto_init(). |
| 66 | * |
| 67 | * When this function returns successfully, it populates a file in |
| 68 | * persistent storage. Once the file has been created, this function |
| 69 | * can no longer succeed. |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 70 | * |
| 71 | * If any error occurs, this function does not change the system state. |
| 72 | * You can call this function again after correcting the reason for the |
| 73 | * error if possible. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 74 | * |
| 75 | * \warning This function **can** fail! Callers MUST check the return status. |
| 76 | * |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 77 | * \warning If you use this function, you should use it as part of a |
| 78 | * factory provisioning process. The value of the injected seed |
| 79 | * is critical to the security of the device. It must be |
| 80 | * *secret*, *unpredictable* and (statistically) *unique per device*. |
| 81 | * You should be generate it randomly using a cryptographically |
| 82 | * secure random generator seeded from trusted entropy sources. |
| 83 | * You should transmit it securely to the device and ensure |
| 84 | * that its value is not leaked or stored anywhere beyond the |
| 85 | * needs of transmitting it from the point of generation to |
| 86 | * the call of this function, and erase all copies of the value |
| 87 | * once this function returns. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 88 | * |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 89 | * This is an Mbed TLS extension. |
| 90 | * |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 91 | * \note This function is only available on the following platforms: |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 92 | * * If the compile-time options MBEDTLS_ENTROPY_NV_SEED and |
| 93 | * MBEDTLS_PSA_HAS_ITS_IO are both enabled. Note that you |
| 94 | * must provide compatible implementations of mbedtls_nv_seed_read |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 95 | * and mbedtls_nv_seed_write. |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 96 | * * In a client-server integration of PSA Cryptography, on the client side, |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 97 | * if the server supports this feature. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 98 | * \param[in] seed Buffer containing the seed value to inject. |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 99 | * \param[in] seed_size Size of the \p seed buffer. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 100 | * The size of the seed in bytes must be greater |
| 101 | * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM |
| 102 | * and #MBEDTLS_ENTROPY_BLOCK_SIZE. |
| 103 | * It must be less or equal to |
| 104 | * #MBEDTLS_ENTROPY_MAX_SEED_SIZE. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 105 | * |
| 106 | * \retval #PSA_SUCCESS |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 107 | * The seed value was injected successfully. The random generator |
| 108 | * of the PSA Crypto implementation is now ready for use. |
| 109 | * You may now call psa_crypto_init() and use the PSA Crypto |
| 110 | * implementation. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 111 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 112 | * \p seed_size is out of range. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 113 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 114 | * \retval `PSA_ITS_ERROR_XXX` |
| 115 | * There was a failure reading or writing from storage. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 116 | * \retval #PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 117 | * The library has already been initialized. It is no longer |
| 118 | * possible to call this function. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 119 | */ |
| 120 | psa_status_t mbedtls_psa_inject_entropy(const unsigned char *seed, |
| 121 | size_t seed_size); |
| 122 | |
Gilles Peskine | 5dcd3ce | 2019-01-18 16:41:31 +0100 | [diff] [blame] | 123 | /** Set up a key derivation operation. |
| 124 | * |
| 125 | * FIMXE This function is no longer part of the official API. Its prototype |
| 126 | * is only kept around for the sake of tests that haven't been updated yet. |
| 127 | * |
Gilles Peskine | 3be6b7f | 2019-03-05 19:32:26 +0100 | [diff] [blame^] | 128 | * 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] | 129 | * two non-secret inputs \p label and p salt. |
| 130 | * The result of this function is a byte generator which can |
| 131 | * be used to produce keys and other cryptographic material. |
| 132 | * |
| 133 | * The role of \p label and \p salt is as follows: |
| 134 | * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step |
| 135 | * and \p label is the info string used in the "expand" step. |
| 136 | * |
| 137 | * \param[in,out] generator The generator object to set up. It must have |
| 138 | * been initialized as per the documentation for |
| 139 | * #psa_crypto_generator_t and not yet in use. |
| 140 | * \param handle Handle to the secret key. |
| 141 | * \param alg The key derivation algorithm to compute |
| 142 | * (\c PSA_ALG_XXX value such that |
| 143 | * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true). |
| 144 | * \param[in] salt Salt to use. |
| 145 | * \param salt_length Size of the \p salt buffer in bytes. |
| 146 | * \param[in] label Label to use. |
| 147 | * \param label_length Size of the \p label buffer in bytes. |
| 148 | * \param capacity The maximum number of bytes that the |
| 149 | * generator will be able to provide. |
| 150 | * |
| 151 | * \retval #PSA_SUCCESS |
| 152 | * Success. |
| 153 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 154 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 155 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 156 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 157 | * \c key is not compatible with \c alg, |
| 158 | * or \p capacity is too large for the specified algorithm and key. |
| 159 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 160 | * \c alg is not supported or is not a key derivation algorithm. |
| 161 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 162 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 163 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 164 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 165 | * \retval #PSA_ERROR_BAD_STATE |
| 166 | * The library has not been previously initialized by psa_crypto_init(). |
| 167 | * It is implementation-dependent whether a failure to initialize |
| 168 | * results in this error code. |
| 169 | */ |
| 170 | psa_status_t psa_key_derivation(psa_crypto_generator_t *generator, |
| 171 | psa_key_handle_t handle, |
| 172 | psa_algorithm_t alg, |
| 173 | const uint8_t *salt, |
| 174 | size_t salt_length, |
| 175 | const uint8_t *label, |
| 176 | size_t label_length, |
| 177 | size_t capacity); |
| 178 | |
Gilles Peskine | 6843c29 | 2019-01-18 16:44:49 +0100 | [diff] [blame] | 179 | /* FIXME Deprecated. Remove this as soon as all the tests are updated. */ |
| 180 | #define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001) |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 181 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 182 | #ifdef __cplusplus |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | #endif /* PSA_CRYPTO_EXTRA_H */ |