Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/webcrypto/webcrypto_impl.h" | 5 #include "content/renderer/webcrypto/webcrypto_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/file_util.h" | |
| 13 #include "base/json/json_reader.h" | |
| 12 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 13 #include "base/logging.h" | 15 #include "base/logging.h" |
| 14 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/path_service.h" | |
| 15 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/values.h" | |
| 20 #include "content/public/common/content_paths.h" | |
| 16 #include "content/public/renderer/content_renderer_client.h" | 21 #include "content/public/renderer/content_renderer_client.h" |
| 17 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | 22 #include "content/renderer/renderer_webkitplatformsupport_impl.h" |
| 18 #include "content/renderer/webcrypto/webcrypto_util.h" | 23 #include "content/renderer/webcrypto/webcrypto_util.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 25 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 26 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 22 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 23 #include "third_party/WebKit/public/platform/WebCryptoKey.h" | 28 #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
| 29 #include "third_party/re2/re2/re2.h" | |
| 24 | 30 |
| 25 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of | 31 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of |
| 26 // the tests: http://crbug.com/267888 | 32 // the tests: http://crbug.com/267888 |
| 27 #if defined(USE_OPENSSL) | 33 #if defined(USE_OPENSSL) |
| 28 #define MAYBE(test_name) DISABLED_##test_name | 34 #define MAYBE(test_name) DISABLED_##test_name |
| 29 #else | 35 #else |
| 30 #define MAYBE(test_name) test_name | 36 #define MAYBE(test_name) test_name |
| 31 #endif | 37 #endif |
| 32 | 38 |
| 33 // Helper macros to verify the value of a Status. | 39 // Helper macros to verify the value of a Status. |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 56 corrupted_data[corrupted_data.size() / 2] ^= 0x01; | 62 corrupted_data[corrupted_data.size() / 2] ^= 0x01; |
| 57 return corrupted_data; | 63 return corrupted_data; |
| 58 } | 64 } |
| 59 | 65 |
| 60 std::vector<uint8> HexStringToBytes(const std::string& hex) { | 66 std::vector<uint8> HexStringToBytes(const std::string& hex) { |
| 61 std::vector<uint8> bytes; | 67 std::vector<uint8> bytes; |
| 62 base::HexStringToBytes(hex, &bytes); | 68 base::HexStringToBytes(hex, &bytes); |
| 63 return bytes; | 69 return bytes; |
| 64 } | 70 } |
| 65 | 71 |
| 72 void ExpectArrayBufferMatches(const std::vector<uint8>& expected, | |
| 73 const blink::WebArrayBuffer& actual) { | |
| 74 EXPECT_EQ( | |
| 75 base::HexEncode(webcrypto::Uint8VectorStart(expected), expected.size()), | |
| 76 base::HexEncode(actual.data(), actual.byteLength())); | |
| 77 } | |
| 78 | |
| 66 void ExpectArrayBufferMatchesHex(const std::string& expected_hex, | 79 void ExpectArrayBufferMatchesHex(const std::string& expected_hex, |
| 67 const blink::WebArrayBuffer& array_buffer) { | 80 const blink::WebArrayBuffer& array_buffer) { |
| 68 EXPECT_STRCASEEQ( | 81 EXPECT_STRCASEEQ( |
| 69 expected_hex.c_str(), | 82 expected_hex.c_str(), |
| 70 base::HexEncode(array_buffer.data(), array_buffer.byteLength()).c_str()); | 83 base::HexEncode(array_buffer.data(), array_buffer.byteLength()).c_str()); |
| 71 } | 84 } |
| 72 | 85 |
| 73 void ExpectVectorMatchesHex(const std::string& expected_hex, | 86 void ExpectVectorMatches(const std::vector<uint8>& expected, |
| 74 const std::vector<uint8>& bytes) { | 87 const std::vector<uint8>& actual) { |
| 75 EXPECT_STRCASEEQ( | 88 EXPECT_EQ( |
| 76 expected_hex.c_str(), | 89 base::HexEncode(webcrypto::Uint8VectorStart(expected), expected.size()), |
| 77 base::HexEncode(webcrypto::Uint8VectorStart(bytes), | 90 base::HexEncode(webcrypto::Uint8VectorStart(actual), actual.size())); |
| 78 bytes.size()).c_str()); | |
| 79 } | 91 } |
| 80 | 92 |
| 81 std::vector<uint8> MakeJsonVector(const std::string& json_string) { | 93 std::vector<uint8> MakeJsonVector(const std::string& json_string) { |
| 82 return std::vector<uint8>(json_string.begin(), json_string.end()); | 94 return std::vector<uint8>(json_string.begin(), json_string.end()); |
| 83 } | 95 } |
| 84 | 96 |
| 85 std::vector<uint8> MakeJsonVector(const base::DictionaryValue& dict) { | 97 std::vector<uint8> MakeJsonVector(const base::DictionaryValue& dict) { |
| 86 std::string json; | 98 std::string json; |
| 87 base::JSONWriter::Write(&dict, &json); | 99 base::JSONWriter::Write(&dict, &json); |
| 88 return MakeJsonVector(json); | 100 return MakeJsonVector(json); |
| 89 } | 101 } |
| 90 | 102 |
| 103 // ---------------------------------------------------------------- | |
| 104 // Helpers for working with JSON data files for test expectations. | |
| 105 // ---------------------------------------------------------------- | |
| 106 | |
| 107 // Reads a file in "src/content/test/data/webcrypto" to a base::Value. | |
| 108 // The file must be JSON, however it can also include C++ style comments. | |
| 109 ::testing::AssertionResult ReadJsonTestFile( | |
| 110 const char* test_file_name, | |
| 111 scoped_ptr<base::Value>* value) { | |
| 112 base::FilePath test_data_dir; | |
| 113 if (!PathService::Get(DIR_TEST_DATA, &test_data_dir)) | |
| 114 return ::testing::AssertionFailure() << "Couldn't retrieve test dir"; | |
| 115 | |
| 116 base::FilePath file_path = | |
| 117 test_data_dir.AppendASCII("webcrypto").AppendASCII(test_file_name); | |
| 118 | |
| 119 std::string file_contents; | |
| 120 if (!base::ReadFileToString(file_path, &file_contents)) { | |
| 121 return ::testing::AssertionFailure() << "Couldn't read test file: " | |
| 122 << file_path.value(); | |
| 123 } | |
| 124 | |
| 125 // Strip C++ style comments out of the "json" file, otherwise it cannot be | |
| 126 // parsed. | |
| 127 re2::RE2::GlobalReplace(&file_contents, re2::RE2("\\s*//.*"), ""); | |
| 128 | |
| 129 // Parse the JSON to a dictionary. | |
| 130 value->reset(base::JSONReader::Read(file_contents)); | |
| 131 if (!value->get()) { | |
| 132 return ::testing::AssertionFailure() << "Couldn't parse test file JSON: " | |
| 133 << file_path.value(); | |
| 134 } | |
| 135 | |
| 136 return ::testing::AssertionSuccess(); | |
| 137 } | |
| 138 | |
| 139 // Same as ReadJsonTestFile(), but return the value as a List. | |
| 140 ::testing::AssertionResult ReadJsonTestFileToList( | |
| 141 const char* test_file_name, | |
| 142 scoped_ptr<base::ListValue>* list) { | |
| 143 // Read the JSON. | |
| 144 scoped_ptr<base::Value> json; | |
| 145 ::testing::AssertionResult result = ReadJsonTestFile(test_file_name, &json); | |
| 146 if (!result) | |
| 147 return result; | |
| 148 | |
| 149 // Cast to an ListValue. | |
| 150 base::ListValue* list_value = NULL; | |
| 151 if (!json->GetAsList(&list_value) || !list_value) | |
| 152 return ::testing::AssertionFailure() << "The JSON was not a list"; | |
| 153 | |
| 154 list->reset(list_value); | |
| 155 ignore_result(json.release()); | |
| 156 | |
| 157 return ::testing::AssertionSuccess(); | |
| 158 } | |
| 159 | |
| 160 // Read a string property from the dictionary with path |property_name| | |
| 161 // (which can include periods for nested dictionaries). Interprets the | |
| 162 // string as a hex encoded string and converts it to a bytes list. | |
| 163 // | |
| 164 // Returns empty vector on failure. | |
| 165 std::vector<uint8> GetBytesFromHexString( | |
| 166 base::DictionaryValue* dict, | |
| 167 const char* property_name) { | |
| 168 std::string hex_string; | |
| 169 if (!dict->GetString(property_name, &hex_string)) { | |
| 170 EXPECT_TRUE(false) << "Couldn't get string property: " << property_name; | |
| 171 return std::vector<uint8>(); | |
| 172 } | |
| 173 | |
| 174 return HexStringToBytes(hex_string); | |
| 175 } | |
| 176 | |
| 177 // Reads a string property with path "property_name" and converts it to a | |
| 178 // WebCryptoAlgorith. Returns null algorithm on failure. | |
| 179 blink::WebCryptoAlgorithm GetDigestAlgorithm( | |
| 180 base::DictionaryValue* dict, | |
| 181 const char* property_name) { | |
| 182 std::string algorithm_name; | |
| 183 if (!dict->GetString(property_name, &algorithm_name)) { | |
| 184 EXPECT_TRUE(false) << "Couldn't get string property: " << property_name; | |
| 185 return blink::WebCryptoAlgorithm::createNull(); | |
| 186 } | |
| 187 | |
| 188 struct { | |
| 189 const char* name; | |
| 190 blink::WebCryptoAlgorithmId id; | |
| 191 } kDigestNameToId[] = { | |
|
Ryan Sleevi
2014/01/31 20:53:36
make this struct const/static too?
| |
| 192 {"sha-1", blink::WebCryptoAlgorithmIdSha1}, | |
| 193 {"sha-224", blink::WebCryptoAlgorithmIdSha224}, | |
| 194 {"sha-256", blink::WebCryptoAlgorithmIdSha256}, | |
| 195 {"sha-384", blink::WebCryptoAlgorithmIdSha384}, | |
| 196 {"sha-512", blink::WebCryptoAlgorithmIdSha512}, | |
| 197 }; | |
| 198 | |
| 199 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDigestNameToId); ++i) { | |
| 200 if (kDigestNameToId[i].name == algorithm_name) | |
| 201 return webcrypto::CreateAlgorithm(kDigestNameToId[i].id); | |
| 202 } | |
| 203 | |
| 204 return blink::WebCryptoAlgorithm::createNull(); | |
| 205 } | |
| 206 | |
| 91 // Helper for ImportJwkFailures and ImportJwkOctFailures. Restores the JWK JSON | 207 // Helper for ImportJwkFailures and ImportJwkOctFailures. Restores the JWK JSON |
| 92 // dictionary to a good state | 208 // dictionary to a good state |
| 93 void RestoreJwkOctDictionary(base::DictionaryValue* dict) { | 209 void RestoreJwkOctDictionary(base::DictionaryValue* dict) { |
| 94 dict->Clear(); | 210 dict->Clear(); |
| 95 dict->SetString("kty", "oct"); | 211 dict->SetString("kty", "oct"); |
| 96 dict->SetString("alg", "A128CBC"); | 212 dict->SetString("alg", "A128CBC"); |
| 97 dict->SetString("use", "enc"); | 213 dict->SetString("use", "enc"); |
| 98 dict->SetBoolean("extractable", false); | 214 dict->SetBoolean("extractable", false); |
| 99 dict->SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); | 215 dict->SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); |
| 100 } | 216 } |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 "2049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd" | 332 "2049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd" |
| 217 "48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729024027" | 333 "48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729024027" |
| 218 "156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319" | 334 "156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319" |
| 219 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24" | 335 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24" |
| 220 "a79f4d"; | 336 "a79f4d"; |
| 221 | 337 |
| 222 } // namespace | 338 } // namespace |
| 223 | 339 |
| 224 class WebCryptoImplTest : public testing::Test { | 340 class WebCryptoImplTest : public testing::Test { |
| 225 protected: | 341 protected: |
| 226 blink::WebCryptoKey ImportSecretKeyFromRawHexString( | 342 blink::WebCryptoKey ImportSecretKeyFromRaw( |
| 227 const std::string& key_hex, | 343 const std::vector<uint8>& key_raw, |
| 228 const blink::WebCryptoAlgorithm& algorithm, | 344 const blink::WebCryptoAlgorithm& algorithm, |
| 229 blink::WebCryptoKeyUsageMask usage) { | 345 blink::WebCryptoKeyUsageMask usage) { |
| 230 std::vector<uint8> key_raw = HexStringToBytes(key_hex); | |
| 231 | |
| 232 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 346 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 233 bool extractable = true; | 347 bool extractable = true; |
| 234 EXPECT_STATUS_SUCCESS( | 348 EXPECT_STATUS_SUCCESS( |
| 235 crypto_.ImportKeyInternal(blink::WebCryptoKeyFormatRaw, | 349 crypto_.ImportKeyInternal(blink::WebCryptoKeyFormatRaw, |
| 236 webcrypto::Uint8VectorStart(key_raw), | 350 webcrypto::Uint8VectorStart(key_raw), |
| 237 key_raw.size(), | 351 key_raw.size(), |
| 238 algorithm, | 352 algorithm, |
| 239 extractable, | 353 extractable, |
| 240 usage, | 354 usage, |
| 241 &key)); | 355 &key)); |
| 242 | 356 |
| 243 EXPECT_FALSE(key.isNull()); | 357 EXPECT_FALSE(key.isNull()); |
| 244 EXPECT_TRUE(key.handle()); | 358 EXPECT_TRUE(key.handle()); |
| 245 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 359 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 246 EXPECT_EQ(algorithm.id(), key.algorithm().id()); | 360 EXPECT_EQ(algorithm.id(), key.algorithm().id()); |
| 247 EXPECT_EQ(extractable, key.extractable()); | 361 EXPECT_EQ(extractable, key.extractable()); |
| 248 EXPECT_EQ(usage, key.usages()); | 362 EXPECT_EQ(usage, key.usages()); |
| 249 return key; | 363 return key; |
| 250 } | 364 } |
| 251 | 365 |
| 252 void ImportRsaKeyPair( | 366 void ImportRsaKeyPair( |
| 253 const std::string& spki_der_hex, | 367 const std::vector<uint8>& spki_der, |
| 254 const std::string& pkcs8_der_hex, | 368 const std::vector<uint8>& pkcs8_der, |
| 255 const blink::WebCryptoAlgorithm& algorithm, | 369 const blink::WebCryptoAlgorithm& algorithm, |
| 256 bool extractable, | 370 bool extractable, |
| 257 blink::WebCryptoKeyUsageMask usage_mask, | 371 blink::WebCryptoKeyUsageMask usage_mask, |
| 258 blink::WebCryptoKey* public_key, | 372 blink::WebCryptoKey* public_key, |
| 259 blink::WebCryptoKey* private_key) { | 373 blink::WebCryptoKey* private_key) { |
| 260 EXPECT_STATUS_SUCCESS(ImportKeyInternal( | 374 EXPECT_STATUS_SUCCESS(ImportKeyInternal( |
| 261 blink::WebCryptoKeyFormatSpki, | 375 blink::WebCryptoKeyFormatSpki, |
| 262 HexStringToBytes(spki_der_hex), | 376 spki_der, |
| 263 algorithm, | 377 algorithm, |
| 264 true, | 378 true, |
| 265 usage_mask, | 379 usage_mask, |
| 266 public_key)); | 380 public_key)); |
| 267 EXPECT_FALSE(public_key->isNull()); | 381 EXPECT_FALSE(public_key->isNull()); |
| 268 EXPECT_TRUE(public_key->handle()); | 382 EXPECT_TRUE(public_key->handle()); |
| 269 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key->type()); | 383 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key->type()); |
| 270 EXPECT_EQ(algorithm.id(), public_key->algorithm().id()); | 384 EXPECT_EQ(algorithm.id(), public_key->algorithm().id()); |
| 271 EXPECT_EQ(extractable, extractable); | 385 EXPECT_EQ(extractable, extractable); |
| 272 EXPECT_EQ(usage_mask, public_key->usages()); | 386 EXPECT_EQ(usage_mask, public_key->usages()); |
| 273 | 387 |
| 274 EXPECT_STATUS_SUCCESS(ImportKeyInternal( | 388 EXPECT_STATUS_SUCCESS(ImportKeyInternal( |
| 275 blink::WebCryptoKeyFormatPkcs8, | 389 blink::WebCryptoKeyFormatPkcs8, |
| 276 HexStringToBytes(pkcs8_der_hex), | 390 pkcs8_der, |
| 277 algorithm, | 391 algorithm, |
| 278 extractable, | 392 extractable, |
| 279 usage_mask, | 393 usage_mask, |
| 280 private_key)); | 394 private_key)); |
| 281 EXPECT_FALSE(private_key->isNull()); | 395 EXPECT_FALSE(private_key->isNull()); |
| 282 EXPECT_TRUE(private_key->handle()); | 396 EXPECT_TRUE(private_key->handle()); |
| 283 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key->type()); | 397 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key->type()); |
| 284 EXPECT_EQ(algorithm.id(), private_key->algorithm().id()); | 398 EXPECT_EQ(algorithm.id(), private_key->algorithm().id()); |
| 285 EXPECT_EQ(extractable, extractable); | 399 EXPECT_EQ(extractable, extractable); |
| 286 EXPECT_EQ(usage_mask, private_key->usages()); | 400 EXPECT_EQ(usage_mask, private_key->usages()); |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 511 }; | 625 }; |
| 512 | 626 |
| 513 TEST_F(WebCryptoImplTest, StatusToString) { | 627 TEST_F(WebCryptoImplTest, StatusToString) { |
| 514 EXPECT_EQ("Success", Status::Success().ToString()); | 628 EXPECT_EQ("Success", Status::Success().ToString()); |
| 515 EXPECT_EQ("", Status::Error().ToString()); | 629 EXPECT_EQ("", Status::Error().ToString()); |
| 516 EXPECT_EQ("The requested operation is unsupported", | 630 EXPECT_EQ("The requested operation is unsupported", |
| 517 Status::ErrorUnsupported().ToString()); | 631 Status::ErrorUnsupported().ToString()); |
| 518 } | 632 } |
| 519 | 633 |
| 520 TEST_F(WebCryptoImplTest, DigestSampleSets) { | 634 TEST_F(WebCryptoImplTest, DigestSampleSets) { |
| 521 // The results are stored here in hex format for readability. | 635 scoped_ptr<base::ListValue> tests; |
| 522 // | 636 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests)); |
| 523 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced | |
| 524 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 | |
| 525 // | |
| 526 // Results were generated using the command sha{1,224,256,384,512}sum. | |
| 527 struct TestCase { | |
| 528 blink::WebCryptoAlgorithmId algorithm; | |
| 529 const std::string hex_input; | |
| 530 const char* hex_result; | |
| 531 }; | |
| 532 | 637 |
| 533 const TestCase kTests[] = { | 638 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
|
Ryan Sleevi
2014/01/31 20:53:36
So, this is valid code, so consider this a questio
eroman
2014/01/31 22:15:33
Not sure I understand this argument from a "perfor
| |
| 534 { blink::WebCryptoAlgorithmIdSha1, "", | 639 SCOPED_TRACE(test_index); |
| 535 "da39a3ee5e6b4b0d3255bfef95601890afd80709" | 640 base::DictionaryValue* test; |
| 536 }, | 641 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
| 537 { blink::WebCryptoAlgorithmIdSha224, "", | |
| 538 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f" | |
| 539 }, | |
| 540 { blink::WebCryptoAlgorithmIdSha256, "", | |
| 541 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" | |
| 542 }, | |
| 543 { blink::WebCryptoAlgorithmIdSha384, "", | |
| 544 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" | |
| 545 "debfe76f65fbd51ad2f14898b95b" | |
| 546 }, | |
| 547 { blink::WebCryptoAlgorithmIdSha512, "", | |
| 548 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" | |
| 549 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", | |
| 550 }, | |
| 551 { blink::WebCryptoAlgorithmIdSha1, "00", | |
| 552 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", | |
| 553 }, | |
| 554 { blink::WebCryptoAlgorithmIdSha224, "00", | |
| 555 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", | |
| 556 }, | |
| 557 { blink::WebCryptoAlgorithmIdSha256, "00", | |
| 558 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | |
| 559 }, | |
| 560 { blink::WebCryptoAlgorithmIdSha384, "00", | |
| 561 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" | |
| 562 "ec2b413465966817a9c208a11717", | |
| 563 }, | |
| 564 { blink::WebCryptoAlgorithmIdSha512, "00", | |
| 565 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" | |
| 566 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", | |
| 567 }, | |
| 568 { blink::WebCryptoAlgorithmIdSha1, "000102030405", | |
| 569 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", | |
| 570 }, | |
| 571 { blink::WebCryptoAlgorithmIdSha224, "000102030405", | |
| 572 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", | |
| 573 }, | |
| 574 { blink::WebCryptoAlgorithmIdSha256, "000102030405", | |
| 575 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", | |
| 576 }, | |
| 577 { blink::WebCryptoAlgorithmIdSha384, "000102030405", | |
| 578 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | |
| 579 "22780d7e1b95bfeaa86a678e4552", | |
| 580 }, | |
| 581 { blink::WebCryptoAlgorithmIdSha512, "000102030405", | |
| 582 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | |
| 583 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | |
| 584 }, | |
| 585 }; | |
| 586 | 642 |
| 587 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); | 643 blink::WebCryptoAlgorithm test_algorithm = |
| 588 ++test_index) { | 644 GetDigestAlgorithm(test, "algorithm"); |
| 589 SCOPED_TRACE(test_index); | 645 std::vector<uint8> test_input = GetBytesFromHexString(test, "input"); |
| 590 const TestCase& test = kTests[test_index]; | 646 std::vector<uint8> test_output = GetBytesFromHexString(test, "output"); |
| 591 | |
| 592 blink::WebCryptoAlgorithm algorithm = | |
| 593 webcrypto::CreateAlgorithm(test.algorithm); | |
| 594 std::vector<uint8> input = HexStringToBytes(test.hex_input); | |
| 595 | 647 |
| 596 blink::WebArrayBuffer output; | 648 blink::WebArrayBuffer output; |
| 597 ASSERT_STATUS_SUCCESS(DigestInternal(algorithm, input, &output)); | 649 ASSERT_STATUS_SUCCESS(DigestInternal(test_algorithm, test_input, &output)); |
| 598 ExpectArrayBufferMatchesHex(test.hex_result, output); | 650 ExpectArrayBufferMatches(test_output, output); |
| 599 } | 651 } |
| 600 } | 652 } |
| 601 | 653 |
| 602 TEST_F(WebCryptoImplTest, HMACSampleSets) { | 654 TEST_F(WebCryptoImplTest, HMACSampleSets) { |
| 603 struct TestCase { | 655 scoped_ptr<base::ListValue> tests; |
| 604 blink::WebCryptoAlgorithmId algorithm; | 656 ASSERT_TRUE(ReadJsonTestFileToList("hmac.json", &tests)); |
| 605 const char* key; | |
| 606 const char* message; | |
| 607 const char* mac; | |
| 608 }; | |
| 609 | 657 |
| 610 const TestCase kTests[] = { | 658 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
| 611 // Empty sets. Result generated via OpenSSL commandline tool. These | 659 SCOPED_TRACE(test_index); |
| 612 // particular results are also posted on the Wikipedia page examples: | 660 base::DictionaryValue* test; |
| 613 // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code | 661 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
| 614 { | |
| 615 blink::WebCryptoAlgorithmIdSha1, | |
| 616 "", | |
| 617 "", | |
| 618 // openssl dgst -sha1 -hmac "" < /dev/null | |
| 619 "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", | |
| 620 }, | |
| 621 { | |
| 622 blink::WebCryptoAlgorithmIdSha256, | |
| 623 "", | |
| 624 "", | |
| 625 // openssl dgst -sha256 -hmac "" < /dev/null | |
| 626 "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", | |
| 627 }, | |
| 628 // For this data, see http://csrc.nist.gov/groups/STM/cavp/index.html#07 | |
| 629 // Download: | |
| 630 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/hmactestvectors.zip | |
| 631 // L=20 set 45 | |
| 632 { | |
| 633 blink::WebCryptoAlgorithmIdSha1, | |
| 634 // key | |
| 635 "59785928d72516e31272", | |
| 636 // message | |
| 637 "a3ce8899df1022e8d2d539b47bf0e309c66f84095e21438ec355bf119ce5fdcb4e73a6" | |
| 638 "19cdf36f25b369d8c38ff419997f0c59830108223606e31223483fd39edeaa4d3f0d21" | |
| 639 "198862d239c9fd26074130ff6c86493f5227ab895c8f244bd42c7afce5d147a20a5907" | |
| 640 "98c68e708e964902d124dadecdbda9dbd0051ed710e9bf", | |
| 641 // mac | |
| 642 "3c8162589aafaee024fc9a5ca50dd2336fe3eb28", | |
| 643 }, | |
| 644 // L=20 set 299 | |
| 645 { | |
| 646 blink::WebCryptoAlgorithmIdSha1, | |
| 647 // key | |
| 648 "ceb9aedf8d6efcf0ae52bea0fa99a9e26ae81bacea0cff4d5eecf201e3bca3c3577480" | |
| 649 "621b818fd717ba99d6ff958ea3d59b2527b019c343bb199e648090225867d994607962" | |
| 650 "f5866aa62930d75b58f6", | |
| 651 // message | |
| 652 "99958aa459604657c7bf6e4cdfcc8785f0abf06ffe636b5b64ecd931bd8a4563055924" | |
| 653 "21fc28dbcccb8a82acea2be8e54161d7a78e0399a6067ebaca3f2510274dc9f92f2c8a" | |
| 654 "e4265eec13d7d42e9f8612d7bc258f913ecb5a3a5c610339b49fb90e9037b02d684fc6" | |
| 655 "0da835657cb24eab352750c8b463b1a8494660d36c3ab2", | |
| 656 // mac | |
| 657 "4ac41ab89f625c60125ed65ffa958c6b490ea670", | |
| 658 }, | |
| 659 // L=32, set 30 | |
| 660 { | |
| 661 blink::WebCryptoAlgorithmIdSha256, | |
| 662 // key | |
| 663 "9779d9120642797f1747025d5b22b7ac607cab08e1758f2f3a46c8be1e25c53b8c6a8f" | |
| 664 "58ffefa176", | |
| 665 // message | |
| 666 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" | |
| 667 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" | |
| 668 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" | |
| 669 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e", | |
| 670 // mac | |
| 671 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b", | |
| 672 }, | |
| 673 // L=32, set 224 | |
| 674 { | |
| 675 blink::WebCryptoAlgorithmIdSha256, | |
| 676 // key | |
| 677 "4b7ab133efe99e02fc89a28409ee187d579e774f4cba6fc223e13504e3511bef8d4f63" | |
| 678 "8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b08" | |
| 679 "3371289b", | |
| 680 // message | |
| 681 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" | |
| 682 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" | |
| 683 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" | |
| 684 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", | |
| 685 // mac | |
| 686 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", | |
| 687 }, | |
| 688 // L=28, Count=71 | |
| 689 { | |
| 690 blink::WebCryptoAlgorithmIdSha224, | |
| 691 // key | |
| 692 "6c2539f4d0453efbbacc137794930413aeb392e029e0724715f9d943d6dcf7cdcc7fc19" | |
| 693 "7333df4fc476d5737ac3940d40eae", | |
| 694 // message | |
| 695 "1f207b3fa6c905529c9f9f7894b8941b616974df2c0cc482c400f50734f293139b5bbf9" | |
| 696 "7384adfafc56494ca0629ed0ca179daf03056e33295eb19ec8dcd4dff898281b4b9409c" | |
| 697 "a369f662d49091a225a678b1ebb75818dcb6278a2d136319f78f9ba9df5031a4f6305ee" | |
| 698 "fde5b761d2f196ee318e89bcc4acebc2e11ed3b5dc4", | |
| 699 // mac | |
| 700 "4a7d9d13705b0faba0db75356c8ee0635afff1544911c69c2fbb1ab2" | |
| 701 }, | |
| 702 // L=48, Count=50 | |
| 703 { | |
| 704 blink::WebCryptoAlgorithmIdSha384, | |
| 705 // key | |
| 706 "d137f3e6cc4af28554beb03ba7a97e60c9d3959cd3bb08068edbf68d402d0498c6ee0ae" | |
| 707 "9e3a20dc7d8586e5c352f605cee19", | |
| 708 // message | |
| 709 "64a884670d1c1dff555483dcd3da305dfba54bdc4d817c33ccb8fe7eb2ebf6236241031" | |
| 710 "09ec41644fa078491900c59a0f666f0356d9bc0b45bcc79e5fc9850f4543d96bc680090" | |
| 711 "44add0838ac1260e80592fbc557b2ddaf5ed1b86d3ed8f09e622e567f1d39a340857f6a" | |
| 712 "850cceef6060c48dac3dd0071fe68eb4ed2ed9aca01", | |
| 713 // mac | |
| 714 "c550fa53514da34f15e7f98ea87226ab6896cdfae25d3ec2335839f755cdc9a4992092e" | |
| 715 "70b7e5bd422784380b6396cf5" | |
| 716 }, | |
| 717 // L=64, Count=65 | |
| 718 { | |
| 719 blink::WebCryptoAlgorithmIdSha512, | |
| 720 // key | |
| 721 "c367aeb5c02b727883ffe2a4ceebf911b01454beb328fb5d57fc7f11bf744576aba421e2" | |
| 722 "a63426ea8109bd28ff21f53cd2bf1a11c6c989623d6ec27cdb0bbf458250857d819ff844" | |
| 723 "08b4f3dce08b98b1587ee59683af8852a0a5f55bda3ab5e132b4010e", | |
| 724 // message | |
| 725 "1a7331c8ff1b748e3cee96952190fdbbe4ee2f79e5753bbb368255ee5b19c05a4ed9f1b2" | |
| 726 "c72ff1e9b9cb0348205087befa501e7793770faf0606e9c901836a9bc8afa00d7db94ee2" | |
| 727 "9eb191d5cf3fc3e8da95a0f9f4a2a7964289c3129b512bd890de8700a9205420f28a8965" | |
| 728 "b6c67be28ba7fe278e5fcd16f0f22cf2b2eacbb9", | |
| 729 // mac | |
| 730 "4459066109cb11e6870fa9c6bfd251adfa304c0a2928ca915049704972edc560cc7c0bc3" | |
| 731 "8249e9101aae2f7d4da62eaff83fb07134efc277de72b9e4ab360425" | |
| 732 }, | |
| 733 }; | |
| 734 | 662 |
| 735 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); | 663 blink::WebCryptoAlgorithm test_hash = GetDigestAlgorithm(test, "hash"); |
| 736 ++test_index) { | 664 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); |
| 737 SCOPED_TRACE(test_index); | 665 const std::vector<uint8> test_message = |
| 738 const TestCase& test = kTests[test_index]; | 666 GetBytesFromHexString(test, "message"); |
| 667 const std::vector<uint8> test_mac = GetBytesFromHexString(test, "mac"); | |
| 739 | 668 |
| 740 blink::WebCryptoAlgorithm algorithm = | 669 blink::WebCryptoAlgorithm algorithm = |
| 741 webcrypto::CreateHmacAlgorithmByHashId(test.algorithm); | 670 webcrypto::CreateHmacAlgorithmByHashId(test_hash.id()); |
| 742 | 671 |
| 743 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString( | 672 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
| 744 test.key, algorithm, blink::WebCryptoKeyUsageSign); | 673 test_key, algorithm, blink::WebCryptoKeyUsageSign); |
| 745 | 674 |
| 746 // Verify exported raw key is identical to the imported data | 675 // Verify exported raw key is identical to the imported data |
| 747 blink::WebArrayBuffer raw_key; | 676 blink::WebArrayBuffer raw_key; |
| 748 EXPECT_STATUS_SUCCESS( | 677 EXPECT_STATUS_SUCCESS( |
| 749 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 678 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 750 ExpectArrayBufferMatchesHex(test.key, raw_key); | 679 ExpectArrayBufferMatches(test_key, raw_key); |
| 751 | |
| 752 std::vector<uint8> message_raw = HexStringToBytes(test.message); | |
| 753 | 680 |
| 754 blink::WebArrayBuffer output; | 681 blink::WebArrayBuffer output; |
| 755 | 682 |
| 756 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, key, message_raw, &output)); | 683 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, key, test_message, &output)); |
| 757 | 684 |
| 758 ExpectArrayBufferMatchesHex(test.mac, output); | 685 ExpectArrayBufferMatches(test_mac, output); |
| 759 | 686 |
| 760 bool signature_match = false; | 687 bool signature_match = false; |
| 761 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( | 688 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( |
| 762 algorithm, | 689 algorithm, |
| 763 key, | 690 key, |
| 764 static_cast<const unsigned char*>(output.data()), | 691 static_cast<const unsigned char*>(output.data()), |
| 765 output.byteLength(), | 692 output.byteLength(), |
| 766 message_raw, | 693 test_message, |
| 767 &signature_match)); | 694 &signature_match)); |
| 768 EXPECT_TRUE(signature_match); | 695 EXPECT_TRUE(signature_match); |
| 769 | 696 |
| 770 // Ensure truncated signature does not verify by passing one less byte. | 697 // Ensure truncated signature does not verify by passing one less byte. |
| 771 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( | 698 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( |
| 772 algorithm, | 699 algorithm, |
| 773 key, | 700 key, |
| 774 static_cast<const unsigned char*>(output.data()), | 701 static_cast<const unsigned char*>(output.data()), |
| 775 output.byteLength() - 1, | 702 output.byteLength() - 1, |
| 776 message_raw, | 703 test_message, |
| 777 &signature_match)); | 704 &signature_match)); |
| 778 EXPECT_FALSE(signature_match); | 705 EXPECT_FALSE(signature_match); |
| 779 | 706 |
| 780 // Ensure truncated signature does not verify by passing no bytes. | 707 // Ensure truncated signature does not verify by passing no bytes. |
| 781 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( | 708 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( |
| 782 algorithm, | 709 algorithm, |
| 783 key, | 710 key, |
| 784 NULL, | 711 NULL, |
| 785 0, | 712 0, |
| 786 message_raw, | 713 test_message, |
| 787 &signature_match)); | 714 &signature_match)); |
| 788 EXPECT_FALSE(signature_match); | 715 EXPECT_FALSE(signature_match); |
| 789 | 716 |
| 790 // Ensure extra long signature does not cause issues and fails. | 717 // Ensure extra long signature does not cause issues and fails. |
| 791 const unsigned char kLongSignature[1024] = { 0 }; | 718 const unsigned char kLongSignature[1024] = { 0 }; |
| 792 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( | 719 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( |
| 793 algorithm, | 720 algorithm, |
| 794 key, | 721 key, |
| 795 kLongSignature, | 722 kLongSignature, |
| 796 sizeof(kLongSignature), | 723 sizeof(kLongSignature), |
| 797 message_raw, | 724 test_message, |
| 798 &signature_match)); | 725 &signature_match)); |
| 799 EXPECT_FALSE(signature_match); | 726 EXPECT_FALSE(signature_match); |
| 800 } | 727 } |
| 801 } | 728 } |
| 802 | 729 |
| 803 TEST_F(WebCryptoImplTest, AesCbcFailures) { | 730 TEST_F(WebCryptoImplTest, AesCbcFailures) { |
| 804 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c"; | 731 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c"; |
| 805 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString( | 732 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
| 806 key_hex, | 733 HexStringToBytes(key_hex), |
| 807 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 734 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 808 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); | 735 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); |
| 809 | 736 |
| 810 // Verify exported raw key is identical to the imported data | 737 // Verify exported raw key is identical to the imported data |
| 811 blink::WebArrayBuffer raw_key; | 738 blink::WebArrayBuffer raw_key; |
| 812 EXPECT_STATUS_SUCCESS( | 739 EXPECT_STATUS_SUCCESS( |
| 813 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 740 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 814 ExpectArrayBufferMatchesHex(key_hex, raw_key); | 741 ExpectArrayBufferMatchesHex(key_hex, raw_key); |
| 815 | 742 |
| 816 blink::WebArrayBuffer output; | 743 blink::WebArrayBuffer output; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 873 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret | 800 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret |
| 874 // keys). | 801 // keys). |
| 875 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), | 802 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), |
| 876 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output)); | 803 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output)); |
| 877 EXPECT_STATUS(Status::ErrorUnsupported(), | 804 EXPECT_STATUS(Status::ErrorUnsupported(), |
| 878 ExportKeyInternal(blink::WebCryptoKeyFormatPkcs8, key, &output)); | 805 ExportKeyInternal(blink::WebCryptoKeyFormatPkcs8, key, &output)); |
| 879 #endif | 806 #endif |
| 880 } | 807 } |
| 881 | 808 |
| 882 TEST_F(WebCryptoImplTest, MAYBE(AesCbcSampleSets)) { | 809 TEST_F(WebCryptoImplTest, MAYBE(AesCbcSampleSets)) { |
| 883 struct TestCase { | 810 scoped_ptr<base::ListValue> tests; |
| 884 const char* key; | 811 ASSERT_TRUE(ReadJsonTestFileToList("aescbc.json", &tests)); |
|
Ryan Sleevi
2014/01/31 20:53:36
aes_cbc?
| |
| 885 const char* iv; | |
| 886 const char* plain_text; | |
| 887 const char* cipher_text; | |
| 888 }; | |
| 889 | 812 |
| 890 TestCase kTests[] = { | 813 for (size_t index = 0; index < tests->GetSize(); index++) { |
|
Ryan Sleevi
2014/01/31 20:53:36
++index
eroman
2014/01/31 22:55:11
Done.
| |
| 891 // F.2.1 (CBC-AES128.Encrypt) | 814 SCOPED_TRACE(index); |
| 892 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf | 815 base::DictionaryValue* test; |
| 893 { | 816 ASSERT_TRUE(tests->GetDictionary(index, &test)); |
| 894 // key | |
| 895 "2b7e151628aed2a6abf7158809cf4f3c", | |
| 896 | 817 |
| 897 // iv | 818 std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); |
| 898 "000102030405060708090a0b0c0d0e0f", | 819 std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv"); |
| 820 std::vector<uint8> test_plain_text = | |
| 821 GetBytesFromHexString(test, "plain_text"); | |
| 822 std::vector<uint8> test_cipher_text = | |
| 823 GetBytesFromHexString(test, "cipher_text"); | |
| 899 | 824 |
| 900 // plain_text | 825 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
| 901 "6bc1bee22e409f96e93d7e117393172a" | 826 test_key, |
| 902 "ae2d8a571e03ac9c9eb76fac45af8e51" | |
| 903 "30c81c46a35ce411e5fbc1191a0a52ef" | |
| 904 "f69f2445df4f9b17ad2b417be66c3710", | |
| 905 | |
| 906 // cipher_text | |
| 907 "7649abac8119b246cee98e9b12e9197d" | |
| 908 "5086cb9b507219ee95db113a917678b2" | |
| 909 "73bed6b8e3c1743b7116e69e22229516" | |
| 910 "3ff1caa1681fac09120eca307586e1a7" | |
| 911 // Padding block: encryption of {0x10, 0x10, ... 0x10}) (not given by the | |
| 912 // NIST test vector) | |
| 913 "8cb82807230e1321d3fae00d18cc2012" | |
| 914 }, | |
| 915 | |
| 916 // F.2.6 CBC-AES256.Decrypt [*] | |
| 917 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf | |
| 918 // | |
| 919 // [*] Truncated 3 bytes off the plain text, so block 4 differs from the | |
| 920 // NIST vector. | |
| 921 { | |
| 922 // key | |
| 923 "603deb1015ca71be2b73aef0857d7781" | |
| 924 "1f352c073b6108d72d9810a30914dff4", | |
| 925 | |
| 926 // iv | |
| 927 "000102030405060708090a0b0c0d0e0f", | |
| 928 | |
| 929 // plain_text | |
| 930 "6bc1bee22e409f96e93d7e117393172a" | |
| 931 "ae2d8a571e03ac9c9eb76fac45af8e51" | |
| 932 "30c81c46a35ce411e5fbc1191a0a52ef" | |
| 933 // Truncated this last block to make it more interesting. | |
| 934 "f69f2445df4f9b17ad2b417be6", | |
| 935 | |
| 936 // cipher_text | |
| 937 "f58c4c04d6e5f1ba779eabfb5f7bfbd6" | |
| 938 "9cfc4e967edb808d679f777bc6702c7d" | |
| 939 "39f23369a9d9bacfa530e26304231461" | |
| 940 // This block differs from source vector (due to truncation) | |
| 941 "c9aaf02a6a54e9e242ccbf48c59daca6" | |
| 942 }, | |
| 943 | |
| 944 // Taken from encryptor_unittest.cc (EncryptorTest.EmptyEncrypt()) | |
| 945 { | |
| 946 // key | |
| 947 "3132383d5369787465656e4279746573", | |
| 948 | |
| 949 // iv | |
| 950 "5377656574205369787465656e204956", | |
| 951 | |
| 952 // plain_text | |
| 953 "", | |
| 954 | |
| 955 // cipher_text | |
| 956 "8518b8878d34e7185e300d0fcc426396" | |
| 957 }, | |
| 958 }; | |
| 959 | |
| 960 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(kTests); index++) { | |
| 961 SCOPED_TRACE(index); | |
| 962 const TestCase& test = kTests[index]; | |
| 963 | |
| 964 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString( | |
| 965 test.key, | |
| 966 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 827 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 967 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); | 828 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); |
| 968 | 829 |
| 969 // Verify exported raw key is identical to the imported data | 830 // Verify exported raw key is identical to the imported data |
| 970 blink::WebArrayBuffer raw_key; | 831 blink::WebArrayBuffer raw_key; |
| 971 EXPECT_STATUS_SUCCESS(ExportKeyInternal( | 832 EXPECT_STATUS_SUCCESS(ExportKeyInternal( |
| 972 blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 833 blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 973 ExpectArrayBufferMatchesHex(test.key, raw_key); | 834 ExpectArrayBufferMatches(test_key, raw_key); |
| 974 | |
| 975 std::vector<uint8> plain_text = HexStringToBytes(test.plain_text); | |
| 976 std::vector<uint8> iv = HexStringToBytes(test.iv); | |
| 977 | 835 |
| 978 blink::WebArrayBuffer output; | 836 blink::WebArrayBuffer output; |
| 979 | 837 |
| 980 // Test encryption. | 838 // Test encryption. |
| 981 EXPECT_STATUS( | 839 EXPECT_STATUS( |
| 982 Status::Success(), | 840 Status::Success(), |
| 983 EncryptInternal(webcrypto::CreateAesCbcAlgorithm(iv), | 841 EncryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv), |
| 984 key, | 842 key, |
| 985 plain_text, | 843 test_plain_text, |
| 986 &output)); | 844 &output)); |
| 987 ExpectArrayBufferMatchesHex(test.cipher_text, output); | 845 ExpectArrayBufferMatches(test_cipher_text, output); |
| 988 | 846 |
| 989 // Test decryption. | 847 // Test decryption. |
| 990 std::vector<uint8> cipher_text = HexStringToBytes(test.cipher_text); | |
| 991 EXPECT_STATUS( | 848 EXPECT_STATUS( |
| 992 Status::Success(), | 849 Status::Success(), |
| 993 DecryptInternal(webcrypto::CreateAesCbcAlgorithm(iv), | 850 DecryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv), |
| 994 key, | 851 key, |
| 995 cipher_text, | 852 test_cipher_text, |
| 996 &output)); | 853 &output)); |
| 997 ExpectArrayBufferMatchesHex(test.plain_text, output); | 854 ExpectArrayBufferMatches(test_plain_text, output); |
| 998 | 855 |
| 999 const unsigned int kAesCbcBlockSize = 16; | 856 const unsigned int kAesCbcBlockSize = 16; |
| 1000 | 857 |
| 1001 // Decrypt with a padding error by stripping the last block. This also ends | 858 // Decrypt with a padding error by stripping the last block. This also ends |
| 1002 // up testing decryption over empty cipher text. | 859 // up testing decryption over empty cipher text. |
| 1003 if (cipher_text.size() >= kAesCbcBlockSize) { | 860 if (test_cipher_text.size() >= kAesCbcBlockSize) { |
| 1004 EXPECT_STATUS( | 861 EXPECT_STATUS( |
| 1005 Status::Error(), | 862 Status::Error(), |
| 1006 DecryptInternal(webcrypto::CreateAesCbcAlgorithm(iv), | 863 DecryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv), |
| 1007 key, | 864 key, |
| 1008 &cipher_text[0], | 865 &test_cipher_text[0], |
| 1009 cipher_text.size() - kAesCbcBlockSize, | 866 test_cipher_text.size() - kAesCbcBlockSize, |
| 1010 &output)); | 867 &output)); |
| 1011 } | 868 } |
| 1012 | 869 |
| 1013 // Decrypt cipher text which is not a multiple of block size by stripping | 870 // Decrypt cipher text which is not a multiple of block size by stripping |
| 1014 // a few bytes off the cipher text. | 871 // a few bytes off the cipher text. |
| 1015 if (cipher_text.size() > 3) { | 872 if (test_cipher_text.size() > 3) { |
| 1016 EXPECT_STATUS( | 873 EXPECT_STATUS( |
| 1017 Status::Error(), | 874 Status::Error(), |
| 1018 DecryptInternal(webcrypto::CreateAesCbcAlgorithm(iv), | 875 DecryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv), |
| 1019 key, | 876 key, |
| 1020 &cipher_text[0], | 877 &test_cipher_text[0], |
| 1021 cipher_text.size() - 3, | 878 test_cipher_text.size() - 3, |
| 1022 &output)); | 879 &output)); |
| 1023 } | 880 } |
| 1024 } | 881 } |
| 1025 } | 882 } |
| 1026 | 883 |
| 1027 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAes)) { | 884 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAes)) { |
| 1028 // Check key generation for each of AES-CBC, AES-GCM, and AES-KW, and for each | 885 // Check key generation for each of AES-CBC, AES-GCM, and AES-KW, and for each |
| 1029 // allowed key length. | 886 // allowed key length. |
| 1030 std::vector<blink::WebCryptoAlgorithm> algorithm; | 887 std::vector<blink::WebCryptoAlgorithm> algorithm; |
| 1031 const unsigned short kKeyLength[] = {128, 192, 256}; | 888 const unsigned short kKeyLength[] = {128, 192, 256}; |
| (...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1709 blink::WebCryptoKeyFormatSpki, private_key, &output)); | 1566 blink::WebCryptoKeyFormatSpki, private_key, &output)); |
| 1710 } | 1567 } |
| 1711 | 1568 |
| 1712 TEST_F(WebCryptoImplTest, MAYBE(RsaEsRoundTrip)) { | 1569 TEST_F(WebCryptoImplTest, MAYBE(RsaEsRoundTrip)) { |
| 1713 // Import a key pair. | 1570 // Import a key pair. |
| 1714 blink::WebCryptoAlgorithm algorithm = | 1571 blink::WebCryptoAlgorithm algorithm = |
| 1715 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); | 1572 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); |
| 1716 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 1573 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
| 1717 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 1574 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
| 1718 ImportRsaKeyPair( | 1575 ImportRsaKeyPair( |
| 1719 kPublicKeySpkiDerHex, | 1576 HexStringToBytes(kPublicKeySpkiDerHex), |
| 1720 kPrivateKeyPkcs8DerHex, | 1577 HexStringToBytes(kPrivateKeyPkcs8DerHex), |
| 1721 algorithm, | 1578 algorithm, |
| 1722 false, | 1579 false, |
| 1723 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, | 1580 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, |
| 1724 &public_key, | 1581 &public_key, |
| 1725 &private_key); | 1582 &private_key); |
| 1726 | 1583 |
| 1727 // Make a maximum-length data message. RSAES can operate on messages up to | 1584 // Make a maximum-length data message. RSAES can operate on messages up to |
| 1728 // length of k - 11 bytes, where k is the octet length of the RSA modulus. | 1585 // length of k - 11 bytes, where k is the octet length of the RSA modulus. |
| 1729 const unsigned int kMaxMsgSizeBytes = kModulusLength / 8 - 11; | 1586 const unsigned int kMaxMsgSizeBytes = kModulusLength / 8 - 11; |
| 1730 // There are two hex chars for each byte. | 1587 // There are two hex chars for each byte. |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 1756 algorithm, | 1613 algorithm, |
| 1757 private_key, | 1614 private_key, |
| 1758 reinterpret_cast<const unsigned char*>(encrypted_data.data()), | 1615 reinterpret_cast<const unsigned char*>(encrypted_data.data()), |
| 1759 encrypted_data.byteLength(), | 1616 encrypted_data.byteLength(), |
| 1760 &decrypted_data)); | 1617 &decrypted_data)); |
| 1761 ExpectArrayBufferMatchesHex(kTestDataHex[i], decrypted_data); | 1618 ExpectArrayBufferMatchesHex(kTestDataHex[i], decrypted_data); |
| 1762 } | 1619 } |
| 1763 } | 1620 } |
| 1764 | 1621 |
| 1765 TEST_F(WebCryptoImplTest, MAYBE(RsaEsKnownAnswer)) { | 1622 TEST_F(WebCryptoImplTest, MAYBE(RsaEsKnownAnswer)) { |
| 1623 | |
| 1624 scoped_ptr<base::Value> json; | |
| 1625 ASSERT_TRUE(ReadJsonTestFile("rsaes.json", &json)); | |
|
Ryan Sleevi
2014/01/31 20:53:36
rsa_es
| |
| 1626 base::DictionaryValue* test = NULL; | |
| 1627 ASSERT_TRUE(json->GetAsDictionary(&test)); | |
| 1628 | |
| 1766 // Because the random data in PKCS1.5 padding makes the encryption output non- | 1629 // Because the random data in PKCS1.5 padding makes the encryption output non- |
| 1767 // deterministic, we cannot easily do a typical known-answer test for RSA | 1630 // deterministic, we cannot easily do a typical known-answer test for RSA |
| 1768 // encryption / decryption. Instead we will take a known-good encrypted | 1631 // encryption / decryption. Instead we will take a known-good encrypted |
| 1769 // message, decrypt it, re-encrypt it, then decrypt again, verifying that the | 1632 // message, decrypt it, re-encrypt it, then decrypt again, verifying that the |
| 1770 // original known cleartext is the result. | 1633 // original known cleartext is the result. |
| 1771 | 1634 |
| 1772 // The RSA public and private keys used for this test are produced by the | 1635 const std::vector<uint8> rsa_spki_der = |
| 1773 // openssl command line: | 1636 GetBytesFromHexString(test, "rsa_spki_der"); |
| 1774 // % openssl genrsa -out pair.pem 1024 | |
| 1775 // % openssl rsa -in pair.pem -out spki.der -outform DER -pubout | |
| 1776 // % openssl pkcs8 -topk8 -inform PEM -outform DER -in pair.pem -out | |
| 1777 // pkcs8.der -nocrypt | |
| 1778 // % xxd -p spki.der | |
| 1779 // % xxd -p pkcs8.der | |
| 1780 const std::string rsa_spki_der_hex = | |
| 1781 "30819f300d06092a864886f70d010101050003818d0030818902818100a8" | |
| 1782 "d30894b93f376f7822229bfd2483e50da944c4ab803ca31979e0f47e70bf" | |
| 1783 "683c687c6b3e80f280a237cea3643fd1f7f10f7cc664dbc2ecd45be53e1c" | |
| 1784 "9b15a53c37dbdad846c0f8340c472abc7821e4aa7df185867bf38228ac3e" | |
| 1785 "cc1d97d3c8b57e21ea6ba57b2bc3814a436e910ee8ab64a0b7743a927e94" | |
| 1786 "4d3420401f7dd50203010001"; | |
| 1787 const std::string rsa_pkcs8_der_hex = | |
| 1788 "30820276020100300d06092a864886f70d0101010500048202603082025c" | |
| 1789 "02010002818100a8d30894b93f376f7822229bfd2483e50da944c4ab803c" | |
| 1790 "a31979e0f47e70bf683c687c6b3e80f280a237cea3643fd1f7f10f7cc664" | |
| 1791 "dbc2ecd45be53e1c9b15a53c37dbdad846c0f8340c472abc7821e4aa7df1" | |
| 1792 "85867bf38228ac3ecc1d97d3c8b57e21ea6ba57b2bc3814a436e910ee8ab" | |
| 1793 "64a0b7743a927e944d3420401f7dd5020301000102818100896cdffb50a0" | |
| 1794 "691bd00ad9696933243a7c5861a64684e8d74b91aed0d76c28234da9303e" | |
| 1795 "8c6ea2f89b141a9d5ea9a4ddd3d8eb9503dcf05ba0b1fd76060b281e3ae4" | |
| 1796 "b9d497fb5519bdf1127db8ad412d6a722686c78df3e3002acca960c6b2a2" | |
| 1797 "42a83ace5410693c03ce3d74cb9c9a7bacc8e271812920d1f53fee9312ef" | |
| 1798 "4eb1024100d09c14418ce92af7cc62f7cdc79836d8c6e3d0d33e7229cc11" | |
| 1799 "d732cbac75aa4c56c92e409a3ccbe75d4ce63ac5adca33080690782c6371" | |
| 1800 "e3628134c3534ca603024100cf2d3206f6deea2f39b70351c51f85436200" | |
| 1801 "5aa8f643e49e22486736d536e040dc30a2b4f9be3ab212a88d1891280874" | |
| 1802 "b9a170cdeb22eaf61c27c4b082c7d1470240638411a5b3b307ec6e744802" | |
| 1803 "c2d4ba556f8bfe72c7b76e790b89bd91ac13f5c9b51d04138d80b3450c1d" | |
| 1804 "4337865601bf96748b36c8f627be719f71ac3c70b441024065ce92cfe34e" | |
| 1805 "a58bf173a2b8f3024b4d5282540ac581957db3e11a7f528535ec098808dc" | |
| 1806 "a0013ffcb3b88a25716757c86c540e07d2ad8502cdd129118822c30f0240" | |
| 1807 "420a4983040e9db46eb29f1315a0d7b41cf60428f7460fce748e9a1a7d22" | |
| 1808 "d7390fa328948e7e9d1724401374e99d45eb41474781201378a4330e8e80" | |
| 1809 "8ce63551"; | |
| 1810 | 1637 |
| 1811 // Similarly, the cleartext and public key encrypted ciphertext for this test | 1638 const std::vector<uint8> rsa_pkcs8_der = |
| 1812 // are also produced by openssl. Note that since we are using a 1024-bit key, | 1639 GetBytesFromHexString(test, "rsa_pkcs8_der"); |
| 1813 // the cleartext size must be less than or equal to 117 bytes (modulusLength / | 1640 const std::vector<uint8> ciphertext = |
| 1814 // 8 - 11). | 1641 GetBytesFromHexString(test, "ciphertext"); |
| 1815 // % openssl rand -out cleartext.bin 64 | 1642 const std::vector<uint8> cleartext = |
| 1816 // % openssl rsautl -encrypt -inkey spki.der -keyform DER -pubin -in | 1643 GetBytesFromHexString(test, "cleartext"); |
| 1817 // cleartext.bin -out ciphertext.bin | |
| 1818 // % xxd -p cleartext.bin | |
| 1819 // % xxd -p ciphertext.bin | |
| 1820 const std::string cleartext_hex = | |
| 1821 "ec358ed141c45d7e03d4c6338aebad718e8bcbbf8f8ee6f8d9f4b9ef06d8" | |
| 1822 "84739a398c6bcbc688418b2ff64761dc0ccd40e7d52bed03e06946d0957a" | |
| 1823 "eef9e822"; | |
| 1824 const std::string ciphertext_hex = | |
| 1825 "6106441c2b7a4b1a16260ed1ae4fe6135247345dc8e674754bbda6588c6c" | |
| 1826 "0d95a3d4d26bb34cdbcbe327723e80343bd7a15cd4c91c3a44e6cb9c6cd6" | |
| 1827 "7ad2e8bf41523188d9b36dc364a838642dcbc2c25e85dfb2106ba47578ca" | |
| 1828 "3bbf8915055aea4fa7c3cbfdfbcc163f04c234fb6d847f39bab9612ecbee" | |
| 1829 "04626e945c3ccf42"; | |
| 1830 | 1644 |
| 1831 // Import the key pair. | 1645 // Import the key pair. |
| 1832 blink::WebCryptoAlgorithm algorithm = | 1646 blink::WebCryptoAlgorithm algorithm = |
| 1833 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); | 1647 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); |
| 1834 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 1648 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
| 1835 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 1649 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
| 1836 ImportRsaKeyPair( | 1650 ImportRsaKeyPair( |
| 1837 rsa_spki_der_hex, | 1651 rsa_spki_der, |
| 1838 rsa_pkcs8_der_hex, | 1652 rsa_pkcs8_der, |
| 1839 algorithm, | 1653 algorithm, |
| 1840 false, | 1654 false, |
| 1841 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, | 1655 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, |
| 1842 &public_key, | 1656 &public_key, |
| 1843 &private_key); | 1657 &private_key); |
| 1844 | 1658 |
| 1845 // Decrypt the known-good ciphertext with the private key. As a check we must | 1659 // Decrypt the known-good ciphertext with the private key. As a check we must |
| 1846 // get the known original cleartext. | 1660 // get the known original cleartext. |
| 1847 blink::WebArrayBuffer decrypted_data; | 1661 blink::WebArrayBuffer decrypted_data; |
| 1848 ASSERT_STATUS_SUCCESS(DecryptInternal( | 1662 ASSERT_STATUS_SUCCESS(DecryptInternal( |
| 1849 algorithm, | 1663 algorithm, |
| 1850 private_key, | 1664 private_key, |
| 1851 HexStringToBytes(ciphertext_hex), | 1665 ciphertext, |
| 1852 &decrypted_data)); | 1666 &decrypted_data)); |
| 1853 EXPECT_FALSE(decrypted_data.isNull()); | 1667 EXPECT_FALSE(decrypted_data.isNull()); |
| 1854 ExpectArrayBufferMatchesHex(cleartext_hex, decrypted_data); | 1668 ExpectArrayBufferMatches(cleartext, decrypted_data); |
| 1855 | 1669 |
| 1856 // Encrypt this decrypted data with the public key. | 1670 // Encrypt this decrypted data with the public key. |
| 1857 blink::WebArrayBuffer encrypted_data; | 1671 blink::WebArrayBuffer encrypted_data; |
| 1858 ASSERT_STATUS_SUCCESS(EncryptInternal( | 1672 ASSERT_STATUS_SUCCESS(EncryptInternal( |
| 1859 algorithm, | 1673 algorithm, |
| 1860 public_key, | 1674 public_key, |
| 1861 reinterpret_cast<const unsigned char*>(decrypted_data.data()), | 1675 reinterpret_cast<const unsigned char*>(decrypted_data.data()), |
| 1862 decrypted_data.byteLength(), | 1676 decrypted_data.byteLength(), |
| 1863 &encrypted_data)); | 1677 &encrypted_data)); |
| 1864 EXPECT_EQ(128u, encrypted_data.byteLength()); | 1678 EXPECT_EQ(128u, encrypted_data.byteLength()); |
| 1865 | 1679 |
| 1866 // Finally, decrypt the newly encrypted result with the private key, and | 1680 // Finally, decrypt the newly encrypted result with the private key, and |
| 1867 // compare to the known original cleartext. | 1681 // compare to the known original cleartext. |
| 1868 decrypted_data.reset(); | 1682 decrypted_data.reset(); |
| 1869 ASSERT_STATUS_SUCCESS(DecryptInternal( | 1683 ASSERT_STATUS_SUCCESS(DecryptInternal( |
| 1870 algorithm, | 1684 algorithm, |
| 1871 private_key, | 1685 private_key, |
| 1872 reinterpret_cast<const unsigned char*>(encrypted_data.data()), | 1686 reinterpret_cast<const unsigned char*>(encrypted_data.data()), |
| 1873 encrypted_data.byteLength(), | 1687 encrypted_data.byteLength(), |
| 1874 &decrypted_data)); | 1688 &decrypted_data)); |
| 1875 EXPECT_FALSE(decrypted_data.isNull()); | 1689 EXPECT_FALSE(decrypted_data.isNull()); |
| 1876 ExpectArrayBufferMatchesHex(cleartext_hex, decrypted_data); | 1690 ExpectArrayBufferMatches(cleartext, decrypted_data); |
| 1877 } | 1691 } |
| 1878 | 1692 |
| 1879 TEST_F(WebCryptoImplTest, MAYBE(RsaEsFailures)) { | 1693 TEST_F(WebCryptoImplTest, MAYBE(RsaEsFailures)) { |
| 1880 // Import a key pair. | 1694 // Import a key pair. |
| 1881 blink::WebCryptoAlgorithm algorithm = | 1695 blink::WebCryptoAlgorithm algorithm = |
| 1882 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); | 1696 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); |
| 1883 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 1697 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
| 1884 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 1698 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
| 1885 ImportRsaKeyPair( | 1699 ImportRsaKeyPair( |
| 1886 kPublicKeySpkiDerHex, | 1700 HexStringToBytes(kPublicKeySpkiDerHex), |
| 1887 kPrivateKeyPkcs8DerHex, | 1701 HexStringToBytes(kPrivateKeyPkcs8DerHex), |
| 1888 algorithm, | 1702 algorithm, |
| 1889 false, | 1703 false, |
| 1890 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, | 1704 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, |
| 1891 &public_key, | 1705 &public_key, |
| 1892 &private_key); | 1706 &private_key); |
| 1893 | 1707 |
| 1894 // Fail encrypt with a private key. | 1708 // Fail encrypt with a private key. |
| 1895 blink::WebArrayBuffer encrypted_data; | 1709 blink::WebArrayBuffer encrypted_data; |
| 1896 const std::string message_hex_str("0102030405060708090a0b0c0d0e0f"); | 1710 const std::string message_hex_str("0102030405060708090a0b0c0d0e0f"); |
| 1897 const std::vector<uint8> message_hex(HexStringToBytes(message_hex_str)); | 1711 const std::vector<uint8> message_hex(HexStringToBytes(message_hex_str)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1947 } | 1761 } |
| 1948 | 1762 |
| 1949 TEST_F(WebCryptoImplTest, MAYBE(RsaSsaSignVerifyFailures)) { | 1763 TEST_F(WebCryptoImplTest, MAYBE(RsaSsaSignVerifyFailures)) { |
| 1950 // Import a key pair. | 1764 // Import a key pair. |
| 1951 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash( | 1765 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash( |
| 1952 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 1766 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 1953 blink::WebCryptoAlgorithmIdSha1); | 1767 blink::WebCryptoAlgorithmIdSha1); |
| 1954 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 1768 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
| 1955 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 1769 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
| 1956 ImportRsaKeyPair( | 1770 ImportRsaKeyPair( |
| 1957 kPublicKeySpkiDerHex, | 1771 HexStringToBytes(kPublicKeySpkiDerHex), |
| 1958 kPrivateKeyPkcs8DerHex, | 1772 HexStringToBytes(kPrivateKeyPkcs8DerHex), |
| 1959 algorithm, | 1773 algorithm, |
| 1960 false, | 1774 false, |
| 1961 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, | 1775 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, |
| 1962 &public_key, | 1776 &public_key, |
| 1963 &private_key); | 1777 &private_key); |
| 1964 | 1778 |
| 1965 blink::WebArrayBuffer signature; | 1779 blink::WebArrayBuffer signature; |
| 1966 bool signature_match; | 1780 bool signature_match; |
| 1967 | 1781 |
| 1968 // Compute a signature. | 1782 // Compute a signature. |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2071 blink::WebCryptoAlgorithmIdSha256), | 1885 blink::WebCryptoAlgorithmIdSha256), |
| 2072 public_key, | 1886 public_key, |
| 2073 static_cast<const unsigned char*>(signature.data()), | 1887 static_cast<const unsigned char*>(signature.data()), |
| 2074 signature.byteLength(), | 1888 signature.byteLength(), |
| 2075 data, | 1889 data, |
| 2076 &is_match)); | 1890 &is_match)); |
| 2077 EXPECT_FALSE(is_match); | 1891 EXPECT_FALSE(is_match); |
| 2078 } | 1892 } |
| 2079 | 1893 |
| 2080 TEST_F(WebCryptoImplTest, MAYBE(RsaSignVerifyKnownAnswer)) { | 1894 TEST_F(WebCryptoImplTest, MAYBE(RsaSignVerifyKnownAnswer)) { |
| 2081 // Use the NIST test vectors from Example 1 of | 1895 scoped_ptr<base::ListValue> tests; |
| 2082 // ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt | 1896 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests)); |
| 2083 // These vectors are known answers for RSA PKCS#1 v1.5 Signature with a SHA-1 | |
| 2084 // digest, using a predefined key pair. | |
| 2085 | |
| 2086 struct TestCase { | |
| 2087 const std::string message_hex; | |
| 2088 const std::string signature_hex; | |
| 2089 }; | |
| 2090 | |
| 2091 // The following data are the input messages and corresponding computed RSA | |
| 2092 // PKCS#1 v1.5 signatures from the NIST link above. | |
| 2093 const TestCase kTests[] = { | |
| 2094 // PKCS#1 v1.5 Signature Example 1.1 | |
| 2095 {"cdc87da223d786df3b45e0bbbc721326d1ee2af806cc315475cc6f0d9c66e1b6" | |
| 2096 "2371d45ce2392e1ac92844c310102f156a0d8d52c1f4c40ba3aa65095786cb76" | |
| 2097 "9757a6563ba958fed0bcc984e8b517a3d5f515b23b8a41e74aa867693f90dfb0" | |
| 2098 "61a6e86dfaaee64472c00e5f20945729cbebe77f06ce78e08f4098fba41f9d61" | |
| 2099 "93c0317e8b60d4b6084acb42d29e3808a3bc372d85e331170fcbf7cc72d0b71c" | |
| 2100 "296648b3a4d10f416295d0807aa625cab2744fd9ea8fd223c42537029828bd16" | |
| 2101 "be02546f130fd2e33b936d2676e08aed1b73318b750a0167d0", | |
| 2102 "6bc3a06656842930a247e30d5864b4d819236ba7c68965862ad7dbc4e24af28e" | |
| 2103 "86bb531f03358be5fb74777c6086f850caef893f0d6fcc2d0c91ec013693b4ea" | |
| 2104 "00b80cd49aac4ecb5f8911afe539ada4a8f3823d1d13e472d1490547c659c761" | |
| 2105 "7f3d24087ddb6f2b72096167fc097cab18e9a458fcb634cdce8ee35894c484d7"}, | |
| 2106 // PKCS#1 v1.5 Signature Example 1.2 | |
| 2107 {"851384cdfe819c22ed6c4ccb30daeb5cf059bc8e1166b7e3530c4c233e2b5f8f" | |
| 2108 "71a1cca582d43ecc72b1bca16dfc7013226b9e", | |
| 2109 "84fd2ce734ec1da828d0f15bf49a8707c15d05948136de537a3db421384167c8" | |
| 2110 "6fae022587ee9e137daee754738262932d271c744c6d3a189ad4311bdb020492" | |
| 2111 "e322fbddc40406ea860d4e8ea2a4084aa98b9622a446756fdb740ddb3d91db76" | |
| 2112 "70e211661bbf8709b11c08a70771422d1a12def29f0688a192aebd89e0f896f8"}, | |
| 2113 // PKCS#1 v1.5 Signature Example1.3 | |
| 2114 {"a4b159941761c40c6a82f2b80d1b94f5aa2654fd17e12d588864679b54cd04ef" | |
| 2115 "8bd03012be8dc37f4b83af7963faff0dfa225477437c48017ff2be8191cf3955" | |
| 2116 "fc07356eab3f322f7f620e21d254e5db4324279fe067e0910e2e81ca2cab31c7" | |
| 2117 "45e67a54058eb50d993cdb9ed0b4d029c06d21a94ca661c3ce27fae1d6cb20f4" | |
| 2118 "564d66ce4767583d0e5f060215b59017be85ea848939127bd8c9c4d47b51056c" | |
| 2119 "031cf336f17c9980f3b8f5b9b6878e8b797aa43b882684333e17893fe9caa6aa" | |
| 2120 "299f7ed1a18ee2c54864b7b2b99b72618fb02574d139ef50f019c9eef4169713" | |
| 2121 "38e7d470", | |
| 2122 "0b1f2e5180e5c7b4b5e672929f664c4896e50c35134b6de4d5a934252a3a245f" | |
| 2123 "f48340920e1034b7d5a5b524eb0e1cf12befef49b27b732d2c19e1c43217d6e1" | |
| 2124 "417381111a1d36de6375cf455b3c9812639dbc27600c751994fb61799ecf7da6" | |
| 2125 "bcf51540afd0174db4033188556675b1d763360af46feeca5b60f882829ee7b2"}, | |
| 2126 // PKCS#1 v1.5 Signature Example 1.4 | |
| 2127 {"bc656747fa9eafb3f0", | |
| 2128 "45607ad611cf5747a41ac94d0ffec878bdaf63f6b57a4b088bf36e34e109f840" | |
| 2129 "f24b742ada16102dabf951cbc44f8982e94ed4cd09448d20ec0efa73545f80b6" | |
| 2130 "5406bed6194a61c340b4ad1568cbb75851049f11af1734964076e02029aee200" | |
| 2131 "e40e80be0f4361f69841c4f92a4450a2286d43289b405554c54d25c6ecb584f4"}, | |
| 2132 // PKCS#1 v1.5 Signature Example 1.5 | |
| 2133 {"b45581547e5427770c768e8b82b75564e0ea4e9c32594d6bff706544de0a8776" | |
| 2134 "c7a80b4576550eee1b2acabc7e8b7d3ef7bb5b03e462c11047eadd00629ae575" | |
| 2135 "480ac1470fe046f13a2bf5af17921dc4b0aa8b02bee6334911651d7f8525d10f" | |
| 2136 "32b51d33be520d3ddf5a709955a3dfe78283b9e0ab54046d150c177f037fdccc" | |
| 2137 "5be4ea5f68b5e5a38c9d7edcccc4975f455a6909b4", | |
| 2138 "54be9d90877515f450279c15b5f61ad6f15ecc95f18cbed82b65b1667a575809" | |
| 2139 "587994668044f3bc2ae7f884501f64f0b43f588cfa205a6ab704328c2d4ab92a" | |
| 2140 "7ae13440614d3e085f401da9ad28e2105e4a0edb681a6424df047388ce051ee9" | |
| 2141 "df7bc2163fe347520ad51ccd518064383e741acad3cbdc2cb5a7c68e868464c2"}, | |
| 2142 // PKCS#1 v1.5 Signature Example 1.6 | |
| 2143 {"10aae9a0ab0b595d0841207b700d48d75faedde3b775cd6b4cc88ae06e4694ec" | |
| 2144 "74ba18f8520d4f5ea69cbbe7cc2beba43efdc10215ac4eb32dc302a1f53dc6c4" | |
| 2145 "352267e7936cfebf7c8d67035784a3909fa859c7b7b59b8e39c5c2349f1886b7" | |
| 2146 "05a30267d402f7486ab4f58cad5d69adb17ab8cd0ce1caf5025af4ae24b1fb87" | |
| 2147 "94c6070cc09a51e2f9911311e3877d0044c71c57a993395008806b723ac38373" | |
| 2148 "d395481818528c1e7053739282053529510e935cd0fa77b8fa53cc2d474bd4fb" | |
| 2149 "3cc5c672d6ffdc90a00f9848712c4bcfe46c60573659b11e6457e861f0f604b6" | |
| 2150 "138d144f8ce4e2da73", | |
| 2151 "0e6ff63a856b9cbd5dbe423183122047dd39d6f76d1b2310e546fe9ee73b33ef" | |
| 2152 "a7c78f9474455c9e5b88cb383aafc3698668e7b7a59a9cbb5b0897b6c5afb7f8" | |
| 2153 "bac4b924e98d760a15fc43d2814ab2d5187f79bed9915a93397ebc22a7677506" | |
| 2154 "a02e076d3ffdc0441dbd4db00453dc28d830e0573f77b817b505c38b4a4bb5d0"}, | |
| 2155 // PKCS#1 v1.5 Signature Example 1.7 | |
| 2156 {"efb5da1b4d1e6d9a5dff92d0184da7e31f877d1281ddda625664869e8379e67a" | |
| 2157 "d3b75eae74a580e9827abd6eb7a002cb5411f5266797768fb8e95ae40e3e8b34" | |
| 2158 "66f5ab15d69553952939ec23e61d58497fac76aa1c0bb5a3cb4a54383587c7bb" | |
| 2159 "78d13eefda205443e6ce4365802df55c64713497984e7ca96722b3edf84d56", | |
| 2160 "8385d58533a995f72df262b70f40b391ddf515f464b9d2cc2d66398fc05689d8" | |
| 2161 "11632946d62eabdca7a31fcf6cd6c981d28bbc29083e4a6d5b2b378ca4e540f0" | |
| 2162 "60b96d53ad2693f82178b94e2e2f86b9accfa02025107e062ab7080175684501" | |
| 2163 "028f676461d81c008fe4750671649970878fc175cf98e96b2ecbf6874d77dacb"}, | |
| 2164 // PKCS#1 v1.5 Signature Example 1.8 | |
| 2165 {"53bb58ce42f1984940552657233b14969af365c0a561a4132af18af39432280e" | |
| 2166 "3e437082434b19231837184f02cf2b2e726bebf74d7ae3256d8b72f3eafdb134" | |
| 2167 "d33de06f2991d299d59f5468d43b9958d6a968f5969edbbc6e7185cbc716c7c9" | |
| 2168 "45dafa9cc71ddfaaa01094a452ddf5e2407320400bf05ea9729cafbf0600e788" | |
| 2169 "07ef9462e3fde32ed7d981a56f4751ef64fb4549910ecc911d728053b3994300" | |
| 2170 "4740e6f5821fe8d75c0617bf2c6b24bbfc34013fc95f0dedf5ba297f504fb833" | |
| 2171 "da2a436d1d8ff1cc5193e2a64389fced918e7feb6716330f66801db9497549cf" | |
| 2172 "1d3bd97cf1bc6255", | |
| 2173 "8e1f3d26ec7c6bbb8c54c5d25f3120587803af6d3c2b99a37ced6a3657d4ae54" | |
| 2174 "266f63fffde660c866d65d0ab0589e1d12d9ce6054b05c8668ae127171ccaae7" | |
| 2175 "f1cd409677f52157b6123ab227f27a00966d1439b42a32169d1070394026fc8b" | |
| 2176 "c93545b1ac252d0f7da751c02e33a47831fbd71514c2bbbd3adb6740c0fd68ad"}, | |
| 2177 // PKCS#1 v1.5 Signature Example 1.9 | |
| 2178 {"27cadc698450945f204ec3cf8c6cbd8ceb4cc0cbe312274fa96b04deac855160" | |
| 2179 "c0e04e4ac5d38210c27c", | |
| 2180 "7b63f9223356f35f6117f68c8f8220034fc2384ab5dc6904141f139314d6ee89" | |
| 2181 "f54ec6ffd18c413a23c5931c7fbb13c555ccfd590e0eaa853c8c94d2520cd425" | |
| 2182 "0d9a05a193b65dc749b82478af0156ee1de55ddad33ec1f0099cad6c891a3617" | |
| 2183 "c7393d05fbfbbb00528a001df0b204ebdf1a341090dea89f870a877458427f7b"}, | |
| 2184 // PKCS#1 v1.5 Signature Example 1.10 | |
| 2185 {"716407e901b9ef92d761b013fd13eb7ad72aed", | |
| 2186 "2a22dbe3774d5b297201b55a0f17f42dce63b7845cb325cfe951d0badb5c5a14" | |
| 2187 "472143d896c86cc339f83671164215abc97862f2151654e75a3b357c37311b3d" | |
| 2188 "7268cab540202e23bee52736f2cd86cce0c7dbde95e1c600a47395dc5eb0a472" | |
| 2189 "153fbc4fb21b643e0c04ae14dd37e97e617a7567c89652219781001ba6f83298"}, | |
| 2190 // PKCS#1 v1.5 Signature Example 1.11 | |
| 2191 {"46c24e4103001629c712dd4ce8d747ee595d6c744ccc4f71347d9b8abf49d1b8" | |
| 2192 "fb2ef91b95dc899d4c0e3d2997e638f4cf3f68e0498de5aabd13f0dfe02ff26b" | |
| 2193 "a4379104e78ffa95ffbd15067ef8cbd7eb7860fecc71abe13d5c720a66851f2d" | |
| 2194 "efd4e795054d7bec024bb422a46a7368b56d95b47aebafbeadd612812593a70d" | |
| 2195 "b9f96d451ee15edb299308d777f4bb68ed3377c32156b41b7a9c92a14c8b8114" | |
| 2196 "4399c56a5a432f4f770aa97da8415d0bda2e813206031e70620031c881d616bf" | |
| 2197 "fd5f03bf147c1e73766c26246208", | |
| 2198 "12235b0b406126d9d260d447e923a11051fb243079f446fd73a70181d53634d7" | |
| 2199 "a0968e4ee27777eda63f6e4a3a91ad5985998a4848da59ce697b24bb332fa2ad" | |
| 2200 "9ce462ca4affdc21dab908e8ce15af6eb9105b1abcf39142aa17b34c4c092386" | |
| 2201 "a7abbfe028afdbebc14f2ce26fbee5edeca11502d39a6b7403154843d98a62a7"}, | |
| 2202 // PKCS#1 v1.5 Signature Example 1.12 | |
| 2203 {"bc99a932aa16d622bfff79c50b4c42358673261129e28d6a918ff1b0f1c4f46a" | |
| 2204 "d8afa98b0ca0f56f967975b0a29be882e93b6cd3fc33e1faef72e52b2ae0a3f1" | |
| 2205 "2024506e25690e902e782982145556532284cf505789738f4da31fa1333d3af8" | |
| 2206 "62b2ba6b6ce7ab4cce6aba", | |
| 2207 "872ec5ad4f1846256f17e9936ac50e43e9963ea8c1e76f15879b7874d77d122a" | |
| 2208 "609dc8c561145b94bf4ffdffdeb17e6e76ffc6c10c0747f5e37a9f434f5609e7" | |
| 2209 "9da5250215a457afdf12c6507cc1551f54a28010595826a2c9b97fa0aa851cc6" | |
| 2210 "8b705d7a06d720ba027e4a1c0b019500fb63b78071684dcfa9772700b982dc66"}, | |
| 2211 // PKCS#1 v1.5 Signature Example 1.13 | |
| 2212 {"731e172ac063992c5b11ba170dfb23bb000d47ba195329cf278061037381514c" | |
| 2213 "146064c5285db130dd5bae98b772225950eab05d3ea996f6fffb9a8c8622913f" | |
| 2214 "279914c89ada4f3dd77666a868bfcbff2b95b7daf453d4e2c9d75beee7f8e709" | |
| 2215 "05e4066a4f73aecc67f956aa5a3292b8488c917d317cfdc86253e690381e15ab", | |
| 2216 "76204eacc1d63ec1d6ad5bd0692e1a2f686df6e64ca945c77a824de212efa6d9" | |
| 2217 "782d81b4591403ff4020620298c07ebd3a8a61c5bf4dad62cbfc4ae6a03937be" | |
| 2218 "4b49a216d570fc6e81872937876e27bd19cf601effc30ddca573c9d56cd4569b" | |
| 2219 "db4851c450c42cb21e738cdd61027b8be5e9b410fc46aa3f29e4be9e64451346"}, | |
| 2220 // PKCS#1 v1.5 Signature Example 1.14 | |
| 2221 {"0211382683a74d8d2a2cb6a06550563be1c26ca62821e4ff163b720464fc3a28" | |
| 2222 "d91bedddc62749a5538eaf41fbe0c82a77e06ad99383c9e985ffb8a93fd4d7c5" | |
| 2223 "8db51ad91ba461d69a8fd7ddabe2496757a0c49122c1a79a85cc0553e8214d03" | |
| 2224 "6dfe0185efa0d05860c612fa0882c82d246e5830a67355dff18a2c36b732f988" | |
| 2225 "cfedc562264c6254b40fcabb97b760947568dcd6a17cda6ee8855bddbab93702" | |
| 2226 "471aa0cfb1bed2e13118eba1175b73c96253c108d0b2aba05ab8e17e84392e20" | |
| 2227 "085f47404d8365527dc3fb8f2bb48a50038e71361ccf973407", | |
| 2228 "525500918331f1042eae0c5c2054aa7f92deb26991b5796634f229daf9b49eb2" | |
| 2229 "054d87319f3cfa9b466bd075ef6699aea4bd4a195a1c52968b5e2b75e092d846" | |
| 2230 "ea1b5cc27905a8e1d5e5de0edfdb21391ebb951864ebd9f0b0ec35b654287136" | |
| 2231 "0a317b7ef13ae06af684e38e21b1e19bc7298e5d6fe0013a164bfa25d3e7313d"}, | |
| 2232 // PKCS#1 v1.5 Signature Example 1.15 | |
| 2233 {"fc6b700d22583388ab2f8dafcaf1a05620698020da4bae44dafbd0877b501250" | |
| 2234 "6dc3181d5c66bf023f348b41fd9f94795ab96452a4219f2d39d72af359cf1956" | |
| 2235 "51c7", | |
| 2236 "4452a6cc2626b01e95ab306df0d0cc7484fbab3c22e9703283567f66eadc248d" | |
| 2237 "bda58fce7dd0c70cce3f150fca4b369dff3b6237e2b16281ab55b53fb13089c8" | |
| 2238 "5cd265056b3d62a88bfc2135b16791f7fbcab9fd2dc33becb617be419d2c0461" | |
| 2239 "42a4d47b338314552edd4b6fe9ce1104ecec4a9958d7331e930fc09bf08a6e64"}, | |
| 2240 // PKCS#1 v1.5 Signature Example 1.16 | |
| 2241 {"13ba086d709cfa5fedaa557a89181a6140f2300ed6d7c3febb6cf68abebcbc67" | |
| 2242 "8f2bca3dc2330295eec45bb1c4075f3ada987eae88b39c51606cb80429e649d9" | |
| 2243 "8acc8441b1f8897db86c5a4ce0abf28b1b81dca3667697b850696b74a5ebd85d" | |
| 2244 "ec56c90f8abe513efa857853720be319607921bca947522cd8fac8cace5b827c" | |
| 2245 "3e5a129e7ee57f6b84932f14141ac4274e8cbb46e6912b0d3e2177d499d1840c" | |
| 2246 "d47d4d7ae0b4cdc4d3", | |
| 2247 "1f3b5a87db72a2c97bb3eff2a65a301268eacd89f42abc1098c1f2de77b0832a" | |
| 2248 "65d7815feb35070063f221bb3453bd434386c9a3fde18e3ca1687fb649e86c51" | |
| 2249 "d658619dde5debb86fe15491ff77ab748373f1be508880d66ea81e870e91cdf1" | |
| 2250 "704875c17f0b10103188bc64eef5a3551b414c733670215b1a22702562581ab1"}, | |
| 2251 // PKCS#1 v1.5 Signature Example 1.17 | |
| 2252 {"eb1e5935", | |
| 2253 "370cb9839ae6074f84b2acd6e6f6b7921b4b523463757f6446716140c4e6c0e7" | |
| 2254 "5bec6ad0197ebfa86bf46d094f5f6cd36dca3a5cc73c8bbb70e2c7c9ab5d964e" | |
| 2255 "c8e3dfde481b4a1beffd01b4ad15b31ae7aebb9b70344a9411083165fdf9c375" | |
| 2256 "4bbb8b94dd34bd4813dfada1f6937de4267d5597ca09a31e83d7f1a79dd19b5e"}, | |
| 2257 // PKCS#1 v1.5 Signature Example 1.18 | |
| 2258 {"6346b153e889c8228209630071c8a57783f368760b8eb908cfc2b276", | |
| 2259 "2479c975c5b1ae4c4e940f473a9045b8bf5b0bfca78ec29a38dfbedc8a749b7a" | |
| 2260 "2692f7c52d5bc7c831c7232372a00fed3b6b49e760ec99e074ff2eead5134e83" | |
| 2261 "05725dfa39212b84bd4b8d80bc8bc17a512823a3beb18fc08e45ed19c26c8177" | |
| 2262 "07d67fb05832ef1f12a33e90cd93b8a780319e2963ca25a2af7b09ad8f595c21"}, | |
| 2263 // PKCS#1 v1.5 Signature Example 1.19 | |
| 2264 {"64702db9f825a0f3abc361974659f5e9d30c3aa4f56feac69050c72905e77fe0" | |
| 2265 "c22f88a378c21fcf45fe8a5c717302093929", | |
| 2266 "152f3451c858d69594e6567dfb31291c1ee7860b9d15ebd5a5edd276ac3e6f7a" | |
| 2267 "8d1480e42b3381d2be023acf7ebbdb28de3d2163ae44259c6df98c335d045b61" | |
| 2268 "dac9dba9dbbb4e6ab4a083cd76b580cbe472206a1a9fd60680ceea1a570a29b0" | |
| 2269 "881c775eaef5525d6d2f344c28837d0aca422bbb0f1aba8f6861ae18bd73fe44"}, | |
| 2270 // PKCS#1 v1.5 Signature Example 1.20 | |
| 2271 {"941921de4a1c9c1618d6f3ca3c179f6e29bae6ddf9a6a564f929e3ce82cf3265" | |
| 2272 "d7837d5e692be8dcc9e86c", | |
| 2273 "7076c287fc6fff2b20537435e5a3107ce4da10716186d01539413e609d27d1da" | |
| 2274 "6fd952c61f4bab91c045fa4f8683ecc4f8dde74227f773cff3d96db84718c494" | |
| 2275 "4b06affeba94b725f1b07d3928b2490a85c2f1abf492a9177a7cd2ea0c966875" | |
| 2276 "6f825bbec900fa8ac3824e114387ef573780ca334882387b94e5aad7a27a28dc"}}; | |
| 2277 | 1897 |
| 2278 // Import the key pair. | 1898 // Import the key pair. |
| 2279 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash( | 1899 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash( |
| 2280 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 1900 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 2281 blink::WebCryptoAlgorithmIdSha1); | 1901 blink::WebCryptoAlgorithmIdSha1); |
| 2282 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 1902 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
| 2283 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 1903 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
| 2284 ImportRsaKeyPair( | 1904 ImportRsaKeyPair( |
| 2285 kPublicKeySpkiDerHex, | 1905 HexStringToBytes(kPublicKeySpkiDerHex), |
| 2286 kPrivateKeyPkcs8DerHex, | 1906 HexStringToBytes(kPrivateKeyPkcs8DerHex), |
| 2287 algorithm, | 1907 algorithm, |
| 2288 false, | 1908 false, |
| 2289 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, | 1909 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, |
| 2290 &public_key, | 1910 &public_key, |
| 2291 &private_key); | 1911 &private_key); |
| 2292 | 1912 |
| 2293 // Validate the signatures are computed and verified as expected. | 1913 // Validate the signatures are computed and verified as expected. |
| 2294 blink::WebArrayBuffer signature; | 1914 blink::WebArrayBuffer signature; |
| 2295 for (size_t idx = 0; idx < ARRAYSIZE_UNSAFE(kTests); ++idx) { | 1915 for (size_t idx = 0; idx < tests->GetSize(); ++idx) { |
|
Ryan Sleevi
2014/01/31 20:53:36
naming: idx -> index? matches consistency with the
eroman
2014/01/31 22:55:11
Done (test_index throughout)
| |
| 2296 SCOPED_TRACE(idx); | 1916 SCOPED_TRACE(idx); |
| 2297 const TestCase& test = kTests[idx]; | 1917 |
| 2298 const std::vector<uint8> message = HexStringToBytes(test.message_hex); | 1918 base::DictionaryValue* test; |
| 1919 ASSERT_TRUE(tests->GetDictionary(idx, &test)); | |
| 1920 | |
| 1921 std::vector<uint8> test_message = | |
| 1922 GetBytesFromHexString(test, "message_hex"); | |
| 1923 std::vector<uint8> test_signature = | |
| 1924 GetBytesFromHexString(test, "signature_hex"); | |
| 2299 | 1925 |
| 2300 signature.reset(); | 1926 signature.reset(); |
| 2301 ASSERT_STATUS_SUCCESS( | 1927 ASSERT_STATUS_SUCCESS( |
| 2302 SignInternal(algorithm, private_key, message, &signature)); | 1928 SignInternal(algorithm, private_key, test_message, &signature)); |
| 2303 ExpectArrayBufferMatchesHex(test.signature_hex, signature); | 1929 ExpectArrayBufferMatches(test_signature, signature); |
| 2304 | 1930 |
| 2305 bool is_match = false; | 1931 bool is_match = false; |
| 2306 ASSERT_STATUS_SUCCESS(VerifySignatureInternal( | 1932 ASSERT_STATUS_SUCCESS(VerifySignatureInternal( |
| 2307 algorithm, | 1933 algorithm, |
| 2308 public_key, | 1934 public_key, |
| 2309 HexStringToBytes(test.signature_hex), | 1935 test_signature, |
| 2310 message, | 1936 test_message, |
| 2311 &is_match)); | 1937 &is_match)); |
| 2312 EXPECT_TRUE(is_match); | 1938 EXPECT_TRUE(is_match); |
| 2313 } | 1939 } |
| 2314 } | 1940 } |
| 2315 | 1941 |
| 2316 TEST_F(WebCryptoImplTest, MAYBE(AesKwKeyImport)) { | 1942 TEST_F(WebCryptoImplTest, MAYBE(AesKwKeyImport)) { |
| 2317 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 1943 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 2318 blink::WebCryptoAlgorithm algorithm = | 1944 blink::WebCryptoAlgorithm algorithm = |
| 2319 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); | 1945 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); |
| 2320 | 1946 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2404 // * Test decryption when the tag length exceeds input size | 2030 // * Test decryption when the tag length exceeds input size |
| 2405 // * Test decryption with empty input | 2031 // * Test decryption with empty input |
| 2406 // * Test decryption with tag length of 0. | 2032 // * Test decryption with tag length of 0. |
| 2407 TEST_F(WebCryptoImplTest, MAYBE(AesGcmSampleSets)) { | 2033 TEST_F(WebCryptoImplTest, MAYBE(AesGcmSampleSets)) { |
| 2408 // Some Linux test runners may not have a new enough version of NSS. | 2034 // Some Linux test runners may not have a new enough version of NSS. |
| 2409 if (!SupportsAesGcm()) { | 2035 if (!SupportsAesGcm()) { |
| 2410 LOG(WARNING) << "AES GCM not supported, skipping tests"; | 2036 LOG(WARNING) << "AES GCM not supported, skipping tests"; |
| 2411 return; | 2037 return; |
| 2412 } | 2038 } |
| 2413 | 2039 |
| 2414 struct TestCase { | 2040 scoped_ptr<base::ListValue> tests; |
| 2415 const char* key; | 2041 ASSERT_TRUE(ReadJsonTestFileToList("aesgcm.json", &tests)); |
|
Ryan Sleevi
2014/01/31 20:53:36
aes_gcm
| |
| 2416 const char* iv; | |
| 2417 const char* plain_text; | |
| 2418 const char* cipher_text; | |
| 2419 const char* additional_data; | |
| 2420 const char* authentication_tag; | |
| 2421 }; | |
| 2422 | |
| 2423 // These tests come from the NIST GCM test vectors: | |
| 2424 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/gcmtestvectors.zip | |
| 2425 // | |
| 2426 // Both encryption and decryption are expected to work. | |
| 2427 TestCase kTests[] = { | |
| 2428 // [Keylen = 128] | |
| 2429 // [IVlen = 96] | |
| 2430 // [PTlen = 0] | |
| 2431 // [AADlen = 0] | |
| 2432 // [Taglen = 128] | |
| 2433 { | |
| 2434 // key | |
| 2435 "cf063a34d4a9a76c2c86787d3f96db71", | |
| 2436 // iv | |
| 2437 "113b9785971864c83b01c787", | |
| 2438 // plain_text | |
| 2439 "", | |
| 2440 // cipher_text | |
| 2441 "", | |
| 2442 // additional_data | |
| 2443 "", | |
| 2444 // authentication_tag | |
| 2445 "72ac8493e3a5228b5d130a69d2510e42", | |
| 2446 }, | |
| 2447 | |
| 2448 // [Keylen = 128] | |
| 2449 // [IVlen = 96] | |
| 2450 // [PTlen = 0] | |
| 2451 // [AADlen = 128] | |
| 2452 // [Taglen = 120] | |
| 2453 { | |
| 2454 // key | |
| 2455 "6dfa1a07c14f978020ace450ad663d18", | |
| 2456 // iv | |
| 2457 "34edfa462a14c6969a680ec1", | |
| 2458 // plain_text | |
| 2459 "", | |
| 2460 // cipher_text | |
| 2461 "", | |
| 2462 // additional_data | |
| 2463 "2a35c7f5f8578e919a581c60500c04f6", | |
| 2464 // authentication_tag | |
| 2465 "751f3098d59cf4ea1d2fb0853bde1c" | |
| 2466 }, | |
| 2467 | |
| 2468 // [Keylen = 128] | |
| 2469 // [IVlen = 96] | |
| 2470 // [PTlen = 128] | |
| 2471 // [AADlen = 128] | |
| 2472 // [Taglen = 112] | |
| 2473 { | |
| 2474 // key | |
| 2475 "ed6cd876ceba555706674445c229c12d", | |
| 2476 // iv | |
| 2477 "92ecbf74b765bc486383ca2e", | |
| 2478 // plain_text | |
| 2479 "bfaaaea3880d72d4378561e2597a9b35", | |
| 2480 // cipher_text | |
| 2481 "bdd2ed6c66fa087dce617d7fd1ff6d93", | |
| 2482 // additional_data | |
| 2483 "95bd10d77dbe0e87fb34217f1a2e5efe", | |
| 2484 // authentication_tag | |
| 2485 "ba82e49c55a22ed02ca67da4ec6f" | |
| 2486 }, | |
| 2487 | |
| 2488 // [Keylen = 192] | |
| 2489 // [IVlen = 96] | |
| 2490 // [PTlen = 128] | |
| 2491 // [AADlen = 384] | |
| 2492 // [Taglen = 112] | |
| 2493 { | |
| 2494 // key | |
| 2495 "ae7972c025d7f2ca3dd37dcc3d41c506671765087c6b61b8", | |
| 2496 // iv | |
| 2497 "984c1379e6ba961c828d792d", | |
| 2498 // plain_text | |
| 2499 "d30b02c343487105219d6fa080acc743", | |
| 2500 // cipher_text | |
| 2501 "c4489fa64a6edf80e7e6a3b8855bc37c", | |
| 2502 // additional_data | |
| 2503 "edd8f630f9bbc31b0acf122998f15589d6e6e3e1a3ec89e0c6a6ece751610e" | |
| 2504 "bbf57fdfb9d82028ff1d9faebe37a268c1", | |
| 2505 // authentication_tag | |
| 2506 "772ee7de0f91a981c36c93a35c88" | |
| 2507 } | |
| 2508 }; | |
| 2509 | 2042 |
| 2510 // Note that WebCrypto appends the authentication tag to the ciphertext. | 2043 // Note that WebCrypto appends the authentication tag to the ciphertext. |
| 2511 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(kTests); index++) { | 2044 for (size_t index = 0; index < tests->GetSize(); index++) { |
|
Ryan Sleevi
2014/01/31 20:53:36
++index
eroman
2014/01/31 22:55:11
Done.
| |
| 2512 SCOPED_TRACE(index); | 2045 SCOPED_TRACE(index); |
| 2513 const TestCase& test = kTests[index]; | 2046 base::DictionaryValue* test; |
| 2047 ASSERT_TRUE(tests->GetDictionary(index, &test)); | |
| 2514 | 2048 |
| 2515 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString( | 2049 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); |
| 2516 test.key, | 2050 const std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv"); |
| 2051 const std::vector<uint8> test_additional_data = | |
| 2052 GetBytesFromHexString(test, "additional_data"); | |
| 2053 const std::vector<uint8> test_plain_text = | |
| 2054 GetBytesFromHexString(test, "plain_text"); | |
| 2055 const std::vector<uint8> test_authentication_tag = | |
| 2056 GetBytesFromHexString(test, "authentication_tag"); | |
| 2057 const unsigned int test_tag_size_bits = test_authentication_tag.size() * 8; | |
| 2058 const std::vector<uint8> test_cipher_text = | |
| 2059 GetBytesFromHexString(test, "cipher_text"); | |
| 2060 | |
| 2061 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | |
| 2062 test_key, | |
| 2517 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), | 2063 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), |
| 2518 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); | 2064 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); |
| 2519 | 2065 |
| 2520 // Verify exported raw key is identical to the imported data | 2066 // Verify exported raw key is identical to the imported data |
| 2521 blink::WebArrayBuffer raw_key; | 2067 blink::WebArrayBuffer raw_key; |
| 2522 EXPECT_STATUS_SUCCESS(ExportKeyInternal( | 2068 EXPECT_STATUS_SUCCESS(ExportKeyInternal( |
| 2523 blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 2069 blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 2524 ExpectArrayBufferMatchesHex(test.key, raw_key); | 2070 ExpectArrayBufferMatches(test_key, raw_key); |
| 2525 | |
| 2526 const std::vector<uint8> test_iv = HexStringToBytes(test.iv); | |
| 2527 const std::vector<uint8> test_additional_data = | |
| 2528 HexStringToBytes(test.additional_data); | |
| 2529 const std::vector<uint8> test_plain_text = | |
| 2530 HexStringToBytes(test.plain_text); | |
| 2531 const std::vector<uint8> test_authentication_tag = | |
| 2532 HexStringToBytes(test.authentication_tag); | |
| 2533 const unsigned int test_tag_size_bits = test_authentication_tag.size() * 8; | |
| 2534 const std::vector<uint8> test_cipher_text = | |
| 2535 HexStringToBytes(test.cipher_text); | |
| 2536 | 2071 |
| 2537 // Test encryption. | 2072 // Test encryption. |
| 2538 std::vector<uint8> cipher_text; | 2073 std::vector<uint8> cipher_text; |
| 2539 std::vector<uint8> authentication_tag; | 2074 std::vector<uint8> authentication_tag; |
| 2540 EXPECT_STATUS_SUCCESS(AesGcmEncrypt(key, test_iv, test_additional_data, | 2075 EXPECT_STATUS_SUCCESS(AesGcmEncrypt(key, test_iv, test_additional_data, |
| 2541 test_tag_size_bits, test_plain_text, | 2076 test_tag_size_bits, test_plain_text, |
| 2542 &cipher_text, &authentication_tag)); | 2077 &cipher_text, &authentication_tag)); |
| 2543 | 2078 |
| 2544 ExpectVectorMatchesHex(test.cipher_text, cipher_text); | 2079 ExpectVectorMatches(test_cipher_text, cipher_text); |
| 2545 ExpectVectorMatchesHex(test.authentication_tag, authentication_tag); | 2080 ExpectVectorMatches(test_authentication_tag, authentication_tag); |
| 2546 | 2081 |
| 2547 // Test decryption. | 2082 // Test decryption. |
| 2548 blink::WebArrayBuffer plain_text; | 2083 blink::WebArrayBuffer plain_text; |
| 2549 EXPECT_STATUS_SUCCESS(AesGcmDecrypt(key, test_iv, test_additional_data, | 2084 EXPECT_STATUS_SUCCESS(AesGcmDecrypt(key, test_iv, test_additional_data, |
| 2550 test_tag_size_bits, test_cipher_text, | 2085 test_tag_size_bits, test_cipher_text, |
| 2551 test_authentication_tag, &plain_text)); | 2086 test_authentication_tag, &plain_text)); |
| 2552 ExpectArrayBufferMatchesHex(test.plain_text, plain_text); | 2087 ExpectArrayBufferMatches(test_plain_text, plain_text); |
| 2553 | 2088 |
| 2554 // Decryption should fail if any of the inputs are tampered with. | 2089 // Decryption should fail if any of the inputs are tampered with. |
| 2555 EXPECT_STATUS(Status::Error(), | 2090 EXPECT_STATUS(Status::Error(), |
| 2556 AesGcmDecrypt(key, Corrupted(test_iv), test_additional_data, | 2091 AesGcmDecrypt(key, Corrupted(test_iv), test_additional_data, |
| 2557 test_tag_size_bits, test_cipher_text, | 2092 test_tag_size_bits, test_cipher_text, |
| 2558 test_authentication_tag, &plain_text)); | 2093 test_authentication_tag, &plain_text)); |
| 2559 EXPECT_STATUS(Status::Error(), | 2094 EXPECT_STATUS(Status::Error(), |
| 2560 AesGcmDecrypt(key, test_iv, Corrupted(test_additional_data), | 2095 AesGcmDecrypt(key, test_iv, Corrupted(test_additional_data), |
| 2561 test_tag_size_bits, test_cipher_text, | 2096 test_tag_size_bits, test_cipher_text, |
| 2562 test_authentication_tag, &plain_text)); | 2097 test_authentication_tag, &plain_text)); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 2577 if (test_tag_size_bits == wrong_tag_size_bits) | 2112 if (test_tag_size_bits == wrong_tag_size_bits) |
| 2578 continue; | 2113 continue; |
| 2579 EXPECT_STATUS_ERROR(AesGcmDecrypt(key, test_iv, test_additional_data, | 2114 EXPECT_STATUS_ERROR(AesGcmDecrypt(key, test_iv, test_additional_data, |
| 2580 wrong_tag_size_bits, test_cipher_text, | 2115 wrong_tag_size_bits, test_cipher_text, |
| 2581 test_authentication_tag, &plain_text)); | 2116 test_authentication_tag, &plain_text)); |
| 2582 } | 2117 } |
| 2583 } | 2118 } |
| 2584 } | 2119 } |
| 2585 | 2120 |
| 2586 } // namespace content | 2121 } // namespace content |
| OLD | NEW |