diff --git a/AGENTS.md b/AGENTS.md index e6761bb..87f2d44 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -18,9 +18,13 @@ specific to hash. ## Cautions -- 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). +- 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, which does not touch hashing; the pin returns to upstream once the patch is applied. + A moved pin is a change of its own (rule 12). - A test is declared through `hash-run` in `test/Jamfile`, which runs it with exceptions and without. - Every function that computes on a key, a password or a key derivation's secret input holds @@ -31,11 +35,14 @@ specific to hash. holds the guard and gets its call in that backend's `test/timing_coverage__test.cpp`, which fails until it does. The seam support (`test/support/timing_seam.hpp`) counts hash's own guards by their file, under `include/webcpp/hash/`, in `timing_entries`, and every guard in - `timing_all_entries`. + `timing_all_entries`. `checksums::double_sha256` is the one unkeyed hash that holds the guard + (codecs runs it on a WIF key's payload), and is checked in `test/checksums_test.cpp`. - Every wipe of a secret or of a failed output is `secure::erase_secret` over a span (``); a test sees each one through secure's seam `WEBCPP_SECURE_TEST_WIPE_RECORD` (`test/support/wipe_record.hpp`). hash depends on config and - secure, no other webcpp library. + secure, and, through its one target `/webcpp/hash//base58_with_checksum`, on codecs: only + `include/webcpp/hash/base58_with_checksum.hpp` includes a codecs header (an include boundary), + so that `checksums.hpp`, which `hash.hpp` includes, needs none. - `include/webcpp/hash/detail/canary.h` is a C header: it is in neither the headers-alone list nor `meta/include-boundaries.json`. - Every local and parameter of a C++ function in `include/` takes two words, the role then the kind diff --git a/build.jam b/build.jam index c9e1ad0..4fad3c6 100644 --- a/build.jam +++ b/build.jam @@ -20,6 +20,12 @@ alias hash : : : : include /boost//headers /webcpp/co /webcpp/secure//secure ; # end::hash[] +# tag::base58_with_checksum[] +# Base58Check, hash::formats::base58_with_checksum: hash, and codecs, whose +# header it includes. +alias base58_with_checksum : : : : hash /webcpp/codecs//codecs ; +# end::base58_with_checksum[] + # tag::openssl[] # OpenSSL, the backend backends::openssl: the superproject's source-built # OpenSSL 3.5.9 (/webcpp/third_party//openssl, static), the same archives as diff --git a/doc/Jamfile b/doc/Jamfile index 4a834c0..29782ac 100644 --- a/doc/Jamfile +++ b/doc/Jamfile @@ -13,11 +13,13 @@ webcpp.doc hash : hash.adoc ; # The reference reads every header, the backends' and the C libraries' # included: each library's target gives its define WEBCPP_HASH_HAS_ # and its headers' directory, as a program that links it has them; and -# webcpp::asio's, for async.hpp. +# webcpp::asio's, for async.hpp, and webcpp::codecs', for +# base58_with_checksum.hpp. webcpp.reference hash : /third_party//xkcp /third_party//blake3 /third_party//sodium /third_party//bearssl /webcpp/hash//openssl - /third_party//botan /webcpp/asio//asio ; + /third_party//botan /webcpp/asio//asio + /webcpp/codecs//codecs ; # The guide's generated tables and llms.txt (doc/generated/, doc/llms.txt) # against their data, capabilities.json, guide.json and measurements.json: diff --git a/doc/backends.adoc b/doc/backends.adoc index a37b361..b0d815e 100644 --- a/doc/backends.adoc +++ b/doc/backends.adoc @@ -45,7 +45,7 @@ states about its constant time. The backends: | every function of its cells | bearssl -| BearSSL at 32381d6 (webcpporg/bearssl's webcpp branch: upstream 7bea48e (v0.6-39) plus one ECDSA fix that does not touch hashing) +| BearSSL at ae63032 (webcpporg/bearssl's branch upstream/ecdsa-bits2int-constant-time: upstream 7bea48e (v0.6-39) and the ECDSA nonce fix submitted upstream, which does not touch hashing) | MIT | `/third_party//bearssl` | native, wasip2, wasip3, emscripten @@ -2212,10 +2212,10 @@ What each backend's upstream states, and where: * blake3: not stated. BLAKE3 1.8.7: README.md and c/README.md state no constant-time property. * sodium: not stated. libsodium 1.0.22: its headers state constant time for comparisons (sodium/utils.h) alone, not for its hashing, MAC, key derivation and password hashing functions (crypto_hash_*, crypto_xof_*, crypto_generichash_*, crypto_shorthash_*, crypto_auth_*, crypto_kdf_*, crypto_pwhash_*). * bearssl: not stated. -** BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*. -** BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_kdf.h states no constant-time property for br_shake_*. -** BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out. -** BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out. +** BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*. +** BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_kdf.h states no constant-time property for br_shake_*. +** BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out. +** BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out. * openssl: not stated. ** OpenSSL 3.5.9: doc/man7/EVP_MD-*.pod, doc/man3/EVP_DigestInit.pod and providers/implementations/digests/ state no constant-time property. ** OpenSSL 3.5.9: doc/man3/EVP_MAC.pod, doc/man7/EVP_MAC-HMAC.pod, EVP_MAC-KMAC.pod, EVP_MAC-BLAKE2.pod, EVP_MAC-Siphash.pod and providers/implementations/macs/ state no constant-time property. diff --git a/doc/capabilities.json b/doc/capabilities.json index 20e2674..737e8c4 100644 --- a/doc/capabilities.json +++ b/doc/capabilities.json @@ -77,7 +77,7 @@ "name": "bearssl", "tag": "backends::bearssl", "macro": "BEARSSL", - "library": "BearSSL at 32381d6 (webcpporg/bearssl's webcpp branch: upstream 7bea48e (v0.6-39) plus one ECDSA fix that does not touch hashing)", + "library": "BearSSL at ae63032 (webcpporg/bearssl's branch upstream/ecdsa-bits2int-constant-time: upstream 7bea48e (v0.6-39) and the ECDSA nonce fix submitted upstream, which does not touch hashing)", "licence": "MIT", "jamfile": "/third_party//bearssl", "targets": [ @@ -390,7 +390,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." } }, "openssl": { @@ -430,7 +430,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." } }, "openssl": { @@ -478,7 +478,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." } }, "openssl": { @@ -526,7 +526,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." } }, "openssl": { @@ -574,7 +574,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." } }, "openssl": { @@ -630,7 +630,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." } }, "openssl": { @@ -726,7 +726,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*." } }, "openssl": { @@ -1055,7 +1055,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_kdf.h states no constant-time property for br_shake_*." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_kdf.h states no constant-time property for br_shake_*." } }, "openssl": { @@ -1111,7 +1111,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_kdf.h states no constant-time property for br_shake_*." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_kdf.h states no constant-time property for br_shake_*." } }, "openssl": { @@ -2392,7 +2392,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." } }, "openssl": { @@ -2433,7 +2433,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." } }, "openssl": { @@ -2482,7 +2482,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." } }, "openssl": { @@ -2531,7 +2531,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." } }, "openssl": { @@ -2580,7 +2580,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." } }, "openssl": { @@ -2637,7 +2637,7 @@ "hmacs": null, "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out." } }, "openssl": { @@ -3559,7 +3559,7 @@ ], "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out." } }, "openssl": { @@ -3682,7 +3682,7 @@ ], "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out." } }, "openssl": { @@ -3804,7 +3804,7 @@ ], "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out." } }, "openssl": { @@ -3926,7 +3926,7 @@ ], "constant_time": { "value": "not stated", - "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out." + "source": "BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out." } }, "openssl": { diff --git a/doc/changelog.adoc b/doc/changelog.adoc index 2ac8bd3..d30422d 100644 --- a/doc/changelog.adoc +++ b/doc/changelog.adoc @@ -18,7 +18,7 @@ of every version are on webcpp's index page. // tag::unreleased[] * *Breaking change:* Changed the public names: hkdf's input_key_material, pseudorandom_key, application_info, output_key_material and salt, hkdf_max_application_info_size, scrypt_parameters' cost, block_size and parallelism (from n, r, p) and argon2_parameters::memory_kibibytes. * *Breaking change:* Changed HKDF's overloads: `+hkdf(out, {}, {}, {})+`, `+async_hkdf(ex, out, salt, {}, {}, token)+` and `+{vec}+` inputs are ambiguous; pass the vector itself, an empty span or `+byte_pieces{...}+`. -* Added the library: Boost.Hash2's algorithms and HMAC under webcpp::hash's names, keccak_256 (Ethereum's Keccak-256 over XKCP), blake3 (keyed, key derivation, extendable output), every result `+[[nodiscard]]+`. +* Added the library: Boost.Hash2's algorithms and HMAC under webcpp::hash's names, keccak_256 (Ethereum's), blake3 (keyed, key derivation, extendable output), `hash::formats::base58_with_checksum` (Base58Check over codecs), every result `+[[nodiscard]]+`. * Added HKDF (RFC 5869) and PBKDF2 (RFC 8018) on hash2, argon2id (RFC 9106), scrypt and Argon2i/Argon2id on sodium; keyed calls hold webcpp::secure's timing guard (DIT on arm64 Apple, Linux, Windows). * Added HKDF over byte pieces (hkdf_extract, hkdf_expand, hkdf and their async forms), with no cap; on the synchronous backends hash makes no buffer, heap or copy of the inputs. * Added backends::openssl (OpenSSL 3.5.9; native, wasip2, wasip3) and backends::botan (Botan 3.14.1, with exceptions), named in the call: their digests, MACs, key derivations and, on Botan, bcrypt. diff --git a/doc/digests.adoc b/doc/digests.adoc index 09ff98d..4522603 100644 --- a/doc/digests.adoc +++ b/doc/digests.adoc @@ -89,6 +89,15 @@ the bytes are the same on every backend. include::generated/backends-md5-sha1-sha2.adoc[] +*SHA-256 twice, as Bitcoin's checksum.* Bitcoin writes addresses and keys in +Base58Check: the payload followed by the first 4 bytes of SHA-256 of the +SHA-256 of the payload. `webcpp::hash::checksums::double_sha256` +(``, which `` includes) is that +checksum in the shape of webcpp::codecs' `formats::base58_checksum`, and +`hash::formats::base58_with_checksum` is the format built on it +(<>). It runs on a WIF key's bytes, so it holds +`secure::timing_guard` and wipes both digests and both hashers' states. + [#hash-sha3] === SHA-3 and SHAKE diff --git a/doc/generated/backends-hkdf.adoc b/doc/generated/backends-hkdf.adoc index 960fe97..d52580f 100644 --- a/doc/generated/backends-hkdf.adoc +++ b/doc/generated/backends-hkdf.adoc @@ -254,7 +254,7 @@ What each backend states on constant time, as the capability table read it: -* `bearssl`: BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out. +* `bearssl`: BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; br_hkdf_* (src/kdf/hkdf.c) and HKDF over pieces call br_hmac_out. * `botan`: ** Botan 3.14.1: doc/api_ref/kdf.rst states no constant-time property of its KDFs, and their source (src/lib/kdf/) none. ** Botan 3.14.1: doc/api_ref/message_auth_codes.rst states no constant-time property of HMAC, and its source (src/lib/mac/hmac/) none; HKDF over pieces calls its HMAC. diff --git a/doc/generated/backends-hmac.adoc b/doc/generated/backends-hmac.adoc index 016ff85..4388e53 100644 --- a/doc/generated/backends-hmac.adoc +++ b/doc/generated/backends-hmac.adoc @@ -625,7 +625,7 @@ What each backend states on constant time, as the capability table read it: -* `bearssl`: BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out. +* `bearssl`: BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hmac.h states constant time for br_hmac_outCT alone; mac calls br_hmac_out. * `botan`: ** Botan 3.14.1: doc/api_ref/hash.rst warns that Streebog's baseline implementation uses table lookups indexed by the data and is likely vulnerable to cache-based side channels; its constant-time code needs AVX-512 and GFNI, an instruction-set module that /third_party//botan's minimized build does not load. ** Botan 3.14.1: doc/api_ref/hash.rst warns that Whirlpool's baseline implementation uses table lookups indexed by the data and is likely vulnerable to cache-based side channels; its constant-time code needs AVX2 or AVX-512, instruction-set modules that /third_party//botan's minimized build does not load. diff --git a/doc/generated/backends-md5-sha1-sha2.adoc b/doc/generated/backends-md5-sha1-sha2.adoc index 3e764b4..7060eeb 100644 --- a/doc/generated/backends-md5-sha1-sha2.adoc +++ b/doc/generated/backends-md5-sha1-sha2.adoc @@ -331,7 +331,7 @@ What each backend states on constant time, as the capability table read it: -* `bearssl`: BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*. +* `bearssl`: BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_hash.h states constant time for its GHASH implementations alone, not for br_md5_*, br_sha1_*, br_sha224_* to br_sha512_* or br_md5sha1_*. * `botan`: Botan 3.14.1: doc/api_ref/hash.rst states no constant-time property of this hash, and its source (src/lib/hash/) none. * `hash2`: Boost.Hash2 1.92: its headers (boost/hash2/) state no constant-time property. * `openssl`: OpenSSL 3.5.9: doc/man7/EVP_MD-*.pod, doc/man3/EVP_DigestInit.pod and providers/implementations/digests/ state no constant-time property. diff --git a/doc/generated/backends-sha3-shake.adoc b/doc/generated/backends-sha3-shake.adoc index d2b3704..1f19c0d 100644 --- a/doc/generated/backends-sha3-shake.adoc +++ b/doc/generated/backends-sha3-shake.adoc @@ -268,7 +268,7 @@ What each backend states on constant time, as the capability table read it: -* `bearssl`: BearSSL upstream 7bea48e (v0.6-39), unchanged in webcpp's pin 32381d6: bearssl_kdf.h states no constant-time property for br_shake_*. +* `bearssl`: BearSSL upstream 7bea48e (v0.6-39), unchanged at webcpp's pin ae63032: bearssl_kdf.h states no constant-time property for br_shake_*. * `botan`: ** Botan 3.14.1: doc/api_ref/hash.rst states no constant-time property of SHAKE or of its XOF interface, and its source (src/lib/xof/shake_xof/) none. ** Botan 3.14.1: doc/api_ref/hash.rst states no constant-time property of this hash, and its source (src/lib/hash/) none. diff --git a/doc/jamfile.adoc b/doc/jamfile.adoc index 20c40e9..4d5b0bc 100644 --- a/doc/jamfile.adoc +++ b/doc/jamfile.adoc @@ -48,6 +48,11 @@ KBKDF, the one-step KDF, the X9.63 KDF, a digest (Streebog, Whirlpool and Skein-512 among them) or a MAC on `backends::botan` | `/third_party//botan`, in a program built with exceptions | native, emscripten + +| `hash::formats::base58_with_checksum` (Base58Check), which includes +webcpp::codecs +| `/webcpp/hash//base58_with_checksum` in place of `/webcpp/hash//hash` +| every target |=== A program that hashes passwords with Argon2id and derives keys with HKDF on @@ -90,6 +95,13 @@ The library's own target, as `build.jam` declares it: include::{library-root}/build.jam[tag=hash] ---- +and the one of Base58Check, which brings codecs with it: + +[source,bash] +---- +include::{library-root}/build.jam[tag=base58_with_checksum] +---- + [#hash-jamfile-messages] === When the build stops diff --git a/doc/use-cases/cryptocurrencies.adoc b/doc/use-cases/cryptocurrencies.adoc index 33a088b..c9c1485 100644 --- a/doc/use-cases/cryptocurrencies.adoc +++ b/doc/use-cases/cryptocurrencies.adoc @@ -67,8 +67,8 @@ public key until the coins are spent. *The idea.* Legacy (P2PKH) and SegWit version 0 (P2WPKH) addresses commit to hash160 of the public key: RIPEMD-160 of SHA-256 of its 33 compressed bytes. The two digests together give 20 bytes, and an attacker would have to break -both to substitute a key. The address's encoding (Base58Check or Bech32) is -webcpp::bitcoin's. +both to substitute a key. The address's encoding is Base58Check +(<>) or Bech32, the latter webcpp::bitcoin's. [source] ---- @@ -88,6 +88,51 @@ example's value is computed independently with Python. different address; wallets must agree on the form. RIPEMD-160 alone is legacy: use it only inside hash160, never for a new design (<>). +[#hash-crypto-base58check] +=== An address and a key as text: Base58Check + +*The problem.* An address or a private key is bytes, and a person copies it +from a screen or a sheet of paper. One mistyped character must be caught, not +sent to another address or read as another key. + +*The idea.* Base58Check writes a payload, a version byte and the rest of the +address or key, in base58 (Bitcoin's alphabet, without `0`, `O`, `I` and `l`) +after appending the first 4 bytes of its double SHA-256. A reader recomputes +the checksum and refuses the text when it differs: a mistyped text passes only +with a chance of about one in four billion. +`hash::formats::base58_with_checksum` is that format, registered into +webcpp::codecs for a payload of up to 100 bytes (an address is 21, a WIF key +33 or 34, a BIP-32 extended key 78). The format gives the payload: what its +version byte means is webcpp::bitcoin's. + +[source] +---- +include::{examples}/base58_with_checksum.cpp[tag=example,indent=0] +---- + +[listing] +---- +include::{examples}/base58_with_checksum.expected[] +---- + +The address is the Bitcoin wiki's, and the key is one of Bitcoin Core's +`key_io_valid.json`, whose entries the library's test also runs. + +A WIF key is a secret, and a decode with no secret token gives a public +result, so the key is read into a `codecs::secret_bytes<100>`: it stays in the +result itself, with no heap, and is wiped when the result is destroyed. +`codecs::decode(format, text, codecs::tokens::secret)` gives a secret result +too, a heap vector (`codecs::secret_byte_vector`) for a text of dynamic size. +The decode wipes its own scratch in both forms. + +*Mistakes.* Reading a key with the plain decode leaves its bytes in a public +vector that is never wiped. A payload of more than 100 bytes, or a text of more +than 143 characters, is `codecs::error::input_too_long`, found before any +checksum is computed; a mistyped character is `codecs::error::checksum_mismatch` +and a text with `0`, `O`, `I` or `l` is `codecs::error::invalid_character`. A +program links `/webcpp/hash//base58_with_checksum`, which adds codecs to +hash's own target (<>). + [#hash-crypto-ethereum] === An Ethereum address: Keccak-256 and EIP-55 diff --git a/example/Jamfile b/example/Jamfile index 4410c3b..fa52bd3 100644 --- a/example/Jamfile +++ b/example/Jamfile @@ -101,6 +101,7 @@ webcpp.example nostr_event_id.cpp ; webcpp.example nip44_conversation_key.cpp ; webcpp.example bip39_seed.cpp ; webcpp.example bitcoin_hash160.cpp ; +webcpp.example base58_with_checksum.cpp : /webcpp/hash//base58_with_checksum ; webcpp.example ethereum_address.cpp : /third_party//xkcp ; webcpp.example password_storage.cpp : /third_party//sodium /webcpp/crypto//crypto ; diff --git a/example/base58_with_checksum.cpp b/example/base58_with_checksum.cpp new file mode 100644 index 0000000..5ea5e1d --- /dev/null +++ b/example/base58_with_checksum.cpp @@ -0,0 +1,68 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// Base58Check, as a domain library such as webcpp::bitcoin would use it: the address of the +// Bitcoin wiki's "Technical background of version 1 Bitcoin addresses" (version byte 0 and the +// 20 bytes of a hash160, 1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs) written from its payload, and a +// compressed WIF key of Bitcoin Core's key_io_valid.json +// (L5nJeqKmpHp4P7F8ZYyjwc5a7P4d8EabuGAzfGJk7yC1BJyzNaEd, whose key is ff778740...b843f8) read into +// a secret result: version byte 0x80, the 32 bytes of the key and 0x01 for a compressed public +// key, 34 bytes. Both agree with Bitcoin Core's test vectors and with Python's hashlib.sha256 +// over the payload. + +// tag::example[] +#include "hex.hpp" + +#include +#include + +#include +#include +#include + +int main() { + namespace codecs = webcpp::codecs; + auto const& format = webcpp::hash::formats::base58_with_checksum; + + // An address: the version byte 0 and a hash160, then the checksum is appended. + auto const payload = example::from_hex("00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"); + auto const address = codecs::encode(format, payload); + if (!address) { + std::cerr << address.error().message() << '\n'; + return 1; + } + std::cout << "address " << address.value() << '\n'; + + // A WIF key is a secret: the in-place form, a codecs::secret_bytes, holds it in the result + // itself (no heap) and wipes it when destroyed. Decoding it with codecs::tokens::secret + // gives a heap vector of the same secret kind. + std::string const wif = "L5nJeqKmpHp4P7F8ZYyjwc5a7P4d8EabuGAzfGJk7yC1BJyzNaEd"; + auto const key = codecs::decode>(format, wif); + if (!key) { + std::cerr << key.error().message() << '\n'; + return 1; + } + auto const bytes = key.value().view(); + std::cout << "wif payload " << bytes.size() << " bytes, version " + << example::hex(bytes.first(1)) << ", compressed flag " << example::hex(bytes.last(1)) + << '\n'; + + // One character changed is a mistyped key, never another key. + std::string typo = wif; + typo[10] = typo[10] == '2' ? '3' : '2'; + auto const refused = codecs::decode>(format, typo); + std::cout << "typo " << (refused ? "accepted" : refused.error().message()) << '\n'; + + // The key is compared in place, in constant time, never copied into a text of its own. + auto const expected_key = + example::from_hex("ff778740f88ddcf102aeb81daee289c044c4a4571c4b6f287400f4b8e0b843f8"); + if (address.value() != "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs" || bytes.size() != 34 || + !webcpp::secure::constant_time_equal(bytes.subspan(1, 32), expected_key) || refused) { + return 1; + } +} + +// end::example[] diff --git a/example/base58_with_checksum.expected b/example/base58_with_checksum.expected new file mode 100644 index 0000000..a143dbe --- /dev/null +++ b/example/base58_with_checksum.expected @@ -0,0 +1,3 @@ +address 1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs +wif payload 34 bytes, version 80, compressed flag 01 +typo the checksum does not match diff --git a/include/webcpp/hash.hpp b/include/webcpp/hash.hpp index 39c1b44..86d37fc 100644 --- a/include/webcpp/hash.hpp +++ b/include/webcpp/hash.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include diff --git a/include/webcpp/hash/base58_with_checksum.hpp b/include/webcpp/hash/base58_with_checksum.hpp new file mode 100644 index 0000000..42a977a --- /dev/null +++ b/include/webcpp/hash/base58_with_checksum.hpp @@ -0,0 +1,50 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +/** + Base58Check, as Bitcoin writes addresses, WIF keys and BIP-32 keys: base58 in Bitcoin's alphabet + of a payload of at most 100 bytes followed by the first 4 bytes of its double SHA-256, a format + registered into webcpp::codecs. This is the one header of webcpp::hash that includes codecs, and + the target `/webcpp/hash//base58_with_checksum` brings it: + + @code + // exe app : app.cpp /webcpp/hash//base58_with_checksum ; + auto text = webcpp::codecs::encode(webcpp::hash::formats::base58_with_checksum, payload); + auto key = webcpp::codecs::decode>( + webcpp::hash::formats::base58_with_checksum, wif_text); + @endcode + + The payload carries a version byte and the rest of the address or key: the format gives the + payload, the program reads it. A decode with no secret token gives a public result: decode a WIF + key with `codecs::tokens::secret` or into a `codecs::secret_bytes`. + + @see "Digests", in the guide. +*/ +#ifndef WEBCPP_HASH_BASE58_WITH_CHECKSUM_HPP +#define WEBCPP_HASH_BASE58_WITH_CHECKSUM_HPP + +#include + +#include + +#include +#include + +namespace webcpp::hash::formats { + +/** + The type of `base58_with_checksum`: `codecs::formats::basic_base58` of 100 bytes, Bitcoin's + alphabet and the checksum `checksums::double_sha256`. +*/ +using base58_with_checksum_t = + codecs::formats::basic_base58<100, codecs::formats::bitcoin_alphabet, checksums::double_sha256>; + +/** Base58Check: Bitcoin's addresses, WIF keys and BIP-32 keys, a payload of up to 100 bytes. */ +inline constexpr base58_with_checksum_t base58_with_checksum{}; + +} // namespace webcpp::hash::formats + +#endif // WEBCPP_HASH_BASE58_WITH_CHECKSUM_HPP diff --git a/include/webcpp/hash/checksums.hpp b/include/webcpp/hash/checksums.hpp new file mode 100644 index 0000000..d6baa07 --- /dev/null +++ b/include/webcpp/hash/checksums.hpp @@ -0,0 +1,78 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +/** + Checksums that other libraries' formats append to a payload. `checksums::double_sha256` is the + checksum of Bitcoin's Base58Check: the first 4 bytes of SHA-256 applied twice. It has the shape of + webcpp::codecs' `formats::base58_checksum` (a `size` and a `compute`), and this header includes no + codecs header: a program builds its own base58 instance with it, another bound or another + alphabet: + + @code + using long_base58_with_checksum = webcpp::codecs::formats::basic_base58< + 1024, webcpp::codecs::formats::bitcoin_alphabet, webcpp::hash::checksums::double_sha256>; + @endcode + + The format with Bitcoin's alphabet and 100 bytes is ``. + + @see "Digests", in the guide. +*/ +#ifndef WEBCPP_HASH_CHECKSUMS_HPP +#define WEBCPP_HASH_CHECKSUMS_HPP + +#include + +#include +#include + +#include + +#include +#include +#include + +namespace webcpp::hash::checksums { + +/** + The checksum of Base58Check: the first 4 bytes of SHA-256 of SHA-256 of the payload. + + It runs on secrets (a WIF key's payload is one), so it is constant time: Boost.Hash2's SHA-256 is + a fixed sequence of 32-bit operations with no branch on the data and no table indexed by it, with + no backend and no failure, on every target. `compute` holds `secure::timing_guard` and wipes both + digests and both hashers' states. +*/ +struct double_sha256 { + /** The checksum's bytes. */ + static constexpr std::size_t size = 4; + + /** + Writes the first 4 bytes of SHA-256 of SHA-256 of `payload_bytes` to `checksum_bytes`. + + @param payload_bytes The payload, which may be secret. + @param checksum_bytes The checksum. + */ + static void compute(std::span payload_bytes, + std::span checksum_bytes) noexcept { + secure::timing_guard const timing_scope; + boost::hash2::sha2_256 first_hasher; + first_hasher.update(payload_bytes.data(), payload_bytes.size()); + auto first_digest = first_hasher.result(); + boost::hash2::sha2_256 second_hasher; + second_hasher.update(first_digest.data(), first_digest.size()); + auto second_digest = second_hasher.result(); + for (std::size_t byte_index = 0; byte_index < size; ++byte_index) { + checksum_bytes[byte_index] = second_digest.data()[byte_index]; + } + secure::erase_secret(std::span(&first_hasher, 1)); + secure::erase_secret(std::span(&second_hasher, 1)); + secure::erase_secret(std::span(&first_digest, 1)); + secure::erase_secret(std::span(&second_digest, 1)); + } +}; + +} // namespace webcpp::hash::checksums + +#endif // WEBCPP_HASH_CHECKSUMS_HPP diff --git a/include/webcpp/hash/detail/canary.h b/include/webcpp/hash/detail/canary.h index 67dbf85..ec67a09 100644 --- a/include/webcpp/hash/detail/canary.h +++ b/include/webcpp/hash/detail/canary.h @@ -41,7 +41,7 @@ #ifdef WEBCPP_HASH_BEARSSL_CANARY_COMMIT #define WEBCPP_HASH_BEARSSL_COMMIT WEBCPP_HASH_BEARSSL_CANARY_COMMIT #else -#define WEBCPP_HASH_BEARSSL_COMMIT 32381d6 +#define WEBCPP_HASH_BEARSSL_COMMIT ae63032 #endif #ifdef WEBCPP_HASH_BOTAN_CANARY_COMMIT diff --git a/meta/include-boundaries.json b/meta/include-boundaries.json index baae183..16c4f72 100644 --- a/meta/include-boundaries.json +++ b/meta/include-boundaries.json @@ -11,10 +11,11 @@ { "headers": [ "include/webcpp/hash/algorithms.hpp", + "include/webcpp/hash/checksums.hpp", "include/webcpp/hash/hmac.hpp" ], "must-not-include": ["xkcp", "blake3", "sodium", "bearssl", "openssl/", "botan/"], - "why": "Boost.Hash2's algorithms and HMAC need nothing but Boost (spec section 2.1)" + "why": "Boost.Hash2's algorithms and HMAC, and the checksums built on them, need nothing but Boost (spec section 2.1)" }, { "headers": [ @@ -126,6 +127,18 @@ "must-not-include": ["xkcp", "blake3", "sodium", "bearssl", "openssl/", "botan/"], "why": "the asynchronous family includes no C library's header: it calls the synchronous functions, whose headers bring a linked backend's under its WEBCPP_HASH_HAS_ (spec sections 2.13 and 11)" }, + { + "headers": [ + "include/webcpp/hash.hpp", + "include/webcpp/hash/*.hpp", + "include/webcpp/hash/*/*.hpp" + ], + "except": [ + "include/webcpp/hash/base58_with_checksum.hpp" + ], + "must-not-include": ["webcpp/codecs"], + "why": "only base58_with_checksum.hpp depends on webcpp::codecs, through its own target /webcpp/hash//base58_with_checksum: the checksum is usable alone, and a program that includes hash.hpp links no codecs" + }, { "headers": [ "include/webcpp/hash.hpp", diff --git a/test/Jamfile b/test/Jamfile index 87c9651..19e6fcb 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -63,6 +63,7 @@ webcpp.headers-alone hash : ../include : hash/bcrypt.hpp hash/blake2b_subkey.hpp hash/byte_pieces.hpp + hash/checksums.hpp hash/config.hpp hash/detail/engines.hpp hash/detail/limits.hpp @@ -76,6 +77,10 @@ webcpp.headers-alone hash : ../include : hash/mac.hpp hash/pbkdf2.hpp hash/scrypt.hpp ; +# base58_with_checksum.hpp is the one header that includes webcpp::codecs, whose +# target it needs. +webcpp.headers-alone hash : ../include : hash/base58_with_checksum.hpp + : /webcpp/codecs//codecs ; # keccak.hpp compiles alone only with XKCP, whose target defines # WEBCPP_HASH_HAS_XKCP (without it, it stops with its message). webcpp.headers-alone hash : ../include : hash/keccak.hpp : /third_party//xkcp ; @@ -107,14 +112,14 @@ webcpp.headers-alone hash : ../include : hash/backends/sodium.hpp # backends/openssl.hpp compiles alone only with /webcpp/hash//openssl, which # defines WEBCPP_HASH_HAS_OPENSSL, on the targets where webcpp builds OpenSSL. -# Every other library is linked too (Botan natively), and webcpp::asio, so -# that every public header compiles with these options (the lint's aggregate -# translation unit, tools/lint/compile_commands.py). +# Every other library is linked too (Botan natively), and webcpp::asio and +# webcpp::codecs, so that every public header compiles with these options +# (the lint's aggregate translation unit, tools/lint/compile_commands.py). webcpp.headers-alone hash : ../include : hash/backends/openssl.hpp : /webcpp/hash//openssl /third_party//xkcp /third_party//blake3 /third_party//bearssl /third_party//sodium /webcpp/asio//asio - @botan-native-with-exceptions + /webcpp/codecs//codecs @botan-native-with-exceptions : native wasip2 wasip3 ; # backends/botan.hpp compiles alone with Botan, whose target defines @@ -263,6 +268,23 @@ hash-run hkdf_pieces_bearssl_sliced : hkdf_pieces_bearssl_test.cpp WEBCPP_HASH_DETAIL_TEST_HKDF_PIECES_SLICE=7 ; hash-run wipe : wipe_test.cpp : WEBCPP_SECURE_TEST_WIPE_RECORD ; +# The checksum of Base58Check (checksums::double_sha256): its three known +# values, every length around SHA-256's blocks, no allocation, its guard and +# the wipe of every digest and hasher state through the seam +# WEBCPP_SECURE_TEST_WIPE_RECORD; the first program includes no codecs header, +# the second that the checksum has the shape of codecs' base58 checksum. +hash-run checksums : checksums_test.cpp : WEBCPP_SECURE_TEST_WIPE_RECORD ; +hash-run checksums_codecs : checksums_codecs_test.cpp + : /webcpp/hash//base58_with_checksum ; +# Base58Check, hash::formats::base58_with_checksum, against Bitcoin Core's +# key_io_valid.json, read at run time (its 22 addresses and 16 WIF keys), public +# and secret, and its refusals; the wipe of the secret path's scratch through +# the seam WEBCPP_SECURE_TEST_WIPE_RECORD. +hash-run base58_with_checksum : base58_with_checksum_test.cpp + : /webcpp/hash//base58_with_checksum + /webcpp/third_party//bitcoin_base58_vectors /webcpp//boost_json + WEBCPP_SECURE_TEST_WIPE_RECORD ; + # digests::blake2b on backends::sodium, libsodium's crypto_generichash_blake2b # with key, salt, personalisation and output size: BLAKE2's keyed and unkeyed # vectors and libsodium's generichash3.exp, read at run time; the guards. diff --git a/test/base58_with_checksum_test.cpp b/test/base58_with_checksum_test.cpp new file mode 100644 index 0000000..e5aa9d0 --- /dev/null +++ b/test/base58_with_checksum_test.cpp @@ -0,0 +1,392 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// hash::formats::base58_with_checksum, Base58Check (design D2, D4), against Bitcoin Core's +// key_io_valid.json, read at run time from the directory WEBCPP_TEST_BITCOIN_BASE58_VECTORS gives: +// every entry that is no segwit address (22 addresses and 16 WIF keys, the test asserts both +// counts) has its payload built from the entry (P2PKH `76a91488ac` is version 0x00 on main and +// 0x6f on a test chain, P2SH `a91487` is 0x05 and 0xc4, a WIF key is 0x80 or 0xef, the key, and +// 0x01 when it is compressed), which encodes to the entry's string, public and secret, and which +// the string decodes to; each WIF key is also decoded as a secret result, through +// codecs::tokens::secret and into a codecs::secret_bytes (pre-flight F2). The Bitcoin wiki's +// address 1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs comes from its payload; every single-character change +// of it is codecs::error::checksum_mismatch, in the public and in the secret decode, whose scratch +// is wiped (Review Focus 4); "111" is invalid_length; a payload of 101 bytes, a text past 143 +// characters and a text of 105 `1`s (a payload of 101 after the checksum) are input_too_long +// before the checksum is computed (Review Focus 2, pre-flight F4), shown by a checksum of the +// test's own that counts its calls; two zero bytes and 19 more keep two leading `1`s (Review Focus +// 3); the empty payload is the base58 of its checksum (Review Focus 5); a `0` in a text is +// invalid_character. + +#include + +#include "support/wipe_record.hpp" + +#include +#include + +#include +#include +#include + +#include + +#include "support/data_file.hpp" +#include "support/hex.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef WEBCPP_TEST_BITCOIN_BASE58_VECTORS +#error "the test needs /webcpp/third_party//bitcoin_base58_vectors, which defines its directory" +#endif + +namespace { + +namespace codecs = webcpp::codecs; +namespace hash = webcpp::hash; +namespace json = boost::json; +namespace test = webcpp::test; +using hash::formats::base58_with_checksum; +using hash::formats::base58_with_checksum_t; + +static_assert(std::is_same_v>); +static_assert(base58_with_checksum_t::name == "base58"); +static_assert(base58_with_checksum_t::maximum_bytes == 100); +static_assert(base58_with_checksum_t::checksum_size == 4); +static_assert(base58_with_checksum_t::maximum_characters == 143); +static_assert(codecs::encodable, base58_with_checksum_t>); +static_assert(codecs::decodable, base58_with_checksum_t>); +static_assert(codecs::decodable, base58_with_checksum_t>); +// A decode of a dynamic text into the secret token is a heap result, a typed one is in place. +static_assert(std::is_same_v>); +static_assert(std::is_same_v>( + base58_with_checksum, std::string_view{})), + codecs::result>>); + +// The default public decode is a public vector: a WIF key is read with the secret token. +static_assert(std::is_same_v>>); + +using bytes_type = std::vector; + +// A checksum of the test's own in the shape of the format's: it counts its calls, then is the +// double SHA-256 it stands for. +struct counting_checksum { + static constexpr std::size_t size = 4; + + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): the count of the calls + static inline std::size_t calls = 0; + + static void compute(std::span payload_bytes, + std::span checksum_bytes) noexcept { + ++calls; + hash::checksums::double_sha256::compute(payload_bytes, checksum_bytes); + } +}; + +using counting_format_t = + codecs::formats::basic_base58<100, codecs::formats::bitcoin_alphabet, counting_checksum>; +constexpr counting_format_t counting_format{}; + +template +bool failed_with(Result const& actual, codecs::error expected) { + return !actual.has_value() && actual.error() == expected; +} + +bool equal_bytes(std::span left, std::span right) { + return std::ranges::equal(left, right); +} + +// One entry of the file that is a base58 address or key, with its payload built as design D4 says. +struct base58_entry { + std::string text; + bytes_type payload; + bool is_key = false; +}; + +std::uint8_t version_of(std::string_view chain, std::uint8_t main_version, + std::uint8_t test_version) { + if (chain == "main") { + return main_version; + } + // The test chains share Bitcoin's test prefixes; any other name fails the test. + if (chain == "testnet4" || chain == "signet" || chain == "regtest") { + return test_version; + } + std::cerr << "unknown chain: " << chain << '\n'; + BOOST_ERROR("key_io_valid.json names a chain the test does not know"); + return 0; +} + +bool is_segwit(std::string_view text) { + return text.starts_with("bc1") || text.starts_with("tb1") || text.starts_with("bcrt1"); +} + +std::vector entries_of_the_file() { + std::string const text = + test::read_text(std::string{WEBCPP_TEST_BITCOIN_BASE58_VECTORS "/key_io_valid.json"}); + json::value const parsed = json::parse(text); + std::vector entries; + for (json::value const& entry_value : parsed.as_array()) { + json::array const& entry = entry_value.as_array(); + std::string const address{entry.at(0).as_string()}; + if (is_segwit(address)) { + continue; + } + std::string const hex{entry.at(1).as_string()}; + json::object const& flags = entry.at(2).as_object(); + std::string const chain{flags.at("chain").as_string()}; + bool const is_key = flags.at("isPrivkey").as_bool(); + bytes_type const script = test::from_hex(hex); + bytes_type payload; + if (is_key) { + payload.push_back(version_of(chain, 0x80, 0xef)); + payload.insert(payload.end(), script.begin(), script.end()); + if (flags.at("isCompressed").as_bool()) { + payload.push_back(0x01); + } + } else if (hex.starts_with("76a914") && hex.ends_with("88ac") && script.size() == 25) { + payload.push_back(version_of(chain, 0x00, 0x6f)); + payload.insert(payload.end(), script.begin() + 3, script.begin() + 23); + } else if (hex.starts_with("a914") && hex.ends_with("87") && script.size() == 23) { + payload.push_back(version_of(chain, 0x05, 0xc4)); + payload.insert(payload.end(), script.begin() + 2, script.begin() + 22); + } else { + std::cerr << "unknown script: " << hex << '\n'; + BOOST_ERROR("key_io_valid.json holds a script that is neither P2PKH nor P2SH"); + } + entries.push_back({.text = address, .payload = payload, .is_key = is_key}); + } + return entries; +} + +// Bitcoin Core's entries, public: encode to the string, decode to the payload, every buffer form. +void the_file_public(std::vector const& entries) { + std::size_t addresses = 0; + std::size_t keys = 0; + for (base58_entry const& entry : entries) { + (entry.is_key ? keys : addresses) += 1; + auto const encoded = codecs::encode(base58_with_checksum, entry.payload); + if (!BOOST_TEST(encoded.has_value() && encoded.value() == entry.text)) { + std::cerr << " entry " << entry.text << '\n'; + } + BOOST_TEST_EQ(codecs::encoded_size(base58_with_checksum, entry.payload), entry.text.size()); + auto const decoded = codecs::decode(base58_with_checksum, entry.text); + BOOST_TEST(decoded.has_value() && equal_bytes(decoded.value(), entry.payload)); + std::array buffer{}; + auto const written = codecs::decode(base58_with_checksum, entry.text, std::span{buffer}); + BOOST_TEST(written.has_value() && written.value() == entry.payload.size() && + equal_bytes(std::span{buffer}.first(entry.payload.size()), entry.payload)); + } + BOOST_TEST_EQ(addresses, 22U); + BOOST_TEST_EQ(keys, 16U); +} + +// The secret operations on Bitcoin Core's entries: the secret encode gives the string, the secret +// decodes (the token's heap result, the in-place codecs::secret_bytes) the payload. +void the_file_secret(std::vector const& entries) { + for (base58_entry const& entry : entries) { + auto const encoded = + codecs::encode(base58_with_checksum, entry.payload, codecs::tokens::secret); + BOOST_TEST(encoded.has_value() && encoded.value().view() == entry.text); + + auto const heap = codecs::decode(base58_with_checksum, entry.text, codecs::tokens::secret); + static_assert(codecs::is_secret_v); + BOOST_TEST(heap.has_value() && equal_bytes(heap.value().view(), entry.payload)); + + auto const in_place = + codecs::decode>(base58_with_checksum, entry.text); + BOOST_TEST(in_place.has_value() && equal_bytes(in_place.value().view(), entry.payload)); + test::clear_wipes(); + } +} + +constexpr std::string_view wiki_address = "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs"; +constexpr std::string_view wiki_payload = "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"; + +void the_wiki_address() { + bytes_type const payload = test::from_hex(wiki_payload); + auto const encoded = codecs::encode(base58_with_checksum, payload); + BOOST_TEST(encoded.has_value() && encoded.value() == wiki_address); + auto const decoded = codecs::decode(base58_with_checksum, wiki_address); + BOOST_TEST(decoded.has_value() && equal_bytes(decoded.value(), payload)); +} + +// Every position of the address takes every other character of Bitcoin's alphabet: never a payload. +void every_single_character_change() { + std::string_view const alphabet = codecs::formats::bitcoin_alphabet::characters; + std::size_t changes = 0; + std::size_t not_mismatch = 0; + std::size_t secret_not_mismatch = 0; + std::size_t wrong_wipes = 0; + constexpr std::size_t scratch_size = + (std::max)(base58_with_checksum_t::maximum_characters, + base58_with_checksum_t::maximum_bytes + base58_with_checksum_t::checksum_size); + constexpr std::size_t state_size = sizeof(boost::hash2::sha2_256); + std::vector const expected_wipes{state_size, state_size, 32, 32, 4, scratch_size}; + for (std::size_t position = 0; position < wiki_address.size(); ++position) { + for (char const replacement : alphabet) { + if (replacement == wiki_address[position]) { + continue; + } + std::string changed{wiki_address}; + changed[position] = replacement; + ++changes; + auto const plain = codecs::decode(base58_with_checksum, changed); + if (!failed_with(plain, codecs::error::checksum_mismatch)) { + ++not_mismatch; + std::cerr << " public: " << changed << '\n'; + } + test::clear_wipes(); + auto const secret = + codecs::decode(base58_with_checksum, changed, codecs::tokens::secret); + if (!failed_with(secret, codecs::error::checksum_mismatch)) { + ++secret_not_mismatch; + std::cerr << " secret: " << changed << '\n'; + } + // The record holds the secret decode's wipes alone (cleared after the public decode), + // in order: the checksum's two hashers' states and two digests, then codecs' checksum + // buffer, then codecs' scratch of the format's bound; every byte zero (the record + // checks that), so that a decode that kept its scratch unwiped fails here. + auto const sizes = test::wiped_sizes(); + if (!std::ranges::equal(sizes, expected_wipes)) { + ++wrong_wipes; + for (std::size_t const wiped : sizes) { + std::cerr << " wiped " << wiped << '\n'; + } + } + } + } + BOOST_TEST_EQ(changes, wiki_address.size() * (alphabet.size() - 1)); + BOOST_TEST_EQ(not_mismatch, 0U); + BOOST_TEST_EQ(secret_not_mismatch, 0U); + BOOST_TEST_EQ(wrong_wipes, 0U); +} + +void the_refusals() { + BOOST_TEST(failed_with(codecs::decode(base58_with_checksum, std::string_view{"111"}), + codecs::error::invalid_length)); + BOOST_TEST(failed_with( + codecs::decode(base58_with_checksum, std::string_view{"111"}, codecs::tokens::secret), + codecs::error::invalid_length)); + // A character outside the alphabet is found before the checksum: `0`, `O`, `I` and `l`. + for (char const bad : std::string_view{"0OIl"}) { + std::string text{wiki_address}; + text[10] = bad; + BOOST_TEST(failed_with(codecs::decode(base58_with_checksum, text), + codecs::error::invalid_character)); + BOOST_TEST(failed_with(codecs::decode(base58_with_checksum, text, codecs::tokens::secret), + codecs::error::invalid_character)); + } + test::clear_wipes(); +} + +// A payload of 101 bytes, a text of 144 characters and a text whose payload is 101 bytes after the +// checksum split off are input_too_long, and none computes the checksum. +void the_bound_before_the_checksum() { + counting_checksum::calls = 0; + bytes_type const past(101, 0x01); + BOOST_TEST(failed_with(codecs::encode(counting_format, past), codecs::error::input_too_long)); + BOOST_TEST(failed_with(codecs::encode(counting_format, past, codecs::tokens::secret), + codecs::error::input_too_long)); + BOOST_TEST_EQ(codecs::encoded_size(counting_format, past), std::size_t(-1)); + std::string const too_many_characters(144, '2'); + BOOST_TEST(failed_with(codecs::decode(counting_format, too_many_characters), + codecs::error::input_too_long)); + BOOST_TEST( + failed_with(codecs::decode(counting_format, too_many_characters, codecs::tokens::secret), + codecs::error::input_too_long)); + // 105 zero bytes: 101 of payload and the 4 of the checksum. + std::string const too_long_payload(105, '1'); + BOOST_TEST(failed_with(codecs::decode(counting_format, too_long_payload), + codecs::error::input_too_long)); + BOOST_TEST( + failed_with(codecs::decode(counting_format, too_long_payload, codecs::tokens::secret), + codecs::error::input_too_long)); + BOOST_TEST_EQ(counting_checksum::calls, 0U); + // The control: a payload within the bound does compute it, and 100 bytes round trip. + bytes_type const most(100, 0xff); + auto const encoded = codecs::encode(counting_format, most); + BOOST_TEST(encoded.has_value()); + BOOST_TEST_GT(counting_checksum::calls, 0U); + BOOST_TEST(encoded.has_value() && encoded.value().size() <= 143); + BOOST_TEST(encoded.has_value() && + equal_bytes(codecs::decode(base58_with_checksum, encoded.value()).value(), most)); + // The format itself, with the real checksum, refuses the same. + BOOST_TEST( + failed_with(codecs::encode(base58_with_checksum, past), codecs::error::input_too_long)); + BOOST_TEST(failed_with(codecs::decode(base58_with_checksum, too_long_payload), + codecs::error::input_too_long)); + test::clear_wipes(); +} + +// Two zero bytes then 19 bytes (an address of version 0 whose hash starts with a zero byte): two +// leading `1`s, and it round trips. +void leading_zero_bytes() { + bytes_type payload{0x00, 0x00}; + for (std::uint8_t byte = 1; byte <= 19; ++byte) { + payload.push_back(byte); + } + auto const encoded = codecs::encode(base58_with_checksum, payload); + BOOST_TEST(encoded.has_value()); + if (encoded.has_value()) { + std::string const& text = encoded.value(); + BOOST_TEST(text.size() > 2 && text[0] == '1' && text[1] == '1' && text[2] != '1'); + auto const decoded = codecs::decode(base58_with_checksum, text); + BOOST_TEST(decoded.has_value() && equal_bytes(decoded.value(), payload)); + auto const secret = codecs::decode(base58_with_checksum, text, codecs::tokens::secret); + BOOST_TEST(secret.has_value() && equal_bytes(secret.value().view(), payload)); + } + test::clear_wipes(); +} + +// The empty payload is the base58 of its checksum, 5df6e0e2: "3QJmnh", and decodes to nothing. +void the_empty_payload() { + bytes_type const nothing; + auto const encoded = codecs::encode(base58_with_checksum, nothing); + BOOST_TEST(encoded.has_value() && encoded.value() == "3QJmnh"); + auto const decoded = codecs::decode(base58_with_checksum, std::string_view{"3QJmnh"}); + BOOST_TEST(decoded.has_value() && decoded.value().empty()); + auto const secret = + codecs::decode(base58_with_checksum, std::string_view{"3QJmnh"}, codecs::tokens::secret); + BOOST_TEST(secret.has_value()); + if (secret.has_value()) { + auto const& secret_bytes = secret.value(); + BOOST_TEST_EQ(secret_bytes.size(), 0U); + } + auto const secret_encoded = + codecs::encode(base58_with_checksum, nothing, codecs::tokens::secret); + BOOST_TEST(secret_encoded.has_value() && secret_encoded.value().view() == "3QJmnh"); + test::clear_wipes(); +} + +} // namespace + +int main() { + std::vector const entries = entries_of_the_file(); + the_file_public(entries); + the_file_secret(entries); + the_wiki_address(); + every_single_character_change(); + the_refusals(); + the_bound_before_the_checksum(); + leading_zero_bytes(); + the_empty_payload(); + return webcpp::test::exit_status(boost::report_errors()); +} diff --git a/test/checksums_codecs_test.cpp b/test/checksums_codecs_test.cpp new file mode 100644 index 0000000..ad3c58f --- /dev/null +++ b/test/checksums_codecs_test.cpp @@ -0,0 +1,21 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// checksums::double_sha256 is a checksum codecs' base58 takes (design D1): the translation unit +// that includes codecs, beside checksums_test.cpp, which includes none. + +#include + +#include + +#include + +static_assert(webcpp::codecs::formats::base58_checksum); +static_assert(webcpp::hash::checksums::double_sha256::size == 4); + +int main() { + return 0; +} diff --git a/test/checksums_test.cpp b/test/checksums_test.cpp new file mode 100644 index 0000000..8590358 --- /dev/null +++ b/test/checksums_test.cpp @@ -0,0 +1,137 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// checksums::double_sha256 (design D1, D4): the first 4 bytes of SHA-256 of SHA-256 of the payload, +// for the empty payload, for the Bitcoin wiki's "Technical background of version 1 Bitcoin +// addresses" example and for "hello"; a payload of every length around SHA-256's block (a block +// boundary changes which padding the hasher writes) against two applications of the algorithm +// itself; no allocation (operator new and, where the target counts it, the C library's); +// secure::timing_guard entered, through secure's seam, and no byte of the output changed by it; +// and every digest and hasher state erased, through secure's wipe record. This translation unit +// includes no codecs header (the checksum needs none; checksums_codecs_test.cpp checks the shape). + +#include "support/timing_seam.hpp" + +#include + +#include +#include + +#include + +#include "support/counting.hpp" +#include "support/hex.hpp" +#include "support/wipe_record.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef WEBCPP_SECURE_TEST_WIPE_RECORD +#error "checksums_test is built with WEBCPP_SECURE_TEST_WIPE_RECORD (test/Jamfile)" +#endif + +namespace { + +namespace test = webcpp::test; +using webcpp::hash::checksums::double_sha256; + +std::array checksum_of(std::span payload_bytes) { + std::array checksum{}; + double_sha256::compute(payload_bytes, checksum); + return checksum; +} + +std::string hex_of(std::span payload_bytes) { + auto const checksum = checksum_of(payload_bytes); + return test::to_hex(checksum); +} + +std::span bytes_of(std::string_view text) { + return {reinterpret_cast(text.data()), text.size()}; +} + +void known_values() { + BOOST_TEST_EQ(double_sha256::size, 4U); + BOOST_TEST_EQ(hex_of({}), "5df6e0e2"); + auto const wiki = test::from_hex("00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"); + BOOST_TEST_EQ(hex_of(wiki), "c7f18fe8"); + BOOST_TEST_EQ(hex_of(bytes_of("hello")), "9595c9df"); +} + +// The same by two applications of Boost.Hash2's SHA-256, over every length up to three blocks. +void every_length() { + std::vector payload_bytes; + for (std::size_t length = 0; length <= 200; ++length) { + boost::hash2::sha2_256 first_hasher; + first_hasher.update(payload_bytes.data(), payload_bytes.size()); + auto const first_digest = first_hasher.result(); + boost::hash2::sha2_256 second_hasher; + second_hasher.update(first_digest.data(), first_digest.size()); + auto const second_digest = second_hasher.result(); + auto const checksum = checksum_of(payload_bytes); + bool const equal = std::equal(checksum.begin(), checksum.end(), second_digest.data()); + if (!BOOST_TEST(equal)) { + std::cerr << " length " << length << '\n'; + } + payload_bytes.push_back(static_cast((length * 7U) + 3U)); + } +} + +void allocates_nothing() { + auto const payload_bytes = test::from_hex("00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"); + std::array checksum{}; + std::size_t const before = test::allocations(); + double_sha256::compute(payload_bytes, checksum); + BOOST_TEST_EQ(test::allocations(), before); + // The counter moves where it counts, so that the zero above says something. + std::size_t const moved_before = test::allocations(); + ::operator delete(::operator new(16)); + BOOST_TEST_GT(test::allocations(), moved_before); +} + +void enters_its_guard() { + auto const payload_bytes = test::from_hex("00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"); + std::array checksum{}; + test::require_enters("webcpp::hash::checksums::double_sha256::compute", + [&] { double_sha256::compute(payload_bytes, checksum); }); + BOOST_TEST_EQ(test::to_hex(checksum), "c7f18fe8"); + // The guard is the one guard: a call enters exactly one. + std::size_t const before = test::timing_all_entries; + double_sha256::compute(payload_bytes, checksum); + BOOST_TEST_EQ(test::timing_all_entries, before + 1); +} + +// Each digest and each hasher's state is erased: the two hashers' states (the size of Hash2's +// SHA-256 object), then the two digests (32 bytes each), every byte zero afterwards. +void wipes_every_digest() { + auto const payload_bytes = test::from_hex("00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"); + std::array checksum{}; + test::clear_wipes(); + double_sha256::compute(payload_bytes, checksum); + std::vector const sizes = test::wiped_sizes(); + std::size_t const state_size = sizeof(boost::hash2::sha2_256); + std::vector const expected{state_size, state_size, 32, 32}; + BOOST_TEST_ALL_EQ(sizes.begin(), sizes.end(), expected.begin(), expected.end()); + BOOST_TEST_EQ(test::to_hex(checksum), "c7f18fe8"); +} + +} // namespace + +int main() { + known_values(); + every_length(); + allocates_nothing(); + enters_its_guard(); + wipes_every_digest(); + return webcpp::test::exit_status(boost::report_errors()); +} diff --git a/test/link/bearssl.cpp b/test/link/bearssl.cpp index e96c9b8..7c2abfb 100644 --- a/test/link/bearssl.cpp +++ b/test/link/bearssl.cpp @@ -8,7 +8,7 @@ // its link, or with another commit's canary (WEBCPP_HASH_BEARSSL_CANARY_COMMIT), it does not link: // the canary its header reads, // -// webcpp_hash_bearssl_32381d6___add_third_party_bearssl_to_your_link, +// webcpp_hash_bearssl_ae63032___add_third_party_bearssl_to_your_link, // // whose configuration is noseeder on Windows and getentropy elsewhere, is undefined (spec // section 5.4). It names nothing of the test support, which a link-fail program is built without.