"""Smoke test for step-up key derivation parity with the applet."""

import hashlib
import hmac

from aliro_harness.reader.crypto import derive_step_up_session_keys


def _hkdf_manual(ikm: bytes, salt: bytes, info: bytes, length: int) -> bytes:
    """Bare-stdlib RFC 5869 reference. Independent of `cryptography` so the
    cross-check actually catches a wrong-info / wrong-salt regression in
    derive_step_up_session_keys."""
    use_salt = salt if salt else b"\x00" * 32
    prk = hmac.new(use_salt, ikm, hashlib.sha256).digest()
    out = b""
    t = b""
    counter = 1
    while len(out) < length:
        t = hmac.new(prk, t + info + bytes([counter]), hashlib.sha256).digest()
        out += t
        counter += 1
    return out[:length]


def test_derive_step_up_session_keys_matches_rfc5869():
    # Same IKM as AliroCryptoTest.deriveStepUpSessionKeysMatchesManualHkdf:
    # bytes 0xC0..0xDF -- so this Python test pins the same vector the
    # applet test pins.
    step_up_sk = bytes(range(0xC0, 0xE0))

    sk_device, sk_reader = derive_step_up_session_keys(step_up_sk)

    assert len(sk_device) == 32
    assert len(sk_reader) == 32
    assert sk_device != sk_reader
    assert sk_device != step_up_sk

    assert sk_device == _hkdf_manual(step_up_sk, b"", b"SKDevice", 32)
    assert sk_reader == _hkdf_manual(step_up_sk, b"", b"SKReader", 32)
