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
20 changes: 14 additions & 6 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,24 @@ specific to hash.
without.
- Every function that computes on a key, a password or a key derivation's secret input holds
`secure::timing_guard` (webcpp::secure's, `<webcpp/secure/timing_guard.hpp>`) as its first
statement (`timing{engaged}` where the arguments decide whether the call is keyed); unkeyed
digests and Boost.Hash2's types by alias hold none. A new keyed function, or a keyed cell that
`doc/capabilities.json` gains (a MAC, a key derivation, a password hash, on any backend), holds
the guard and gets its call in that backend's `test/timing_coverage_<backend>_test.cpp`, which
fails until it does. The seam support (`test/support/timing_seam.hpp`) counts hash's own guards
by their file, under `include/webcpp/hash/`, in `timing_entries`, and every guard in
statement (`timing_scope{seed_value != 0}` where the arguments decide whether the call is keyed);
unkeyed digests and Boost.Hash2's types by alias hold none. A new keyed function, or a keyed cell
that `doc/capabilities.json` gains (a MAC, a key derivation, a password hash, on any backend),
holds the guard and gets its call in that backend's `test/timing_coverage_<backend>_test.cpp`,
which fails until it does. The seam support (`test/support/timing_seam.hpp`) counts hash's own
guards by their file, under `include/webcpp/hash/`, in `timing_entries`, and every guard in
`timing_all_entries`.
- Every wipe of a secret or of a failed output is `secure::erase_secret` over a span
(`<webcpp/secure/erase_secret.hpp>`); a test sees each one through secure's seam
`WEBCPP_SECURE_TEST_WIPE_RECORD` (`test/support/wipe_record.hpp`). hash depends on config and
secure, no other webcpp library.
- `include/webcpp/hash/detail/canary.h` is a C header: it is in neither the headers-alone list nor
`meta/include-boundaries.json`.
- Every local and parameter of a C++ function in `include/` takes two words, the role then the kind
(`timing_scope`, `salt_bytes`, `completion_token`): MSVC's C4459 at /W4 /WX stops a user's program
that has a global of a one-word name a header's function declares (a template's only where it is
instantiated). `test/user_globals_test.cpp` declares a global of each one-word name a header once
had, then calls every cell. HKDF's public functions keep `salt`, the name the change hkdf-pieces
gives them: a function template's parameter raises no C4459. The XKCP adapter's C declarations
(`detail/xkcp.hpp`) keep `src/xkcp_adapter.c`'s names: a declaration without a body raises no
C4459.
10 changes: 6 additions & 4 deletions doc/async.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,12 @@ to the synchronous function.

HKDF's three asynchronous functions are those of <<hash-hkdf>>, the
pseudorandom key written into a span of the digest's size:
`async_hkdf_extract(executor, pseudorandom_key, salt, input_key_material,
token)`, `async_hkdf_expand(executor, output_key_material, pseudorandom_key,
application_info, token)` and `async_hkdf(executor, output_key_material, salt,
input_key_material, application_info, token)`. Each also has a form over
`async_hkdf_extract(completion_executor, pseudorandom_key, salt,
input_key_material, completion_token)`,
`async_hkdf_expand(completion_executor, output_key_material, pseudorandom_key,
application_info, completion_token)` and `async_hkdf(completion_executor,
output_key_material, salt, input_key_material, application_info,
completion_token)`. Each also has a form over
`byte_pieces` (<<hash-hkdf-pieces>>), with no limit on the pieces, whose
inputs are read in the call: on a synchronous backend the synchronous function
over pieces runs in the call itself and only its error code is posted, and on
Expand Down
93 changes: 48 additions & 45 deletions include/webcpp/hash/argon2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ template <class Backend = default_backend<kdfs::argon2id>>
std::span<std::uint8_t const> password_bytes,
std::span<std::uint8_t const> salt_bytes,
argon2id_parameters const& cost_parameters) {
secure::timing_guard const timing;
auto guarded = detail::argon2id_guards<Backend>(output_bytes.size(), password_bytes.size(),
salt_bytes.size(), cost_parameters);
if (!guarded) {
return guarded;
secure::timing_guard const timing_scope;
auto guard_result = detail::argon2id_guards<Backend>(output_bytes.size(), password_bytes.size(),
salt_bytes.size(), cost_parameters);
if (!guard_result) {
return guard_result;
}
auto derive_result = detail::argon2id_engine<Backend>::derive(
output_bytes, password_bytes, salt_bytes, cost_parameters.passes, cost_parameters.memory);
Expand Down Expand Up @@ -217,20 +217,22 @@ inline constexpr std::uint64_t argon2_min_salt = 8;
The backend's limits within RFC 9106's: whether `Engine` takes these values.

@param output_size The tag's size. @param salt_size The salt's size.
@param parameters The parameters.
@param cost_parameters The parameters.
@return Whether the engine's `limits` hold them all.
*/
template <class Engine>
bool argon2_engine_takes(std::size_t output_size, std::size_t salt_size,
argon2_parameters const& parameters) noexcept {
argon2_parameters const& cost_parameters) noexcept {
// Each bound through exceeds: a limit at the type's own bound would be a tautology.
constexpr argon2_limits limits = Engine::limits;
return !exceeds(limits.min_passes, parameters.passes) &&
!exceeds(parameters.memory_kibibytes, limits.max_memory_kibibytes) &&
!exceeds(parameters.lanes, limits.max_lanes) &&
!exceeds(limits.min_output, output_size) && !exceeds(limits.min_salt, salt_size) &&
!exceeds(salt_size, limits.max_salt) && (limits.secret || parameters.secret.empty()) &&
(limits.associated_data || parameters.associated_data.empty());
constexpr argon2_limits engine_limits = Engine::limits;
return !exceeds(engine_limits.min_passes, cost_parameters.passes) &&
!exceeds(cost_parameters.memory_kibibytes, engine_limits.max_memory_kibibytes) &&
!exceeds(cost_parameters.lanes, engine_limits.max_lanes) &&
!exceeds(engine_limits.min_output, output_size) &&
!exceeds(engine_limits.min_salt, salt_size) &&
!exceeds(salt_size, engine_limits.max_salt) &&
(engine_limits.secret || cost_parameters.secret.empty()) &&
(engine_limits.associated_data || cost_parameters.associated_data.empty());
}

/**
Expand All @@ -239,13 +241,13 @@ bool argon2_engine_takes(std::size_t output_size, std::size_t salt_size,

@tparam Variant The variant. @tparam Backend The backend.
@param output_size The tag's size. @param password_size The password's size.
@param salt_size The salt's size. @param parameters The parameters.
@param salt_size The salt's size. @param cost_parameters The parameters.
@return Success, or the error of the first guard that fails, as `argon2` returns it.
*/
template <class Variant, class Backend>
[[nodiscard]] boost::system::result<void> argon2_guards(
std::size_t output_size, std::size_t password_size, std::size_t salt_size,
argon2_parameters const& parameters) noexcept {
argon2_parameters const& cost_parameters) noexcept {
using engine = argon2_engine<Backend, Variant>;
if (exceeds(argon2_min_output, output_size) || exceeds(output_size, argon2id_max_size)) {
return error::invalid_output_size;
Expand All @@ -254,16 +256,17 @@ template <class Variant, class Backend>
return error::invalid_salt_size;
}
if (exceeds(password_size, argon2id_max_size) ||
exceeds(parameters.secret.size(), argon2id_max_size) ||
exceeds(parameters.associated_data.size(), argon2id_max_size)) {
exceeds(cost_parameters.secret.size(), argon2id_max_size) ||
exceeds(cost_parameters.associated_data.size(), argon2id_max_size)) {
return error::input_too_long;
}
if (parameters.passes < 1 || parameters.lanes < 1 || parameters.lanes > argon2_max_lanes ||
parameters.memory_kibibytes <
argon2_min_memory_kibibytes_per_lane * std::uint64_t{parameters.lanes}) {
if (cost_parameters.passes < 1 || cost_parameters.lanes < 1 ||
cost_parameters.lanes > argon2_max_lanes ||
cost_parameters.memory_kibibytes <
argon2_min_memory_kibibytes_per_lane * std::uint64_t{cost_parameters.lanes}) {
return error::invalid_parameters;
}
if (!argon2_engine_takes<engine>(output_size, salt_size, parameters)) {
if (!argon2_engine_takes<engine>(output_size, salt_size, cost_parameters)) {
return error::unsupported_by_backend;
}
return {};
Expand All @@ -272,42 +275,42 @@ template <class Variant, class Backend>
} // namespace detail

/**
Argon2 version 0x13 (RFC 9106) of the variant `Variant` with every parameter: fills `output` from
`password` and `salt`.
Argon2 version 0x13 (RFC 9106) of the variant `Variant` with every parameter: fills `output_bytes`
from `password_bytes` and `salt_bytes`.

Every guard of RFC 9106 runs first, then the backend's own limits (`unsupported_by_backend`), and
a refusal leaves `output` as it was; a backend's failure zeroes it.
a refusal leaves `output_bytes` as it was; a backend's failure zeroes it.

@tparam Variant `kdfs::argon2id` (recommended for passwords), `kdfs::argon2i` or `kdfs::argon2d`.
@tparam Backend The backend; by default @ref default_backend of `Variant` (spec 4.1):
`backends::sodium` for Argon2id and Argon2i, none for Argon2d, which names its backend
(`backends::openssl`, which has every variant with every parameter, or `backends::botan`, which
has every variant with at most 128 lanes and 8 GiB of memory, 2 GiB on wasm32).
@param output The tag, 4 to 4,294,967,295 bytes; it must not overlap the inputs.
@param password The password, at most 4,294,967,295 bytes.
@param salt The salt, 8 to 4,294,967,295 bytes (16 random bytes are the usual choice).
@param parameters The costs, the lanes, the secret and the associated data.
@param output_bytes The tag, 4 to 4,294,967,295 bytes; it must not overlap the inputs.
@param password_bytes The password, at most 4,294,967,295 bytes.
@param salt_bytes The salt, 8 to 4,294,967,295 bytes (16 random bytes are the usual choice).
@param cost_parameters The costs, the lanes, the secret and the associated data.
@return Success; or the first guard's error (`invalid_output_size`, `invalid_salt_size`,
`input_too_long`, `invalid_parameters`, `unsupported_by_backend`), `output` untouched; or
`errc::not_enough_memory` or `backend_failure`, `output` zeroed.
`input_too_long`, `invalid_parameters`, `unsupported_by_backend`), `output_bytes` untouched; or
`errc::not_enough_memory` or `backend_failure`, `output_bytes` zeroed.
*/
template <class Variant, class Backend = default_backend<Variant>>
[[nodiscard]] boost::system::result<void> argon2(std::span<std::uint8_t> output,
std::span<std::uint8_t const> password,
std::span<std::uint8_t const> salt,
argon2_parameters const& parameters) {
secure::timing_guard const timing;
auto guarded = detail::argon2_guards<Variant, Backend>(output.size(), password.size(),
salt.size(), parameters);
if (!guarded) {
return guarded;
[[nodiscard]] boost::system::result<void> argon2(std::span<std::uint8_t> output_bytes,
std::span<std::uint8_t const> password_bytes,
std::span<std::uint8_t const> salt_bytes,
argon2_parameters const& cost_parameters) {
secure::timing_guard const timing_scope;
auto guard_result = detail::argon2_guards<Variant, Backend>(
output_bytes.size(), password_bytes.size(), salt_bytes.size(), cost_parameters);
if (!guard_result) {
return guard_result;
}
auto derived =
detail::argon2_engine<Backend, Variant>::derive(output, password, salt, parameters);
if (!derived) {
secure::erase_secret(output);
auto derive_result = detail::argon2_engine<Backend, Variant>::derive(
output_bytes, password_bytes, salt_bytes, cost_parameters);
if (!derive_result) {
secure::erase_secret(output_bytes);
}
return derived;
return derive_result;
}

} // namespace webcpp::hash
Expand Down
Loading
Loading