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