Coverage Report

Created: 2026-01-13 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mbedtls/framework/tests/src/random.c
Line
Count
Source
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 OR GPL-2.0-or-later
11
 */
12
13
/*
14
 * for arc4random_buf() from <stdlib.h>
15
 */
16
#if defined(__NetBSD__)
17
#define _NETBSD_SOURCE 1
18
#elif defined(__OpenBSD__)
19
#define _BSD_SOURCE 1
20
#endif
21
22
#include <test/macros.h>
23
#include <test/random.h>
24
#include <string.h>
25
26
#if !defined(MBEDTLS_VERSION_MAJOR) || MBEDTLS_VERSION_MAJOR >= 4
27
#include <mbedtls/private/entropy.h>
28
#else
29
#include <mbedtls/entropy.h>
30
#endif
31
#include <alignment.h>
32
33
int mbedtls_test_rnd_std_rand(void *rng_state,
34
                              unsigned char *output,
35
                              size_t len)
36
0
{
37
0
#if !defined(__OpenBSD__) && !defined(__NetBSD__)
38
0
    size_t i;
39
40
0
    if (rng_state != NULL) {
41
0
        rng_state  = NULL;
42
0
    }
43
44
0
    for (i = 0; i < len; ++i) {
45
0
        output[i] = rand();
46
0
    }
47
#else
48
    if (rng_state != NULL) {
49
        rng_state = NULL;
50
    }
51
52
    arc4random_buf(output, len);
53
#endif /* !OpenBSD && !NetBSD */
54
55
0
    return 0;
56
0
}
57
58
int mbedtls_test_rnd_zero_rand(void *rng_state,
59
                               unsigned char *output,
60
                               size_t len)
61
0
{
62
0
    if (rng_state != NULL) {
63
0
        rng_state  = NULL;
64
0
    }
65
66
0
    memset(output, 0, len);
67
68
0
    return 0;
69
0
}
70
71
int mbedtls_test_rnd_buffer_rand(void *rng_state,
72
                                 unsigned char *output,
73
                                 size_t len)
74
0
{
75
0
    mbedtls_test_rnd_buf_info *info = (mbedtls_test_rnd_buf_info *) rng_state;
76
0
    size_t use_len;
77
78
0
    if (rng_state == NULL) {
79
0
        return mbedtls_test_rnd_std_rand(NULL, output, len);
80
0
    }
81
82
0
    use_len = len;
83
0
    if (len > info->length) {
84
0
        use_len = info->length;
85
0
    }
86
87
0
    if (use_len) {
88
0
        memcpy(output, info->buf, use_len);
89
0
        info->buf += use_len;
90
0
        info->length -= use_len;
91
0
    }
92
93
0
    if (len - use_len > 0) {
94
0
        if (info->fallback_f_rng != NULL) {
95
0
            return info->fallback_f_rng(info->fallback_p_rng,
96
0
                                        output + use_len,
97
0
                                        len - use_len);
98
0
        } else {
99
0
            return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
100
0
        }
101
0
    }
102
103
0
    return 0;
104
0
}
105
106
int mbedtls_test_rnd_pseudo_rand(void *rng_state,
107
                                 unsigned char *output,
108
                                 size_t len)
109
0
{
110
0
    mbedtls_test_rnd_pseudo_info *info =
111
0
        (mbedtls_test_rnd_pseudo_info *) rng_state;
112
0
    uint32_t i, *k, sum, delta = 0x9E3779B9;
113
0
    unsigned char result[4], *out = output;
114
115
0
    if (rng_state == NULL) {
116
0
        return mbedtls_test_rnd_std_rand(NULL, output, len);
117
0
    }
118
119
0
    k = info->key;
120
121
0
    while (len > 0) {
122
0
        size_t use_len = (len > 4) ? 4 : len;
123
0
        sum = 0;
124
125
0
        for (i = 0; i < 32; i++) {
126
0
            info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5))
127
0
                         + info->v1) ^ (sum + k[sum & 3]);
128
0
            sum += delta;
129
0
            info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5))
130
0
                         + info->v0) ^ (sum + k[(sum>>11) & 3]);
131
0
        }
132
133
0
        MBEDTLS_PUT_UINT32_BE(info->v0, result, 0);
134
0
        memcpy(out, result, use_len);
135
0
        len -= use_len;
136
0
        out += 4;
137
0
    }
138
139
0
    return 0;
140
0
}