1 /** \file fake_external_rng_for_test.c
2  *
3  * \brief Helper functions to test PSA crypto functionality.
4  */
5 
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 
11 #include <test/fake_external_rng_for_test.h>
12 
13 #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
14 #include <test/random.h>
15 #include <psa/crypto.h>
16 
17 static int test_insecure_external_rng_enabled = 0;
18 
mbedtls_test_enable_insecure_external_rng(void)19 void mbedtls_test_enable_insecure_external_rng(void)
20 {
21     test_insecure_external_rng_enabled = 1;
22 }
23 
mbedtls_test_disable_insecure_external_rng(void)24 void mbedtls_test_disable_insecure_external_rng(void)
25 {
26     test_insecure_external_rng_enabled = 0;
27 }
28 
mbedtls_psa_external_get_random(mbedtls_psa_external_random_context_t * context,uint8_t * output,size_t output_size,size_t * output_length)29 psa_status_t mbedtls_psa_external_get_random(
30     mbedtls_psa_external_random_context_t *context,
31     uint8_t *output, size_t output_size, size_t *output_length)
32 {
33     (void) context;
34 
35     if (!test_insecure_external_rng_enabled) {
36         return PSA_ERROR_INSUFFICIENT_ENTROPY;
37     }
38 
39     /* This implementation is for test purposes only!
40      * Use the libc non-cryptographic random generator. */
41     mbedtls_test_rnd_std_rand(NULL, output, output_size);
42     *output_length = output_size;
43     return PSA_SUCCESS;
44 }
45 #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
46