blob: 0f13c13927d53b8bd94feca96e46c6d50d861a10 [file] [log] [blame]
Gilles Peskine90edc992020-11-13 15:39:19 +01001/** \file psa_crypto_random.h
2 *
3 * \brief PSA crypto random generator abstraction.
4 */
5/*
6 * Copyright The Mbed TLS Contributors
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22#ifndef PSA_CRYPTO_RANDOM_H
23#define PSA_CRYPTO_RANDOM_H
24
Gilles Peskine4fc21fd2020-11-13 18:47:18 +010025#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
26
27#include <string.h>
28#include <mbedtls/entropy.h> // only for error codes
29#include <psa/crypto.h>
30
31typedef mbedtls_psa_external_random_context_t mbedtls_psa_random_context_t;
32
33static inline int mbedtls_psa_get_random( void *p_rng,
34 unsigned char *output,
35 size_t output_size )
36{
37 (void) p_rng;
38 psa_status_t status = psa_generate_random( output, output_size );
39 if( status == PSA_SUCCESS )
40 return( 0 );
41 else
42 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
43}
44
45static inline void *mbedtls_psa_random_state( mbedtls_psa_random_context_t *rng )
46{
47 (void) rng;
48 return( NULL );
49}
50
51#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
52
Gilles Peskine82e57d12020-11-13 21:31:17 +010053/* Choose a DRBG based on configuration and availability */
54#if defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
55
56#include "mbedtls/hmac_drbg.h"
57
58#elif defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine30524eb2020-11-13 17:02:26 +010059
60#include "mbedtls/ctr_drbg.h"
Gilles Peskine82e57d12020-11-13 21:31:17 +010061
62#elif defined(MBEDTLS_HMAC_DRBG_C)
63
64#include "mbedtls/hmac_drbg.h"
65#if defined(MBEDTLS_SHA512_C) && defined(MBEDTLS_SHA256_C)
66#include <limits.h>
67#if SIZE_MAX > 0xffffffff
68/* Looks like a 64-bit system, so prefer SHA-512. */
69#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
70#else
71/* Looks like a 32-bit system, so prefer SHA-256. */
72#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256
73#endif
74#elif defined(MBEDTLS_SHA512_C)
75#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
76#elif defined(MBEDTLS_SHA256_C)
77#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256
78#else
79#error "No hash algorithm available for HMAC_DBRG."
80#endif
81
82#else
83#error "No DRBG module available for the psa_crypto module."
84#endif
85
Gilles Peskine30524eb2020-11-13 17:02:26 +010086#include "mbedtls/entropy.h"
87
88/** The type of the PSA DRBG context.
89 */
Gilles Peskine82e57d12020-11-13 21:31:17 +010090#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine30524eb2020-11-13 17:02:26 +010091typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
Gilles Peskine82e57d12020-11-13 21:31:17 +010092#elif defined(MBEDTLS_HMAC_DRBG_C)
93typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
94#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +010095
96/** Initialize the PSA DRBG.
97 *
98 * \param p_rng Pointer to the Mbed TLS DRBG state.
99 */
100static inline void mbedtls_psa_drbg_init( mbedtls_psa_drbg_context_t *p_rng )
101{
Gilles Peskine82e57d12020-11-13 21:31:17 +0100102#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine30524eb2020-11-13 17:02:26 +0100103 mbedtls_ctr_drbg_init( p_rng );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100104#elif defined(MBEDTLS_HMAC_DRBG_C)
105 mbedtls_hmac_drbg_init( p_rng );
106#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +0100107}
108
109/** Deinitialize the PSA DRBG.
110 *
111 * \param p_rng Pointer to the Mbed TLS DRBG state.
112 */
113static inline void mbedtls_psa_drbg_free( mbedtls_psa_drbg_context_t *p_rng )
114{
Gilles Peskine82e57d12020-11-13 21:31:17 +0100115#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine30524eb2020-11-13 17:02:26 +0100116 mbedtls_ctr_drbg_free( p_rng );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100117#elif defined(MBEDTLS_HMAC_DRBG_C)
118 mbedtls_hmac_drbg_free( p_rng );
119#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +0100120}
121
122/** The type of the PSA random generator context.
123 *
124 * The random generator context is composed of an entropy context and
125 * a DRBG context.
126 */
127typedef struct
128{
129 void (* entropy_init )( mbedtls_entropy_context *ctx );
130 void (* entropy_free )( mbedtls_entropy_context *ctx );
131 mbedtls_entropy_context entropy;
132 mbedtls_psa_drbg_context_t drbg;
133} mbedtls_psa_random_context_t;
134
135/** Return random data.
136 *
137 * This function is suitable as the \p f_rng parameter to Mbed TLS functions
138 * that require a random generator. Use mbedtls_psa_random_state() to
139 * obtain the \p p_rng parameter.
140 *
Gilles Peskine82e57d12020-11-13 21:31:17 +0100141 * \param p_rng The DRBG context. This must be
Gilles Peskine30524eb2020-11-13 17:02:26 +0100142 * mbedtls_psa_random_state( \c rng )
143 * where \c rng is a pointer to a
144 * ::mbedtls_psa_random_context_t structure.
145 * \param output The buffer to fill.
146 * \param output_len The length of the buffer in bytes.
147 * It must be at most #MBEDTLS_PSA_RANDOM_MAX_REQUEST.
148 *
Gilles Peskine82e57d12020-11-13 21:31:17 +0100149 * \retval \c 0 on success.
150 * \return \c MBEDTLS_ERR_xxx_DRBG_xxx or
151 * \c MBEDTLS_ERR_PLATFORM_xxx on failure.
Gilles Peskine30524eb2020-11-13 17:02:26 +0100152 */
153static inline int mbedtls_psa_get_random( void *p_rng,
154 unsigned char *output,
155 size_t output_len )
156{
Gilles Peskine82e57d12020-11-13 21:31:17 +0100157#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine30524eb2020-11-13 17:02:26 +0100158 return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100159#elif defined(MBEDTLS_HMAC_DRBG_C)
160 return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) );
161#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +0100162}
163
164/** The maximum number of bytes that mbedtls_psa_get_random() is expected to
165 * return.
166 */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100167#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine30524eb2020-11-13 17:02:26 +0100168#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_CTR_DRBG_MAX_REQUEST
Gilles Peskine82e57d12020-11-13 21:31:17 +0100169#elif defined(MBEDTLS_HMAC_DRBG_C)
170#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_HMAC_DRBG_MAX_REQUEST
171#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +0100172
173/** Retrieve the DRBG state from the PSA RNG state.
174 *
175 * \param rng Pointer to the PSA random generator state.
176 *
177 * \return The DRBG state (\c p_rng argument ).
178 */
179static inline mbedtls_psa_drbg_context_t *mbedtls_psa_random_state(
180 mbedtls_psa_random_context_t *rng )
181{
182 return( &rng->drbg );
183}
184
Gilles Peskine30524eb2020-11-13 17:02:26 +0100185/** Seed the PSA DRBG.
186 *
187 * \param rng DRBG context to be seeded.
188 * \param custom The personalization string.
189 * This can be \c NULL, in which case the personalization
190 * string is empty regardless of the value of \p len.
191 * \param len The length of the personalization string.
192 *
193 * \return \c 0 on success.
194 * \return An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure.
195 */
196static inline int mbedtls_psa_drbg_seed(
197 mbedtls_psa_random_context_t *rng,
198 const unsigned char *custom, size_t len )
199{
Gilles Peskine82e57d12020-11-13 21:31:17 +0100200#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine30524eb2020-11-13 17:02:26 +0100201 return( mbedtls_ctr_drbg_seed( mbedtls_psa_random_state( rng ),
202 mbedtls_entropy_func,
203 &rng->entropy,
204 custom, len ) );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100205#elif defined(MBEDTLS_HMAC_DRBG_C)
206 const mbedtls_md_info_t *md_info =
207 mbedtls_md_info_from_type( MBEDTLS_PSA_HMAC_DRBG_MD_TYPE );
208 return( mbedtls_hmac_drbg_seed( mbedtls_psa_random_state( rng ),
209 md_info,
210 mbedtls_entropy_func,
211 &rng->entropy,
212 custom, len ) );
213#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +0100214}
Gilles Peskine90edc992020-11-13 15:39:19 +0100215
Gilles Peskine4fc21fd2020-11-13 18:47:18 +0100216#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
217
Gilles Peskine90edc992020-11-13 15:39:19 +0100218#endif /* PSA_CRYPTO_RANDOM_H */