• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import absolute_import, print_function
2
3import os
4
5from cryptography.hazmat.backends import default_backend
6from cryptography.hazmat.primitives import hashes
7from cryptography.hazmat.primitives.asymmetric import ec
8from cryptography.hazmat.primitives.asymmetric.utils import (
9    encode_dss_signature
10)
11
12from tests.utils import (
13    load_fips_ecdsa_signing_vectors, load_vectors_from_file
14)
15
16CRYPTOGRAPHY_HASH_TYPES = {
17    "SHA-1": hashes.SHA1,
18    "SHA-224": hashes.SHA224,
19    "SHA-256": hashes.SHA256,
20    "SHA-384": hashes.SHA384,
21    "SHA-512": hashes.SHA512,
22}
23
24
25def verify_one_vector(vector):
26    digest_algorithm = vector['digest_algorithm']
27    message = vector['message']
28    x = vector['x']
29    y = vector['y']
30    signature = encode_dss_signature(vector['r'], vector['s'])
31
32    numbers = ec.EllipticCurvePublicNumbers(
33        x, y,
34        ec.SECP256K1()
35    )
36
37    key = numbers.public_key(default_backend())
38
39    verifier = key.verifier(
40        signature,
41        ec.ECDSA(CRYPTOGRAPHY_HASH_TYPES[digest_algorithm]())
42    )
43    verifier.update(message)
44    return verifier.verify()
45
46
47def verify_vectors(vectors):
48    for vector in vectors:
49        assert verify_one_vector(vector)
50
51
52vector_path = os.path.join("asymmetric", "ECDSA", "SECP256K1", "SigGen.txt")
53
54secp256k1_vectors = load_vectors_from_file(
55    vector_path,
56    load_fips_ecdsa_signing_vectors
57)
58
59verify_vectors(secp256k1_vectors)
60