Commit 4d7a0e04 authored by eroman's avatar eroman Committed by Commit bot

[webcrypto] Implement AES-CTR using BoringSSL.

BUG=399084

Review URL: https://codereview.chromium.org/491763002

Cr-Commit-Position: refs/heads/master@{#292053}
parent 28c59a24
......@@ -21,6 +21,7 @@ class AlgorithmRegistry {
: sha_(CreatePlatformShaImplementation()),
aes_gcm_(CreatePlatformAesGcmImplementation()),
aes_cbc_(CreatePlatformAesCbcImplementation()),
aes_ctr_(CreatePlatformAesCtrImplementation()),
aes_kw_(CreatePlatformAesKwImplementation()),
hmac_(CreatePlatformHmacImplementation()),
rsa_ssa_(CreatePlatformRsaSsaImplementation()),
......@@ -40,6 +41,8 @@ class AlgorithmRegistry {
return aes_gcm_.get();
case blink::WebCryptoAlgorithmIdAesCbc:
return aes_cbc_.get();
case blink::WebCryptoAlgorithmIdAesCtr:
return aes_ctr_.get();
case blink::WebCryptoAlgorithmIdAesKw:
return aes_kw_.get();
case blink::WebCryptoAlgorithmIdHmac:
......@@ -57,6 +60,7 @@ class AlgorithmRegistry {
scoped_ptr<AlgorithmImplementation> sha_;
scoped_ptr<AlgorithmImplementation> aes_gcm_;
scoped_ptr<AlgorithmImplementation> aes_cbc_;
scoped_ptr<AlgorithmImplementation> aes_ctr_;
scoped_ptr<AlgorithmImplementation> aes_kw_;
scoped_ptr<AlgorithmImplementation> hmac_;
scoped_ptr<AlgorithmImplementation> rsa_ssa_;
......
......@@ -6,6 +6,7 @@
#include "base/lazy_instance.h"
#include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/platform_crypto.h"
#include "crypto/nss_util.h"
#include "crypto/scoped_nss_types.h"
......@@ -79,6 +80,11 @@ void PlatformInit() {
crypto::EnsureNSSInit();
}
AlgorithmImplementation* CreatePlatformAesCtrImplementation() {
// TODO(eroman): http://crbug.com/399084
return NULL;
}
} // namespace webcrypto
} // namespace content
......@@ -11,6 +11,7 @@
#include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/openssl/aes_key_openssl.h"
#include "content/child/webcrypto/openssl/key_openssl.h"
#include "content/child/webcrypto/openssl/util_openssl.h"
#include "content/child/webcrypto/status.h"
#include "content/child/webcrypto/webcrypto_util.h"
#include "crypto/openssl_util.h"
......@@ -35,10 +36,7 @@ const EVP_CIPHER* GetAESCipherByKeyLength(unsigned int key_length_bytes) {
}
}
// OpenSSL constants for EVP_CipherInit_ex(), do not change
enum CipherOperation { kDoDecrypt = 0, kDoEncrypt = 1 };
Status AesCbcEncryptDecrypt(CipherOperation cipher_operation,
Status AesCbcEncryptDecrypt(EncryptOrDecrypt cipher_operation,
const blink::WebCryptoAlgorithm& algorithm,
const blink::WebCryptoKey& key,
const CryptoData& data,
......@@ -119,14 +117,14 @@ class AesCbcImplementation : public AesAlgorithm {
const blink::WebCryptoKey& key,
const CryptoData& data,
std::vector<uint8_t>* buffer) const OVERRIDE {
return AesCbcEncryptDecrypt(kDoEncrypt, algorithm, key, data, buffer);
return AesCbcEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer);
}
virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
const blink::WebCryptoKey& key,
const CryptoData& data,
std::vector<uint8_t>* buffer) const OVERRIDE {
return AesCbcEncryptDecrypt(kDoDecrypt, algorithm, key, data, buffer);
return AesCbcEncryptDecrypt(DECRYPT, algorithm, key, data, buffer);
}
};
......
This diff is collapsed.
......@@ -19,7 +19,9 @@ namespace webcrypto {
class CryptoData;
class Status;
enum EncryptOrDecrypt { ENCRYPT, DECRYPT };
// The values of these constants correspond with the "enc" parameter of
// EVP_CipherInit_ex(), do not change.
enum EncryptOrDecrypt { DECRYPT=0, ENCRYPT=1 };
const EVP_MD* GetDigest(blink::WebCryptoAlgorithmId id);
......
......@@ -25,6 +25,7 @@ scoped_ptr<blink::WebCryptoDigestor> CreatePlatformDigestor(
AlgorithmImplementation* CreatePlatformShaImplementation();
AlgorithmImplementation* CreatePlatformAesCbcImplementation();
AlgorithmImplementation* CreatePlatformAesCtrImplementation();
AlgorithmImplementation* CreatePlatformAesGcmImplementation();
AlgorithmImplementation* CreatePlatformAesKwImplementation();
AlgorithmImplementation* CreatePlatformHmacImplementation();
......
......@@ -150,6 +150,21 @@ Status Status::ErrorIncorrectSizeAesCbcIv() {
"The \"iv\" has an unexpected length -- must be 16 bytes");
}
Status Status::ErrorIncorrectSizeAesCtrCounter() {
return Status(blink::WebCryptoErrorTypeData,
"The \"counter\" has an unexpected length -- must be 16 bytes");
}
Status Status::ErrorInvalidAesCtrCounterLength() {
return Status(blink::WebCryptoErrorTypeData,
"The \"length\" property must be >= 1 and <= 128");
}
Status Status::ErrorAesCtrInputTooLongCounterRepeated() {
return Status(blink::WebCryptoErrorTypeData,
"The input is too large for the counter length.");
}
Status Status::ErrorDataTooLarge() {
return Status(blink::WebCryptoErrorTypeData,
"The provided data is too large");
......
......@@ -144,6 +144,18 @@ class CONTENT_EXPORT Status {
// bytes.
static Status ErrorIncorrectSizeAesCbcIv();
// When doing AES-CTR encryption/decryption, the "counter" parameter was not
// 16 bytes.
static Status ErrorIncorrectSizeAesCtrCounter();
// When doing AES-CTR encryption/decryption, the "length" parameter for the
// counter was out of range.
static Status ErrorInvalidAesCtrCounterLength();
// The input to encrypt/decrypt was too large. Based on the counter size, it
// would cause the counter to wraparound and repeat earlier values.
static Status ErrorAesCtrInputTooLongCounterRepeated();
// The data provided to an encrypt/decrypt/sign/verify operation was too
// large. This can either represent an internal limitation (for instance
// representing buffer lengths as uints).
......
// Copyright 2014 The Chromium 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 "base/stl_util.h"
#include "content/child/webcrypto/algorithm_dispatch.h"
#include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/status.h"
#include "content/child/webcrypto/test/test_helpers.h"
#include "content/child/webcrypto/webcrypto_util.h"
#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
#include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
namespace content {
namespace webcrypto {
namespace {
bool SupportsAesCtr() {
#if defined(USE_OPENSSL)
return true;
#else
return false;
#endif
}
// Creates an AES-CTR algorithm for encryption/decryption.
blink::WebCryptoAlgorithm CreateAesCtrAlgorithm(
const std::vector<uint8_t>& counter,
uint8_t length_bits) {
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
blink::WebCryptoAlgorithmIdAesCtr,
new blink::WebCryptoAesCtrParams(
length_bits, vector_as_array(&counter), counter.size()));
}
TEST(WebCryptoAesCtrTest, EncryptDecryptKnownAnswer) {
if (!SupportsAesCtr()) {
LOG(WARNING) << "Skipping test because AES-CTR is not supported";
return;
}
scoped_ptr<base::ListValue> tests;
ASSERT_TRUE(ReadJsonTestFileToList("aes_ctr.json", &tests));
for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
SCOPED_TRACE(test_index);
base::DictionaryValue* test;
ASSERT_TRUE(tests->GetDictionary(test_index, &test));
std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key");
std::vector<uint8_t> test_counter = GetBytesFromHexString(test, "counter");
int counter_length_bits = 0;
ASSERT_TRUE(test->GetInteger("length", &counter_length_bits));
std::vector<uint8_t> test_plain_text =
GetBytesFromHexString(test, "plain_text");
std::vector<uint8_t> test_cipher_text =
GetBytesFromHexString(test, "cipher_text");
blink::WebCryptoKey key = ImportSecretKeyFromRaw(
test_key,
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
EXPECT_EQ(test_key.size() * 8, key.algorithm().aesParams()->lengthBits());
std::vector<uint8_t> output;
// Test encryption.
EXPECT_EQ(Status::Success(),
Encrypt(CreateAesCtrAlgorithm(test_counter, counter_length_bits),
key,
CryptoData(test_plain_text),
&output));
EXPECT_BYTES_EQ(test_cipher_text, output);
// Test decryption.
EXPECT_EQ(Status::Success(),
Decrypt(CreateAesCtrAlgorithm(test_counter, counter_length_bits),
key,
CryptoData(test_cipher_text),
&output));
EXPECT_BYTES_EQ(test_plain_text, output);
}
}
// The counter block must be exactly 16 bytes.
TEST(WebCryptoAesCtrTest, InvalidCounterBlockLength) {
if (!SupportsAesCtr()) {
LOG(WARNING) << "Skipping test because AES-CTR is not supported";
return;
}
const unsigned int kBadCounterBlockLengthBytes[] = {0, 15, 17};
blink::WebCryptoKey key = ImportSecretKeyFromRaw(
std::vector<uint8>(16), // 128-bit key of all zeros.
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
std::vector<uint8_t> input(32);
std::vector<uint8_t> output;
for (size_t i = 0; i < arraysize(kBadCounterBlockLengthBytes); ++i) {
std::vector<uint8_t> bad_counter(kBadCounterBlockLengthBytes[i]);
EXPECT_EQ(Status::ErrorIncorrectSizeAesCtrCounter(),
Encrypt(CreateAesCtrAlgorithm(bad_counter, 128),
key,
CryptoData(input),
&output));
EXPECT_EQ(Status::ErrorIncorrectSizeAesCtrCounter(),
Decrypt(CreateAesCtrAlgorithm(bad_counter, 128),
key,
CryptoData(input),
&output));
}
}
// The counter length cannot be less than 1 or greater than 128.
TEST(WebCryptoAesCtrTest, InvalidCounterLength) {
if (!SupportsAesCtr()) {
LOG(WARNING) << "Skipping test because AES-CTR is not supported";
return;
}
const uint8_t kBadCounterLengthBits[] = {0, 129};
blink::WebCryptoKey key = ImportSecretKeyFromRaw(
std::vector<uint8>(16), // 128-bit key of all zeros.
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
std::vector<uint8_t> counter(16);
std::vector<uint8_t> input(32);
std::vector<uint8_t> output;
for (size_t i = 0; i < arraysize(kBadCounterLengthBits); ++i) {
uint8_t bad_counter_length_bits = kBadCounterLengthBits[i];
EXPECT_EQ(Status::ErrorInvalidAesCtrCounterLength(),
Encrypt(CreateAesCtrAlgorithm(counter, bad_counter_length_bits),
key,
CryptoData(input),
&output));
EXPECT_EQ(Status::ErrorInvalidAesCtrCounterLength(),
Decrypt(CreateAesCtrAlgorithm(counter, bad_counter_length_bits),
key,
CryptoData(input),
&output));
}
}
// Tests wrap-around using a 4-bit counter.
//
// Wrap-around is allowed, however if the counter repeats itself an error should
// be thrown.
//
// Using a 4-bit counter it is possible to encrypt 16 blocks. However the 17th
// block would end up wrapping back to the starting value.
TEST(WebCryptoAesCtrTest, OverflowAndRepeatCounter) {
if (!SupportsAesCtr()) {
LOG(WARNING) << "Skipping test because AES-CTR is not supported";
return;
}
const uint8_t kCounterLengthBits = 4;
const uint8_t kStartCounter[] = {0, 1, 15};
blink::WebCryptoKey key = ImportSecretKeyFromRaw(
std::vector<uint8>(16), // 128-bit key of all zeros.
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr),
blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
std::vector<uint8_t> buffer(272);
// 16 and 17 AES blocks worth of data respectively (AES blocks are 16 bytes
// long).
CryptoData input_16(vector_as_array(&buffer), 256);
CryptoData input_17(vector_as_array(&buffer), 272);
std::vector<uint8_t> output;
for (size_t i = 0; i < arraysize(kStartCounter); ++i) {
std::vector<uint8_t> counter(16);
counter[15] = kStartCounter[i];
// Baseline test: Encrypting 16 blocks should work (don't bother to check
// output, the known answer tests already do that).
EXPECT_EQ(Status::Success(),
Encrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits),
key,
input_16,
&output));
// Encrypting/Decrypting 17 however should fail.
EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(),
Encrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits),
key,
input_17,
&output));
EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(),
Decrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits),
key,
input_17,
&output));
}
}
} // namespace
} // namespace webcrypto
} // namespace content
......@@ -268,6 +268,7 @@
],
'webcrypto_openssl_sources': [
'child/webcrypto/openssl/aes_cbc_openssl.cc',
'child/webcrypto/openssl/aes_ctr_openssl.cc',
'child/webcrypto/openssl/aes_gcm_openssl.cc',
'child/webcrypto/openssl/aes_key_openssl.cc',
'child/webcrypto/openssl/aes_key_openssl.h',
......
......@@ -642,9 +642,10 @@
'child/touch_fling_gesture_curve_unittest.cc',
'child/web_url_loader_impl_unittest.cc',
'child/webcrypto/test/aes_cbc_unittest.cc',
'child/webcrypto/test/aes_ctr_unittest.cc',
'child/webcrypto/test/aes_gcm_unittest.cc',
'child/webcrypto/test/status_unittest.cc',
'child/webcrypto/test/aes_kw_unittest.cc',
'child/webcrypto/test/status_unittest.cc',
'child/webcrypto/test/hmac_unittest.cc',
'child/webcrypto/test/rsa_oaep_unittest.cc',
'child/webcrypto/test/rsa_ssa_unittest.cc',
......
[
// From https://www.rfc-editor.org/rfc/rfc3686.txt
// Test Vector #3: Encrypting 36 octets using AES-CTR with 128-bit key
{
"key": "7691BE035E5020A8AC6E618529F9A0DC",
"plain_text": "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223",
"counter": "00E0017B27777F3F4A1786F000000001",
"length": 32,
"cipher_text": "C1CF48A89F2FFDD9CF4652E9EFDB72D74540A42BDE6D7836D59A5CEAAEF3105325B2072F"
},
// From https://www.rfc-editor.org/rfc/rfc3686.txt
// Test Vector #8: Encrypting 32 octets using AES-CTR with 256-bit key
{
"key": "F6D66D6BD52D59BB0796365879EFF886C66DD51A5B6A99744B50590C87A23884",
"plain_text": "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F",
"counter": "00FAAC24C1585EF15A43D87500000001",
"length": 32,
"cipher_text": "F05E231B3894612C49EE000B804EB2A9B8306B508F839D6A5530831D9344AF1C"
},
// Empty plaintext, using a 256-bit key.
{
"key": "F6D66D6BD52D59BB0796365879EFF886C66DD51A5B6A99744B50590C87A23884",
"plain_text": "",
"counter": "00FAAC24C1585EF15A43D87500000001",
"length": 32,
"cipher_text": ""
},
// 32-bit counter wrap-around (manually constructed).
//
// Starts a 32-bit counter at 0xFFFFFFFF and encrypts 3 blocks worth of data.
// The counter will wrap around and take on values of 0 and 1.
{
"key": "F6D66D6BD52D59BB0796365879EFF886C66DD51A5B6A99744B50590C87A23884",
"plain_text": "F05E231B3894612C49EE000B804EB2A9B8306B508F839D6A5530831D9344AF1CC1CF48A89F2FFDD9CF4652E9EFDB72D7",
"counter": "00FAAC24C1585EF15A43D875FFFFFFFF",
"length": 32,
"cipher_text": "2E32E02FF9E69A1D6B78AC4308A67592C5DD5505589B79183D4189619A1467E4319069B0A3BE9AF28EA158E96398CE71"
},
// 1-bit counter wrap-around (manually constructed).
//
// Starts a 1-bit counter at 1 and encrypts 2 blocks worth of data.
{
"key": "7691BE035E5020A8AC6E618529F9A0DC",
"plain_text": "C05E231B3894612C49EE000B804EB2A6B8306B508F839D6A5530831D9344AF1C",
"counter": "00FAAC24C1585EF15A43D875000000FF",
"length": 1,
"cipher_text": "52334727723A84F4278FB319386CD7B5587DD8B2D9AA394D83EF8A826C4761AA"
},
// 4-bit counter wrap-around (manually constructed).
//
// Starts a 4-bit counter at 14 and encrypts 3 blocks worth of data.
{
"key": "7691BE035E5020A8AC6E618529F9A0DC",
"plain_text": "C05E231B3894612C49EE000B804EB2A6B8306B508F839D6A5530831D9344AF1C1415161718191A1B1C1D1E1F20212223",
"counter": "00FAAC24C1585EF15A43D8750000111E",
"length": 4,
"cipher_text": "5573894046DEF46162ED54966A22D8F0517B61A0CE7E657A5A5124A7F62AAE149A3C7856711C59D67F34F31374CF7A72"
},
// Same test as above, however the plaintext/ciphertext is not a multiple of block size.
{
"key": "7691BE035E5020A8AC6E618529F9A0DC",
"plain_text": "C05E231B3894612C49EE000B804EB2A6B8306B508F839D6A5530831D9344AF1C1415161718191A1B1C1D1E1F20",
"counter": "00FAAC24C1585EF15A43D8750000111E",
"length": 4,
"cipher_text": "5573894046DEF46162ED54966A22D8F0517B61A0CE7E657A5A5124A7F62AAE149A3C7856711C59D67F34F31374"
},
// 128-bit counter wrap-around (manually constructed).
{
"key": "7691BE035E5020A8AC6E618529F9A0DC",
"plain_text": "C05E231B3894612C49EE000B804EB2A6B8306B508F839D6A5530831D9344AF1C1415161718191A1B1C1D1E1F20212223",
"counter": "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE",
"length": 128,
"cipher_text": "D2C49B275BC73814DC90ECE98959041C9A3481F2247E08B0AF5D8DE3F521C9DAF535B0A8156DF9D2370EE7328103C8AD"
}
]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment