#if defined(OHOS_UNITTESTS)
#define private public
#include "crypto/encryptor.h"
#include "crypto/symmetric_key.h"
#undef private
#include "crypto/encryptor.cc"
#else
#include "crypto/encryptor.h"
#include "crypto/symmetric_key.h"
#endif
#include <stddef.h>
#include <memory>
#include <string>
#include "base/containers/span.h"
#include "base/strings/string_number_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OHOS_UNITTESTS)
TEST(EncryptorTest, Init001) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
std::vector<uint8_t> iv(12, 0x03);
auto mode = static_cast<crypto::Encryptor::Mode>(-1);
EXPECT_EQ(true, encryptor.Init(key.get(), mode, iv));
}
TEST(EncryptorTest, Init002) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
auto iv = base::span<const uint8_t>();
auto mode = crypto::Encryptor::CBC;
EXPECT_EQ(false, encryptor.Init(key.get(), mode, iv));
}
TEST(EncryptorTest, Init003) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
auto iv = base::span<const uint8_t>();
auto mode = crypto::Encryptor::CTR;
EXPECT_EQ(true, encryptor.Init(key.get(), mode, iv));
}
TEST(EncryptorTest, Init004) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
auto iv = base::span<const uint8_t>();
auto mode = crypto::Encryptor::GCM;
EXPECT_EQ(false, encryptor.Init(key.get(), mode, iv));
}
TEST(EncryptorTest, Init005) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
std::vector<uint8_t> iv(12, 0x03);
auto mode = crypto::Encryptor::GCM;
EXPECT_EQ(true, encryptor.Init(key.get(), mode, iv));
}
TEST(EncryptorTest, CryptString001) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
std::vector<uint8_t> iv(12, 0x03);
auto mode = crypto::Encryptor::GCM;
encryptor.Init(key.get(), mode, iv);
bool do_encrypt = true;
base::StringPiece input;
std::string output("the iv: 12 b");
EXPECT_EQ(false, encryptor.CryptString(do_encrypt, input, &output));
}
TEST(EncryptorTest, CryptString002) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
std::vector<uint8_t> iv(12, 0x03);
auto mode = crypto::Encryptor::GCM;
encryptor.Init(key.get(), mode, iv);
bool do_encrypt = false;
base::StringPiece input;
std::string output("the iv: 12 b");
EXPECT_EQ(false, encryptor.CryptString(do_encrypt, input, &output));
}
TEST(EncryptorTest, CryptString003) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
std::vector<uint8_t> iv(12, 0x03);
auto mode = crypto::Encryptor::GCM;
auto is_init = encryptor.Init(key.get(), mode, iv);
EXPECT_EQ(true, is_init);
bool do_encrypt = false;
std::string long_string("This is a very long string");
base::StringPiece input(long_string.data(), long_string.length());
std::string output("the input.length > 16");
EXPECT_EQ(false, encryptor.CryptString(do_encrypt, input, &output));
}
TEST(EncryptorTest, CryptString004) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
std::vector<uint8_t> iv(12, 0x03);
auto mode = crypto::Encryptor::GCM;
auto is_init = encryptor.Init(key.get(), mode, iv);
EXPECT_EQ(true, is_init);
bool do_encrypt = true;
std::string long_string("This is string");
base::StringPiece input(long_string.data(), long_string.length());
std::string output("this is ouput string");
EXPECT_EQ(true, encryptor.CryptString(do_encrypt, input, &output));
}
TEST(EncryptorTest, MaxOutput001) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
std::vector<uint8_t> iv(12, 0x03);
auto mode = crypto::Encryptor::GCM;
auto is_init = encryptor.Init(key.get(), mode, iv);
EXPECT_EQ(true, is_init);
size_t length_ = 12;
bool do_encrypt = true;
auto result = encryptor.MaxOutput(do_encrypt, length_);
auto lhs_result = length_ + 12;
EXPECT_EQ(lhs_result, result);
}
TEST(EncryptorTest, MaxOutput002) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
std::vector<uint8_t> iv(12, 0x03);
auto mode = crypto::Encryptor::GCM;
auto is_init = encryptor.Init(key.get(), mode, iv);
EXPECT_EQ(true, is_init);
size_t length_ = 12;
bool do_encrypt = false;
auto result = encryptor.MaxOutput(do_encrypt, length_);
EXPECT_EQ(length_, result);
}
TEST(EncryptorTest, MaxOutput003) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
crypto::Encryptor encryptor;
std::vector<uint8_t> iv(0, 0x03);
auto mode = crypto::Encryptor::CTR;
auto is_init = encryptor.Init(key.get(), mode, iv);
EXPECT_EQ(true, is_init);
size_t length_ = 12;
bool do_encrypt = false;
auto result = encryptor.MaxOutput(do_encrypt, length_);
EXPECT_EQ(length_, result);
}
#endif
TEST(EncryptorTest, EncryptDecrypt) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
EXPECT_TRUE(key.get());
crypto::Encryptor encryptor;
std::string iv("the iv: 16 bytes");
EXPECT_EQ(16U, iv.size());
EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv));
std::string plaintext("this is the plaintext");
std::string ciphertext;
EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
EXPECT_LT(0U, ciphertext.size());
std::string decrypted;
EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted));
EXPECT_EQ(plaintext, decrypted);
std::vector<uint8_t> plaintext_vec(plaintext.begin(), plaintext.end());
std::vector<uint8_t> ciphertext_vec;
EXPECT_TRUE(encryptor.Encrypt(plaintext_vec, &ciphertext_vec));
EXPECT_LT(0U, ciphertext_vec.size());
std::vector<uint8_t> decrypted_vec;
EXPECT_TRUE(encryptor.Decrypt(ciphertext_vec, &decrypted_vec));
EXPECT_EQ(plaintext_vec, decrypted_vec);
}
TEST(EncryptorTest, DecryptWrongKey) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
EXPECT_TRUE(key.get());
std::unique_ptr<crypto::SymmetricKey> wrong_key(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "wrongword", "sweetest", 1000, 256));
EXPECT_TRUE(wrong_key.get());
std::unique_ptr<crypto::SymmetricKey> wrong_key2(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "wrongword+", "sweetest", 1000, 256));
EXPECT_TRUE(wrong_key2.get());
std::unique_ptr<crypto::SymmetricKey> wrong_key3(
crypto::SymmetricKey::DeriveKeyFromPasswordUsingPbkdf2(
crypto::SymmetricKey::AES, "wrongwordx", "sweetest", 1000, 256));
EXPECT_TRUE(wrong_key3.get());
crypto::Encryptor encryptor;
std::string iv("the iv: 16 bytes");
EXPECT_EQ(16U, iv.size());
EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv));
std::string plaintext("this is the plaintext");
std::string ciphertext;
EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
static const unsigned char expected_ciphertext[] = {
0x7D, 0x67, 0x5B, 0x53, 0xE6, 0xD8, 0x0F, 0x27,
0x74, 0xB1, 0x90, 0xFE, 0x6E, 0x58, 0x4A, 0xA0,
0x0E, 0x35, 0xE3, 0x01, 0xC0, 0xFE, 0x9A, 0xD8,
0x48, 0x1D, 0x42, 0xB0, 0xBA, 0x21, 0xB2, 0x0C
};
ASSERT_EQ(std::size(expected_ciphertext), ciphertext.size());
for (size_t i = 0; i < ciphertext.size(); ++i) {
ASSERT_EQ(expected_ciphertext[i],
static_cast<unsigned char>(ciphertext[i]));
}
std::string decrypted;
crypto::Encryptor decryptor;
EXPECT_TRUE(decryptor.Init(wrong_key.get(), crypto::Encryptor::CBC, iv));
EXPECT_FALSE(decryptor.Decrypt(ciphertext, &decrypted));
crypto::Encryptor decryptor2;
EXPECT_TRUE(decryptor2.Init(wrong_key2.get(), crypto::Encryptor::CBC, iv));
EXPECT_TRUE(decryptor2.Decrypt(ciphertext, &decrypted));
crypto::Encryptor decryptor3;
EXPECT_TRUE(decryptor3.Init(wrong_key3.get(), crypto::Encryptor::CBC, iv));
EXPECT_FALSE(decryptor3.Decrypt(ciphertext, &decrypted));
}
namespace {
const unsigned char kAES128CTRKey[] = {
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
};
const unsigned char kAES256CTRKey[] = {
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
};
const unsigned char kAESCTRInitCounter[] = {
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
};
const unsigned char kAESCTRPlaintext[] = {
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
};
const unsigned char kAES128CTRCiphertext[] = {
0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26,
0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce,
0x98, 0x06, 0xf6, 0x6b, 0x79, 0x70, 0xfd, 0xff,
0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff,
0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e,
0x5b, 0x4f, 0x09, 0x02, 0x0d, 0xb0, 0x3e, 0xab,
0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1,
0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee
};
const unsigned char kAES256CTRCiphertext[] = {
0x60, 0x1e, 0xc3, 0x13, 0x77, 0x57, 0x89, 0xa5,
0xb7, 0xa7, 0xf5, 0x04, 0xbb, 0xf3, 0xd2, 0x28,
0xf4, 0x43, 0xe3, 0xca, 0x4d, 0x62, 0xb5, 0x9a,
0xca, 0x84, 0xe9, 0x90, 0xca, 0xca, 0xf5, 0xc5,
0x2b, 0x09, 0x30, 0xda, 0xa2, 0x3d, 0xe9, 0x4c,
0xe8, 0x70, 0x17, 0xba, 0x2d, 0x84, 0x98, 0x8d,
0xdf, 0xc9, 0xc5, 0x8d, 0xb6, 0x7a, 0xad, 0xa6,
0x13, 0xc2, 0xdd, 0x08, 0x45, 0x79, 0x41, 0xa6
};
void TestAESCTREncrypt(
const unsigned char* key, size_t key_size,
const unsigned char* init_counter, size_t init_counter_size,
const unsigned char* plaintext, size_t plaintext_size,
const unsigned char* ciphertext, size_t ciphertext_size) {
std::string key_str(reinterpret_cast<const char*>(key), key_size);
std::unique_ptr<crypto::SymmetricKey> sym_key(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key_str));
ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, ""));
base::StringPiece init_counter_str(
reinterpret_cast<const char*>(init_counter), init_counter_size);
base::StringPiece plaintext_str(
reinterpret_cast<const char*>(plaintext), plaintext_size);
EXPECT_TRUE(encryptor.SetCounter(init_counter_str));
std::string encrypted;
EXPECT_TRUE(encryptor.Encrypt(plaintext_str, &encrypted));
EXPECT_EQ(ciphertext_size, encrypted.size());
EXPECT_EQ(0, memcmp(encrypted.data(), ciphertext, encrypted.size()));
std::string decrypted;
EXPECT_TRUE(encryptor.SetCounter(init_counter_str));
EXPECT_TRUE(encryptor.Decrypt(encrypted, &decrypted));
EXPECT_EQ(plaintext_str, decrypted);
EXPECT_TRUE(
encryptor.SetCounter(base::make_span(init_counter, init_counter_size)));
std::vector<uint8_t> encrypted_vec;
EXPECT_TRUE(encryptor.Encrypt(base::make_span(plaintext, plaintext_size),
&encrypted_vec));
EXPECT_EQ(ciphertext_size, encrypted_vec.size());
EXPECT_EQ(0, memcmp(encrypted_vec.data(), ciphertext, encrypted_vec.size()));
std::vector<uint8_t> decrypted_vec;
EXPECT_TRUE(
encryptor.SetCounter(base::make_span(init_counter, init_counter_size)));
EXPECT_TRUE(encryptor.Decrypt(encrypted_vec, &decrypted_vec));
EXPECT_EQ(std::vector<uint8_t>(plaintext, plaintext + plaintext_size),
decrypted_vec);
}
void TestAESCTRMultipleDecrypt(
const unsigned char* key, size_t key_size,
const unsigned char* init_counter, size_t init_counter_size,
const unsigned char* plaintext, size_t plaintext_size,
const unsigned char* ciphertext, size_t ciphertext_size) {
std::string key_str(reinterpret_cast<const char*>(key), key_size);
std::unique_ptr<crypto::SymmetricKey> sym_key(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key_str));
ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, ""));
EXPECT_TRUE(encryptor.SetCounter(base::StringPiece(
reinterpret_cast<const char*>(init_counter), init_counter_size)));
std::string ciphertext_str(reinterpret_cast<const char*>(ciphertext),
ciphertext_size);
int kTestDecryptSizes[] = { 32, 16, 8 };
int offset = 0;
for (size_t i = 0; i < std::size(kTestDecryptSizes); ++i) {
std::string decrypted;
size_t len = kTestDecryptSizes[i];
EXPECT_TRUE(
encryptor.Decrypt(ciphertext_str.substr(offset, len), &decrypted));
EXPECT_EQ(len, decrypted.size());
EXPECT_EQ(0, memcmp(decrypted.data(), plaintext + offset, len));
offset += len;
}
}
}
TEST(EncryptorTest, EncryptAES128CTR) {
TestAESCTREncrypt(kAES128CTRKey, std::size(kAES128CTRKey), kAESCTRInitCounter,
std::size(kAESCTRInitCounter), kAESCTRPlaintext,
std::size(kAESCTRPlaintext), kAES128CTRCiphertext,
std::size(kAES128CTRCiphertext));
}
TEST(EncryptorTest, EncryptAES256CTR) {
TestAESCTREncrypt(kAES256CTRKey, std::size(kAES256CTRKey), kAESCTRInitCounter,
std::size(kAESCTRInitCounter), kAESCTRPlaintext,
std::size(kAESCTRPlaintext), kAES256CTRCiphertext,
std::size(kAES256CTRCiphertext));
}
TEST(EncryptorTest, EncryptAES128CTR_MultipleDecrypt) {
TestAESCTRMultipleDecrypt(kAES128CTRKey, std::size(kAES128CTRKey),
kAESCTRInitCounter, std::size(kAESCTRInitCounter),
kAESCTRPlaintext, std::size(kAESCTRPlaintext),
kAES128CTRCiphertext,
std::size(kAES128CTRCiphertext));
}
TEST(EncryptorTest, EncryptAES256CTR_MultipleDecrypt) {
TestAESCTRMultipleDecrypt(kAES256CTRKey, std::size(kAES256CTRKey),
kAESCTRInitCounter, std::size(kAESCTRInitCounter),
kAESCTRPlaintext, std::size(kAESCTRPlaintext),
kAES256CTRCiphertext,
std::size(kAES256CTRCiphertext));
}
TEST(EncryptorTest, EncryptDecryptCTR) {
std::unique_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 128));
EXPECT_TRUE(key.get());
const std::string kInitialCounter = "0000000000000000";
crypto::Encryptor encryptor;
EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CTR, ""));
EXPECT_TRUE(encryptor.SetCounter(kInitialCounter));
std::string plaintext("normal plaintext of random length");
std::string ciphertext;
EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
EXPECT_LT(0U, ciphertext.size());
std::string decrypted;
EXPECT_TRUE(encryptor.SetCounter(kInitialCounter));
EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted));
EXPECT_EQ(plaintext, decrypted);
plaintext = "0123456789012345";
EXPECT_TRUE(encryptor.SetCounter(kInitialCounter));
EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
EXPECT_LT(0U, ciphertext.size());
EXPECT_TRUE(encryptor.SetCounter(kInitialCounter));
EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted));
EXPECT_EQ(plaintext, decrypted);
}
TEST(EncryptorTest, EncryptAES256CBC) {
static const unsigned char kRawKey[] = {
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
};
static const unsigned char kRawIv[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};
static const unsigned char kRawPlaintext[] = {
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10,
};
static const unsigned char kRawCiphertext[] = {
0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba,
0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d,
0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d,
0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf,
0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc,
0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b,
0x3f, 0x46, 0x17, 0x96, 0xd6, 0xb0, 0xd6, 0xb2,
0xe0, 0xc2, 0xa7, 0x2b, 0x4d, 0x80, 0xe6, 0x44
};
std::string key(reinterpret_cast<const char*>(kRawKey), sizeof(kRawKey));
std::unique_ptr<crypto::SymmetricKey> sym_key(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
std::string iv(reinterpret_cast<const char*>(kRawIv), sizeof(kRawIv));
EXPECT_EQ(16U, iv.size());
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
std::string plaintext(reinterpret_cast<const char*>(kRawPlaintext),
sizeof(kRawPlaintext));
std::string ciphertext;
EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
EXPECT_EQ(sizeof(kRawCiphertext), ciphertext.size());
EXPECT_EQ(0, memcmp(ciphertext.data(), kRawCiphertext, ciphertext.size()));
std::string decrypted;
EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted));
EXPECT_EQ(plaintext, decrypted);
}
TEST(EncryptorTest, EncryptAES128CBCRegression) {
std::string key = "128=SixteenBytes";
std::string iv = "Sweet Sixteen IV";
std::string plaintext = "Plain text with a g-clef U+1D11E \360\235\204\236";
std::string expected_ciphertext_hex =
"D4A67A0BA33C30F207344D81D1E944BBE65587C3D7D9939A"
"C070C62B9C15A3EA312EA4AD1BC7929F4D3C16B03AD5ADA8";
std::unique_ptr<crypto::SymmetricKey> sym_key(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
EXPECT_EQ(16U, iv.size());
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
std::string ciphertext;
EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(),
ciphertext.size()));
std::string decrypted;
EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted));
EXPECT_EQ(plaintext, decrypted);
}
TEST(EncryptorTest, UnsupportedKeySize) {
std::string key = "7 = bad";
std::string iv = "Sweet Sixteen IV";
std::unique_ptr<crypto::SymmetricKey> sym_key(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
if (!sym_key.get())
return;
crypto::Encryptor encryptor;
EXPECT_EQ(16U, iv.size());
EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
}
TEST(EncryptorTest, UnsupportedIV) {
std::string key = "128=SixteenBytes";
std::string iv = "OnlyForteen :(";
std::unique_ptr<crypto::SymmetricKey> sym_key(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
}
TEST(EncryptorTest, EmptyEncryptCBC) {
std::string key = "128=SixteenBytes";
std::string iv = "Sweet Sixteen IV";
std::string plaintext;
std::string expected_ciphertext_hex = "8518B8878D34E7185E300D0FCC426396";
std::unique_ptr<crypto::SymmetricKey> sym_key(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
EXPECT_EQ(16U, iv.size());
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
std::string ciphertext;
EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(),
ciphertext.size()));
std::string decrypted;
EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted));
EXPECT_EQ(decrypted, plaintext);
EXPECT_FALSE(encryptor.Decrypt(std::string(), &decrypted));
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
std::vector<uint8_t> ciphertext_bytes;
EXPECT_TRUE(
encryptor.Encrypt(base::span<const uint8_t>(), &ciphertext_bytes));
EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext_bytes));
std::vector<uint8_t> decrypted_bytes;
EXPECT_TRUE(encryptor.Decrypt(ciphertext_bytes, &decrypted_bytes));
EXPECT_EQ(decrypted_bytes.size(), 0u);
EXPECT_FALSE(
encryptor.Decrypt(base::span<const uint8_t>(), &decrypted_bytes));
}
TEST(EncryptorTest, EmptyEncryptCTR) {
std::string key = "128=SixteenBytes";
std::string iv = "Sweet Sixteen IV";
std::string plaintext;
std::string expected_ciphertext;
std::unique_ptr<crypto::SymmetricKey> sym_key(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, ""));
ASSERT_TRUE(encryptor.SetCounter(iv));
std::string ciphertext;
EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
EXPECT_EQ(expected_ciphertext, ciphertext);
std::string decrypted;
EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted));
EXPECT_EQ(decrypted, plaintext);
ASSERT_TRUE(encryptor.SetCounter(iv));
std::vector<uint8_t> ciphertext_bytes;
EXPECT_TRUE(
encryptor.Encrypt(base::span<const uint8_t>(), &ciphertext_bytes));
EXPECT_EQ(ciphertext_bytes.size(), 0u);
std::vector<uint8_t> decrypted_bytes;
EXPECT_TRUE(encryptor.Decrypt(base::span<const uint8_t>(), &decrypted_bytes));
EXPECT_EQ(decrypted_bytes.size(), 0u);
}
TEST(EncryptorTest, CipherTextNotMultipleOfBlockSize) {
std::string key = "128=SixteenBytes";
std::string iv = "Sweet Sixteen IV";
std::unique_ptr<crypto::SymmetricKey> sym_key(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
EXPECT_EQ(16U, iv.size());
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
std::unique_ptr<char[]> ciphertext(new char[1]);
std::string plaintext;
EXPECT_FALSE(
encryptor.Decrypt(base::StringPiece(ciphertext.get(), 1), &plaintext));
}
TEST(EncryptorTest, EncryptGCM) {
std::string key = "128=SixteenBytes";
std::string iv = "121212121212";
crypto::Encryptor encryptor;
const uint8_t in_data[] = {0x01, 0x02, 0x03, 0x04, 0x05};
uint8_t out_data[] = {0x01, 0x02, 0x03, 0x04, 0x05};
base::span<const uint8_t> input = in_data;
base::span<uint8_t> output = out_data;
std::string myString;
myString.assign("Hello, World!");
std::unique_ptr<crypto::SymmetricKey> sym_key(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
ASSERT_TRUE(sym_key.get());
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::GCM, iv));
EXPECT_NE(encryptor.EncryptGCM(input, output, &myString), absl::nullopt);
}
#if defined(OHOS_UNITTESTS)
TEST(EncryptorTest, GetCipherForKeyGCM_Case16) {
auto key = crypto::SymmetricKey::Import(crypto::SymmetricKey::AES,
"0123456789abcdef");
const EVP_CIPHER* cipher = crypto::GetCipherForKeyGCM(key.get());
EXPECT_EQ(cipher, EVP_aes_128_gcm());
}
TEST(EncryptorTest, GetCipherForKeyGCM_Case32) {
auto key = crypto::SymmetricKey::Import(crypto::SymmetricKey::AES,
"0123456789abcdef0123456789abcdef");
const EVP_CIPHER* cipher = crypto::GetCipherForKeyGCM(key.get());
EXPECT_EQ(cipher, EVP_aes_256_gcm());
}
TEST(EncryptorTest, GetCipherForKeyGCM_Default) {
auto key = std::make_shared<crypto::SymmetricKey>();
key->key_ = "12356465416";
const EVP_CIPHER* cipher = crypto::GetCipherForKeyGCM(key.get());
EXPECT_EQ(cipher, nullptr);
}
#endif