1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
// 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 <stddef.h>
#include <stdint.h>
#include "base/macros.h"
#include "base/values.h"
#include "components/webcrypto/algorithm_dispatch.h"
#include "components/webcrypto/algorithms/test_helpers.h"
#include "components/webcrypto/crypto_data.h"
#include "components/webcrypto/status.h"
#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
#include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
namespace webcrypto {
namespace {
// 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, counter));
}
class WebCryptoAesCtrTest : public WebCryptoTestBase {};
TEST_F(WebCryptoAesCtrTest, EncryptDecryptKnownAnswer) {
std::unique_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_F(WebCryptoAesCtrTest, InvalidCounterBlockLength) {
const unsigned int kBadCounterBlockLengthBytes[] = {0, 15, 17};
blink::WebCryptoKey key = ImportSecretKeyFromRaw(
std::vector<uint8_t>(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_F(WebCryptoAesCtrTest, InvalidCounterLength) {
const uint8_t kBadCounterLengthBits[] = {0, 129};
blink::WebCryptoKey key = ImportSecretKeyFromRaw(
std::vector<uint8_t>(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_F(WebCryptoAesCtrTest, OverflowAndRepeatCounter) {
const uint8_t kCounterLengthBits = 4;
const uint8_t kStartCounter[] = {0, 1, 15};
blink::WebCryptoKey key = ImportSecretKeyFromRaw(
std::vector<uint8_t>(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(buffer.data(), 256);
CryptoData input_17(buffer.data(), 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
|