OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/crypto/rsa_private_key.h" | |
wtc
2011/04/07 05:35:53
It is correct to include "crypto/rsa_private_key.h
| |
6 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "crypto/rsa_private_key.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 // Generate random private keys with two different sizes. Reimport, then | 9 // Generate random private keys with two different sizes. Reimport, then |
10 // export them again. We should get back the same exact bytes. | 10 // export them again. We should get back the same exact bytes. |
11 TEST(RSAPrivateKeyUnitTest, InitRandomTest) { | 11 TEST(RSAPrivateKeyUnitTest, InitRandomTest) { |
12 scoped_ptr<base::RSAPrivateKey> keypair1(base::RSAPrivateKey::Create(1024)); | 12 scoped_ptr<crypto::RSAPrivateKey> keypair1( |
13 scoped_ptr<base::RSAPrivateKey> keypair2(base::RSAPrivateKey::Create(2048)); | 13 crypto::RSAPrivateKey::Create(1024)); |
14 scoped_ptr<crypto::RSAPrivateKey> keypair2( | |
15 crypto::RSAPrivateKey::Create(2048)); | |
14 ASSERT_TRUE(keypair1.get()); | 16 ASSERT_TRUE(keypair1.get()); |
15 ASSERT_TRUE(keypair2.get()); | 17 ASSERT_TRUE(keypair2.get()); |
16 | 18 |
17 std::vector<uint8> privkey1; | 19 std::vector<uint8> privkey1; |
18 std::vector<uint8> privkey2; | 20 std::vector<uint8> privkey2; |
19 std::vector<uint8> pubkey1; | 21 std::vector<uint8> pubkey1; |
20 std::vector<uint8> pubkey2; | 22 std::vector<uint8> pubkey2; |
21 | 23 |
22 ASSERT_TRUE(keypair1->ExportPrivateKey(&privkey1)); | 24 ASSERT_TRUE(keypair1->ExportPrivateKey(&privkey1)); |
23 ASSERT_TRUE(keypair2->ExportPrivateKey(&privkey2)); | 25 ASSERT_TRUE(keypair2->ExportPrivateKey(&privkey2)); |
24 ASSERT_TRUE(keypair1->ExportPublicKey(&pubkey1)); | 26 ASSERT_TRUE(keypair1->ExportPublicKey(&pubkey1)); |
25 ASSERT_TRUE(keypair2->ExportPublicKey(&pubkey2)); | 27 ASSERT_TRUE(keypair2->ExportPublicKey(&pubkey2)); |
26 | 28 |
27 scoped_ptr<base::RSAPrivateKey> keypair3( | 29 scoped_ptr<crypto::RSAPrivateKey> keypair3( |
28 base::RSAPrivateKey::CreateFromPrivateKeyInfo(privkey1)); | 30 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(privkey1)); |
29 scoped_ptr<base::RSAPrivateKey> keypair4( | 31 scoped_ptr<crypto::RSAPrivateKey> keypair4( |
30 base::RSAPrivateKey::CreateFromPrivateKeyInfo(privkey2)); | 32 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(privkey2)); |
31 ASSERT_TRUE(keypair3.get()); | 33 ASSERT_TRUE(keypair3.get()); |
32 ASSERT_TRUE(keypair4.get()); | 34 ASSERT_TRUE(keypair4.get()); |
33 | 35 |
34 std::vector<uint8> privkey3; | 36 std::vector<uint8> privkey3; |
35 std::vector<uint8> privkey4; | 37 std::vector<uint8> privkey4; |
36 ASSERT_TRUE(keypair3->ExportPrivateKey(&privkey3)); | 38 ASSERT_TRUE(keypair3->ExportPrivateKey(&privkey3)); |
37 ASSERT_TRUE(keypair4->ExportPrivateKey(&privkey4)); | 39 ASSERT_TRUE(keypair4->ExportPrivateKey(&privkey4)); |
38 | 40 |
39 ASSERT_EQ(privkey1.size(), privkey3.size()); | 41 ASSERT_EQ(privkey1.size(), privkey3.size()); |
40 ASSERT_EQ(privkey2.size(), privkey4.size()); | 42 ASSERT_EQ(privkey2.size(), privkey4.size()); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 0xfc, 0x2c, 0x72, 0xba, 0xac, 0xea, 0x7e, 0x18, | 154 0xfc, 0x2c, 0x72, 0xba, 0xac, 0xea, 0x7e, 0x18, |
153 0x53, 0x56, 0xa6, 0x83, 0xa2, 0xce, 0x93, 0x93, | 155 0x53, 0x56, 0xa6, 0x83, 0xa2, 0xce, 0x93, 0x93, |
154 0xe7, 0x1f, 0x0f, 0xe6, 0x0f, 0x02, 0x03, 0x01, | 156 0xe7, 0x1f, 0x0f, 0xe6, 0x0f, 0x02, 0x03, 0x01, |
155 0x00, 0x01 | 157 0x00, 0x01 |
156 }; | 158 }; |
157 | 159 |
158 std::vector<uint8> input; | 160 std::vector<uint8> input; |
159 input.resize(sizeof(private_key_info)); | 161 input.resize(sizeof(private_key_info)); |
160 memcpy(&input.front(), private_key_info, sizeof(private_key_info)); | 162 memcpy(&input.front(), private_key_info, sizeof(private_key_info)); |
161 | 163 |
162 scoped_ptr<base::RSAPrivateKey> key( | 164 scoped_ptr<crypto::RSAPrivateKey> key( |
163 base::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); | 165 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input)); |
164 ASSERT_TRUE(key.get()); | 166 ASSERT_TRUE(key.get()); |
165 | 167 |
166 std::vector<uint8> output; | 168 std::vector<uint8> output; |
167 ASSERT_TRUE(key->ExportPublicKey(&output)); | 169 ASSERT_TRUE(key->ExportPublicKey(&output)); |
168 | 170 |
169 ASSERT_TRUE( | 171 ASSERT_TRUE( |
170 memcmp(expected_public_key_info, &output.front(), output.size()) == 0); | 172 memcmp(expected_public_key_info, &output.front(), output.size()) == 0); |
171 } | 173 } |
172 | 174 |
173 // These two test keys each contain an integer that has 0x00 for its most | 175 // These two test keys each contain an integer that has 0x00 for its most |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 std::vector<uint8> input2; | 357 std::vector<uint8> input2; |
356 | 358 |
357 input1.resize(sizeof(short_integer_with_high_bit)); | 359 input1.resize(sizeof(short_integer_with_high_bit)); |
358 input2.resize(sizeof(short_integer_without_high_bit)); | 360 input2.resize(sizeof(short_integer_without_high_bit)); |
359 | 361 |
360 memcpy(&input1.front(), short_integer_with_high_bit, | 362 memcpy(&input1.front(), short_integer_with_high_bit, |
361 sizeof(short_integer_with_high_bit)); | 363 sizeof(short_integer_with_high_bit)); |
362 memcpy(&input2.front(), short_integer_without_high_bit, | 364 memcpy(&input2.front(), short_integer_without_high_bit, |
363 sizeof(short_integer_without_high_bit)); | 365 sizeof(short_integer_without_high_bit)); |
364 | 366 |
365 scoped_ptr<base::RSAPrivateKey> keypair1( | 367 scoped_ptr<crypto::RSAPrivateKey> keypair1( |
366 base::RSAPrivateKey::CreateFromPrivateKeyInfo(input1)); | 368 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input1)); |
367 scoped_ptr<base::RSAPrivateKey> keypair2( | 369 scoped_ptr<crypto::RSAPrivateKey> keypair2( |
368 base::RSAPrivateKey::CreateFromPrivateKeyInfo(input2)); | 370 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input2)); |
369 ASSERT_TRUE(keypair1.get()); | 371 ASSERT_TRUE(keypair1.get()); |
370 ASSERT_TRUE(keypair2.get()); | 372 ASSERT_TRUE(keypair2.get()); |
371 | 373 |
372 std::vector<uint8> output1; | 374 std::vector<uint8> output1; |
373 std::vector<uint8> output2; | 375 std::vector<uint8> output2; |
374 ASSERT_TRUE(keypair1->ExportPrivateKey(&output1)); | 376 ASSERT_TRUE(keypair1->ExportPrivateKey(&output1)); |
375 ASSERT_TRUE(keypair2->ExportPrivateKey(&output2)); | 377 ASSERT_TRUE(keypair2->ExportPrivateKey(&output2)); |
376 | 378 |
377 ASSERT_EQ(input1.size(), output1.size()); | 379 ASSERT_EQ(input1.size(), output1.size()); |
378 ASSERT_EQ(input2.size(), output2.size()); | 380 ASSERT_EQ(input2.size(), output2.size()); |
379 ASSERT_TRUE(0 == memcmp(&output1.front(), &input1.front(), | 381 ASSERT_TRUE(0 == memcmp(&output1.front(), &input1.front(), |
380 input1.size())); | 382 input1.size())); |
381 ASSERT_TRUE(0 == memcmp(&output2.front(), &input2.front(), | 383 ASSERT_TRUE(0 == memcmp(&output2.front(), &input2.front(), |
382 input2.size())); | 384 input2.size())); |
383 } | 385 } |
OLD | NEW |