1 /**
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 /*
9  *  Copyright The Mbed TLS Contributors
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.
23  */
24 
25 /*
26  * for arc4random_buf() from <stdlib.h>
27  */
28 #if defined(__NetBSD__)
29 #define _NETBSD_SOURCE 1
30 #elif defined(__OpenBSD__)
31 #define _BSD_SOURCE 1
32 #endif
33 
34 #include <test/macros.h>
35 #include <test/random.h>
36 #include <string.h>
37 
38 #include <mbedtls/entropy.h>
39 
mbedtls_test_rnd_std_rand(void * rng_state,unsigned char * output,size_t len)40 int mbedtls_test_rnd_std_rand( void *rng_state,
41                                unsigned char *output,
42                                size_t len )
43 {
44 #if !defined(__OpenBSD__) && !defined(__NetBSD__)
45     size_t i;
46 
47     if( rng_state != NULL )
48         rng_state  = NULL;
49 
50     for( i = 0; i < len; ++i )
51         output[i] = rand();
52 #else
53     if( rng_state != NULL )
54         rng_state = NULL;
55 
56     arc4random_buf( output, len );
57 #endif /* !OpenBSD && !NetBSD */
58 
59     return( 0 );
60 }
61 
mbedtls_test_rnd_zero_rand(void * rng_state,unsigned char * output,size_t len)62 int mbedtls_test_rnd_zero_rand( void *rng_state,
63                                 unsigned char *output,
64                                 size_t len )
65 {
66     if( rng_state != NULL )
67         rng_state  = NULL;
68 
69     memset( output, 0, len );
70 
71     return( 0 );
72 }
73 
mbedtls_test_rnd_buffer_rand(void * rng_state,unsigned char * output,size_t len)74 int mbedtls_test_rnd_buffer_rand( void *rng_state,
75                                   unsigned char *output,
76                                   size_t len )
77 {
78     mbedtls_test_rnd_buf_info *info = (mbedtls_test_rnd_buf_info *) rng_state;
79     size_t use_len;
80 
81     if( rng_state == NULL )
82         return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
83 
84     use_len = len;
85     if( len > info->length )
86         use_len = info->length;
87 
88     if( use_len )
89     {
90         memcpy( output, info->buf, use_len );
91         info->buf += use_len;
92         info->length -= use_len;
93     }
94 
95     if( len - use_len > 0 )
96     {
97         if( info->fallback_f_rng != NULL )
98         {
99             return( info->fallback_f_rng( info->fallback_p_rng,
100                                           output + use_len,
101                                           len - use_len ) );
102         }
103         else
104             return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
105     }
106 
107     return( 0 );
108 }
109 
mbedtls_test_rnd_pseudo_rand(void * rng_state,unsigned char * output,size_t len)110 int mbedtls_test_rnd_pseudo_rand( void *rng_state,
111                                   unsigned char *output,
112                                   size_t len )
113 {
114     mbedtls_test_rnd_pseudo_info *info =
115         (mbedtls_test_rnd_pseudo_info *) rng_state;
116     uint32_t i, *k, sum, delta=0x9E3779B9;
117     unsigned char result[4], *out = output;
118 
119     if( rng_state == NULL )
120         return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
121 
122     k = info->key;
123 
124     while( len > 0 )
125     {
126         size_t use_len = ( len > 4 ) ? 4 : len;
127         sum = 0;
128 
129         for( i = 0; i < 32; i++ )
130         {
131             info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
132                             + info->v1 ) ^ ( sum + k[sum & 3] );
133             sum += delta;
134             info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
135                             + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
136         }
137 
138         PUT_UINT32_BE( info->v0, result, 0 );
139         memcpy( out, result, use_len );
140         len -= use_len;
141         out += 4;
142     }
143 
144     return( 0 );
145 }
146