Skip to content

Commit a534bab

Browse files
author
davidben@chromium.org
committed
Desupport AES-192 in crypto::SymmetricKey.
BoringSSL does not support AES-192. No current consumer uses AES-192, so remove the test which asserts it works. This fixes crypto_unittests in the Mac OpenSSL port. Blacklist AES-192 in the NSS implementation so that we do not accidentally grow a new dependency on it. BUG=338885 Review URL: https://codereview.chromium.org/420883003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285678 0039d316-1c4b-4281-b951-d872f2087c98
1 parent df94963 commit a534bab

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

crypto/encryptor_unittest.cc

-26
Original file line numberDiff line numberDiff line change
@@ -456,32 +456,6 @@ TEST(EncryptorTest, EncryptAES128CBCRegression) {
456456
EXPECT_EQ(plaintext, decrypted);
457457
}
458458

459-
// Expected output derived from the NSS implementation.
460-
TEST(EncryptorTest, EncryptAES192CBCRegression) {
461-
std::string key = "192bitsIsTwentyFourByte!";
462-
std::string iv = "Sweet Sixteen IV";
463-
std::string plaintext = "Small text";
464-
std::string expected_ciphertext_hex = "78DE5D7C2714FC5C61346C5416F6C89A";
465-
466-
scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
467-
crypto::SymmetricKey::AES, key));
468-
ASSERT_TRUE(sym_key.get());
469-
470-
crypto::Encryptor encryptor;
471-
// The IV must be exactly as long a the cipher block size.
472-
EXPECT_EQ(16U, iv.size());
473-
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
474-
475-
std::string ciphertext;
476-
EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
477-
EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(),
478-
ciphertext.size()));
479-
480-
std::string decrypted;
481-
EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted));
482-
EXPECT_EQ(plaintext, decrypted);
483-
}
484-
485459
// Not all platforms allow import/generation of symmetric keys with an
486460
// unsupported size.
487461
#if !defined(USE_NSS) && !defined(OS_WIN) && !defined(OS_MACOSX)

crypto/symmetric_key_nss.cc

+22-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm,
2020
DCHECK_EQ(AES, algorithm);
2121

2222
EnsureNSSInit();
23-
if (key_size_in_bits == 0)
23+
24+
// Whitelist supported key sizes to avoid accidentaly relying on
25+
// algorithms available in NSS but not BoringSSL and vice
26+
// versa. Note that BoringSSL does not support AES-192.
27+
if (key_size_in_bits != 128 && key_size_in_bits != 256)
2428
return NULL;
2529

2630
ScopedPK11Slot slot(PK11_GetInternalSlot());
@@ -45,6 +49,14 @@ SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
4549
if (salt.empty() || iterations == 0 || key_size_in_bits == 0)
4650
return NULL;
4751

52+
if (algorithm == AES) {
53+
// Whitelist supported key sizes to avoid accidentaly relying on
54+
// algorithms available in NSS but not BoringSSL and vice
55+
// versa. Note that BoringSSL does not support AES-192.
56+
if (key_size_in_bits != 128 && key_size_in_bits != 256)
57+
return NULL;
58+
}
59+
4860
SECItem password_item;
4961
password_item.type = siBuffer;
5062
password_item.data = reinterpret_cast<unsigned char*>(
@@ -84,6 +96,15 @@ SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
8496
SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
8597
const std::string& raw_key) {
8698
EnsureNSSInit();
99+
100+
if (algorithm == AES) {
101+
// Whitelist supported key sizes to avoid accidentaly relying on
102+
// algorithms available in NSS but not BoringSSL and vice
103+
// versa. Note that BoringSSL does not support AES-192.
104+
if (raw_key.size() != 128/8 && raw_key.size() != 256/8)
105+
return NULL;
106+
}
107+
87108
CK_MECHANISM_TYPE cipher =
88109
algorithm == AES ? CKM_AES_CBC : CKM_SHA_1_HMAC;
89110

crypto/symmetric_key_openssl.cc

+24
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ SymmetricKey::~SymmetricKey() {
2424
SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm,
2525
size_t key_size_in_bits) {
2626
DCHECK_EQ(AES, algorithm);
27+
28+
// Whitelist supported key sizes to avoid accidentaly relying on
29+
// algorithms available in NSS but not BoringSSL and vice
30+
// versa. Note that BoringSSL does not support AES-192.
31+
if (key_size_in_bits != 128 && key_size_in_bits != 256)
32+
return NULL;
33+
2734
size_t key_size_in_bytes = key_size_in_bits / 8;
2835
DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8);
2936

@@ -46,6 +53,15 @@ SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
4653
size_t iterations,
4754
size_t key_size_in_bits) {
4855
DCHECK(algorithm == AES || algorithm == HMAC_SHA1);
56+
57+
if (algorithm == AES) {
58+
// Whitelist supported key sizes to avoid accidentaly relying on
59+
// algorithms available in NSS but not BoringSSL and vice
60+
// versa. Note that BoringSSL does not support AES-192.
61+
if (key_size_in_bits != 128 && key_size_in_bits != 256)
62+
return NULL;
63+
}
64+
4965
size_t key_size_in_bytes = key_size_in_bits / 8;
5066
DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8);
5167

@@ -67,6 +83,14 @@ SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
6783
// static
6884
SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
6985
const std::string& raw_key) {
86+
if (algorithm == AES) {
87+
// Whitelist supported key sizes to avoid accidentaly relying on
88+
// algorithms available in NSS but not BoringSSL and vice
89+
// versa. Note that BoringSSL does not support AES-192.
90+
if (raw_key.size() != 128/8 && raw_key.size() != 256/8)
91+
return NULL;
92+
}
93+
7094
scoped_ptr<SymmetricKey> key(new SymmetricKey);
7195
key->key_ = raw_key;
7296
return key.release();

0 commit comments

Comments
 (0)