From 40a355a42d4a9444dc753c04c6608dade2f06a23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:27 +0200 Subject: Adding upstream version 125.0.1. Signed-off-by: Daniel Baumann --- security/nss/gtests/pk11_gtest/manifest.mn | 2 + .../nss/gtests/pk11_gtest/pk11_eddsa_unittest.cc | 177 +++++++++++++++++++++ .../nss/gtests/pk11_gtest/pk11_eddsa_vectors.h | 164 +++++++++++++++++++ security/nss/gtests/pk11_gtest/pk11_gtest.gyp | 2 + .../nss/gtests/pk11_gtest/pk11_import_unittest.cc | 1 + security/nss/gtests/pk11_gtest/pk11_keygen.cc | 7 + .../nss/gtests/pk11_gtest/pk11_pbe_unittest.cc | 69 ++++++++ .../nss/gtests/pk11_gtest/pk11_signature_test.cc | 25 +-- .../nss/gtests/pk11_gtest/pk11_signature_test.h | 24 ++- 9 files changed, 457 insertions(+), 14 deletions(-) create mode 100644 security/nss/gtests/pk11_gtest/pk11_eddsa_unittest.cc create mode 100644 security/nss/gtests/pk11_gtest/pk11_eddsa_vectors.h create mode 100644 security/nss/gtests/pk11_gtest/pk11_pbe_unittest.cc (limited to 'security/nss/gtests/pk11_gtest') diff --git a/security/nss/gtests/pk11_gtest/manifest.mn b/security/nss/gtests/pk11_gtest/manifest.mn index 7bfcb82f58..f9efc25e70 100644 --- a/security/nss/gtests/pk11_gtest/manifest.mn +++ b/security/nss/gtests/pk11_gtest/manifest.mn @@ -19,6 +19,7 @@ CPPSRCS = \ pk11_des_unittest.cc \ pk11_dsa_unittest.cc \ pk11_ecdsa_unittest.cc \ + pk11_eddsa_unittest.cc \ pk11_ecdh_unittest.cc \ pk11_encrypt_derive_unittest.cc \ pk11_export_unittest.cc \ @@ -33,6 +34,7 @@ CPPSRCS = \ pk11_keygen.cc \ pk11_key_unittest.cc \ pk11_module_unittest.cc \ + pk11_pbe_unittest.cc \ pk11_pbkdf2_unittest.cc \ pk11_prf_unittest.cc \ pk11_prng_unittest.cc \ diff --git a/security/nss/gtests/pk11_gtest/pk11_eddsa_unittest.cc b/security/nss/gtests/pk11_gtest/pk11_eddsa_unittest.cc new file mode 100644 index 0000000000..669ac75243 --- /dev/null +++ b/security/nss/gtests/pk11_gtest/pk11_eddsa_unittest.cc @@ -0,0 +1,177 @@ +/* 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 +#include "nss.h" +#include "pk11pub.h" +#include "sechash.h" +#include "cryptohi.h" + +#include "cpputil.h" +#include "json_reader.h" +#include "nss_scoped_ptrs.h" +#include "testvectors_base/test-structs.h" + +#include "pk11_eddsa_vectors.h" +#include "pk11_signature_test.h" +#include "pk11_keygen.h" + +namespace nss_test { +static const Pkcs11SignatureTestParams kEddsaVectors[] = { + {DataBuffer(kEd25519Pkcs8_1, sizeof(kEd25519Pkcs8_1)), + DataBuffer(kEd25519Spki_1, sizeof(kEd25519Spki_1)), + DataBuffer(kEd25519Message_1, sizeof(kEd25519Message_1)), + DataBuffer(kEd25519Signature_1, sizeof(kEd25519Signature_1))}, + + {DataBuffer(kEd25519Pkcs8_2, sizeof(kEd25519Pkcs8_2)), + DataBuffer(kEd25519Spki_2, sizeof(kEd25519Spki_2)), + DataBuffer(kEd25519Message_2, sizeof(kEd25519Message_2)), + DataBuffer(kEd25519Signature_2, sizeof(kEd25519Signature_2))}, + + {DataBuffer(kEd25519Pkcs8_3, sizeof(kEd25519Pkcs8_3)), + DataBuffer(kEd25519Spki_3, sizeof(kEd25519Spki_3)), + DataBuffer(kEd25519Message_3, sizeof(kEd25519Message_3)), + DataBuffer(kEd25519Signature_3, sizeof(kEd25519Signature_3))}}; + +class Pkcs11EddsaTest + : public Pk11SignatureTest, + public ::testing::WithParamInterface { + protected: + Pkcs11EddsaTest() : Pk11SignatureTest(CKM_EDDSA) {} +}; + +TEST_P(Pkcs11EddsaTest, SignAndVerify) { SignAndVerifyRaw(GetParam()); } + +TEST_P(Pkcs11EddsaTest, ImportExport) { ImportExport(GetParam().pkcs8_); } + +TEST_P(Pkcs11EddsaTest, ImportConvertToPublic) { + ScopedSECKEYPrivateKey privKey(ImportPrivateKey(GetParam().pkcs8_)); + ASSERT_TRUE(privKey); + + ScopedSECKEYPublicKey pubKey(SECKEY_ConvertToPublicKey(privKey.get())); + ASSERT_TRUE(pubKey); +} + +TEST_P(Pkcs11EddsaTest, ImportPublicCreateSubjectPKInfo) { + ScopedSECKEYPrivateKey privKey(ImportPrivateKey(GetParam().pkcs8_)); + ASSERT_TRUE(privKey); + + ScopedSECKEYPublicKey pubKey( + (SECKEYPublicKey*)SECKEY_ConvertToPublicKey(privKey.get())); + ASSERT_TRUE(pubKey); + + ScopedSECItem der_spki(SECKEY_EncodeDERSubjectPublicKeyInfo(pubKey.get())); + ASSERT_TRUE(der_spki); + ASSERT_EQ(der_spki->len, GetParam().spki_.len()); + ASSERT_EQ(0, memcmp(der_spki->data, GetParam().spki_.data(), der_spki->len)); +} + +INSTANTIATE_TEST_SUITE_P(EddsaSignVerify, Pkcs11EddsaTest, + ::testing::ValuesIn(kEddsaVectors)); + +class Pkcs11EddsaRoundtripTest + : public Pk11SignatureTest, + public ::testing::WithParamInterface { + protected: + Pkcs11EddsaRoundtripTest() : Pk11SignatureTest(CKM_EDDSA) {} + + protected: + void GenerateExportImportSignVerify(Pkcs11SignatureTestParams params) { + Pkcs11KeyPairGenerator generator(CKM_EC_EDWARDS_KEY_PAIR_GEN); + ScopedSECKEYPrivateKey priv; + ScopedSECKEYPublicKey pub; + generator.GenerateKey(&priv, &pub, false); + + DataBuffer exported; + ExportPrivateKey(&priv, exported); + + ScopedSECKEYPrivateKey privKey(ImportPrivateKey(exported)); + ASSERT_NE(privKey, nullptr); + DataBuffer sig; + + SignRaw(privKey, params.data_, &sig); + Verify(pub, params.data_, sig); + } +}; + +TEST_P(Pkcs11EddsaRoundtripTest, GenerateExportImportSignVerify) { + GenerateExportImportSignVerify(GetParam()); +} + +INSTANTIATE_TEST_SUITE_P(EddsaRound, Pkcs11EddsaRoundtripTest, + ::testing::ValuesIn(kEddsaVectors)); + +class Pkcs11EddsaWycheproofTest : public ::testing::Test { + protected: + void Run(const std::string& name) { + WycheproofHeader(name, "EDDSA", "eddsa_verify_schema.json", + [this](JsonReader& r) { RunGroup(r); }); + } + + private: + void RunGroup(JsonReader& r) { + std::vector tests; + std::vector public_key; + + while (r.NextItem()) { + std::string n = r.ReadLabel(); + if (n == "") { + break; + } + + if (n == "jwk" || n == "key" || n == "keyPem") { + r.SkipValue(); + } else if (n == "keyDer") { + public_key = r.ReadHex(); + } else if (n == "type") { + ASSERT_EQ("EddsaVerify", r.ReadString()); + } else if (n == "tests") { + WycheproofReadTests(r, &tests, ReadTestAttr); + } else { + FAIL() << "unknown label in group: " << n; + } + } + + for (auto& t : tests) { + std::cout << "Running test " << t.id << std::endl; + t.public_key = public_key; + Derive(t); + } + } + + static void ReadTestAttr(EddsaTestVector& t, const std::string& n, + JsonReader& r) { + if (n == "msg") { + t.msg = r.ReadHex(); + } else if (n == "sig") { + t.sig = r.ReadHex(); + } else { + FAIL() << "unknown test key: " << n; + } + } + + void Derive(const EddsaTestVector& vec) { + SECItem spki_item = {siBuffer, toUcharPtr(vec.public_key.data()), + static_cast(vec.public_key.size())}; + SECItem sig_item = {siBuffer, toUcharPtr(vec.sig.data()), + static_cast(vec.sig.size())}; + SECItem msg_item = {siBuffer, toUcharPtr(vec.msg.data()), + static_cast(vec.msg.size())}; + + ScopedCERTSubjectPublicKeyInfo cert_spki( + SECKEY_DecodeDERSubjectPublicKeyInfo(&spki_item)); + ASSERT_TRUE(cert_spki); + + ScopedSECKEYPublicKey pub_key(SECKEY_ExtractPublicKey(cert_spki.get())); + ASSERT_TRUE(pub_key); + + SECStatus rv = PK11_VerifyWithMechanism(pub_key.get(), CKM_EDDSA, nullptr, + &sig_item, &msg_item, nullptr); + EXPECT_EQ(rv, vec.valid ? SECSuccess : SECFailure); + }; +}; + +TEST_F(Pkcs11EddsaWycheproofTest, Ed25519) { Run("eddsa"); } + +} // namespace nss_test diff --git a/security/nss/gtests/pk11_gtest/pk11_eddsa_vectors.h b/security/nss/gtests/pk11_gtest/pk11_eddsa_vectors.h new file mode 100644 index 0000000000..896906ad50 --- /dev/null +++ b/security/nss/gtests/pk11_gtest/pk11_eddsa_vectors.h @@ -0,0 +1,164 @@ +/* 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/. */ + +namespace nss_test { +/* The test vectors are coming from + * https://tools.ietf.org/html/rfc8032#section-7. + * The first TV is skipped, as NSS does not support signing empty messages. + */ + +const uint8_t kEd25519Pkcs8_1[] = { + 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, + 0x04, 0x22, 0x04, 0x20, 0x4c, 0xcd, 0x08, 0x9b, 0x28, 0xff, 0x96, 0xda, + 0x9d, 0xb6, 0xc3, 0x46, 0xec, 0x11, 0x4e, 0x0f, 0x5b, 0x8a, 0x31, 0x9f, + 0x35, 0xab, 0xa6, 0x24, 0xda, 0x8c, 0xf6, 0xed, 0x4f, 0xb8, 0xa6, 0xfb, +}; + +const uint8_t kEd25519Spki_1[] = { + 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x70, 0x03, 0x21, + 0x00, 0x3d, 0x40, 0x17, 0xc3, 0xe8, 0x43, 0x89, 0x5a, 0x92, 0xb7, + 0x0a, 0xa7, 0x4d, 0x1b, 0x7e, 0xbc, 0x9c, 0x98, 0x2c, 0xcf, 0x2e, + 0xc4, 0x96, 0x8c, 0xc0, 0xcd, 0x55, 0xf1, 0x2a, 0xf4, 0x66, 0x0c}; + +const uint8_t kEd25519Message_1[] = {0x72}; + +const uint8_t kEd25519Signature_1[64] = { + 0x92, 0xa0, 0x09, 0xa9, 0xf0, 0xd4, 0xca, 0xb8, 0x72, 0x0e, 0x82, + 0x0b, 0x5f, 0x64, 0x25, 0x40, 0xa2, 0xb2, 0x7b, 0x54, 0x16, 0x50, + 0x3f, 0x8f, 0xb3, 0x76, 0x22, 0x23, 0xeb, 0xdb, 0x69, 0xda, 0x08, + 0x5a, 0xc1, 0xe4, 0x3e, 0x15, 0x99, 0x6e, 0x45, 0x8f, 0x36, 0x13, + 0xd0, 0xf1, 0x1d, 0x8c, 0x38, 0x7b, 0x2e, 0xae, 0xb4, 0x30, 0x2a, + 0xee, 0xb0, 0x0d, 0x29, 0x16, 0x12, 0xbb, 0x0c, 0x00}; + +const uint8_t kEd25519Pkcs8_2[] = { + 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, + 0x04, 0x22, 0x04, 0x20, 0xc5, 0xaa, 0x8d, 0xf4, 0x3f, 0x9f, 0x83, 0x7b, + 0xed, 0xb7, 0x44, 0x2f, 0x31, 0xdc, 0xb7, 0xb1, 0x66, 0xd3, 0x85, 0x35, + 0x07, 0x6f, 0x09, 0x4b, 0x85, 0xce, 0x3a, 0x2e, 0x0b, 0x44, 0x58, 0xf7}; + +const uint8_t kEd25519Spki_2[] = { + 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x70, 0x03, 0x21, + 0x00, 0xfc, 0x51, 0xcd, 0x8e, 0x62, 0x18, 0xa1, 0xa3, 0x8d, 0xa4, + 0x7e, 0xd0, 0x02, 0x30, 0xf0, 0x58, 0x08, 0x16, 0xed, 0x13, 0xba, + 0x33, 0x03, 0xac, 0x5d, 0xeb, 0x91, 0x15, 0x48, 0x90, 0x80, 0x25}; + +const uint8_t kEd25519Message_2[] = {0xaf, 0x82}; + +const uint8_t kEd25519Signature_2[64] = { + 0x62, 0x91, 0xd6, 0x57, 0xde, 0xec, 0x24, 0x02, 0x48, 0x27, 0xe6, + 0x9c, 0x3a, 0xbe, 0x01, 0xa3, 0x0c, 0xe5, 0x48, 0xa2, 0x84, 0x74, + 0x3a, 0x44, 0x5e, 0x36, 0x80, 0xd7, 0xdb, 0x5a, 0xc3, 0xac, 0x18, + 0xff, 0x9b, 0x53, 0x8d, 0x16, 0xf2, 0x90, 0xae, 0x67, 0xf7, 0x60, + 0x98, 0x4d, 0xc6, 0x59, 0x4a, 0x7c, 0x15, 0xe9, 0x71, 0x6e, 0xd2, + 0x8d, 0xc0, 0x27, 0xbe, 0xce, 0xea, 0x1e, 0xc4, 0x0a}; + +const uint8_t kEd25519Pkcs8_3[] = { + 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, + 0x04, 0x22, 0x04, 0x20, 0xf5, 0xe5, 0x76, 0x7c, 0xf1, 0x53, 0x31, 0x95, + 0x17, 0x63, 0x0f, 0x22, 0x68, 0x76, 0xb8, 0x6c, 0x81, 0x60, 0xcc, 0x58, + 0x3b, 0xc0, 0x13, 0x74, 0x4c, 0x6b, 0xf2, 0x55, 0xf5, 0xcc, 0x0e, 0xe5}; + +const uint8_t kEd25519Spki_3[] = { + 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x70, 0x03, 0x21, + 0x00, 0x27, 0x81, 0x17, 0xfc, 0x14, 0x4c, 0x72, 0x34, 0x0f, 0x67, + 0xd0, 0xf2, 0x31, 0x6e, 0x83, 0x86, 0xce, 0xff, 0xbf, 0x2b, 0x24, + 0x28, 0xc9, 0xc5, 0x1f, 0xef, 0x7c, 0x59, 0x7f, 0x1d, 0x42, 0x6e}; + +const uint8_t kEd25519Message_3[] = { + 0x08, 0xb8, 0xb2, 0xb7, 0x33, 0x42, 0x42, 0x43, 0x76, 0x0f, 0xe4, 0x26, + 0xa4, 0xb5, 0x49, 0x08, 0x63, 0x21, 0x10, 0xa6, 0x6c, 0x2f, 0x65, 0x91, + 0xea, 0xbd, 0x33, 0x45, 0xe3, 0xe4, 0xeb, 0x98, 0xfa, 0x6e, 0x26, 0x4b, + 0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0xd9, 0xd0, 0x34, 0x12, 0xc0, 0x91, 0xc6, + 0x31, 0x5c, 0xf4, 0xfd, 0xe7, 0xcb, 0x68, 0x60, 0x69, 0x37, 0x38, 0x0d, + 0xb2, 0xea, 0xaa, 0x70, 0x7b, 0x4c, 0x41, 0x85, 0xc3, 0x2e, 0xdd, 0xcd, + 0xd3, 0x06, 0x70, 0x5e, 0x4d, 0xc1, 0xff, 0xc8, 0x72, 0xee, 0xee, 0x47, + 0x5a, 0x64, 0xdf, 0xac, 0x86, 0xab, 0xa4, 0x1c, 0x06, 0x18, 0x98, 0x3f, + 0x87, 0x41, 0xc5, 0xef, 0x68, 0xd3, 0xa1, 0x01, 0xe8, 0xa3, 0xb8, 0xca, + 0xc6, 0x0c, 0x90, 0x5c, 0x15, 0xfc, 0x91, 0x08, 0x40, 0xb9, 0x4c, 0x00, + 0xa0, 0xb9, 0xd0}; + +const uint8_t kEd25519Signature_3[64] = { + 0x0a, 0xab, 0x4c, 0x90, 0x05, 0x01, 0xb3, 0xe2, 0x4d, 0x7c, 0xdf, + 0x46, 0x63, 0x32, 0x6a, 0x3a, 0x87, 0xdf, 0x5e, 0x48, 0x43, 0xb2, + 0xcb, 0xdb, 0x67, 0xcb, 0xf6, 0xe4, 0x60, 0xfe, 0xc3, 0x50, 0xaa, + 0x53, 0x71, 0xb1, 0x50, 0x8f, 0x9f, 0x45, 0x28, 0xec, 0xea, 0x23, + 0xc4, 0x36, 0xd9, 0x4b, 0x5e, 0x8f, 0xcd, 0x4f, 0x68, 0x1e, 0x30, + 0xa6, 0xac, 0x00, 0xa9, 0x70, 0x4a, 0x18, 0x8a, 0x03}; + +} // namespace nss_test diff --git a/security/nss/gtests/pk11_gtest/pk11_gtest.gyp b/security/nss/gtests/pk11_gtest/pk11_gtest.gyp index 792d6546e4..c14dbf860e 100644 --- a/security/nss/gtests/pk11_gtest/pk11_gtest.gyp +++ b/security/nss/gtests/pk11_gtest/pk11_gtest.gyp @@ -25,6 +25,7 @@ 'pk11_des_unittest.cc', 'pk11_dsa_unittest.cc', 'pk11_ecdsa_unittest.cc', + 'pk11_eddsa_unittest.cc', 'pk11_ecdh_unittest.cc', 'pk11_encrypt_derive_unittest.cc', 'pk11_find_certs_unittest.cc', @@ -38,6 +39,7 @@ 'pk11_keygen.cc', 'pk11_key_unittest.cc', 'pk11_module_unittest.cc', + 'pk11_pbe_unittest.cc', 'pk11_pbkdf2_unittest.cc', 'pk11_prf_unittest.cc', 'pk11_prng_unittest.cc', diff --git a/security/nss/gtests/pk11_gtest/pk11_import_unittest.cc b/security/nss/gtests/pk11_gtest/pk11_import_unittest.cc index 7fcc1cc4d7..6e11477045 100644 --- a/security/nss/gtests/pk11_gtest/pk11_import_unittest.cc +++ b/security/nss/gtests/pk11_gtest/pk11_import_unittest.cc @@ -88,6 +88,7 @@ class Pk11KeyImportTestBase : public ::testing::Test { case dhKey: return pub_key->u.dh.publicValue; case ecKey: + case edKey: return pub_key->u.ec.publicValue; case kyberKey: return pub_key->u.kyber.publicValue; diff --git a/security/nss/gtests/pk11_gtest/pk11_keygen.cc b/security/nss/gtests/pk11_gtest/pk11_keygen.cc index 1a300ca4c1..92c8ba7ed8 100644 --- a/security/nss/gtests/pk11_gtest/pk11_keygen.cc +++ b/security/nss/gtests/pk11_gtest/pk11_keygen.cc @@ -82,9 +82,11 @@ class DhParamHolder : public PqgParamHolder { SECKEYDHParams params_; }; +/* Also used for EdDSA. */ class EcParamHolder : public ParamHolder { public: EcParamHolder(SECOidTag curve_oid) { + /* For the case of ED curve_oid contains a EdDSA OID. */ SECOidData* curve = SECOID_FindOIDByTag(curve_oid); EXPECT_NE(nullptr, curve); @@ -142,6 +144,11 @@ std::unique_ptr Pkcs11KeyPairGenerator::MakeParams() const { return std::unique_ptr(new DhParamHolder(pqg_params)); } + case CKM_EC_EDWARDS_KEY_PAIR_GEN: + std::cerr << "Generate ED pair on " << curve_ << std::endl; + return std::unique_ptr( + new EcParamHolder(SEC_OID_ED25519_PUBLIC_KEY)); + case CKM_EC_KEY_PAIR_GEN: std::cerr << "Generate EC pair on " << curve_ << std::endl; return std::unique_ptr(new EcParamHolder(curve_)); diff --git a/security/nss/gtests/pk11_gtest/pk11_pbe_unittest.cc b/security/nss/gtests/pk11_gtest/pk11_pbe_unittest.cc new file mode 100644 index 0000000000..5e90fd1bc9 --- /dev/null +++ b/security/nss/gtests/pk11_gtest/pk11_pbe_unittest.cc @@ -0,0 +1,69 @@ +/* -*- 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 +#include "nss.h" +#include "pk11pub.h" + +#include "gtest/gtest.h" +#include "nss_scoped_ptrs.h" + +namespace nss_test { + +static unsigned char* ToUcharPtr(std::string& str) { + return const_cast( + reinterpret_cast(str.c_str())); +} + +class Pkcs11PbeTest : public ::testing::Test { + public: + void Derive(std::vector& derived) { + // Shared between test vectors. + const unsigned int kIterations = 4096; + std::string pass("passwordPASSWORDpassword"); + std::string salt("saltSALTsaltSALTsaltSALTsaltSALTsalt"); + + // Derivation must succeed with the right values. + EXPECT_TRUE(DeriveBytes(pass, salt, derived, kIterations)); + } + + private: + bool DeriveBytes(std::string& pass, std::string& salt, + std::vector& derived, unsigned int kIterations) { + SECItem pass_item = {siBuffer, ToUcharPtr(pass), + static_cast(pass.length())}; + SECItem salt_item = {siBuffer, ToUcharPtr(salt), + static_cast(salt.length())}; + + // Set up PBE params. + ScopedSECAlgorithmID alg_id(PK11_CreatePBEAlgorithmID( + SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_2KEY_TRIPLE_DES_CBC, kIterations, + &salt_item)); + + // Derive. + ScopedPK11SlotInfo slot(PK11_GetInternalSlot()); + ScopedPK11SymKey sym_key( + PK11_PBEKeyGen(slot.get(), alg_id.get(), &pass_item, false, nullptr)); + + SECStatus rv = PK11_ExtractKeyValue(sym_key.get()); + EXPECT_EQ(rv, SECSuccess); + + SECItem* key_data = PK11_GetKeyData(sym_key.get()); + + return key_data->len == derived.size() && + !memcmp(&derived[0], key_data->data, key_data->len); + } +}; + +TEST_F(Pkcs11PbeTest, DeriveKnown) { + std::vector derived = {0x86, 0x6b, 0xce, 0xef, 0x26, 0xa4, + 0x4f, 0x02, 0x4a, 0x26, 0xcd, 0xd0, + 0x4f, 0x7c, 0x19, 0xad}; + + Derive(derived); +} + +} // namespace nss_test diff --git a/security/nss/gtests/pk11_gtest/pk11_signature_test.cc b/security/nss/gtests/pk11_gtest/pk11_signature_test.cc index c9700707fe..bb029cd3a4 100644 --- a/security/nss/gtests/pk11_gtest/pk11_signature_test.cc +++ b/security/nss/gtests/pk11_gtest/pk11_signature_test.cc @@ -54,9 +54,8 @@ ScopedSECKEYPublicKey Pk11SignatureTest::ImportPublicKey( return ScopedSECKEYPublicKey(SECKEY_ExtractPublicKey(certSpki.get())); } -bool Pk11SignatureTest::SignHashedData(ScopedSECKEYPrivateKey& privKey, - const DataBuffer& hash, - DataBuffer* sig) { +bool Pk11SignatureTest::SignRaw(ScopedSECKEYPrivateKey& privKey, + const DataBuffer& hash, DataBuffer* sig) { SECItem hashItem = {siBuffer, toUcharPtr(hash.data()), static_cast(hash.len())}; unsigned int sigLen = PK11_SignatureLen(privKey.get()); @@ -70,8 +69,8 @@ bool Pk11SignatureTest::SignHashedData(ScopedSECKEYPrivateKey& privKey, return rv == SECSuccess; } -bool Pk11SignatureTest::SignData(ScopedSECKEYPrivateKey& privKey, - const DataBuffer& data, DataBuffer* sig) { +bool Pk11SignatureTest::DigestAndSign(ScopedSECKEYPrivateKey& privKey, + const DataBuffer& data, DataBuffer* sig) { unsigned int sigLen = PK11_SignatureLen(privKey.get()); bool result = true; EXPECT_LT(0, (int)sigLen); @@ -123,11 +122,11 @@ bool Pk11SignatureTest::ImportPrivateKeyAndSignHashedData( ADD_FAILURE() << "Failed to compute hash"; return false; } - if (!SignHashedData(privKey, hash, sig)) { + if (!SignRaw(privKey, hash, sig)) { ADD_FAILURE() << "Failed to sign hashed data"; return false; } - if (!SignData(privKey, data, sig2)) { + if (!DigestAndSign(privKey, data, sig2)) { /* failure was already added by SignData, with an error message */ return false; } @@ -138,11 +137,20 @@ void Pk11SignatureTest::Verify(ScopedSECKEYPublicKey& pubKey, const DataBuffer& data, const DataBuffer& sig, bool valid) { SECStatus rv; - DataBuffer hash; SECItem sigItem = {siBuffer, toUcharPtr(sig.data()), static_cast(sig.len())}; + if (skip_digest_) { + SECItem dataItem = {siBuffer, toUcharPtr(data.data()), + static_cast(data.len())}; + rv = PK11_VerifyWithMechanism(pubKey.get(), mechanism_, parameters(), + &sigItem, &dataItem, nullptr); + EXPECT_EQ(rv, valid ? SECSuccess : SECFailure); + return; + } + + DataBuffer hash; /* RSA single shot requires encoding the hash before calling * VerifyWithMechanism. We already check that mechanism * with the VFY_ interface, so just do the combined hash/Verify @@ -175,5 +183,4 @@ void Pk11SignatureTest::Verify(ScopedSECKEYPublicKey& pubKey, << "verify failed Error:" << PORT_ErrorToString(PORT_GetError()) << "\n"; PK11_DestroyContext(context, PR_TRUE); } - } // namespace nss_test diff --git a/security/nss/gtests/pk11_gtest/pk11_signature_test.h b/security/nss/gtests/pk11_gtest/pk11_signature_test.h index c4a8c52c38..f00a588fff 100644 --- a/security/nss/gtests/pk11_gtest/pk11_signature_test.h +++ b/security/nss/gtests/pk11_gtest/pk11_signature_test.h @@ -11,7 +11,6 @@ #include "databuffer.h" #include "gtest/gtest.h" - namespace nss_test { // For test vectors. @@ -28,6 +27,11 @@ class Pk11SignatureTest : public ::testing::Test { CK_MECHANISM_TYPE combo) : mechanism_(mech), hash_oid_(hash_oid), combo_(combo) { skip_raw_ = false; + skip_digest_ = false; + } + + Pk11SignatureTest(CK_MECHANISM_TYPE mech) : mechanism_(mech) { + skip_digest_ = true; } virtual const SECItem* parameters() const { return nullptr; } @@ -54,10 +58,10 @@ class Pk11SignatureTest : public ::testing::Test { return rv == SECSuccess; } - bool SignHashedData(ScopedSECKEYPrivateKey& privKey, const DataBuffer& hash, - DataBuffer* sig); - bool SignData(ScopedSECKEYPrivateKey& privKey, const DataBuffer& data, - DataBuffer* sig); + bool SignRaw(ScopedSECKEYPrivateKey& privKey, const DataBuffer& hash, + DataBuffer* sig); + bool DigestAndSign(ScopedSECKEYPrivateKey& privKey, const DataBuffer& data, + DataBuffer* sig); bool ImportPrivateKeyAndSignHashedData(const DataBuffer& pkcs8, const DataBuffer& data, DataBuffer* sig, DataBuffer* sig2); @@ -96,6 +100,15 @@ class Pk11SignatureTest : public ::testing::Test { Verify(params, sig2, true); } + void SignAndVerifyRaw(const Pkcs11SignatureTestParams& params) { + ScopedSECKEYPrivateKey privKey(ImportPrivateKey(params.pkcs8_)); + ASSERT_NE(privKey, nullptr); + DataBuffer sig; + SignRaw(privKey, params.data_, &sig); + EXPECT_EQ(sig, params.signature_); + Verify(params, sig, true); + } + // Importing a private key in PKCS#8 format and reexporting it should // result in the same binary representation. void ImportExport(const DataBuffer& k) { @@ -110,6 +123,7 @@ class Pk11SignatureTest : public ::testing::Test { SECOidTag hash_oid_; CK_MECHANISM_TYPE combo_; bool skip_raw_; + bool skip_digest_; }; } // namespace nss_test -- cgit v1.2.3