diff --git a/README.md b/README.md index f9414ae..72c5b38 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,11 @@ MSVC), wasip2, wasip3 and Emscripten. - `webcpp::crypto::curves::p256`: P-256's ECDSA signatures over SHA-256, low S or any S as the verification policy says, and its ECDH, on BearSSL natively and on WebCrypto in a browser. - `webcpp::crypto::formats`: every curve's keys and signatures in their formats, as OpenSSL, - WebCrypto and node write them, through `crypto::encode` and `crypto::decode` (codecs' value - codecs): a key's raw bytes with its point forms, RFC 5480's and RFC 8410's SPKI, PKCS#8, an ECDSA - signature's strict DER, and the PEM of the SPKI and PKCS#8, `formats::pem`, codecs' armor over - the DER, each decoded strictly. + WebCrypto and node write them, through `codecs::encode` and `codecs::decode` (crypto has no + verbs of its own; its types are declared to codecs): a key's raw bytes with its point forms, + RFC 5480's and RFC 8410's SPKI, PKCS#8, an ECDSA signature's strict DER, and the PEM of the SPKI + and PKCS#8, `formats::pem`, codecs' armor over the DER with its label in its type, each decoded + strictly, and an input past a format's bound refused as `codecs::error::input_too_long`. - `webcpp::crypto::public_key_encryption`: NaCl's and libsodium's boxes between two key pairs (`box`, `box_xchacha20poly1305`), refusing a peer key of small order, and sealed boxes (`sealed_box`), anonymous encryption to a public key. diff --git a/doc/changelog.adoc b/doc/changelog.adoc index fbdc41b..e4a20b9 100644 --- a/doc/changelog.adoc +++ b/doc/changelog.adoc @@ -16,14 +16,14 @@ of every version are on webcpp's index page. === Unreleased // tag::unreleased[] -* *Breaking change:* Changed the key formats into codecs formats: `crypto::encode`, `crypto::decode` and members replace encode_spki, to_der and their kin, `formats::max_encoded_size` their sizes, and `formats::pem` replaces `crypto::pem` and pem-default. +* *Breaking change:* Removed `crypto::encode` and `crypto::decode`: `codecs::encode` and `codecs::decode` are the verbs, the members unchanged. +* *Breaking change:* Changed the key formats into codecs formats: `codecs::encode`, `codecs::decode` and members replace encode_spki, to_der and their kin, `formats::max_encoded_size` their sizes, and `formats::pem` replaces `crypto::pem` and pem-default. * *Breaking change:* Changed the schemes into `crypto::signing_schemes`; every verification, `recover` and `can_verify` takes a policy after the scheme, `low_s_only`, `any_s` or `canonical` (Ed25519's former `strict`). * *Breaking change:* Changed low S into signatures' `is_low_s()` and `low_s()`, free tweak_xonly, span Schnorr verify and `xonly()` bytes into the typed `xonly_public_key`, and secp256k1's SPKI default to uncompressed. +* Changed PEM: its label is in its type, a PEM result is exactly as long as its longest text, and a hand-built armor over a key format does not build. +* Changed PEM decode: through codecs' over, in place; an input past a format's bound is codecs' `input_too_long` on every path. * Added crypto, C-backed primitives on webcpp::secure's guard (DIT on arm64): errors 1 to 20, `+[[nodiscard]]+` results, typed signatures, key pair concepts, factories, WEBCPP_CRYPTO_HAS_THREADS, Asio's asynchronous functions. -* Added random_device, the AES block ciphers, ciphers::gcm (incremental seal, two-pass open), supports_v, per-target defaults and /webcpp/crypto//-default, refusals naming their Jamfile line. -* Added backends::bearssl (AES-GCM, ChaCha20-Poly1305, curves::p256: keys, low-S ECDSA, agree; allocating nothing), backends::openssl natively and on WASI, its memory option, and backends::ctaes. -* Added curves::secp256k1: ECDSA (hedged too), recoverable ECDSA over SHA-256 or Keccak-256, BIP 340 Schnorr, agree's three forms, tweaks, ElligatorSwift, MuSig2, silent payments. -* Added curves::ed25519 and curves::x25519 on libsodium: canonical verification (twelve edge cases), Ed25519ph and its streaming prehashed_signer, the conversion, all-zero agreements refused. -* Added backends::sodium (shared with webcpp::hash), every backend's canary and C suite, ChaCha20-Poly1305 AEADs, secretbox and secretbox_xchacha20poly1305, boxes, sealed boxes, kx_keypair. -* Added the page: chapters per namespace and use case, the key formats' table generated from the one list, Jamfiles, messages, llms.txt, release-built sizes and throughput, timing findings. +* Added random_device, the AES block ciphers, ciphers::gcm, per-target defaults, backends::bearssl (AES-GCM, ChaCha20-Poly1305, curves::p256 keys, low-S ECDSA, agree), backends::openssl and backends::ctaes. +* Added curves::secp256k1 (ECDSA, recoverable ECDSA, BIP 340 Schnorr, agree, tweaks, ElligatorSwift, MuSig2, silent payments), and curves::ed25519 and curves::x25519 on libsodium, Ed25519's verification canonical. +* Added backends::sodium, ChaCha20-Poly1305 AEADs, secretbox, boxes, sealed boxes, kx_keypair, and the page: chapters per namespace and use case, generated tables, Jamfiles, messages, llms.txt, measurements. // end::unreleased[] diff --git a/doc/curves.adoc b/doc/curves.adoc index 3befbb5..2873b4c 100644 --- a/doc/curves.adoc +++ b/doc/curves.adoc @@ -44,7 +44,7 @@ derivation and of watch-only wallets: `secret_key_tweak_add`, `public_key_tweak_mul`, `public_key_negate` and `public_key_combine` (<>); * `secp256k1/formats.hpp`, the formats of its keys and signatures for -`crypto::encode` and `crypto::decode`: a public key's point (uncompressed by +`codecs::encode` and `codecs::decode`: a public key's point (uncompressed by default, compressed, and its x-only key), the SPKI and PKCS#8 files that OpenSSL and node exchange, their PEM, the x-only key's 32 bytes and an ECDSA signature's strict DER (<>); @@ -199,9 +199,9 @@ key, `curves::secp256k1::xonly_public_key`, made by its `from_bytes`, which lift the 32 bytes to the point of even y and refuses those that are no point's x, so that a key is never unset (design D12). It is also a format's value: a Nostr event's 32-byte key decodes with -`crypto::decode(crypto::formats::raw, bytes)`, +`codecs::decode(crypto::formats::raw, bytes)`, and a `public_key` writes its x-only key with -`crypto::encode(crypto::formats::raw(crypto::formats::xonly), key)` +`codecs::encode(crypto::formats::raw(crypto::formats::xonly), key)` (<>). Its `tweak_add(tweak)` is BIP 341's output key and parity, a `tweaked_xonly_key`, and `verify_tweak(tweak, tweaked)` checks an output key against its internal key, so that Taproot's arithmetic needs no @@ -367,9 +367,9 @@ passes the same scheme to `sign` and to `verify`. DER, low S and the 64-byte form are forms of a signature, not schemes, and they need the curve's order. DER and the 64-byte form are formats: -`crypto::encode(crypto::formats::der, signature)` writes strict DER, 8 to 72 +`codecs::encode(crypto::formats::der, signature)` writes strict DER, 8 to 72 bytes, allocating nothing, and -`crypto::decode(crypto::formats::der, der)` reads strict DER +`codecs::decode(crypto::formats::der, der)` reads strict DER back into the curve's `ecdsa_signature` (each curve's `formats.hpp`, <>). diff --git a/doc/ed25519.adoc b/doc/ed25519.adoc index ae89bfb..e4f005f 100644 --- a/doc/ed25519.adoc +++ b/doc/ed25519.adoc @@ -41,15 +41,15 @@ policy's to judge, so a key that `crypto::verify` refuses can still be made. `` gives the three types their formats (<>), synchronous on every backend: -`crypto::encode(crypto::formats::spki, public_key)` and -`crypto::decode(crypto::formats::spki, bytes)`, or the members +`codecs::encode(crypto::formats::spki, public_key)` and +`codecs::decode(crypto::formats::spki, bytes)`, or the members `public_key.encode(format)` and `public_key::decode(format, input)`, with `formats::raw`, `formats::spki` and `formats::pem(formats::spki)` for a public key; `formats::raw` (the seed, secret), `formats::pkcs8` and `formats::pem(formats::pkcs8)` for a key pair, whose results are secret and wiped; and `formats::raw` for a signature, which has no DER form. Without that header, a member's call is the compiler's "deduced return type used before it -is defined", and `crypto::encode` names the header to include. +is defined", and `codecs::encode` names the header to include. Each operation has its four forms, the member and its free form, synchronous and asynchronous, with the same work, errors and refusals diff --git a/doc/errors.adoc b/doc/errors.adoc index 8920c05..0284841 100644 --- a/doc/errors.adoc +++ b/doc/errors.adoc @@ -45,8 +45,8 @@ bytes) or 04 (65 bytes), or not a point of the curve. DER, or an r or an s that is zero or not below the group order. |12 |`backend_failure` |A backend failed where its contract says it does not. |13 |`memory_functions_refused` |OpenSSL's memory functions could not be installed: OpenSSL has -already allocated, or another party installed its own. From `backends::openssl::install_memory_functions`, -with `WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE`. +already allocated, or another party installed its own. From +`backends::openssl::install_memory_functions`, with `WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE`. |14 |`memory_functions_not_installed` |OpenSSL's memory functions are not webcpp::crypto's. From every `from_key` of the OpenSSL backend, with `WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE`. |15 |`weak_public_key` |A public key of small order, or one that gives an all-zero shared secret. @@ -55,9 +55,9 @@ every `from_key` of the OpenSSL backend, with `WEBCPP_CRYPTO_OPENSSL_MEMORY_RESO |18 |`invalid_padding` |Padding that cannot be removed, whatever the cause: one value for every cause, so that no padding oracle is built from it. |19 |`invalid_encoding` |A key's `spki` or `pkcs8` that does not decode, or is not the one encoding -the format allows, or a PEM whose payload is longer than its inner format's longest encoding. A -`raw` or a `der` gives the key's or the signature's own error, and the armor's own faults are -codecs' (below). +the format allows. An input longer than the pair's bound is codecs' +`input_too_long`, not this. A `raw` or a `der` gives the key's or the signature's own error, and the +armor's own faults are codecs' (below). |20 |`invalid_ciphertext_size` |A KEM or RSA ciphertext of another size than the key's. |=== @@ -73,8 +73,8 @@ include::generated/backends-error.adoc[] [#crypto-errors-categories] *Two categories: crypto's and codecs'.* The key formats are formats of -webcpp::codecs (<>), so a call of `crypto::encode` or -`crypto::decode` reports either category, and a program that tells refusals +webcpp::codecs (<>), so a call of `codecs::encode` or +`codecs::decode` reports either category, and a program that tells refusals apart compares with the enumeration of the category it is in (`error_code::category()` says which; each enumeration compares equal only to its own): @@ -83,24 +83,22 @@ its own): |=== |Refusal |Value |Category -|An `spki` or a `pkcs8` that is not the one encoding: another curve's -identifier, BER where DER is required, a byte left over, PKCS#8 version 2, a +|An `spki` or a `pkcs8` within the pair's bound that is not the one encoding: +another curve's identifier, BER where DER is required, a byte left over, a point or a scalar the key refuses, a PKCS#8 whose public key is not its scalar's |`crypto::error::invalid_encoding` |`webcpp.crypto` -|A PEM whose payload is longer than its inner format's longest encoding (an RSA -key's SPKI read as P-256's) -|`crypto::error::invalid_encoding` |`webcpp.crypto` -|A public key's `raw` of a size its form does not have, a point that is not on -the curve, a compressed x that is no point's or not below p +|A public key's `raw` of a size its form does not have, within the bound, a point +that is not on the curve, a compressed x that is no point's or not below p |`crypto::error::invalid_public_key` |`webcpp.crypto` -|A key pair's `raw` of another size than 32 bytes, or a secret key that is -zero or not below the order +|A key pair's `raw` shorter than 32 bytes (a longer one is `input_too_long`), or a +secret key that is zero or not below the order |`crypto::error::invalid_key_size`, `invalid_secret_key` |`webcpp.crypto` |A key pair's decode whose `from_secret` fails: secp256k1's signing context without entropy, a backend's failure (P-256's PKCS#8 on BearSSL) |`crypto::error::entropy_unavailable`, `backend_failure` |`webcpp.crypto` -|A signature's `raw` of another size; a `der` that is not strict DER; a +|A signature's `raw` shorter than its size (a longer one is `input_too_long`); a +`der` that is not strict DER; a signature given to `der` or `low_s()` whose r or s is 0 or not below n |`crypto::error::invalid_signature` |`webcpp.crypto` |A moved-from key pair given to a private format @@ -115,7 +113,19 @@ encoding |`codecs::error::invalid_character`, `invalid_padding` |`webcpp.codecs` |A span or a fixed token shorter than the exact encoding |`codecs::error::output_too_small` |`webcpp.codecs` -|An input past a bound codecs checks +|An input longer than the pair's bound, `formats::max_decode_input_size`, +on every path: a DER or a raw input past it (an RSA key's SPKI given to P-256's +`spki`, an Ed25519 or X25519 PKCS#8 version 2 of 83 bytes given to `pkcs8`, whose +bound is 48), and a PEM whose payload is past its inner format's bound (the same key +as `pem(spki)`), checked before the value codec reads a byte. A pair's bound is +its longest encoding, the cell of the table in <> +(a PEM's is its inner format's): Ed25519's and X25519's public key `raw` 32 and +`spki` 44, their key pair `raw` 32 and `pkcs8` 48, Ed25519's signature `raw` 64; +P-256's public key `raw` 65 (33 for `raw(formats::compressed)`) and `spki` 91 +(59), its key pair `raw` 32 and `pkcs8` 138, its signature `raw` 64 and `der` 72; +secp256k1's public key `raw` 65 (33) and `spki` 88 (56), its x-only key `raw` +32, its key pair `raw` 32 and `pkcs8` 135, its ECDSA signature `raw` 64 and `der` +72, its recoverable signature `raw` 65 and its Schnorr signature `raw` 64 |`codecs::error::input_too_long` |`webcpp.codecs` |A Base58Check text (a WIF key, an address) whose checksum differs, through webcpp::hash's `base58_with_checksum` diff --git a/doc/everyday.adoc b/doc/everyday.adoc index 06b715d..1b6b218 100644 --- a/doc/everyday.adoc +++ b/doc/everyday.adoc @@ -151,9 +151,9 @@ server stores it and later checks what the device signs. *The idea.* The browser exports the key with `exportKey("spki")`, or as PEM text, and the server reads it with -`crypto::decode(crypto::formats::pem(crypto::formats::spki), text)`, +`codecs::decode(crypto::formats::pem(crypto::formats::spki), text)`, or `crypto::formats::spki` for the bytes, which checks that it is P-256's SPKI -and a point of the curve; it stores the bytes `crypto::encode` writes back, the +and a point of the curve; it stores the bytes `codecs::encode` writes back, the one encoding (<>, <>). Nothing of it allocates. @@ -177,7 +177,7 @@ include::{examples}/uploaded_key.expected[] ---- *Mistakes to avoid.* Storing what the browser sent rather than what -`crypto::encode` writes back: a key uploaded as a compressed SPKI, 59 bytes, +`codecs::encode` writes back: a key uploaded as a compressed SPKI, 59 bytes, and the same key uncompressed, 91, compare unequal as bytes, though both decode to the one key; the strict decode refuses every other second encoding. Reading an SPKI as another algorithm's key: the decode of the type the program diff --git a/doc/formats.adoc b/doc/formats.adoc index b5cce39..7ae5c75 100644 --- a/doc/formats.adoc +++ b/doc/formats.adoc @@ -28,11 +28,11 @@ Every one of them is a format of webcpp::codecs tag of `webcpp::crypto::formats`, in ``, and, for each pair of a format and a type, a `codecs::value_codec` specialization in the type's curve's formats header, `/formats.hpp>`. -crypto's verbs, `crypto::encode` and `crypto::decode`, check the pair against -the type's formats and call codecs' verbs; a program may call -`codecs::encode` and `codecs::decode` with the same formats and gets the same -bytes. A program that uses one curve's formats includes that curve's formats -header, and pulls no other curve and no other backend. +crypto has no verbs of its own: `codecs::encode` and `codecs::decode` are the +verbs, and check every pair against crypto's declaration of the type to codecs, +`codecs::value_formats`; the types' members call the same verbs. A program that +uses one curve's formats includes that curve's formats header, and pulls no +other curve and no other backend. [#crypto-key-formats-forms] ==== The four forms of a call @@ -42,17 +42,17 @@ header, and pulls no other curve and no other backend. | | Encode | Decode | Free, the main form -| `crypto::encode(format, value)`: a result held on the stack, `codecs::bytes` +| `codecs::encode(format, value)`: a result held on the stack, `codecs::bytes` or `codecs::text`, or their secret forms -| `crypto::decode(format, input)`: `boost::system::result` +| `codecs::decode(format, input)`: `boost::system::result` | Free, a token -| `crypto::encode(format, value, token)`: any token of codecs', such as +| `codecs::encode(format, value, token)`: any token of codecs', such as `codecs::tokens::append_to(text)`, or of Boost.URL's | (the typed decode builds `T`) | Free, the caller's memory -| `crypto::encode(format, value, output)`: the size written, into a span the +| `codecs::encode(format, value, output)`: the size written, into a span the caller sized with `formats::max_encoded_size` | @@ -61,7 +61,7 @@ caller sized with `formats::max_encoded_size` | `T::decode(format, input)` |=== -The four give the same bytes. The members call the free verbs, and are defined +The four give the same bytes. The members call codecs' verbs, and are defined by the curve's formats header: a curve's own header declares them and includes no format, so that calling `key.encode(formats::spki)` without `/formats.hpp>` is the compiler's "function with @@ -84,9 +84,10 @@ or `pkcs8` (<>). The library holds one list of them, `detail::all_formats`: `raw`, `spki`, `pkcs8`, `der`, `pem(spki)` and `pem(pkcs8)`, each of its default form. Each type declares the formats it has in one place, beside its value codecs, a list of formats of that list, and crypto's -check, the tests and the table below derive from it. A format added later, JWK, -is one more tag, one line of the list, and its value codecs, with no change to -any call (<>). +declaration to codecs, the tests and the table below derive from it. A format +added later, JWK, is one more tag, one line of the list, and its value codecs, +with no change to any call +(<>). The valid pairs, each cell the exact size `encode` writes, in bytes for the binary formats and in characters for PEM; "secret" marks a pair whose results @@ -98,27 +99,38 @@ The table is generated from `doc/tables/crypto.json`, whose list of formats generate.py checks against `detail::all_formats` as `formats.hpp` declares it; the test `formats_table` takes every format of `detail::all_formats` with every point form over every type, and asserts that codecs' own concepts, -`codecs::encodable` and `codecs::decodable`, and crypto's check are true for -the table's pairs and no other, and that each binary size is the value codec's -`max_encoded_size`. A key pair is on a backend that holds its secret: one whose -key pairs live in WebCrypto has no private format (<>). +`codecs::encodable` and `codecs::decodable`, and codecs' check of crypto's +declaration are true for the table's pairs and no other, and that each binary +size is the value codec's `max_encoded_size`. A key pair is on a backend that +holds its secret: one whose key pairs live in WebCrypto has no private format +(<>). A PEM's size is RFC 7468's: for P-256's SPKI of 91 bytes, 124 base64 characters in 2 lines of at most 64, each ended by a line feed, 27 characters before them and 25 after, 178 in all. -A pair the type does not have stops the build in crypto's check, before codecs -is reached, with a fixed sentence and, in the failed requirement the compiler -prints, the type's list of formats, so that the diagnostic names what the type -has with no text written by hand. Some pairs add a sentence of their own: `der` +A pair the type does not have stops the build with one error, from codecs' +verbs. crypto declares each of its types to codecs, `codecs::value_formats`, with +the type's list of formats and the reasons that are crypto's. A pair the list +lacks gets codecs' message with the type's list in the failed requirement the +compiler prints, so that the diagnostic names what the type has with no text +written by hand; a pair that has a reason of crypto's gets crypto's own sentence +instead, which codecs reports as the declaration states it. The reasons are: `der` of an Ed25519 or BIP340 signature ("their 64 bytes are the signature"), `der` of a recoverable signature, `raw(formats::xonly)` decoded into a `secp256k1::public_key` ("an x-only key decodes into curves::secp256k1::xonly_public_key"), a private format of a key pair whose secret lives in WebCrypto, a type whose curve's formats header is not included -("include /formats.hpp>"), and `pem` of another -format than `spki` and `pkcs8`. <> gives each with its fix. A type that is not crypto's is left to -codecs, whose own diagnostic says the pair has no value codec. +("include /formats.hpp>"), +and an armor of codecs' built by hand over a +key's DER, `codecs::over` or a +`codecs::formats::basic_armor` of another label's value than the inner format's +("a key's PEM is formats::pem(formats::spki) or formats::pem(formats::pkcs8), +whose label is the inner format's"); `pem` of another format than `spki` and +`pkcs8` is refused by `pem` itself, the build's only error also where the format +then reaches a verb or a member on a type of crypto's. +<> gives each with its fix. A +type that is not crypto's is left to codecs, whose own diagnostic says the pair +has no value codec. [#crypto-key-formats-point-forms] ==== Point forms @@ -157,7 +169,8 @@ parity is not the prefix's, on public data only. Then the point is checked as `from_bytes` checks it. An x not below p, an x that is no point's, a prefix `02` or `03` with 65 bytes and a prefix `04` with 33 are `invalid_public_key` in `raw`, and `invalid_encoding` in `spki`, whose decoder refuses every input that -is not the one encoding with that error +is not the one encoding with that error, but one longer than the key's SPKI, +which is `codecs::error::input_too_long` (<>, <>). Ed25519's and X25519's keys have one form, their 32 bytes, and take no modifier. @@ -169,25 +182,31 @@ they are the value's own bytes, which `raw` calls, not a second format API. ==== PEM over codecs' armor `formats::pem(formats::spki)` and `formats::pem(formats::pkcs8)` are codecs' -armor over the key's DER, `codecs::over` -(link:{webcpp-libs}/codecs/{webcpp-page}#codecs-armor[codecs' armor]): RFC 7468's -text, its label `PUBLIC KEY` for every point form of `spki` and `PRIVATE KEY` -for `pkcs8`, taken from the inner format, never from the caller, so that no -call writes a public key under a private key's label. `pem` of any other format -stops the build: "PEM carries a DER key: pem(formats::spki) or -pem(formats::pkcs8)". Its base64 is codecs', constant time on every call, a -private key's included, and needs no backend. - -`crypto::decode` of a PEM decodes in place, in two steps: the armor's payload -into a buffer on the stack of the inner format's decode bound, +armor over the key's DER, with the label in the format's type: +`codecs::over>, Inner>` +(link:{webcpp-libs}/codecs/{webcpp-page}#codecs-armor[codecs' armor]). The +label is RFC 7468's, `PUBLIC KEY` for every point form of `spki` and +`PRIVATE KEY` for `pkcs8`, the inner format's, never the caller's, so that no +value of the type holds another and no call writes a public key under a private +key's label. `pem` of any other format stops the build: "PEM carries a DER key: +pem(formats::spki) or pem(formats::pkcs8)". An armor built by hand over a key's +DER does not build either: codecs' `armor_t` of any label, or a `basic_armor` +whose label is not the inner format's, stops with "a key's PEM is +formats::pem(formats::spki) or formats::pem(formats::pkcs8), whose label is the +inner format's"; a `basic_armor` of the same label's value composes. Its base64 +is codecs', constant time on every call, a private key's included, and needs no +backend. + +A PEM decodes in place through codecs' `over`, in two steps: the armor's +payload into a buffer on the stack of the inner format's decode bound, `formats::max_decode_input_size` (at most 138 bytes, P-256's PKCS#8), then the inner format's value codec; the buffer is wiped after a -PKCS#8, on every path. Nothing is allocated. A payload longer than the inner -format's longest encoding, an RSA key's SPKI labelled `PUBLIC KEY` among them, -is crypto's `invalid_encoding`, found before the buffer is written past. -`codecs::decode` of the same text, from a text of dynamic size, takes the -armor's intermediate from its allocator: the result is the same, and only -crypto's verb avoids the heap. +PKCS#8, on every path. Nothing is allocated, from a text of dynamic size too, +`codecs::decode` and the member alike. An input longer than the inner +format's bound, a DER or a payload, an RSA key's SPKI labelled `PUBLIC KEY` +among them, is `codecs::error::input_too_long`, as it is on every path and in +every format, after the armor's own errors and before the buffer is written +past; the inner value codec reads no byte of it. PEM is read as Windows and OpenSSL's tools write it: lines ended by a line feed or by a carriage return and a line feed, the last line's end optional. Text @@ -199,7 +218,7 @@ encoding of its bytes, are codecs' errors (<>). [#crypto-key-formats-results] ==== codecs' results and tokens -The result of `crypto::encode` is codecs': `codecs::bytes` for a binary format and +The result of `codecs::encode` is codecs': `codecs::bytes` for a binary format and `codecs::text` for PEM, held in place, N the value codec's `max_encoded_size`, so that nothing is allocated; `size()` is the encoding's length, `capacity()` N, and `view()` the bytes or the characters, a view of the @@ -210,16 +229,18 @@ copied by `clone()` alone, with no `==`, and wiped by `secure::erase_secret` when destroyed or moved from (link:{webcpp-libs}/codecs/{webcpp-page}#codecs-results[codecs' results], link:{webcpp-libs}/codecs/{webcpp-page}#codecs-secrecy[codecs' secrecy]). A -PEM result's capacity is codecs' bound of the armor for the longest label, 64 -characters: 221 for Ed25519's SPKI of 44 bytes, 286 for P-256's of 91, 347 for -P-256's PKCS#8 of 138, while its size is the table's. +PEM result is exactly as long as its longest text, the armor of the inner +format's `max_encoded_size` bytes under the format's own label: 113 characters +for Ed25519's SPKI of 44 bytes, 178 for P-256's of 91, 241 for P-256's PKCS#8 +of 138, and its size is the table's. Two bounds size a program's own buffers: `formats::max_encoded_size`, the longest encoding `encode` writes, and `formats::max_decode_input_size`, the longest input `decode` accepts; the two are equal for every pair of this version, and an encode-only pair, -`raw(formats::xonly)` of a `secp256k1::public_key`, states the first alone. The -span form, `crypto::encode(format, value, output)`, takes +`raw(formats::xonly)` of a `secp256k1::public_key`, states the first alone. An +input longer than the second is `codecs::error::input_too_long`, on every path. +The span form, `codecs::encode(format, value, output)`, takes `std::span` for a binary format and `std::span` for PEM, writes at the start of the span, and refuses one shorter than the exact encoding with `codecs::error::output_too_small`, writing nothing; it writes @@ -237,7 +258,7 @@ What each call costs in memory: | `encode` of every format, the default result | none -| the result, at most 347 characters (P-256's `pem(pkcs8)` capacity), and for a +| the result, at most 241 characters (P-256's `pem(pkcs8)`), and for a PEM the armor's intermediate of the inner format's bound, at most 138 bytes, wiped for `pkcs8` @@ -253,13 +274,9 @@ wiped for `pkcs8` | none | a re-encoding to check that the input is the one encoding, wiped for `pkcs8` -| `crypto::decode` of `pem(F)` +| `codecs::decode` of `pem(F)`, from a text of any size | none | the DER, at most `max_decode_input_size` of the inner format, wiped for `pkcs8` - -| `codecs::decode` of `pem(F)` from a text of dynamic size -| the armor's intermediate, from its allocator -| the result |=== secp256k1's key pair is the exception it is everywhere: decoding one makes its @@ -273,10 +290,9 @@ for bytes no encoder writes, `invalid_public_key`, `invalid_signature`, key pair given to a private format, with nothing written; a key pair's decode also gives the errors of its `from_secret`, `entropy_unavailable` and `backend_failure` (secp256k1's signing context, P-256's PKCS#8 on BearSSL). -The armor's faults, -a short output and an input past a bound codecs checks are codecs' errors, in -the category `webcpp.codecs`: <> lists which category -each refusal is in. +The armor's faults, a short output and an input past the pair's bound, +`input_too_long`, are codecs' errors, in the category `webcpp.codecs`: +<> lists which category each refusal is in. [#crypto-key-formats-before] ==== Before and after @@ -291,17 +307,17 @@ of crypto's own. What a program wrote before, and writes now: | `p256::encode_spki(public_key, der)` into an array of `p256::max_spki_size`, then `+pem::armour(pem::public_key_label, der, text)+` into an array of `+pem::armoured_size(...)+` -| `crypto::encode(formats::pem(formats::spki), public_key)`, or +| `codecs::encode(formats::pem(formats::spki), public_key)`, or `public_key.encode(formats::pem(formats::spki))` | `+pem::unarmour(pem::private_key_label, pem_text, der)+`, then `p256::decode_pkcs8(der)` -| `crypto::decode(formats::pem(formats::pkcs8), pem_text)`, or +| `codecs::decode(formats::pem(formats::pkcs8), pem_text)`, or `p256::keypair::decode(formats::pem(formats::pkcs8), pem_text)` | `key.sign(crypto::ecdsa_sha256, message)`, then `p256::to_der(signature, der)` | `key.sign(signing_schemes::ecdsa_sha256, message)`, then -`crypto::encode(formats::der, signature)` +`codecs::encode(formats::der, signature)` | `crypto::verify(crypto::ecdsa_sha256, signature, message, key)` | `crypto::verify(signing_schemes::ecdsa_sha256, verification_policies::any_s, @@ -332,7 +348,7 @@ replacement: | Removed | Its replacement | each curve's `encode_spki`, `decode_spki`, `encode_pkcs8`, `decode_pkcs8` -| `crypto::encode` and `crypto::decode` with `formats::spki` and +| `codecs::encode` and `codecs::decode` with `formats::spki` and `formats::pkcs8`, or the members | secp256k1's `decode_pkcs8(bytes, resource)` @@ -340,8 +356,8 @@ replacement: `pem(pkcs8)` | P-256's and secp256k1's `to_der`, `from_der` -| `crypto::encode(formats::der, signature)`, -`crypto::decode(formats::der, bytes)` +| `codecs::encode(formats::der, signature)`, +`codecs::decode(formats::der, bytes)` | `max_spki_size`, `max_pkcs8_size`, `max_der_size`, secp256k1's `spki_compressed_size` and `spki_uncompressed_size` @@ -374,7 +390,7 @@ pair's `tweak_xonly` stays | secp256k1's `crypto::verify` of a Schnorr signature over a 32-byte span | `crypto::verify` with a `secp256k1::xonly_public_key`, made from the 32 bytes -by `from_bytes` or `crypto::decode` +by `from_bytes` or `codecs::decode` | secp256k1's `public_key::xonly()`, MuSig2's `aggregate_key::xonly_public_key()` and `tweaked_xonly_key::key`, each @@ -412,7 +428,7 @@ tests compare every byte with OpenSSL's natively and on WASI bytes. A public key, which names no backend, is encoded and decoded on its family's default, libsodium on every target (`/webcpp/crypto//ed25519-default` and `/webcpp/crypto//x25519-default` link it); -`crypto::decode(formats::spki, bytes)` gives the typed key +`codecs::decode(formats::spki, bytes)` gives the typed key a key pair's `agree` takes. For P-256, RFC 5480's SPKI and a PKCS#8 holding RFC 5915's ECPrivateKey with @@ -435,8 +451,10 @@ not below n with `invalid_signature` (<>). gives the same bytes (design D30): the one form that OpenSSL and WebCrypto write, and nothing else. It refuses with `invalid_encoding` another curve's identifier (an Ed25519 key given to X25519's decoder), a byte left over, a -length in BER's long form where DER requires the short one, PKCS#8 version 2, -which carries the public key beside the seed, and attributes. A key that one +length in BER's long form where DER requires the short one, and attributes. +PKCS#8 version 2, which carries the public key beside the seed, is 83 bytes for +Ed25519 and X25519, past the pair's bound of 48, and every verb refuses it with +`codecs::error::input_too_long` before the decoder reads a byte. A key that one library accepts and another refuses lets two programs that should agree on a key disagree on it; one form, and one verdict on every backend, closes that door. A program that receives version 2 converts it with OpenSSL's @@ -447,8 +465,8 @@ include::generated/backends-ed25519_formats.adoc[] include::generated/backends-x25519_formats.adoc[] -*Example.* RFC 8410's own Ed25519 key pair read from PEM with `crypto::decode`, -its public key written back as PEM with `crypto::encode`, which is the RFC's to +*Example.* RFC 8410's own Ed25519 key pair read from PEM with `codecs::decode`, +its public key written back as PEM with `codecs::encode`, which is the RFC's to the character, and a signature; an X25519 public key's PEM, with its member `encode`; and the RFC's version 2 of the same private key refused: @@ -470,7 +488,7 @@ backend whose key pairs live in a WebCrypto `CryptoKey` (the asynchronous family's browser backend, <>) holds no secret bytes, and a non-extractable key has none to give. The private formats, a key pair's `raw`, `pkcs8` and `pem(pkcs8)`, have no value codec for such a backend, and -`crypto::encode` says so, naming WebCrypto's `exportKey`; its public key keeps +`codecs::encode` says so, naming WebCrypto's `exportKey`; its public key keeps `raw`, `spki` and `pem(spki)`, computed from the public key the backend keeps. [#crypto-key-formats-jwk] @@ -498,8 +516,11 @@ builds the typed keys through `raw`: `p256::keypair::from_secret(d)`, and Trystero's keys (<>). PKCS#8 version 2, RFC 5958's OneAsymmetricKey with `[1] publicKey` after the -seed (83 bytes for Ed25519 and X25519), is refused with `invalid_encoding` in -this version. A later version accepts it on decoding only, the public key +seed (83 bytes for Ed25519 and X25519), is refused with +`codecs::error::input_too_long` in this version: its 83 bytes are past the +pair's decode bound of 48, which every verb checks before the value codec reads +a byte (the value codec called directly refuses it with `invalid_encoding`). A +later version accepts it on decoding only, the public key checked against the seed's: its decode bound, `formats::max_decode_input_size`, becomes 83 for those two types, the encoder keeps writing version 1, and no call changes. @@ -508,7 +529,7 @@ keeps writing version 1, and no call changes. ==== Pitfalls * A PKCS#8, as DER or as PEM, is the secret key: kept in the secret result -`crypto::encode` gives, or decoded in place as the example does, it is wiped +`codecs::encode` gives, or decoded in place as the example does, it is wiped when it goes; written to a file or a database, it is sealed first, as any secret at rest (<>). A secret result's `clone()` is a second copy to look after. @@ -521,9 +542,11 @@ does. * SPKI names the algorithm, the raw 32 bytes do not: an Ed25519 key and an X25519 key of the same bytes are different keys, and the identifier keeps a program from using one as the other. -* `codecs::decode` of a PEM from a `std::string` allocates the armor's -intermediate; `crypto::decode` of the same text does not. A program that -must not allocate calls crypto's verb. +* An input longer than the pair's bound, `formats::max_decode_input_size`, is +`codecs::error::input_too_long` whatever its format: an RSA key's SPKI given to +P-256's decoder, as DER or as PEM, is not `invalid_encoding`, and a program that +tells the refusals apart compares with codecs' category +(<>). It allocates nothing. * A decode with the default form reads both points; a program that must store one form encodes with that form named, as secp256k1's `spki` changed its default in this version. diff --git a/doc/generated/backends-ed25519_formats.adoc b/doc/generated/backends-ed25519_formats.adoc index 8ba18d9..60eed01 100644 --- a/doc/generated/backends-ed25519_formats.adoc +++ b/doc/generated/backends-ed25519_formats.adoc @@ -7,7 +7,7 @@ // Generated by doc/tables/generate.py from doc/tables/crypto.json, the test Jamfiles, // the message tests and the Jamfile fixtures: run it again, never edit this file. -The backends of `+crypto::encode+`, `+crypto::decode+`, `+curves::ed25519::public_key::encode+`, `+curves::ed25519::public_key::decode+`, `+curves::ed25519::keypair::encode+`, `+curves::ed25519::keypair::decode+`, `+curves::ed25519::signature::encode+`, `+curves::ed25519::signature::decode+`, from the test programs that run it: +The backends of `+codecs::encode+`, `+codecs::decode+`, `+curves::ed25519::public_key::encode+`, `+curves::ed25519::public_key::decode+`, `+curves::ed25519::keypair::encode+`, `+curves::ed25519::keypair::decode+`, `+curves::ed25519::signature::encode+`, `+curves::ed25519::signature::decode+`, from the test programs that run it: [cols="1,2,2,4",options="header"] |=== diff --git a/doc/generated/backends-key_formats.adoc b/doc/generated/backends-key_formats.adoc index c591d8e..6fa6343 100644 --- a/doc/generated/backends-key_formats.adoc +++ b/doc/generated/backends-key_formats.adoc @@ -7,7 +7,7 @@ // Generated by doc/tables/generate.py from doc/tables/crypto.json, the test Jamfiles, // the message tests and the Jamfile fixtures: run it again, never edit this file. -The backends of `+crypto::encode+`, `+crypto::decode+`, `+curves::secp256k1::public_key::encode+`, `+curves::secp256k1::public_key::decode+`, `+curves::secp256k1::xonly_public_key::encode+`, `+curves::secp256k1::xonly_public_key::decode+`, `+curves::secp256k1::keypair::encode+`, `+curves::secp256k1::keypair::decode (also with a std::pmr::memory_resource)+`, `+curves::secp256k1::ecdsa_signature::encode+`, `+curves::secp256k1::ecdsa_signature::decode+`, `+curves::secp256k1::recoverable_signature::encode+`, `+curves::secp256k1::recoverable_signature::decode+`, `+curves::secp256k1::schnorr_signature::encode+`, `+curves::secp256k1::schnorr_signature::decode+`, from the test programs that run it: +The backends of `+codecs::encode+`, `+codecs::decode+`, `+curves::secp256k1::public_key::encode+`, `+curves::secp256k1::public_key::decode+`, `+curves::secp256k1::xonly_public_key::encode+`, `+curves::secp256k1::xonly_public_key::decode+`, `+curves::secp256k1::keypair::encode+`, `+curves::secp256k1::keypair::decode (also with a std::pmr::memory_resource)+`, `+curves::secp256k1::ecdsa_signature::encode+`, `+curves::secp256k1::ecdsa_signature::decode+`, `+curves::secp256k1::recoverable_signature::encode+`, `+curves::secp256k1::recoverable_signature::decode+`, `+curves::secp256k1::schnorr_signature::encode+`, `+curves::secp256k1::schnorr_signature::decode+`, from the test programs that run it: [cols="1,2,2,4",options="header"] |=== diff --git a/doc/generated/backends-p256_formats.adoc b/doc/generated/backends-p256_formats.adoc index f89d123..c20ca47 100644 --- a/doc/generated/backends-p256_formats.adoc +++ b/doc/generated/backends-p256_formats.adoc @@ -7,7 +7,7 @@ // Generated by doc/tables/generate.py from doc/tables/crypto.json, the test Jamfiles, // the message tests and the Jamfile fixtures: run it again, never edit this file. -The backends of `+crypto::encode+`, `+crypto::decode+`, `+curves::p256::public_key::encode+`, `+curves::p256::public_key::decode+`, `+curves::p256::keypair::encode+`, `+curves::p256::keypair::decode+`, `+curves::p256::ecdsa_signature::encode+`, `+curves::p256::ecdsa_signature::decode+`, from the test programs that run it: +The backends of `+codecs::encode+`, `+codecs::decode+`, `+curves::p256::public_key::encode+`, `+curves::p256::public_key::decode+`, `+curves::p256::keypair::encode+`, `+curves::p256::keypair::decode+`, `+curves::p256::ecdsa_signature::encode+`, `+curves::p256::ecdsa_signature::decode+`, from the test programs that run it: [cols="1,2,2,4",options="header"] |=== diff --git a/doc/generated/backends-x25519_formats.adoc b/doc/generated/backends-x25519_formats.adoc index e850441..e975050 100644 --- a/doc/generated/backends-x25519_formats.adoc +++ b/doc/generated/backends-x25519_formats.adoc @@ -7,7 +7,7 @@ // Generated by doc/tables/generate.py from doc/tables/crypto.json, the test Jamfiles, // the message tests and the Jamfile fixtures: run it again, never edit this file. -The backends of `+crypto::encode+`, `+crypto::decode+`, `+curves::x25519::public_key::encode+`, `+curves::x25519::public_key::decode+`, `+curves::x25519::keypair::encode+`, `+curves::x25519::keypair::decode+`, from the test programs that run it: +The backends of `+codecs::encode+`, `+codecs::decode+`, `+curves::x25519::public_key::encode+`, `+curves::x25519::public_key::decode+`, `+curves::x25519::keypair::encode+`, `+curves::x25519::keypair::decode+`, from the test programs that run it: [cols="1,2,2,4",options="header"] |=== diff --git a/doc/generated/matrix.adoc b/doc/generated/matrix.adoc index c328821..84570ba 100644 --- a/doc/generated/matrix.adoc +++ b/doc/generated/matrix.adoc @@ -151,12 +151,12 @@ |native, wasip2, wasip3, emscripten |`+x25519_sodium+`, `+oracle_25519+` -|`+crypto::encode+`, `+crypto::decode+`, `+curves::ed25519::public_key::encode+`, `+curves::ed25519::public_key::decode+`, `+curves::ed25519::keypair::encode+`, `+curves::ed25519::keypair::decode+`, `+curves::ed25519::signature::encode+`, `+curves::ed25519::signature::decode+` +|`+codecs::encode+`, `+codecs::decode+`, `+curves::ed25519::public_key::encode+`, `+curves::ed25519::public_key::decode+`, `+curves::ed25519::keypair::encode+`, `+curves::ed25519::keypair::decode+`, `+curves::ed25519::signature::encode+`, `+curves::ed25519::signature::decode+` |libsodium |native, wasip2, wasip3, emscripten |`+formats_ed25519_sodium+`, `+formats_25519_openssl_oracle+`, `+formats_25519_webcrypto_oracle+`, `+allocation_sodium+` -|`+crypto::encode+`, `+crypto::decode+`, `+curves::x25519::public_key::encode+`, `+curves::x25519::public_key::decode+`, `+curves::x25519::keypair::encode+`, `+curves::x25519::keypair::decode+` +|`+codecs::encode+`, `+codecs::decode+`, `+curves::x25519::public_key::encode+`, `+curves::x25519::public_key::decode+`, `+curves::x25519::keypair::encode+`, `+curves::x25519::keypair::decode+` |libsodium |native, wasip2, wasip3, emscripten |`+formats_x25519_sodium+`, `+formats_25519_openssl_oracle+`, `+formats_25519_webcrypto_oracle+`, `+allocation_sodium+` @@ -236,7 +236,7 @@ |native, wasip2, wasip3, emscripten |`+secp256k1_tweak+` -|`+crypto::encode+`, `+crypto::decode+`, `+curves::secp256k1::public_key::encode+`, `+curves::secp256k1::public_key::decode+`, `+curves::secp256k1::xonly_public_key::encode+`, `+curves::secp256k1::xonly_public_key::decode+`, `+curves::secp256k1::keypair::encode+`, `+curves::secp256k1::keypair::decode (also with a std::pmr::memory_resource)+`, `+curves::secp256k1::ecdsa_signature::encode+`, `+curves::secp256k1::ecdsa_signature::decode+`, `+curves::secp256k1::recoverable_signature::encode+`, `+curves::secp256k1::recoverable_signature::decode+`, `+curves::secp256k1::schnorr_signature::encode+`, `+curves::secp256k1::schnorr_signature::decode+` +|`+codecs::encode+`, `+codecs::decode+`, `+curves::secp256k1::public_key::encode+`, `+curves::secp256k1::public_key::decode+`, `+curves::secp256k1::xonly_public_key::encode+`, `+curves::secp256k1::xonly_public_key::decode+`, `+curves::secp256k1::keypair::encode+`, `+curves::secp256k1::keypair::decode (also with a std::pmr::memory_resource)+`, `+curves::secp256k1::ecdsa_signature::encode+`, `+curves::secp256k1::ecdsa_signature::decode+`, `+curves::secp256k1::recoverable_signature::encode+`, `+curves::secp256k1::recoverable_signature::decode+`, `+curves::secp256k1::schnorr_signature::encode+`, `+curves::secp256k1::schnorr_signature::decode+` |libsecp256k1 |native, wasip2, wasip3, emscripten |`+secp256k1_formats+`, `+secp256k1_formats_openssl+`, `+secp256k1_node_oracle+`, `+secp256k1_der+`, `+secp256k1_allocation+` @@ -296,12 +296,12 @@ |refused at compile time |`+p256_on_sodium+` -|`+crypto::encode+`, `+crypto::decode+`, `+curves::p256::public_key::encode+`, `+curves::p256::public_key::decode+`, `+curves::p256::keypair::encode+`, `+curves::p256::keypair::decode+`, `+curves::p256::ecdsa_signature::encode+`, `+curves::p256::ecdsa_signature::decode+` +|`+codecs::encode+`, `+codecs::decode+`, `+curves::p256::public_key::encode+`, `+curves::p256::public_key::decode+`, `+curves::p256::keypair::encode+`, `+curves::p256::keypair::decode+`, `+curves::p256::ecdsa_signature::encode+`, `+curves::p256::ecdsa_signature::decode+` |BearSSL |native, wasip2, wasip3, emscripten |`+p256_formats_bearssl+`, `+p256_openssl_oracle+`, `+p256_webcrypto_oracle+`, `+allocation_bearssl+` -|`+crypto::encode+`, `+crypto::decode+`, `+curves::p256::public_key::encode+`, `+curves::p256::public_key::decode+`, `+curves::p256::keypair::encode+`, `+curves::p256::keypair::decode+`, `+curves::p256::ecdsa_signature::encode+`, `+curves::p256::ecdsa_signature::decode+` +|`+codecs::encode+`, `+codecs::decode+`, `+curves::p256::public_key::encode+`, `+curves::p256::public_key::decode+`, `+curves::p256::keypair::encode+`, `+curves::p256::keypair::decode+`, `+curves::p256::ecdsa_signature::encode+`, `+curves::p256::ecdsa_signature::decode+` |libsodium |refused at compile time |`+p256_on_sodium+` @@ -433,13 +433,13 @@ The same, by target: the backends each operation runs on. |libsodium |libsodium -|`+crypto::encode+`, `+crypto::decode+`, `+curves::ed25519::public_key::encode+`, `+curves::ed25519::public_key::decode+`, `+curves::ed25519::keypair::encode+`, `+curves::ed25519::keypair::decode+`, `+curves::ed25519::signature::encode+`, `+curves::ed25519::signature::decode+` +|`+codecs::encode+`, `+codecs::decode+`, `+curves::ed25519::public_key::encode+`, `+curves::ed25519::public_key::decode+`, `+curves::ed25519::keypair::encode+`, `+curves::ed25519::keypair::decode+`, `+curves::ed25519::signature::encode+`, `+curves::ed25519::signature::decode+` |libsodium |libsodium |libsodium |libsodium -|`+crypto::encode+`, `+crypto::decode+`, `+curves::x25519::public_key::encode+`, `+curves::x25519::public_key::decode+`, `+curves::x25519::keypair::encode+`, `+curves::x25519::keypair::decode+` +|`+codecs::encode+`, `+codecs::decode+`, `+curves::x25519::public_key::encode+`, `+curves::x25519::public_key::decode+`, `+curves::x25519::keypair::encode+`, `+curves::x25519::keypair::decode+` |libsodium |libsodium |libsodium @@ -517,7 +517,7 @@ The same, by target: the backends each operation runs on. |libsecp256k1 |libsecp256k1 -|`+crypto::encode+`, `+crypto::decode+`, `+curves::secp256k1::public_key::encode+`, `+curves::secp256k1::public_key::decode+`, `+curves::secp256k1::xonly_public_key::encode+`, `+curves::secp256k1::xonly_public_key::decode+`, `+curves::secp256k1::keypair::encode+`, `+curves::secp256k1::keypair::decode (also with a std::pmr::memory_resource)+`, `+curves::secp256k1::ecdsa_signature::encode+`, `+curves::secp256k1::ecdsa_signature::decode+`, `+curves::secp256k1::recoverable_signature::encode+`, `+curves::secp256k1::recoverable_signature::decode+`, `+curves::secp256k1::schnorr_signature::encode+`, `+curves::secp256k1::schnorr_signature::decode+` +|`+codecs::encode+`, `+codecs::decode+`, `+curves::secp256k1::public_key::encode+`, `+curves::secp256k1::public_key::decode+`, `+curves::secp256k1::xonly_public_key::encode+`, `+curves::secp256k1::xonly_public_key::decode+`, `+curves::secp256k1::keypair::encode+`, `+curves::secp256k1::keypair::decode (also with a std::pmr::memory_resource)+`, `+curves::secp256k1::ecdsa_signature::encode+`, `+curves::secp256k1::ecdsa_signature::decode+`, `+curves::secp256k1::recoverable_signature::encode+`, `+curves::secp256k1::recoverable_signature::decode+`, `+curves::secp256k1::schnorr_signature::encode+`, `+curves::secp256k1::schnorr_signature::decode+` |libsecp256k1 |libsecp256k1 |libsecp256k1 @@ -565,7 +565,7 @@ The same, by target: the backends each operation runs on. |BearSSL |BearSSL -|`+crypto::encode+`, `+crypto::decode+`, `+curves::p256::public_key::encode+`, `+curves::p256::public_key::decode+`, `+curves::p256::keypair::encode+`, `+curves::p256::keypair::decode+`, `+curves::p256::ecdsa_signature::encode+`, `+curves::p256::ecdsa_signature::decode+` +|`+codecs::encode+`, `+codecs::decode+`, `+curves::p256::public_key::encode+`, `+curves::p256::public_key::decode+`, `+curves::p256::keypair::encode+`, `+curves::p256::keypair::decode+`, `+curves::p256::ecdsa_signature::encode+`, `+curves::p256::ecdsa_signature::decode+` |BearSSL |BearSSL |BearSSL diff --git a/doc/generated/messages.adoc b/doc/generated/messages.adoc index 7e7fde7..8c64678 100644 --- a/doc/generated/messages.adoc +++ b/doc/generated/messages.adoc @@ -293,6 +293,18 @@ The fix: Name a backend of `+webcpp::crypto::backends+` that has the family, as The fix: Call `+webcpp::secure::constant_time_equal+`, from `++`, the same comparison in constant time; `+crypto::constant_time_equal+` moved to webcpp::secure, with no alias. +|`+crypto_decode_removed+` +|native, wasip2, wasip3 +|`+no member named 'decode' in namespace 'webcpp::crypto'+` + +The fix: Read a key or a signature with `+codecs::decode(format, input)+` (`+webcpp/codecs.hpp+`) or the type's member `+T::decode(format, input)+`; `+crypto::decode+` is gone, with no alias. + +|`+crypto_encode_removed+` +|native, wasip2, wasip3 +|`+no member named 'encode' in namespace 'webcpp::crypto'+` + +The fix: Write a key or a signature with `+codecs::encode(format, value)+` (`+webcpp/codecs.hpp+`) or the type's member `+value.encode(format)+`; `+crypto::encode+` is gone, with no alias. + |`+ctaes_not_linked+` |native, wasip2, wasip3 |`+webcpp::crypto: the backend ctaes is used and not linked. In the program's Jamfile, add /third_party//ctaes beside /webcpp/crypto//crypto. (without b2: define WEBCPP_CRYPTO_HAS_CTAES=1 and link ctaes.c with webcpp's src/canary_ctaes.c)+` @@ -353,7 +365,7 @@ The fix: Encode a BIP340 signature with `+crypto::formats::raw+`, or its member `+no member named 'from_der' in namespace 'webcpp::crypto::curves::p256'+` -The fix: Write a P-256 signature's DER with `+crypto::encode(crypto::formats::der, signature)+` or its member `+signature.encode(crypto::formats::der)+`, and read it with `+crypto::decode(crypto::formats::der, bytes)+`; the free `+to_der+` and `+from_der+` are gone, with no alias. +The fix: Write a P-256 signature's DER with `+codecs::encode(crypto::formats::der, signature)+` or its member `+signature.encode(crypto::formats::der)+`, and read it with `+codecs::decode(crypto::formats::der, bytes)+`; the free `+to_der+` and `+from_der+` are gone, with no alias. |`+ed25519_default_not_linked+` |native, wasip2, wasip3 @@ -409,6 +421,12 @@ The fix: Call `+webcpp::secure::constant_time_equal+`, the same comparison of tw The fix: Call `+webcpp::secure::erase_secret+`, from `++`, the same erasure; `+crypto::erase_secret+` moved to webcpp::secure, with no alias. +|`+foreign_format_on_key+` +|native, wasip2, wasip3 +|`+webcpp::codecs::detail::format_listed, webcpp::crypto::formats::spki_t, webcpp::codecs::over>>, webcpp::crypto::formats::spki_t, std::allocator>>>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion+` + +The fix: Encode or decode a key in one of the formats of its list, the `+codecs::format_list+` the assertion names; for its bytes in hexadecimal, compose a format of its list, `+codecs::over{codecs::formats::hex, crypto::formats::raw}+`. + |`+format_header_missing_toy+` |native, wasip2, wasip3 |`+webcpp::crypto: include to encode or decode this type+` @@ -417,9 +435,9 @@ The fix: Include the formats header of the type's curve, the one the assertion n |`+format_refused_toy+` |native, wasip2, wasip3 -|`+webcpp::crypto::detail::format_list, webcpp::crypto::formats::spki_t, webcpp::codecs::over, std::allocator>>>': webcpp::crypto: this type has no such format; the formats it has are the format_list named in this assertion+` +|`+webcpp::codecs::format_list, webcpp::crypto::formats::spki_t, webcpp::codecs::over>>, webcpp::crypto::formats::spki_t, std::allocator>>>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion+` -The fix: Encode or decode a type in a format of its list, the `+format_list+` the assertion names: another type's format (`+der+` is an ECDSA signature's, `+pkcs8+` a key pair's) does not convert it. +The fix: Encode or decode a type in a format of its list, the `+codecs::format_list+` the assertion names: another type's format (`+der+` is an ECDSA signature's, `+pkcs8+` a key pair's) does not convert it. |`+format_without_header+` |native, wasip2, wasip3 @@ -479,15 +497,27 @@ The fix: Name `+crypto::public_key_encryption::box+`, `+box_xchacha20poly1305+` The fix: Name nonce_size, max_nonce_size and error::invalid_nonce_size: crypto spells its names out. +|`+keypair_decode_resource_composition+` +|native, wasip2, wasip3 +|`+webcpp::crypto: a key pair decodes with a memory resource from formats::raw, formats::pkcs8 or formats::pem(formats::pkcs8)+` + +The fix: Decode a secp256k1 key pair with a memory resource from `+crypto::formats::raw+`, `+crypto::formats::pkcs8+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`: decode a composition (its base64, its hexadecimal) with `+codecs::decode+` to the PKCS#8's bytes first, `+codecs::decode>(codecs::formats::base64, text)+`, then decode those with `+crypto::formats::pkcs8+`; or decode the key pair with `+codecs::decode+`, its signing context from the default resource. + |`+keypair_decode_resource_from_text+` |native, wasip2, wasip3 |`+webcpp::crypto: a key pair decodes from bytes: a std::span, or what converts to one+` The fix: Give the key pair's decode with a memory resource bytes for `+crypto::formats::raw+` and `+crypto::formats::pkcs8+`, a `+std::span+` or what converts to one (`+std::array+`, `+std::vector+`, a result's `+view()+`); a PEM text decodes with `+crypto::formats::pem(crypto::formats::pkcs8)+`. +|`+keypair_decode_resource_pem_from_bytes+` +|native, wasip2, wasip3 +|`+webcpp::crypto: a PEM decodes from a text: a std::string_view, or a contiguous sized range of char+` + +The fix: Give the key pair's decode with a memory resource a text for `+crypto::formats::pem(crypto::formats::pkcs8)+`: a `+std::string_view+`, a `+std::string+`, or a contiguous sized range of `+char+` such as `+std::span+`; DER bytes decode with `+crypto::formats::pkcs8+` itself. + |`+keypair_decode_resource_spki+` |native, wasip2, wasip3 -|`+webcpp::crypto: this type has no such format; the formats it has are the format_list named in this assertion+` +|`+webcpp::codecs::format_list, webcpp::crypto::formats::pkcs8_t, webcpp::codecs::over>, webcpp::crypto::formats::pkcs8_t, std::allocator>>>': webcpp::codecs: this type has no such format; the formats it has are the format_list named in this assertion+` The fix: Decode a secp256k1 key pair, with or without a memory resource, from one of its formats: `+crypto::formats::raw+`, its secret key, `+crypto::formats::pkcs8+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`; an SPKI holds a public key, which `+crypto::curves::secp256k1::public_key::decode(crypto::formats::spki, bytes)+` gives. @@ -515,6 +545,12 @@ The fix: Only an ECDSA signature has low S: Ed25519 and Schnorr signatures have The fix: Call the factory named by its input, member or free: `+from_secret+` for a key pair, `+from_bytes+` for a public key, `+from_key+` for a cipher (`+crypto::from_secret(bytes)+`, `+K::from_secret(bytes)+`); `+make+` is gone, with no alias. +|`+member_der_on_ed25519+` +|native, wasip2, wasip3 +|`+webcpp::crypto: Ed25519 and BIP340 signatures have no DER form; their 64 bytes are the signature+` + +The fix: As `+der_on_ed25519+`: encode an Ed25519 signature with `+signature.encode(crypto::formats::raw)+`, or `+codecs::encode(crypto::formats::raw, signature)+`: its 64 bytes are the signature, and it has no DER form. + |`+musig_nonce_copied+` |native, wasip2 |`+call to deleted constructor of+` @@ -595,21 +631,21 @@ The fix: Leave the backend out, as in `+curves::p256::keypair+`, or name `+backe |`+pem_decode_from_bytes+` |native, wasip2, wasip3 -|`+webcpp::crypto: a PEM decodes from a text: a std::string_view, or a contiguous range of char+` +|`+static assertion failed due to requirement 'webcpp::codecs::detail::converts, webcpp::codecs::over>>, webcpp::crypto::formats::spki_t, std::allocator>>': webcpp::codecs: this value codec decodes from text: a std::string_view, or a contiguous sized range of char+` -The fix: Give a PEM to `+crypto::decode+` as a text: a `+std::string_view+`, a `+std::string+`, or a contiguous range of `+char+` such as `+std::span+`; DER bytes decode with `+crypto::formats::spki+` or `+crypto::formats::pkcs8+` themselves. +The fix: Give a PEM to `+codecs::decode+`, or a type's member `+decode+`, as a text: a `+std::string_view+`, a `+std::string+`, or a contiguous sized range of `+char+` such as `+std::span+`; DER bytes decode with `+crypto::formats::spki+` or `+crypto::formats::pkcs8+` themselves. |`+pem_header_removed+` |native, wasip2, wasip3 |`+'webcpp/crypto/pem.hpp' file not found+` -The fix: Write and read PEM with `+crypto::encode+` and `+crypto::decode+` and `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`, in `++`, codecs' armor over the key's DER; `+pem.hpp+` is gone, with no header left at its path. +The fix: Write and read PEM with `+codecs::encode+` and `+codecs::decode+` and `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`, in `++`, codecs' armor over the key's DER; `+pem.hpp+` is gone, with no header left at its path. |`+pem_namespace_removed+` |native, wasip2, wasip3 |`+no member named 'pem' in namespace 'webcpp::crypto'+` -The fix: Write and read PEM with `+crypto::encode+` and `+crypto::decode+` and `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`, which take the label from the inner format; `+crypto::pem+`, its `+armour+`, `+unarmour+`, labels and `+armoured_size+`, is gone, with no alias. +The fix: Write and read PEM with `+codecs::encode+` and `+codecs::decode+` and `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`, which take the label from the inner format; `+crypto::pem+`, its `+armour+`, `+unarmour+`, labels and `+armoured_size+`, is gone, with no alias. |`+pem_of_der+` |native, wasip2, wasip3 @@ -623,6 +659,18 @@ The fix: Armor the DER of a key: `+crypto::formats::pem(crypto::formats::spki)+` The fix: As `+pem_of_der+`: armor the DER of a key, `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`; a key's raw bytes have no PEM. +|`+pem_other_armor+` +|native, wasip2, wasip3 +|`+webcpp::crypto: a key's PEM is formats::pem(formats::spki) or formats::pem(formats::pkcs8), whose label is the inner format's+` + +The fix: Encode or decode a key's PEM with `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`, whose label, `+PUBLIC KEY+` or `+PRIVATE KEY+`, is in its type; a text under another label, a certificate's among them, is no key, and decoding it gives `+codecs::error::invalid_prefix+`. + +|`+pem_other_basic_armor+` +|native, wasip2, wasip3 +|`+webcpp::crypto: a key's PEM is formats::pem(formats::spki) or formats::pem(formats::pkcs8), whose label is the inner format's+` + +The fix: As `+pem_other_armor+`: name a key's PEM by `+crypto::formats::pem(F)+`, never by `+codecs::formats::basic_armor+` of a label, so that the label is always the inner format's. + |`+ph_removed+` |native, wasip2, wasip3 |`+no member named 'ph' in namespace+` @@ -1153,7 +1201,7 @@ The fix: Make the x-only key with a value: `+curves::secp256k1::xonly_public_key |native, wasip2, wasip3 |`+webcpp::crypto: an x-only key decodes into curves::secp256k1::xonly_public_key+` -The fix: A 32-byte x-only key decodes into `+crypto::curves::secp256k1::xonly_public_key+`: `+crypto::decode(crypto::formats::raw(crypto::formats::xonly), bytes)+`, or its member `+xonly_public_key::decode+`; `+raw(crypto::formats::xonly)+` encodes a public key's x-only key, and its 33- or 65-byte SEC 1 point decodes into a `+public_key+` with `+crypto::formats::raw+`. +The fix: A 32-byte x-only key decodes into `+crypto::curves::secp256k1::xonly_public_key+`: `+codecs::decode(crypto::formats::raw(crypto::formats::xonly), bytes)+`, or its member `+xonly_public_key::decode+`; `+raw(crypto::formats::xonly)+` encodes a public key's x-only key, and its 33- or 65-byte SEC 1 point decodes into a `+public_key+` with `+crypto::formats::raw+`. |=== At build and link time, from the fixtures of the user's Jamfiles, each built on a scratch superproject: the texts the build's output contains, per target and linker, as each fixture checks them. @@ -1986,6 +2034,14 @@ On MSVC, each case of `test/msvc` is proven to stop the build, at compile or at |the compile |Call `+webcpp::secure::constant_time_equal+`, from `++`, the same comparison in constant time; `+crypto::constant_time_equal+` moved to webcpp::secure, with no alias. +|`+crypto_decode_removed+` +|the compile +|Read a key or a signature with `+codecs::decode(format, input)+` (`+webcpp/codecs.hpp+`) or the type's member `+T::decode(format, input)+`; `+crypto::decode+` is gone, with no alias. + +|`+crypto_encode_removed+` +|the compile +|Write a key or a signature with `+codecs::encode(format, value)+` (`+webcpp/codecs.hpp+`) or the type's member `+value.encode(format)+`; `+crypto::encode+` is gone, with no alias. + |`+ctaes_not_linked+` |the compile |Add `+/third_party//ctaes+`. @@ -2024,7 +2080,7 @@ On MSVC, each case of `test/msvc` is proven to stop the build, at compile or at |`+der_short_name_removed_p256+` |the compile -|Write a P-256 signature's DER with `+crypto::encode(crypto::formats::der, signature)+` or its member `+signature.encode(crypto::formats::der)+`, and read it with `+crypto::decode(crypto::formats::der, bytes)+`; the free `+to_der+` and `+from_der+` are gone, with no alias. +|Write a P-256 signature's DER with `+codecs::encode(crypto::formats::der, signature)+` or its member `+signature.encode(crypto::formats::der)+`, and read it with `+codecs::decode(crypto::formats::der, bytes)+`; the free `+to_der+` and `+from_der+` are gone, with no alias. |`+ed25519_default_not_linked+` |the compile @@ -2062,13 +2118,17 @@ On MSVC, each case of `test/msvc` is proven to stop the build, at compile or at |the compile |Call `+webcpp::secure::erase_secret+`, from `++`, the same erasure; `+crypto::erase_secret+` moved to webcpp::secure, with no alias. +|`+foreign_format_on_key+` +|the compile +|Encode or decode a key in one of the formats of its list, the `+codecs::format_list+` the assertion names; for its bytes in hexadecimal, compose a format of its list, `+codecs::over{codecs::formats::hex, crypto::formats::raw}+`. + |`+format_header_missing_toy+` |the compile |Include the formats header of the type's curve, the one the assertion names, `+/formats.hpp>+`, beside the curve's own header, which declares no format. |`+format_refused_toy+` |the compile -|Encode or decode a type in a format of its list, the `+format_list+` the assertion names: another type's format (`+der+` is an ECDSA signature's, `+pkcs8+` a key pair's) does not convert it. +|Encode or decode a type in a format of its list, the `+codecs::format_list+` the assertion names: another type's format (`+der+` is an ECDSA signature's, `+pkcs8+` a key pair's) does not convert it. |`+format_without_header+` |the compile @@ -2106,10 +2166,18 @@ On MSVC, each case of `test/msvc` is proven to stop the build, at compile or at |the compile |Name nonce_size, max_nonce_size and error::invalid_nonce_size: crypto spells its names out. +|`+keypair_decode_resource_composition+` +|the compile +|Decode a secp256k1 key pair with a memory resource from `+crypto::formats::raw+`, `+crypto::formats::pkcs8+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`: decode a composition (its base64, its hexadecimal) with `+codecs::decode+` to the PKCS#8's bytes first, `+codecs::decode>(codecs::formats::base64, text)+`, then decode those with `+crypto::formats::pkcs8+`; or decode the key pair with `+codecs::decode+`, its signing context from the default resource. + |`+keypair_decode_resource_from_text+` |the compile |Give the key pair's decode with a memory resource bytes for `+crypto::formats::raw+` and `+crypto::formats::pkcs8+`, a `+std::span+` or what converts to one (`+std::array+`, `+std::vector+`, a result's `+view()+`); a PEM text decodes with `+crypto::formats::pem(crypto::formats::pkcs8)+`. +|`+keypair_decode_resource_pem_from_bytes+` +|the compile +|Give the key pair's decode with a memory resource a text for `+crypto::formats::pem(crypto::formats::pkcs8)+`: a `+std::string_view+`, a `+std::string+`, or a contiguous sized range of `+char+` such as `+std::span+`; DER bytes decode with `+crypto::formats::pkcs8+` itself. + |`+keypair_decode_resource_spki+` |the compile |Decode a secp256k1 key pair, with or without a memory resource, from one of its formats: `+crypto::formats::raw+`, its secret key, `+crypto::formats::pkcs8+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`; an SPKI holds a public key, which `+crypto::curves::secp256k1::public_key::decode(crypto::formats::spki, bytes)+` gives. @@ -2130,6 +2198,10 @@ On MSVC, each case of `test/msvc` is proven to stop the build, at compile or at |the compile |Call the factory named by its input, member or free: `+from_secret+` for a key pair, `+from_bytes+` for a public key, `+from_key+` for a cipher (`+crypto::from_secret(bytes)+`, `+K::from_secret(bytes)+`); `+make+` is gone, with no alias. +|`+member_der_on_ed25519+` +|the compile +|As `+der_on_ed25519+`: encode an Ed25519 signature with `+signature.encode(crypto::formats::raw)+`, or `+codecs::encode(crypto::formats::raw, signature)+`: its 64 bytes are the signature, and it has no DER form. + |`+memory_missing_definition_from_key+` |the link |Define `+std::pmr::memory_resource& webcpp::crypto::openssl_memory_resource() noexcept+` once in the program, returning a resource that is never destroyed. @@ -2184,15 +2256,15 @@ On MSVC, each case of `test/msvc` is proven to stop the build, at compile or at |`+pem_decode_from_bytes+` |the compile -|Give a PEM to `+crypto::decode+` as a text: a `+std::string_view+`, a `+std::string+`, or a contiguous range of `+char+` such as `+std::span+`; DER bytes decode with `+crypto::formats::spki+` or `+crypto::formats::pkcs8+` themselves. +|Give a PEM to `+codecs::decode+`, or a type's member `+decode+`, as a text: a `+std::string_view+`, a `+std::string+`, or a contiguous sized range of `+char+` such as `+std::span+`; DER bytes decode with `+crypto::formats::spki+` or `+crypto::formats::pkcs8+` themselves. |`+pem_header_removed+` |the compile -|Write and read PEM with `+crypto::encode+` and `+crypto::decode+` and `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`, in `++`, codecs' armor over the key's DER; `+pem.hpp+` is gone, with no header left at its path. +|Write and read PEM with `+codecs::encode+` and `+codecs::decode+` and `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`, in `++`, codecs' armor over the key's DER; `+pem.hpp+` is gone, with no header left at its path. |`+pem_namespace_removed+` |the compile -|Write and read PEM with `+crypto::encode+` and `+crypto::decode+` and `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`, which take the label from the inner format; `+crypto::pem+`, its `+armour+`, `+unarmour+`, labels and `+armoured_size+`, is gone, with no alias. +|Write and read PEM with `+codecs::encode+` and `+codecs::decode+` and `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`, which take the label from the inner format; `+crypto::pem+`, its `+armour+`, `+unarmour+`, labels and `+armoured_size+`, is gone, with no alias. |`+pem_of_der+` |the compile @@ -2202,6 +2274,14 @@ On MSVC, each case of `test/msvc` is proven to stop the build, at compile or at |the compile |As `+pem_of_der+`: armor the DER of a key, `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`; a key's raw bytes have no PEM. +|`+pem_other_armor+` +|the compile +|Encode or decode a key's PEM with `+crypto::formats::pem(crypto::formats::spki)+` or `+crypto::formats::pem(crypto::formats::pkcs8)+`, whose label, `+PUBLIC KEY+` or `+PRIVATE KEY+`, is in its type; a text under another label, a certificate's among them, is no key, and decoding it gives `+codecs::error::invalid_prefix+`. + +|`+pem_other_basic_armor+` +|the compile +|As `+pem_other_armor+`: name a key's PEM by `+crypto::formats::pem(F)+`, never by `+codecs::formats::basic_armor+` of a label, so that the label is always the inner format's. + |`+ph_removed+` |the compile |Name Ed25519ph's streaming signer `+curves::ed25519::prehashed_signer+`, the same `+begin+`, `+update+`, `+sign+` and `+verify+`; `+curves::ed25519::ph+` is gone, with no alias. @@ -2520,7 +2600,7 @@ On MSVC, each case of `test/msvc` is proven to stop the build, at compile or at |`+xonly_into_public_key+` |the compile -|A 32-byte x-only key decodes into `+crypto::curves::secp256k1::xonly_public_key+`: `+crypto::decode(crypto::formats::raw(crypto::formats::xonly), bytes)+`, or its member `+xonly_public_key::decode+`; `+raw(crypto::formats::xonly)+` encodes a public key's x-only key, and its 33- or 65-byte SEC 1 point decodes into a `+public_key+` with `+crypto::formats::raw+`. +|A 32-byte x-only key decodes into `+crypto::curves::secp256k1::xonly_public_key+`: `+codecs::decode(crypto::formats::raw(crypto::formats::xonly), bytes)+`, or its member `+xonly_public_key::decode+`; `+raw(crypto::formats::xonly)+` encodes a public key's x-only key, and its 33- or 65-byte SEC 1 point decodes into a `+public_key+` with `+crypto::formats::raw+`. |=== Elsewhere than MSVC these mistakes build, and nothing stops them; the fixtures record it: diff --git a/doc/generated/namespace-root.adoc b/doc/generated/namespace-root.adoc index b279056..6038486 100644 --- a/doc/generated/namespace-root.adoc +++ b/doc/generated/namespace-root.adoc @@ -53,7 +53,7 @@ |none |none |its values are the errors -|codecs' `invalid_prefix`, `invalid_length`, `invalid_character`, `invalid_padding` for the armor; `invalid_encoding` for its DER +|codecs' `invalid_prefix`, `invalid_length`, `invalid_character`, `invalid_padding` for the armor; `invalid_encoding` for its DER; `input_too_long` past the inner format's bound |`memory_functions_refused`, `memory_functions_not_installed` |`verify`: `invalid_signature`, and `invalid_public_key` for 32 x-only bytes that are no point; `sign`: `invalid_state`, `entropy_unavailable` (Schnorr's drawn randomness), `backend_failure`; a scheme and an input that do not go together, and a policy the scheme does not take or none, stop the build |the concepts: none, checked at compile time; `sign` and `agree`: what the key pair's own returns, and a key pair that does not do the operation, or arguments it does not take, stop the build @@ -77,7 +77,7 @@ |a secret sits in a `std::string`, a `std::vector` or an array of the program's and must not outlive its use; never `memset`, which the optimiser may drop; <> |crypto returns a secret: it wipes itself when destroyed or moved from, is copied only by `clone()`, and gives its bytes with `bytes()`; keep it in one variable; <> |you branch on a failure: forged data, a wrong size, no entropy -|a key goes to or comes from a file, a form or a tool as `-----BEGIN PUBLIC KEY-----` text: `crypto::encode(formats::pem(formats::spki), public_key)`, or `formats::pem(formats::pkcs8)` of a key pair, on every curve, codecs' armor over the DER with the label of the inner format; <> +|a key goes to or comes from a file, a form or a tool as `-----BEGIN PUBLIC KEY-----` text: `codecs::encode(formats::pem(formats::spki), public_key)`, or `formats::pem(formats::pkcs8)` of a key pair, on every curve, codecs' armor over the DER with the label of the inner format; <> |a native program on OpenSSL must allocate only from its own arena; on WebAssembly, BearSSL allocates nothing |a key pair signs with `key_pair.sign(scheme, input)` or `sign(key_pair, scheme, input)`, and a signature is checked with `public_key.verify(scheme, policy, signature, input)` or `verify(scheme, policy, signature, input, public_key)`, the policy one the scheme takes (<>); a digest computed elsewhere, such as a Bitcoin sighash, an Ethereum EIP-191 digest or the SHA-512 Ed25519ph signs, goes in marked as `prehashed`, never as a bare span; the two recoverable schemes have no `verify`: their signature gives its key to `curves::secp256k1::recover`; a signature is a type of its curve and scheme, made from received bytes by `from_bytes` (<>) |a template takes any curve's key pair: `from_secret`, `generate`, a typed `public_key()`, `secret()`; a `signing_keypair` (Ed25519, P-256, secp256k1) adds `sign(scheme, input)`, whose `crypto::sign(key_pair, scheme, input)` is the free form, an `agreement_keypair` (X25519, P-256, secp256k1) `agree(peer)`, whose free form is `agree(key_pair, peer)` (<>) diff --git a/doc/generated/use-cases.adoc b/doc/generated/use-cases.adoc index 2f82efd..df9c0f6 100644 --- a/doc/generated/use-cases.adoc +++ b/doc/generated/use-cases.adoc @@ -117,7 +117,7 @@ |<> |Store or exchange a secp256k1 key with OpenSSL, node or a config file, or publish its x-only key (the point, compressed or not, x-only, SPKI, PKCS#8, as DER or PEM text) -|In `+curves::secp256k1+`: `+crypto::encode+` and `+crypto::decode+` with `+crypto::formats::raw+`, `+spki+`, `+pkcs8+` and `+pem+`, and `+crypto::formats::compressed+` and `+xonly+`, on secp256k1's keys +|In `+curves::secp256k1+`: `+codecs::encode+` and `+codecs::decode+` with `+crypto::formats::raw+`, `+spki+`, `+pkcs8+` and `+pem+`, and `+crypto::formats::compressed+` and `+xonly+`, on secp256k1's keys |libsecp256k1 |`+/webcpp/crypto//crypto+` + `+/third_party//secp256k1+` @@ -153,7 +153,7 @@ |<> |Read or write a wallet's key as WIF and its address as Base58Check text (a domain library's job) -|In `+curves::secp256k1+`: `+crypto::decode(crypto::formats::raw, bytes)+`, `+keypair.encode(crypto::formats::raw)+`, `+crypto::encode(crypto::formats::raw(crypto::formats::compressed), public_key)+`, with webcpp::hash's `+hash::formats::base58_with_checksum+` +|In `+curves::secp256k1+`: `+codecs::decode(crypto::formats::raw, bytes)+`, `+keypair.encode(crypto::formats::raw)+`, `+codecs::encode(crypto::formats::raw(crypto::formats::compressed), public_key)+`, with webcpp::hash's `+hash::formats::base58_with_checksum+` |libsecp256k1 |`+/webcpp/crypto//crypto+` + `+/third_party//secp256k1+` + @@ -222,7 +222,7 @@ |<> |Export or import an Ed25519 or X25519 key in the standard forms OpenSSL and WebCrypto use: SPKI and PKCS#8 (RFC 8410), as DER or PEM text -|In `+curves+`: `+crypto::encode+` and `+crypto::decode+` with `+crypto::formats::raw+`, `+spki+`, `+pkcs8+` and `+pem+`, on Ed25519's and X25519's keys +|In `+curves+`: `+codecs::encode+` and `+codecs::decode+` with `+crypto::formats::raw+`, `+spki+`, `+pkcs8+` and `+pem+`, on Ed25519's and X25519's keys |libsodium |`+/webcpp/crypto//crypto+` + `+/webcpp/crypto//ed25519-default+` + @@ -274,7 +274,7 @@ |<> |Sign what a browser's WebCrypto verifies and verify what it signs (ECDSA on P-256 with SHA-256), low S where a protocol requires it, as webcpp::trystero's handshakes -|In `+curves::p256+`: `+keypair::sign(crypto::signing_schemes::ecdsa_sha256, message)+`, `+crypto::verify+`, `+signature.is_low_s()+`, `+signature.low_s()+`; `+crypto::encode+` and `+crypto::decode+` with `+crypto::formats::der+` for X.509 +|In `+curves::p256+`: `+keypair::sign(crypto::signing_schemes::ecdsa_sha256, message)+`, `+crypto::verify+`, `+signature.is_low_s()+`, `+signature.low_s()+`; `+codecs::encode+` and `+codecs::decode+` with `+crypto::formats::der+` for X.509 |BearSSL |`+/webcpp/crypto//crypto+` + `+/webcpp/crypto//p256-default+` @@ -288,14 +288,14 @@ |<> |Export or import a P-256 key in the forms OpenSSL and WebCrypto's raw, spki and pkcs8 use (the point, compressed or not, SPKI, PKCS#8), as DER or PEM text -|In `+curves::p256+`: `+crypto::encode+` and `+crypto::decode+` with `+crypto::formats::raw+`, `+spki+`, `+pkcs8+` and `+pem+`, and `+crypto::formats::compressed+`, on P-256's keys +|In `+curves::p256+`: `+codecs::encode+` and `+codecs::decode+` with `+crypto::formats::raw+`, `+spki+`, `+pkcs8+` and `+pem+`, and `+crypto::formats::compressed+`, on P-256's keys |BearSSL |`+/webcpp/crypto//crypto+` + `+/webcpp/crypto//p256-default+` |<> |Register a public key a browser or a device uploads as PEM, store it as the one SPKI, and verify its signatures, high S too -|In `+curves::p256+`: `+crypto::decode(crypto::formats::pem(crypto::formats::spki), text)+`, `+crypto::encode(crypto::formats::spki, key)+`, `+crypto::verify+` under `+crypto::verification_policies::any_s+` +|In `+curves::p256+`: `+codecs::decode(crypto::formats::pem(crypto::formats::spki), text)+`, `+codecs::encode(crypto::formats::spki, key)+`, `+crypto::verify+` under `+crypto::verification_policies::any_s+` |BearSSL |`+/webcpp/crypto//crypto+` + `+/webcpp/crypto//p256-default+` diff --git a/doc/llms.txt b/doc/llms.txt index b413455..856359d 100644 --- a/doc/llms.txt +++ b/doc/llms.txt @@ -162,18 +162,18 @@ Generated by doc/tables/generate.py with the page's tables. Page: https://webcpp - [curves::x25519::public_key](https://webcpporg.github.io/libs/crypto/#crypto-x25519): An X25519 public key, its 32 bytes, which a key pair's `agree` takes: made from bytes with only their size checked, since a key of low order is `agree`'s refusal, `weak_public_key`. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: agree a shared secret with a peer's public key (X25519, RFC 7748), or with the X25519 form of an Ed25519 key. - from_bytes: Makes a public key from its 32 bytes, as RFC 7748 encodes it and WebCrypto's `exportKey("raw")` writes it. - bytes: The key's bytes. -- [crypto::encode, crypto::decode, curves::ed25519::public_key::encode, curves::ed25519::public_key::decode, curves::ed25519::keypair::encode, curves::ed25519::keypair::decode, curves::ed25519::signature::encode, curves::ed25519::signature::decode](https://webcpporg.github.io/libs/crypto/#crypto-key-formats): Ed25519's formats as codecs value codecs: a public key's `formats::raw` (32 bytes), `formats::spki` (RFC 8410, 44 bytes) and `formats::pem(formats::spki)`; a key pair's `formats::raw` (its seed), `formats::pkcs8` (RFC 8410's version 1, 48 bytes) and `formats::pem(formats::pkcs8)`, secret; a signature's `formats::raw` (64 bytes); the forms OpenSSL and WebCrypto write, the DER decoded strictly: any other bytes are `invalid_encoding`. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: export or import an Ed25519 or X25519 key in the standard forms OpenSSL and WebCrypto use: SPKI and PKCS#8 (RFC 8410), as DER or PEM text. - - public_key::encode: Encodes the key in `format`, as `crypto::encode(format, key)` does. - - public_key::decode: Decodes a public key from `input` in `format`, as `crypto::decode` does. - - basic_keypair::encode: Encodes the key pair in `format`, as `crypto::encode(format, key_pair)` does. - - basic_keypair::decode: Decodes a key pair from `input` in `format`, as `crypto::decode` does. - - basic_signature::encode: Encodes the signature in `format`, as `crypto::encode(format, signature)` does. - - basic_signature::decode: Decodes a signature from `input` in `format`, as `crypto::decode` does. -- [crypto::encode, crypto::decode, curves::x25519::public_key::encode, curves::x25519::public_key::decode, curves::x25519::keypair::encode, curves::x25519::keypair::decode](https://webcpporg.github.io/libs/crypto/#crypto-key-formats): X25519's formats as codecs value codecs: a public key's `formats::raw` (32 bytes), `formats::spki` (RFC 8410, 44 bytes) and `formats::pem(formats::spki)`; a key pair's `formats::raw` (its secret key), `formats::pkcs8` (RFC 8410's version 1, 48 bytes) and `formats::pem(formats::pkcs8)`, secret; the forms OpenSSL and WebCrypto write, an SPKI decoded into the typed key, the DER decoded strictly: any other bytes are `invalid_encoding`. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: export or import an Ed25519 or X25519 key in the standard forms OpenSSL and WebCrypto use: SPKI and PKCS#8 (RFC 8410), as DER or PEM text. - - public_key::encode: Encodes the key in `format`, as `crypto::encode(format, key)` does. - - public_key::decode: Decodes a public key from `input` in `format`, as `crypto::decode` does. - - basic_keypair::encode: Encodes the key pair in `format`, as `crypto::encode(format, key_pair)` does. - - basic_keypair::decode: Decodes a key pair from `input` in `format`, as `crypto::decode` does. +- [codecs::encode, codecs::decode, curves::ed25519::public_key::encode, curves::ed25519::public_key::decode, curves::ed25519::keypair::encode, curves::ed25519::keypair::decode, curves::ed25519::signature::encode, curves::ed25519::signature::decode](https://webcpporg.github.io/libs/crypto/#crypto-key-formats): Ed25519's formats as codecs value codecs: a public key's `formats::raw` (32 bytes), `formats::spki` (RFC 8410, 44 bytes) and `formats::pem(formats::spki)`; a key pair's `formats::raw` (its seed), `formats::pkcs8` (RFC 8410's version 1, 48 bytes) and `formats::pem(formats::pkcs8)`, secret; a signature's `formats::raw` (64 bytes); the forms OpenSSL and WebCrypto write, the DER decoded strictly: any other bytes are `invalid_encoding`, and an input longer than the pair's bound (32 bytes, 44 for an SPKI, 48 for a PKCS#8, 64 for a signature's `raw`) is `codecs::error::input_too_long`, as a PEM's payload is. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: export or import an Ed25519 or X25519 key in the standard forms OpenSSL and WebCrypto use: SPKI and PKCS#8 (RFC 8410), as DER or PEM text. + - public_key::encode: Encodes the key in `format`, as `codecs::encode(format, key)` does. + - public_key::decode: Decodes a public key from `input` in `format`, as `codecs::decode` does. + - basic_keypair::encode: Encodes the key pair in `format`, as `codecs::encode(format, key_pair)` does. + - basic_keypair::decode: Decodes a key pair from `input` in `format`, as `codecs::decode` does. + - basic_signature::encode: Encodes the signature in `format`, as `codecs::encode(format, signature)` does. + - basic_signature::decode: Decodes a signature from `input` in `format`, as `codecs::decode` does. +- [codecs::encode, codecs::decode, curves::x25519::public_key::encode, curves::x25519::public_key::decode, curves::x25519::keypair::encode, curves::x25519::keypair::decode](https://webcpporg.github.io/libs/crypto/#crypto-key-formats): X25519's formats as codecs value codecs: a public key's `formats::raw` (32 bytes), `formats::spki` (RFC 8410, 44 bytes) and `formats::pem(formats::spki)`; a key pair's `formats::raw` (its secret key), `formats::pkcs8` (RFC 8410's version 1, 48 bytes) and `formats::pem(formats::pkcs8)`, secret; the forms OpenSSL and WebCrypto write, an SPKI decoded into the typed key, the DER decoded strictly: any other bytes are `invalid_encoding`, and an input longer than the pair's bound (32 bytes, 44 for an SPKI, 48 for a PKCS#8) is `codecs::error::input_too_long`, as a PEM's payload is. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: export or import an Ed25519 or X25519 key in the standard forms OpenSSL and WebCrypto use: SPKI and PKCS#8 (RFC 8410), as DER or PEM text. + - public_key::encode: Encodes the key in `format`, as `codecs::encode(format, key)` does. + - public_key::decode: Decodes a public key from `input` in `format`, as `codecs::decode` does. + - basic_keypair::encode: Encodes the key pair in `format`, as `codecs::encode(format, key_pair)` does. + - basic_keypair::decode: Decodes a key pair from `input` in `format`, as `codecs::decode` does. - [curves::secp256k1::recover](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-recovery): The public key that made a recoverable ECDSA signature, `recover(crypto::signing_schemes::ecdsa_sha256_recoverable, crypto::verification_policies::low_s_only, signature, input)` or Ethereum's `recover(crypto::signing_schemes::ecdsa_keccak256_recoverable, crypto::verification_policies::low_s_only, signature, input)`, the input as signing took it: a message hashed inside (with SHA-256, or Keccak-256) or `crypto::signing_schemes::prehashed{digest}`; a high S is refused before recovery. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. Use it to: find who signed a message or a transaction without being sent their key (Ethereum's signatures). - recover: The public key that made a recoverable signature (`secp256k1_ecdsa_recover`) of ECDSA over SHA-256: the same inputs as `sign(crypto::signing_schemes::ecdsa_sha256_recoverable, ...)`. - [curves::secp256k1::public_key](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-keys): A secp256k1 public key, a point checked on the curve, which `crypto::verify` and a key pair's `agree` take: made from its compressed or uncompressed SEC 1 form, and giving its compressed and uncompressed forms and its x-only key, a `curves::secp256k1::xonly_public_key`. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. @@ -190,16 +190,16 @@ Generated by doc/tables/generate.py with the page's tables. Page: https://webcpp - public_key_tweak_mul: A public key P times t (`secp256k1_ec_pubkey_tweak_mul`). - public_key_negate: The negation -P of a public key (`secp256k1_ec_pubkey_negate`). - public_key_combine: The sum of public keys (`secp256k1_ec_pubkey_combine`), in a workspace the caller lends for libsecp256k1's array of pointers. -- [crypto::encode, crypto::decode, curves::secp256k1::public_key::encode, curves::secp256k1::public_key::decode, curves::secp256k1::xonly_public_key::encode, curves::secp256k1::xonly_public_key::decode, curves::secp256k1::keypair::encode, curves::secp256k1::keypair::decode (also with a std::pmr::memory_resource), curves::secp256k1::ecdsa_signature::encode, curves::secp256k1::ecdsa_signature::decode, curves::secp256k1::recoverable_signature::encode, curves::secp256k1::recoverable_signature::decode, curves::secp256k1::schnorr_signature::encode, curves::secp256k1::schnorr_signature::decode](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-formats): secp256k1's formats as codecs value codecs: a public key's `formats::raw` (SEC 1's uncompressed point, 65 bytes; `raw(formats::compressed)` 33; `raw(formats::xonly)` 32, to encode only), `formats::spki` (RFC 5480, 88 bytes, the default in this version, where it was the compressed point before; `spki(formats::compressed)` 56) and `formats::pem(formats::spki)`, the default forms decoding both points, so that a stored compressed SPKI still decodes; an x-only key's `formats::raw` (BIP340's 32 bytes, by default and as `raw(formats::xonly)`); a key pair's `formats::raw` (its secret key) and `formats::pkcs8` (RFC 5915's ECPrivateKey with its uncompressed public key and no parameters, 135 bytes) and `formats::pem(formats::pkcs8)`, secret, a decoded key pair's context from the default memory resource, or from the program's own with `keypair::decode(format, input, memory_resource)`; an ECDSA signature's `formats::raw` (r then s, 64 bytes) and `formats::der` (strict DER, BIP66, 8 to 72 bytes, r or s 0 or not below n refused), a recoverable signature's `formats::raw` (65 bytes) and a BIP340 signature's (64); the forms OpenSSL and node write, each decoded strictly: `spki` and `pkcs8` refuse any other bytes with `invalid_encoding`, a public key's and an x-only key's `raw` refuse with `invalid_public_key`, as `from_bytes` does, a key pair's `raw` with `invalid_key_size` or `invalid_secret_key`, as `from_secret` does, and `der` and a signature's `raw` refuse with `invalid_signature`. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. Use it to: store or exchange a secp256k1 key with OpenSSL, node or a config file, or publish its x-only key (the point, compressed or not, x-only, SPKI, PKCS#8, as DER or PEM text). - - public_key::encode: Encodes the key in `format`, as `crypto::encode(format, key)` does. - - public_key::decode: Decodes a public key from `input` in `format`, as `crypto::decode` does. - - xonly_public_key::encode: Encodes the x-only key in `format`, as `crypto::encode(format, key)` does. - - xonly_public_key::decode: Decodes an x-only key from `input` in `format`, as `crypto::decode` does. - - basic_keypair::encode: Encodes the key pair in `format`, as `crypto::encode(format, key_pair)` does. - - basic_keypair::decode: Decodes a key pair from `input` in `format`, as `crypto::decode` does. +- [codecs::encode, codecs::decode, curves::secp256k1::public_key::encode, curves::secp256k1::public_key::decode, curves::secp256k1::xonly_public_key::encode, curves::secp256k1::xonly_public_key::decode, curves::secp256k1::keypair::encode, curves::secp256k1::keypair::decode (also with a std::pmr::memory_resource), curves::secp256k1::ecdsa_signature::encode, curves::secp256k1::ecdsa_signature::decode, curves::secp256k1::recoverable_signature::encode, curves::secp256k1::recoverable_signature::decode, curves::secp256k1::schnorr_signature::encode, curves::secp256k1::schnorr_signature::decode](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-formats): secp256k1's formats as codecs value codecs: a public key's `formats::raw` (SEC 1's uncompressed point, 65 bytes; `raw(formats::compressed)` 33; `raw(formats::xonly)` 32, to encode only), `formats::spki` (RFC 5480, 88 bytes, the default in this version, where it was the compressed point before; `spki(formats::compressed)` 56) and `formats::pem(formats::spki)`, the default forms decoding both points, so that a stored compressed SPKI still decodes; an x-only key's `formats::raw` (BIP340's 32 bytes, by default and as `raw(formats::xonly)`); a key pair's `formats::raw` (its secret key) and `formats::pkcs8` (RFC 5915's ECPrivateKey with its uncompressed public key and no parameters, 135 bytes) and `formats::pem(formats::pkcs8)`, secret, a decoded key pair's context from the default memory resource, or from the program's own with `keypair::decode(format, input, memory_resource)`; an ECDSA signature's `formats::raw` (r then s, 64 bytes) and `formats::der` (strict DER, BIP66, 8 to 72 bytes, r or s 0 or not below n refused), a recoverable signature's `formats::raw` (65 bytes) and a BIP340 signature's (64); the forms OpenSSL and node write, each decoded strictly: `spki` and `pkcs8` refuse any other bytes with `invalid_encoding`, an input past the pair's bound (a PEM's payload too) with `codecs::error::input_too_long`: 65 bytes for a public key's `raw` (33 compressed), 88 for an SPKI (56), 32 for an x-only key's and a key pair's `raw`, 135 for a PKCS#8, 64 for an ECDSA or Schnorr signature's `raw`, 65 for a recoverable one's, 72 for a `der`; within the bound, a public key's and an x-only key's `raw` of a size it does not have refuse with `invalid_public_key`, as `from_bytes` does, a key pair's `raw` shorter than 32 bytes with `invalid_key_size` and a secret key that is not valid with `invalid_secret_key`, as `from_secret` does, and `der` and a signature's `raw` refuse with `invalid_signature`. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. Use it to: store or exchange a secp256k1 key with OpenSSL, node or a config file, or publish its x-only key (the point, compressed or not, x-only, SPKI, PKCS#8, as DER or PEM text). + - public_key::encode: Encodes the key in `format`, as `codecs::encode(format, key)` does. + - public_key::decode: Decodes a public key from `input` in `format`, as `codecs::decode` does. + - xonly_public_key::encode: Encodes the x-only key in `format`, as `codecs::encode(format, key)` does. + - xonly_public_key::decode: Decodes an x-only key from `input` in `format`, as `codecs::decode` does. + - basic_keypair::encode: Encodes the key pair in `format`, as `codecs::encode(format, key_pair)` does. + - basic_keypair::decode: Decodes a key pair from `input` in `format`, as `codecs::decode` does. - basic_keypair::decode(std::pmr::memory_resource): Decodes a key pair from `input` in `format`, its signing context from `memory_resource`, as `from_secret` with a resource takes it: the decode without a resource, which takes the context from `std::pmr::get_default_resource()`, into the program's own arena. - - basic_signature::encode: Encodes the signature in `format`, as `crypto::encode(format, signature)` does. - - basic_signature::decode: Decodes a signature from `input` in `format`, as `crypto::decode` does. + - basic_signature::encode: Encodes the signature in `format`, as `codecs::encode(format, signature)` does. + - basic_signature::decode: Decodes a signature from `input` in `format`, as `codecs::decode` does. - [curves::secp256k1::ellswift_encode, ellswift_decode, ellswift_create, ellswift_xdh](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-ellswift): A public key written as 64 bytes that look random, and BIP 324's key exchange of two such encodings. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. Use it to: make a peer-to-peer connection's key exchange look like random bytes (BIP 324's transport; its packets are a domain library's). - ellswift_encode: A public key's ElligatorSwift encoding (`secp256k1_ellswift_encode`): one of many, chosen by the randomness, each of which decodes to the key. - ellswift_decode: The public key of any 64 bytes (`secp256k1_ellswift_decode`): every input decodes. @@ -256,13 +256,13 @@ Generated by doc/tables/generate.py with the page's tables. Page: https://webcpp - [curves::p256::keypair::agree](https://webcpporg.github.io/libs/crypto/#crypto-p256-ecdh): ECDH with a validated public key: the shared point's raw x coordinate, 32 bytes in a `secure::secret<32>`, as WebCrypto's `deriveBits` gives it; a protocol hashes it into its keys. Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten. Default: BearSSL on native, wasip2, wasip3, emscripten. Use it to: agree on a key with a browser's WebCrypto (ECDH on P-256, deriveBits), then derive the session's keys with HKDF, as Trystero 0.27.0's pairwise keys are derived. - agree: ECDH with a peer's public key: the x coordinate of the shared point d * Q, raw, as WebCrypto's `deriveBits` with ECDH on P-256 gives its 256 bits. - async_agree: ECDH with a peer's public key, asynchronously: `agree` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_agree(completion_executor, key_pair, peer_key, completion_token)`, which calls it. -- [crypto::encode, crypto::decode, curves::p256::public_key::encode, curves::p256::public_key::decode, curves::p256::keypair::encode, curves::p256::keypair::decode, curves::p256::ecdsa_signature::encode, curves::p256::ecdsa_signature::decode](https://webcpporg.github.io/libs/crypto/#crypto-p256-formats): P-256's formats as codecs value codecs: a public key's `formats::raw` (SEC 1's uncompressed point, 65 bytes; `raw(formats::compressed)` 33), `formats::spki` (RFC 5480, 91 bytes; `spki(formats::compressed)` 59) and `formats::pem(formats::spki)`, the default forms decoding both points; a key pair's `formats::raw` (its scalar), `formats::pkcs8` (RFC 5915's ECPrivateKey with its public key and no parameters, 138 bytes) and `formats::pem(formats::pkcs8)`, secret; a signature's `formats::raw` (r then s, 64 bytes) and `formats::der` (strict DER, 8 to 72 bytes, r or s 0 or not below n refused); the forms OpenSSL and WebCrypto write, each decoded strictly: `spki` and `pkcs8` refuse any other bytes with `invalid_encoding`, a public key's `raw` refuses with `invalid_public_key`, as `from_bytes` does, a key pair's `raw` with `invalid_key_size` or `invalid_secret_key`, as `from_secret` does, and `der` and a signature's `raw` refuse with `invalid_signature`. Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten. Default: BearSSL on native, wasip2, wasip3, emscripten. Use it to: export or import a P-256 key in the forms OpenSSL and WebCrypto's raw, spki and pkcs8 use (the point, compressed or not, SPKI, PKCS#8), as DER or PEM text. - - basic_public_key::encode: Encodes the key in `format`, as `crypto::encode(format, key)` does. - - basic_public_key::decode: Decodes a public key from `input` in `format`, as `crypto::decode` does. - - basic_keypair::encode: Encodes the key pair in `format`, as `crypto::encode(format, key_pair)` does. - - basic_keypair::decode: Decodes a key pair from `input` in `format`, as `crypto::decode` does. - - basic_signature::encode: Encodes the signature in `format`, as `crypto::encode(format, signature)` does. - - basic_signature::decode: Decodes a signature from `input` in `format`, as `crypto::decode` does. +- [codecs::encode, codecs::decode, curves::p256::public_key::encode, curves::p256::public_key::decode, curves::p256::keypair::encode, curves::p256::keypair::decode, curves::p256::ecdsa_signature::encode, curves::p256::ecdsa_signature::decode](https://webcpporg.github.io/libs/crypto/#crypto-p256-formats): P-256's formats as codecs value codecs: a public key's `formats::raw` (SEC 1's uncompressed point, 65 bytes; `raw(formats::compressed)` 33), `formats::spki` (RFC 5480, 91 bytes; `spki(formats::compressed)` 59) and `formats::pem(formats::spki)`, the default forms decoding both points; a key pair's `formats::raw` (its scalar), `formats::pkcs8` (RFC 5915's ECPrivateKey with its public key and no parameters, 138 bytes) and `formats::pem(formats::pkcs8)`, secret; a signature's `formats::raw` (r then s, 64 bytes) and `formats::der` (strict DER, 8 to 72 bytes, r or s 0 or not below n refused); the forms OpenSSL and WebCrypto write, each decoded strictly: `spki` and `pkcs8` refuse any other bytes with `invalid_encoding`, an input past the pair's bound (a PEM's payload too) with `codecs::error::input_too_long`: 65 bytes for a public key's `raw` (33 compressed), 91 for an SPKI (59), 32 for a key pair's `raw`, 138 for a PKCS#8, 64 for a signature's `raw`, 72 for a `der`; within the bound, a public key's `raw` of a size it does not have refuses with `invalid_public_key`, as `from_bytes` does, a key pair's `raw` shorter than 32 bytes with `invalid_key_size` and a secret key that is not valid with `invalid_secret_key`, as `from_secret` does, and `der` and a signature's `raw` refuse with `invalid_signature`. Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten. Default: BearSSL on native, wasip2, wasip3, emscripten. Use it to: export or import a P-256 key in the forms OpenSSL and WebCrypto's raw, spki and pkcs8 use (the point, compressed or not, SPKI, PKCS#8), as DER or PEM text. + - basic_public_key::encode: Encodes the key in `format`, as `codecs::encode(format, key)` does. + - basic_public_key::decode: Decodes a public key from `input` in `format`, as `codecs::decode` does. + - basic_keypair::encode: Encodes the key pair in `format`, as `codecs::encode(format, key_pair)` does. + - basic_keypair::decode: Decodes a key pair from `input` in `format`, as `codecs::decode` does. + - basic_signature::encode: Encodes the signature in `format`, as `codecs::encode(format, signature)` does. + - basic_signature::decode: Decodes a signature from `input` in `format`, as `codecs::decode` does. ## webcpp::crypto::public_key_encryption @@ -372,12 +372,12 @@ Generated by doc/tables/generate.py with the page's tables. Page: https://webcpp - [Derive a Nostr direct message's conversation key (NIP-44; the format is a domain library's)](https://webcpporg.github.io/libs/crypto/#crypto-p2p-nostr-dm): `keypair::agree`, then HKDF on Boost.Hash2 (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/ecdh_x_coordinate.cpp. - [Find who signed a message or a transaction without being sent their key (Ethereum's signatures)](https://webcpporg.github.io/libs/crypto/#crypto-wallets-recovery): `keypair::sign(crypto::signing_schemes::ecdsa_keccak256_recoverable, message)`, `recover` (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1 /third_party//xkcp. Example: example/ethereum_personal_sign.cpp. - [Derive child keys for a watch-only wallet (BIP 32's derivation is a domain library's)](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-tweaks): `secret_key_tweak_add`, `public_key_tweak_add` (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/key_tweak.cpp. -- [Store or exchange a secp256k1 key with OpenSSL, node or a config file, or publish its x-only key (the point, compressed or not, x-only, SPKI, PKCS#8, as DER or PEM text)](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-formats): `crypto::encode` and `crypto::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, and `crypto::formats::compressed` and `xonly`, on secp256k1's keys (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/secp256k1_key_formats.cpp. +- [Store or exchange a secp256k1 key with OpenSSL, node or a config file, or publish its x-only key (the point, compressed or not, x-only, SPKI, PKCS#8, as DER or PEM text)](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-formats): `codecs::encode` and `codecs::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, and `crypto::formats::compressed` and `xonly`, on secp256k1's keys (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/secp256k1_key_formats.cpp. - [Make a peer-to-peer connection's key exchange look like random bytes (BIP 324's transport; its packets are a domain library's)](https://webcpporg.github.io/libs/crypto/#crypto-p2p-v2): `ellswift_create`, `ellswift_decode`, `ellswift_xdh` (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/ellswift_handshake.cpp. - [Publish one donation address that never shows two payments as related (BIP 352's addresses and transactions are a domain library's)](https://webcpporg.github.io/libs/crypto/#crypto-wallets-silent): `create_outputs`, `make_previous_outputs_summary`, `scan_outputs` (webcpp::crypto::curves::secp256k1::silent_payments); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/silent_payment.cpp. - [Require two devices to sign for a wallet, with one ordinary signature on the chain](https://webcpporg.github.io/libs/crypto/#crypto-wallets-musig): `key_aggregate`, `generate_nonce`, `partial_sign`, `partial_signature_aggregate` (webcpp::crypto::curves::secp256k1::musig); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/musig_wallet.cpp. - [Make and keep a wallet's key](https://webcpporg.github.io/libs/crypto/#crypto-wallets-key): `keypair::from_secret`, `keypair::public_key`, `public_key::xonly`, `public_key::compressed`; `aes256_gcm` at rest (webcpp::crypto::curves::secp256k1); backend libsecp256k1, and AES-GCM's default; Jamfile /webcpp/crypto//crypto /third_party//secp256k1 /webcpp/crypto//gcm-default. Example: example/wallet_key.cpp. -- [Read or write a wallet's key as WIF and its address as Base58Check text (a domain library's job)](https://webcpporg.github.io/libs/crypto/#crypto-wallets-wif): `crypto::decode(crypto::formats::raw, bytes)`, `keypair.encode(crypto::formats::raw)`, `crypto::encode(crypto::formats::raw(crypto::formats::compressed), public_key)`, with webcpp::hash's `hash::formats::base58_with_checksum` (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1 /webcpp/hash//base58_with_checksum. Example: example/wallet_wif.cpp. +- [Read or write a wallet's key as WIF and its address as Base58Check text (a domain library's job)](https://webcpporg.github.io/libs/crypto/#crypto-wallets-wif): `codecs::decode(crypto::formats::raw, bytes)`, `keypair.encode(crypto::formats::raw)`, `codecs::encode(crypto::formats::raw(crypto::formats::compressed), public_key)`, with webcpp::hash's `hash::formats::base58_with_checksum` (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1 /webcpp/hash//base58_with_checksum. Example: example/wallet_wif.cpp. - [Sign a Bitcoin SegWit v0 input (a domain library's job)](https://webcpporg.github.io/libs/crypto/#crypto-wallets-der): `keypair::sign(crypto::signing_schemes::ecdsa_sha256, crypto::signing_schemes::prehashed{sighash})`, `signature.encode(crypto::formats::der)`, `signature.is_low_s()`, `signature.low_s()` (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/bitcoin_sighash.cpp. - [Derive a Bitcoin Taproot output key and spend by key path (a domain library's job)](https://webcpporg.github.io/libs/crypto/#crypto-wallets-taproot): `keypair::xonly_public_key`, `xonly_public_key::tweak_add`, `xonly_public_key::verify_tweak`, `keypair::tweak_xonly` (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/taproot_tweak.cpp. - [Sign where allocation is restricted to the program's arena](https://webcpporg.github.io/libs/crypto/#crypto-memory): `keypair::from_secret(secret, resource)`, or a stored key's `keypair::decode(crypto::formats::pkcs8, bytes, resource)` (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/arena_signer.cpp. @@ -387,17 +387,17 @@ Generated by doc/tables/generate.py with the page's tables. Page: https://webcpp - [Sign and verify with Ed25519 (RFC 8032), or with Ed25519ph a message's SHA-512 or a large message in pieces](https://webcpporg.github.io/libs/crypto/#crypto-ed25519): `keypair::from_secret`, `keypair::generate`, `keypair::sign(crypto::signing_schemes::eddsa, message)`, `keypair::sign(crypto::signing_schemes::eddsa_prehashed, crypto::signing_schemes::prehashed{digest})`, `crypto::verify`; `prehashed_signer` (webcpp::crypto::curves::ed25519); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//ed25519-default. Example: example/ed25519_sign.cpp. - [Agree a shared secret with a peer's public key (X25519, RFC 7748), or with the X25519 form of an Ed25519 key](https://webcpporg.github.io/libs/crypto/#crypto-x25519): `keypair::from_secret`, `keypair::generate`, `public_key::from_bytes`, `keypair::agree`; `curves::ed25519::keypair::to_x25519`, `curves::ed25519::public_key::to_x25519`, `crypto::to_x25519` (webcpp::crypto::curves::x25519); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//x25519-default. Example: example/x25519_agree.cpp. - [Give a client and a server a key for each direction from each other's public key, as libsodium's `crypto_kx` does](https://webcpporg.github.io/libs/crypto/#crypto-kx): `kx_keypair::from_keypair`, `kx_keypair::derive_from_seed`, `kx_keypair::generate`, `client_session_keys`, `server_session_keys` (webcpp::crypto::key_exchange); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//kx-default. Example: example/kx_session.cpp. -- [Export or import an Ed25519 or X25519 key in the standard forms OpenSSL and WebCrypto use: SPKI and PKCS#8 (RFC 8410), as DER or PEM text](https://webcpporg.github.io/libs/crypto/#crypto-key-formats): `crypto::encode` and `crypto::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, on Ed25519's and X25519's keys (webcpp::crypto::curves); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//ed25519-default /webcpp/crypto//x25519-default. Example: example/key_formats.cpp. +- [Export or import an Ed25519 or X25519 key in the standard forms OpenSSL and WebCrypto use: SPKI and PKCS#8 (RFC 8410), as DER or PEM text](https://webcpporg.github.io/libs/crypto/#crypto-key-formats): `codecs::encode` and `codecs::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, on Ed25519's and X25519's keys (webcpp::crypto::curves); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//ed25519-default /webcpp/crypto//x25519-default. Example: example/key_formats.cpp. - [Encrypt messages in a chat app, on the web and natively, each with a random nonce](https://webcpporg.github.io/libs/crypto/#crypto-chacha): `xchacha20_poly1305<>`: `from_key`, `seal`, `open` (webcpp::crypto::ciphers); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//xchacha20_poly1305-default. Example: example/xchacha_seal.cpp. - [Encrypt a record or a file under one secret key, each with a random nonce, in libsodium's secret-box format](https://webcpporg.github.io/libs/crypto/#crypto-secretbox): `secretbox<>`, `secretbox_xchacha20poly1305<>`: `from_key`, `seal(nonce, data)`, `open(nonce, data, tag)`; the box forms `seal(box, nonce, data)`, `open(box, nonce, data, tag)` (webcpp::crypto::secret_key_encryption); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//secretbox_xsalsa20poly1305-default. Example: example/file_share.cpp. - [Send messages between two people who know each other's public keys, as NaCl's and libsodium's crypto_box](https://webcpporg.github.io/libs/crypto/#crypto-box): `box::from_keys`, `box::seal`, `box::open` (webcpp::crypto::public_key_encryption); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//box_xsalsa20poly1305-default. Example: example/box_messages.cpp. - [Let anyone send a message that only the recipient can read, without saying who sent it (anonymous feedback, a drop box)](https://webcpporg.github.io/libs/crypto/#crypto-sealed-box): `sealed_box::seal`, `sealed_box::open` (webcpp::crypto::public_key_encryption); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//sealed_box-default. Example: example/file_share.cpp. - [Share a file with someone known only by a public key, as a file-sharing app does](https://webcpporg.github.io/libs/crypto/#crypto-everyday-files): `secret_key_encryption::secretbox<>` for the file under a key drawn for it, `public_key_encryption::sealed_box::seal` for that key (webcpp::crypto); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//secretbox_xsalsa20poly1305-default /webcpp/crypto//sealed_box-default /webcpp/crypto//x25519-default. Example: example/file_share.cpp. - [Make and keep a P-256 key pair, and import a browser's public key (WebCrypto's raw form), as webcpp::trystero's peers do](https://webcpporg.github.io/libs/crypto/#crypto-p256-keys): `keypair::from_secret`, `keypair::generate`, `keypair::public_key`, `keypair::secret`, `public_key::from_bytes` (webcpp::crypto::curves::p256); backend BearSSL; Jamfile /webcpp/crypto//crypto /webcpp/crypto//p256-default. Example: example/p256_keys.cpp. -- [Sign what a browser's WebCrypto verifies and verify what it signs (ECDSA on P-256 with SHA-256), low S where a protocol requires it, as webcpp::trystero's handshakes](https://webcpporg.github.io/libs/crypto/#crypto-p256-ecdsa): `keypair::sign(crypto::signing_schemes::ecdsa_sha256, message)`, `crypto::verify`, `signature.is_low_s()`, `signature.low_s()`; `crypto::encode` and `crypto::decode` with `crypto::formats::der` for X.509 (webcpp::crypto::curves::p256); backend BearSSL; Jamfile /webcpp/crypto//crypto /webcpp/crypto//p256-default. Example: example/p256_sign.cpp. +- [Sign what a browser's WebCrypto verifies and verify what it signs (ECDSA on P-256 with SHA-256), low S where a protocol requires it, as webcpp::trystero's handshakes](https://webcpporg.github.io/libs/crypto/#crypto-p256-ecdsa): `keypair::sign(crypto::signing_schemes::ecdsa_sha256, message)`, `crypto::verify`, `signature.is_low_s()`, `signature.low_s()`; `codecs::encode` and `codecs::decode` with `crypto::formats::der` for X.509 (webcpp::crypto::curves::p256); backend BearSSL; Jamfile /webcpp/crypto//crypto /webcpp/crypto//p256-default. Example: example/p256_sign.cpp. - [Agree on a key with a browser's WebCrypto (ECDH on P-256, deriveBits), then derive the session's keys with HKDF, as Trystero 0.27.0's pairwise keys are derived](https://webcpporg.github.io/libs/crypto/#crypto-p256-ecdh): `keypair::agree`; `webcpp::hash::hkdf` for the keys (webcpp::crypto::curves::p256); backend BearSSL; Jamfile /webcpp/crypto//crypto /webcpp/crypto//p256-default. Example: example/p256_ecdh.cpp. -- [Export or import a P-256 key in the forms OpenSSL and WebCrypto's raw, spki and pkcs8 use (the point, compressed or not, SPKI, PKCS#8), as DER or PEM text](https://webcpporg.github.io/libs/crypto/#crypto-p256-formats): `crypto::encode` and `crypto::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, and `crypto::formats::compressed`, on P-256's keys (webcpp::crypto::curves::p256); backend BearSSL; Jamfile /webcpp/crypto//crypto /webcpp/crypto//p256-default. Example: example/p256_key_formats.cpp. -- [Register a public key a browser or a device uploads as PEM, store it as the one SPKI, and verify its signatures, high S too](https://webcpporg.github.io/libs/crypto/#crypto-everyday-uploaded-key): `crypto::decode(crypto::formats::pem(crypto::formats::spki), text)`, `crypto::encode(crypto::formats::spki, key)`, `crypto::verify` under `crypto::verification_policies::any_s` (webcpp::crypto::curves::p256); backend BearSSL; Jamfile /webcpp/crypto//crypto /webcpp/crypto//p256-default. Example: example/uploaded_key.cpp. +- [Export or import a P-256 key in the forms OpenSSL and WebCrypto's raw, spki and pkcs8 use (the point, compressed or not, SPKI, PKCS#8), as DER or PEM text](https://webcpporg.github.io/libs/crypto/#crypto-p256-formats): `codecs::encode` and `codecs::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, and `crypto::formats::compressed`, on P-256's keys (webcpp::crypto::curves::p256); backend BearSSL; Jamfile /webcpp/crypto//crypto /webcpp/crypto//p256-default. Example: example/p256_key_formats.cpp. +- [Register a public key a browser or a device uploads as PEM, store it as the one SPKI, and verify its signatures, high S too](https://webcpporg.github.io/libs/crypto/#crypto-everyday-uploaded-key): `codecs::decode(crypto::formats::pem(crypto::formats::spki), text)`, `codecs::encode(crypto::formats::spki, key)`, `crypto::verify` under `crypto::verification_policies::any_s` (webcpp::crypto::curves::p256); backend BearSSL; Jamfile /webcpp/crypto//crypto /webcpp/crypto//p256-default. Example: example/uploaded_key.cpp. - [Sign a peer-to-peer handshake a browser's WebCrypto verifies, low S, check the peer's, and derive the session key from ECDH (Trystero's protocol is webcpp::trystero's)](https://webcpporg.github.io/libs/crypto/#crypto-p2p-handshake): `keypair::sign(crypto::signing_schemes::ecdsa_sha256, crypto::signing_schemes::prehashed{digest})`, `signature.is_low_s()`, `crypto::verify`, `keypair::agree`; `webcpp::hash::hkdf` for the key (webcpp::crypto::curves::p256); backend BearSSL; Jamfile /webcpp/crypto//crypto /webcpp/crypto//p256-default. Example: example/p256_handshake.cpp. ## Examples @@ -419,7 +419,7 @@ Generated by doc/tables/generate.py with the page's tables. Page: https://webcpp - example/failure_reasons.cpp: Shows the three ways AES-GCM can refuse a message: a tampered ciphertext, a nonce of the wrong size and a tag of the wrong size. A program must tell the first from the others: only authentication_failed means someone changed the data, and open then wipes the plaintext so that no unauthenticated bytes are ever used. - example/file_share.cpp: A file-sharing app shares a file with a recipient it knows only by an X25519 public key: the file is sealed once in a secret box under a key drawn for this file, and that file key is sealed to the recipient's public key in a sealed box. The upload holds both; only the recipient's key pair opens the file key, and the file key opens the file. Prints what the recipient reads, the sizes on the wire, and the refusals of a changed upload and of another key pair. - example/four_forms.cpp: Makes RFC 6979's P-256 key pair (a published scalar, never a real key), signs "sample" with crypto::signing_schemes::ecdsa_sha256 and verifies the signature, each operation in its four forms: the member, its free form, and the asynchronous member and its free form, which complete through an event loop. RFC 6979's nonce makes the four signatures the same bytes, and the four verifications agree. -- example/key_formats.cpp: Reads RFC 8410's Ed25519 private key in PEM (section 10.3), as OpenSSL or WebCrypto export it, with crypto::decode, writes its public key as PEM with crypto::encode, which is the RFC's section 10.1 to the character, and signs with it; writes an X25519 key pair's public key the same way, with its member encode; and refuses the RFC's version 2 of the private key, which carries the public key too. Prints the PEM texts and each verdict. +- example/key_formats.cpp: Reads RFC 8410's Ed25519 private key in PEM (section 10.3), as OpenSSL or WebCrypto export it, with codecs::decode, writes its public key as PEM with codecs::encode, which is the RFC's section 10.1 to the character, and signs with it; writes an X25519 key pair's public key the same way, with its member encode; and refuses the RFC's version 2 of the private key, which carries the public key too. Prints the PEM texts and each verdict. - example/key_tweak.cpp: A watch-only wallet: a server that holds the wallet's public key and no secret gives each payment a fresh key, and the signing device, which holds the secret, derives the same keys' secrets. Both add the same tweak, one to the public key and one to the secret key; the device's tweaked key pair signs (BIP 340), and the signature verifies under the server's tweaked public key. The tweak here is an HMAC-SHA-256 of the public key and the payment's number under a chain code both sides share and no one else sees; BIP 32 derives such tweaks its own way, with HMAC-SHA-512, and is a domain library's. - example/kx_session.cpp: A chat app's client and server, each with a crypto_kx key pair made from libsodium's kx test's seeds, exchange public keys and compute their session keys; the client seals a message with its transmit key and the server opens it with its receive key, on ChaCha20-Poly1305; a peer key of low order is refused. Prints the public keys and the client's session keys in hex. - example/musig_network.cpp: The wallet of musig_wallet.cpp with its two devices apart: every public piece of BIP 327's MuSig2 crosses the network as bytes. Each device sends its public nonce, 66 bytes, to a coordinator, which holds no secret: it sums the nonces and sends the aggregate nonce, 66 bytes, back. Each device then signs and sends its partial signature, 32 bytes; the coordinator parses each part and checks it with partial_signature_verify before it adds the parts, so that a part damaged on the way, or a faulty signer's, is refused by name rather than spoiling the signature. The devices' secrets are fixed here, so that the program runs the same each time; the nonces are fresh in every run, and nothing random is printed. @@ -430,14 +430,14 @@ Generated by doc/tables/generate.py with the page's tables. Page: https://webcpp - example/order_reference.cpp: Turns a sequential order id into an opaque 16-byte reference with the raw AES block cipher, and back. Customers then cannot guess other orders' numbers. This is NOT authenticated encryption: a forged reference still decrypts, to garbage, so the program checks the padding. - example/p256_ecdh.cpp: Two P-256 key pairs, made from fixed scalars, agree on the shared point's raw x coordinate with agree, the 256 bits WebCrypto's deriveBits gives with ECDH on P-256; then HKDF-SHA-256 of that x, with webcpp::hash, derives the 32-byte key a protocol would encrypt with. The x coordinate is not a key: the derivation, its salt and its info are the protocol's. Cross-checked by hand once with OpenSSL 4.1.0 (2026-11-21), each scalar s (32 bytes of 11, then of 22) written as SEC 1's ECPrivateKey on prime256v1: echo 30310201010420${s}a00a06082a8648ce3d030107 | xxd -r -p > alice.der openssl ec -inform DER -in alice.der -out alice.pem (likewise bob) openssl pkey -in bob.pem -pubout -out bob_public.pem openssl pkeyutl -derive -inkey alice.pem -peerkey bob_public.pem | xxd -p -c 64 openssl kdf -keylen 32 -kdfopt digest:SHA256 -kdfopt hexkey: -kdfopt salt:"example salt" -kdfopt info:"example chat v1 key" HKDF (one line) which print the x and the key of p256_ecdh.expected. - example/p256_handshake.cpp: A signed handshake between two peers on P-256, as a browser's WebCrypto can take part in it: each peer keeps a long-lived identity key pair as its scalar, and makes a fresh key pair for the session; each sends its two public keys raw, signs the SHA-256 digest of the handshake's bytes with its identity (sign gives low S), and checks the other's signature, low S first, since another implementation, a browser's WebCrypto among them, may sign high S; both derive the session key from ECDH's x through HKDF-SHA-256. Every scalar is fixed, so that the output is; a real program draws its key pairs with keypair::generate(). What is signed, when, and the low-S rule are a protocol's: this one is an example, not webcpp::trystero's. -- example/p256_key_formats.cpp: RFC 6979's P-256 key in the files other programs read: its public key written as PEM SPKI with crypto::encode, the text OpenSSL reads and whose DER a WebCrypto peer's importKey("spki") reads, printed and read back with crypto::decode; its compressed point, 33 bytes, printed and read back with formats::raw, which takes either point; its key pair written as PEM PKCS#8 for the program's own key file, a secret result never printed, and read back. The key is RFC 6979's published one, so that the output is fixed: a real program draws its own. +- example/p256_key_formats.cpp: RFC 6979's P-256 key in the files other programs read: its public key written as PEM SPKI with codecs::encode, the text OpenSSL reads and whose DER a WebCrypto peer's importKey("spki") reads, printed and read back with codecs::decode; its compressed point, 33 bytes, printed and read back with formats::raw, which takes either point; its key pair written as PEM PKCS#8 for the program's own key file, a secret result never printed, and read back. The key is RFC 6979's published one, so that the output is fixed: a real program draws its own. - example/p256_keys.cpp: Makes a P-256 key pair from RFC 6979's key, prints its public key as the 65 bytes WebCrypto's exportKey("raw") gives, keeps its scalar and makes the same key pair from it again, then imports that public key as a peer's and refuses the same bytes with one bit changed. Draws a fresh key pair too, whose bytes differ on every run and are not printed. - example/p256_sign.cpp: Signs "sample" with RFC 6979's P-256 key and prints the signature in the raw form WebCrypto uses, low S (the RFC's r, and n minus the RFC's high s); then its DER encoding, which X.509 carries; and verifies the signature read back from DER with the public key alone, given the message and given its SHA-256 digest, and refuses another message. - example/recover_signer.cpp: Finds who signed a message without being sent their key, as Ethereum finds a transaction's signer: a wallet signs a message with a recoverable signature, and a reader that holds no key recovers the signer's public key from the signature and the message, then compares it with the key the wallet published. Recovery always finds some key, so the comparison is the check: a changed message recovers another key. - example/reset_token.cpp: Draws a 32-byte password-reset token and a six-digit one-time code from the system's random generator. Tokens that can be guessed or repeated let an attacker take over accounts, so both come from random_device, never from rand() or the clock. - example/schnorr_sign.cpp: Signs BIP340's first test vector with secp256k1 and verifies it, then tweaks the key pair by a 32-byte tweak, as a domain library derives one, and signs and verifies under the tweaked key. Prints each key and signature in hex. - example/sealed_records.cpp: Seals data in pieces two ways: one message in chunks with the incremental seal, and a stream as separate records, each bound to its position and to whether it is the last. The number in the associated data stops records from being reordered or replayed at another position, and the last-record flag stops a stream from being cut short without being noticed. Each stream has a key of its own, drawn when the stream starts: the counter nonces 0, 1, 2 of one stream are safe only because no other stream seals under its key. -- example/secp256k1_key_formats.cpp: A server's secp256k1 key in files that OpenSSL also reads: its public key written as PEM SPKI with crypto::encode for a configuration file its clients read, uncompressed as OpenSSL and node write it, printed and read back with crypto::decode, and an SPKI stored compressed, as the library's former default wrote it, read back too; its x-only key, BIP340's 32 bytes, as a Nostr client publishes it; its key pair written as PEM PKCS#8 for the server's own key file, a secret result never printed, and read back. The secret is the fixed 0x11 repeated, so that the output is fixed: a real server draws its own. +- example/secp256k1_key_formats.cpp: A server's secp256k1 key in files that OpenSSL also reads: its public key written as PEM SPKI with codecs::encode for a configuration file its clients read, uncompressed as OpenSSL and node write it, printed and read back with codecs::decode, and an SPKI stored compressed, as the library's former default wrote it, read back too; its x-only key, BIP340's 32 bytes, as a Nostr client publishes it; its key pair written as PEM PKCS#8 for the server's own key file, a secret result never printed, and read back. The secret is the fixed 0x11 repeated, so that the output is fixed: a real server draws its own. - example/signed_message.cpp: An author signs a message with a BIP340 Schnorr signature and a reader verifies it with the author's public key alone. No server vouches for the message: any change to the text breaks the signature, so peers can trust what they receive from untrusted relays. A Nostr event carries its author's key as the 32 bytes of its x coordinate (NIP-01): the reader makes the typed x-only key of them and verifies under it as well. - example/signed_update.cpp: Checks an update with the publisher's ECDSA signature before installing it. The public key is shipped inside the program, so a tampered download, or one from a mirror that is not the publisher, fails the check and is never run. - example/silent_payment.cpp: A donation page that publishes one reusable address (BIP 352's silent payments): a pair of public keys, scan and spend. A donor's wallet with one input pays it: the output key it makes is new, tied to the donor's input. The recipient scans the transaction's outputs with its scan key pair, finds the payment, and makes the key pair that spends it with secret_key_tweak_add. A second donation to the same address gives an output key unrelated to the first, so that an onlooker sees no two payments to one address. The address's encoding, the transaction and the smallest outpoint's choice are a wallet's (a domain library's); the keys are fixed here, so that the program runs the same each time, where a real wallet draws them from crypto's random device. diff --git a/doc/memory.adoc b/doc/memory.adoc index 094585a..4f152ed 100644 --- a/doc/memory.adoc +++ b/doc/memory.adoc @@ -24,7 +24,7 @@ open's), `fill`, the signatures (hedged and recoverable too), `agree`, `agree_hashed`, `agree_point`, the verifications, `recover`, `public_key::from_bytes` and the public key's byte forms, the tweaks and negations, `public_key_combine` (in a `workspace` the caller lends), the formats' -`crypto::encode` and `crypto::decode` (a key pair decoded from `formats::raw`, +`codecs::encode` and `codecs::decode` (a key pair decoded from `formats::raw`, `formats::pkcs8` or its PEM is made as `from_secret` makes one, its context the one allocation above, from `std::pmr::get_default_resource()`, or, with `keypair::decode(format, input, resource)`, from the resource the program @@ -52,7 +52,7 @@ and a key never should be. *Example.* A key pair whose context comes from an arena on the stack, through a resource that counts what it gives, then read back from its PKCS#8 into the same arena with `keypair::decode(crypto::formats::pkcs8, bytes, resource)`, the -decode that takes the program's resource (`crypto::decode` and the decode +decode that takes the program's resource (`codecs::decode` and the decode without a resource take the context from `std::pmr::get_default_resource()`): [source,cpp] diff --git a/doc/p256.adoc b/doc/p256.adoc index 50eb68d..4bfd8ae 100644 --- a/doc/p256.adoc +++ b/doc/p256.adoc @@ -51,8 +51,8 @@ Emscripten (`p256_webcrypto_oracle`, the ECDSA and ECDH that Trystero's peers run): it verifies the signatures of `sign`, which are low S, its own signatures verify with `crypto::verify` under `any_s`, whatever their S, its `deriveBits` gives the bytes of `agree`, and its `exportKey` of the keys it imported gives the -bytes of `crypto::encode` in `formats::spki`, `formats::pkcs8` and -`formats::raw`, byte for byte, which `crypto::decode` reads back. +bytes of `codecs::encode` in `formats::spki`, `formats::pkcs8` and +`formats::raw`, byte for byte, which `codecs::decode` reads back. `p256_webcrypto_oracle_corrupted` flips one bit of one signature and of one shared x, and passes only when WebCrypto disagrees on exactly those two. @@ -230,11 +230,11 @@ ES256 carries (link:https://www.rfc-editor.org/rfc/rfc7518#section-3.4[RFC 7518, section 3.4]). *DER*, 8 to 72 bytes: a SEQUENCE of two INTEGERs, is the form of X.509 certificates and OpenSSL (link:https://www.rfc-editor.org/rfc/rfc3279#section-2.2.3[RFC 3279, section -2.2.3]): `crypto::encode(crypto::formats::der, signature)`, or the member +2.2.3]): `codecs::encode(crypto::formats::der, signature)`, or the member `signature.encode(crypto::formats::der)`, writes it, its exact size, 6 bytes and the length of r and of s, known before any write, r or s 0 or not below n refused with `invalid_signature`; and -`crypto::decode(crypto::formats::der, bytes)` reads it back, +`codecs::decode(crypto::formats::der, bytes)` reads it back, accepting strict DER only, the encoding encoding it again writes, and refusing anything else with `invalid_signature` (<>). @@ -301,7 +301,7 @@ compile. * A passkey's ES256 is ECDSA on P-256 with SHA-256, and its assertion's signature arrives in DER (link:https://www.w3.org/TR/webauthn-2/#sctn-signature-attestation-types[W3C -Web Authentication, section 6.5.5]): `crypto::decode` of `formats::der`, then +Web Authentication, section 6.5.5]): `codecs::decode` of `formats::der`, then `+crypto::verify(crypto::signing_schemes::ecdsa_sha256, crypto::verification_policies::any_s, ...)+` of what the authenticator signed, which it hashes with SHA-256. Reading the COSE key and building those signed bytes are WebAuthn's, and stay outside crypto. @@ -415,8 +415,8 @@ WebCrypto's `exportKey("spki")` and `exportKey("pkcs8")` produce and its `importKey` reads (link:https://www.w3.org/TR/WebCryptoAPI/[W3C]). `` registers P-256's formats into -webcpp::codecs, for `crypto::encode(format, value)`, -`crypto::decode(format, input)` and the types' members `encode` and +webcpp::codecs, for `codecs::encode(format, value)`, +`codecs::decode(format, input)` and the types' members `encode` and `decode` (<>). On P-256 each has one layout, fixed bytes around the key's own: @@ -451,7 +451,10 @@ key pair with `invalid_state` and a buffer shorter than the exact encoding with `codecs::error::output_too_small`, writing nothing then. The decoders are strict (design D30): they accept exactly the bytes the -encoders write, and refuse anything else with `invalid_encoding`. The point +encoders write, and refuse anything else with `invalid_encoding`, but an input +longer than the pair's `formats::max_decode_input_size`, which is +`codecs::error::input_too_long`, a DER, a raw input or a PEM's payload alike, +before a byte is read. The point must be on the curve, the scalar between 1 and n - 1, and the public key inside a PKCS#8 the scalar's own: the decoder makes the key pair and compares its point with the one the file carries. Refused, too: another curve, explicit @@ -473,8 +476,9 @@ PEM is the DER as base64 text between `-----BEGIN PUBLIC KEY-----` or `formats::pem(formats::pkcs8)`, 241, through codecs' armor, decoded in place with no heap. It reads PEM as Windows and OpenSSL's tools write it, with CRLF lines or without its final line feed, and refuses text before BEGIN with -`codecs::error::invalid_prefix`, and a payload longer than the format's DER, an -RSA key's SPKI labelled `PUBLIC KEY` among them, with `invalid_encoding`. +`codecs::error::invalid_prefix`, and an input longer than the format's DER, an +RSA key's SPKI labelled `PUBLIC KEY` among them, with +`codecs::error::input_too_long`. include::generated/backends-p256_formats.adoc[] @@ -519,6 +523,6 @@ that is not its scalar's, and a program that shows or checks that key, rather than the one the scalar gives, trusts the wrong key. `formats::pkcs8` refuses such a file. * The PKCS#8, as DER or as PEM, is the secret key in clear: base64 is an -encoding, not encryption. Keep it in the secret result `crypto::encode` gives, +encoding, not encryption. Keep it in the secret result `codecs::encode` gives, as the example does, and seal it before it is written anywhere (<>). diff --git a/doc/p2p.adoc b/doc/p2p.adoc index 4a93036..6dfc580 100644 --- a/doc/p2p.adoc +++ b/doc/p2p.adoc @@ -86,7 +86,7 @@ signed this way, with BIP 340 over the event's SHA-256 (link:https://github.com/nostr-protocol/nips/blob/master/01.md[NIP-01]), and name their author by the 32 bytes of the key's x coordinate alone, BIP 340's x-only key. The reader makes the typed key of them, -`crypto::decode(crypto::formats::raw, bytes)`, +`codecs::decode(crypto::formats::raw, bytes)`, which refuses 32 bytes that are no point's x, and verifies under `crypto::verification_policies::canonical`, the policy BIP 340's verification takes (<>, <>). diff --git a/doc/secp256k1.adoc b/doc/secp256k1.adoc index c1ae77b..438a89d 100644 --- a/doc/secp256k1.adoc +++ b/doc/secp256k1.adoc @@ -111,7 +111,7 @@ of the ECDSA signatures; its `combine_workspace_size`: the tweaks, negation and combination of whole keys (<>); * ``: the formats of the keys and -signatures for `crypto::encode`, `crypto::decode` and the types' members +signatures for `codecs::encode`, `codecs::decode` and the types' members `encode` and `decode`: a public key's point, uncompressed by default, compressed, or its x-only key; its SPKI; the x-only key's 32 bytes; a key pair's secret key and PKCS#8; the PEM of either; the signatures' bytes and @@ -596,8 +596,8 @@ curve holds RFC 5915's ECPrivateKey: the secret, and optionally the curve and the public key (link:https://www.rfc-editor.org/rfc/rfc5915[RFC 5915]). `` registers secp256k1's formats -into webcpp::codecs, for `crypto::encode(format, value)`, -`crypto::decode(format, input)` and the types' members `encode` and +into webcpp::codecs, for `codecs::encode(format, value)`, +`codecs::decode(format, input)` and the types' members `encode` and `decode` (<>). Each has one layout, fixed bytes around the key's own: @@ -614,7 +614,7 @@ decodes, and `spki(formats::compressed)` writes it again. the format's name, with the errors of `from_bytes`. `formats::raw(formats::xonly)` writes the key's BIP 340 x-only key, `xonly().bytes()`, 32 bytes, as Nostr publishes a key; it encodes only, since 32 bytes are an `xonly_public_key`, -which `crypto::decode(formats::raw(formats::xonly), bytes)` +which `codecs::decode(formats::raw(formats::xonly), bytes)` gives (BIP 340's lift of x, the point of even y). Decoding them into a `public_key` stops the build with a message that names `xonly_public_key`. * `formats::pkcs8` writes 135 bytes: version 0, the same algorithm, and an @@ -634,7 +634,13 @@ secret buffer wiped. A program that keeps its allocations in its own arena (<>) decodes with `keypair::decode(format, input, resource)`, `raw`, `pkcs8` or `pem(formats::pkcs8)`, its context from that resource, as -`from_secret(secret, resource)` takes it, with the same errors. +`from_secret(secret, resource)` takes it, with the same errors, and an input past +the bound is `codecs::error::input_too_long` there too. A format the key pair +lacks stops the build as `codecs::decode` does, with crypto's sentence for a +reason of crypto's or codecs' message with the type's list; a composition codecs +converts, as `over{base64, pkcs8}`, stops with crypto's sentence that names +those three formats; `raw` and `pkcs8` given a text, and `pem(formats::pkcs8)` +given bytes, stop with crypto's sentence that names the input they take. * An ECDSA signature's `formats::raw` is its 64 bytes, r then s, and `formats::der` its strict DER, BIP 66's, 8 to 72 bytes, written by libsecp256k1's `secp256k1_ecdsa_signature_serialize_der` and read by its @@ -650,7 +656,10 @@ moved-from key pair with `invalid_state` and a buffer shorter than the exact encoding with `codecs::error::output_too_small`, writing nothing then. The decoders are strict (design D30): they accept exactly the bytes the -encoders write, and refuse anything else, each with its error: `spki` and +encoders write, and refuse anything else, an input longer than the pair's +`formats::max_decode_input_size` with `codecs::error::input_too_long` (a DER, a +raw input or a PEM's payload alike) before a byte is read, the rest each with +its error: `spki` and `pkcs8` with `invalid_encoding`; a public key's and an x-only key's `raw` with `invalid_public_key`, as `from_bytes` refuses it; a key pair's `raw` with `invalid_key_size` or `invalid_secret_key`, as `from_secret` refuses it; `der` @@ -675,8 +684,9 @@ DER in base64 between a `-----BEGIN PUBLIC KEY-----` or armor, in a time that does not depend on the bytes, decoded in place with no heap. It reads PEM as Windows and OpenSSL's tools write it, with CRLF lines or without its final line feed, and refuses text before BEGIN with -`codecs::error::invalid_prefix`, and a payload longer than the format's DER, an -RSA key's SPKI labelled `PUBLIC KEY` among them, with `invalid_encoding`. The +`codecs::error::invalid_prefix`, and an input longer than the format's DER, an +RSA key's SPKI labelled `PUBLIC KEY` among them, with +`codecs::error::input_too_long`. The tests compare the text crypto writes with that of OpenSSL's `PEM_write_bio_PUBKEY` and `PEM_write_bio_PrivateKey`, character for character. diff --git a/doc/tables/crypto.json b/doc/tables/crypto.json index 739be20..a5b8b07 100644 --- a/doc/tables/crypto.json +++ b/doc/tables/crypto.json @@ -502,9 +502,9 @@ "functions": ["public_key::encode", "public_key::decode", "basic_keypair::encode", "basic_keypair::decode", "basic_signature::encode", "basic_signature::decode"], "function_headers": {"public_key::encode": "", "public_key::decode": "", "basic_keypair::encode": "", "basic_keypair::decode": "", "basic_signature::encode": "", "basic_signature::decode": ""}, "namespace": "curves", - "api": "crypto::encode, crypto::decode, curves::ed25519::public_key::encode, curves::ed25519::public_key::decode, curves::ed25519::keypair::encode, curves::ed25519::keypair::decode, curves::ed25519::signature::encode, curves::ed25519::signature::decode", + "api": "codecs::encode, codecs::decode, curves::ed25519::public_key::encode, curves::ed25519::public_key::decode, curves::ed25519::keypair::encode, curves::ed25519::keypair::decode, curves::ed25519::signature::encode, curves::ed25519::signature::decode", "header": "", - "what": "Ed25519's formats as codecs value codecs: a public key's `formats::raw` (32 bytes), `formats::spki` (RFC 8410, 44 bytes) and `formats::pem(formats::spki)`; a key pair's `formats::raw` (its seed), `formats::pkcs8` (RFC 8410's version 1, 48 bytes) and `formats::pem(formats::pkcs8)`, secret; a signature's `formats::raw` (64 bytes); the forms OpenSSL and WebCrypto write, the DER decoded strictly: any other bytes are `invalid_encoding`.", + "what": "Ed25519's formats as codecs value codecs: a public key's `formats::raw` (32 bytes), `formats::spki` (RFC 8410, 44 bytes) and `formats::pem(formats::spki)`; a key pair's `formats::raw` (its seed), `formats::pkcs8` (RFC 8410's version 1, 48 bytes) and `formats::pem(formats::pkcs8)`, secret; a signature's `formats::raw` (64 bytes); the forms OpenSSL and WebCrypto write, the DER decoded strictly: any other bytes are `invalid_encoding`, and an input longer than the pair's bound (32 bytes, 44 for an SPKI, 48 for a PKCS#8, 64 for a signature's `raw`) is `codecs::error::input_too_long`, as a PEM's payload is.", "section": "crypto-key-formats", "backends": {"sodium": {"tests": ["formats_ed25519_sodium", "formats_25519_openssl_oracle", "formats_25519_webcrypto_oracle", "allocation_sodium"]}} }, @@ -514,9 +514,9 @@ "functions": ["public_key::encode", "public_key::decode", "basic_keypair::encode", "basic_keypair::decode"], "function_headers": {"public_key::encode": "", "public_key::decode": "", "basic_keypair::encode": "", "basic_keypair::decode": ""}, "namespace": "curves", - "api": "crypto::encode, crypto::decode, curves::x25519::public_key::encode, curves::x25519::public_key::decode, curves::x25519::keypair::encode, curves::x25519::keypair::decode", + "api": "codecs::encode, codecs::decode, curves::x25519::public_key::encode, curves::x25519::public_key::decode, curves::x25519::keypair::encode, curves::x25519::keypair::decode", "header": "", - "what": "X25519's formats as codecs value codecs: a public key's `formats::raw` (32 bytes), `formats::spki` (RFC 8410, 44 bytes) and `formats::pem(formats::spki)`; a key pair's `formats::raw` (its secret key), `formats::pkcs8` (RFC 8410's version 1, 48 bytes) and `formats::pem(formats::pkcs8)`, secret; the forms OpenSSL and WebCrypto write, an SPKI decoded into the typed key, the DER decoded strictly: any other bytes are `invalid_encoding`.", + "what": "X25519's formats as codecs value codecs: a public key's `formats::raw` (32 bytes), `formats::spki` (RFC 8410, 44 bytes) and `formats::pem(formats::spki)`; a key pair's `formats::raw` (its secret key), `formats::pkcs8` (RFC 8410's version 1, 48 bytes) and `formats::pem(formats::pkcs8)`, secret; the forms OpenSSL and WebCrypto write, an SPKI decoded into the typed key, the DER decoded strictly: any other bytes are `invalid_encoding`, and an input longer than the pair's bound (32 bytes, 44 for an SPKI, 48 for a PKCS#8) is `codecs::error::input_too_long`, as a PEM's payload is.", "section": "crypto-key-formats", "backends": {"sodium": {"tests": ["formats_x25519_sodium", "formats_25519_openssl_oracle", "formats_25519_webcrypto_oracle", "allocation_sodium"]}} }, @@ -685,9 +685,9 @@ "functions": ["public_key::encode", "public_key::decode", "xonly_public_key::encode", "xonly_public_key::decode", "basic_keypair::encode", "basic_keypair::decode", "basic_keypair::decode(std::pmr::memory_resource)", "basic_signature::encode", "basic_signature::decode"], "function_headers": {"public_key::encode": "", "public_key::decode": "", "xonly_public_key::encode": "", "xonly_public_key::decode": "", "basic_keypair::encode": "", "basic_keypair::decode": "", "basic_keypair::decode(std::pmr::memory_resource)": "", "basic_signature::encode": "", "basic_signature::decode": ""}, "namespace": "curves", - "api": "crypto::encode, crypto::decode, curves::secp256k1::public_key::encode, curves::secp256k1::public_key::decode, curves::secp256k1::xonly_public_key::encode, curves::secp256k1::xonly_public_key::decode, curves::secp256k1::keypair::encode, curves::secp256k1::keypair::decode (also with a std::pmr::memory_resource), curves::secp256k1::ecdsa_signature::encode, curves::secp256k1::ecdsa_signature::decode, curves::secp256k1::recoverable_signature::encode, curves::secp256k1::recoverable_signature::decode, curves::secp256k1::schnorr_signature::encode, curves::secp256k1::schnorr_signature::decode", + "api": "codecs::encode, codecs::decode, curves::secp256k1::public_key::encode, curves::secp256k1::public_key::decode, curves::secp256k1::xonly_public_key::encode, curves::secp256k1::xonly_public_key::decode, curves::secp256k1::keypair::encode, curves::secp256k1::keypair::decode (also with a std::pmr::memory_resource), curves::secp256k1::ecdsa_signature::encode, curves::secp256k1::ecdsa_signature::decode, curves::secp256k1::recoverable_signature::encode, curves::secp256k1::recoverable_signature::decode, curves::secp256k1::schnorr_signature::encode, curves::secp256k1::schnorr_signature::decode", "header": "", - "what": "secp256k1's formats as codecs value codecs: a public key's `formats::raw` (SEC 1's uncompressed point, 65 bytes; `raw(formats::compressed)` 33; `raw(formats::xonly)` 32, to encode only), `formats::spki` (RFC 5480, 88 bytes, the default in this version, where it was the compressed point before; `spki(formats::compressed)` 56) and `formats::pem(formats::spki)`, the default forms decoding both points, so that a stored compressed SPKI still decodes; an x-only key's `formats::raw` (BIP340's 32 bytes, by default and as `raw(formats::xonly)`); a key pair's `formats::raw` (its secret key) and `formats::pkcs8` (RFC 5915's ECPrivateKey with its uncompressed public key and no parameters, 135 bytes) and `formats::pem(formats::pkcs8)`, secret, a decoded key pair's context from the default memory resource, or from the program's own with `keypair::decode(format, input, memory_resource)`; an ECDSA signature's `formats::raw` (r then s, 64 bytes) and `formats::der` (strict DER, BIP66, 8 to 72 bytes, r or s 0 or not below n refused), a recoverable signature's `formats::raw` (65 bytes) and a BIP340 signature's (64); the forms OpenSSL and node write, each decoded strictly: `spki` and `pkcs8` refuse any other bytes with `invalid_encoding`, a public key's and an x-only key's `raw` refuse with `invalid_public_key`, as `from_bytes` does, a key pair's `raw` with `invalid_key_size` or `invalid_secret_key`, as `from_secret` does, and `der` and a signature's `raw` refuse with `invalid_signature`.", + "what": "secp256k1's formats as codecs value codecs: a public key's `formats::raw` (SEC 1's uncompressed point, 65 bytes; `raw(formats::compressed)` 33; `raw(formats::xonly)` 32, to encode only), `formats::spki` (RFC 5480, 88 bytes, the default in this version, where it was the compressed point before; `spki(formats::compressed)` 56) and `formats::pem(formats::spki)`, the default forms decoding both points, so that a stored compressed SPKI still decodes; an x-only key's `formats::raw` (BIP340's 32 bytes, by default and as `raw(formats::xonly)`); a key pair's `formats::raw` (its secret key) and `formats::pkcs8` (RFC 5915's ECPrivateKey with its uncompressed public key and no parameters, 135 bytes) and `formats::pem(formats::pkcs8)`, secret, a decoded key pair's context from the default memory resource, or from the program's own with `keypair::decode(format, input, memory_resource)`; an ECDSA signature's `formats::raw` (r then s, 64 bytes) and `formats::der` (strict DER, BIP66, 8 to 72 bytes, r or s 0 or not below n refused), a recoverable signature's `formats::raw` (65 bytes) and a BIP340 signature's (64); the forms OpenSSL and node write, each decoded strictly: `spki` and `pkcs8` refuse any other bytes with `invalid_encoding`, an input past the pair's bound (a PEM's payload too) with `codecs::error::input_too_long`: 65 bytes for a public key's `raw` (33 compressed), 88 for an SPKI (56), 32 for an x-only key's and a key pair's `raw`, 135 for a PKCS#8, 64 for an ECDSA or Schnorr signature's `raw`, 65 for a recoverable one's, 72 for a `der`; within the bound, a public key's and an x-only key's `raw` of a size it does not have refuse with `invalid_public_key`, as `from_bytes` does, a key pair's `raw` shorter than 32 bytes with `invalid_key_size` and a secret key that is not valid with `invalid_secret_key`, as `from_secret` does, and `der` and a signature's `raw` refuse with `invalid_signature`.", "section": "crypto-secp256k1-formats", "backends": {"secp256k1": {"tests": ["secp256k1_formats", "secp256k1_formats_openssl", "secp256k1_node_oracle", "secp256k1_der", "secp256k1_allocation"]}} }, @@ -794,9 +794,9 @@ "functions": ["basic_public_key::encode", "basic_public_key::decode", "basic_keypair::encode", "basic_keypair::decode", "basic_signature::encode", "basic_signature::decode"], "function_headers": {"basic_public_key::encode": "", "basic_public_key::decode": "", "basic_keypair::encode": "", "basic_keypair::decode": "", "basic_signature::encode": "", "basic_signature::decode": ""}, "namespace": "curves", - "api": "crypto::encode, crypto::decode, curves::p256::public_key::encode, curves::p256::public_key::decode, curves::p256::keypair::encode, curves::p256::keypair::decode, curves::p256::ecdsa_signature::encode, curves::p256::ecdsa_signature::decode", + "api": "codecs::encode, codecs::decode, curves::p256::public_key::encode, curves::p256::public_key::decode, curves::p256::keypair::encode, curves::p256::keypair::decode, curves::p256::ecdsa_signature::encode, curves::p256::ecdsa_signature::decode", "header": "", - "what": "P-256's formats as codecs value codecs: a public key's `formats::raw` (SEC 1's uncompressed point, 65 bytes; `raw(formats::compressed)` 33), `formats::spki` (RFC 5480, 91 bytes; `spki(formats::compressed)` 59) and `formats::pem(formats::spki)`, the default forms decoding both points; a key pair's `formats::raw` (its scalar), `formats::pkcs8` (RFC 5915's ECPrivateKey with its public key and no parameters, 138 bytes) and `formats::pem(formats::pkcs8)`, secret; a signature's `formats::raw` (r then s, 64 bytes) and `formats::der` (strict DER, 8 to 72 bytes, r or s 0 or not below n refused); the forms OpenSSL and WebCrypto write, each decoded strictly: `spki` and `pkcs8` refuse any other bytes with `invalid_encoding`, a public key's `raw` refuses with `invalid_public_key`, as `from_bytes` does, a key pair's `raw` with `invalid_key_size` or `invalid_secret_key`, as `from_secret` does, and `der` and a signature's `raw` refuse with `invalid_signature`.", + "what": "P-256's formats as codecs value codecs: a public key's `formats::raw` (SEC 1's uncompressed point, 65 bytes; `raw(formats::compressed)` 33), `formats::spki` (RFC 5480, 91 bytes; `spki(formats::compressed)` 59) and `formats::pem(formats::spki)`, the default forms decoding both points; a key pair's `formats::raw` (its scalar), `formats::pkcs8` (RFC 5915's ECPrivateKey with its public key and no parameters, 138 bytes) and `formats::pem(formats::pkcs8)`, secret; a signature's `formats::raw` (r then s, 64 bytes) and `formats::der` (strict DER, 8 to 72 bytes, r or s 0 or not below n refused); the forms OpenSSL and WebCrypto write, each decoded strictly: `spki` and `pkcs8` refuse any other bytes with `invalid_encoding`, an input past the pair's bound (a PEM's payload too) with `codecs::error::input_too_long`: 65 bytes for a public key's `raw` (33 compressed), 91 for an SPKI (59), 32 for a key pair's `raw`, 138 for a PKCS#8, 64 for a signature's `raw`, 72 for a `der`; within the bound, a public key's `raw` of a size it does not have refuses with `invalid_public_key`, as `from_bytes` does, a key pair's `raw` shorter than 32 bytes with `invalid_key_size` and a secret key that is not valid with `invalid_secret_key`, as `from_secret` does, and `der` and a signature's `raw` refuse with `invalid_signature`.", "section": "crypto-p256-formats", "backends": { "bearssl": {"tests": ["p256_formats_bearssl", "p256_openssl_oracle", "p256_webcrypto_oracle", "allocation_bearssl"]}, @@ -1170,7 +1170,7 @@ ["`secure::erase_secret`", "Erasing a password, a key or a plaintext from a buffer the program owns", "``", "none", "{cwe14}", "a secret sits in a `std::string`, a `std::vector` or an array of the program's and must not outlive its use; never `memset`, which the optimiser may drop; <>"], ["`secure::secret`", "Holding a secret crypto gives back: a key pair's `secret()`, a shared secret, a session key", "``", "none", "{cwe14}", "crypto returns a secret: it wipes itself when destroyed or moved from, is copied only by `clone()`, and gives its bytes with `bytes()`; keep it in one variable; <>"], ["`error`", "Telling failures apart", "``", "its values are the errors", "none", "you branch on a failure: forged data, a wrong size, no entropy"], - ["`formats::pem`", "A key's SPKI or PKCS#8 as text, to paste, store or exchange with OpenSSL", "``", "codecs' `invalid_prefix`, `invalid_length`, `invalid_character`, `invalid_padding` for the armor; `invalid_encoding` for its DER", "{rfc7468}", "a key goes to or comes from a file, a form or a tool as `-----BEGIN PUBLIC KEY-----` text: `crypto::encode(formats::pem(formats::spki), public_key)`, or `formats::pem(formats::pkcs8)` of a key pair, on every curve, codecs' armor over the DER with the label of the inner format; <>"], + ["`formats::pem`", "A key's SPKI or PKCS#8 as text, to paste, store or exchange with OpenSSL", "``", "codecs' `invalid_prefix`, `invalid_length`, `invalid_character`, `invalid_padding` for the armor; `invalid_encoding` for its DER; `input_too_long` past the inner format's bound", "{rfc7468}", "a key goes to or comes from a file, a form or a tool as `-----BEGIN PUBLIC KEY-----` text: `codecs::encode(formats::pem(formats::spki), public_key)`, or `formats::pem(formats::pkcs8)` of a key pair, on every curve, codecs' armor over the DER with the label of the inner format; <>"], ["`openssl_memory_resource`", "Keeping every allocation of OpenSSL's in the program's own resource, natively", "``", "`memory_functions_refused`, `memory_functions_not_installed`", "{openssl-mem}", "a native program on OpenSSL must allocate only from its own arena; on WebAssembly, BearSSL allocates nothing"], ["`eddsa`, `eddsa_prehashed`, `ecdsa_sha256`, `ecdsa_sha256_recoverable`, `ecdsa_keccak256_recoverable`, `schnorr`, `prehashed`, `can_sign`, `can_verify`, `verify`, `low_s_only`, `any_s`, `canonical`: Signatures", "Naming the scheme and the verification policy of every signature, a digest as a digest, and a signature as a value of its curve and scheme, on every curve", "``", "`verify`: `invalid_signature`, and `invalid_public_key` for 32 x-only bytes that are no point; `sign`: `invalid_state`, `entropy_unavailable` (Schnorr's drawn randomness), `backend_failure`; a scheme and an input that do not go together, and a policy the scheme does not take or none, stop the build", "{rfc8032}, {fips186-5}, {bip340}, {webcrypto}", "a key pair signs with `key_pair.sign(scheme, input)` or `sign(key_pair, scheme, input)`, and a signature is checked with `public_key.verify(scheme, policy, signature, input)` or `verify(scheme, policy, signature, input, public_key)`, the policy one the scheme takes (<>); a digest computed elsewhere, such as a Bitcoin sighash, an Ethereum EIP-191 digest or the SHA-512 Ed25519ph signs, goes in marked as `prehashed`, never as a bare span; the two recoverable schemes have no `verify`: their signature gives its key to `curves::secp256k1::recover`; a signature is a type of its curve and scheme, made from received bytes by `from_bytes` (<>)"], ["`keypair`, `signing_keypair`, `agreement_keypair`, `sign`, `agree`: The key pair concepts", "Code written once for every curve's key pair, by what it does", "``", "the concepts: none, checked at compile time; `sign` and `agree`: what the key pair's own returns, and a key pair that does not do the operation, or arguments it does not take, stop the build", "{rfc8032}, {rfc7748}, {fips186-5}, {sec1}", "a template takes any curve's key pair: `from_secret`, `generate`, a typed `public_key()`, `secret()`; a `signing_keypair` (Ed25519, P-256, secp256k1) adds `sign(scheme, input)`, whose `crypto::sign(key_pair, scheme, input)` is the free form, an `agreement_keypair` (X25519, P-256, secp256k1) `agree(peer)`, whose free form is `agree(key_pair, peer)` (<>)"], @@ -1254,12 +1254,12 @@ {"need": "Derive a Nostr direct message's conversation key (NIP-44; the format is a domain library's)", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`keypair::agree`, then HKDF on Boost.Hash2", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1", "example": "ecdh_x_coordinate", "section": "crypto-p2p-nostr-dm", "answers": "crypto-secp256k1-ecdh"}, {"need": "Find who signed a message or a transaction without being sent their key (Ethereum's signatures)", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`keypair::sign(crypto::signing_schemes::ecdsa_keccak256_recoverable, message)`, `recover`", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1 /third_party//xkcp", "example": "ethereum_personal_sign", "section": "crypto-wallets-recovery", "answers": "crypto-secp256k1-recovery"}, {"need": "Derive child keys for a watch-only wallet (BIP 32's derivation is a domain library's)", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`secret_key_tweak_add`, `public_key_tweak_add`", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1", "example": "key_tweak", "section": "crypto-secp256k1-tweaks"}, - {"need": "Store or exchange a secp256k1 key with OpenSSL, node or a config file, or publish its x-only key (the point, compressed or not, x-only, SPKI, PKCS#8, as DER or PEM text)", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`crypto::encode` and `crypto::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, and `crypto::formats::compressed` and `xonly`, on secp256k1's keys", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1", "example": "secp256k1_key_formats", "section": "crypto-secp256k1-formats"}, + {"need": "Store or exchange a secp256k1 key with OpenSSL, node or a config file, or publish its x-only key (the point, compressed or not, x-only, SPKI, PKCS#8, as DER or PEM text)", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`codecs::encode` and `codecs::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, and `crypto::formats::compressed` and `xonly`, on secp256k1's keys", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1", "example": "secp256k1_key_formats", "section": "crypto-secp256k1-formats"}, {"need": "Make a peer-to-peer connection's key exchange look like random bytes (BIP 324's transport; its packets are a domain library's)", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`ellswift_create`, `ellswift_decode`, `ellswift_xdh`", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1", "example": "ellswift_handshake", "section": "crypto-p2p-v2", "answers": "crypto-secp256k1-ellswift"}, {"need": "Publish one donation address that never shows two payments as related (BIP 352's addresses and transactions are a domain library's)", "namespace": "webcpp::crypto::curves::secp256k1::silent_payments", "api": "`create_outputs`, `make_previous_outputs_summary`, `scan_outputs`", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1", "example": "silent_payment", "section": "crypto-wallets-silent", "answers": "crypto-secp256k1-silent-payments"}, {"need": "Require two devices to sign for a wallet, with one ordinary signature on the chain", "namespace": "webcpp::crypto::curves::secp256k1::musig", "api": "`key_aggregate`, `generate_nonce`, `partial_sign`, `partial_signature_aggregate`", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1", "example": "musig_wallet", "section": "crypto-wallets-musig", "answers": "crypto-secp256k1-musig"}, {"need": "Make and keep a wallet's key", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`keypair::from_secret`, `keypair::public_key`, `public_key::xonly`, `public_key::compressed`; `aes256_gcm` at rest", "backend": "libsecp256k1, and AES-GCM's default", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1 /webcpp/crypto//gcm-default", "example": "wallet_key", "section": "crypto-wallets-key"}, - {"need": "Read or write a wallet's key as WIF and its address as Base58Check text (a domain library's job)", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`crypto::decode(crypto::formats::raw, bytes)`, `keypair.encode(crypto::formats::raw)`, `crypto::encode(crypto::formats::raw(crypto::formats::compressed), public_key)`, with webcpp::hash's `hash::formats::base58_with_checksum`", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1 /webcpp/hash//base58_with_checksum", "example": "wallet_wif", "section": "crypto-wallets-wif"}, + {"need": "Read or write a wallet's key as WIF and its address as Base58Check text (a domain library's job)", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`codecs::decode(crypto::formats::raw, bytes)`, `keypair.encode(crypto::formats::raw)`, `codecs::encode(crypto::formats::raw(crypto::formats::compressed), public_key)`, with webcpp::hash's `hash::formats::base58_with_checksum`", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1 /webcpp/hash//base58_with_checksum", "example": "wallet_wif", "section": "crypto-wallets-wif"}, {"need": "Sign a Bitcoin SegWit v0 input (a domain library's job)", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`keypair::sign(crypto::signing_schemes::ecdsa_sha256, crypto::signing_schemes::prehashed{sighash})`, `signature.encode(crypto::formats::der)`, `signature.is_low_s()`, `signature.low_s()`", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1", "example": "bitcoin_sighash", "section": "crypto-wallets-der"}, {"need": "Derive a Bitcoin Taproot output key and spend by key path (a domain library's job)", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`keypair::xonly_public_key`, `xonly_public_key::tweak_add`, `xonly_public_key::verify_tweak`, `keypair::tweak_xonly`", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1", "example": "taproot_tweak", "section": "crypto-wallets-taproot"}, {"need": "Sign where allocation is restricted to the program's arena", "namespace": "webcpp::crypto::curves::secp256k1", "api": "`keypair::from_secret(secret, resource)`, or a stored key's `keypair::decode(crypto::formats::pkcs8, bytes, resource)`", "backend": "libsecp256k1", "jamfile": "/webcpp/crypto//crypto /third_party//secp256k1", "example": "arena_signer", "section": "crypto-memory"}, @@ -1269,17 +1269,17 @@ {"need": "Sign and verify with Ed25519 (RFC 8032), or with Ed25519ph a message's SHA-512 or a large message in pieces", "namespace": "webcpp::crypto::curves::ed25519", "api": "`keypair::from_secret`, `keypair::generate`, `keypair::sign(crypto::signing_schemes::eddsa, message)`, `keypair::sign(crypto::signing_schemes::eddsa_prehashed, crypto::signing_schemes::prehashed{digest})`, `crypto::verify`; `prehashed_signer`", "backend": "libsodium", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//ed25519-default", "example": "ed25519_sign", "section": "crypto-ed25519"}, {"need": "Agree a shared secret with a peer's public key (X25519, RFC 7748), or with the X25519 form of an Ed25519 key", "namespace": "webcpp::crypto::curves::x25519", "api": "`keypair::from_secret`, `keypair::generate`, `public_key::from_bytes`, `keypair::agree`; `curves::ed25519::keypair::to_x25519`, `curves::ed25519::public_key::to_x25519`, `crypto::to_x25519`", "backend": "libsodium", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//x25519-default", "example": "x25519_agree", "section": "crypto-x25519"}, {"need": "Give a client and a server a key for each direction from each other's public key, as libsodium's `crypto_kx` does", "namespace": "webcpp::crypto::key_exchange", "api": "`kx_keypair::from_keypair`, `kx_keypair::derive_from_seed`, `kx_keypair::generate`, `client_session_keys`, `server_session_keys`", "backend": "libsodium", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//kx-default", "example": "kx_session", "section": "crypto-kx"}, - {"need": "Export or import an Ed25519 or X25519 key in the standard forms OpenSSL and WebCrypto use: SPKI and PKCS#8 (RFC 8410), as DER or PEM text", "namespace": "webcpp::crypto::curves", "api": "`crypto::encode` and `crypto::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, on Ed25519's and X25519's keys", "backend": "libsodium", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//ed25519-default /webcpp/crypto//x25519-default", "example": "key_formats", "section": "crypto-key-formats"}, + {"need": "Export or import an Ed25519 or X25519 key in the standard forms OpenSSL and WebCrypto use: SPKI and PKCS#8 (RFC 8410), as DER or PEM text", "namespace": "webcpp::crypto::curves", "api": "`codecs::encode` and `codecs::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, on Ed25519's and X25519's keys", "backend": "libsodium", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//ed25519-default /webcpp/crypto//x25519-default", "example": "key_formats", "section": "crypto-key-formats"}, {"need": "Encrypt messages in a chat app, on the web and natively, each with a random nonce", "namespace": "webcpp::crypto::ciphers", "api": "`xchacha20_poly1305<>`: `from_key`, `seal`, `open`", "backend": "libsodium", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//xchacha20_poly1305-default", "example": "xchacha_seal", "section": "crypto-chacha"}, {"need": "Encrypt a record or a file under one secret key, each with a random nonce, in libsodium's secret-box format", "namespace": "webcpp::crypto::secret_key_encryption", "api": "`secretbox<>`, `secretbox_xchacha20poly1305<>`: `from_key`, `seal(nonce, data)`, `open(nonce, data, tag)`; the box forms `seal(box, nonce, data)`, `open(box, nonce, data, tag)`", "backend": "libsodium", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//secretbox_xsalsa20poly1305-default", "example": "file_share", "section": "crypto-secretbox"}, {"need": "Send messages between two people who know each other's public keys, as NaCl's and libsodium's crypto_box", "namespace": "webcpp::crypto::public_key_encryption", "api": "`box::from_keys`, `box::seal`, `box::open`", "backend": "libsodium", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//box_xsalsa20poly1305-default", "example": "box_messages", "section": "crypto-box"}, {"need": "Let anyone send a message that only the recipient can read, without saying who sent it (anonymous feedback, a drop box)", "namespace": "webcpp::crypto::public_key_encryption", "api": "`sealed_box::seal`, `sealed_box::open`", "backend": "libsodium", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//sealed_box-default", "example": "file_share", "section": "crypto-sealed-box"}, {"need": "Share a file with someone known only by a public key, as a file-sharing app does", "namespace": "webcpp::crypto", "api": "`secret_key_encryption::secretbox<>` for the file under a key drawn for it, `public_key_encryption::sealed_box::seal` for that key", "backend": "libsodium", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//secretbox_xsalsa20poly1305-default /webcpp/crypto//sealed_box-default /webcpp/crypto//x25519-default", "example": "file_share", "section": "crypto-everyday-files"}, {"need": "Make and keep a P-256 key pair, and import a browser's public key (WebCrypto's raw form), as webcpp::trystero's peers do", "namespace": "webcpp::crypto::curves::p256", "api": "`keypair::from_secret`, `keypair::generate`, `keypair::public_key`, `keypair::secret`, `public_key::from_bytes`", "backend": "BearSSL", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//p256-default", "example": "p256_keys", "section": "crypto-p256-keys"}, - {"need": "Sign what a browser's WebCrypto verifies and verify what it signs (ECDSA on P-256 with SHA-256), low S where a protocol requires it, as webcpp::trystero's handshakes", "namespace": "webcpp::crypto::curves::p256", "api": "`keypair::sign(crypto::signing_schemes::ecdsa_sha256, message)`, `crypto::verify`, `signature.is_low_s()`, `signature.low_s()`; `crypto::encode` and `crypto::decode` with `crypto::formats::der` for X.509", "backend": "BearSSL", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//p256-default", "example": "p256_sign", "section": "crypto-p256-ecdsa"}, + {"need": "Sign what a browser's WebCrypto verifies and verify what it signs (ECDSA on P-256 with SHA-256), low S where a protocol requires it, as webcpp::trystero's handshakes", "namespace": "webcpp::crypto::curves::p256", "api": "`keypair::sign(crypto::signing_schemes::ecdsa_sha256, message)`, `crypto::verify`, `signature.is_low_s()`, `signature.low_s()`; `codecs::encode` and `codecs::decode` with `crypto::formats::der` for X.509", "backend": "BearSSL", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//p256-default", "example": "p256_sign", "section": "crypto-p256-ecdsa"}, {"need": "Agree on a key with a browser's WebCrypto (ECDH on P-256, deriveBits), then derive the session's keys with HKDF, as Trystero 0.27.0's pairwise keys are derived", "namespace": "webcpp::crypto::curves::p256", "api": "`keypair::agree`; `webcpp::hash::hkdf` for the keys", "backend": "BearSSL", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//p256-default", "example": "p256_ecdh", "section": "crypto-p256-ecdh"}, - {"need": "Export or import a P-256 key in the forms OpenSSL and WebCrypto's raw, spki and pkcs8 use (the point, compressed or not, SPKI, PKCS#8), as DER or PEM text", "namespace": "webcpp::crypto::curves::p256", "api": "`crypto::encode` and `crypto::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, and `crypto::formats::compressed`, on P-256's keys", "backend": "BearSSL", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//p256-default", "example": "p256_key_formats", "section": "crypto-p256-formats"}, - {"need": "Register a public key a browser or a device uploads as PEM, store it as the one SPKI, and verify its signatures, high S too", "namespace": "webcpp::crypto::curves::p256", "api": "`crypto::decode(crypto::formats::pem(crypto::formats::spki), text)`, `crypto::encode(crypto::formats::spki, key)`, `crypto::verify` under `crypto::verification_policies::any_s`", "backend": "BearSSL", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//p256-default", "example": "uploaded_key", "section": "crypto-everyday-uploaded-key"}, + {"need": "Export or import a P-256 key in the forms OpenSSL and WebCrypto's raw, spki and pkcs8 use (the point, compressed or not, SPKI, PKCS#8), as DER or PEM text", "namespace": "webcpp::crypto::curves::p256", "api": "`codecs::encode` and `codecs::decode` with `crypto::formats::raw`, `spki`, `pkcs8` and `pem`, and `crypto::formats::compressed`, on P-256's keys", "backend": "BearSSL", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//p256-default", "example": "p256_key_formats", "section": "crypto-p256-formats"}, + {"need": "Register a public key a browser or a device uploads as PEM, store it as the one SPKI, and verify its signatures, high S too", "namespace": "webcpp::crypto::curves::p256", "api": "`codecs::decode(crypto::formats::pem(crypto::formats::spki), text)`, `codecs::encode(crypto::formats::spki, key)`, `crypto::verify` under `crypto::verification_policies::any_s`", "backend": "BearSSL", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//p256-default", "example": "uploaded_key", "section": "crypto-everyday-uploaded-key"}, {"need": "Sign a peer-to-peer handshake a browser's WebCrypto verifies, low S, check the peer's, and derive the session key from ECDH (Trystero's protocol is webcpp::trystero's)", "namespace": "webcpp::crypto::curves::p256", "api": "`keypair::sign(crypto::signing_schemes::ecdsa_sha256, crypto::signing_schemes::prehashed{digest})`, `signature.is_low_s()`, `crypto::verify`, `keypair::agree`; `webcpp::hash::hkdf` for the key", "backend": "BearSSL", "jamfile": "/webcpp/crypto//crypto /webcpp/crypto//p256-default", "example": "p256_handshake", "section": "crypto-p2p-handshake"} ], "fixtures": { @@ -1446,7 +1446,7 @@ "verify_schnorr_as_ecdsa": "Verify a BIP340 signature with `crypto::signing_schemes::schnorr`, its scheme; `crypto::signing_schemes::ecdsa_sha256` takes a `curves::secp256k1::ecdsa_signature`, which `sign(crypto::signing_schemes::ecdsa_sha256, input)` gives.", "verify_ed25519_bytes_signature": "Make the 64 bytes received into a `curves::ed25519::signature` with `curves::ed25519::signature::from_bytes`, which checks their size, and verify that.", "low_s_on_ed25519": "Only an ECDSA signature has low S: Ed25519 and Schnorr signatures have no `is_low_s()` or `low_s()`, and need none, for no other S verifies as theirs.", - "format_refused_toy": "Encode or decode a type in a format of its list, the `format_list` the assertion names: another type's format (`der` is an ECDSA signature's, `pkcs8` a key pair's) does not convert it.", + "format_refused_toy": "Encode or decode a type in a format of its list, the `codecs::format_list` the assertion names: another type's format (`der` is an ECDSA signature's, `pkcs8` a key pair's) does not convert it.", "pem_of_der": "Armor the DER of a key: `crypto::formats::pem(crypto::formats::spki)` for a public key, `crypto::formats::pem(crypto::formats::pkcs8)` for a key pair; a signature's DER has no PEM.", "pem_of_raw": "As `pem_of_der`: armor the DER of a key, `crypto::formats::pem(crypto::formats::spki)` or `crypto::formats::pem(crypto::formats::pkcs8)`; a key's raw bytes have no PEM.", "format_header_missing_toy": "Include the formats header of the type's curve, the one the assertion names, `/formats.hpp>`, beside the curve's own header, which declares no format.", @@ -1455,21 +1455,29 @@ "format_without_header": "Include ``, or the curve's `/formats.hpp>`, which includes it: `crypto::formats` is declared by `config.hpp` and filled by the formats headers alone.", "decode_refusal_reason_toy": "Decode the x-only form into the type that holds it, as secp256k1's `curves::secp256k1::xonly_public_key`; the type this refuses has the form to encode only.", "refusal_reason_toy": "Encode the signature with `crypto::formats::raw`: an Ed25519 or BIP340 signature has no DER form, its 64 bytes are the signature.", - "pem_decode_from_bytes": "Give a PEM to `crypto::decode` as a text: a `std::string_view`, a `std::string`, or a contiguous range of `char` such as `std::span`; DER bytes decode with `crypto::formats::spki` or `crypto::formats::pkcs8` themselves.", + "pem_decode_from_bytes": "Give a PEM to `codecs::decode`, or a type's member `decode`, as a text: a `std::string_view`, a `std::string`, or a contiguous sized range of `char` such as `std::span`; DER bytes decode with `crypto::formats::spki` or `crypto::formats::pkcs8` themselves.", "forms_without_default": "List `crypto::formats::default_form_t` in each `detail::forms_of` specialization, with the other point forms the type accepts.", "der_on_ed25519": "Encode an Ed25519 signature with `crypto::formats::raw`, or its member `signature.encode(crypto::formats::raw)`: its 64 bytes are the signature, and it has no DER form.", "ed25519_formats_header_missing": "Include ``, which registers Ed25519's formats, beside ``.", - "der_short_name_removed_p256": "Write a P-256 signature's DER with `crypto::encode(crypto::formats::der, signature)` or its member `signature.encode(crypto::formats::der)`, and read it with `crypto::decode(crypto::formats::der, bytes)`; the free `to_der` and `from_der` are gone, with no alias.", + "der_short_name_removed_p256": "Write a P-256 signature's DER with `codecs::encode(crypto::formats::der, signature)` or its member `signature.encode(crypto::formats::der)`, and read it with `codecs::decode(crypto::formats::der, bytes)`; the free `to_der` and `from_der` are gone, with no alias.", + "crypto_encode_removed": "Write a key or a signature with `codecs::encode(format, value)` (`webcpp/codecs.hpp`) or the type's member `value.encode(format)`; `crypto::encode` is gone, with no alias.", + "crypto_decode_removed": "Read a key or a signature with `codecs::decode(format, input)` (`webcpp/codecs.hpp`) or the type's member `T::decode(format, input)`; `crypto::decode` is gone, with no alias.", "pkcs8_on_asynchronous_p256": "Export the key pair with WebCrypto's `exportKey`, on the backend that keeps it, or make the key pair on a backend whose secret is in the program, BearSSL, to encode it in `crypto::formats::pkcs8`, `crypto::formats::raw` or `crypto::formats::pem(crypto::formats::pkcs8)`; its public key's formats are available on every backend.", "pkcs8_on_asynchronous_ed25519": "Export the key pair with WebCrypto's `exportKey`, on the backend that keeps it, or make the key pair on a backend whose secret is in the program, libsodium, to encode it in `crypto::formats::pkcs8`, `crypto::formats::raw` or `crypto::formats::pem(crypto::formats::pkcs8)`; its public key's formats are available on every backend.", "raw_on_asynchronous_x25519": "Export the key pair with WebCrypto's `exportKey`, on the backend that keeps it, or make the key pair on a backend whose secret is in the program, libsodium, to read its secret with `crypto::formats::raw`, `crypto::formats::pkcs8` or `crypto::formats::pem(crypto::formats::pkcs8)`; its public key's formats are available on every backend.", - "pem_header_removed": "Write and read PEM with `crypto::encode` and `crypto::decode` and `crypto::formats::pem(crypto::formats::spki)` or `crypto::formats::pem(crypto::formats::pkcs8)`, in ``, codecs' armor over the key's DER; `pem.hpp` is gone, with no header left at its path.", - "pem_namespace_removed": "Write and read PEM with `crypto::encode` and `crypto::decode` and `crypto::formats::pem(crypto::formats::spki)` or `crypto::formats::pem(crypto::formats::pkcs8)`, which take the label from the inner format; `crypto::pem`, its `armour`, `unarmour`, labels and `armoured_size`, is gone, with no alias.", - "xonly_into_public_key": "A 32-byte x-only key decodes into `crypto::curves::secp256k1::xonly_public_key`: `crypto::decode(crypto::formats::raw(crypto::formats::xonly), bytes)`, or its member `xonly_public_key::decode`; `raw(crypto::formats::xonly)` encodes a public key's x-only key, and its 33- or 65-byte SEC 1 point decodes into a `public_key` with `crypto::formats::raw`.", + "pem_header_removed": "Write and read PEM with `codecs::encode` and `codecs::decode` and `crypto::formats::pem(crypto::formats::spki)` or `crypto::formats::pem(crypto::formats::pkcs8)`, in ``, codecs' armor over the key's DER; `pem.hpp` is gone, with no header left at its path.", + "pem_namespace_removed": "Write and read PEM with `codecs::encode` and `codecs::decode` and `crypto::formats::pem(crypto::formats::spki)` or `crypto::formats::pem(crypto::formats::pkcs8)`, which take the label from the inner format; `crypto::pem`, its `armour`, `unarmour`, labels and `armoured_size`, is gone, with no alias.", + "xonly_into_public_key": "A 32-byte x-only key decodes into `crypto::curves::secp256k1::xonly_public_key`: `codecs::decode(crypto::formats::raw(crypto::formats::xonly), bytes)`, or its member `xonly_public_key::decode`; `raw(crypto::formats::xonly)` encodes a public key's x-only key, and its 33- or 65-byte SEC 1 point decodes into a `public_key` with `crypto::formats::raw`.", "der_on_recoverable": "Encode a recoverable signature with `crypto::formats::raw`, its 65 bytes, r, s and the recovery id, or its member `signature.encode(crypto::formats::raw)`; it has no DER form. A Bitcoin script's DER is that of an `ecdsa_signature`: sign with `crypto::signing_schemes::ecdsa_sha256` and encode it in `crypto::formats::der`.", "der_on_schnorr": "Encode a BIP340 signature with `crypto::formats::raw`, or its member `signature.encode(crypto::formats::raw)`: its 64 bytes are the signature, and it has no DER form.", "keypair_decode_resource_spki": "Decode a secp256k1 key pair, with or without a memory resource, from one of its formats: `crypto::formats::raw`, its secret key, `crypto::formats::pkcs8` or `crypto::formats::pem(crypto::formats::pkcs8)`; an SPKI holds a public key, which `crypto::curves::secp256k1::public_key::decode(crypto::formats::spki, bytes)` gives.", "keypair_decode_resource_from_text": "Give the key pair's decode with a memory resource bytes for `crypto::formats::raw` and `crypto::formats::pkcs8`, a `std::span` or what converts to one (`std::array`, `std::vector`, a result's `view()`); a PEM text decodes with `crypto::formats::pem(crypto::formats::pkcs8)`.", + "keypair_decode_resource_pem_from_bytes": "Give the key pair's decode with a memory resource a text for `crypto::formats::pem(crypto::formats::pkcs8)`: a `std::string_view`, a `std::string`, or a contiguous sized range of `char` such as `std::span`; DER bytes decode with `crypto::formats::pkcs8` itself.", + "keypair_decode_resource_composition": "Decode a secp256k1 key pair with a memory resource from `crypto::formats::raw`, `crypto::formats::pkcs8` or `crypto::formats::pem(crypto::formats::pkcs8)`: decode a composition (its base64, its hexadecimal) with `codecs::decode` to the PKCS#8's bytes first, `codecs::decode>(codecs::formats::base64, text)`, then decode those with `crypto::formats::pkcs8`; or decode the key pair with `codecs::decode`, its signing context from the default resource.", + "pem_other_armor": "Encode or decode a key's PEM with `crypto::formats::pem(crypto::formats::spki)` or `crypto::formats::pem(crypto::formats::pkcs8)`, whose label, `PUBLIC KEY` or `PRIVATE KEY`, is in its type; a text under another label, a certificate's among them, is no key, and decoding it gives `codecs::error::invalid_prefix`.", + "pem_other_basic_armor": "As `pem_other_armor`: name a key's PEM by `crypto::formats::pem(F)`, never by `codecs::formats::basic_armor` of a label, so that the label is always the inner format's.", + "member_der_on_ed25519": "As `der_on_ed25519`: encode an Ed25519 signature with `signature.encode(crypto::formats::raw)`, or `codecs::encode(crypto::formats::raw, signature)`: its 64 bytes are the signature, and it has no DER form.", + "foreign_format_on_key": "Encode or decode a key in one of the formats of its list, the `codecs::format_list` the assertion names; for its bytes in hexadecimal, compose a format of its list, `codecs::over{codecs::formats::hex, crypto::formats::raw}`.", "x25519_formats_header_missing": "Include ``, which registers X25519's formats, beside ``.", "recover_ecdsa_signature": "Give `recover` a `curves::secp256k1::recoverable_signature`, which `sign(crypto::signing_schemes::ecdsa_sha256_recoverable, input)` gives, or made from 65 bytes with `from_bytes`; an ECDSA signature without its recovery id verifies with `crypto::verify` and the signer's public key.", "async_verify_p256_bytes_signature": "As `verify_p256_secp256k1_signature`: make the 64 bytes received into a `curves::p256::ecdsa_signature` with `from_bytes`, then give it to `async_verify`.", diff --git a/doc/tables/generate.py b/doc/tables/generate.py index 6b58e7e..ecc4576 100644 --- a/doc/tables/generate.py +++ b/doc/tables/generate.py @@ -1749,7 +1749,7 @@ def ed25519_cases_page(data: Data, cases: Data) -> str: # The library's one list of formats, detail::all_formats, as formats.hpp declares it: the formats # table's columns are its formats, which crypto.json's `formats` list must name in its order. FORMATS_HEADER = Path('include/webcpp/crypto/formats.hpp') -ALL_FORMATS = re.compile(r'using all_formats\s*=\s*format_list<(.*?)>;', re.S) +ALL_FORMATS = re.compile(r'using all_formats\s*=\s*codecs::format_list<(.*?)>;', re.S) # A format of the list as formats.hpp writes it: `formats::raw_t<>`, `formats::pkcs8_t`, # `formats::pem_t>`. FORMAT_TYPE = re.compile(r'formats::([a-z0-9]+)_t(?:<>)?') @@ -1769,7 +1769,7 @@ def compiled_formats(header: Path) -> List[str]: or a format the reader cannot name, is a fault.""" found = ALL_FORMATS.search(header.read_text(encoding='utf-8')) if found is None: - raise Fault(f'{header.name} declares no `using all_formats = format_list<...>;`') + raise Fault(f'{header.name} declares no `using all_formats = codecs::format_list<...>;`') entries: List[str] = [] depth, start = 0, 0 text = found.group(1) diff --git a/doc/tables/generate_test.py b/doc/tables/generate_test.py index acb9167..7d23252 100644 --- a/doc/tables/generate_test.py +++ b/doc/tables/generate_test.py @@ -1233,10 +1233,10 @@ def test_an_example_s_description_holding_a_tag_directive_is_a_fault(scratch: Pa # The library's list as formats.hpp declares it, the shape the reader takes. ALL_FORMATS_TEXT = ( - 'template \nstruct format_list {};\n\n' '/** The library\'s one list of formats. */\nusing all_formats =\n' - ' format_list, formats::spki_t<>, formats::pkcs8_t, formats::der_t,\n' - ' formats::pem_t>, formats::pem_t>;\n') + ' codecs::format_list, formats::spki_t<>, formats::pkcs8_t,\n' + ' formats::der_t, formats::pem_t>,\n' + ' formats::pem_t>;\n') COMPILED = ['raw', 'spki', 'pkcs8', 'der', 'pem(spki)', 'pem(pkcs8)'] @@ -1271,7 +1271,7 @@ def test_the_compiled_list_is_read_from_formats_hpp(scratch: Path) -> None: assert 'formats::jwk' in str(fault), fault else: raise AssertionError('a format the reader cannot name was read') - none = written(scratch, 'none.hpp', 'struct format_list {};\n') + none = written(scratch, 'none.hpp', 'using all_formats = format_list>;\n') try: generate.compiled_formats(none) except generate.Fault as fault: diff --git a/doc/wallets.adoc b/doc/wallets.adoc index bff4d57..3557393 100644 --- a/doc/wallets.adoc +++ b/doc/wallets.adoc @@ -72,7 +72,7 @@ the compressed public key; it is read into codecs' secret result, which holds it in place and wipes it, a secret decode that codecs runs in constant time (dudect under wasmtime on wasip2 measures it without judging it, an open finding: <>), and its 32 bytes become a key pair through -`crypto::decode(crypto::formats::raw, bytes)`: +`codecs::decode(crypto::formats::raw, bytes)`: [source,cpp] ---- @@ -145,7 +145,7 @@ include::{examples}/bitcoin_sighash.cpp[tag=sighash,indent=0] include::{examples}/bitcoin_sighash.expected[] ---- -`crypto::decode(crypto::formats::der, bytes)` +`codecs::decode(crypto::formats::der, bytes)` decodes strict DER (BIP66) and refuses anything else. A signature from a signer that does not enforce low S is refused by `crypto::verify` under `crypto::verification_policies::low_s_only`, secp256k1's ECDSA's only policy; diff --git a/doc/x25519.adoc b/doc/x25519.adoc index 733507e..3adf096 100644 --- a/doc/x25519.adoc +++ b/doc/x25519.adoc @@ -55,8 +55,8 @@ and not a `crypto::signing_keypair`: X25519 agrees and does not sign `` gives both types their formats (<>), synchronous on every backend: -`crypto::encode(crypto::formats::spki, public_key)` and -`crypto::decode(crypto::formats::spki, bytes)`, which gives the +`codecs::encode(crypto::formats::spki, public_key)` and +`codecs::decode(crypto::formats::spki, bytes)`, which gives the typed key, or the members `public_key.encode(format)` and `public_key::decode(format, input)`, with `formats::raw`, `formats::spki` and `formats::pem(formats::spki)` for a public key; and `formats::raw` (the secret @@ -64,7 +64,7 @@ key, secret), `formats::pkcs8` and `formats::pem(formats::pkcs8)` for a key pair, whose results are secret and wiped. A key pair whose secret lives in WebCrypto has none of the three. Without that header, a member's call is the compiler's "deduced return type used before it is defined", and -`crypto::encode` names the header to include. +`codecs::encode` names the header to include. include::generated/backends-x25519_keypair.adoc[] diff --git a/example/arena_signer.cpp b/example/arena_signer.cpp index 2d576cd..c3fe885 100644 --- a/example/arena_signer.cpp +++ b/example/arena_signer.cpp @@ -22,6 +22,7 @@ #include namespace crypto = webcpp::crypto; +namespace codecs = webcpp::codecs; namespace secure = webcpp::secure; namespace secp256k1 = webcpp::crypto::curves::secp256k1; @@ -89,7 +90,7 @@ int main() { // The key pair stored as PKCS#8, a secret result wiped when it goes, and read back with its // signing context in the same arena: decode without a resource would take it from // std::pmr::get_default_resource(). - auto const stored = crypto::encode(crypto::formats::pkcs8, *key); + auto const stored = codecs::encode(crypto::formats::pkcs8, *key); if (!stored) { return 1; } diff --git a/example/bitcoin_sighash.cpp b/example/bitcoin_sighash.cpp index d35703b..d65d6c3 100644 --- a/example/bitcoin_sighash.cpp +++ b/example/bitcoin_sighash.cpp @@ -84,7 +84,7 @@ int main() { return 1; } // end::sign[] - // Its strict DER, as a script carries it: the signature's member of crypto::encode. + // Its strict DER, as a script carries it: the signature's member of codecs::encode. auto const der = signature->encode(crypto::formats::der); if (!der) { return 1; diff --git a/example/ecdsa_der.cpp b/example/ecdsa_der.cpp index 577368f..aca84f1 100644 --- a/example/ecdsa_der.cpp +++ b/example/ecdsa_der.cpp @@ -27,6 +27,7 @@ #include namespace crypto = webcpp::crypto; +namespace codecs = webcpp::codecs; namespace secure = webcpp::secure; namespace secp256k1 = webcpp::crypto::curves::secp256k1; @@ -103,7 +104,7 @@ int main() { return 1; } // Its strict DER for the wire, at most 72 bytes, in a result that holds them. - auto const der = crypto::encode(crypto::formats::der, *signature); + auto const der = codecs::encode(crypto::formats::der, *signature); if (!der) { return 1; } @@ -111,7 +112,7 @@ int main() { // end::sign[] auto const parsed = - crypto::decode(crypto::formats::der, der->view()); + codecs::decode(crypto::formats::der, der->view()); if (!parsed || *parsed != *signature) { return 1; } diff --git a/example/key_formats.cpp b/example/key_formats.cpp index b00edcd..99f06c1 100644 --- a/example/key_formats.cpp +++ b/example/key_formats.cpp @@ -5,17 +5,17 @@ // https://www.boost.org/LICENSE_1_0.txt) // Reads RFC 8410's Ed25519 private key in PEM (section 10.3), as OpenSSL or WebCrypto export it, -// with crypto::decode, writes its public key as PEM with crypto::encode, which is the RFC's +// with codecs::decode, writes its public key as PEM with codecs::encode, which is the RFC's // section 10.1 to the character, and signs with it; writes an X25519 key pair's public key the // same way, with its member encode; and refuses the RFC's version 2 of the private key, which // carries the public key too. Prints the PEM texts and each verdict. // // tag::program[] +#include #include #include #include #include -#include #include #include @@ -24,6 +24,7 @@ #include #include +namespace codecs = webcpp::codecs; namespace crypto = webcpp::crypto; namespace formats = webcpp::crypto::formats; namespace ed25519 = webcpp::crypto::curves::ed25519; @@ -48,7 +49,7 @@ constexpr std::string_view version2_pem = // An Ed25519 key pair from PEM, its public key back to PEM, and a signature. bool ed25519_from_pem() { // Decoded in place: the PKCS#8 that holds the seed is wiped once the key pair is made. - auto const pair = crypto::decode(formats::pem(formats::pkcs8), private_pem); + auto const pair = codecs::decode(formats::pem(formats::pkcs8), private_pem); if (!pair) { return false; } @@ -56,7 +57,7 @@ bool ed25519_from_pem() { if (!public_key) { return false; } - auto const text = crypto::encode(formats::pem(formats::spki), *public_key); + auto const text = codecs::encode(formats::pem(formats::spki), *public_key); if (!text) { return false; } @@ -91,11 +92,12 @@ bool x25519_to_pem() { return true; } -// Version 2 is refused: strict decoding takes one form, and its PKCS#8 is longer than version 1's. +// Version 2 is refused: strict decoding takes one form, and its PKCS#8 is longer than version 1's, +// past the format's decode bound, so codecs refuses it before the value codec reads it. bool version2_refused() { - auto const pair = crypto::decode(formats::pem(formats::pkcs8), version2_pem); - if (!pair && pair.error() == crypto::error::invalid_encoding) { - std::printf("version 2 refused: invalid_encoding\n"); + auto const pair = codecs::decode(formats::pem(formats::pkcs8), version2_pem); + if (!pair && pair.error() == codecs::error::input_too_long) { + std::printf("version 2 refused: input_too_long\n"); return true; } return false; diff --git a/example/key_formats.expected b/example/key_formats.expected index 206a1ee..82602a3 100644 --- a/example/key_formats.expected +++ b/example/key_formats.expected @@ -4,5 +4,5 @@ MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE= -----BEGIN PUBLIC KEY----- MCowBQYDK2VuAyEAhSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmo= -----END PUBLIC KEY----- -version 2 refused: invalid_encoding +version 2 refused: input_too_long signed and verified diff --git a/example/p256_key_formats.cpp b/example/p256_key_formats.cpp index 1c539f4..21ee331 100644 --- a/example/p256_key_formats.cpp +++ b/example/p256_key_formats.cpp @@ -5,8 +5,8 @@ // https://www.boost.org/LICENSE_1_0.txt) // RFC 6979's P-256 key in the files other programs read: its public key written as PEM SPKI with -// crypto::encode, the text OpenSSL reads and whose DER a WebCrypto peer's importKey("spki") reads, -// printed and read back with crypto::decode; its compressed point, 33 bytes, printed and read back +// codecs::encode, the text OpenSSL reads and whose DER a WebCrypto peer's importKey("spki") reads, +// printed and read back with codecs::decode; its compressed point, 33 bytes, printed and read back // with formats::raw, which takes either point; its key pair written as PEM PKCS#8 for the program's // own key file, a secret result never printed, and read back. The key is RFC 6979's published one, // so that the output is fixed: a real program draws its own. @@ -20,7 +20,7 @@ #include #include -namespace crypto = webcpp::crypto; +namespace codecs = webcpp::codecs; namespace formats = webcpp::crypto::formats; namespace p256 = webcpp::crypto::curves::p256; @@ -29,12 +29,12 @@ namespace { // The public key as PEM SPKI, printed as a configuration file holds it, and read back from that // text: the same point. bool public_key_to_pem(p256::public_key const& key) { - auto const text = crypto::encode(formats::pem(formats::spki), key); + auto const text = codecs::encode(formats::pem(formats::spki), key); if (!text) { return false; } std::printf("%.*s", static_cast(text->view().size()), text->view().data()); - auto const back = crypto::decode(formats::pem(formats::spki), text->view()); + auto const back = codecs::decode(formats::pem(formats::spki), text->view()); return back && *back == key; } @@ -81,7 +81,7 @@ int main() { 0x57, 0x67, 0xb1, 0xd6, 0x93, 0x4e, 0x50, 0xc3, 0xdb, 0x36, 0xe8, 0x9b, 0x12, 0x7b, 0x8a, 0x62, 0x2b, 0x12, 0x0f, 0x67, 0x21, }; - auto const key_pair = crypto::decode(formats::raw, scalar); + auto const key_pair = codecs::decode(formats::raw, scalar); auto const key = key_pair ? key_pair->public_key() : key_pair.error(); if (!key || !public_key_to_pem(*key) || !compressed_point(*key) || !key_pair_to_pem(*key_pair)) { diff --git a/example/secp256k1_key_formats.cpp b/example/secp256k1_key_formats.cpp index 271440d..684e220 100644 --- a/example/secp256k1_key_formats.cpp +++ b/example/secp256k1_key_formats.cpp @@ -5,8 +5,8 @@ // https://www.boost.org/LICENSE_1_0.txt) // A server's secp256k1 key in files that OpenSSL also reads: its public key written as PEM SPKI -// with crypto::encode for a configuration file its clients read, uncompressed as OpenSSL and node -// write it, printed and read back with crypto::decode, and an SPKI stored compressed, as the +// with codecs::encode for a configuration file its clients read, uncompressed as OpenSSL and node +// write it, printed and read back with codecs::decode, and an SPKI stored compressed, as the // library's former default wrote it, read back too; its x-only key, BIP340's 32 bytes, as a Nostr // client publishes it; its key pair written as PEM PKCS#8 for the server's own key file, a secret // result never printed, and read back. The secret is the fixed 0x11 repeated, so that the output @@ -21,7 +21,7 @@ #include #include -namespace crypto = webcpp::crypto; +namespace codecs = webcpp::codecs; namespace formats = webcpp::crypto::formats; namespace secp256k1 = webcpp::crypto::curves::secp256k1; @@ -30,13 +30,13 @@ namespace { // The public key as PEM SPKI, printed as the configuration file holds it, and read back from that // text: the same key. A file written compressed reads back to it as well. bool public_key_to_pem(secp256k1::public_key const& key) { - auto const text = crypto::encode(formats::pem(formats::spki), key); + auto const text = codecs::encode(formats::pem(formats::spki), key); if (!text) { return false; } std::printf("%.*s", static_cast(text->view().size()), text->view().data()); auto const back = - crypto::decode(formats::pem(formats::spki), text->view()); + codecs::decode(formats::pem(formats::spki), text->view()); auto const stored = key.encode(formats::pem(formats::spki(formats::compressed))); if (!stored) { return false; @@ -48,7 +48,7 @@ bool public_key_to_pem(secp256k1::public_key const& key) { // The x-only key, as a Nostr client publishes it, and read back into the typed x-only key. bool xonly_key(secp256k1::public_key const& key) { - auto const xonly = crypto::encode(formats::raw(formats::xonly), key); + auto const xonly = codecs::encode(formats::raw(formats::xonly), key); if (!xonly) { return false; } @@ -57,7 +57,7 @@ bool xonly_key(secp256k1::public_key const& key) { std::printf("%02x", byte); } std::printf("\n"); - auto const back = crypto::decode(formats::raw, xonly->view()); + auto const back = codecs::decode(formats::raw, xonly->view()); return back && *back == key.xonly(); } @@ -84,7 +84,7 @@ bool key_pair_to_pem(secp256k1::keypair const& key_pair) { int main() { std::array secret_key{}; secret_key.fill(0x11); - auto const key_pair = crypto::decode(formats::raw, secret_key); + auto const key_pair = codecs::decode(formats::raw, secret_key); auto const key = key_pair ? key_pair->public_key() : key_pair.error(); if (!key || !public_key_to_pem(*key) || !xonly_key(*key) || !key_pair_to_pem(*key_pair)) { std::printf("failed\n"); diff --git a/example/signed_message.cpp b/example/signed_message.cpp index 939965a..3b7bd90 100644 --- a/example/signed_message.cpp +++ b/example/signed_message.cpp @@ -27,6 +27,7 @@ #include namespace crypto = webcpp::crypto; +namespace codecs = webcpp::codecs; namespace secure = webcpp::secure; namespace secp256k1 = webcpp::crypto::curves::secp256k1; @@ -104,8 +105,8 @@ int main() { // A Nostr event names its author by 32 bytes, the key's x alone: the reader makes the typed // x-only key of them, which checks that they are a point's x, and verifies under it. auto const published = - crypto::encode(crypto::formats::raw(crypto::formats::xonly), *public_key); - auto const event_key = published ? crypto::decode( + codecs::encode(crypto::formats::raw(crypto::formats::xonly), *public_key); + auto const event_key = published ? codecs::decode( crypto::formats::raw, published->view()) : published.error(); if (!event_key || !event_key->verify(crypto::signing_schemes::schnorr, diff --git a/example/uploaded_key.cpp b/example/uploaded_key.cpp index 56ebed2..3e97506 100644 --- a/example/uploaded_key.cpp +++ b/example/uploaded_key.cpp @@ -23,6 +23,7 @@ #include namespace crypto = webcpp::crypto; +namespace codecs = webcpp::codecs; namespace formats = webcpp::crypto::formats; namespace p256 = webcpp::crypto::curves::p256; @@ -57,7 +58,7 @@ int main() { auto const device = p256::keypair::from_secret(device_scalar); auto const device_key = device ? device->public_key() : device.error(); auto const uploaded = - device_key ? crypto::encode(formats::pem(formats::spki), *device_key) : device_key.error(); + device_key ? codecs::encode(formats::pem(formats::spki), *device_key) : device_key.error(); if (!uploaded) { std::printf("failed\n"); return 1; @@ -66,10 +67,10 @@ int main() { // tag::register[] // The server reads the uploaded text: P-256's SPKI, and a point of the curve, or an error. auto const registered = - crypto::decode(formats::pem(formats::spki), uploaded->view()); + codecs::decode(formats::pem(formats::spki), uploaded->view()); // It stores the one encoding crypto writes, 91 bytes, not the text it was sent. auto const stored = - registered ? crypto::encode(formats::spki, *registered) : registered.error(); + registered ? codecs::encode(formats::spki, *registered) : registered.error(); // end::register[] if (!stored) { std::printf("failed\n"); @@ -82,7 +83,7 @@ int main() { // verifies under any_s, and either form verifies. auto const challenge = bytes_of("challenge 7f3a"); auto const signature = device->sign(crypto::signing_schemes::ecdsa_sha256, challenge); - auto const key = crypto::decode(formats::spki, stored->view()); + auto const key = codecs::decode(formats::spki, stored->view()); bool const low_form = signature && key && crypto::verify(crypto::signing_schemes::ecdsa_sha256, crypto::verification_policies::any_s, @@ -96,9 +97,9 @@ int main() { high_form ? "yes" : "no"); // The same key as a compressed SPKI: other bytes, the same key once decoded. - auto const compressed = crypto::encode(formats::spki(formats::compressed), *registered); + auto const compressed = codecs::encode(formats::spki(formats::compressed), *registered); auto const read_back = compressed - ? crypto::decode(formats::spki, compressed->view()) + ? codecs::decode(formats::spki, compressed->view()) : compressed.error(); bool const same_bytes = compressed && std::ranges::equal(compressed->view(), stored->view()); std::printf("compressed SPKI: %zu bytes, the same bytes: %s, the same key: %s\n", diff --git a/example/wallet_wif.cpp b/example/wallet_wif.cpp index 0867999..517f5e8 100644 --- a/example/wallet_wif.cpp +++ b/example/wallet_wif.cpp @@ -65,7 +65,7 @@ boost::system::result read_wif(std::string_view wif) { bytes.back() != compressed_flag) { return crypto::error::invalid_encoding; } - return crypto::decode(formats::raw, bytes.subspan(1, 32)); + return codecs::decode(formats::raw, bytes.subspan(1, 32)); } // end::read[] @@ -95,7 +95,7 @@ bool writes_back(secp256k1::keypair const& key_pair, std::string_view wif) { // The address: version byte 0, then the hash160 of the compressed public key, SHA-256 then // RIPEMD-160 (webcpp::hash's), written with the checksum. bool print_address(secp256k1::public_key const& key) { - auto const point = crypto::encode(formats::raw(formats::compressed), key); + auto const point = codecs::encode(formats::raw(formats::compressed), key); if (!point) { return false; } diff --git a/include/webcpp/crypto/async.hpp b/include/webcpp/crypto/async.hpp index 4a76ecf..72b3f79 100644 --- a/include/webcpp/crypto/async.hpp +++ b/include/webcpp/crypto/async.hpp @@ -1200,7 +1200,7 @@ auto async_verify(Executor const& completion_executor, * @param verification_policy `crypto::verification_policies::low_s_only` or `any_s`. * @param signature_value The signature, r then s (IEEE P1363's form, WebCrypto's); bytes from a * peer are made into one by `curves::p256::ecdsa_signature::from_bytes`, and DER by - * `crypto::decode(crypto::formats::der, bytes)`. + * `codecs::decode(crypto::formats::der, bytes)`. * @param signing_input The signed message, or `crypto::signing_schemes::prehashed{digest}`, its * 32-byte SHA-256 digest. * @param public_key The signer's public key, validated when it was made. diff --git a/include/webcpp/crypto/curves/ed25519.hpp b/include/webcpp/crypto/curves/ed25519.hpp index bc60126..cb5e923 100644 --- a/include/webcpp/crypto/curves/ed25519.hpp +++ b/include/webcpp/crypto/curves/ed25519.hpp @@ -443,7 +443,7 @@ class public_key { } /** - * Encodes the key in `format`, as `crypto::encode(format, key)` does. + * Encodes the key in `format`, as `codecs::encode(format, key)` does. * * The formats are `formats::raw`, its 32 bytes, `formats::spki`, RFC 8410's 44 bytes, and * `formats::pem(formats::spki)`, their PEM. Defined by @@ -452,14 +452,14 @@ class public_key { * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, key)` returns. + * @return What `codecs::encode(format, key)` returns. */ template [[nodiscard]] auto encode(Format const& format) const noexcept(detail::encode_is_noexcept()); /** - * Decodes a public key from `input` in `format`, as `crypto::decode` does. + * Decodes a public key from `input` in `format`, as `codecs::decode` does. * * Defined by ``, as `encode` is. * @@ -467,7 +467,7 @@ class public_key { * @tparam Input The input's type: bytes, or a text for a PEM. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template [[nodiscard]] static auto decode(Format const& format, Input const& input) noexcept( @@ -815,7 +815,7 @@ class basic_keypair { } /** - * Encodes the key pair in `format`, as `crypto::encode(format, key_pair)` does. + * Encodes the key pair in `format`, as `codecs::encode(format, key_pair)` does. * * The formats are `formats::raw`, its seed (`secret()` under the format's name), * `formats::pkcs8`, RFC 8410's 48 bytes, and `formats::pem(formats::pkcs8)`, their PEM; each @@ -825,14 +825,14 @@ class basic_keypair { * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, key_pair)` returns. + * @return What `codecs::encode(format, key_pair)` returns. */ template [[nodiscard]] auto encode(Format const& format) const noexcept(detail::encode_is_noexcept()); /** - * Decodes a key pair from `input` in `format`, as `crypto::decode` does. + * Decodes a key pair from `input` in `format`, as `codecs::decode` does. * * Defined by ``, as `encode` is. * @@ -840,7 +840,7 @@ class basic_keypair { * @tparam Input The input's type: bytes, or a text for a PEM. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template [[nodiscard]] static auto decode(Format const& format, Input const& input) noexcept( diff --git a/include/webcpp/crypto/curves/ed25519/formats.hpp b/include/webcpp/crypto/curves/ed25519/formats.hpp index 2961a2c..5a9d1ba 100644 --- a/include/webcpp/crypto/curves/ed25519/formats.hpp +++ b/include/webcpp/crypto/curves/ed25519/formats.hpp @@ -10,14 +10,16 @@ WebCrypto's `exportKey("spki")` write; a key pair's `formats::raw`, its seed, and `formats::pkcs8`, RFC 8410's version 1, the 48 bytes of the seed alone that OpenSSL's `i2d_PrivateKey` and WebCrypto's `exportKey("pkcs8")` write, both secret; `formats::pem` of - either DER, through codecs' armor; and a signature's `formats::raw`, its 64 bytes. Each is a - `codecs::value_codec`, which `crypto::encode`, `crypto::decode`, codecs' verbs and the types' - members `encode` and `decode`, defined here, call. The DER formats decode strictly, the one - encoding that encoding the key again gives (design D30): any other bytes, a version 2 PKCS#8 - among them, are `invalid_encoding`. + either DER, through codecs' `basic_armor`, its label in its type; and a signature's `formats::raw`, + its 64 bytes. Each is a `codecs::value_codec`, which codecs' verbs, `codecs::encode` and + `codecs::decode`, and the types' members `encode` and `decode`, defined here, call. The DER formats + decode strictly, the one encoding that encoding the key again gives (design D30): any other bytes + within the pair's bound are `invalid_encoding`. A version 2 PKCS#8, 83 bytes, is past `pkcs8`'s + bound of 48, which codecs' verbs and the members refuse with `codecs::error::input_too_long` before + the value codec reads it. The key pair's private formats exist on a backend whose secret is in the program; on a backend - whose key pairs live in WebCrypto (`detail::asynchronous_backend_v`), `crypto::encode` refuses + whose key pairs live in WebCrypto (`detail::asynchronous_backend_v`), codecs' verbs refuse them, naming WebCrypto's `exportKey` (design D7). A public key, which names no backend, is encoded and decoded on the family's default, libsodium on every target. @@ -133,7 +135,7 @@ template <> struct formats_of { /** Its formats. */ using type = - format_list, formats::spki_t<>, formats::pem_t>>; + codecs::format_list, formats::spki_t<>, formats::pem_t>>; }; /** @@ -144,14 +146,15 @@ struct formats_of { template struct formats_of> { /** Its formats. */ - using type = format_list, formats::pkcs8_t, formats::pem_t>; + using type = + codecs::format_list, formats::pkcs8_t, formats::pem_t>; }; /** An Ed25519 signature has `raw`, its 64 bytes. */ template <> struct formats_of { /** Its formats. */ - using type = format_list>; + using type = codecs::format_list>; }; /** An Ed25519 signature has no DER form: its 64 bytes are the signature (design D3). */ @@ -396,8 +399,9 @@ struct value_codec, crypto::curves::ed25519::basic_keyp * An Ed25519 key pair's `formats::pkcs8`: RFC 8410's version 1, 16 bytes then the seed, 48 bytes, * secret. Each call holds `secure::timing_guard`; the encode of a key pair moved from is * `invalid_state`, with nothing written; the decode refuses any other bytes with - * `invalid_encoding`, version 2 among them (design "Deferred: PKCS#8 v2"), and wipes the seed it - * read. + * `invalid_encoding`, version 2 among them when it is called directly (design "Deferred: PKCS#8 + * v2"), and wipes the seed it read. codecs' verbs refuse a version 2, 83 bytes, with + * `codecs::error::input_too_long` first, past `max_decode_input_size`, before this decode runs. * * @tparam Backend The key pair's backend, one whose secret is in the program. */ @@ -557,61 +561,61 @@ static_assert(::webcpp::crypto::detail::check_bounds< namespace webcpp::crypto::curves::ed25519 { /** - * Encodes the key in `format`: the member form of `crypto::encode`. + * Encodes the key in `format`: the member form of `codecs::encode`. * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, *this)` returns. + * @return What `codecs::encode(format, *this)` returns. */ template auto public_key::encode(Format const& format) const noexcept(detail::encode_is_noexcept()) { - return crypto::encode(format, *this); + return codecs::encode(format, *this); } /** - * Decodes a public key from `input` in `format`: the member form of `crypto::decode`. + * Decodes a public key from `input` in `format`: the member form of `codecs::decode`. * * @tparam Format The format's type. * @tparam Input The input's type. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template auto public_key::decode(Format const& format, Input const& input) noexcept( detail::decode_is_noexcept()) { - return crypto::decode(format, input); + return codecs::decode(format, input); } /** - * Encodes the key pair in `format`: the member form of `crypto::encode`. + * Encodes the key pair in `format`: the member form of `codecs::encode`. * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, *this)` returns. + * @return What `codecs::encode(format, *this)` returns. */ template template auto basic_keypair::encode(Format const& format) const noexcept(detail::encode_is_noexcept()) { - return crypto::encode(format, *this); + return codecs::encode(format, *this); } /** - * Decodes a key pair from `input` in `format`: the member form of `crypto::decode`. + * Decodes a key pair from `input` in `format`: the member form of `codecs::decode`. * * @tparam Format The format's type. * @tparam Input The input's type. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template template auto basic_keypair::decode(Format const& format, Input const& input) noexcept( detail::decode_is_noexcept()) { - return crypto::decode(format, input); + return codecs::decode(format, input); } } // namespace webcpp::crypto::curves::ed25519 diff --git a/include/webcpp/crypto/curves/p256.hpp b/include/webcpp/crypto/curves/p256.hpp index 36472ca..90c1252 100644 --- a/include/webcpp/crypto/curves/p256.hpp +++ b/include/webcpp/crypto/curves/p256.hpp @@ -300,7 +300,7 @@ class basic_public_key { } /** - * Encodes the key in `format`, as `crypto::encode(format, key)` does. + * Encodes the key in `format`, as `codecs::encode(format, key)` does. * * The formats are `formats::raw`, the uncompressed point, 65 bytes (`raw(formats::compressed)` * 33), `formats::spki`, RFC 5480's 91 bytes (`spki(formats::compressed)` 59), and @@ -310,14 +310,14 @@ class basic_public_key { * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, key)` returns. + * @return What `codecs::encode(format, key)` returns. */ template [[nodiscard]] auto encode(Format const& format) const noexcept(detail::encode_is_noexcept()); /** - * Decodes a public key from `input` in `format`, as `crypto::decode` does. + * Decodes a public key from `input` in `format`, as `codecs::decode` does. * * Defined by ``, as `encode` is. * @@ -325,7 +325,7 @@ class basic_public_key { * @tparam Input The input's type: bytes, or a text for a PEM. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template [[nodiscard]] static auto decode(Format const& format, Input const& input) noexcept( @@ -844,7 +844,7 @@ class basic_keypair { CompletionToken&& completion_token) const; /** - * Encodes the key pair in `format`, as `crypto::encode(format, key_pair)` does. + * Encodes the key pair in `format`, as `codecs::encode(format, key_pair)` does. * * The formats are `formats::raw`, its secret scalar (`secret()` under the format's name), * `formats::pkcs8`, RFC 5208's 138 bytes around RFC 5915's ECPrivateKey, and @@ -854,14 +854,14 @@ class basic_keypair { * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, key_pair)` returns. + * @return What `codecs::encode(format, key_pair)` returns. */ template [[nodiscard]] auto encode(Format const& format) const noexcept(detail::encode_is_noexcept()); /** - * Decodes a key pair from `input` in `format`, as `crypto::decode` does. + * Decodes a key pair from `input` in `format`, as `codecs::decode` does. * * Defined by ``, as `encode` is. * @@ -869,7 +869,7 @@ class basic_keypair { * @tparam Input The input's type: bytes, or a text for a PEM. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template [[nodiscard]] static auto decode(Format const& format, Input const& input) noexcept( @@ -938,7 +938,7 @@ namespace webcpp::crypto { * @param verification_policy `crypto::verification_policies::low_s_only` or `any_s`. * @param signature_value The signature, r then s (IEEE P1363, WebCrypto's form); bytes from a * peer are made into one by `curves::p256::ecdsa_signature::from_bytes`, and DER by - * `crypto::decode(crypto::formats::der, bytes)`. + * `codecs::decode(crypto::formats::der, bytes)`. * @param signing_input The signed message, hashed with SHA-256 inside the call; or * `crypto::signing_schemes::prehashed{digest}`, its 32-byte SHA-256 digest. * @param public_key The signer's public key. diff --git a/include/webcpp/crypto/curves/p256/formats.hpp b/include/webcpp/crypto/curves/p256/formats.hpp index 9194f7b..fae47d8 100644 --- a/include/webcpp/crypto/curves/p256/formats.hpp +++ b/include/webcpp/crypto/curves/p256/formats.hpp @@ -11,10 +11,10 @@ 91 bytes, which OpenSSL's `i2d_PUBKEY` and WebCrypto's `exportKey("spki")` write, or 59 around the compressed point; a key pair's `formats::raw`, its secret scalar, and `formats::pkcs8`, RFC 5208 around RFC 5915's ECPrivateKey with its public key and no parameters, 138 bytes, both secret; - `formats::pem` of either DER, through codecs' armor; and a signature's `formats::raw`, r then s, - 64 bytes, and `formats::der`, strict DER, 8 to 72 bytes. Each is a `codecs::value_codec`, which - `crypto::encode`, `crypto::decode`, codecs' verbs and the types' members `encode` and `decode`, - defined here, call. + `formats::pem` of either DER, through codecs' `basic_armor`, its label in its type; and a + signature's `formats::raw`, r then s, 64 bytes, and `formats::der`, strict DER, 8 to 72 bytes. Each + is a `codecs::value_codec`, which codecs' verbs, `codecs::encode` and `codecs::decode`, and the + types' members `encode` and `decode`, defined here, call. Each decoder accepts exactly what its encoder writes (design D30); a format named without a point form decodes both forms, the uncompressed and the compressed. A compressed point is decoded @@ -23,7 +23,7 @@ checked as every point is (design R16, as the owner amended it on 2026-12-01). The key pair's private formats exist on a backend whose secret is in the program; on a backend - whose key pairs live in WebCrypto (`detail::asynchronous_backend_v`), `crypto::encode` refuses + whose key pairs live in WebCrypto (`detail::asynchronous_backend_v`), codecs' verbs refuse them, naming WebCrypto's `exportKey` (design D7). A signature, which names no backend, is converted to and from DER on the family's default, BearSSL on every target. @@ -176,7 +176,7 @@ template struct formats_of> { /** Its formats. */ using type = - format_list, formats::spki_t<>, formats::pem_t>>; + codecs::format_list, formats::spki_t<>, formats::pem_t>>; }; /** @@ -210,14 +210,15 @@ struct forms_of, curves::p256::basic_public_key> { template struct formats_of> { /** Its formats. */ - using type = format_list, formats::pkcs8_t, formats::pem_t>; + using type = + codecs::format_list, formats::pkcs8_t, formats::pem_t>; }; /** A P-256 ECDSA signature has `raw`, r then s, and `der` (design D6). */ template <> struct formats_of { /** Its formats. */ - using type = format_list, formats::der_t>; + using type = codecs::format_list, formats::der_t>; }; /** @@ -824,63 +825,63 @@ static_assert(::webcpp::crypto::detail::check_bounds< namespace webcpp::crypto::curves::p256 { /** - * Encodes the key in `format`: the member form of `crypto::encode`. + * Encodes the key in `format`: the member form of `codecs::encode`. * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, *this)` returns. + * @return What `codecs::encode(format, *this)` returns. */ template template auto basic_public_key::encode(Format const& format) const noexcept(detail::encode_is_noexcept()) { - return crypto::encode(format, *this); + return codecs::encode(format, *this); } /** - * Decodes a public key from `input` in `format`: the member form of `crypto::decode`. + * Decodes a public key from `input` in `format`: the member form of `codecs::decode`. * * @tparam Format The format's type. * @tparam Input The input's type. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template template auto basic_public_key::decode(Format const& format, Input const& input) noexcept( detail::decode_is_noexcept()) { - return crypto::decode(format, input); + return codecs::decode(format, input); } /** - * Encodes the key pair in `format`: the member form of `crypto::encode`. + * Encodes the key pair in `format`: the member form of `codecs::encode`. * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, *this)` returns. + * @return What `codecs::encode(format, *this)` returns. */ template template auto basic_keypair::encode(Format const& format) const noexcept(detail::encode_is_noexcept()) { - return crypto::encode(format, *this); + return codecs::encode(format, *this); } /** - * Decodes a key pair from `input` in `format`: the member form of `crypto::decode`. + * Decodes a key pair from `input` in `format`: the member form of `codecs::decode`. * * @tparam Format The format's type. * @tparam Input The input's type. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template template auto basic_keypair::decode(Format const& format, Input const& input) noexcept( detail::decode_is_noexcept()) { - return crypto::decode(format, input); + return codecs::decode(format, input); } } // namespace webcpp::crypto::curves::p256 diff --git a/include/webcpp/crypto/curves/secp256k1.hpp b/include/webcpp/crypto/curves/secp256k1.hpp index 8a7f690..642c9ec 100644 --- a/include/webcpp/crypto/curves/secp256k1.hpp +++ b/include/webcpp/crypto/curves/secp256k1.hpp @@ -22,7 +22,7 @@ key, under either recoverable scheme and `low_s_only`; ECDH in three forms, `agree` (the shared x, as P-256's), `agree_hashed` and `agree_point`, each a secret; and the workspace a function over many keys borrows. Its keys' and signatures' formats, - `crypto::encode` and `crypto::decode` and the types' members `encode` and `decode`, are + `codecs::encode` and `codecs::decode` and the types' members `encode` and `decode`, are ``'s. @see "secp256k1", in the guide. @@ -940,7 +940,7 @@ class xonly_public_key { } /** - * Encodes the x-only key in `format`, as `crypto::encode(format, key)` does. + * Encodes the x-only key in `format`, as `codecs::encode(format, key)` does. * * The formats are `formats::raw` and `formats::raw(formats::xonly)`, its 32 bytes, as BIP340 * serializes it. Defined by ``: without it, the @@ -948,14 +948,14 @@ class xonly_public_key { * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, key)` returns. + * @return What `codecs::encode(format, key)` returns. */ template [[nodiscard]] auto encode(Format const& format) const noexcept(detail::encode_is_noexcept()); /** - * Decodes an x-only key from `input` in `format`, as `crypto::decode` does. + * Decodes an x-only key from `input` in `format`, as `codecs::decode` does. * * Defined by ``, as `encode` is. * @@ -963,7 +963,7 @@ class xonly_public_key { * @tparam Input The input's type: bytes, or a text for a PEM. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template [[nodiscard]] static auto decode(Format const& format, Input const& input) noexcept( @@ -1270,7 +1270,7 @@ class public_key { } /** - * Encodes the key in `format`, as `crypto::encode(format, key)` does. + * Encodes the key in `format`, as `codecs::encode(format, key)` does. * * The formats are `formats::raw`, the uncompressed point, 65 bytes (`raw(formats::compressed)` * 33, and `raw(formats::xonly)` 32, the x-only key's, which decodes into an `xonly_public_key` @@ -1281,14 +1281,14 @@ class public_key { * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, key)` returns. + * @return What `codecs::encode(format, key)` returns. */ template [[nodiscard]] auto encode(Format const& format) const noexcept(detail::encode_is_noexcept()); /** - * Decodes a public key from `input` in `format`, as `crypto::decode` does. + * Decodes a public key from `input` in `format`, as `codecs::decode` does. * * Defined by ``, as `encode` is. * @@ -1296,7 +1296,7 @@ class public_key { * @tparam Input The input's type: bytes, or a text for a PEM. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template [[nodiscard]] static auto decode(Format const& format, Input const& input) noexcept( @@ -1676,7 +1676,7 @@ class basic_keypair { } /** - * Encodes the key pair in `format`, as `crypto::encode(format, key_pair)` does. + * Encodes the key pair in `format`, as `codecs::encode(format, key_pair)` does. * * The formats are `formats::raw`, its secret key, 32 bytes, `formats::pkcs8`, 135 bytes, and * `formats::pem(formats::pkcs8)`, private formats each. Defined by @@ -1687,14 +1687,14 @@ class basic_keypair { * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, key_pair)` returns. + * @return What `codecs::encode(format, key_pair)` returns. */ template [[nodiscard]] auto encode(Format const& format) const noexcept(detail::encode_is_noexcept()); /** - * Decodes a key pair from `input` in `format`, as `crypto::decode` does. + * Decodes a key pair from `input` in `format`, as `codecs::decode` does. * * Defined by ``, as `encode` is. * @@ -1702,7 +1702,7 @@ class basic_keypair { * @tparam Input The input's type: bytes, or a text for a PEM. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template [[nodiscard]] static auto decode(Format const& format, Input const& input) noexcept( @@ -2381,7 +2381,7 @@ namespace webcpp::crypto { * @param verification_policy `crypto::verification_policies::low_s_only`. * @param signature_value The signature, r then s; bytes from a peer are made into one by * `curves::secp256k1::ecdsa_signature::from_bytes`, and DER by - * `crypto::decode(crypto::formats::der, der_bytes)`. + * `codecs::decode(crypto::formats::der, der_bytes)`. * @param signing_input The signed message, hashed with SHA-256 inside the call; or * `crypto::signing_schemes::prehashed{digest}`, the 32-byte digest signed (a Bitcoin sighash). * @param public_key The signer's public key: a key pair's `public_key()`, or diff --git a/include/webcpp/crypto/curves/secp256k1/formats.hpp b/include/webcpp/crypto/curves/secp256k1/formats.hpp index f05b43c..87ab231 100644 --- a/include/webcpp/crypto/curves/secp256k1/formats.hpp +++ b/include/webcpp/crypto/curves/secp256k1/formats.hpp @@ -14,11 +14,11 @@ `formats::raw`, its 32 bytes, BIP340's lift of x; a key pair's `formats::raw`, its secret key, and `formats::pkcs8`, RFC 5208 around RFC 5915's ECPrivateKey with its uncompressed public key and no parameters, 135 bytes, as OpenSSL writes it, both secret; `formats::pem` of either DER, - through codecs' armor; an ECDSA signature's `formats::raw`, r then s, 64 bytes, and `formats::der`, - strict DER (BIP66), 8 to 72 bytes, through libsecp256k1's own encoder and parser; and the - recoverable signature's and the BIP340 signature's `formats::raw`, 65 and 64 bytes. Each is a - `codecs::value_codec`, which `crypto::encode`, `crypto::decode`, codecs' verbs and the types' - members `encode` and `decode`, defined here, call. + through codecs' `basic_armor`, its label in its type; an ECDSA signature's `formats::raw`, r then + s, 64 bytes, and `formats::der`, strict DER (BIP66), 8 to 72 bytes, through libsecp256k1's own + encoder and parser; and the recoverable signature's and the BIP340 signature's `formats::raw`, 65 + and 64 bytes. Each is a `codecs::value_codec`, which codecs' verbs, `codecs::encode` and + `codecs::decode`, and the types' members `encode` and `decode`, defined here, call. Each decoder accepts exactly what its encoder writes (design D30); a format named without a point form decodes both forms, so that an SPKI stored compressed, the former default, still decodes @@ -55,7 +55,9 @@ #include #include #include +#include #include +#include #include namespace webcpp::crypto::detail { @@ -205,6 +207,46 @@ secp256k1_keypair_of_raw(std::span input_bytes, return curves::secp256k1::basic_keypair::from_secret(input_bytes, memory_resource); } +/** + * Whether `Format` is `formats::pem(formats::pkcs8)`, its intermediate's allocator whichever: the + * PEM `basic_keypair::decode` with a resource decodes through its armor. + * + * @tparam Format The format's type. + */ +template +inline constexpr bool secp256k1_pem_pkcs8_v = false; + +/** codecs' armor with PKCS#8's label in its type over `formats::pkcs8`. */ +template +inline constexpr bool secp256k1_pem_pkcs8_v>, formats::pkcs8_t, Allocator>> = true; + +/** + * Whether `Input` is a text a PEM decodes from, as codecs takes one: a type that converts to + * `std::string_view`, or a contiguous sized range of `char` (`std::span`, + * `std::vector`). + * + * @tparam Input The input's type. + */ +template +inline constexpr bool secp256k1_pem_text_v = + std::is_convertible_v || + (std::ranges::contiguous_range && std::ranges::sized_range && + std::is_same_v>, char>); + +/** + * Whether `basic_keypair::decode` with a resource takes `Format`: `formats::raw`, + * `formats::pkcs8` or `formats::pem(formats::pkcs8)`, the formats whose detail decode takes the + * resource. A composition codecs converts, as `codecs::over{codecs::formats::base64, + * formats::pkcs8}`, is not one of them. + * + * @tparam Format The format's type. + */ +template +inline constexpr bool secp256k1_resource_format_v = + std::is_same_v> || std::is_same_v || + secp256k1_pem_pkcs8_v; + /** * The key pair of a PKCS#8 that is exactly what a key pair's `formats::pkcs8` writes for its * secret, its signing context from `memory_resource`: the decode of `formats::pkcs8`, which its @@ -279,7 +321,7 @@ template <> struct formats_of { /** Its formats. */ using type = - format_list, formats::spki_t<>, formats::pem_t>>; + codecs::format_list, formats::spki_t<>, formats::pem_t>>; }; /** @@ -316,7 +358,7 @@ inline constexpr refusal_reason template <> struct formats_of { /** Its formats. */ - using type = format_list>; + using type = codecs::format_list>; }; /** An x-only key's `raw` is its 32 bytes, by default and as `raw(xonly)`. */ @@ -334,28 +376,29 @@ struct forms_of, curves::secp256k1::xonly_public_key> { template struct formats_of> { /** Its formats. */ - using type = format_list, formats::pkcs8_t, formats::pem_t>; + using type = + codecs::format_list, formats::pkcs8_t, formats::pem_t>; }; /** A secp256k1 ECDSA signature has `raw`, r then s, and `der` (design D6). */ template <> struct formats_of { /** Its formats. */ - using type = format_list, formats::der_t>; + using type = codecs::format_list, formats::der_t>; }; /** A recoverable signature has `raw`, r, s and the recovery id. */ template <> struct formats_of { /** Its formats. */ - using type = format_list>; + using type = codecs::format_list>; }; /** A BIP340 signature has `raw`, its 64 bytes. */ template <> struct formats_of { /** Its formats. */ - using type = format_list>; + using type = codecs::format_list>; }; /** A BIP340 signature has no DER form: its 64 bytes are the signature (design D3). */ @@ -453,7 +496,7 @@ struct value_codec, crypto::curves::secp256k1::publ /** * A secp256k1 public key's `formats::raw(formats::xonly)`: its BIP340 x-only key's 32 bytes, * `xonly().bytes()` under the format's name, to encode only: 32 bytes decode into a - * `curves::secp256k1::xonly_public_key`, and `crypto::decode` of this pair stops the build saying + * `curves::secp256k1::xonly_public_key`, and `codecs::decode` of this pair stops the build saying * so (design D3, D4). It holds `secure::timing_guard`: libsecp256k1 drops the parity of y. */ template <> @@ -1153,95 +1196,105 @@ static_assert(::webcpp::crypto::detail::check_bounds< namespace webcpp::crypto::curves::secp256k1 { /** - * Encodes the key in `format`: the member form of `crypto::encode`. + * Encodes the key in `format`: the member form of `codecs::encode`. * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, *this)` returns. + * @return What `codecs::encode(format, *this)` returns. */ template auto public_key::encode(Format const& format) const noexcept(detail::encode_is_noexcept()) { - return crypto::encode(format, *this); + return codecs::encode(format, *this); } /** - * Decodes a public key from `input` in `format`: the member form of `crypto::decode`. + * Decodes a public key from `input` in `format`: the member form of `codecs::decode`. * * @tparam Format The format's type. * @tparam Input The input's type. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template auto public_key::decode(Format const& format, Input const& input) noexcept( detail::decode_is_noexcept()) { - return crypto::decode(format, input); + return codecs::decode(format, input); } /** - * Encodes the x-only key in `format`: the member form of `crypto::encode`. + * Encodes the x-only key in `format`: the member form of `codecs::encode`. * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, *this)` returns. + * @return What `codecs::encode(format, *this)` returns. */ template auto xonly_public_key::encode(Format const& format) const noexcept(detail::encode_is_noexcept()) { - return crypto::encode(format, *this); + return codecs::encode(format, *this); } /** - * Decodes an x-only key from `input` in `format`: the member form of `crypto::decode`. + * Decodes an x-only key from `input` in `format`: the member form of `codecs::decode`. * * @tparam Format The format's type. * @tparam Input The input's type. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template auto xonly_public_key::decode(Format const& format, Input const& input) noexcept( detail::decode_is_noexcept()) { - return crypto::decode(format, input); + return codecs::decode(format, input); } /** - * Encodes the key pair in `format`: the member form of `crypto::encode`. + * Encodes the key pair in `format`: the member form of `codecs::encode`. * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, *this)` returns. + * @return What `codecs::encode(format, *this)` returns. */ template template auto basic_keypair::encode(Format const& format) const noexcept(detail::encode_is_noexcept()) { - return crypto::encode(format, *this); + return codecs::encode(format, *this); } /** - * Decodes a key pair from `input` in `format`: the member form of `crypto::decode`. + * Decodes a key pair from `input` in `format`: the member form of `codecs::decode`. * * @tparam Format The format's type. * @tparam Input The input's type. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template template auto basic_keypair::decode(Format const& format, Input const& input) noexcept( detail::decode_is_noexcept()) { - return crypto::decode(format, input); + return codecs::decode(format, input); } /** * Decodes a key pair from `input` in `format`, its signing context from `memory_resource`: the - * pair checked as `crypto::decode` checks it, then the detail decode the value codec runs with the - * default resource (design R2), run with the caller's. + * pair checked by `codecs::check_conversion`, with the diagnostics of codecs' verbs (crypto's + * sentence, or codecs' message with the type's list), a composition codecs converts refused with + * crypto's sentence that names the three formats it takes, then the detail decode the value codec + * runs with the default resource (design R2, R13), run with the caller's. + * + * An input longer than the pair's decode bound, `formats::max_decode_input_size`, is + * `codecs::error::input_too_long`, as `codecs::decode` gives it, before the detail decode reads a + * byte. A PEM is decoded by codecs' typed decode of its armor, `format.outer`, whose label is the + * inner format's, into a `codecs::secret_bytes` of the PKCS#8's decode bound (no heap, wiped when + * it goes, the armor's errors first, a payload past the bound `input_too_long`), then the detail + * decode of its DER. An input the format does not read stops the build with crypto's sentence + * that names what it takes: bytes for `formats::raw` and `formats::pkcs8`, a text for the PEM. * * @tparam Format The format's type. * @tparam Input The input's type. @@ -1256,25 +1309,51 @@ auto basic_keypair::decode(Format const& format, Input const& input, std::pmr::memory_resource* memory_resource) { using result_type = boost::system::result; secure::timing_guard const timing_scope; - if constexpr (!detail::pair_accepted()) { - detail::refuse_pair(); + codecs::check_conversion(); + if constexpr (!codecs::decodable) { + // Unreachable once the check stops the build: an error, never a value. + return result_type{error::invalid_encoding}; + } else if constexpr (!detail::secp256k1_resource_format_v) { + static_assert(detail::secp256k1_resource_format_v, + "webcpp::crypto: a key pair decodes with a memory resource from " + "formats::raw, formats::pkcs8 or formats::pem(formats::pkcs8)"); return result_type{error::invalid_encoding}; - } else if constexpr (std::is_same_v>) { - return detail::decode_pem_with( - format, input, [memory_resource](std::span der_bytes) { - return detail::secp256k1_keypair_of_pkcs8(der_bytes, memory_resource); - }); + } else if constexpr (detail::secp256k1_pem_pkcs8_v && + !detail::secp256k1_pem_text_v) { + static_assert(detail::secp256k1_pem_text_v, + "webcpp::crypto: a PEM decodes from a text: a std::string_view, or a " + "contiguous sized range of char"); + return result_type{error::invalid_encoding}; + } else if constexpr (detail::secp256k1_pem_pkcs8_v) { + constexpr std::size_t bound = + formats::max_decode_input_size; + auto const der_bytes = codecs::decode>(format.outer, input); + if (!der_bytes) { + // A payload past the bound does not fit: the error over gives past the same bound. + if (der_bytes.error() == codecs::error::output_too_small) { + return result_type{codecs::error::input_too_long}; + } + return result_type{der_bytes.error()}; + } + // Read unchecked: the error was returned above. + return detail::secp256k1_keypair_of_pkcs8(der_bytes.unsafe_value().view(), + memory_resource); } else { static_assert(std::is_convertible_v>, "webcpp::crypto: a key pair decodes from bytes: a " "std::span, or what converts to one"); if constexpr (!std::is_convertible_v>) { return result_type{error::invalid_encoding}; - } else if constexpr (std::is_same_v) { - return detail::secp256k1_keypair_of_pkcs8(input, memory_resource); } else { - static_assert(std::is_same_v>); - return detail::secp256k1_keypair_of_raw(input, memory_resource); + std::span const input_bytes = input; + if (input_bytes.size() > formats::max_decode_input_size) { + return result_type{codecs::error::input_too_long}; + } + if constexpr (std::is_same_v) { + return detail::secp256k1_keypair_of_pkcs8(input_bytes, memory_resource); + } else { + return detail::secp256k1_keypair_of_raw(input_bytes, memory_resource); + } } } } diff --git a/include/webcpp/crypto/curves/x25519.hpp b/include/webcpp/crypto/curves/x25519.hpp index 2be7301..ce4533f 100644 --- a/include/webcpp/crypto/curves/x25519.hpp +++ b/include/webcpp/crypto/curves/x25519.hpp @@ -162,7 +162,7 @@ class public_key { } /** - * Encodes the key in `format`, as `crypto::encode(format, key)` does. + * Encodes the key in `format`, as `codecs::encode(format, key)` does. * * The formats are `formats::raw`, its 32 bytes, `formats::spki`, RFC 8410's 44 bytes, and * `formats::pem(formats::spki)`, their PEM. Defined by @@ -171,14 +171,14 @@ class public_key { * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, key)` returns. + * @return What `codecs::encode(format, key)` returns. */ template [[nodiscard]] auto encode(Format const& format) const noexcept(detail::encode_is_noexcept()); /** - * Decodes a public key from `input` in `format`, as `crypto::decode` does. + * Decodes a public key from `input` in `format`, as `codecs::decode` does. * * Defined by ``, as `encode` is. * @@ -186,7 +186,7 @@ class public_key { * @tparam Input The input's type: bytes, or a text for a PEM. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template [[nodiscard]] static auto decode(Format const& format, Input const& input) noexcept( @@ -453,7 +453,7 @@ class basic_keypair { CompletionToken&& completion_token) const; /** - * Encodes the key pair in `format`, as `crypto::encode(format, key_pair)` does. + * Encodes the key pair in `format`, as `codecs::encode(format, key_pair)` does. * * The formats are `formats::raw`, its secret key (`secret()` under the format's name), * `formats::pkcs8`, RFC 8410's 48 bytes, and `formats::pem(formats::pkcs8)`, their PEM; each @@ -462,14 +462,14 @@ class basic_keypair { * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, key_pair)` returns. + * @return What `codecs::encode(format, key_pair)` returns. */ template [[nodiscard]] auto encode(Format const& format) const noexcept(detail::encode_is_noexcept()); /** - * Decodes a key pair from `input` in `format`, as `crypto::decode` does. + * Decodes a key pair from `input` in `format`, as `codecs::decode` does. * * Defined by ``, as `encode` is. * @@ -477,7 +477,7 @@ class basic_keypair { * @tparam Input The input's type: bytes, or a text for a PEM. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template [[nodiscard]] static auto decode(Format const& format, Input const& input) noexcept( diff --git a/include/webcpp/crypto/curves/x25519/formats.hpp b/include/webcpp/crypto/curves/x25519/formats.hpp index 23b8858..ce88e93 100644 --- a/include/webcpp/crypto/curves/x25519/formats.hpp +++ b/include/webcpp/crypto/curves/x25519/formats.hpp @@ -10,14 +10,15 @@ `i2d_PUBKEY` and WebCrypto's `exportKey("spki")` write; a key pair's `formats::raw`, its secret key, and `formats::pkcs8`, RFC 8410's version 1, the 48 bytes of the secret key alone that OpenSSL's `i2d_PrivateKey` and WebCrypto's `exportKey("pkcs8")` write, both secret; and - `formats::pem` of either DER, through codecs' armor. Each is a `codecs::value_codec`, which - `crypto::encode`, `crypto::decode`, codecs' verbs and the types' members `encode` and `decode`, - defined here, call. The DER formats decode strictly, the one encoding that encoding the key - again gives (design D30): any other bytes, a version 2 PKCS#8 among them, are - `invalid_encoding`. + `formats::pem` of either DER, through codecs' `basic_armor`, its label in its type. Each is a + `codecs::value_codec`, which codecs' verbs, `codecs::encode` and `codecs::decode`, and the types' + members `encode` and `decode`, defined here, call. The DER formats decode strictly, the one + encoding that encoding the key again gives (design D30): any other bytes within the pair's bound + are `invalid_encoding`. A version 2 PKCS#8, 83 bytes, is past `pkcs8`'s bound of 48, which codecs' + verbs and the members refuse with `codecs::error::input_too_long` before the value codec reads it. The key pair's private formats exist on a backend whose secret is in the program; on a backend - whose key pairs live in WebCrypto (`detail::asynchronous_backend_v`), `crypto::encode` refuses + whose key pairs live in WebCrypto (`detail::asynchronous_backend_v`), codecs' verbs refuse them, naming WebCrypto's `exportKey` (design D7). A public key, which names no backend, is encoded and decoded on the family's default, libsodium on every target. @@ -97,7 +98,7 @@ template <> struct formats_of { /** Its formats. */ using type = - format_list, formats::spki_t<>, formats::pem_t>>; + codecs::format_list, formats::spki_t<>, formats::pem_t>>; }; /** @@ -108,7 +109,8 @@ struct formats_of { template struct formats_of> { /** Its formats. */ - using type = format_list, formats::pkcs8_t, formats::pem_t>; + using type = + codecs::format_list, formats::pkcs8_t, formats::pem_t>; }; /** @@ -347,8 +349,9 @@ struct value_codec, crypto::curves::x25519::basic_keypa * An X25519 key pair's `formats::pkcs8`: RFC 8410's version 1, 16 bytes then the secret key, 48 * bytes, secret. Each call holds `secure::timing_guard`; the encode of a key pair moved from is * `invalid_state`, with nothing written; the decode refuses any other bytes with - * `invalid_encoding`, version 2 among them (design "Deferred: PKCS#8 v2"), and wipes the secret - * key it read. + * `invalid_encoding`, version 2 among them when it is called directly (design "Deferred: PKCS#8 + * v2"), and wipes the secret key it read. codecs' verbs refuse a version 2, 83 bytes, with + * `codecs::error::input_too_long` first, past `max_decode_input_size`, before this decode runs. * * @tparam Backend The key pair's backend, one whose secret is in the program. */ @@ -449,61 +452,61 @@ static_assert(::webcpp::crypto::detail::check_bounds<::webcpp::crypto::formats:: namespace webcpp::crypto::curves::x25519 { /** - * Encodes the key in `format`: the member form of `crypto::encode`. + * Encodes the key in `format`: the member form of `codecs::encode`. * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, *this)` returns. + * @return What `codecs::encode(format, *this)` returns. */ template auto public_key::encode(Format const& format) const noexcept(detail::encode_is_noexcept()) { - return crypto::encode(format, *this); + return codecs::encode(format, *this); } /** - * Decodes a public key from `input` in `format`: the member form of `crypto::decode`. + * Decodes a public key from `input` in `format`: the member form of `codecs::decode`. * * @tparam Format The format's type. * @tparam Input The input's type. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template auto public_key::decode(Format const& format, Input const& input) noexcept( detail::decode_is_noexcept()) { - return crypto::decode(format, input); + return codecs::decode(format, input); } /** - * Encodes the key pair in `format`: the member form of `crypto::encode`. + * Encodes the key pair in `format`: the member form of `codecs::encode`. * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, *this)` returns. + * @return What `codecs::encode(format, *this)` returns. */ template template auto basic_keypair::encode(Format const& format) const noexcept(detail::encode_is_noexcept()) { - return crypto::encode(format, *this); + return codecs::encode(format, *this); } /** - * Decodes a key pair from `input` in `format`: the member form of `crypto::decode`. + * Decodes a key pair from `input` in `format`: the member form of `codecs::decode`. * * @tparam Format The format's type. * @tparam Input The input's type. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template template auto basic_keypair::decode(Format const& format, Input const& input) noexcept( detail::decode_is_noexcept()) { - return crypto::decode(format, input); + return codecs::decode(format, input); } } // namespace webcpp::crypto::curves::x25519 diff --git a/include/webcpp/crypto/detail/curve_of.hpp b/include/webcpp/crypto/detail/curve_of.hpp index e674f5a..c49f9b0 100644 --- a/include/webcpp/crypto/detail/curve_of.hpp +++ b/include/webcpp/crypto/detail/curve_of.hpp @@ -7,7 +7,7 @@ /** The curve of each value type of crypto's that has formats (design D3): `detail::curve_id` and `detail::curve_of_v`, which each curve's own header specializes for its types, so that a call of - `crypto::encode` or `crypto::decode` whose curve's formats header is not included names the header + `codecs::encode` or `codecs::decode` whose curve's formats header is not included names the header to include. It includes no codecs header, so that a curve's own header, which never includes its formats header (design D2), declares its types' curve. */ diff --git a/include/webcpp/crypto/detail/format_members.hpp b/include/webcpp/crypto/detail/format_members.hpp index 380fe02..b92f0fd 100644 --- a/include/webcpp/crypto/detail/format_members.hpp +++ b/include/webcpp/crypto/detail/format_members.hpp @@ -5,11 +5,11 @@ // https://www.boost.org/LICENSE_1_0.txt) /** - What the members `encode` and `decode` of crypto's value types declare (design D2): the traits + What the members `encode` and `decode` of crypto's value types declare (design D1): the traits `detail::encode_is_noexcept` and `detail::decode_is_noexcept`, whose answer each member's - exception specification is, as the verbs `crypto::encode` and `crypto::decode` it calls give - theirs. Declared here, with no codecs header, so that a curve's own header and the signature - template declare their members; `` defines them. + exception specification is, the one codecs' verbs `codecs::encode` and `codecs::decode` it calls + give for the same arguments. Declared here, with no codecs header, so that a curve's own header + and the signature template declare their members; `` defines them. */ #ifndef WEBCPP_CRYPTO_DETAIL_FORMAT_MEMBERS_HPP #define WEBCPP_CRYPTO_DETAIL_FORMAT_MEMBERS_HPP @@ -19,7 +19,7 @@ namespace webcpp::crypto::detail { /** - * Whether `crypto::encode` of `Value` in `Format`, with `Rest`, cannot throw: codecs' verb's own + * Whether `codecs::encode` of `Value` in `Format`, with `Rest`, cannot throw: codecs' verb's own * answer, true for every pair of crypto's into its default result or a span (`formats.hpp` * defines it). * @@ -32,9 +32,9 @@ template consteval bool encode_is_noexcept(); /** - * Whether `crypto::decode` of `Input` in `Format` cannot throw: true for every pair of - * crypto's but a key pair whose decode allocates from a memory resource (secp256k1's signing - * context) (`formats.hpp` defines it). + * Whether `codecs::decode` of `Input` in `Format` cannot throw: codecs' verb's own answer, + * true for every pair of crypto's but a key pair whose decode allocates from a memory resource + * (secp256k1's signing context) (`formats.hpp` defines it). * * @tparam Value The value's type. * @tparam Format The format's type. diff --git a/include/webcpp/crypto/detail/signature.hpp b/include/webcpp/crypto/detail/signature.hpp index 668968b..1914574 100644 --- a/include/webcpp/crypto/detail/signature.hpp +++ b/include/webcpp/crypto/detail/signature.hpp @@ -132,7 +132,7 @@ class basic_signature { } /** - * Encodes the signature in `format`, as `crypto::encode(format, signature)` does. + * Encodes the signature in `format`, as `codecs::encode(format, signature)` does. * * The formats are `formats::raw`, its bytes, and, for an ECDSA signature, `formats::der`. * Defined by ``, which the curve's formats header includes: @@ -140,14 +140,14 @@ class basic_signature { * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, signature)` returns. + * @return What `codecs::encode(format, signature)` returns. */ template [[nodiscard]] auto encode(Format const& format) const noexcept(detail::encode_is_noexcept()); /** - * Decodes a signature from `input` in `format`, as `crypto::decode` does. + * Decodes a signature from `input` in `format`, as `codecs::decode` does. * * Defined by ``, as `encode` is. * @@ -155,7 +155,7 @@ class basic_signature { * @tparam Input The input's type: bytes. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template [[nodiscard]] static auto decode(Format const& format, Input const& input) noexcept( diff --git a/include/webcpp/crypto/formats.hpp b/include/webcpp/crypto/formats.hpp index 54913f7..9b8b76b 100644 --- a/include/webcpp/crypto/formats.hpp +++ b/include/webcpp/crypto/formats.hpp @@ -8,20 +8,23 @@ The formats of keys and signatures, registered into webcpp::codecs (design D1, D2, D3 and D11): the format tags `formats::raw`, `formats::spki`, `formats::pkcs8` and `formats::der`, each a `codecs::format`; the point forms `formats::compressed`, `formats::uncompressed` and - `formats::xonly`, which `raw(form)` and `spki(form)` take; `formats::pem(inner)`, codecs' armor - over `spki` or `pkcs8` with the label of the inner format; the library's one list of formats, - `detail::all_formats`, and the traits each value type declares its own with - (`detail::formats_of`, `detail::forms_of`, `detail::curve_of_v`, the refusal reasons); the two - bounds of a value codec, `formats::max_encoded_size` and `formats::max_decode_input_size`; and the - verbs `crypto::encode` and `crypto::decode`, which check a pair of crypto's against the type's - list and then call codecs' verbs, decoding a PEM in place; and the members `encode` and `decode` - of every signature type (`detail::basic_signature`), which call the verbs. + `formats::xonly`, which `raw(form)` and `spki(form)` take; `formats::pem(inner)`, codecs' + `basic_armor` over `spki` or `pkcs8` with the inner format's label in its type + (`detail::pem_label`); the library's one list of formats, `detail::all_formats`, and the traits + each value type declares its own with (`detail::formats_of`, `detail::forms_of`, + `detail::curve_of_v`, the refusal reasons); the two bounds of a value codec, + `formats::max_encoded_size` and `formats::max_decode_input_size`; crypto's declaration of every + one of its value types to codecs, `codecs::value_formats` (the type's list and the reasons of + crypto's sentences), through which `codecs::encode` and `codecs::decode` check every pair; the + members `encode` and `decode` of every signature type (`detail::basic_signature`), which call + codecs' verbs. crypto has no verbs of its own: `codecs::encode` and `codecs::decode` are the + one pair. Each pair of a format and a type is a `codecs::value_codec` specialization in the type's curve's `curves//formats.hpp`; this header includes no curve and no backend, and is the only one, - with those, that includes a codecs header. A program may call `codecs::encode` and - `codecs::decode` with the same formats: it gets the same bytes and the same values, and - codecs' generic diagnostic for a pair that does not exist. + with those, that includes a codecs header. codecs' verbs check every pair: a pair a type does not + have stops the build with one error, crypto's sentence (reported in this header) for a reason of + crypto's, or codecs' message with the type's list (reported in codecs' header). @see "Key formats: raw, SPKI, PKCS#8, DER and PEM", in the guide. */ @@ -36,14 +39,10 @@ #include #include #include -#include -#include #include -#include #include #include -#include #include #include #include @@ -242,21 +241,28 @@ inline constexpr der_t der{}; namespace detail { /** - * The label of the PEM of `Inner`: `"PUBLIC KEY"` for every `formats::spki_t
` and - * `"PRIVATE KEY"` for `formats::pkcs8_t` (RFC 7468); empty for any other, which `pem` refuses. + * The label of the PEM of `Inner`, the `Label` of codecs' `basic_armor` in `formats::pem_t` + * (RFC 7468): its `value` is `"PUBLIC KEY"` for every `formats::spki_t` and `"PRIVATE KEY"` + * for `formats::pkcs8_t`. Undefined for any other format, which `pem` refuses. * * @tparam Inner The format inside the armor. */ template -inline constexpr std::string_view pem_label_v{}; +struct pem_label; /** An SPKI's armor is labelled `PUBLIC KEY`, whatever its point form. */ template -inline constexpr std::string_view pem_label_v> = "PUBLIC KEY"; +struct pem_label> { + /** The label. */ + static constexpr std::string_view value = "PUBLIC KEY"; +}; /** A PKCS#8's armor is labelled `PRIVATE KEY`. */ template <> -inline constexpr std::string_view pem_label_v = "PRIVATE KEY"; +struct pem_label { + /** The label. */ + static constexpr std::string_view value = "PRIVATE KEY"; +}; /** * Whether `pem` takes `Inner`: an `formats::spki_t` or `formats::pkcs8_t`, the DER formats @@ -275,20 +281,57 @@ inline constexpr bool is_pem_inner_v> = true; template <> inline constexpr bool is_pem_inner_v = true; +/** + * The format `formats::pem(inner)` gives for an `Inner` that is no DER format of a key, never a + * program's: `pem`'s assertion has stopped the build. crypto's declaration to codecs gives every + * pair of it a reason whose refusal adds no error (`refuse_with_sentence`), so that the assertion + * stays the build's only error when the format reaches codecs' verbs or a member on a type of + * crypto's. Its links are named as `codecs::over`'s: `outer`, an armor without a label, and + * `inner`. + * + * @tparam Inner The format that is no DER format of a key. + */ +template +struct refused_pem_t { + /** The format's name. */ + static constexpr std::string_view name = "pem of no DER key"; + + /** An armor without a label. */ + codecs::formats::armor_t outer = codecs::formats::armor; + /** The format given to `pem`. */ + Inner inner; +}; + +/** + * Whether `Format` is a `refused_pem_t`. + * + * @tparam Format The format's type. + */ +template +inline constexpr bool is_refused_pem_v = false; + +/** A `refused_pem_t`. */ +template +inline constexpr bool is_refused_pem_v> = true; + } // namespace detail namespace formats { /** - * The type of `formats::pem(inner)`: codecs' armor over `Inner` (design R23). + * The type of `formats::pem(inner)`: codecs' armor whose label is in its type, over `Inner` + * (design D3, codecs' D11). * - * The armor is configured with `Inner`'s label. The links are the chain's members `outer`, the - * armor, and `inner`. + * The label is `Inner`'s, `detail::pem_label::value`: `PUBLIC KEY` for `spki_t` and + * `PRIVATE KEY` for `pkcs8_t`, so that no value of the type holds another, and every verb writes + * and reads that label. The links are the chain's members `outer`, the armor, and `inner`. Its + * default result is exactly as long as the armor of the inner pair's `max_encoded_size` bytes with + * that label (codecs' R24). * * @tparam Inner The DER format inside the armor, `spki_t` or `pkcs8_t`. */ template -using pem_t = codecs::over; +using pem_t = codecs::over>, Inner>; /** * The type of @ref pem, which makes the PEM format of a DER format of a key. @@ -300,25 +343,30 @@ struct pem_builder_t { explicit pem_builder_t() = default; /** - * The PEM of `inner`: codecs' `formats::armor` with `inner`'s label over `inner`. + * The PEM of `inner`: codecs' `formats::basic_armor` with `inner`'s label in its type, over + * `inner`, a `pem_t`. * * The label is `PUBLIC KEY` for `spki` (any point form) and `PRIVATE KEY` for `pkcs8`: no * function of crypto takes a label from its caller. Any other `inner`, `raw` or `der`, stops - * the build. + * the build, with that error alone, also where the format is then given to codecs' verbs or + * to a member on a type of crypto's. * * @tparam Inner The inner format's type. * @param inner The inner format. * @return The PEM format. */ template - [[nodiscard]] constexpr pem_t operator()(Inner inner) const noexcept { + [[nodiscard]] constexpr auto operator()(Inner inner) const noexcept { static_assert(detail::is_pem_inner_v, "webcpp::crypto: PEM carries a DER key: pem(formats::spki) or " "pem(formats::pkcs8)"); if constexpr (detail::is_pem_inner_v) { - return pem_t{codecs::formats::armor(detail::pem_label_v), inner}; + return pem_t{codecs::formats::basic_armor>{}, inner}; } else { - return pem_t{codecs::formats::armor, inner}; + // Never a program's: the assertion above stops the build. A format that names no + // pem_label of `Inner`, which has none, and that crypto's declaration refuses with no + // error of its own, so that the assertion is the build's only error. + return detail::refused_pem_t{.inner = inner}; } } }; @@ -331,7 +379,7 @@ inline constexpr pem_builder_t pem{}; /** * The longest encoding the value codec of `Format` and `T` writes: the capacity of the default - * result of `crypto::encode(format, value)` (design R20). + * result of `codecs::encode(format, value)` (design R20). * * @tparam Format The format's type. * @tparam T The value's type. @@ -341,10 +389,12 @@ inline constexpr std::size_t max_encoded_size = codecs::value_codec:: /** * The decode bound of the value codec of `Format` and `T`: the longest encoded input its decode - * accepts (owner decision 23, design R37). + * accepts (owner decision 23, design R37), the pair's acceptance contract. * - * It sizes the in-place decode of the pair's PEM, and equals @ref max_encoded_size for every pair - * of crypto's until a change rules otherwise. + * codecs refuses a longer input with `codecs::error::input_too_long` before the value codec reads + * a byte, directly and through PEM (codecs' owner decision 29); it sizes the in-place intermediate + * of the pair's PEM in codecs' `over`, and equals @ref max_encoded_size for every pair of crypto's + * until a change rules otherwise. * * @tparam Format The format's type. * @tparam T The value's type. @@ -358,21 +408,13 @@ inline constexpr std::size_t max_decode_input_size = namespace detail { /** - * A list of formats: crypto's own, `all_formats`, and each value type's, `formats_of`. - * - * @tparam Formats The formats, each a format of the base form. - */ -template -struct format_list {}; - -/** - * The library's one list of formats (design D1): the formats of keys and signatures, each of its - * base form, from which `is_format_v`, the check of every pair, the tests and the guide's table - * derive. A later format (JWK) is one more entry. + * The library's one list of formats (design D1), a `codecs::format_list`: the formats of keys and + * signatures, each of its base form, from which `is_format_v`, crypto's declaration of every type + * to codecs, the tests and the guide's table derive. A later format (JWK) is one more entry. */ using all_formats = - format_list, formats::spki_t<>, formats::pkcs8_t, formats::der_t, - formats::pem_t>, formats::pem_t>; + codecs::format_list, formats::spki_t<>, formats::pkcs8_t, formats::der_t, + formats::pem_t>, formats::pem_t>; /** * A list of point forms: the ones a value type accepts for a format, `forms_of`. @@ -383,7 +425,7 @@ template struct form_list {}; /** - * Whether `Item` is one of a `format_list`'s formats or a `form_list`'s forms. + * Whether `Item` is one of a `codecs::format_list`'s formats or a `form_list`'s forms. * * @tparam Item The type to look for. * @tparam List The list. @@ -393,7 +435,7 @@ inline constexpr bool in_list_v = false; /** A format is in a list of formats when it is one of them. */ template -inline constexpr bool in_list_v> = +inline constexpr bool in_list_v> = (std::is_same_v || ...); /** A form is in a list of forms when it is one of them. */ @@ -506,20 +548,21 @@ inline constexpr bool is_format_v = in_list_v, all_formats * Whether every format of `List` is one of `all_formats`: what a curve's formats header asserts * beside each `formats_of` it declares (design D1). * - * @tparam List A `format_list`. + * @tparam List A `codecs::format_list`. */ template inline constexpr bool known_formats_v = false; /** A list of formats is known when each of its formats is in `all_formats`. */ template -inline constexpr bool known_formats_v> = +inline constexpr bool known_formats_v> = (in_list_v && ...); /** - * The formats a value type of crypto's has: a `format_list` of formats of `all_formats`, declared - * by the type's curve's `formats.hpp`, in one place beside its value codecs; `void` for a type - * whose formats header is not included. + * The formats a value type of crypto's has: a `codecs::format_list` of formats of `all_formats`, + * declared by the type's curve's `formats.hpp`, in one place beside its value codecs; `void` for a + * type whose formats header is not included. crypto's declaration to codecs states it as the type's + * `formats`. * * @tparam T The value's type. */ @@ -543,8 +586,8 @@ using formats_of_t = formats_of::type; * * A specialization lists `formats::default_form_t` among its forms: a format named without a * point form names it, and every pair of design D4 has its default form (the uncompressed point, - * or the x-only key's own 32 bytes). The verbs' check stops the build on a list without it, before - * the type's list is read. + * or the x-only key's own 32 bytes). crypto's declaration stops the build on a list without it, + * before the type's list is read. * * @tparam Format The base form of a format. * @tparam T The value's type. @@ -566,21 +609,21 @@ using forms_of_t = forms_of::type; /** * Whether the value type `T`, whose formats are `List`, refuses `Format`: the base form of - * `Format` is not in `List`, or the point form it names is not in `forms_of`. Its template - * arguments, the type's list among them, are what a compiler prints in the failed requirement of - * crypto's check (design D3); false for a type that declares no list. + * `Format` is not in `List`, or the point form it names is not in `forms_of`. crypto's declaration + * to codecs states the type's `has_format` as its negation (design D2); false for a type that + * declares no list. * * @tparam Format The format's type. * @tparam T The value's type. - * @tparam List The type's `format_list`. + * @tparam List The type's `codecs::format_list`. */ template inline constexpr bool format_refused = false; /** Refused when the base form is not listed, or the point form is not accepted. */ template -inline constexpr bool format_refused> = - !in_list_v, format_list> || +inline constexpr bool format_refused> = + !in_list_v, codecs::format_list> || !in_list_v::type, forms_of_t::type, T>>; @@ -593,7 +636,8 @@ template inline constexpr bool formats_declared_v = !std::is_void_v>; /** - * A pair's own reason for its refusal, which adds a fixed sentence to crypto's check (design D3). + * A pair's own reason for its refusal, whose fixed sentence crypto's declaration gives codecs + * (design D2, `formats`' D3). */ enum class refusal_reason : unsigned char { /** No reason of its own: the type's list says it. */ @@ -619,8 +663,8 @@ template inline constexpr refusal_reason refusal_reason_v = refusal_reason::none; /** - * The reason a curve gives for refusing `Format` on `T` when decoding alone, consulted by - * `crypto::decode` before `refusal_reason_v` (an encode-only pair, as `raw(xonly)` of a + * The reason a curve gives for refusing `Format` on `T` when decoding alone, consulted by crypto's + * declaration before `refusal_reason_v` (an encode-only pair, as `raw(xonly)` of a * `curves::secp256k1::public_key`); `none` unless the curve's formats header specializes it. * * @tparam Format The format's type. @@ -683,65 +727,78 @@ consteval bool check_bounds() noexcept { } /** - * Whether the value codec of `Format` and `Value` is secret: it states `secret`, and it is true. - * The member is optional in codecs' contract, and absent from a public value codec. + * Whether `T`'s `forms_of` for `Format` lists `formats::default_form_t`, as every specialization + * must (a format named without a point form names it). * * @tparam Format The format's type. - * @tparam Value The value's type. + * @tparam T The value's type. */ -template -inline constexpr bool value_codec_secret_v = - requires { requires static_cast(codecs::value_codec::secret); }; +template +inline constexpr bool default_form_listed_v = + in_list_v::type, T>>; /** - * Whether crypto checks a call of `Format` on `Value`: `Value` is a type of crypto's and `Format` - * a format of crypto's. Any other pair is codecs' to check. + * Whether `Label`, the label type of a `basic_armor` over `Inner`, holds another label than + * `Inner`'s: its `value` is not `pem_label::value`, or it has none. False for an `Inner` + * that is no DER format of a key, whose label is never read. * - * @tparam Format The format's type. - * @tparam Value The value's type. - */ -template -inline constexpr bool checked_by_crypto_v = - curve_of_v != curve_id::none && is_format_v; + * @tparam Label The armor's label type. + * @tparam Inner The format inside the armor. + * @return Whether the label is not `Inner`'s. + */ +template +[[nodiscard]] consteval bool other_label() noexcept { + if constexpr (!is_pem_inner_v) { + return false; + } else if constexpr (requires { std::string_view{Label::value}; }) { + return std::string_view{Label::value} != pem_label::value; + } else { + return true; + } +} /** - * Whether `T`'s `forms_of` for `Format` lists `formats::default_form_t`, as every specialization - * must (a format named without a point form names it). + * Whether `Format` is an armor over a DER format of a key whose label is not the inner format's + * (design D2's fifth reason, R5): codecs' `armor_t`, with any label, or a `basic_armor` whose + * label's value is not the inner format's, over `formats::spki_t` or `formats::pkcs8_t`. A + * key's PEM is `formats::pem(inner)`, whose label is in its type; a `basic_armor` of the same + * label's value composes, as `basic_armor>>` over `spki_t` does. + * An armor over `raw` or `der` is a composition, as `codecs::over{codecs::formats::base64, inner}` + * is, and no reason of crypto's. * * @tparam Format The format's type. - * @tparam T The value's type. */ -template -inline constexpr bool default_form_listed_v = - in_list_v::type, T>>; +template +inline constexpr bool other_armor_v = false; + +/** codecs' armor over a DER format of a key, whatever its label. */ +template +inline constexpr bool other_armor_v> = + is_pem_inner_v; + +/** codecs' armor with its label in its type over a DER format of a key, the label not its own. */ +template +inline constexpr bool + other_armor_v, Inner, Allocator>> = + other_label(); /** - * Whether the pair passes crypto's check, in the direction `Decoding` says: always for a pair - * crypto does not check; else the type's formats are declared, the pair has no reason of its own, - * its list holds the format and its form, and codecs converts it. + * Whether `T` refuses `Format` for a reason of crypto's, in the direction `Decoding` says: what + * crypto's declaration to codecs states as `has_reason` (design D2), in its order. The type's + * formats header is not included; its `forms_of` for the format lacks the default form; decoding, + * the pair's reason of decoding alone; the pair's reason of both directions; or the format is an + * armor over a DER format of a key other than `formats::pem_t` of it; or the format is what + * `formats::pem` gives for no DER format of a key (`refused_pem_t`), whose refusal is `pem`'s. * - * @tparam Decoding Whether the call decodes. + * @tparam Decoding Whether the conversion decodes. * @tparam Format The format's type. - * @tparam Value The value's type. - * @return Whether the call may proceed to codecs. + * @tparam T The value's type. */ -template -consteval bool pair_accepted() noexcept { - if constexpr (!checked_by_crypto_v) { - return true; - } else if constexpr (Decoding) { - return formats_declared_v && default_form_listed_v && - decode_refusal_reason_v == refusal_reason::none && - refusal_reason_v == refusal_reason::none && - !format_refused> && - codecs::decodable; - } else { - return formats_declared_v && default_form_listed_v && - refusal_reason_v == refusal_reason::none && - !format_refused> && - codecs::encodable; - } -} +template +inline constexpr bool refused_for_reason_v = + is_refused_pem_v || !formats_declared_v || !default_form_listed_v || + (Decoding && decode_refusal_reason_v != refusal_reason::none) || + refusal_reason_v != refusal_reason::none || other_armor_v; /** * Stops the build with the sentence of `Reason` (design D3). @@ -764,58 +821,52 @@ constexpr void refuse_for_reason() noexcept { } /** - * Stops the build for a pair `pair_accepted` refuses, with the first of crypto's sentences that - * applies (design D3): the formats header to include; a `forms_of` without the default form; the - * pair's own reason; that the type has no such format, the type's list printed in the failed - * requirement; or that codecs does not convert the pair in this direction. + * Stops the build for a pair `refused_for_reason_v` holds for, with the first of crypto's + * sentences that applies (design D2): the formats header to include; a `forms_of` without the + * default form; the pair's reason when decoding, then in both directions; or that a key's PEM is + * `formats::pem(formats::spki)` or `formats::pem(formats::pkcs8)`. What crypto's declaration to + * codecs states as `refuse`, which codecs calls in place of converting. A `refused_pem_t` adds no + * error: `formats::pem`'s assertion has stopped the build, and codecs' operation returns an error. * - * @tparam Decoding Whether the call decodes. + * @tparam Decoding Whether the conversion decodes. * @tparam Format The format's type. - * @tparam Value The value's type. + * @tparam T The value's type. */ -template -constexpr void refuse_pair() noexcept { - if constexpr (!formats_declared_v) { - static_assert(curve_of_v != curve_id::ed25519 || formats_declared_v, +template +constexpr void refuse_with_sentence() noexcept { + if constexpr (is_refused_pem_v) { + // formats::pem's assertion is the build's only error. + } else if constexpr (!formats_declared_v) { + static_assert(curve_of_v != curve_id::ed25519 || formats_declared_v, "webcpp::crypto: include to " "encode or decode this type"); - static_assert(curve_of_v != curve_id::x25519 || formats_declared_v, + static_assert(curve_of_v != curve_id::x25519 || formats_declared_v, "webcpp::crypto: include to " "encode or decode this type"); - static_assert(curve_of_v != curve_id::p256 || formats_declared_v, + static_assert(curve_of_v != curve_id::p256 || formats_declared_v, "webcpp::crypto: include to encode " "or decode this type"); - static_assert(curve_of_v != curve_id::secp256k1 || formats_declared_v, + static_assert(curve_of_v != curve_id::secp256k1 || formats_declared_v, "webcpp::crypto: include to " "encode or decode this type"); - } else if constexpr (!default_form_listed_v) { - static_assert(default_form_listed_v, + } else if constexpr (!default_form_listed_v) { + static_assert(default_form_listed_v, "webcpp::crypto: a forms_of specialization lists formats::default_form_t, " "the form of a format named without one"); - } else if constexpr (Decoding && - decode_refusal_reason_v != refusal_reason::none) { - refuse_for_reason>(); - } else if constexpr (refusal_reason_v != refusal_reason::none) { - refuse_for_reason>(); - } else if constexpr (format_refused>) { - // Qualified, so that the failed requirement names the trait and the type's list. - static_assert(!webcpp::crypto::detail::format_refused>, - "webcpp::crypto: this type has no such format; the formats it has are the " - "format_list named in this assertion"); - } else if constexpr (Decoding) { - static_assert(codecs::decodable, - "webcpp::crypto: this type lists the format, and no codecs::value_codec of " - "the pair decodes it"); + } else if constexpr (Decoding && decode_refusal_reason_v != refusal_reason::none) { + refuse_for_reason>(); + } else if constexpr (refusal_reason_v != refusal_reason::none) { + refuse_for_reason>(); } else { - static_assert(codecs::encodable, - "webcpp::crypto: this type lists the format, and no codecs::value_codec of " - "the pair encodes it"); + static_assert(!other_armor_v, + "webcpp::crypto: a key's PEM is formats::pem(formats::spki) or " + "formats::pem(formats::pkcs8), whose label is the inner format's"); } } /** - * Whether `crypto::encode` of the pair, with `Rest`, cannot throw: codecs' verb's own answer for a - * pair that passes the check (a pair of crypto's is `noexcept`). + * Whether `codecs::encode` of `Value` in `Format`, with `Rest`, cannot throw: codecs' verb's own + * answer, which each member `encode` gives (design D1). * * @tparam Format The format's type. * @tparam Value The value's type. @@ -824,141 +875,14 @@ constexpr void refuse_pair() noexcept { */ template consteval bool encode_is_noexcept() { - if constexpr (pair_accepted()) { - return noexcept(codecs::encode(std::declval(), std::declval(), - std::declval()...)); - } else { - return true; - } + return noexcept(codecs::encode(std::declval(), std::declval(), + std::declval()...)); } /** - * The inner format of a PEM format, `type`; none for another format. - * - * @tparam Format The format's type. - */ -template -struct pem_inner {}; - -/** The inner link of a PEM. */ -template -struct pem_inner> { - /** The DER format inside the armor. */ - using type = Inner; -}; - -/** - * Whether a call of `Format` on `Value` runs with the inner format's fixed label (owner decision - * 1, "the label can never be wrong"): `Format` is a PEM and the pair is crypto's. A hand-built - * `formats::pem_t` whose armor holds another label, or none, encodes and decodes with - * `pem_label_v` all the same; any other pair keeps its own armor. - * - * @tparam Format The format's type. - * @tparam Value The value's type. - */ -template -inline constexpr bool fixes_pem_label_v = false; - -/** A PEM of crypto's pair has its inner format's label. */ -template -inline constexpr bool fixes_pem_label_v, Value> = - checked_by_crypto_v, Value>; - -/** - * The armor a PEM call of `Value` in `format` runs: codecs' armor with `pem_label_v` for a - * pair of crypto's (`fixes_pem_label_v`), whatever armor `format` holds; `format`'s own for any - * other pair. - * - * @tparam Value The value's type. - * @tparam Inner The DER format inside the armor. - * @param format The PEM format. - * @return The armor. - */ -template -[[nodiscard]] constexpr codecs::formats::armor_t pem_armor_of( - formats::pem_t const& format) noexcept { - if constexpr (fixes_pem_label_v, Value>) { - return codecs::formats::armor(pem_label_v); - } else { - return format.outer; - } -} - -/** - * Whether `Text` is a text a PEM decodes from, as codecs takes one: a type that converts to - * `std::string_view`, or a contiguous sized range of `char` (`std::span`, - * `std::vector`). - * - * @tparam Text The input's type. - */ -template -concept pem_text = - std::is_convertible_v || - (std::ranges::contiguous_range && std::ranges::sized_range && - std::is_same_v, char>); - -/** - * Whether the inner value codec of a PEM of `Value` states its decode bound, which sizes the - * in-place decode (owner decision 23). - * - * @tparam Inner The PEM's inner format. - * @tparam Value The value's type. - */ -template -inline constexpr bool states_decode_bound_v = - requires { codecs::value_codec::max_decode_input_size; }; - -/** - * Whether `crypto::decode(format, input)` decodes in place (design R24): `Format` is a PEM, - * and either the pair is crypto's, which never goes to codecs' heap path (`decode_pem` stops the - * build on an input that is no text or an inner pair without its decode bound), or `Input` is a - * text and the inner value codec decodes bytes into `Value` and states its decode bound. - * - * @tparam Value The value's type. - * @tparam Format The format's type. - * @tparam Input The input's type. - */ -template -inline constexpr bool decodes_in_place_v = false; - -/** A PEM of crypto's pair, or of a text whose inner value codec states its bound, is in place. */ -template -inline constexpr bool decodes_in_place_v, Input> = - checked_by_crypto_v, Value> || requires { - requires pem_text; - requires codecs::decodable; - requires codecs::value_codec::output == codecs::output_kind::bytes; - requires codecs::value_codec::max_decode_input_size > 0; - }; - -/** - * Whether the in-place decode of a PEM in `Format` into a `Value` cannot throw: the armor's step - * cannot, so it is as the inner value codec's decode of bytes, as codecs calls it; `noexcept` for - * every pair of crypto's but a key pair whose decode allocates from a memory resource (secp256k1's - * signing context). - * - * @tparam Value The value's type. - * @tparam Format The PEM format's type, `formats::pem_t`. - * @return Whether the decode is `noexcept`. - */ -template -consteval bool pem_decode_is_noexcept() { - using inner_type = pem_inner::type; - if constexpr (requires { - codecs::decode(std::declval(), - std::declval>()); - }) { - return noexcept(codecs::decode(std::declval(), - std::declval>())); - } else { - return true; - } -} - -/** - * Whether `crypto::decode` of `Input` in `Format` cannot throw: the in-place PEM decode is - * as its inner value codec's decode; any other is as codecs' verb, `noexcept` for every pair of - * crypto's but a key pair whose decode allocates from a memory resource. + * Whether `codecs::decode` of `Input` in `Format` cannot throw: codecs' verb's own answer, + * which each member `decode` gives (design D1); `noexcept` for every pair of crypto's but a key + * pair whose decode allocates from a memory resource (secp256k1's signing context). * * @tparam Value The value's type. * @tparam Format The format's type. @@ -967,278 +891,109 @@ consteval bool pem_decode_is_noexcept() { */ template consteval bool decode_is_noexcept() { - if constexpr (!pair_accepted()) { - return true; - } else if constexpr (decodes_in_place_v) { - return pem_decode_is_noexcept(); - } else { - return noexcept( - codecs::decode(std::declval(), std::declval())); - } + return noexcept( + codecs::decode(std::declval(), std::declval())); } -/** - * The text of `text` as a `std::string_view`: its conversion, or its characters. - * - * @tparam Text A `pem_text`. - * @param text The text. - * @return A view of its characters. - */ -template -[[nodiscard]] std::string_view pem_text_view(Text const& text) noexcept { - if constexpr (std::is_convertible_v) { - return text; - } else { - return {std::ranges::data(text), std::ranges::size(text)}; - } -} +} // namespace detail -/** - * The two steps of the in-place PEM decode into `der`: the armor through codecs' buffer form, then - * `inner_decode` over the bytes it wrote. A payload longer than `der` is crypto's - * `invalid_encoding`; every other error of either step is returned unchanged. - * - * @tparam Value The value's type. - * @tparam InnerDecode The inner step's type. - * @param armor The armor, `pem_armor_of`'s. - * @param text The PEM text. - * @param der The buffer of the inner format's decode bound. - * @param inner_decode The inner step: the value of the DER's bytes, or the error. - * @return The value, or the error. - */ -template -[[nodiscard]] codecs::result decode_pem_into( - codecs::formats::armor_t const& armor, std::string_view text, std::span der, - InnerDecode const& - inner_decode) noexcept(noexcept(inner_decode(std::span{}))) { - codecs::result const written = codecs::decode(armor, text, der); - if (!written) { - if (written.error() == codecs::error::output_too_small) { - return error::invalid_encoding; - } - return written.error(); - } - // Read unchecked: the error was returned above. - return inner_decode(std::span(der.first(written.unsafe_value()))); -} +} // namespace webcpp::crypto -/** - * The stack buffer of the in-place PEM decode, wiped by `secure::erase_secret` when it goes, on - * every path, when the decode is secret. - * - * @tparam Size The inner format's decode bound. - * @tparam Secret Whether the decode is secret. - */ -template -struct pem_buffer { - /** The DER the armor writes. */ - std::array bytes{}; - - /** Zeroed bytes. */ - pem_buffer() = default; - /** Not copied: the bytes stay in one place, where they are wiped. */ - pem_buffer(pem_buffer const&) = delete; - /** Not copied. */ - pem_buffer& operator=(pem_buffer const&) = delete; - /** Not moved: the bytes stay in one place, where they are wiped. */ - pem_buffer(pem_buffer&&) = delete; - /** Not moved. */ - pem_buffer& operator=(pem_buffer&&) = delete; - - /** Wipes the bytes of a secret decode. */ - ~pem_buffer() { - if constexpr (Secret) { - secure::erase_secret(bytes); - } - } -}; +namespace webcpp::codecs { /** - * Decodes the PEM `text` into a `Value` in place (design R24), the inner step `inner_decode`: the - * armor through codecs' buffer form into a stack buffer of the inner format's decode bound, - * `max_decode_input_size` (owner decision 23), then `inner_decode` over the DER, allocating - * nothing of its own. `decode_pem`'s inner step is the inner value codec, as codecs calls it; a - * secp256k1 key pair's decode with a memory resource gives its own, which takes the key pair's - * signing context from the caller's resource (design R2). + * crypto's declaration of every one of its value types to codecs (design D2, codecs' D9): each type + * whose `crypto::detail::curve_of_v` names a curve, its formats header included or not, so that + * `codecs::encode`, `codecs::decode`, `codecs::encodable`, `codecs::decodable`, + * `codecs::check_conversion` and the members convert exactly crypto's pairs and refuse any other + * with one error. * - * `text` converts to a `std::string_view` or is a contiguous sized range of `char`, as codecs - * takes a text; any other input, and an inner pair that states no decode bound, stop the build. A - * payload longer than the buffer is crypto's `invalid_encoding`; the armor's errors are codecs', - * unchanged, and come first, as `codecs::decode` gives them. When the inner value codec is secret, - * or the value is (`codecs::is_secret_v`), the call holds `secure::timing_guard` and the buffer is - * wiped by `secure::erase_secret` on every path, an exception's (a key pair's memory resource) - * included. + * Its `formats` is the type's `crypto::detail::formats_of_t`, `codecs::format_list<>` for a type + * whose formats header is not included; `has_format` holds when the format's base form is + * listed and its point form accepted (`crypto::detail::format_refused`, negated); `has_reason` and + * `refuse` are crypto's reasons, in the order of design D2: the formats header to include, a + * `forms_of` without the default form, the pair's reason of decoding, its reason of both + * directions, and an armor over `spki` or `pkcs8` other than `crypto::formats::pem` of it. Each + * reason stops the build with crypto's sentence, reported in this header; a format the type does + * not have is refused with codecs' message that names the type's `codecs::format_list`, and a + * listed pair with no value codec with codecs' message, both in codecs' header. A composition of a + * format of the type under another codec, `codecs::over{codecs::formats::base64, + * crypto::formats::spki}`, converts as its inner format does. * - * @tparam Value The value's type. - * @tparam Format The PEM format's type, `formats::pem_t`. - * @tparam Text The text's type. - * @tparam InnerDecode The inner step's type. - * @param format The PEM format. - * @param text The PEM text. - * @param inner_decode The inner step: the value of the DER's bytes, or the error. - * @return The value, or the error. - */ -template -[[nodiscard]] codecs::result -decode_pem_with(Format const& format, Text const& text, InnerDecode const& inner_decode) noexcept( - noexcept(inner_decode(std::span{}))) { - using inner_type = pem_inner::type; - constexpr bool secret = value_codec_secret_v || codecs::is_secret_v; - secure::timing_guard const timing_scope{secret}; - static_assert(pem_text, - "webcpp::crypto: a PEM decodes from a text: a std::string_view, or a contiguous " - "range of char"); - if constexpr (pem_text) { - static_assert(states_decode_bound_v, - "webcpp::crypto: a value codec that decodes states max_decode_input_size, " - "its decode bound"); - if constexpr (states_decode_bound_v) { - pem_buffer, secret> der; - return decode_pem_into(pem_armor_of(format), pem_text_view(text), - std::span(der.bytes), inner_decode); - } else { - return error::invalid_encoding; - } - } else { - return error::invalid_encoding; - } -} - -/** - * Decodes the PEM `text` into a `Value` in place (design R24): `decode_pem_with` whose inner step - * is the inner value codec, as `codecs::decode` calls it, holding `secure::timing_guard` - * when the decode is secret, as `decode_pem_with` does. - * - * @tparam Value The value's type. - * @tparam Format The PEM format's type, `formats::pem_t`. - * @tparam Text The text's type. - * @param format The PEM format. - * @param text The PEM text. - * @return The value, or the error. + * @tparam T The value's type. */ -template -[[nodiscard]] codecs::result decode_pem(Format const& format, Text const& text) noexcept( - pem_decode_is_noexcept()) { - using inner_type = pem_inner::type; - // Its own guard first, as every function of crypto's that a secret decode enters. - constexpr bool secret = value_codec_secret_v || codecs::is_secret_v; - secure::timing_guard const timing_scope{secret}; - return decode_pem_with( - format, text, - // Generic, so that its body is instantiated only where the armor's step reaches it. - [&format](auto const der_bytes) noexcept(pem_decode_is_noexcept()) - -> codecs::result { return codecs::decode(format.inner, der_bytes); }); -} +template + requires(crypto::detail::curve_of_v != crypto::detail::curve_id::none) +struct value_formats { + /** The type's formats, or none when its formats header is not included. */ + using formats = std::conditional_t, + crypto::detail::formats_of_t, format_list<>>; -} // namespace detail + /** + * Whether the type has `Format`: its base form is listed, and its point form accepted. + * + * @tparam Format The format's type. + */ + template + static constexpr bool has_format = + crypto::detail::formats_declared_v && + !crypto::detail::format_refused>; -/** - * Encodes `value` in `format`: checks the pair against crypto's list, then returns - * `codecs::encode(format, value, rest...)` (design R18), codecs' default result (`bytes`, - * `text`, `secret_bytes`, `secret_text`, `N` the value codec's `max_encoded_size`), the - * result of a token of codecs' (`codecs::tokens::append_to(text)`), or, into a caller's - * `std::span` or `std::span`, the size written. - * - * For a type of crypto's and a format of crypto's, the build stops when the type's curve's formats - * header is not included, when the pair has a reason of its own (an Ed25519 signature has no DER - * form), when the type's list lacks the format or its point form (the list printed in the failed - * requirement), or when no value codec encodes the pair. A PEM of crypto's pair is written with - * its inner format's label, `PUBLIC KEY` or `PRIVATE KEY`, whatever armor a hand-built - * `formats::pem_t` holds. Any other pair is passed to codecs untouched. The span form refuses an - * output shorter than the exact encoding with `codecs::error::output_too_small` and writes nothing - * on any error (design R34). - * - * @tparam Format The format's type. - * @tparam Value The value's type. - * @tparam Rest The output or the token, if any. - * @param format The format. - * @param value The value. - * @param rest Nothing, a token of codecs', or the caller's output. - * @return The encoding, or the error. - */ -template -[[nodiscard]] auto encode(Format const& format, Value const& value, Rest&&... rest) noexcept( - detail::encode_is_noexcept()) { - if constexpr (!detail::pair_accepted()) { - detail::refuse_pair(); - return codecs::result{std::size_t{0}}; - } else if constexpr (detail::fixes_pem_label_v) { - return codecs::encode(Format{detail::pem_armor_of(format), format.inner}, value, - std::forward(rest)...); - } else { - return codecs::encode(format, value, std::forward(rest)...); - } -} + /** + * Whether the pair of `Format` has a reason of crypto's in `Direction`. + * + * @tparam Format The format's type. + * @tparam Direction The direction of the conversion. + */ + template + static constexpr bool has_reason = + crypto::detail::refused_for_reason_v; -/** - * Decodes `input` in `format` into a `Value`: checks the pair against crypto's list, as `encode` - * does, then decodes a PEM text in place (design R24), and anything else with - * `codecs::decode(format, input)` (design R18). - * - * `input` is a `std::span` (or any byte input) for a binary format and a - * `std::string_view` (or any text) for a PEM, as the value codec's output kind says (design R4). - * The PEM's armor is codecs': lines ended by a line feed or a carriage return and a line feed, the - * last ending optional, nothing before BEGIN or after END; its errors are codecs'; a payload longer - * than the inner format's decode bound is `crypto::error::invalid_encoding`. A PEM of crypto's pair - * is always decoded in place, never on codecs' heap path, and read with its inner format's label, - * whatever armor a hand-built `formats::pem_t` holds: another label is codecs' - * `invalid_prefix`. `noexcept` on every pair of crypto's but a key pair whose decode allocates from - * a memory resource (secp256k1's signing context); a pair crypto does not check is as - * `codecs::decode` is, which allocates for a heap result. - * - * @tparam Value The value's type. - * @tparam Format The format's type. - * @tparam Input The input's type. - * @param format The format. - * @param input The bytes, or the text. - * @return The value, or the error. - */ -template -[[nodiscard]] codecs::result decode(Format const& format, Input const& input) noexcept( - detail::decode_is_noexcept()) { - if constexpr (!detail::pair_accepted()) { - detail::refuse_pair(); - return error::invalid_encoding; - } else if constexpr (detail::decodes_in_place_v) { - return detail::decode_pem(format, input); - } else { - return codecs::decode(format, input); + /** + * Stops the build with crypto's sentence for the pair's reason in `Direction`. + * + * @tparam Format The format's type. + * @tparam Direction The direction of the conversion. + */ + template + static constexpr void refuse() noexcept { + crypto::detail::refuse_with_sentence(); } -} +}; -} // namespace webcpp::crypto +} // namespace webcpp::codecs namespace webcpp::crypto::detail { /** - * Encodes the signature in `format`: the member form of `crypto::encode`, which every signature + * Encodes the signature in `format`: the member form of `codecs::encode`, which every signature * type of crypto's declares (`detail/signature.hpp`). * * @tparam Format The format's type. * @param format The format. - * @return What `crypto::encode(format, *this)` returns. + * @return What `codecs::encode(format, *this)` returns. */ template template auto basic_signature::encode(Format const& format) const noexcept(detail::encode_is_noexcept()) { - return crypto::encode(format, *this); + return codecs::encode(format, *this); } /** - * Decodes a signature from `input` in `format`: the member form of `crypto::decode`. + * Decodes a signature from `input` in `format`: the member form of `codecs::decode`. * * @tparam Format The format's type. * @tparam Input The input's type. * @param format The format. * @param input The encoding. - * @return What `crypto::decode(format, input)` returns. + * @return What `codecs::decode(format, input)` returns. */ template template auto basic_signature::decode(Format const& format, Input const& input) noexcept( detail::decode_is_noexcept()) { - return crypto::decode(format, input); + return codecs::decode(format, input); } } // namespace webcpp::crypto::detail diff --git a/test/Jamfile b/test/Jamfile index 36c7beb..94237ae 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -223,26 +223,32 @@ crypto-run signing_namespaces : signing_namespaces_test.cpp crypto-run formats_vocabulary : formats_vocabulary_test.cpp ; # The verbs over the tests' toy type (support/formats_toy.hpp): every form # gives one encoding, nothing on the heap, strict decoding, secret results -# wiped when moved from, the two bounds and check_bounds, a moved-from key -# pair refused, a type crypto does not own passed to codecs untouched; the -# wipes through secure's seam (support/wipe_record.hpp). +# wiped when moved from, the two bounds and check_bounds, each pair's largest +# valid input decoded and one byte more input_too_long on every path, a +# moved-from key pair refused, crypto's declaration to codecs, the members as +# codecs' verbs in values, errors and noexcept, a type crypto does not own +# passed to codecs untouched and a format that is not crypto's refused on a +# crypto type; the wipes through secure's seam (support/wipe_record.hpp). crypto-run formats_verbs : formats_verbs_test.cpp : WEBCPP_SECURE_TEST_WIPE_RECORD ; -# The in-place PEM decode over the toy (design R24): the text OpenSSL writes, -# its CRLF form and one without the final line feed, no heap where codecs' -# decode of a std::string_view has one, the armor's refusals, a payload past -# the decode bound (an RSA key's) invalid_encoding, the DER buffer wiped after -# a pkcs8, the guard held for a private format alone, the same values and -# errors as codecs::decode. +# PEM over the toy through codecs' over, in place: the text OpenSSL writes, +# its CRLF form and one without the final line feed, no heap through +# codecs::decode or the member, the armor's refusals, a payload +# past the decode bound (an RSA key's) input_too_long as its DER is, codecs' +# intermediate wiped after a pkcs8 and none made past the bound, codecs' guard +# held for a private format alone, the same values and errors on every path, +# each pair's label in its format's type, another label invalid_prefix. crypto-run formats_pem : formats_pem_test.cpp : WEBCPP_SECURE_TEST_WIPE_RECORD ; # The room for JWK (design R17): a stand-in text format of the tests' own, -# added to a copy of the list, through crypto::encode and crypto::decode with -# no change to either. +# added to a copy of the list and to the list of a key of the tests' own, +# through codecs::encode and codecs::decode and crypto's declaration to codecs +# with no change to any of them; the toy key, whose list lacks it, refuses it. crypto-run formats_stand_in : formats_stand_in_test.cpp ; # The table of the valid pairs (design D4): every format of detail::all_formats -# with every point form over every type of the table, codecs' concepts and -# crypto's check agreeing, against doc/tables/crypto.json's formats section, +# with every point form over every type of the table, codecs' concepts, which +# crypto's declaration governs, and the compositions of each format, each PEM +# pair's exact capacity, against doc/tables/crypto.json's formats section, # read at run time from the directory WEBCPP_TEST_CRYPTO_TABLES gives (quoted, # with forward slashes, preopened on WASI), from which generate.py writes the # guide's table. @@ -457,7 +463,8 @@ crypto-run secp256k1_wycheproof : secp256k1_wycheproof_test.cpp crypto-run secp256k1_ecdh : secp256k1_ecdh_test.cpp : /third_party//secp256k1 /third_party//wycheproof /webcpp//boost_json ; -# The parsed public key in both SEC1 forms, and its refusals. +# The parsed public key in both SEC1 forms, and its refusals (input_too_long +# past raw's decode bound). crypto-run secp256k1_keys : secp256k1_keys_test.cpp : /third_party//secp256k1 ; # Recoverable ECDSA signatures, libsecp256k1's edge cases, and hedged ECDSA. crypto-run secp256k1_recovery : secp256k1_recovery_test.cpp : /third_party//secp256k1 ; @@ -479,9 +486,11 @@ crypto-run secp256k1_tweak : secp256k1_tweak_test.cpp : /third_party//s # uncompressed by default, decoding a stored compressed one; the x-only key's # raw; the key pair's raw and pkcs8; the signatures' raw and the ECDSA one's # DER), the generator's key, 64 seeded key pairs, a Nostr key, the strict -# decoders' refusals, Wycheproof's ECDH SPKIs and ECDSA keys, PEM through -# codecs' armor; the key pair's decode with a memory resource into an arena; -# a throwing resource's unwinding through pem(pkcs8), its buffers wiped. +# decoders' refusals, input_too_long past each pair's bound and its largest +# valid input decoded, Wycheproof's ECDH SPKIs and ECDSA keys, PEM through +# codecs' armor; the key pair's decode with a memory resource into an arena, +# its refusals at the bounds as codecs::decode's; a throwing resource's +# unwinding through pem(pkcs8), its buffers wiped. crypto-run secp256k1_formats : secp256k1_formats_test.cpp : /third_party//secp256k1 /third_party//wycheproof /webcpp//boost_json WEBCPP_SECURE_TEST_WIPE_RECORD ; @@ -653,7 +662,8 @@ crypto-run p256_keys_bearssl : p256_keys_bearssl_test.cpp # and its refusals, low S checked and normalized, DER both ways and its strict # decoder. crypto-run p256_ecdsa_bearssl : p256_ecdsa_bearssl_test.cpp : /third_party//bearssl ; -# Wycheproof's P-256 SHA-256 ECDSA vectors, raw (IEEE P1363) and DER. +# Wycheproof's P-256 SHA-256 ECDSA vectors, raw (IEEE P1363) and DER (a DER +# past 72 bytes input_too_long). crypto-run p256_wycheproof_bearssl : p256_wycheproof_bearssl_test.cpp : /third_party//bearssl /third_party//wycheproof /webcpp//boost_json ; @@ -662,10 +672,11 @@ crypto-run p256_wycheproof_bearssl : p256_wycheproof_bearssl_test.cpp crypto-run p256_ecdh_bearssl : p256_ecdh_bearssl_test.cpp : /third_party//bearssl /third_party//wycheproof /webcpp//boost_json ; -# P-256's formats on BearSSL: every pair of design D4 in every form, the -# compressed point's square root, BearSSL's own PKCS#8 encoder as an oracle of -# the layout, the strict decoders' refusals, Wycheproof's compressed points, -# ECDH SPKIs and PEMs and ECDSA DER, PEM through codecs' armor. +# P-256's formats on BearSSL: every pair of design D4 in every form, each +# pair's largest valid input and one byte more input_too_long, the compressed +# point's square root, BearSSL's own PKCS#8 encoder as an oracle of the layout, +# the strict decoders' refusals, Wycheproof's compressed points, ECDH SPKIs and +# PEMs and ECDSA DER, PEM through codecs' armor, an RSA key's input_too_long. crypto-run p256_formats_bearssl : p256_formats_bearssl_test.cpp : /third_party//bearssl /third_party//wycheproof /webcpp//boost_json WEBCPP_SECURE_TEST_WIPE_RECORD ; @@ -784,11 +795,13 @@ crypto-run oracle_25519_corrupted : oracle_25519_test.cpp # RFC 8410's SPKI and PKCS#8 for Ed25519 and X25519, and PEM, on libsodium # (design D30): RFC 8410's own keys, round trips of random keys, every -# malformed input refused with invalid_encoding and nothing written. OpenSSL's +# malformed input refused with invalid_encoding, or input_too_long past the +# pair's decode bound, and nothing written. OpenSSL's # i2d_PUBKEY, i2d_PrivateKey and PEM writers as an outside oracle on native, # wasip2 and wasip3 (linked in the test alone), and node's WebCrypto importKey # and exportKey on Emscripten (the JSPI link, for EM_ASYNC_JS): 200 seeded -# cases per curve each. +# cases per curve each. An input past a pair's decode bound is input_too_long, +# each pair's largest valid input decodes. crypto-run formats_ed25519_sodium : formats_ed25519_sodium_test.cpp : /third_party//sodium /third_party//wycheproof /webcpp//boost_json WEBCPP_SECURE_TEST_WIPE_RECORD ; diff --git a/test/allocation_bearssl_test.cpp b/test/allocation_bearssl_test.cpp index ad78ba7..66d8daa 100644 --- a/test/allocation_bearssl_test.cpp +++ b/test/allocation_bearssl_test.cpp @@ -44,6 +44,7 @@ namespace { // digest), ECDH's agree and the formats' eight. void p256_calls() { namespace crypto = webcpp::crypto; + namespace codecs = webcpp::codecs; namespace p256 = crypto::curves::p256; namespace data = webcpp::test::p256; using keypair = p256::basic_keypair; @@ -77,10 +78,10 @@ void p256_calls() { namespace formats = crypto::formats; std::array> der{}; - auto const written = crypto::encode(formats::der, *signature, std::span(der)); + auto const written = codecs::encode(formats::der, *signature, std::span(der)); if (BOOST_TEST(written.has_value())) { BOOST_TEST( - crypto::decode(formats::der, std::span(der).first(*written)) + codecs::decode(formats::der, std::span(der).first(*written)) .has_value()); } BOOST_TEST(signature->low_s().has_value()); @@ -89,13 +90,13 @@ void p256_calls() { std::array, public_key>> spki{}; std::array> pkcs8{}; std::array compressed{}; - BOOST_TEST(crypto::encode(formats::spki, *key, std::span(spki)).has_value()); - BOOST_TEST(crypto::decode(formats::spki, spki).has_value()); + BOOST_TEST(codecs::encode(formats::spki, *key, std::span(spki)).has_value()); + BOOST_TEST(codecs::decode(formats::spki, spki).has_value()); BOOST_TEST( - crypto::encode(formats::raw(formats::compressed), *key, std::span(compressed)).has_value()); - BOOST_TEST(crypto::decode(formats::raw, compressed).has_value()); - BOOST_TEST(crypto::encode(formats::pkcs8, *pair, std::span(pkcs8)).has_value()); - BOOST_TEST(crypto::decode(formats::pkcs8, pkcs8).has_value()); + codecs::encode(formats::raw(formats::compressed), *key, std::span(compressed)).has_value()); + BOOST_TEST(codecs::decode(formats::raw, compressed).has_value()); + BOOST_TEST(codecs::encode(formats::pkcs8, *pair, std::span(pkcs8)).has_value()); + BOOST_TEST(codecs::decode(formats::pkcs8, pkcs8).has_value()); } } // namespace diff --git a/test/allocation_sodium_test.cpp b/test/allocation_sodium_test.cpp index 62b8ddc..fb39682 100644 --- a/test/allocation_sodium_test.cpp +++ b/test/allocation_sodium_test.cpp @@ -9,7 +9,7 @@ // verification under canonical, Ed25519ph and the conversion to X25519; X25519's public key and // agreement, a refused one included; crypto_kx's key pairs, from a seed and from an X25519 key // pair, and session keys, a refused peer included; RFC 8410's SPKI and PKCS#8 of both curves and -// their PEM, both ways, both key pairs' raw too, through crypto::encode and crypto::decode; +// their PEM, both ways, both key pairs' raw too, through codecs::encode and codecs::decode; // the two boxes' from_keys, seal and open, a refused peer key and a failed open included; and the // sealed boxes' seal and open, a failed open included, make no call of operator new, nor of the C // library's allocator where the target lets a program count it; a control proves the counter moves. @@ -45,6 +45,7 @@ namespace { namespace crypto = webcpp::crypto; +namespace codecs = webcpp::codecs; /** Makes an AEAD, seals, opens, and opens a forged message: none of it allocates. */ template @@ -277,7 +278,7 @@ void expect_no_allocation_sealed_box() { } /** - * Encodes and decodes, through crypto::encode and crypto::decode, an Ed25519 key pair's raw, + * Encodes and decodes, through codecs::encode and codecs::decode, an Ed25519 key pair's raw, * PKCS#8 and PEM and its public key's SPKI and PEM: none of it allocates. */ void expect_no_allocation_formats() { @@ -290,32 +291,32 @@ void expect_no_allocation_formats() { using keypair = ed25519::basic_keypair; auto const pair = keypair::from_secret(key); BOOST_TEST(pair.has_value()); - auto const ed_pkcs8 = crypto::encode(formats::pkcs8, *pair); + auto const ed_pkcs8 = codecs::encode(formats::pkcs8, *pair); BOOST_TEST(ed_pkcs8.has_value() && - crypto::decode(formats::pkcs8, ed_pkcs8->view()).has_value()); - auto const ed_raw = crypto::encode(formats::raw, *pair); + codecs::decode(formats::pkcs8, ed_pkcs8->view()).has_value()); + auto const ed_raw = codecs::encode(formats::raw, *pair); BOOST_TEST(ed_raw.has_value() && - crypto::decode(formats::raw, ed_raw->view()).has_value()); - auto const ed_text = crypto::encode(formats::pem(formats::pkcs8), *pair); + codecs::decode(formats::raw, ed_raw->view()).has_value()); + auto const ed_text = codecs::encode(formats::pem(formats::pkcs8), *pair); BOOST_TEST( ed_text.has_value() && - crypto::decode(formats::pem(formats::pkcs8), ed_text->view()).has_value()); + codecs::decode(formats::pem(formats::pkcs8), ed_text->view()).has_value()); auto const public_key = pair->public_key(); BOOST_TEST(public_key.has_value()); - auto const ed_spki = crypto::encode(formats::spki, *public_key); + auto const ed_spki = codecs::encode(formats::spki, *public_key); BOOST_TEST(ed_spki.has_value() && - crypto::decode(formats::spki, ed_spki->view()).has_value()); - auto const ed_public_text = crypto::encode(formats::pem(formats::spki), *public_key); + codecs::decode(formats::spki, ed_spki->view()).has_value()); + auto const ed_public_text = codecs::encode(formats::pem(formats::spki), *public_key); BOOST_TEST( ed_public_text.has_value() && - crypto::decode(formats::pem(formats::spki), ed_public_text->view()) + codecs::decode(formats::pem(formats::spki), ed_public_text->view()) .has_value()); } BOOST_TEST_EQ(webcpp::test::allocations(), before); } /** - * Encodes and decodes, through crypto::encode and crypto::decode, an X25519 key pair's raw, PKCS#8 + * Encodes and decodes, through codecs::encode and codecs::decode, an X25519 key pair's raw, PKCS#8 * and PEM and its public key's SPKI and PEM; refuses its PKCS#8 as Ed25519's: none of it * allocates. */ @@ -330,27 +331,27 @@ void expect_no_allocation_x25519_formats() { using x_keypair = x25519::basic_keypair; auto const x_pair = x_keypair::from_secret(key); BOOST_TEST(x_pair.has_value()); - auto const pkcs8 = crypto::encode(formats::pkcs8, *x_pair); + auto const pkcs8 = codecs::encode(formats::pkcs8, *x_pair); BOOST_TEST(pkcs8.has_value() && - crypto::decode(formats::pkcs8, pkcs8->view()).has_value()); - BOOST_TEST(crypto::decode(formats::pkcs8, pkcs8->view()).error() == + codecs::decode(formats::pkcs8, pkcs8->view()).has_value()); + BOOST_TEST(codecs::decode(formats::pkcs8, pkcs8->view()).error() == crypto::error::invalid_encoding); - auto const x_raw = crypto::encode(formats::raw, *x_pair); + auto const x_raw = codecs::encode(formats::raw, *x_pair); BOOST_TEST(x_raw.has_value() && - crypto::decode(formats::raw, x_raw->view()).has_value()); - auto const x_text = crypto::encode(formats::pem(formats::pkcs8), *x_pair); + codecs::decode(formats::raw, x_raw->view()).has_value()); + auto const x_text = codecs::encode(formats::pem(formats::pkcs8), *x_pair); BOOST_TEST( x_text.has_value() && - crypto::decode(formats::pem(formats::pkcs8), x_text->view()).has_value()); + codecs::decode(formats::pem(formats::pkcs8), x_text->view()).has_value()); auto const x_public = x_pair->public_key(); BOOST_TEST(x_public.has_value()); - auto const spki = crypto::encode(formats::spki, *x_public); + auto const spki = codecs::encode(formats::spki, *x_public); BOOST_TEST(spki.has_value() && - crypto::decode(formats::spki, spki->view()).has_value()); - auto const x_public_text = crypto::encode(formats::pem(formats::spki), *x_public); + codecs::decode(formats::spki, spki->view()).has_value()); + auto const x_public_text = codecs::encode(formats::pem(formats::spki), *x_public); BOOST_TEST( x_public_text.has_value() && - crypto::decode(formats::pem(formats::spki), x_public_text->view()) + codecs::decode(formats::pem(formats::spki), x_public_text->view()) .has_value()); } BOOST_TEST_EQ(webcpp::test::allocations(), before); diff --git a/test/constant_time/paths.hpp b/test/constant_time/paths.hpp index 43768d3..44b7b12 100644 --- a/test/constant_time/paths.hpp +++ b/test/constant_time/paths.hpp @@ -297,9 +297,9 @@ int private_formats(Keypair const& pair, std::size_t pem_secret_at, std::string_ auto const named = [&](std::string_view step) { return std::string(curve) + "'s " + std::string(step) + std::string(where); }; - auto const pkcs8 = crypto::encode(formats::pkcs8, pair); - auto const raw = crypto::encode(formats::raw, pair); - auto const text = crypto::encode(formats::pem(formats::pkcs8), pair); + auto const pkcs8 = codecs::encode(formats::pkcs8, pair); + auto const raw = codecs::encode(formats::raw, pair); + auto const text = codecs::encode(formats::pem(formats::pkcs8), pair); int failures = check(pkcs8.has_value() && carries_mark(pkcs8->view()), named("pkcs8 encode").c_str()) + @@ -318,14 +318,14 @@ int private_formats(Keypair const& pair, std::size_t pem_secret_at, std::string_ if (!decoded) { return false; } - auto const encoded_again = crypto::encode(formats::pkcs8, *decoded); + auto const encoded_again = codecs::encode(formats::pkcs8, *decoded); return encoded_again.has_value() && carries_mark(encoded_again->view()); }; - failures += check(traced_again(crypto::decode(formats::pkcs8, pkcs8->view())), + failures += check(traced_again(codecs::decode(formats::pkcs8, pkcs8->view())), named("pkcs8 decode").c_str()); - failures += check(traced_again(crypto::decode(formats::raw, raw->view())), + failures += check(traced_again(codecs::decode(formats::raw, raw->view())), named("raw decode").c_str()); - auto const from_text = crypto::decode(formats::pem(formats::pkcs8), text->view()); + auto const from_text = codecs::decode(formats::pem(formats::pkcs8), text->view()); failures += check(from_text.has_value() && (!Checker::exact_subtraction || traced_again(from_text)), named("pem(pkcs8) decode").c_str()); diff --git a/test/crypto_secp256k1_test.cpp b/test/crypto_secp256k1_test.cpp index 9875ff3..b922362 100644 --- a/test/crypto_secp256k1_test.cpp +++ b/test/crypto_secp256k1_test.cpp @@ -37,11 +37,11 @@ static_assert(webcpp::crypto::can_verify); static_assert(std::is_function_v)>); static_assert(std::is_function_v)>); -static_assert(std::is_same_v( +static_assert(std::is_same_v( webcpp::crypto::formats::spki, std::declval>())), webcpp::codecs::result>); -static_assert(std::is_same_v( +static_assert(std::is_same_v( webcpp::crypto::formats::pkcs8, std::declval>())), webcpp::codecs::result>); diff --git a/test/formats_25519_openssl_oracle_test.cpp b/test/formats_25519_openssl_oracle_test.cpp index 6077252..aa4ee54 100644 --- a/test/formats_25519_openssl_oracle_test.cpp +++ b/test/formats_25519_openssl_oracle_test.cpp @@ -6,10 +6,10 @@ // OpenSSL as an outside oracle of RFC 8410's SPKI and PKCS#8 and of PEM on libsodium (design // D30): for 200 seeded key pairs per curve, Ed25519 and X25519, OpenSSL's i2d_PUBKEY and -// i2d_PrivateKey give the bytes that crypto::encode gives in formats::spki and formats::pkcs8, its +// i2d_PrivateKey give the bytes that codecs::encode gives in formats::spki and formats::pkcs8, its // PEM writers PEM_write_bio_PUBKEY and PEM_write_bio_PrivateKey give the text of formats::pem, and // its d2i_PUBKEY, d2i_AutoPrivateKey and PEM readers decode ours to the same raw keys; and -// crypto::decode reads OpenSSL's DER and PEM to the same keys. OpenSSL, the source-built provider, +// codecs::decode reads OpenSSL's DER and PEM to the same keys. OpenSSL, the source-built provider, // is linked in this test alone: the formats run on libsodium. #include @@ -44,6 +44,7 @@ #include namespace crypto = webcpp::crypto; +namespace codecs = webcpp::codecs; namespace formats = webcpp::crypto::formats; namespace ed25519 = webcpp::crypto::curves::ed25519; namespace x25519 = webcpp::crypto::curves::x25519; @@ -195,7 +196,7 @@ bool agree(int type, std::span secret, our_encodings con } /** - * crypto::decode reads what OpenSSL writes of the key pair of `seed`, of type `type` + * codecs::decode reads what OpenSSL writes of the key pair of `seed`, of type `type` * (EVP_PKEY_ED25519 or EVP_PKEY_X25519): its i2d_PUBKEY and i2d_PrivateKey, and its PEM writers' * texts, each to the key pair's public key and seed, Ed25519's, or secret key, X25519's. */ @@ -220,15 +221,15 @@ bool reads_openssl(int type, std::span seed, auto const pkcs8 = der_by(key.get(), i2d_PrivateKey); auto const public_text = pem_by(key.get(), PEM_write_bio_PUBKEY); auto const private_text = pem_by(key.get(), write_private); - auto const from_spki = crypto::decode(formats::spki, spki); + auto const from_spki = codecs::decode(formats::spki, spki); auto const from_public_text = - crypto::decode(formats::pem(formats::spki), public_text); + codecs::decode(formats::pem(formats::spki), public_text); bool const read_public = BOOST_TEST(from_spki.has_value() && *from_spki == public_key && from_public_text.has_value() && *from_public_text == public_key); bool const read_private = - BOOST_TEST(same_pair(crypto::decode(formats::pkcs8, pkcs8))) && - BOOST_TEST(same_pair(crypto::decode(formats::pem(formats::pkcs8), private_text))); + BOOST_TEST(same_pair(codecs::decode(formats::pkcs8, pkcs8))) && + BOOST_TEST(same_pair(codecs::decode(formats::pem(formats::pkcs8), private_text))); return read_public && read_private; } @@ -243,10 +244,10 @@ void ed25519_against_openssl(std::mt19937_64& generator) { test::to_hex(seed_bytes).c_str())); continue; } - auto const spki = crypto::encode(formats::spki, *public_key); - auto const pkcs8 = crypto::encode(formats::pkcs8, *pair); - auto const public_text = crypto::encode(formats::pem(formats::spki), *public_key); - auto const private_text = crypto::encode(formats::pem(formats::pkcs8), *pair); + auto const spki = codecs::encode(formats::spki, *public_key); + auto const pkcs8 = codecs::encode(formats::pkcs8, *pair); + auto const public_text = codecs::encode(formats::pem(formats::spki), *public_key); + auto const private_text = codecs::encode(formats::pem(formats::pkcs8), *pair); if (!BOOST_TEST(spki && pkcs8 && public_text && private_text)) { continue; } @@ -275,10 +276,10 @@ void x25519_against_openssl(std::mt19937_64& generator) { test::to_hex(secret_key).c_str())); continue; } - auto const spki = crypto::encode(formats::spki, *public_key); - auto const pkcs8 = crypto::encode(formats::pkcs8, *key_pair); - auto const public_text = crypto::encode(formats::pem(formats::spki), *public_key); - auto const private_text = crypto::encode(formats::pem(formats::pkcs8), *key_pair); + auto const spki = codecs::encode(formats::spki, *public_key); + auto const pkcs8 = codecs::encode(formats::pkcs8, *key_pair); + auto const public_text = codecs::encode(formats::pem(formats::spki), *public_key); + auto const private_text = codecs::encode(formats::pem(formats::pkcs8), *key_pair); if (!BOOST_TEST(spki && pkcs8 && public_text && private_text)) { continue; } diff --git a/test/formats_25519_webcrypto_oracle_test.cpp b/test/formats_25519_webcrypto_oracle_test.cpp index 20ea2f2..1dcea88 100644 --- a/test/formats_25519_webcrypto_oracle_test.cpp +++ b/test/formats_25519_webcrypto_oracle_test.cpp @@ -6,9 +6,9 @@ // WebCrypto, node's `globalThis.crypto.subtle`, as an outside oracle of RFC 8410's SPKI and // PKCS#8 on libsodium (design D30), on Emscripten: for 200 seeded keys per curve, importKey of -// ours with "spki" and "pkcs8", through crypto::encode, and exportKey again give our bytes, an +// ours with "spki" and "pkcs8", through codecs::encode, and exportKey again give our bytes, an // Ed25519 key signs as ours does and an X25519 key agrees as ours does; and for 200 key pairs per -// curve that WebCrypto generates, its "spki", "pkcs8" and "raw" decode through crypto::decode to +// curve that WebCrypto generates, its "spki", "pkcs8" and "raw" decode through codecs::decode to // a key pair whose public key is the SPKI's and the raw key's and that signs as WebCrypto's does // (Ed25519) or agrees with a peer as WebCrypto's deriveBits does, both ways (X25519), and encode // again to the same bytes. Each WebCrypto call is an EM_ASYNC_JS function, which suspends the @@ -39,6 +39,7 @@ #include namespace crypto = webcpp::crypto; +namespace codecs = webcpp::codecs; namespace formats = webcpp::crypto::formats; namespace ed25519 = webcpp::crypto::curves::ed25519; namespace x25519 = webcpp::crypto::curves::x25519; @@ -175,8 +176,8 @@ void ed25519_through_webcrypto(std::mt19937_64& generator) { test::to_hex(seed_bytes).c_str())); continue; } - auto const pkcs8 = crypto::encode(formats::pkcs8, *pair); - auto const spki = crypto::encode(formats::spki, *public_key); + auto const pkcs8 = codecs::encode(formats::pkcs8, *pair); + auto const spki = codecs::encode(formats::spki, *public_key); if (!BOOST_TEST(pkcs8.has_value() && spki.has_value())) { continue; } @@ -205,9 +206,9 @@ void ed25519_case_from_webcrypto(std::span out) { auto const pkcs8 = out.first(48); auto const spki = out.subspan(48, 44); auto const raw = out.subspan(92, 32); - auto const pair = crypto::decode(formats::pkcs8, pkcs8); - auto const public_key = crypto::decode(formats::spki, spki); - auto const raw_key = crypto::decode(formats::raw, raw); + auto const pair = codecs::decode(formats::pkcs8, pkcs8); + auto const public_key = codecs::decode(formats::spki, spki); + auto const raw_key = codecs::decode(formats::raw, raw); if (!BOOST_TEST(pair.has_value() && public_key.has_value() && raw_key.has_value())) { return; } @@ -219,9 +220,9 @@ void ed25519_case_from_webcrypto(std::span out) { BOOST_TEST(*its_public == *public_key); BOOST_TEST(*raw_key == *public_key); BOOST_TEST(std::ranges::equal(signature->bytes(), out.subspan(124, 64))); - auto const pkcs8_again = crypto::encode(formats::pkcs8, *pair); - auto const spki_again = crypto::encode(formats::spki, *public_key); - auto const raw_again = crypto::encode(formats::raw, *raw_key); + auto const pkcs8_again = codecs::encode(formats::pkcs8, *pair); + auto const spki_again = codecs::encode(formats::spki, *public_key); + auto const raw_again = codecs::encode(formats::raw, *raw_key); BOOST_TEST(pkcs8_again.has_value() && std::ranges::equal(pkcs8_again->view(), pkcs8)); BOOST_TEST(spki_again.has_value() && std::ranges::equal(spki_again->view(), spki)); BOOST_TEST(raw_again.has_value() && std::ranges::equal(raw_again->view(), raw)); @@ -256,9 +257,9 @@ void x25519_case_through_webcrypto(std::array const& secret_ke if (!BOOST_TEST(public_key.has_value() && peer_public.has_value())) { return; } - auto const pkcs8 = crypto::encode(formats::pkcs8, *key_pair); - auto const spki = crypto::encode(formats::spki, *public_key); - auto const peer_spki = crypto::encode(formats::spki, *peer_public); + auto const pkcs8 = codecs::encode(formats::pkcs8, *key_pair); + auto const spki = codecs::encode(formats::spki, *public_key); + auto const peer_spki = codecs::encode(formats::spki, *peer_public); if (!BOOST_TEST(pkcs8.has_value() && spki.has_value() && peer_spki.has_value())) { return; } @@ -299,9 +300,9 @@ void x25519_case_from_webcrypto(std::span out, auto const spki = out.subspan(48, 44); auto const raw = out.subspan(92, 32); auto const derived = out.subspan(124, 32); - auto const pair = crypto::decode(formats::pkcs8, pkcs8); - auto const public_key = crypto::decode(formats::spki, spki); - auto const raw_key = crypto::decode(formats::raw, raw); + auto const pair = codecs::decode(formats::pkcs8, pkcs8); + auto const public_key = codecs::decode(formats::spki, spki); + auto const raw_key = codecs::decode(formats::raw, raw); auto const peer_public = peer.public_key(); if (!BOOST_TEST(pair.has_value() && public_key.has_value() && raw_key.has_value() && peer_public.has_value())) { @@ -314,9 +315,9 @@ void x25519_case_from_webcrypto(std::span out, auto const peer_agreed = peer.agree(*public_key); BOOST_TEST(agreed.has_value() && std::ranges::equal(agreed->bytes(), derived)); BOOST_TEST(peer_agreed.has_value() && std::ranges::equal(peer_agreed->bytes(), derived)); - auto const pkcs8_again = crypto::encode(formats::pkcs8, *pair); - auto const spki_again = crypto::encode(formats::spki, *public_key); - auto const raw_again = crypto::encode(formats::raw, *raw_key); + auto const pkcs8_again = codecs::encode(formats::pkcs8, *pair); + auto const spki_again = codecs::encode(formats::spki, *public_key); + auto const raw_again = codecs::encode(formats::raw, *raw_key); BOOST_TEST(pkcs8_again.has_value() && std::ranges::equal(pkcs8_again->view(), pkcs8)); BOOST_TEST(spki_again.has_value() && std::ranges::equal(spki_again->view(), spki)); BOOST_TEST(raw_again.has_value() && std::ranges::equal(raw_again->view(), raw)); diff --git a/test/formats_asynchronous_test.cpp b/test/formats_asynchronous_test.cpp index 733b1d3..b26f60d 100644 --- a/test/formats_asynchronous_test.cpp +++ b/test/formats_asynchronous_test.cpp @@ -8,7 +8,8 @@ // whose key pair takes an asynchronous backend, Ed25519, X25519 and P-256 (secp256k1's key pair has // none, ruling F26), a key pair of the stand-in backend (support/asynchronous_stand_in.hpp) has // no raw, no pkcs8 and no pem(pkcs8) value codec, codecs' own concepts saying so, and crypto's -// check refuses each with the reason that names WebCrypto's exportKey; its public key keeps raw, +// declaration to codecs refuses each, in both directions, with the reason that names WebCrypto's +// exportKey; its public key keeps raw, // spki and pem(spki), as on a synchronous backend. The messages are the message tests // pkcs8_on_asynchronous_ed25519, raw_on_asynchronous_x25519 and pkcs8_on_asynchronous_p256. // Everything here is checked at compile time: no key pair of the stand-in is made. @@ -37,10 +38,12 @@ using pem_spki_t = formats::pem_t>; /** Whether crypto refuses `Format` on `Value` for the reason D7 names, encoding and decoding. */ template constexpr bool refused_as_private_on_asynchronous() noexcept { + using declaration = codecs::value_formats; return detail::refusal_reason_v == detail::refusal_reason::private_on_asynchronous && - !detail::pair_accepted() && - !detail::pair_accepted(); + declaration::template has_reason && + declaration::template has_reason && + !codecs::encodable && !codecs::decodable; } /** @@ -66,10 +69,7 @@ constexpr bool private_formats_refused() noexcept { static_assert(codecs::encodable>); static_assert(codecs::encodable); static_assert(codecs::decodable>); - static_assert(detail::pair_accepted, public_key>()); - static_assert(detail::pair_accepted, public_key>()); - static_assert(detail::pair_accepted()); - static_assert(detail::pair_accepted()); + static_assert(codecs::decodable); return true; } diff --git a/test/formats_before_after_test.cpp b/test/formats_before_after_test.cpp index 3d14a65..4177a90 100644 --- a/test/formats_before_after_test.cpp +++ b/test/formats_before_after_test.cpp @@ -6,8 +6,9 @@ // The guide's "Before and after" (design "Before and after"): the calls that replaced each // curve's format functions, crypto::pem, the scheme tags in crypto:: and the free low-S -// functions, on real keys, each result checked. The region `after` is what the guide shows; the -// two calls it names as refused are the message tests der_on_ed25519 and verify_secp256k1_any_s. +// functions, on real keys, each result checked; a value is encoded and decoded by codecs' verbs +// (one-verb's D1). The region `after` is what the guide shows; the two calls it names as refused +// are the message tests der_on_ed25519 and verify_secp256k1_any_s. #include #include @@ -104,7 +105,7 @@ void before_and_after() { auto const event_id = filled(0x44); auto const event_signature = writer.sign(schemes::schnorr, event_id).value(); auto const writer_key = - crypto::encode(formats::raw(formats::xonly), writer.public_key().value()); + codecs::encode(formats::raw(formats::xonly), writer.public_key().value()); std::span const bytes = writer_key.value().view(); auto const internal_key = writer.xonly_public_key().value(); auto const tap_tweak = filled(0x55); @@ -112,40 +113,40 @@ void before_and_after() { // tag::after[] // A P-256 public key to PEM. - auto const text = crypto::encode(formats::pem(formats::spki), public_key); // text->view() + auto const text = codecs::encode(formats::pem(formats::spki), public_key); // text->view() auto const same_text = public_key.encode(formats::pem(formats::spki)); // A key pair from PKCS#8 PEM. - auto const read = crypto::decode(formats::pem(formats::pkcs8), pem_text); + auto const read = codecs::decode(formats::pem(formats::pkcs8), pem_text); auto const same_read = p256::keypair::decode(formats::pem(formats::pkcs8), pem_text); // Sign, then DER; verify a WebCrypto peer's signature, whatever its S, then normalize it. auto const signature = key.sign(schemes::ecdsa_sha256, message); - auto const der = crypto::encode(formats::der, *signature); + auto const der = codecs::encode(formats::der, *signature); auto const ok = crypto::verify(schemes::ecdsa_sha256, policies::any_s, peer_signature, message, peer_key); auto const low = peer_signature.low_s(); // Refused at compile time, each with crypto's sentence: an Ed25519 signature has no DER - // (`crypto::encode(formats::der, ed25519_signature)`), and secp256k1's ECDSA takes + // (`codecs::encode(formats::der, ed25519_signature)`), and secp256k1's ECDSA takes // low_s_only alone (`crypto::verify(schemes::ecdsa_sha256, policies::any_s, ...)`). // A Nostr key in 32 bytes, its Schnorr signature, a Taproot tweak. auto const nostr_key = - crypto::decode(formats::raw(formats::xonly), bytes); + codecs::decode(formats::raw(formats::xonly), bytes); auto const valid = nostr_key->verify(schemes::schnorr, policies::canonical, event_signature, event_id); auto const output_key = internal_key.tweak_add(tap_tweak); // X25519 and the point forms, new. - auto const x = crypto::encode(formats::spki, *x25519_key.public_key()); - auto const small = crypto::encode(formats::raw(formats::compressed), p256_public_key); + auto const x = codecs::encode(formats::spki, *x25519_key.public_key()); + auto const small = codecs::encode(formats::raw(formats::compressed), p256_public_key); // A caller that manages memory, and a token of codecs'. std::array, p256::public_key>> out{}; - auto const written = crypto::encode(formats::spki, public_key, std::span(out)); + auto const written = codecs::encode(formats::spki, public_key, std::span(out)); std::string pem_string; - auto const appended = crypto::encode(formats::pem(formats::spki), public_key, + auto const appended = codecs::encode(formats::pem(formats::spki), public_key, codecs::tokens::append_to(pem_string)); // end::after[] diff --git a/test/formats_ed25519_sodium_test.cpp b/test/formats_ed25519_sodium_test.cpp index ec0dd73..8c6afe5 100644 --- a/test/formats_ed25519_sodium_test.cpp +++ b/test/formats_ed25519_sodium_test.cpp @@ -5,18 +5,22 @@ // https://www.boost.org/LICENSE_1_0.txt) // Ed25519's formats on libsodium (design D4, D11, D30): RFC 8410's own key pair (sections 10.1 and -// 10.3), its private PEM decoded through crypto::decode, whose public key encodes to the RFC's +// 10.3), its private PEM decoded through codecs::decode, whose public key encodes to the RFC's // public PEM, both encoded again to the same text; both bounds of every pair; every form of every -// pair (support/format_checks.hpp: crypto::encode, codecs::encode, the members, the span form, +// pair (support/format_checks.hpp: codecs::encode, the members, the span form, // codecs' token, a short span refused untouched), no heap, strictness, the secret results; a key // pair's raw, its secret() and from_secret; Wycheproof's EdDSA public keys through raw, spki and // pem(spki); round trips of random key pairs; a PEM with CRLF lines and without its final line // feed decoded, and one with text before BEGIN refused (Review Focus 1); a moved-from key pair -// refused by every private format with nothing written (Review Focus 3); and every malformed DER -// refused with invalid_encoding: the other curve's OID, a trailing byte, a length in long form, -// PKCS#8 version 2 with the public key (30 51, 83 bytes, and RFC 8410's with an attribute), each -// byte of a prefix changed. The armor's own faults are codecs' (formats_pem_test pins crypto's -// pem over it); X25519's formats are formats_x25519_sodium_test's. +// refused by every private format with nothing written (Review Focus 3); each pair's largest +// valid input, exactly its decode bound, decoded on every path and one byte more codecs' +// input_too_long (one-verb's R9); RFC 8410's private PEM through codecs::decode with no heap, its +// 48-byte intermediate wiped; and every malformed DER refused: the other curve's OID, a trailing +// byte, a length in long form, each byte of a prefix changed with invalid_encoding, PKCS#8 version +// 2 with the public key (30 51, 83 bytes, and RFC 8410's of 116 with an attribute), past the +// PKCS#8's decode bound, with codecs' input_too_long, directly and through PEM. The armor's own +// faults are codecs' (formats_pem_test pins crypto's pem over it); X25519's formats are +// formats_x25519_sodium_test's. #include #include @@ -103,7 +107,7 @@ std::vector der_of(std::string_view label, std::string_view text) */ void rfc8410_keys() { auto const key_pair = - crypto::decode(formats::pem(formats::pkcs8), rfc8410_private); + codecs::decode(formats::pem(formats::pkcs8), rfc8410_private); if (!BOOST_TEST(key_pair.has_value())) { return; } @@ -114,36 +118,41 @@ void rfc8410_keys() { return; } BOOST_TEST(bytes_of(public_key->bytes()) == test::from_hex(rfc8410_public_key)); - auto const public_pem = crypto::encode(formats::pem(formats::spki), *public_key); + auto const public_pem = codecs::encode(formats::pem(formats::spki), *public_key); BOOST_TEST(public_pem.has_value() && public_pem->view() == rfc8410_public); - auto const private_pem = crypto::encode(formats::pem(formats::pkcs8), *key_pair); + auto const private_pem = codecs::encode(formats::pem(formats::pkcs8), *key_pair); BOOST_TEST(private_pem.has_value() && private_pem->view() == rfc8410_private); // The public PEM decodes to the same key, and the DER of each encodes to the RFC's. auto const decoded_public = - crypto::decode(formats::pem(formats::spki), rfc8410_public); + codecs::decode(formats::pem(formats::spki), rfc8410_public); BOOST_TEST(decoded_public.has_value() && *decoded_public == *public_key); - auto const spki = crypto::encode(formats::spki, *public_key); + auto const spki = codecs::encode(formats::spki, *public_key); BOOST_TEST(spki.has_value() && bytes_of(spki->view()) == der_of("PUBLIC KEY", rfc8410_public)); - auto const pkcs8 = crypto::encode(formats::pkcs8, *key_pair); + auto const pkcs8 = codecs::encode(formats::pkcs8, *key_pair); BOOST_TEST(pkcs8.has_value() && bytes_of(pkcs8->view()) == der_of("PRIVATE KEY", rfc8410_private)); - // Section 10.3's version 2, with an attribute and the public key: 114 bytes of DER, past - // the decode bound in PEM, and refused as DER. + // Section 10.3's version 2, with an attribute and the public key: 116 bytes of DER, past + // the decode bound, codecs' input_too_long in PEM, through the verb and the member, and as DER. auto const version2 = - crypto::decode(formats::pem(formats::pkcs8), rfc8410_private_v2); - BOOST_TEST(!version2.has_value() && version2.error() == crypto::error::invalid_encoding); + codecs::decode(formats::pem(formats::pkcs8), rfc8410_private_v2); + BOOST_TEST(!version2.has_value() && version2.error() == codecs::error::input_too_long); + auto const version2_member = + ed25519::keypair::decode(formats::pem(formats::pkcs8), rfc8410_private_v2); + BOOST_TEST(!version2_member.has_value() && + version2_member.error() == codecs::error::input_too_long); + auto const version2_der = der_of("PRIVATE KEY", rfc8410_private_v2); + BOOST_TEST_EQ(version2_der.size(), 116U); crypto::test::check_refused( - formats::pkcs8, der_of("PRIVATE KEY", rfc8410_private_v2), crypto::error::invalid_encoding, - "RFC 8410's version 2"); + formats::pkcs8, version2_der, codecs::error::input_too_long, "RFC 8410's version 2"); } /** * Both bounds of every pair, through formats::max_encoded_size and formats::max_decode_input_size * (owner decision 23): equal today; the deferred PKCS#8 version 2 would make pkcs8's decode bound - * 83. The PEM results' capacity is codecs' bound of the armor over every label, their exact size - * RFC 7468's (design D4). + * 83. The PEM results' capacity is exact, the armor of the inner pair's longest encoding with its + * own label (one-verb's D3), RFC 7468's size (design D4). */ void bounds() { using ed25519::public_key; @@ -159,22 +168,22 @@ void bounds() { static_assert(formats::max_decode_input_size == 48); static_assert(formats::max_encoded_size, signature> == 64); static_assert(formats::max_decode_input_size, signature> == 64); - static_assert(formats::max_encoded_size>, public_key> == 221); - static_assert(formats::max_encoded_size, key_pair> == 225); + static_assert(formats::max_encoded_size>, public_key> == 113); + static_assert(formats::max_encoded_size, key_pair> == 119); // The results: public bytes and text, secret bytes and text. - using spki_result = decltype(crypto::encode(formats::spki, std::declval())); + using spki_result = decltype(codecs::encode(formats::spki, std::declval())); static_assert(std::is_same_v>>); using public_pem_result = - decltype(crypto::encode(formats::pem(formats::spki), std::declval())); - static_assert(std::is_same_v>>); - using raw_result = decltype(crypto::encode(formats::raw, std::declval())); + decltype(codecs::encode(formats::pem(formats::spki), std::declval())); + static_assert(std::is_same_v>>); + using raw_result = decltype(codecs::encode(formats::raw, std::declval())); static_assert(std::is_same_v>>); - using pkcs8_result = decltype(crypto::encode(formats::pkcs8, std::declval())); + using pkcs8_result = decltype(codecs::encode(formats::pkcs8, std::declval())); static_assert(std::is_same_v>>); using private_pem_result = - decltype(crypto::encode(formats::pem(formats::pkcs8), std::declval())); - static_assert(std::is_same_v>>); + decltype(codecs::encode(formats::pem(formats::pkcs8), std::declval())); + static_assert(std::is_same_v>>); auto const pair = key_pair::from_secret(test::from_hex(rfc8410_seed)); if (!BOOST_TEST(pair.has_value())) { @@ -193,14 +202,13 @@ void refused_pairs() { namespace detail = crypto::detail; static_assert(detail::refusal_reason_v == detail::refusal_reason::no_der_ed25519_bip340); - static_assert(!detail::pair_accepted()); - static_assert(!detail::pair_accepted()); - static_assert(detail::pair_accepted()); + static_assert(!codecs::encodable); + static_assert(!codecs::decodable); + static_assert(codecs::encodable); // The public key has no PKCS#8, the key pair no SPKI, the signature no PEM. - static_assert(!detail::pair_accepted()); - static_assert(!detail::pair_accepted, ed25519::keypair>()); - static_assert(!detail::pair_accepted, - ed25519::public_key>()); + static_assert(!codecs::encodable); + static_assert(!codecs::decodable>); + static_assert(!codecs::encodable>); } // Every pair has the members, so that support/format_checks.hpp never skips them, and they are @@ -229,11 +237,15 @@ static_assert(noexcept(std::declval().encode(formats::pem(format static_assert(noexcept(keypair::decode(formats::pkcs8, std::declval()))); static_assert(noexcept(std::declval().encode(formats::raw))); static_assert(noexcept(signature::decode(formats::raw, std::declval()))); +// codecs' decode of each PEM pair is noexcept (one-verb's R10). +static_assert(noexcept(codecs::decode(formats::pem(formats::spki), + std::string_view{}))); +static_assert(noexcept(codecs::decode(formats::pem(formats::pkcs8), std::string_view{}))); } // namespace member_checks /** * Every form of every pair on RFC 8410's key pair and a signature of it: the same encoding from - * crypto::encode, codecs::encode, the members, the span form and codecs' token; the decodes; a + * codecs::encode, the members, the span form and codecs' token; the decodes; a * short span refused untouched_byte; no heap; strict decoding of the DER; the private results * secret. */ @@ -275,6 +287,31 @@ void every_form() { crypto::test::check_secret_result(formats::pkcs8, *key_pair); crypto::test::check_secret_result(formats::pem(formats::pkcs8), *key_pair); crypto::test::check_secret_result(formats::raw, *key_pair); + + // The decode bound is each pair's acceptance contract (one-verb's R9). + crypto::test::check_largest_input, ed25519::public_key>(formats::raw, + public_key->bytes()); + crypto::test::check_largest_input, ed25519::public_key>(formats::spki, spki); + crypto::test::check_largest_input, ed25519::keypair>(formats::raw, seed); + crypto::test::check_largest_input(formats::pkcs8, pkcs8); + crypto::test::check_largest_input, ed25519::signature>(formats::raw, + signature->bytes()); +} + +/** + * RFC 8410's private PEM, as OpenSSL writes it, through codecs::decode: no heap, and codecs' + * intermediate, the PKCS#8's 48 bytes, wiped (secure's wipe record). + */ +void private_pem_through_codecs() { + webcpp::test::clear_wipes(); + std::size_t const before = webcpp::test::allocations(); + auto const decoded = + codecs::decode(formats::pem(formats::pkcs8), rfc8410_private); + std::size_t const after = webcpp::test::allocations(); + BOOST_TEST_EQ(after, before); + BOOST_TEST(decoded.has_value()); + std::vector const sizes = webcpp::test::wiped_sizes(); + BOOST_TEST_EQ(std::ranges::count(sizes, std::size_t{48}), 1); } /** A key pair's raw is its secret() and from_secret under the format's name (design R2). */ @@ -283,22 +320,22 @@ void key_pair_raw() { if (!BOOST_TEST(key_pair.has_value())) { return; } - auto const encoded = crypto::encode(formats::raw, *key_pair); + auto const encoded = codecs::encode(formats::raw, *key_pair); auto const seed = key_pair->secret(); if (!BOOST_TEST(encoded.has_value()) || !BOOST_TEST(seed.has_value())) { return; } BOOST_TEST(std::ranges::equal(encoded->view(), seed->bytes())); - auto const decoded = crypto::decode(formats::raw, seed->bytes()); + auto const decoded = codecs::decode(formats::raw, seed->bytes()); BOOST_TEST(decoded.has_value() && decoded->public_key().has_value() && *decoded->public_key() == *key_pair->public_key()); // Another size is from_secret's refusal. auto const short_seed = - crypto::decode(formats::raw, std::span(seed->bytes()).first(31)); + codecs::decode(formats::raw, std::span(seed->bytes()).first(31)); BOOST_TEST(!short_seed.has_value() && short_seed.error() == crypto::error::invalid_key_size); auto const key_bytes = key_pair->public_key()->bytes(); auto const short_key = - crypto::decode(formats::raw, std::span(key_bytes).first(31)); + codecs::decode(formats::raw, std::span(key_bytes).first(31)); BOOST_TEST(!short_key.has_value() && short_key.error() == crypto::error::invalid_public_key); } @@ -318,10 +355,10 @@ void wycheproof_public_keys() { auto const raw = test::from_hex(test::json_text(group.at("publicKey").at("pk"))); auto const der = test::from_hex(test::json_text(group.at("publicKeyDer"))); auto const text = test::json_text(group.at("publicKeyPem")); - auto const from_raw = crypto::decode(formats::raw, raw); - auto const from_der = crypto::decode(formats::spki, der); + auto const from_raw = codecs::decode(formats::raw, raw); + auto const from_der = codecs::decode(formats::spki, der); auto const from_text = - crypto::decode(formats::pem(formats::spki), text); + codecs::decode(formats::pem(formats::spki), text); if (!BOOST_TEST(from_raw.has_value() && from_der.has_value() && from_text.has_value())) { static_cast( std::fprintf(stderr, "Wycheproof's key refused: %s\n", test::to_hex(raw).c_str())); @@ -329,9 +366,9 @@ void wycheproof_public_keys() { } BOOST_TEST(*from_der == *from_raw); BOOST_TEST(*from_text == *from_raw); - auto const raw_again = crypto::encode(formats::raw, *from_raw); - auto const der_again = crypto::encode(formats::spki, *from_raw); - auto const text_again = crypto::encode(formats::pem(formats::spki), *from_raw); + auto const raw_again = codecs::encode(formats::raw, *from_raw); + auto const der_again = codecs::encode(formats::spki, *from_raw); + auto const text_again = codecs::encode(formats::pem(formats::spki), *from_raw); BOOST_TEST(raw_again.has_value() && bytes_of(raw_again->view()) == raw); BOOST_TEST(der_again.has_value() && bytes_of(der_again->view()) == der); BOOST_TEST(text_again.has_value() && text_again->view() == text); @@ -346,13 +383,13 @@ void layouts() { key[i] = static_cast(i); } std::string const key_hex = test::to_hex(key); - auto const ed_spki = crypto::encode(formats::spki, *ed25519::public_key::from_bytes(key)); + auto const ed_spki = codecs::encode(formats::spki, *ed25519::public_key::from_bytes(key)); BOOST_TEST(ed_spki.has_value() && test::to_hex(ed_spki->view()) == "302a300506032b6570032100" + key_hex); auto const pair = ed25519::keypair::from_secret(key); BOOST_TEST(pair.has_value()); if (pair) { - auto const ed_pkcs8 = crypto::encode(formats::pkcs8, *pair); + auto const ed_pkcs8 = codecs::encode(formats::pkcs8, *pair); BOOST_TEST(ed_pkcs8.has_value() && test::to_hex(ed_pkcs8->view()) == "302e020100300506032b657004220420" + key_hex); } @@ -443,12 +480,12 @@ void malformed_der() { /** * PEM as Windows and OpenSSL's tools write it (Review Focus 1): CRLF line ends and no final line * feed decode to the key, through codecs' armor; explanatory text before BEGIN, as `openssl pkey - * -text` prints it, is codecs' invalid_prefix, never a key; the other label is refused, and a - * hand-built pem_t reads and writes the inner format's label alone (support/format_checks.hpp). + * -text` prints it, is codecs' invalid_prefix, never a key; the other label is refused, and each + * PEM format reads and writes its inner format's label, in its type (support/format_checks.hpp). */ void pem_variants() { auto const expected = - crypto::decode(formats::pem(formats::pkcs8), rfc8410_private); + codecs::decode(formats::pem(formats::pkcs8), rfc8410_private); if (!BOOST_TEST(expected.has_value()) || !BOOST_TEST(expected->public_key().has_value())) { return; } @@ -487,6 +524,7 @@ int main() { bounds(); refused_pairs(); every_form(); + private_pem_through_codecs(); key_pair_raw(); wycheproof_public_keys(); layouts(); diff --git a/test/formats_pem_test.cpp b/test/formats_pem_test.cpp index 69146a0..d571f17 100644 --- a/test/formats_pem_test.cpp +++ b/test/formats_pem_test.cpp @@ -4,16 +4,20 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// PEM over the tests' toy type (design R23, R24, owner decision 23, Review Focus 1 and 5): -// crypto::encode(pem(spki), key) is codecs' armor of the SPKI, labelled from the inner format; -// crypto::decode reads it back in place, also with CRLF lines and without its final line feed, -// allocating nothing where codecs::decode of a std::string_view puts its intermediate on the heap -// (the documented difference); the armor's refusals are codecs' (a CERTIFICATE label, text before -// BEGIN); a payload past the inner format's decode bound, an RSA key's SPKI, and an armor of no -// bytes are invalid_encoding, nothing on the heap; the DER buffer is sized by the decode bound, so -// that pkcs8's 16-byte "version 2" decodes where a buffer of max_encoded_size would refuse it, and -// is wiped after a pkcs8, the guard held for a private format alone; every text gives the same -// value or the same error through crypto::decode and codecs::decode. +// PEM over the tests' toy type (design D4, R7, R8, R9, owner decision 29, Review Focus 4): +// codecs::encode(pem(spki), key) is codecs' armor of the SPKI, labelled from the inner format; +// codecs::decode and the member read it back through codecs' over, in place, also +// with CRLF lines and without its final line feed, allocating nothing; the armor's refusals are +// codecs' (a CERTIFICATE label, text before BEGIN); a payload past the inner format's decode bound, +// an RSA key's SPKI, is codecs' input_too_long on every path, as the same DER given directly is, +// nothing on the heap; an armor of no bytes is the toy's invalid_encoding; pkcs8's 16-byte "version +// 2", its bound, decodes and 17 bytes are input_too_long; codecs' intermediate of a pkcs8 is wiped, +// the written bytes, under codecs' guard, and none is made past the bound; every text gives the +// same value or the same error through codecs::decode and the member; codecs' +// decode of every PEM pair of the toy is noexcept. The label is in the format's type (one-verb's +// D3): every PEM pair through codecs::encode and the member writes its inner format's label, its +// default result exactly as long as its PEM, and a text under another label is codecs' +// invalid_prefix through every verb. // The timing seam is defined before any webcpp header reads secure's guard. #include "support/timing_seam.hpp" @@ -29,6 +33,7 @@ #include #include +#include #include #include @@ -38,6 +43,7 @@ #include #include #include +#include #include namespace crypto = webcpp::crypto; @@ -77,6 +83,19 @@ constexpr std::string_view rsa_pem = "DwIDAQAB\n" "-----END PUBLIC KEY-----\n"; +// codecs' decode of every PEM pair of the toy is noexcept (design R10), as crypto's and the +// member's are, from every text. +static_assert(noexcept(codecs::decode(formats::pem(formats::spki), + std::declval()))); +static_assert(noexcept(codecs::decode(formats::pem(formats::spki), + std::declval const&>()))); +static_assert(noexcept(codecs::decode(formats::pem(formats::pkcs8), + std::declval()))); +static_assert(noexcept(codecs::decode(formats::pem(formats::pkcs8), + std::declval()))); +static_assert(noexcept(toy_keypair::decode(formats::pem(formats::pkcs8), + std::declval()))); + /** `text` with every line feed made a carriage return and a line feed. */ std::string with_crlf(std::string_view text) { std::string crlf; @@ -100,7 +119,7 @@ std::string armored(std::string_view label, std::span der) { /** The PEM of the toy key is the armor of its SPKI, labelled PUBLIC KEY; every form gives it. */ void encode_public() { toy_key const key{key_bytes}; - auto const encoded = crypto::encode(formats::pem(formats::spki), key); + auto const encoded = codecs::encode(formats::pem(formats::spki), key); using result_type = decltype(encoded)::value_type; constexpr std::size_t capacity = codecs::value_codec>, toy_key>::max_encoded_size; @@ -122,7 +141,7 @@ void encode_public() { /** The PEM of the toy key pair is the armor of its PKCS#8, labelled PRIVATE KEY, secret. */ void encode_private() { auto const key_pair = toy_keypair::from_secret(secret_bytes); - auto const encoded = crypto::encode(formats::pem(formats::pkcs8), key_pair); + auto const encoded = codecs::encode(formats::pem(formats::pkcs8), key_pair); using result_type = decltype(encoded)::value_type; static_assert(std::is_same_v, toy_keypair>>>); @@ -137,6 +156,58 @@ void encode_private() { crypto::test::check_secret_result(formats::pem(formats::pkcs8), key_pair); } +/** + * Each PEM pair writes its inner format's label through codecs::encode and the member, the label + * in the format's type, its default result exactly as long as its PEM (codecs' R24). + */ +void labels_in_the_type() { + toy_key const key{key_bytes}; + auto const key_pair = toy_keypair::from_secret(secret_bytes); + auto const public_text = codecs::encode(formats::pem(formats::spki), key); + auto const public_member = key.encode(formats::pem(formats::spki)); + auto const private_text = codecs::encode(formats::pem(formats::pkcs8), key_pair); + auto const private_member = key_pair.encode(formats::pem(formats::pkcs8)); + if (!BOOST_TEST(public_text.has_value()) || !BOOST_TEST(public_member.has_value()) || + !BOOST_TEST(private_text.has_value()) || !BOOST_TEST(private_member.has_value())) { + return; + } + BOOST_TEST(public_text->view().starts_with("-----BEGIN PUBLIC KEY-----\n")); + BOOST_TEST(public_text->view().ends_with("-----END PUBLIC KEY-----\n")); + BOOST_TEST(public_member->view() == public_text->view()); + BOOST_TEST(private_text->view().starts_with("-----BEGIN PRIVATE KEY-----\n")); + BOOST_TEST(private_text->view().ends_with("-----END PRIVATE KEY-----\n")); + BOOST_TEST(private_member->view() == private_text->view()); + // The 12 bytes of each DER fill the exact capacity of its label's armor. + BOOST_TEST_EQ(public_text->size(), public_text->capacity()); + BOOST_TEST_EQ(private_text->size(), private_text->capacity()); +} + +/** Whether `decoded` is codecs' invalid_prefix. */ +template +bool invalid_prefix(Decoded const& decoded) { + return !decoded.has_value() && decoded.error() == codecs::error::invalid_prefix; +} + +/** + * A text under another label than its pair's, the other key's among them, is codecs' + * invalid_prefix through codecs::decode and the member. + */ +void other_labels_refused() { + for (std::string_view const other : {"CERTIFICATE", "EC PRIVATE KEY", "PRIVATE KEY"}) { + std::string const text = armored(other, key_spki); + BOOST_TEST(invalid_prefix(codecs::decode(formats::pem(formats::spki), text))); + BOOST_TEST(invalid_prefix(codecs::decode(formats::pem(formats::spki), text))); + BOOST_TEST(invalid_prefix(toy_key::decode(formats::pem(formats::spki), text))); + } + auto const pkcs8 = joined(std::array{0x30, 0x0a, 0x02, 0x00}, secret_bytes); + for (std::string_view const other : {"CERTIFICATE", "EC PRIVATE KEY", "PUBLIC KEY"}) { + std::string const text = armored(other, pkcs8); + BOOST_TEST(invalid_prefix(codecs::decode(formats::pem(formats::pkcs8), text))); + BOOST_TEST(invalid_prefix(codecs::decode(formats::pem(formats::pkcs8), text))); + BOOST_TEST(invalid_prefix(toy_keypair::decode(formats::pem(formats::pkcs8), text))); + } +} + /** * The text OpenSSL's tools write, its CRLF form and one without the final line feed decode to the * key (Review Focus 1). @@ -146,43 +217,48 @@ void decode_variants() { std::string const crlf = with_crlf(key_pem); std::string_view const no_final_line_feed = key_pem.substr(0, key_pem.size() - 1); for (std::string_view const text : {key_pem, std::string_view{crlf}, no_final_line_feed}) { - auto const decoded = crypto::decode(formats::pem(formats::spki), text); + auto const decoded = codecs::decode(formats::pem(formats::spki), text); if (BOOST_TEST(decoded.has_value())) { BOOST_TEST(*decoded == key); } } auto const from_string = - crypto::decode(formats::pem(formats::spki), std::string{key_pem}); + codecs::decode(formats::pem(formats::spki), std::string{key_pem}); if (BOOST_TEST(from_string.has_value())) { BOOST_TEST(*from_string == key); } } /** - * The in-place decode allocates nothing; codecs::decode of the same std::string_view holds its - * intermediate on the heap (design R24, the guide's documented difference). + * Every decode of a PEM allocates nothing (design D4): codecs::decode and the member, of both PEM + * pairs, from a std::string_view, a span of characters and a vector of them, + * the vector made before the count. */ void decode_in_place() { - std::size_t const before = webcpp::test::allocations(); - auto const in_place = crypto::decode(formats::pem(formats::spki), key_pem); - std::size_t const after_in_place = webcpp::test::allocations(); - auto const through_codecs = codecs::decode(formats::pem(formats::spki), key_pem); - std::size_t const after_codecs = webcpp::test::allocations(); - BOOST_TEST_EQ(after_in_place, before); - BOOST_TEST_GT(after_codecs, after_in_place); - BOOST_TEST(in_place.has_value()); - BOOST_TEST(through_codecs.has_value()); - - // Every text codecs takes decodes in place too, never on codecs' heap path: a span of - // characters and a vector of them, the vector made before the count. + auto const key_pair = toy_keypair::from_secret(secret_bytes); + auto const private_pem = codecs::encode(formats::pem(formats::pkcs8), key_pair); + if (!BOOST_TEST(private_pem.has_value())) { + return; + } + std::string_view const private_text = private_pem->view(); std::vector const characters(key_pem.begin(), key_pem.end()); std::span const span_text(key_pem.data(), key_pem.size()); - std::size_t const before_ranges = webcpp::test::allocations(); - auto const from_span = crypto::decode(formats::pem(formats::spki), span_text); - auto const from_vector = crypto::decode(formats::pem(formats::spki), characters); - std::size_t const after_ranges = webcpp::test::allocations(); - BOOST_TEST_EQ(after_ranges, before_ranges); - if (BOOST_TEST(from_span.has_value()) && BOOST_TEST(from_vector.has_value()) && in_place) { + std::size_t const before = webcpp::test::allocations(); + auto const in_place = codecs::decode(formats::pem(formats::spki), key_pem); + auto const through_member = toy_key::decode(formats::pem(formats::spki), key_pem); + auto const private_in_place = + codecs::decode(formats::pem(formats::pkcs8), private_text); + auto const private_through_member = + toy_keypair::decode(formats::pem(formats::pkcs8), private_text); + auto const from_span = codecs::decode(formats::pem(formats::spki), span_text); + auto const from_vector = codecs::decode(formats::pem(formats::spki), characters); + std::size_t const after = webcpp::test::allocations(); + BOOST_TEST_EQ(after, before); + BOOST_TEST(private_in_place.has_value()); + BOOST_TEST(private_through_member.has_value()); + if (BOOST_TEST(in_place.has_value()) && BOOST_TEST(through_member.has_value()) && + BOOST_TEST(from_span.has_value()) && BOOST_TEST(from_vector.has_value())) { + BOOST_TEST(*through_member == *in_place); BOOST_TEST(*from_span == *in_place); BOOST_TEST(*from_vector == *in_place); } @@ -194,144 +270,162 @@ void decode_in_place() { */ void armor_refusals() { std::string const certificate = armored("CERTIFICATE", key_spki); - BOOST_TEST(crypto::decode(formats::pem(formats::spki), certificate).error() == + BOOST_TEST(codecs::decode(formats::pem(formats::spki), certificate).error() == codecs::error::invalid_prefix); std::string const explained = "Public-Key: (64 bit)\n" + std::string{key_pem}; - BOOST_TEST(crypto::decode(formats::pem(formats::spki), explained).error() == + BOOST_TEST(codecs::decode(formats::pem(formats::spki), explained).error() == codecs::error::invalid_prefix); - BOOST_TEST(crypto::decode(formats::pem(formats::pkcs8), key_pem).error() == + BOOST_TEST(codecs::decode(formats::pem(formats::pkcs8), key_pem).error() == codecs::error::invalid_prefix); } /** - * A payload past the inner format's decode bound, an RSA key's SPKI of 294 bytes, and an armor of - * no bytes are invalid_encoding, with nothing on the heap (Review Focus 5); the payload of 294 - * bytes is written nowhere past the stack buffer (the fuzz lane's address sanitizer reads that). + * A payload past the inner format's decode bound, an RSA key's SPKI of 294 bytes, is codecs' + * input_too_long through codecs::decode and the member, with nothing on the heap + * (Review Focus 4), as the same 294 bytes given directly to codecs::decode are; no intermediate is + * made for it, so nothing is written past the bound (the fuzz lane's address sanitizer reads + * that). An armor of no bytes reaches the toy's value codec: its invalid_encoding. */ void payload_past_the_bound() { std::size_t const before = webcpp::test::allocations(); - auto const rsa = crypto::decode(formats::pem(formats::spki), rsa_pem); + auto const rsa = codecs::decode(formats::pem(formats::spki), rsa_pem); + auto const rsa_member = toy_key::decode(formats::pem(formats::spki), rsa_pem); std::size_t const after = webcpp::test::allocations(); BOOST_TEST_EQ(after, before); - BOOST_TEST(rsa.error() == crypto::error::invalid_encoding); + BOOST_TEST(rsa.error() == codecs::error::input_too_long); + BOOST_TEST(rsa_member.error() == codecs::error::input_too_long); auto const rsa_der = codecs::decode(codecs::formats::armor("PUBLIC KEY"), rsa_pem); if (BOOST_TEST(rsa_der.has_value())) { BOOST_TEST_EQ(rsa_der->size(), 294U); + std::span const der(*rsa_der); + BOOST_TEST(codecs::decode(formats::spki, der).error() == + codecs::error::input_too_long); + BOOST_TEST(toy_key::decode(formats::spki, der).error() == codecs::error::input_too_long); } std::string const empty = armored("PUBLIC KEY", {}); BOOST_TEST_EQ(empty, std::string{"-----BEGIN PUBLIC KEY-----\n-----END PUBLIC KEY-----\n"}); - BOOST_TEST(crypto::decode(formats::pem(formats::spki), empty).error() == + BOOST_TEST(codecs::decode(formats::pem(formats::spki), empty).error() == + crypto::error::invalid_encoding); + BOOST_TEST(toy_key::decode(formats::pem(formats::spki), empty).error() == crypto::error::invalid_encoding); } /** - * The DER buffer is sized by the inner format's decode bound (owner decision 23): the toy key - * pair's pkcs8 "version 2", 16 bytes, past its max_encoded_size of 12, decodes in place; 17 bytes - * are invalid_encoding. + * The decode bound is the pair's acceptance contract (design R9): the toy key pair's pkcs8 + * "version 2", 16 bytes, past its max_encoded_size of 12, decodes in place; 17 bytes are + * input_too_long through codecs::decode and the member. */ void decode_bound_sizes_the_buffer() { static_assert(formats::max_encoded_size == 12); static_assert(formats::max_decode_input_size == 16); std::string const version_2 = armored("PRIVATE KEY", key_pair_pkcs8_v2); std::size_t const before = webcpp::test::allocations(); - auto const decoded = crypto::decode(formats::pem(formats::pkcs8), version_2); + auto const decoded = codecs::decode(formats::pem(formats::pkcs8), version_2); + auto const through_member = toy_keypair::decode(formats::pem(formats::pkcs8), version_2); std::size_t const after = webcpp::test::allocations(); BOOST_TEST_EQ(after, before); - if (BOOST_TEST(decoded.has_value())) { + if (BOOST_TEST(decoded.has_value()) && BOOST_TEST(through_member.has_value())) { BOOST_TEST(std::ranges::equal(decoded->secret(), secret_bytes)); + BOOST_TEST(std::ranges::equal(through_member->secret(), secret_bytes)); } std::array longer{}; std::ranges::copy(key_pair_pkcs8_v2, longer.begin()); - BOOST_TEST( - crypto::decode(formats::pem(formats::pkcs8), armored("PRIVATE KEY", longer)) - .error() == crypto::error::invalid_encoding); + std::string const too_long = armored("PRIVATE KEY", longer); + BOOST_TEST(codecs::decode(formats::pem(formats::pkcs8), too_long).error() == + codecs::error::input_too_long); + BOOST_TEST(toy_keypair::decode(formats::pem(formats::pkcs8), too_long).error() == + codecs::error::input_too_long); } /** - * The DER buffer of a pkcs8 is wiped, its whole decode bound, on success and on error; the one of a - * public decode is not. secure's wipe record sees every wipe leave only zero bytes. + * codecs' intermediate of a pkcs8, the DER the armor wrote (12 or 16 bytes, its payload's size), + * is wiped on success and on the value codec's refusal; past the bound, and on the armor's + * refusal, none is made, so the toy's decode reads no buffer; a public decode wipes none. secure's + * wipe record sees every wipe leave only zero bytes. */ void buffer_wiped() { auto const key_pair = toy_keypair::from_secret(secret_bytes); - auto const encoded = crypto::encode(formats::pem(formats::pkcs8), key_pair); + auto const encoded = codecs::encode(formats::pem(formats::pkcs8), key_pair); if (!BOOST_TEST(encoded.has_value())) { return; } - constexpr std::size_t buffer_size = - formats::max_decode_input_size; - // The buffer is found by its address, which the toy's pkcs8 decode records as its input. - webcpp::crypto::test::toy_pkcs8_decoded_from = nullptr; - webcpp::test::clear_wipes(); - { - auto const decoded = - crypto::decode(formats::pem(formats::pkcs8), encoded->view()); - BOOST_TEST(decoded.has_value()); - void const* const buffer = webcpp::crypto::test::toy_pkcs8_decoded_from; - BOOST_TEST(buffer != nullptr); - BOOST_TEST(webcpp::test::wiped(buffer, buffer_size)); + // The intermediate is found by its address, which the toy's pkcs8 decode records as its input. + auto const wiped_once = [](std::string_view text, std::size_t payload_size, bool valid) { + webcpp::crypto::test::toy_pkcs8_decoded_from = nullptr; + webcpp::test::clear_wipes(); + auto const decoded = codecs::decode(formats::pem(formats::pkcs8), text); + BOOST_TEST_EQ(decoded.has_value(), valid); + void const* const intermediate = webcpp::crypto::test::toy_pkcs8_decoded_from; + BOOST_TEST(intermediate != nullptr); + BOOST_TEST(webcpp::test::wiped(intermediate, payload_size)); std::vector const sizes = webcpp::test::wiped_sizes(); - BOOST_TEST(std::ranges::count(sizes, buffer_size) == 1); - } - webcpp::test::clear_wipes(); - { - std::string const corrupt = armored("PRIVATE KEY", std::span(key_pair_pkcs8_v2).first(15)); - auto const refused = crypto::decode(formats::pem(formats::pkcs8), corrupt); - BOOST_TEST(refused.error() == crypto::error::invalid_encoding); - BOOST_TEST(webcpp::test::wiped(webcpp::crypto::test::toy_pkcs8_decoded_from, buffer_size)); - std::vector const sizes = webcpp::test::wiped_sizes(); - BOOST_TEST(std::ranges::count(sizes, buffer_size) == 1); - } - { - // A payload past the decode bound (Review Focus 5 for a secret): the armor refuses to - // write it, so the toy's decode never sees the buffer and records no address; one wipe of - // the buffer's size is all the record shows (another call's address would not be this - // one's under other inlining or AddressSanitizer's fake stacks). - std::array longer{}; - std::ranges::copy(key_pair_pkcs8_v2, longer.begin()); - auto const refused = crypto::decode(formats::pem(formats::pkcs8), - armored("PRIVATE KEY", longer)); - BOOST_TEST(refused.error() == crypto::error::invalid_encoding); - std::vector const sizes = webcpp::test::wiped_sizes(); - BOOST_TEST(std::ranges::count(sizes, buffer_size) == 1); - } - { - auto const refused = crypto::decode(formats::pem(formats::pkcs8), - armored("CERTIFICATE", key_spki)); - BOOST_TEST(refused.error() == codecs::error::invalid_prefix); - std::vector const sizes = webcpp::test::wiped_sizes(); - BOOST_TEST(std::ranges::count(sizes, buffer_size) == 1); - } - { - auto const decoded = crypto::decode(formats::pem(formats::spki), key_pem); - BOOST_TEST(decoded.has_value()); - // codecs' armor wipes its own state on every call; crypto wipes no buffer of a public key. + BOOST_TEST_EQ(std::ranges::count(sizes, payload_size), 1); + }; + wiped_once(encoded->view(), 12, true); + wiped_once(armored("PRIVATE KEY", key_pair_pkcs8_v2), 16, true); + wiped_once(armored("PRIVATE KEY", std::span(key_pair_pkcs8_v2).first(15)), 15, false); + + auto const no_intermediate = [](std::string_view text, boost::system::error_code expected) { + webcpp::crypto::test::toy_pkcs8_decoded_from = nullptr; + webcpp::test::clear_wipes(); + auto const refused = codecs::decode(formats::pem(formats::pkcs8), text); + BOOST_TEST(refused.error() == expected); + BOOST_TEST(webcpp::crypto::test::toy_pkcs8_decoded_from == nullptr); std::vector const sizes = webcpp::test::wiped_sizes(); - BOOST_TEST(std::ranges::count( - sizes, formats::max_decode_input_size, toy_key>) == 0); - } + for (std::size_t const payload_size : {12U, 16U, 17U}) { + BOOST_TEST_EQ(std::ranges::count(sizes, payload_size), 0); + } + }; + std::array longer{}; + std::ranges::copy(key_pair_pkcs8_v2, longer.begin()); + no_intermediate(armored("PRIVATE KEY", longer), codecs::error::input_too_long); + no_intermediate(armored("CERTIFICATE", key_spki), codecs::error::invalid_prefix); + + webcpp::test::clear_wipes(); + auto const decoded = codecs::decode(formats::pem(formats::spki), key_pem); + BOOST_TEST(decoded.has_value()); + // codecs' armor wipes its own state on every call; no intermediate of a public key is wiped. + std::vector const sizes = webcpp::test::wiped_sizes(); + BOOST_TEST_EQ(std::ranges::count(sizes, key_spki.size()), 0); } /** - * The in-place decode of a private format holds crypto's guard, decode_pem's, first; a public one - * enters none of crypto's. + * The decode of a private format holds codecs' guard, the core's (design R12), first, refused or + * not; a public one enters no guard. The toy's value codecs are the tests', so crypto's own guards + * are not counted: the curves' formats tests read those. */ void guard_for_a_private_format() { auto const key_pair = toy_keypair::from_secret(secret_bytes); - auto const encoded = crypto::encode(formats::pem(formats::pkcs8), key_pair); + auto const encoded = codecs::encode(formats::pem(formats::pkcs8), key_pair); if (!BOOST_TEST(encoded.has_value())) { return; } - webcpp::test::require_enters("detail::decode_pem of pem(pkcs8)", [&] { + auto const enters_a_guard = [](auto const& call) { + std::size_t const before = webcpp::test::timing_all_entries; + webcpp::test::timing_first_entry = nullptr; + call(); + bool const entered = webcpp::test::timing_all_entries != before && + webcpp::test::timing_first_entry != nullptr && + std::string_view{webcpp::test::timing_first_entry}.find( + "webcpp::codecs::") != std::string_view::npos; + webcpp::test::timing_first_entry = nullptr; + return entered; + }; + BOOST_TEST(enters_a_guard([&] { + BOOST_TEST( + codecs::decode(formats::pem(formats::pkcs8), encoded->view()).has_value()); + })); + BOOST_TEST(enters_a_guard([&] { BOOST_TEST( - crypto::decode(formats::pem(formats::pkcs8), encoded->view()).has_value()); - }); - webcpp::test::require_enters("detail::decode_pem of pem(pkcs8), refused", [&] { - BOOST_TEST(crypto::decode(formats::pem(formats::pkcs8), key_pem).error() == + codecs::decode(formats::pem(formats::pkcs8), encoded->view()).has_value()); + })); + BOOST_TEST(enters_a_guard([&] { + BOOST_TEST(codecs::decode(formats::pem(formats::pkcs8), key_pem).error() == codecs::error::invalid_prefix); - }); - webcpp::test::uncovered("detail::decode_pem of pem(spki)", [&] { - BOOST_TEST(crypto::decode(formats::pem(formats::spki), key_pem).has_value()); - }); + })); + std::size_t const before = webcpp::test::timing_all_entries; + BOOST_TEST(codecs::decode(formats::pem(formats::spki), key_pem).has_value()); + BOOST_TEST_EQ(webcpp::test::timing_all_entries, before); } /** Whether two results hold equal values, or the same error. */ @@ -347,10 +441,10 @@ void same_results(codecs::result const& expected, codecs::result c } /** - * crypto::decode of `text`, as a std::string, a span of characters and a vector of them, and - * codecs::decode of it give the same value, or the same error, for both PEM pairs. + * codecs::decode of `text` as a std::string, as a span of characters and as a vector of them, and + * the member, give the same value, or the same error, for both PEM pairs. */ -void same_as_codecs_on(std::string const& text) { +void every_input_and_the_member_agree_on(std::string const& text) { auto const same_key = [](toy_key const& first, toy_key const& second) { return first == second; }; @@ -359,22 +453,28 @@ void same_as_codecs_on(std::string const& text) { }; std::span const span_text(text.data(), text.size()); std::vector const characters(text.begin(), text.end()); - auto const in_place = crypto::decode(formats::pem(formats::spki), text); - same_results(in_place, codecs::decode(formats::pem(formats::spki), text), same_key); - same_results(in_place, crypto::decode(formats::pem(formats::spki), span_text), + auto const in_place = codecs::decode(formats::pem(formats::spki), text); + same_results(in_place, codecs::decode(formats::pem(formats::spki), span_text), same_key); - same_results(in_place, crypto::decode(formats::pem(formats::spki), characters), + same_results(in_place, codecs::decode(formats::pem(formats::spki), characters), same_key); - auto const private_in_place = crypto::decode(formats::pem(formats::pkcs8), text); - same_results(private_in_place, codecs::decode(formats::pem(formats::pkcs8), text), + same_results(in_place, toy_key::decode(formats::pem(formats::spki), text), same_key); + auto const private_in_place = codecs::decode(formats::pem(formats::pkcs8), text); + same_results(private_in_place, + codecs::decode(formats::pem(formats::pkcs8), span_text), + same_key_pair); + same_results(private_in_place, toy_keypair::decode(formats::pem(formats::pkcs8), text), same_key_pair); same_results(private_in_place, - crypto::decode(formats::pem(formats::pkcs8), characters), + codecs::decode(formats::pem(formats::pkcs8), characters), same_key_pair); } -/** crypto::decode and codecs::decode give the same value, or the same error, on every text. */ -void same_as_codecs() { +/** + * codecs::decode from every input type and the member give the same value, or the same error, on + * every text. + */ +void every_input_and_the_member_agree() { std::string const crlf = with_crlf(key_pem); std::string const certificate = armored("CERTIFICATE", key_spki); std::string const explained = "Public-Key: (64 bit)\n" + std::string{key_pem}; @@ -384,7 +484,7 @@ void same_as_codecs() { std::ranges::copy(key_pair_pkcs8_v2, longer.begin()); std::string const too_long = armored("PRIVATE KEY", longer); auto const key_pair = toy_keypair::from_secret(secret_bytes); - auto const private_pem = crypto::encode(formats::pem(formats::pkcs8), key_pair); + auto const private_pem = codecs::encode(formats::pem(formats::pkcs8), key_pair); if (!BOOST_TEST(private_pem.has_value())) { return; } @@ -404,7 +504,7 @@ void same_as_codecs() { "-----BEGIN PUBLIC KEY-----\n", }; for (std::string const& text : texts) { - same_as_codecs_on(text); + every_input_and_the_member_agree_on(text); } } @@ -413,6 +513,8 @@ void same_as_codecs() { int main() { encode_public(); encode_private(); + labels_in_the_type(); + other_labels_refused(); decode_variants(); decode_in_place(); armor_refusals(); @@ -420,6 +522,6 @@ int main() { decode_bound_sizes_the_buffer(); buffer_wiped(); guard_for_a_private_format(); - same_as_codecs(); + every_input_and_the_member_agree(); return webcpp::test::exit_status(boost::report_errors()); } diff --git a/test/formats_stand_in_test.cpp b/test/formats_stand_in_test.cpp index e9a7f9f..649603a 100644 --- a/test/formats_stand_in_test.cpp +++ b/test/formats_stand_in_test.cpp @@ -4,11 +4,14 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// The room for JWK (design D1, R17): a text format of the tests' own, the stand-in of -// support/formats_toy.hpp, added to a copy of the library's one list, goes through crypto::encode -// and crypto::decode, which give codecs' text result and take a std::string_view, with no change to -// either; over the toy key's list with the stand-in added, crypto's check accepts it and still -// refuses der, as it does on the toy key's own list. +// The room for JWK (design D1, R17; the spec's scenario "The room for JWK"): a text format of the +// tests' own, the stand-in of support/formats_toy.hpp, added to a copy of the library's one list +// and to the list of a key of the tests' own, toy_jwk_key (toy_key's list with the stand-in added, +// and the stand-in's value codec), goes through codecs::encode and codecs::decode, which give +// codecs' text result and take a std::string_view, through crypto's declaration of the type to +// codecs, with no change to any of them; over that list crypto's declaration has the stand-in and +// still refuses der, as it does on the toy key's own list. The toy key, whose list lacks the +// stand-in, refuses it (one-verb's R4). #include "support/formats_toy.hpp" @@ -31,6 +34,7 @@ namespace formats = webcpp::crypto::formats; namespace detail = webcpp::crypto::detail; using webcpp::crypto::test::all_formats_with_stand_in; using webcpp::crypto::test::stand_in_text_t; +using webcpp::crypto::test::toy_jwk_key; using webcpp::crypto::test::toy_key; namespace { @@ -42,51 +46,66 @@ using toy_key_formats_with_stand_in = // The copy holds the library's six formats and the stand-in; the library's own list is unchanged. static_assert( std::is_same_v, formats::spki_t<>, formats::pkcs8_t, + codecs::format_list, formats::spki_t<>, formats::pkcs8_t, formats::der_t, formats::pem_t>, formats::pem_t, stand_in_text_t>>); +// The JWK's toy key lists the toy key's formats and the stand-in, and crypto declares it to codecs. +static_assert(std::is_same_v, toy_key_formats_with_stand_in>); +static_assert( + std::is_same_v::formats, toy_key_formats_with_stand_in>); +static_assert(codecs::value_formats::has_format); +static_assert(!codecs::value_formats::has_format); static_assert(detail::in_list_v); static_assert(!detail::in_list_v); static_assert(!detail::is_format_v); -// crypto's check over the list with the stand-in: the stand-in is no refusal, der still is. +// crypto's list check over the list with the stand-in: the stand-in is no refusal, der still is. static_assert(!detail::format_refused); static_assert(detail::format_refused); static_assert(detail::format_refused>); static_assert(!detail::format_refused, toy_key, toy_key_formats_with_stand_in>); -// codecs' concepts and results take the stand-in as they take crypto's formats. +// codecs' concepts and results take the stand-in as they take crypto's formats; der stays refused. static_assert(codecs::format); -static_assert(codecs::encodable); -static_assert(codecs::decodable); -static_assert(formats::max_encoded_size == 16); -static_assert(formats::max_decode_input_size == 16); +static_assert(codecs::encodable); +static_assert(codecs::decodable); +static_assert(!codecs::encodable); +static_assert(formats::max_encoded_size == 16); +static_assert(formats::max_decode_input_size == 16); + +// The toy key's list lacks the stand-in: refused by its list (one-verb's R4). +static_assert(!codecs::encodable); +static_assert(!codecs::decodable); -/** The stand-in through crypto's verbs, which give codecs' text result and take a text. */ +/** The stand-in through codecs' verbs, which give codecs' text result and take a text. */ void through_the_verbs() { - toy_key const key{std::array{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}}; - auto const encoded = crypto::encode(webcpp::crypto::test::stand_in_text, key); + toy_jwk_key const key{ + std::array{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}}; + auto const encoded = codecs::encode(webcpp::crypto::test::stand_in_text, key); static_assert(std::is_same_v>); if (BOOST_TEST(encoded.has_value())) { BOOST_TEST(encoded->view() == std::string_view{"0102030405060708"}); } std::string appended = "jwk: "; - auto const token = crypto::encode(webcpp::crypto::test::stand_in_text, key, + auto const token = codecs::encode(webcpp::crypto::test::stand_in_text, key, codecs::tokens::append_to(appended)); BOOST_TEST(token.has_value()); BOOST_TEST_EQ(appended, std::string{"jwk: 0102030405060708"}); - auto const decoded = crypto::decode(webcpp::crypto::test::stand_in_text, - std::string_view{"0102030405060708"}); + auto const decoded = codecs::decode(webcpp::crypto::test::stand_in_text, + std::string_view{"0102030405060708"}); if (BOOST_TEST(decoded.has_value())) { BOOST_TEST(*decoded == key); } - BOOST_TEST(crypto::decode(webcpp::crypto::test::stand_in_text, - std::string_view{"010203040506070G"}) + BOOST_TEST(codecs::decode(webcpp::crypto::test::stand_in_text, + std::string_view{"010203040506070G"}) .error() == crypto::error::invalid_encoding); - BOOST_TEST(crypto::decode(webcpp::crypto::test::stand_in_text, - std::string_view{"01020304050607"}) + BOOST_TEST(codecs::decode(webcpp::crypto::test::stand_in_text, + std::string_view{"01020304050607"}) .error() == crypto::error::invalid_encoding); + BOOST_TEST(codecs::decode(webcpp::crypto::test::stand_in_text, + std::string_view{"0102030405060708a"}) + .error() == codecs::error::input_too_long); } } // namespace diff --git a/test/formats_table_test.cpp b/test/formats_table_test.cpp index 4eafe0a..4eef063 100644 --- a/test/formats_table_test.cpp +++ b/test/formats_table_test.cpp @@ -6,21 +6,28 @@ // The table of the valid pairs (design D4; the spec's scenario "A list in one place"): every format // of detail::all_formats, each with every point form it takes, over every type of the table, -// through codecs' own concepts, codecs::encodable and codecs::decodable, and crypto's check, -// detail::pair_accepted, which agree on every pair; then against doc/tables/crypto.json's -// `formats` section, read at run time: its list is all_formats's, in its order, its types are -// these, in this order, and each type's pairs are exactly the ones it has, a pair marked -// `encode_only` having no decode, one marked `secret` giving codecs' secret results, and the size -// of each binary pair being its max_encoded_size (a range's last number for `der`). The guide's -// table, doc/generated/formats.adoc, is generate.py's from the same section, which refuses a list -// that is not formats.hpp's: the table, the list and the types cannot drift apart unseen. A sample -// of each type is encoded in each of its pairs, its size the cell's (DER's shortest, r and s of 1, -// its range's first number; a PEM's RFC 7468's count of its inner pair's). The -// key pairs are on a synchronous backend (BearSSL, libsodium, libsecp256k1): one whose keys live -// in WebCrypto has no private format (design D7, formats_asynchronous_test.cpp). It prints each -// type's pairs as it finds them. +// through codecs' own concepts, codecs::encodable and codecs::decodable, which crypto's +// declaration to codecs governs (one-verb's D2): a composition under base64 converts exactly as its +// inner format does, an armor over raw or der likewise (codecs' R20), codecs' armor_t over spki or +// pkcs8, and basic_armor of the other key's label, never (R5), and hex no type (R4); then against +// doc/tables/crypto.json's `formats` section, read at run time: its list is all_formats's, in its +// order, its types are these, in this order, and each type's pairs are exactly the ones it has, a +// pair marked `encode_only` having no decode, one marked `secret` giving codecs' secret results, +// and the size of each binary pair being its max_encoded_size (a range's last number for `der`). +// The guide's table, doc/generated/formats.adoc, is generate.py's from the same section, which +// refuses a list that is not formats.hpp's: the table, the list and the types cannot drift apart +// unseen. A sample of each type is encoded in each of its pairs, its size the cell's (DER's +// shortest, r and s of 1, its range's first number; a PEM's RFC 7468's count of its inner pair's). +// The key pairs are on a synchronous backend (BearSSL, libsodium, libsecp256k1): one whose keys +// live in WebCrypto has no private format (design D7, formats_asynchronous_test.cpp). It prints +// each type's pairs as it finds them. Each PEM pair's default result is exact (one-verb's D3, +// Review Focus 3), full for the pair's longest PEM, and formats::pem(F) is a pem_t whose armor's +// label is detail::pem_label::value. #include +#include +#include +#include #include #include #include @@ -128,13 +135,13 @@ struct spelling> { */ template struct variants_of { - using type = detail::format_list; + using type = codecs::format_list; }; /** The point forms of design D1, the default first. */ template