diff --git a/doc/digests.adoc b/doc/digests.adoc index aa8802e..c669a4a 100644 --- a/doc/digests.adoc +++ b/doc/digests.adoc @@ -79,7 +79,7 @@ guesses per second on a graphics card. Passwords go through Argon2id (<>). * *A digest is compared as a public value.* Comparing two digests with `==` is fine when both are public, a file's expected digest for example; a tag that -depends on a secret is compared with `webcpp::crypto::equal` +depends on a secret is compared with `webcpp::crypto::constant_time_equal` (<>). * *Other backends, by name.* `backends::bearssl`, `backends::openssl` and `backends::botan` compute SHA-2 too, never as a default: a call names one @@ -439,10 +439,11 @@ the output given to `digest_of` or `finish` has exactly that size same way on every side, since the library does not guess a padding. * *A key is a secret, the salt and personalisation are not.* For a MAC, use a random key of 16 to 64 bytes from `webcpp::crypto::random_device`, compare -tags with `webcpp::crypto::equal`, and prefer `mac` -(<>); a key of 0 bytes is the unkeyed hash. With a -key, `digest_of` and each of `make`, `update` and `finish` of a `hasher` set -DIT around the call (<>); without one, none does. +tags with `webcpp::crypto::constant_time_equal`, and prefer +`mac` (<>); a key of 0 bytes is the +unkeyed hash. With a key, `digest_of` and each of `make`, `update` and `finish` +of a `hasher` set DIT around the call (<>); without one, none +does. * *`backends::hash2` has BLAKE2b-512 alone.* Named in the call, it answers `unsupported_by_backend` to a shorter output, a key, a salt or a personalisation, rather than ignore one. diff --git a/doc/generated/choose.adoc b/doc/generated/choose.adoc index 936824e..a01b0a9 100644 --- a/doc/generated/choose.adoc +++ b/doc/generated/choose.adoc @@ -25,7 +25,7 @@ | `example/content_address.cpp` | prove a message comes from someone who holds a shared secret key (an API request, a cookie, a webhook) -| <> +| <> | `hash2`: `/webcpp/hash//hash` | none: one backend | `example/hmac_sha256.cpp` diff --git a/doc/guide.json b/doc/guide.json index bc75b18..83ef802 100644 --- a/doc/guide.json +++ b/doc/guide.json @@ -1702,7 +1702,7 @@ "use_cases": [ {"need": "check that a file or message was not changed by accident or on the way", "use": "sha2_256 (or blake3 for large data)", "function": "sha2_256", "backend": "none: one backend", "example": "sha256.cpp", "anchor": "hash-sha2"}, {"need": "name content by what it holds (a file-sharing app, a cache, a store of blobs)", "use": "blake3", "function": "blake3", "backend": "link /third_party//blake3", "example": "content_address.cpp", "anchor": "hash-p2p-content"}, - {"need": "prove a message comes from someone who holds a shared secret key (an API request, a cookie, a webhook)", "use": "hmac_sha2_256, checked with webcpp::crypto::equal", "function": "hmac_sha2_256", "backend": "none: one backend", "example": "hmac_sha256.cpp", "anchor": "hash-hmac"}, + {"need": "prove a message comes from someone who holds a shared secret key (an API request, a cookie, a webhook)", "use": "hmac_sha2_256, checked with webcpp::crypto::constant_time_equal", "function": "hmac_sha2_256", "backend": "none: one backend", "example": "hmac_sha256.cpp", "anchor": "hash-hmac"}, {"need": "store users' passwords for a login", "use": "argon2id with argon2id_interactive or stronger, a random 16-byte salt per user", "function": "argon2id", "backend": "link /third_party//sodium", "example": "password_storage.cpp", "anchor": "hash-privacy-passwords"}, {"need": "turn a password into an encryption key (a password manager, an encrypted backup)", "use": "argon2id with argon2id_moderate or argon2id_sensitive, then hkdf for each purpose", "function": "argon2id", "backend": "link /third_party//sodium", "example": "argon2id.cpp", "anchor": "hash-argon2id"}, {"need": "check or make the bcrypt strings a system already stores, or read and write OpenSSH's encrypted private keys", "use": "bcrypt and bcrypt_verify at a cost of 10 or more (bcrypt_pbkdf for OpenSSH's keys); argon2id for a new design", "function": "bcrypt", "backend": "name backends::botan and link /third_party//botan, with exceptions", "example": "bcrypt.cpp", "anchor": "hash-bcrypt"}, diff --git a/doc/llms.txt b/doc/llms.txt index e3b0366..658ebce 100644 --- a/doc/llms.txt +++ b/doc/llms.txt @@ -168,7 +168,7 @@ Fast hashes for hash tables, checksums against accidents and identifiers that no - I need to check that a file or message was not changed by accident or on the way: sha2_256 (or blake3 for large data); backend: none: one backend; example libs/hash/example/sha256.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-sha2) - I need to name content by what it holds (a file-sharing app, a cache, a store of blobs): blake3; backend: link /third_party//blake3; example libs/hash/example/content_address.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-p2p-content) -- I need to prove a message comes from someone who holds a shared secret key (an API request, a cookie, a webhook): hmac_sha2_256, checked with webcpp::crypto::equal; backend: none: one backend; example libs/hash/example/hmac_sha256.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-hmac) +- I need to prove a message comes from someone who holds a shared secret key (an API request, a cookie, a webhook): hmac_sha2_256, checked with webcpp::crypto::constant_time_equal; backend: none: one backend; example libs/hash/example/hmac_sha256.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-hmac) - I need to store users' passwords for a login: argon2id with argon2id_interactive or stronger, a random 16-byte salt per user; backend: link /third_party//sodium; example libs/hash/example/password_storage.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-privacy-passwords) - I need to turn a password into an encryption key (a password manager, an encrypted backup): argon2id with argon2id_moderate or argon2id_sensitive, then hkdf for each purpose; backend: link /third_party//sodium; example libs/hash/example/argon2id.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-argon2id) - I need to check or make the bcrypt strings a system already stores, or read and write OpenSSH's encrypted private keys: bcrypt and bcrypt_verify at a cost of 10 or more (bcrypt_pbkdf for OpenSSH's keys); argon2id for a new design; backend: name backends::botan and link /third_party//botan, with exceptions; example libs/hash/example/bcrypt.cpp. [Section](https://webcpporg.github.io/libs/hash/#hash-bcrypt) diff --git a/doc/macs.adoc b/doc/macs.adoc index f438017..49a4361 100644 --- a/doc/macs.adoc +++ b/doc/macs.adoc @@ -53,12 +53,12 @@ include::{examples}/hmac_sha256.expected[] *Pitfalls.* -* *Compare tags with `webcpp::crypto::equal`, never with `==`.* The digest's -`operator==` is `memcmp`, which returns at the first byte that differs; an -attacker who sends guesses and times the answers learns how many leading -bytes are right and builds a valid tag one byte at a time, in a few thousand -tries per byte instead of 2^256^ in all. `equal` reads every byte whatever the -data. +* *Compare tags with `webcpp::crypto::constant_time_equal`, never with `==`.* +The digest's `operator==` is `memcmp`, which returns at the first byte that +differs; an attacker who sends guesses and times the answers learns how many +leading bytes are right and builds a valid tag one byte at a time, in a few +thousand tries per byte instead of 2^256^ in all. `constant_time_equal` reads +every byte whatever the data. * *A key is random and as long as the digest.* A 32-byte key from `webcpp::crypto::random_device` for HMAC-SHA-256; a password is not a key (<>). A key longer than the hash's block is hashed @@ -154,7 +154,8 @@ include::{examples}/kmac.expected[] *Pitfalls.* -* *Compare with `webcpp::crypto::equal`.* As for every tag (<>). +* *Compare with `webcpp::crypto::constant_time_equal`.* As for every tag +(<>). * *The key sets the strength.* SP 800-185 (section 8.4.1) forbids a key shorter than the security the application needs: 16 bytes for KMAC128, 32 for KMAC256. `mac` takes any key, the empty one included, as the standard @@ -196,7 +197,8 @@ include::{examples}/mac_family.expected[] *Pitfalls.* -* *Compare with `webcpp::crypto::equal`.* As for every tag (<>). +* *Compare with `webcpp::crypto::constant_time_equal`.* As for every tag +(<>). * *A cut tag is a weaker tag.* Ten bytes, 80 bits, is the shortest `mac` gives; keep the whole digest unless a protocol fixes a shorter tag. * *The tag's name is the digest's.* `macs::hmac` cut to 32 @@ -274,7 +276,7 @@ include::{examples}/hmac_sodium.expected[] *Pitfalls.* * *The pitfalls of <> hold:* compare tags with -`webcpp::crypto::equal`, and use a random key, not a password. +`webcpp::crypto::constant_time_equal`, and use a random key, not a password. * *"HMAC-SHA-512-256" has two meanings.* A program that must agree with libsodium's `crypto_auth_hmacsha512256` names `macs::hmac` and a 32-byte output; one that follows a standard naming HMAC over @@ -307,7 +309,8 @@ include::{examples}/blake2b_mac_sodium.expected[] *Pitfalls.* -* *Compare with `webcpp::crypto::equal`.* As for every tag (<>). +* *Compare with `webcpp::crypto::constant_time_equal`.* As for every tag +(<>). * *The length is a parameter, not a cut.* The tag's size goes into the hash: a 16-byte tag is not the first 16 bytes of a 32-byte one, and the receiver uses the sender's size. diff --git a/doc/overview.adoc b/doc/overview.adoc index 125bd61..110de45 100644 --- a/doc/overview.adoc +++ b/doc/overview.adoc @@ -147,9 +147,9 @@ hash is not a new implementation of a primitive: every permutation, compression function and hash is Boost.Hash2's or a C library's, and the two constructions webcpp writes over them are listed in <>. It is not webcpp::crypto either: encryption, signatures, randomness and -constant-time comparison stay there, and `webcpp::crypto::equal` is what -compares a MAC. It holds primitives only: what a protocol defines on top of -them (Bitcoin's BIP39 seeds and hash160, Ethereum's EIP-55 addresses, Nostr's +constant-time comparison stay there, and `webcpp::crypto::constant_time_equal` +is what compares a MAC. It holds primitives only: what a protocol defines on top +of them (Bitcoin's BIP39 seeds and hash160, Ethereum's EIP-55 addresses, Nostr's event ids and NIP-44's keys) belongs to domain libraries, webcpp::bitcoin, webcpp::ethereum and webcpp::nostr, which build them from these functions. The chapters by use case show those steps as examples, each marked as what such a diff --git a/doc/password_hashing.adoc b/doc/password_hashing.adoc index 9acac3a..8aba2d2 100644 --- a/doc/password_hashing.adoc +++ b/doc/password_hashing.adoc @@ -93,7 +93,7 @@ and a browser tab may not get a gigabyte: measure on the target. same memory, passes and salt; a program that raises its costs later must know which costs each stored tag used. hash has no `$argon2id$` string format: the program stores the three values in its own record (<>). -* *Compare the tag with `webcpp::crypto::equal`.* +* *Compare the tag with `webcpp::crypto::constant_time_equal`.* * *The memory is the backend's.* libsodium allocates the matrix with `mmap` where there is one, else from the C heap; OpenSSL from its own allocator, the C heap; both outside any allocator of the program (<>). @@ -251,7 +251,7 @@ program can spend, time one hash, then trade. *Pitfalls.* * *A random salt per password, the costs stored with the result,* and the -result compared with `webcpp::crypto::equal`, as for Argon2id. +result compared with `webcpp::crypto::constant_time_equal`, as for Argon2id. * *libsodium's own limit:* N at most 4,294,967,295; hash refuses a larger one with `invalid_parameters` on `backends::sodium`. * *OpenSSL's own limits:* r p below 2^24^ (`invalid_parameters`), and a diff --git a/doc/use-cases/privacy.adoc b/doc/use-cases/privacy.adoc index c106549..8ff7bd0 100644 --- a/doc/use-cases/privacy.adoc +++ b/doc/use-cases/privacy.adoc @@ -44,8 +44,8 @@ one guess tests every user; tables of precomputed hashes of common passwords work at once. * *With a fast hash* (SHA-256, MD5) instead of Argon2id, a graphics card tests billions of guesses per second: most human passwords fall in hours. -* *With `==`* instead of `webcpp::crypto::equal`, the time of the comparison -tells an attacker at the network how much of a guessed tag is right. +* *With `==`* instead of `webcpp::crypto::constant_time_equal`, the time of the +comparison tells an attacker at the network how much of a guessed tag is right. * *Without the costs in the record,* the service cannot raise them later: the record must say which memory and passes made its tag, so that a login can verify an old tag and store a new one with today's costs. diff --git a/example/Jamfile b/example/Jamfile index 94cb28a..3dde4d1 100644 --- a/example/Jamfile +++ b/example/Jamfile @@ -39,8 +39,9 @@ webcpp.example streebog.cpp : $(.botan) : native emscripten ; webcpp.example whirlpool.cpp : $(.botan) : native emscripten ; webcpp.example skein.cpp : $(.botan) : native emscripten ; -# Message authentication: a MAC is compared with webcpp::crypto::equal, and a -# hash table's key comes from webcpp::crypto::random_device. +# Message authentication: a MAC is compared with +# webcpp::crypto::constant_time_equal, and a hash table's key comes from +# webcpp::crypto::random_device. webcpp.example hmac_sha256.cpp : /webcpp/crypto//crypto ; webcpp.example siphash.cpp : /webcpp/crypto//crypto ; webcpp.example mac_family.cpp : /webcpp/crypto//crypto ; diff --git a/example/blake2b_mac_sodium.cpp b/example/blake2b_mac_sodium.cpp index af336b5..c9ba138 100644 --- a/example/blake2b_mac_sodium.cpp +++ b/example/blake2b_mac_sodium.cpp @@ -12,7 +12,7 @@ // tag::example[] #include "hex.hpp" -#include +#include #include #include @@ -41,8 +41,8 @@ int main() { // The receiver computes the tag again and compares it in constant time. std::array again{}; - bool const genuine = - hash::mac(again, key, message) && webcpp::crypto::equal(again, tag); + bool const genuine = hash::mac(again, key, message) && + webcpp::crypto::constant_time_equal(again, tag); std::cout << "genuine: " << (genuine ? "yes" : "no") << '\n'; // The tag's size is a parameter of the hash: a 16-byte tag is not the 32-byte one cut. diff --git a/example/hmac_sha256.cpp b/example/hmac_sha256.cpp index 605a679..dc1394e 100644 --- a/example/hmac_sha256.cpp +++ b/example/hmac_sha256.cpp @@ -10,7 +10,7 @@ // tag::example[] #include "hex.hpp" -#include +#include #include #include @@ -42,12 +42,13 @@ int main() { // The receiver computes the tag again from the message it got, and compares it with the one // it got. Never with ==: memcmp stops at the first different byte, so an attacker who // measures how long the answer takes learns how many leading bytes of a guess are right, - // and guesses a valid tag one byte at a time. webcpp::crypto::equal reads every byte. - bool const genuine = webcpp::crypto::equal(tag_of(key, message), tag); + // and guesses a valid tag one byte at a time. crypto::constant_time_equal reads every byte. + bool const genuine = webcpp::crypto::constant_time_equal(tag_of(key, message), tag); std::cout << "genuine: " << (genuine ? "yes" : "no") << '\n'; // One byte of the message changed in transit: the tags differ. - bool const forged = webcpp::crypto::equal(tag_of(key, "what do ya want for nothing!"), tag); + bool const forged = + webcpp::crypto::constant_time_equal(tag_of(key, "what do ya want for nothing!"), tag); std::cout << "tampered: " << (forged ? "accepted" : "refused") << '\n'; if (!genuine || forged) { return 1; diff --git a/example/hmac_sodium.cpp b/example/hmac_sodium.cpp index f469587..3c74973 100644 --- a/example/hmac_sodium.cpp +++ b/example/hmac_sodium.cpp @@ -12,7 +12,7 @@ // tag::example[] #include "hex.hpp" -#include +#include #include #include @@ -42,7 +42,7 @@ int main() { // The receiver computes the tag again and compares it in constant time. std::array again{}; bool const genuine = hash::mac, sodium>(again, key, message) && - webcpp::crypto::equal(again, tag); + webcpp::crypto::constant_time_equal(again, tag); std::cout << "genuine: " << (genuine ? "yes" : "no") << '\n'; // libsodium's crypto_auth_hmacsha512256 is HMAC-SHA-512 cut to 32 bytes: the same tag, with a diff --git a/example/kmac.cpp b/example/kmac.cpp index c5266cf..63900d3 100644 --- a/example/kmac.cpp +++ b/example/kmac.cpp @@ -6,12 +6,13 @@ // KMAC128 with mac, on its default backend, XKCP, against NIST's KMAC sample #2 (the key 0x40 to // 0x5F, the message 00 01 02 03, S = "My Tagged Application"); the receiver's check with -// webcpp::crypto::equal; and KMACXOF128 of the same inputs, another tag, NIST's KMACXOF sample #2. +// webcpp::crypto::constant_time_equal; and KMACXOF128 of the same inputs, another tag, NIST's +// KMACXOF sample #2. // tag::example[] #include "hex.hpp" -#include +#include #include #include @@ -39,7 +40,7 @@ int main() { std::array expected{}; bool const genuine = hash::mac(expected, key, message, {.customisation = application}).has_value() && - webcpp::crypto::equal(expected, tag); + webcpp::crypto::constant_time_equal(expected, tag); std::cout << "genuine: " << (genuine ? "yes" : "no") << '\n'; // KMACXOF128: the same inputs, another tag. diff --git a/example/mac_family.cpp b/example/mac_family.cpp index 90ddd79..88f7d0b 100644 --- a/example/mac_family.cpp +++ b/example/mac_family.cpp @@ -5,12 +5,13 @@ // https://www.boost.org/LICENSE_1_0.txt) // The MAC family: HMAC-SHA-256 with mac, in one call, against RFC 4231's test case 2; the -// receiver's check with webcpp::crypto::equal; and a tag cut to 16 bytes, RFC 4231's test case 5. +// receiver's check with webcpp::crypto::constant_time_equal; and a tag cut to 16 bytes, RFC 4231's +// test case 5. // tag::example[] #include "hex.hpp" -#include +#include #include #include @@ -36,7 +37,7 @@ int main() { // The receiver computes the tag again and compares it in constant time, never with ==. std::array expected{}; bool const genuine = hash::mac(expected, key, message).has_value() && - webcpp::crypto::equal(expected, tag); + webcpp::crypto::constant_time_equal(expected, tag); std::cout << "genuine: " << (genuine ? "yes" : "no") << '\n'; // A tag cut to 16 bytes is the digest's first 16 (RFC 2104, section 5); shorter than 10 is diff --git a/example/password_storage.cpp b/example/password_storage.cpp index 7eb350e..055c33f 100644 --- a/example/password_storage.cpp +++ b/example/password_storage.cpp @@ -12,7 +12,7 @@ // tag::example[] #include "hex.hpp" -#include +#include #include #include @@ -61,7 +61,7 @@ boost::system::result check(credential const& stored, std::string_view pas if (!done) { return done.error(); } - return webcpp::crypto::equal(candidate, stored.tag); + return webcpp::crypto::constant_time_equal(candidate, stored.tag); } } // namespace diff --git a/include/webcpp/hash/hmac.hpp b/include/webcpp/hash/hmac.hpp index 06e9175..6bf57c8 100644 --- a/include/webcpp/hash/hmac.hpp +++ b/include/webcpp/hash/hmac.hpp @@ -8,15 +8,15 @@ Boost.Hash2's HMAC under webcpp::hash's names: HMAC, RFC 2104, over each of Hash2's cryptographic algorithms. - A program checks a MAC with `webcpp::crypto::equal`, never with `digest::operator==`, which - does not run in constant time: + A program checks a MAC with `webcpp::crypto::constant_time_equal`, never with + `digest::operator==`, which does not run in constant time: @code // key, message: the shared key and the message received; tag: the tag received with it, // a webcpp::hash::hmac_sha2_256::result_type webcpp::hash::hmac_sha2_256 mac(key.data(), key.size()); mac.update(message.data(), message.size()); - bool genuine = webcpp::crypto::equal(mac.result(), tag); + bool genuine = webcpp::crypto::constant_time_equal(mac.result(), tag); @endcode @see "HMAC", in the guide. diff --git a/include/webcpp/hash/mac.hpp b/include/webcpp/hash/mac.hpp index b09b44f..7bc90bf 100644 --- a/include/webcpp/hash/mac.hpp +++ b/include/webcpp/hash/mac.hpp @@ -16,14 +16,14 @@ the MAC accepts and the chosen backend does not `unsupported_by_backend`. A guard's refusal leaves the output as it was; a backend's failure zeroes it. - A program checks a tag with `webcpp::crypto::equal`, which runs in constant time, never with - `==`: + A program checks a tag with `webcpp::crypto::constant_time_equal`, which runs in constant time, + never with `==`: @code std::array tag{}; auto done = webcpp::hash::mac>( tag, key, message); - bool genuine = done && webcpp::crypto::equal(tag, received_tag); + bool genuine = done && webcpp::crypto::constant_time_equal(tag, received_tag); @endcode @see "Message authentication", in the guide.