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 <memory> | 9 #include <memory> |
10 #include <vector> | 10 #include <vector> |
(...skipping 23 matching lines...) Expand all Loading... |
34 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2)); | 34 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2)); |
35 EXPECT_EQ(raw_pubkey1, raw_pubkey2); | 35 EXPECT_EQ(raw_pubkey1, raw_pubkey2); |
36 } | 36 } |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 // Generate random private keys. Export, then re-import in several ways. We | 40 // Generate random private keys. Export, then re-import in several ways. We |
41 // should get back the same exact public key, and the private key should have | 41 // should get back the same exact public key, and the private key should have |
42 // the same value and elliptic curve params. | 42 // the same value and elliptic curve params. |
43 TEST(ECPrivateKeyUnitTest, InitRandomTest) { | 43 TEST(ECPrivateKeyUnitTest, InitRandomTest) { |
44 static const char kPassword1[] = ""; | |
45 static const char kPassword2[] = "test"; | |
46 | |
47 std::unique_ptr<crypto::ECPrivateKey> keypair(crypto::ECPrivateKey::Create()); | 44 std::unique_ptr<crypto::ECPrivateKey> keypair(crypto::ECPrivateKey::Create()); |
48 ASSERT_TRUE(keypair); | 45 ASSERT_TRUE(keypair); |
49 | 46 |
50 // Re-import as a PrivateKeyInfo. | 47 // Re-import as a PrivateKeyInfo. |
51 std::vector<uint8_t> privkey; | 48 std::vector<uint8_t> privkey; |
52 EXPECT_TRUE(keypair->ExportPrivateKey(&privkey)); | 49 EXPECT_TRUE(keypair->ExportPrivateKey(&privkey)); |
53 std::unique_ptr<crypto::ECPrivateKey> keypair_copy = | 50 std::unique_ptr<crypto::ECPrivateKey> keypair_copy = |
54 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(privkey); | 51 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(privkey); |
55 ASSERT_TRUE(keypair_copy); | 52 ASSERT_TRUE(keypair_copy); |
56 ExpectKeysEqual(keypair.get(), keypair_copy.get()); | 53 ExpectKeysEqual(keypair.get(), keypair_copy.get()); |
57 | 54 |
58 // Re-import as an EncryptedPrivateKeyInfo with kPassword1. | 55 // Re-import as an EncryptedPrivateKeyInfo with kPassword1. |
59 std::vector<uint8_t> encrypted_privkey; | 56 std::vector<uint8_t> encrypted_privkey; |
60 std::vector<uint8_t> pubkey; | 57 std::vector<uint8_t> pubkey; |
61 EXPECT_TRUE( | 58 EXPECT_TRUE(keypair->ExportEncryptedPrivateKey(&encrypted_privkey)); |
62 keypair->ExportEncryptedPrivateKey(kPassword1, 1, &encrypted_privkey)); | |
63 EXPECT_TRUE(keypair->ExportPublicKey(&pubkey)); | 59 EXPECT_TRUE(keypair->ExportPublicKey(&pubkey)); |
64 keypair_copy = crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 60 keypair_copy = crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
65 kPassword1, encrypted_privkey, pubkey); | 61 encrypted_privkey, pubkey); |
66 ASSERT_TRUE(keypair_copy); | |
67 ExpectKeysEqual(keypair.get(), keypair_copy.get()); | |
68 | |
69 // Re-import as an EncryptedPrivateKeyInfo with kPassword2. | |
70 EXPECT_TRUE( | |
71 keypair->ExportEncryptedPrivateKey(kPassword2, 1, &encrypted_privkey)); | |
72 keypair_copy = crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | |
73 kPassword2, encrypted_privkey, pubkey); | |
74 ASSERT_TRUE(keypair_copy); | 62 ASSERT_TRUE(keypair_copy); |
75 ExpectKeysEqual(keypair.get(), keypair_copy.get()); | 63 ExpectKeysEqual(keypair.get(), keypair_copy.get()); |
76 } | 64 } |
77 | 65 |
78 TEST(ECPrivateKeyUnitTest, Copy) { | 66 TEST(ECPrivateKeyUnitTest, Copy) { |
79 std::unique_ptr<crypto::ECPrivateKey> keypair1( | 67 std::unique_ptr<crypto::ECPrivateKey> keypair1( |
80 crypto::ECPrivateKey::Create()); | 68 crypto::ECPrivateKey::Create()); |
81 std::unique_ptr<crypto::ECPrivateKey> keypair2(keypair1->Copy()); | 69 std::unique_ptr<crypto::ECPrivateKey> keypair2(keypair1->Copy()); |
82 ASSERT_TRUE(keypair1); | 70 ASSERT_TRUE(keypair1); |
83 ASSERT_TRUE(keypair2); | 71 ASSERT_TRUE(keypair2); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 0xce, 0x6c, 0x72, 0xfb, 0x21, 0xb3, 0x02, 0x87, 0xe4, 0xfd, 0x61, 0xca, | 181 0xce, 0x6c, 0x72, 0xfb, 0x21, 0xb3, 0x02, 0x87, 0xe4, 0xfd, 0x61, 0xca, |
194 0x00, 0x42, 0x19, 0xf0, 0xda, 0x5a, 0x53, 0xe3, 0xb1, 0xc5, 0x15, 0xf3, | 182 0x00, 0x42, 0x19, 0xf0, 0xda, 0x5a, 0x53, 0xe3, 0xb1, 0xc5, 0x15, 0xf3, |
195 }; | 183 }; |
196 | 184 |
197 std::unique_ptr<crypto::ECPrivateKey> key = | 185 std::unique_ptr<crypto::ECPrivateKey> key = |
198 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(std::vector<uint8_t>( | 186 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(std::vector<uint8_t>( |
199 std::begin(kPrivateKeyInfo), std::end(kPrivateKeyInfo))); | 187 std::begin(kPrivateKeyInfo), std::end(kPrivateKeyInfo))); |
200 EXPECT_FALSE(key); | 188 EXPECT_FALSE(key); |
201 } | 189 } |
202 | 190 |
203 TEST(ECPrivateKeyUnitTest, BadPasswordTest) { | |
204 const std::string password1; | |
205 const std::string password2 = "test"; | |
206 | |
207 std::unique_ptr<crypto::ECPrivateKey> keypair1( | |
208 crypto::ECPrivateKey::Create()); | |
209 ASSERT_TRUE(keypair1); | |
210 | |
211 std::vector<uint8_t> privkey1; | |
212 std::vector<uint8_t> pubkey1; | |
213 ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey( | |
214 password1, 1, &privkey1)); | |
215 ASSERT_TRUE(keypair1->ExportPublicKey(&pubkey1)); | |
216 | |
217 std::unique_ptr<crypto::ECPrivateKey> keypair2( | |
218 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | |
219 password2, privkey1, pubkey1)); | |
220 ASSERT_FALSE(keypair2); | |
221 } | |
222 | |
223 TEST(ECPrivateKeyUnitTest, LoadNSSKeyTest) { | 191 TEST(ECPrivateKeyUnitTest, LoadNSSKeyTest) { |
224 static const uint8_t kNSSKey[] = { | 192 static const uint8_t kNSSKey[] = { |
225 0x30, 0x81, 0xb8, 0x30, 0x23, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, | 193 0x30, 0x81, 0xb8, 0x30, 0x23, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, |
226 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x15, 0x04, 0x10, 0x3f, 0xac, 0xe9, | 194 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x15, 0x04, 0x10, 0x3f, 0xac, 0xe9, |
227 0x38, 0xdb, 0x40, 0x6b, 0x26, 0x89, 0x09, 0x73, 0x18, 0x8d, 0x7f, 0x1c, | 195 0x38, 0xdb, 0x40, 0x6b, 0x26, 0x89, 0x09, 0x73, 0x18, 0x8d, 0x7f, 0x1c, |
228 0x82, 0x02, 0x01, 0x01, 0x04, 0x81, 0x90, 0x5e, 0x5e, 0x11, 0xef, 0xbb, | 196 0x82, 0x02, 0x01, 0x01, 0x04, 0x81, 0x90, 0x5e, 0x5e, 0x11, 0xef, 0xbb, |
229 0x7c, 0x4d, 0xec, 0xc0, 0xdc, 0xc7, 0x23, 0xd2, 0xc4, 0x77, 0xbc, 0xf4, | 197 0x7c, 0x4d, 0xec, 0xc0, 0xdc, 0xc7, 0x23, 0xd2, 0xc4, 0x77, 0xbc, 0xf4, |
230 0x5d, 0x59, 0x4c, 0x07, 0xc2, 0x8a, 0x26, 0xfa, 0x25, 0x1c, 0xaa, 0x42, | 198 0x5d, 0x59, 0x4c, 0x07, 0xc2, 0x8a, 0x26, 0xfa, 0x25, 0x1c, 0xaa, 0x42, |
231 0xed, 0xd0, 0xed, 0xbb, 0x5c, 0xe9, 0x13, 0x07, 0xaa, 0xdd, 0x52, 0x3c, | 199 0xed, 0xd0, 0xed, 0xbb, 0x5c, 0xe9, 0x13, 0x07, 0xaa, 0xdd, 0x52, 0x3c, |
232 0x65, 0x25, 0xbf, 0x94, 0x02, 0xaf, 0xd6, 0x97, 0xe9, 0x33, 0x00, 0x76, | 200 0x65, 0x25, 0xbf, 0x94, 0x02, 0xaf, 0xd6, 0x97, 0xe9, 0x33, 0x00, 0x76, |
(...skipping 12 matching lines...) Expand all Loading... |
245 0x42, 0x00, 0x04, 0x85, 0x92, 0x9e, 0x95, 0x5c, 0x6b, 0x9e, 0xd6, 0x1e, | 213 0x42, 0x00, 0x04, 0x85, 0x92, 0x9e, 0x95, 0x5c, 0x6b, 0x9e, 0xd6, 0x1e, |
246 0xb8, 0x64, 0xea, 0xc2, 0xb3, 0xef, 0x18, 0xed, 0x3a, 0x5e, 0xc4, 0x5c, | 214 0xb8, 0x64, 0xea, 0xc2, 0xb3, 0xef, 0x18, 0xed, 0x3a, 0x5e, 0xc4, 0x5c, |
247 0x15, 0x37, 0x6a, 0xe9, 0xaa, 0x0b, 0x34, 0x03, 0xfd, 0xca, 0x83, 0x0f, | 215 0x15, 0x37, 0x6a, 0xe9, 0xaa, 0x0b, 0x34, 0x03, 0xfd, 0xca, 0x83, 0x0f, |
248 0xd7, 0x5c, 0x5d, 0xc5, 0x53, 0x6e, 0xe5, 0xa9, 0x33, 0xd5, 0xcc, 0xab, | 216 0xd7, 0x5c, 0x5d, 0xc5, 0x53, 0x6e, 0xe5, 0xa9, 0x33, 0xd5, 0xcc, 0xab, |
249 0x53, 0x78, 0xdd, 0xd6, 0x12, 0x3a, 0x5e, 0xeb, 0xbf, 0xdf, 0x16, 0xd3, | 217 0x53, 0x78, 0xdd, 0xd6, 0x12, 0x3a, 0x5e, 0xeb, 0xbf, 0xdf, 0x16, 0xd3, |
250 0x2c, 0x3b, 0xe8, 0xdb, 0x19, 0xfc, 0x5e, | 218 0x2c, 0x3b, 0xe8, 0xdb, 0x19, 0xfc, 0x5e, |
251 }; | 219 }; |
252 | 220 |
253 std::unique_ptr<crypto::ECPrivateKey> keypair_nss( | 221 std::unique_ptr<crypto::ECPrivateKey> keypair_nss( |
254 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 222 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
255 "", std::vector<uint8_t>(std::begin(kNSSKey), std::end(kNSSKey)), | 223 std::vector<uint8_t>(std::begin(kNSSKey), std::end(kNSSKey)), |
256 std::vector<uint8_t>(std::begin(kNSSPublicKey), | 224 std::vector<uint8_t>(std::begin(kNSSPublicKey), |
257 std::end(kNSSPublicKey)))); | 225 std::end(kNSSPublicKey)))); |
258 | 226 |
259 EXPECT_TRUE(keypair_nss); | 227 EXPECT_TRUE(keypair_nss); |
260 } | 228 } |
261 | 229 |
262 TEST(ECPrivateKeyUnitTest, LoadOpenSSLKeyTest) { | 230 TEST(ECPrivateKeyUnitTest, LoadOpenSSLKeyTest) { |
263 static const uint8_t kOpenSSLKey[] = { | 231 static const uint8_t kOpenSSLKey[] = { |
264 0x30, 0x81, 0xb0, 0x30, 0x1b, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, | 232 0x30, 0x81, 0xb0, 0x30, 0x1b, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, |
265 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0d, 0x04, 0x08, 0xb2, 0xfe, 0x68, | 233 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0d, 0x04, 0x08, 0xb2, 0xfe, 0x68, |
(...skipping 25 matching lines...) Expand all Loading... |
291 0xb9, 0xda, 0x0d, 0x71, 0x60, 0xb3, 0x63, 0x28, 0x22, 0x67, 0xe7, | 259 0xb9, 0xda, 0x0d, 0x71, 0x60, 0xb3, 0x63, 0x28, 0x22, 0x67, 0xe7, |
292 0xe0, 0xa3, 0xf8, 0x00, 0x8e, 0x4c, 0x89, 0xed, 0x31, 0x34, 0xf6, | 260 0xe0, 0xa3, 0xf8, 0x00, 0x8e, 0x4c, 0x89, 0xed, 0x31, 0x34, 0xf6, |
293 0xdb, 0xc4, 0xfe, 0x0b, 0x5d, 0xe1, 0x11, 0x39, 0x49, 0xa6, 0x50, | 261 0xdb, 0xc4, 0xfe, 0x0b, 0x5d, 0xe1, 0x11, 0x39, 0x49, 0xa6, 0x50, |
294 0xa8, 0xe3, 0x4a, 0xc0, 0x40, 0x88, 0xb8, 0x38, 0x3f, 0x56, 0xfb, | 262 0xa8, 0xe3, 0x4a, 0xc0, 0x40, 0x88, 0xb8, 0x38, 0x3f, 0x56, 0xfb, |
295 0x33, 0x8d, 0xd4, 0x64, 0x91, 0xd6, 0x15, 0x77, 0x42, 0x27, 0xc5, | 263 0x33, 0x8d, 0xd4, 0x64, 0x91, 0xd6, 0x15, 0x77, 0x42, 0x27, 0xc5, |
296 0xaa, 0x44, 0xff, 0xab, 0x4d, 0xb5, 0x7e, 0x25, 0x3d, | 264 0xaa, 0x44, 0xff, 0xab, 0x4d, 0xb5, 0x7e, 0x25, 0x3d, |
297 }; | 265 }; |
298 | 266 |
299 std::unique_ptr<crypto::ECPrivateKey> keypair_openssl( | 267 std::unique_ptr<crypto::ECPrivateKey> keypair_openssl( |
300 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 268 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
301 "", | |
302 std::vector<uint8_t>(std::begin(kOpenSSLKey), std::end(kOpenSSLKey)), | 269 std::vector<uint8_t>(std::begin(kOpenSSLKey), std::end(kOpenSSLKey)), |
303 std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), | 270 std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), |
304 std::end(kOpenSSLPublicKey)))); | 271 std::end(kOpenSSLPublicKey)))); |
305 | 272 |
306 EXPECT_TRUE(keypair_openssl); | 273 EXPECT_TRUE(keypair_openssl); |
307 | 274 |
308 std::vector<uint8_t> public_key; | 275 std::vector<uint8_t> public_key; |
309 EXPECT_TRUE(keypair_openssl->ExportPublicKey(&public_key)); | 276 EXPECT_TRUE(keypair_openssl->ExportPublicKey(&public_key)); |
310 EXPECT_EQ(std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), | 277 EXPECT_EQ(std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), |
311 std::end(kOpenSSLPublicKey)), | 278 std::end(kOpenSSLPublicKey)), |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04, 0xde, 0x09, 0x08, 0x07, 0x03, | 353 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04, 0xde, 0x09, 0x08, 0x07, 0x03, |
387 0x2e, 0x8f, 0x37, 0x9a, 0xd5, 0xad, 0xe5, 0xc6, 0x9d, 0xd4, 0x63, 0xc7, | 354 0x2e, 0x8f, 0x37, 0x9a, 0xd5, 0xad, 0xe5, 0xc6, 0x9d, 0xd4, 0x63, 0xc7, |
388 0x4a, 0xe7, 0x20, 0xcb, 0x90, 0xa0, 0x1f, 0x18, 0x18, 0x72, 0xb5, 0x21, | 355 0x4a, 0xe7, 0x20, 0xcb, 0x90, 0xa0, 0x1f, 0x18, 0x18, 0x72, 0xb5, 0x21, |
389 0x88, 0x38, 0xc0, 0xdb, 0xba, 0xf6, 0x99, 0xd8, 0xa5, 0x3b, 0x83, 0xe9, | 356 0x88, 0x38, 0xc0, 0xdb, 0xba, 0xf6, 0x99, 0xd8, 0xa5, 0x3b, 0x83, 0xe9, |
390 0xe3, 0xd5, 0x61, 0x99, 0x73, 0x42, 0xc6, 0x6c, 0xe8, 0x0a, 0x95, 0x40, | 357 0xe3, 0xd5, 0x61, 0x99, 0x73, 0x42, 0xc6, 0x6c, 0xe8, 0x0a, 0x95, 0x40, |
391 0x41, 0x3b, 0x0d, 0x10, 0xa7, 0x4a, 0x93, 0xdb, 0x5a, 0xe7, 0xec, | 358 0x41, 0x3b, 0x0d, 0x10, 0xa7, 0x4a, 0x93, 0xdb, 0x5a, 0xe7, 0xec, |
392 }; | 359 }; |
393 | 360 |
394 std::unique_ptr<crypto::ECPrivateKey> keypair_openssl( | 361 std::unique_ptr<crypto::ECPrivateKey> keypair_openssl( |
395 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 362 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
396 "", | |
397 std::vector<uint8_t>(std::begin(kOpenSSLKey), std::end(kOpenSSLKey)), | 363 std::vector<uint8_t>(std::begin(kOpenSSLKey), std::end(kOpenSSLKey)), |
398 std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), | 364 std::vector<uint8_t>(std::begin(kOpenSSLPublicKey), |
399 std::end(kOpenSSLPublicKey)))); | 365 std::end(kOpenSSLPublicKey)))); |
400 | 366 |
401 EXPECT_TRUE(keypair_openssl); | 367 EXPECT_TRUE(keypair_openssl); |
402 } | 368 } |
OLD | NEW |