[email protected] | 5ee44d4 | 2012-02-08 00:14:54 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 8 | #include "base/sha1.h" |
[email protected] | 5ee44d4 | 2012-02-08 00:14:54 | [diff] [blame] | 9 | #include "crypto/rsa_private_key.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 10 | #include "crypto/signature_creator.h" |
| 11 | #include "crypto/signature_verifier.h" |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | // This is the algorithm ID for SHA-1 with RSA encryption. |
| 17 | const uint8 kSHA1WithRSAAlgorithmID[] = { |
| 18 | 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 19 | 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00 |
| 20 | }; |
| 21 | |
| 22 | } |
| 23 | |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 24 | TEST(SignatureCreatorTest, BasicTest) { |
| 25 | // Do a verify round trip. |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 26 | scoped_ptr<crypto::RSAPrivateKey> key_original( |
| 27 | crypto::RSAPrivateKey::Create(1024)); |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 28 | ASSERT_TRUE(key_original.get()); |
| 29 | |
| 30 | std::vector<uint8> key_info; |
| 31 | key_original->ExportPrivateKey(&key_info); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 32 | scoped_ptr<crypto::RSAPrivateKey> key( |
| 33 | crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 34 | ASSERT_TRUE(key.get()); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 35 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 36 | scoped_ptr<crypto::SignatureCreator> signer( |
| 37 | crypto::SignatureCreator::Create(key.get())); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 38 | ASSERT_TRUE(signer.get()); |
| 39 | |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 40 | std::string data("Hello, World!"); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 41 | ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8*>(data.c_str()), |
| 42 | data.size())); |
| 43 | |
| 44 | std::vector<uint8> signature; |
| 45 | ASSERT_TRUE(signer->Final(&signature)); |
| 46 | |
| 47 | std::vector<uint8> public_key_info; |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 48 | ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 49 | |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 50 | crypto::SignatureVerifier verifier; |
| 51 | ASSERT_TRUE(verifier.VerifyInit( |
| 52 | kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), |
| 53 | &signature.front(), signature.size(), |
| 54 | &public_key_info.front(), public_key_info.size())); |
| 55 | |
| 56 | verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), |
| 57 | data.size()); |
| 58 | ASSERT_TRUE(verifier.VerifyFinal()); |
| 59 | } |
| 60 | |
| 61 | TEST(SignatureCreatorTest, SignDigestTest) { |
| 62 | // Do a verify round trip. |
| 63 | scoped_ptr<crypto::RSAPrivateKey> key_original( |
| 64 | crypto::RSAPrivateKey::Create(1024)); |
| 65 | ASSERT_TRUE(key_original.get()); |
| 66 | |
| 67 | std::vector<uint8> key_info; |
| 68 | key_original->ExportPrivateKey(&key_info); |
| 69 | scoped_ptr<crypto::RSAPrivateKey> key( |
| 70 | crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
| 71 | ASSERT_TRUE(key.get()); |
| 72 | |
| 73 | std::string data("Hello, World!"); |
| 74 | std::string sha1 = base::SHA1HashString(data); |
| 75 | // Sign sha1 of the input data. |
| 76 | std::vector<uint8> signature; |
| 77 | ASSERT_TRUE(crypto::SignatureCreator::Sign( |
| 78 | key.get(), |
| 79 | reinterpret_cast<const uint8*>(sha1.c_str()), |
| 80 | sha1.size(), |
| 81 | &signature)); |
| 82 | |
| 83 | std::vector<uint8> public_key_info; |
| 84 | ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
| 85 | |
| 86 | // Verify the input data. |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 87 | crypto::SignatureVerifier verifier; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 88 | ASSERT_TRUE(verifier.VerifyInit( |
| 89 | kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), |
| 90 | &signature.front(), signature.size(), |
| 91 | &public_key_info.front(), public_key_info.size())); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 92 | |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 93 | verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), |
| 94 | data.size()); |
| 95 | ASSERT_TRUE(verifier.VerifyFinal()); |
| 96 | } |