core.kdf

Implement TLS 1.2 and TLS 1.3 key derivation for validation.

Use HMAC and hashlib from the Python standard library.

class TLS12PRF[source]

Bases: object

Implement the TLS 1.2 pseudorandom function from RFC 5246.

The default HMAC hash is SHA-256. Callers can supply the hash specified by the cipher suite.

static p_hash(secret, seed, length, hash_algo='sha256')[source]

Expand the seed with the selected HMAC hash as specified by RFC 5246.

Parameters:
Return type:

bytes

static prf(secret, label, seed, length, hash_algo='sha256')[source]

Apply P_hash to the label followed by the seed.

Parameters:
Return type:

bytes

static derive_master_secret(pre_master_secret, client_random, server_random, hash_algo='sha256')[source]

Derive the 48-byte master secret (RFC 5246 Section 8.1).

Parameters:
Return type:

bytes

static derive_key_block(master_secret, server_random, client_random, length, hash_algo='sha256')[source]

Derive the TLS 1.2 key block in RFC 5246 Section 6.3.

Use the server random followed by the client random in the seed.

Parameters:
Return type:

bytes

class TLS13HKDF[source]

Bases: object

TLS 1.3 HKDF functions (RFC 8446, Section 7).

Implements HKDF-Extract and HKDF-Expand (RFC 5869) plus the TLS 1.3 specific HKDF-Expand-Label and Derive-Secret helpers.

static hkdf_extract(salt, ikm, hash_algo='sha256')[source]

Extract a pseudorandom key as specified by RFC 5869 Section 2.2.

An empty salt uses an all-zero string of the hash output length.

Parameters:
Return type:

bytes

static hkdf_expand(prk, info, length, hash_algo='sha256')[source]

Expand a pseudorandom key as specified by RFC 5869 Section 2.3.

Raise ValueError if the request exceeds 255 hash output blocks.

Parameters:
Return type:

bytes

static hkdf_expand_label(secret, label, context, length, hash_algo='sha256')[source]

HKDF-Expand-Label: builds HkdfLabel struct (RFC 8446 Section 7.1).

Parameters:
Return type:

bytes

static derive_secret(secret, label, messages_hash, hash_algo='sha256')[source]

Derive-Secret (RFC 8446 Section 7.1).

Parameters:
Return type:

bytes

Define the interface and parameters for key derivation plugins.

class KDFParams[source]

Bases: object

Parameters for a key derivation operation.

hash_algo: str = 'sha256'
key_lengths: tuple = (16, 32)
labels: tuple = ()
context: bytes = b''
extra: Dict[str, Any]
__init__(hash_algo='sha256', key_lengths=(16, 32), labels=(), context=b'', extra=<factory>)
Parameters:
Return type:

None

class BaseKDF[source]

Bases: ABC

Define the operations that a key derivation plugin supports.

Set the plugin name, protocol, and supported versions. Implement derivation, expansion, and pair validation. Override supported_secret_types() to advertise expansion support.

name: str = ''
protocol: str = ''
versions: Set[str] = {}
abstractmethod derive(secret, params)[source]

Derive output key material from secret using params.

Parameters:
Return type:

bytes

abstractmethod expand_traffic_secret(secret, key_lengths=None, hash_algo='sha256')[source]

Expand a traffic/session secret into derived keys and IVs.

Parameters:
  • secret (CryptoSecret)

  • key_lengths (List[int] | None)

  • hash_algo (str)

Return type:

List[CryptoSecret]

abstractmethod validate_pair(candidate_a, candidate_b, dump_data, hash_algo='sha256', hash_candidates=None)[source]

Test whether candidate_a and candidate_b are KDF-related.

hash_candidates is an optional list of byte-strings discovered in the dump that may serve as protocol-specific KDF inputs (e.g. the SSH-2 exchange hash H and session_id). Plugins that do not need such inputs (TLS 1.2 / 1.3) ignore it; SSH-2 requires it to validate honestly.

Returns a confidence score between 0.0 (unrelated) and 1.0 (confirmed relationship).

Parameters:
Return type:

float

supported_secret_types()[source]

Return secret types this KDF can expand.

Override in subclasses; the default returns an empty set (no expansion capability).

Return type:

Set[str]

Discover built-in and installed key derivation plugins.

Installed packages can register subclasses in the memdiver.kdfs entry-point group.

KDF_ENTRY_POINT_GROUP = 'memdiver.kdfs'

Entry-point group under which out-of-tree packages advertise KDFs.

class KDFRegistry[source]

Bases: object

Discover and manage KDF plugins from core/kdf_*.py modules.

__init__()[source]
discover()[source]

Walk core/kdf_*.py modules and register BaseKDF subclasses.

Per-plugin instantiation failures are isolated (a broken KDF no longer aborts discovery of the rest). Also loads any KDFs advertised by installed packages under the memdiver.kdfs entry-point group.

Return type:

None

get(name)[source]

Return KDF plugin by name, or None.

Parameters:

name (str)

Return type:

BaseKDF | None

get_for_protocol(protocol, version)[source]

Return the first KDF matching protocol and version.

Parameters:
  • protocol (str)

  • version (str)

Return type:

BaseKDF | None

list_all()[source]

Return all registered KDF plugins.

Return type:

List[BaseKDF]

get_kdf_registry()[source]

Return the lazily-initialised global KDF registry.

Return type:

KDFRegistry