summaryrefslogtreecommitdiff
path: root/src/ecdsa/test_curves.py
blob: 93b6c9bdedd21dd9a2ee0fa104dd0761780cf704 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
try:
    import unittest2 as unittest
except ImportError:
    import unittest

import base64
import pytest
from .curves import (
    Curve,
    NIST256p,
    curves,
    UnknownCurveError,
    PRIME_FIELD_OID,
    curve_by_name,
)
from .ellipticcurve import CurveFp, PointJacobi, CurveEdTw
from . import der
from .util import number_to_string


class TestParameterEncoding(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # minimal, but with cofactor (excludes seed when compared to
        # OpenSSL output)
        cls.base64_params = (
            "MIHgAgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP/////////"
            "//////zBEBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12K"
            "o6k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsEQQRrF9Hy4SxCR/i85uVjpEDyd"
            "wN9gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1"
            "AiEA/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQE="
        )

    def test_from_pem(self):
        pem_params = (
            "-----BEGIN EC PARAMETERS-----\n"
            "MIHgAgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP/////////\n"
            "//////zBEBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12K\n"
            "o6k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsEQQRrF9Hy4SxCR/i85uVjpEDyd\n"
            "wN9gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1\n"
            "AiEA/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQE=\n"
            "-----END EC PARAMETERS-----\n"
        )
        curve = Curve.from_pem(pem_params)

        self.assertIs(curve, NIST256p)

    def test_from_pem_with_explicit_when_explicit_disabled(self):
        pem_params = (
            "-----BEGIN EC PARAMETERS-----\n"
            "MIHgAgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP/////////\n"
            "//////zBEBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12K\n"
            "o6k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsEQQRrF9Hy4SxCR/i85uVjpEDyd\n"
            "wN9gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1\n"
            "AiEA/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQE=\n"
            "-----END EC PARAMETERS-----\n"
        )
        with self.assertRaises(der.UnexpectedDER) as e:
            Curve.from_pem(pem_params, ["named_curve"])

        self.assertIn("explicit curve parameters not", str(e.exception))

    def test_from_pem_with_named_curve_with_named_curve_disabled(self):
        pem_params = (
            "-----BEGIN EC PARAMETERS-----\n"
            "BggqhkjOPQMBBw==\n"
            "-----END EC PARAMETERS-----\n"
        )
        with self.assertRaises(der.UnexpectedDER) as e:
            Curve.from_pem(pem_params, ["explicit"])

        self.assertIn("named_curve curve parameters not", str(e.exception))

    def test_from_pem_with_wrong_header(self):
        pem_params = (
            "-----BEGIN PARAMETERS-----\n"
            "MIHgAgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP/////////\n"
            "//////zBEBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12K\n"
            "o6k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsEQQRrF9Hy4SxCR/i85uVjpEDyd\n"
            "wN9gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1\n"
            "AiEA/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQE=\n"
            "-----END PARAMETERS-----\n"
        )
        with self.assertRaises(der.UnexpectedDER) as e:
            Curve.from_pem(pem_params)

        self.assertIn("PARAMETERS PEM header", str(e.exception))

    def test_to_pem(self):
        pem_params = (
            b"-----BEGIN EC PARAMETERS-----\n"
            b"BggqhkjOPQMBBw==\n"
            b"-----END EC PARAMETERS-----\n"
        )
        encoding = NIST256p.to_pem()

        self.assertEqual(pem_params, encoding)

    def test_compare_with_different_object(self):
        self.assertNotEqual(NIST256p, 256)

    def test_named_curve_params_der(self):
        encoded = NIST256p.to_der()

        # just the encoding of the NIST256p OID (prime256v1)
        self.assertEqual(b"\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07", encoded)

    def test_verify_that_default_is_named_curve_der(self):
        encoded_default = NIST256p.to_der()
        encoded_named = NIST256p.to_der("named_curve")

        self.assertEqual(encoded_default, encoded_named)

    def test_encoding_to_explicit_params(self):
        encoded = NIST256p.to_der("explicit")

        self.assertEqual(encoded, bytes(base64.b64decode(self.base64_params)))

    def test_encoding_to_unsupported_type(self):
        with self.assertRaises(ValueError) as e:
            NIST256p.to_der("unsupported")

        self.assertIn("Only 'named_curve'", str(e.exception))

    def test_encoding_to_explicit_compressed_params(self):
        encoded = NIST256p.to_der("explicit", "compressed")

        compressed_base_point = (
            "MIHAAgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP//////////"
            "/////zBEBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12Ko6"
            "k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsEIQNrF9Hy4SxCR/i85uVjpEDydwN9"
            "gS3rM6D0oTlF2JjClgIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8YyVR"
            "AgEB"
        )

        self.assertEqual(
            encoded, bytes(base64.b64decode(compressed_base_point))
        )

    def test_decoding_explicit_from_openssl(self):
        # generated with openssl 1.1.1k using
        # openssl ecparam -name P-256 -param_enc explicit -out /tmp/file.pem
        p256_explicit = (
            "MIH3AgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP//////////"
            "/////zBbBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12Ko6"
            "k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsDFQDEnTYIhucEk2pmeOETnSa3gZ9+"
            "kARBBGsX0fLhLEJH+Lzm5WOkQPJ3A32BLeszoPShOUXYmMKWT+NC4v4af5uO5+tK"
            "fA+eFivOM1drMV7Oy7ZAaDe/UfUCIQD/////AAAAAP//////////vOb6racXnoTz"
            "ucrC/GMlUQIBAQ=="
        )

        decoded = Curve.from_der(bytes(base64.b64decode(p256_explicit)))

        self.assertEqual(NIST256p, decoded)

    def test_decoding_well_known_from_explicit_params(self):
        curve = Curve.from_der(bytes(base64.b64decode(self.base64_params)))

        self.assertIs(curve, NIST256p)

    def test_decoding_with_incorrect_valid_encodings(self):
        with self.assertRaises(ValueError) as e:
            Curve.from_der(b"", ["explicitCA"])

        self.assertIn("Only named_curve", str(e.exception))

    def test_compare_curves_with_different_generators(self):
        curve_fp = CurveFp(23, 1, 7)
        base_a = PointJacobi(curve_fp, 13, 3, 1, 9, generator=True)
        base_b = PointJacobi(curve_fp, 1, 20, 1, 9, generator=True)

        curve_a = Curve("unknown", curve_fp, base_a, None)
        curve_b = Curve("unknown", curve_fp, base_b, None)

        self.assertNotEqual(curve_a, curve_b)

    def test_default_encode_for_custom_curve(self):
        curve_fp = CurveFp(23, 1, 7)
        base_point = PointJacobi(curve_fp, 13, 3, 1, 9, generator=True)

        curve = Curve("unknown", curve_fp, base_point, None)

        encoded = curve.to_der()

        decoded = Curve.from_der(encoded)

        self.assertEqual(curve, decoded)

        expected = "MCECAQEwDAYHKoZIzj0BAQIBFzAGBAEBBAEHBAMEDQMCAQk="

        self.assertEqual(encoded, bytes(base64.b64decode(expected)))

    def test_named_curve_encode_for_custom_curve(self):
        curve_fp = CurveFp(23, 1, 7)
        base_point = PointJacobi(curve_fp, 13, 3, 1, 9, generator=True)

        curve = Curve("unknown", curve_fp, base_point, None)

        with self.assertRaises(UnknownCurveError) as e:
            curve.to_der("named_curve")

        self.assertIn("Can't encode curve", str(e.exception))

    def test_try_decoding_binary_explicit(self):
        sect113r1_explicit = (
            "MIGRAgEBMBwGByqGSM49AQIwEQIBcQYJKoZIzj0BAgMCAgEJMDkEDwAwiCUMpufH"
            "/mSc6Fgg9wQPAOi+5NPiJgdEGIvg6ccjAxUAEOcjqxTWluZ2h1YVF1b+v4/LSakE"
            "HwQAnXNhbzX0qxQH1zViwQ8ApSgwJ3lY7oTRMV7TGIYCDwEAAAAAAAAA2czsijnl"
            "bwIBAg=="
        )

        with self.assertRaises(UnknownCurveError) as e:
            Curve.from_der(base64.b64decode(sect113r1_explicit))

        self.assertIn("Characteristic 2 curves unsupported", str(e.exception))

    def test_decode_malformed_named_curve(self):
        bad_der = der.encode_oid(*NIST256p.oid) + der.encode_integer(1)

        with self.assertRaises(der.UnexpectedDER) as e:
            Curve.from_der(bad_der)

        self.assertIn("Unexpected data after OID", str(e.exception))

    def test_decode_malformed_explicit_garbage_after_ECParam(self):
        bad_der = bytes(
            base64.b64decode(self.base64_params)
        ) + der.encode_integer(1)

        with self.assertRaises(der.UnexpectedDER) as e:
            Curve.from_der(bad_der)

        self.assertIn("Unexpected data after ECParameters", str(e.exception))

    def test_decode_malformed_unknown_version_number(self):
        bad_der = der.encode_sequence(der.encode_integer(2))

        with self.assertRaises(der.UnexpectedDER) as e:
            Curve.from_der(bad_der)

        self.assertIn("Unknown parameter encoding format", str(e.exception))

    def test_decode_malformed_unknown_field_type(self):
        curve_p = NIST256p.curve.p()
        bad_der = der.encode_sequence(
            der.encode_integer(1),
            der.encode_sequence(
                der.encode_oid(1, 2, 3), der.encode_integer(curve_p)
            ),
            der.encode_sequence(
                der.encode_octet_string(
                    number_to_string(NIST256p.curve.a() % curve_p, curve_p)
                ),
                der.encode_octet_string(
                    number_to_string(NIST256p.curve.b(), curve_p)
                ),
            ),
            der.encode_octet_string(
                NIST256p.generator.to_bytes("uncompressed")
            ),
            der.encode_integer(NIST256p.generator.order()),
        )

        with self.assertRaises(UnknownCurveError) as e:
            Curve.from_der(bad_der)

        self.assertIn("Unknown field type: (1, 2, 3)", str(e.exception))

    def test_decode_malformed_garbage_after_prime(self):
        curve_p = NIST256p.curve.p()
        bad_der = der.encode_sequence(
            der.encode_integer(1),
            der.encode_sequence(
                der.encode_oid(*PRIME_FIELD_OID),
                der.encode_integer(curve_p),
                der.encode_integer(1),
            ),
            der.encode_sequence(
                der.encode_octet_string(
                    number_to_string(NIST256p.curve.a() % curve_p, curve_p)
                ),
                der.encode_octet_string(
                    number_to_string(NIST256p.curve.b(), curve_p)
                ),
            ),
            der.encode_octet_string(
                NIST256p.generator.to_bytes("uncompressed")
            ),
            der.encode_integer(NIST256p.generator.order()),
        )

        with self.assertRaises(der.UnexpectedDER) as e:
            Curve.from_der(bad_der)

        self.assertIn("Prime-p element", str(e.exception))


class TestCurveSearching(unittest.TestCase):
    def test_correct_name(self):
        c = curve_by_name("NIST256p")
        self.assertIs(c, NIST256p)

    def test_openssl_name(self):
        c = curve_by_name("prime256v1")
        self.assertIs(c, NIST256p)

    def test_unknown_curve(self):
        with self.assertRaises(UnknownCurveError) as e:
            curve_by_name("foo bar")

        self.assertIn(
            "name 'foo bar' unknown, only curves supported: "
            "['NIST192p', 'NIST224p'",
            str(e.exception),
        )

    def test_with_None_as_parameter(self):
        with self.assertRaises(UnknownCurveError) as e:
            curve_by_name(None)

        self.assertIn(
            "name None unknown, only curves supported: "
            "['NIST192p', 'NIST224p'",
            str(e.exception),
        )


@pytest.mark.parametrize("curve", curves, ids=[i.name for i in curves])
def test_curve_params_encode_decode_named(curve):
    ret = Curve.from_der(curve.to_der("named_curve"))

    assert curve == ret


@pytest.mark.parametrize("curve", curves, ids=[i.name for i in curves])
def test_curve_params_encode_decode_explicit(curve):
    if isinstance(curve.curve, CurveEdTw):
        with pytest.raises(UnknownCurveError):
            curve.to_der("explicit")
    else:
        ret = Curve.from_der(curve.to_der("explicit"))

        assert curve == ret


@pytest.mark.parametrize("curve", curves, ids=[i.name for i in curves])
def test_curve_params_encode_decode_default(curve):
    ret = Curve.from_der(curve.to_der())

    assert curve == ret


@pytest.mark.parametrize("curve", curves, ids=[i.name for i in curves])
def test_curve_params_encode_decode_explicit_compressed(curve):
    if isinstance(curve.curve, CurveEdTw):
        with pytest.raises(UnknownCurveError):
            curve.to_der("explicit", "compressed")
    else:
        ret = Curve.from_der(curve.to_der("explicit", "compressed"))

        assert curve == ret