@@ -33158,6 +33158,12 @@ static int test_wolfSSL_RAND_bytes(void)
3315833158 const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
3315933159 int max_bufsize;
3316033160 byte *my_buf = NULL;
33161+ #if defined(HAVE_GETPID)
33162+ byte seed[16] = {0};
33163+ byte randbuf[8] = {0};
33164+ int pipefds[2] = {0};
33165+ pid_t pid = 0;
33166+ #endif
3316133167
3316233168 /* sanity check */
3316333169 ExpectIntEQ(RAND_bytes(NULL, 16), 0);
@@ -33177,6 +33183,46 @@ static int test_wolfSSL_RAND_bytes(void)
3317733183 ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
3317833184 ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
3317933185
33186+ #if defined(OPENSSL_EXTRA) && defined(HAVE_GETPID)
33187+ XMEMSET(seed, 0, sizeof(seed));
33188+ RAND_cleanup();
33189+
33190+ /* No global methods set. */
33191+ ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
33192+
33193+ ExpectIntEQ(pipe(pipefds), 0);
33194+ pid = fork();
33195+ ExpectIntGE(pid, 0);
33196+ if (pid == 0) {
33197+ ssize_t n_written = 0;
33198+
33199+ /* Child process. */
33200+ close(pipefds[0]);
33201+ RAND_bytes(randbuf, sizeof(randbuf));
33202+ n_written = write(pipefds[1], randbuf, sizeof(randbuf));
33203+ close(pipefds[1]);
33204+ exit(n_written == sizeof(randbuf) ? 0 : 1);
33205+ }
33206+ else {
33207+ /* Parent process. */
33208+ word64 childrand64 = 0;
33209+ int waitstatus = 0;
33210+
33211+ close(pipefds[1]);
33212+ ExpectIntEQ(RAND_bytes(randbuf, sizeof(randbuf)), 1);
33213+ ExpectIntEQ(read(pipefds[0], &childrand64, sizeof(childrand64)),
33214+ sizeof(childrand64));
33215+ #ifdef WOLFSSL_NO_GETPID
33216+ ExpectBufEQ(randbuf, &childrand64, sizeof(randbuf));
33217+ #else
33218+ ExpectBufNE(randbuf, &childrand64, sizeof(randbuf));
33219+ #endif
33220+ close(pipefds[0]);
33221+ waitpid(pid, &waitstatus, 0);
33222+ }
33223+ RAND_cleanup();
33224+ #endif
33225+
3318033226 XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3318133227#endif
3318233228 return EXPECT_RESULT();
@@ -33209,50 +33255,60 @@ static int test_wolfSSL_RAND(void)
3320933255}
3321033256
3321133257
33258+ #if defined(WC_RNG_SEED_CB) && defined(OPENSSL_EXTRA)
33259+ static int wc_DummyGenerateSeed(OS_Seed* os, byte* output, word32 sz)
33260+ {
33261+ word32 i;
33262+ for (i = 0; i < sz; i++ )
33263+ output[i] = (byte)i;
33264+
33265+ (void)os;
33266+
33267+ return 0;
33268+ }
33269+ #endif /* WC_RNG_SEED_CB */
33270+
33271+
3321233272static int test_wolfSSL_RAND_poll(void)
3321333273{
3321433274 EXPECT_DECLS;
3321533275
33216- #if defined(OPENSSL_EXTRA) && defined(__linux__)
33217- byte seed[16] = {0};
33218- byte randbuf[8] = {0};
33219- int pipefds[2] = {0};
33220- pid_t pid = 0;
33276+ #if defined(OPENSSL_EXTRA)
33277+ byte seed[16];
33278+ byte rand1[16];
33279+ #ifdef WC_RNG_SEED_CB
33280+ byte rand2[16];
33281+ #endif
3322133282
3322233283 XMEMSET(seed, 0, sizeof(seed));
33284+ ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
33285+ ExpectIntEQ(RAND_poll(), 1);
33286+ ExpectIntEQ(RAND_bytes(rand1, 16), 1);
33287+ RAND_cleanup();
33288+
33289+ #ifdef WC_RNG_SEED_CB
33290+ /* Test with custom seed and poll */
33291+ wc_SetSeed_Cb(wc_DummyGenerateSeed);
3322333292
33224- /* No global methods set. */
3322533293 ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
33294+ ExpectIntEQ(RAND_bytes(rand1, 16), 1);
33295+ RAND_cleanup();
3322633296
33227- ExpectIntEQ(pipe(pipefds), 0);
33228- pid = fork();
33229- ExpectIntGE(pid, 0);
33230- if (pid == 0)
33231- {
33232- ssize_t n_written = 0;
33297+ /* test that the same value is generated twice with dummy seed function */
33298+ ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
33299+ ExpectIntEQ(RAND_bytes(rand2, 16), 1);
33300+ ExpectIntEQ(XMEMCMP(rand1, rand2, 16), 0);
33301+ RAND_cleanup();
3323333302
33234- /* Child process. */
33235- close(pipefds[0]);
33236- RAND_poll();
33237- RAND_bytes(randbuf, sizeof(randbuf));
33238- n_written = write(pipefds[1], randbuf, sizeof(randbuf));
33239- close(pipefds[1]);
33240- exit(n_written == sizeof(randbuf) ? 0 : 1);
33241- }
33242- else
33243- {
33244- /* Parent process. */
33245- word64 childrand64 = 0;
33246- int waitstatus = 0;
33303+ /* test that doing a poll is reseeding RNG */
33304+ ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
33305+ ExpectIntEQ(RAND_poll(), 1);
33306+ ExpectIntEQ(RAND_bytes(rand2, 16), 1);
33307+ ExpectIntNE(XMEMCMP(rand1, rand2, 16), 0);
3324733308
33248- close(pipefds[1]);
33249- ExpectIntEQ(RAND_poll(), 1);
33250- ExpectIntEQ(RAND_bytes(randbuf, sizeof(randbuf)), 1);
33251- ExpectIntEQ(read(pipefds[0], &childrand64, sizeof(childrand64)), sizeof(childrand64));
33252- ExpectBufNE(randbuf, &childrand64, sizeof(randbuf));
33253- close(pipefds[0]);
33254- waitpid(pid, &waitstatus, 0);
33255- }
33309+ /* reset the seed function used */
33310+ wc_SetSeed_Cb(wc_GenerateSeed);
33311+ #endif
3325633312 RAND_cleanup();
3325733313
3325833314 ExpectIntEQ(RAND_egd(NULL), -1);
0 commit comments