Overview
The cryptography module provides secure encryption, decryption, and key derivation functions for the C2 framework. It uses AES-256-GCM for authenticated encryption and HKDF-SHA256 for key derivation. File:common/crypto.py
Constants
Size of the nonce for AES-GCM encryption in bytes
Required key size for AES-256 (32 bytes = 256 bits)
Authentication tag length for GCM mode
Context label for HKDF to ensure domain separation
Functions
derive_key()
Derive a cryptographic key from a pre-shared key using HKDF-SHA256.Pre-shared key material (must not be empty)
Salt value for key derivation (must not be empty)
Derived 32-byte key suitable for AES-256-GCM
CryptoError- If PSK or salt is empty, or if derivation fails
encrypt()
Encrypt plaintext using AES-256-GCM with a random nonce.Data to encrypt (must not be empty)
32-byte AES-256 key
A tuple of
(ciphertext_with_tag, nonce) where:ciphertext_with_tag: Encrypted data with 16-byte authentication tag appendednonce: 12-byte nonce used for this encryption (must be sent with ciphertext)
CryptoError- If plaintext is empty, key is wrong size, or encryption fails
- A new random nonce is generated for each encryption
- The nonce must be transmitted with the ciphertext (it’s not secret)
- Never reuse a nonce with the same key
decrypt()
Decrypt ciphertext using AES-256-GCM and verify authenticity.Encrypted data with 16-byte authentication tag appended (must not be empty)
12-byte nonce that was used during encryption
32-byte AES-256 key (must match the key used for encryption)
Decrypted plaintext
CryptoError- If ciphertext is empty, nonce/key wrong size, authentication fails, or decryption fails
- The authentication tag is automatically verified
- If the ciphertext has been tampered with,
CryptoErroris raised - Wrong key also raises
CryptoErrordue to tag verification failure
get_session_key()
Convenience function to get a ready-to-use session key from configuration.32-byte session key derived from
config.PRE_SHARED_KEYCryptoError- Ifconfig.PRE_SHARED_KEYis not exactly 32 bytes
- Reads
PRE_SHARED_KEYfromcommon.config - Derives key using HKDF with fixed salt
b'c2-lab-fixed-salt-v1' - Validates key length before derivation
Error Handling
All cryptographic errors raiseCryptoError (from common.utils). This includes:
- Invalid parameters (empty, wrong size)
- Key derivation failures
- Encryption/decryption failures
- Authentication tag verification failures (tampered data)
Security Properties
Authenticated Encryption
AES-GCM provides both confidentiality and authenticity. Tampering is automatically detected.
Nonce Uniqueness
Each encryption uses a cryptographically random 12-byte nonce, ensuring security even with key reuse.
Key Derivation
HKDF-SHA256 with domain separation ensures derived keys are cryptographically independent.
Constant-Time Operations
Uses
cryptography library which provides timing-attack resistant implementations.Dependencies
See Also
- Message Format - Uses crypto functions for envelope encryption
- Configuration - Defines
PRE_SHARED_KEY - Common Utilities - Defines
CryptoErrorexception