blob: 3345f78be3d7b4738977eb39ec772744f5998ac5 [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
Ronald Cron2058d562020-06-09 17:11:47 +020027#include <test/macros.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +020028#include <test/random.h>
Ronald Cron2058d562020-06-09 17:11:47 +020029#include <string.h>
30
Ronald Cron351f0ee2020-06-10 12:12:18 +020031int mbedtls_test_rnd_std_rand( void *rng_state,
32 unsigned char *output,
33 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020034{
35#if !defined(__OpenBSD__)
36 size_t i;
37
38 if( rng_state != NULL )
39 rng_state = NULL;
40
41 for( i = 0; i < len; ++i )
42 output[i] = rand();
43#else
44 if( rng_state != NULL )
45 rng_state = NULL;
46
47 arc4random_buf( output, len );
48#endif /* !OpenBSD */
49
50 return( 0 );
51}
52
Ronald Cron351f0ee2020-06-10 12:12:18 +020053int mbedtls_test_rnd_zero_rand( void *rng_state,
54 unsigned char *output,
55 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020056{
57 if( rng_state != NULL )
58 rng_state = NULL;
59
60 memset( output, 0, len );
61
62 return( 0 );
63}
64
Ronald Cron351f0ee2020-06-10 12:12:18 +020065int mbedtls_test_rnd_buffer_rand( void *rng_state,
66 unsigned char *output,
67 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020068{
Ronald Cron351f0ee2020-06-10 12:12:18 +020069 mbedtls_test_rnd_buf_info *info = (mbedtls_test_rnd_buf_info *) rng_state;
Ronald Cron2058d562020-06-09 17:11:47 +020070 size_t use_len;
71
72 if( rng_state == NULL )
Ronald Cron351f0ee2020-06-10 12:12:18 +020073 return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
Ronald Cron2058d562020-06-09 17:11:47 +020074
75 use_len = len;
76 if( len > info->length )
77 use_len = info->length;
78
79 if( use_len )
80 {
81 memcpy( output, info->buf, use_len );
82 info->buf += use_len;
83 info->length -= use_len;
84 }
85
86 if( len - use_len > 0 )
Ronald Cron351f0ee2020-06-10 12:12:18 +020087 return( mbedtls_test_rnd_std_rand( NULL, output + use_len,
88 len - use_len ) );
Ronald Cron2058d562020-06-09 17:11:47 +020089
90 return( 0 );
91}
92
Ronald Cron351f0ee2020-06-10 12:12:18 +020093int mbedtls_test_rnd_pseudo_rand( void *rng_state,
94 unsigned char *output,
95 size_t len )
Ronald Cron2058d562020-06-09 17:11:47 +020096{
Ronald Cron351f0ee2020-06-10 12:12:18 +020097 mbedtls_test_rnd_pseudo_info *info =
98 (mbedtls_test_rnd_pseudo_info *) rng_state;
Ronald Cron2058d562020-06-09 17:11:47 +020099 uint32_t i, *k, sum, delta=0x9E3779B9;
100 unsigned char result[4], *out = output;
101
102 if( rng_state == NULL )
Ronald Cron351f0ee2020-06-10 12:12:18 +0200103 return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
Ronald Cron2058d562020-06-09 17:11:47 +0200104
105 k = info->key;
106
107 while( len > 0 )
108 {
109 size_t use_len = ( len > 4 ) ? 4 : len;
110 sum = 0;
111
112 for( i = 0; i < 32; i++ )
113 {
114 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
115 + info->v1 ) ^ ( sum + k[sum & 3] );
116 sum += delta;
117 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
118 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
119 }
120
121 PUT_UINT32_BE( info->v0, result, 0 );
122 memcpy( out, result, use_len );
123 len -= use_len;
124 out += 4;
125 }
126
127 return( 0 );
128}