Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
92 changes: 89 additions & 3 deletions CODING-GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,16 @@ exactly its authors' and the program carries only what it calls.
1. The C library is a submodule under `third_party/libs/<name>`, pinned at a
commit and never edited. When upstream is not on GitHub, an unmodified
`webcpporg/<name>` mirror stands in (BearSSL's is `webcpporg/bearssl`),
refreshed only when the pin moves.
refreshed only when the pin moves. One exception, a security fix upstream
does not have yet: a branch `webcpp` of the mirror carries upstream's
commit and the fix, one commit each, pinned there and carried to each new
pin until upstream has it. The fix is minimal, preserves every value the
library computes, and is reported upstream (an owner item). A library
whose upstream is not mirrored under webcpporg takes such a fix the same
way, in a `webcpporg/<name>` fork created for it, with the same `webcpp`
branch rule. BearSSL's has one, of 2026-11-21: ECDSA decodes RFC 6979's
nonce at its full length, so that signing does not branch on the nonce's
bit length (chapter 15, webcpp::crypto's P-256).
2. `third_party/Jamfile` builds it from its own sources, file by file, as a
static library, in the library's own C dialect, on every target and with
every file: a file that does not apply to a target still compiles, to the
Expand Down Expand Up @@ -971,7 +980,7 @@ update --init third_party/libs/<name>`; a build that needs none never looks.

| Target | What it is | Licence |
| --- | --- | --- |
| `bearssl` | BearSSL from webcpp's unmodified mirror `webcpporg/bearssl` at `7bea48e`; a static library built file by file by `third_party/Jamfile` with webcpp::crypto's and webcpp::hash's canaries (hash's when `libs/hash` is checked out), on every target; `WEBCPP_CRYPTO_HAS_BEARSSL=1`, and `WEBCPP_HASH_HAS_BEARSSL=1` with hash's canary | MIT |
| `bearssl` | BearSSL from webcpp's mirror `webcpporg/bearssl` at `32381d6`, its branch `webcpp`: upstream's `7bea48e` and one fix, ECDSA's nonce decoded at its full length (chapter 5 rule 1); a static library built file by file by `third_party/Jamfile` with webcpp::crypto's and webcpp::hash's canaries (hash's when `libs/hash` is checked out), on every target; `WEBCPP_CRYPTO_HAS_BEARSSL=1`, and `WEBCPP_HASH_HAS_BEARSSL=1` with hash's canary | MIT |
| `bips` | `bitcoin/bips` at `927b6de9`, shallow, test data: crypto's tests read BIP340's `bip-0340/test-vectors.csv` and BIP341's `bip-0341/wallet-test-vectors.json` at run time under the directory its define `WEBCPP_TEST_BIPS` gives, on every target (wasmtime is given it, `webcpp.data`), and run again when it changes | BSD-2-Clause, as BIP340's header states |
| `blake2_vectors` | BLAKE2's reference code package `BLAKE2/BLAKE2` at `ed1974e` (2023-02-12), the submodule `third_party/libs/blake2`, shallow, test data that nothing compiles: hash's tests read `testvectors/blake2b-kat.txt` and `testvectors/blake2-kat.json` at run time under the directory its define `WEBCPP_TEST_BLAKE2_VECTORS` gives, on every target (wasmtime is given it, `webcpp.data`), and run again when one of them changes | CC0-1.0 |
| `blake3` | BLAKE3 1.8.7 at `f3149ec`; `third_party/Jamfile` compiles `c/blake3.c`, `blake3_dispatch.c` and `blake3_portable.c` on every target, the intrinsics files on their instruction sets alone (an exception to chapter 5 rule 2), with webcpp::hash's canary; `WEBCPP_HASH_HAS_BLAKE3=1` | CC0-1.0, Apache-2.0, or Apache-2.0 with LLVM exception |
Expand Down Expand Up @@ -7141,6 +7150,63 @@ and not built is in chapter 17.
Source: `libraries/crypto/openspec/changes/genesis/design.md` (proposals);
`test/secp256k1_xonly_tweak_test.cpp` (libs/crypto).

**P-256 (the foundation's increment 3, its first part; built on the branch
`crypto/p256`, merged 2026-11-22).**

- **`curves::p256` is a family of its own, on BearSSL, its one backend and
default on all four targets; OpenSSL and Botan join in their increments.**
A key pair made from its 32-byte scalar (`make`, refused before BearSSL when
0 or not below n, by a constant-time check whose verdict alone is public) or
drawn (`generate`), its public point computed once; a `public_key` validated
once where it arrives, SEC 1's uncompressed point or its two coordinates;
ECDSA over a SHA-256 digest with RFC 6979's nonce, raw (WebCrypto's form)
and strict DER; ECDH's raw x, as WebCrypto's `deriveBits` gives it. BearSSL
allocates nothing; natively it runs `br_ec_p256_m62` where BearSSL has a
128-bit product, on WebAssembly `br_ec_p256_m31` always. BearSSL's own
stack frames keep what they compute from the scalar, which webcpp cannot
wipe; the page says so.
Source: `libraries/crypto/openspec/changes/foundation/design.md` D36
(proposals); `include/webcpp/crypto/curves/p256.hpp` and
`backends/bearssl.hpp` (libs/crypto).
- **The key formats are fixed layouts: SPKI of 91 bytes (RFC 5480, the
uncompressed point) and PKCS#8 of 138 bytes (version 0, RFC 5915's
ECPrivateKey version 1 without parameters and with the public key); a
decoder accepts exactly what the encoder writes, the point checked against
the scalar.** The bytes OpenSSL's and BearSSL's encoders and node's
WebCrypto write, checked by the oracles; anything else is
`invalid_encoding`.
Source: `include/webcpp/crypto/curves/p256/formats.hpp` (libs/crypto).
- **Low S is two primitives, not a mode: `sign_ecdsa` returns S as computed
and `verify_ecdsa` accepts both forms, as FIPS 186-5 and WebCrypto do;
`normalize_ecdsa` gives the low form and `check_low_s` refuses the high
one, neither calling a backend.** A protocol that requires low S
(Trystero's handshakes) applies them; the protocol itself is
webcpp::trystero's (D24), which the guide's section on signed handshakes
shows with an example. Their verdict is marked public for the constant-time
checkers, a signature being public once made.
Source: `include/webcpp/crypto/curves/p256.hpp`, `doc/p2p.adoc`
(libs/crypto).
- **Constant time, sizes and throughput cover the family.** memcheck and
MemorySanitizer run the key pair's making, signing with its low-S
normalization and check, `ecdh_x` and `decode_pkcs8` (`paths.hpp`), each
checker failing on a planted early exit in a copy of the scalar check;
BearSSL's own branches on its signing's verdicts are listed for memcheck in
`bearssl_verdicts.supp`. dudect times `sign_ecdsa` and `ecdh_x` against the
scalar natively and on wasip2. The sizes lane measures signing,
verification and `ecdh_x`, and the throughput page the same three, from
release builds. The checkers ran on 2026-11-21 in an arm64 Linux container
(`linux-lint:arm64`, clang 18, Valgrind 3.22), the lane's CI command: both
found upstream BearSSL's signing branching on the RFC 6979 nonce's bit
length, `br_ecdsa_i31_bits2int` decoding it with its actual length and
`br_i31_rshift` looping by it (a Minerva-class leak), and nothing once
webcpp's BearSSL carried the fix, one commit setting the order's length
before the shift (chapter 5 rule 1); the planted programs failed and dudect
passed there too.
Source: `libs/crypto/test/constant_time/Jamfile`, `test/sizes/Jamfile`,
`bench/throughput.cpp`, `doc/backends.adoc` (libs/crypto);
https://github.com/webcpporg/bearssl/commit/32381d6e787a32cbb7a687246349200d78b8ff7c
(the fix) and BearSSL's `src/ec/ecdsa_i31_bits.c` at `7bea48e` (upstream).

**Errors and exceptions.**

- **Every operation that can fail returns `boost::system::result<T>` or
Expand All @@ -7165,7 +7231,8 @@ and not built is in chapter 17.
measured, a port is a new thing to review.
Source: `libraries/crypto/openspec/changes/genesis/design.md` D1
(proposals).
- **BearSSL comes unmodified from webcpp's mirror `webcpporg/bearssl`; its
- **BearSSL comes from webcpp's mirror `webcpporg/bearssl`, upstream's code
with one security fix on its branch `webcpp` (chapter 5 rule 1); its
notice ships as `LICENSE-BEARSSL.txt`, and webcpp's canary lives in the
library's `src/`. The backend names its code itself: the CTR class of
AES-NI, then POWER8, then `aes_ct64`, and GHASH by PCLMUL, then POWER8,
Expand Down Expand Up @@ -8724,6 +8791,25 @@ increments 1 and 2, whose state is first.
from release builds through `webcpp.program-sizes`.
Source: `libs/crypto/test/constant_time/Jamfile` and `test/sizes/Jamfile`.

**The foundation's increment 3 (plan Tasks 3.1 to 3.11; its P-256 part,
Tasks 3.1 to 3.6, built on its branch, merged 2026-11-22).**

- **P-256 on BearSSL is built on the branch `crypto/p256` of the library and
the superproject, for webcpp::trystero's port of Trystero 0.27.0 (design
D36): the family, its formats, its oracles (OpenSSL natively and on WASI,
node's WebCrypto on Emscripten), its constant-time paths, sizes and
throughput, and the guide's signed handshakes.** Chapter 15 holds what is
built. NIST's ACVP vectors wait for the `acvp` submodule of Task 3.8, and
the WebCrypto oracle runs in node, Chrome with the browser lane's increment.
Source: `libraries/crypto/openspec/changes/foundation/reference/plan.md`
Tasks 3.1 to 3.6 (proposals).
- **The rest of increment 3 is outlined, each task written in full when
P-256 has merged: P-384 and P-521 on the same engine (3.7), X25519 on
BearSSL and the `acvp` submodule (3.8), AES-CTR, AES-CBC, AES-CCM and
AES-EAX on BearSSL (3.9), RSA (3.10), and the increment's close (3.11).**
Source: `libraries/crypto/openspec/changes/foundation/reference/plan.md`
Tasks 3.7 to 3.11 (proposals).

**Constant time (increment 1, Task 14; merged).**

- **Constant time is checked deterministically on Linux x86-64 and arm64
Expand Down
2 changes: 1 addition & 1 deletion libs/crypto
Submodule crypto updated 102 files
18 changes: 10 additions & 8 deletions third_party/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,16 @@ rule crypto-checked-out ( properties * )
.c-library = $(.c-sections) <conditional>@crypto-checked-out
<include>$(CRYPTO)/include ;

# BearSSL (MIT), from webcpp's unmodified mirror webcpporg/bearssl. Every .c
# of src/, the POWER8 ones too, which off POWER compile to the stubs other
# files call. One source of entropy, getentropy, with RDRAND and /dev/urandom
# off (libs/crypto's random_device reads the same source): wasi-libc and
# Emscripten declare getentropy only under _DEFAULT_SOURCE, and macOS only in
# <sys/random.h>, which src/rand/sysrng.c does not include. On Windows
# BearSSL's only seeder is CryptGenRandom, not the system's BCryptGenRandom,
# so every seeder is off there and BearSSL has none.
# BearSSL (MIT), from webcpp's fork webcpporg/bearssl, branch webcpp: upstream
# 7bea48e with one commit, ECDSA's nonce decoded at its full length so that
# signing does not branch on its bit length (CODING-GUIDELINES chapter 5,
# rule 1). Every .c of src/, the POWER8 ones too, which off POWER compile to
# the stubs other files call. One source of entropy, getentropy, with RDRAND
# and /dev/urandom off (libs/crypto's random_device reads the same source):
# wasi-libc and Emscripten declare getentropy only under _DEFAULT_SOURCE, and
# macOS only in <sys/random.h>, which src/rand/sysrng.c does not include. On
# Windows BearSSL's only seeder is CryptGenRandom, not the system's
# BCryptGenRandom, so every seeder is off there and BearSSL has none.
path-constant BEARSSL : libs/bearssl ;

rule bearssl-checked-out ( properties * )
Expand Down
2 changes: 1 addition & 1 deletion third_party/libs/bearssl
42 changes: 41 additions & 1 deletion tools/test/crypto_collect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
directory, b2's bench/<toolset>/<variant>/..., names another variant, or does not say, is refused
before anything runs or is written, the guard's `--timing` program as the throughput's; one at
variant=release is taken (it gets as far as running); a cell whose operation is not the one
doc/tables/crypto.json states for its family is refused; `--timing` alone stores the guard's cost
doc/tables/crypto.json states for its family is refused; a further operation the program lists
after its family and backend is measured by that name, stated as the family's "sized_operations"
entry and rendered after the family's fixed operation, and one crypto.json does not state is
refused; `--timing` alone stores the guard's cost
beside the data's throughput, which it keeps, and the page renders it, with UTC's date; `--timing`
without data is refused with a message; `--provisional` marks each part, the throughput and the
guard, on its own, numbers unchanged, a measurement clears only the mark of the part it measured,
Expand Down Expand Up @@ -100,6 +103,42 @@ def test_an_operation_crypto_json_does_not_state_is_refused(scratch: Path) -> No
raise AssertionError('a file was written')


def test_a_further_operation_is_measured_by_its_name(scratch: Path) -> None:
def line(operation: str, median: float) -> str:
return json.dumps({'family': 'p256', 'backend': 'bearssl', 'operation': operation,
'target': 'native', 'compiler': 'clang', 'repetitions': 1,
'runs_us': [median], 'median_us': median})
fixed = line('ECDSA signature of a 32-byte SHA-256 digest', 20.0)
further = line("x coordinate of the ECDH shared point with a peer's public key", 40.0)
# The stand-in answers the further operation only when it is named after the backend; its
# lines are files, since the operation's text holds a quote.
(scratch / 'fixed.json').write_text(fixed + '\n')
(scratch / 'further.json').write_text(further + '\n')
exe = program(scratch, 'release',
'if [ "$1" = --list ]; then printf "p256 bearssl ecdh_x\\np256 bearssl\\n"; '
f'elif [ "$3" = ecdh_x ]; then cat "{scratch}/further.json"; '
f'elif [ "$3" = --runs ]; then cat "{scratch}/fixed.json"; else exit 3; fi\n')
data, page = scratch / 'data.json', scratch / 'page.adoc'
done = run('--exe', f'native={exe}', '--invocations', '1', '--data', data, '--page', page)
expect(done, 0)
measured = {case['operation']: case['median_us']
for case in json.loads(data.read_text())['cases']}
if measured != {'ECDSA signature of a 32-byte SHA-256 digest': 20.0,
"x coordinate of the ECDH shared point with a peer's public key": 40.0}:
raise AssertionError(f'measured {measured}')
text = page.read_text()
first = text.index('| P-256: ECDSA signature of a 32-byte SHA-256 digest')
second = text.index("| P-256: x coordinate of the ECDH shared point with a peer's public key")
if first > second:
raise AssertionError('the further operation is rendered before the fixed one')
unknown = program(scratch / 'unknown', 'release',
'if [ "$1" = --list ]; then echo "p256 bearssl recover"; else exit 3; fi\n')
done = run('--exe', f'native={unknown}', '--invocations', '1', '--data',
scratch / 'other.json', '--page', scratch / 'other.adoc')
expect(done, 1, "native lists p256 bearssl recover, and crypto.json's family p256 has no "
'sized operation recover')


def test_a_timing_program_not_built_at_variant_release_is_refused(scratch: Path) -> None:
data, page = scratch / 'data.json', scratch / 'page.adoc'
data.write_text(DATA.read_text())
Expand Down Expand Up @@ -382,6 +421,7 @@ def test_the_page_states_the_load_and_warns_above_half_the_cores(scratch: Path)
test_a_program_not_built_at_variant_release_is_refused,
test_a_program_built_at_variant_release_is_measured,
test_an_operation_crypto_json_does_not_state_is_refused,
test_a_further_operation_is_measured_by_its_name,
test_a_timing_program_not_built_at_variant_release_is_refused,
test_the_guard_is_measured_kept_beside_the_throughput_and_rendered,
test_the_throughput_measured_alone_keeps_the_guards_mark,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ build: fails
# tag::messages[]
[native-darwin]
contains: Undefined symbols for architecture
contains: _webcpp_crypto_bearssl_7bea48e_getentropy__add_third_party_bearssl_to_your_link
contains: _webcpp_crypto_bearssl_32381d6_getentropy__add_third_party_bearssl_to_your_link
[native-linux]
contains: webcpp_crypto_bearssl_7bea48e_getentropy__add_third_party_bearssl_to_your_link
contains: undefined reference to `webcpp_crypto_bearssl_7bea48e_getentropy__add_third_party_bearssl_to_your_link'
contains: webcpp_crypto_bearssl_32381d6_getentropy__add_third_party_bearssl_to_your_link
contains: undefined reference to `webcpp_crypto_bearssl_32381d6_getentropy__add_third_party_bearssl_to_your_link'
[wasip2]
contains: wasm-ld: error:
contains: undefined symbol: webcpp_crypto_bearssl_7bea48e_getentropy__add_third_party_bearssl_to_your_link
contains: undefined symbol: webcpp_crypto_bearssl_32381d6_getentropy__add_third_party_bearssl_to_your_link
[wasip3]
contains: wasm-ld: error:
contains: undefined symbol: webcpp_crypto_bearssl_7bea48e_getentropy__add_third_party_bearssl_to_your_link
contains: undefined symbol: webcpp_crypto_bearssl_32381d6_getentropy__add_third_party_bearssl_to_your_link
[emscripten]
contains: wasm-ld: error:
contains: undefined symbol: webcpp_crypto_bearssl_7bea48e_getentropy__add_third_party_bearssl_to_your_link
contains: undefined symbol: webcpp_crypto_bearssl_32381d6_getentropy__add_third_party_bearssl_to_your_link
contains: em++: error:
# end::messages[]
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ build: fails
# tag::messages[]
[native-darwin]
contains: Undefined symbols for architecture
contains: _webcpp_crypto_bearssl_7bea48e_sysrng__add_third_party_bearssl_to_your_link
contains: _webcpp_crypto_bearssl_32381d6_sysrng__add_third_party_bearssl_to_your_link
[native-linux]
contains: webcpp_crypto_bearssl_7bea48e_sysrng__add_third_party_bearssl_to_your_link
contains: undefined reference to `webcpp_crypto_bearssl_7bea48e_sysrng__add_third_party_bearssl_to_your_link'
contains: webcpp_crypto_bearssl_32381d6_sysrng__add_third_party_bearssl_to_your_link
contains: undefined reference to `webcpp_crypto_bearssl_32381d6_sysrng__add_third_party_bearssl_to_your_link'
[wasip2]
contains: wasm-ld: error:
contains: undefined symbol: webcpp_crypto_bearssl_7bea48e_sysrng__add_third_party_bearssl_to_your_link
contains: undefined symbol: webcpp_crypto_bearssl_32381d6_sysrng__add_third_party_bearssl_to_your_link
[wasip3]
contains: wasm-ld: error:
contains: undefined symbol: webcpp_crypto_bearssl_7bea48e_sysrng__add_third_party_bearssl_to_your_link
contains: undefined symbol: webcpp_crypto_bearssl_32381d6_sysrng__add_third_party_bearssl_to_your_link
[emscripten]
contains: wasm-ld: error:
contains: undefined symbol: webcpp_crypto_bearssl_7bea48e_sysrng__add_third_party_bearssl_to_your_link
contains: undefined symbol: webcpp_crypto_bearssl_32381d6_sysrng__add_third_party_bearssl_to_your_link
contains: em++: error:
# end::messages[]
Loading
Loading