Crypto: Hasher state export/import for the built-in backend (closes #53)
This commit is contained in:
1 parent
217b20e37a
commit
7d6834979e
5 files changed
+325
No files matched your search
@@ -465,3 +465,41 @@ Void::Testing::Test crypto_hmac_sha256_and_hkdf_sha256_vectors_and_guards()
|
||||
Void::Testing::expect(!constant_time_equal(a, c));
|
||||
Void::Testing::expect(!constant_time_equal(a, d));
|
||||
}
|
||||
|
||||
Void::Testing::Test crypto_hasher_state_export_import_round_trips()
|
||||
{
|
||||
using namespace Void::Crypto;
|
||||
const ProviderOptions builtin{.prefer_builtin = true};
|
||||
const std::string prefix{"hello "};
|
||||
const std::string suffix{"world"};
|
||||
const std::string whole = prefix + suffix;
|
||||
|
||||
for (const auto algorithm :
|
||||
{HashAlgorithm::Sha1, HashAlgorithm::Sha256, HashAlgorithm::Sha512,
|
||||
HashAlgorithm::Blake2b256, HashAlgorithm::Blake2b512}) {
|
||||
auto hasher = Hasher::create(algorithm, builtin);
|
||||
Void::Testing::require(hasher.has_value());
|
||||
Void::Testing::require(hasher->update(prefix).has_value());
|
||||
|
||||
auto state = hasher->export_state();
|
||||
Void::Testing::require(state.has_value());
|
||||
|
||||
auto resumed = Hasher::create(algorithm, builtin);
|
||||
Void::Testing::require(resumed.has_value());
|
||||
Void::Testing::require(resumed->import_state(*state).has_value());
|
||||
Void::Testing::require(resumed->update(suffix).has_value());
|
||||
auto resumed_digest = resumed->finalize();
|
||||
Void::Testing::require(resumed_digest.has_value());
|
||||
|
||||
auto expected = hash(algorithm, as_bytes(whole), builtin);
|
||||
Void::Testing::require(expected.has_value());
|
||||
Void::Testing::expect_eq(to_hex(*resumed_digest), to_hex(*expected));
|
||||
}
|
||||
|
||||
// Malformed state is rejected and the hasher fails closed.
|
||||
auto hasher = Hasher::create(HashAlgorithm::Sha256, builtin);
|
||||
Void::Testing::require(hasher.has_value());
|
||||
const std::array<std::byte, 8> garbage{};
|
||||
Void::Testing::expect(!hasher->import_state(garbage).has_value());
|
||||
Void::Testing::expect(hasher->state() == HasherState::Failed);
|
||||
}
|
||||
Reference in new issue
Block a user