pub fn decrypt_rules(
encrypted_rules: &[u8],
file_id: FileId,
version: VersionNumber,
expected_hash: [u8; 32],
) -> Result<Vec<u8>>Expand description
Decrypt rules content using file-specific key derivation
This function performs the reverse of encrypt_rules:
- Extracts the 12-byte nonce from the beginning of encrypted data
- Derives the encryption key from
file_id+ version (same as encryption) - Decrypts using ChaCha20-Poly1305 AEAD
- Verifies the decrypted data hash matches
expected_hash
§Arguments
encrypted_rules- Encrypted data with prepended nonce (nonce || ciphertext)file_id- File identifier used for key derivationversion- Version number used for key derivation and AADexpected_hash- Expected BLAKE3 hash of the plaintext rules
§Returns
Ok(Vec<u8>)- Decrypted plaintext rulesErr(AionError)- On decryption failure or hash mismatch
§Errors
AionError::DecryptionFailedif:- Encrypted data is too short (< 12 bytes for nonce)
- Authentication tag is invalid (tampering detected)
- Wrong key or nonce used
AionError::HashMismatchif decrypted data hash doesn’t match expected
§Security
- Uses deterministic key derivation from
file_idand version - Verifies authentication tag (prevents tampering)
- Verifies content hash after decryption (defense in depth)
- Constant-time operations where possible
§Example
use aion_context::operations::decrypt_rules;
use aion_context::types::{FileId, VersionNumber};
// Assuming we have encrypted rules from the file
// let encrypted = parser.encrypted_rules()?;
// let version_entry = parser.get_version_entry(0)?;
//
// let plaintext = decrypt_rules(
// encrypted,
// FileId::new(12345),
// VersionNumber::GENESIS,
// version_entry.rules_hash,
// )?;