Source code for core.kdf

"""Implement TLS 1.2 and TLS 1.3 key derivation for validation.

Use HMAC and hashlib from the Python standard library.
"""

import hashlib
import hmac
import logging

logger = logging.getLogger("memdiver.kdf")


# -- Hash length lookup --------------------------------------------------- #


def _hash_length(hash_algo: str) -> int:
    """Return the output length in bytes for *hash_algo*."""
    return hashlib.new(hash_algo).digest_size


[docs] class TLS12PRF: """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. """
[docs] @staticmethod def p_hash( secret: bytes, seed: bytes, length: int, hash_algo: str = "sha256", ) -> bytes: """Expand the seed with the selected HMAC hash as specified by RFC 5246.""" result = bytearray() a_value = seed # A(0) = seed while len(result) < length: # A(i) = HMAC(secret, A(i-1)) a_value = hmac.new(secret, a_value, hash_algo).digest() # P_hash chunk = HMAC(secret, A(i) + seed) result.extend(hmac.new(secret, a_value + seed, hash_algo).digest()) return bytes(result[:length])
[docs] @staticmethod def prf( secret: bytes, label: bytes, seed: bytes, length: int, hash_algo: str = "sha256", ) -> bytes: """Apply P_hash to the label followed by the seed.""" return TLS12PRF.p_hash(secret, label + seed, length, hash_algo)
[docs] @staticmethod def derive_master_secret( pre_master_secret: bytes, client_random: bytes, server_random: bytes, hash_algo: str = "sha256", ) -> bytes: """Derive the 48-byte master secret (RFC 5246 Section 8.1).""" return TLS12PRF.prf( pre_master_secret, b"master secret", client_random + server_random, 48, hash_algo, )
[docs] @staticmethod def derive_key_block( master_secret: bytes, server_random: bytes, client_random: bytes, length: int, hash_algo: str = "sha256", ) -> bytes: """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. """ return TLS12PRF.prf( master_secret, b"key expansion", server_random + client_random, length, hash_algo, )
[docs] class TLS13HKDF: """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. """
[docs] @staticmethod def hkdf_extract( salt: bytes, ikm: bytes, hash_algo: str = "sha256", ) -> bytes: """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. """ if not salt: salt = b"\x00" * _hash_length(hash_algo) return hmac.new(salt, ikm, hash_algo).digest()
[docs] @staticmethod def hkdf_expand( prk: bytes, info: bytes, length: int, hash_algo: str = "sha256", ) -> bytes: """Expand a pseudorandom key as specified by RFC 5869 Section 2.3. Raise ``ValueError`` if the request exceeds 255 hash output blocks. """ hash_len = _hash_length(hash_algo) n = (length + hash_len - 1) // hash_len if n > 255: raise ValueError( f"HKDF-Expand: requested {length} bytes exceeds maximum " f"({255 * hash_len} bytes for {hash_algo})" ) okm = bytearray() t_prev = b"" for i in range(1, n + 1): t_prev = hmac.new( prk, t_prev + info + bytes([i]), hash_algo ).digest() okm.extend(t_prev) return bytes(okm[:length])
[docs] @staticmethod def hkdf_expand_label( secret: bytes, label: str, context: bytes, length: int, hash_algo: str = "sha256", ) -> bytes: """HKDF-Expand-Label: builds HkdfLabel struct (RFC 8446 Section 7.1).""" full_label = b"tls13 " + label.encode("ascii") hkdf_label = ( length.to_bytes(2, "big") + len(full_label).to_bytes(1, "big") + full_label + len(context).to_bytes(1, "big") + context ) return TLS13HKDF.hkdf_expand(secret, hkdf_label, length, hash_algo)
[docs] @staticmethod def derive_secret( secret: bytes, label: str, messages_hash: bytes, hash_algo: str = "sha256", ) -> bytes: """Derive-Secret (RFC 8446 Section 7.1).""" return TLS13HKDF.hkdf_expand_label( secret, label, messages_hash, _hash_length(hash_algo), hash_algo, )