blob: 8148f4d0b5a1a7f6b691831f631d606fe868ddaa [file] [log] [blame]
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cryptohome/cryptolib.h"
#include <openssl/rsa.h>
#include <base/base64.h>
#include <brillo/secure_blob.h>
#include <crypto/scoped_openssl_types.h>
#include <gtest/gtest.h>
using brillo::SecureBlob;
namespace cryptohome {
namespace {
void CheckBlob(const brillo::SecureBlob& original_blob,
const brillo::SecureBlob& key,
const brillo::SecureBlob& wrapped_blob,
const std::string& original_str) {
brillo::SecureBlob decrypted_blob(wrapped_blob.size());
CryptoError error;
EXPECT_TRUE(CryptoLib::DeprecatedDecryptScryptBlob(wrapped_blob, key,
&decrypted_blob, &error));
const std::string decrypted_str(decrypted_blob.begin(), decrypted_blob.end());
EXPECT_EQ(original_str, decrypted_str);
}
} // namespace
TEST(CryptoLibTest, RsaOaepDecrypt) {
// Generate the input data.
constexpr int kKeySizeBits = 1024;
constexpr int kKeySizeBytes = kKeySizeBits / 8;
constexpr int kPlaintextSize = 32;
crypto::ScopedRSA rsa(RSA_new());
CHECK(rsa);
crypto::ScopedBIGNUM e(BN_new());
CHECK(e);
EXPECT_TRUE(BN_set_word(e.get(), kWellKnownExponent));
EXPECT_TRUE(RSA_generate_key_ex(rsa.get(), kKeySizeBits, e.get(), nullptr));
const auto plaintext = CryptoLib::CreateSecureRandomBlob(kPlaintextSize);
// Test decryption when a non-empty label is used.
const SecureBlob kFirstOaepLabel("foo");
SecureBlob first_padded_data(kKeySizeBytes);
ASSERT_EQ(
1, RSA_padding_add_PKCS1_OAEP(
first_padded_data.data(), kKeySizeBytes, plaintext.data(),
plaintext.size(), kFirstOaepLabel.data(), kFirstOaepLabel.size()));
SecureBlob first_ciphertext(kKeySizeBytes);
ASSERT_NE(-1, RSA_public_encrypt(kKeySizeBytes, first_padded_data.data(),
first_ciphertext.data(), rsa.get(),
RSA_NO_PADDING));
SecureBlob first_decrypt_result;
EXPECT_TRUE(CryptoLib::RsaOaepDecrypt(first_ciphertext, kFirstOaepLabel,
rsa.get(), &first_decrypt_result));
EXPECT_EQ(plaintext, first_decrypt_result);
// Test the empty label case in which the encryption is done by a single call
// to OpenSSL.
SecureBlob second_ciphertext(kKeySizeBytes);
ASSERT_NE(-1, RSA_public_encrypt(kPlaintextSize, plaintext.data(),
second_ciphertext.data(), rsa.get(),
RSA_PKCS1_OAEP_PADDING));
SecureBlob second_decrypt_result;
EXPECT_TRUE(CryptoLib::RsaOaepDecrypt(second_ciphertext, SecureBlob(),
rsa.get(), &second_decrypt_result));
EXPECT_EQ(plaintext, second_decrypt_result);
}
TEST(CryptoLibTest, TestRocaVulnerable) {
// This is a modulus from key generated by a TPM running vulnerable firmware.
const uint8_t vulnerable_modulus[] = {
0x00, 0x9e, 0x31, 0xea, 0x73, 0xed, 0x06, 0x22, 0x52, 0x30, 0x85, 0x22,
0x75, 0xa8, 0x60, 0x6e, 0x08, 0x56, 0xbc, 0xee, 0xb1, 0xba, 0xd5, 0x62,
0xe0, 0x3b, 0x03, 0xc4, 0x68, 0x2a, 0x20, 0x72, 0xa2, 0x5c, 0x7a, 0xd8,
0x9d, 0x00, 0xf8, 0xb3, 0xf8, 0x83, 0xc3, 0x97, 0xaa, 0x5d, 0x55, 0xfe,
0x75, 0x1f, 0x0a, 0x25, 0xbf, 0xe0, 0x89, 0x0c, 0x02, 0x30, 0x6b, 0x5f,
0xfa, 0x0f, 0x6c, 0xc6, 0x20, 0x79, 0xc9, 0x6a, 0x32, 0x4a, 0x15, 0xf3,
0x87, 0xf8, 0x24, 0x0b, 0x1b, 0x62, 0x9d, 0xcc, 0xe5, 0xc5, 0x14, 0x5d,
0x69, 0xcc, 0x2f, 0x97, 0x3f, 0x40, 0x51, 0xe3, 0x35, 0x38, 0x99, 0x14,
0xcc, 0x45, 0x91, 0x93, 0x65, 0x31, 0x98, 0x03, 0x80, 0x2a, 0x13, 0x37,
0x89, 0x0b, 0xfb, 0x87, 0xae, 0x99, 0xa1, 0x75, 0x72, 0xdc, 0x53, 0x64,
0x71, 0x6f, 0xdc, 0x13, 0x91, 0xf8, 0x16, 0x5c, 0xdc, 0xb9, 0x07, 0x9c,
0xc2, 0x0e, 0x5b, 0x71, 0xf7, 0x6d, 0x70, 0xba, 0x05, 0x1a, 0x47, 0x06,
0xb2, 0x7e, 0x65, 0xdf, 0xae, 0x8f, 0x49, 0xb5, 0x4e, 0x5e, 0x7a, 0x8d,
0x1e, 0x81, 0x6f, 0x2e, 0x31, 0x35, 0x88, 0x03, 0x1d, 0xe7, 0xe0, 0x87,
0x7a, 0x87, 0xc0, 0x8b, 0xe0, 0xbb, 0x9c, 0x05, 0x68, 0x89, 0xe8, 0x04,
0x69, 0xc1, 0x33, 0xec, 0x14, 0xe0, 0x11, 0xd1, 0xae, 0x4a, 0xd0, 0xd9,
0x3a, 0x5b, 0x79, 0xc7, 0x12, 0x78, 0x2d, 0x8a, 0x8f, 0x2d, 0x00, 0xf7,
0x0d, 0x5e, 0x00, 0xa0, 0x35, 0x9a, 0x02, 0xb0, 0x73, 0xad, 0xbc, 0x44,
0xd2, 0x67, 0x73, 0x64, 0x08, 0xc8, 0x60, 0x58, 0x04, 0xf1, 0xa5, 0xd2,
0xd5, 0x18, 0x4e, 0x39, 0x3e, 0x68, 0xe6, 0xfa, 0xa7, 0x55, 0xd9, 0xeb,
0xd8, 0x5f, 0xe7, 0xde, 0xab, 0x2e, 0x8b, 0x17, 0x5d, 0x08, 0x79, 0x6b,
0x7a, 0x7e, 0xf0, 0x06, 0x61,
};
crypto::ScopedBIGNUM vulnerable_modulus_bn(
BN_bin2bn(vulnerable_modulus, sizeof(vulnerable_modulus), nullptr));
EXPECT_TRUE(CryptoLib::TestRocaVulnerable(vulnerable_modulus_bn.get()));
// A key generated by a non-vulnerable TPM.
const uint8_t good_modulus[] = {
0x00, 0xcc, 0xe8, 0xcf, 0xb5, 0x6e, 0x36, 0x99, 0x21, 0x7b, 0x95, 0xb9,
0x75, 0xa6, 0x80, 0x12, 0xb0, 0x54, 0x1c, 0x62, 0x10, 0x77, 0x06, 0xbf,
0x2c, 0xad, 0xa6, 0x5a, 0x79, 0x6a, 0x23, 0x06, 0x87, 0x2a, 0xf8, 0x37,
0x4c, 0x47, 0xa7, 0xcf, 0x82, 0x7e, 0xa1, 0xd5, 0x73, 0x56, 0x04, 0xc4,
0x60, 0xd7, 0x43, 0x5d, 0xa6, 0x6b, 0x44, 0x83, 0x77, 0xf9, 0x72, 0xff,
0x7d, 0xc4, 0x5c, 0x74, 0x3a, 0x43, 0x97, 0x68, 0xa1, 0x01, 0x57, 0x94,
0x22, 0xd8, 0xea, 0x19, 0x50, 0xf0, 0x4d, 0x29, 0x59, 0x04, 0xca, 0x92,
0x64, 0xb1, 0x3e, 0x13, 0x9e, 0x38, 0x82, 0xbf, 0xaa, 0xb5, 0x25, 0x57,
0xa1, 0xe0, 0x46, 0x89, 0x7f, 0x5d, 0x22, 0x03, 0x82, 0x89, 0x93, 0xa7,
0x6f, 0xb9, 0xb5, 0x2f, 0x51, 0x98, 0xa1, 0x8a, 0xae, 0xca, 0x97, 0x6b,
0x1d, 0x33, 0xbf, 0xc0, 0x04, 0x63, 0x47, 0x04, 0x5c, 0xfc, 0x98, 0x88,
0x6c, 0xb1, 0x05, 0x9b, 0xab, 0x69, 0x91, 0xca, 0xab, 0xa0, 0x39, 0x62,
0xcd, 0x0e, 0xa2, 0xb0, 0x04, 0x36, 0xa3, 0x1f, 0x08, 0x82, 0xf0, 0x16,
0xd9, 0xf8, 0xdf, 0x08, 0xaa, 0xa6, 0xac, 0x2e, 0x60, 0x77, 0xb3, 0xbb,
0x17, 0x71, 0x60, 0x7e, 0xb1, 0x46, 0x0d, 0x7b, 0xf2, 0x81, 0xef, 0x45,
0xb0, 0xa5, 0xbd, 0x3f, 0x8a, 0xe4, 0x3d, 0x81, 0x51, 0x3b, 0xbe, 0xc4,
0x84, 0x5d, 0x82, 0xba, 0xff, 0xca, 0x6c, 0x21, 0x90, 0x9c, 0x94, 0x3f,
0x1e, 0x34, 0x41, 0x02, 0x87, 0xcb, 0xa9, 0xd8, 0x01, 0x48, 0xe5, 0x8b,
0x7f, 0x38, 0xd4, 0x6e, 0xf3, 0xf8, 0x7b, 0xd8, 0xa3, 0x8e, 0x3d, 0xb9,
0x58, 0x8c, 0xab, 0x57, 0x03, 0x3b, 0xff, 0x94, 0x0b, 0x8b, 0x94, 0xf4,
0x36, 0xd7, 0x7f, 0x4f, 0xf6, 0x56, 0x3f, 0x80, 0x2a, 0x4a, 0xea, 0xfd,
0x74, 0x20, 0x5f, 0x90, 0xa3,
};
crypto::ScopedBIGNUM good_modulus_bn(
BN_bin2bn(good_modulus, sizeof(good_modulus), nullptr));
EXPECT_FALSE(CryptoLib::TestRocaVulnerable(good_modulus_bn.get()));
}
// This is not a known vector but a very simple test against the API.
TEST(CryptoLibTest, AesGcmTestSimple) {
brillo::SecureBlob key(kAesGcm256KeySize);
brillo::SecureBlob iv(kAesGcmIVSize);
brillo::SecureBlob tag(kAesGcmTagSize);
brillo::SecureBlob ciphertext(4096, '\0');
std::string message = "I am encrypting this message.";
brillo::SecureBlob plaintext(message.begin(), message.end());
CryptoLib::GetSecureRandom(key.data(), key.size());
EXPECT_TRUE(CryptoLib::AesGcmEncrypt(plaintext, key, &iv, &tag, &ciphertext));
// Validity check that the encryption actually did something.
EXPECT_NE(ciphertext, plaintext);
EXPECT_EQ(ciphertext.size(), plaintext.size());
brillo::SecureBlob decrypted_plaintext(4096);
EXPECT_TRUE(
CryptoLib::AesGcmDecrypt(ciphertext, tag, key, iv, &decrypted_plaintext));
EXPECT_EQ(plaintext, decrypted_plaintext);
}
TEST(CryptoLibTest, AesGcmTestWrongKey) {
brillo::SecureBlob key(kAesGcm256KeySize);
brillo::SecureBlob iv(kAesGcmIVSize);
brillo::SecureBlob tag(kAesGcmTagSize);
brillo::SecureBlob ciphertext(4096, '\0');
std::string message = "I am encrypting this message.";
brillo::SecureBlob plaintext(message.begin(), message.end());
CryptoLib::GetSecureRandom(key.data(), key.size());
EXPECT_TRUE(CryptoLib::AesGcmEncrypt(plaintext, key, &iv, &tag, &ciphertext));
// Validity check that the encryption actually did something.
EXPECT_NE(ciphertext, plaintext);
EXPECT_EQ(ciphertext.size(), plaintext.size());
brillo::SecureBlob wrong_key(kAesGcm256KeySize);
CryptoLib::GetSecureRandom(wrong_key.data(), wrong_key.size());
brillo::SecureBlob decrypted_plaintext(4096);
EXPECT_FALSE(CryptoLib::AesGcmDecrypt(ciphertext, tag, wrong_key, iv,
&decrypted_plaintext));
EXPECT_NE(plaintext, decrypted_plaintext);
}
TEST(CryptoLibTest, AesGcmTestWrongIV) {
brillo::SecureBlob key(kAesGcm256KeySize);
brillo::SecureBlob iv(kAesGcmIVSize);
brillo::SecureBlob tag(kAesGcmTagSize);
brillo::SecureBlob ciphertext(4096, '\0');
std::string message = "I am encrypting this message.";
brillo::SecureBlob plaintext(message.begin(), message.end());
CryptoLib::GetSecureRandom(key.data(), key.size());
EXPECT_TRUE(CryptoLib::AesGcmEncrypt(plaintext, key, &iv, &tag, &ciphertext));
// Validity check that the encryption actually did something.
EXPECT_NE(ciphertext, plaintext);
EXPECT_EQ(ciphertext.size(), plaintext.size());
brillo::SecureBlob wrong_iv(kAesGcmIVSize);
CryptoLib::GetSecureRandom(wrong_iv.data(), wrong_iv.size());
brillo::SecureBlob decrypted_plaintext(4096);
EXPECT_FALSE(CryptoLib::AesGcmDecrypt(ciphertext, tag, key, wrong_iv,
&decrypted_plaintext));
EXPECT_NE(plaintext, decrypted_plaintext);
}
TEST(CryptoLibTest, AesGcmTestWrongTag) {
brillo::SecureBlob key(kAesGcm256KeySize);
brillo::SecureBlob iv(kAesGcmIVSize);
brillo::SecureBlob tag(kAesGcmTagSize);
brillo::SecureBlob ciphertext(4096, '\0');
std::string message = "I am encrypting this message.";
brillo::SecureBlob plaintext(message.begin(), message.end());
CryptoLib::GetSecureRandom(key.data(), key.size());
EXPECT_TRUE(CryptoLib::AesGcmEncrypt(plaintext, key, &iv, &tag, &ciphertext));
// Validity check that the encryption actually did something.
EXPECT_NE(ciphertext, plaintext);
EXPECT_EQ(ciphertext.size(), plaintext.size());
brillo::SecureBlob wrong_tag(kAesGcmTagSize);
CryptoLib::GetSecureRandom(wrong_tag.data(), wrong_tag.size());
brillo::SecureBlob decrypted_plaintext(4096);
EXPECT_FALSE(CryptoLib::AesGcmDecrypt(ciphertext, wrong_tag, key, iv,
&decrypted_plaintext));
}
// This tests that AesGcmEncrypt produces a different IV on subsequent runs.
// Note that this is in no way a mathematical test of secure randomness. It
// makes sure nobody in the future, for some reason, changes AesGcmEncrypt to
// use a fixed IV without tests failing, at which point they will find this
// test, and see that AesGcmEncrypt *must* return random IVs.
TEST(CryptoLibTest, AesGcmTestUniqueIVs) {
brillo::SecureBlob key(kAesGcm256KeySize);
brillo::SecureBlob tag(kAesGcmTagSize);
brillo::SecureBlob ciphertext(4096, '\0');
std::string message = "I am encrypting this message.";
brillo::SecureBlob plaintext(message.begin(), message.end());
CryptoLib::GetSecureRandom(key.data(), key.size());
brillo::SecureBlob iv(kAesGcmIVSize);
EXPECT_TRUE(CryptoLib::AesGcmEncrypt(plaintext, key, &iv, &tag, &ciphertext));
brillo::SecureBlob iv2(kAesGcmIVSize);
EXPECT_TRUE(
CryptoLib::AesGcmEncrypt(plaintext, key, &iv2, &tag, &ciphertext));
brillo::SecureBlob iv3(kAesGcmIVSize);
EXPECT_TRUE(
CryptoLib::AesGcmEncrypt(plaintext, key, &iv3, &tag, &ciphertext));
EXPECT_NE(iv, iv2);
EXPECT_NE(iv, iv3);
}
// These tests check that DeprecatedEncryptScryptBlob and
// DeprecatedDecryptScryptBlob continue to perform the same function, and
// interoperate correctly, as they are re-written and re-factored. These do not
// prove cryptographic properties of the functions, or formal verification. They
// are validity checks for compatibility.
TEST(CryptoLibTest, EncryptScryptTest1) {
const std::string blob_str = "nOaVD3qRNqWhqQTDgyGb";
brillo::SecureBlob blob(blob_str.begin(), blob_str.end());
const std::string key_source_str = "UNdGe2HbyyXqIzpuxhVn";
brillo::SecureBlob key_source(key_source_str.begin(), key_source_str.end());
brillo::SecureBlob wrapped_blob;
EXPECT_TRUE(
CryptoLib::DeprecatedEncryptScryptBlob(blob, key_source, &wrapped_blob));
CheckBlob(blob, key_source, wrapped_blob, blob_str);
brillo::SecureBlob fixed_bytes_blob = {
0x73, 0x63, 0x72, 0x79, 0x70, 0x74, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x02, 0x96, 0x22, 0x20, 0xd6, 0x95, 0x85, 0x9c, 0x3e,
0xf0, 0xd4, 0x8f, 0x75, 0x64, 0x67, 0xa5, 0xd3, 0x0a, 0x67, 0xb7, 0xb8,
0xa1, 0xcf, 0x97, 0xec, 0x6a, 0x34, 0xf5, 0xa6, 0x7e, 0x76, 0x2d, 0xa8,
0x4f, 0xea, 0x98, 0x03, 0x46, 0xaf, 0x54, 0x1c, 0x1a, 0x5a, 0x65, 0x0b,
0x65, 0x84, 0xcb, 0x96, 0x4b, 0x81, 0x3f, 0x3d, 0x4a, 0xf6, 0xfe, 0xac,
0xa2, 0xd0, 0xb4, 0x3f, 0xe7, 0xef, 0x87, 0x00, 0x95, 0x60, 0xb7, 0x92,
0x4e, 0x44, 0x11, 0x0b, 0xb6, 0xdc, 0x7c, 0x7e, 0x14, 0xa4, 0x59, 0x2d,
0x24, 0xe7, 0x00, 0x72, 0x2b, 0x35, 0xd3, 0xd2, 0x06, 0xfe, 0xc7, 0x61,
0x65, 0xfd, 0xa3, 0xe5, 0x7a, 0xed, 0xfd, 0x13, 0x2f, 0x32, 0x4f, 0xa4,
0x0c, 0x51, 0x40, 0xf4, 0xc5, 0x89, 0x46, 0x79, 0x2c, 0xdb, 0xb8, 0x19,
0xa3, 0x49, 0x4e, 0x31, 0xd2, 0x09, 0xe8, 0x63, 0x01, 0xdb, 0x7d, 0x43,
0x54, 0xaa, 0x1e, 0xb3};
CheckBlob(blob, key_source, fixed_bytes_blob, blob_str);
}
// This is a validity check that AES-CTR-256 encryption encrypts and returns the
// same message.
TEST(CryptoLibTest, SimpleAesCtrEncryption) {
std::string message = "ENCRYPT ME";
brillo::SecureBlob key(32, 'A');
brillo::SecureBlob iv(16, 'B');
brillo::SecureBlob ciphertext;
EXPECT_TRUE(CryptoLib::AesEncryptSpecifyBlockMode(
brillo::SecureBlob(message.begin(), message.end()), 0, message.size(),
key, iv, CryptoLib::kPaddingStandard, CryptoLib::kCtr, &ciphertext));
brillo::SecureBlob decrypted;
EXPECT_TRUE(CryptoLib::AesDecryptSpecifyBlockMode(
ciphertext, 0, ciphertext.size(), key, iv, CryptoLib::kPaddingStandard,
CryptoLib::kCtr, &decrypted));
std::string decrypted_str(decrypted.begin(), decrypted.end());
EXPECT_EQ(message, decrypted_str);
}
// Known test vectors for AES-256-CTR from
// https://boringssl.googlesource.com/boringssl/+/2490/crypto/cipher/test/cipher_test.txt#180
TEST(CryptoLibTest, AesCTRKnownVector1) {
brillo::SecureBlob key = {
0x77, 0x6B, 0xEF, 0xF2, 0x85, 0x1D, 0xB0, 0x6F, 0x4C, 0x8A, 0x05,
0x42, 0xC8, 0x69, 0x6F, 0x6C, 0x6A, 0x81, 0xAF, 0x1E, 0xEC, 0x96,
0xB4, 0xD3, 0x7F, 0xC1, 0xD6, 0x89, 0xE6, 0xC1, 0xC1, 0x04,
};
brillo::SecureBlob iv = {
0x00, 0x00, 0x00, 0x60, 0xDB, 0x56, 0x72, 0xC9,
0x7A, 0xA8, 0xF0, 0xB2, 0x00, 0x00, 0x00, 0x01,
};
brillo::SecureBlob plaintext = {
0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67,
};
brillo::SecureBlob expected_ciphertext = {
0x14, 0x5A, 0xD0, 0x1D, 0xBF, 0x82, 0x4E, 0xC7,
0x56, 0x08, 0x63, 0xDC, 0x71, 0xE3, 0xE0, 0xC0,
};
brillo::SecureBlob ciphertext;
EXPECT_TRUE(CryptoLib::AesEncryptSpecifyBlockMode(
plaintext, 0, plaintext.size(), key, iv, CryptoLib::kPaddingStandard,
CryptoLib::kCtr, &ciphertext));
EXPECT_EQ(expected_ciphertext, ciphertext);
brillo::SecureBlob resulting_plaintext;
EXPECT_TRUE(CryptoLib::AesDecryptSpecifyBlockMode(
expected_ciphertext, 0, expected_ciphertext.size(), key, iv,
CryptoLib::kPaddingStandard, CryptoLib::kCtr, &resulting_plaintext));
EXPECT_EQ(plaintext, resulting_plaintext);
}
TEST(CryptoLibTest, AesCTRKnownVector2) {
brillo::SecureBlob key = {
0xF6, 0xD6, 0x6D, 0x6B, 0xD5, 0x2D, 0x59, 0xBB, 0x07, 0x96, 0x36,
0x58, 0x79, 0xEF, 0xF8, 0x86, 0xC6, 0x6D, 0xD5, 0x1A, 0x5B, 0x6A,
0x99, 0x74, 0x4B, 0x50, 0x59, 0x0C, 0x87, 0xA2, 0x38, 0x84,
};
brillo::SecureBlob iv = {
0x00, 0xFA, 0xAC, 0x24, 0xC1, 0x58, 0x5E, 0xF1,
0x5A, 0x43, 0xD8, 0x75, 0x00, 0x00, 0x00, 0x01,
};
brillo::SecureBlob plaintext = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
};
brillo::SecureBlob expected_ciphertext = {
0xF0, 0x5E, 0x23, 0x1B, 0x38, 0x94, 0x61, 0x2C, 0x49, 0xEE, 0x00,
0x0B, 0x80, 0x4E, 0xB2, 0xA9, 0xB8, 0x30, 0x6B, 0x50, 0x8F, 0x83,
0x9D, 0x6A, 0x55, 0x30, 0x83, 0x1D, 0x93, 0x44, 0xAF, 0x1C,
};
brillo::SecureBlob ciphertext;
EXPECT_TRUE(CryptoLib::AesEncryptSpecifyBlockMode(
plaintext, 0, plaintext.size(), key, iv, CryptoLib::kPaddingStandard,
CryptoLib::kCtr, &ciphertext));
EXPECT_EQ(expected_ciphertext, ciphertext);
brillo::SecureBlob resulting_plaintext;
EXPECT_TRUE(CryptoLib::AesDecryptSpecifyBlockMode(
expected_ciphertext, 0, expected_ciphertext.size(), key, iv,
CryptoLib::kPaddingStandard, CryptoLib::kCtr, &resulting_plaintext));
EXPECT_EQ(plaintext, resulting_plaintext);
}
TEST(CryptoLibTest, AesCTRKnownVector3) {
brillo::SecureBlob key = {
0xFF, 0x7A, 0x61, 0x7C, 0xE6, 0x91, 0x48, 0xE4, 0xF1, 0x72, 0x6E,
0x2F, 0x43, 0x58, 0x1D, 0xE2, 0xAA, 0x62, 0xD9, 0xF8, 0x05, 0x53,
0x2E, 0xDF, 0xF1, 0xEE, 0xD6, 0x87, 0xFB, 0x54, 0x15, 0x3D,
};
brillo::SecureBlob iv = {
0x00, 0x1C, 0xC5, 0xB7, 0x51, 0xA5, 0x1D, 0x70,
0xA1, 0xC1, 0x11, 0x48, 0x00, 0x00, 0x00, 0x01,
};
brillo::SecureBlob plaintext = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
};
brillo::SecureBlob expected_ciphertext = {
0xEB, 0x6C, 0x52, 0x82, 0x1D, 0x0B, 0xBB, 0xF7, 0xCE, 0x75, 0x94, 0x46,
0x2A, 0xCA, 0x4F, 0xAA, 0xB4, 0x07, 0xDF, 0x86, 0x65, 0x69, 0xFD, 0x07,
0xF4, 0x8C, 0xC0, 0xB5, 0x83, 0xD6, 0x07, 0x1F, 0x1E, 0xC0, 0xE6, 0xB8,
};
brillo::SecureBlob ciphertext;
EXPECT_TRUE(CryptoLib::AesEncryptSpecifyBlockMode(
plaintext, 0, plaintext.size(), key, iv, CryptoLib::kPaddingStandard,
CryptoLib::kCtr, &ciphertext));
EXPECT_EQ(expected_ciphertext, ciphertext);
brillo::SecureBlob resulting_plaintext;
EXPECT_TRUE(CryptoLib::AesDecryptSpecifyBlockMode(
expected_ciphertext, 0, expected_ciphertext.size(), key, iv,
CryptoLib::kPaddingStandard, CryptoLib::kCtr, &resulting_plaintext));
EXPECT_EQ(plaintext, resulting_plaintext);
}
} // namespace cryptohome