Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: crypto/ec_private_key_unittest.cc

Issue 8413024: Add ECPrivateKey for Elliptic Curve keypair generation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review changes Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "crypto/ec_private_key.h"
6
7 #include <vector>
8
9 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 #if defined(USE_OPENSSL)
13 // Once ECPrivateKey is implemented for OpenSSL, remove this #if block.
14 TEST(ECPrivateKeyUnitTest, OpenSSLStub) {
15 scoped_ptr<crypto::ECPrivateKey> keypair1(
16 crypto::ECPrivateKey::Create());
17 ASSERT_FALSE(keypair1.get());
18 }
Ryan Sleevi 2011/11/04 03:21:25 nit: If/when OpenSSL is implemented, I think you s
mattm 2011/11/08 02:12:27 Good idea.
19 #else
20 // Generate random private keys. Export, then re-import. We should get
21 // back the same exact public key, and the private key should have the same
22 // value and elliptic curve params.
Ryan Sleevi 2011/11/04 03:21:25 nit: Much like the existing tests for RSA, this is
wtc 2011/11/04 21:52:49 RSA key pair generation is much slower than EC key
mattm 2011/11/08 02:12:27 Yeah, from the linux_valgrind try run: ECPrivateKe
23 TEST(ECPrivateKeyUnitTest, InitRandomTest) {
24 const std::string password1 = "";
25 const std::string password2 = "test";
26
27 scoped_ptr<crypto::ECPrivateKey> keypair1(
28 crypto::ECPrivateKey::Create());
29 scoped_ptr<crypto::ECPrivateKey> keypair2(
30 crypto::ECPrivateKey::Create());
31 ASSERT_TRUE(keypair1.get());
32 ASSERT_TRUE(keypair2.get());
33
34 std::vector<uint8> key1value;
35 std::vector<uint8> key2value;
36 std::vector<uint8> key1params;
37 std::vector<uint8> key2params;
38 EXPECT_TRUE(keypair1->ExportValue(&key1value));
39 EXPECT_TRUE(keypair2->ExportValue(&key2value));
40 EXPECT_TRUE(keypair1->ExportECParams(&key1params));
41 EXPECT_TRUE(keypair2->ExportECParams(&key2params));
42
43 std::vector<uint8> privkey1;
44 std::vector<uint8> privkey2;
45 std::vector<uint8> pubkey1;
46 std::vector<uint8> pubkey2;
Ryan Sleevi 2011/11/04 03:21:25 nit: The naming here (key as suffix) seems to conf
mattm 2011/11/08 02:12:27 I thought about that, but the alternatives I thoug
47 std::vector<uint8> tmp_pubkey;
48 ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey(
49 password1, &privkey1, &pubkey1));
50 ASSERT_TRUE(keypair2->ExportEncryptedPrivateKey(
51 password2, &privkey2, &pubkey2));
52 EXPECT_TRUE(keypair1->ExportPublicKey(&tmp_pubkey));
53 EXPECT_EQ(pubkey1, tmp_pubkey);
54 EXPECT_TRUE(keypair2->ExportPublicKey(&tmp_pubkey));
55 EXPECT_EQ(pubkey2, tmp_pubkey);
56
57 scoped_ptr<crypto::ECPrivateKey> keypair3(
58 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
59 password1, privkey1, pubkey1));
60 scoped_ptr<crypto::ECPrivateKey> keypair4(
61 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
62 password2, privkey2, pubkey2));
63 ASSERT_TRUE(keypair3.get());
64 ASSERT_TRUE(keypair4.get());
65
66 std::vector<uint8> key3value;
67 std::vector<uint8> key4value;
68 std::vector<uint8> key3params;
69 std::vector<uint8> key4params;
70 EXPECT_TRUE(keypair3->ExportValue(&key3value));
71 EXPECT_TRUE(keypair4->ExportValue(&key4value));
72 EXPECT_TRUE(keypair3->ExportECParams(&key3params));
73 EXPECT_TRUE(keypair4->ExportECParams(&key4params));
74
75 EXPECT_EQ(key1value, key3value);
76 EXPECT_EQ(key2value, key4value);
77 EXPECT_EQ(key1params, key3params);
78 EXPECT_EQ(key2params, key4params);
79
80 std::vector<uint8> pubkey3;
81 std::vector<uint8> pubkey4;
82 EXPECT_TRUE(keypair3->ExportPublicKey(&pubkey3));
83 EXPECT_TRUE(keypair4->ExportPublicKey(&pubkey4));
84
85 EXPECT_EQ(pubkey1, pubkey3);
86 EXPECT_EQ(pubkey2, pubkey4);
87 }
88
89 TEST(ECPrivateKeyUnitTest, BadPasswordTest) {
Ryan Sleevi 2011/11/04 03:21:25 Explanation as to what the test covers? More impo
mattm 2011/11/08 02:12:27 No, just a functional test, wrong password should
90 const std::string password1 = "";
91 const std::string password2 = "test";
92
93 scoped_ptr<crypto::ECPrivateKey> keypair1(
94 crypto::ECPrivateKey::Create());
95 ASSERT_TRUE(keypair1.get());
96
97 std::vector<uint8> privkey1;
98 std::vector<uint8> pubkey1;
99 ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey(
100 password1, &privkey1, &pubkey1));
101
102 scoped_ptr<crypto::ECPrivateKey> keypair2(
103 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
104 password2, privkey1, pubkey1));
105 ASSERT_FALSE(keypair2.get());
106 }
107 #endif // !defined(USE_OPENSSL)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698