blob: af88d98419c122926398e3ce3f6148d91d63d913 [file] [log] [blame]
Ronald Cronb7eb67f2020-06-09 16:57:42 +02001/**
2 * \file random.c
3 *
4 * \brief This file contains the helper functions to generate random numbers
5 * for the purpose of testing.
6 */
7
Bence Szépkúti86974652020-06-15 11:59:37 +02008/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02009 * Copyright The Mbed TLS Contributors
Ronald Cronb7eb67f2020-06-09 16:57:42 +020010 * 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 Cronb7eb67f2020-06-09 16:57:42 +020023 */
24
Ronald Cron2058d562020-06-09 17:11:47 +020025#include <test/macros.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +020026#include <test/random.h>
Ronald Cron2058d562020-06-09 17:11:47 +020027#include <string.h>
28
Ronald Cron351f0ee2020-06-10 12:12:18 +020029int mbedtls_test_rnd_std_rand( void *rng_state,
30 unsigned char *output,
31 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020032{
gufe44c2620da2020-08-03 17:56:50 +020033#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Ronald Cron2058d562020-06-09 17:11:47 +020034 size_t i;
35
36 if( rng_state != NULL )
37 rng_state = NULL;
38
39 for( i = 0; i < len; ++i )
40 output[i] = rand();
41#else
42 if( rng_state != NULL )
43 rng_state = NULL;
44
45 arc4random_buf( output, len );
gufe44c2620da2020-08-03 17:56:50 +020046#endif /* !OpenBSD && !NetBSD */
Ronald Cron2058d562020-06-09 17:11:47 +020047
48 return( 0 );
49}
50
Ronald Cron351f0ee2020-06-10 12:12:18 +020051int mbedtls_test_rnd_zero_rand( void *rng_state,
52 unsigned char *output,
53 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020054{
55 if( rng_state != NULL )
56 rng_state = NULL;
57
58 memset( output, 0, len );
59
60 return( 0 );
61}
62
Ronald Cron351f0ee2020-06-10 12:12:18 +020063int mbedtls_test_rnd_buffer_rand( void *rng_state,
64 unsigned char *output,
65 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020066{
Ronald Cron351f0ee2020-06-10 12:12:18 +020067 mbedtls_test_rnd_buf_info *info = (mbedtls_test_rnd_buf_info *) rng_state;
Ronald Cron2058d562020-06-09 17:11:47 +020068 size_t use_len;
69
70 if( rng_state == NULL )
Ronald Cron351f0ee2020-06-10 12:12:18 +020071 return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
Ronald Cron2058d562020-06-09 17:11:47 +020072
73 use_len = len;
74 if( len > info->length )
75 use_len = info->length;
76
77 if( use_len )
78 {
79 memcpy( output, info->buf, use_len );
80 info->buf += use_len;
81 info->length -= use_len;
82 }
83
84 if( len - use_len > 0 )
Ronald Cron351f0ee2020-06-10 12:12:18 +020085 return( mbedtls_test_rnd_std_rand( NULL, output + use_len,
86 len - use_len ) );
Ronald Cron2058d562020-06-09 17:11:47 +020087
88 return( 0 );
89}
90
Ronald Cron351f0ee2020-06-10 12:12:18 +020091int mbedtls_test_rnd_pseudo_rand( void *rng_state,
92 unsigned char *output,
93 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020094{
Ronald Cron351f0ee2020-06-10 12:12:18 +020095 mbedtls_test_rnd_pseudo_info *info =
96 (mbedtls_test_rnd_pseudo_info *) rng_state;
Ronald Cron2058d562020-06-09 17:11:47 +020097 uint32_t i, *k, sum, delta=0x9E3779B9;
98 unsigned char result[4], *out = output;
99
100 if( rng_state == NULL )
Ronald Cron351f0ee2020-06-10 12:12:18 +0200101 return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
Ronald Cron2058d562020-06-09 17:11:47 +0200102
103 k = info->key;
104
105 while( len > 0 )
106 {
107 size_t use_len = ( len > 4 ) ? 4 : len;
108 sum = 0;
109
110 for( i = 0; i < 32; i++ )
111 {
112 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
113 + info->v1 ) ^ ( sum + k[sum & 3] );
114 sum += delta;
115 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
116 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
117 }
118
119 PUT_UINT32_BE( info->v0, result, 0 );
120 memcpy( out, result, use_len );
121 len -= use_len;
122 out += 4;
123 }
124
125 return( 0 );
126}