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 #if defined(USE_OPENSSL)
13 // Once ECPrivateKey is implemented for OpenSSL, remove this #if block.
14 // TODO(mattm): When that happens, also add some exported keys from each to test
15 // interop between NSS and OpenSSL.
16 TEST(ECPrivateKeyUnitTest
, OpenSSLStub
) {
17 scoped_ptr
<crypto::ECPrivateKey
> keypair1(
18 crypto::ECPrivateKey::Create());
19 ASSERT_FALSE(keypair1
.get());
22 // Generate random private keys. Export, then re-import. We should get
23 // back the same exact public key, and the private key should have the same
24 // value and elliptic curve params.
25 TEST(ECPrivateKeyUnitTest
, InitRandomTest
) {
26 const std::string password1
;
27 const std::string password2
= "test";
29 scoped_ptr
<crypto::ECPrivateKey
> keypair1(
30 crypto::ECPrivateKey::Create());
31 scoped_ptr
<crypto::ECPrivateKey
> keypair2(
32 crypto::ECPrivateKey::Create());
33 ASSERT_TRUE(keypair1
.get());
34 ASSERT_TRUE(keypair2
.get());
36 std::vector
<uint8
> key1value
;
37 std::vector
<uint8
> key2value
;
38 std::vector
<uint8
> key1params
;
39 std::vector
<uint8
> key2params
;
40 EXPECT_TRUE(keypair1
->ExportValue(&key1value
));
41 EXPECT_TRUE(keypair2
->ExportValue(&key2value
));
42 EXPECT_TRUE(keypair1
->ExportECParams(&key1params
));
43 EXPECT_TRUE(keypair2
->ExportECParams(&key2params
));
45 std::vector
<uint8
> privkey1
;
46 std::vector
<uint8
> privkey2
;
47 std::vector
<uint8
> pubkey1
;
48 std::vector
<uint8
> pubkey2
;
49 ASSERT_TRUE(keypair1
->ExportEncryptedPrivateKey(
50 password1
, 1, &privkey1
));
51 ASSERT_TRUE(keypair2
->ExportEncryptedPrivateKey(
52 password2
, 1, &privkey2
));
53 EXPECT_TRUE(keypair1
->ExportPublicKey(&pubkey1
));
54 EXPECT_TRUE(keypair2
->ExportPublicKey(&pubkey2
));
56 scoped_ptr
<crypto::ECPrivateKey
> keypair3(
57 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
58 password1
, privkey1
, pubkey1
));
59 scoped_ptr
<crypto::ECPrivateKey
> keypair4(
60 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
61 password2
, privkey2
, pubkey2
));
62 ASSERT_TRUE(keypair3
.get());
63 ASSERT_TRUE(keypair4
.get());
65 std::vector
<uint8
> key3value
;
66 std::vector
<uint8
> key4value
;
67 std::vector
<uint8
> key3params
;
68 std::vector
<uint8
> key4params
;
69 EXPECT_TRUE(keypair3
->ExportValue(&key3value
));
70 EXPECT_TRUE(keypair4
->ExportValue(&key4value
));
71 EXPECT_TRUE(keypair3
->ExportECParams(&key3params
));
72 EXPECT_TRUE(keypair4
->ExportECParams(&key4params
));
74 EXPECT_EQ(key1value
, key3value
);
75 EXPECT_EQ(key2value
, key4value
);
76 EXPECT_EQ(key1params
, key3params
);
77 EXPECT_EQ(key2params
, key4params
);
79 std::vector
<uint8
> pubkey3
;
80 std::vector
<uint8
> pubkey4
;
81 EXPECT_TRUE(keypair3
->ExportPublicKey(&pubkey3
));
82 EXPECT_TRUE(keypair4
->ExportPublicKey(&pubkey4
));
84 EXPECT_EQ(pubkey1
, pubkey3
);
85 EXPECT_EQ(pubkey2
, pubkey4
);
88 TEST(ECPrivateKeyUnitTest
, BadPasswordTest
) {
89 const std::string password1
;
90 const std::string password2
= "test";
92 scoped_ptr
<crypto::ECPrivateKey
> keypair1(
93 crypto::ECPrivateKey::Create());
94 ASSERT_TRUE(keypair1
.get());
96 std::vector
<uint8
> privkey1
;
97 std::vector
<uint8
> pubkey1
;
98 ASSERT_TRUE(keypair1
->ExportEncryptedPrivateKey(
99 password1
, 1, &privkey1
));
100 ASSERT_TRUE(keypair1
->ExportPublicKey(&pubkey1
));
102 scoped_ptr
<crypto::ECPrivateKey
> keypair2(
103 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
104 password2
, privkey1
, pubkey1
));
105 ASSERT_FALSE(keypair2
.get());
107 #endif // !defined(USE_OPENSSL)