blob: dfdefa688e7899707063217efc8705af5f50cfb9 [file] [log] [blame]
Ronald Cronb7eb67f2020-06-09 16:57:42 +02001/**
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/* Copyright (C) 2020, ARM Limited, All Rights Reserved
9 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
23 * This file is part of mbed TLS (https://tls.mbed.org)
24 */
25
26#ifndef TEST_RANDOM_H
27#define TEST_RANDOM_H
28
29#if !defined(MBEDTLS_CONFIG_FILE)
30#include "mbedtls/config.h"
31#else
32#include MBEDTLS_CONFIG_FILE
33#endif
34
Ronald Cron2058d562020-06-09 17:11:47 +020035#include <stddef.h>
36#include <stdint.h>
37
38typedef struct
39{
40 unsigned char *buf;
41 size_t length;
Ronald Cron351f0ee2020-06-10 12:12:18 +020042} mbedtls_test_rnd_buf_info;
Ronald Cron2058d562020-06-09 17:11:47 +020043
44/**
45 * Info structure for the pseudo random function
46 *
47 * Key should be set at the start to a test-unique value.
48 * Do not forget endianness!
49 * State( v0, v1 ) should be set to zero.
50 */
51typedef struct
52{
53 uint32_t key[16];
54 uint32_t v0, v1;
Ronald Cron351f0ee2020-06-10 12:12:18 +020055} mbedtls_test_rnd_pseudo_info;
Ronald Cron2058d562020-06-09 17:11:47 +020056
57/**
58 * This function just returns data from rand().
59 * Although predictable and often similar on multiple
60 * runs, this does not result in identical random on
61 * each run. So do not use this if the results of a
62 * test depend on the random data that is generated.
63 *
64 * rng_state shall be NULL.
65 */
Ronald Cron351f0ee2020-06-10 12:12:18 +020066int mbedtls_test_rnd_std_rand( void *rng_state,
67 unsigned char *output,
68 size_t len );
Ronald Cron2058d562020-06-09 17:11:47 +020069
70/**
71 * This function only returns zeros
72 *
73 * rng_state shall be NULL.
74 */
Ronald Cron351f0ee2020-06-10 12:12:18 +020075int mbedtls_test_rnd_zero_rand( void *rng_state,
76 unsigned char *output,
77 size_t len );
Ronald Cron2058d562020-06-09 17:11:47 +020078
79/**
80 * This function returns random based on a buffer it receives.
81 *
82 * rng_state shall be a pointer to a rnd_buf_info structure.
83 *
84 * The number of bytes released from the buffer on each call to
85 * the random function is specified by per_call. (Can be between
86 * 1 and 4)
87 *
88 * After the buffer is empty it will return rand();
89 */
Ronald Cron351f0ee2020-06-10 12:12:18 +020090int mbedtls_test_rnd_buffer_rand( void *rng_state,
91 unsigned char *output,
92 size_t len );
Ronald Cron2058d562020-06-09 17:11:47 +020093
94/**
95 * This function returns random based on a pseudo random function.
96 * This means the results should be identical on all systems.
97 * Pseudo random is based on the XTEA encryption algorithm to
98 * generate pseudorandom.
99 *
100 * rng_state shall be a pointer to a rnd_pseudo_info structure.
101 */
Ronald Cron351f0ee2020-06-10 12:12:18 +0200102int mbedtls_test_rnd_pseudo_rand( void *rng_state,
103 unsigned char *output,
104 size_t len );
Ronald Cron2058d562020-06-09 17:11:47 +0200105
Ronald Cronb7eb67f2020-06-09 16:57:42 +0200106#endif /* TEST_RANDOM_H */