Ronald Cron | b7eb67f | 2020-06-09 16:57:42 +0200 | [diff] [blame] | 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 | |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 8 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 9 | * Copyright The Mbed TLS Contributors |
Ronald Cron | b7eb67f | 2020-06-09 16:57:42 +0200 | [diff] [blame] | 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. |
Ronald Cron | b7eb67f | 2020-06-09 16:57:42 +0200 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #ifndef TEST_RANDOM_H |
| 26 | #define TEST_RANDOM_H |
| 27 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 28 | #include "mbedtls/build_info.h" |
Ronald Cron | b7eb67f | 2020-06-09 16:57:42 +0200 | [diff] [blame] | 29 | |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 30 | #include <stddef.h> |
| 31 | #include <stdint.h> |
| 32 | |
| 33 | typedef struct |
| 34 | { |
Gilles Peskine | ecacc3c | 2021-03-24 00:48:57 +0100 | [diff] [blame] | 35 | unsigned char *buf; /* Pointer to a buffer of length bytes. */ |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 36 | size_t length; |
Gilles Peskine | ecacc3c | 2021-03-24 00:48:57 +0100 | [diff] [blame] | 37 | /* If fallback_f_rng is NULL, fail after delivering length bytes. */ |
| 38 | int ( *fallback_f_rng )( void*, unsigned char *, size_t ); |
| 39 | void *fallback_p_rng; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 40 | } mbedtls_test_rnd_buf_info; |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Info structure for the pseudo random function |
| 44 | * |
| 45 | * Key should be set at the start to a test-unique value. |
| 46 | * Do not forget endianness! |
| 47 | * State( v0, v1 ) should be set to zero. |
| 48 | */ |
| 49 | typedef struct |
| 50 | { |
| 51 | uint32_t key[16]; |
| 52 | uint32_t v0, v1; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 53 | } mbedtls_test_rnd_pseudo_info; |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * This function just returns data from rand(). |
| 57 | * Although predictable and often similar on multiple |
| 58 | * runs, this does not result in identical random on |
| 59 | * each run. So do not use this if the results of a |
| 60 | * test depend on the random data that is generated. |
| 61 | * |
| 62 | * rng_state shall be NULL. |
| 63 | */ |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 64 | int mbedtls_test_rnd_std_rand( void *rng_state, |
| 65 | unsigned char *output, |
| 66 | size_t len ); |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 67 | |
| 68 | /** |
Gilles Peskine | 0b1b0ab | 2021-03-24 00:14:53 +0100 | [diff] [blame] | 69 | * This function only returns zeros. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 70 | * |
Gilles Peskine | 0b1b0ab | 2021-03-24 00:14:53 +0100 | [diff] [blame] | 71 | * \p rng_state shall be \c NULL. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 72 | */ |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 73 | int mbedtls_test_rnd_zero_rand( void *rng_state, |
| 74 | unsigned char *output, |
| 75 | size_t len ); |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 76 | |
| 77 | /** |
Gilles Peskine | 0b1b0ab | 2021-03-24 00:14:53 +0100 | [diff] [blame] | 78 | * This function returns random data based on a buffer it receives. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 79 | * |
Gilles Peskine | 0b1b0ab | 2021-03-24 00:14:53 +0100 | [diff] [blame] | 80 | * \p rng_state shall be a pointer to a #mbedtls_test_rnd_buf_info structure. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 81 | * |
| 82 | * The number of bytes released from the buffer on each call to |
Gilles Peskine | c7eeeb1 | 2021-06-02 21:17:36 +0200 | [diff] [blame] | 83 | * the random function is specified by \p len. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 84 | * |
Gilles Peskine | ecacc3c | 2021-03-24 00:48:57 +0100 | [diff] [blame] | 85 | * After the buffer is empty, this function will call the fallback RNG in the |
| 86 | * #mbedtls_test_rnd_buf_info structure if there is one, and |
| 87 | * will return #MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 88 | */ |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 89 | int mbedtls_test_rnd_buffer_rand( void *rng_state, |
| 90 | unsigned char *output, |
| 91 | size_t len ); |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * This function returns random based on a pseudo random function. |
| 95 | * This means the results should be identical on all systems. |
| 96 | * Pseudo random is based on the XTEA encryption algorithm to |
| 97 | * generate pseudorandom. |
| 98 | * |
Gilles Peskine | 0b1b0ab | 2021-03-24 00:14:53 +0100 | [diff] [blame] | 99 | * \p rng_state shall be a pointer to a #mbedtls_test_rnd_pseudo_info structure. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 100 | */ |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 101 | int mbedtls_test_rnd_pseudo_rand( void *rng_state, |
| 102 | unsigned char *output, |
| 103 | size_t len ); |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 104 | |
Ronald Cron | b7eb67f | 2020-06-09 16:57:42 +0200 | [diff] [blame] | 105 | #endif /* TEST_RANDOM_H */ |