Overview
Theimpacket.krb5.crypto module provides cryptographic primitives for Kerberos v5, implementing encryption/decryption, key derivation, checksum operations, and string-to-key functions for multiple cipher suites.
Module Location
impacket/krb5/crypto.py
Supported Encryption Types
Enctype Class
Encryption type identifiers:Cksumtype Class
Checksum type identifiers:Key Class
Represents a Kerberos encryption key.enctype: Encryption type identifiercontents: Raw key bytes
- DES: 8 bytes
- DES3: 24 bytes
- AES128: 16 bytes
- AES256: 32 bytes
- RC4: 16 bytes
String-to-Key Operations
string_to_key Function
Derive key from password and salt.enctype: Encryption type (int)string: Password (str or bytes)salt: Salt value (str or bytes)params: Optional algorithm parameters
Key object
Example:
Salt Generation
Standard Kerberos salt formats:Algorithm-Specific Details
AES String-to-Key (PBKDF2)
- PBKDF2-HMAC-SHA1(password, salt, iterations, keysize)
- Derive using “kerberos” constant
RC4 String-to-Key
DES3 String-to-Key
- n-fold(password + salt, 21)
- Random-to-key with parity bits
- Derive with “kerberos” constant
Encryption Operations
encrypt Function
Encrypt plaintext with key.key:Keyobjectkeyusage: Key usage number (int)plaintext: Data to encrypt (bytes)confounder: Optional confounder (None = random)
decrypt Function
Decrypt ciphertext with key.key:Keyobjectkeyusage: Key usage number (int)ciphertext: Encrypted data (bytes)
InvalidChecksum if integrity check fails
Example:
Key Usage Numbers
Standard key usage values from RFC 4120:Checksum Operations
make_checksum Function
Compute keyed checksum.cksumtype: Checksum type (int)key:Keyobjectkeyusage: Key usage number (int)text: Data to checksum (bytes)
verify_checksum Function
Verify keyed checksum.cksumtype: Checksum type (int)key:Keyobjectkeyusage: Key usage number (int)text: Data that was checksummed (bytes)cksum: Checksum to verify (bytes)
InvalidChecksum if verification fails
Example:
Advanced Key Operations
random_to_key Function
Convert random bytes to key.enctype: Encryption type (int)seed: Random seed bytes (seedsize length)
Key object
Example:
- DES: 8 bytes
- DES3: 21 bytes
- AES128: 16 bytes
- AES256: 32 bytes
- RC4: 16 bytes
prf Function
Pseudo-Random Function for key derivation.key:Keyobjectstring: Input string (bytes)
cf2 Function
Combine two keys (RFC 6113 KRB-FX-CF2).enctype: Target encryption typekey1: FirstKeyobjectkey2: SecondKeyobjectpepper1: First pepper (bytes)pepper2: Second pepper (bytes)
Key object
Example:
Cipher Suite Details
AES Encryption (Simplified Profile)
AES128 and AES256 use RFC 3961 simplified profile: Encryption Process:- Derive Ki = DK(key, usage | 0x55)
- Derive Ke = DK(key, usage | 0xAA)
- Generate random confounder (16 bytes)
- Plaintext’ = confounder + plaintext (zero-padded)
- HMAC = HMAC-SHA1(Ki, plaintext’)
- Ciphertext = E(Ke, plaintext’) + HMAC[0:12]
- Derive Ki and Ke
- Split ciphertext and MAC
- Decrypt: plaintext’ = D(Ke, ciphertext)
- Verify: HMAC-SHA1(Ki, plaintext’)[0:12] == MAC
- Remove confounder: plaintext = plaintext’[16:]
RC4 Encryption
RC4-HMAC (also known as ARCFOUR-HMAC-MD5): Encryption Process:- Ki = HMAC-MD5(key, usage)
- Checksum = HMAC-MD5(Ki, confounder + plaintext)
- Ke = HMAC-MD5(Ki, checksum)
- Ciphertext = checksum + RC4(Ke, confounder + plaintext)
DES3 Encryption
Triple DES with CBC mode: Encryption Process:- Derive Ki = DK(key, usage | 0x55)
- Derive Ke = DK(key, usage | 0xAA)
- Generate random confounder (8 bytes)
- Plaintext’ = confounder + plaintext (zero-padded to 8-byte boundary)
- HMAC = HMAC-SHA1(Ki, plaintext’)
- Ciphertext = E-DES3-CBC(Ke, plaintext’) + HMAC
Key Derivation
DK Function (Key Derivation)
Internal function for deriving keys:usage | 0x55: Integrity key (Ki)usage | 0xAA: Encryption key (Ke)usage | 0x99: Checksum key (Kc)b'kerberos': Base key derivation
Practical Examples
Generate Kerberos Keys
Encrypt/Decrypt Timestamp
Decrypt AS-REP
Compute Authenticator Checksum
Generate Keys from Hash
Security Considerations
Weak Encryption Types
Avoid deprecated algorithms:Key Storage
Protect cryptographic keys:Random Number Generation
Use cryptographically secure RNG:Error Handling
InvalidChecksum Exception
ValueError Exceptions
Performance Considerations
Cipher Selection
Relative Performance (fastest to slowest):- RC4 - Very fast but deprecated
- AES128 - Fast and secure
- AES256 - Secure, slightly slower
- DES3 - Slow, avoid