2020-06-09 16:57:42 +02:00
|
|
|
/**
|
|
|
|
* \file random.c
|
|
|
|
*
|
|
|
|
* \brief This file contains the helper functions to generate random numbers
|
|
|
|
* for the purpose of testing.
|
|
|
|
*/
|
|
|
|
|
2020-06-15 11:59:37 +02:00
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2020-06-09 16:57:42 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-08-17 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* for arc4random_buf() from <stdlib.h>
|
|
|
|
*/
|
|
|
|
#if defined(__NetBSD__)
|
|
|
|
#define _NETBSD_SOURCE 1
|
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
#define _BSD_SOURCE 1
|
|
|
|
#endif
|
|
|
|
|
2020-06-09 17:11:47 +02:00
|
|
|
#include <test/macros.h>
|
2020-06-09 16:57:42 +02:00
|
|
|
#include <test/random.h>
|
2020-06-09 17:11:47 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-03-24 00:48:57 +01:00
|
|
|
#include <mbedtls/entropy.h>
|
2023-07-06 14:19:49 +02:00
|
|
|
#include <alignment.h>
|
2021-03-24 00:48:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_test_rnd_std_rand(void *rng_state,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t len)
|
2020-06-09 17:11:47 +02:00
|
|
|
{
|
2020-08-03 17:56:50 +02:00
|
|
|
#if !defined(__OpenBSD__) && !defined(__NetBSD__)
|
2020-06-09 17:11:47 +02:00
|
|
|
size_t i;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rng_state != NULL) {
|
2020-06-09 17:11:47 +02:00
|
|
|
rng_state = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-06-09 17:11:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < len; ++i) {
|
2020-06-09 17:11:47 +02:00
|
|
|
output[i] = rand();
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-06-09 17:11:47 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rng_state != NULL) {
|
2020-06-09 17:11:47 +02:00
|
|
|
rng_state = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-06-09 17:11:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
arc4random_buf(output, len);
|
2020-08-03 17:56:50 +02:00
|
|
|
#endif /* !OpenBSD && !NetBSD */
|
2020-06-09 17:11:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-06-09 17:11:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_test_rnd_zero_rand(void *rng_state,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t len)
|
2020-06-09 17:11:47 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rng_state != NULL) {
|
2020-06-09 17:11:47 +02:00
|
|
|
rng_state = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-06-09 17:11:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, 0, len);
|
2020-06-09 17:11:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-06-09 17:11:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_test_rnd_buffer_rand(void *rng_state,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t len)
|
2020-06-09 17:11:47 +02:00
|
|
|
{
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_buf_info *info = (mbedtls_test_rnd_buf_info *) rng_state;
|
2020-06-09 17:11:47 +02:00
|
|
|
size_t use_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rng_state == NULL) {
|
|
|
|
return mbedtls_test_rnd_std_rand(NULL, output, len);
|
|
|
|
}
|
2020-06-09 17:11:47 +02:00
|
|
|
|
|
|
|
use_len = len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len > info->length) {
|
2020-06-09 17:11:47 +02:00
|
|
|
use_len = info->length;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-06-09 17:11:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (use_len) {
|
|
|
|
memcpy(output, info->buf, use_len);
|
2020-06-09 17:11:47 +02:00
|
|
|
info->buf += use_len;
|
|
|
|
info->length -= use_len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len - use_len > 0) {
|
|
|
|
if (info->fallback_f_rng != NULL) {
|
|
|
|
return info->fallback_f_rng(info->fallback_p_rng,
|
|
|
|
output + use_len,
|
|
|
|
len - use_len);
|
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
|
2021-03-24 00:48:57 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-09 17:11:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-06-09 17:11:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_test_rnd_pseudo_rand(void *rng_state,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t len)
|
2020-06-09 17:11:47 +02:00
|
|
|
{
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info *info =
|
|
|
|
(mbedtls_test_rnd_pseudo_info *) rng_state;
|
2023-01-11 14:50:10 +01:00
|
|
|
uint32_t i, *k, sum, delta = 0x9E3779B9;
|
2020-06-09 17:11:47 +02:00
|
|
|
unsigned char result[4], *out = output;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rng_state == NULL) {
|
|
|
|
return mbedtls_test_rnd_std_rand(NULL, output, len);
|
|
|
|
}
|
2020-06-09 17:11:47 +02:00
|
|
|
|
|
|
|
k = info->key;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (len > 0) {
|
|
|
|
size_t use_len = (len > 4) ? 4 : len;
|
2020-06-09 17:11:47 +02:00
|
|
|
sum = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5))
|
|
|
|
+ info->v1) ^ (sum + k[sum & 3]);
|
2020-06-09 17:11:47 +02:00
|
|
|
sum += delta;
|
2023-01-11 14:50:10 +01:00
|
|
|
info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5))
|
|
|
|
+ info->v0) ^ (sum + k[(sum>>11) & 3]);
|
2020-06-09 17:11:47 +02:00
|
|
|
}
|
|
|
|
|
2023-03-10 17:44:08 +00:00
|
|
|
MBEDTLS_PUT_UINT32_BE(info->v0, result, 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(out, result, use_len);
|
2020-06-09 17:11:47 +02:00
|
|
|
len -= use_len;
|
|
|
|
out += 4;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-06-09 17:11:47 +02:00
|
|
|
}
|