Repository navigation
Commit 85b178b
authored
Rely on WC_RNG lock for global RNG and make RAND_cleanup a no-op (#12251)
* Make RAND_cleanup a no-op, free the global RNG in wolfSSL_Cleanup
OpenSSL made RAND_cleanup() a no-op in 1.1.0 ("it does nothing and
should not be called"), so code written for it may call it at any
time. Do the same: the teardown moves to wolfssl_rand_cleanup(), which
wolfSSL_Cleanup() calls on the last reference. It runs the RAND method
cleanup and frees the global RNG.
This lets the global RNG be used without globalRNGMutex: nothing can
free it under a running generate.
The global RNG now stays allocated until wolfSSL_Cleanup(). A program
that uses the RAND API without wolfSSL_Init() and wolfSSL_Cleanup()
no longer has a call that frees it.
The tests that need a fresh global RNG call wolfssl_rand_cleanup().
* Global RNG: rely on the WC_RNG lock
Since 8eacfc6 a WC_RNG has its own lock, held by
wc_RNG_GenerateBlock() and the reseed and stir calls. Stop taking
globalRNGMutex around those uses of the global RNG. It is still taken
to init and free the RNG and to guard gDrbgDefCtx.
Builds where the global RNG has no lock of its own (no
WC_RNG_HAVE_AUTO_LOCK, e.g. FIPS before v7, or
WC_RNG_AUTO_LOCK_DEFAULT_OFF) still need the mutex around each use.
So do WOLF_CRYPTO_CB_FIND builds, as the RNG callback runs outside the
RNG's own lock, and builds without atomics. That now lives in three
helpers, so the callers make one call and hold no lock:
- wolfssl_global_rng_generate() fills a buffer,
- wolfssl_global_rng_mix() stirs in caller material,
- wolfssl_global_rng_reseed() reseeds from the system.
The generate and mix helpers init the global RNG when needed, before
taking the lock, as wolfSSL_RAND_Init() takes globalRNGMutex. Reseed
does not init: RAND_poll fails when the RNG is not initialized. All
three check the init flag again under the lock. initGlobalRNG is now
atomic so a thread that sees it set also sees the initialized RNG.
The pid check that old FIPS and selftest builds do in
wolfSSL_RAND_bytes() moves to its own function; those builds always
have the mutex.
Add a test that uses RAND_bytes, BN_rand, RAND_status and RAND_poll
from several threads.1 parent bd431d2 commit 85b178b
8 files changed
Lines changed: 347 additions & 285 deletions
File tree
- src
- tests/api
- wolfssl
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
293 | 293 | | |
294 | 294 | | |
295 | 295 | | |
296 | | - | |
| 296 | + | |
297 | 297 | | |
298 | 298 | | |
299 | 299 | | |
| |||
303 | 303 | | |
304 | 304 | | |
305 | 305 | | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
306 | 449 | | |
307 | 450 | | |
308 | 451 | | |
| |||
311 | 454 | | |
312 | 455 | | |
313 | 456 | | |
314 | | - | |
| 457 | + | |
315 | 458 | | |
316 | 459 | | |
317 | 460 | | |
| |||
3834 | 3977 | | |
3835 | 3978 | | |
3836 | 3979 | | |
3837 | | - | |
| 3980 | + | |
3838 | 3981 | | |
3839 | 3982 | | |
3840 | 3983 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2182 | 2182 | | |
2183 | 2183 | | |
2184 | 2184 | | |
2185 | | - | |
2186 | 2185 | | |
2187 | 2186 | | |
2188 | 2187 | | |
| |||
2212 | 2211 | | |
2213 | 2212 | | |
2214 | 2213 | | |
2215 | | - | |
2216 | | - | |
2217 | | - | |
2218 | | - | |
2219 | | - | |
2220 | | - | |
2221 | 2214 | | |
2222 | | - | |
2223 | | - | |
| 2215 | + | |
| 2216 | + | |
2224 | 2217 | | |
2225 | 2218 | | |
2226 | 2219 | | |
2227 | | - | |
2228 | | - | |
2229 | | - | |
2230 | | - | |
2231 | | - | |
2232 | | - | |
2233 | 2220 | | |
2234 | | - | |
2235 | | - | |
2236 | | - | |
2237 | | - | |
2238 | | - | |
2239 | | - | |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
2240 | 2225 | | |
2241 | 2226 | | |
2242 | 2227 | | |
| |||
0 commit comments