Add chromium_builder_lkgr_drmemory_win target for LKGR DrMemory build on ClusterFuzz
[chromium-blink-merge.git] / crypto / ec_private_key_unittest.cc
blob0f4479b72f0d8b7c14ead8e919e82358faf9e4a4
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"
7 #include <vector>
9 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
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 const std::string password1;
17 const std::string password2 = "test";
19 scoped_ptr<crypto::ECPrivateKey> keypair1(crypto::ECPrivateKey::Create());
20 scoped_ptr<crypto::ECPrivateKey> keypair2(crypto::ECPrivateKey::Create());
21 ASSERT_TRUE(keypair1.get());
22 ASSERT_TRUE(keypair2.get());
24 std::vector<uint8> key1value;
25 std::vector<uint8> key2value;
26 std::vector<uint8> key1params;
27 std::vector<uint8> key2params;
28 EXPECT_TRUE(keypair1->ExportValue(&key1value));
29 EXPECT_TRUE(keypair2->ExportValue(&key2value));
30 EXPECT_TRUE(keypair1->ExportECParams(&key1params));
31 EXPECT_TRUE(keypair2->ExportECParams(&key2params));
33 std::vector<uint8> privkey1;
34 std::vector<uint8> privkey2;
35 std::vector<uint8> pubkey1;
36 std::vector<uint8> pubkey2;
37 std::string raw_pubkey1;
38 std::string raw_pubkey2;
39 ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey(password1, 1, &privkey1));
40 ASSERT_TRUE(keypair2->ExportEncryptedPrivateKey(password2, 1, &privkey2));
41 EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1));
42 EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2));
43 EXPECT_TRUE(keypair1->ExportRawPublicKey(&raw_pubkey1));
44 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2));
46 scoped_ptr<crypto::ECPrivateKey> keypair3(
47 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
48 password1, privkey1, pubkey1));
49 scoped_ptr<crypto::ECPrivateKey> keypair4(
50 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
51 password2, privkey2, pubkey2));
52 ASSERT_TRUE(keypair3.get());
53 ASSERT_TRUE(keypair4.get());
55 std::vector<uint8> key3value;
56 std::vector<uint8> key4value;
57 std::vector<uint8> key3params;
58 std::vector<uint8> key4params;
59 EXPECT_TRUE(keypair3->ExportValue(&key3value));
60 EXPECT_TRUE(keypair4->ExportValue(&key4value));
61 EXPECT_TRUE(keypair3->ExportECParams(&key3params));
62 EXPECT_TRUE(keypair4->ExportECParams(&key4params));
64 EXPECT_EQ(key1value, key3value);
65 EXPECT_EQ(key2value, key4value);
66 EXPECT_EQ(key1params, key3params);
67 EXPECT_EQ(key2params, key4params);
69 std::vector<uint8> pubkey3;
70 std::vector<uint8> pubkey4;
71 std::string raw_pubkey3;
72 std::string raw_pubkey4;
73 EXPECT_TRUE(keypair3->ExportPublicKey(&pubkey3));
74 EXPECT_TRUE(keypair4->ExportPublicKey(&pubkey4));
75 EXPECT_TRUE(keypair3->ExportRawPublicKey(&raw_pubkey3));
76 EXPECT_TRUE(keypair4->ExportRawPublicKey(&raw_pubkey4));
78 EXPECT_EQ(pubkey1, pubkey3);
79 EXPECT_EQ(pubkey2, pubkey4);
80 EXPECT_EQ(raw_pubkey1, raw_pubkey3);
81 EXPECT_EQ(raw_pubkey2, raw_pubkey4);
84 #if !defined(USE_OPENSSL)
85 TEST(ECPrivateKeyUnitTest, Copy) {
86 scoped_ptr<crypto::ECPrivateKey> keypair1(crypto::ECPrivateKey::Create());
87 scoped_ptr<crypto::ECPrivateKey> keypair2(keypair1->Copy());
88 ASSERT_TRUE(keypair1.get());
89 ASSERT_TRUE(keypair2.get());
91 std::vector<uint8> key1value;
92 std::vector<uint8> key2value;
93 EXPECT_TRUE(keypair1->ExportValue(&key1value));
94 EXPECT_TRUE(keypair2->ExportValue(&key2value));
95 EXPECT_EQ(key1value, key2value);
97 std::vector<uint8> key1params;
98 std::vector<uint8> key2params;
99 EXPECT_TRUE(keypair1->ExportECParams(&key1params));
100 EXPECT_TRUE(keypair2->ExportECParams(&key2params));
101 EXPECT_EQ(key1params, key2params);
103 std::vector<uint8> pubkey1;
104 std::vector<uint8> pubkey2;
105 EXPECT_TRUE(keypair1->ExportPublicKey(&pubkey1));
106 EXPECT_TRUE(keypair2->ExportPublicKey(&pubkey2));
107 EXPECT_EQ(pubkey1, pubkey2);
109 std::string raw_pubkey1;
110 std::string raw_pubkey2;
111 EXPECT_TRUE(keypair1->ExportRawPublicKey(&raw_pubkey1));
112 EXPECT_TRUE(keypair2->ExportRawPublicKey(&raw_pubkey2));
113 EXPECT_EQ(raw_pubkey1, raw_pubkey2);
115 #endif // !defined(USE_OPENSSL)
117 TEST(ECPrivateKeyUnitTest, BadPasswordTest) {
118 const std::string password1;
119 const std::string password2 = "test";
121 scoped_ptr<crypto::ECPrivateKey> keypair1(
122 crypto::ECPrivateKey::Create());
123 ASSERT_TRUE(keypair1.get());
125 std::vector<uint8> privkey1;
126 std::vector<uint8> pubkey1;
127 ASSERT_TRUE(keypair1->ExportEncryptedPrivateKey(
128 password1, 1, &privkey1));
129 ASSERT_TRUE(keypair1->ExportPublicKey(&pubkey1));
131 scoped_ptr<crypto::ECPrivateKey> keypair2(
132 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
133 password2, privkey1, pubkey1));
134 ASSERT_FALSE(keypair2.get());
137 TEST(ECPrivateKeyUnitTest, LoadNSSKeyTest) {
138 static const unsigned char nss_key[] = {
139 0x30, 0x81, 0xb8, 0x30, 0x23, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7,
140 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x15, 0x04, 0x10, 0x3f, 0xac, 0xe9,
141 0x38, 0xdb, 0x40, 0x6b, 0x26, 0x89, 0x09, 0x73, 0x18, 0x8d, 0x7f, 0x1c,
142 0x82, 0x02, 0x01, 0x01, 0x04, 0x81, 0x90, 0x5e, 0x5e, 0x11, 0xef, 0xbb,
143 0x7c, 0x4d, 0xec, 0xc0, 0xdc, 0xc7, 0x23, 0xd2, 0xc4, 0x77, 0xbc, 0xf4,
144 0x5d, 0x59, 0x4c, 0x07, 0xc2, 0x8a, 0x26, 0xfa, 0x25, 0x1c, 0xaa, 0x42,
145 0xed, 0xd0, 0xed, 0xbb, 0x5c, 0xe9, 0x13, 0x07, 0xaa, 0xdd, 0x52, 0x3c,
146 0x65, 0x25, 0xbf, 0x94, 0x02, 0xaf, 0xd6, 0x97, 0xe9, 0x33, 0x00, 0x76,
147 0x64, 0x4a, 0x73, 0xab, 0xfb, 0x99, 0x6e, 0x83, 0x12, 0x05, 0x86, 0x72,
148 0x6c, 0xd5, 0xa4, 0xcf, 0xb1, 0xd5, 0x4d, 0x54, 0x87, 0x8b, 0x4b, 0x95,
149 0x1d, 0xcd, 0xf3, 0xfe, 0xa8, 0xda, 0xe0, 0xb6, 0x72, 0x13, 0x3f, 0x2e,
150 0x66, 0xe0, 0xb9, 0x2e, 0xfa, 0x69, 0x40, 0xbe, 0xd7, 0x67, 0x6e, 0x53,
151 0x2b, 0x3f, 0x53, 0xe5, 0x39, 0x54, 0x77, 0xe1, 0x1d, 0xe6, 0x81, 0x92,
152 0x58, 0x82, 0x14, 0xfb, 0x47, 0x85, 0x3c, 0xc3, 0xdf, 0xdd, 0xcc, 0x79,
153 0x9f, 0x41, 0x83, 0x72, 0xf2, 0x0a, 0xe9, 0xe1, 0x2c, 0x12, 0xb0, 0xb0,
154 0x0a, 0xb2, 0x1d, 0xca, 0x15, 0xb2, 0xca};
155 unsigned int nss_key_len = 187;
156 static const unsigned char nss_pub_key[] = {
157 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
158 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
159 0x42, 0x00, 0x04, 0x85, 0x92, 0x9e, 0x95, 0x5c, 0x6b, 0x9e, 0xd6, 0x1e,
160 0xb8, 0x64, 0xea, 0xc2, 0xb3, 0xef, 0x18, 0xed, 0x3a, 0x5e, 0xc4, 0x5c,
161 0x15, 0x37, 0x6a, 0xe9, 0xaa, 0x0b, 0x34, 0x03, 0xfd, 0xca, 0x83, 0x0f,
162 0xd7, 0x5c, 0x5d, 0xc5, 0x53, 0x6e, 0xe5, 0xa9, 0x33, 0xd5, 0xcc, 0xab,
163 0x53, 0x78, 0xdd, 0xd6, 0x12, 0x3a, 0x5e, 0xeb, 0xbf, 0xdf, 0x16, 0xd3,
164 0x2c, 0x3b, 0xe8, 0xdb, 0x19, 0xfc, 0x5e};
165 unsigned int nss_pub_key_len = 91;
167 scoped_ptr<crypto::ECPrivateKey> keypair_nss(
168 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
170 std::vector<uint8>(nss_key, nss_key + nss_key_len),
171 std::vector<uint8>(nss_pub_key, nss_pub_key + nss_pub_key_len)));
173 EXPECT_TRUE(keypair_nss.get());
176 // The plan is to migrate to OpenSSL everywhere, so making NSS implementation be
177 // able to load the OpenSSL generated keys isn't that important.
178 #if defined(USE_OPENSSL)
179 TEST(ECPrivateKeyUnitTest, LoadOpenSSLKeyTest) {
180 static const unsigned char openssl_key[] = {
181 0x30, 0x82, 0x01, 0xa1, 0x30, 0x1b, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86,
182 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0d, 0x04, 0x08, 0x86, 0xaa,
183 0xd7, 0xdf, 0x3b, 0x91, 0x97, 0x60, 0x02, 0x01, 0x01, 0x04, 0x82, 0x01,
184 0x80, 0xcb, 0x2a, 0x14, 0xaa, 0x4f, 0x38, 0x4c, 0xe1, 0x49, 0x00, 0xe2,
185 0x1a, 0x3a, 0x75, 0x87, 0x7e, 0x3d, 0xea, 0x4d, 0x53, 0xd4, 0x46, 0x47,
186 0x23, 0x8f, 0xa1, 0x72, 0x51, 0x92, 0x86, 0x8b, 0xeb, 0x53, 0xe6, 0x6a,
187 0x0a, 0x6b, 0xb6, 0xa0, 0xdc, 0x0f, 0xdc, 0x20, 0xc3, 0x45, 0x85, 0xf1,
188 0x95, 0x90, 0x5c, 0xf4, 0xfa, 0xee, 0x47, 0xaf, 0x35, 0xd0, 0xd0, 0xd3,
189 0x14, 0xde, 0x0d, 0xca, 0x1b, 0xd3, 0xbb, 0x20, 0xec, 0x9d, 0x6a, 0xd4,
190 0xc1, 0xce, 0x60, 0x81, 0xab, 0x0c, 0x72, 0x10, 0xfa, 0x28, 0x3c, 0xac,
191 0x87, 0x7b, 0x82, 0x85, 0x00, 0xb8, 0x58, 0x9c, 0x07, 0xc4, 0x7d, 0xa9,
192 0xc5, 0x94, 0x95, 0xf7, 0x23, 0x93, 0x3f, 0xed, 0xef, 0x92, 0x55, 0x25,
193 0x74, 0xbb, 0xd3, 0xd1, 0x67, 0x3b, 0x3d, 0x5a, 0xfe, 0x84, 0xf8, 0x97,
194 0x7d, 0x7c, 0x01, 0xc7, 0xd7, 0x0d, 0xf8, 0xc3, 0x6d, 0xd6, 0xf1, 0xaa,
195 0x9d, 0x1f, 0x69, 0x97, 0x45, 0x06, 0xc4, 0x1c, 0x95, 0x3c, 0xe0, 0xef,
196 0x11, 0xb2, 0xb3, 0x72, 0x91, 0x9e, 0x7d, 0x0f, 0x7f, 0xc8, 0xf6, 0x64,
197 0x49, 0x5e, 0x3c, 0x53, 0x37, 0x79, 0x03, 0x1c, 0x3f, 0x29, 0x6c, 0x6b,
198 0xea, 0x4c, 0x35, 0x9b, 0x6d, 0x1b, 0x59, 0x43, 0x4c, 0x14, 0x47, 0x2a,
199 0x36, 0x39, 0x2a, 0xd8, 0x96, 0x90, 0xdc, 0xfc, 0xd2, 0xdd, 0x23, 0x0e,
200 0x2c, 0xb3, 0x83, 0xf9, 0xf2, 0xe3, 0xe6, 0x99, 0x53, 0x57, 0x33, 0xc5,
201 0x5f, 0xf9, 0xfd, 0x56, 0x0b, 0x32, 0xd4, 0xf3, 0x9d, 0x5b, 0x34, 0xe5,
202 0x94, 0xbf, 0xb6, 0xc0, 0xce, 0xe1, 0x73, 0x5c, 0x02, 0x7a, 0x4c, 0xed,
203 0xde, 0x23, 0x38, 0x89, 0x9f, 0xcd, 0x51, 0xf3, 0x90, 0x80, 0xd3, 0x4b,
204 0x83, 0xd3, 0xee, 0xf2, 0x9e, 0x35, 0x91, 0xa5, 0xa3, 0xc0, 0x5c, 0xce,
205 0xdb, 0xaa, 0x70, 0x1e, 0x1d, 0xc1, 0x44, 0xea, 0x3b, 0xa7, 0x5a, 0x11,
206 0xd1, 0xf3, 0xf3, 0xd0, 0xf4, 0x5a, 0xc4, 0x99, 0xaf, 0x8d, 0xe2, 0xbc,
207 0xa2, 0xb9, 0x3d, 0x86, 0x5e, 0xba, 0xa0, 0xdf, 0x78, 0x81, 0x7c, 0x54,
208 0x31, 0xe3, 0x98, 0xb5, 0x46, 0xcb, 0x4d, 0x26, 0x4b, 0xf8, 0xac, 0x3a,
209 0x54, 0x1b, 0x77, 0x5a, 0x18, 0xa5, 0x43, 0x0e, 0x14, 0xde, 0x7b, 0xb7,
210 0x4e, 0x45, 0x99, 0x03, 0xd1, 0x3d, 0x18, 0xb2, 0x36, 0x00, 0x48, 0x07,
211 0x72, 0xbb, 0x4f, 0x21, 0x25, 0x3e, 0xda, 0x25, 0x24, 0x5b, 0xc8, 0xa0,
212 0x28, 0xd5, 0x9b, 0x96, 0x87, 0x07, 0x77, 0x84, 0xff, 0xd7, 0xac, 0x71,
213 0xf6, 0x61, 0x63, 0x0b, 0xfb, 0x42, 0xfd, 0x52, 0xf4, 0xc4, 0x35, 0x0c,
214 0xc2, 0xc1, 0x55, 0x22, 0x42, 0x2f, 0x13, 0x7d, 0x93, 0x27, 0xc8, 0x11,
215 0x35, 0xc5, 0xe3, 0xc5, 0xaa, 0x15, 0x3c, 0xac, 0x30, 0xbc, 0x45, 0x16,
216 0xed};
217 unsigned int openssl_key_len = 421;
218 static const unsigned char openssl_pub_key[] = {
219 0x30, 0x82, 0x01, 0x4b, 0x30, 0x82, 0x01, 0x03, 0x06, 0x07, 0x2a, 0x86,
220 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30, 0x81, 0xf7, 0x02, 0x01, 0x01, 0x30,
221 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21,
222 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
223 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
224 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x30, 0x5b, 0x04,
225 0x20, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
226 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
227 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x04, 0x20, 0x5a,
228 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb, 0xbd, 0x55, 0x76,
229 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6, 0x3b,
230 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b, 0x03, 0x15, 0x00, 0xc4, 0x9d,
231 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93, 0x6a, 0x66, 0x78, 0xe1, 0x13, 0x9d,
232 0x26, 0xb7, 0x81, 0x9f, 0x7e, 0x90, 0x04, 0x41, 0x04, 0x6b, 0x17, 0xd1,
233 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40,
234 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39,
235 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f,
236 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33,
237 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51,
238 0xf5, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
239 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, 0xe6, 0xfa, 0xad,
240 0xa7, 0x17, 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51,
241 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04, 0xde, 0x09, 0x08, 0x07, 0x03,
242 0x2e, 0x8f, 0x37, 0x9a, 0xd5, 0xad, 0xe5, 0xc6, 0x9d, 0xd4, 0x63, 0xc7,
243 0x4a, 0xe7, 0x20, 0xcb, 0x90, 0xa0, 0x1f, 0x18, 0x18, 0x72, 0xb5, 0x21,
244 0x88, 0x38, 0xc0, 0xdb, 0xba, 0xf6, 0x99, 0xd8, 0xa5, 0x3b, 0x83, 0xe9,
245 0xe3, 0xd5, 0x61, 0x99, 0x73, 0x42, 0xc6, 0x6c, 0xe8, 0x0a, 0x95, 0x40,
246 0x41, 0x3b, 0x0d, 0x10, 0xa7, 0x4a, 0x93, 0xdb, 0x5a, 0xe7, 0xec};
247 unsigned int openssl_pub_key_len = 335;
249 scoped_ptr<crypto::ECPrivateKey> keypair_openssl(
250 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
252 std::vector<uint8>(openssl_key, openssl_key + openssl_key_len),
253 std::vector<uint8>(openssl_pub_key,
254 openssl_pub_key + openssl_pub_key_len)));
256 EXPECT_TRUE(keypair_openssl.get());
258 #endif // defined(USE_OPENSSL)