@@ -33105,6 +33105,12 @@ static int test_wolfSSL_RAND_bytes(void)
3310533105 const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
3310633106 int max_bufsize;
3310733107 byte *my_buf = NULL;
33108+ #if defined(HAVE_GETPID)
33109+ byte seed[16] = {0};
33110+ byte randbuf[8] = {0};
33111+ int pipefds[2] = {0};
33112+ pid_t pid = 0;
33113+ #endif
3310833114
3310933115 /* sanity check */
3311033116 ExpectIntEQ(RAND_bytes(NULL, 16), 0);
@@ -33124,6 +33130,42 @@ static int test_wolfSSL_RAND_bytes(void)
3312433130 ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
3312533131 ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
3312633132
33133+ #if defined(OPENSSL_EXTRA) && defined(HAVE_GETPID)
33134+ XMEMSET(seed, 0, sizeof(seed));
33135+ RAND_cleanup();
33136+
33137+ /* No global methods set. */
33138+ ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
33139+
33140+ ExpectIntEQ(pipe(pipefds), 0);
33141+ pid = fork();
33142+ ExpectIntGE(pid, 0);
33143+ if (pid == 0) {
33144+ ssize_t n_written = 0;
33145+
33146+ /* Child process. */
33147+ close(pipefds[0]);
33148+ RAND_bytes(randbuf, sizeof(randbuf));
33149+ n_written = write(pipefds[1], randbuf, sizeof(randbuf));
33150+ close(pipefds[1]);
33151+ exit(n_written == sizeof(randbuf) ? 0 : 1);
33152+ }
33153+ else {
33154+ /* Parent process. */
33155+ word64 childrand64 = 0;
33156+ int waitstatus = 0;
33157+
33158+ close(pipefds[1]);
33159+ ExpectIntEQ(RAND_bytes(randbuf, sizeof(randbuf)), 1);
33160+ ExpectIntEQ(read(pipefds[0], &childrand64, sizeof(childrand64)),
33161+ sizeof(childrand64));
33162+ ExpectBufNE(randbuf, &childrand64, sizeof(randbuf));
33163+ close(pipefds[0]);
33164+ waitpid(pid, &waitstatus, 0);
33165+ }
33166+ RAND_cleanup();
33167+ #endif
33168+
3312733169 XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3312833170#endif
3312933171 return EXPECT_RESULT();
@@ -33156,50 +33198,60 @@ static int test_wolfSSL_RAND(void)
3315633198}
3315733199
3315833200
33201+ #ifdef WC_RNG_SEED_CB
33202+ static int wc_DummyGenerateSeed(OS_Seed* os, byte* output, word32 sz)
33203+ {
33204+ word32 i;
33205+ for (i = 0; i < sz; i++ )
33206+ output[i] = (byte)i;
33207+
33208+ (void)os;
33209+
33210+ return 0;
33211+ }
33212+ #endif /* WC_RNG_SEED_CB */
33213+
33214+
3315933215static int test_wolfSSL_RAND_poll(void)
3316033216{
3316133217 EXPECT_DECLS;
3316233218
33163- #if defined(OPENSSL_EXTRA) && defined(__linux__)
33164- byte seed[16] = {0};
33165- byte randbuf[8] = {0};
33166- int pipefds[2] = {0};
33167- pid_t pid = 0;
33219+ #if defined(OPENSSL_EXTRA)
33220+ byte seed[16];
33221+ byte rand1[16];
33222+ #ifdef WC_RNG_SEED_CB
33223+ byte rand2[16];
33224+ #endif
3316833225
3316933226 XMEMSET(seed, 0, sizeof(seed));
33227+ ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
33228+ ExpectIntEQ(RAND_poll(), 1);
33229+ ExpectIntEQ(RAND_bytes(rand1, 16), 1);
33230+ RAND_cleanup();
33231+
33232+ #ifdef WC_RNG_SEED_CB
33233+ /* Test with custom seed and poll */
33234+ wc_SetSeed_Cb(wc_DummyGenerateSeed);
3317033235
33171- /* No global methods set. */
3317233236 ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
33237+ ExpectIntEQ(RAND_bytes(rand1, 16), 1);
33238+ RAND_cleanup();
3317333239
33174- ExpectIntEQ(pipe(pipefds), 0);
33175- pid = fork();
33176- ExpectIntGE(pid, 0);
33177- if (pid == 0)
33178- {
33179- ssize_t n_written = 0;
33240+ /* test that the same value is generated twice with dummy seed function */
33241+ ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
33242+ ExpectIntEQ(RAND_bytes(rand2, 16), 1);
33243+ ExpectIntEQ(XMEMCMP(rand1, rand2, 16), 0);
33244+ RAND_cleanup();
3318033245
33181- /* Child process. */
33182- close(pipefds[0]);
33183- RAND_poll();
33184- RAND_bytes(randbuf, sizeof(randbuf));
33185- n_written = write(pipefds[1], randbuf, sizeof(randbuf));
33186- close(pipefds[1]);
33187- exit(n_written == sizeof(randbuf) ? 0 : 1);
33188- }
33189- else
33190- {
33191- /* Parent process. */
33192- word64 childrand64 = 0;
33193- int waitstatus = 0;
33246+ /* test that doing a poll is reseeding RNG */
33247+ ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
33248+ ExpectIntEQ(RAND_poll(), 1);
33249+ ExpectIntEQ(RAND_bytes(rand2, 16), 1);
33250+ ExpectIntNE(XMEMCMP(rand1, rand2, 16), 0);
3319433251
33195- close(pipefds[1]);
33196- ExpectIntEQ(RAND_poll(), 1);
33197- ExpectIntEQ(RAND_bytes(randbuf, sizeof(randbuf)), 1);
33198- ExpectIntEQ(read(pipefds[0], &childrand64, sizeof(childrand64)), sizeof(childrand64));
33199- ExpectBufNE(randbuf, &childrand64, sizeof(randbuf));
33200- close(pipefds[0]);
33201- waitpid(pid, &waitstatus, 0);
33202- }
33252+ /* reset the seed function used */
33253+ wc_SetSeed_Cb(wc_GenerateSeed);
33254+ #endif
3320333255 RAND_cleanup();
3320433256
3320533257 ExpectIntEQ(RAND_egd(NULL), -1);
0 commit comments