blob: 95974d93edee88e0117bfaeb35ef9ddffa59264a [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 Peskine30524eb2020-11-13 17:02:26 +010025/* Currently, the only supported RNG is Mbed TLS's CTR_DRBG seeded with
26 * mbedtls_entropy_func(). */
27
28#include "mbedtls/ctr_drbg.h"
29#include "mbedtls/entropy.h"
30
31/** The type of the PSA DRBG context.
32 */
33typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
34
35/** Initialize the PSA DRBG.
36 *
37 * \param p_rng Pointer to the Mbed TLS DRBG state.
38 */
39static inline void mbedtls_psa_drbg_init( mbedtls_psa_drbg_context_t *p_rng )
40{
41 mbedtls_ctr_drbg_init( p_rng );
42}
43
44/** Deinitialize the PSA DRBG.
45 *
46 * \param p_rng Pointer to the Mbed TLS DRBG state.
47 */
48static inline void mbedtls_psa_drbg_free( mbedtls_psa_drbg_context_t *p_rng )
49{
50 mbedtls_ctr_drbg_free( p_rng );
51}
52
53/** The type of the PSA random generator context.
54 *
55 * The random generator context is composed of an entropy context and
56 * a DRBG context.
57 */
58typedef struct
59{
60 void (* entropy_init )( mbedtls_entropy_context *ctx );
61 void (* entropy_free )( mbedtls_entropy_context *ctx );
62 mbedtls_entropy_context entropy;
63 mbedtls_psa_drbg_context_t drbg;
64} mbedtls_psa_random_context_t;
65
66/** Return random data.
67 *
68 * This function is suitable as the \p f_rng parameter to Mbed TLS functions
69 * that require a random generator. Use mbedtls_psa_random_state() to
70 * obtain the \p p_rng parameter.
71 *
72 * \param p_rng The CTR_DRBG context. This must be
73 * mbedtls_psa_random_state( \c rng )
74 * where \c rng is a pointer to a
75 * ::mbedtls_psa_random_context_t structure.
76 * \param output The buffer to fill.
77 * \param output_len The length of the buffer in bytes.
78 * It must be at most #MBEDTLS_PSA_RANDOM_MAX_REQUEST.
79 *
80 * \return \c 0 on success.
81 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
82 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
83 */
84static inline int mbedtls_psa_get_random( void *p_rng,
85 unsigned char *output,
86 size_t output_len )
87{
88 return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) );
89}
90
91/** The maximum number of bytes that mbedtls_psa_get_random() is expected to
92 * return.
93 */
94#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_CTR_DRBG_MAX_REQUEST
95
96/** Retrieve the DRBG state from the PSA RNG state.
97 *
98 * \param rng Pointer to the PSA random generator state.
99 *
100 * \return The DRBG state (\c p_rng argument ).
101 */
102static inline mbedtls_psa_drbg_context_t *mbedtls_psa_random_state(
103 mbedtls_psa_random_context_t *rng )
104{
105 return( &rng->drbg );
106}
107
108
109/** Seed the PSA DRBG.
110 *
111 * \param rng DRBG context to be seeded.
112 * \param custom The personalization string.
113 * This can be \c NULL, in which case the personalization
114 * string is empty regardless of the value of \p len.
115 * \param len The length of the personalization string.
116 *
117 * \return \c 0 on success.
118 * \return An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure.
119 */
120static inline int mbedtls_psa_drbg_seed(
121 mbedtls_psa_random_context_t *rng,
122 const unsigned char *custom, size_t len )
123{
124 return( mbedtls_ctr_drbg_seed( mbedtls_psa_random_state( rng ),
125 mbedtls_entropy_func,
126 &rng->entropy,
127 custom, len ) );
128}
Gilles Peskine90edc992020-11-13 15:39:19 +0100129
130#endif /* PSA_CRYPTO_RANDOM_H */