#include #include #include #include #include #include #include #include #include #include #include Void::Testing::Test security_secure_buffer_wipe_and_lock_policies() { using namespace Void::Security; const auto wiped_before = SecureBuffer::total_wiped_bytes(); { auto buf = SecureBuffer::from_string("top-secret-key-material"); Void::Testing::require(buf.has_value()); Void::Testing::expect_eq(buf->string_view(), std::string_view{"top-secret-key-material"}); Void::Testing::expect(!buf->is_locked()); auto moved = std::move(*buf); Void::Testing::expect(buf->empty()); Void::Testing::expect_eq(moved.size(), 23U); } Void::Testing::expect(SecureBuffer::total_wiped_bytes() >= wiped_before + 23U); // LockedRequired fails closed on locked allocation failure (no downgrade). { ScopedFaultGuard fault{ FaultConfig{FaultOperation::locked_alloc, SecurityErrorCode::locked_memory_unavailable, 0}}; auto required = SecureBuffer::allocate(64, LockPolicy::LockedRequired); Void::Testing::require(!required.has_value()); Void::Testing::expect_eq( required.error().code().value, static_cast( SecurityErrorCode::locked_memory_unavailable)); auto optional = SecureBuffer::allocate(64, LockPolicy::LockedOptional); Void::Testing::require(optional.has_value()); Void::Testing::expect(!optional->is_locked()); } // Oversized allocation rejected before touching backend. auto huge = SecureBuffer::allocate(SecurityLimits::kMaxSecretPlaintextBytes + 1U); Void::Testing::require(!huge.has_value()); Void::Testing::expect_eq( huge.error().code().value, static_cast(SecurityErrorCode::input_too_large)); } Void::Testing::Test security_input_caps_cost_budget_and_fault_seams() { using namespace Void::Security; // Excessive Argon2id memory/iteration cost rejected BEFORE allocation. const std::string malicious_mem = "$argon2id$v=19$m=4194304,t=3,p=1$AAAAAAAAAAAAAAAAAAAAAA$" "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"; auto parsed = parse_argon2id_cost(malicious_mem); Void::Testing::require(parsed.has_value()); Void::Testing::expect_eq(parsed->memory_kib, 4194304ULL); PasswordVerifyBudget tight_budget{ .max_memory_bytes = 64ULL * 1024ULL * 1024ULL, .max_iterations = 4, .max_parallelism = 2, }; auto rejected = verify_password_budgeted("pw", malicious_mem, tight_budget); Void::Testing::require(!rejected.has_value()); Void::Testing::expect_eq( rejected.error().code().value, static_cast(SecurityErrorCode::cost_budget_exceeded)); // Malformed hash corpus rejected cleanly. for (const std::string_view bad : { "", "$argon2i$v=19$m=65536,t=2,p=1$aaaa$bbbb", "$argon2id$v=18$m=65536,t=2,p=1$aaaa$bbbb", "$argon2id$v=19$m=0,t=2,p=1$aaaa$bbbb", "$argon2id$v=19$m=99999999999999999999,t=2,p=1$aaaa$bbbb", "$argon2id$v=19$m=65536,t=2$aaaa$bbbb", "$argon2id$v=19$m=65536,t=2,p=1$aaaa$", }) { Void::Testing::expect(!parse_argon2id_cost(bad).has_value()); } // Input length caps: >1 KiB password, >512 B token. const std::string long_pw(SecurityLimits::kMaxPasswordBytes + 1U, 'p'); auto pw_err = hash_password(long_pw, PasswordHashStrength::Interactive); Void::Testing::require(!pw_err.has_value()); Void::Testing::expect_eq( pw_err.error().code().value, static_cast(SecurityErrorCode::input_too_large)); const std::string long_tok(SecurityLimits::kMaxTokenBytes + 1U, 't'); auto tok_err = hash_token(long_tok); Void::Testing::require(!tok_err.has_value()); Void::Testing::expect_eq( tok_err.error().code().value, static_cast(SecurityErrorCode::input_too_large)); // Sodium init / randomness failure publishes no token or key. { ScopedFaultGuard fault{FaultConfig{FaultOperation::sodium_init, SecurityErrorCode::backend_error, 0}}; Void::Testing::expect(!random_token().has_value()); Void::Testing::expect(!random_secret_key().has_value()); } { ScopedFaultGuard fault{FaultConfig{FaultOperation::random_bytes, SecurityErrorCode::backend_error, 0}}; Void::Testing::expect(!random_token().has_value()); Void::Testing::expect(!random_secret_key().has_value()); } // Legacy envelope exact byte layout preserved: 24B nonce + 16B MAC + pt. auto key = random_secret_key(); Void::Testing::require(key.has_value()); const std::vector pt{'v', 'o', 'i', 'd'}; auto legacy = seal_secret(pt, *key); Void::Testing::require(legacy.has_value()); Void::Testing::expect_eq(legacy->size(), secret_nonce_bytes + secret_authentication_bytes + pt.size()); auto opened = open_secret(*legacy, *key); Void::Testing::require(opened.has_value()); Void::Testing::expect_eq(*opened, pt); } Void::Testing::Test security_versioned_envelope_rotation_and_no_legacy_fallback() { using namespace Void::Security; SecretKeyRing ring; Void::Testing::require(ring.generate_and_add_key("k1", true).has_value()); Void::Testing::expect_eq(ring.active_key_id().value_or(""), std::string{"k1"}); const std::vector pt{'a', 'p', 'i', '-', 'k', 'e', 'y'}; const std::vector aad{'t', 'e', 'n', 'a', 'n', 't', ':', '1'}; const std::vector wrong_aad{'t', 'e', 'n', 'a', 'n', 't', ':', '2'}; auto env_k1 = ring.seal(pt, aad); Void::Testing::require(env_k1.has_value()); auto hdr = inspect_envelope(*env_k1); Void::Testing::require(hdr.has_value()); Void::Testing::expect_eq(hdr->version, kEnvelopeVersionV1); Void::Testing::expect_eq(hdr->algorithm, kEnvelopeAlgXChaCha20Poly1305Ietf); Void::Testing::expect_eq(hdr->key_id, std::string{"k1"}); Void::Testing::expect_eq(hdr->plaintext_size, static_cast(pt.size())); // Rotate to k2: new writes use k2, old k1 envelopes still decrypt. Void::Testing::require(ring.generate_and_add_key("k2", true).has_value()); Void::Testing::expect_eq(ring.active_key_id().value_or(""), std::string{"k2"}); auto env_k2 = ring.seal(pt, aad); Void::Testing::require(env_k2.has_value()); auto hdr2 = inspect_envelope(*env_k2); Void::Testing::require(hdr2.has_value()); Void::Testing::expect_eq(hdr2->key_id, std::string{"k2"}); auto dec1 = ring.open(*env_k1, aad); auto dec2 = ring.open(*env_k2, aad); Void::Testing::require(dec1.has_value() && dec2.has_value()); Void::Testing::expect(constant_time_equal(dec1->view(), pt)); Void::Testing::expect(constant_time_equal(dec2->view(), pt)); // Wrong AAD fails authentication. auto bad_aad = ring.open(*env_k1, wrong_aad); Void::Testing::require(!bad_aad.has_value()); Void::Testing::expect_eq( bad_aad.error().code().value, static_cast(SecurityErrorCode::authentication_failed)); // Tampering any header/key_id/ciphertext byte fails authentication. auto tampered = *env_k1; tampered.back() ^= 0x80U; Void::Testing::expect(!ring.open(tampered, aad).has_value()); auto bad_ver = *env_k1; bad_ver[4] = 99U; auto ver_res = ring.open(bad_ver, aad); Void::Testing::require(!ver_res.has_value()); Void::Testing::expect_eq( ver_res.error().code().value, static_cast(SecurityErrorCode::unsupported_version)); // Legacy secretbox envelope MUST NOT silently fall back in SecretKeyRing. auto legacy_key = random_secret_key(); Void::Testing::require(legacy_key.has_value()); auto legacy_env = seal_secret(pt, *legacy_key); Void::Testing::require(legacy_env.has_value()); auto no_fallback = ring.open(*legacy_env, {}); Void::Testing::require(!no_fallback.has_value()); Void::Testing::expect_eq( no_fallback.error().code().value, static_cast(SecurityErrorCode::unsupported_version)); // Key retirement guards: cannot retire active key or key still in inventory. const std::vector empty_inv{}; auto retire_active = ring.retire_key("k2", empty_inv); Void::Testing::require(!retire_active.has_value()); Void::Testing::expect_eq( retire_active.error().code().value, static_cast(SecurityErrorCode::key_in_use)); const std::vector still_used{"k1", "k2"}; auto retire_in_use = ring.retire_key("k1", still_used); Void::Testing::require(!retire_in_use.has_value()); Void::Testing::expect_eq( retire_in_use.error().code().value, static_cast(SecurityErrorCode::key_in_use)); const std::vector migrated_inv{"k2"}; Void::Testing::require(ring.retire_key("k1", migrated_inv).has_value()); Void::Testing::expect(!ring.has_key("k1")); auto retired_open = ring.open(*env_k1, aad); Void::Testing::require(!retired_open.has_value()); Void::Testing::expect_eq( retired_open.error().code().value, static_cast(SecurityErrorCode::key_not_found)); } Void::Testing::Test security_authorization_default_deny_and_limits() { using namespace Void::Security; auto policy = PolicySnapshot::create( 7, { PolicyRule{ .id = "allow-owner-read", .effect = Effect::Allow, .action = "doc:read", .condition = Predicate::all_of( {Predicate::resource_type("document"), Predicate::owner_is_subject()}), }, PolicyRule{ .id = "allow-admin-all", .effect = Effect::Allow, .action = "*", .condition = Predicate::has_role("admin"), }, PolicyRule{ .id = "deny-classified", .effect = Effect::Deny, .action = "*", .condition = Predicate::resource_attr("tier", "classified"), }, }); Void::Testing::require(policy.has_value()); Principal alice{ .subject = "alice", .roles = {"editor"}, .policy_generation = 7}; Principal admin{ .subject = "root", .roles = {"admin"}, .policy_generation = 7}; ResourceAttributes doc{ .resource_type = "document", .resource_id = "d1", .attributes = {{"owner", "alice"}, {"tier", "standard"}}, }; ResourceAttributes secret_doc{ .resource_type = "document", .resource_id = "d2", .attributes = {{"owner", "alice"}, {"tier", "classified"}}, }; // Explicit allow for owner. auto d1 = policy->authorize(alice, "doc:read", doc); Void::Testing::expect(d1.allowed); Void::Testing::expect_eq(d1.matched_rule_id, std::string{"allow-owner-read"}); // Default deny for unmatched action. auto d2 = policy->authorize(alice, "doc:delete", doc); Void::Testing::expect(!d2.allowed); // Deny overrides allow even for admin. auto d3 = policy->authorize(admin, "doc:read", secret_doc); Void::Testing::expect(!d3.allowed); Void::Testing::expect_eq(d3.matched_rule_id, std::string{"deny-classified"}); // Principal exceeding 64 roles fails closed. Principal bloated{.subject = "bob"}; bloated.roles.assign(SecurityLimits::kMaxRolesPerPrincipal + 1U, "admin"); Void::Testing::expect(!policy->authorize(bloated, "doc:read", doc).allowed); // Rule count > 4096 rejected. std::vector too_many( SecurityLimits::kMaxPolicyRules + 1U, PolicyRule{.id = "r", .effect = Effect::Allow, .action = "x", .condition = Predicate::always_true()}); Void::Testing::expect(!PolicySnapshot::create(1, std::move(too_many)).has_value()); // Predicate depth > 16 rejected. Predicate deep = Predicate::always_true(); for (std::size_t i = 0; i < SecurityLimits::kMaxPredicateDepth + 1U; ++i) { deep = Predicate::negate(std::move(deep)); } Void::Testing::expect( !PolicySnapshot::create( 1, {PolicyRule{.id = "deep", .effect = Effect::Allow, .action = "x", .condition = std::move(deep)}}) .has_value()); } Void::Testing::Test security_random_bytes_fills_and_fails_closed() { using namespace Void::Security; std::array first{}; std::array second{}; first.fill(0xAA); second.fill(0xAA); Void::Testing::require(random_bytes(first).has_value()); Void::Testing::require(random_bytes(second).has_value()); Void::Testing::expect(!constant_time_equal(first, second)); Void::Testing::expect(std::any_of(first.begin(), first.end(), [](unsigned char b) { return b != 0xAA; })); // An empty destination is a successful no-op. std::array empty{}; Void::Testing::expect(random_bytes(empty).has_value()); // Injected failure wipes the destination instead of leaving partial bytes. { ScopedFaultGuard fault{FaultConfig{FaultOperation::random_bytes, SecurityErrorCode::backend_error, 0}}; std::array output{}; output.fill(0xCC); auto result = random_bytes(output); Void::Testing::require(!result.has_value()); Void::Testing::expect(std::all_of( output.begin(), output.end(), [](unsigned char b) { return b == 0; })); } }