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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
// Copyright 2014 The Chromium Authors
// 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 <array>
#include "base/containers/span.h"
#include "components/webcrypto/algorithm_dispatch.h"
#include "components/webcrypto/algorithms/test_helpers.h"
#include "components/webcrypto/status.h"
#include "third_party/blink/public/platform/web_crypto_algorithm_params.h"
#include "third_party/blink/public/platform/web_crypto_key_algorithm.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::kWebCryptoAlgorithmIdAesCtr,
new blink::WebCryptoAesCtrParams(length_bits, counter));
}
blink::WebCryptoKey AesCtrKeyFromBytes(const std::vector<uint8_t>& bytes) {
blink::WebCryptoKey key = ImportSecretKeyFromRaw(
bytes, CreateAlgorithm(blink::kWebCryptoAlgorithmIdAesCtr),
blink::kWebCryptoKeyUsageEncrypt | blink::kWebCryptoKeyUsageDecrypt);
EXPECT_EQ(bytes.size() * 8, key.Algorithm().AesParams()->LengthBits());
return key;
}
class WebCryptoAesCtrTest : public WebCryptoTestBase {};
struct AesCtrKnownAnswer {
const char* key;
const char* plaintext;
const char* counter;
size_t counter_length;
const char* ciphertext;
};
const char k128BitTestKey[] = "7691BE035E5020A8AC6E618529F9A0DC";
const char k256BitTestKey[] =
"F6D66D6BD52D59BB0796365879EFF886C66DD51A5B6A99744B50590C87A23884";
constexpr auto kAesCtrKnownAnswers = std::to_array<AesCtrKnownAnswer>({
// RFC 3686 test vector #3:
{k128BitTestKey,
"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223",
"00E0017B27777F3F4A1786F000000001", 32,
"C1CF48A89F2FFDD9CF4652E9EFDB72D74540A42BDE6D7836D59A5CEAAEF3105325B2072"
"F"},
// RFC 3686 test vector #8:
{k256BitTestKey,
"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F",
"00FAAC24C1585EF15A43D87500000001", 32,
"F05E231B3894612C49EE000B804EB2A9B8306B508F839D6A5530831D9344AF1C"},
// Empty plaintext, same key as above:
{k256BitTestKey, "", "00FAAC24C1585EF15A43D87500000001", 32, ""},
// 32-bit counter wraparound:
{k256BitTestKey,
"F05E231B3894612C49EE000B804EB2A9B8306B508F839D6A5530831D9344AF1CC1CF48A89"
"F2FFDD9CF4652E9EFDB72D7",
"00FAAC24C1585EF15A43D875FFFFFFFF", 32,
"2E32E02FF9E69A1D6B78AC4308A67592C5DD5505589B79183D4189619A1467E4319069B0A"
"3BE9AF28EA158E96398CE71"},
// 1-bit counter wraparound:
{k128BitTestKey,
"C05E231B3894612C49EE000B804EB2A6B8306B508F839D6A5530831D9344AF1C",
"00FAAC24C1585EF15A43D875000000FF", 1,
"52334727723A84F4278FB319386CD7B5587DD8B2D9AA394D83EF8A826C4761AA"},
// 4-bit counter wraparound:
{k128BitTestKey,
"C05E231B3894612C49EE000B804EB2A6B8306B508F839D6A5530831D9344AF1C141516171"
"8191A1B1C1D1E1F20212223",
"00FAAC24C1585EF15A43D8750000111E", 4,
"5573894046DEF46162ED54966A22D8F0517B61A0CE7E657A5A5124A7F62AAE149A3C78567"
"11C59D67F34F31374CF7A72"},
// same, but plaintext is not a multiple of block size:
{k128BitTestKey,
"C05E231B3894612C49EE000B804EB2A6B8306B508F839D6A5530831D9344AF1C141516171"
"8191A1B1C1D1E1F20",
"00FAAC24C1585EF15A43D8750000111E", 4,
"5573894046DEF46162ED54966A22D8F0517B61A0CE7E657A5A5124A7F62AAE149A3C78567"
"11C59D67F34F31374"},
// 128-bit counter wraparound:
{k128BitTestKey,
"C05E231B3894612C49EE000B804EB2A6B8306B508F839D6A5530831D9344AF1C141516171"
"8191A1B1C1D1E1F20212223",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE", 128,
"D2C49B275BC73814DC90ECE98959041C9A3481F2247E08B0AF5D8DE3F521C9DAF535B0A81"
"56DF9D2370EE7328103C8AD"},
});
TEST_F(WebCryptoAesCtrTest, EncryptDecryptKnownAnswer) {
for (const auto& test : kAesCtrKnownAnswers) {
SCOPED_TRACE(&test - &kAesCtrKnownAnswers[0]);
std::vector<uint8_t> key_bytes = HexStringToBytes(test.key);
std::vector<uint8_t> counter = HexStringToBytes(test.counter);
std::vector<uint8_t> plaintext = HexStringToBytes(test.plaintext);
std::vector<uint8_t> ciphertext = HexStringToBytes(test.ciphertext);
blink::WebCryptoKey key = AesCtrKeyFromBytes(key_bytes);
std::vector<uint8_t> output;
// Test encryption.
EXPECT_EQ(Status::Success(),
Encrypt(CreateAesCtrAlgorithm(counter, test.counter_length), key,
plaintext, &output));
EXPECT_EQ(ciphertext, output);
// Test decryption.
EXPECT_EQ(Status::Success(),
Decrypt(CreateAesCtrAlgorithm(counter, test.counter_length), key,
ciphertext, &output));
EXPECT_EQ(plaintext, output);
}
}
// The counter block must be exactly 16 bytes.
TEST_F(WebCryptoAesCtrTest, InvalidCounterBlockLength) {
blink::WebCryptoKey key = AesCtrKeyFromBytes(std::vector<uint8_t>(16));
std::vector<uint8_t> input(32);
std::vector<uint8_t> output;
for (size_t bad_length : {0, 15, 17}) {
std::vector<uint8_t> bad_counter(bad_length);
EXPECT_EQ(
Status::ErrorIncorrectSizeAesCtrCounter(),
Encrypt(CreateAesCtrAlgorithm(bad_counter, 128), key, input, &output));
EXPECT_EQ(
Status::ErrorIncorrectSizeAesCtrCounter(),
Decrypt(CreateAesCtrAlgorithm(bad_counter, 128), key, input, &output));
}
}
TEST_F(WebCryptoAesCtrTest, InvalidCounterLength) {
blink::WebCryptoKey key = AesCtrKeyFromBytes(std::vector<uint8_t>(16));
std::vector<uint8_t> counter(16);
std::vector<uint8_t> input(32);
std::vector<uint8_t> output;
// The counter length cannot be less than 1 or greater than 128.
for (uint8_t bad_length : {0, 129}) {
EXPECT_EQ(Status::ErrorInvalidAesCtrCounterLength(),
Encrypt(CreateAesCtrAlgorithm(counter, bad_length), key, input,
&output));
EXPECT_EQ(Status::ErrorInvalidAesCtrCounterLength(),
Decrypt(CreateAesCtrAlgorithm(counter, bad_length), key, 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;
blink::WebCryptoKey key = AesCtrKeyFromBytes(std::vector<uint8_t>(16));
std::vector<uint8_t> buffer(272);
// 16 and 17 AES blocks worth of data respectively (AES blocks are 16 bytes
// long).
auto input_16 = base::span(buffer).first<256>();
auto input_17 = base::span(buffer).first<272>();
std::vector<uint8_t> output;
for (uint8_t start : {0, 1, 15}) {
std::vector<uint8_t> counter(16);
counter[15] = start;
// 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
|