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