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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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]), | 266 CreateAesCbcKeyGenAlgorithm(kKeyLength[key_length_i]), |
267 true, | 267 true, |
268 0, | 268 blink::WebCryptoKeyUsageEncrypt, |
269 &key)); | 269 &key)); |
270 EXPECT_TRUE(key.handle()); | 270 EXPECT_TRUE(key.handle()); |
271 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 271 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
272 ASSERT_EQ(Status::Success(), | 272 ASSERT_EQ(Status::Success(), |
273 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); | 273 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); |
274 EXPECT_EQ(key_bytes.size() * 8, | 274 EXPECT_EQ(key_bytes.size() * 8, |
275 key.algorithm().aesParams()->lengthBits()); | 275 key.algorithm().aesParams()->lengthBits()); |
276 keys.push_back(key_bytes); | 276 keys.push_back(key_bytes); |
277 } | 277 } |
278 // 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 |
(...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
955 SCOPED_TRACE(i); | 955 SCOPED_TRACE(i); |
956 | 956 |
957 blink::WebCryptoKey key; | 957 blink::WebCryptoKey key; |
958 | 958 |
959 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), | 959 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), |
960 GenerateSecretKey( | 960 GenerateSecretKey( |
961 CreateAesCbcKeyGenAlgorithm(128), true, bad_usages[i], &key)); | 961 CreateAesCbcKeyGenAlgorithm(128), true, bad_usages[i], &key)); |
962 } | 962 } |
963 } | 963 } |
964 | 964 |
| 965 // Generate an AES-CBC key with no usages. |
| 966 TEST(WebCryptoAesCbcTest, GenerateKeyEmptyUsages) { |
| 967 blink::WebCryptoKey key; |
| 968 |
| 969 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), |
| 970 GenerateSecretKey( |
| 971 CreateAesCbcKeyGenAlgorithm(128), true, 0, &key)); |
| 972 } |
| 973 |
965 // Generate an AES-CBC key and an RSA key pair. Use the AES-CBC key to wrap the | 974 // Generate an AES-CBC key and an RSA key pair. Use the AES-CBC key to wrap the |
966 // key pair (using SPKI format for public key, PKCS8 format for private key). | 975 // key pair (using SPKI format for public key, PKCS8 format for private key). |
967 // Then unwrap the wrapped key pair and verify that the key data is the same. | 976 // Then unwrap the wrapped key pair and verify that the key data is the same. |
968 TEST(WebCryptoAesCbcTest, WrapUnwrapRoundtripSpkiPkcs8) { | 977 TEST(WebCryptoAesCbcTest, WrapUnwrapRoundtripSpkiPkcs8) { |
969 if (!SupportsRsaPrivateKeyImport()) | 978 if (!SupportsRsaPrivateKeyImport()) |
970 return; | 979 return; |
971 | 980 |
972 // Generate the wrapping key. | 981 // Generate the wrapping key. |
973 blink::WebCryptoKey wrapping_key; | 982 blink::WebCryptoKey wrapping_key; |
974 ASSERT_EQ(Status::Success(), | 983 ASSERT_EQ(Status::Success(), |
975 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), | 984 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), |
976 true, | 985 true, |
977 blink::WebCryptoKeyUsageWrapKey | | 986 blink::WebCryptoKeyUsageWrapKey | |
978 blink::WebCryptoKeyUsageUnwrapKey, | 987 blink::WebCryptoKeyUsageUnwrapKey, |
979 &wrapping_key)); | 988 &wrapping_key)); |
980 | 989 |
981 // Generate an RSA key pair to be wrapped. | 990 // Generate an RSA key pair to be wrapped. |
982 const unsigned int modulus_length = 256; | 991 const unsigned int modulus_length = 256; |
983 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); | 992 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); |
984 | 993 |
985 blink::WebCryptoKey public_key; | 994 blink::WebCryptoKey public_key; |
986 blink::WebCryptoKey private_key; | 995 blink::WebCryptoKey private_key; |
987 ASSERT_EQ(Status::Success(), | 996 ASSERT_EQ(Status::Success(), |
988 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( | 997 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( |
989 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 998 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
990 blink::WebCryptoAlgorithmIdSha256, | 999 blink::WebCryptoAlgorithmIdSha256, |
991 modulus_length, | 1000 modulus_length, |
992 public_exponent), | 1001 public_exponent), |
993 true, | 1002 true, |
994 0, | 1003 blink::WebCryptoKeyUsageSign, |
995 &public_key, | 1004 &public_key, |
996 &private_key)); | 1005 &private_key)); |
997 | 1006 |
998 // Export key pair as SPKI + PKCS8 | 1007 // Export key pair as SPKI + PKCS8 |
999 std::vector<uint8_t> public_key_spki; | 1008 std::vector<uint8_t> public_key_spki; |
1000 ASSERT_EQ( | 1009 ASSERT_EQ( |
1001 Status::Success(), | 1010 Status::Success(), |
1002 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); | 1011 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); |
1003 | 1012 |
1004 std::vector<uint8_t> private_key_pkcs8; | 1013 std::vector<uint8_t> private_key_pkcs8; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1046 | 1055 |
1047 blink::WebCryptoKey unwrapped_private_key; | 1056 blink::WebCryptoKey unwrapped_private_key; |
1048 | 1057 |
1049 ASSERT_EQ(Status::Success(), | 1058 ASSERT_EQ(Status::Success(), |
1050 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, | 1059 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, |
1051 CryptoData(wrapped_private_key), | 1060 CryptoData(wrapped_private_key), |
1052 wrapping_key, | 1061 wrapping_key, |
1053 wrap_algorithm, | 1062 wrap_algorithm, |
1054 rsa_import_algorithm, | 1063 rsa_import_algorithm, |
1055 true, | 1064 true, |
1056 0, | 1065 blink::WebCryptoKeyUsageSign, |
1057 &unwrapped_private_key)); | 1066 &unwrapped_private_key)); |
1058 | 1067 |
1059 // Export unwrapped key pair as SPKI + PKCS8 | 1068 // Export unwrapped key pair as SPKI + PKCS8 |
1060 std::vector<uint8_t> unwrapped_public_key_spki; | 1069 std::vector<uint8_t> unwrapped_public_key_spki; |
1061 ASSERT_EQ(Status::Success(), | 1070 ASSERT_EQ(Status::Success(), |
1062 ExportKey(blink::WebCryptoKeyFormatSpki, | 1071 ExportKey(blink::WebCryptoKeyFormatSpki, |
1063 unwrapped_public_key, | 1072 unwrapped_public_key, |
1064 &unwrapped_public_key_spki)); | 1073 &unwrapped_public_key_spki)); |
1065 | 1074 |
1066 std::vector<uint8_t> unwrapped_private_key_pkcs8; | 1075 std::vector<uint8_t> unwrapped_private_key_pkcs8; |
1067 ASSERT_EQ(Status::Success(), | 1076 ASSERT_EQ(Status::Success(), |
1068 ExportKey(blink::WebCryptoKeyFormatPkcs8, | 1077 ExportKey(blink::WebCryptoKeyFormatPkcs8, |
1069 unwrapped_private_key, | 1078 unwrapped_private_key, |
1070 &unwrapped_private_key_pkcs8)); | 1079 &unwrapped_private_key_pkcs8)); |
1071 | 1080 |
1072 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); | 1081 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); |
1073 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); | 1082 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); |
1074 | 1083 |
1075 EXPECT_NE(public_key_spki, wrapped_public_key); | 1084 EXPECT_NE(public_key_spki, wrapped_public_key); |
1076 EXPECT_NE(private_key_pkcs8, wrapped_private_key); | 1085 EXPECT_NE(private_key_pkcs8, wrapped_private_key); |
1077 } | 1086 } |
1078 | 1087 |
1079 } // namespace | 1088 } // namespace |
1080 | 1089 |
1081 } // namespace webcrypto | 1090 } // namespace webcrypto |
1082 | 1091 |
1083 } // namespace content | 1092 } // namespace content |
OLD | NEW |