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
9  *
10  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
11  *  not use this file except in compliance with the License.
12  *  You may obtain a copy of the License at
13  *
14  *  http://www.apache.org/licenses/LICENSE-2.0
15  *
16  *  Unless required by applicable law or agreed to in writing, software
17  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  *  See the License for the specific language governing permissions and
20  *  limitations under the License.
21  */
22 
23 #include <test/fake_external_rng_for_test.h>
24 
25 #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
26 #include <test/random.h>
27 #include <psa/crypto.h>
28 
29 static int test_insecure_external_rng_enabled = 0;
30 
mbedtls_test_enable_insecure_external_rng(void)31 void mbedtls_test_enable_insecure_external_rng( void )
32 {
33     test_insecure_external_rng_enabled = 1;
34 }
35 
mbedtls_test_disable_insecure_external_rng(void)36 void mbedtls_test_disable_insecure_external_rng( void )
37 {
38     test_insecure_external_rng_enabled = 0;
39 }
40 
mbedtls_psa_external_get_random(mbedtls_psa_external_random_context_t * context,uint8_t * output,size_t output_size,size_t * output_length)41 psa_status_t mbedtls_psa_external_get_random(
42     mbedtls_psa_external_random_context_t *context,
43     uint8_t *output, size_t output_size, size_t *output_length )
44 {
45     (void) context;
46 
47     if( !test_insecure_external_rng_enabled )
48         return( PSA_ERROR_INSUFFICIENT_ENTROPY );
49 
50     /* This implementation is for test purposes only!
51      * Use the libc non-cryptographic random generator. */
52     mbedtls_test_rnd_std_rand( NULL, output, output_size );
53     *output_length = output_size;
54     return( PSA_SUCCESS );
55 }
56 #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
57