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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 blink::WebCryptoNamedCurve named_curve = blink::WebCryptoNamedCurveP256; | 57 blink::WebCryptoNamedCurve named_curve = blink::WebCryptoNamedCurveP256; |
58 | 58 |
59 std::vector<std::vector<uint8_t>> serialized_keys; | 59 std::vector<std::vector<uint8_t>> serialized_keys; |
60 | 60 |
61 // Generate a small sample of keys. | 61 // Generate a small sample of keys. |
62 for (int j = 0; j < 4; ++j) { | 62 for (int j = 0; j < 4; ++j) { |
63 blink::WebCryptoKey public_key; | 63 blink::WebCryptoKey public_key; |
64 blink::WebCryptoKey private_key; | 64 blink::WebCryptoKey private_key; |
65 | 65 |
66 ASSERT_EQ(Status::Success(), | 66 ASSERT_EQ(Status::Success(), |
67 GenerateKeyPair(CreateEcdsaKeyGenAlgorithm(named_curve), true, 0, | 67 GenerateKeyPair(CreateEcdsaKeyGenAlgorithm(named_curve), true, |
68 &public_key, &private_key)); | 68 blink::WebCryptoKeyUsageSign, &public_key, |
| 69 &private_key)); |
69 | 70 |
70 // Basic sanity checks on the generated key pair. | 71 // Basic sanity checks on the generated key pair. |
71 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); | 72 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
72 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); | 73 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
73 EXPECT_EQ(named_curve, public_key.algorithm().ecParams()->namedCurve()); | 74 EXPECT_EQ(named_curve, public_key.algorithm().ecParams()->namedCurve()); |
74 EXPECT_EQ(named_curve, private_key.algorithm().ecParams()->namedCurve()); | 75 EXPECT_EQ(named_curve, private_key.algorithm().ecParams()->namedCurve()); |
75 | 76 |
76 // Export the key pair to JWK. | 77 // Export the key pair to JWK. |
77 std::vector<uint8_t> key_bytes; | 78 std::vector<uint8_t> key_bytes; |
78 ASSERT_EQ(Status::Success(), | 79 ASSERT_EQ(Status::Success(), |
79 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &key_bytes)); | 80 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &key_bytes)); |
80 serialized_keys.push_back(key_bytes); | 81 serialized_keys.push_back(key_bytes); |
81 | 82 |
82 ASSERT_EQ(Status::Success(), | 83 ASSERT_EQ(Status::Success(), |
83 ExportKey(blink::WebCryptoKeyFormatJwk, private_key, &key_bytes)); | 84 ExportKey(blink::WebCryptoKeyFormatJwk, private_key, &key_bytes)); |
84 serialized_keys.push_back(key_bytes); | 85 serialized_keys.push_back(key_bytes); |
85 } | 86 } |
86 | 87 |
87 // Ensure all entries in the key sample set are unique. This is a simplistic | 88 // Ensure all entries in the key sample set are unique. This is a simplistic |
88 // estimate of whether the generated keys appear random. | 89 // estimate of whether the generated keys appear random. |
89 EXPECT_FALSE(CopiesExist(serialized_keys)); | 90 EXPECT_FALSE(CopiesExist(serialized_keys)); |
90 } | 91 } |
91 | 92 |
| 93 TEST(WebCryptoEcdsaTest, GenerateKeyEmptyUsage) { |
| 94 if (!SupportsEcdsa()) |
| 95 return; |
| 96 |
| 97 blink::WebCryptoNamedCurve named_curve = blink::WebCryptoNamedCurveP256; |
| 98 blink::WebCryptoKey public_key; |
| 99 blink::WebCryptoKey private_key; |
| 100 ASSERT_EQ(Status::ErrorCreateKeyEmptyUsages(), |
| 101 GenerateKeyPair(CreateEcdsaKeyGenAlgorithm(named_curve), true, 0, |
| 102 &public_key, &private_key)); |
| 103 } |
| 104 |
92 // Verify that ECDSA signatures are probabilistic. Signing the same message two | 105 // Verify that ECDSA signatures are probabilistic. Signing the same message two |
93 // times should yield different signatures. However both signatures should | 106 // times should yield different signatures. However both signatures should |
94 // verify correctly. | 107 // verify correctly. |
95 TEST(WebCryptoEcdsaTest, SignatureIsRandom) { | 108 TEST(WebCryptoEcdsaTest, SignatureIsRandom) { |
96 if (!SupportsEcdsa()) | 109 if (!SupportsEcdsa()) |
97 return; | 110 return; |
98 | 111 |
99 // Import a public and private keypair from "ec_private_keys.json". It doesn't | 112 // Import a public and private keypair from "ec_private_keys.json". It doesn't |
100 // really matter which one is used since they are all valid. In this case | 113 // really matter which one is used since they are all valid. In this case |
101 // using the first one. | 114 // using the first one. |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 ExportKey(blink::WebCryptoKeyFormatJwk, key, &exported_bytes)); | 307 ExportKey(blink::WebCryptoKeyFormatJwk, key, &exported_bytes)); |
295 EXPECT_EQ(CryptoData(jwk_bytes), CryptoData(exported_bytes)); | 308 EXPECT_EQ(CryptoData(jwk_bytes), CryptoData(exported_bytes)); |
296 } | 309 } |
297 } | 310 } |
298 | 311 |
299 } // namespace | 312 } // namespace |
300 | 313 |
301 } // namespace webcrypto | 314 } // namespace webcrypto |
302 | 315 |
303 } // namespace content | 316 } // namespace content |
OLD | NEW |