• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
4
5from __future__ import absolute_import, division, print_function
6
7import binascii
8
9import pytest
10
11from cryptography.hazmat.primitives.asymmetric.x448 import (
12    X448PrivateKey,
13    X448PublicKey,
14)
15
16
17@pytest.mark.supported(
18    only_if=lambda backend: backend.x448_supported(),
19    skip_message="Requires OpenSSL with X448 support",
20)
21@pytest.mark.wycheproof_tests("x448_test.json")
22def test_x448(backend, wycheproof):
23    assert set(wycheproof.testgroup.items()) == {
24        ("curve", "curve448"),
25        ("type", "XdhComp"),
26    }
27
28    private_key = X448PrivateKey.from_private_bytes(
29        binascii.unhexlify(wycheproof.testcase["private"])
30    )
31    public_key_bytes = binascii.unhexlify(wycheproof.testcase["public"])
32    if len(public_key_bytes) == 57:
33        assert wycheproof.acceptable
34        assert wycheproof.has_flag("NonCanonicalPublic")
35        with pytest.raises(ValueError):
36            X448PublicKey.from_public_bytes(public_key_bytes)
37        return
38
39    public_key = X448PublicKey.from_public_bytes(public_key_bytes)
40
41    assert wycheproof.valid or wycheproof.acceptable
42
43    expected = binascii.unhexlify(wycheproof.testcase["shared"])
44    if expected == b"\x00" * 56:
45        assert wycheproof.acceptable
46        # OpenSSL returns an error on all zeros shared key
47        with pytest.raises(ValueError):
48            private_key.exchange(public_key)
49    else:
50        assert private_key.exchange(public_key) == expected
51