summaryrefslogtreecommitdiffstats
path: root/crypto/ec_private_key_unittest.cc
diff options
context:
space:
mode:
authormattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-09 05:08:51 +0000
committermattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-09 05:08:51 +0000
commiteaa60485f027b00047a2e142d9616ede4333a46b (patch)
treeeb20edb20255f8f76d76f3ff4f1399a04a6d9398 /crypto/ec_private_key_unittest.cc
parente80f64a0f8ed441443bec21eeb5c732398d11377 (diff)
downloadchromium_src-eaa60485f027b00047a2e142d9616ede4333a46b.zip
chromium_src-eaa60485f027b00047a2e142d9616ede4333a46b.tar.gz
chromium_src-eaa60485f027b00047a2e142d9616ede4333a46b.tar.bz2
Add ECPrivateKey for Elliptic Curve keypair generation.
The implementation uses NSS on all platforms unless USE_OPENSSL is defined (which is only stubbed out in this CL). BUG=88782 TEST=ECPrivateKeyUnitTest Review URL: http://codereview.chromium.org/8413024 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@109188 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'crypto/ec_private_key_unittest.cc')
-rw-r--r--crypto/ec_private_key_unittest.cc107
1 files changed, 107 insertions, 0 deletions
diff --git a/crypto/ec_private_key_unittest.cc b/crypto/ec_private_key_unittest.cc
new file mode 100644
index 0000000..a052a9a
--- /dev/null
+++ b/crypto/ec_private_key_unittest.cc
@@ -0,0 +1,107 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "crypto/ec_private_key.h"
+
+#include <vector>
+
+#include "base/memory/scoped_ptr.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+#if defined(USE_OPENSSL)
+// Once ECPrivateKey is implemented for OpenSSL, remove this #if block.
+// TODO(mattm): When that happens, also add some exported keys from each to test
+// interop between NSS and OpenSSL.
+TEST(ECPrivateKeyUnitTest, OpenSSLStub) {
+ scoped_ptr<crypto::ECPrivateKey> keypair1(
+ crypto::ECPrivateKey::Create());
+ ASSERT_FALSE(keypair1.get());
+}
+#else
+// Generate random private keys. Export, then re-import. We should get
+// back the same exact public key, and the private key should have the same
+// value and elliptic curve params.
+TEST(ECPrivateKeyUnitTest, InitRandomTest) {
+ const std::string password1 = "";
+ const std::string password2 = "test";
+
+ scoped_ptr<crypto::ECPrivateKey> keypair1(
+ crypto::ECPrivateKey::Create());
+ scoped_ptr<crypto::ECPrivateKey> keypair2(
+ crypto::ECPrivateKey::Create());
+ ASSERT_TRUE(keypair1.get());
+ ASSERT_TRUE(keypair2.get());
+
+ std::vector<uint8> key1value;
+ std::vector<uint8> key2value;
+ std::vector<uint8> key1params;
+ std::vector<uint8> key2params;
+ EXPECT_TRUE(keypair1->ExportValue(&key1value));
+ EXPECT_TRUE(keypair2->ExportValue(&key2value));
+ EXPECT_TRUE(keypair1->ExportECParams(&key1params));
+ EXPECT_TRUE(keypair2->ExportECParams(&key2params));
+
+ std::vector<uint8> privkey1;
+ std::vector<uint8> privkey2;
+ std::vector<uint8> pubkey1;
+ std::vector<uint8> pubkey2;
+ ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey(
+ password1, 1, &privkey1));
+ ASSERT_TRUE(keypair2->ExportEncryptedPrivateKey(
+ password2, 1, &privkey2));
+ EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1));
+ EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2));
+
+ scoped_ptr<crypto::ECPrivateKey> keypair3(
+ crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
+ password1, privkey1, pubkey1));
+ scoped_ptr<crypto::ECPrivateKey> keypair4(
+ crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
+ password2, privkey2, pubkey2));
+ ASSERT_TRUE(keypair3.get());
+ ASSERT_TRUE(keypair4.get());
+
+ std::vector<uint8> key3value;
+ std::vector<uint8> key4value;
+ std::vector<uint8> key3params;
+ std::vector<uint8> key4params;
+ EXPECT_TRUE(keypair3->ExportValue(&key3value));
+ EXPECT_TRUE(keypair4->ExportValue(&key4value));
+ EXPECT_TRUE(keypair3->ExportECParams(&key3params));
+ EXPECT_TRUE(keypair4->ExportECParams(&key4params));
+
+ EXPECT_EQ(key1value, key3value);
+ EXPECT_EQ(key2value, key4value);
+ EXPECT_EQ(key1params, key3params);
+ EXPECT_EQ(key2params, key4params);
+
+ std::vector<uint8> pubkey3;
+ std::vector<uint8> pubkey4;
+ EXPECT_TRUE(keypair3->ExportPublicKey(&pubkey3));
+ EXPECT_TRUE(keypair4->ExportPublicKey(&pubkey4));
+
+ EXPECT_EQ(pubkey1, pubkey3);
+ EXPECT_EQ(pubkey2, pubkey4);
+}
+
+TEST(ECPrivateKeyUnitTest, BadPasswordTest) {
+ const std::string password1 = "";
+ const std::string password2 = "test";
+
+ scoped_ptr<crypto::ECPrivateKey> keypair1(
+ crypto::ECPrivateKey::Create());
+ ASSERT_TRUE(keypair1.get());
+
+ std::vector<uint8> privkey1;
+ std::vector<uint8> pubkey1;
+ ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey(
+ password1, 1, &privkey1));
+ ASSERT_TRUE(keypair1->ExportPublicKey(&pubkey1));
+
+ scoped_ptr<crypto::ECPrivateKey> keypair2(
+ crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
+ password2, privkey1, pubkey1));
+ ASSERT_FALSE(keypair2.get());
+}
+#endif // !defined(USE_OPENSSL)