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
3 // found in the LICENSE file.
5 #include "crypto/ec_private_key.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 // TODO(mattm): Add some exported keys from each to test
13 // interop between NSS and OpenSSL.
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
17 // value and elliptic curve params.
18 TEST(ECPrivateKeyUnitTest
, InitRandomTest
) {
19 const std::string password1
;
20 const std::string password2
= "test";
22 scoped_ptr
<crypto::ECPrivateKey
> keypair1(
23 crypto::ECPrivateKey::Create());
24 scoped_ptr
<crypto::ECPrivateKey
> keypair2(
25 crypto::ECPrivateKey::Create());
26 ASSERT_TRUE(keypair1
.get());
27 ASSERT_TRUE(keypair2
.get());
29 std::vector
<uint8
> key1value
;
30 std::vector
<uint8
> key2value
;
31 std::vector
<uint8
> key1params
;
32 std::vector
<uint8
> key2params
;
33 EXPECT_TRUE(keypair1
->ExportValue(&key1value
));
34 EXPECT_TRUE(keypair2
->ExportValue(&key2value
));
35 EXPECT_TRUE(keypair1
->ExportECParams(&key1params
));
36 EXPECT_TRUE(keypair2
->ExportECParams(&key2params
));
38 std::vector
<uint8
> privkey1
;
39 std::vector
<uint8
> privkey2
;
40 std::vector
<uint8
> pubkey1
;
41 std::vector
<uint8
> pubkey2
;
42 ASSERT_TRUE(keypair1
->ExportEncryptedPrivateKey(
43 password1
, 1, &privkey1
));
44 ASSERT_TRUE(keypair2
->ExportEncryptedPrivateKey(
45 password2
, 1, &privkey2
));
46 EXPECT_TRUE(keypair1
->ExportPublicKey(&pubkey1
));
47 EXPECT_TRUE(keypair2
->ExportPublicKey(&pubkey2
));
49 scoped_ptr
<crypto::ECPrivateKey
> keypair3(
50 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
51 password1
, privkey1
, pubkey1
));
52 scoped_ptr
<crypto::ECPrivateKey
> keypair4(
53 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
54 password2
, privkey2
, pubkey2
));
55 ASSERT_TRUE(keypair3
.get());
56 ASSERT_TRUE(keypair4
.get());
58 std::vector
<uint8
> key3value
;
59 std::vector
<uint8
> key4value
;
60 std::vector
<uint8
> key3params
;
61 std::vector
<uint8
> key4params
;
62 EXPECT_TRUE(keypair3
->ExportValue(&key3value
));
63 EXPECT_TRUE(keypair4
->ExportValue(&key4value
));
64 EXPECT_TRUE(keypair3
->ExportECParams(&key3params
));
65 EXPECT_TRUE(keypair4
->ExportECParams(&key4params
));
67 EXPECT_EQ(key1value
, key3value
);
68 EXPECT_EQ(key2value
, key4value
);
69 EXPECT_EQ(key1params
, key3params
);
70 EXPECT_EQ(key2params
, key4params
);
72 std::vector
<uint8
> pubkey3
;
73 std::vector
<uint8
> pubkey4
;
74 EXPECT_TRUE(keypair3
->ExportPublicKey(&pubkey3
));
75 EXPECT_TRUE(keypair4
->ExportPublicKey(&pubkey4
));
77 EXPECT_EQ(pubkey1
, pubkey3
);
78 EXPECT_EQ(pubkey2
, pubkey4
);
81 TEST(ECPrivateKeyUnitTest
, BadPasswordTest
) {
82 const std::string password1
;
83 const std::string password2
= "test";
85 scoped_ptr
<crypto::ECPrivateKey
> keypair1(
86 crypto::ECPrivateKey::Create());
87 ASSERT_TRUE(keypair1
.get());
89 std::vector
<uint8
> privkey1
;
90 std::vector
<uint8
> pubkey1
;
91 ASSERT_TRUE(keypair1
->ExportEncryptedPrivateKey(
92 password1
, 1, &privkey1
));
93 ASSERT_TRUE(keypair1
->ExportPublicKey(&pubkey1
));
95 scoped_ptr
<crypto::ECPrivateKey
> keypair2(
96 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
97 password2
, privkey1
, pubkey1
));
98 ASSERT_FALSE(keypair2
.get());