1from __future__ import absolute_import, print_function 2 3import hashlib 4import os 5from binascii import hexlify 6from collections import defaultdict 7 8from ecdsa import SECP256k1, SigningKey 9from ecdsa.util import sigdecode_der, sigencode_der 10 11from cryptography_vectors import open_vector_file 12 13from tests.utils import load_fips_ecdsa_signing_vectors, load_vectors_from_file 14 15HASHLIB_HASH_TYPES = { 16 "SHA-1": hashlib.sha1, 17 "SHA-224": hashlib.sha224, 18 "SHA-256": hashlib.sha256, 19 "SHA-384": hashlib.sha384, 20 "SHA-512": hashlib.sha512, 21} 22 23 24class TruncatedHash(object): 25 def __init__(self, hasher): 26 self.hasher = hasher 27 28 def __call__(self, data): 29 self.hasher.update(data) 30 return self 31 32 def digest(self): 33 return self.hasher.digest()[: 256 // 8] 34 35 36def build_vectors(fips_vectors): 37 vectors = defaultdict(list) 38 for vector in fips_vectors: 39 vectors[vector["digest_algorithm"]].append(vector["message"]) 40 41 for digest_algorithm, messages in vectors.items(): 42 if digest_algorithm not in HASHLIB_HASH_TYPES: 43 continue 44 45 yield "" 46 yield "[K-256,{0}]".format(digest_algorithm) 47 yield "" 48 49 for message in messages: 50 # Make a hash context 51 hash_func = TruncatedHash(HASHLIB_HASH_TYPES[digest_algorithm]()) 52 53 # Sign the message using warner/ecdsa 54 secret_key = SigningKey.generate(curve=SECP256k1) 55 public_key = secret_key.get_verifying_key() 56 signature = secret_key.sign( 57 message, hashfunc=hash_func, sigencode=sigencode_der 58 ) 59 60 r, s = sigdecode_der(signature, None) 61 62 yield "Msg = {0}".format(hexlify(message)) 63 yield "d = {0:x}".format(secret_key.privkey.secret_multiplier) 64 yield "Qx = {0:x}".format(public_key.pubkey.point.x()) 65 yield "Qy = {0:x}".format(public_key.pubkey.point.y()) 66 yield "R = {0:x}".format(r) 67 yield "S = {0:x}".format(s) 68 yield "" 69 70 71def write_file(lines, dest): 72 for line in lines: 73 print(line) 74 print(line, file=dest) 75 76 77source_path = os.path.join("asymmetric", "ECDSA", "FIPS_186-3", "SigGen.txt") 78dest_path = os.path.join("asymmetric", "ECDSA", "SECP256K1", "SigGen.txt") 79 80fips_vectors = load_vectors_from_file( 81 source_path, load_fips_ecdsa_signing_vectors 82) 83 84with open_vector_file(dest_path, "w") as dest_file: 85 write_file(build_vectors(fips_vectors), dest_file) 86