OLD | NEW |
(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 // Generate random private keys. Export then, re-import. We should get |
| 13 // back the same exact public key, and the private key should have the same |
| 14 // value and elliptic curve params. |
| 15 TEST(ECPrivateKeyUnitTest, InitRandomTest) { |
| 16 #if defined(USE_OPENSSL) |
| 17 // Once ECPrivateKey is implemented for OpenSSL, remove this #if block. |
| 18 scoped_ptr<crypto::ECPrivateKey> keypair1( |
| 19 crypto::ECPrivateKey::Create()); |
| 20 ASSERT_FALSE(keypair1.get()); |
| 21 #else |
| 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()); |
| 28 |
| 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)); |
| 37 |
| 38 std::vector<uint8> privkey1; |
| 39 std::vector<uint8> privkey2; |
| 40 std::vector<uint8> pubkey1; |
| 41 std::vector<uint8> pubkey2; |
| 42 ASSERT_TRUE(keypair1->ExportPrivateKey(&privkey1)); |
| 43 ASSERT_TRUE(keypair2->ExportPrivateKey(&privkey2)); |
| 44 EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1)); |
| 45 EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2)); |
| 46 |
| 47 scoped_ptr<crypto::ECPrivateKey> keypair3( |
| 48 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(privkey1)); |
| 49 scoped_ptr<crypto::ECPrivateKey> keypair4( |
| 50 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(privkey2)); |
| 51 ASSERT_TRUE(keypair3.get()); |
| 52 ASSERT_TRUE(keypair4.get()); |
| 53 |
| 54 std::vector<uint8> key3value; |
| 55 std::vector<uint8> key4value; |
| 56 std::vector<uint8> key3params; |
| 57 std::vector<uint8> key4params; |
| 58 EXPECT_TRUE(keypair3->ExportValue(&key3value)); |
| 59 EXPECT_TRUE(keypair4->ExportValue(&key4value)); |
| 60 EXPECT_TRUE(keypair3->ExportECParams(&key3params)); |
| 61 EXPECT_TRUE(keypair4->ExportECParams(&key4params)); |
| 62 |
| 63 EXPECT_EQ(key1value, key3value); |
| 64 EXPECT_EQ(key2value, key4value); |
| 65 EXPECT_EQ(key1params, key3params); |
| 66 EXPECT_EQ(key2params, key4params); |
| 67 |
| 68 std::vector<uint8> pubkey3; |
| 69 std::vector<uint8> pubkey4; |
| 70 EXPECT_TRUE(keypair3->ExportPublicKey(&pubkey3)); |
| 71 EXPECT_TRUE(keypair4->ExportPublicKey(&pubkey4)); |
| 72 |
| 73 EXPECT_EQ(pubkey1, pubkey3); |
| 74 EXPECT_EQ(pubkey2, pubkey4); |
| 75 #endif // !defined(USE_OPENSSL) |
| 76 } |
OLD | NEW |