Skip to main content

Overview

The crypto module provides cryptographic functions used in Windows protocols, including implementations of RFC 4493 (AES-CMAC), RFC 4615, NIST SP 800-108 KDF, and Windows-specific cryptographic operations.

AES-CMAC

AES_CMAC()

Compute AES-CMAC (Cipher-based Message Authentication Code) as defined in RFC 4493.

Parameters

  • K (bytes): 128-bit AES key
  • M (bytes): Message to authenticate
  • length (int): Length of message in bytes

Returns

16-byte authentication code

AES-CMAC-PRF-128

AES_CMAC_PRF_128()

AES-CMAC-based Pseudo-Random Function as defined in RFC 4615.

Parameters

  • VK (bytes): Variable-length key
  • M (bytes): Message/data
  • VKlen (int): Length of VK in bytes
  • Mlen (int): Length of M in bytes

Returns

128-bit pseudo-random value

Key Derivation

KDF_CounterMode()

Key Derivation Function in Counter Mode (NIST SP 800-108 Section 5.1) using HMAC-SHA256 as PRF.

Parameters

  • KI (bytes): Key derivation key
  • Label (bytes): Purpose identifier
  • Context (bytes): Context information
  • L (int): Desired output length in bits

Returns

Derived key material (L bits)

Example: Session Key Derivation

LSA Secret Encryption

LSA_SECRET_XP

Structure for LSA secrets on Windows XP/2003.

decryptSecret()

Decrypt LSA secrets using the system key.

Parameters

  • key (bytes): System encryption key (from SYSTEM hive)
  • value (bytes): Encrypted LSA secret

Returns

Decrypted secret data

encryptSecret()

Encrypt data as LSA secret.

SAM Hash Operations

SamDecryptNTLMHash()

Decrypt NTLM hash from SAM database.

Parameters

  • encryptedHash (bytes): 16-byte encrypted hash
  • key (bytes): 14+ byte decryption key

Returns

16-byte decrypted NTLM hash

SamEncryptNTLMHash()

Encrypt NTLM hash for SAM storage.

Key Transformation

transformKey()

Transform 7-byte key into 8-byte DES key with parity bits.

Parameters

  • InputKey (bytes): 7-byte input key

Returns

8-byte DES key with parity bits set

Low-Level Functions

Generate_Subkey()

Generate AES-CMAC subkeys K1 and K2.

XOR_128()

XOR two 128-bit values.

PAD()

Pad data to 16-byte boundary for AES-CMAC.

Complete Examples

Decrypt LSA Secrets

Derive Session Keys

SAM Hash Extraction

Algorithm Constants

The module uses the following cryptographic algorithms:

Security Notes

  1. Use secure random sources for key generation (os.urandom)
  2. Protect sensitive keys in memory
  3. Validate input lengths before cryptographic operations
  4. Use constant-time comparisons for MAC verification
  5. Follow key derivation best practices (unique labels/contexts)

References