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 "base/stl_util.h" | 5 #include "base/stl_util.h" |
6 #include "content/child/webcrypto/algorithm_dispatch.h" | 6 #include "content/child/webcrypto/algorithm_dispatch.h" |
7 #include "content/child/webcrypto/crypto_data.h" | 7 #include "content/child/webcrypto/crypto_data.h" |
8 #include "content/child/webcrypto/status.h" | 8 #include "content/child/webcrypto/status.h" |
9 #include "content/child/webcrypto/test/test_helpers.h" | 9 #include "content/child/webcrypto/test/test_helpers.h" |
10 #include "content/child/webcrypto/webcrypto_util.h" | 10 #include "content/child/webcrypto/webcrypto_util.h" |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 ++key_length_i) { | 256 ++key_length_i) { |
257 blink::WebCryptoKey key; | 257 blink::WebCryptoKey key; |
258 | 258 |
259 std::vector<std::vector<uint8_t> > keys; | 259 std::vector<std::vector<uint8_t> > keys; |
260 std::vector<uint8_t> key_bytes; | 260 std::vector<uint8_t> key_bytes; |
261 | 261 |
262 // Generate a small sample of keys. | 262 // Generate a small sample of keys. |
263 for (int j = 0; j < 16; ++j) { | 263 for (int j = 0; j < 16; ++j) { |
264 ASSERT_EQ(Status::Success(), | 264 ASSERT_EQ(Status::Success(), |
265 GenerateSecretKey( | 265 GenerateSecretKey( |
266 CreateAesCbcKeyGenAlgorithm(kKeyLength[key_length_i]), true, | 266 CreateAesCbcKeyGenAlgorithm(kKeyLength[key_length_i]), |
267 blink::WebCryptoKeyUsageEncrypt, &key)); | 267 true, |
| 268 0, |
| 269 &key)); |
268 EXPECT_TRUE(key.handle()); | 270 EXPECT_TRUE(key.handle()); |
269 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 271 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
270 ASSERT_EQ(Status::Success(), | 272 ASSERT_EQ(Status::Success(), |
271 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); | 273 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); |
272 EXPECT_EQ(key_bytes.size() * 8, | 274 EXPECT_EQ(key_bytes.size() * 8, |
273 key.algorithm().aesParams()->lengthBits()); | 275 key.algorithm().aesParams()->lengthBits()); |
274 keys.push_back(key_bytes); | 276 keys.push_back(key_bytes); |
275 } | 277 } |
276 // Ensure all entries in the key sample set are unique. This is a simplistic | 278 // Ensure all entries in the key sample set are unique. This is a simplistic |
277 // estimate of whether the generated keys appear random. | 279 // estimate of whether the generated keys appear random. |
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
953 SCOPED_TRACE(i); | 955 SCOPED_TRACE(i); |
954 | 956 |
955 blink::WebCryptoKey key; | 957 blink::WebCryptoKey key; |
956 | 958 |
957 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), | 959 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), |
958 GenerateSecretKey( | 960 GenerateSecretKey( |
959 CreateAesCbcKeyGenAlgorithm(128), true, bad_usages[i], &key)); | 961 CreateAesCbcKeyGenAlgorithm(128), true, bad_usages[i], &key)); |
960 } | 962 } |
961 } | 963 } |
962 | 964 |
963 // Generate an AES-CBC key with no usages. | |
964 TEST(WebCryptoAesCbcTest, GenerateKeyEmptyUsages) { | |
965 blink::WebCryptoKey key; | |
966 | |
967 ASSERT_EQ(Status::ErrorCreateKeyEmptyUsages(), | |
968 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), true, 0, &key)); | |
969 } | |
970 | |
971 // Generate an AES-CBC key and an RSA key pair. Use the AES-CBC key to wrap the | 965 // Generate an AES-CBC key and an RSA key pair. Use the AES-CBC key to wrap the |
972 // key pair (using SPKI format for public key, PKCS8 format for private key). | 966 // key pair (using SPKI format for public key, PKCS8 format for private key). |
973 // Then unwrap the wrapped key pair and verify that the key data is the same. | 967 // Then unwrap the wrapped key pair and verify that the key data is the same. |
974 TEST(WebCryptoAesCbcTest, WrapUnwrapRoundtripSpkiPkcs8) { | 968 TEST(WebCryptoAesCbcTest, WrapUnwrapRoundtripSpkiPkcs8) { |
975 if (!SupportsRsaPrivateKeyImport()) | 969 if (!SupportsRsaPrivateKeyImport()) |
976 return; | 970 return; |
977 | 971 |
978 // Generate the wrapping key. | 972 // Generate the wrapping key. |
979 blink::WebCryptoKey wrapping_key; | 973 blink::WebCryptoKey wrapping_key; |
980 ASSERT_EQ(Status::Success(), | 974 ASSERT_EQ(Status::Success(), |
981 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), | 975 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), |
982 true, | 976 true, |
983 blink::WebCryptoKeyUsageWrapKey | | 977 blink::WebCryptoKeyUsageWrapKey | |
984 blink::WebCryptoKeyUsageUnwrapKey, | 978 blink::WebCryptoKeyUsageUnwrapKey, |
985 &wrapping_key)); | 979 &wrapping_key)); |
986 | 980 |
987 // Generate an RSA key pair to be wrapped. | 981 // Generate an RSA key pair to be wrapped. |
988 const unsigned int modulus_length = 256; | 982 const unsigned int modulus_length = 256; |
989 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); | 983 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); |
990 | 984 |
991 blink::WebCryptoKey public_key; | 985 blink::WebCryptoKey public_key; |
992 blink::WebCryptoKey private_key; | 986 blink::WebCryptoKey private_key; |
993 ASSERT_EQ(Status::Success(), | 987 ASSERT_EQ(Status::Success(), |
994 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( | 988 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( |
995 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 989 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
996 blink::WebCryptoAlgorithmIdSha256, | 990 blink::WebCryptoAlgorithmIdSha256, |
997 modulus_length, public_exponent), | 991 modulus_length, |
998 true, blink::WebCryptoKeyUsageSign, &public_key, | 992 public_exponent), |
| 993 true, |
| 994 0, |
| 995 &public_key, |
999 &private_key)); | 996 &private_key)); |
1000 | 997 |
1001 // Export key pair as SPKI + PKCS8 | 998 // Export key pair as SPKI + PKCS8 |
1002 std::vector<uint8_t> public_key_spki; | 999 std::vector<uint8_t> public_key_spki; |
1003 ASSERT_EQ( | 1000 ASSERT_EQ( |
1004 Status::Success(), | 1001 Status::Success(), |
1005 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); | 1002 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); |
1006 | 1003 |
1007 std::vector<uint8_t> private_key_pkcs8; | 1004 std::vector<uint8_t> private_key_pkcs8; |
1008 ASSERT_EQ( | 1005 ASSERT_EQ( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1042 CryptoData(wrapped_public_key), | 1039 CryptoData(wrapped_public_key), |
1043 wrapping_key, | 1040 wrapping_key, |
1044 wrap_algorithm, | 1041 wrap_algorithm, |
1045 rsa_import_algorithm, | 1042 rsa_import_algorithm, |
1046 true, | 1043 true, |
1047 0, | 1044 0, |
1048 &unwrapped_public_key)); | 1045 &unwrapped_public_key)); |
1049 | 1046 |
1050 blink::WebCryptoKey unwrapped_private_key; | 1047 blink::WebCryptoKey unwrapped_private_key; |
1051 | 1048 |
1052 ASSERT_EQ( | 1049 ASSERT_EQ(Status::Success(), |
1053 Status::Success(), | 1050 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, |
1054 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, CryptoData(wrapped_private_key), | 1051 CryptoData(wrapped_private_key), |
1055 wrapping_key, wrap_algorithm, rsa_import_algorithm, true, | 1052 wrapping_key, |
1056 blink::WebCryptoKeyUsageSign, &unwrapped_private_key)); | 1053 wrap_algorithm, |
| 1054 rsa_import_algorithm, |
| 1055 true, |
| 1056 0, |
| 1057 &unwrapped_private_key)); |
1057 | 1058 |
1058 // Export unwrapped key pair as SPKI + PKCS8 | 1059 // Export unwrapped key pair as SPKI + PKCS8 |
1059 std::vector<uint8_t> unwrapped_public_key_spki; | 1060 std::vector<uint8_t> unwrapped_public_key_spki; |
1060 ASSERT_EQ(Status::Success(), | 1061 ASSERT_EQ(Status::Success(), |
1061 ExportKey(blink::WebCryptoKeyFormatSpki, | 1062 ExportKey(blink::WebCryptoKeyFormatSpki, |
1062 unwrapped_public_key, | 1063 unwrapped_public_key, |
1063 &unwrapped_public_key_spki)); | 1064 &unwrapped_public_key_spki)); |
1064 | 1065 |
1065 std::vector<uint8_t> unwrapped_private_key_pkcs8; | 1066 std::vector<uint8_t> unwrapped_private_key_pkcs8; |
1066 ASSERT_EQ(Status::Success(), | 1067 ASSERT_EQ(Status::Success(), |
1067 ExportKey(blink::WebCryptoKeyFormatPkcs8, | 1068 ExportKey(blink::WebCryptoKeyFormatPkcs8, |
1068 unwrapped_private_key, | 1069 unwrapped_private_key, |
1069 &unwrapped_private_key_pkcs8)); | 1070 &unwrapped_private_key_pkcs8)); |
1070 | 1071 |
1071 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); | 1072 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); |
1072 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); | 1073 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); |
1073 | 1074 |
1074 EXPECT_NE(public_key_spki, wrapped_public_key); | 1075 EXPECT_NE(public_key_spki, wrapped_public_key); |
1075 EXPECT_NE(private_key_pkcs8, wrapped_private_key); | 1076 EXPECT_NE(private_key_pkcs8, wrapped_private_key); |
1076 } | 1077 } |
1077 | 1078 |
1078 } // namespace | 1079 } // namespace |
1079 | 1080 |
1080 } // namespace webcrypto | 1081 } // namespace webcrypto |
1081 | 1082 |
1082 } // namespace content | 1083 } // namespace content |
OLD | NEW |