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