Skip to content

Instantly share code, notes, and snippets.

@randombit
Created May 31, 2018 21:15
Show Gist options
  • Save randombit/56a0b18ab8dd3890a35fcbba57bd7564 to your computer and use it in GitHub Desktop.
Save randombit/56a0b18ab8dd3890a35fcbba57bd7564 to your computer and use it in GitHub Desktop.
Format Wycheproof ECDSA test data
!/usr/bin/python
import json
import hashlib
import binascii
from pyasn1.codec.der.decoder import decode as der_decoder
from pyasn1.type.univ import Sequence
from pyasn1.type.univ import Integer
from pyasn1.type.namedtype import NamedTypes
from pyasn1.type.namedtype import NamedType
def hash_msg(msg, sha):
if sha =='SHA-224':
h = hashlib.sha224()
elif sha =='SHA-256':
h = hashlib.sha256()
elif sha =='SHA-384':
h = hashlib.sha384()
elif sha =='SHA-512':
h = hashlib.sha512()
else:
raise Exception('what hash is ' + sha)
h.update(binascii.unhexlify(msg))
return h.hexdigest()
class DERSig(Sequence):
componentType = NamedTypes(
NamedType('r', Integer()),
NamedType('s', Integer())
)
def format_der_sig(sig, sz):
sig, rest = der_decoder(binascii.unhexlify(sig), asn1Spec=DERSig())
if len(rest) != 0:
raise Exception('Bad encoding')
r = sig['r']
s = sig['s']
r_enc = hex(r)[2:]
s_enc = hex(s)[2:]
r_padding = "0" * (2*sz - len(r_enc))
s_padding = "0" * (2*sz - len(s_enc))
return (r_padding + r_enc + s_padding + s_enc)
last_group = None
last_msg = None
last_hash = None
last_valid = None
def format_test(test):
#print(test)
wx = int(test['key']['wx'], 16)
wy = int(test['key']['wy'], 16)
sha = test['sha']
group = test['key']['curve']
sz = int(test['key']['keySize']) // 8
group = group.replace('brainpoolP', 'brainpool')
global last_group
global last_msg
global last_hash
global last_valid
if last_group != group:
print("Group = %s" % group)
last_group = group
if last_hash != sha:
print("Hash = %s\n" % (sha))
last_hash = sha
print("Px = 0x%x" % wx)
print("Py = 0x%x" % wy)
for t in test['tests']:
msg = t['msg']
comment = t['comment']
sig = t['sig']
tcid = int(t['tcId'])
valid = t['result'] == 'valid'
print("# Test %d (%s)" %( tcid, comment))
if msg != last_msg:
print("Msg = %s" % msg)
last_msg = msg
if valid != last_valid:
print("Valid = %d" % (valid))
last_valid = valid
print("Signature = %s" % sig)
print()
def format_vec(vec):
blob = json.loads(open(vec).read())
for test in blob['testGroups']:
format_test(test)
vecs = [
'ecdsa_brainpoolP224r1_sha224_test.json',
'ecdsa_brainpoolP256r1_sha256_test.json',
'ecdsa_brainpoolP320r1_sha384_test.json',
'ecdsa_brainpoolP384r1_sha384_test.json',
'ecdsa_brainpoolP512r1_sha512_test.json',
'ecdsa_secp224r1_sha224_test.json',
'ecdsa_secp224r1_sha256_test.json',
'ecdsa_secp256k1_sha256_test.json',
'ecdsa_secp256r1_sha256_test.json',
'ecdsa_secp384r1_sha384_test.json',
'ecdsa_secp384r1_sha512_test.json',
'ecdsa_secp521r1_sha512_test.json',
]
for vec in vecs:
format_vec(vec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment