Chromium Code Reviews| 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. | |
|
davidben
2014/09/10 22:03:54
Nit: s/SHA-1/SHA-256/
| |
| 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); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 crypto::SignatureVerifier verifier; | 94 crypto::SignatureVerifier verifier; |
| 88 ASSERT_TRUE(verifier.VerifyInit( | 95 ASSERT_TRUE(verifier.VerifyInit( |
| 89 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), | 96 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), |
| 90 &signature.front(), signature.size(), | 97 &signature.front(), signature.size(), |
| 91 &public_key_info.front(), public_key_info.size())); | 98 &public_key_info.front(), public_key_info.size())); |
| 92 | 99 |
| 93 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), | 100 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), |
| 94 data.size()); | 101 data.size()); |
| 95 ASSERT_TRUE(verifier.VerifyFinal()); | 102 ASSERT_TRUE(verifier.VerifyFinal()); |
| 96 } | 103 } |
| 104 | |
| 105 TEST(SignatureCreatorTest, SignSHA256DigestTest) { | |
| 106 // Do a verify round trip. | |
| 107 scoped_ptr<crypto::RSAPrivateKey> key_original( | |
| 108 crypto::RSAPrivateKey::Create(1024)); | |
| 109 ASSERT_TRUE(key_original.get()); | |
| 110 | |
| 111 std::vector<uint8> key_info; | |
| 112 key_original->ExportPrivateKey(&key_info); | |
| 113 scoped_ptr<crypto::RSAPrivateKey> key( | |
| 114 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); | |
| 115 ASSERT_TRUE(key.get()); | |
| 116 | |
| 117 std::string data("Hello, World!"); | |
| 118 std::string sha256 = crypto::SHA256HashString(data); | |
| 119 // Sign sha256 of the input data. | |
| 120 std::vector<uint8> signature; | |
| 121 ASSERT_TRUE(crypto::SignatureCreator::SignUsingSpecifiedHash( | |
| 122 key.get(), | |
| 123 crypto::SignatureCreator::HashAlgorithm::SHA256, | |
| 124 reinterpret_cast<const uint8*>(sha256.c_str()), | |
| 125 sha256.size(), | |
| 126 &signature)); | |
| 127 | |
| 128 std::vector<uint8> public_key_info; | |
| 129 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); | |
| 130 | |
| 131 // Verify the input data. | |
| 132 crypto::SignatureVerifier verifier; | |
| 133 ASSERT_TRUE(verifier.VerifyInit( | |
| 134 kSHA256WithRSAAlgorithmID, sizeof(kSHA256WithRSAAlgorithmID), | |
| 135 &signature.front(), signature.size(), | |
| 136 &public_key_info.front(), public_key_info.size())); | |
| 137 | |
| 138 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), | |
| 139 data.size()); | |
| 140 ASSERT_TRUE(verifier.VerifyFinal()); | |
| 141 } | |
| OLD | NEW |