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