blob: c32fceaac2b5526a1917096d1fd680dd5645fb03 [file] [log] [blame]
Hanno Becker87837b22018-11-08 13:32:02 +00001/**
Hanno Beckerafebf5a2018-11-13 21:01:41 +00002 * \file psa_util.h
Hanno Becker87837b22018-11-08 13:32:02 +00003 *
4 * \brief Utility functions for the use of the PSA Crypto library.
5 *
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +02006 * \warning These functions are not part of the public API and may
Hanno Becker87837b22018-11-08 13:32:02 +00007 * change at any time.
8 */
9/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020010 * Copyright The Mbed TLS Contributors
Hanno Becker87837b22018-11-08 13:32:02 +000011 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Hanno Becker87837b22018-11-08 13:32:02 +000024 */
25
Hanno Becker186b65a2018-11-19 15:14:21 +000026#ifndef MBEDTLS_PSA_UTIL_H
27#define MBEDTLS_PSA_UTIL_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020028#include "mbedtls/private_access.h"
Hanno Becker87837b22018-11-08 13:32:02 +000029
Bence Szépkútic662b362021-05-27 11:25:03 +020030#include "mbedtls/build_info.h"
Hanno Becker87837b22018-11-08 13:32:02 +000031
Jerry Yub02ee182022-03-16 10:30:41 +080032#if defined(MBEDTLS_PSA_CRYPTO_C)
Hanno Becker87837b22018-11-08 13:32:02 +000033
Gilles Peskinee3ed8022021-02-03 20:04:08 +010034/* Expose whatever RNG the PSA subsystem uses to applications using the
Gilles Peskine996f2162021-02-16 16:50:00 +010035 * mbedtls_xxx API. The declarations and definitions here need to be
36 * consistent with the implementation in library/psa_crypto_random_impl.h.
37 * See that file for implementation documentation. */
Jerry Yu406cf272022-03-22 11:33:42 +080038
Gilles Peskinee3ed8022021-02-03 20:04:08 +010039
40/* The type of a `f_rng` random generator function that many library functions
41 * take.
42 *
43 * This type name is not part of the Mbed TLS stable API. It may be renamed
44 * or moved without warning.
45 */
Gilles Peskine449bd832023-01-11 14:50:10 +010046typedef int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +010047
48#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
49
50/** The random generator function for the PSA subsystem.
51 *
52 * This function is suitable as the `f_rng` random generator function
Gilles Peskine2cff7e22021-02-16 16:49:42 +010053 * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE
54 * to obtain the \p p_rng parameter.
Gilles Peskinee3ed8022021-02-03 20:04:08 +010055 *
56 * The implementation of this function depends on the configuration of the
57 * library.
Gilles Peskine2cff7e22021-02-16 16:49:42 +010058 *
Gilles Peskinee3ed8022021-02-03 20:04:08 +010059 * \note Depending on the configuration, this may be a function or
60 * a pointer to a function.
61 *
62 * \note This function may only be used if the PSA crypto subsystem is active.
63 * This means that you must call psa_crypto_init() before any call to
64 * this function, and you must not call this function after calling
65 * mbedtls_psa_crypto_free().
66 *
67 * \param p_rng The random generator context. This must be
68 * #MBEDTLS_PSA_RANDOM_STATE. No other state is
69 * supported.
70 * \param output The buffer to fill. It must have room for
71 * \c output_size bytes.
72 * \param output_size The number of bytes to write to \p output.
73 * This function may fail if \p output_size is too
74 * large. It is guaranteed to accept any output size
75 * requested by Mbed TLS library functions. The
76 * maximum request size depends on the library
77 * configuration.
78 *
79 * \return \c 0 on success.
80 * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
Gilles Peskine2cff7e22021-02-16 16:49:42 +010081 * `MBEDTLS_ERR_PLATFORM_xxx,
Gilles Peskinee3ed8022021-02-03 20:04:08 +010082 * `MBEDTLS_ERR_CTR_DRBG_xxx` or
83 * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
84 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085int mbedtls_psa_get_random(void *p_rng,
86 unsigned char *output,
87 size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +010088
89/** The random generator state for the PSA subsystem.
90 *
91 * This macro expands to an expression which is suitable as the `p_rng`
92 * random generator state parameter of many `mbedtls_xxx` functions.
93 * It must be used in combination with the random generator function
94 * mbedtls_psa_get_random().
95 *
96 * The implementation of this macro depends on the configuration of the
97 * library. Do not make any assumption on its nature.
98 */
99#define MBEDTLS_PSA_RANDOM_STATE NULL
100
101#else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
102
103#if defined(MBEDTLS_CTR_DRBG_C)
104#include "mbedtls/ctr_drbg.h"
105typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
106static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random;
107#elif defined(MBEDTLS_HMAC_DRBG_C)
108#include "mbedtls/hmac_drbg.h"
109typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
110static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random;
111#endif
112extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
113
114#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
115
116#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
117
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500118#endif /* MBEDTLS_PSA_CRYPTO_C */
Hanno Becker186b65a2018-11-19 15:14:21 +0000119#endif /* MBEDTLS_PSA_UTIL_H */