blob: 78419c4d4dca481e0b3fdee617516b0367cbafff [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/*
9 * Copyright (C) 2020, ARM Limited, All Rights Reserved
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.
23 *
24 * This file is part of mbed TLS (https://tls.mbed.org)
25 */
26
gufe44c54888e2020-08-17 15:04:06 +020027/*
28 * for arc4random_buf() from <stdlib.h>
29 */
30#if defined(__NetBSD__)
31#define _NETBSD_SOURCE 1
32#elif defined(__OpenBSD__)
33#define _BSD_SOURCE 1
34#endif
35
Ronald Cron2058d562020-06-09 17:11:47 +020036#include <test/macros.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +020037#include <test/random.h>
Ronald Cron2058d562020-06-09 17:11:47 +020038#include <string.h>
39
Ronald Cron351f0ee2020-06-10 12:12:18 +020040int mbedtls_test_rnd_std_rand( void *rng_state,
41 unsigned char *output,
42 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020043{
44#if !defined(__OpenBSD__)
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 */
58
59 return( 0 );
60}
61
Ronald Cron351f0ee2020-06-10 12:12:18 +020062int mbedtls_test_rnd_zero_rand( void *rng_state,
63 unsigned char *output,
64 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020065{
66 if( rng_state != NULL )
67 rng_state = NULL;
68
69 memset( output, 0, len );
70
71 return( 0 );
72}
73
Ronald Cron351f0ee2020-06-10 12:12:18 +020074int mbedtls_test_rnd_buffer_rand( void *rng_state,
75 unsigned char *output,
76 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020077{
Ronald Cron351f0ee2020-06-10 12:12:18 +020078 mbedtls_test_rnd_buf_info *info = (mbedtls_test_rnd_buf_info *) rng_state;
Ronald Cron2058d562020-06-09 17:11:47 +020079 size_t use_len;
80
81 if( rng_state == NULL )
Ronald Cron351f0ee2020-06-10 12:12:18 +020082 return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
Ronald Cron2058d562020-06-09 17:11:47 +020083
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 )
Ronald Cron351f0ee2020-06-10 12:12:18 +020096 return( mbedtls_test_rnd_std_rand( NULL, output + use_len,
97 len - use_len ) );
Ronald Cron2058d562020-06-09 17:11:47 +020098
99 return( 0 );
100}
101
Ronald Cron351f0ee2020-06-10 12:12:18 +0200102int mbedtls_test_rnd_pseudo_rand( void *rng_state,
103 unsigned char *output,
104 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +0200105{
Ronald Cron351f0ee2020-06-10 12:12:18 +0200106 mbedtls_test_rnd_pseudo_info *info =
107 (mbedtls_test_rnd_pseudo_info *) rng_state;
Ronald Cron2058d562020-06-09 17:11:47 +0200108 uint32_t i, *k, sum, delta=0x9E3779B9;
109 unsigned char result[4], *out = output;
110
111 if( rng_state == NULL )
Ronald Cron351f0ee2020-06-10 12:12:18 +0200112 return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
Ronald Cron2058d562020-06-09 17:11:47 +0200113
114 k = info->key;
115
116 while( len > 0 )
117 {
118 size_t use_len = ( len > 4 ) ? 4 : len;
119 sum = 0;
120
121 for( i = 0; i < 32; i++ )
122 {
123 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
124 + info->v1 ) ^ ( sum + k[sum & 3] );
125 sum += delta;
126 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
127 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
128 }
129
130 PUT_UINT32_BE( info->v0, result, 0 );
131 memcpy( out, result, use_len );
132 len -= use_len;
133 out += 4;
134 }
135
136 return( 0 );
137}