| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/stl_util.h" | |
| 6 #include "components/webcrypto/algorithm_dispatch.h" | |
| 7 #include "components/webcrypto/crypto_data.h" | |
| 8 #include "components/webcrypto/status.h" | |
| 9 #include "components/webcrypto/test/test_helpers.h" | |
| 10 #include "components/webcrypto/webcrypto_util.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
| 13 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | |
| 14 | |
| 15 namespace webcrypto { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Creates an AES-GCM algorithm. | |
| 20 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm( | |
| 21 const std::vector<uint8_t>& iv, | |
| 22 const std::vector<uint8_t>& additional_data, | |
| 23 unsigned int tag_length_bits) { | |
| 24 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | |
| 25 blink::WebCryptoAlgorithmIdAesGcm, | |
| 26 new blink::WebCryptoAesGcmParams( | |
| 27 vector_as_array(&iv), static_cast<unsigned int>(iv.size()), true, | |
| 28 vector_as_array(&additional_data), | |
| 29 static_cast<unsigned int>(additional_data.size()), true, | |
| 30 tag_length_bits)); | |
| 31 } | |
| 32 | |
| 33 blink::WebCryptoAlgorithm CreateAesGcmKeyGenAlgorithm( | |
| 34 unsigned short key_length_bits) { | |
| 35 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesGcm, | |
| 36 key_length_bits); | |
| 37 } | |
| 38 | |
| 39 Status AesGcmEncrypt(const blink::WebCryptoKey& key, | |
| 40 const std::vector<uint8_t>& iv, | |
| 41 const std::vector<uint8_t>& additional_data, | |
| 42 unsigned int tag_length_bits, | |
| 43 const std::vector<uint8_t>& plain_text, | |
| 44 std::vector<uint8_t>* cipher_text, | |
| 45 std::vector<uint8_t>* authentication_tag) { | |
| 46 blink::WebCryptoAlgorithm algorithm = | |
| 47 CreateAesGcmAlgorithm(iv, additional_data, tag_length_bits); | |
| 48 | |
| 49 std::vector<uint8_t> output; | |
| 50 Status status = Encrypt(algorithm, key, CryptoData(plain_text), &output); | |
| 51 if (status.IsError()) | |
| 52 return status; | |
| 53 | |
| 54 if ((tag_length_bits % 8) != 0) { | |
| 55 ADD_FAILURE() << "Encrypt should have failed."; | |
| 56 return Status::OperationError(); | |
| 57 } | |
| 58 | |
| 59 size_t tag_length_bytes = tag_length_bits / 8; | |
| 60 | |
| 61 if (tag_length_bytes > output.size()) { | |
| 62 ADD_FAILURE() << "tag length is larger than output"; | |
| 63 return Status::OperationError(); | |
| 64 } | |
| 65 | |
| 66 // The encryption result is cipher text with authentication tag appended. | |
| 67 cipher_text->assign(output.begin(), | |
| 68 output.begin() + (output.size() - tag_length_bytes)); | |
| 69 authentication_tag->assign(output.begin() + cipher_text->size(), | |
| 70 output.end()); | |
| 71 | |
| 72 return Status::Success(); | |
| 73 } | |
| 74 | |
| 75 Status AesGcmDecrypt(const blink::WebCryptoKey& key, | |
| 76 const std::vector<uint8_t>& iv, | |
| 77 const std::vector<uint8_t>& additional_data, | |
| 78 unsigned int tag_length_bits, | |
| 79 const std::vector<uint8_t>& cipher_text, | |
| 80 const std::vector<uint8_t>& authentication_tag, | |
| 81 std::vector<uint8_t>* plain_text) { | |
| 82 blink::WebCryptoAlgorithm algorithm = | |
| 83 CreateAesGcmAlgorithm(iv, additional_data, tag_length_bits); | |
| 84 | |
| 85 // Join cipher text and authentication tag. | |
| 86 std::vector<uint8_t> cipher_text_with_tag; | |
| 87 cipher_text_with_tag.reserve(cipher_text.size() + authentication_tag.size()); | |
| 88 cipher_text_with_tag.insert(cipher_text_with_tag.end(), cipher_text.begin(), | |
| 89 cipher_text.end()); | |
| 90 cipher_text_with_tag.insert(cipher_text_with_tag.end(), | |
| 91 authentication_tag.begin(), | |
| 92 authentication_tag.end()); | |
| 93 | |
| 94 return Decrypt(algorithm, key, CryptoData(cipher_text_with_tag), plain_text); | |
| 95 } | |
| 96 | |
| 97 TEST(WebCryptoAesGcmTest, GenerateKeyBadLength) { | |
| 98 const unsigned short kKeyLen[] = {0, 127, 257}; | |
| 99 blink::WebCryptoKey key; | |
| 100 for (size_t i = 0; i < arraysize(kKeyLen); ++i) { | |
| 101 SCOPED_TRACE(i); | |
| 102 EXPECT_EQ(Status::ErrorGenerateAesKeyLength(), | |
| 103 GenerateSecretKey(CreateAesGcmKeyGenAlgorithm(kKeyLen[i]), true, | |
| 104 blink::WebCryptoKeyUsageDecrypt, &key)); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 TEST(WebCryptoAesGcmTest, GenerateKeyEmptyUsage) { | |
| 109 blink::WebCryptoKey key; | |
| 110 EXPECT_EQ(Status::ErrorCreateKeyEmptyUsages(), | |
| 111 GenerateSecretKey(CreateAesGcmKeyGenAlgorithm(256), true, 0, &key)); | |
| 112 } | |
| 113 | |
| 114 TEST(WebCryptoAesGcmTest, ImportExportJwk) { | |
| 115 const blink::WebCryptoAlgorithm algorithm = | |
| 116 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm); | |
| 117 | |
| 118 // AES-GCM 128 | |
| 119 ImportExportJwkSymmetricKey( | |
| 120 128, algorithm, | |
| 121 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, | |
| 122 "A128GCM"); | |
| 123 | |
| 124 // AES-GCM 256 | |
| 125 ImportExportJwkSymmetricKey(256, algorithm, blink::WebCryptoKeyUsageDecrypt, | |
| 126 "A256GCM"); | |
| 127 } | |
| 128 | |
| 129 // TODO(eroman): | |
| 130 // * Test decryption when the tag length exceeds input size | |
| 131 // * Test decryption with empty input | |
| 132 // * Test decryption with tag length of 0. | |
| 133 TEST(WebCryptoAesGcmTest, SampleSets) { | |
| 134 scoped_ptr<base::ListValue> tests; | |
| 135 ASSERT_TRUE(ReadJsonTestFileToList("aes_gcm.json", &tests)); | |
| 136 | |
| 137 // Note that WebCrypto appends the authentication tag to the ciphertext. | |
| 138 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { | |
| 139 SCOPED_TRACE(test_index); | |
| 140 base::DictionaryValue* test; | |
| 141 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); | |
| 142 | |
| 143 const std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key"); | |
| 144 const std::vector<uint8_t> test_iv = GetBytesFromHexString(test, "iv"); | |
| 145 const std::vector<uint8_t> test_additional_data = | |
| 146 GetBytesFromHexString(test, "additional_data"); | |
| 147 const std::vector<uint8_t> test_plain_text = | |
| 148 GetBytesFromHexString(test, "plain_text"); | |
| 149 const std::vector<uint8_t> test_authentication_tag = | |
| 150 GetBytesFromHexString(test, "authentication_tag"); | |
| 151 const unsigned int test_tag_size_bits = | |
| 152 static_cast<unsigned int>(test_authentication_tag.size()) * 8; | |
| 153 const std::vector<uint8_t> test_cipher_text = | |
| 154 GetBytesFromHexString(test, "cipher_text"); | |
| 155 | |
| 156 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | |
| 157 test_key, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), | |
| 158 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); | |
| 159 | |
| 160 // Verify exported raw key is identical to the imported data | |
| 161 std::vector<uint8_t> raw_key; | |
| 162 EXPECT_EQ(Status::Success(), | |
| 163 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | |
| 164 | |
| 165 EXPECT_BYTES_EQ(test_key, raw_key); | |
| 166 | |
| 167 // Test encryption. | |
| 168 std::vector<uint8_t> cipher_text; | |
| 169 std::vector<uint8_t> authentication_tag; | |
| 170 EXPECT_EQ( | |
| 171 Status::Success(), | |
| 172 AesGcmEncrypt(key, test_iv, test_additional_data, test_tag_size_bits, | |
| 173 test_plain_text, &cipher_text, &authentication_tag)); | |
| 174 | |
| 175 EXPECT_BYTES_EQ(test_cipher_text, cipher_text); | |
| 176 EXPECT_BYTES_EQ(test_authentication_tag, authentication_tag); | |
| 177 | |
| 178 // Test decryption. | |
| 179 std::vector<uint8_t> plain_text; | |
| 180 EXPECT_EQ( | |
| 181 Status::Success(), | |
| 182 AesGcmDecrypt(key, test_iv, test_additional_data, test_tag_size_bits, | |
| 183 test_cipher_text, test_authentication_tag, &plain_text)); | |
| 184 EXPECT_BYTES_EQ(test_plain_text, plain_text); | |
| 185 | |
| 186 // Decryption should fail if any of the inputs are tampered with. | |
| 187 EXPECT_EQ(Status::OperationError(), | |
| 188 AesGcmDecrypt(key, Corrupted(test_iv), test_additional_data, | |
| 189 test_tag_size_bits, test_cipher_text, | |
| 190 test_authentication_tag, &plain_text)); | |
| 191 EXPECT_EQ(Status::OperationError(), | |
| 192 AesGcmDecrypt(key, test_iv, Corrupted(test_additional_data), | |
| 193 test_tag_size_bits, test_cipher_text, | |
| 194 test_authentication_tag, &plain_text)); | |
| 195 EXPECT_EQ(Status::OperationError(), | |
| 196 AesGcmDecrypt(key, test_iv, test_additional_data, | |
| 197 test_tag_size_bits, Corrupted(test_cipher_text), | |
| 198 test_authentication_tag, &plain_text)); | |
| 199 EXPECT_EQ(Status::OperationError(), | |
| 200 AesGcmDecrypt(key, test_iv, test_additional_data, | |
| 201 test_tag_size_bits, test_cipher_text, | |
| 202 Corrupted(test_authentication_tag), &plain_text)); | |
| 203 | |
| 204 // Try different incorrect tag lengths | |
| 205 uint8_t kAlternateTagLengths[] = {0, 8, 96, 120, 128, 160, 255}; | |
| 206 for (size_t tag_i = 0; tag_i < arraysize(kAlternateTagLengths); ++tag_i) { | |
| 207 unsigned int wrong_tag_size_bits = kAlternateTagLengths[tag_i]; | |
| 208 if (test_tag_size_bits == wrong_tag_size_bits) | |
| 209 continue; | |
| 210 EXPECT_NE(Status::Success(), | |
| 211 AesGcmDecrypt(key, test_iv, test_additional_data, | |
| 212 wrong_tag_size_bits, test_cipher_text, | |
| 213 test_authentication_tag, &plain_text)); | |
| 214 } | |
| 215 } | |
| 216 } | |
| 217 | |
| 218 } // namespace | |
| 219 | |
| 220 } // namespace webcrypto | |
| OLD | NEW |