blob: 25fa4cf336ca6529ac7bc864fbb46e6bd94e3c3d [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
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
Ronald Cron2058d562020-06-09 17:11:47 +020026#include <test/macros.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +020027#include <test/random.h>
Ronald Cron2058d562020-06-09 17:11:47 +020028#include <string.h>
29
Ronald Cron351f0ee2020-06-10 12:12:18 +020030int mbedtls_test_rnd_std_rand( void *rng_state,
31 unsigned char *output,
32 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020033{
34#if !defined(__OpenBSD__)
35 size_t i;
36
37 if( rng_state != NULL )
38 rng_state = NULL;
39
40 for( i = 0; i < len; ++i )
41 output[i] = rand();
42#else
43 if( rng_state != NULL )
44 rng_state = NULL;
45
46 arc4random_buf( output, len );
47#endif /* !OpenBSD */
48
49 return( 0 );
50}
51
Ronald Cron351f0ee2020-06-10 12:12:18 +020052int mbedtls_test_rnd_zero_rand( void *rng_state,
53 unsigned char *output,
54 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020055{
56 if( rng_state != NULL )
57 rng_state = NULL;
58
59 memset( output, 0, len );
60
61 return( 0 );
62}
63
Ronald Cron351f0ee2020-06-10 12:12:18 +020064int mbedtls_test_rnd_buffer_rand( void *rng_state,
65 unsigned char *output,
66 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020067{
Ronald Cron351f0ee2020-06-10 12:12:18 +020068 mbedtls_test_rnd_buf_info *info = (mbedtls_test_rnd_buf_info *) rng_state;
Ronald Cron2058d562020-06-09 17:11:47 +020069 size_t use_len;
70
71 if( rng_state == NULL )
Ronald Cron351f0ee2020-06-10 12:12:18 +020072 return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
Ronald Cron2058d562020-06-09 17:11:47 +020073
74 use_len = len;
75 if( len > info->length )
76 use_len = info->length;
77
78 if( use_len )
79 {
80 memcpy( output, info->buf, use_len );
81 info->buf += use_len;
82 info->length -= use_len;
83 }
84
85 if( len - use_len > 0 )
Ronald Cron351f0ee2020-06-10 12:12:18 +020086 return( mbedtls_test_rnd_std_rand( NULL, output + use_len,
87 len - use_len ) );
Ronald Cron2058d562020-06-09 17:11:47 +020088
89 return( 0 );
90}
91
Ronald Cron351f0ee2020-06-10 12:12:18 +020092int mbedtls_test_rnd_pseudo_rand( void *rng_state,
93 unsigned char *output,
94 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020095{
Ronald Cron351f0ee2020-06-10 12:12:18 +020096 mbedtls_test_rnd_pseudo_info *info =
97 (mbedtls_test_rnd_pseudo_info *) rng_state;
Ronald Cron2058d562020-06-09 17:11:47 +020098 uint32_t i, *k, sum, delta=0x9E3779B9;
99 unsigned char result[4], *out = output;
100
101 if( rng_state == NULL )
Ronald Cron351f0ee2020-06-10 12:12:18 +0200102 return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
Ronald Cron2058d562020-06-09 17:11:47 +0200103
104 k = info->key;
105
106 while( len > 0 )
107 {
108 size_t use_len = ( len > 4 ) ? 4 : len;
109 sum = 0;
110
111 for( i = 0; i < 32; i++ )
112 {
113 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
114 + info->v1 ) ^ ( sum + k[sum & 3] );
115 sum += delta;
116 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
117 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
118 }
119
120 PUT_UINT32_BE( info->v0, result, 0 );
121 memcpy( out, result, use_len );
122 len -= use_len;
123 out += 4;
124 }
125
126 return( 0 );
127}