Skip to content
0.2.0-dev Download ZIP
168 lines ·5.9 KB Raw
1/*
2 * PSA crypto random generator.
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 */
8
9#include "tf_psa_crypto_common.h"
10
11#if defined(MBEDTLS_PSA_CRYPTO_C) && !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
12
13#include "psa_crypto_core.h"
14#include "psa_crypto_random.h"
15#include "psa_crypto_random_impl.h"
16#include "threading_internal.h"
17
18#if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE)
19/* For getpid(), for fork protection */
20#include <unistd.h>
21#if defined(MBEDTLS_HAVE_TIME)
22#include <mbedtls/platform_time.h>
23#else
24/* For gettimeofday(), for fork protection without actual entropy */
25#include <sys/time.h>
26#endif
27#endif
28
29void psa_random_internal_init(mbedtls_psa_random_context_t *rng)
30{
31 /* Set default configuration if
32 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
33 if (rng->entropy_init == NULL) {
34 rng->entropy_init = mbedtls_entropy_init;
35 }
36 if (rng->entropy_free == NULL) {
37 rng->entropy_free = mbedtls_entropy_free;
38 }
39
40 rng->entropy_init(&rng->entropy);
41
42 mbedtls_psa_drbg_init(&rng->drbg);
43}
44
45void psa_random_internal_free(mbedtls_psa_random_context_t *rng)
46{
47 mbedtls_psa_drbg_free(&rng->drbg);
48 rng->entropy_free(&rng->entropy);
49}
50psa_status_t psa_random_internal_seed(mbedtls_psa_random_context_t *rng)
51{
52 const unsigned char drbg_seed[] = "PSA";
53 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
54 drbg_seed, sizeof(drbg_seed) - 1);
55#if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE)
56 rng->pid = getpid();
57#endif
58 return mbedtls_to_psa_error(ret);
59}
60
61#if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE)
62static psa_status_t psa_random_internal_reseed_child(
63 mbedtls_psa_random_context_t *rng,
64 intmax_t pid)
65{
66 /* Reseeding from actual entropy gives the child a unique RNG state
67 * which the parent process cannot predict, and wipes the
68 * parent's RNG state from the child.
69 *
70 * However, in some library configurations, there is no actual
71 * entropy source, only a nonvolatile seed (MBEDTLS_ENTROPY_NV_SEED
72 * enabled and no actual entropy source enabled). In such a
73 * configuration, the reseed operation is deterministic and
74 * always injects the same content, so with the DRBG reseed
75 * process alone, for example, two child processes forked in
76 * close sequence would end up with the same RNG state.
77
78 * To avoid this, we use a personalization string that has a high
79 * likelihood of being unique. This way, the child has a unique state.
80 * The parent can predict the child's RNG state until the next time
81 * it reseeds or generates some random output, but that's
82 * unavoidable in the absence of actual entropy.
83 */
84 struct {
85 /* Using the PID mostly guarantees that each child gets a
86 * unique state. */
87 /* Use intmax_t, not pid_t, because some Unix-like platforms
88 * don't define pid_t, or more likely nowadays they define
89 * pid_t but only with certain platform macros which might not
90 * be the exact ones we use. In practice, this only costs
91 * a couple of instructions to pass and compare two words
92 * rather than one.
93 */
94 intmax_t pid;
95 /* In case an old child had died and its PID is reused for
96 * a new child of the same process, also include the time. */
97#if defined(MBEDTLS_HAVE_TIME)
98 mbedtls_ms_time_t now;
99#else
100 struct timeval now;
101#endif
102 } perso;
103 memset(&perso, 0, sizeof(perso));
104 perso.pid = pid;
105#if defined(MBEDTLS_HAVE_TIME)
106 perso.now = mbedtls_ms_time();
107#else
108 /* We don't have mbedtls_ms_time(), but the platform has getpid().
109 * Use gettimeofday(), which is a classic Unix function. Modern POSIX
110 * has stopped requiring gettimeofday() (in favor of clock_gettime()),
111 * but this is fallback code for restricted configurations, so it's
112 * more likely to be used on embedded platforms that only have a subset
113 * of Unix APIs and are more likely to have the classic gettimeofday(). */
114 if (gettimeofday(&perso.now, NULL) == -1) {
115 return PSA_ERROR_INSUFFICIENT_ENTROPY;
116 }
117#endif
118 int ret = mbedtls_psa_drbg_reseed(&rng->drbg,
119 (unsigned char *) &perso, sizeof(perso));
120 return mbedtls_to_psa_error(ret);
121}
122#endif /* MBEDTLS_PLATFORM_IS_UNIXLIKE */
123
124psa_status_t psa_random_internal_generate(
125 mbedtls_psa_random_context_t *rng,
126 uint8_t *output, size_t output_size)
127{
128#if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE)
129 intmax_t pid = getpid();
130 if (pid != rng->pid) {
131 /* This is a (grand...)child of the original process, but
132 * we inherited the RNG state from our parent. We must reseed! */
133#if defined(MBEDTLS_THREADING_C)
134 mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex);
135#endif /* defined(MBEDTLS_THREADING_C) */
136 psa_status_t status = psa_random_internal_reseed_child(rng, pid);
137 if (status == PSA_SUCCESS) {
138 rng->pid = pid;
139 }
140#if defined(MBEDTLS_THREADING_C)
141 mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex);
142#endif /* defined(MBEDTLS_THREADING_C) */
143 if (status != PSA_SUCCESS) {
144 return status;
145 }
146 }
147#endif /* MBEDTLS_PLATFORM_IS_UNIXLIKE */
148
149 while (output_size > 0) {
150 size_t request_size =
151 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
152 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
153 output_size);
154#if defined(MBEDTLS_CTR_DRBG_C)
155 int ret = mbedtls_ctr_drbg_random(&rng->drbg, output, request_size);
156#elif defined(MBEDTLS_HMAC_DRBG_C)
157 int ret = mbedtls_hmac_drbg_random(&rng->drbg, output, request_size);
158#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
159 if (ret != 0) {
160 return mbedtls_to_psa_error(ret);
161 }
162 output_size -= request_size;
163 output += request_size;
164 }
165 return PSA_SUCCESS;
166}
167
168#endif /* MBEDTLS_PSA_CRYPTO_C && !MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */