Overview
The authenticated encryption module provides a simple wrapper around AES-128-GCM-SIV (Galois/Counter Mode with Synthetic Initialization Vector) specialized for encryptingu64 values. This implementation is optimized for SPL Token-2022 where account balances are always 64-bit unsigned integers.
Key Concepts
AES-GCM-SIV
AES-GCM-SIV is an authenticated encryption with associated data (AEAD) cipher that provides:- Confidentiality: Encrypted data cannot be read without the key
- Authenticity: Tampering with ciphertext is detectable
- Nonce-misuse resistance: Reusing nonces degrades to standard deterministic encryption
Fixed-Size Encryption
This implementation specializes in encryptingu64 values:
- Plaintext: 8 bytes (u64 in little-endian)
- Nonce: 12 bytes (randomly generated)
- Ciphertext: 24 bytes (8 bytes encrypted + 16 byte authentication tag)
- Total: 36 bytes per encrypted value
Core Types
AeKey
An AES-128 key for authenticated encryption. Instances are zeroized on drop.Methods
Key GenerationAeCiphertext
An authenticated encryption ciphertext containing nonce and encrypted data.Methods
Usage Examples
Basic Encryption and Decryption
Deriving Keys from Solana Signer
Key Derivation from Seed
Key Derivation from Signature
Serialization and Deserialization
Key Conversion
Non-Deterministic Encryption
Security Considerations
Tamper Detection
The authenticated encryption scheme detects any tampering:Nonce Tampering
Tampering with the nonce also causes decryption to fail:Key Management
- Keys are automatically zeroized on drop
- Never expose keys or serialize them insecurely
- Use proper key derivation when deriving from Solana signers
- The seed-based KDF is non-standard and may be refactored in future versions
Nonce Reuse
AES-GCM-SIV provides nonce-misuse resistance:- Accidentally reusing a nonce degrades to deterministic encryption
- Still maintains confidentiality but loses semantic security
- The implementation generates random nonces for each encryption
Algorithm Details
Internal Implementation
The module uses theaes-gcm-siv crate with AES-128:
Encryption Process
- Convert
u64amount to 8-byte little-endian representation - Generate random 12-byte nonce using
OsRng - Encrypt plaintext using AES-128-GCM-SIV
- Return nonce and ciphertext (24 bytes = 8 bytes + 16 byte tag)
Decryption Process
- Extract nonce (first 12 bytes) and ciphertext (remaining 24 bytes)
- Attempt to decrypt using AES-128-GCM-SIV
- Verify authentication tag (automatic in AEAD)
- Convert decrypted bytes back to
u64 - Return
Noneif authentication fails or length is incorrect
Comparison with ElGamal
| Feature | Authenticated Encryption | ElGamal |
|---|---|---|
| Security | Symmetric (shared key) | Asymmetric (public key) |
| Ciphertext Size | 36 bytes | 64 bytes |
| Homomorphic | No | Yes |
| Authentication | Yes (AEAD) | No |
| Decryption Speed | Very fast | Requires discrete log |
| Use Case | Private storage | Multi-party protocols |
Use Cases
SPL Token-2022 Balances
The primary use case is encrypting token account balances:Private Data Storage
Anyu64 value that needs authenticated encryption:
Error Handling
Decryption returnsOption<u64> instead of Result: