[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 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 5 | #include "crypto/signature_creator.h" |
| 6 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 9 | #include <memory> |
davidben | 6004dc5 | 2017-02-03 04:15:29 | [diff] [blame] | 10 | #include <string> |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 13 | #include "base/sha1.h" |
[email protected] | 5ee44d4 | 2012-02-08 00:14:54 | [diff] [blame] | 14 | #include "crypto/rsa_private_key.h" |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 15 | #include "crypto/sha2.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 16 | #include "crypto/signature_verifier.h" |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | TEST(SignatureCreatorTest, BasicTest) { |
| 20 | // Do a verify round trip. |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 21 | std::unique_ptr<crypto::RSAPrivateKey> key_original( |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 22 | crypto::RSAPrivateKey::Create(1024)); |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 23 | ASSERT_TRUE(key_original.get()); |
| 24 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 25 | std::vector<uint8_t> key_info; |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 26 | key_original->ExportPrivateKey(&key_info); |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 27 | std::unique_ptr<crypto::RSAPrivateKey> key( |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 28 | crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
[email protected] | 308379a5 | 2009-10-07 02:46:31 | [diff] [blame] | 29 | ASSERT_TRUE(key.get()); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 30 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 31 | std::unique_ptr<crypto::SignatureCreator> signer( |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 32 | crypto::SignatureCreator::Create(key.get(), |
| 33 | crypto::SignatureCreator::SHA1)); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 34 | ASSERT_TRUE(signer.get()); |
| 35 | |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 36 | std::string data("Hello, World!"); |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 37 | ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8_t*>(data.c_str()), |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 38 | data.size())); |
| 39 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 40 | std::vector<uint8_t> signature; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 41 | ASSERT_TRUE(signer->Final(&signature)); |
| 42 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 43 | std::vector<uint8_t> public_key_info; |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 44 | ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 45 | |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 46 | crypto::SignatureVerifier verifier; |
| 47 | ASSERT_TRUE(verifier.VerifyInit( |
davidben | 9c97a36 | 2016-03-03 16:18:26 | [diff] [blame] | 48 | crypto::SignatureVerifier::RSA_PKCS1_SHA1, &signature.front(), |
| 49 | signature.size(), &public_key_info.front(), public_key_info.size())); |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 50 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 51 | verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()), |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 52 | data.size()); |
| 53 | ASSERT_TRUE(verifier.VerifyFinal()); |
| 54 | } |
| 55 | |
| 56 | TEST(SignatureCreatorTest, SignDigestTest) { |
| 57 | // Do a verify round trip. |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 58 | std::unique_ptr<crypto::RSAPrivateKey> key_original( |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 59 | crypto::RSAPrivateKey::Create(1024)); |
| 60 | ASSERT_TRUE(key_original.get()); |
| 61 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 62 | std::vector<uint8_t> key_info; |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 63 | key_original->ExportPrivateKey(&key_info); |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 64 | std::unique_ptr<crypto::RSAPrivateKey> key( |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 65 | crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
| 66 | ASSERT_TRUE(key.get()); |
| 67 | |
| 68 | std::string data("Hello, World!"); |
| 69 | std::string sha1 = base::SHA1HashString(data); |
| 70 | // Sign sha1 of the input data. |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 71 | std::vector<uint8_t> signature; |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 72 | ASSERT_TRUE(crypto::SignatureCreator::Sign( |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 73 | key.get(), crypto::SignatureCreator::SHA1, |
| 74 | reinterpret_cast<const uint8_t*>(sha1.c_str()), sha1.size(), &signature)); |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 75 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 76 | std::vector<uint8_t> public_key_info; |
[email protected] | ed31834b | 2013-07-09 08:32:40 | [diff] [blame] | 77 | ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
| 78 | |
| 79 | // Verify the input data. |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 80 | crypto::SignatureVerifier verifier; |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 81 | ASSERT_TRUE(verifier.VerifyInit( |
davidben | 9c97a36 | 2016-03-03 16:18:26 | [diff] [blame] | 82 | crypto::SignatureVerifier::RSA_PKCS1_SHA1, &signature.front(), |
| 83 | signature.size(), &public_key_info.front(), public_key_info.size())); |
[email protected] | 28ae8fe | 2009-06-05 18:25:06 | [diff] [blame] | 84 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 85 | verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()), |
[email protected] | 7eee790 | 2009-06-06 06:06:30 | [diff] [blame] | 86 | data.size()); |
| 87 | ASSERT_TRUE(verifier.VerifyFinal()); |
| 88 | } |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 89 | |
| 90 | TEST(SignatureCreatorTest, SignSHA256DigestTest) { |
| 91 | // Do a verify round trip. |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 92 | std::unique_ptr<crypto::RSAPrivateKey> key_original( |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 93 | crypto::RSAPrivateKey::Create(1024)); |
| 94 | ASSERT_TRUE(key_original.get()); |
| 95 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 96 | std::vector<uint8_t> key_info; |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 97 | key_original->ExportPrivateKey(&key_info); |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 98 | std::unique_ptr<crypto::RSAPrivateKey> key( |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 99 | crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
| 100 | ASSERT_TRUE(key.get()); |
| 101 | |
| 102 | std::string data("Hello, World!"); |
| 103 | std::string sha256 = crypto::SHA256HashString(data); |
| 104 | // Sign sha256 of the input data. |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 105 | std::vector<uint8_t> signature; |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 106 | ASSERT_TRUE(crypto::SignatureCreator::Sign( |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 107 | key.get(), crypto::SignatureCreator::HashAlgorithm::SHA256, |
| 108 | reinterpret_cast<const uint8_t*>(sha256.c_str()), sha256.size(), |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 109 | &signature)); |
| 110 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 111 | std::vector<uint8_t> public_key_info; |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 112 | ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
| 113 | |
| 114 | // Verify the input data. |
| 115 | crypto::SignatureVerifier verifier; |
| 116 | ASSERT_TRUE(verifier.VerifyInit( |
davidben | 9c97a36 | 2016-03-03 16:18:26 | [diff] [blame] | 117 | crypto::SignatureVerifier::RSA_PKCS1_SHA256, &signature.front(), |
| 118 | signature.size(), &public_key_info.front(), public_key_info.size())); |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 119 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 120 | verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()), |
dougsteed | 0cf460ec | 2014-09-19 18:46:09 | [diff] [blame] | 121 | data.size()); |
| 122 | ASSERT_TRUE(verifier.VerifyFinal()); |
| 123 | } |