feat(crypto,archive): implement VL-007 built-in hashes and VL-006 archive creation/gzip/tar fallback
This commit is contained in:
1 parent
b50131f52e
commit
22c8538754
19 files changed
+3326
-133
No files matched your search
@@ -12,8 +12,12 @@ int main()
|
||||
.content = {std::byte{'h'}, std::byte{'i'}},
|
||||
},
|
||||
}};
|
||||
Void::Archive::CreationOptions opts;
|
||||
opts.compression = Void::Archive::has_libarchive_backend()
|
||||
? Void::Archive::ArchiveCompression::Zstd
|
||||
: Void::Archive::ArchiveCompression::None;
|
||||
Void::Archive::VectorByteSink sink;
|
||||
if (!Void::Archive::create_archive(sink, entries).has_value()) {
|
||||
if (!Void::Archive::create(sink, entries, opts).has_value()) {
|
||||
return 1;
|
||||
}
|
||||
Void::Archive::SpanByteSource src{sink.bytes()};
|
||||
|
||||
@@ -9,23 +9,33 @@ int main()
|
||||
if (!digest.has_value() || digest->size() != 32) {
|
||||
return 1;
|
||||
}
|
||||
auto key = Void::Crypto::Ed25519PrivateKey::generate();
|
||||
if (!key.has_value()) {
|
||||
return 2;
|
||||
}
|
||||
auto pub = key->public_key();
|
||||
if (!pub.has_value()) {
|
||||
return 3;
|
||||
auto b2 = Void::Crypto::blake2b256("abc");
|
||||
if (!b2.has_value() || b2->size() != 32) {
|
||||
return 6;
|
||||
}
|
||||
const std::array<std::byte, 4> msg{std::byte{1}, std::byte{2}, std::byte{3},
|
||||
std::byte{4}};
|
||||
auto sig = Void::Crypto::sign_ed25519(msg, *key);
|
||||
if (!sig.has_value()) {
|
||||
return 4;
|
||||
auto mac = Void::Crypto::hmac_sha256(msg, msg);
|
||||
if (!mac.has_value() || mac->size() != Void::Crypto::HmacSha256Size) {
|
||||
return 7;
|
||||
}
|
||||
auto ok = Void::Crypto::verify_ed25519(msg, *sig, *pub);
|
||||
if (!ok.has_value() || !*ok) {
|
||||
return 5;
|
||||
if (Void::Crypto::has_openssl_backend()) {
|
||||
auto key = Void::Crypto::Ed25519PrivateKey::generate();
|
||||
if (!key.has_value()) {
|
||||
return 2;
|
||||
}
|
||||
auto pub = key->public_key();
|
||||
if (!pub.has_value()) {
|
||||
return 3;
|
||||
}
|
||||
auto sig = Void::Crypto::sign_ed25519(msg, *key);
|
||||
if (!sig.has_value()) {
|
||||
return 4;
|
||||
}
|
||||
auto ok = Void::Crypto::verify_ed25519(msg, *sig, *pub);
|
||||
if (!ok.has_value() || !*ok) {
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -3,8 +3,10 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
@@ -13,23 +15,95 @@
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
enum class FixtureEntryType { Regular, Directory, Symlink };
|
||||
|
||||
struct ArchiveEntry {
|
||||
std::string_view path;
|
||||
std::string_view contents;
|
||||
unsigned int type{AE_IFREG};
|
||||
FixtureEntryType type{FixtureEntryType::Regular};
|
||||
std::string_view link;
|
||||
};
|
||||
|
||||
void write_archive(const std::filesystem::path &path,
|
||||
std::span<const ArchiveEntry>
|
||||
entries)
|
||||
void write_octal(std::span<char> dst, std::uint64_t val)
|
||||
{
|
||||
dst.back() = '\0';
|
||||
std::size_t pos = dst.size() - 1;
|
||||
while (pos > 0) {
|
||||
--pos;
|
||||
dst[pos] = static_cast<char>('0' + (val & 7ULL));
|
||||
val >>= 3ULL;
|
||||
}
|
||||
}
|
||||
|
||||
void write_raw_tar(const std::filesystem::path& path,
|
||||
std::span<const ArchiveEntry> entries)
|
||||
{
|
||||
std::ofstream out{path, std::ios::binary | std::ios::trunc};
|
||||
Void::Testing::require(out.is_open());
|
||||
|
||||
for (const auto& spec : entries) {
|
||||
std::array<char, 512> hdr{};
|
||||
std::memcpy(hdr.data(), spec.path.data(),
|
||||
std::min<std::size_t>(spec.path.size(), 100));
|
||||
const std::uint32_t perm =
|
||||
spec.type == FixtureEntryType::Directory ? 0755U : 0644U;
|
||||
write_octal(std::span{hdr}.subspan(100, 8), perm);
|
||||
write_octal(std::span{hdr}.subspan(108, 8), 0);
|
||||
write_octal(std::span{hdr}.subspan(116, 8), 0);
|
||||
const std::uint64_t sz =
|
||||
spec.type == FixtureEntryType::Regular ? spec.contents.size()
|
||||
: 0ULL;
|
||||
write_octal(std::span{hdr}.subspan(124, 12), sz);
|
||||
write_octal(std::span{hdr}.subspan(136, 12), 0);
|
||||
std::fill(hdr.begin() + 148, hdr.begin() + 156, ' ');
|
||||
hdr[156] = spec.type == FixtureEntryType::Directory
|
||||
? '5'
|
||||
: (spec.type == FixtureEntryType::Symlink ? '2' : '0');
|
||||
if (!spec.link.empty()) {
|
||||
std::memcpy(hdr.data() + 157, spec.link.data(),
|
||||
std::min<std::size_t>(spec.link.size(), 100));
|
||||
}
|
||||
std::memcpy(hdr.data() + 257, "ustar\0", 6);
|
||||
hdr[263] = '0';
|
||||
hdr[264] = '0';
|
||||
std::uint32_t sum = 0;
|
||||
for (const char ch : hdr) {
|
||||
sum += static_cast<unsigned char>(ch);
|
||||
}
|
||||
write_octal(std::span{hdr}.subspan(148, 7), sum);
|
||||
hdr[155] = ' ';
|
||||
|
||||
out.write(hdr.data(), hdr.size());
|
||||
if (spec.type == FixtureEntryType::Regular && !spec.contents.empty()) {
|
||||
out.write(spec.contents.data(),
|
||||
static_cast<std::streamsize>(spec.contents.size()));
|
||||
const std::size_t rem = spec.contents.size() % 512U;
|
||||
if (rem != 0) {
|
||||
std::array<char, 512> pad{};
|
||||
out.write(pad.data(),
|
||||
static_cast<std::streamsize>(512U - rem));
|
||||
}
|
||||
}
|
||||
}
|
||||
std::array<char, 1024> eof{};
|
||||
out.write(eof.data(), eof.size());
|
||||
out.close();
|
||||
Void::Testing::require(out.good());
|
||||
}
|
||||
|
||||
void write_archive(const std::filesystem::path& path,
|
||||
std::span<const ArchiveEntry> entries)
|
||||
{
|
||||
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
||||
using Writer = std::unique_ptr<archive, decltype(&archive_write_free)>;
|
||||
using Entry =
|
||||
std::unique_ptr<archive_entry, decltype(&archive_entry_free)>;
|
||||
@@ -47,14 +121,21 @@ namespace
|
||||
writer.get(), path.c_str()) == ARCHIVE_OK);
|
||||
#endif
|
||||
|
||||
for (const auto &specification : entries) {
|
||||
for (const auto& specification : entries) {
|
||||
Entry entry{archive_entry_new(), &archive_entry_free};
|
||||
const std::string entryPath{specification.path};
|
||||
archive_entry_set_pathname(entry.get(), entryPath.c_str());
|
||||
archive_entry_set_filetype(entry.get(), specification.type);
|
||||
const unsigned int ae_type =
|
||||
specification.type == FixtureEntryType::Directory
|
||||
? AE_IFDIR
|
||||
: (specification.type == FixtureEntryType::Symlink
|
||||
? AE_IFLNK
|
||||
: AE_IFREG);
|
||||
archive_entry_set_filetype(entry.get(), ae_type);
|
||||
archive_entry_set_perm(
|
||||
entry.get(), specification.type == AE_IFDIR ? 0755 : 0644);
|
||||
if (specification.type == AE_IFREG) {
|
||||
entry.get(),
|
||||
specification.type == FixtureEntryType::Directory ? 0755 : 0644);
|
||||
if (specification.type == FixtureEntryType::Regular) {
|
||||
archive_entry_set_size(
|
||||
entry.get(),
|
||||
static_cast<la_int64_t>(specification.contents.size()));
|
||||
@@ -78,18 +159,22 @@ namespace
|
||||
ARCHIVE_OK);
|
||||
}
|
||||
Void::Testing::require(archive_write_close(writer.get()) == ARCHIVE_OK);
|
||||
#else
|
||||
write_raw_tar(path, entries);
|
||||
#endif
|
||||
}
|
||||
|
||||
void write_archive(const std::filesystem::path &path,
|
||||
void write_archive(const std::filesystem::path& path,
|
||||
std::string_view entryPath, std::string_view contents,
|
||||
unsigned int type = AE_IFREG, std::string_view link = {})
|
||||
FixtureEntryType type = FixtureEntryType::Regular,
|
||||
std::string_view link = {})
|
||||
{
|
||||
const std::array entries{ArchiveEntry{entryPath, contents, type, link}};
|
||||
write_archive(path, entries);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool
|
||||
extraction_file_visible(const std::filesystem::path &destination)
|
||||
extraction_file_visible(const std::filesystem::path& destination)
|
||||
{
|
||||
std::error_code error;
|
||||
if (std::filesystem::exists(destination / "anchor", error)) {
|
||||
@@ -129,8 +214,9 @@ Void::Testing::Test archive_accepts_explicit_directory_entries()
|
||||
Void::FileSystem::TempDir temporary{"void_archive_"};
|
||||
const auto source = temporary / "directory.tar.zst";
|
||||
const auto destination = temporary / "output";
|
||||
const std::array entries{ArchiveEntry{"bin/", {}, AE_IFDIR},
|
||||
ArchiveEntry{"bin/tool", "payload"}};
|
||||
const std::array entries{
|
||||
ArchiveEntry{"bin/", {}, FixtureEntryType::Directory},
|
||||
ArchiveEntry{"bin/tool", "payload"}};
|
||||
write_archive(source, entries);
|
||||
|
||||
auto extracted = Void::Archive::extract(source, destination);
|
||||
@@ -154,7 +240,8 @@ Void::Testing::Test archive_rejects_traversal_and_links()
|
||||
Void::Testing::expect(
|
||||
!std::filesystem::exists(temporary.path() / "escape"));
|
||||
const auto symlink = temporary / "symlink.tar.zst";
|
||||
write_archive(symlink, "link", {}, AE_IFLNK, "/tmp/escape");
|
||||
write_archive(symlink, "link", {}, FixtureEntryType::Symlink,
|
||||
"/tmp/escape");
|
||||
Void::Testing::expect(
|
||||
!Void::Archive::extract(symlink, temporary / "out2").has_value());
|
||||
const auto trailingFile = temporary / "trailing-file.tar.zst";
|
||||
|
||||
@@ -3,12 +3,16 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
@@ -16,6 +20,8 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <sys/stat.h>
|
||||
#include <thread>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
@@ -39,6 +45,72 @@ namespace
|
||||
std::istreambuf_iterator<char>{}};
|
||||
}
|
||||
|
||||
void write_octal(std::span<std::byte> dst, std::uint64_t val)
|
||||
{
|
||||
dst.back() = std::byte{0};
|
||||
std::size_t pos = dst.size() - 1;
|
||||
while (pos > 0) {
|
||||
--pos;
|
||||
dst[pos] = static_cast<std::byte>('0' + (val & 7ULL));
|
||||
val >>= 3ULL;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::byte> make_raw_tar_ustar(
|
||||
std::span<const std::tuple<std::string_view, std::string_view, char,
|
||||
std::string_view>>
|
||||
entries)
|
||||
{
|
||||
std::vector<std::byte> out;
|
||||
for (const auto& [path, content, typeflag, link] : entries) {
|
||||
std::array<std::byte, 512> hdr{};
|
||||
std::memcpy(hdr.data(), path.data(),
|
||||
std::min<std::size_t>(path.size(), 100));
|
||||
write_octal(std::span{hdr}.subspan(100, 8),
|
||||
typeflag == '5' ? 0755ULL : 0644ULL);
|
||||
write_octal(std::span{hdr}.subspan(108, 8), 0);
|
||||
write_octal(std::span{hdr}.subspan(116, 8), 0);
|
||||
const std::uint64_t sz = (typeflag == '0') ? content.size() : 0ULL;
|
||||
write_octal(std::span{hdr}.subspan(124, 12), sz);
|
||||
write_octal(std::span{hdr}.subspan(136, 12), 0);
|
||||
std::fill(hdr.begin() + 148, hdr.begin() + 156, std::byte{' '});
|
||||
hdr[156] = static_cast<std::byte>(typeflag);
|
||||
if (!link.empty()) {
|
||||
std::memcpy(hdr.data() + 157, link.data(),
|
||||
std::min<std::size_t>(link.size(), 100));
|
||||
}
|
||||
std::memcpy(hdr.data() + 257, "ustar\0", 6);
|
||||
hdr[263] = std::byte{'0'};
|
||||
hdr[264] = std::byte{'0'};
|
||||
std::uint32_t sum = 0;
|
||||
for (const std::byte b : hdr) {
|
||||
sum += static_cast<std::uint8_t>(b);
|
||||
}
|
||||
write_octal(std::span{hdr}.subspan(148, 7), sum);
|
||||
hdr[155] = std::byte{' '};
|
||||
|
||||
out.insert(out.end(), hdr.begin(), hdr.end());
|
||||
if (typeflag == '0' && !content.empty()) {
|
||||
const auto cb =
|
||||
std::as_bytes(std::span{content.data(), content.size()});
|
||||
out.insert(out.end(), cb.begin(), cb.end());
|
||||
const std::size_t rem = content.size() % 512U;
|
||||
if (rem != 0) {
|
||||
out.resize(out.size() + (512U - rem), std::byte{0});
|
||||
}
|
||||
}
|
||||
}
|
||||
out.resize(out.size() + 1024U, std::byte{0});
|
||||
return out;
|
||||
}
|
||||
|
||||
ArchiveCompression default_test_compression() noexcept
|
||||
{
|
||||
return has_libarchive_backend() ? ArchiveCompression::Zstd
|
||||
: ArchiveCompression::None;
|
||||
}
|
||||
|
||||
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
||||
std::vector<std::byte> make_raw_tar(
|
||||
std::span<const std::tuple<std::string_view, std::string_view,
|
||||
unsigned int, std::string_view>>
|
||||
@@ -95,6 +167,7 @@ namespace
|
||||
Void::Testing::require(archive_write_close(writer.get()) == ARCHIVE_OK);
|
||||
return out;
|
||||
}
|
||||
#endif
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
|
||||
@@ -116,7 +189,7 @@ Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
|
||||
|
||||
VectorByteSink sink_a;
|
||||
VectorByteSink sink_b;
|
||||
CreationOptions copts{.compression = ArchiveCompression::Zstd,
|
||||
CreationOptions copts{.compression = default_test_compression(),
|
||||
.reproducible = true};
|
||||
Void::Testing::require(create_archive(sink_a, order_a, copts).has_value());
|
||||
Void::Testing::require(create_archive(sink_b, order_b, copts).has_value());
|
||||
@@ -173,8 +246,26 @@ Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
|
||||
Void::Testing::expect(zero_entries.validate().has_value());
|
||||
}
|
||||
|
||||
// 4. File-as-parent conflict rejected cleanly (both orderings).
|
||||
// 4. File-as-parent conflict rejected cleanly (pure USTAR works with and
|
||||
// without LibArchive; additionally exercise zstd when LibArchive exists).
|
||||
{
|
||||
const std::array<
|
||||
std::tuple<std::string_view, std::string_view, char,
|
||||
std::string_view>,
|
||||
2>
|
||||
conflict_ustar{{
|
||||
{"foo", "file-bytes", '0', ""},
|
||||
{"foo/child", "nested", '0', ""},
|
||||
}};
|
||||
const auto raw_ustar = make_raw_tar_ustar(conflict_ustar);
|
||||
SpanByteSource src{raw_ustar};
|
||||
auto listed = list_entries(src, {}, false);
|
||||
Void::Testing::require(!listed.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
listed.error().code().value,
|
||||
static_cast<std::int32_t>(ArchiveErrorCode::unsafe_entry));
|
||||
|
||||
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
||||
const std::array<
|
||||
std::tuple<std::string_view, std::string_view, unsigned int,
|
||||
std::string_view>,
|
||||
@@ -184,26 +275,27 @@ Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
|
||||
{"foo/child", "nested", AE_IFREG, ""},
|
||||
}};
|
||||
const auto raw = make_raw_tar(conflict_entries);
|
||||
SpanByteSource src{raw};
|
||||
auto listed = list_entries(src, {}, false);
|
||||
Void::Testing::require(!listed.has_value());
|
||||
SpanByteSource src_la{raw};
|
||||
auto listed_la = list_entries(src_la, {}, false);
|
||||
Void::Testing::require(!listed_la.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
listed.error().code().value,
|
||||
listed_la.error().code().value,
|
||||
static_cast<std::int32_t>(ArchiveErrorCode::unsafe_entry));
|
||||
#endif
|
||||
}
|
||||
|
||||
// 5. Normal vs forensic listing + sanitized diagnostics.
|
||||
{
|
||||
const std::array<
|
||||
std::tuple<std::string_view, std::string_view, unsigned int,
|
||||
std::tuple<std::string_view, std::string_view, char,
|
||||
std::string_view>,
|
||||
3>
|
||||
mixed{{
|
||||
{"safe.txt", "ok", AE_IFREG, ""},
|
||||
{"bad\nlink", "", AE_IFLNK, "/etc/passwd"},
|
||||
{"../escape", "nope", AE_IFREG, ""},
|
||||
mixed_ustar{{
|
||||
{"safe.txt", "ok", '0', ""},
|
||||
{"bad\nlink", "", '2', "/etc/passwd"},
|
||||
{"../escape", "nope", '0', ""},
|
||||
}};
|
||||
const auto raw = make_raw_tar(mixed);
|
||||
const auto raw = make_raw_tar_ustar(mixed_ustar);
|
||||
|
||||
SpanByteSource src1{raw};
|
||||
Void::Testing::expect(!list_entries(src1, {}, false).has_value());
|
||||
@@ -226,10 +318,123 @@ Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
|
||||
std::string{"a\\nb\\rc"});
|
||||
}
|
||||
|
||||
Void::Testing::Test archive_create_formats_builtin_ustar_pax_and_gzip()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_archive_formats_"};
|
||||
|
||||
// Built-in pure-C++ USTAR/PAX creation works regardless of LibArchive,
|
||||
// handles long PAX paths (>120 bytes), custom canonical metadata, and
|
||||
// produces byte-identical output across runs and entry permutations.
|
||||
const std::string long_path =
|
||||
"very/deep/nested/package/hierarchy/with/many/subdirectories/"
|
||||
"extending/well/past/one/hundred/bytes/component/artifact.txt";
|
||||
const std::array<CreateEntry, 3> entries_a{{
|
||||
CreateEntry{.path = long_path,
|
||||
.type = EntryType::RegularFile,
|
||||
.executable = false,
|
||||
.content = to_bytes("pax-long-path-content")},
|
||||
CreateEntry{.path = "bin/tool",
|
||||
.type = EntryType::RegularFile,
|
||||
.executable = false,
|
||||
.content = to_bytes("tool-bytes"),
|
||||
.mode = 0755U},
|
||||
CreateEntry{.path = "bin/", .type = EntryType::Directory},
|
||||
}};
|
||||
const std::array<CreateEntry, 3> entries_b{
|
||||
entries_a[2], entries_a[0], entries_a[1]};
|
||||
|
||||
CreationOptions builtin_opts{
|
||||
.compression = ArchiveCompression::None,
|
||||
.reproducible = true,
|
||||
.canonical_mtime = 1700000000,
|
||||
.canonical_uid = 1000,
|
||||
.canonical_gid = 1000,
|
||||
.prefer_builtin_tar = true,
|
||||
};
|
||||
|
||||
VectorByteSink raw1;
|
||||
VectorByteSink raw2;
|
||||
Void::Testing::require(create(raw1, entries_a, builtin_opts).has_value());
|
||||
Void::Testing::require(create(raw2, entries_b, builtin_opts).has_value());
|
||||
Void::Testing::expect_eq(raw1.bytes(), raw2.bytes());
|
||||
|
||||
const auto tar_file = tmp / "builtin.tar";
|
||||
Void::Testing::require(create(tar_file, entries_a, builtin_opts).has_value());
|
||||
auto extracted = extract(tar_file, tmp / "unpacked_tar");
|
||||
Void::Testing::require(extracted.has_value());
|
||||
Void::Testing::expect_eq(read_file_str(tmp / "unpacked_tar" / long_path),
|
||||
std::string{"pax-long-path-content"});
|
||||
Void::Testing::expect_eq(read_file_str(tmp / "unpacked_tar/bin/tool"),
|
||||
std::string{"tool-bytes"});
|
||||
|
||||
struct stat st_tool{};
|
||||
Void::Testing::require(::stat((tmp / "unpacked_tar/bin/tool").c_str(),
|
||||
&st_tool) == 0);
|
||||
Void::Testing::expect_eq(st_tool.st_mode & 0777, mode_t{0755});
|
||||
|
||||
// Staged extraction over built-in tar also succeeds (exercising
|
||||
// LibArchive interop when LibArchive is linked, or built-in Reader when
|
||||
// LibArchive is absent).
|
||||
auto staged_tar = extract_staged(tar_file, tmp / "staged_tar");
|
||||
Void::Testing::require(staged_tar.has_value());
|
||||
auto pub_tar = staged_tar->publish("v1");
|
||||
Void::Testing::require(pub_tar.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
read_file_str(tmp / "staged_tar/v1" / long_path),
|
||||
std::string{"pax-long-path-content"});
|
||||
|
||||
if (has_libarchive_backend()) {
|
||||
CreationOptions gzip_opts{
|
||||
.compression = ArchiveCompression::Gzip,
|
||||
.reproducible = true,
|
||||
.canonical_mtime = 1700000000,
|
||||
};
|
||||
VectorByteSink gz1;
|
||||
VectorByteSink gz2;
|
||||
Void::Testing::require(create(gz1, entries_a, gzip_opts).has_value());
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{15});
|
||||
Void::Testing::require(create(gz2, entries_b, gzip_opts).has_value());
|
||||
Void::Testing::expect_eq(gz1.bytes(), gz2.bytes());
|
||||
|
||||
const auto gz_path = tmp / "bundle.tar.gz";
|
||||
Void::Testing::require(
|
||||
create(gz_path, entries_a, gzip_opts).has_value());
|
||||
auto gz_ext = extract(gz_path, tmp / "unpacked_gz");
|
||||
Void::Testing::require(gz_ext.has_value());
|
||||
Void::Testing::expect_eq(read_file_str(tmp / "unpacked_gz" / long_path),
|
||||
std::string{"pax-long-path-content"});
|
||||
|
||||
auto gz_staged = extract_staged(gz_path, tmp / "staged_gz");
|
||||
Void::Testing::require(gz_staged.has_value());
|
||||
Void::Testing::require(gz_staged->publish("v1").has_value());
|
||||
Void::Testing::expect_eq(
|
||||
read_file_str(tmp / "staged_gz/v1/bin/tool"),
|
||||
std::string{"tool-bytes"});
|
||||
} else {
|
||||
VectorByteSink unused;
|
||||
auto zst_err =
|
||||
create(unused, entries_a,
|
||||
CreationOptions{.compression = ArchiveCompression::Zstd});
|
||||
Void::Testing::require(!zst_err.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
zst_err.error().code().value,
|
||||
static_cast<std::int32_t>(ArchiveErrorCode::unsupported_format));
|
||||
|
||||
auto gz_err =
|
||||
create(unused, entries_a,
|
||||
CreationOptions{.compression = ArchiveCompression::Gzip});
|
||||
Void::Testing::require(!gz_err.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
gz_err.error().code().value,
|
||||
static_cast<std::int32_t>(ArchiveErrorCode::unsupported_format));
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test archive_staged_extraction_atomic_publish_unknown_and_cleanup()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_archive_staged_"};
|
||||
const auto archive_path = tmp / "pkg.tar.zst";
|
||||
const auto archive_path =
|
||||
tmp / (has_libarchive_backend() ? "pkg.tar.zst" : "pkg.tar");
|
||||
const auto parent_dir = tmp / "versions";
|
||||
std::filesystem::create_directories(parent_dir);
|
||||
|
||||
@@ -243,7 +448,9 @@ Void::Testing::Test archive_staged_extraction_atomic_publish_unknown_and_cleanup
|
||||
.executable = false,
|
||||
.content = to_bytes("key=val")},
|
||||
}};
|
||||
Void::Testing::require(create_archive(archive_path, entries).has_value());
|
||||
const CreationOptions create_opts{.compression = default_test_compression()};
|
||||
Void::Testing::require(
|
||||
create_archive(archive_path, entries, create_opts).has_value());
|
||||
|
||||
// 1. Staged extraction + atomic no-replace publication + safe modes.
|
||||
std::size_t progress_calls = 0;
|
||||
@@ -344,7 +551,8 @@ Void::Testing::Test archive_isolated_worker_extracts_verifies_and_publishes()
|
||||
{
|
||||
#ifdef VOID_ARCHIVE_WORKER_PATH
|
||||
Void::FileSystem::TempDir tmp{"void_archive_iso_"};
|
||||
const auto archive_path = tmp / "untrusted.tar.zst";
|
||||
const auto archive_path =
|
||||
tmp / (has_libarchive_backend() ? "untrusted.tar.zst" : "untrusted.tar");
|
||||
const auto parent_dir = tmp / "out";
|
||||
std::filesystem::create_directories(parent_dir);
|
||||
|
||||
@@ -358,7 +566,10 @@ Void::Testing::Test archive_isolated_worker_extracts_verifies_and_publishes()
|
||||
.executable = false,
|
||||
.content = to_bytes("verified-by-parent")},
|
||||
}};
|
||||
Void::Testing::require(create_archive(archive_path, entries).has_value());
|
||||
Void::Testing::require(
|
||||
create_archive(archive_path, entries,
|
||||
CreationOptions{.compression = default_test_compression()})
|
||||
.has_value());
|
||||
|
||||
IsolationOptions iso{};
|
||||
iso.worker_executable = VOID_ARCHIVE_WORKER_PATH;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -20,19 +21,122 @@ 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{"ba7816bf8f01cfea414140de5dae2223b0036"
|
||||
"1a396177a9cb410ff61f20015ad"});
|
||||
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<const std::byte>{};
|
||||
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()
|
||||
@@ -50,8 +154,45 @@ Void::Testing::Test crypto_streaming_and_file_hash_match()
|
||||
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()
|
||||
@@ -67,6 +208,15 @@ Void::Testing::Test crypto_ed25519_rfc8032_vector()
|
||||
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<std::int32_t>(
|
||||
Void::Crypto::CryptoErrorCode::unsupported_algorithm));
|
||||
return;
|
||||
}
|
||||
auto valid = Void::Crypto::verifyEd25519({}, signature, publicKey);
|
||||
Void::Testing::require(valid.has_value());
|
||||
Void::Testing::expect(*valid);
|
||||
|
||||
@@ -123,6 +123,35 @@ Void::Testing::Test crypto_ed25519_signing_vectors_and_boundaries()
|
||||
{
|
||||
using namespace Void::Crypto;
|
||||
|
||||
if (!has_openssl_backend()) {
|
||||
auto gen_unavail = Ed25519PrivateKey::generate();
|
||||
Void::Testing::require(!gen_unavail.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
gen_unavail.error().code().value,
|
||||
static_cast<std::int32_t>(CryptoErrorCode::unsupported_algorithm));
|
||||
|
||||
const std::array<std::byte, Ed25519PublicKeySize> dummy_pub{};
|
||||
const std::array<std::byte, Ed25519SignatureSize> dummy_sig{};
|
||||
for (const std::size_t bad_key_len : {0U, 1U, 31U, 33U, 64U}) {
|
||||
std::vector<std::byte> bad_key(bad_key_len, std::byte{0x11});
|
||||
auto r = verifyEd25519({}, dummy_sig, bad_key);
|
||||
Void::Testing::require(!r.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
r.error().code().value,
|
||||
static_cast<std::int32_t>(CryptoErrorCode::invalid_key_size));
|
||||
}
|
||||
for (const std::size_t bad_sig_len : {0U, 1U, 63U, 65U}) {
|
||||
std::vector<std::byte> bad_sig(bad_sig_len, std::byte{0x22});
|
||||
auto r = verifyEd25519({}, bad_sig, dummy_pub);
|
||||
Void::Testing::require(!r.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
r.error().code().value,
|
||||
static_cast<std::int32_t>(
|
||||
CryptoErrorCode::invalid_signature_size));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
struct Rfc8032Vector {
|
||||
std::string_view seed_hex;
|
||||
std::string_view pub_hex;
|
||||
@@ -357,6 +386,30 @@ Void::Testing::Test crypto_hmac_sha256_and_hkdf_sha256_vectors_and_guards()
|
||||
[](std::byte b) { return b == std::byte{0x00}; }));
|
||||
}
|
||||
|
||||
// Also verify built-in HMAC-SHA-256 and HKDF-SHA-256 produce identical
|
||||
// outputs without OpenSSL.
|
||||
{
|
||||
const ProviderOptions builtin_opts{.prefer_builtin = true};
|
||||
const std::vector<std::byte> key(20, std::byte{0x0b});
|
||||
auto mac = hmac_sha256(key, as_bytes("Hi There"), builtin_opts);
|
||||
Void::Testing::require(mac.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
to_hex(*mac),
|
||||
std::string{"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e93"
|
||||
"76c2e32cff7"});
|
||||
|
||||
const std::vector<std::byte> ikm(22, std::byte{0x0b});
|
||||
const auto salt = from_hex("000102030405060708090a0b0c");
|
||||
const auto info = from_hex("f0f1f2f3f4f5f6f7f8f9");
|
||||
std::array<std::byte, 42> okm{};
|
||||
auto r = hkdf_sha256(ikm, salt, info, okm, builtin_opts);
|
||||
Void::Testing::require(r.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
to_hex(okm),
|
||||
std::string{"3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02"
|
||||
"d56ecc4c5bf34007208d5b887185865"});
|
||||
}
|
||||
|
||||
// constant_time_equal checks
|
||||
const auto a = from_hex("01020304");
|
||||
const auto b = from_hex("01020304");
|
||||
|
||||
Reference in new issue
Block a user