Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 1 | /** |
Hanno Becker | afebf5a | 2018-11-13 21:01:41 +0000 | [diff] [blame] | 2 | * \file psa_util.h |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 3 | * |
| 4 | * \brief Utility functions for the use of the PSA Crypto library. |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
Hanno Becker | 186b65a | 2018-11-19 15:14:21 +0000 | [diff] [blame] | 23 | #ifndef MBEDTLS_PSA_UTIL_H |
| 24 | #define MBEDTLS_PSA_UTIL_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 25 | #include "mbedtls/private_access.h" |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 26 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 27 | #include "mbedtls/build_info.h" |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 28 | |
Jerry Yu | b02ee18 | 2022-03-16 10:30:41 +0800 | [diff] [blame] | 29 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 30 | |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 31 | /* Expose whatever RNG the PSA subsystem uses to applications using the |
Gilles Peskine | 996f216 | 2021-02-16 16:50:00 +0100 | [diff] [blame] | 32 | * mbedtls_xxx API. The declarations and definitions here need to be |
| 33 | * consistent with the implementation in library/psa_crypto_random_impl.h. |
| 34 | * See that file for implementation documentation. */ |
Jerry Yu | 406cf27 | 2022-03-22 11:33:42 +0800 | [diff] [blame] | 35 | |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 36 | |
| 37 | /* The type of a `f_rng` random generator function that many library functions |
| 38 | * take. |
| 39 | * |
| 40 | * This type name is not part of the Mbed TLS stable API. It may be renamed |
| 41 | * or moved without warning. |
| 42 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | typedef int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size); |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 44 | |
| 45 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 46 | |
| 47 | /** The random generator function for the PSA subsystem. |
| 48 | * |
| 49 | * This function is suitable as the `f_rng` random generator function |
Gilles Peskine | 2cff7e2 | 2021-02-16 16:49:42 +0100 | [diff] [blame] | 50 | * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE |
| 51 | * to obtain the \p p_rng parameter. |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 52 | * |
| 53 | * The implementation of this function depends on the configuration of the |
| 54 | * library. |
Gilles Peskine | 2cff7e2 | 2021-02-16 16:49:42 +0100 | [diff] [blame] | 55 | * |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 56 | * \note Depending on the configuration, this may be a function or |
| 57 | * a pointer to a function. |
| 58 | * |
| 59 | * \note This function may only be used if the PSA crypto subsystem is active. |
| 60 | * This means that you must call psa_crypto_init() before any call to |
| 61 | * this function, and you must not call this function after calling |
| 62 | * mbedtls_psa_crypto_free(). |
| 63 | * |
| 64 | * \param p_rng The random generator context. This must be |
| 65 | * #MBEDTLS_PSA_RANDOM_STATE. No other state is |
| 66 | * supported. |
| 67 | * \param output The buffer to fill. It must have room for |
| 68 | * \c output_size bytes. |
| 69 | * \param output_size The number of bytes to write to \p output. |
| 70 | * This function may fail if \p output_size is too |
| 71 | * large. It is guaranteed to accept any output size |
| 72 | * requested by Mbed TLS library functions. The |
| 73 | * maximum request size depends on the library |
| 74 | * configuration. |
| 75 | * |
| 76 | * \return \c 0 on success. |
| 77 | * \return An `MBEDTLS_ERR_ENTROPY_xxx`, |
Gilles Peskine | 2cff7e2 | 2021-02-16 16:49:42 +0100 | [diff] [blame] | 78 | * `MBEDTLS_ERR_PLATFORM_xxx, |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 79 | * `MBEDTLS_ERR_CTR_DRBG_xxx` or |
| 80 | * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error. |
| 81 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | int mbedtls_psa_get_random(void *p_rng, |
| 83 | unsigned char *output, |
| 84 | size_t output_size); |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 85 | |
| 86 | /** The random generator state for the PSA subsystem. |
| 87 | * |
| 88 | * This macro expands to an expression which is suitable as the `p_rng` |
| 89 | * random generator state parameter of many `mbedtls_xxx` functions. |
| 90 | * It must be used in combination with the random generator function |
| 91 | * mbedtls_psa_get_random(). |
| 92 | * |
| 93 | * The implementation of this macro depends on the configuration of the |
| 94 | * library. Do not make any assumption on its nature. |
| 95 | */ |
| 96 | #define MBEDTLS_PSA_RANDOM_STATE NULL |
| 97 | |
| 98 | #else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
| 99 | |
| 100 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 101 | #include "mbedtls/ctr_drbg.h" |
| 102 | typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t; |
| 103 | static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random; |
| 104 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 105 | #include "mbedtls/hmac_drbg.h" |
| 106 | typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t; |
| 107 | static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random; |
| 108 | #endif |
| 109 | extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state; |
| 110 | |
| 111 | #define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state |
| 112 | |
| 113 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
| 114 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 115 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
Hanno Becker | 186b65a | 2018-11-19 15:14:21 +0000 | [diff] [blame] | 116 | #endif /* MBEDTLS_PSA_UTIL_H */ |