Skip to content

Commit 90fc523

Browse files
committed
Improve usage of rand() for certain LCG implementations
Signed-off-by: Jan Bartels <[email protected]>
1 parent c765c83 commit 90fc523

File tree

5 files changed

+50
-28
lines changed

5 files changed

+50
-28
lines changed

library/rsa.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,14 +2890,18 @@ void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
28902890
static int myrand(void *rng_state, unsigned char *output, size_t len)
28912891
{
28922892
#if !defined(__OpenBSD__) && !defined(__NetBSD__)
2893-
size_t i;
2894-
28952893
if (rng_state != NULL) {
28962894
rng_state = NULL;
28972895
}
28982896

2899-
for (i = 0; i < len; ++i) {
2900-
output[i] = rand();
2897+
while (len > 0) {
2898+
#if (RAND_MAX >= 0x00FFFFFF)
2899+
*output = (unsigned char) (rand() >> 16);
2900+
#else
2901+
*output = (unsigned char) rand() ; /* e. g. Visual C */
2902+
#endif
2903+
output += 1;
2904+
len -= 1;
29012905
}
29022906
#else
29032907
if (rng_state != NULL) {

programs/fuzz/common.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ int fuzz_recv(void *ctx, unsigned char *buf, size_t len)
5959
int dummy_random(void *p_rng, unsigned char *output, size_t output_len)
6060
{
6161
int ret;
62-
size_t i;
6362

6463
#if defined(MBEDTLS_CTR_DRBG_C)
6564
//mbedtls_ctr_drbg_random requires a valid mbedtls_ctr_drbg_context in p_rng
@@ -74,24 +73,35 @@ int dummy_random(void *p_rng, unsigned char *output, size_t output_len)
7473
(void) p_rng;
7574
ret = 0;
7675
#endif
77-
for (i = 0; i < output_len; i++) {
78-
//replace result with pseudo random
79-
output[i] = (unsigned char) rand();
76+
//replace result with pseudo random
77+
while (output_len > 0) {
78+
#if (RAND_MAX >= 0x00FFFFFF)
79+
*output = (unsigned char) (rand() >> 16);
80+
#else
81+
*output = (unsigned char) rand() ; /* e. g. Visual C */
82+
#endif
83+
output += 1;
84+
output_len -= 1;
8085
}
8186
return ret;
8287
}
8388

8489
int dummy_entropy(void *data, unsigned char *output, size_t len)
8590
{
86-
size_t i;
8791
(void) data;
8892

8993
//use mbedtls_entropy_func to find bugs in it
9094
//test performance impact of entropy
9195
//ret = mbedtls_entropy_func(data, output, len);
92-
for (i = 0; i < len; i++) {
93-
//replace result with pseudo random
94-
output[i] = (unsigned char) rand();
96+
//replace result with pseudo random
97+
while (len > 0) {
98+
#if (RAND_MAX >= 0x00FFFFFF)
99+
*output = (unsigned char) (rand() >> 16);
100+
#else
101+
*output = (unsigned char) rand() ; /* e. g. Visual C */
102+
#endif
103+
output += 1;
104+
len -= 1;
95105
}
96106
return 0;
97107
}

programs/ssl/ssl_test_lib.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,19 @@ mbedtls_time_t dummy_constant_time(mbedtls_time_t *time)
4848
#if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
4949
static int dummy_entropy(void *data, unsigned char *output, size_t len)
5050
{
51-
size_t i;
5251
int ret;
5352
(void) data;
5453

5554
ret = mbedtls_entropy_func(data, output, len);
56-
for (i = 0; i < len; i++) {
57-
//replace result with pseudo random
58-
output[i] = (unsigned char) rand();
55+
//replace result with pseudo random
56+
while (len > 0) {
57+
#if (RAND_MAX >= 0x00FFFFFF)
58+
*output = (unsigned char) (rand() >> 16);
59+
#else
60+
*output = (unsigned char) rand() ; /* e. g. Visual C */
61+
#endif
62+
output += 1;
63+
len -= 1;
5964
}
6065
return ret;
6166
}

programs/test/benchmark.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -455,23 +455,20 @@ static void mbedtls_set_alarm(int seconds)
455455

456456
static int myrand(void *rng_state, unsigned char *output, size_t len)
457457
{
458-
size_t use_len;
459458
int rnd;
460459

461460
if (rng_state != NULL) {
462461
rng_state = NULL;
463462
}
464463

465464
while (len > 0) {
466-
use_len = len;
467-
if (use_len > sizeof(int)) {
468-
use_len = sizeof(int);
469-
}
470-
471-
rnd = rand();
472-
memcpy(output, &rnd, use_len);
473-
output += use_len;
474-
len -= use_len;
465+
#if (RAND_MAX >= 0x00FFFFFF)
466+
*output = (unsigned char) (rand() >> 16);
467+
#else
468+
*output = (unsigned char) rand() ; /* e. g. Visual C */
469+
#endif
470+
output += 1;
471+
len -= 1;
475472
}
476473

477474
return 0;

tests/src/test_helpers/ssl_helpers.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len)
1616
{
1717
(void) p_rng;
18-
for (size_t i = 0; i < output_len; i++) {
19-
output[i] = rand();
18+
while (output_len > 0) {
19+
#if (RAND_MAX >= 0x00FFFFFF)
20+
*output = (unsigned char) (rand() >> 16);
21+
#else
22+
*output = (unsigned char) rand() ; /* e. g. Visual C */
23+
#endif
24+
output += 1;
25+
output_len -= 1;
2026
}
2127

2228
return 0;

0 commit comments

Comments
 (0)