Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: crypto/signature_creator_unittest.cc

Issue 18697003: Introduce RSAPrivateKey::SignDigest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: For landing Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « crypto/signature_creator_openssl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 const uint8 kSHA1WithRSAAlgorithmID[] = {
18 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
19 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00
20 };
21
22 }
23
13 TEST(SignatureCreatorTest, BasicTest) { 24 TEST(SignatureCreatorTest, BasicTest) {
14 // Do a verify round trip. 25 // Do a verify round trip.
15 scoped_ptr<crypto::RSAPrivateKey> key_original( 26 scoped_ptr<crypto::RSAPrivateKey> key_original(
16 crypto::RSAPrivateKey::Create(1024)); 27 crypto::RSAPrivateKey::Create(1024));
17 ASSERT_TRUE(key_original.get()); 28 ASSERT_TRUE(key_original.get());
18 29
19 std::vector<uint8> key_info; 30 std::vector<uint8> key_info;
20 key_original->ExportPrivateKey(&key_info); 31 key_original->ExportPrivateKey(&key_info);
21 scoped_ptr<crypto::RSAPrivateKey> key( 32 scoped_ptr<crypto::RSAPrivateKey> key(
22 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info)); 33 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
23 ASSERT_TRUE(key.get()); 34 ASSERT_TRUE(key.get());
24 35
25 scoped_ptr<crypto::SignatureCreator> signer( 36 scoped_ptr<crypto::SignatureCreator> signer(
26 crypto::SignatureCreator::Create(key.get())); 37 crypto::SignatureCreator::Create(key.get()));
27 ASSERT_TRUE(signer.get()); 38 ASSERT_TRUE(signer.get());
28 39
29 std::string data("Hello, World!"); 40 std::string data("Hello, World!");
30 ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8*>(data.c_str()), 41 ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8*>(data.c_str()),
31 data.size())); 42 data.size()));
32 43
33 std::vector<uint8> signature; 44 std::vector<uint8> signature;
34 ASSERT_TRUE(signer->Final(&signature)); 45 ASSERT_TRUE(signer->Final(&signature));
35 46
36 std::vector<uint8> public_key_info; 47 std::vector<uint8> public_key_info;
37 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); 48 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
38 49
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; 50 crypto::SignatureVerifier verifier;
46 ASSERT_TRUE(verifier.VerifyInit( 51 ASSERT_TRUE(verifier.VerifyInit(
47 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID), 52 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID),
53 &signature.front(), signature.size(),
54 &public_key_info.front(), public_key_info.size()));
55
56 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()),
57 data.size());
58 ASSERT_TRUE(verifier.VerifyFinal());
59 }
60
61 TEST(SignatureCreatorTest, SignDigestTest) {
62 // Do a verify round trip.
63 scoped_ptr<crypto::RSAPrivateKey> key_original(
64 crypto::RSAPrivateKey::Create(1024));
65 ASSERT_TRUE(key_original.get());
66
67 std::vector<uint8> key_info;
68 key_original->ExportPrivateKey(&key_info);
69 scoped_ptr<crypto::RSAPrivateKey> key(
70 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
71 ASSERT_TRUE(key.get());
72
73 std::string data("Hello, World!");
74 std::string sha1 = base::SHA1HashString(data);
75 // Sign sha1 of the input data.
76 std::vector<uint8> signature;
77 ASSERT_TRUE(crypto::SignatureCreator::Sign(
78 key.get(),
79 reinterpret_cast<const uint8*>(sha1.c_str()),
80 sha1.size(),
81 &signature));
82
83 std::vector<uint8> public_key_info;
84 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
85
86 // Verify the input data.
87 crypto::SignatureVerifier verifier;
88 ASSERT_TRUE(verifier.VerifyInit(
89 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID),
48 &signature.front(), signature.size(), 90 &signature.front(), signature.size(),
49 &public_key_info.front(), public_key_info.size())); 91 &public_key_info.front(), public_key_info.size()));
50 92
51 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), 93 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()),
52 data.size()); 94 data.size());
53 ASSERT_TRUE(verifier.VerifyFinal()); 95 ASSERT_TRUE(verifier.VerifyFinal());
54 } 96 }
OLDNEW
« no previous file with comments | « crypto/signature_creator_openssl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698