2020-01-02 21:06:40 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
/* 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 "secerr.h"
|
|
|
|
|
|
|
|
|
|
#include "nss_scoped_ptrs.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
namespace nss_test {
|
2026-06-30 06:37:32 +01:00
|
|
|
class Pkcs11SeedCbcTest : public ::testing::Test {
|
2020-01-02 21:06:40 +01:00
|
|
|
protected:
|
2026-06-30 06:37:32 +01:00
|
|
|
enum class Action { Encrypt, Decrypt };
|
|
|
|
|
|
|
|
|
|
SECStatus EncryptDecryptSeed(Action action, unsigned int input_size,
|
|
|
|
|
unsigned int output_size) {
|
2020-01-02 21:06:40 +01:00
|
|
|
// Generate a random key.
|
|
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
|
|
|
|
|
ScopedPK11SymKey sym_key(
|
2026-06-30 06:37:32 +01:00
|
|
|
PK11_KeyGen(slot.get(), kMech, nullptr, 16, nullptr));
|
2020-01-02 21:06:40 +01:00
|
|
|
EXPECT_TRUE(!!sym_key);
|
|
|
|
|
|
2026-06-30 06:37:32 +01:00
|
|
|
std::vector<uint8_t> data(input_size);
|
2020-01-02 21:06:40 +01:00
|
|
|
std::vector<uint8_t> init_vector(16);
|
2026-06-30 06:37:32 +01:00
|
|
|
std::vector<uint8_t> output(output_size);
|
|
|
|
|
SECItem params = {siBuffer, init_vector.data(),
|
|
|
|
|
(unsigned int)init_vector.size()};
|
2020-01-02 21:06:40 +01:00
|
|
|
|
2026-06-30 06:37:32 +01:00
|
|
|
// Try to encrypt/decrypt.
|
2020-01-02 21:06:40 +01:00
|
|
|
unsigned int output_len = 0;
|
2026-06-30 06:37:32 +01:00
|
|
|
if (action == Action::Encrypt) {
|
|
|
|
|
return PK11_Encrypt(sym_key.get(), kMech, ¶ms, output.data(),
|
|
|
|
|
&output_len, output_size, data.data(), data.size());
|
|
|
|
|
} else {
|
|
|
|
|
return PK11_Decrypt(sym_key.get(), kMech, ¶ms, output.data(),
|
|
|
|
|
&output_len, output_size, data.data(), data.size());
|
2020-01-02 21:06:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-30 06:37:32 +01:00
|
|
|
const CK_MECHANISM_TYPE kMech = CKM_SEED_CBC;
|
2020-01-02 21:06:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// The intention here is to test the arguments of these functions
|
|
|
|
|
// The resulted content is already tested in EncryptDeriveTests.
|
|
|
|
|
// SEED_CBC needs an IV of 16 bytes.
|
|
|
|
|
// The input data size must be multiple of 16.
|
|
|
|
|
// If not, some padding should be added.
|
|
|
|
|
// The output size must be at least the size of input data.
|
2026-06-30 06:37:32 +01:00
|
|
|
TEST_F(Pkcs11SeedCbcTest, SeedCBC_ValidArgs) {
|
|
|
|
|
EXPECT_EQ(SECSuccess, EncryptDecryptSeed(Action::Encrypt, 16, 16));
|
|
|
|
|
EXPECT_EQ(SECSuccess, EncryptDecryptSeed(Action::Decrypt, 16, 16));
|
2020-01-02 21:06:40 +01:00
|
|
|
// No problem if maxLen is bigger than input data.
|
2026-06-30 06:37:32 +01:00
|
|
|
EXPECT_EQ(SECSuccess, EncryptDecryptSeed(Action::Encrypt, 16, 32));
|
|
|
|
|
EXPECT_EQ(SECSuccess, EncryptDecryptSeed(Action::Decrypt, 16, 32));
|
2020-01-02 21:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-30 06:37:32 +01:00
|
|
|
TEST_F(Pkcs11SeedCbcTest, SeedCBC_InvalidArgs) {
|
2020-01-02 21:06:40 +01:00
|
|
|
// maxLen lower than input data.
|
2026-06-30 06:37:32 +01:00
|
|
|
EXPECT_EQ(SECFailure, EncryptDecryptSeed(Action::Encrypt, 16, 10));
|
|
|
|
|
EXPECT_EQ(SECFailure, EncryptDecryptSeed(Action::Decrypt, 16, 10));
|
2020-01-02 21:06:40 +01:00
|
|
|
// input data not multiple of SEED_BLOCK_SIZE (16)
|
2026-06-30 06:37:32 +01:00
|
|
|
EXPECT_EQ(SECFailure, EncryptDecryptSeed(Action::Encrypt, 17, 32));
|
|
|
|
|
EXPECT_EQ(SECFailure, EncryptDecryptSeed(Action::Decrypt, 17, 32));
|
2020-01-02 21:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-30 06:37:32 +01:00
|
|
|
} // namespace nss_test
|