2018-01-19 03:59:58 +08:00
|
|
|
/* -*- 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"
|
|
|
|
|
|
2018-02-06 11:46:26 +01:00
|
|
|
#include "cpputil.h"
|
2018-12-15 01:42:53 +01:00
|
|
|
#include "nss_scoped_ptrs.h"
|
2018-01-19 03:59:58 +08:00
|
|
|
|
2019-02-22 21:39:59 +08:00
|
|
|
#include "testvectors/chachapoly-vectors.h"
|
2018-02-06 11:46:26 +01:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
2018-01-19 03:59:58 +08:00
|
|
|
namespace nss_test {
|
|
|
|
|
|
2019-02-22 21:39:59 +08:00
|
|
|
class Pkcs11ChaCha20Poly1305Test
|
|
|
|
|
: public ::testing::TestWithParam<chacha_testvector> {
|
2018-01-19 03:59:58 +08:00
|
|
|
public:
|
2019-02-22 21:39:59 +08:00
|
|
|
void EncryptDecrypt(PK11SymKey* symKey, const bool invalid_iv,
|
|
|
|
|
const bool invalid_tag, const uint8_t* data,
|
|
|
|
|
size_t data_len, const uint8_t* aad, size_t aad_len,
|
|
|
|
|
const uint8_t* iv, size_t iv_len,
|
|
|
|
|
const uint8_t* ct = nullptr, size_t ct_len = 0) {
|
2018-01-19 03:59:58 +08:00
|
|
|
// Prepare AEAD params.
|
|
|
|
|
CK_NSS_AEAD_PARAMS aead_params;
|
|
|
|
|
aead_params.pNonce = toUcharPtr(iv);
|
|
|
|
|
aead_params.ulNonceLen = iv_len;
|
|
|
|
|
aead_params.pAAD = toUcharPtr(aad);
|
|
|
|
|
aead_params.ulAADLen = aad_len;
|
|
|
|
|
aead_params.ulTagLen = 16;
|
|
|
|
|
|
|
|
|
|
SECItem params = {siBuffer, reinterpret_cast<unsigned char*>(&aead_params),
|
|
|
|
|
sizeof(aead_params)};
|
|
|
|
|
|
|
|
|
|
// Encrypt.
|
|
|
|
|
unsigned int outputLen = 0;
|
|
|
|
|
std::vector<uint8_t> output(data_len + aead_params.ulTagLen);
|
2019-02-22 21:39:59 +08:00
|
|
|
SECStatus rv = PK11_Encrypt(symKey, mech, ¶ms, output.data(),
|
|
|
|
|
&outputLen, output.size(), data, data_len);
|
|
|
|
|
|
|
|
|
|
// Return if encryption failure was expected due to invalid IV.
|
|
|
|
|
// Without valid ciphertext, all further tests can be skipped.
|
|
|
|
|
if (invalid_iv) {
|
|
|
|
|
EXPECT_EQ(rv, SECFailure);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
EXPECT_EQ(rv, SECSuccess);
|
|
|
|
|
}
|
2018-01-19 03:59:58 +08:00
|
|
|
|
|
|
|
|
// Check ciphertext and tag.
|
|
|
|
|
if (ct) {
|
2019-02-22 21:39:59 +08:00
|
|
|
ASSERT_EQ(ct_len, outputLen);
|
|
|
|
|
EXPECT_TRUE(!memcmp(ct, output.data(), outputLen) != invalid_tag);
|
2018-01-19 03:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decrypt.
|
|
|
|
|
unsigned int decryptedLen = 0;
|
|
|
|
|
std::vector<uint8_t> decrypted(data_len);
|
2019-02-22 21:39:59 +08:00
|
|
|
rv = PK11_Decrypt(symKey, mech, ¶ms, decrypted.data(), &decryptedLen,
|
|
|
|
|
decrypted.size(), output.data(), outputLen);
|
2018-01-19 03:59:58 +08:00
|
|
|
EXPECT_EQ(rv, SECSuccess);
|
|
|
|
|
|
|
|
|
|
// Check the plaintext.
|
2019-02-22 21:39:59 +08:00
|
|
|
ASSERT_EQ(data_len, decryptedLen);
|
|
|
|
|
EXPECT_TRUE(!memcmp(data, decrypted.data(), decryptedLen));
|
2018-01-19 03:59:58 +08:00
|
|
|
|
|
|
|
|
// Decrypt with bogus data.
|
2019-02-22 21:39:59 +08:00
|
|
|
// Skip if there's no data to modify.
|
|
|
|
|
if (outputLen != 0) {
|
2018-01-19 03:59:58 +08:00
|
|
|
std::vector<uint8_t> bogusCiphertext(output);
|
|
|
|
|
bogusCiphertext[0] ^= 0xff;
|
2019-02-22 21:39:59 +08:00
|
|
|
rv = PK11_Decrypt(symKey, mech, ¶ms, decrypted.data(), &decryptedLen,
|
|
|
|
|
decrypted.size(), bogusCiphertext.data(), outputLen);
|
2018-01-19 03:59:58 +08:00
|
|
|
EXPECT_NE(rv, SECSuccess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decrypt with bogus tag.
|
2019-02-22 21:39:59 +08:00
|
|
|
// Skip if there's no tag to modify.
|
|
|
|
|
if (outputLen != 0) {
|
2018-01-19 03:59:58 +08:00
|
|
|
std::vector<uint8_t> bogusTag(output);
|
|
|
|
|
bogusTag[outputLen - 1] ^= 0xff;
|
2019-02-22 21:39:59 +08:00
|
|
|
rv = PK11_Decrypt(symKey, mech, ¶ms, decrypted.data(), &decryptedLen,
|
|
|
|
|
decrypted.size(), bogusTag.data(), outputLen);
|
2018-01-19 03:59:58 +08:00
|
|
|
EXPECT_NE(rv, SECSuccess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decrypt with bogus IV.
|
2019-02-22 21:39:59 +08:00
|
|
|
// iv_len == 0 is invalid and should be caught earlier.
|
|
|
|
|
// Still skip, if there's no IV to modify.
|
|
|
|
|
if (iv_len != 0) {
|
2018-01-19 03:59:58 +08:00
|
|
|
SECItem bogusParams(params);
|
|
|
|
|
CK_NSS_AEAD_PARAMS bogusAeadParams(aead_params);
|
|
|
|
|
bogusParams.data = reinterpret_cast<unsigned char*>(&bogusAeadParams);
|
|
|
|
|
|
|
|
|
|
std::vector<uint8_t> bogusIV(iv, iv + iv_len);
|
2019-02-22 21:39:59 +08:00
|
|
|
bogusAeadParams.pNonce = toUcharPtr(bogusIV.data());
|
2018-01-19 03:59:58 +08:00
|
|
|
bogusIV[0] ^= 0xff;
|
|
|
|
|
|
2019-02-22 21:39:59 +08:00
|
|
|
rv = PK11_Decrypt(symKey, mech, &bogusParams, decrypted.data(),
|
|
|
|
|
&decryptedLen, data_len, output.data(), outputLen);
|
2018-01-19 03:59:58 +08:00
|
|
|
EXPECT_NE(rv, SECSuccess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decrypt with bogus additional data.
|
2019-02-22 21:39:59 +08:00
|
|
|
// Skip when AAD was empty and can't be modified.
|
|
|
|
|
// Alternatively we could generate random aad.
|
|
|
|
|
if (aad_len != 0) {
|
2018-01-19 03:59:58 +08:00
|
|
|
SECItem bogusParams(params);
|
|
|
|
|
CK_NSS_AEAD_PARAMS bogusAeadParams(aead_params);
|
|
|
|
|
bogusParams.data = reinterpret_cast<unsigned char*>(&bogusAeadParams);
|
|
|
|
|
|
|
|
|
|
std::vector<uint8_t> bogusAAD(aad, aad + aad_len);
|
2019-02-22 21:39:59 +08:00
|
|
|
bogusAeadParams.pAAD = toUcharPtr(bogusAAD.data());
|
2018-01-19 03:59:58 +08:00
|
|
|
bogusAAD[0] ^= 0xff;
|
|
|
|
|
|
2019-02-22 21:39:59 +08:00
|
|
|
rv = PK11_Decrypt(symKey, mech, &bogusParams, decrypted.data(),
|
|
|
|
|
&decryptedLen, data_len, output.data(), outputLen);
|
2018-01-19 03:59:58 +08:00
|
|
|
EXPECT_NE(rv, SECSuccess);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-22 21:39:59 +08:00
|
|
|
void EncryptDecrypt(const chacha_testvector testvector) {
|
2018-01-19 03:59:58 +08:00
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
|
2019-02-22 21:39:59 +08:00
|
|
|
SECItem keyItem = {siBuffer, toUcharPtr(testvector.Key.data()),
|
|
|
|
|
static_cast<unsigned int>(testvector.Key.size())};
|
2018-01-19 03:59:58 +08:00
|
|
|
|
|
|
|
|
// Import key.
|
|
|
|
|
ScopedPK11SymKey symKey(PK11_ImportSymKey(
|
|
|
|
|
slot.get(), mech, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, nullptr));
|
|
|
|
|
EXPECT_TRUE(!!symKey);
|
|
|
|
|
|
|
|
|
|
// Check.
|
2019-02-22 21:39:59 +08:00
|
|
|
EncryptDecrypt(symKey.get(), testvector.invalid_iv, testvector.invalid_tag,
|
|
|
|
|
testvector.Data.data(), testvector.Data.size(),
|
|
|
|
|
testvector.AAD.data(), testvector.AAD.size(),
|
|
|
|
|
testvector.IV.data(), testvector.IV.size(),
|
|
|
|
|
testvector.CT.data(), testvector.CT.size());
|
2018-01-19 03:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
CK_MECHANISM_TYPE mech = CKM_NSS_CHACHA20_POLY1305;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_F(Pkcs11ChaCha20Poly1305Test, GenerateEncryptDecrypt) {
|
|
|
|
|
// Generate a random key.
|
|
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
|
|
|
|
|
ScopedPK11SymKey symKey(PK11_KeyGen(slot.get(), mech, nullptr, 32, nullptr));
|
|
|
|
|
EXPECT_TRUE(!!symKey);
|
|
|
|
|
|
|
|
|
|
// Generate random data.
|
|
|
|
|
std::vector<uint8_t> data(512);
|
2019-02-22 21:39:59 +08:00
|
|
|
SECStatus rv =
|
|
|
|
|
PK11_GenerateRandomOnSlot(slot.get(), data.data(), data.size());
|
2018-01-19 03:59:58 +08:00
|
|
|
EXPECT_EQ(rv, SECSuccess);
|
|
|
|
|
|
|
|
|
|
// Generate random AAD.
|
|
|
|
|
std::vector<uint8_t> aad(16);
|
2019-02-22 21:39:59 +08:00
|
|
|
rv = PK11_GenerateRandomOnSlot(slot.get(), aad.data(), aad.size());
|
2018-01-19 03:59:58 +08:00
|
|
|
EXPECT_EQ(rv, SECSuccess);
|
|
|
|
|
|
|
|
|
|
// Generate random IV.
|
|
|
|
|
std::vector<uint8_t> iv(12);
|
2019-02-22 21:39:59 +08:00
|
|
|
rv = PK11_GenerateRandomOnSlot(slot.get(), iv.data(), iv.size());
|
2018-01-19 03:59:58 +08:00
|
|
|
EXPECT_EQ(rv, SECSuccess);
|
|
|
|
|
|
|
|
|
|
// Check.
|
2019-02-22 21:39:59 +08:00
|
|
|
EncryptDecrypt(symKey.get(), false, false, data.data(), data.size(),
|
|
|
|
|
aad.data(), aad.size(), iv.data(), iv.size());
|
2018-01-19 03:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
2019-02-22 21:39:59 +08:00
|
|
|
TEST_P(Pkcs11ChaCha20Poly1305Test, TestVectors) { EncryptDecrypt(GetParam()); }
|
2018-01-19 03:59:58 +08:00
|
|
|
|
2019-02-22 21:39:59 +08:00
|
|
|
INSTANTIATE_TEST_CASE_P(NSSTestVector, Pkcs11ChaCha20Poly1305Test,
|
|
|
|
|
::testing::ValuesIn(kChaCha20Vectors));
|
|
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(WycheproofTestVector, Pkcs11ChaCha20Poly1305Test,
|
|
|
|
|
::testing::ValuesIn(kChaCha20WycheproofVectors));
|
2018-01-19 03:59:58 +08:00
|
|
|
|
|
|
|
|
} // namespace nss_test
|