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