aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/test_impl.py
blob: a61aabd021931e5da7e487743523ecbe78a2bf71 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from copy import copy
from os.path import join
from typing import Any, Generator

import pytest
from click.testing import CliRunner

from pyecsca.ec.formula import NegationFormula
from pyecsca.ec.key_agreement import ECDH_SHA1
from pyecsca.ec.mod import mod
from pyecsca.ec.mult import ScalarMultiplier, WindowBoothMultiplier
from pyecsca.ec.signature import ECDSA_SHA1, SignatureResult

from pyecsca.codegen.builder import build_impl
from pyecsca.codegen.client import HostTarget


@pytest.fixture(
    scope="module",
    params=[
        pytest.param(("--mul", "KARATSUBA", "--sqr", "KARATSUBA"), id="Karatsuba"),
        pytest.param(("--mul", "TOOM_COOK", "--sqr", "TOOM_COOK"), id="ToomCook"),
        pytest.param(("--red", "BARRETT"), id="Barrett"),
        pytest.param(("--red", "MONTGOMERY"), id="Montgomery"),
    ],
)
def additional(request):
    return request.param


@pytest.fixture(scope="module")
def target(
    simple_multiplier, additional, secp128r1
) -> Generator[HostTarget, Any, None]:
    mult_class, mult_kwargs = simple_multiplier
    mult_name = mult_class.__name__
    formulas = ["add-1998-cmo", "dbl-1998-cmo"]
    if NegationFormula in mult_class.requires:
        formulas.append("neg")
    runner = CliRunner()
    with runner.isolated_filesystem() as tmpdir:
        res = runner.invoke(
            build_impl,
            [
                "--platform",
                "HOST",
                "--ecdsa",
                "--ecdh",
                secp128r1.curve.model.shortname,
                secp128r1.curve.coordinate_model.name,
                *formulas,
                f"{mult_name}({','.join(f'{key}={value}' for key, value in mult_kwargs.items())})",
                ".",
            ],
            env={
                "CFLAGS": "-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
            },
        )
        assert res.exit_code == 0
        target = HostTarget(
            secp128r1.curve.model,
            secp128r1.curve.coordinate_model,
            binary=join(tmpdir, "pyecsca-codegen-HOST.elf"),
        )
        formula_instances = [
            secp128r1.curve.coordinate_model.formulas[formula] for formula in formulas
        ]
        mult = mult_class(*formula_instances, **mult_kwargs)
        target.mult = mult
        yield target


@pytest.fixture(scope="module")
def mult(target) -> ScalarMultiplier:
    return target.mult  # noqa


def test_init(target):
    target.connect()
    target.init_prng(bytes([0x12, 0x34, 0x56, 0x78]))
    target.disconnect()


def test_setup(target, mult, secp128r1):
    priv = 57
    mult.init(secp128r1, secp128r1.generator)
    pub = mult.multiply(priv).to_affine()
    target.connect()
    target.set_privkey(priv)
    target.set_pubkey(pub)
    target.disconnect()


def test_debug(target, secp128r1):
    target.connect()
    model, coords = target.debug()
    assert model == secp128r1.curve.model.shortname
    assert coords == secp128r1.curve.coordinate_model.name
    target.disconnect()


def test_keygen(target, mult, secp128r1):
    target.connect()
    target.set_params(secp128r1)
    mult.init(secp128r1, secp128r1.generator)
    for _ in range(10):
        priv, pub = target.generate()
        assert secp128r1.curve.is_on_curve(pub)
        expected = mult.multiply(priv).to_affine()
        assert pub == expected
    target.disconnect()


def test_scalarmult(target, mult, secp128r1):
    target.connect()
    target.set_params(secp128r1)
    mult.init(secp128r1, secp128r1.generator)
    values = [15, 2355498743, 3253857901321912443757746]
    for value in values:
        result = target.scalar_mult(value, target.params.generator)
        expected = mult.multiply(value)
        assert result == expected
    target.disconnect()


def test_ecdh(target, mult, secp128r1):
    target.connect()
    target.set_params(secp128r1)
    mult.init(secp128r1, secp128r1.generator)
    other_privs = [15, 2355498743, 3253857901321912443757746]
    for other_priv in other_privs:
        priv, pub = target.generate()
        other_pub = mult.multiply(other_priv)
        ecdh = ECDH_SHA1(copy(mult), secp128r1, other_pub, mod(priv, secp128r1.order))
        result = target.ecdh(other_pub)
        expected = ecdh.perform()
        assert result == expected
    target.disconnect()


def test_ecdsa(target, mult, secp128r1):
    target.connect()
    target.set_params(secp128r1)
    mult.init(secp128r1, secp128r1.generator)

    messages = [b"something", b"something quite a bit longer than the hash thing"]
    for message in messages:
        priv, pub = target.generate()
        ecdsa = ECDSA_SHA1(
            copy(mult),
            secp128r1,
            mult.formulas["add"],
            pub.to_model(secp128r1.curve.coordinate_model, secp128r1.curve),
            mod(priv, secp128r1.order),
        )

        signature_data = target.ecdsa_sign(message)
        result = SignatureResult.from_DER(signature_data)
        assert ecdsa.verify_data(result, message)

        expected = ecdsa.sign_data(message).to_DER()
        assert target.ecdsa_verify(message, expected)
    target.disconnect()