Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions doc/digests.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ guesses per second on a graphics card. Passwords go through Argon2id
(<<hash-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`
(<<hash-hmac>>).
* *Other backends, by name.* `backends::bearssl`, `backends::openssl` and
`backends::botan` compute SHA-2 too, never as a default: a call names one
Expand Down Expand Up @@ -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<macs::blake2b_mac>`
(<<hash-blake2b-mac-sodium>>); 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 (<<hash-backends-dit>>); without one, none does.
tags with `webcpp::crypto::constant_time_equal`, and prefer
`mac<macs::blake2b_mac>` (<<hash-blake2b-mac-sodium>>); 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 (<<hash-backends-dit>>); 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.
Expand Down
2 changes: 1 addition & 1 deletion doc/generated/choose.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
| <<hash-hmac,hmac_sha2_256, checked with webcpp::crypto::equal>>
| <<hash-hmac,hmac_sha2_256, checked with webcpp::crypto::constant_time_equal>>
| `hash2`: `/webcpp/hash//hash`
| none: one backend
| `example/hmac_sha256.cpp`
Expand Down
2 changes: 1 addition & 1 deletion doc/guide.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion doc/llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 13 additions & 10 deletions doc/macs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
(<<hash-password-hashing>>). A key longer than the hash's block is hashed
Expand Down Expand Up @@ -154,7 +154,8 @@ include::{examples}/kmac.expected[]

*Pitfalls.*

* *Compare with `webcpp::crypto::equal`.* As for every tag (<<hash-hmac>>).
* *Compare with `webcpp::crypto::constant_time_equal`.* As for every tag
(<<hash-hmac>>).
* *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
Expand Down Expand Up @@ -196,7 +197,8 @@ include::{examples}/mac_family.expected[]

*Pitfalls.*

* *Compare with `webcpp::crypto::equal`.* As for every tag (<<hash-hmac>>).
* *Compare with `webcpp::crypto::constant_time_equal`.* As for every tag
(<<hash-hmac>>).
* *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<digests::sha2_512>` cut to 32
Expand Down Expand Up @@ -274,7 +276,7 @@ include::{examples}/hmac_sodium.expected[]
*Pitfalls.*

* *The pitfalls of <<hash-hmac>> 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<digests::sha2_512>`
and a 32-byte output; one that follows a standard naming HMAC over
Expand Down Expand Up @@ -307,7 +309,8 @@ include::{examples}/blake2b_mac_sodium.expected[]

*Pitfalls.*

* *Compare with `webcpp::crypto::equal`.* As for every tag (<<hash-hmac>>).
* *Compare with `webcpp::crypto::constant_time_equal`.* As for every tag
(<<hash-hmac>>).
* *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.
Expand Down
6 changes: 3 additions & 3 deletions doc/overview.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<hash-departures>>. 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
Expand Down
4 changes: 2 additions & 2 deletions doc/password_hashing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<<hash-privacy-passwords>>).
* *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 (<<hash-backend-conditions>>).
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions doc/use-cases/privacy.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions example/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 : <library>/webcpp/crypto//crypto ;
webcpp.example siphash.cpp : <library>/webcpp/crypto//crypto ;
webcpp.example mac_family.cpp : <library>/webcpp/crypto//crypto ;
Expand Down
6 changes: 3 additions & 3 deletions example/blake2b_mac_sodium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// tag::example[]
#include "hex.hpp"

#include <webcpp/crypto/equal.hpp>
#include <webcpp/crypto/constant_time_equal.hpp>
#include <webcpp/hash/mac.hpp>

#include <array>
Expand Down Expand Up @@ -41,8 +41,8 @@ int main() {

// The receiver computes the tag again and compares it in constant time.
std::array<std::uint8_t, 32> again{};
bool const genuine =
hash::mac<blake2b_mac>(again, key, message) && webcpp::crypto::equal(again, tag);
bool const genuine = hash::mac<blake2b_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.
Expand Down
9 changes: 5 additions & 4 deletions example/hmac_sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// tag::example[]
#include "hex.hpp"

#include <webcpp/crypto/equal.hpp>
#include <webcpp/crypto/constant_time_equal.hpp>
#include <webcpp/hash/hmac.hpp>

#include <iostream>
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions example/hmac_sodium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// tag::example[]
#include "hex.hpp"

#include <webcpp/crypto/equal.hpp>
#include <webcpp/crypto/constant_time_equal.hpp>
#include <webcpp/hash/mac.hpp>

#include <array>
Expand Down Expand Up @@ -42,7 +42,7 @@ int main() {
// The receiver computes the tag again and compares it in constant time.
std::array<std::uint8_t, 32> again{};
bool const genuine = hash::mac<hmac<digests::sha2_256>, 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
Expand Down
7 changes: 4 additions & 3 deletions example/kmac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <webcpp/crypto/equal.hpp>
#include <webcpp/crypto/constant_time_equal.hpp>
#include <webcpp/hash/mac.hpp>

#include <array>
Expand Down Expand Up @@ -39,7 +40,7 @@ int main() {
std::array<std::uint8_t, 32> expected{};
bool const genuine =
hash::mac<kmac_128>(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.
Expand Down
7 changes: 4 additions & 3 deletions example/mac_family.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <webcpp/crypto/equal.hpp>
#include <webcpp/crypto/constant_time_equal.hpp>
#include <webcpp/hash/mac.hpp>

#include <array>
Expand All @@ -36,7 +37,7 @@ int main() {
// The receiver computes the tag again and compares it in constant time, never with ==.
std::array<std::uint8_t, 32> expected{};
bool const genuine = hash::mac<hmac_sha256>(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
Expand Down
4 changes: 2 additions & 2 deletions example/password_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// tag::example[]
#include "hex.hpp"

#include <webcpp/crypto/equal.hpp>
#include <webcpp/crypto/constant_time_equal.hpp>
#include <webcpp/crypto/random_device.hpp>
#include <webcpp/hash/argon2.hpp>

Expand Down Expand Up @@ -61,7 +61,7 @@ boost::system::result<bool> 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
Expand Down
6 changes: 3 additions & 3 deletions include/webcpp/hash/hmac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<N>::operator==`, which
does not run in constant time:
A program checks a MAC with `webcpp::crypto::constant_time_equal`, never with
`digest<N>::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.
Expand Down
6 changes: 3 additions & 3 deletions include/webcpp/hash/mac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint8_t, 32> tag{};
auto done = webcpp::hash::mac<webcpp::hash::macs::hmac<webcpp::hash::digests::sha2_256>>(
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.
Expand Down
Loading