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 | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 65 | /** \addtogroup policy |
| 66 | * @{ |
| 67 | */ |
| 68 | |
| 69 | /** \brief Set the enrollment algorithm in a key policy. |
| 70 | * |
| 71 | * An operation on a key may indifferently use the algorithm set with |
| 72 | * psa_key_policy_set_usage() or with this function. |
| 73 | * |
| 74 | * \param[in,out] policy The key policy to modify. It must have been |
| 75 | * initialized as per the documentation for |
| 76 | * #psa_key_policy_t. |
| 77 | * \param alg2 A second algorithm that the key may be used for, |
| 78 | * in addition to the algorithm set with |
| 79 | * psa_key_policy_set_usage(). |
| 80 | * |
| 81 | * \warning Setting an enrollment algorithm is not recommended, because |
| 82 | * using the same key with different algorithms can allow some |
| 83 | * attacks based on arithmetic relations between different |
| 84 | * computations made with the same key, or can escalate harmless |
| 85 | * side channels into exploitable ones. Use this function only |
Gilles Peskine | 549ea86 | 2019-05-22 11:45:59 +0200 | [diff] [blame] | 86 | * if it is necessary to support a protocol for which it has been |
Gilles Peskine | d6f371b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 87 | * verified that the usage of the key with multiple algorithms |
| 88 | * is safe. |
| 89 | */ |
| 90 | void psa_key_policy_set_enrollment_algorithm(psa_key_policy_t *policy, |
| 91 | psa_algorithm_t alg2); |
| 92 | |
| 93 | /** \brief Retrieve the enrollment algorithm field of a policy structure. |
| 94 | * |
| 95 | * \param[in] policy The policy object to query. |
| 96 | * |
| 97 | * \return The enrollment algorithm for a key with this policy. |
| 98 | */ |
| 99 | psa_algorithm_t psa_key_policy_get_enrollment_algorithm( |
| 100 | const psa_key_policy_t *policy); |
| 101 | |
| 102 | /**@}*/ |
| 103 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 104 | /** |
| 105 | * \brief Library deinitialization. |
| 106 | * |
| 107 | * This function clears all data associated with the PSA layer, |
| 108 | * including the whole key store. |
| 109 | * |
| 110 | * This is an Mbed TLS extension. |
| 111 | */ |
| 112 | void mbedtls_psa_crypto_free( void ); |
| 113 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 114 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 115 | /** |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 116 | * \brief Inject an initial entropy seed for the random generator into |
| 117 | * secure storage. |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 118 | * |
| 119 | * This function injects data to be used as a seed for the random generator |
| 120 | * used by the PSA Crypto implementation. On devices that lack a trusted |
| 121 | * entropy source (preferably a hardware random number generator), |
| 122 | * the Mbed PSA Crypto implementation uses this value to seed its |
| 123 | * random generator. |
| 124 | * |
| 125 | * On devices without a trusted entropy source, this function must be |
| 126 | * called exactly once in the lifetime of the device. On devices with |
| 127 | * a trusted entropy source, calling this function is optional. |
| 128 | * In all cases, this function may only be called before calling any |
| 129 | * other function in the PSA Crypto API, including psa_crypto_init(). |
| 130 | * |
| 131 | * When this function returns successfully, it populates a file in |
| 132 | * persistent storage. Once the file has been created, this function |
| 133 | * can no longer succeed. |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 134 | * |
| 135 | * If any error occurs, this function does not change the system state. |
| 136 | * You can call this function again after correcting the reason for the |
| 137 | * error if possible. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 138 | * |
| 139 | * \warning This function **can** fail! Callers MUST check the return status. |
| 140 | * |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 141 | * \warning If you use this function, you should use it as part of a |
| 142 | * factory provisioning process. The value of the injected seed |
| 143 | * is critical to the security of the device. It must be |
| 144 | * *secret*, *unpredictable* and (statistically) *unique per device*. |
| 145 | * You should be generate it randomly using a cryptographically |
| 146 | * secure random generator seeded from trusted entropy sources. |
| 147 | * You should transmit it securely to the device and ensure |
| 148 | * that its value is not leaked or stored anywhere beyond the |
| 149 | * needs of transmitting it from the point of generation to |
| 150 | * the call of this function, and erase all copies of the value |
| 151 | * once this function returns. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 152 | * |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 153 | * This is an Mbed TLS extension. |
| 154 | * |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 155 | * \note This function is only available on the following platforms: |
Gilles Peskine | e3dbdd8 | 2019-02-25 11:04:06 +0100 | [diff] [blame] | 156 | * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled. |
| 157 | * Note that you must provide compatible implementations of |
| 158 | * mbedtls_nv_seed_read and mbedtls_nv_seed_write. |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 159 | * * In a client-server integration of PSA Cryptography, on the client side, |
Netanel Gonen | 1d7195f | 2018-11-22 16:24:48 +0200 | [diff] [blame] | 160 | * if the server supports this feature. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 161 | * \param[in] seed Buffer containing the seed value to inject. |
Gilles Peskine | 0cfaed1 | 2018-11-22 17:11:45 +0200 | [diff] [blame] | 162 | * \param[in] seed_size Size of the \p seed buffer. |
Netanel Gonen | 596e65e | 2018-11-22 18:41:43 +0200 | [diff] [blame] | 163 | * The size of the seed in bytes must be greater |
| 164 | * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM |
| 165 | * and #MBEDTLS_ENTROPY_BLOCK_SIZE. |
| 166 | * It must be less or equal to |
| 167 | * #MBEDTLS_ENTROPY_MAX_SEED_SIZE. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 168 | * |
| 169 | * \retval #PSA_SUCCESS |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 170 | * The seed value was injected successfully. The random generator |
| 171 | * of the PSA Crypto implementation is now ready for use. |
| 172 | * You may now call psa_crypto_init() and use the PSA Crypto |
| 173 | * implementation. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 174 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | ee2ffd3 | 2018-11-16 11:02:49 +0100 | [diff] [blame] | 175 | * \p seed_size is out of range. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 176 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 177 | * There was a failure reading or writing from storage. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 178 | * \retval #PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 0338ded | 2018-11-15 18:19:27 +0100 | [diff] [blame] | 179 | * The library has already been initialized. It is no longer |
| 180 | * possible to call this function. |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 181 | */ |
| 182 | psa_status_t mbedtls_psa_inject_entropy(const unsigned char *seed, |
| 183 | size_t seed_size); |
| 184 | |
Netanel Gonen | 2bcd312 | 2018-11-19 11:53:02 +0200 | [diff] [blame] | 185 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 186 | #ifdef __cplusplus |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | #endif /* PSA_CRYPTO_EXTRA_H */ |