Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / crypto / signature_creator_unittest.cc
blob694becdcb4475effc0e4c35dceebe0c6db2a14d5
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
3 // found in the LICENSE file.
5 #include <vector>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/sha1.h"
9 #include "crypto/rsa_private_key.h"
10 #include "crypto/sha2.h"
11 #include "crypto/signature_creator.h"
12 #include "crypto/signature_verifier.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace {
17 // This is the algorithm ID for SHA-1 with RSA encryption.
18 const uint8 kSHA1WithRSAAlgorithmID[] = {
19 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
20 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00
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
31 TEST(SignatureCreatorTest, BasicTest) {
32 // Do a verify round trip.
33 scoped_ptr<crypto::RSAPrivateKey> key_original(
34 crypto::RSAPrivateKey::Create(1024));
35 ASSERT_TRUE(key_original.get());
37 std::vector<uint8> key_info;
38 key_original->ExportPrivateKey(&key_info);
39 scoped_ptr<crypto::RSAPrivateKey> key(
40 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
41 ASSERT_TRUE(key.get());
43 scoped_ptr<crypto::SignatureCreator> signer(
44 crypto::SignatureCreator::Create(key.get(),
45 crypto::SignatureCreator::SHA1));
46 ASSERT_TRUE(signer.get());
48 std::string data("Hello, World!");
49 ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8*>(data.c_str()),
50 data.size()));
52 std::vector<uint8> signature;
53 ASSERT_TRUE(signer->Final(&signature));
55 std::vector<uint8> public_key_info;
56 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
58 crypto::SignatureVerifier verifier;
59 ASSERT_TRUE(verifier.VerifyInit(
60 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID),
61 &signature.front(), signature.size(),
62 &public_key_info.front(), public_key_info.size()));
64 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()),
65 data.size());
66 ASSERT_TRUE(verifier.VerifyFinal());
69 TEST(SignatureCreatorTest, SignDigestTest) {
70 // Do a verify round trip.
71 scoped_ptr<crypto::RSAPrivateKey> key_original(
72 crypto::RSAPrivateKey::Create(1024));
73 ASSERT_TRUE(key_original.get());
75 std::vector<uint8> key_info;
76 key_original->ExportPrivateKey(&key_info);
77 scoped_ptr<crypto::RSAPrivateKey> key(
78 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
79 ASSERT_TRUE(key.get());
81 std::string data("Hello, World!");
82 std::string sha1 = base::SHA1HashString(data);
83 // Sign sha1 of the input data.
84 std::vector<uint8> signature;
85 ASSERT_TRUE(crypto::SignatureCreator::Sign(
86 key.get(),
87 crypto::SignatureCreator::SHA1,
88 reinterpret_cast<const uint8*>(sha1.c_str()),
89 sha1.size(),
90 &signature));
92 std::vector<uint8> public_key_info;
93 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
95 // Verify the input data.
96 crypto::SignatureVerifier verifier;
97 ASSERT_TRUE(verifier.VerifyInit(
98 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID),
99 &signature.front(), signature.size(),
100 &public_key_info.front(), public_key_info.size()));
102 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()),
103 data.size());
104 ASSERT_TRUE(verifier.VerifyFinal());
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());
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());
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));
130 std::vector<uint8> public_key_info;
131 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
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()));
140 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()),
141 data.size());
142 ASSERT_TRUE(verifier.VerifyFinal());