# webcpp::crypto > Cryptographic primitives for C++20 on native, wasip2, wasip3 and Emscripten: AES-GCM, the AES block cipher, secp256k1 (BIP 340 Schnorr, ECDSA and recoverable signatures, ECDH, tweaks, ElligatorSwift, MuSig2, silent payments), Ed25519 and Ed25519ph (RFC 8032), X25519 (RFC 7748), their keys in SPKI, PKCS#8 (RFC 8410) and PEM, P-256 key pairs, ECDSA and ECDH on BearSSL, libsodium's crypto_kx, the ChaCha20-Poly1305 AEADs, libsodium's secret boxes, boxes and sealed boxes, a random_device on the system's generator, each over a widely used, reviewed C library (BearSSL, OpenSSL, ctaes, libsecp256k1, libsodium) that the program names in its Jamfile. Hashing and HMAC are Boost.Hash2's; HKDF and password hashing are webcpp::hash's; the constant-time comparison, the erasure of a buffer and secret are webcpp::secure's. Every failure is a boost::system::result value. Protocols (Bitcoin, Ethereum, Nostr) belong to domain libraries built on these primitives. Generated by doc/tables/generate.py with the page's tables. Page: https://webcpporg.github.io/libs/crypto/ ## webcpp::crypto - [random_device](https://webcpporg.github.io/libs/crypto/#crypto-entropy): Unpredictable bytes from the operating system's or the browser's generator, and nothing else. Header . Backends: the system's generator (/webcpp/crypto//crypto) on native, wasip2, wasip3, emscripten. Use it to: generate a session token, a password-reset link or an API key; draw an unbiased number in a range, such as a six-digit code. - fill: Fills `output_bytes` from the system's generator. - operator(): A value from the system's generator, as the standard library's random device gives one. - min: The smallest value `operator()` gives. - max: The largest value `operator()` gives. - entropy: The entropy of a value, in bits: every bit is the system's. - [async_from_key, secret_key_encryption::aead::async_seal, secret_key_encryption::aead::async_open](https://webcpporg.github.io/libs/crypto/#crypto-async): The asynchronous from_key, seal and open of AES-GCM on every backend, and on a native backend of every other AEAD and secret box, with the box forms `secret_key_encryption::async_seal` and `async_open` of a secret box or a crypto_box, each completing through a post to the executor the call gives, with any Asio completion token. Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten; OpenSSL (/webcpp/crypto//openssl) on native, wasip2, wasip3; libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Use it to: seal or open a record inside an event loop without waiting. - async_from_key: Makes an AEAD, a stream cipher or a secret box from its key bytes, asynchronously: `Cipher::from_key(key_bytes)` run when the operation is initiated, its result posted to `completion_executor`. - async_seal: Seals a message in place, asynchronously: `aead_cipher.seal` run when the operation is initiated, its result posted to `completion_executor`. - async_open: Opens a message in place, asynchronously: `aead_cipher.open` run when the operation is initiated, its result posted to `completion_executor`. - [async_from_secret, async_generate, async_sign, async_verify](https://webcpporg.github.io/libs/crypto/#crypto-async): Ed25519's asynchronous from_secret, generate, sign and verify with `crypto::signing_schemes::eddsa`, and on a native backend with `crypto::signing_schemes::eddsa_prehashed` (Ed25519ph, which WebCrypto lacks), each completing through a post to the executor the call gives, with any Asio completion token. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Use it to: sign and verify with Ed25519 in a coroutine. - async_from_secret: Makes an Ed25519, an X25519 or a P-256 key pair from its secret bytes, asynchronously: `Keypair::from_secret(secret_bytes)` run when the operation is initiated, its result posted to `completion_executor`. - async_generate: Makes an Ed25519, an X25519 or a P-256 key pair from a fresh secret, asynchronously: `Keypair::generate()` run when the operation is initiated (the system's generator is drawn from then), its result posted to `completion_executor`. - async_sign: Signs with an Ed25519 or a P-256 key pair, asynchronously: `key_pair.sign(signing_scheme, signing_input)` run when the operation is initiated, its result posted to `completion_executor`. - async_verify: Verifies an Ed25519 signature (RFC 8032's pure Ed25519) under a verification policy, asynchronously: `crypto::verify(crypto::signing_schemes::eddsa, crypto::verification_policies::canonical, ...)` run when the operation is initiated, its result posted to `completion_executor`. - [async_from_secret, async_generate, async_agree](https://webcpporg.github.io/libs/crypto/#crypto-async): X25519's asynchronous from_secret, generate and agree, of a key pair and a peer's `curves::x25519::public_key`, each completing through a post to the executor the call gives, with any Asio completion token. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. - async_from_secret: Makes an Ed25519, an X25519 or a P-256 key pair from its secret bytes, asynchronously: `Keypair::from_secret(secret_bytes)` run when the operation is initiated, its result posted to `completion_executor`. - async_generate: Makes an Ed25519, an X25519 or a P-256 key pair from a fresh secret, asynchronously: `Keypair::generate()` run when the operation is initiated (the system's generator is drawn from then), its result posted to `completion_executor`. - async_agree: Agrees a shared secret, asynchronously: `key_pair.agree(peer_public_key)` run when the operation is initiated, its result posted to `completion_executor`: X25519's (RFC 7748), or P-256's ECDH raw x coordinate, as WebCrypto's `deriveBits` with ECDH on P-256 gives its 256 bits. - [async_from_secret, async_generate, async_sign, async_verify, async_agree](https://webcpporg.github.io/libs/crypto/#crypto-async): P-256's asynchronous from_secret, generate, ECDSA sign and verify with `crypto::signing_schemes::ecdsa_sha256`, of a message hashed with SHA-256 or `crypto::signing_schemes::prehashed{digest}`, and ECDH's raw x, each completing through a post to the executor the call gives, with any Asio completion token. Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten. - async_from_secret: Makes an Ed25519, an X25519 or a P-256 key pair from its secret bytes, asynchronously: `Keypair::from_secret(secret_bytes)` run when the operation is initiated, its result posted to `completion_executor`. - async_generate: Makes an Ed25519, an X25519 or a P-256 key pair from a fresh secret, asynchronously: `Keypair::generate()` run when the operation is initiated (the system's generator is drawn from then), its result posted to `completion_executor`. - async_sign: Signs with an Ed25519 or a P-256 key pair, asynchronously: `key_pair.sign(signing_scheme, signing_input)` run when the operation is initiated, its result posted to `completion_executor`. - async_verify: Verifies a P-256 ECDSA signature over SHA-256 under a verification policy, asynchronously: `crypto::verify(crypto::signing_schemes::ecdsa_sha256, verification_policy, ...)` run when the operation is initiated, its result posted to `completion_executor`; the message is hashed with SHA-256, as WebCrypto's ECDSA with SHA-256 verifies, or `crypto::signing_schemes::prehashed{digest}` gives its digest. - async_agree: Agrees a shared secret, asynchronously: `key_pair.agree(peer_public_key)` run when the operation is initiated, its result posted to `completion_executor`: X25519's (RFC 7748), or P-256's ECDH raw x coordinate, as WebCrypto's `deriveBits` with ECDH on P-256 gives its 256 bits. - [error](https://webcpporg.github.io/libs/crypto/#crypto-errors): Every failure's value, fixed and never reused, in the category webcpp.crypto. Header . Backends: no backend (/webcpp/crypto//crypto) on native, wasip2, wasip3, emscripten. Use it to: tell a forged message from a malformed one. - error_category: The category of every error. - make_error_code: Makes the `error_code` of an error, as its implicit conversion does. - message_of: The message of a value of error, or the unknown message for a value it does not define. - [openssl_memory_resource, backends::openssl::install_memory_functions](https://webcpporg.github.io/libs/crypto/#crypto-memory-openssl): With `WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE`, natively, every allocation of OpenSSL's in the process goes to a `std::pmr::memory_resource` the program defines. Header . Backends: OpenSSL (/webcpp/crypto//openssl) on native. Use it to: keep every allocation of OpenSSL's in the program's arena. - openssl_memory_resource: The resource every allocation of OpenSSL's in the process goes to, from any thread: declared by `` under `WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE` and defined once by the program. - install_memory_functions: Installs webcpp's adapter as OpenSSL's memory functions, so that every allocation of OpenSSL's in the process goes to `openssl_memory_resource()`. ## webcpp::crypto::ciphers - [ciphers::aes128, aes192, aes256](https://webcpporg.github.io/libs/crypto/#crypto-block): The AES block cipher on whole 16-byte blocks: a building block, not encryption of data. Header , . Backends: OpenSSL (/webcpp/crypto//openssl) on native, wasip2, wasip3; ctaes (/third_party//ctaes) on native, wasip2, wasip3, emscripten. Default: OpenSSL on native, Clang and GCC; OpenSSL on native, MSVC; ctaes on wasip2, wasip3, emscripten. Use it to: turn a sequential id into an opaque reference. - from_key: Expands a key. - encrypt: Encrypts whole blocks in place. - decrypt: Decrypts whole blocks in place. - [ciphers::gcm, aes128_gcm, aes256_gcm](https://webcpporg.github.io/libs/crypto/#crypto-gcm): AES-GCM, authenticated encryption in place with an incremental seal and an incremental open in two passes. Header , . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten; OpenSSL (/webcpp/crypto//openssl) on native, wasip2, wasip3. Default: OpenSSL on native, Clang and GCC; OpenSSL on native, MSVC; BearSSL on wasip2, wasip3; BearSSL on emscripten. Use it to: encrypt a database field, a cookie or a token at rest. - from_key: Expands a key into a new object. - async_from_key: Expands a key into a new object, asynchronously: `from_key` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_from_key(executor, key_bytes, token)`, which calls it. - seal: Seals a message in place: `data_bytes` holds the plaintext before and the ciphertext after. - async_seal: Seals a message in place, asynchronously: `seal` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::aead::async_seal(executor, aead, nonce_bytes, associated_data_bytes, data_bytes, token)`, which calls it. - open: Opens a message in place: `data_bytes` holds the ciphertext before and the plaintext after. - async_open: Opens a message in place, asynchronously: `open` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::aead::async_open`, which calls it with the executor and the object before this function's arguments. - begin_seal: Begins an incremental seal: the nonce and the whole associated data first, both read before this returns. - update: Encrypts the next piece of the message in place. - result: Ends the message with its tag. - begin_open: Begins an incremental open: the nonce and the whole associated data first, both read before this returns, the nonce kept for the second pass. - authenticate: The first pass: authenticates the next piece of the ciphertext, which is read and never written. - verify: Ends the first pass: checks the tag of the pieces authenticated, and on a match begins the second pass. - decrypt: The second pass: decrypts the next piece in place, the bytes `authenticate` read in the same place of the message. - end_open: Ends the open, whose message's state is wiped. - [ciphers::chacha20_poly1305](https://webcpporg.github.io/libs/crypto/#crypto-chacha): RFC 8439's ChaCha20-Poly1305, authenticated encryption in place with a 12-byte nonce. Header , . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten; OpenSSL (/webcpp/crypto//openssl) on native, wasip2, wasip3; libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. - from_key: Makes an object under a key. - async_from_key: Makes an object under a key, asynchronously: `from_key` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_from_key(executor, key_bytes, token)`, which calls it. - seal: Seals a message in place: `data_bytes` holds the plaintext before and the ciphertext after. - async_seal: Seals a message in place, asynchronously: `seal` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::aead::async_seal(executor, cipher, nonce_bytes, associated_data_bytes, data_bytes, token)`, which calls it. - open: Opens a message in place: `data_bytes` holds the ciphertext before and the plaintext after. - async_open: Opens a message in place, asynchronously: `open` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::aead::async_open`, which calls it with the executor and the object before this function's arguments. - [ciphers::chacha20_poly1305_original](https://webcpporg.github.io/libs/crypto/#crypto-chacha): The original ChaCha20-Poly1305, authenticated encryption in place with an 8-byte nonce a counter numbers. Header , . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. - from_key: Makes an object under a key. - async_from_key: Makes an object under a key, asynchronously: `from_key` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_from_key(executor, key_bytes, token)`, which calls it. - seal: Seals a message in place: `data_bytes` holds the plaintext before and the ciphertext after. - async_seal: Seals a message in place, asynchronously: `seal` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::aead::async_seal(executor, cipher, nonce_bytes, associated_data_bytes, data_bytes, token)`, which calls it. - open: Opens a message in place: `data_bytes` holds the ciphertext before and the plaintext after. - async_open: Opens a message in place, asynchronously: `open` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::aead::async_open`, which calls it with the executor and the object before this function's arguments. - [ciphers::xchacha20_poly1305](https://webcpporg.github.io/libs/crypto/#crypto-chacha): XChaCha20-Poly1305, authenticated encryption in place with a 24-byte nonce safe to draw at random. Header , . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: encrypt messages in a chat app, on the web and natively, each with a random nonce. - from_key: Makes an object under a key. - async_from_key: Makes an object under a key, asynchronously: `from_key` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_from_key(executor, key_bytes, token)`, which calls it. - seal: Seals a message in place: `data_bytes` holds the plaintext before and the ciphertext after. - async_seal: Seals a message in place, asynchronously: `seal` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::aead::async_seal(executor, cipher, nonce_bytes, associated_data_bytes, data_bytes, token)`, which calls it. - open: Opens a message in place: `data_bytes` holds the ciphertext before and the plaintext after. - async_open: Opens a message in place, asynchronously: `open` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::aead::async_open`, which calls it with the executor and the object before this function's arguments. - [ciphers::chacha20](https://webcpporg.github.io/libs/crypto/#crypto-chacha20): RFC 8439's ChaCha20, the raw stream cipher: the keystream XORed into the data in place from a 32-bit block counter the caller names, with a 12-byte nonce and no authentication, for a protocol that brings its own (NIP-44). Header , . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten; OpenSSL (/webcpp/crypto//openssl) on native, wasip2, wasip3; libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: apply ChaCha20's keystream for a protocol that fixes its own construction (NIP-44). - from_key: Makes an object under a key. - async_from_key: Makes an object under a key, asynchronously: `from_key` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_from_key(executor, key_bytes, token)`, which calls it. - apply_keystream: XORs RFC 8439 section 2.4's keystream into `data_bytes`, in place, its first block the block of counter `initial_counter`: encryption and decryption are this one call. - async_apply_keystream: XORs the keystream into `data_bytes`, in place, asynchronously: `apply_keystream` run when the operation is initiated, its result posted to `completion_executor`; the member form of `unauthenticated_encryption::async_apply_keystream(executor, cipher, nonce_bytes, data_bytes, initial_counter, token)`, which calls it. ## webcpp::crypto::secret_key_encryption - [secret_key_encryption::secretbox](https://webcpporg.github.io/libs/crypto/#crypto-secretbox): NaCl's and libsodium's `crypto_secretbox`, XSalsa20-Poly1305: authenticated encryption in place under a 32-byte key, with a 24-byte nonce safe to draw at random and no associated data, an `authenticated_box` whose free forms are `secret_key_encryption::seal` and `open`. Header , . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: encrypt a record or a file under one secret key, each with a random nonce, in libsodium's secret-box format. - from_key: Makes a box under a key. - async_from_key: Makes a box under a key, asynchronously: `from_key` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_from_key(executor, key_bytes, token)`, which calls it. - seal: Seals a message in place: `data_bytes` holds the plaintext before and the ciphertext after. - async_seal: Seals a message in place, asynchronously: `seal` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::async_seal(executor, box, nonce_bytes, data_bytes, token)`, which calls it. - open: Opens a message in place: `data_bytes` holds the ciphertext before and the plaintext after. - async_open: Opens a message in place, asynchronously: `open` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::async_open(executor, box, nonce_bytes, data_bytes, tag_bytes, token)`, which calls it. - [secret_key_encryption::secretbox_xchacha20poly1305](https://webcpporg.github.io/libs/crypto/#crypto-secretbox): libsodium's `crypto_secretbox_xchacha20poly1305`: the secret box with XChaCha20 in place of XSalsa20. Header , . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: encrypt a record or a file under one secret key, each with a random nonce, in libsodium's secret-box format. - from_key: Makes a box under a key. - async_from_key: Makes a box under a key, asynchronously: `from_key` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_from_key(executor, key_bytes, token)`, which calls it. - seal: Seals a message in place: `data_bytes` holds the plaintext before and the ciphertext after. - async_seal: Seals a message in place, asynchronously: `seal` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::async_seal(executor, box, nonce_bytes, data_bytes, token)`, which calls it. - open: Opens a message in place: `data_bytes` holds the ciphertext before and the plaintext after. - async_open: Opens a message in place, asynchronously: `open` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::async_open(executor, box, nonce_bytes, data_bytes, tag_bytes, token)`, which calls it. ## webcpp::crypto::curves - [curves::secp256k1::keypair](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1): A secp256k1 key pair: made from its 32-byte secret key, which `secret()` gives back, or drawn from `random_device`; its public key, a `curves::secp256k1::public_key`, and its x-only key, a `curves::secp256k1::xonly_public_key`, with the parity of its y; `sign(crypto::signing_schemes::schnorr, message)` (BIP 340, its auxiliary randomness drawn or given), `sign(crypto::signing_schemes::ecdsa_sha256, input)` (hedged too), `sign(crypto::signing_schemes::ecdsa_sha256_recoverable, input)` and Ethereum's `sign(crypto::signing_schemes::ecdsa_keccak256_recoverable, input)`, the input a message hashed inside (with Keccak-256 for Ethereum's, which needs `/third_party//xkcp`) or `crypto::signing_schemes::prehashed{digest}`; ECDH in three forms, `agree` (the shared x), `agree_hashed` and `agree_point`, each with a peer's `public_key`; and the x-only tweak. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. - from_secret: Makes a key pair from a secret key, as a program that kept `secret()` loads it again. - generate: Generates a key pair from the system's source: draws a 32-byte secret key from the system's generator and makes the key pair from it, drawing again when the 32 bytes are not a secret key (zero or not below the group order, which happens with a probability near 2^-128), at most `max_draws` (four) draws in all. - public_key: The public key, which `crypto::verify` takes, and `agree` as a peer's; its `compressed()`, `uncompressed()` and `xonly()` give its bytes. - xonly_public_key: The key pair's BIP340 x-only key (`secp256k1_keypair_xonly_pub`), which a Schnorr signature of the key pair verifies under: `public_key()->xonly()`. - xonly_parity: The parity of the y of the key pair's public key (`secp256k1_keypair_xonly_pub`), which the x-only key drops: what BIP341's control block and `tweaked_xonly_key::parity` carry. - secret: The secret key (`secp256k1_keypair_sec`), for a program that keeps the key pair and makes it again with `from_secret`, or exports it. - sign: Signs with one of secp256k1's four schemes: ECDSA over SHA-256 with RFC 6979's nonce, low S, as libsecp256k1 signs; the same with the recovery id; Ethereum's recoverable ECDSA, the same over Keccak-256 (EIP-2's low S); or BIP340's Schnorr, with 32 bytes of auxiliary randomness drawn from the system's generator, as BIP340 recommends. - agree: ECDH with a peer's public key: the shared point's x coordinate, unhashed, as P-256's `agree` and WebCrypto's `deriveBits` give it, and what Wycheproof's ECDH vectors, Nostr's NIP-44 and other protocols that hash it themselves take. - agree_hashed: ECDH with a peer's public key, hashed as libsecp256k1 hashes it by default: the SHA-256 of the compressed shared point. - agree_point: ECDH with a peer's public key: the shared point itself, SEC1 uncompressed (0x04, x, y). - tweak_xonly: Tweaks this key pair in place by a 32-byte tweak t: its x-only public key P becomes P + t*G, and its secret key follows (`secp256k1_keypair_xonly_tweak_add`). - [crypto::verify, curves::secp256k1::recover](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1): Verifies a BIP 340 signature against a `curves::secp256k1::public_key` or a `curves::secp256k1::xonly_public_key`, `crypto::verify(crypto::signing_schemes::schnorr, crypto::verification_policies::canonical, signature, message, key)`, or an ECDSA one against a `curves::secp256k1::public_key`, `crypto::verify(crypto::signing_schemes::ecdsa_sha256, crypto::verification_policies::low_s_only, signature, input, public_key)`, refusing a high S; `recover(crypto::signing_schemes::ecdsa_sha256_recoverable, crypto::verification_policies::low_s_only, signature, input)` and `recover(crypto::signing_schemes::ecdsa_keccak256_recoverable, crypto::verification_policies::low_s_only, signature, input)` give the signer's key, refusing a high S before recovery. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. - verify: Verifies an ECDSA signature over SHA-256 with a secp256k1 public key, with libsecp256k1's static context, which allocates nothing, after its self-test. - 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::ecdsa_signature::is_low_s, low_s; recoverable_signature::is_low_s, low_s](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1): Low S as the members `is_low_s()` and `low_s()` of the ECDSA signatures, a recoverable one's `low_s()` flipping its recovery id; ECDSA's strict DER (BIP66) is `formats::der`'s (secp256k1's formats). Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. - is_low_s: Whether the signature is in its low-S form: 0 < r < n and 0 < s <= (n - 1) / 2, n the group order. - low_s: The signature's low-S form: s replaced by n - s when it is above (n - 1) / 2; a low-S signature is returned as it is. - [curves::secp256k1::xonly_public_key, tweaked_xonly_key](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-xonly): BIP 340's x-only public key, typed and never unset: made from its 32 bytes (the point of even y) by `from_bytes`, a public key's `xonly()`, a key pair's `xonly_public_key()` or MuSig2's aggregate key; `bytes()`, `==` and `<=>` as its bytes; `verify(crypto::signing_schemes::schnorr, crypto::verification_policies::canonical, signature, message)`; `tweak_add(tweak)`, BIP 341's output key and its parity, a `tweaked_xonly_key`, and `verify_tweak(tweak, tweaked)`, which checks one. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. - from_bytes: Makes an x-only key from its 32 bytes, BIP340's lift_x: the point of even y whose x they are. - bytes: The key's 32 bytes, x big-endian, as BIP340 serializes it. - verify: Verifies a BIP340 signature with this key under `crypto::verification_policies::canonical`, BIP340's verification: `crypto::verify(crypto::signing_schemes::schnorr, crypto::verification_policies::canonical, signature_value, message_input, key)`, of which it is the member form, with the same work and results, with libsecp256k1's static context, which allocates nothing. - tweak_add: Tweaks this key by a 32-byte tweak t: the x-only key of P + t*G and the parity of its y (`secp256k1_xonly_pubkey_tweak_add`, then `secp256k1_xonly_pubkey_from_pubkey`), with libsecp256k1's static context, which allocates nothing; BIP341's output key of an internal key, its tweak the caller's (`tagged_hash("TapTweak", ...)`). - verify_tweak: Whether `tweaked` is this key tweaked by `tweak_bytes`, its parity included (`secp256k1_xonly_pubkey_tweak_add_check`), with libsecp256k1's static context, which allocates nothing: a taproot output key checked against its internal key. - [curves::ed25519::keypair](https://webcpporg.github.io/libs/crypto/#crypto-ed25519): An Ed25519 key pair (RFC 8032): made from a 32-byte seed, which `secret()` gives back, or drawn from `random_device`; its public key, a `curves::ed25519::public_key`; 64-byte signatures of a message with `sign(crypto::signing_schemes::eddsa, message)`, and of a message's SHA-512 with `sign(crypto::signing_schemes::eddsa_prehashed, crypto::signing_schemes::prehashed{digest})` (Ed25519ph, native backends); and its X25519 key pair, `to_x25519()`. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: sign and verify with Ed25519 (RFC 8032), or with Ed25519ph a message's SHA-512 or a large message in pieces. - from_secret: Makes a key pair from a seed, RFC 8032's secret key. - generate: Makes a key pair from a fresh seed, drawn from the system's generator. - async_from_secret: Makes a key pair from a seed, asynchronously: `from_secret` run when the operation is initiated, its result posted to `completion_executor`; the member form of the free `crypto::async_from_secret`, with the same arguments, which calls it. - async_generate: Makes a key pair from a fresh seed, asynchronously: `generate` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_generate(completion_executor, completion_token)`, which calls it. - public_key: The public key, which `crypto::verify` takes. - secret: The seed, RFC 8032's secret key, for a program that keeps the key pair and makes it again with `from_secret`, or exports it (PKCS#8 holds the seed alone). - sign: Signs with Ed25519 (RFC 8032): under `crypto::signing_schemes::eddsa`, the pure form, a message, the message itself, of any length, empty included, with no digest taken; under `crypto::signing_schemes::eddsa_prehashed`, Ed25519ph, the 64 bytes of a message's SHA-512, `crypto::signing_schemes::prehashed{digest}` (a message given in pieces is prehashed_signer's). - async_sign: Signs with Ed25519, asynchronously: `sign` run when the operation is initiated, its result posted to `completion_executor`; the member form of the free `crypto::async_sign`, with the same arguments and this key pair, which calls it. - to_x25519: The X25519 key pair of this key pair, on its backend: its secret key the clamped first half of the seed's SHA-512, its public key `public_key()->to_x25519()`, so that one seed serves for signatures and for agreement. - [curves::ed25519::public_key](https://webcpporg.github.io/libs/crypto/#crypto-ed25519): An Ed25519 public key, its 32 bytes, which `crypto::verify(crypto::signing_schemes::eddsa, ...)` takes: made from bytes with only their size checked, since whether the point is canonical and not of small order is the verification policy's judgment; and its X25519 public key, `to_x25519()`. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. - from_bytes: Makes a public key from its 32 bytes, as RFC 8032 encodes it and WebCrypto's `exportKey("raw")` writes it. - bytes: The key's bytes. - verify: Verifies an Ed25519 signature (RFC 8032's pure Ed25519) with this key under a verification policy (design D9): `crypto::verify(crypto::signing_schemes::eddsa, crypto::verification_policies::canonical, ...)`, of which it is the member form, with the same work, results and refusals. - async_verify: Verifies an Ed25519 signature with this key under a verification policy, asynchronously: `verify` run when the operation is initiated, its result posted to `completion_executor`; the member form of the free `crypto::async_verify`, with the same arguments and this key, which calls it. - to_x25519: The X25519 public key of this key (the birational map from edwards25519 to Curve25519), so that a peer known by its Ed25519 key is agreed with by an X25519 key pair's `agree`; the key pair's own side is `basic_keypair::to_x25519`. - [crypto::verify](https://webcpporg.github.io/libs/crypto/#crypto-ed25519): Verifies an Ed25519 signature with a public key alone, `crypto::verify(crypto::signing_schemes::eddsa, crypto::verification_policies::canonical, signature, message, public_key)`, or an Ed25519ph one, `crypto::verify(crypto::signing_schemes::eddsa_prehashed, crypto::verification_policies::canonical, signature, crypto::signing_schemes::prehashed{digest}, public_key)`, under the policy `canonical`, the only one Ed25519 takes: libsodium's rules, which refuse non-canonical encodings and keys of small order. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: sign and verify with Ed25519 (RFC 8032), or with Ed25519ph a message's SHA-512 or a large message in pieces. - verify: Verifies an Ed25519 signature (RFC 8032's pure Ed25519) under a verification policy (design D9), `crypto::verification_policies::canonical`: libsodium's rules. - [curves::ed25519::prehashed_signer](https://webcpporg.github.io/libs/crypto/#crypto-ed25519): Ed25519ph (RFC 8032), the streaming form of the scheme `crypto::signing_schemes::eddsa_prehashed`: a message given in pieces, hashed with SHA-512, then signed with a key pair or verified with `verify(crypto::verification_policies::canonical, signature, public_key)`, the signature `sign(crypto::signing_schemes::eddsa_prehashed, crypto::signing_schemes::prehashed{digest})` gives of its SHA-512. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: sign and verify with Ed25519 (RFC 8032), or with Ed25519ph a message's SHA-512 or a large message in pieces. - begin: Begins a message. - update: Adds a piece of the message. - sign: Signs the message given, which it ends. - verify: Verifies a signature of the message given, which it ends, under a verification policy, `crypto::verification_policies::canonical`, libsodium's rules, the only one Ed25519ph takes (design D9). - [crypto::to_x25519](https://webcpporg.github.io/libs/crypto/#crypto-x25519): Converts an Ed25519 key pair to an X25519 key pair of its backend, and an Ed25519 public key to an X25519 public key (the birational map), refusing a key of small order or not canonical with `invalid_public_key`: the free form of both members, `to_x25519()`, synchronous on every backend. 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. - to_x25519: Converts an Ed25519 key pair or public key to X25519's, `converted_value.to_x25519()`: the free form of the member, which it calls, with its results and its errors. - [curves::x25519::keypair](https://webcpporg.github.io/libs/crypto/#crypto-x25519): An X25519 key pair (RFC 7748): made from its 32-byte secret key, which `secret()` gives back, or drawn from `random_device`; its public key, a `curves::x25519::public_key`, computed once; and `agree(peer)`, the 32-byte shared secret with a peer's public key, a `secure::secret<32>`, refused with `weak_public_key` when it is all zero (a peer key of low order). 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_secret: Makes a key pair from its secret key; its public key is computed here, once. - generate: Makes a key pair from a fresh secret key, drawn from the system's generator. - async_from_secret: Makes a key pair from its secret key, asynchronously: `from_secret` run when the operation is initiated, its result posted to `completion_executor`; the member form of the free `crypto::async_from_secret`, with the same arguments, which calls it. - async_generate: Makes a key pair from a fresh secret key, asynchronously: `generate` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_generate(completion_executor, completion_token)`, which calls it. - public_key: The public key, which a peer's `agree` takes: a copy of the one computed when the key pair was made. - secret: The secret key, for a program that keeps the key pair and makes it again with `from_secret`, or exports it: a copy. - agree: The shared secret of this key pair and a peer's public key (RFC 7748's X25519), refused when it is all zero, on every backend. - async_agree: The shared secret of this key pair and 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. - [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. - [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. - from_bytes: Makes a public key from SEC1 bytes, 33 with prefix 02 or 03, or 65 with prefix 04. - compressed: The key, SEC1 compressed: 02 or 03 by the parity of y, then x; Bitcoin's form. - uncompressed: The key, SEC1 uncompressed: 04, x, y; Ethereum's form, and OpenSSL's default. - xonly: The key's BIP340 x-only key, its x coordinate, which `crypto::verify(crypto::signing_schemes::schnorr, ...)` takes as the key itself does; the parity of y is dropped (`secp256k1_xonly_pubkey_from_pubkey`). - verify: Verifies a signature with this key under a verification policy, of ECDSA over SHA-256 or of BIP340's Schnorr (design R22, D9): `crypto::verify(signing_scheme, verification_policy, signature_value, signing_input, key)`, of which it is the member form, with the same work, results and refusals. - [curves::secp256k1::secret_key_tweak_add, secret_key_tweak_mul, secret_key_negate, public_key_tweak_add, public_key_tweak_mul, public_key_negate, public_key_combine](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-tweaks): A secret or public key plus or times a tweak, its negation, and the sum of public keys. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. Use it to: derive child keys for a watch-only wallet (BIP 32's derivation is a domain library's). - secret_key_tweak_add: A secret key plus a tweak modulo the group order (`secp256k1_ec_seckey_tweak_add`). - secret_key_tweak_mul: A secret key times a tweak modulo the group order (`secp256k1_ec_seckey_tweak_mul`). - secret_key_negate: The group order minus a secret key (`secp256k1_ec_seckey_negate`). - public_key_tweak_add: A public key P plus t times the generator (`secp256k1_ec_pubkey_tweak_add`). - 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. - [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 `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. - ellswift_create: The encoding of a key pair's public key, made from its secret with the key pair's context (`secp256k1_ellswift_create`), so that the public key is never computed apart. - ellswift_xdh: The shared secret of an ElligatorSwift key exchange, hashed as BIP 324 hashes it (`secp256k1_ellswift_xdh` with `secp256k1_ellswift_xdh_hash_function_bip324`): a tagged hash of both encodings and the shared point's x coordinate. - [curves::secp256k1::musig::aggregate_key, key_aggregate, generate_nonce, nonce_aggregate, nonce_process, partial_sign, partial_signature_verify, partial_signature_aggregate, parse_public_nonce, serialize_public_nonce, parse_aggregate_nonce, serialize_aggregate_nonce, parse_partial_signature, serialize_partial_signature](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-musig): BIP 327's MuSig2: many signers' keys aggregated into one, and their two rounds into one BIP 340 signature, each secret nonce consumed by its partial signature. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. Use it to: require two devices to sign for a wallet, with one ordinary signature on the chain. - xonly_public_key: The BIP 340 x-only key the final signature verifies under (`secp256k1_musig_pubkey_get`, then `secp256k1_xonly_pubkey_from_pubkey`), which `crypto::verify` of `crypto::signing_schemes::schnorr` takes. - full_public_key: The aggregate as a full point (`secp256k1_musig_pubkey_get`), which a plain tweak acts on. - tweak_add: Adds t times G as a plain tweak (BIP 327's ApplyTweak, is_xonly false, `secp256k1_musig_pubkey_ec_tweak_add`). - tweak_xonly_add: Adds t times G as an x-only tweak (BIP 327's ApplyTweak, is_xonly true, `secp256k1_musig_pubkey_xonly_tweak_add`), as Taproot tweaks an output key. - workspace_size: The workspace `key_aggregate`, `nonce_aggregate` and `partial_signature_aggregate` need for `item_count` items, in bytes. - key_aggregate: Aggregates the signers' keys in the order given (`secp256k1_musig_pubkey_agg`). - generate_nonce: A fresh nonce for one signature (`secp256k1_musig_nonce_gen`), its 32 bytes of session randomness drawn inside from crypto's random device, with the key pair's secret, its public key and the aggregate key. - nonce_aggregate: Sums the signers' public nonces (`secp256k1_musig_nonce_agg`), as one of them or a coordinator does once every public nonce is in. - nonce_process: The session of one message (`secp256k1_musig_nonce_process`): the final nonce and the challenge every signer signs. - partial_sign: This signer's partial signature (`secp256k1_musig_partial_sign`), consuming the nonce: it is taken by value, wiped by the call and destroyed at its end. - partial_signature_verify: Verifies one signer's partial signature (`secp256k1_musig_partial_sig_verify`), which tells a dishonest or faulty signer apart before the signature is aggregated. - partial_signature_aggregate: The BIP 340 signature of the partial signatures (`secp256k1_musig_partial_sig_agg`), which `crypto::verify` of `crypto::signing_schemes::schnorr`, under `crypto::verification_policies::canonical`, checks under `aggregate_key::xonly_public_key()`. - parse_public_nonce: Parses a signer's public nonce (`secp256k1_musig_pubnonce_parse`): two compressed points. - serialize_public_nonce: Serialises a signer's public nonce (`secp256k1_musig_pubnonce_serialize`). - parse_aggregate_nonce: Parses an aggregate nonce (`secp256k1_musig_aggnonce_parse`): two compressed points, either of which may be the point at infinity, written as 33 zero bytes. - serialize_aggregate_nonce: Serialises an aggregate nonce (`secp256k1_musig_aggnonce_serialize`). - parse_partial_signature: Parses a partial signature (`secp256k1_musig_partial_sig_parse`). - serialize_partial_signature: Serialises a partial signature (`secp256k1_musig_partial_sig_serialize`). - [curves::secp256k1::silent_payments::create_outputs, create_label, create_labeled_spend_key, make_previous_outputs_summary, scan_outputs, send_workspace_size, summary_workspace_size, scan_workspace_size](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-silent-payments): BIP 352's silent payments: the sender's outputs to a reusable address of a scan and a spend key, the recipient's labels, and its scan of a transaction's outputs. Header . Backends: libsecp256k1 (/third_party//secp256k1) on native, wasip2, wasip3, emscripten. Default: libsecp256k1 on native, wasip2, wasip3, emscripten. Use it to: publish one donation address that never shows two payments as related (BIP 352's addresses and transactions are a domain library's). - send_workspace_size: The workspace `create_outputs` needs, in bytes. - summary_workspace_size: The workspace `make_previous_outputs_summary` needs, in bytes. - scan_workspace_size: The workspace `scan_outputs` needs, in bytes. - create_outputs: The sender's outputs (`secp256k1_silentpayments_sender_create_outputs`): one x-only key per recipient, in the recipients' order, from the secret keys of the transaction's eligible inputs, Taproot inputs (their keys negated for an odd y, as BIP 352 says) apart from the others, and the smallest outpoint, which the caller determines. - create_label: The `m`-th label of a scan key (`secp256k1_silentpayments_recipient_label_create`); 0 is BIP 352's change label. - create_labeled_spend_key: The spend key of a labelled address (`..._create_labeled_spend_pubkey`). - make_previous_outputs_summary: The previous outputs' summary (`secp256k1_silentpayments_recipient_prevouts_summary_create`) from the eligible inputs' public keys. - scan_outputs: Scans a transaction's x-only outputs (`..._recipient_scan_outputs`) with the scan key pair and the unlabelled spend key; `label_lookup` is called with a candidate label's 33 bytes and returns a span over its tweak, in bytes the lookup owns, or `std::nullopt`, noexcept (no labels: the default). - [curves::p256::keypair](https://webcpporg.github.io/libs/crypto/#crypto-p256-keys): A P-256 key pair: made from its 32-byte scalar, refused unless 0 < d < n, or drawn from `random_device`; its validated public key and its scalar, `secret()`, a `secure::secret<32>`. Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten. Default: BearSSL on native, wasip2, wasip3, emscripten. Use it to: 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. - from_secret: Makes a key pair from its secret scalar, as a program that kept `secret()` loads it again. - generate: Makes a key pair from a fresh scalar, drawn from the system's generator until it is below the group order (a draw is refused about once in 2^32). - async_from_secret: Makes a key pair from its secret scalar, asynchronously: `from_secret` run when the operation is initiated, its result posted to `completion_executor`; the member form of the free `crypto::async_from_secret`, with the same arguments, which calls it. - async_generate: Makes a key pair from a fresh scalar, asynchronously: `generate` run when the operation is initiated, its result posted to `completion_executor`; the member form of `crypto::async_generate(completion_executor, completion_token)`, which calls it. - public_key: The public key, computed when the key pair was made. - secret: The secret scalar, for a program that keeps the key pair and makes it again with `from_secret`. - [curves::p256::public_key](https://webcpporg.github.io/libs/crypto/#crypto-p256-keys): A validated P-256 public key, made from SEC 1's 65-byte uncompressed point, its 33-byte compressed point (y computed by BearSSL's internal big integers, a square root modulo p) or its two coordinates; a point off the curve, a coordinate not below p, an x that is no point's or another form is `invalid_public_key`; `uncompressed()` and `compressed()` give its point back. Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten. Default: BearSSL on native, wasip2, wasip3, emscripten. Use it to: 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. - from_bytes: Makes a public key from SEC 1's uncompressed point, as WebCrypto's `exportKey("raw")` writes it, or from its compressed point, on every backend. - from_coordinates: Makes a public key from its two coordinates, as a JSON Web Key's `x` and `y` carry them. - uncompressed: The point, as WebCrypto's `exportKey("raw")` writes it. - compressed: The compressed point, as OpenSSL's `POINT_CONVERSION_COMPRESSED` writes it: copied from the point the key holds, with no backend call. - verify: Verifies an ECDSA signature over SHA-256 with this key under a verification policy: `crypto::verify(crypto::signing_schemes::ecdsa_sha256, verification_policy, signature_value, signing_input, key)`, of which it is the member form, with the same work, results and refusals. - async_verify: Verifies an ECDSA signature over SHA-256 with this key under a verification policy, asynchronously: `verify` run when the operation is initiated, its result posted to `completion_executor`; the member form of the free `crypto::async_verify`, with the same arguments and this key, which calls it. - [curves::p256::keypair::sign; crypto::verify; ecdsa_signature::is_low_s, low_s](https://webcpporg.github.io/libs/crypto/#crypto-p256-ecdsa): ECDSA over SHA-256 with RFC 6979's nonce, `sign(crypto::signing_schemes::ecdsa_sha256, input)` and `crypto::verify(crypto::signing_schemes::ecdsa_sha256, policy, signature, input, public_key)`, the input a message hashed inside or `crypto::signing_schemes::prehashed{digest}`; the signature a `curves::p256::ecdsa_signature`, r and s, signed low S, verified under `crypto::verification_policies::low_s_only` (a high S refused before BearSSL) or `any_s` (WebCrypto's and a passkey's, whatever its S); `is_low_s()` and `low_s()` of the signature, for one made elsewhere; its strict DER is `formats::der`'s (P-256's formats). Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten. Default: BearSSL on native, wasip2, wasip3, emscripten. Use it to: 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. - sign: Signs with ECDSA over SHA-256 and RFC 6979's deterministic nonce (no random number is drawn: the same key and input give the same signature), S always low, at most (n - 1) / 2, as libsecp256k1 signs: the signature is normalized after it is made. - async_sign: Signs with ECDSA over SHA-256, asynchronously: `sign` run when the operation is initiated, its result posted to `completion_executor`; the member form of the free `crypto::async_sign`, with the same arguments and this key pair, which calls it. - verify: Verifies an ECDSA signature over SHA-256 with a P-256 public key under a verification policy. - is_low_s: Whether the signature is in its low-S form: 0 < r < n and 0 < s <= (n - 1) / 2, n the group order. - low_s: The signature's low-S form: s replaced by n - s when it is above (n - 1) / 2; a low-S signature is returned as it is. - [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. - [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 - [public_key_encryption::box](https://webcpporg.github.io/libs/crypto/#crypto-box): NaCl's and libsodium's `crypto_box`: X25519 agrees a key once per pair of keys, from the own X25519 key pair and the peer's X25519 public key, refusing a peer key of small order, then XSalsa20-Poly1305 seals and opens each message in place, with a 24-byte nonce safe to draw at random. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: send messages between two people who know each other's public keys, as NaCl's and libsodium's crypto_box. - from_keys: Agrees the box's key from the own key pair and the peer's public key. - seal: Seals a message in place: `data_bytes` holds the plaintext before and the ciphertext after. - async_seal: Seals a message in place, asynchronously: `seal` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::async_seal(executor, box, nonce_bytes, data_bytes, token)`, which calls it. - open: Opens a message in place: `data_bytes` holds the ciphertext before and the plaintext after. - async_open: Opens a message in place, asynchronously: `open` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::async_open(executor, box, nonce_bytes, data_bytes, tag_bytes, token)`, which calls it. - [public_key_encryption::box_xchacha20poly1305](https://webcpporg.github.io/libs/crypto/#crypto-box): libsodium's `crypto_box_curve25519xchacha20poly1305`: the box with XChaCha20 in place of XSalsa20. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. - from_keys: Agrees the box's key from the own key pair and the peer's public key. - seal: Seals a message in place: `data_bytes` holds the plaintext before and the ciphertext after. - async_seal: Seals a message in place, asynchronously: `seal` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::async_seal(executor, box, nonce_bytes, data_bytes, token)`, which calls it. - open: Opens a message in place: `data_bytes` holds the ciphertext before and the plaintext after. - async_open: Opens a message in place, asynchronously: `open` run when the operation is initiated, its result posted to `completion_executor`; the member form of `secret_key_encryption::async_open(executor, box, nonce_bytes, data_bytes, tag_bytes, token)`, which calls it. - [public_key_encryption::sealed_box](https://webcpporg.github.io/libs/crypto/#crypto-sealed-box): libsodium's sealed boxes, `crypto_box_seal`: anonymous encryption to an X25519 public key under a key pair drawn per message, 48 bytes longer than the message, which only the recipient's X25519 key pair opens. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: let anyone send a message that only the recipient can read, without saying who sent it (anonymous feedback, a drop box). - seal: Seals `message_bytes` into `output_bytes`, of `message_bytes.size() + overhead` bytes: the drawn public key, the tag, then the ciphertext. - open: Opens `sealed_bytes` into `output_bytes`, of `sealed_bytes.size() - overhead` bytes, with the recipient's key pair. - [public_key_encryption::hpke::suite](https://webcpporg.github.io/libs/crypto/#crypto-hpke): RFC 9180's HPKE composed from crypto's X25519 or P-256 agreement, webcpp::hash's HKDF over pieces and crypto's AES-GCM or ChaCha20-Poly1305: suites as types, the mode following from the arguments (base, psk, auth, auth_psk), contexts that seal and open in order and export secrets, single-shot `seal` and `open`, and `derive_from_seed`, RFC 9180's DeriveKeyPair; every RFC 9180 test vector of those suites passes and OpenSSL's HPKE agrees; no backend of its own, no size cap, no allocation. Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten; libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Use it to: encrypt to a public key as RFC 9180 says, byte for byte, as MLS's Welcome and UpdatePath and Marmot's backups do. - begin_sending: Sets up a sender in base mode (RFC 9180's `SetupBaseS`): an ephemeral key pair drawn from the system's generator, its agreement with the recipient's key, then the key schedule. - begin_receiving: Sets up a recipient in base mode (RFC 9180's `SetupBaseR`): the encapsulated key's agreement with the recipient's key pair, then the key schedule. - seal: Seals one message to a public key in base mode (RFC 9180's `SealBase`): `begin_sending(recipient_public_key, application_info)`, then one `seal`. - open: Opens one message in base mode, RFC 9180's `OpenBase`: `begin_receiving` with the encapsulated key, the recipient's key pair and the application info, then one `open`. - derive_from_seed: Derives a key pair from a seed on the suite's key backend (RFC 9180's `DeriveKeyPair`), as MLS derives every tree key pair: the key encapsulation's `derive_from_seed`. - async_begin_sending: Sets up a sender, asynchronously: `begin_sending` run with the arguments between the executor and the token when the operation is initiated, its result posted to `completion_executor`; the member form of `hpke::async_begin_sending`. - async_begin_receiving: Sets up a recipient, asynchronously: `begin_receiving` run with the arguments between the executor and the token when the operation is initiated, its result posted to `completion_executor`; the member form of `hpke::async_begin_receiving`. - async_seal: Seals one message, asynchronously: the single-shot `seal` run with the arguments between the executor and the token when the operation is initiated, its result posted to `completion_executor`; the member form of `hpke::async_seal`. - async_open: Opens one message, asynchronously: the single-shot `open` run with the arguments between the executor and the token when the operation is initiated, its result posted to `completion_executor`; the member form of `hpke::async_open`. - async_derive_from_seed: Derives a key pair from a seed, asynchronously: `derive_from_seed` run when the operation is initiated, its result posted to `completion_executor`; the member form of `hpke::async_derive_from_seed(completion_executor, seed, completion_token)`. ## webcpp::crypto::unauthenticated_encryption - [async_from_key, unauthenticated_encryption::async_apply_keystream](https://webcpporg.github.io/libs/crypto/#crypto-async): The asynchronous from_key and keystream of ChaCha20, RFC 8439's raw stream cipher, on a native backend, member and free, each completing through a post to the executor the call gives, with any Asio completion token; the free form names no backend, so that a stream cipher of another shape runs through it. Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten; OpenSSL (/webcpp/crypto//openssl) on native, wasip2, wasip3; libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. - async_apply_keystream: XORs a stream cipher's keystream into `data_bytes`, in place, asynchronously: `keyed_cipher.apply_keystream` run when the operation is initiated, its result posted to `completion_executor`. - [unauthenticated_encryption::stream_cipher, unauthenticated_encryption::apply_keystream](https://webcpporg.github.io/libs/crypto/#crypto-unauthenticated-encryption): The raw stream cipher's contract: the concept `stream_cipher`, which reads the algorithm's own key, nonce and block sizes and counter type, and the free form `apply_keystream(cipher, nonce, data, initial_counter)`, which calls the cipher's member; encryption that authenticates nothing, for a protocol that authenticates the nonce and the ciphertext itself. Header . Backends: BearSSL (/third_party//bearssl) on native, wasip2, wasip3, emscripten; OpenSSL (/webcpp/crypto//openssl) on native, wasip2, wasip3; libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. - apply_keystream: XORs the keystream into `data_bytes`, in place, `keyed_cipher.apply_keystream(nonce_bytes, data_bytes, initial_counter)`: the free form of the stream cipher's own `apply_keystream`, which it calls, with its results and its errors. ## webcpp::crypto::single_use_key_authentication - [authenticators::poly1305](https://webcpporg.github.io/libs/crypto/#crypto-poly1305): RFC 8439's Poly1305, the one-time authenticator: a 16-byte tag of a message under a 32-byte key used for one message alone, as a protocol derives it per message; the key enters every call, and no object keeps it. Header . Backends: OpenSSL (/webcpp/crypto//openssl) on native, wasip2, wasip3; libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: authenticate one message under a one-time key. - authenticate: RFC 8439 section 2.5's tag of `message_bytes` under the one-time key `key_bytes`, r and s, r clamped by the algorithm, not by the caller. - verify: Verifies that `tag_bytes` is RFC 8439 section 2.5's tag of `message_bytes` under the one-time key `key_bytes`. - async_authenticate: The tag of a message, asynchronously: `authenticate` run when the operation is initiated, its result posted to `completion_executor`; the member form of `single_use_key_authentication::async_authenticate(executor, key_bytes, message_bytes, token)`, which calls it. - async_verify: Verifies a message's tag, asynchronously: `verify` run when the operation is initiated, its result posted to `completion_executor`; the member form of `single_use_key_authentication::async_verify(executor, key_bytes, message_bytes, tag_bytes, token)`, which calls it. - [single_use_key_authentication::single_use_key_authenticator, single_use_key_authentication::authenticate, single_use_key_authentication::verify](https://webcpporg.github.io/libs/crypto/#crypto-single-use-key-authentication): The single-use key authenticator's contract: the concept `single_use_key_authenticator`, which reads the algorithm's key and tag sizes, its tag type and its static `authenticate` and `verify`, and their free forms `authenticate(key, message)` and `verify(key, message, tag)`, which call them; a key that authenticates one message and never a second. Header . Backends: OpenSSL (/webcpp/crypto//openssl) on native, wasip2, wasip3; libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. - authenticate: Authenticates a message under a key used for it alone, `Authenticator::authenticate(key_bytes, message_bytes)`: the free form of the authenticator's static `authenticate`, which it calls, with its results and its errors. - verify: Verifies a message's tag under a key used for it alone, `Authenticator::verify(key_bytes, message_bytes, tag_bytes)`: the free form of the authenticator's static `verify`, which it calls, with its results and its errors. - [single_use_key_authentication::async_authenticate, single_use_key_authentication::async_verify](https://webcpporg.github.io/libs/crypto/#crypto-async): The asynchronous tag and verification of a single-use key authenticator, Poly1305, on a native backend, the free forms of the static `async_authenticate` and `async_verify`, which they call, each completing through a post to the executor the call gives, with any Asio completion token; WebCrypto has no Poly1305, and no form runs on the browser's backend. Header . Backends: OpenSSL (/webcpp/crypto//openssl) on native, wasip2, wasip3; libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. - async_authenticate: Authenticates a message under a key used for it alone, asynchronously: `Authenticator::authenticate(key_bytes, message_bytes)` run when the operation is initiated, its result posted to `completion_executor`. - async_verify: Verifies a message's tag under a key used for it alone, asynchronously: `Authenticator::verify(key_bytes, message_bytes, tag_bytes)` run when the operation is initiated, its result posted to `completion_executor`. ## webcpp::crypto::key_exchange - [key_exchange::kx_keypair](https://webcpporg.github.io/libs/crypto/#crypto-kx): libsodium's `crypto_kx` on X25519's typed keys: a key pair that holds an X25519 key pair, taken as is, derived from a 32-byte seed as libsodium does (BLAKE2b-256 of the seed) or drawn from `random_device`, and the two session keys of a client or a server, each a `secure::secret<32>`, from the peer's X25519 public key; a peer key of low order is refused with `weak_public_key`. Header . Backends: libsodium (/third_party//sodium) on native, wasip2, wasip3, emscripten. Default: libsodium on native, wasip2, wasip3, emscripten. Use it to: give a client and a server a key for each direction from each other's public key, as libsodium's `crypto_kx` does. - from_keypair: Takes an X25519 key pair as it is: its secret key is the key pair's, as `crypto_kx` uses it, with no hash (`derive_from_seed` hashes its seed). - derive_from_seed: Derives a key pair from a seed, `crypto_kx_seed_keypair`: the same seed gives the same key pair, its secret key BLAKE2b-256 of the seed, not the seed itself. - generate: Makes a key pair from a fresh seed, drawn from the system's generator. - keypair: The X25519 key pair it holds, for a program that keeps its secret key (`secret()`) or agrees with it through X25519 itself. - public_key: The public key, which the peer needs: the X25519 key pair's. - client_session_keys: The client's keys, from the server's public key, `crypto_kx_client_session_keys`. - server_session_keys: The server's keys, from the client's public key, `crypto_kx_server_session_keys`. ## Default backend per family - AES-GCM, native, Clang and GCC: OpenSSL; OpenSSL's assembly uses the CPU's AES and carry-less multiplication instructions on x86-64 and ARMv8 ([OpenSSL's AES assembly for ARMv8 and x86-64](https://github.com/openssl/openssl/tree/master/crypto/aes/asm); [OpenSSL's GHASH and AES-GCM assembly](https://github.com/openssl/openssl/tree/master/crypto/modes/asm)); BearSSL has them on x86 only ([BearSSL, the AES implementations it selects](https://www.bearssl.org/support.html)). OpenSSL whatever the program links: a program that links BearSSL alone names `gcm>`. - AES-GCM, native, MSVC: OpenSSL; the same rule as with Clang and GCC, but here OpenSSL is built no-asm: table-based AES and 4-bit GHASH, no AES-NI, not constant time ([OpenSSL's INSTALL.md, no-asm](https://github.com/openssl/openssl/blob/master/INSTALL.md)); where timing attacks matter, link BearSSL and name `gcm>`. - AES-GCM, wasip2, wasip3: BearSSL; constant time by design ([BearSSL, constant-time crypto](https://www.bearssl.org/constanttime.html)), where OpenSSL is built no-asm and its AES and GHASH are table-based ([OpenSSL's INSTALL.md, no-asm](https://github.com/openssl/openssl/blob/master/INSTALL.md)). BearSSL whatever the program links: a program that links OpenSSL alone names `gcm>`. - AES-GCM, emscripten: BearSSL; the only AES-GCM backend there: webcpp does not build OpenSSL for Emscripten. - AES block cipher, native, Clang and GCC: OpenSSL; OpenSSL's assembly uses the CPU's AES instructions on x86-64 and ARMv8 ([OpenSSL's AES assembly for ARMv8 and x86-64](https://github.com/openssl/openssl/tree/master/crypto/aes/asm)); ctaes is bitsliced, constant time and several times slower. OpenSSL whatever the program links: a program that links ctaes alone names `aes256`. - AES block cipher, native, MSVC: OpenSSL; the same rule as with Clang and GCC, but here OpenSSL is built no-asm: table-based AES, no AES-NI, not constant time ([OpenSSL's INSTALL.md, no-asm](https://github.com/openssl/openssl/blob/master/INSTALL.md)); where timing attacks matter, link ctaes and name `aes256`. - AES block cipher, wasip2, wasip3, emscripten: ctaes; constant time by design ([ctaes, constant-time AES](https://github.com/bitcoin/bitcoin/tree/master/src/crypto/ctaes)), where OpenSSL is built no-asm and its AES is table-based ([OpenSSL's INSTALL.md, no-asm](https://github.com/openssl/openssl/blob/master/INSTALL.md)), and webcpp does not build OpenSSL for Emscripten. - secp256k1, native, wasip2, wasip3, emscripten: libsecp256k1; the only backend ([libsecp256k1's README](https://github.com/bitcoin-core/secp256k1#readme)). - Ed25519, native, wasip2, wasip3, emscripten: libsodium; the only backend in this version, on every target, and its verification is the policy `canonical` ([libsodium's documentation, public-key signatures](https://doc.libsodium.org/public-key_cryptography/public-key_signatures); [RFC 8032, the Edwards-curve Digital Signature Algorithm (EdDSA)](https://www.rfc-editor.org/rfc/rfc8032)). - X25519, native, wasip2, wasip3, emscripten: libsodium; the only backend in this version, on every target; it refuses an all-zero shared secret itself ([RFC 7748, Elliptic Curves for Security (X25519 and X448)](https://www.rfc-editor.org/rfc/rfc7748)). - crypto_kx, native, wasip2, wasip3, emscripten: libsodium; the only backend that has it: libsodium's own construction, whose session keys only another `crypto_kx` derives ([libsodium's documentation, key exchange](https://doc.libsodium.org/key_exchange)). - ChaCha20-Poly1305, native, wasip2, wasip3, emscripten: libsodium; one library for the three ChaCha20-Poly1305 AEADs on every target, where BearSSL and OpenSSL have RFC 8439's alone ([libsodium's ChaCha20-Poly1305 constructions](https://doc.libsodium.org/secret-key_cryptography/aead/chacha20-poly1305)); a program that links BearSSL or OpenSSL alone names it in the type, as in `chacha20_poly1305`. - original ChaCha20-Poly1305, native, wasip2, wasip3, emscripten: libsodium; the only backend that has it: libsodium's own C code, the same on every target ([libsodium's ChaCha20-Poly1305 constructions](https://doc.libsodium.org/secret-key_cryptography/aead/chacha20-poly1305)). - XChaCha20-Poly1305, native, wasip2, wasip3, emscripten: libsodium; the only backend that has it: libsodium's own C code, the same on every target ([libsodium's ChaCha20-Poly1305 constructions](https://doc.libsodium.org/secret-key_cryptography/aead/chacha20-poly1305)). - ChaCha20, native, wasip2, wasip3, emscripten: libsodium; ChaCha20-Poly1305's default, so that a program that uses both links one library; libsodium's own C code, constant time, the same on every target ([libsodium's ChaCha20 stream cipher, crypto_stream_chacha20_ietf_xor_ic](https://doc.libsodium.org/advanced/stream_ciphers/chacha20)). - Poly1305, native, wasip2, wasip3, emscripten: libsodium; ChaCha20's and ChaCha20-Poly1305's default, so that a program composing them links one library ([libsodium's one-time authentication, crypto_onetimeauth_poly1305](https://doc.libsodium.org/advanced/poly1305)). - XSalsa20-Poly1305 secret box, native, wasip2, wasip3, emscripten: libsodium; the only backend that has it: libsodium's own C code, NaCl's `crypto_secretbox`, the same on every target ([libsodium's documentation, secret-key authenticated encryption](https://doc.libsodium.org/secret-key_cryptography/secretbox)). - XChaCha20-Poly1305 secret box, native, wasip2, wasip3, emscripten: libsodium; the only backend that has it: libsodium's own C code, the same on every target ([libsodium's documentation, secret-key authenticated encryption](https://doc.libsodium.org/secret-key_cryptography/secretbox)). - X25519-XSalsa20-Poly1305 box, native, wasip2, wasip3, emscripten: libsodium; the only backend that has it: libsodium's own C code, NaCl's `crypto_box`, the same on every target ([libsodium's documentation, public-key authenticated encryption](https://doc.libsodium.org/public-key_cryptography/authenticated_encryption)). - X25519-XChaCha20-Poly1305 box, native, wasip2, wasip3, emscripten: libsodium; the only backend that has it: libsodium's own C code, the same on every target ([libsodium's documentation, public-key authenticated encryption](https://doc.libsodium.org/public-key_cryptography/authenticated_encryption)). - X25519-XSalsa20-Poly1305 sealed box, native, wasip2, wasip3, emscripten: libsodium; the only backend that has it: libsodium's own C code, which draws the key pair of each message from its own generator, the same on every target ([libsodium's documentation, sealed boxes](https://doc.libsodium.org/public-key_cryptography/sealed_boxes)). - ElligatorSwift, native, wasip2, wasip3, emscripten: libsecp256k1; the only backend ([libsecp256k1's README](https://github.com/bitcoin-core/secp256k1#readme)). - MuSig2, native, wasip2, wasip3, emscripten: libsecp256k1; the only backend ([libsecp256k1's README](https://github.com/bitcoin-core/secp256k1#readme)). - BIP 352 silent payments, native, wasip2, wasip3, emscripten: libsecp256k1; the only backend ([libsecp256k1's README](https://github.com/bitcoin-core/secp256k1#readme)). - P-256, native, wasip2, wasip3, emscripten: BearSSL; the one backend of the P-curves on all four targets, constant time by design and allocating nothing ([BearSSL, its home page](https://www.bearssl.org/)); OpenSSL and Botan join in increments 8 and 7. ## Constant time, measured - AES-GCM: BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged; on a runner GitHub hosts, by two of three runs), nothing found; wasip2, dudect (printed, not judged: under wasmtime its timing follows the runner's processor); not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3. - AES block cipher: OpenSSL: not measured yet. ctaes: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged; on a runner GitHub hosts, by two of three runs), nothing found; wasip2, dudect (printed, not judged: under wasmtime its timing follows the runner's processor); not measured yet on wasip3, emscripten. - secp256k1: libsecp256k1: native, Valgrind's memcheck (Linux; libsecp256k1's own `ctime_tests.c`, and webcpp's code around it: the key pair's making and `secret()`, its signatures and x-only tweak, ECDH in three forms (`agree`, `agree_hashed` and `agree_point`), the secret key's tweaks and negation, and PKCS#8), MemorySanitizer (Linux, clang; the same paths as memcheck's) and dudect (judged; on a runner GitHub hosts, by two of three runs; `agree`), nothing found; wasip2, dudect (judged; on a runner GitHub hosts, by two of three runs; `agree`), nothing found; not measured yet on wasip3, emscripten. - Ed25519: libsodium: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged; on a runner GitHub hosts, by two of three runs), nothing found; wasip2, dudect (judged; on a runner GitHub hosts, by two of three runs), nothing found; not measured yet on wasip3, emscripten. - X25519: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found, and dudect (judged; on a runner GitHub hosts, by two of three runs): nothing found with arm64's DIT (data-independent timing) bit set, which webcpp sets around every call on 64-bit ARM under macOS, iOS, Linux, Android and Windows where the CPU has it, as Apple requires of constant-time code ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)); the lane's dudect sets nothing itself and measures what a program gets: |t| 1.8 to 2.8 against the threshold 10 (2026-11-21, five runs), while its `dudect_planted`, an early-exit scan of the secret key run inside the same guard, reads |t| 66.72 and 65.53 (2026-11-21): DIT does not hide a leak. Without DIT, dudect times `agree` with an all-zero secret key against random ones at |t| 6.7 to 14.5 (2026-11-19) and 4.3 to 7.8 (2026-11-21, three runs): about one nanosecond in 15 µs. Each on an Apple M5 Max, from a release build. libsodium does not set DIT, and webcpp sets nothing on x86 or WebAssembly; wasip2, dudect (judged; on a runner GitHub hosts, by two of three runs): an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at |t| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), and 25.25 in the lane on 2026-11-21, against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads |t| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next; not measured yet on wasip3, emscripten. - crypto_kx: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - ChaCha20-Poly1305: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged; on a runner GitHub hosts, by two of three runs), nothing found; wasip2, dudect (printed, not judged: under wasmtime its timing follows the runner's processor); not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3. - original ChaCha20-Poly1305: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - XChaCha20-Poly1305: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - ChaCha20: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged; on a runner GitHub hosts, by two of three runs), nothing found; wasip2, dudect (printed, not judged: under wasmtime its timing follows the runner's processor); not measured yet on wasip3, emscripten. OpenSSL: native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3. - Poly1305: libsodium: native, Valgrind's memcheck (Linux; `authenticate`, then `verify` with the right tag and with a wrong one, the key, the message and the tags secret; the verdict of `verify` declassified by crypto where it first branches on it), MemorySanitizer (Linux, clang; the same paths as memcheck's) and dudect (judged; on a runner GitHub hosts, by two of three runs; `authenticate` against its key, the all-zero key against random ones; `verify` against its tag, the right tag with its last byte flipped against it with its first byte flipped, both classes failing: a success and a failure return by different paths, about 0.4 ns apart, which dudect finds and which is public, the verdict being reported), nothing found; wasip2, dudect (judged; on a runner GitHub hosts, by two of three runs; `authenticate` against its key, the all-zero key against random ones; `verify` against its tag, the right tag with its last byte flipped against it with its first byte flipped, both classes failing: a success and a failure return by different paths, about 0.4 ns apart, which dudect finds and which is public, the verdict being reported), nothing found; not measured yet on wasip3, emscripten. OpenSSL (`authenticate` and `verify`, as on libsodium): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3. - XSalsa20-Poly1305 secret box: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - XChaCha20-Poly1305 secret box: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - X25519-XSalsa20-Poly1305 box: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - X25519-XChaCha20-Poly1305 box: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - X25519-XSalsa20-Poly1305 sealed box: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - ElligatorSwift: libsecp256k1: native, Valgrind's memcheck (Linux; libsecp256k1's own `ctime_tests.c`, and webcpp's `ellswift_create` and `ellswift_xdh` around it) and MemorySanitizer (Linux, clang; the same paths as memcheck's), nothing found; not measured yet on wasip2, wasip3, emscripten. - MuSig2: libsecp256k1: native, Valgrind's memcheck (Linux; libsecp256k1's own `ctime_tests.c`, and webcpp's `generate_nonce` and `partial_sign` around it) and MemorySanitizer (Linux, clang; the same paths as memcheck's), nothing found; not measured yet on wasip2, wasip3, emscripten. - BIP 352 silent payments: libsecp256k1: native, Valgrind's memcheck (Linux; libsecp256k1's own `ctime_tests.c`, and webcpp's `create_outputs`, `create_label` and `scan_outputs` around it, each workspace's wipe included) and MemorySanitizer (Linux, clang; the same paths as memcheck's), nothing found; not measured yet on wasip2, wasip3, emscripten. - P-256: BearSSL: native, Valgrind's memcheck (Linux; the key pair's making, `sign` of a digest (`crypto::signing_schemes::prehashed`), which makes its signature low S without a branch, then `low_s()` and `is_low_s()` of it, `agree` and the private formats, `pkcs8`, `raw` and `pem(pkcs8)` both ways; BearSSL's own branches on its signing's verdicts listed in `bearssl_verdicts.supp`; nothing found at webcpp's pin f8a2f3d, whose patches submitted upstream close upstream's leak of the nonce's bit length in `sign` that this checker found on 2026-11-21 and the jump on the scalar in P-256's scalar check that it found under `agree` on the x86-64 runner on 2026-12-03, `sign` with its low-S step included (Valgrind 3.22, arm64 Linux container, 2026-12-02; x86-64, `ctgrind` and `ctgrind_space`, 2026-12-04)), MemorySanitizer (Linux, clang; the same paths as memcheck's; the same leak in `sign` on 2026-11-21 on upstream's BearSSL, nothing at webcpp's pin ae63032, `sign` with its low-S step included (clang 18, arm64 Linux container, 2026-12-02); MemorySanitizer checks the select of P-256's scalar check before the code generator makes it a jump, so the second patch changes nothing it sees) and dudect (judged; on a runner GitHub hosts, by two of three runs; `sign` of a digest and `agree`, each against the key pair's scalar; `sign` with its low-S step judged constant time, |t| 1.70 on Linux and 1.63 natively on an Apple M5 Max (release, one run each, 2026-11-22; provisional, measured again in the final quiet pass)), nothing found; wasip2, dudect (printed, not judged: under wasmtime its timing follows the runner's processor); not measured yet on wasip3, emscripten. ## Rules of use - [Never reuse a nonce under one key](https://webcpporg.github.io/libs/crypto/#crypto-gcm-rules): two AES-GCM messages under one key and one nonce reveal the XOR of their plaintexts and let an attacker forge tags for every later message ([A. Joux, Authentication Failures in NIST version of GCM, 2006](https://csrc.nist.gov/csrc/media/projects/block-cipher-techniques/documents/bcm/comments/800-38-series-drafts/gcm/joux_comments.pdf); [H. Boeck et al., Nonce-Disrespecting Adversaries, 2016](https://eprint.iacr.org/2016/475)); draw a random 12-byte nonce per message, or use a counter that never repeats, a restart included. - [The usage limit](https://webcpporg.github.io/libs/crypto/#crypto-gcm-rules): with random nonces, at most 2^32 messages under one key ([NIST SP 800-38D, GCM and GMAC](https://csrc.nist.gov/pubs/sp/800/38/d/final), section 8.3); rotate the key before. - [A failed open zeroes data](https://webcpporg.github.io/libs/crypto/#crypto-gcm-rules): when the tag does not match, `open` returns `authentication_failed` and zeroes the whole of `data`; keep a copy of the ciphertext if it is needed after a failure. the incremental open authenticates every piece and verifies the tag before it decrypts any; a stream read only once is sealed in records. - [Records: each stream its own key](https://webcpporg.github.io/libs/crypto/#crypto-gcm-incremental): records numbered from 0 with the number as nonce are safe only under a key used for that one stream: derive a key per stream (HKDF of the storage key with a random salt) or draw one; never number two streams' records under one key. - [ECDH's form](https://webcpporg.github.io/libs/crypto/#crypto-secp256k1-ecdh): `keypair::agree` returns the raw x coordinate of the shared point, which protocols such as Nostr's NIP-44 ([Nostr NIP-44, encrypted payloads](https://github.com/nostr-protocol/nips/blob/master/44.md)) take; `keypair::agree_hashed` returns libsecp256k1's hashed form, the SHA-256 of the compressed shared point. Both sides must use the same form, or they derive different keys. Derive keys with a KDF, never use a shared secret directly. - [The x-only tweak is not BIP 32](https://webcpporg.github.io/libs/crypto/#crypto-wallets-taproot): `xonly_public_key::tweak_add` and `keypair::tweak_xonly` are BIP 341's even-y arithmetic ([BIP 341, Taproot](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki)); BIP 32's child derivation tweaks the full key, with `secret_key_tweak_add` and `public_key_tweak_add`, and computes the tweak with HMAC-SHA-512, a domain library's ([BIP 32, hierarchical deterministic wallets](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)). - [Ethereum](https://webcpporg.github.io/libs/crypto/#crypto-curves): this version signs Ethereum's recoverable ECDSA over Keccak-256 (`keypair::sign(crypto::signing_schemes::ecdsa_keccak256_recoverable, input)`, a message hashed inside or the 32-byte digest a program builds), recovers a signer's public key from a signature and the same input (`recover`), and writes the uncompressed key ([The Ethereum yellow paper, appendix F: ECDSA on secp256k1](https://ethereum.github.io/yellowpaper/paper.pdf)); the recovery id's `v` encoding, the digests Ethereum signs (EIP-191, EIP-712, a transaction's), transactions and addresses are a domain library's, which the guide shows over webcpp::hash. - [Compare secrets in constant time](https://webcpporg.github.io/libs/crypto/#crypto-constant-time-equal): compare a tag, a MAC or a token with `secure::constant_time_equal`, never `==` or `memcmp` ([CWE-208, observable timing discrepancy](https://cwe.mitre.org/data/definitions/208.html)). - [Secrets come from random_device](https://webcpporg.github.io/libs/crypto/#crypto-entropy): keys, nonces and tokens are drawn from `random_device`, never `std::mt19937` or `rand` ([CWE-338, a weak generator used for security](https://cwe.mitre.org/data/definitions/338.html)). - [Never reuse a nonce under one ChaCha20-Poly1305 key](https://webcpporg.github.io/libs/crypto/#crypto-chacha): two messages under one key and one nonce reveal the XOR of their plaintexts and let an attacker forge tags ([RFC 8439, section 4: a nonce is never repeated under one key](https://www.rfc-editor.org/rfc/rfc8439#section-4)). `xchacha20_poly1305` and the secret boxes: draw 24 bytes at random per message. `chacha20_poly1305`: number the 12-byte nonces with a counter that never repeats, a restart included, or draw them at random for at most 2^32 messages per key, AES-GCM's bound for random 12-byte nonces ([NIST SP 800-38D, GCM and GMAC](https://csrc.nist.gov/pubs/sp/800/38/d/final)). `chacha20_poly1305_original`: a counter only, its 8 bytes are too short to draw. - [ChaCha20 authenticates nothing](https://webcpporg.github.io/libs/crypto/#crypto-chacha20): `ciphers::chacha20` and the namespace `unauthenticated_encryption` encrypt without a tag: a bit flipped in the ciphertext flips the same bit of the plaintext, and a nonce repeated under one key reveals the XOR of two plaintexts ([RFC 8439, section 4: a nonce is never repeated under one key](https://www.rfc-editor.org/rfc/rfc8439#section-4)). Use them only where a protocol fixes its own construction and authenticates the nonce and the ciphertext itself, as NIP-44 does with HMAC-SHA256 ([Nostr NIP-44, encrypted payloads](https://github.com/nostr-protocol/nips/blob/master/44.md)); a program's own messages take `chacha20_poly1305` or `xchacha20_poly1305`. - [A Poly1305 key authenticates one message](https://webcpporg.github.io/libs/crypto/#crypto-single-use-key-authentication): the 32-byte key of `authenticators::poly1305` is used for one message alone: tags of two messages under one key let an attacker forge others ([RFC 8439, section 2.5: the one-time key is used for one message alone; an attacker who sees two tags under it can forge](https://www.rfc-editor.org/rfc/rfc8439#section-2.5)). A protocol derives the key for each message, as RFC 8439's AEAD takes it from ChaCha20's keystream; a key used for many messages takes the HMAC of `webcpp::hash`, and a program's own messages an AEAD. - [A shared secret is not a key](https://webcpporg.github.io/libs/crypto/#crypto-x25519): derive keys from X25519's shared secret and both public keys with a KDF such as webcpp::hash's HKDF, never use its bytes as a cipher's key ([RFC 7748, section 6.1: a KDF over the shared secret and both public keys](https://www.rfc-editor.org/rfc/rfc7748#section-6.1)); `kx_keypair` and the boxes do this themselves. - [The default on MSVC is not constant time](https://webcpporg.github.io/libs/crypto/#crypto-backends-defaults): natively the default is OpenSSL, which MSVC builds no-asm, table-based ([OpenSSL's INSTALL.md, no-asm](https://github.com/openssl/openssl/blob/master/INSTALL.md)); where timing attacks matter, link BearSSL and name `gcm>`, and for the block cipher link ctaes and name `aes256`. ## Choose by use case - [Seal or open a record inside an event loop without waiting](https://webcpporg.github.io/libs/crypto/#crypto-async): `secret_key_encryption::aead::async_seal`, `async_open` (webcpp::crypto); backend the default of AES-GCM; Jamfile /webcpp/crypto//crypto /webcpp/crypto//gcm-default /webcpp/asio//asio. Example: example/async_seal.cpp. - [Sign and verify with Ed25519 in a coroutine](https://webcpporg.github.io/libs/crypto/#crypto-async): `async_sign`, `async_verify` (webcpp::crypto); backend the default of Ed25519; Jamfile /webcpp/crypto//crypto /webcpp/crypto//ed25519-default /webcpp/asio//asio. Example: example/async_sign.cpp. - [Generate a session token, a password-reset link or an API key](https://webcpporg.github.io/libs/crypto/#crypto-entropy): `random_device::fill` (webcpp::crypto); backend the system's generator; Jamfile /webcpp/crypto//crypto. Example: example/reset_token.cpp. - [Draw an unbiased number in a range, such as a six-digit code](https://webcpporg.github.io/libs/crypto/#crypto-entropy): `random_device` with `std::uniform_int_distribution` (webcpp::crypto); backend the system's generator; Jamfile /webcpp/crypto//crypto. Example: example/reset_token.cpp. - [Check a webhook's or an API's HMAC signature](https://webcpporg.github.io/libs/crypto/#crypto-constant-time-equal): `secure::constant_time_equal`, with Boost.Hash2's `hmac` (webcpp::crypto); backend none; Jamfile /webcpp/crypto//crypto. Example: example/constant_time_equal_hmac.cpp. - [Encrypt a database field, a cookie or a token at rest](https://webcpporg.github.io/libs/crypto/#crypto-gcm): `aes256_gcm` (webcpp::crypto::ciphers); backend the default: OpenSSL natively, BearSSL on WebAssembly; Jamfile /webcpp/crypto//crypto /webcpp/crypto//gcm-default. Example: example/store_token.cpp. - [Encrypt a large file or a stream](https://webcpporg.github.io/libs/crypto/#crypto-gcm-incremental): `aes256_gcm`: records, or `begin_seal`, `update` and `result` (webcpp::crypto::ciphers); backend the default; Jamfile /webcpp/crypto//crypto /webcpp/crypto//gcm-default. Example: example/sealed_records.cpp. - [Tell a forged message from a malformed one](https://webcpporg.github.io/libs/crypto/#crypto-errors): `error` (webcpp::crypto); backend any; Jamfile /webcpp/crypto//crypto. Example: example/failure_reasons.cpp. - [Turn a sequential id into an opaque reference](https://webcpporg.github.io/libs/crypto/#crypto-block): `aes256` (webcpp::crypto::ciphers); backend ctaes, or OpenSSL natively and on WASI; Jamfile /webcpp/crypto//crypto /third_party//ctaes. Example: example/order_reference.cpp. - [Prove who wrote a message posted to a relay](https://webcpporg.github.io/libs/crypto/#crypto-p2p-signed): `keypair::sign(crypto::signing_schemes::schnorr, message)`, `crypto::verify` (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/signed_message.cpp. - [Check a software update or a licence file](https://webcpporg.github.io/libs/crypto/#crypto-everyday-update): `keypair::sign(crypto::signing_schemes::ecdsa_sha256, message)`, `public_key::from_bytes`, `crypto::verify` (webcpp::crypto::curves::secp256k1); backend libsecp256k1; Jamfile /webcpp/crypto//crypto /third_party//secp256k1. Example: example/signed_update.cpp. - [Agree on a key with a peer through a relay that sees everything](https://webcpporg.github.io/libs/crypto/#crypto-p2p-session): `keypair::agree_hashed` (SHA-256 of the shared point), then HKDF on Boost.Hash2 and `aes256_gcm` (webcpp::crypto::curves::secp256k1); backend libsecp256k1, and AES-GCM's default; Jamfile /webcpp/crypto//crypto /third_party//secp256k1 /webcpp/crypto//gcm-default. Example: example/ecdh_session.cpp. - [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): `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): `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. - [Keep every allocation of OpenSSL's in the program's arena](https://webcpporg.github.io/libs/crypto/#crypto-memory-openssl): `openssl_memory_resource`, `backends::openssl::install_memory_functions` (webcpp::crypto); backend OpenSSL, natively; Jamfile `/webcpp/crypto//crypto` in the program; in the project requirements, `WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE` and `/webcpp/crypto//openssl`. Example: example/openssl_memory.cpp. - [Seed BearSSL's own generator for code that calls BearSSL directly](https://webcpporg.github.io/libs/crypto/#crypto-entropy-bearssl): `random_device::fill` (webcpp::crypto); backend BearSSL's C API; Jamfile /webcpp/crypto//crypto /third_party//bearssl. Example: example/bearssl_prng.cpp. - [Hash, or compute an HMAC](https://webcpporg.github.io/libs/crypto/#crypto-overview-hash2): `boost::hash2::sha2_256`, `hmac_sha2_256` (Boost.Hash2, not this library); backend none; Jamfile `/webcpp/crypto//crypto` brings Boost's headers. Example: example/constant_time_equal_hmac.cpp. - [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): `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. - [Apply ChaCha20's keystream for a protocol that fixes its own construction (NIP-44)](https://webcpporg.github.io/libs/crypto/#crypto-chacha20): `chacha20<>`: `from_key`, `apply_keystream(nonce, data, initial_counter)`; `unauthenticated_encryption::apply_keystream` in code written once for every stream cipher (webcpp::crypto::ciphers); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//chacha20-default. Example: example/chacha20_keystream.cpp. - [Authenticate one message under a one-time key](https://webcpporg.github.io/libs/crypto/#crypto-poly1305): `poly1305<>`: static `authenticate(key, message)`, `verify(key, message, tag)`; `single_use_key_authentication::authenticate` and `verify` in code written once for every single-use key authenticator (webcpp::crypto::authenticators); backend libsodium; Jamfile /webcpp/crypto//crypto /webcpp/crypto//poly1305-default. Example: example/poly1305_one_time_tag.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. - [Encrypt to a public key as RFC 9180 says, byte for byte, as MLS's Welcome and UpdatePath and Marmot's backups do](https://webcpporg.github.io/libs/crypto/#crypto-hpke): `suite<...>::begin_sending`, `begin_receiving`, `seal`, `open`, `derive_from_seed`; the contexts' `seal`, `open` and `export_secret` (webcpp::crypto::public_key_encryption::hpke); backend the defaults of X25519, P-256, AES-GCM and ChaCha20-Poly1305; Jamfile /webcpp/crypto//crypto /webcpp/crypto//x25519-default /webcpp/crypto//gcm-default. Example: example/hpke_base.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()`; `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): `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 - example/aes256_gcm_bearssl.cpp: Seals one zero block with AES-256-GCM on BearSSL, names the backend in the type, checks the result against test case 14 of the GCM specification, and opens it again. Naming the backend pins the implementation, so the same bytes and the same code run on every target. - example/arena_signer.cpp: Gives the secp256k1 key pair its one allocation from a fixed buffer owned by the program, and counts it, then reads the key pair back from its PKCS#8 into the same buffer, one allocation more. With a null upstream nothing else can allocate, so a program with strict memory limits knows exactly what the library needs and that it never reaches for the heap. - example/async_seal.cpp: An audit record sealed and opened through an event loop: the GCM specification's test case 16 (AES-256, 12-byte nonce, associated data), a published key, never a real one. Each completion runs from the loop's run, after the call that started it has returned, on every target; the handler of the seal prints the tag and starts the open, the handler of the open compares the plaintext. The loop ends when no operation is pending: nothing waits for a time. - example/async_sign.cpp: A coroutine on an event loop makes an Ed25519 key pair from RFC 8032's second test seed (a published seed, never a real key), signs the test's message with crypto::signing_schemes::eddsa, and verifies the signature with the public key alone, each step a co_await of an asynchronous function with use_awaitable. The coroutine ends, and the loop with it, when the last completion has run: nothing waits for a time. - example/bearssl_prng.cpp: Seeds BearSSL's own HMAC-DRBG from webcpp::crypto::random_device and draws bytes from it. A generator is only as good as its seed: the seed must come from the system's entropy, never from the clock or a counter. - example/bitcoin_sighash.cpp: Signs a Bitcoin legacy or segwit v0 input's sighash: SHA-256d of the transaction's serialization, computed by the program with webcpp::hash, signed as a digest with ECDSA over SHA-256, verified, and written as strict DER for the witness. Crypto computes no sighash: the serialization and its hash are the protocol's, a domain library's work; the sighash is the SHA-256 of a SHA-256, so the second hash is the scheme's own and the digest is honest under crypto::signing_schemes::ecdsa_sha256. A taproot sighash, BIP341's tagged hash, is the message of crypto::signing_schemes::schnorr instead (musig_taproot.cpp, taproot_tweak.cpp). - example/box_messages.cpp: Two people who know each other's X25519 public keys exchange messages in libsodium's box: each makes a box from the own key pair and the other's public key, agreeing one key for the pair, then seals each message with a nonce drawn at random. What one seals the other opens; a stranger's box, from another key pair, does not. Prints what each reads. - example/chacha20_keystream.cpp: ChaCha20 alone, the raw stream cipher, which authenticates nothing: RFC 8439 section 2.4.2's key, nonce and initial counter 1 encrypt the section's 114-byte plaintext in place, the ciphertext printed in hex and compared with the RFC's; the same call at the same counter gives the plaintext back. Data that needs more blocks than the 32-bit counter has left is refused with counter_overflow and left untouched. A program's own messages take chacha20_poly1305: the raw cipher is for a protocol that fixes its own construction and authentication, as NIP-44 does. - example/constant_time_equal_hmac.cpp: Checks an HMAC-SHA-256 tag from Boost.Hash2 with webcpp::secure::constant_time_equal, the constant-time comparison. Comparing tags with == or memcmp stops at the first difference, and that timing lets an attacker guess a valid tag one byte at a time. - example/ecdh_session.cpp: Two peers agree on a shared secret with ECDH, derive a session key from it with HKDF-SHA-256 and talk over AES-256-GCM. The raw ECDH output is not a key: HKDF turns it into one and ties it to this application with a context string. - example/ecdh_x_coordinate.cpp: Two key pairs agree on the shared point's raw x coordinate with agree, the form Nostr's encrypted messages (NIP-44) take, then derive from it what a NIP-44 library would: the conversation key, HKDF-extract with the salt "nip44-v2". The x coordinate is not a key; the derivation that makes one is the protocol's, and belongs to a domain library. - example/ecdsa_der.cpp: Signs with ECDSA the way a domain library does: a sighash computed by the library, a 64-byte signature, its strict DER form for the wire, and the low-S rule. The same signature has a twin with S replaced by n - S; accepting both makes transaction ids malleable, so verifiers refuse the high-S twin. - example/ed25519_sign.cpp: Signs RFC 8032's second Ed25519 test with a key pair made from its seed and verifies the signature with the public key alone, `key.sign(crypto::signing_schemes::eddsa, message)` and `crypto::verify(crypto::signing_schemes::eddsa, crypto::verification_policies::canonical, signature, message, public_key)`; then signs the message "abc" given in two pieces with Ed25519ph's streaming signer, `curves::ed25519::prehashed_signer`, as RFC 8032's Ed25519ph test does, and verifies it with `verify(crypto::verification_policies::canonical, signature, public_key)`; then signs the message's SHA-512, a digest the program has, under the scheme `crypto::signing_schemes::eddsa_prehashed`, which gives the same signature, and verifies it with `crypto::verify(crypto::signing_schemes::eddsa_prehashed, crypto::verification_policies::canonical, signature, crypto::signing_schemes::prehashed{digest}, public_key)`. Prints each key and signature in hex, and whether the two Ed25519ph signatures are equal. - example/ellswift_handshake.cpp: The first messages of a peer-to-peer connection whose key exchange looks like random bytes, as BIP 324's transport starts: each peer sends its public key as an ElligatorSwift encoding, 64 bytes with no recognisable prefix, and both compute the same shared secret from the two encodings. The secrets and the randomness are fixed here, so that the program runs the same each time; a real program draws all four from random_device. Nothing random is printed. - example/ethereum_personal_sign.cpp: Signs a message as an Ethereum wallet's personal_sign does, then finds the signer's address from the signature alone. The program builds EIP-191's digest itself with webcpp::hash's Keccak-256, signs it as crypto::signing_schemes::prehashed with crypto::signing_schemes::ecdsa_keccak256_recoverable, prints r, s and v, recovers the key and prints its address. crypto computes none of the protocol's encodings: not the digest (EIP-191's here, EIP-712's or a transaction's elsewhere), not v (27 + the recovery id for personal_sign, 35 + 2 x chain_id + the id for EIP-155, the id itself as a typed transaction's yParity), not the address. The key and the message are web3.js's documented example (web3.eth.accounts.sign('Some data', ...)), whose signature this prints. - 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/hpke_base.cpp: HPKE (RFC 9180) in base mode with MLS's suite 0x0001: DHKEM(X25519, HKDF-SHA256), HKDF-SHA256 and AES-128-GCM, on the default backends. The recipient's key pair is derived from a seed, as MLS derives its tree keys; the sender seals two messages to its public key on one context, and sends the encapsulated key with them; the recipient opens them in order. Both contexts export the same secret, as MLS's external join does; a message opened out of its order is refused. Prints what the recipient reads and whether the exports agree. - 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. - example/musig_taproot.cpp: The wallet of musig_wallet.cpp receiving to a Taproot output (BIP 341) with a key path only: the two signers' aggregate key is the internal key, and the output key on the chain is that key tweaked by its TapTweak tagged hash. The tweak is applied to the aggregate key with tweak_xonly_add before any nonce is made, since the session and the partial signatures need the key as tweaked, and generate_nonce mixes in the key it is given (BIP 327's NonceGen takes it only as an optional input); then the signers run MuSig2's two rounds as before, and the signature verifies under the output key and not under the internal one. The tweak's hash is the protocol's, which a domain library computes; it is shown here with webcpp::hash's SHA-256. 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. - example/musig_wallet.cpp: A wallet split across two devices, a phone and a laptop, whose coins move only when both sign (BIP 327's MuSig2): the two public keys are sorted and aggregated into one; each device makes a nonce and sends its public half; each signs its part once every public nonce is in; the parts add up to one BIP 340 signature under the one x-only key, which on the chain looks like any single signer's. The devices' secrets are fixed here, so that the program runs the same each time; a real wallet draws them from crypto's random device. The nonces are fresh in every run, drawn inside generate_nonce; nothing random is printed. - example/opened_in_two_passes.cpp: Opens a sealed file of 1 MiB without holding it whole, in two passes over the file: the first authenticates every piece of 64 KiB and verifies the tag, the second decrypts the same pieces. No byte of plaintext leaves the object before the tag matched. The file is any std::istream read twice: a program reads its own disk through a std::ifstream; this one keeps the file in a std::stringstream, so that it runs where the program has no directory (WASI). - example/openssl_memory.cpp: Sends every allocation of OpenSSL's to the program's own memory resource, natively, then seals and opens one block with AES-256-GCM on OpenSSL, on a worker thread as an audio engine's or a server's would. Built with WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE, /webcpp/crypto//openssl and threads (example/Jamfile). - 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 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/poly1305_one_time_tag.cpp: Poly1305 alone, the one-time authenticator: RFC 8439 section 2.5.2's key and message give the section's 16-byte tag, printed in hex and compared with the RFC's; verify accepts the tag received and refuses it with one bit flipped. Its key authenticates this one message and never a second: a protocol derives a fresh key per message (RFC 8439's AEAD takes it from ChaCha20's keystream), and a key used for many messages takes webcpp::hash's HMAC. - 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 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. - example/store_token.cpp: Stores a user's API token encrypted in a database row, bound to the user's id. The id is associated data: authenticated but not encrypted, so a row copied to another user's record no longer opens. Without that binding a database writer could swap the rows of two users. - example/taproot_tweak.cpp: Tweaks an internal key as Bitcoin's Taproot does for a key-path-only output (BIP341): the tweak is a tagged hash of the internal key, and the output key commits to it, which anyone holding the internal key checks. The same tweak applied to the key pair lets the owner sign for the output key, so spending needs no script. - example/uploaded_key.cpp: A server registers a device's P-256 public key, uploaded as PEM text, as a browser's WebCrypto exports it; it stores the one SPKI crypto writes back, and later verifies what the device signs under any_s, WebCrypto's policy, its high-S form too. The device is played here by a key pair of crypto's own. A compressed SPKI of the same key reads as the same key, though its bytes differ. - example/wallet_key.cpp: Makes a wallet's key pair from a random secret, then seals the secret at rest with AES-256-GCM and restores it. A secret that sits unencrypted on disk, or lingers in memory after use, is the first thing an attacker looks for, so the copies are wiped as soon as they are not needed. - example/wallet_wif.cpp: A wallet's key as text, as a domain library such as webcpp::bitcoin would write it, over webcpp::hash's Base58Check: the compressed WIF key of Bitcoin Core's key_io_valid.json (L5nJeqKmpHp4P7F8ZYyjwc5a7P4d8EabuGAzfGJk7yC1BJyzNaEd, whose key is ff778740...b843f8) read into a secret result and made into a secp256k1 key pair through formats::raw, written back from the key pair's formats::raw to the same text, and the key's pay-to-public-key-hash address, version byte 0 and the hash160 of the compressed public key, 18YrtUdbxosh7wha8nq7M1g6F64RyW8rLH, which Python's hashlib and a plain implementation of secp256k1's arithmetic give too. - example/x25519_agree.cpp: Alice and Bob of RFC 7748 section 6.1, each an X25519 key pair made from a secret key, agree a shared secret from each other's public keys; then two Ed25519 key pairs, made from RFC 8032's seeds, agree one through their X25519 forms; and a peer key of low order is refused. Prints each public key and the shared secrets in hex. - example/xchacha_seal.cpp: A chat app, in the browser and natively alike, encrypts each message of a conversation under the conversation's key with XChaCha20-Poly1305, a fresh 24-byte nonce drawn at random per message and the conversation's id as associated data; a message changed on the way, or moved to another conversation, does not open. Prints what the recipient reads. ## Optional - [Every message and its fix](https://webcpporg.github.io/libs/crypto/#crypto-jamfile-messages) - [The backends](https://webcpporg.github.io/libs/crypto/#crypto-backends) - [Reference](https://webcpporg.github.io/libs/crypto/#reference)