aboutsummaryrefslogtreecommitdiffstats
path: root/python/libghdl/thin/vhdl/lists.py
blob: 8fc18007306244b5990c769c07392d2ebecfe9d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from libghdl import libghdl
from ctypes import c_int32, c_bool, POINTER, Structure

List_Type = c_int32

class Iterator(Structure):
    _fields_ = [("chunk", c_int32),
                ("chunk_idx", c_int32),
                ("remain", c_int32)]

Iterate = libghdl.vhdl__lists__iterate
Iterate.argstype = [List_Type]
Iterate.restype = Iterator

Is_Valid = libghdl.vhdl__lists__is_valid
Is_Valid.argstype = [POINTER(Iterator)]
Is_Valid.restype = c_bool

Next = libghdl.vhdl__lists__next
Next.argstype = [POINTER(Iterator)]
Next.restype = None

Get_Element = libghdl.vhdl__lists__get_element
Get_Element.argstype = [POINTER(Iterator)]
Get_Element.restype = c_int32

Get_Nbr_Elements = libghdl.vhdl__lists__get_nbr_elements
Get_Nbr_Elements.argtype = [List_Type]
Get_Nbr_Elements.restype = c_int32

Create_Iir_List = libghdl.vhdl__lists__create_list

Destroy_Iir_List = libghdl.vhdl__lists__destroy_list
lass="n">sha256, "SHA-384": hashlib.sha384, "SHA-512": hashlib.sha512, } class TruncatedHash(object): def __init__(self, hasher): self.hasher = hasher def __call__(self, data): self.hasher.update(data) return self def digest(self): return self.hasher.digest()[:256 // 8] def build_vectors(fips_vectors): vectors = defaultdict(list) for vector in fips_vectors: vectors[vector['digest_algorithm']].append(vector['message']) for digest_algorithm, messages in vectors.items(): if digest_algorithm not in HASHLIB_HASH_TYPES: continue yield "" yield "[K-256,{0}]".format(digest_algorithm) yield "" for message in messages: # Make a hash context hash_func = TruncatedHash(HASHLIB_HASH_TYPES[digest_algorithm]()) # Sign the message using warner/ecdsa secret_key = SigningKey.generate(curve=SECP256k1) public_key = secret_key.get_verifying_key() signature = secret_key.sign(message, hashfunc=hash_func, sigencode=sigencode_der) r, s = sigdecode_der(signature, None) yield "Msg = {0}".format(hexlify(message)) yield "d = {0:x}".format(secret_key.privkey.secret_multiplier) yield "Qx = {0:x}".format(public_key.pubkey.point.x()) yield "Qy = {0:x}".format(public_key.pubkey.point.y()) yield "R = {0:x}".format(r) yield "S = {0:x}".format(s) yield "" def write_file(lines, dest): for line in lines: print(line) print(line, file=dest) source_path = os.path.join("asymmetric", "ECDSA", "FIPS_186-3", "SigGen.txt") dest_path = os.path.join("asymmetric", "ECDSA", "SECP256K1", "SigGen.txt") fips_vectors = load_vectors_from_file( source_path, load_fips_ecdsa_signing_vectors ) with open_vector_file(dest_path, "w") as dest_file: write_file( build_vectors(fips_vectors), dest_file )