diff --git a/AGENTS.md b/AGENTS.md index aa184ecd..0901329e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -40,15 +40,21 @@ specific to crypto. - An algorithm type does every guard; a backend's header supplies only the six traits of `detail/engines.hpp` (`block_state`, `block_engine`, `gcm_state`, `gcm_engine`, `aead_state`, - `aead_engine`) and, for the curves and `hybrid`, the functions of its own header. + `aead_engine`), the two of `detail/p_curves.hpp` per P-curve (`p_curve_state`, + `p_curve_engine`) and, for the other curves and `hybrid`, the functions of its own header. - A new public function that calls a backend holds `detail::timing_guard` as its first statement (it sets arm64's DIT for the call, `detail/timing.hpp`) and joins the coverage test of its backend, `test/timing_coverage__test.cpp`, which checks through the seam that each call entered its own guard. A function that only copies or encodes bytes, with no backend call, holds none. +- The constant-time lane's P-256 paths (`test/constant_time/paths.hpp`): BearSSL's ECDSA signing + branches on its own verdicts, listed for memcheck in `bearssl_verdicts.supp`, which a later + P-curve's signing joins; MemorySanitizer counts BearSSL's window selections public (masks of the + form `(q | -q) >> 31`), so the public point and `ecdh_x` are checked traced by memcheck alone. - A test of a backend is a `.cpp` of its own per backend, never a define that changes the backend of one source (spec section 4.3's pitfall). -- Every C library is unchanged upstream code. A moved pin is a change of its own - (CODING-GUIDELINES.md chapter 5, rule 12). +- Every C library is upstream code, unchanged except where CODING-GUIDELINES.md chapter 5 rule 1 + allows a security fix on the mirror's `webcpp` branch (BearSSL: 32381d6, ECDSA's nonce + decoding); a moved pin carries that commit. 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. diff --git a/bench/collect.py b/bench/collect.py index bbee5de7..acf1ef40 100755 --- a/bench/collect.py +++ b/bench/collect.py @@ -27,7 +27,10 @@ times five runs of the family's fixed operation and reports their median, in microseconds per operation. The data holds, per cell, every invocation's median, their median, and the fastest and slowest single run. The operation each cell's program reports must be the one -doc/tables/crypto.json states for its family (the size matrix's), or the cell is refused. A time +doc/tables/crypto.json states for its family (the size matrix's), or the cell is refused. A cell +the program lists with a third word, ` `, is a further operation of +the family, measured as `throughput `, whose text is the entry of +that name in the family's "sized_operations". A time never fails this script: only a program that does not run, or a cell it does not know, does. `--render-only` rewrites the page from the data. @@ -260,37 +263,57 @@ def run(target, command): def cells_of(target, exe, families): - """The cells the program was built with, which it lists without measuring; each must be a - family of crypto.json.""" - cells = [tuple(line.split()) for line in run(target, [exe, "--list"]).splitlines() if line] - for family, backend in cells: + """The cells the program was built with, which it lists without measuring, each as (family, + backend, operation), the operation "" for the family's fixed one; each must be a family of + crypto.json, and a further operation one of its "sized_operations".""" + cells = [] + for line in run(target, [exe, "--list"]).splitlines(): + words = line.split() + if not words: + continue + if len(words) not in (2, 3): + sys.exit(f"collect.py: {target} lists {line!r}, which is not " + "\" []\"") + family, backend = words[0], words[1] + further = words[2] if len(words) == 3 else "" if family not in families: sys.exit(f"collect.py: {target} lists {family} {backend}, and crypto.json has no " f"family {family}") + if further and further not in families[family].get("sized_operations", {}): + sys.exit(f"collect.py: {target} lists {family} {backend} {further}, and crypto.json's " + f"family {family} has no sized operation {further}") + cells.append((family, backend, further)) return cells +def stated_operation(family, further): + """The operation crypto.json states for a cell: the family's fixed one, or the further one of + that name.""" + return family["sized_operations"][further] if further else family["measured"] + + def measure(exes, invocations): data = crypto_json() families = {family["name"]: family for family in data["families"]} cases = {} for target, exe in exes.items(): - for family, backend in cells_of(target, exe, families): - cases[(target, family, backend)] = {"exe": exe, "results": []} + for family, backend, further in cells_of(target, exe, families): + cases[(target, family, backend, further)] = {"exe": exe, "results": []} loads = [os.getloadavg()] for _ in range(invocations): - for (target, family, backend), case in cases.items(): - command = [case["exe"], family, backend, "--runs", "5"] + for (target, family, backend, further), case in cases.items(): + command = [case["exe"], family, backend] + ([further] if further else []) + [ + "--runs", "5"] case["results"].append(program_line(target, command, run(target, command), THROUGHPUT_LINE_SHAPE)) loads.append(os.getloadavg()) out = [] - for (target, family, backend), case in cases.items(): + for (target, family, backend, further), case in cases.items(): results = case["results"] - stated = families[family]["measured"] + stated = stated_operation(families[family], further) if results[0]["operation"] != stated: - sys.exit(f"collect.py: {target} {family} {backend} measures " - f"{results[0]['operation']!r}, and crypto.json states {stated!r}") + sys.exit(f"collect.py: {target} {family} {backend}{' ' + further if further else ''} " + f"measures {results[0]['operation']!r}, and crypto.json states {stated!r}") medians = [r["median_us"] for r in results] runs = [t for r in results for t in r["runs_us"]] out.append({ @@ -308,8 +331,8 @@ def measure(exes, invocations): "max_us": max(runs), }) return { - "measurement": "each family's fixed operation per backend and target, " - "bench/operations.hpp", + "measurement": "each family's fixed operation, and its further sized operations, per " + "backend and target, bench/operations.hpp", "date": utc_today(), "machine": machine(), "load_average_before_after": [list(loads[0]), list(loads[1])], @@ -479,8 +502,9 @@ def render(data, page): "[#crypto-performance]", "=== Throughput", "", - "How long each family's fixed operation takes on each backend and target, the operation", - "the size matrix's programs call (bench/operations.hpp). A dated reference, measured by", + "How long each family's fixed operation, and each further operation the size matrix", + "measures, takes on each backend and target, the operations the size matrix's programs", + "call (bench/operations.hpp). A dated reference, measured by", "hand, that decides nothing: every default is chosen by the reason", "<> states, whatever these figures say.", "", @@ -515,8 +539,17 @@ def render(data, page): "| Operation | Backend | Target | Median (µs) | Fastest (µs) | Slowest (µs) " "| Per second", ] + operations = {family["name"]: [family["measured"]] + + list(family.get("sized_operations", {}).values()) + for family in table["families"]} + + def operation_rank(case): + # A further operation crypto.json no longer states sorts after the stated ones. + stated = operations[case["family"]] + return stated.index(case["operation"]) if case["operation"] in stated else len(stated) + cases = sorted(data["cases"], key=lambda c: (order.index(c["family"]), c["backend"], - TARGETS.index(c["target"]))) + operation_rank(c), TARGETS.index(c["target"]))) for c in cases: lines += [ "", diff --git a/bench/operations.hpp b/bench/operations.hpp index 7e9b293a..cac5b079 100644 --- a/bench/operations.hpp +++ b/bench/operations.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #ifdef WEBCPP_CRYPTO_HAS_SECP256K1 @@ -452,6 +453,99 @@ class secp256k1_silent_payments { }; #endif +// An ECDSA signature of a 32-byte SHA-256 digest on P-256, RFC 6979's nonce, S as computed. +template +class p256 { +public: + static constexpr std::string_view operation = "ECDSA signature of a 32-byte SHA-256 digest"; + + explicit p256(std::uint8_t seed) + : digest_(filled<32>(static_cast(seed + 3U))), + key_(take(curves::p256::basic_keypair::make( + filled<32>(static_cast(seed | 1U))), + "make")) {} + + std::uint8_t run() { return take(key_.sign_ecdsa(digest_), "sign_ecdsa")[0]; } + + bool check() { + auto const signature = take(key_.sign_ecdsa(digest_), "sign_ecdsa"); + auto const public_key = take(key_.public_key(), "public_key"); + return curves::p256::verify_ecdsa(signature, digest_, public_key).has_value(); + } + +private: + std::array digest_{}; + curves::p256::basic_keypair key_; +}; + +// The verification of an ECDSA signature of a 32-byte SHA-256 digest on P-256; the signature and +// the public key are made once, outside what is measured. +template +class p256_verify_ecdsa { +public: + static constexpr std::string_view operation = "ECDSA verification of a 32-byte SHA-256 digest"; + + explicit p256_verify_ecdsa(std::uint8_t seed) + : digest_(filled<32>(static_cast(seed + 3U))), + key_(take(curves::p256::basic_keypair::make( + filled<32>(static_cast(seed | 1U))), + "make")), + public_key_(take(key_.public_key(), "public_key")), + signature_(take(key_.sign_ecdsa(digest_), "sign_ecdsa")) {} + + std::uint8_t run() { + take(curves::p256::verify_ecdsa(signature_, digest_, public_key_), "verify_ecdsa"); + return signature_[0]; + } + + bool check() { + auto other = signature_; + other[63] ^= 1U; + return curves::p256::verify_ecdsa(signature_, digest_, public_key_).has_value() && + !curves::p256::verify_ecdsa(other, digest_, public_key_).has_value(); + } + +private: + std::array digest_{}; + curves::p256::basic_keypair key_; + curves::p256::basic_public_key public_key_; + std::array signature_{}; +}; + +// The x coordinate of the ECDH shared point of a P-256 key pair and a peer's public key. +template +class p256_ecdh_x { +public: + static constexpr std::string_view operation = + "x coordinate of the ECDH shared point with a peer's public key"; + + explicit p256_ecdh_x(std::uint8_t seed) + : key_(take(curves::p256::basic_keypair::make( + filled<32>(static_cast(seed | 1U))), + "make")), + peer_(take(curves::p256::basic_keypair::make( + filled<32>(static_cast(seed + 6U))), + "make")), + peer_public_(take(peer_.public_key(), "public_key")) {} + + std::uint8_t run() { + auto const shared = take(key_.ecdh_x(peer_public_), "ecdh_x"); + return shared.bytes()[0]; + } + + bool check() { + auto const own_public = take(key_.public_key(), "public_key"); + auto const shared = take(key_.ecdh_x(peer_public_), "ecdh_x"); + auto const peer_shared = take(peer_.ecdh_x(own_public), "ecdh_x"); + return std::ranges::equal(shared.bytes(), peer_shared.bytes()); + } + +private: + curves::p256::basic_keypair key_; + curves::p256::basic_keypair peer_; + curves::p256::basic_public_key peer_public_; +}; + // An Ed25519 signature of a 32-byte message. template class ed25519 { diff --git a/bench/throughput.cpp b/bench/throughput.cpp index d075e42a..703b08a5 100644 --- a/bench/throughput.cpp +++ b/bench/throughput.cpp @@ -4,19 +4,24 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// The throughput of each family's fixed operation (bench/operations.hpp) on each backend this -// build links: AES-256-GCM on BearSSL and OpenSSL, the AES block cipher on ctaes and OpenSSL, -// BIP340 signing on libsecp256k1, Ed25519 signing and X25519 agreement on libsodium, and the -// ChaCha20-Poly1305 family on libsodium, BearSSL and OpenSSL (OpenSSL natively and on WASI only). +// The throughput of each family's fixed operation (bench/operations.hpp) on each backend this build +// links: AES-256-GCM on BearSSL and OpenSSL, the AES block cipher on ctaes and OpenSSL, BIP340 +// signing on libsecp256k1, P-256's ECDSA signing, its verification and its ECDH's x on BearSSL, +// Ed25519 signing and X25519 agreement on libsodium, and the ChaCha20-Poly1305 family on libsodium, +// BearSSL and OpenSSL (OpenSSL natively and on WASI only). A family's further operation, beside its +// fixed one, is named after it, as crypto.json's `sized_operations` names it (P-256's +// `verify_ecdsa` and `ecdh_x`). // -// Run with a family and a backend, `throughput [--runs N]`, it repeats the -// operation enough times for a run to last at least 50 ms, times N runs (5 by default), and prints -// one JSON line: the family, the backend, the operation, the target, the compiler, the repetitions -// of a run, each run's microseconds per operation and their median. `--list` prints the cells this -// build has, one " " per line. Run with no argument, it is the correctness test: -// each cell's operation is checked once (an open of what it sealed, a verification of what it -// signed, both sides of an agreement), and the program exits 0 only when every check holds. A -// measurement never fails on its time; bench/collect.py runs it and writes doc/performance.adoc. +// Run with a family and a backend, `throughput [] [--runs N]`, it +// repeats the operation, the fixed one when none is named, enough times for a run to last at least +// 50 ms, times N runs (5 by default), and prints one JSON line: the family, the backend, the +// operation, the target, the compiler, the repetitions of a run, each run's microseconds per +// operation and their median. `--list` prints the cells this build has, one " " +// per line, with " " after it for a further operation. Run with no argument, it is the +// correctness test: each cell's operation is checked once (an open of what it sealed, a +// verification of what it signed, both sides of an agreement), and the program exits 0 only when +// every check holds. A measurement never fails on its time; bench/collect.py runs it and writes +// doc/performance.adoc. #include #include @@ -35,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -73,6 +79,8 @@ constexpr double shortest_run = 0.05; struct cell { std::string_view family; std::string_view backend; + // The further operation's name, crypto.json's `sized_operations` key; empty for the fixed one. + std::string_view further; std::string_view operation; void (*measure)(std::string_view family, int runs); bool (*check)(); @@ -141,9 +149,16 @@ struct on : Family { }; template