• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "crypto/signature_creator.h"
6 
7 #include <stdint.h>
8 
9 #include <memory>
10 #include <vector>
11 
12 #include "base/sha1.h"
13 #include "crypto/rsa_private_key.h"
14 #include "crypto/sha2.h"
15 #include "crypto/signature_verifier.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 
TEST(SignatureCreatorTest,BasicTest)18 TEST(SignatureCreatorTest, BasicTest) {
19   // Do a verify round trip.
20   std::unique_ptr<crypto::RSAPrivateKey> key_original(
21       crypto::RSAPrivateKey::Create(1024));
22   ASSERT_TRUE(key_original.get());
23 
24   std::vector<uint8_t> key_info;
25   key_original->ExportPrivateKey(&key_info);
26   std::unique_ptr<crypto::RSAPrivateKey> key(
27       crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
28   ASSERT_TRUE(key.get());
29 
30   std::unique_ptr<crypto::SignatureCreator> signer(
31       crypto::SignatureCreator::Create(key.get(),
32                                        crypto::SignatureCreator::SHA1));
33   ASSERT_TRUE(signer.get());
34 
35   std::string data("Hello, World!");
36   ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8_t*>(data.c_str()),
37                              data.size()));
38 
39   std::vector<uint8_t> signature;
40   ASSERT_TRUE(signer->Final(&signature));
41 
42   std::vector<uint8_t> public_key_info;
43   ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
44 
45   crypto::SignatureVerifier verifier;
46   ASSERT_TRUE(verifier.VerifyInit(
47       crypto::SignatureVerifier::RSA_PKCS1_SHA1, &signature.front(),
48       signature.size(), &public_key_info.front(), public_key_info.size()));
49 
50   verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()),
51                         data.size());
52   ASSERT_TRUE(verifier.VerifyFinal());
53 }
54 
TEST(SignatureCreatorTest,SignDigestTest)55 TEST(SignatureCreatorTest, SignDigestTest) {
56   // Do a verify round trip.
57   std::unique_ptr<crypto::RSAPrivateKey> key_original(
58       crypto::RSAPrivateKey::Create(1024));
59   ASSERT_TRUE(key_original.get());
60 
61   std::vector<uint8_t> key_info;
62   key_original->ExportPrivateKey(&key_info);
63   std::unique_ptr<crypto::RSAPrivateKey> key(
64       crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
65   ASSERT_TRUE(key.get());
66 
67   std::string data("Hello, World!");
68   std::string sha1 = base::SHA1HashString(data);
69   // Sign sha1 of the input data.
70   std::vector<uint8_t> signature;
71   ASSERT_TRUE(crypto::SignatureCreator::Sign(
72       key.get(), crypto::SignatureCreator::SHA1,
73       reinterpret_cast<const uint8_t*>(sha1.c_str()), sha1.size(), &signature));
74 
75   std::vector<uint8_t> public_key_info;
76   ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
77 
78   // Verify the input data.
79   crypto::SignatureVerifier verifier;
80   ASSERT_TRUE(verifier.VerifyInit(
81       crypto::SignatureVerifier::RSA_PKCS1_SHA1, &signature.front(),
82       signature.size(), &public_key_info.front(), public_key_info.size()));
83 
84   verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()),
85                         data.size());
86   ASSERT_TRUE(verifier.VerifyFinal());
87 }
88 
TEST(SignatureCreatorTest,SignSHA256DigestTest)89 TEST(SignatureCreatorTest, SignSHA256DigestTest) {
90   // Do a verify round trip.
91   std::unique_ptr<crypto::RSAPrivateKey> key_original(
92       crypto::RSAPrivateKey::Create(1024));
93   ASSERT_TRUE(key_original.get());
94 
95   std::vector<uint8_t> key_info;
96   key_original->ExportPrivateKey(&key_info);
97   std::unique_ptr<crypto::RSAPrivateKey> key(
98       crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
99   ASSERT_TRUE(key.get());
100 
101   std::string data("Hello, World!");
102   std::string sha256 = crypto::SHA256HashString(data);
103   // Sign sha256 of the input data.
104   std::vector<uint8_t> signature;
105   ASSERT_TRUE(crypto::SignatureCreator::Sign(
106       key.get(), crypto::SignatureCreator::HashAlgorithm::SHA256,
107       reinterpret_cast<const uint8_t*>(sha256.c_str()), sha256.size(),
108       &signature));
109 
110   std::vector<uint8_t> public_key_info;
111   ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
112 
113   // Verify the input data.
114   crypto::SignatureVerifier verifier;
115   ASSERT_TRUE(verifier.VerifyInit(
116       crypto::SignatureVerifier::RSA_PKCS1_SHA256, &signature.front(),
117       signature.size(), &public_key_info.front(), public_key_info.size()));
118 
119   verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()),
120                         data.size());
121   ASSERT_TRUE(verifier.VerifyFinal());
122 }
123