OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "crypto/ec_private_key.h" | 5 #include "crypto/ec_private_key.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 // Generate random private keys. Export, then re-import. We should get | 15 // Generate random private keys. Export, then re-import. We should get |
16 // back the same exact public key, and the private key should have the same | 16 // back the same exact public key, and the private key should have the same |
17 // value and elliptic curve params. | 17 // value and elliptic curve params. |
18 TEST(ECPrivateKeyUnitTest, InitRandomTest) { | 18 TEST(ECPrivateKeyUnitTest, InitRandomTest) { |
19 const std::string password1; | 19 const std::string password1; |
20 const std::string password2 = "test"; | 20 const std::string password2 = "test"; |
21 | 21 |
22 scoped_ptr<crypto::ECPrivateKey> keypair1(crypto::ECPrivateKey::Create()); | 22 scoped_ptr<crypto::ECPrivateKey> keypair1(crypto::ECPrivateKey::Create()); |
23 scoped_ptr<crypto::ECPrivateKey> keypair2(crypto::ECPrivateKey::Create()); | 23 scoped_ptr<crypto::ECPrivateKey> keypair2(crypto::ECPrivateKey::Create()); |
24 ASSERT_TRUE(keypair1.get()); | 24 ASSERT_TRUE(keypair1.get()); |
25 ASSERT_TRUE(keypair2.get()); | 25 ASSERT_TRUE(keypair2.get()); |
26 | 26 |
27 std::vector<uint8_t> key1value; | 27 std::vector<uint8_t> key1value; |
28 std::vector<uint8_t> key2value; | 28 std::vector<uint8_t> key2value; |
29 std::vector<uint8_t> key1params; | 29 EXPECT_TRUE(keypair1->ExportValueForTesting(&key1value)); |
30 std::vector<uint8_t> key2params; | 30 EXPECT_TRUE(keypair2->ExportValueForTesting(&key2value)); |
31 EXPECT_TRUE(keypair1->ExportValue(&key1value)); | |
32 EXPECT_TRUE(keypair2->ExportValue(&key2value)); | |
33 EXPECT_TRUE(keypair1->ExportECParams(&key1params)); | |
34 EXPECT_TRUE(keypair2->ExportECParams(&key2params)); | |
35 | 31 |
36 std::vector<uint8_t> privkey1; | 32 std::vector<uint8_t> privkey1; |
37 std::vector<uint8_t> privkey2; | 33 std::vector<uint8_t> privkey2; |
38 std::vector<uint8_t> pubkey1; | 34 std::vector<uint8_t> pubkey1; |
39 std::vector<uint8_t> pubkey2; | 35 std::vector<uint8_t> pubkey2; |
40 std::string raw_pubkey1; | 36 std::string raw_pubkey1; |
41 std::string raw_pubkey2; | 37 std::string raw_pubkey2; |
42 ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey(password1, 1, &privkey1)); | 38 ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey(password1, 1, &privkey1)); |
43 ASSERT_TRUE(keypair2->ExportEncryptedPrivateKey(password2, 1, &privkey2)); | 39 ASSERT_TRUE(keypair2->ExportEncryptedPrivateKey(password2, 1, &privkey2)); |
44 EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1)); | 40 EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1)); |
45 EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2)); | 41 EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2)); |
46 EXPECT_TRUE(keypair1->ExportRawPublicKey(&raw_pubkey1)); | 42 EXPECT_TRUE(keypair1->ExportRawPublicKey(&raw_pubkey1)); |
47 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2)); | 43 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2)); |
48 | 44 |
49 scoped_ptr<crypto::ECPrivateKey> keypair3( | 45 scoped_ptr<crypto::ECPrivateKey> keypair3( |
50 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 46 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
51 password1, privkey1, pubkey1)); | 47 password1, privkey1, pubkey1)); |
52 scoped_ptr<crypto::ECPrivateKey> keypair4( | 48 scoped_ptr<crypto::ECPrivateKey> keypair4( |
53 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 49 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
54 password2, privkey2, pubkey2)); | 50 password2, privkey2, pubkey2)); |
55 ASSERT_TRUE(keypair3.get()); | 51 ASSERT_TRUE(keypair3.get()); |
56 ASSERT_TRUE(keypair4.get()); | 52 ASSERT_TRUE(keypair4.get()); |
57 | 53 |
58 std::vector<uint8_t> key3value; | 54 std::vector<uint8_t> key3value; |
59 std::vector<uint8_t> key4value; | 55 std::vector<uint8_t> key4value; |
60 std::vector<uint8_t> key3params; | 56 EXPECT_TRUE(keypair3->ExportValueForTesting(&key3value)); |
61 std::vector<uint8_t> key4params; | 57 EXPECT_TRUE(keypair4->ExportValueForTesting(&key4value)); |
62 EXPECT_TRUE(keypair3->ExportValue(&key3value)); | |
63 EXPECT_TRUE(keypair4->ExportValue(&key4value)); | |
64 EXPECT_TRUE(keypair3->ExportECParams(&key3params)); | |
65 EXPECT_TRUE(keypair4->ExportECParams(&key4params)); | |
66 | 58 |
67 EXPECT_EQ(key1value, key3value); | 59 EXPECT_EQ(key1value, key3value); |
68 EXPECT_EQ(key2value, key4value); | 60 EXPECT_EQ(key2value, key4value); |
69 EXPECT_EQ(key1params, key3params); | |
70 EXPECT_EQ(key2params, key4params); | |
71 | 61 |
72 std::vector<uint8_t> pubkey3; | 62 std::vector<uint8_t> pubkey3; |
73 std::vector<uint8_t> pubkey4; | 63 std::vector<uint8_t> pubkey4; |
74 std::string raw_pubkey3; | 64 std::string raw_pubkey3; |
75 std::string raw_pubkey4; | 65 std::string raw_pubkey4; |
76 EXPECT_TRUE(keypair3->ExportPublicKey(&pubkey3)); | 66 EXPECT_TRUE(keypair3->ExportPublicKey(&pubkey3)); |
77 EXPECT_TRUE(keypair4->ExportPublicKey(&pubkey4)); | 67 EXPECT_TRUE(keypair4->ExportPublicKey(&pubkey4)); |
78 EXPECT_TRUE(keypair3->ExportRawPublicKey(&raw_pubkey3)); | 68 EXPECT_TRUE(keypair3->ExportRawPublicKey(&raw_pubkey3)); |
79 EXPECT_TRUE(keypair4->ExportRawPublicKey(&raw_pubkey4)); | 69 EXPECT_TRUE(keypair4->ExportRawPublicKey(&raw_pubkey4)); |
80 | 70 |
81 EXPECT_EQ(pubkey1, pubkey3); | 71 EXPECT_EQ(pubkey1, pubkey3); |
82 EXPECT_EQ(pubkey2, pubkey4); | 72 EXPECT_EQ(pubkey2, pubkey4); |
83 EXPECT_EQ(raw_pubkey1, raw_pubkey3); | 73 EXPECT_EQ(raw_pubkey1, raw_pubkey3); |
84 EXPECT_EQ(raw_pubkey2, raw_pubkey4); | 74 EXPECT_EQ(raw_pubkey2, raw_pubkey4); |
85 } | 75 } |
86 | 76 |
87 TEST(ECPrivateKeyUnitTest, Copy) { | 77 TEST(ECPrivateKeyUnitTest, Copy) { |
88 scoped_ptr<crypto::ECPrivateKey> keypair1(crypto::ECPrivateKey::Create()); | 78 scoped_ptr<crypto::ECPrivateKey> keypair1(crypto::ECPrivateKey::Create()); |
89 scoped_ptr<crypto::ECPrivateKey> keypair2(keypair1->Copy()); | 79 scoped_ptr<crypto::ECPrivateKey> keypair2(keypair1->Copy()); |
90 ASSERT_TRUE(keypair1.get()); | 80 ASSERT_TRUE(keypair1.get()); |
91 ASSERT_TRUE(keypair2.get()); | 81 ASSERT_TRUE(keypair2.get()); |
92 | 82 |
93 std::vector<uint8_t> key1value; | 83 std::vector<uint8_t> key1value; |
94 std::vector<uint8_t> key2value; | 84 std::vector<uint8_t> key2value; |
95 EXPECT_TRUE(keypair1->ExportValue(&key1value)); | 85 EXPECT_TRUE(keypair1->ExportValueForTesting(&key1value)); |
96 EXPECT_TRUE(keypair2->ExportValue(&key2value)); | 86 EXPECT_TRUE(keypair2->ExportValueForTesting(&key2value)); |
97 EXPECT_EQ(key1value, key2value); | 87 EXPECT_EQ(key1value, key2value); |
98 | 88 |
99 std::vector<uint8_t> key1params; | |
100 std::vector<uint8_t> key2params; | |
101 EXPECT_TRUE(keypair1->ExportECParams(&key1params)); | |
102 EXPECT_TRUE(keypair2->ExportECParams(&key2params)); | |
103 EXPECT_EQ(key1params, key2params); | |
104 | |
105 std::vector<uint8_t> pubkey1; | 89 std::vector<uint8_t> pubkey1; |
106 std::vector<uint8_t> pubkey2; | 90 std::vector<uint8_t> pubkey2; |
107 EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1)); | 91 EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1)); |
108 EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2)); | 92 EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2)); |
109 EXPECT_EQ(pubkey1, pubkey2); | 93 EXPECT_EQ(pubkey1, pubkey2); |
110 | 94 |
111 std::string raw_pubkey1; | 95 std::string raw_pubkey1; |
112 std::string raw_pubkey2; | 96 std::string raw_pubkey2; |
113 EXPECT_TRUE(keypair1->ExportRawPublicKey(&raw_pubkey1)); | 97 EXPECT_TRUE(keypair1->ExportRawPublicKey(&raw_pubkey1)); |
114 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2)); | 98 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2)); |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 scoped_ptr<crypto::ECPrivateKey> keypair_openssl( | 270 scoped_ptr<crypto::ECPrivateKey> keypair_openssl( |
287 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 271 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
288 "", std::vector<uint8_t>(openssl_key, | 272 "", std::vector<uint8_t>(openssl_key, |
289 openssl_key + arraysize(openssl_key)), | 273 openssl_key + arraysize(openssl_key)), |
290 std::vector<uint8_t>(openssl_pub_key, | 274 std::vector<uint8_t>(openssl_pub_key, |
291 openssl_pub_key + arraysize(openssl_pub_key)))); | 275 openssl_pub_key + arraysize(openssl_pub_key)))); |
292 | 276 |
293 EXPECT_TRUE(keypair_openssl.get()); | 277 EXPECT_TRUE(keypair_openssl.get()); |
294 } | 278 } |
295 #endif // defined(USE_OPENSSL) | 279 #endif // defined(USE_OPENSSL) |
OLD | NEW |