Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ specific to crypto.
- Every C library is upstream code (CODING-GUIDELINES.md chapter 5 rule 1): never modify
third-party code; a defect found in a dependency is reported to the owner, who submits the fix
upstream, and until upstream applies it the pin may point at our fork's branch carrying exactly
that patch. BearSSL is ae63032 of webcpporg/bearssl's branch
upstream/ecdsa-bits2int-constant-time: upstream 7bea48e and the ECDSA nonce fix submitted
upstream (bits2int, a Minerva-class leak); the pin returns to upstream once the patch is
applied. A moved pin is a change of its own (rule 12).
that patch. BearSSL is f8a2f3d of webcpporg/bearssl's branch webcpp-pin: upstream 7bea48e
and two fixes, each submitted upstream on a branch of its own: the ECDSA nonce fix (ae63032,
upstream/ecdsa-bits2int-constant-time; bits2int, a Minerva-class leak) and P-256's scalar
check (ec57f88, upstream/ec-p256-check-scalar-constant-time; clang made a jump on the secret
scalar on x86-64), carried by the owner's explicit exception to the rule; the pin returns to
upstream once upstream applies them. A moved pin is a change of its own (rule 12).
- X25519's constant time on wasip2 is an open finding (the page's constant-time table): do not
describe it as a pass.
46 changes: 38 additions & 8 deletions doc/backends.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,19 @@ is BearSSL's `br_ecdsa_i31_sign_raw` and `br_ecdsa_i31_vrfy_raw` on every
target, over the curve code chosen.

[#crypto-backends-bearssl-fix]
webcpp's BearSSL is `ae63032` of its fork's branch
`upstream/ecdsa-bits2int-constant-time`
webcpp's BearSSL is `f8a2f3d` of its fork's branch `webcpp-pin`
(link:https://github.com/webcpporg/bearssl/commit/f8a2f3d96fc6e3ce8c7b626fec4fc506fef2b0ce[webcpporg/bearssl
f8a2f3d]): upstream's commit `7bea48e` and exactly two patches, the fixes below,
each on a branch of its own on top of `7bea48e`, which the owner submits
upstream: `ae63032` on `upstream/ecdsa-bits2int-constant-time`
(link:https://github.com/webcpporg/bearssl/commit/ae63032d9f30bf53674db5445875adf71b8626b7[webcpporg/bearssl
ae63032]): upstream's commit `7bea48e` and exactly one patch, the fix below,
which the owner submits upstream. webcpp never modifies third-party code: a
defect found in a dependency is reported to the owner, who submits the fix
upstream, and the pin returns to upstream once upstream applies it
(CODING-GUIDELINES.md chapter 5, rule 1). Upstream's ECDSA signing reads RFC 6979's nonce through
ae63032]) and `ec57f88` on `upstream/ec-p256-check-scalar-constant-time`
(link:https://github.com/webcpporg/bearssl/commit/ec57f886ee0a0783acfca1b465b43ccd62b71046[webcpporg/bearssl
ec57f88]). webcpp never modifies third-party code: a defect found in a
dependency is reported to the owner, who submits the fix upstream, and the pin
returns to upstream once upstream applies it (CODING-GUIDELINES.md chapter 5,
rule 1); both patches are carried by the owner's explicit exception to that
rule. Upstream's ECDSA signing reads RFC 6979's nonce through
`br_ecdsa_i31_bits2int`
(link:https://www.bearssl.org/gitweb/?p=BearSSL;a=blob;f=src/ec/ecdsa_i31_bits.c;hb=7bea48e5e850ab4cafbe68d3765cdaba13a86d6f#l44[ecdsa_i31_bits.c,
lines 44 and 45]): `br_i31_decode` writes the nonce's actual bit length into
Expand All @@ -90,6 +95,27 @@ MemorySanitizer; dudect, which would see one measurement in about 256 differ
by a few instructions, did not. At the pin both checkers find nothing in
P-256's `sign` (<<crypto-backends-constant-time>>).

The second patch is in P-256's check of the scalar, `check_scalar` in
the `mul` of `br_ec_p256_m62`, which ECDH (`agree`) calls on the secret scalar
(link:https://www.bearssl.org/gitweb/?p=BearSSL;a=blob;f=src/ec/ec_p256_m62.c;hb=7bea48e5e850ab4cafbe68d3765cdaba13a86d6f#l1623[ec_p256_m62.c,
lines 1623 to 1627]). Its C compares the scalar with the order byte by byte
through masks, without a branch, but the compiler reads the mask as a select,
and Clang on x86-64 turns that select into a conditional jump on the running
comparison: Clang 18 in its pass that converts a `cmov` in a loop into a
jump, Clang 22 and Apple Clang 21 earlier, in CodeGenPrepare. The jump tells
how many leading bytes of the scalar equal the order's, a prefix about one
scalar in 256 has; under callgrind the instructions `mul` executes differ with
that prefix. GCC, and Clang on arm64, keep the select. The patch computes
0 < k < n as k - 1 < n - 1, two borrow chains in one loop with no test of an
intermediate value, which every compiler tried keeps without a jump: Clang 18
and 22 and GCC 13 on x86-64, Clang 18 and GCC 13 on arm64, from `-O0` to `-O3`
and `-Os`, and Apple Clang 21. It changes `check_scalar` in `ec_p256_m62.c`
and its copy in `ec_p256_m64.c` alone, and gives the same verdict as before on
11 million scalars. The constant-time lane found the jump on 2026-12-03 under
memcheck on the x86-64 runner (MemorySanitizer, which checks the select before
the code generator, cannot see it, and the lane's arm64 runs had none); at
the pin memcheck finds nothing in `agree`.

A program has one copy of each library: libsecp256k1's tables and OpenSSL's
state are global symbols, so webcpp's build is never linked with another copy.

Expand Down Expand Up @@ -188,7 +214,11 @@ Apple M5 Max, while webcpp's BearSSL carried a fix of its own on its mirror's
`webcpp` branch, replaced on 2026-12-02 by the same fix submitted upstream
(<<crypto-backends-bearssl-fix>>). Every path above passed under both, P-256's
signing with its low-S step included, and again on 2026-12-02 at the pin
`ae63032`. The run of 2026-11-21 on upstream's BearSSL, without the fix,
`ae63032`. On 2026-12-03 memcheck's two programs, `ctgrind` and
`ctgrind_space`, ran on x86-64 too (Ubuntu 24.04's image `linux-lint:amd64`,
under emulation, clang 18): at `ae63032` they reported the jump of P-256's
scalar check above, as the CI's x86-64 runner did, and at the pin `f8a2f3d`
they passed. The run of 2026-11-21 on upstream's BearSSL, without the fix,
reported the nonce's leak under both. Each planted program failed where it must, and the
lane's dudect programs passed there too, with `dudect_planted` at 34.89 on the
2026-11-21 run.
Expand Down
2 changes: 1 addition & 1 deletion doc/generated/backend-facts.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ The library behind each, and what it costs a program:
|Backend |Library |Licence |Its own API, reachable |Allocation

|BearSSL
|BearSSL at webcpporg/bearssl commit ae63032d, upstream commit 7bea48e5 plus exactly the ECDSA nonce fix submitted upstream, until upstream applies it, built from source
|BearSSL at webcpporg/bearssl commit f8a2f3d9, upstream commit 7bea48e5 plus exactly the two fixes submitted upstream (the ECDSA nonce's and P-256's scalar check's), until upstream applies them, built from source
|MIT
|`+<bearssl.h>+`, all of it
|none: the key schedule and GCM's state live inside the object
Expand Down
4 changes: 2 additions & 2 deletions doc/generated/constant-time.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
|Backend |By design |Measured, native |Measured, wasip2 |Measured, wasip3 |Measured, emscripten

|BearSSL
|by design: `+aes_ct64+` and `+ghash_ctmul64+`, or AES-NI and PCLMUL where the CPU has them; on a 32-bit host a 64-bit multiplication may not be constant time, which BearSSL documents; ECDSA signing at webcpp's pin, whose patch submitted upstream closes upstream's leak of the RFC 6979 nonce's bit length (link:https://www.bearssl.org/gitweb/?p=BearSSL;a=blob;f=src/ec/ecdsa_i31_bits.c;hb=7bea48e5e850ab4cafbe68d3765cdaba13a86d6f#l44[BearSSL's ecdsa_i31_bits.c at upstream 7bea48e, lines 44 and 45: the nonce decoded with its actual bit length in the integer's header, then shifted over the words that length needs]; link:https://minerva.crocs.fi.muni.cz/[Minerva: the leak of an ECDSA nonce's bit length through timing, and the lattice that recovers the key (CVE-2019-15809)]; the patch: link:https://github.com/webcpporg/bearssl/commit/ae63032d9f30bf53674db5445875adf71b8626b7[webcpporg/bearssl, commit ae63032 on its branch upstream/ecdsa-bits2int-constant-time, webcpp's pin: upstream 7bea48e plus exactly the patch submitted upstream, br_ecdsa_i31_bits2int and br_ecdsa_i15_bits2int setting the integer's header, before the right shift, to the larger of the decoded bit length and the order's, both public; the pin returns to upstream once upstream applies it]); link:https://www.bearssl.org/constanttime.html[BearSSL, constant-time crypto]; link:https://www.bearssl.org/ctmul.html[BearSSL, constant-time multiplications]
|Valgrind's memcheck (Linux): AES-GCM, ChaCha20-Poly1305, P-256 (the key pair's making, `+sign+` of a digest (`+crypto::signing_schemes::prehashed+`), which makes its signature low S without a branch, then `+low_s()+` and `+is_low_s()+` of it, `+agree+` and the private formats, `+pkcs8+`, `+raw+` and `+pem(pkcs8)+` both ways; BearSSL's own branches on its signing's verdicts listed in `+bearssl_verdicts.supp+`; nothing found at webcpp's pin ae63032, whose patch submitted upstream closes upstream's leak of the nonce's bit length in `+sign+` that this checker found on 2026-11-21, `+sign+` with its low-S step included (Valgrind 3.22, arm64 Linux container, 2026-12-02)); MemorySanitizer (Linux, clang): AES-GCM, ChaCha20-Poly1305, P-256 (the same paths as memcheck's; the same leak in `+sign+` on 2026-11-21 on upstream's BearSSL, nothing at webcpp's pin ae63032, `+sign+` with its low-S step included (clang 18, arm64 Linux container, 2026-12-02)); dudect (judged; on a runner GitHub hosts, by two of three runs): AES-GCM, ChaCha20-Poly1305, P-256 (`+sign+` of a digest and `+agree+`, each against the key pair's scalar; `+sign+` with its low-S step judged constant time, \|t\| 1.70 on Linux and 1.63 natively on an Apple M5 Max (release, one run each, 2026-11-22; provisional, measured again in the final quiet pass))
|by design: `+aes_ct64+` and `+ghash_ctmul64+`, or AES-NI and PCLMUL where the CPU has them; on a 32-bit host a 64-bit multiplication may not be constant time, which BearSSL documents; ECDSA signing at webcpp's pin, whose patch submitted upstream closes upstream's leak of the RFC 6979 nonce's bit length (link:https://www.bearssl.org/gitweb/?p=BearSSL;a=blob;f=src/ec/ecdsa_i31_bits.c;hb=7bea48e5e850ab4cafbe68d3765cdaba13a86d6f#l44[BearSSL's ecdsa_i31_bits.c at upstream 7bea48e, lines 44 and 45: the nonce decoded with its actual bit length in the integer's header, then shifted over the words that length needs]; link:https://minerva.crocs.fi.muni.cz/[Minerva: the leak of an ECDSA nonce's bit length through timing, and the lattice that recovers the key (CVE-2019-15809)]; the patch: link:https://github.com/webcpporg/bearssl/commit/ae63032d9f30bf53674db5445875adf71b8626b7[webcpporg/bearssl, commit ae63032 on its branch upstream/ecdsa-bits2int-constant-time, one of the two patches of webcpp's pin f8a2f3d (upstream 7bea48e plus exactly the patches submitted upstream): br_ecdsa_i31_bits2int and br_ecdsa_i15_bits2int setting the integer's header, before the right shift, to the larger of the decoded bit length and the order's, both public; the pin returns to upstream once upstream applies it]); P-256's `+mul+`, which ECDH calls, at webcpp's pin, whose other patch submitted upstream removes the jump on the secret scalar that Clang makes on x86-64 of upstream's scalar check (link:https://www.bearssl.org/gitweb/?p=BearSSL;a=blob;f=src/ec/ec_p256_m62.c;hb=7bea48e5e850ab4cafbe68d3765cdaba13a86d6f#l1623[BearSSL's ec_p256_m62.c at upstream 7bea48e, lines 1623 to 1627: the scalar compared with the order through masks that Clang on x86-64 compiles into a conditional jump on the comparison so far]; the patch: link:https://github.com/webcpporg/bearssl/commit/ec57f886ee0a0783acfca1b465b43ccd62b71046[webcpporg/bearssl, commit ec57f88 on its branch upstream/ec-p256-check-scalar-constant-time, the other patch of webcpp's pin f8a2f3d: check_scalar in ec_p256_m62.c and ec_p256_m64.c computing 0 < k < n as k - 1 < n - 1 with two borrow chains, no select for a compiler to turn into a jump; the pin returns to upstream once upstream applies it]); link:https://www.bearssl.org/constanttime.html[BearSSL, constant-time crypto]; link:https://www.bearssl.org/ctmul.html[BearSSL, constant-time multiplications]
|Valgrind's memcheck (Linux): AES-GCM, ChaCha20-Poly1305, P-256 (the key pair's making, `+sign+` of a digest (`+crypto::signing_schemes::prehashed+`), which makes its signature low S without a branch, then `+low_s()+` and `+is_low_s()+` of it, `+agree+` and the private formats, `+pkcs8+`, `+raw+` and `+pem(pkcs8)+` both ways; BearSSL's own branches on its signing's verdicts listed in `+bearssl_verdicts.supp+`; nothing found at webcpp's pin f8a2f3d, whose patches submitted upstream close upstream's leak of the nonce's bit length in `+sign+` that this checker found on 2026-11-21 and the jump on the scalar in P-256's scalar check that it found under `+agree+` on the x86-64 runner on 2026-12-03, `+sign+` with its low-S step included (Valgrind 3.22, arm64 Linux container, 2026-12-02; x86-64, `+ctgrind+` and `+ctgrind_space+`, 2026-12-04)); MemorySanitizer (Linux, clang): AES-GCM, ChaCha20-Poly1305, P-256 (the same paths as memcheck's; the same leak in `+sign+` on 2026-11-21 on upstream's BearSSL, nothing at webcpp's pin ae63032, `+sign+` with its low-S step included (clang 18, arm64 Linux container, 2026-12-02); MemorySanitizer checks the select of P-256's scalar check before the code generator makes it a jump, so the second patch changes nothing it sees); dudect (judged; on a runner GitHub hosts, by two of three runs): AES-GCM, ChaCha20-Poly1305, P-256 (`+sign+` of a digest and `+agree+`, each against the key pair's scalar; `+sign+` with its low-S step judged constant time, \|t\| 1.70 on Linux and 1.63 natively on an Apple M5 Max (release, one run each, 2026-11-22; provisional, measured again in the final quiet pass))
|dudect (judged; on a runner GitHub hosts, by two of three runs): AES-GCM, ChaCha20-Poly1305, P-256 (`+sign+` of a digest and `+agree+`, each against the key pair's scalar; `+sign+` with its low-S step judged constant time, \|t\| 1.70 on Linux and 1.63 natively on an Apple M5 Max (release, one run each, 2026-11-22; provisional, measured again in the final quiet pass))
|not measured yet
|not measured yet
Expand Down
36 changes: 18 additions & 18 deletions doc/generated/messages.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1307,19 +1307,19 @@ On each target, the build's output contains:
+
* native, macOS:
** `+Undefined symbols for architecture+`
** `+_webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link+`
** `+_webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link+`
* native, Linux:
** `+webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link+`
** `pass:[undefined reference to &#96;webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link']`
** `+webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link+`
** `pass:[undefined reference to &#96;webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link']`
* wasip2:
** `+wasm-ld: error:+`
** `+undefined symbol: webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link+`
** `+undefined symbol: webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link+`
* wasip3:
** `+wasm-ld: error:+`
** `+undefined symbol: webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link+`
** `+undefined symbol: webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link+`
* emscripten:
** `+wasm-ld: error:+`
** `+undefined symbol: webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link+`
** `+undefined symbol: webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link+`
** `pass:[em++: error:]`

`+canary_other_commit+`:: A BearSSL of another commit than the headers'. The fix: Build the library at the commit of webcpp's submodule, or link it through b2.
Expand Down Expand Up @@ -1363,19 +1363,19 @@ On each target, the build's output contains:
+
* native, macOS:
** `+Undefined symbols for architecture+`
** `+_webcpp_crypto_bearssl_ae63032_sysrng__add_third_party_bearssl_to_your_link+`
** `+_webcpp_crypto_bearssl_f8a2f3d_sysrng__add_third_party_bearssl_to_your_link+`
* native, Linux:
** `+webcpp_crypto_bearssl_ae63032_sysrng__add_third_party_bearssl_to_your_link+`
** `pass:[undefined reference to &#96;webcpp_crypto_bearssl_ae63032_sysrng__add_third_party_bearssl_to_your_link']`
** `+webcpp_crypto_bearssl_f8a2f3d_sysrng__add_third_party_bearssl_to_your_link+`
** `pass:[undefined reference to &#96;webcpp_crypto_bearssl_f8a2f3d_sysrng__add_third_party_bearssl_to_your_link']`
* wasip2:
** `+wasm-ld: error:+`
** `+undefined symbol: webcpp_crypto_bearssl_ae63032_sysrng__add_third_party_bearssl_to_your_link+`
** `+undefined symbol: webcpp_crypto_bearssl_f8a2f3d_sysrng__add_third_party_bearssl_to_your_link+`
* wasip3:
** `+wasm-ld: error:+`
** `+undefined symbol: webcpp_crypto_bearssl_ae63032_sysrng__add_third_party_bearssl_to_your_link+`
** `+undefined symbol: webcpp_crypto_bearssl_f8a2f3d_sysrng__add_third_party_bearssl_to_your_link+`
* emscripten:
** `+wasm-ld: error:+`
** `+undefined symbol: webcpp_crypto_bearssl_ae63032_sysrng__add_third_party_bearssl_to_your_link+`
** `+undefined symbol: webcpp_crypto_bearssl_f8a2f3d_sysrng__add_third_party_bearssl_to_your_link+`
** `pass:[em++: error:]`

`+canary_secp256k1_missing+`:: libsecp256k1's headers used without its library. The fix: Link `+/third_party//secp256k1+`.
Expand Down Expand Up @@ -1475,19 +1475,19 @@ On each target, the build's output contains:
+
* native, macOS:
** `+Undefined symbols for architecture+`
** `+_webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link+`
** `+_webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link+`
* native, Linux:
** `+webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link+`
** `pass:[undefined reference to &#96;webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link']`
** `+webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link+`
** `pass:[undefined reference to &#96;webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link']`
* wasip2:
** `+wasm-ld: error:+`
** `+undefined symbol: webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link+`
** `+undefined symbol: webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link+`
* wasip3:
** `+wasm-ld: error:+`
** `+undefined symbol: webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link+`
** `+undefined symbol: webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link+`
* emscripten:
** `+wasm-ld: error:+`
** `+undefined symbol: webcpp_crypto_bearssl_ae63032_getentropy__add_third_party_bearssl_to_your_link+`
** `+undefined symbol: webcpp_crypto_bearssl_f8a2f3d_getentropy__add_third_party_bearssl_to_your_link+`
** `pass:[em++: error:]`

`+chacha_default_not_linked_on_emscripten+`:: A program for Emscripten that calls the three ChaCha20-Poly1305 AEADs on their default, libsodium, and links none. The fix: As `+chacha_default_not_linked+`.
Expand Down
Loading
Loading