396 lines
13 KiB
C++
396 lines
13 KiB
C++
#include <Void/Crypto/Crypto.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
int hex_nibble(char c)
|
|
{
|
|
if (c >= '0' && c <= '9') {
|
|
return c - '0';
|
|
}
|
|
if (c >= 'a' && c <= 'f') {
|
|
return c - 'a' + 10;
|
|
}
|
|
if (c >= 'A' && c <= 'F') {
|
|
return c - 'A' + 10;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
std::vector<std::byte> from_hex(std::string_view hex)
|
|
{
|
|
Void::Testing::require(hex.size() % 2 == 0);
|
|
std::vector<std::byte> out;
|
|
out.reserve(hex.size() / 2);
|
|
for (std::size_t i = 0; i < hex.size(); i += 2) {
|
|
const int hi = hex_nibble(hex[i]);
|
|
const int lo = hex_nibble(hex[i + 1]);
|
|
Void::Testing::require(hi >= 0 && lo >= 0);
|
|
out.push_back(static_cast<std::byte>((hi << 4) | lo));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string to_hex(std::span<const std::byte> data)
|
|
{
|
|
static constexpr char kHex[] = "0123456789abcdef";
|
|
std::string out;
|
|
out.reserve(data.size() * 2);
|
|
for (const std::byte b : data) {
|
|
const auto v = static_cast<unsigned char>(b);
|
|
out.push_back(kHex[v >> 4]);
|
|
out.push_back(kHex[v & 0x0F]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::span<const std::byte> as_bytes(std::string_view text)
|
|
{
|
|
return std::as_bytes(std::span{text.data(), text.size()});
|
|
}
|
|
|
|
std::string hotp(std::span<const std::byte> secret, std::uint64_t counter,
|
|
int digits)
|
|
{
|
|
std::array<std::byte, 8> message{};
|
|
for (std::size_t i = 0; i < message.size(); ++i) {
|
|
message[message.size() - 1 - i] =
|
|
static_cast<std::byte>((counter >> (8 * i)) & 0xFFU);
|
|
}
|
|
auto mac = Void::Crypto::hmac_sha1(secret, message);
|
|
Void::Testing::require(mac.has_value());
|
|
const auto offset = static_cast<std::size_t>(
|
|
std::to_integer<unsigned>(mac->back()) & 0x0FU);
|
|
const std::uint32_t binary =
|
|
((std::to_integer<std::uint32_t>((*mac)[offset]) & 0x7FU) << 24U) |
|
|
((std::to_integer<std::uint32_t>((*mac)[offset + 1]) & 0xFFU)
|
|
<< 16U) |
|
|
((std::to_integer<std::uint32_t>((*mac)[offset + 2]) & 0xFFU)
|
|
<< 8U) |
|
|
(std::to_integer<std::uint32_t>((*mac)[offset + 3]) & 0xFFU);
|
|
std::uint32_t modulus = 1;
|
|
for (int i = 0; i < digits; ++i) {
|
|
modulus *= 10U;
|
|
}
|
|
std::string code = std::to_string(binary % modulus);
|
|
while (code.size() < static_cast<std::size_t>(digits)) {
|
|
code.insert(code.begin(), '0');
|
|
}
|
|
return code;
|
|
}
|
|
|
|
std::string totp(std::span<const std::byte> secret, std::uint64_t unix_time,
|
|
int digits)
|
|
{
|
|
return hotp(secret, unix_time / 30U, digits);
|
|
}
|
|
} // namespace
|
|
|
|
Void::Testing::Test crypto_hmac_sha1_rfc2202_vectors()
|
|
{
|
|
using namespace Void::Crypto;
|
|
|
|
const std::array<std::string_view, 6> macs = {
|
|
"b617318655057264e28bc0b6fb378c8ef146be00",
|
|
"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
|
|
"125d7342b9ac11cd91a39af48aa17b4f63f175d3",
|
|
"4c9007f4026250c6bc8414f9bf50c86c2d7235da",
|
|
"aa4ae5e15272d00e95705637ce8a3b55ed402112",
|
|
"e8e99d0f45237d786d6bbaa7965c7808bbff1a91"};
|
|
|
|
const std::vector<std::byte> twenty_0b(20, std::byte{0x0b});
|
|
const std::vector<std::byte> twenty_aa(20, std::byte{0xaa});
|
|
const std::vector<std::byte> eighty_aa(80, std::byte{0xaa});
|
|
const std::vector<std::byte> fifty_dd(50, std::byte{0xdd});
|
|
const std::vector<std::byte> fifty_cd(50, std::byte{0xcd});
|
|
const auto key4 =
|
|
from_hex("0102030405060708090a0b0c0d0e0f10111213141516171819");
|
|
|
|
// Keys/messages that are not pure hex are supplied directly.
|
|
struct RawVector {
|
|
std::span<const std::byte> key;
|
|
std::span<const std::byte> data;
|
|
std::string_view mac_hex;
|
|
};
|
|
const std::array<RawVector, 6> raw = {{
|
|
{twenty_0b, as_bytes("Hi There"), macs[0]},
|
|
{as_bytes("Jefe"), as_bytes("what do ya want for nothing?"),
|
|
macs[1]},
|
|
{twenty_aa, fifty_dd, macs[2]},
|
|
{key4, fifty_cd, macs[3]},
|
|
{eighty_aa,
|
|
as_bytes("Test Using Larger Than Block-Size Key - Hash Key First"),
|
|
macs[4]},
|
|
{eighty_aa,
|
|
as_bytes("Test Using Larger Than Block-Size Key and Larger Than One "
|
|
"Block-Size Data"),
|
|
macs[5]},
|
|
}};
|
|
|
|
const ProviderOptions builtin{.prefer_builtin = true};
|
|
for (const auto& vec : raw) {
|
|
for (const auto& opts : {ProviderOptions{}, builtin}) {
|
|
if (opts.prefer_builtin == false && !has_openssl_backend()) {
|
|
continue;
|
|
}
|
|
auto mac = hmac_sha1(vec.key, vec.data, opts);
|
|
Void::Testing::require(mac.has_value());
|
|
Void::Testing::expect_eq(to_hex(*mac), std::string{vec.mac_hex});
|
|
}
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test crypto_hmac_sha1_boundaries_and_guards()
|
|
{
|
|
using namespace Void::Crypto;
|
|
|
|
// Empty key and message are valid.
|
|
auto empty = hmac_sha1({}, {});
|
|
Void::Testing::require(empty.has_value());
|
|
Void::Testing::expect_eq(empty->size(), HmacSha1Size);
|
|
|
|
// Wrong output size is rejected before touching the buffer.
|
|
std::array<std::byte, HmacSha1Size + 1> wrong_size{};
|
|
auto bad_size =
|
|
hmac_sha1_into(as_bytes("key"), as_bytes("msg"), wrong_size);
|
|
Void::Testing::require(!bad_size.has_value());
|
|
Void::Testing::expect_eq(
|
|
bad_size.error().code().value,
|
|
static_cast<std::int32_t>(CryptoErrorCode::invalid_argument));
|
|
|
|
// Overlapping input/output is rejected.
|
|
std::array<std::byte, 64> shared{};
|
|
shared.fill(std::byte{0x5A});
|
|
auto overlap = hmac_sha1_into(std::span{shared}.subspan(0, 20),
|
|
std::span{shared}.subspan(20, 20),
|
|
std::span{shared}.subspan(0, 20));
|
|
Void::Testing::require(!overlap.has_value());
|
|
Void::Testing::expect_eq(
|
|
overlap.error().code().value,
|
|
static_cast<std::int32_t>(CryptoErrorCode::overlapping_buffers));
|
|
|
|
// Injected backend fault wipes the destination.
|
|
std::array<std::byte, HmacSha1Size> wiped{};
|
|
wiped.fill(std::byte{0xCC});
|
|
{
|
|
ScopedFaultGuard fault{FaultConfig{FaultOperation::hmac,
|
|
CryptoErrorCode::provider_error, 0}};
|
|
auto failed = hmac_sha1_into(as_bytes("key"), as_bytes("msg"), wiped);
|
|
Void::Testing::require(!failed.has_value());
|
|
}
|
|
Void::Testing::expect(
|
|
std::all_of(wiped.begin(), wiped.end(),
|
|
[](std::byte b) { return b == std::byte{0}; }));
|
|
|
|
// property_query is unavailable on the built-in backend.
|
|
ProviderOptions bad_provider{.property_query = "provider=none",
|
|
.prefer_builtin = true};
|
|
auto unavailable =
|
|
hmac_sha1(as_bytes("key"), as_bytes("msg"), bad_provider);
|
|
Void::Testing::require(!unavailable.has_value());
|
|
Void::Testing::expect_eq(
|
|
unavailable.error().code().value,
|
|
static_cast<std::int32_t>(CryptoErrorCode::provider_error));
|
|
}
|
|
|
|
Void::Testing::Test crypto_hotp_totp_rfc4226_6238_vectors()
|
|
{
|
|
const auto secret = as_bytes("12345678901234567890");
|
|
|
|
// RFC 4226 Appendix D.
|
|
const std::array<std::string_view, 10> hotp_expected = {
|
|
"755224", "287082", "359152", "969429", "338314",
|
|
"254676", "287922", "162583", "399871", "520489"};
|
|
for (std::uint64_t counter = 0; counter < hotp_expected.size(); ++counter) {
|
|
Void::Testing::expect_eq(hotp(secret, counter, 6),
|
|
std::string{hotp_expected[counter]});
|
|
}
|
|
|
|
// RFC 6238 Appendix B (SHA-1 rows, 8 digits).
|
|
struct TotpVector {
|
|
std::uint64_t time;
|
|
std::string_view code;
|
|
};
|
|
const std::array<TotpVector, 6> totp_vectors = {{
|
|
{59, "94287082"},
|
|
{1111111109, "07081804"},
|
|
{1111111111, "14050471"},
|
|
{1234567890, "89005924"},
|
|
{2000000000, "69279037"},
|
|
{20000000000, "65353130"},
|
|
}};
|
|
for (const auto& vec : totp_vectors) {
|
|
Void::Testing::expect_eq(totp(secret, vec.time, 8),
|
|
std::string{vec.code});
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test crypto_pbkdf2_hmac_sha256_rfc7914_vectors()
|
|
{
|
|
using namespace Void::Crypto;
|
|
|
|
const ProviderOptions builtin{.prefer_builtin = true};
|
|
|
|
// RFC 7914 section 11, first vector (c = 1, 64-byte output).
|
|
{
|
|
const auto expected = std::string{
|
|
"55ac046e56e3089fec1691c22544b605f94185216dde0465e68b9d57c20dacbc"
|
|
"49ca9cccf179b645991664b39d77ef317c71b845b1e30bd509112041d3a19783"};
|
|
for (const auto& opts : {ProviderOptions{}, builtin}) {
|
|
if (!opts.prefer_builtin && !has_openssl_backend()) {
|
|
continue;
|
|
}
|
|
auto derived = pbkdf2_hmac_sha256(as_bytes("passwd"),
|
|
as_bytes("salt"), 1, 64, opts);
|
|
Void::Testing::require(derived.has_value());
|
|
Void::Testing::expect_eq(to_hex(*derived), expected);
|
|
}
|
|
}
|
|
|
|
// RFC 7914 section 11, second vector (c = 80000, 64-byte output).
|
|
{
|
|
auto derived = pbkdf2_hmac_sha256(as_bytes("Password"),
|
|
as_bytes("NaCl"), 80000, 64);
|
|
Void::Testing::require(derived.has_value());
|
|
Void::Testing::expect_eq(
|
|
to_hex(*derived),
|
|
std::string{
|
|
"4ddcd8f60b98be21830cee5ef22701f9641a4418d04c0414aeff"
|
|
"08876b34ab56a1d425a1225833549adb841b51c9b3176a272bdebba1d0"
|
|
"78478f62b397f33c8d"});
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test crypto_pbkdf2_hmac_sha256_boundaries_and_guards()
|
|
{
|
|
using namespace Void::Crypto;
|
|
|
|
const auto password = as_bytes("password");
|
|
const auto salt = as_bytes("salt");
|
|
|
|
// Zero and oversized iteration counts are rejected.
|
|
for (const std::uint32_t iterations :
|
|
{0U, CryptoLimits::kMaxPbkdf2Iterations + 1U}) {
|
|
std::array<std::byte, 32> out{};
|
|
auto r = pbkdf2_hmac_sha256_into(password, salt, iterations, out);
|
|
Void::Testing::require(!r.has_value());
|
|
Void::Testing::expect_eq(
|
|
r.error().code().value,
|
|
static_cast<std::int32_t>(CryptoErrorCode::invalid_argument));
|
|
}
|
|
|
|
// Oversized salt and output are rejected without mutating the output.
|
|
{
|
|
std::vector<std::byte> big_salt(CryptoLimits::kMaxPbkdf2SaltBytes + 1U,
|
|
std::byte{0x11});
|
|
std::array<std::byte, 32> out{};
|
|
auto r = pbkdf2_hmac_sha256_into(password, big_salt, 1, out);
|
|
Void::Testing::require(!r.has_value());
|
|
Void::Testing::expect_eq(
|
|
r.error().code().value,
|
|
static_cast<std::int32_t>(CryptoErrorCode::message_too_large));
|
|
|
|
std::vector<std::byte> big_out(CryptoLimits::kMaxPbkdf2OutputBytes + 1U,
|
|
std::byte{0x7A});
|
|
auto r_big = pbkdf2_hmac_sha256_into(password, salt, 1, big_out);
|
|
Void::Testing::require(!r_big.has_value());
|
|
Void::Testing::expect_eq(
|
|
r_big.error().code().value,
|
|
static_cast<std::int32_t>(CryptoErrorCode::output_too_large));
|
|
Void::Testing::expect(
|
|
std::all_of(big_out.begin(), big_out.end(),
|
|
[](std::byte b) { return b == std::byte{0x7A}; }));
|
|
|
|
std::span<std::byte> empty_out;
|
|
auto r_empty = pbkdf2_hmac_sha256_into(password, salt, 1, empty_out);
|
|
Void::Testing::require(!r_empty.has_value());
|
|
Void::Testing::expect_eq(
|
|
r_empty.error().code().value,
|
|
static_cast<std::int32_t>(CryptoErrorCode::invalid_argument));
|
|
|
|
// The Digest overload rejects an overflowing length before allocating.
|
|
auto r_overflow = pbkdf2_hmac_sha256(
|
|
password, salt, 1, std::numeric_limits<std::size_t>::max());
|
|
Void::Testing::require(!r_overflow.has_value());
|
|
Void::Testing::expect_eq(
|
|
r_overflow.error().code().value,
|
|
static_cast<std::int32_t>(CryptoErrorCode::output_too_large));
|
|
}
|
|
|
|
// Overlapping output is rejected.
|
|
{
|
|
std::array<std::byte, 64> shared{};
|
|
auto r = pbkdf2_hmac_sha256_into(std::span{shared}.subspan(0, 16), {},
|
|
1, std::span{shared}.subspan(8, 32));
|
|
Void::Testing::require(!r.has_value());
|
|
Void::Testing::expect_eq(
|
|
r.error().code().value,
|
|
static_cast<std::int32_t>(CryptoErrorCode::overlapping_buffers));
|
|
}
|
|
|
|
// Injected fault wipes the destination.
|
|
{
|
|
std::array<std::byte, 32> wiped{};
|
|
wiped.fill(std::byte{0xCC});
|
|
{
|
|
ScopedFaultGuard fault{FaultConfig{
|
|
FaultOperation::pbkdf2, CryptoErrorCode::provider_error, 0}};
|
|
auto r = pbkdf2_hmac_sha256_into(password, salt, 1, wiped);
|
|
Void::Testing::require(!r.has_value());
|
|
}
|
|
Void::Testing::expect(
|
|
std::all_of(wiped.begin(), wiped.end(),
|
|
[](std::byte b) { return b == std::byte{0}; }));
|
|
}
|
|
|
|
// Empty password and salt are valid; match hashlib reference values.
|
|
{
|
|
const auto expected = std::string{
|
|
"f7ce0b653d2d72a4108cf5abe912ffdd777616dbbb27a70e8204f3ae2d0f6fad"};
|
|
for (const auto& opts :
|
|
{ProviderOptions{}, ProviderOptions{.prefer_builtin = true}}) {
|
|
if (!opts.prefer_builtin && !has_openssl_backend()) {
|
|
continue;
|
|
}
|
|
auto derived = pbkdf2_hmac_sha256({}, {}, 1, 32, opts);
|
|
Void::Testing::require(derived.has_value());
|
|
Void::Testing::expect_eq(to_hex(*derived), expected);
|
|
}
|
|
}
|
|
|
|
// Partial final block and built-in/backend parity.
|
|
{
|
|
auto odd = pbkdf2_hmac_sha256(password, salt, 2, 33);
|
|
Void::Testing::require(odd.has_value());
|
|
Void::Testing::expect_eq(odd->size(), 33U);
|
|
auto prefix = pbkdf2_hmac_sha256(password, salt, 2, 32);
|
|
Void::Testing::require(prefix.has_value());
|
|
Void::Testing::expect(
|
|
std::equal(prefix->begin(), prefix->end(), odd->begin()));
|
|
|
|
auto builtin = pbkdf2_hmac_sha256(
|
|
password, salt, 2, 33, ProviderOptions{.prefer_builtin = true});
|
|
Void::Testing::require(builtin.has_value());
|
|
Void::Testing::expect_eq(*builtin, *odd);
|
|
|
|
auto gitea = pbkdf2_hmac_sha256(as_bytes("password"),
|
|
as_bytes("somesalt"), 10000, 32);
|
|
Void::Testing::require(gitea.has_value());
|
|
Void::Testing::expect_eq(
|
|
to_hex(*gitea),
|
|
std::string{"f47eaaf9fcacaf867696eaa1d205356d2d029bff08828a05d0dab"
|
|
"60b6e0c2de2"});
|
|
}
|
|
}
|