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/child/webcrypto/shared_crypto.h" | 5 #include "content/child/webcrypto/shared_crypto.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 2352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2363 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; | 2363 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; |
2364 EXPECT_STATUS(Status::Error(), | 2364 EXPECT_STATUS(Status::Error(), |
2365 ImportKey(blink::WebCryptoKeyFormatRaw, | 2365 ImportKey(blink::WebCryptoKeyFormatRaw, |
2366 CryptoData(HexStringToBytes(key_raw_hex_in)), | 2366 CryptoData(HexStringToBytes(key_raw_hex_in)), |
2367 algorithm, | 2367 algorithm, |
2368 true, | 2368 true, |
2369 blink::WebCryptoKeyUsageWrapKey, | 2369 blink::WebCryptoKeyUsageWrapKey, |
2370 &key)); | 2370 &key)); |
2371 } | 2371 } |
2372 | 2372 |
| 2373 TEST_F(SharedCryptoTest, MAYBE(UnwrapFailures)) { |
| 2374 // This test exercises the code path common to all unwrap operations. |
| 2375 scoped_ptr<base::ListValue> tests; |
| 2376 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); |
| 2377 base::DictionaryValue* test; |
| 2378 ASSERT_TRUE(tests->GetDictionary(0, &test)); |
| 2379 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek"); |
| 2380 const std::vector<uint8> test_ciphertext = |
| 2381 GetBytesFromHexString(test, "ciphertext"); |
| 2382 |
| 2383 // Using a key that does not have unwrapKey usage should fail. |
| 2384 blink::WebCryptoKey bad_wrapping_key = ImportSecretKeyFromRaw( |
| 2385 test_kek, |
| 2386 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), |
| 2387 blink::WebCryptoKeyUsageDecrypt); // <-- should be UnwrapKey |
| 2388 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); |
| 2389 EXPECT_STATUS( |
| 2390 Status::ErrorUnexpected(), |
| 2391 UnwrapKey(blink::WebCryptoKeyFormatRaw, |
| 2392 CryptoData(test_ciphertext), |
| 2393 bad_wrapping_key, |
| 2394 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), |
| 2395 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 2396 true, |
| 2397 blink::WebCryptoKeyUsageEncrypt, |
| 2398 &unwrapped_key)); |
| 2399 |
| 2400 // Using a wrapping algorithm that does not match the wrapping key algorithm |
| 2401 // should fail. |
| 2402 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( |
| 2403 test_kek, |
| 2404 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), |
| 2405 blink::WebCryptoKeyUsageUnwrapKey); |
| 2406 EXPECT_STATUS( |
| 2407 Status::ErrorUnexpected(), |
| 2408 UnwrapKey(blink::WebCryptoKeyFormatRaw, |
| 2409 CryptoData(test_ciphertext), |
| 2410 wrapping_key, |
| 2411 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 2412 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 2413 true, |
| 2414 blink::WebCryptoKeyUsageEncrypt, |
| 2415 &unwrapped_key)); |
| 2416 } |
| 2417 |
2373 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyWrapUnwrapKnownAnswer)) { | 2418 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyWrapUnwrapKnownAnswer)) { |
2374 scoped_ptr<base::ListValue> tests; | 2419 scoped_ptr<base::ListValue> tests; |
2375 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); | 2420 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); |
2376 | 2421 |
2377 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { | 2422 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
2378 SCOPED_TRACE(test_index); | 2423 SCOPED_TRACE(test_index); |
2379 base::DictionaryValue* test; | 2424 base::DictionaryValue* test; |
2380 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); | 2425 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
2381 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek"); | 2426 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek"); |
2382 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); | 2427 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2525 UnwrapKey(blink::WebCryptoKeyFormatRaw, | 2570 UnwrapKey(blink::WebCryptoKeyFormatRaw, |
2526 CryptoData(Corrupted(test_ciphertext)), | 2571 CryptoData(Corrupted(test_ciphertext)), |
2527 wrapping_key, | 2572 wrapping_key, |
2528 wrapping_algorithm, | 2573 wrapping_algorithm, |
2529 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 2574 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
2530 true, | 2575 true, |
2531 blink::WebCryptoKeyUsageEncrypt, | 2576 blink::WebCryptoKeyUsageEncrypt, |
2532 &unwrapped_key)); | 2577 &unwrapped_key)); |
2533 } | 2578 } |
2534 | 2579 |
| 2580 TEST_F(SharedCryptoTest, MAYBE(AesKwJwkSymkeyUnwrapKnownData)) { |
| 2581 // The following data lists a known HMAC SHA-256 key, then a JWK |
| 2582 // representation of this key which was encrypted ("wrapped") using AES-KW and |
| 2583 // the following wrapping key. |
| 2584 // For reference, the intermediate clear JWK is |
| 2585 // {"alg":"HS256","ext":true,"k":<b64urlKey>,"key_ops":["verify"],"kty":"oct"} |
| 2586 // (Not shown is space padding to ensure the cleartext meets the size |
| 2587 // requirements of the AES-KW algorithm.) |
| 2588 const std::vector<uint8> key_data = HexStringToBytes( |
| 2589 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"); |
| 2590 const std::vector<uint8> wrapped_key_data = HexStringToBytes( |
| 2591 "14E6380B35FDC5B72E1994764B6CB7BFDD64E7832894356AAEE6C3768FC3D0F115E6B0" |
| 2592 "6729756225F999AA99FDF81FD6A359F1576D3D23DE6CB69C3937054EB497AC1E8C38D5" |
| 2593 "5E01B9783A20C8D930020932CF25926103002213D0FC37279888154FEBCEDF31832158" |
| 2594 "97938C5CFE5B10B4254D0C399F39D0"); |
| 2595 const std::vector<uint8> wrapping_key_data = |
| 2596 HexStringToBytes("000102030405060708090A0B0C0D0E0F"); |
| 2597 const blink::WebCryptoAlgorithm wrapping_algorithm = |
| 2598 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); |
| 2599 |
| 2600 // Import the wrapping key. |
| 2601 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( |
| 2602 wrapping_key_data, wrapping_algorithm, blink::WebCryptoKeyUsageUnwrapKey); |
| 2603 |
| 2604 // Unwrap the known wrapped key data to produce a new key |
| 2605 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); |
| 2606 ASSERT_STATUS_SUCCESS(UnwrapKey(blink::WebCryptoKeyFormatJwk, |
| 2607 CryptoData(wrapped_key_data), |
| 2608 wrapping_key, |
| 2609 wrapping_algorithm, |
| 2610 blink::WebCryptoAlgorithm::createNull(), |
| 2611 true, |
| 2612 blink::WebCryptoKeyUsageVerify, |
| 2613 &unwrapped_key)); |
| 2614 |
| 2615 // Validate the new key's attributes. |
| 2616 EXPECT_FALSE(unwrapped_key.isNull()); |
| 2617 EXPECT_TRUE(unwrapped_key.handle()); |
| 2618 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, unwrapped_key.type()); |
| 2619 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, unwrapped_key.algorithm().id()); |
| 2620 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, |
| 2621 unwrapped_key.algorithm().hmacParams()->hash().id()); |
| 2622 EXPECT_EQ(true, unwrapped_key.extractable()); |
| 2623 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, unwrapped_key.usages()); |
| 2624 |
| 2625 // Export the new key's raw data and compare to the known original. |
| 2626 blink::WebArrayBuffer raw_key; |
| 2627 EXPECT_STATUS_SUCCESS( |
| 2628 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key)); |
| 2629 EXPECT_TRUE(ArrayBufferMatches(key_data, raw_key)); |
| 2630 } |
| 2631 |
2535 // TODO(eroman): | 2632 // TODO(eroman): |
2536 // * Test decryption when the tag length exceeds input size | 2633 // * Test decryption when the tag length exceeds input size |
2537 // * Test decryption with empty input | 2634 // * Test decryption with empty input |
2538 // * Test decryption with tag length of 0. | 2635 // * Test decryption with tag length of 0. |
2539 TEST_F(SharedCryptoTest, MAYBE(AesGcmSampleSets)) { | 2636 TEST_F(SharedCryptoTest, MAYBE(AesGcmSampleSets)) { |
2540 // Some Linux test runners may not have a new enough version of NSS. | 2637 // Some Linux test runners may not have a new enough version of NSS. |
2541 if (!SupportsAesGcm()) { | 2638 if (!SupportsAesGcm()) { |
2542 LOG(WARNING) << "AES GCM not supported, skipping tests"; | 2639 LOG(WARNING) << "AES GCM not supported, skipping tests"; |
2543 return; | 2640 return; |
2544 } | 2641 } |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2807 EXPECT_STATUS(Status::ErrorDataTooSmall(), | 2904 EXPECT_STATUS(Status::ErrorDataTooSmall(), |
2808 UnwrapKey(blink::WebCryptoKeyFormatRaw, | 2905 UnwrapKey(blink::WebCryptoKeyFormatRaw, |
2809 CryptoData(emtpy_data), | 2906 CryptoData(emtpy_data), |
2810 private_key, | 2907 private_key, |
2811 wrapping_algorithm, | 2908 wrapping_algorithm, |
2812 key_algorithm, | 2909 key_algorithm, |
2813 true, | 2910 true, |
2814 blink::WebCryptoKeyUsageSign, | 2911 blink::WebCryptoKeyUsageSign, |
2815 &unwrapped_key)); | 2912 &unwrapped_key)); |
2816 | 2913 |
2817 // Unwapping data too large for the wrapping key should fail. | 2914 // Unwrapping data too large for the wrapping key should fail. |
2818 EXPECT_STATUS(Status::ErrorDataTooLarge(), | 2915 EXPECT_STATUS(Status::ErrorDataTooLarge(), |
2819 UnwrapKey(blink::WebCryptoKeyFormatRaw, | 2916 UnwrapKey(blink::WebCryptoKeyFormatRaw, |
2820 CryptoData(big_data), | 2917 CryptoData(big_data), |
2821 private_key, | 2918 private_key, |
2822 wrapping_algorithm, | 2919 wrapping_algorithm, |
2823 key_algorithm, | 2920 key_algorithm, |
2824 true, | 2921 true, |
2825 blink::WebCryptoKeyUsageSign, | 2922 blink::WebCryptoKeyUsageSign, |
2826 &unwrapped_key)); | 2923 &unwrapped_key)); |
2827 } | 2924 } |
2828 | 2925 |
2829 } // namespace webcrypto | 2926 } // namespace webcrypto |
2830 | 2927 |
2831 } // namespace content | 2928 } // namespace content |
OLD | NEW |