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 OR GPL-2.0-or-later 11 */ 12 13 #ifndef TEST_RANDOM_H 14 #define TEST_RANDOM_H 15 16 #if !defined(MBEDTLS_CONFIG_FILE) 17 #include "mbedtls/config.h" 18 #else 19 #include MBEDTLS_CONFIG_FILE 20 #endif 21 22 #include <stddef.h> 23 #include <stdint.h> 24 25 typedef struct { 26 unsigned char *buf; /* Pointer to a buffer of length bytes. */ 27 size_t length; 28 /* If fallback_f_rng is NULL, fail after delivering length bytes. */ 29 int (*fallback_f_rng)(void *, unsigned char *, size_t); 30 void *fallback_p_rng; 31 } mbedtls_test_rnd_buf_info; 32 33 /** 34 * Info structure for the pseudo random function 35 * 36 * Key should be set at the start to a test-unique value. 37 * Do not forget endianness! 38 * State( v0, v1 ) should be set to zero. 39 */ 40 typedef struct { 41 uint32_t key[16]; 42 uint32_t v0, v1; 43 } mbedtls_test_rnd_pseudo_info; 44 45 /** 46 * This function just returns data from rand(). 47 * Although predictable and often similar on multiple 48 * runs, this does not result in identical random on 49 * each run. So do not use this if the results of a 50 * test depend on the random data that is generated. 51 * 52 * rng_state shall be NULL. 53 */ 54 int mbedtls_test_rnd_std_rand(void *rng_state, 55 unsigned char *output, 56 size_t len); 57 58 /** 59 * This function only returns zeros. 60 * 61 * \p rng_state shall be \c NULL. 62 */ 63 int mbedtls_test_rnd_zero_rand(void *rng_state, 64 unsigned char *output, 65 size_t len); 66 67 /** 68 * This function returns random data based on a buffer it receives. 69 * 70 * \p rng_state shall be a pointer to a #mbedtls_test_rnd_buf_info structure. 71 * 72 * The number of bytes released from the buffer on each call to 73 * the random function is specified by \p len. 74 * 75 * After the buffer is empty, this function will call the fallback RNG in the 76 * #mbedtls_test_rnd_buf_info structure if there is one, and 77 * will return #MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise. 78 */ 79 int mbedtls_test_rnd_buffer_rand(void *rng_state, 80 unsigned char *output, 81 size_t len); 82 83 /** 84 * This function returns random based on a pseudo random function. 85 * This means the results should be identical on all systems. 86 * Pseudo random is based on the XTEA encryption algorithm to 87 * generate pseudorandom. 88 * 89 * \p rng_state shall be a pointer to a #mbedtls_test_rnd_pseudo_info structure. 90 */ 91 int mbedtls_test_rnd_pseudo_rand(void *rng_state, 92 unsigned char *output, 93 size_t len); 94 95 #endif /* TEST_RANDOM_H */ 96