blob: 5ca333a675adee4662ed38cfdadc856ce2bc75bb [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/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02009 * Copyright The Mbed TLS Contributors
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.
Ronald Cronb7eb67f2020-06-09 16:57:42 +020023 */
24
gufe44c54888e2020-08-17 15:04:06 +020025/*
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
Ronald Cron2058d562020-06-09 17:11:47 +020034#include <test/macros.h>
Ronald Cronb7eb67f2020-06-09 16:57:42 +020035#include <test/random.h>
Ronald Cron2058d562020-06-09 17:11:47 +020036#include <string.h>
37
Gilles Peskineecacc3c2021-03-24 00:48:57 +010038#include <mbedtls/entropy.h>
Dave Rodgmand51b1c52023-03-10 17:44:08 +000039#include "../../library/alignment.h"
Gilles Peskineecacc3c2021-03-24 00:48:57 +010040
Gilles Peskine449bd832023-01-11 14:50:10 +010041int mbedtls_test_rnd_std_rand(void *rng_state,
42 unsigned char *output,
43 size_t len)
Ronald Cron2058d562020-06-09 17:11:47 +020044{
gufe44c2620da2020-08-03 17:56:50 +020045#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Ronald Cron2058d562020-06-09 17:11:47 +020046 size_t i;
47
Gilles Peskine449bd832023-01-11 14:50:10 +010048 if (rng_state != NULL) {
Ronald Cron2058d562020-06-09 17:11:47 +020049 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010050 }
Ronald Cron2058d562020-06-09 17:11:47 +020051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 for (i = 0; i < len; ++i) {
Ronald Cron2058d562020-06-09 17:11:47 +020053 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +010054 }
Ronald Cron2058d562020-06-09 17:11:47 +020055#else
Gilles Peskine449bd832023-01-11 14:50:10 +010056 if (rng_state != NULL) {
Ronald Cron2058d562020-06-09 17:11:47 +020057 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010058 }
Ronald Cron2058d562020-06-09 17:11:47 +020059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 arc4random_buf(output, len);
gufe44c2620da2020-08-03 17:56:50 +020061#endif /* !OpenBSD && !NetBSD */
Ronald Cron2058d562020-06-09 17:11:47 +020062
Gilles Peskine449bd832023-01-11 14:50:10 +010063 return 0;
Ronald Cron2058d562020-06-09 17:11:47 +020064}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066int mbedtls_test_rnd_zero_rand(void *rng_state,
67 unsigned char *output,
68 size_t len)
Ronald Cron2058d562020-06-09 17:11:47 +020069{
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if (rng_state != NULL) {
Ronald Cron2058d562020-06-09 17:11:47 +020071 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010072 }
Ronald Cron2058d562020-06-09 17:11:47 +020073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 memset(output, 0, len);
Ronald Cron2058d562020-06-09 17:11:47 +020075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 return 0;
Ronald Cron2058d562020-06-09 17:11:47 +020077}
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079int mbedtls_test_rnd_buffer_rand(void *rng_state,
80 unsigned char *output,
81 size_t len)
Ronald Cron2058d562020-06-09 17:11:47 +020082{
Ronald Cron351f0ee2020-06-10 12:12:18 +020083 mbedtls_test_rnd_buf_info *info = (mbedtls_test_rnd_buf_info *) rng_state;
Ronald Cron2058d562020-06-09 17:11:47 +020084 size_t use_len;
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (rng_state == NULL) {
87 return mbedtls_test_rnd_std_rand(NULL, output, len);
88 }
Ronald Cron2058d562020-06-09 17:11:47 +020089
90 use_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (len > info->length) {
Ronald Cron2058d562020-06-09 17:11:47 +020092 use_len = info->length;
Gilles Peskine449bd832023-01-11 14:50:10 +010093 }
Ronald Cron2058d562020-06-09 17:11:47 +020094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if (use_len) {
96 memcpy(output, info->buf, use_len);
Ronald Cron2058d562020-06-09 17:11:47 +020097 info->buf += use_len;
98 info->length -= use_len;
99 }
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (len - use_len > 0) {
102 if (info->fallback_f_rng != NULL) {
103 return info->fallback_f_rng(info->fallback_p_rng,
104 output + use_len,
105 len - use_len);
106 } else {
107 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Gilles Peskineecacc3c2021-03-24 00:48:57 +0100108 }
Gilles Peskineecacc3c2021-03-24 00:48:57 +0100109 }
Ronald Cron2058d562020-06-09 17:11:47 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 return 0;
Ronald Cron2058d562020-06-09 17:11:47 +0200112}
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114int mbedtls_test_rnd_pseudo_rand(void *rng_state,
115 unsigned char *output,
116 size_t len)
Ronald Cron2058d562020-06-09 17:11:47 +0200117{
Ronald Cron351f0ee2020-06-10 12:12:18 +0200118 mbedtls_test_rnd_pseudo_info *info =
119 (mbedtls_test_rnd_pseudo_info *) rng_state;
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 uint32_t i, *k, sum, delta = 0x9E3779B9;
Ronald Cron2058d562020-06-09 17:11:47 +0200121 unsigned char result[4], *out = output;
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (rng_state == NULL) {
124 return mbedtls_test_rnd_std_rand(NULL, output, len);
125 }
Ronald Cron2058d562020-06-09 17:11:47 +0200126
127 k = info->key;
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 while (len > 0) {
130 size_t use_len = (len > 4) ? 4 : len;
Ronald Cron2058d562020-06-09 17:11:47 +0200131 sum = 0;
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 for (i = 0; i < 32; i++) {
134 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5))
135 + info->v1) ^ (sum + k[sum & 3]);
Ronald Cron2058d562020-06-09 17:11:47 +0200136 sum += delta;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5))
138 + info->v0) ^ (sum + k[(sum>>11) & 3]);
Ronald Cron2058d562020-06-09 17:11:47 +0200139 }
140
Dave Rodgmand51b1c52023-03-10 17:44:08 +0000141 MBEDTLS_PUT_UINT32_BE(info->v0, result, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 memcpy(out, result, use_len);
Ronald Cron2058d562020-06-09 17:11:47 +0200143 len -= use_len;
144 out += 4;
145 }
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return 0;
Ronald Cron2058d562020-06-09 17:11:47 +0200148}