From aaefe0464b0aadf3881edc96d8d39cb3e5818861 Mon Sep 17 00:00:00 2001 From: R Date: Fri, 20 Nov 2026 06:25:47 -0300 Subject: [PATCH] crypto: dudect's classes differ in the secret's bytes alone, and a runner GitHub hosts prints dudect's |t| without judging it The CI's x86-64 runs of the lane constant-time failed dudect_bearssl: equal at |t| 21 to 25 and once ChaCha20-Poly1305's seal at 10.7. clang 18's x86-64 code of equal and of BearSSL's SSE2 ChaCha20 and ctmulq Poly1305 has no branch or index on a secret, and memcheck passed on it. The cause was the harness: choose copied class 0's input from the fixed array and class 1's from bytes just drawn, still in the store buffer, and the measured call inherited that difference whatever it computed. Measured on GitHub's runners (2026-11-20): equal at |t| 31 to 56 on an Intel Xeon 6975P-C and the seal at 28 to 85 on AMD EPYC 9V45 and 9V74 with that copy; through a blend that reads both sources and writes the secret with the same instructions for either class, 23 runs of 24 below 4.4 on the Xeon, an EPYC 9V74 and an EPYC 7763, idle and with every processor busy. The 24th read 69.8 on a case below 4.4 everywhere else: a hosted runner is a shared virtual machine whose processor changes from run to run, and a verdict there would pass or fail with the machine drawn. There the Jamfile defines WEBCPP_TEST_DUDECT_UNJUDGED (RUNNER_ENVIRONMENT is github-hosted), and dudect_bearssl, dudect_sodium and dudect_secp256k1 print their |t| and pass; memcheck, MemorySanitizer and dudect_planted stay judged, and dudect is judged on any other machine. The constant-time section and the page's checker name say so. --- doc/backends.adoc | 12 ++++ doc/generated/constant-time.adoc | 20 +++--- doc/generated/namespace-ciphers.adoc | 6 +- doc/generated/namespace-curves.adoc | 6 +- doc/llms.txt | 12 ++-- doc/tables/crypto.json | 2 +- doc/testing.adoc | 7 +- test/constant_time/Jamfile | 19 ++++-- test/constant_time/dudect.hpp | 70 +++++++++++++++++--- test/constant_time/dudect_secp256k1_test.cpp | 2 +- test/constant_time/dudect_sodium_test.cpp | 2 +- test/constant_time/dudect_test.cpp | 6 +- 12 files changed, 116 insertions(+), 48 deletions(-) diff --git a/doc/backends.adoc b/doc/backends.adoc index 28f7ac31..ae81ba4d 100644 --- a/doc/backends.adoc +++ b/doc/backends.adoc @@ -122,6 +122,18 @@ experiment and not the lane, the case passes. The WebAssembly cannot set it, so the lane prints its value on wasip2 without judging it, and judges everything else. +dudect's two classes differ in the secret's bytes and in nothing else: the +preparation of each measurement reads both inputs and writes the secret with +the same instructions whatever the class, since anything a class does +differently before the measured call shows in its time. On a runner GitHub +hosts, a virtual machine shared with other tenants whose processor changes from +run to run, the lane prints each dudect |t| and judges none but +`dudect_planted`'s: measured there on 2026-11-20 on three processors, 23 runs of +24 stayed below 4.4 and one read 69.8, on both seeds, on a case below 4.4 in +every other run, so a verdict there would pass or fail with the machine drawn. +Valgrind's memcheck and MemorySanitizer, which do not measure time, are judged +there; dudect is judged wherever else the lane runs, on a machine of one's own. + Whether a tag matched, or a shared secret came out all zero, is public by design, since the operation reports it, but it is computed from the secrets: it is the one kind of value webcpp marks public, where webcpp's code first diff --git a/doc/generated/constant-time.adoc b/doc/generated/constant-time.adoc index 8d6f31fe..5cc30c44 100644 --- a/doc/generated/constant-time.adoc +++ b/doc/generated/constant-time.adoc @@ -13,8 +13,8 @@ |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; 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; MemorySanitizer (Linux, clang): AES-GCM, ChaCha20-Poly1305; dudect: AES-GCM, ChaCha20-Poly1305 -|dudect: AES-GCM, ChaCha20-Poly1305 +|Valgrind's memcheck (Linux): AES-GCM, ChaCha20-Poly1305; MemorySanitizer (Linux, clang): AES-GCM, ChaCha20-Poly1305; dudect (judged off the runners GitHub hosts): AES-GCM, ChaCha20-Poly1305 +|dudect (judged off the runners GitHub hosts): AES-GCM, ChaCha20-Poly1305 |not measured yet |not measured yet @@ -27,22 +27,22 @@ |ctaes |by design: bitsliced, no table and no branch on secret data; link:https://github.com/bitcoin/bitcoin/tree/master/src/crypto/ctaes[ctaes, constant-time AES] -|Valgrind's memcheck (Linux): AES block cipher; MemorySanitizer (Linux, clang): AES block cipher; dudect: AES block cipher -|dudect: AES block cipher +|Valgrind's memcheck (Linux): AES block cipher; MemorySanitizer (Linux, clang): AES block cipher; dudect (judged off the runners GitHub hosts): AES block cipher +|dudect (judged off the runners GitHub hosts): AES block cipher |not measured yet |not measured yet |libsecp256k1 |by design: signing and key generation are constant time and constant memory access; link:https://github.com/bitcoin-core/secp256k1#readme[libsecp256k1's README] -|Valgrind's memcheck (Linux): secp256k1 (libsecp256k1's own `+ctime_tests.c+`, and webcpp's code around it: the key pair's making, its signatures and x-only tweak, ECDH in three forms, the secret key's tweaks and negation, and PKCS#8), ElligatorSwift (libsecp256k1's own `+ctime_tests.c+`, and webcpp's `+ellswift_create+` and `+ellswift_xdh+` around it), MuSig2 (libsecp256k1's own `+ctime_tests.c+`, and webcpp's `+nonce_gen+` and `+partial_sign+` around it), BIP 352 silent payments (libsecp256k1's own `+ctime_tests.c+`, and webcpp's `+create_outputs+`, `+create_label+` and `+scan_outputs+` around it, each workspace's wipe included); MemorySanitizer (Linux, clang): secp256k1 (the same paths as memcheck's), ElligatorSwift (the same paths as memcheck's), MuSig2 (the same paths as memcheck's), BIP 352 silent payments (the same paths as memcheck's); dudect: secp256k1 (`+ecdh_x+`) -|dudect: secp256k1 (`+ecdh_x+`) +|Valgrind's memcheck (Linux): secp256k1 (libsecp256k1's own `+ctime_tests.c+`, and webcpp's code around it: the key pair's making, its signatures and x-only tweak, ECDH in three forms, the secret key's tweaks and negation, and PKCS#8), ElligatorSwift (libsecp256k1's own `+ctime_tests.c+`, and webcpp's `+ellswift_create+` and `+ellswift_xdh+` around it), MuSig2 (libsecp256k1's own `+ctime_tests.c+`, and webcpp's `+nonce_gen+` and `+partial_sign+` around it), BIP 352 silent payments (libsecp256k1's own `+ctime_tests.c+`, and webcpp's `+create_outputs+`, `+create_label+` and `+scan_outputs+` around it, each workspace's wipe included); MemorySanitizer (Linux, clang): secp256k1 (the same paths as memcheck's), ElligatorSwift (the same paths as memcheck's), MuSig2 (the same paths as memcheck's), BIP 352 silent payments (the same paths as memcheck's); dudect (judged off the runners GitHub hosts): secp256k1 (`+ecdh_x+`) +|dudect (judged off the runners GitHub hosts): secp256k1 (`+ecdh_x+`) |not measured yet |not measured yet |libsodium |by its documentation for the paths crypto calls, without a guarantee for the whole library: secrets are compared in constant time (link:https://doc.libsodium.org/internals#branches[libsodium's documentation, internals: branches]), ChaCha20 is not sensitive to timing attacks (link:https://doc.libsodium.org/secret-key_cryptography/aead[libsodium's documentation, AEAD constructions: ChaCha20 and timing attacks]), and Ed25519 and X25519 come from designs with no secret branch and no secret array index (link:https://ed25519.cr.yp.to/[Ed25519's design: no secret branch conditions, no secret array indices]); webcpp's lane checks each path it calls -|Valgrind's memcheck (Linux): Ed25519, X25519, crypto_kx, ChaCha20-Poly1305, original ChaCha20-Poly1305, XChaCha20-Poly1305, XSalsa20-Poly1305 secret box, XChaCha20-Poly1305 secret box, X25519-XSalsa20-Poly1305 box, X25519-XChaCha20-Poly1305 box, X25519-XSalsa20-Poly1305 sealed box; MemorySanitizer (Linux, clang): Ed25519, X25519, crypto_kx, ChaCha20-Poly1305, original ChaCha20-Poly1305, XChaCha20-Poly1305, XSalsa20-Poly1305 secret box, XChaCha20-Poly1305 secret box, X25519-XSalsa20-Poly1305 box, X25519-XChaCha20-Poly1305 box, X25519-XSalsa20-Poly1305 sealed box; dudect: Ed25519, X25519 (X25519 measured differently, below) -|dudect: Ed25519, X25519 (X25519 measured differently, below) +|Valgrind's memcheck (Linux): Ed25519, X25519, crypto_kx, ChaCha20-Poly1305, original ChaCha20-Poly1305, XChaCha20-Poly1305, XSalsa20-Poly1305 secret box, XChaCha20-Poly1305 secret box, X25519-XSalsa20-Poly1305 box, X25519-XChaCha20-Poly1305 box, X25519-XSalsa20-Poly1305 sealed box; MemorySanitizer (Linux, clang): Ed25519, X25519, crypto_kx, ChaCha20-Poly1305, original ChaCha20-Poly1305, XChaCha20-Poly1305, XSalsa20-Poly1305 secret box, XChaCha20-Poly1305 secret box, X25519-XSalsa20-Poly1305 box, X25519-XChaCha20-Poly1305 box, X25519-XSalsa20-Poly1305 sealed box; dudect (judged off the runners GitHub hosts): Ed25519, X25519 (X25519 measured differently, below) +|dudect (judged off the runners GitHub hosts): Ed25519, X25519 (X25519 measured differently, below) |not measured yet |not measured yet @@ -55,8 +55,8 @@ |no backend |`+equal+` is constant time in the contents: every byte is read, no branch and no index depends on one -|Valgrind's memcheck (Linux): `+equal+`, `+is_all_zero+` and the wipe of `+secret+`; MemorySanitizer (Linux, clang): `+equal+`, `+is_all_zero+` and the wipe of `+secret+`; dudect: `+equal+` -|dudect: `+equal+` +|Valgrind's memcheck (Linux): `+equal+`, `+is_all_zero+` and the wipe of `+secret+`; MemorySanitizer (Linux, clang): `+equal+`, `+is_all_zero+` and the wipe of `+secret+`; dudect (judged off the runners GitHub hosts): `+equal+` +|dudect (judged off the runners GitHub hosts): `+equal+` |not measured yet |not measured yet |=== diff --git a/doc/generated/namespace-ciphers.adoc b/doc/generated/namespace-ciphers.adoc index bad0ad16..ebd8ee24 100644 --- a/doc/generated/namespace-ciphers.adoc +++ b/doc/generated/namespace-ciphers.adoc @@ -41,8 +41,8 @@ |ctaes: yes (link:https://github.com/bitcoin/bitcoin/tree/master/src/crypto/ctaes[ctaes, constant-time AES]); OpenSSL with assembly: yes (link:https://github.com/openssl/openssl/tree/master/crypto/aes/asm[OpenSSL's AES assembly for ARMv8 and x86-64]); OpenSSL no-asm: no (link:https://github.com/openssl/openssl/blob/master/crypto/aes/aes_core.c[OpenSSL's crypto/aes/aes_core.c, OPENSSL_AES_CONST_TIME]) |Constant time, measured -|BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect, nothing found; wasip2, dudect, nothing found; not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3 (<>) -|OpenSSL: not measured yet. ctaes: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect, nothing found; wasip2, dudect, nothing found; not measured yet on wasip3, emscripten (<>) +|BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3 (<>) +|OpenSSL: not measured yet. ctaes: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten (<>) |Standards |link:https://csrc.nist.gov/pubs/sp/800/38/d/final[NIST SP 800-38D, GCM and GMAC] @@ -102,7 +102,7 @@ |libsodium: yes (link:https://doc.libsodium.org/secret-key_cryptography/aead[libsodium's documentation, AEAD constructions: ChaCha20 and timing attacks]) |Constant time, measured -|libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect, nothing found; wasip2, dudect, nothing found; not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3 (<>) +|libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3 (<>) |libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten (<>) |libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten (<>) diff --git a/doc/generated/namespace-curves.adoc b/doc/generated/namespace-curves.adoc index a1abcebb..63d39838 100644 --- a/doc/generated/namespace-curves.adoc +++ b/doc/generated/namespace-curves.adoc @@ -37,9 +37,9 @@ |agreement of a secret key and a public key: native, libsodium 15.9 µs; wasip2, libsodium 40.4 µs; wasip3, libsodium 40.4 µs; emscripten, libsodium 35.5 µs (2026-11-19, Apple M5 Max; <>) |Constant time, measured -|libsecp256k1: native, Valgrind's memcheck (Linux; libsecp256k1's own `+ctime_tests.c+`, and webcpp's code around it: the key pair's making, its signatures and x-only tweak, ECDH in three forms, the secret key's tweaks and negation, and PKCS#8), MemorySanitizer (Linux, clang; the same paths as memcheck's) and dudect (`+ecdh_x+`), nothing found; wasip2, dudect (`+ecdh_x+`), nothing found; not measured yet on wasip3, emscripten (<>) -|libsodium: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect, nothing found; wasip2, dudect, nothing found; not measured yet on wasip3, emscripten (<>) -|libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found, and dudect: nothing found with arm64's DIT (data-independent timing) bit set, as the lane runs dudect on macOS and Linux on arm64 (elsewhere its harness sets no DIT) and as Apple requires of constant-time code (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]); without it, dudect times `agree` with an all-zero secret key against random ones at \|t\| 6.7 to 14.5, against the threshold 10, on an Apple M5 Max, from a release build (2026-11-19): about one nanosecond in 15 µs. libsodium does not set DIT: a program on Apple silicon sets it around its secret operations (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]); wasip2, dudect: an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at \|t\| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads \|t\| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next; not measured yet on wasip3, emscripten (<>) +|libsecp256k1: native, Valgrind's memcheck (Linux; libsecp256k1's own `+ctime_tests.c+`, and webcpp's code around it: the key pair's making, its signatures and x-only tweak, ECDH in three forms, the secret key's tweaks and negation, and PKCS#8), MemorySanitizer (Linux, clang; the same paths as memcheck's) and dudect (judged off the runners GitHub hosts; `+ecdh_x+`), nothing found; wasip2, dudect (judged off the runners GitHub hosts; `+ecdh_x+`), nothing found; not measured yet on wasip3, emscripten (<>) +|libsodium: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten (<>) +|libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found, and dudect (judged off the runners GitHub hosts): nothing found with arm64's DIT (data-independent timing) bit set, as the lane runs dudect on macOS and Linux on arm64 (elsewhere its harness sets no DIT) and as Apple requires of constant-time code (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]); without it, dudect times `agree` with an all-zero secret key against random ones at \|t\| 6.7 to 14.5, against the threshold 10, on an Apple M5 Max, from a release build (2026-11-19): about one nanosecond in 15 µs. libsodium does not set DIT: a program on Apple silicon sets it around its secret operations (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]); wasip2, dudect (judged off the runners GitHub hosts): an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at \|t\| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads \|t\| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next; not measured yet on wasip3, emscripten (<>) |Standards |link:https://www.secg.org/sec2-v2.pdf[SEC 2, Recommended Elliptic Curve Domain Parameters], link:https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki[BIP 340, Schnorr signatures for secp256k1], link:https://www.rfc-editor.org/rfc/rfc6979[RFC 6979, deterministic ECDSA nonces], link:https://github.com/bitcoin/bips/blob/master/bip-0324.mediawiki[BIP 324, version 2 P2P encrypted transport protocol], link:https://github.com/bitcoin/bips/blob/master/bip-0327.mediawiki[BIP 327, MuSig2 for BIP340-compatible multi-signatures], link:https://github.com/bitcoin/bips/blob/master/bip-0352.mediawiki[BIP 352, Silent Payments] diff --git a/doc/llms.txt b/doc/llms.txt index a25acfff..d4a3061d 100644 --- a/doc/llms.txt +++ b/doc/llms.txt @@ -210,14 +210,14 @@ Generated by doc/tables/generate.py with the page's tables. Page: https://webcpp ## Constant time, measured -- AES-GCM: BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect, nothing found; wasip2, dudect, nothing found; not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3. -- AES block cipher: OpenSSL: not measured yet. ctaes: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect, nothing found; wasip2, dudect, nothing found; not measured yet on wasip3, emscripten. -- secp256k1: libsecp256k1: native, Valgrind's memcheck (Linux; libsecp256k1's own `ctime_tests.c`, and webcpp's code around it: the key pair's making, its signatures and x-only tweak, ECDH in three forms, the secret key's tweaks and negation, and PKCS#8), MemorySanitizer (Linux, clang; the same paths as memcheck's) and dudect (`ecdh_x`), nothing found; wasip2, dudect (`ecdh_x`), nothing found; not measured yet on wasip3, emscripten. -- Ed25519: libsodium: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect, nothing found; wasip2, dudect, nothing found; not measured yet on wasip3, emscripten. -- X25519: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found, and dudect: nothing found with arm64's DIT (data-independent timing) bit set, as the lane runs dudect on macOS and Linux on arm64 (elsewhere its harness sets no DIT) and as Apple requires of constant-time code ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)); without it, dudect times `agree` with an all-zero secret key against random ones at |t| 6.7 to 14.5, against the threshold 10, on an Apple M5 Max, from a release build (2026-11-19): about one nanosecond in 15 µs. libsodium does not set DIT: a program on Apple silicon sets it around its secret operations ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)); wasip2, dudect: an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at |t| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads |t| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next; not measured yet on wasip3, emscripten. +- AES-GCM: BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3. +- AES block cipher: OpenSSL: not measured yet. ctaes: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten. +- secp256k1: libsecp256k1: native, Valgrind's memcheck (Linux; libsecp256k1's own `ctime_tests.c`, and webcpp's code around it: the key pair's making, its signatures and x-only tweak, ECDH in three forms, the secret key's tweaks and negation, and PKCS#8), MemorySanitizer (Linux, clang; the same paths as memcheck's) and dudect (judged off the runners GitHub hosts; `ecdh_x`), nothing found; wasip2, dudect (judged off the runners GitHub hosts; `ecdh_x`), nothing found; not measured yet on wasip3, emscripten. +- Ed25519: libsodium: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten. +- X25519: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found, and dudect (judged off the runners GitHub hosts): nothing found with arm64's DIT (data-independent timing) bit set, as the lane runs dudect on macOS and Linux on arm64 (elsewhere its harness sets no DIT) and as Apple requires of constant-time code ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)); without it, dudect times `agree` with an all-zero secret key against random ones at |t| 6.7 to 14.5, against the threshold 10, on an Apple M5 Max, from a release build (2026-11-19): about one nanosecond in 15 µs. libsodium does not set DIT: a program on Apple silicon sets it around its secret operations ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)); wasip2, dudect (judged off the runners GitHub hosts): an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at |t| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads |t| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next; not measured yet on wasip3, emscripten. - crypto_kx: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - PEM: libsodium: not measured yet. -- ChaCha20-Poly1305: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect, nothing found; wasip2, dudect, nothing found; not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3. +- ChaCha20-Poly1305: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3. - original ChaCha20-Poly1305: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - XChaCha20-Poly1305: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - XSalsa20-Poly1305 secret box: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. diff --git a/doc/tables/crypto.json b/doc/tables/crypto.json index 92520917..e9fb413d 100644 --- a/doc/tables/crypto.json +++ b/doc/tables/crypto.json @@ -4,7 +4,7 @@ "llms": "Cryptographic primitives for C++20 on native, wasip2, wasip3 and Emscripten: AES-GCM, the AES block cipher, secp256k1 (BIP 340 Schnorr, ECDSA and recoverable signatures, ECDH, tweaks, ElligatorSwift, MuSig2, silent payments), Ed25519 and Ed25519ph (RFC 8032), X25519 (RFC 7748), their keys in SPKI, PKCS#8 (RFC 8410) and PEM, libsodium's crypto_kx, the ChaCha20-Poly1305 AEADs, libsodium's secret boxes, boxes and sealed boxes, a random_device on the system's generator and a constant-time equal, each over a widely used, reviewed C library (BearSSL, OpenSSL, ctaes, libsecp256k1, libsodium) that the program names in its Jamfile. Hashing and HMAC are Boost.Hash2's; HKDF and password hashing are webcpp::hash's. Every failure is a boost::system::result value. Protocols (Bitcoin, Ethereum, Nostr) belong to domain libraries built on these primitives.", "targets": ["native", "wasip2", "wasip3", "emscripten"], "compilers": {"clang": "Clang", "gcc": "GCC", "msvc": "MSVC"}, - "checkers": {"memcheck": "Valgrind's memcheck (Linux)", "msan": "MemorySanitizer (Linux, clang)", "dudect": "dudect"}, + "checkers": {"memcheck": "Valgrind's memcheck (Linux)", "msan": "MemorySanitizer (Linux, clang)", "dudect": "dudect (judged off the runners GitHub hosts)"}, "sources": { "fips197": ["FIPS 197, the Advanced Encryption Standard", "https://csrc.nist.gov/pubs/fips/197/final"], "sp800-38d": ["NIST SP 800-38D, GCM and GMAC", "https://csrc.nist.gov/pubs/sp/800/38/d/final"], diff --git a/doc/testing.adoc b/doc/testing.adoc index 1be35eda..3584a2f3 100644 --- a/doc/testing.adoc +++ b/doc/testing.adoc @@ -44,9 +44,10 @@ those of crypto's operations among them (`sign`, `box`, `box2`, `box7`, PEM's engine calls), compared with its `.exp`. The own lane `constant-time` checks constant time (<>): natively, Valgrind and clang's MemorySanitizer on Linux and dudect's statistics on Linux and -macOS, and dudect's under wasmtime on wasip2; the native lane's command with -`libs/crypto/test/constant_time//constant-time` in place of the two -directories. +macOS, and dudect's under wasmtime on wasip2, whose |t| a runner GitHub hosts +prints without judging it (<> says why); the +native lane's command with `libs/crypto/test/constant_time//constant-time` in +place of the two directories. What the tests check: diff --git a/test/constant_time/Jamfile b/test/constant_time/Jamfile index 3814a1f0..2d0b4682 100644 --- a/test/constant_time/Jamfile +++ b/test/constant_time/Jamfile @@ -122,7 +122,7 @@ path-constant SODIUM : ../../../../third_party/libs/libsodium ; # The peek reads that project's private names: a rename or a respelling of any # of them there stops this build here, rather than build libsodium with part of # its configuration missing or with its inline assembly back in. -import errors modules project set ; +import errors modules os project set ; local third-party = [ project.find /third_party : . ] ; local sodium-peeked ; for local name in .sodium-common .sodium-posix .sodium-linux @@ -217,15 +217,24 @@ webcpp.run secp256k1_ctime_msan : $(SECP256K1_CTIME) # dudect's statistics on BearSSL, ctaes and equal, on libsodium's X25519 and # Ed25519, and on libsecp256k1's ecdh_x, natively and under wasmtime; under DIT -# on macOS and Linux on arm64 only (dudect.hpp), without it elsewhere. +# on macOS and Linux on arm64 only (dudect.hpp), without it elsewhere. On a +# runner GitHub hosts, a virtual machine shared with other tenants whose +# processor changes from run to run, each |t| is printed and not judged: +# there it moved with what the measurement cannot control (dudect.hpp, +# judged). dudect_planted is judged everywhere. +DUDECT = release ; +if [ os.environ RUNNER_ENVIRONMENT ] = github-hosted +{ + DUDECT += WEBCPP_TEST_DUDECT_UNJUDGED ; +} webcpp.run dudect_bearssl : dudect_test.cpp - : /third_party//bearssl /third_party//ctaes release + : /third_party//bearssl /third_party//ctaes $(DUDECT) : native wasip2 ; # On wasip2, X25519's agreement is measured and printed but not judged: an # open finding, whose numbers doc/tables/crypto.json holds (the X25519 # family's constant_time on wasip2) and the constant-time section renders. webcpp.run dudect_sodium : dudect_sodium_test.cpp - : /third_party//sodium release : native wasip2 ; + : /third_party//sodium $(DUDECT) : native wasip2 ; # The planted leak under dudect: X25519's agreement with an early-exit scan of # its secret key before it, a few nanoseconds longer for the all-zero key; the # program builds and runs only this case, not Ed25519's signing. It passes only @@ -238,7 +247,7 @@ webcpp.run dudect_planted : dudect_planted_test.cpp # libsecp256k1's ecdh_x, natively and under wasmtime. webcpp.run dudect_secp256k1 : dudect_secp256k1_test.cpp - : /third_party//secp256k1 release : native wasip2 ; + : /third_party//secp256k1 $(DUDECT) : native wasip2 ; webcpp.lane constant-time : ctgrind ctgrind_planted ctgrind_space msan msan_planted secp256k1_ctime secp256k1_ctime_msan dudect_bearssl diff --git a/test/constant_time/dudect.hpp b/test/constant_time/dudect.hpp index 4183c311..205f8515 100644 --- a/test/constant_time/dudect.hpp +++ b/test/constant_time/dudect.hpp @@ -222,17 +222,26 @@ inline double largest_t(std::vector const& timings, std::vector con } /** - * Draws `Size` bytes, then copies into `input` either `fixed`, for class 0, or the drawn bytes, - * for class 1. Both classes draw the same bytes and store the same way: the source is chosen - * through a volatile pointer, which the compiler cannot see through, so that it cannot split - * the copy by class into a store of constants for one and of drawn bytes for the other. Such - * a split, which an optimiser makes of `which == 0 ? 0 : drawn`, leaks the class into the - * measured call (measured natively on an Apple M5, 2026-11-18: |t| 16 to 18 on equal and 9 to - * 26 on open with the split, below 3 for both through this copy). + * Draws `Size` bytes, then writes into `input` either `fixed`, for class 0, or the drawn bytes, + * for class 1, so that the two classes differ in the bytes written and in nothing else. Both + * classes draw the same bytes, read both `fixed` and the drawn bytes, and write `input` with the + * same instructions: each byte is blended through a mask read from a volatile, which the + * compiler cannot see through, so that it can neither split the work by class nor read one + * source alone. Whatever a class does differently before the measured call leaks the class into + * it. A copy from a volatile pointer to the chosen source (until 2026-11-20) read the fixed + * input for class 0 and the bytes just drawn, still in the store buffer, for class 1: on the + * CI's x86-64 runners the measured call then differed by class whatever it computed (measured + * on GitHub's ubuntu-24.04 runners, 2026-11-20: equal at |t| 31 to 56 on an Intel Xeon 6975P-C + * and ChaCha20-Poly1305's seal at 28 to 85 on AMD EPYC 9V45 and 9V74; through this blend, on + * the Xeon, the EPYC 9V74 and an AMD EPYC 7763, idle and with every processor busy, every case + * of 23 runs of 24 stayed below 4.4, the 24th is in `judged`). Before it, + * a choice the optimiser made of `which == 0 ? 0 : drawn` split the copy into a store of + * constants and a store of drawn bytes (measured natively on an Apple M5, 2026-11-18: |t| 16 to + * 18 on equal and 9 to 26 on open with the split, below 3 for both without it). * @param which The class, 0 or 1. * @param generator The seeded generator. * @param fixed The class 0 input. - * @param input Where the input of the class is copied. + * @param input Where the input of the class is written. */ template void choose(int which, Generator& generator, std::array const& fixed, @@ -241,9 +250,13 @@ void choose(int which, Generator& generator, std::array cons for (auto& byte : drawn) { byte = static_cast(generator()); } - std::uint8_t const* volatile chosen = which == 0 ? fixed.data() : drawn.data(); - std::uint8_t const* const source = chosen; - std::copy(source, source + Size, input.begin()); + std::uint8_t volatile const chosen = which == 0 ? 0xFFU : 0x00U; + unsigned const keep = chosen; + unsigned const drop = keep ^ 0xFFU; + for (std::size_t index = 0; index < Size; ++index) { + input[index] = static_cast((unsigned{fixed[index]} & keep) | + (unsigned{drawn[index]} & drop)); + } } /** @@ -291,6 +304,41 @@ double twice(std::size_t count, Prepare&& prepare, Run&& run) { return first < 10 ? first : (std::min)(first, measure(20261012, count, prepare, run)); } +/** + * Whether this build's verdict is judged: false where the lane's Jamfile defines + * WEBCPP_TEST_DUDECT_UNJUDGED, on a runner GitHub hosts (RUNNER_ENVIRONMENT=github-hosted), true + * elsewhere. A hosted runner is a virtual machine shared with other tenants, whose processor + * changes from one run to the next (an Intel Xeon 6975P-C, an AMD EPYC 7763, 9V45 or 9V74 on + * 2026-11-20), and there |t| moved with what the measurement could not control: through the + * class-blind preparation of `choose`, one run of 24 read |t| 69.8 on ChaCha20-Poly1305's open, + * on both seeds, where the 23 others read below 4.4 (an AMD EPYC 7763, idle); through the + * earlier copy, a measured call that does nothing read up to 117 with every processor busy. A + * verdict there would pass or fail with the machine drawn, so it prints its |t| and passes; + * Valgrind's memcheck and MemorySanitizer, which do not measure time, are judged there. dudect + * is judged wherever else the lane runs, natively and on wasip2, on a machine of one's own; + * dudect_planted is judged everywhere, as it shows that the measurement still finds a leak of a + * few nanoseconds on the machine it ran on. + */ +#ifdef WEBCPP_TEST_DUDECT_UNJUDGED +inline constexpr bool judged = false; +#else +inline constexpr bool judged = true; +#endif + +/** + * The exit status of a dudect program: 0 when every case passes, else 1, where `judged`; 0 + * elsewhere, after a line that says so. + * @param passes Whether every case's |t| is below the threshold. + * @return The exit status. + */ +inline int verdict(bool passes) noexcept { + if (!judged) { + std::printf("printed, not judged: a runner GitHub hosts (dudect.hpp, judged)\n"); + return 0; + } + return passes ? 0 : 1; +} + } // namespace webcpp::test #endif diff --git a/test/constant_time/dudect_secp256k1_test.cpp b/test/constant_time/dudect_secp256k1_test.cpp index 4873f151..045063b2 100644 --- a/test/constant_time/dudect_secp256k1_test.cpp +++ b/test/constant_time/dudect_secp256k1_test.cpp @@ -64,5 +64,5 @@ int main() { [&] { static_cast(pair->ecdh_x(*peer_key)); }); std::printf("|t| secp256k1 ecdh_x %.2f (threshold 10)\n", agree_t); - return agree_t < 10 ? 0 : 1; + return webcpp::test::verdict(agree_t < 10); } diff --git a/test/constant_time/dudect_sodium_test.cpp b/test/constant_time/dudect_sodium_test.cpp index f4ee0436..56f33c70 100644 --- a/test/constant_time/dudect_sodium_test.cpp +++ b/test/constant_time/dudect_sodium_test.cpp @@ -95,6 +95,6 @@ int main() { #endif std::printf("|t| X25519 agreement %.2f%s, Ed25519 signing %.2f (threshold 10)\n", agree_t, agree_note, sign_t); - return agree_passes && sign_t < 10 ? 0 : 1; + return webcpp::test::verdict(agree_passes && sign_t < 10); #endif } diff --git a/test/constant_time/dudect_test.cpp b/test/constant_time/dudect_test.cpp index b5ac1349..8a2de5a7 100644 --- a/test/constant_time/dudect_test.cpp +++ b/test/constant_time/dudect_test.cpp @@ -121,8 +121,6 @@ int main() { "|t| seal %.2f, open %.2f, chacha seal %.2f, chacha open %.2f, ctaes %.2f, equal %.2f " "(threshold 10)\n", gcm.seal, gcm.open, chacha.seal, chacha.open, block_t, equal_t); - return gcm.seal < 10 && gcm.open < 10 && chacha.seal < 10 && chacha.open < 10 && block_t < 10 && - equal_t < 10 - ? 0 - : 1; + return webcpp::test::verdict(gcm.seal < 10 && gcm.open < 10 && chacha.seal < 10 && + chacha.open < 10 && block_t < 10 && equal_t < 10); }