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