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.x25519 import ( 12 X25519PrivateKey, 13 X25519PublicKey, 14) 15 16 17@pytest.mark.supported( 18 only_if=lambda backend: backend.x25519_supported(), 19 skip_message="Requires OpenSSL with X25519 support", 20) 21@pytest.mark.wycheproof_tests("x25519_test.json") 22def test_x25519(backend, wycheproof): 23 assert set(wycheproof.testgroup.items()) == { 24 ("curve", "curve25519"), 25 ("type", "XdhComp"), 26 } 27 28 private_key = X25519PrivateKey.from_private_bytes( 29 binascii.unhexlify(wycheproof.testcase["private"]) 30 ) 31 public_key = X25519PublicKey.from_public_bytes( 32 binascii.unhexlify(wycheproof.testcase["public"]) 33 ) 34 35 assert wycheproof.valid or wycheproof.acceptable 36 37 expected = binascii.unhexlify(wycheproof.testcase["shared"]) 38 if expected == b"\x00" * 32: 39 assert wycheproof.acceptable 40 # OpenSSL returns an error on all zeros shared key 41 with pytest.raises(ValueError): 42 private_key.exchange(public_key) 43 else: 44 assert private_key.exchange(public_key) == expected 45