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 657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 | 668 |
669 blink::WebCryptoAlgorithm importAlgorithm = | 669 blink::WebCryptoAlgorithm importAlgorithm = |
670 CreateHmacImportAlgorithm(test_hash.id()); | 670 CreateHmacImportAlgorithm(test_hash.id()); |
671 | 671 |
672 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | 672 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
673 test_key, | 673 test_key, |
674 importAlgorithm, | 674 importAlgorithm, |
675 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify); | 675 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify); |
676 | 676 |
677 EXPECT_EQ(test_hash.id(), key.algorithm().hmacParams()->hash().id()); | 677 EXPECT_EQ(test_hash.id(), key.algorithm().hmacParams()->hash().id()); |
| 678 #if defined(WEBCRYPTO_HMAC_KEY_HAS_LENGTH) |
| 679 EXPECT_EQ(test_key.size() * 8, key.algorithm().hmacParams()->lengthBits()); |
| 680 #endif |
678 | 681 |
679 // Verify exported raw key is identical to the imported data | 682 // Verify exported raw key is identical to the imported data |
680 blink::WebArrayBuffer raw_key; | 683 blink::WebArrayBuffer raw_key; |
681 EXPECT_STATUS_SUCCESS( | 684 EXPECT_STATUS_SUCCESS( |
682 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 685 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
683 EXPECT_TRUE(ArrayBufferMatches(test_key, raw_key)); | 686 EXPECT_TRUE(ArrayBufferMatches(test_key, raw_key)); |
684 | 687 |
685 blink::WebArrayBuffer output; | 688 blink::WebArrayBuffer output; |
686 | 689 |
687 ASSERT_STATUS_SUCCESS( | 690 ASSERT_STATUS_SUCCESS( |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
948 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 951 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
949 blink::WebCryptoAlgorithm algorithm = | 952 blink::WebCryptoAlgorithm algorithm = |
950 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 512); | 953 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 512); |
951 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); | 954 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); |
952 EXPECT_FALSE(key.isNull()); | 955 EXPECT_FALSE(key.isNull()); |
953 EXPECT_TRUE(key.handle()); | 956 EXPECT_TRUE(key.handle()); |
954 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 957 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
955 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 958 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
956 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 959 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
957 key.algorithm().hmacParams()->hash().id()); | 960 key.algorithm().hmacParams()->hash().id()); |
| 961 #if defined(WEBCRYPTO_HMAC_KEY_HAS_LENGTH) |
| 962 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); |
| 963 #endif |
958 | 964 |
959 blink::WebArrayBuffer raw_key; | 965 blink::WebArrayBuffer raw_key; |
960 ASSERT_STATUS_SUCCESS( | 966 ASSERT_STATUS_SUCCESS( |
961 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 967 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
962 EXPECT_EQ(64U, raw_key.byteLength()); | 968 EXPECT_EQ(64U, raw_key.byteLength()); |
963 keys.push_back(raw_key); | 969 keys.push_back(raw_key); |
964 } | 970 } |
965 // Ensure all entries in the key sample set are unique. This is a simplistic | 971 // Ensure all entries in the key sample set are unique. This is a simplistic |
966 // estimate of whether the generated keys appear random. | 972 // estimate of whether the generated keys appear random. |
967 EXPECT_FALSE(CopiesExist(keys)); | 973 EXPECT_FALSE(CopiesExist(keys)); |
968 } | 974 } |
969 | 975 |
970 // If the key length is not provided, then the block size is used. | 976 // If the key length is not provided, then the block size is used. |
971 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmacNoLength)) { | 977 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmacNoLength)) { |
972 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 978 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
973 blink::WebCryptoAlgorithm algorithm = | 979 blink::WebCryptoAlgorithm algorithm = |
974 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); | 980 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); |
975 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); | 981 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); |
976 EXPECT_TRUE(key.handle()); | 982 EXPECT_TRUE(key.handle()); |
977 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 983 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 984 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
| 985 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
| 986 key.algorithm().hmacParams()->hash().id()); |
| 987 #if defined(WEBCRYPTO_HMAC_KEY_HAS_LENGTH) |
| 988 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); |
| 989 #endif |
978 blink::WebArrayBuffer raw_key; | 990 blink::WebArrayBuffer raw_key; |
979 ASSERT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 991 ASSERT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
980 EXPECT_EQ(64U, raw_key.byteLength()); | 992 EXPECT_EQ(64U, raw_key.byteLength()); |
981 | 993 |
982 // The block size for HMAC SHA-512 is larger. | 994 // The block size for HMAC SHA-512 is larger. |
983 algorithm = CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); | 995 algorithm = CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); |
984 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); | 996 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); |
| 997 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
985 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, | 998 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, |
986 key.algorithm().hmacParams()->hash().id()); | 999 key.algorithm().hmacParams()->hash().id()); |
| 1000 #if defined(WEBCRYPTO_HMAC_KEY_HAS_LENGTH) |
| 1001 EXPECT_EQ(1024u, key.algorithm().hmacParams()->lengthBits()); |
| 1002 #endif |
987 ASSERT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 1003 ASSERT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
988 EXPECT_EQ(128U, raw_key.byteLength()); | 1004 EXPECT_EQ(128U, raw_key.byteLength()); |
989 } | 1005 } |
990 | 1006 |
991 TEST_F(SharedCryptoTest, MAYBE(ImportSecretKeyNoAlgorithm)) { | 1007 TEST_F(SharedCryptoTest, MAYBE(ImportSecretKeyNoAlgorithm)) { |
992 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 1008 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
993 | 1009 |
994 // This fails because the algorithm is null. | 1010 // This fails because the algorithm is null. |
995 EXPECT_STATUS(Status::ErrorMissingAlgorithmImportRawKey(), | 1011 EXPECT_STATUS(Status::ErrorMissingAlgorithmImportRawKey(), |
996 ImportKey(blink::WebCryptoKeyFormatRaw, | 1012 ImportKey(blink::WebCryptoKeyFormatRaw, |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1353 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); | 1369 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); |
1354 std::vector<uint8> json_vec = MakeJsonVector(dict); | 1370 std::vector<uint8> json_vec = MakeJsonVector(dict); |
1355 EXPECT_STATUS_SUCCESS(ImportKeyJwk( | 1371 EXPECT_STATUS_SUCCESS(ImportKeyJwk( |
1356 CryptoData(json_vec), algorithm, extractable, usage_mask, &key)); | 1372 CryptoData(json_vec), algorithm, extractable, usage_mask, &key)); |
1357 EXPECT_TRUE(key.handle()); | 1373 EXPECT_TRUE(key.handle()); |
1358 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 1374 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
1359 EXPECT_EQ(extractable, key.extractable()); | 1375 EXPECT_EQ(extractable, key.extractable()); |
1360 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 1376 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
1361 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, | 1377 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, |
1362 key.algorithm().hmacParams()->hash().id()); | 1378 key.algorithm().hmacParams()->hash().id()); |
| 1379 #if defined(WEBCRYPTO_HMAC_KEY_HAS_LENGTH) |
| 1380 EXPECT_EQ(320u, key.algorithm().hmacParams()->lengthBits()); |
| 1381 #endif |
1363 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, key.usages()); | 1382 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, key.usages()); |
1364 key = blink::WebCryptoKey::createNull(); | 1383 key = blink::WebCryptoKey::createNull(); |
1365 | 1384 |
1366 // Consistency rules when JWK value exists: Fail if inconsistency is found. | 1385 // Consistency rules when JWK value exists: Fail if inconsistency is found. |
1367 | 1386 |
1368 // Pass: All input values are consistent with the JWK values. | 1387 // Pass: All input values are consistent with the JWK values. |
1369 dict.Clear(); | 1388 dict.Clear(); |
1370 dict.SetString("kty", "oct"); | 1389 dict.SetString("kty", "oct"); |
1371 dict.SetString("alg", "HS256"); | 1390 dict.SetString("alg", "HS256"); |
1372 dict.SetString("use", "sig"); | 1391 dict.SetString("use", "sig"); |
(...skipping 1238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2611 blink::WebCryptoKeyUsageVerify, | 2630 blink::WebCryptoKeyUsageVerify, |
2612 &unwrapped_key)); | 2631 &unwrapped_key)); |
2613 | 2632 |
2614 // Validate the new key's attributes. | 2633 // Validate the new key's attributes. |
2615 EXPECT_FALSE(unwrapped_key.isNull()); | 2634 EXPECT_FALSE(unwrapped_key.isNull()); |
2616 EXPECT_TRUE(unwrapped_key.handle()); | 2635 EXPECT_TRUE(unwrapped_key.handle()); |
2617 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, unwrapped_key.type()); | 2636 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, unwrapped_key.type()); |
2618 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, unwrapped_key.algorithm().id()); | 2637 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, unwrapped_key.algorithm().id()); |
2619 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, | 2638 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, |
2620 unwrapped_key.algorithm().hmacParams()->hash().id()); | 2639 unwrapped_key.algorithm().hmacParams()->hash().id()); |
| 2640 #if defined(WEBCRYPTO_HMAC_KEY_HAS_LENGTH) |
| 2641 EXPECT_EQ(256u, unwrapped_key.algorithm().hmacParams()->lengthBits()); |
| 2642 #endif |
2621 EXPECT_EQ(true, unwrapped_key.extractable()); | 2643 EXPECT_EQ(true, unwrapped_key.extractable()); |
2622 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, unwrapped_key.usages()); | 2644 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, unwrapped_key.usages()); |
2623 | 2645 |
2624 // Export the new key's raw data and compare to the known original. | 2646 // Export the new key's raw data and compare to the known original. |
2625 blink::WebArrayBuffer raw_key; | 2647 blink::WebArrayBuffer raw_key; |
2626 EXPECT_STATUS_SUCCESS( | 2648 EXPECT_STATUS_SUCCESS( |
2627 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key)); | 2649 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key)); |
2628 EXPECT_TRUE(ArrayBufferMatches(key_data, raw_key)); | 2650 EXPECT_TRUE(ArrayBufferMatches(key_data, raw_key)); |
2629 } | 2651 } |
2630 | 2652 |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2974 wrapping_algorithm, | 2996 wrapping_algorithm, |
2975 key_algorithm, | 2997 key_algorithm, |
2976 true, | 2998 true, |
2977 blink::WebCryptoKeyUsageSign, | 2999 blink::WebCryptoKeyUsageSign, |
2978 &unwrapped_key)); | 3000 &unwrapped_key)); |
2979 } | 3001 } |
2980 | 3002 |
2981 } // namespace webcrypto | 3003 } // namespace webcrypto |
2982 | 3004 |
2983 } // namespace content | 3005 } // namespace content |
OLD | NEW |