1 /** 2 * \file random.h 3 * 4 * \brief This file contains the prototypes of helper functions to generate 5 * random numbers for the purpose of testing. 6 */ 7 8 /* 9 * Copyright The Mbed TLS Contributors 10 * SPDX-License-Identifier: Apache-2.0 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); you may 13 * not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 */ 24 25 #ifndef TEST_RANDOM_H 26 #define TEST_RANDOM_H 27 28 #include "mbedtls/build_info.h" 29 30 #include <stddef.h> 31 #include <stdint.h> 32 33 typedef struct { 34 unsigned char *buf; /* Pointer to a buffer of length bytes. */ 35 size_t length; 36 /* If fallback_f_rng is NULL, fail after delivering length bytes. */ 37 int (*fallback_f_rng)(void *, unsigned char *, size_t); 38 void *fallback_p_rng; 39 } mbedtls_test_rnd_buf_info; 40 41 /** 42 * Info structure for the pseudo random function 43 * 44 * Key should be set at the start to a test-unique value. 45 * Do not forget endianness! 46 * State( v0, v1 ) should be set to zero. 47 */ 48 typedef struct { 49 uint32_t key[16]; 50 uint32_t v0, v1; 51 } mbedtls_test_rnd_pseudo_info; 52 53 /** 54 * This function just returns data from rand(). 55 * Although predictable and often similar on multiple 56 * runs, this does not result in identical random on 57 * each run. So do not use this if the results of a 58 * test depend on the random data that is generated. 59 * 60 * rng_state shall be NULL. 61 */ 62 int mbedtls_test_rnd_std_rand(void *rng_state, 63 unsigned char *output, 64 size_t len); 65 66 /** 67 * This function only returns zeros. 68 * 69 * \p rng_state shall be \c NULL. 70 */ 71 int mbedtls_test_rnd_zero_rand(void *rng_state, 72 unsigned char *output, 73 size_t len); 74 75 /** 76 * This function returns random data based on a buffer it receives. 77 * 78 * \p rng_state shall be a pointer to a #mbedtls_test_rnd_buf_info structure. 79 * 80 * The number of bytes released from the buffer on each call to 81 * the random function is specified by \p len. 82 * 83 * After the buffer is empty, this function will call the fallback RNG in the 84 * #mbedtls_test_rnd_buf_info structure if there is one, and 85 * will return #MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise. 86 */ 87 int mbedtls_test_rnd_buffer_rand(void *rng_state, 88 unsigned char *output, 89 size_t len); 90 91 /** 92 * This function returns random based on a pseudo random function. 93 * This means the results should be identical on all systems. 94 * Pseudo random is based on the XTEA encryption algorithm to 95 * generate pseudorandom. 96 * 97 * \p rng_state shall be a pointer to a #mbedtls_test_rnd_pseudo_info structure. 98 */ 99 int mbedtls_test_rnd_pseudo_rand(void *rng_state, 100 unsigned char *output, 101 size_t len); 102 103 #endif /* TEST_RANDOM_H */ 104