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
6 changes: 2 additions & 4 deletions include/webcpp/hash/backends/bearssl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <span>

#ifdef WEBCPP_HASH_BEARSSL_CANARY_CONFIGURATION
Expand Down Expand Up @@ -493,8 +492,7 @@ struct hkdf_pieces_engine<backends::bearssl, Hmac> {

/** The most bytes of one update. */
static constexpr std::uint64_t slice_max = hkdf_pieces_slice<backends::bearssl>;
static_assert(slice_max <= std::numeric_limits<std::size_t>::max(),
"br_hmac_update takes every slice's length");
static_assert(slice_max <= SIZE_MAX, "br_hmac_update takes every slice's length");

/**
Feeds every byte of `input_pieces` to `mac_context`, in slices of at most @ref slice_max bytes.
Expand Down Expand Up @@ -570,7 +568,7 @@ struct hkdf_pieces_engine<backends::bearssl, Hmac> {
secure::erase_secret(std::span(&mac_context, 1));
if (call_succeeded) {
std::size_t const copied_count =
std::min(t_block.size(), output_key_material.size() - written_count);
(std::min)(t_block.size(), output_key_material.size() - written_count);
std::copy_n(t_block.data(), copied_count,
output_key_material.data() + written_count);
written_count += copied_count;
Expand Down
8 changes: 3 additions & 5 deletions include/webcpp/hash/backends/botan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <limits>
#include <memory>
#include <new>
#include <span>
Expand Down Expand Up @@ -154,7 +153,7 @@ struct backend_limits<backends::botan> {
/** PBKDF2's output. */
static constexpr std::uint64_t pbkdf2_output = no_limit;
/** PBKDF2's iteration count, a `size_t` of Botan's (`PBKDF2_Family::from_params`). */
static constexpr std::uint64_t pbkdf2_iterations = std::numeric_limits<std::size_t>::max();
static constexpr std::uint64_t pbkdf2_iterations = SIZE_MAX;
/** Argon2id's password. */
static constexpr std::uint64_t argon2id_password = no_limit;
/** Argon2id's salt. */
Expand Down Expand Up @@ -1354,8 +1353,7 @@ struct hkdf_pieces_engine<backends::botan, Hmac> {

/** The most bytes of one update. */
static constexpr std::uint64_t slice_max = hkdf_pieces_slice<backends::botan>;
static_assert(slice_max <= std::numeric_limits<std::size_t>::max(),
"Botan's update takes every slice's length");
static_assert(slice_max <= SIZE_MAX, "Botan's update takes every slice's length");

/**
Makes Botan's HMAC over the hmac's hash, "HMAC(<hash>)", into `mac_object`; Botan throws
Expand Down Expand Up @@ -1443,7 +1441,7 @@ struct hkdf_pieces_engine<backends::botan, Hmac> {
mac_instance.update(static_cast<std::uint8_t>(block_counter));
mac_instance.final(std::span<std::uint8_t>(t_block.data(), t_block.size()));
std::size_t const copied_count =
std::min(t_block.size(), output_key_material.size() - written_count);
(std::min)(t_block.size(), output_key_material.size() - written_count);
std::copy_n(t_block.data(), copied_count,
output_key_material.data() + written_count);
written_count += copied_count;
Expand Down
8 changes: 4 additions & 4 deletions include/webcpp/hash/backends/hash2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct hkdf_engine<backends::hash2, Hmac> {
t_block = mac_state.result();
secure::erase_secret(std::span(&mac_state, 1));
const std::size_t copied_count =
std::min(t_block.size(), output_key_material.size() - written_count);
(std::min)(t_block.size(), output_key_material.size() - written_count);
std::memcpy(output_key_material.data() + written_count, t_block.data(), copied_count);
written_count += copied_count;
}
Expand Down Expand Up @@ -220,7 +220,7 @@ struct hkdf_pieces_engine<backends::hash2, Hmac> {
t_block = mac_state.result();
secure::erase_secret(std::span(&mac_state, 1));
const std::size_t copied_count =
std::min(t_block.size(), output_key_material.size() - written_count);
(std::min)(t_block.size(), output_key_material.size() - written_count);
std::memcpy(output_key_material.data() + written_count, t_block.data(), copied_count);
written_count += copied_count;
}
Expand Down Expand Up @@ -293,7 +293,7 @@ struct pbkdf2_engine<backends::hash2, Hmac> {
}
secure::erase_secret(std::span(&mac_state, 1));
const std::size_t copied_count =
std::min(t_block.size(), output_bytes.size() - written_count);
(std::min)(t_block.size(), output_bytes.size() - written_count);
std::memcpy(output_bytes.data() + written_count, t_block.data(), copied_count);
written_count += copied_count;
}
Expand Down Expand Up @@ -343,7 +343,7 @@ struct hash2_digest_engine : full_engine {
state& digest_state, std::span<std::uint8_t> output_bytes) noexcept {
while (!output_bytes.empty()) {
auto result_bytes = digest_state.result();
auto const taken_size = std::min(output_bytes.size(), result_bytes.size());
auto const taken_size = (std::min)(output_bytes.size(), result_bytes.size());
std::memcpy(output_bytes.data(), result_bytes.data(), taken_size);
secure::erase_secret(std::span(&result_bytes, 1));
output_bytes = output_bytes.subspan(taken_size);
Expand Down
12 changes: 5 additions & 7 deletions include/webcpp/hash/backends/openssl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
#include <climits>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <span>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -665,8 +664,7 @@ struct hkdf_pieces_engine<backends::openssl, Hmac> {

/** The most bytes of one update. */
static constexpr std::uint64_t slice_max = hkdf_pieces_slice<backends::openssl>;
static_assert(slice_max <= std::numeric_limits<std::size_t>::max(),
"EVP_MAC_update takes every slice's length");
static_assert(slice_max <= SIZE_MAX, "EVP_MAC_update takes every slice's length");

/**
A context of `mac_algorithm` keyed by `key_bytes` with the hmac's digest (`EVP_MAC_CTX_new`,
Expand Down Expand Up @@ -787,7 +785,7 @@ struct hkdf_pieces_engine<backends::openssl, Hmac> {
EVP_MAC_CTX_free(block_context);
if (call_succeeded) {
std::size_t const copied_count =
std::min(t_block.size(), output_key_material.size() - written_count);
(std::min)(t_block.size(), output_key_material.size() - written_count);
std::copy_n(t_block.data(), copied_count,
output_key_material.data() + written_count);
written_count += copied_count;
Expand Down Expand Up @@ -852,8 +850,8 @@ struct pbkdf2_engine<backends::openssl, Hmac> {
The most memory OpenSSL's Argon2 takes, in KiB: m is a `uint32_t`, and the memory, m blocks of
1 KiB, a `size_t` (4,194,303 KiB on wasm32, where a larger m overflows it).
*/
inline constexpr std::uint32_t openssl_argon2_max_memory_kibibytes = static_cast<std::uint32_t>(
std::min<std::uint64_t>(0xffffffffULL, std::numeric_limits<std::size_t>::max() / 1024));
inline constexpr std::uint32_t openssl_argon2_max_memory_kibibytes =
static_cast<std::uint32_t>(std::min<std::uint64_t>(0xffffffffULL, SIZE_MAX / 1024));

/**
One Argon2 of OpenSSL, version 0x13 (its default), with one thread: OpenSSL's Argon2 runs its
Expand Down Expand Up @@ -1028,7 +1026,7 @@ struct scrypt_engine<backends::openssl> {
std::span<std::uint8_t> output_bytes, std::span<std::uint8_t const> password_bytes,
std::span<std::uint8_t const> salt_bytes, std::uint64_t cost_n, std::uint32_t block_size_r,
std::uint32_t parallelism_p) noexcept {
std::uint64_t memory_limit = std::numeric_limits<std::uint64_t>::max();
std::uint64_t memory_limit = UINT64_MAX;
std::array<OSSL_PARAM, 7> ossl_params{
openssl_octets("pass", password_bytes),
openssl_octets("salt", salt_bytes),
Expand Down
7 changes: 3 additions & 4 deletions include/webcpp/hash/backends/sodium.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@
#include <algorithm>
#include <array>
#include <cerrno>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <span>

#ifdef WEBCPP_HASH_SODIUM_CANARY_CONFIGURATION
Expand Down Expand Up @@ -411,8 +411,7 @@ struct sodium_hkdf_pieces_engine {

/** The most bytes of one update. */
static constexpr std::uint64_t slice_max = hkdf_pieces_slice<backends::sodium>;
static_assert(slice_max <= std::numeric_limits<unsigned long long>::max(),
"libsodium's _update takes every slice's length");
static_assert(slice_max <= ULLONG_MAX, "libsodium's _update takes every slice's length");

/**
Feeds every byte of `input_pieces` to `mac_state`, in slices of at most @ref slice_max bytes.
Expand Down Expand Up @@ -495,7 +494,7 @@ struct sodium_hkdf_pieces_engine {
secure::erase_secret(std::span(&mac_state, 1));
if (call_succeeded) {
std::size_t const copied_count =
std::min(t_block.size(), output_key_material.size() - written_count);
(std::min)(t_block.size(), output_key_material.size() - written_count);
std::copy_n(t_block.data(), copied_count,
output_key_material.data() + written_count);
written_count += copied_count;
Expand Down
3 changes: 2 additions & 1 deletion include/webcpp/hash/backends/webcrypto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,8 @@ class webcrypto_hmac_hkdf_operation {
return;
}
webcrypto_secret tag_secret(uint8_array(promise_value));
std::size_t const block_size = std::min(request_.digest_length, output_.size() - written_);
std::size_t const block_size =
(std::min)(request_.digest_length, output_.size() - written_);
copy(tag_secret.bytes(), written_, block_size);
written_ += block_size;
if (written_ == output_.size()) {
Expand Down
5 changes: 2 additions & 3 deletions include/webcpp/hash/detail/engines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include <cstddef>
#include <cstdint>
#include <limits>
#include <span>
#include <string_view>
#include <type_traits>
Expand Down Expand Up @@ -85,7 +84,7 @@ template <class Mac>
struct mac_traits;

/** No limit beyond the spec's own: the largest 64-bit size. */
inline constexpr std::uint64_t no_limit = std::numeric_limits<std::uint64_t>::max();
inline constexpr std::uint64_t no_limit = UINT64_MAX;

/**
The longest input and output a backend's C library takes, in bytes, where its interface holds a
Expand Down Expand Up @@ -207,7 +206,7 @@ static_assert(hkdf_pieces_slice_seam >= 1,
@ref for_each_slice requires.
*/
template <class Backend>
inline constexpr std::uint64_t hkdf_pieces_update_max = std::numeric_limits<std::size_t>::max();
inline constexpr std::uint64_t hkdf_pieces_update_max = SIZE_MAX;

/**
The most bytes an engine of HKDF over pieces on `Backend` hands its HMAC in one update: the
Expand Down
8 changes: 3 additions & 5 deletions include/webcpp/hash/detail/limits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <cstddef>
#include <cstdint>
#include <limits>
#include <span>

namespace webcpp::hash::detail {
Expand Down Expand Up @@ -42,7 +41,7 @@ inline constexpr std::uint64_t sp800_185_max_bytes = WEBCPP_HASH_DETAIL_TEST_SP8
(`input_too_long` for a string, `invalid_key_size` for a KMAC key, `invalid_output_size` for an
output). The test seam `WEBCPP_HASH_DETAIL_TEST_SP800_185_MAX_BYTES` lowers it.
*/
inline constexpr std::uint64_t sp800_185_max_bytes = std::numeric_limits<std::size_t>::max() / 8;
inline constexpr std::uint64_t sp800_185_max_bytes = SIZE_MAX / 8;
#endif

#ifdef WEBCPP_HASH_DETAIL_TEST_KANGAROOTWELVE_MAX_BYTES
Expand All @@ -60,9 +59,8 @@ inline constexpr std::uint64_t kangarootwelve_max_bytes =
*/
inline constexpr std::uint64_t kangarootwelve_max_bytes =
sizeof(std::size_t) < sizeof(std::uint64_t)
? (std::uint64_t{std::numeric_limits<std::size_t>::max()} * 8192) -
(sizeof(std::size_t) + 1)
: std::numeric_limits<std::uint64_t>::max();
? (std::uint64_t{SIZE_MAX} * 8192) - (sizeof(std::size_t) + 1)
: UINT64_MAX;
#endif

/** The byte @ref nonnull points at for an empty span: a constant, never written. */
Expand Down
2 changes: 1 addition & 1 deletion include/webcpp/hash/keccak.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ inline void keccak_256::update(unsigned char const* input_data, std::size_t inpu
return;
}
if (m_ > 0) {
std::size_t const absorbed_count = std::min(input_size, block_size - m_);
std::size_t const absorbed_count = (std::min)(input_size, block_size - m_);
webcpp_hash_xkcp_add_bytes(state_.data(), input_data, m_, absorbed_count);
input_data += absorbed_count;
input_size -= absorbed_count;
Expand Down
3 changes: 1 addition & 2 deletions include/webcpp/hash/scrypt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

#include <cstddef>
#include <cstdint>
#include <limits>
#include <span>

namespace webcpp::hash {
Expand Down Expand Up @@ -108,7 +107,7 @@ template <class Backend>
}
// 128 r (N + p) bytes must fit size_t: N + p cannot wrap, N being at most 2^63.
constexpr std::uint64_t block_bytes_count = 128;
constexpr std::uint64_t size_max = std::numeric_limits<std::size_t>::max();
constexpr std::uint64_t size_max = SIZE_MAX;
if (exceeds(cost_n + parallelism_p, size_max / block_bytes_count / block_size_r)) {
return error::invalid_parameters;
}
Expand Down
7 changes: 7 additions & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,13 @@ hash-run user_globals : user_globals_test.cpp
<conditional>@botan-native-with-exceptions <toolset>msvc:<cxxflags>/bigobj
: native ;

# A bare <windows.h> before every public header: no header's max or min is
# expanded as the macro (MSVC's C2589 otherwise), with every backend linked.
hash-run windows_h : windows_h_test.cpp
: <library>/webcpp/hash//openssl <library>/third_party//bearssl
<library>/third_party//sodium
: native ;

build-project messages ;

# The canaries at link time, each C library's absence and another commit of
Expand Down
53 changes: 53 additions & 0 deletions test/windows_h_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2026 WebCpp.org
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

// A bare <windows.h> (no NOMINMAX, no WIN32_LEAN_AND_MEAN) before every public header of hash,
// every backend's included, and max and min used as functions after them. A bare <windows.h>
// defines the macros min and max for the whole program, which is what its author asked for: every
// header writes `(std::numeric_limits<T>::max)()` and `(std::min)(a, b)`, which no macro expands.
// Before that, a header that wrote `std::numeric_limits<T>::max()` (detail/limits.hpp) stopped
// MSVC with C2589 where crypto's HPKE included webcpp::hash's HKDF after <windows.h>. Elsewhere
// than Windows the program is the headers and the calls alone.

#ifdef _WIN32
#include <windows.h>
#endif

#include <webcpp/hash.hpp>
#include <webcpp/hash/backends/bearssl.hpp>
#include <webcpp/hash/backends/openssl.hpp>
#include <webcpp/hash/backends/sodium.hpp>
#include <webcpp/hash/hkdf.hpp>
#include <webcpp/hash/kdf.hpp>
#include <webcpp/test/exit_status.hpp>

#include <boost/core/lightweight_test.hpp>

#include <array>
#include <cstdint>
#include <span>

int main() {
// HKDF over pieces and over spans, on its default backend, which reach detail/limits.hpp.
std::array<std::uint8_t, 32> output{};
std::array<std::uint8_t, 4> const salt{1, 2, 3, 4};
std::array<std::uint8_t, 8> const input{5, 6, 7, 8, 9, 10, 11, 12};
BOOST_TEST(webcpp::hash::hkdf<webcpp::hash::hmac_sha2_256>(output, salt,
std::span<std::uint8_t const>(input),
std::span<std::uint8_t const>(salt))
.has_value());
std::array<std::uint8_t, 32> pieces_output{};
std::span<std::uint8_t const> const whole_input(input);
std::span<std::uint8_t const> const info(salt);
BOOST_TEST(webcpp::hash::hkdf<webcpp::hash::hmac_sha2_256>(
pieces_output, salt, {whole_input.first(3), whole_input.subspan(3)},
webcpp::hash::byte_pieces{info})
.has_value());
BOOST_TEST(output == pieces_output);
// The program's own max and min, the macros' or the standard's, still work in parentheses.
BOOST_TEST_EQ((std::max)(1, 2), 2);
return webcpp::test::exit_status(boost::report_errors());
}
Loading