| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/sha1.h" | 8 #include "base/sha1.h" |
| 9 #include "crypto/rsa_private_key.h" | 9 #include "crypto/rsa_private_key.h" |
| 10 #include "crypto/sha2.h" |
| 10 #include "crypto/signature_creator.h" | 11 #include "crypto/signature_creator.h" |
| 11 #include "crypto/signature_verifier.h" | 12 #include "crypto/signature_verifier.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // This is the algorithm ID for SHA-1 with RSA encryption. | 17 // This is the algorithm ID for SHA-1 with RSA encryption. |
| 17 const uint8 kSHA1WithRSAAlgorithmID[] = { | 18 const uint8 kSHA1WithRSAAlgorithmID[] = { |
| 18 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, | 19 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 19 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00 | 20 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00 |
| 20 }; | 21 }; |
| 21 | 22 |
| 23 // This is the algorithm ID for SHA-1 with RSA encryption. |
| 24 const uint8 kSHA256WithRSAAlgorithmID[] = { |
| 25 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 26 0xf7, 0x0d, 0x01, 0x01, 0x0B, 0x05, 0x00 |
| 27 }; |
| 28 |
| 22 } | 29 } |
| 23 | 30 |
| 24 TEST(SignatureCreatorTest, BasicTest) { | 31 TEST(SignatureCreatorTest, BasicTest) { |
| 25 // Do a verify round trip. | 32 // Do a verify round trip. |
| 26 scoped_ptr<crypto::RSAPrivateKey> key_original( | 33 scoped_ptr<crypto::RSAPrivateKey> key_original( |
| 27 crypto::RSAPrivateKey::Create(1024)); | 34 crypto::RSAPrivateKey::Create(1024)); |
| 28 ASSERT_TRUE(key_original.get()); | 35 ASSERT_TRUE(key_original.get()); |
| 29 | 36 |
| 30 std::vector<uint8> key_info; | 37 std::vector<uint8> key_info; |
| 31 key_original->ExportPrivateKey(&key_info); | 38 key_original->ExportPrivateKey(&key_info); |
| 32 scoped_ptr<crypto::RSAPrivateKey> key( | 39 scoped_ptr<crypto::RSAPrivateKey> key( |
| 33 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); | 40 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
| 34 ASSERT_TRUE(key.get()); | 41 ASSERT_TRUE(key.get()); |
| 35 | 42 |
| 36 scoped_ptr<crypto::SignatureCreator> signer( | 43 scoped_ptr<crypto::SignatureCreator> signer( |
| 37 crypto::SignatureCreator::Create(key.get())); | 44 crypto::SignatureCreator::Create(key.get(), |
| 45 crypto::SignatureCreator::SHA1)); |
| 38 ASSERT_TRUE(signer.get()); | 46 ASSERT_TRUE(signer.get()); |
| 39 | 47 |
| 40 std::string data("Hello, World!"); | 48 std::string data("Hello, World!"); |
| 41 ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8*>(data.c_str()), | 49 ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8*>(data.c_str()), |
| 42 data.size())); | 50 data.size())); |
| 43 | 51 |
| 44 std::vector<uint8> signature; | 52 std::vector<uint8> signature; |
| 45 ASSERT_TRUE(signer->Final(&signature)); | 53 ASSERT_TRUE(signer->Final(&signature)); |
| 46 | 54 |
| 47 std::vector<uint8> public_key_info; | 55 std::vector<uint8> public_key_info; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 69 scoped_ptr<crypto::RSAPrivateKey> key( | 77 scoped_ptr<crypto::RSAPrivateKey> key( |
| 70 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); | 78 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
| 71 ASSERT_TRUE(key.get()); | 79 ASSERT_TRUE(key.get()); |
| 72 | 80 |
| 73 std::string data("Hello, World!"); | 81 std::string data("Hello, World!"); |
| 74 std::string sha1 = base::SHA1HashString(data); | 82 std::string sha1 = base::SHA1HashString(data); |
| 75 // Sign sha1 of the input data. | 83 // Sign sha1 of the input data. |
| 76 std::vector<uint8> signature; | 84 std::vector<uint8> signature; |
| 77 ASSERT_TRUE(crypto::SignatureCreator::Sign( | 85 ASSERT_TRUE(crypto::SignatureCreator::Sign( |
| 78 key.get(), | 86 key.get(), |
| 87 crypto::SignatureCreator::SHA1, |
| 79 reinterpret_cast<const uint8*>(sha1.c_str()), | 88 reinterpret_cast<const uint8*>(sha1.c_str()), |
| 80 sha1.size(), | 89 sha1.size(), |
| 81 &signature)); | 90 &signature)); |
| 82 | 91 |
| 83 std::vector<uint8> public_key_info; | 92 std::vector<uint8> public_key_info; |
| 84 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); | 93 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
| 85 | 94 |
| 86 // Verify the input data. | 95 // Verify the input data. |
| 87 crypto::SignatureVerifier verifier; | 96 crypto::SignatureVerifier verifier; |
| 88 ASSERT_TRUE(verifier.VerifyInit( | 97 ASSERT_TRUE(verifier.VerifyInit( |
| 89 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), | 98 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), |
| 90 &signature.front(), signature.size(), | 99 &signature.front(), signature.size(), |
| 91 &public_key_info.front(), public_key_info.size())); | 100 &public_key_info.front(), public_key_info.size())); |
| 92 | 101 |
| 93 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), | 102 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), |
| 94 data.size()); | 103 data.size()); |
| 95 ASSERT_TRUE(verifier.VerifyFinal()); | 104 ASSERT_TRUE(verifier.VerifyFinal()); |
| 96 } | 105 } |
| 106 |
| 107 TEST(SignatureCreatorTest, SignSHA256DigestTest) { |
| 108 // Do a verify round trip. |
| 109 scoped_ptr<crypto::RSAPrivateKey> key_original( |
| 110 crypto::RSAPrivateKey::Create(1024)); |
| 111 ASSERT_TRUE(key_original.get()); |
| 112 |
| 113 std::vector<uint8> key_info; |
| 114 key_original->ExportPrivateKey(&key_info); |
| 115 scoped_ptr<crypto::RSAPrivateKey> key( |
| 116 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
| 117 ASSERT_TRUE(key.get()); |
| 118 |
| 119 std::string data("Hello, World!"); |
| 120 std::string sha256 = crypto::SHA256HashString(data); |
| 121 // Sign sha256 of the input data. |
| 122 std::vector<uint8> signature; |
| 123 ASSERT_TRUE(crypto::SignatureCreator::Sign( |
| 124 key.get(), |
| 125 crypto::SignatureCreator::HashAlgorithm::SHA256, |
| 126 reinterpret_cast<const uint8*>(sha256.c_str()), |
| 127 sha256.size(), |
| 128 &signature)); |
| 129 |
| 130 std::vector<uint8> public_key_info; |
| 131 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
| 132 |
| 133 // Verify the input data. |
| 134 crypto::SignatureVerifier verifier; |
| 135 ASSERT_TRUE(verifier.VerifyInit( |
| 136 kSHA256WithRSAAlgorithmID, sizeof(kSHA256WithRSAAlgorithmID), |
| 137 &signature.front(), signature.size(), |
| 138 &public_key_info.front(), public_key_info.size())); |
| 139 |
| 140 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), |
| 141 data.size()); |
| 142 ASSERT_TRUE(verifier.VerifyFinal()); |
| 143 } |
| OLD | NEW |