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.exceptions import InvalidSignature 12from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey 13from cryptography.hazmat.primitives.asymmetric.ed448 import Ed448PublicKey 14 15 16@pytest.mark.supported( 17 only_if=lambda backend: backend.ed25519_supported(), 18 skip_message="Requires OpenSSL with Ed25519 support", 19) 20@pytest.mark.wycheproof_tests("eddsa_test.json") 21def test_ed25519_signature(backend, wycheproof): 22 # We want to fail if/when wycheproof adds more edwards curve tests 23 # so we can add them as well. 24 assert wycheproof.testgroup["key"]["curve"] == "edwards25519" 25 26 key = Ed25519PublicKey.from_public_bytes( 27 binascii.unhexlify(wycheproof.testgroup["key"]["pk"]) 28 ) 29 30 if wycheproof.valid or wycheproof.acceptable: 31 key.verify( 32 binascii.unhexlify(wycheproof.testcase["sig"]), 33 binascii.unhexlify(wycheproof.testcase["msg"]), 34 ) 35 else: 36 with pytest.raises(InvalidSignature): 37 key.verify( 38 binascii.unhexlify(wycheproof.testcase["sig"]), 39 binascii.unhexlify(wycheproof.testcase["msg"]), 40 ) 41 42 43@pytest.mark.supported( 44 only_if=lambda backend: backend.ed448_supported(), 45 skip_message="Requires OpenSSL with Ed448 support", 46) 47@pytest.mark.wycheproof_tests("ed448_test.json") 48def test_ed448_signature(backend, wycheproof): 49 key = Ed448PublicKey.from_public_bytes( 50 binascii.unhexlify(wycheproof.testgroup["key"]["pk"]) 51 ) 52 53 if wycheproof.valid or wycheproof.acceptable: 54 key.verify( 55 binascii.unhexlify(wycheproof.testcase["sig"]), 56 binascii.unhexlify(wycheproof.testcase["msg"]), 57 ) 58 else: 59 with pytest.raises(InvalidSignature): 60 key.verify( 61 binascii.unhexlify(wycheproof.testcase["sig"]), 62 binascii.unhexlify(wycheproof.testcase["msg"]), 63 ) 64