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