Commit f8e7a39a authored by Kevin Jacobs's avatar Kevin Jacobs

Bug 1672823 - Add Wycheproof DSA test cases. r=jcj

Differential Revision: https://phabricator.services.mozilla.com/D94495

--HG--
extra : moz-landing-system : lando
parent d1f32b40
This diff is collapsed.
......@@ -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;
......
......@@ -158,10 +158,31 @@ class ECDH():
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'])
......@@ -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)
......@@ -448,6 +469,17 @@ curve25519_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',
......@@ -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,
......
This diff is collapsed.
......@@ -16,6 +16,7 @@ CPPSRCS = \
pk11_curve25519_unittest.cc \
pk11_der_private_key_import_unittest.cc \
pk11_des_unittest.cc \
pk11_dsa_unittest.cc \
pk11_ecdsa_unittest.cc \
pk11_ecdh_unittest.cc \
pk11_encrypt_derive_unittest.cc \
......
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <memory>
#include "nss.h"
#include "pk11pub.h"
#include "sechash.h"
#include "cryptohi.h"
#include "cpputil.h"
#include "databuffer.h"
#include "gtest/gtest.h"
#include "nss_scoped_ptrs.h"
#include "testvectors/dsa-vectors.h"
namespace nss_test {
class Pkcs11DsaTest : public ::testing::TestWithParam<DsaTestVector> {
protected:
void Derive(const uint8_t* sig, size_t sig_len, const uint8_t* spki,
size_t spki_len, const uint8_t* data, size_t data_len,
bool expect_success, const uint32_t test_id,
const SECOidTag hash_oid) {
std::stringstream s;
s << "Test with original ID #" << test_id << " failed.\n";
s << "Expected Success: " << expect_success << "\n";
std::string msg = s.str();
SECItem spki_item = {siBuffer, toUcharPtr(spki),
static_cast<unsigned int>(spki_len)};
ScopedCERTSubjectPublicKeyInfo cert_spki(
SECKEY_DecodeDERSubjectPublicKeyInfo(&spki_item));
ASSERT_TRUE(cert_spki) << msg;
ScopedSECKEYPublicKey pub_key(SECKEY_ExtractPublicKey(cert_spki.get()));
ASSERT_TRUE(pub_key) << msg;
SECItem sig_item = {siBuffer, toUcharPtr(sig),
static_cast<unsigned int>(sig_len)};
ScopedSECItem decoded_sig_item(
DSAU_DecodeDerSigToLen(&sig_item, SECKEY_SignatureLen(pub_key.get())));
if (!decoded_sig_item) {
ASSERT_FALSE(expect_success) << msg;
return;
}
DataBuffer hash;
hash.Allocate(static_cast<size_t>(HASH_ResultLenByOidTag(hash_oid)));
SECStatus rv = PK11_HashBuf(hash_oid, toUcharPtr(hash.data()),
toUcharPtr(data), data_len);
ASSERT_EQ(SECSuccess, rv) << msg;
// Verify.
SECItem hash_item = {siBuffer, toUcharPtr(hash.data()),
static_cast<unsigned int>(hash.len())};
rv = PK11_VerifyWithMechanism(pub_key.get(), CKM_DSA, nullptr,
decoded_sig_item.get(), &hash_item, nullptr);
EXPECT_EQ(expect_success ? SECSuccess : SECFailure, rv);
};
void Derive(const DsaTestVector vector) {
Derive(vector.sig.data(), vector.sig.size(), vector.public_key.data(),
vector.public_key.size(), vector.msg.data(), vector.msg.size(),
vector.valid, vector.id, vector.hash_oid);
};
};
TEST_P(Pkcs11DsaTest, WycheproofVectors) { Derive(GetParam()); }
INSTANTIATE_TEST_CASE_P(DsaTest, Pkcs11DsaTest,
::testing::ValuesIn(kDsaWycheproofVectors));
} // namespace nss_test
......@@ -22,6 +22,7 @@
'pk11_curve25519_unittest.cc',
'pk11_der_private_key_import_unittest.cc',
'pk11_des_unittest.cc',
'pk11_dsa_unittest.cc',
'pk11_ecdsa_unittest.cc',
'pk11_ecdh_unittest.cc',
'pk11_encrypt_derive_unittest.cc',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment