[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 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 <vector> |
| 6 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 7 | #include "base/memory/scoped_ptr.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 8 | #include "crypto/signature_creator.h" |
| 9 | #include "crypto/signature_verifier.h" |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | TEST(SignatureCreatorTest, BasicTest) { |
| 13 | // Do a verify round trip. |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 14 | scoped_ptr<crypto::RSAPrivateKey> key_original( |
| 15 | crypto::RSAPrivateKey::Create(1024)); |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 16 | ASSERT_TRUE(key_original.get()); |
| 17 | |
| 18 | std::vector<uint8> key_info; |
| 19 | key_original->ExportPrivateKey(&key_info); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 20 | scoped_ptr<crypto::RSAPrivateKey> key( |
| 21 | crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 22 | ASSERT_TRUE(key.get()); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 23 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 24 | scoped_ptr<crypto::SignatureCreator> signer( |
| 25 | crypto::SignatureCreator::Create(key.get())); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 26 | ASSERT_TRUE(signer.get()); |
| 27 | |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 28 | std::string data("Hello, World!"); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 29 | ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8*>(data.c_str()), |
| 30 | data.size())); |
| 31 | |
| 32 | std::vector<uint8> signature; |
| 33 | ASSERT_TRUE(signer->Final(&signature)); |
| 34 | |
| 35 | std::vector<uint8> public_key_info; |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 36 | ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 37 | |
| 38 | // This is the algorithm ID for SHA-1 with RSA encryption. |
| 39 | // TODO(aa): Factor this out into some shared location. |
| 40 | const uint8 kSHA1WithRSAAlgorithmID[] = { |
| 41 | 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 42 | 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00 |
| 43 | }; |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 44 | crypto::SignatureVerifier verifier; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 45 | ASSERT_TRUE(verifier.VerifyInit( |
| 46 | kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), |
| 47 | &signature.front(), signature.size(), |
| 48 | &public_key_info.front(), public_key_info.size())); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 49 | |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 50 | verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), |
| 51 | data.size()); |
| 52 | ASSERT_TRUE(verifier.VerifyFinal()); |
| 53 | } |