Fix C challenge nonce CSPRNG handling#4864
Conversation
godd-ctrl
left a comment
There was a problem hiding this comment.
Approved current head f07fe188f7eb97111e24d114f6450141f48d5670.
This fixes the nonce-generation issue I flagged on the previous C challenge PR: Linux now loops until the full nonce buffer is filled, retries EINTR, fails closed on other getrandom failures, initializes the Challenge struct before use, and removes the weak timestamp/rand fallback. Unsupported platforms also fail closed rather than mixing predictable bytes.
Validation performed:
- git diff --name-status origin/main...HEAD -> challenge_response.c modified, tests/test_challenge_response_secure_nonce.py added
- git diff --check origin/main...HEAD -> passed
- python tools\bcos_spdx_check.py --base-ref origin/main -> passed
- python -m py_compile tests\test_challenge_response_secure_nonce.py -> passed
- python -m pytest tests\test_challenge_response_secure_nonce.py -q -> 4 passed
- rg confirmed rand/srand removal and getrandom/arc4random_buf secure nonce paths
- gcc --version -> unavailable in this Windows environment, so I could not compile the C file locally
No blocking issues found in the reviewed diff.
saim256
left a comment
There was a problem hiding this comment.
Approving current head f07fe18.
This is a focused fix for #4861 and closes the blocker I saw in the earlier C nonce attempt: Linux getrandom() is now checked in a retry loop, short reads advance by the returned byte count, EINTR is retried, unsupported platforms fail closed, and generate_challenge() zero-initializes the struct before failing rather than returning a weak or partially initialized nonce. The rand()/srand() nonce path is gone.
Validation performed:
python -m pytest tests\test_challenge_response_secure_nonce.py -q-> 4 passedpython -m py_compile tests\test_challenge_response_secure_nonce.py-> passedpython tools\bcos_spdx_check.py --base-ref origin/main-> BCOS SPDX check: OKgit diff --check origin/main...HEAD -- rips/rustchain-core/src/anti_spoof/challenge_response.c tests/test_challenge_response_secure_nonce.py-> passedwhere.exe gccandwhere.exe clang-> no compiler installed locally, so I could not build the C file in this environment
dazer1234
left a comment
There was a problem hiding this comment.
Code review for RustChain bounty #73.
Summary: This is a focused CSPRNG replacement for challenge nonces. The Linux path loops until the full nonce buffer is filled, retries EINTR, and the Apple path uses arc4random_buf; the tests also explicitly prevent rand()/srand() from returning.
Findings: No blocking issues found. The fail-closed path for unsupported platforms is appropriate for a security-sensitive nonce generator.
Verdict: Looks good.
Reviewed with OpenAI Codex assistance.
loganoe
left a comment
There was a problem hiding this comment.
Review for Scottcjn/rustchain-bounties#73. Approved current head f07fe1850e26010c400dfa31dec098a375dac9fe.
I focused on the Linux C build path that prior reviews could not exercise locally. The secure nonce helper uses getrandom() in a short-read loop, advances by the returned byte count, retries EINTR, and fails closed on unsupported platforms. generate_challenge() zero-initializes the struct before calling the helper and exits before returning a challenge when nonce generation fails.
Validation performed:
gcc -O0 rips/rustchain-core/src/anti_spoof/challenge_response.c -o /tmp/challenge_response_linux-> passed on Linuxgcc -std=gnu11 -Wall -Wextra -c rips/rustchain-core/src/anti_spoof/challenge_response.c -o /tmp/challenge_response_gnu.o-> passed with only pre-existing unused-variable/parameter warningsPYTEST_DISABLE_PLUGIN_AUTOLOAD=1 uv run --no-project --with pytest --with flask python -m pytest tests/test_challenge_response_secure_nonce.py -q-> 4 passedgit diff --check origin/main...HEAD-> passedpython3 tools/bcos_spdx_check.py --base-ref origin/main-> OK
No blocking findings from the Linux compile/test pass.
Code Review: Fix C challenge nonce CSPRNG handlingSummaryThis PR replaces the insecure Positive Observations ✅
Issues Found 🔍1. No fallback for non-Apple, non-Linux platforms (MEDIUM)The function returns
Recommendation: Add BSD and Windows support: #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
arc4random_buf(nonce, len);
return 0;
#elif defined(_WIN32)
return BCryptGenRandom(NULL, nonce, len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) == 0 ? 0 : -1;2.
|
jaxint
left a comment
There was a problem hiding this comment.
Great contribution! LGTM.
jaxint
left a comment
There was a problem hiding this comment.
Great contribution! LGTM.
jaxint
left a comment
There was a problem hiding this comment.
Great contribution! LGTM.
jaxint
left a comment
There was a problem hiding this comment.
Great contribution! LGTM.
jaxint
left a comment
There was a problem hiding this comment.
Great contribution! LGTM.
jaxint
left a comment
There was a problem hiding this comment.
Great contribution! LGTM.
jaxint
left a comment
There was a problem hiding this comment.
Great contribution! LGTM.
Summary
rand()/srand()challenge nonces with OS CSPRNG bytes:arc4random_buf()on macOS and a checked/retriedgetrandom()loop on Linux.Fixes #4861.
Validation
python -m pytest tests\test_challenge_response_secure_nonce.py -q-> 4 passedpython -m py_compile tests\test_challenge_response_secure_nonce.py-> passedgit diff --check -- rips\rustchain-core\src\anti_spoof\challenge_response.c tests\test_challenge_response_secure_nonce.py-> passedpython tools\bcos_spdx_check.py --base-ref origin/main-> OKgcc --version/clang --version-> unavailable in this Windows environment, so I could not compile the C file locally