"""Define the interface and parameters for key derivation plugins."""
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Set
from memdiver.core.models import CryptoSecret
[docs]
@dataclass(frozen=True)
class KDFParams:
"""Parameters for a key derivation operation."""
hash_algo: str = "sha256"
key_lengths: tuple = (16, 32)
labels: tuple = ()
context: bytes = b""
extra: Dict[str, Any] = field(default_factory=dict)
[docs]
class BaseKDF(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
:meth:`supported_secret_types` to advertise expansion support.
"""
name: str = ""
protocol: str = ""
versions: Set[str] = set()
[docs]
@abstractmethod
def derive(self, secret: bytes, params: KDFParams) -> bytes:
"""Derive output key material from *secret* using *params*."""
[docs]
@abstractmethod
def expand_traffic_secret(
self,
secret: CryptoSecret,
key_lengths: Optional[List[int]] = None,
hash_algo: str = "sha256",
) -> List[CryptoSecret]:
"""Expand a traffic/session secret into derived keys and IVs."""
[docs]
@abstractmethod
def validate_pair(
self,
candidate_a: bytes,
candidate_b: bytes,
dump_data: bytes,
hash_algo: str = "sha256",
hash_candidates: Optional[List[bytes]] = None,
) -> float:
"""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).
"""
[docs]
def supported_secret_types(self) -> Set[str]:
"""Return secret types this KDF can expand.
Override in subclasses; the default returns an empty set
(no expansion capability).
"""
return set()
def __repr__(self) -> str:
return f"{type(self).__name__}(name={self.name!r}, protocol={self.protocol!r})"