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/jwk.h" | 8 #include "content/child/webcrypto/jwk.h" |
9 #include "content/child/webcrypto/status.h" | 9 #include "content/child/webcrypto/status.h" |
10 #include "content/child/webcrypto/test/test_helpers.h" | 10 #include "content/child/webcrypto/test/test_helpers.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 blink::WebCryptoNamedCurve named_curve = blink::WebCryptoNamedCurveP256; | 78 blink::WebCryptoNamedCurve named_curve = blink::WebCryptoNamedCurveP256; |
79 | 79 |
80 std::vector<std::vector<uint8_t>> serialized_keys; | 80 std::vector<std::vector<uint8_t>> serialized_keys; |
81 | 81 |
82 // Generate a small sample of keys. | 82 // Generate a small sample of keys. |
83 for (int j = 0; j < 4; ++j) { | 83 for (int j = 0; j < 4; ++j) { |
84 blink::WebCryptoKey public_key; | 84 blink::WebCryptoKey public_key; |
85 blink::WebCryptoKey private_key; | 85 blink::WebCryptoKey private_key; |
86 | 86 |
87 ASSERT_EQ(Status::Success(), | 87 ASSERT_EQ(Status::Success(), |
88 GenerateKeyPair(CreateEcdsaKeyGenAlgorithm(named_curve), true, 0, | 88 GenerateKeyPair(CreateEcdsaKeyGenAlgorithm(named_curve), true, |
89 &public_key, &private_key)); | 89 blink::WebCryptoKeyUsageSign, &public_key, |
| 90 &private_key)); |
90 | 91 |
91 // Basic sanity checks on the generated key pair. | 92 // Basic sanity checks on the generated key pair. |
92 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); | 93 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
93 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); | 94 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
94 EXPECT_EQ(named_curve, public_key.algorithm().ecParams()->namedCurve()); | 95 EXPECT_EQ(named_curve, public_key.algorithm().ecParams()->namedCurve()); |
95 EXPECT_EQ(named_curve, private_key.algorithm().ecParams()->namedCurve()); | 96 EXPECT_EQ(named_curve, private_key.algorithm().ecParams()->namedCurve()); |
96 | 97 |
97 // Export the key pair to JWK. | 98 // Export the key pair to JWK. |
98 std::vector<uint8_t> key_bytes; | 99 std::vector<uint8_t> key_bytes; |
99 ASSERT_EQ(Status::Success(), | 100 ASSERT_EQ(Status::Success(), |
100 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &key_bytes)); | 101 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &key_bytes)); |
101 serialized_keys.push_back(key_bytes); | 102 serialized_keys.push_back(key_bytes); |
102 | 103 |
103 ASSERT_EQ(Status::Success(), | 104 ASSERT_EQ(Status::Success(), |
104 ExportKey(blink::WebCryptoKeyFormatJwk, private_key, &key_bytes)); | 105 ExportKey(blink::WebCryptoKeyFormatJwk, private_key, &key_bytes)); |
105 serialized_keys.push_back(key_bytes); | 106 serialized_keys.push_back(key_bytes); |
106 } | 107 } |
107 | 108 |
108 // Ensure all entries in the key sample set are unique. This is a simplistic | 109 // Ensure all entries in the key sample set are unique. This is a simplistic |
109 // estimate of whether the generated keys appear random. | 110 // estimate of whether the generated keys appear random. |
110 EXPECT_FALSE(CopiesExist(serialized_keys)); | 111 EXPECT_FALSE(CopiesExist(serialized_keys)); |
111 } | 112 } |
112 | 113 |
| 114 TEST(WebCryptoEcdsaTest, GenerateKeyEmptyUsage) { |
| 115 if (!SupportsEcdsa()) |
| 116 return; |
| 117 |
| 118 blink::WebCryptoNamedCurve named_curve = blink::WebCryptoNamedCurveP256; |
| 119 blink::WebCryptoKey public_key; |
| 120 blink::WebCryptoKey private_key; |
| 121 ASSERT_EQ(Status::ErrorCreateKeyEmptyUsages(), |
| 122 GenerateKeyPair(CreateEcdsaKeyGenAlgorithm(named_curve), true, 0, |
| 123 &public_key, &private_key)); |
| 124 } |
| 125 |
113 // Verify that ECDSA signatures are probabilistic. Signing the same message two | 126 // Verify that ECDSA signatures are probabilistic. Signing the same message two |
114 // times should yield different signatures. However both signatures should | 127 // times should yield different signatures. However both signatures should |
115 // verify correctly. | 128 // verify correctly. |
116 TEST(WebCryptoEcdsaTest, SignatureIsRandom) { | 129 TEST(WebCryptoEcdsaTest, SignatureIsRandom) { |
117 if (!SupportsEcdsa()) | 130 if (!SupportsEcdsa()) |
118 return; | 131 return; |
119 | 132 |
120 // Import a public and private keypair from "ec_private_keys.json". It doesn't | 133 // Import a public and private keypair from "ec_private_keys.json". It doesn't |
121 // really matter which one is used since they are all valid. In this case | 134 // really matter which one is used since they are all valid. In this case |
122 // using the first one. | 135 // using the first one. |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 ExportKey(blink::WebCryptoKeyFormatJwk, key, &exported_bytes)); | 326 ExportKey(blink::WebCryptoKeyFormatJwk, key, &exported_bytes)); |
314 EXPECT_EQ(CryptoData(jwk_bytes), CryptoData(exported_bytes)); | 327 EXPECT_EQ(CryptoData(jwk_bytes), CryptoData(exported_bytes)); |
315 } | 328 } |
316 } | 329 } |
317 | 330 |
318 } // namespace | 331 } // namespace |
319 | 332 |
320 } // namespace webcrypto | 333 } // namespace webcrypto |
321 | 334 |
322 } // namespace content | 335 } // namespace content |
OLD | NEW |