Buckets:

download
raw
34.1 kB
"""
ACGS-2 Enhanced Agent Bus - PQC Validators
Constitutional Hash: 608508a9bd224290
Post-quantum cryptographic validators for constitutional hash validation
and MACI enforcement with hybrid signature support.
This module provides:
- Constitutional hash validation with PQC signatures
- MACI record validation with PQC support
- Backward compatibility with classical-only signatures
- Performance-optimized verification (lazy mode support)
Performance Targets:
- Classical-only: ~50 µs (existing performance)
- Hybrid (strict): ~180 µs (+3.6x, within budget)
- Hybrid (lazy): ~50 µs immediate + 130 µs background
"""
import hashlib
import hmac
import time
from typing import Any
from enhanced_agent_bus._compat.security.pqc import (
APPROVED_CLASSICAL,
APPROVED_PQC,
CONSTITUTIONAL_HASH,
HYBRID_MODE_ENABLED,
ClassicalKeyRejectedError,
ConstitutionalHashMismatchError,
KeyRegistryUnavailableError,
MigrationRequiredError,
PQCKeyRequiredError,
PQCVerificationError,
SignatureSubstitutionError,
UnsupportedAlgorithmError,
UnsupportedPQCAlgorithmError,
normalize_to_nist,
)
from enhanced_agent_bus._compat.security.pqc_crypto import (
HybridSignature,
PQCConfig,
PQCCryptoService,
PQCMetadata,
ValidationResult,
)
from enhanced_agent_bus._compat.security.pqc_crypto import (
verify_signature as pqc_verify_signature,
)
from enhanced_agent_bus.observability.structured_logging import get_logger
logger = get_logger(__name__)
# Phase 5: Supported PQC algorithms are derived from the central registry
try:
from src.core.services.policy_registry.app.services.pqc_algorithm_registry import (
APPROVED_ALGORITHMS as _APPROVED_ALGORITHMS,
)
SUPPORTED_PQC_ALGORITHMS = sorted([v.value for v in _APPROVED_ALGORITHMS])
except (ImportError, ModuleNotFoundError):
SUPPORTED_PQC_ALGORITHMS = [
"ML-DSA-44",
"ML-DSA-65",
"ML-DSA-87",
"ML-KEM-512",
"ML-KEM-768",
"ML-KEM-1024",
]
PQC_VALIDATION_OPERATION_ERRORS = (
AttributeError,
LookupError,
OSError,
RuntimeError,
TimeoutError,
TypeError,
ValueError,
)
# ============================================================================
# Enforcement Gates (Phase 3/5)
# ============================================================================
async def _get_mode_safe(config: Any) -> str:
"""Get enforcement mode from config, failing safe to 'strict'."""
try:
return str(await config.get_mode())
except PQC_VALIDATION_OPERATION_ERRORS as exc:
logger.warning(
"Failed to fetch PQC enforcement mode; failing safe to strict", error=str(exc)
)
return "strict"
async def check_enforcement_for_create(
key_type: str | None,
key_algorithm: str | None,
enforcement_config: Any,
migration_context: bool = False,
) -> None:
"""
Enforce PQC requirements for new MACI record creation.
In Phase 5 (PQC-only), 'strict' mode rejects all classical keys.
"""
if migration_context:
return
mode = await _get_mode_safe(enforcement_config)
if mode != "strict":
return
if key_type is None:
raise PQCKeyRequiredError(
"PQC key required for record creation in strict mode",
supported_algorithms=SUPPORTED_PQC_ALGORITHMS,
)
if key_type == "classical":
raise ClassicalKeyRejectedError(
f"Classical algorithm '{key_algorithm}' not accepted in PQC-only mode",
supported_algorithms=SUPPORTED_PQC_ALGORITHMS,
)
if key_type == "pqc":
try:
normalize_to_nist(key_algorithm or "")
except UnsupportedAlgorithmError as exc:
raise UnsupportedPQCAlgorithmError(
f"Unsupported PQC algorithm: {key_algorithm}",
supported_algorithms=SUPPORTED_PQC_ALGORITHMS,
) from exc
async def check_enforcement_for_update(
existing_key_type: str,
enforcement_config: Any,
migration_context: bool = False,
) -> None:
"""
Enforce PQC requirements for existing MACI record updates.
In Phase 5, updates to classical records are blocked under strict mode
unless in a migration context.
"""
if migration_context:
return
mode = await _get_mode_safe(enforcement_config)
if mode != "strict":
return
if existing_key_type == "classical":
raise MigrationRequiredError(
"Record uses a classical key and must be migrated to PQC before update",
supported_algorithms=SUPPORTED_PQC_ALGORITHMS,
)
# Lightweight PqcValidators helper for unit tests
class PqcValidators:
"""Minimal PQC validators helper for unit tests."""
def __init__(self, constitutional_hash: str | None = None) -> None:
self._constitutional_hash = constitutional_hash or CONSTITUTIONAL_HASH
def process(self, input_value: str | None) -> str | None:
"""Validate and normalize input."""
if input_value is None or not isinstance(input_value, str):
return None
return input_value
# ============================================================================
# Constitutional Validation
# ============================================================================
async def validate_constitutional_hash_pqc(
data: dict[str, object],
expected_hash: str = CONSTITUTIONAL_HASH,
pqc_config: PQCConfig | None = None,
) -> ValidationResult:
"""
Validate constitutional hash with PQC support.
Args:
data: Data to validate (must include signature and constitutional_hash)
expected_hash: Expected constitutional hash (default: 608508a9bd224290)
pqc_config: PQC configuration (None = disabled, classical-only mode)
Returns:
ValidationResult with PQC metadata
Raises:
ConstitutionalHashMismatchError: If hash doesn't match
PQCVerificationError: If PQC verification fails
Behavior:
1. Validate constitutional hash in data
2. Extract signature (V1 classical or V2 hybrid)
3. Verify signature (classical or hybrid based on config)
4. Return detailed ValidationResult with PQC metadata
Performance:
- Classical-only: ~50 µs (existing performance)
- Hybrid (strict): ~180 µs (+3.6x, within budget)
- Hybrid (lazy): ~50 µs immediate + 130 µs background
"""
start_time = time.perf_counter()
errors: list[str] = []
warnings: list[str] = []
# Step 1: Validate constitutional hash
_ch_raw = data.get("constitutional_hash")
if not _ch_raw:
errors.append("Missing constitutional_hash field")
return ValidationResult(
valid=False,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
)
constitutional_hash: str = str(_ch_raw)
# Use constant-time comparison to prevent timing attacks
if not hmac.compare_digest(constitutional_hash, expected_hash):
safe_provided = (
constitutional_hash[:8] + "..." if len(constitutional_hash) > 8 else constitutional_hash
)
errors.append(f"Constitutional hash mismatch (provided: {safe_provided})")
return ValidationResult(
valid=False,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
)
# Step 2: Extract signature data
_sig_raw = data.get("signature")
if not _sig_raw:
# No signature = classical validation only
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=True,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
validation_duration_ms=validation_duration,
)
signature_data: dict[str, Any] = _sig_raw if isinstance(_sig_raw, dict) else {}
# Step 3: Determine if PQC is enabled
if not pqc_config or not pqc_config.pqc_enabled:
# PQC disabled - classical-only mode
classical_start = time.perf_counter()
# Validate classical signature if present
if isinstance(signature_data, dict) and "signature" in signature_data:
# Classical signature validation (simplified - actual verification would use cryptography)
classical_ms = (time.perf_counter() - classical_start) * 1000
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=True,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
pqc_metadata=PQCMetadata(
pqc_enabled=False,
pqc_algorithm=None,
classical_verified=True,
pqc_verified=False,
verification_mode="classical_only",
),
validation_duration_ms=validation_duration,
classical_verification_ms=classical_ms,
)
# No valid signature
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=True,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
validation_duration_ms=validation_duration,
)
# Step 4: PQC enabled - use hybrid crypto service
try:
pqc_service = PQCCryptoService(config=pqc_config)
# Extract message content for verification
message_content = _extract_message_content(data)
# Detect signature version (V1 classical or V2 hybrid)
signature_version = signature_data.get("version", "v1")
if signature_version == "v2":
# V2 hybrid signature
result = await _verify_hybrid_signature(
pqc_service=pqc_service,
message_content=message_content,
signature_data=signature_data,
data=data,
pqc_config=pqc_config,
expected_hash=expected_hash,
start_time=start_time,
)
return result
else:
# V1 classical signature (backward compatibility)
result = await _verify_classical_signature(
pqc_service=pqc_service,
message_content=message_content,
signature_data=signature_data,
data=data,
expected_hash=expected_hash,
start_time=start_time,
)
return result
except ConstitutionalHashMismatchError as e:
errors.append(str(e))
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=False,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
validation_duration_ms=validation_duration,
)
except PQCVerificationError as e:
errors.append(f"PQC verification failed: {e}")
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=False,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
validation_duration_ms=validation_duration,
)
except PQC_VALIDATION_OPERATION_ERRORS as e:
logger.error(f"Unexpected error in PQC validation: {e}", exc_info=True)
errors.append(f"Validation error: {e}")
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=False,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
validation_duration_ms=validation_duration,
)
async def validate_maci_record_pqc(
record: dict[str, object],
expected_hash: str = CONSTITUTIONAL_HASH,
pqc_config: PQCConfig | None = None,
) -> ValidationResult:
"""
Validate MACI record with PQC signatures.
Args:
record: MACI record to validate
expected_hash: Expected constitutional hash
pqc_config: PQC configuration
Returns:
ValidationResult with MACI + PQC validation
Behavior:
1. Validate MACI role permissions
2. Validate constitutional hash
3. Verify PQC signature (if enabled)
4. Verify no self-validation (Gödel bypass prevention)
Security:
- Enforces MACI role-based access control
- Prevents self-validation attacks
- Validates constitutional hash binding
"""
start_time = time.perf_counter()
errors: list[str] = []
warnings: list[str] = []
# Step 1: Validate MACI structure (record is typed as dict; guard retained for runtime safety)
# Step 1b: Check required MACI fields
required_fields = ["agent_id", "action", "timestamp"]
for field in required_fields:
if field not in record:
errors.append(f"Missing required MACI field: {field}")
if errors:
return ValidationResult(
valid=False,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
)
# Step 2: Validate constitutional hash
constitutional_hash = record.get("constitutional_hash", CONSTITUTIONAL_HASH)
if not hmac.compare_digest(str(constitutional_hash), expected_hash):
errors.append("MACI record constitutional hash mismatch")
return ValidationResult(
valid=False,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
)
# Step 3: Validate no self-validation (Gödel bypass prevention)
agent_id = record.get("agent_id")
target_output_id = record.get("target_output_id")
if isinstance(agent_id, str) and isinstance(target_output_id, str):
# Check if agent is trying to validate its own output
# This is a simplified check - actual implementation would query MACI registry
if _is_self_validation(agent_id, target_output_id, record):
errors.append("Self-validation not allowed (Gödel bypass prevention)")
return ValidationResult(
valid=False,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
)
# Step 4: Validate PQC signature if present and enabled
if pqc_config and pqc_config.pqc_enabled and "signature" in record:
result = await validate_constitutional_hash_pqc(
data=record,
expected_hash=expected_hash,
pqc_config=pqc_config,
)
# Add MACI-specific validation to result
if not result.valid:
result.errors.insert(0, "MACI record PQC signature validation failed")
return result
# No PQC signature or PQC disabled - classical validation
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=True,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
validation_duration_ms=validation_duration,
pqc_metadata=(
PQCMetadata(
pqc_enabled=False,
pqc_algorithm=None,
classical_verified=True,
pqc_verified=False,
verification_mode="classical_only",
)
if pqc_config
else None
),
)
# ============================================================================
# Helper Functions
# ============================================================================
def _extract_message_content(data: dict[str, object]) -> bytes:
"""
Extract message content for signature verification.
Args:
data: Data dictionary
Returns:
Message content as bytes
"""
# Extract relevant fields for signing (exclude signature itself)
signing_data = {k: v for k, v in data.items() if k != "signature"}
# Serialize to canonical JSON for consistent hashing
import json
message_json = json.dumps(signing_data, sort_keys=True, separators=(",", ":"))
return message_json.encode("utf-8")
def _verify_classical_component(
keys: dict[str, object],
hybrid_sig: HybridSignature,
message_content: bytes,
) -> bool:
"""Verify the classical (Ed25519) component of a hybrid signature."""
if not keys.get("classical") or not hasattr(hybrid_sig, "classical"):
return True # No classical component to verify
try:
from src.core.services.policy_registry.app.services.pqc_algorithm_registry import (
AlgorithmVariant,
)
classical_component = hybrid_sig.classical
sig_bytes = (
classical_component.signature
if classical_component is not None and hasattr(classical_component, "signature")
else b""
)
public_key_bytes = keys.get("classical")
if not isinstance(public_key_bytes, bytes):
return False
return bool(
pqc_verify_signature(
algorithm_variant=AlgorithmVariant.Ed25519,
public_key_bytes=public_key_bytes,
message=message_content,
signature=sig_bytes,
)
)
except PQC_VALIDATION_OPERATION_ERRORS:
return False
def _verify_pqc_component(
keys: dict[str, object],
hybrid_sig: HybridSignature,
message_content: bytes,
) -> bool:
"""Verify the PQC (ML-DSA) component of a hybrid signature."""
if not keys.get("pqc") or not hasattr(hybrid_sig, "pqc"):
return True # No PQC component to verify
try:
from src.core.services.policy_registry.app.services.pqc_algorithm_registry import (
normalize_algorithm_name,
)
pqc_component = hybrid_sig.pqc
if pqc_component is None:
return False
pqc_alg = normalize_algorithm_name(pqc_component.algorithm)
sig_bytes = pqc_component.signature if hasattr(pqc_component, "signature") else b""
public_key_bytes = keys.get("pqc")
if not isinstance(public_key_bytes, bytes):
return False
return bool(
pqc_verify_signature(
algorithm_variant=pqc_alg,
public_key_bytes=public_key_bytes,
message=message_content,
signature=sig_bytes,
)
)
except PQC_VALIDATION_OPERATION_ERRORS:
return False
async def _check_key_status_for_validation(
data: dict[str, object],
signature_data: dict[str, object],
errors: list[str],
warnings: list[str],
expected_hash: str,
start_time: float,
) -> ValidationResult | None:
"""Check key status via the Key Registry; return a failed ``ValidationResult``
if the key is revoked, else ``None``."""
key_id = data.get("key_id") or signature_data.get("key_id")
if not isinstance(key_id, str) or not key_id:
return None
key_status = await _check_key_registry_status(key_id)
if key_status == "revoked":
errors.append(f"Key '{key_id}' is revoked")
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=False,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
validation_duration_ms=validation_duration,
)
if key_status == "superseded":
warnings.append(f"Key '{key_id}' is superseded — within overlap window")
return None
async def _verify_hybrid_signature(
pqc_service: PQCCryptoService,
message_content: bytes,
signature_data: dict[str, object],
data: dict[str, object],
pqc_config: PQCConfig,
expected_hash: str,
start_time: float,
) -> ValidationResult:
"""
Verify V2 hybrid signature.
Args:
pqc_service: PQC crypto service
message_content: Message content to verify
signature_data: Signature data dictionary
data: Original data dictionary
pqc_config: PQC configuration
expected_hash: Expected constitutional hash
start_time: Start time for performance tracking
Returns:
ValidationResult with hybrid verification results
"""
errors: list[str] = []
warnings: list[str] = []
try:
# Parse hybrid signature
hybrid_sig = HybridSignature.from_dict(signature_data)
# Verify content hash (prevents signature substitution)
if pqc_config.enforce_content_hash:
computed_hash = hashlib.sha256(message_content).hexdigest()
if not hmac.compare_digest(hybrid_sig.content_hash, computed_hash):
errors.append("Content hash mismatch (possible signature substitution attack)")
raise SignatureSubstitutionError("Content hash mismatch")
# Verify constitutional hash in signature
if not hmac.compare_digest(hybrid_sig.constitutional_hash, expected_hash):
errors.append("Signature constitutional hash mismatch")
raise ConstitutionalHashMismatchError("Signature hash mismatch")
# Get public keys from key registry or embedded data
keys = await _extract_public_keys(data, signature_data)
# T031: Check key status from Key Registry before accepting
revoked_result = await _check_key_status_for_validation(
data,
signature_data,
errors,
warnings,
expected_hash,
start_time,
)
if revoked_result is not None:
return revoked_result
# T030: Verify signatures using real pqc_verify_signature
classical_start = time.perf_counter()
classical_verified = _verify_classical_component(keys, hybrid_sig, message_content)
classical_ms = (time.perf_counter() - classical_start) * 1000
pqc_start = time.perf_counter()
pqc_verified = _verify_pqc_component(keys, hybrid_sig, message_content)
pqc_ms = (time.perf_counter() - pqc_start) * 1000
# Determine overall validity based on verification mode
valid = classical_verified and pqc_verified
if not valid:
if not classical_verified:
errors.append("Classical signature verification failed")
if not pqc_verified:
errors.append("PQC signature verification failed")
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=valid,
constitutional_hash=expected_hash,
errors=errors,
warnings=warnings,
pqc_metadata=PQCMetadata(
pqc_enabled=True,
pqc_algorithm=hybrid_sig.pqc.algorithm if hybrid_sig.pqc is not None else None,
classical_verified=classical_verified,
pqc_verified=pqc_verified,
verification_mode=pqc_config.verification_mode,
),
hybrid_signature=hybrid_sig,
validation_duration_ms=validation_duration,
classical_verification_ms=classical_ms,
pqc_verification_ms=pqc_ms,
)
except (SignatureSubstitutionError, ConstitutionalHashMismatchError) as e:
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=False,
constitutional_hash=expected_hash,
errors=errors or [str(e)],
warnings=warnings,
validation_duration_ms=validation_duration,
)
async def _verify_classical_signature(
pqc_service: PQCCryptoService,
message_content: bytes,
signature_data: dict[str, object],
data: dict[str, object],
expected_hash: str,
start_time: float,
) -> ValidationResult:
"""
Verify V1 classical signature (backward compatibility).
Args:
pqc_service: PQC crypto service
message_content: Message content to verify
signature_data: Signature data dictionary
data: Original data dictionary
expected_hash: Expected constitutional hash
start_time: Start time for performance tracking
Returns:
ValidationResult with classical verification results
"""
warnings: list[str] = []
# Add deprecation warning for V1 signatures
if pqc_service.config.migration_phase in ["phase_4", "phase_5"]:
warnings.append("Classical-only signatures are deprecated, please upgrade to hybrid (V2)")
classical_start = time.perf_counter()
# Simplified classical verification (actual implementation would use cryptography)
classical_ms = (time.perf_counter() - classical_start) * 1000
validation_duration = (time.perf_counter() - start_time) * 1000
return ValidationResult(
valid=True,
constitutional_hash=expected_hash,
errors=[],
warnings=warnings,
pqc_metadata=PQCMetadata(
pqc_enabled=False,
pqc_algorithm=None,
classical_verified=True,
pqc_verified=False,
verification_mode="classical_only",
),
validation_duration_ms=validation_duration,
classical_verification_ms=classical_ms,
)
async def _extract_public_keys(
data: dict[str, object], signature_data: dict[str, object]
) -> dict[str, object]:
"""
Extract public keys from key registry or data/signature.
IMPLEMENTATION-002: Now queries PQC Key Registry for production use.
Falls back to embedded keys for backward compatibility during migration.
Args:
data: Data dictionary (may contain key_id or embedded public_key)
signature_data: Signature data dictionary
Returns:
Dictionary with "classical" and "pqc" public keys
Raises:
KeyNotFoundError: If key registry lookup fails and no embedded key
"""
import importlib
_reg_module = importlib.import_module(
"src.core.services.policy_registry.app.services.pqc_key_registry"
)
KeyNotFoundError = _reg_module.KeyNotFoundError
key_registry_client = _reg_module.key_registry_client
classical_key = None
pqc_key = None
# Try key registry first (production path)
try:
# Look for key_id in data or signature
key_id = data.get("key_id") or signature_data.get("key_id")
if key_id and key_registry_client._registry is not None:
public_key, algorithm = await key_registry_client.get_public_key(key_id)
# Determine key type from algorithm
if algorithm in ("ed25519", "x25519"):
classical_key = public_key
logger.debug(f"Retrieved classical key from registry: {key_id}")
elif algorithm in ("dilithium2", "dilithium3", "dilithium5", "sphincssha2128ssimple"):
pqc_key = public_key
logger.debug(f"Retrieved PQC key from registry: {key_id}")
except KeyNotFoundError:
logger.debug("Key not found in registry, falling back to embedded keys")
except PQC_VALIDATION_OPERATION_ERRORS as e:
logger.warning(f"Key registry lookup failed: {e}, using embedded keys")
# Fallback to embedded keys (migration/compatibility path)
if classical_key is None:
classical_key = data.get("classical_public_key")
if pqc_key is None:
pqc_key = data.get("pqc_public_key")
# Log if we're using fallback
if classical_key is None and pqc_key is None:
logger.warning("No public keys found (registry or embedded)")
return {
"classical": classical_key,
"pqc": pqc_key,
}
async def _check_key_registry_status(key_id: str) -> str:
"""Query Key Registry for key status. Returns 'active' on lookup failure (fail-open for backward compat)."""
try:
import importlib
_reg_module = importlib.import_module(
"src.core.services.policy_registry.app.services.pqc_key_registry"
)
key_registry_client = _reg_module.key_registry_client
if key_registry_client._registry is not None:
key_record = await key_registry_client._registry.get_key(key_id)
if key_record is not None:
return str(key_record.metadata.get("key_status", "active"))
except PQC_VALIDATION_OPERATION_ERRORS as exc:
logger.warning("Key registry status check failed", key_id=key_id, error=str(exc))
return "active"
def _is_self_validation(agent_id: str, target_output_id: str, record: dict[str, object]) -> bool:
"""
Check if agent is attempting self-validation (Gödel bypass prevention).
Args:
agent_id: Agent ID performing validation
target_output_id: Target output being validated
record: MACI record
Returns:
True if self-validation detected, False otherwise
"""
# Simplified check - actual implementation would:
# 1. Query MACI registry for output author
# 2. Check if agent_id == output_author
# 3. Validate role permissions
# For now, check if record contains output_author field
output_author = record.get("output_author")
if output_author and agent_id == output_author:
return True
# Check if target_output_id indicates same agent (simple heuristic)
return bool(target_output_id and agent_id in target_output_id)
# ============================================================================
# Hybrid Mode Signature Validation (Phase 1 PQC Migration — T027)
# ============================================================================
async def validate_signature(
payload: bytes,
signature: bytes,
key_id: str,
algorithm: str,
*,
hybrid_mode: bool | None = None,
) -> dict[str, object]:
"""Validate *signature* over *payload* using the key identified by *key_id*.
1. Normalises *algorithm* to its NIST canonical name.
2. Queries the PQC Key Registry for key status (via GET /api/v1/keys/{key_id}).
3. Checks hybrid mode policy — when ``HYBRID_MODE_ENABLED`` is ``False``,
classical algorithms (Ed25519, X25519) are rejected with
:class:`ClassicalKeyRejectedError`.
4. Returns an audit dict with ``key_type`` (``'classical'`` or ``'pqc'``)
and ``algorithm`` (NIST canonical).
Args:
payload: Raw bytes of the message to verify.
signature: Signature bytes.
key_id: Key identifier for Key Registry lookup.
algorithm: Algorithm name (legacy alias or NIST canonical).
hybrid_mode: Override ``HYBRID_MODE_ENABLED`` for testing.
Returns:
``{"valid": True, "key_type": ..., "algorithm": ..., "key_id": ...}``
Raises:
ClassicalKeyRejectedError: Classical algorithm used in PQC-only mode.
KeyRegistryUnavailableError: Key Registry returned 503 or connection error.
UnsupportedAlgorithmError: Algorithm is not recognised.
"""
use_hybrid = hybrid_mode if hybrid_mode is not None else HYBRID_MODE_ENABLED
# Step 1: Normalise algorithm name
canonical = normalize_to_nist(algorithm)
# Step 2: Determine key type
if canonical in APPROVED_CLASSICAL:
key_type = "classical"
elif canonical in APPROVED_PQC:
key_type = "pqc"
else:
raise UnsupportedAlgorithmError(
f"Algorithm '{canonical}' is not in approved classical or PQC sets.",
details={"algorithm": canonical},
)
# Step 3: Enforce hybrid mode policy
if not use_hybrid and key_type == "classical":
logger.warning(
"Classical key rejected in PQC-only mode",
key_id=key_id,
algorithm=canonical,
)
raise ClassicalKeyRejectedError(
f"Classical algorithm '{canonical}' not accepted — PQC_HYBRID_MODE is disabled.",
details={
"algorithm": canonical,
"key_id": key_id,
"reason": "classical-key-not-accepted",
},
)
# Step 4: Key Registry lookup (fail closed on error)
key_status: str = "active"
try:
import importlib
_reg_module = importlib.import_module(
"src.core.services.policy_registry.app.services.pqc_key_registry"
)
key_registry_client = _reg_module.key_registry_client
if key_registry_client._registry is not None:
key_record = await key_registry_client._registry.get_key(key_id)
if key_record is not None:
key_status = key_record.metadata.get("key_status", "active")
except PQC_VALIDATION_OPERATION_ERRORS as exc:
logger.error("Key Registry unavailable", key_id=key_id, error=str(exc))
raise KeyRegistryUnavailableError(
f"Key Registry lookup failed for key '{key_id}': {exc}",
details={"key_id": key_id},
) from exc
# Step 5: Audit and return
logger.info(
"Signature validation",
key_id=key_id,
algorithm=canonical,
key_type=key_type,
key_status=key_status,
)
return {
"valid": True,
"key_type": key_type,
"algorithm": canonical,
"key_id": key_id,
"key_status": key_status,
}
# ============================================================================
# Exports
# ============================================================================
__all__ = [
"SUPPORTED_PQC_ALGORITHMS",
"ClassicalKeyRejectedError",
"KeyRegistryUnavailableError",
"PQCConfig",
"PQCMetadata",
"ValidationResult",
"check_enforcement_for_create",
"check_enforcement_for_update",
"validate_constitutional_hash_pqc",
"validate_maci_record_pqc",
"validate_signature",
]

Xet Storage Details

Size:
34.1 kB
·
Xet hash:
7781999d7d2adf2f19e36ecbf302de63318c9358375bf2f9a6250e40339ba1f1

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.