Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 1672823 - Add Wycheproof DSA test cases. r=jcj
Differential Revision: https://phabricator.services.mozilla.com/D94495

--HG--
extra : moz-landing-system : lando
  • Loading branch information
Kevin Jacobs committed Nov 3, 2020
1 parent d1f32b4 commit f8e7a39
Show file tree
Hide file tree
Showing 7 changed files with 21,189 additions and 3 deletions.
12,798 changes: 12,798 additions & 0 deletions gtests/common/testvectors/dsa-vectors.h

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions gtests/common/testvectors_base/test-structs.h
Expand Up @@ -66,6 +66,8 @@ typedef struct EcdsaTestVectorStr {
bool valid;
} EcdsaTestVector;

typedef EcdsaTestVector DsaTestVector;

typedef struct EcdhTestVectorStr {
uint32_t id;
std::vector<uint8_t> private_key;
Expand Down
39 changes: 36 additions & 3 deletions gtests/common/wycheproof/genTestVectors.py
Expand Up @@ -158,10 +158,31 @@ def format_testcase(self, testcase, curve):

return result

class DSA():
pub_keys = {}
def format_testcase(self, testcase, key, hash_oid, keySize, out_defs):
key_name = "kPubKey"
if key in self.pub_keys:
key_name = self.pub_keys[key]
else:
key_name += str(len(self.pub_keys))
self.pub_keys[key] = key_name
out_defs.append('static const std::vector<uint8_t> ' + key_name + string_to_hex_array(key) + ';\n\n')
result = '\n// Comment: {}'.format(testcase['comment'])
result += '\n// tcID: {}\n'.format(testcase['tcId'])
result += '{{{}, {},\n'.format(hash_oid, testcase['tcId'])
result += '{},\n'.format(string_to_hex_array(testcase['sig']))
result += '{},\n'.format(key_name)
result += '{},\n'.format(string_to_hex_array(testcase['msg']))
valid = testcase['result'] == 'valid' or (testcase['result'] == 'acceptable' and 'NoLeadingZero' in testcase['flags'])
result += '{}}},\n'.format(str(valid).lower())

return result

class ECDSA():
"""Class that provides the generator function for a single ECDSA test case."""
def format_testcase(self, testcase, key, hash_oid, keySize):

def format_testcase(self, testcase, key, hash_oid, keySize, out_defs):
result = '\n// Comment: {}'.format(testcase['comment'])
result += '\n// tcID: {}\n'.format(testcase['tcId'])
result += '{{{}, {},\n'.format(hash_oid, testcase['tcId'])
Expand Down Expand Up @@ -327,7 +348,7 @@ def generate_vectors_file(params):
if 'key' in group:
if 'curve' in group['key'] and group['key']['curve'] not in ['secp256r1', 'secp384r1', 'secp521r1']:
continue
vectors_file += params['formatter'].format_testcase(test, group['keyDer'], getSha(group['sha']), group['key']['keySize'])
vectors_file += params['formatter'].format_testcase(test, group['keyDer'], getSha(group['sha']), group['key']['keySize'], shared_defs)
elif 'type' in group and group['type'] == 'RsassaPssVerify':
sLen = group['sLen'] if 'sLen' in group else 0
vectors_file += params['formatter'].format_testcase(test, group['keyDer'], getSha(group['sha']), getMgfSha(group['mgfSha']), sLen, shared_defs)
Expand Down Expand Up @@ -448,6 +469,17 @@ def generate_vectors_file(params):
// tests at this moment because validity of the public key is not checked.\n'
}

dsa_params = {
'source_dir': 'source_vectors/',
'source_file': 'dsa_test.json',
'target': '../testvectors/dsa-vectors.h',
'array_init': 'const DsaTestVector kDsaWycheproofVectors[] = {\n',
'formatter' : DSA(),
'crop_size_end': -2,
'section': 'dsa_vectors_h__',
'comment' : ''
}

p256ecdh_params = {
'source_dir': 'source_vectors/',
'source_file': 'ecdh_secp256r1_test.json',
Expand Down Expand Up @@ -830,6 +862,7 @@ def generate_test_vectors():
aes_gcm_params,
chacha_poly_params,
curve25519_params,
dsa_params,
p256ecdsa_sha256_params,
p384ecdsa_sha384_params,
p521ecdsa_sha512_params,
Expand Down

0 comments on commit f8e7a39

Please sign in to comment.