aboutsummaryrefslogtreecommitdiff
path: root/tools/tests/test_gen_fixture_wrap.py
blob: 1c72e662de0fd57ae729b40ab0490dcab5204693 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Author:  cirakg & claude ai
# Summary: Tests that the fixture PIN wrap generator matches the constants in source/app/Fixtures.mc and RFC 5869.
from __future__ import annotations

import re
import shutil
from pathlib import Path

import gen_fixture_wrap
import pytest

ROOT = Path(__file__).resolve().parents[2]


def fixture_constant(name: str) -> str:
    text = (ROOT / "source" / "app" / "Fixtures.mc").read_text(encoding="utf-8")
    match = re.search(rf'const {name} = "([0-9a-f]+)"', text)
    assert match, f"{name} not found in Fixtures.mc"
    return match.group(1)


def test_hkdf_matches_rfc5869_case_3() -> None:
    # RFC 5869 A.3: empty salt and info.
    okm = gen_fixture_wrap.hkdf(bytes([0x0B] * 22), b"", 42)
    assert okm.hex() == (
        "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8"  # RFC 5869 A.3
    )


@pytest.mark.skipif(shutil.which("openssl") is None, reason="needs openssl for AES")
def test_generated_wrap_matches_the_fixture() -> None:
    assert gen_fixture_wrap.fixture_wrap().hex() == fixture_constant("FIXTURE_WRAP")
    assert gen_fixture_wrap.MASTER_KEY.hex() == fixture_constant("FIXTURE_MASTER_KEY")