blob: bb0df7a71124ad745272562d8547f13455890f1c [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
30int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
31{
32#if !defined(__OpenBSD__)
33 size_t i;
34
35 if( rng_state != NULL )
36 rng_state = NULL;
37
38 for( i = 0; i < len; ++i )
39 output[i] = rand();
40#else
41 if( rng_state != NULL )
42 rng_state = NULL;
43
44 arc4random_buf( output, len );
45#endif /* !OpenBSD */
46
47 return( 0 );
48}
49
50int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
51{
52 if( rng_state != NULL )
53 rng_state = NULL;
54
55 memset( output, 0, len );
56
57 return( 0 );
58}
59
60int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
61{
62 rnd_buf_info *info = (rnd_buf_info *) rng_state;
63 size_t use_len;
64
65 if( rng_state == NULL )
66 return( rnd_std_rand( NULL, output, len ) );
67
68 use_len = len;
69 if( len > info->length )
70 use_len = info->length;
71
72 if( use_len )
73 {
74 memcpy( output, info->buf, use_len );
75 info->buf += use_len;
76 info->length -= use_len;
77 }
78
79 if( len - use_len > 0 )
80 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
81
82 return( 0 );
83}
84
85int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
86{
87 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
88 uint32_t i, *k, sum, delta=0x9E3779B9;
89 unsigned char result[4], *out = output;
90
91 if( rng_state == NULL )
92 return( rnd_std_rand( NULL, output, len ) );
93
94 k = info->key;
95
96 while( len > 0 )
97 {
98 size_t use_len = ( len > 4 ) ? 4 : len;
99 sum = 0;
100
101 for( i = 0; i < 32; i++ )
102 {
103 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
104 + info->v1 ) ^ ( sum + k[sum & 3] );
105 sum += delta;
106 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
107 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
108 }
109
110 PUT_UINT32_BE( info->v0, result, 0 );
111 memcpy( out, result, use_len );
112 len -= use_len;
113 out += 4;
114 }
115
116 return( 0 );
117}