Commit 4c000c27 authored by Kevin Jacobs's avatar Kevin Jacobs

Bug 1612259 - Add Wycheproof vectors for P384 and P521 ECDH. r=bbeurdouche

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

--HG--
extra : moz-landing-system : lando
parent b7965436
This diff is collapsed.
This diff is collapsed.
......@@ -126,8 +126,8 @@ class Curve25519():
return result
class P256_ECDH():
"""Class that provides the generator function for a single P256_ECDH test case."""
class ECDH():
"""Class that provides the generator function for a single ECDH test case."""
def format_testcase(self, testcase, curve):
......@@ -135,7 +135,7 @@ class P256_ECDH():
result += '\n// tcID: {}'.format(testcase['tcId'])
private_key = ec.derive_private_key(
int(testcase["private"], 16),
ec.SECP256R1(),
curve,
default_backend()
).private_bytes(
encoding=serialization.Encoding.DER,
......@@ -197,16 +197,20 @@ def generate_vectors_file(params):
for group in cases['testGroups']:
for test in group['tests']:
if 'key' in group:
if 'curve' in group['key'] and group['key']['curve'] not in ['secp256r1']:
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'])
elif 'curve' in group:
if group['curve'] not in ['secp256r1', 'curve25519']:
continue
if(group['curve'] == 'secp256r1'):
if group['curve'] == 'secp256r1':
curve = ec.SECP256R1()
else:
elif group['curve'] == 'secp384r1':
curve = ec.SECP384R1()
elif group['curve'] == 'secp521r1':
curve = ec.SECP521R1()
elif group['curve'] == 'curve25519':
curve = "curve25519"
else:
continue
vectors_file += params['formatter'].format_testcase(test, curve)
else:
vectors_file += params['formatter'].format_testcase(test)
......@@ -309,12 +313,34 @@ p256ecdh_params = {
'source_file': 'ecdh_secp256r1_test.json',
'target': '../testvectors/p256ecdh-vectors.h',
'array_init': 'const EcdhTestVectorStr kP256EcdhWycheproofVectors[] = {\n',
'formatter' : P256_ECDH(),
'formatter' : ECDH(),
'crop_size_end': -2,
'section': 'p256ecdh_vectors_h__',
'comment' : ''
}
p384ecdh_params = {
'source_dir': 'source_vectors/',
'source_file': 'ecdh_secp384r1_test.json',
'target': '../testvectors/p384ecdh-vectors.h',
'array_init': 'const EcdhTestVectorStr kP384EcdhWycheproofVectors[] = {\n',
'formatter' : ECDH(),
'crop_size_end': -2,
'section': 'p384ecdh_vectors_h__',
'comment' : ''
}
p521ecdh_params = {
'source_dir': 'source_vectors/',
'source_file': 'ecdh_secp521r1_test.json',
'target': '../testvectors/p521ecdh-vectors.h',
'array_init': 'const EcdhTestVectorStr kP521EcdhWycheproofVectors[] = {\n',
'formatter' : ECDH(),
'crop_size_end': -2,
'section': 'p521ecdh_vectors_h__',
'comment' : ''
}
def update_tests(tests):
remote = "https://raw.githubusercontent.com/google/wycheproof/master/testvectors/"
......@@ -329,7 +355,9 @@ def generate_test_vectors():
aes_gcm_params,
chacha_poly_params,
curve25519_params,
p256ecdh_params]
p256ecdh_params,
p384ecdh_params,
p521ecdh_params]
update_tests(all_tests)
for test in all_tests:
generate_vectors_file(test)
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -10,11 +10,13 @@
#include "nss_scoped_ptrs.h"
#include "testvectors/p256ecdh-vectors.h"
#include "testvectors/p384ecdh-vectors.h"
#include "testvectors/p521ecdh-vectors.h"
#include "gtest/gtest.h"
namespace nss_test {
class Pkcs11P256EcdhTest : public ::testing::TestWithParam<EcdhTestVector> {
class Pkcs11EcdhTest : public ::testing::TestWithParam<EcdhTestVector> {
protected:
void Derive(const EcdhTestVector vec) {
std::string err = "Test #" + std::to_string(vec.id) + " failed";
......@@ -72,9 +74,13 @@ class Pkcs11P256EcdhTest : public ::testing::TestWithParam<EcdhTestVector> {
};
};
TEST_P(Pkcs11P256EcdhTest, TestVectors) { Derive(GetParam()); }
TEST_P(Pkcs11EcdhTest, TestVectors) { Derive(GetParam()); }
INSTANTIATE_TEST_CASE_P(WycheproofP256EcdhTest, Pkcs11P256EcdhTest,
INSTANTIATE_TEST_CASE_P(WycheproofP256EcdhTest, Pkcs11EcdhTest,
::testing::ValuesIn(kP256EcdhWycheproofVectors));
INSTANTIATE_TEST_CASE_P(WycheproofP384EcdhTest, Pkcs11EcdhTest,
::testing::ValuesIn(kP384EcdhWycheproofVectors));
INSTANTIATE_TEST_CASE_P(WycheproofP521EcdhTest, Pkcs11EcdhTest,
::testing::ValuesIn(kP521EcdhWycheproofVectors));
} // namespace nss_test
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