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 "crypto/rsa_private_key.h" | 9 #include "crypto/rsa_private_key.h" |
| 9 #include "crypto/signature_creator.h" | 10 #include "crypto/signature_creator.h" |
| 10 #include "crypto/signature_verifier.h" | 11 #include "crypto/signature_verifier.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 14 namespace { | |
| 15 | |
| 16 // This is the algorithm ID for SHA-1 with RSA encryption. | |
| 17 // TODO(aa): Factor this out into some shared location. | |
|
Ryan Sleevi
2013/07/08 21:56:20
comment nit: remove the TODO. Ain't gonna happen a
pfeldman
2013/07/09 05:21:07
Done.
| |
| 18 const uint8 kSHA1WithRSAAlgorithmID[] = { | |
| 19 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, | |
| 20 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00 | |
| 21 }; | |
| 22 | |
| 23 } | |
| 24 | |
| 13 TEST(SignatureCreatorTest, BasicTest) { | 25 TEST(SignatureCreatorTest, BasicTest) { |
| 14 // Do a verify round trip. | 26 // Do a verify round trip. |
| 15 scoped_ptr<crypto::RSAPrivateKey> key_original( | 27 scoped_ptr<crypto::RSAPrivateKey> key_original( |
| 16 crypto::RSAPrivateKey::Create(1024)); | 28 crypto::RSAPrivateKey::Create(1024)); |
| 17 ASSERT_TRUE(key_original.get()); | 29 ASSERT_TRUE(key_original.get()); |
| 18 | 30 |
| 19 std::vector<uint8> key_info; | 31 std::vector<uint8> key_info; |
| 20 key_original->ExportPrivateKey(&key_info); | 32 key_original->ExportPrivateKey(&key_info); |
| 21 scoped_ptr<crypto::RSAPrivateKey> key( | 33 scoped_ptr<crypto::RSAPrivateKey> key( |
| 22 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); | 34 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
| 23 ASSERT_TRUE(key.get()); | 35 ASSERT_TRUE(key.get()); |
| 24 | 36 |
| 25 scoped_ptr<crypto::SignatureCreator> signer( | 37 scoped_ptr<crypto::SignatureCreator> signer( |
| 26 crypto::SignatureCreator::Create(key.get())); | 38 crypto::SignatureCreator::Create(key.get())); |
| 27 ASSERT_TRUE(signer.get()); | 39 ASSERT_TRUE(signer.get()); |
| 28 | 40 |
| 29 std::string data("Hello, World!"); | 41 std::string data("Hello, World!"); |
| 30 ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8*>(data.c_str()), | 42 ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8*>(data.c_str()), |
| 31 data.size())); | 43 data.size())); |
| 32 | 44 |
| 33 std::vector<uint8> signature; | 45 std::vector<uint8> signature; |
| 34 ASSERT_TRUE(signer->Final(&signature)); | 46 ASSERT_TRUE(signer->Final(&signature)); |
| 35 | 47 |
| 36 std::vector<uint8> public_key_info; | 48 std::vector<uint8> public_key_info; |
| 37 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); | 49 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
| 38 | 50 |
| 39 // This is the algorithm ID for SHA-1 with RSA encryption. | |
| 40 // TODO(aa): Factor this out into some shared location. | |
| 41 const uint8 kSHA1WithRSAAlgorithmID[] = { | |
| 42 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, | |
| 43 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00 | |
| 44 }; | |
| 45 crypto::SignatureVerifier verifier; | 51 crypto::SignatureVerifier verifier; |
| 46 ASSERT_TRUE(verifier.VerifyInit( | 52 ASSERT_TRUE(verifier.VerifyInit( |
| 47 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), | 53 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), |
| 54 &signature.front(), signature.size(), | |
| 55 &public_key_info.front(), public_key_info.size())); | |
| 56 | |
| 57 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), | |
| 58 data.size()); | |
| 59 ASSERT_TRUE(verifier.VerifyFinal()); | |
| 60 } | |
| 61 | |
| 62 TEST(SignatureCreatorTest, SignDigestTest) { | |
| 63 // Do a verify round trip. | |
| 64 scoped_ptr<crypto::RSAPrivateKey> key_original( | |
| 65 crypto::RSAPrivateKey::Create(1024)); | |
| 66 ASSERT_TRUE(key_original.get()); | |
| 67 | |
| 68 std::vector<uint8> key_info; | |
| 69 key_original->ExportPrivateKey(&key_info); | |
| 70 scoped_ptr<crypto::RSAPrivateKey> key( | |
| 71 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); | |
| 72 ASSERT_TRUE(key.get()); | |
| 73 | |
| 74 std::string data("Hello, World!"); | |
| 75 std::string sha1 = base::SHA1HashString(data); | |
| 76 // Sign sha1 of the input data. | |
| 77 std::vector<uint8> signature; | |
| 78 ASSERT_TRUE(crypto::SignatureCreator::Sign( | |
| 79 key.get(), | |
| 80 reinterpret_cast<const uint8*>(sha1.c_str()), | |
| 81 sha1.size(), | |
| 82 &signature)); | |
|
Ryan Sleevi
2013/07/08 21:57:25
style nit: I think it's only four spaces indent he
pfeldman
2013/07/09 05:21:07
Done.
| |
| 83 | |
| 84 std::vector<uint8> public_key_info; | |
| 85 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); | |
| 86 | |
| 87 // Verify the input data. | |
| 88 crypto::SignatureVerifier verifier; | |
| 89 ASSERT_TRUE(verifier.VerifyInit( | |
| 90 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), | |
| 48 &signature.front(), signature.size(), | 91 &signature.front(), signature.size(), |
| 49 &public_key_info.front(), public_key_info.size())); | 92 &public_key_info.front(), public_key_info.size())); |
| 50 | 93 |
| 51 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), | 94 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), |
| 52 data.size()); | 95 data.size()); |
| 53 ASSERT_TRUE(verifier.VerifyFinal()); | 96 ASSERT_TRUE(verifier.VerifyFinal()); |
| 54 } | 97 } |
| OLD | NEW |