#include #include #include #include #include #include #include #include namespace { std::string hex(const Void::Crypto::Digest& digest) { return Void::Encoding::Hex::encode(digest); } } // namespace Void::Testing::Test crypto_known_hash_vectors() { auto sha1 = Void::Crypto::sha1("abc"); auto sha256 = Void::Crypto::sha256("abc"); auto sha512 = Void::Crypto::sha512("abc"); auto b2_256 = Void::Crypto::blake2b256("abc"); auto b2_512 = Void::Crypto::blake2b512("abc"); Void::Testing::require(sha1.has_value()); Void::Testing::require(sha256.has_value()); Void::Testing::require(sha512.has_value()); Void::Testing::require(b2_256.has_value()); Void::Testing::require(b2_512.has_value()); Void::Testing::expect_eq( hex(*sha1), std::string{"a9993e364706816aba3e25717850c26c9cd0d89d"}); Void::Testing::expect_eq( hex(*sha256), std::string{"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f" "20015ad"}); Void::Testing::expect_eq( hex(*sha512), std::string{ "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a21" "92992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"}); Void::Testing::expect_eq( hex(*b2_256), std::string{"bddd813c634239723171ef3fee98579b94964e3bb1cb3e427262c8c06" "8d52319"}); Void::Testing::expect_eq( hex(*b2_512), std::string{ "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d" "87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"}); // NIST CAVP multi-block & empty vectors on built-in backend and OpenSSL // backend (when available). const Void::Crypto::ProviderOptions builtin_opts{.prefer_builtin = true}; const auto empty_bytes = std::span{}; auto b_sha256_empty = Void::Crypto::hash(Void::Crypto::HashAlgorithm::Sha256, empty_bytes, builtin_opts); Void::Testing::require(b_sha256_empty.has_value()); Void::Testing::expect_eq( hex(*b_sha256_empty), std::string{"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7" "852b855"}); constexpr std::string_view k448 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; auto b_sha256_448 = Void::Crypto::hash( Void::Crypto::HashAlgorithm::Sha256, std::as_bytes(std::span{k448.data(), k448.size()}), builtin_opts); Void::Testing::require(b_sha256_448.has_value()); Void::Testing::expect_eq( hex(*b_sha256_448), std::string{"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd41" "9db06c1"}); constexpr std::string_view k896 = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno" "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; auto b_sha512_896 = Void::Crypto::hash( Void::Crypto::HashAlgorithm::Sha512, std::as_bytes(std::span{k896.data(), k896.size()}), builtin_opts); Void::Testing::require(b_sha512_896.has_value()); Void::Testing::expect_eq( hex(*b_sha512_896), std::string{ "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb688901850" "1d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"}); auto b_b2_256_empty = Void::Crypto::hash(Void::Crypto::HashAlgorithm::Blake2b256, empty_bytes, builtin_opts); auto b_b2_512_empty = Void::Crypto::hash(Void::Crypto::HashAlgorithm::Blake2b512, empty_bytes, builtin_opts); Void::Testing::require(b_b2_256_empty.has_value()); Void::Testing::require(b_b2_512_empty.has_value()); Void::Testing::expect_eq( hex(*b_b2_256_empty), std::string{"0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf" "12fe3a8"}); Void::Testing::expect_eq( hex(*b_b2_512_empty), std::string{ "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d2" "5e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce"}); // 1,000,000 x 'a' FIPS 180-4 SHA-256 test vector on built-in backend. { auto h = Void::Crypto::Hasher::create(Void::Crypto::HashAlgorithm::Sha256, builtin_opts); Void::Testing::require(h.has_value()); const std::string chunk(1000, 'a'); for (int i = 0; i < 1000; ++i) { Void::Testing::require(h->update(chunk).has_value()); } auto big = h->finalize(); Void::Testing::require(big.has_value()); Void::Testing::expect_eq( hex(*big), std::string{"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39c" "cc7112cd0"}); } // Verify OpenSSL & built-in parity when OpenSSL is linked. if (Void::Crypto::has_openssl_backend()) { for (const auto alg : {Void::Crypto::HashAlgorithm::Sha1, Void::Crypto::HashAlgorithm::Sha256, Void::Crypto::HashAlgorithm::Sha512, Void::Crypto::HashAlgorithm::Blake2b512}) { auto d_ossl = Void::Crypto::hash( alg, std::as_bytes(std::span{k896.data(), k896.size()})); auto d_builtin = Void::Crypto::hash( alg, std::as_bytes(std::span{k896.data(), k896.size()}), builtin_opts); Void::Testing::require(d_ossl.has_value()); Void::Testing::require(d_builtin.has_value()); Void::Testing::expect_eq(*d_ossl, *d_builtin); } } } Void::Testing::Test crypto_streaming_and_file_hash_match() { auto hasher = Void::Crypto::Hasher::create(Void::Crypto::HashAlgorithm::Sha256); Void::Testing::require(hasher.has_value()); Void::Testing::require(hasher->update("ab").has_value()); Void::Testing::require(hasher->update("c").has_value()); auto streamed = hasher->finalize(); Void::Testing::require(streamed.has_value()); Void::Testing::expect(!hasher->update("again").has_value()); Void::FileSystem::TempDir temporary{"void_crypto_"}; const auto path = temporary / "data"; std::ofstream{path, std::ios::binary} << "abc"; auto file = Void::Crypto::sha256File(path); auto file_snake = Void::Crypto::sha256_file(path); Void::Testing::require(file.has_value()); Void::Testing::require(file_snake.has_value()); Void::Testing::expect_eq(*file, *streamed); Void::Testing::expect_eq(*file_snake, *streamed); // Odd-size streaming checks across block boundaries for built-in SHA-256 & // BLAKE2b (1, 7, 63, 64, 65, 127, 128, 129, 257). const std::string sample(300, 'x'); const Void::Crypto::ProviderOptions builtin_opts{.prefer_builtin = true}; for (const auto alg : {Void::Crypto::HashAlgorithm::Sha256, Void::Crypto::HashAlgorithm::Sha512, Void::Crypto::HashAlgorithm::Blake2b256, Void::Crypto::HashAlgorithm::Blake2b512}) { auto one_shot = Void::Crypto::hash( alg, std::as_bytes(std::span{sample.data(), sample.size()}), builtin_opts); Void::Testing::require(one_shot.has_value()); for (const std::size_t step : {1U, 7U, 63U, 64U, 65U, 127U, 128U, 129U}) { auto sh = Void::Crypto::Hasher::create(alg, builtin_opts); Void::Testing::require(sh.has_value()); for (std::size_t off = 0; off < sample.size(); off += step) { const auto len = std::min(step, sample.size() - off); Void::Testing::require( sh->update(std::string_view{sample}.substr(off, len)) .has_value()); } auto fin = sh->finalize(); Void::Testing::require(fin.has_value()); Void::Testing::expect_eq(*fin, *one_shot); } } auto b2_256_f = Void::Crypto::blake2b256_file(path); auto b2_512_f = Void::Crypto::blake2b512_file(path); Void::Testing::require(b2_256_f.has_value()); Void::Testing::require(b2_512_f.has_value()); Void::Testing::expect_eq(*b2_256_f, *Void::Crypto::blake2b256("abc")); Void::Testing::expect_eq(*b2_512_f, *Void::Crypto::blake2b512("abc")); } Void::Testing::Test crypto_ed25519_rfc8032_vector() { const auto publicText = Void::Encoding::Hex::decode( "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"); const auto signatureText = Void::Encoding::Hex::decode( "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e06522490155" "5fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"); Void::Testing::require(publicText.has_value()); Void::Testing::require(signatureText.has_value()); const auto publicKey = std::as_bytes(std::span{publicText->data(), publicText->size()}); const auto signature = std::as_bytes(std::span{signatureText->data(), signatureText->size()}); if (!Void::Crypto::has_openssl_backend()) { auto unavail = Void::Crypto::verifyEd25519({}, signature, publicKey); Void::Testing::require(!unavail.has_value()); Void::Testing::expect_eq( unavail.error().code().value, static_cast( Void::Crypto::CryptoErrorCode::unsupported_algorithm)); return; } auto valid = Void::Crypto::verifyEd25519({}, signature, publicKey); Void::Testing::require(valid.has_value()); Void::Testing::expect(*valid); const std::array message{std::byte{1}}; auto invalid = Void::Crypto::verifyEd25519(message, signature, publicKey); Void::Testing::require(invalid.has_value()); Void::Testing::expect(!*invalid); }