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 
mbedtls_test_rnd_std_rand(void * rng_state,unsigned char * output,size_t len)38 int mbedtls_test_rnd_std_rand( void *rng_state,
39                                unsigned char *output,
40                                size_t len )
41 {
42 #if !defined(__OpenBSD__) && !defined(__NetBSD__)
43     size_t i;
44 
45     if( rng_state != NULL )
46         rng_state  = NULL;
47 
48     for( i = 0; i < len; ++i )
49         output[i] = rand();
50 #else
51     if( rng_state != NULL )
52         rng_state = NULL;
53 
54     arc4random_buf( output, len );
55 #endif /* !OpenBSD && !NetBSD */
56 
57     return( 0 );
58 }
59 
mbedtls_test_rnd_zero_rand(void * rng_state,unsigned char * output,size_t len)60 int mbedtls_test_rnd_zero_rand( void *rng_state,
61                                 unsigned char *output,
62                                 size_t len )
63 {
64     if( rng_state != NULL )
65         rng_state  = NULL;
66 
67     memset( output, 0, len );
68 
69     return( 0 );
70 }
71 
mbedtls_test_rnd_buffer_rand(void * rng_state,unsigned char * output,size_t len)72 int mbedtls_test_rnd_buffer_rand( void *rng_state,
73                                   unsigned char *output,
74                                   size_t len )
75 {
76     mbedtls_test_rnd_buf_info *info = (mbedtls_test_rnd_buf_info *) rng_state;
77     size_t use_len;
78 
79     if( rng_state == NULL )
80         return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
81 
82     use_len = len;
83     if( len > info->length )
84         use_len = info->length;
85 
86     if( use_len )
87     {
88         memcpy( output, info->buf, use_len );
89         info->buf += use_len;
90         info->length -= use_len;
91     }
92 
93     if( len - use_len > 0 )
94         return( mbedtls_test_rnd_std_rand( NULL, output + use_len,
95                                            len - use_len ) );
96 
97     return( 0 );
98 }
99 
mbedtls_test_rnd_pseudo_rand(void * rng_state,unsigned char * output,size_t len)100 int mbedtls_test_rnd_pseudo_rand( void *rng_state,
101                                   unsigned char *output,
102                                   size_t len )
103 {
104     mbedtls_test_rnd_pseudo_info *info =
105         (mbedtls_test_rnd_pseudo_info *) rng_state;
106     uint32_t i, *k, sum, delta=0x9E3779B9;
107     unsigned char result[4], *out = output;
108 
109     if( rng_state == NULL )
110         return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
111 
112     k = info->key;
113 
114     while( len > 0 )
115     {
116         size_t use_len = ( len > 4 ) ? 4 : len;
117         sum = 0;
118 
119         for( i = 0; i < 32; i++ )
120         {
121             info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
122                             + info->v1 ) ^ ( sum + k[sum & 3] );
123             sum += delta;
124             info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
125                             + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
126         }
127 
128         PUT_UINT32_BE( info->v0, result, 0 );
129         memcpy( out, result, use_len );
130         len -= use_len;
131         out += 4;
132     }
133 
134     return( 0 );
135 }
136