blob: 2f135cc7091215cdddf841a91345eef7c4d44194 [file] [log] [blame]
[email protected]5ee44d42012-02-08 00:14:541// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]28ae8fe2009-06-05 18:25:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
thakisd1a18472016-04-08 22:30:415#include "crypto/signature_creator.h"
6
avidd373b8b2015-12-21 21:34:437#include <stdint.h>
8
thakisd1a18472016-04-08 22:30:419#include <memory>
davidben6004dc52017-02-03 04:15:2910#include <string>
[email protected]28ae8fe2009-06-05 18:25:0611#include <vector>
12
[email protected]ed31834b2013-07-09 08:32:4013#include "base/sha1.h"
[email protected]5ee44d42012-02-08 00:14:5414#include "crypto/rsa_private_key.h"
dougsteed0cf460ec2014-09-19 18:46:0915#include "crypto/sha2.h"
[email protected]4b559b4d2011-04-14 17:37:1416#include "crypto/signature_verifier.h"
[email protected]28ae8fe2009-06-05 18:25:0617#include "testing/gtest/include/gtest/gtest.h"
18
19TEST(SignatureCreatorTest, BasicTest) {
20 // Do a verify round trip.
thakisd1a18472016-04-08 22:30:4121 std::unique_ptr<crypto::RSAPrivateKey> key_original(
[email protected]4b559b4d2011-04-14 17:37:1422 crypto::RSAPrivateKey::Create(1024));
[email protected]7eee7902009-06-06 06:06:3023 ASSERT_TRUE(key_original.get());
24
avidd373b8b2015-12-21 21:34:4325 std::vector<uint8_t> key_info;
[email protected]7eee7902009-06-06 06:06:3026 key_original->ExportPrivateKey(&key_info);
thakisd1a18472016-04-08 22:30:4127 std::unique_ptr<crypto::RSAPrivateKey> key(
[email protected]4b559b4d2011-04-14 17:37:1428 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
[email protected]308379a52009-10-07 02:46:3129 ASSERT_TRUE(key.get());
[email protected]28ae8fe2009-06-05 18:25:0630
thakisd1a18472016-04-08 22:30:4131 std::unique_ptr<crypto::SignatureCreator> signer(
dougsteed0cf460ec2014-09-19 18:46:0932 crypto::SignatureCreator::Create(key.get(),
33 crypto::SignatureCreator::SHA1));
[email protected]28ae8fe2009-06-05 18:25:0634 ASSERT_TRUE(signer.get());
35
[email protected]7eee7902009-06-06 06:06:3036 std::string data("Hello, World!");
avidd373b8b2015-12-21 21:34:4337 ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8_t*>(data.c_str()),
[email protected]28ae8fe2009-06-05 18:25:0638 data.size()));
39
avidd373b8b2015-12-21 21:34:4340 std::vector<uint8_t> signature;
[email protected]28ae8fe2009-06-05 18:25:0641 ASSERT_TRUE(signer->Final(&signature));
42
avidd373b8b2015-12-21 21:34:4343 std::vector<uint8_t> public_key_info;
[email protected]7eee7902009-06-06 06:06:3044 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
[email protected]28ae8fe2009-06-05 18:25:0645
[email protected]ed31834b2013-07-09 08:32:4046 crypto::SignatureVerifier verifier;
47 ASSERT_TRUE(verifier.VerifyInit(
davidben9c97a362016-03-03 16:18:2648 crypto::SignatureVerifier::RSA_PKCS1_SHA1, &signature.front(),
49 signature.size(), &public_key_info.front(), public_key_info.size()));
[email protected]ed31834b2013-07-09 08:32:4050
avidd373b8b2015-12-21 21:34:4351 verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()),
[email protected]ed31834b2013-07-09 08:32:4052 data.size());
53 ASSERT_TRUE(verifier.VerifyFinal());
54}
55
56TEST(SignatureCreatorTest, SignDigestTest) {
57 // Do a verify round trip.
thakisd1a18472016-04-08 22:30:4158 std::unique_ptr<crypto::RSAPrivateKey> key_original(
[email protected]ed31834b2013-07-09 08:32:4059 crypto::RSAPrivateKey::Create(1024));
60 ASSERT_TRUE(key_original.get());
61
avidd373b8b2015-12-21 21:34:4362 std::vector<uint8_t> key_info;
[email protected]ed31834b2013-07-09 08:32:4063 key_original->ExportPrivateKey(&key_info);
thakisd1a18472016-04-08 22:30:4164 std::unique_ptr<crypto::RSAPrivateKey> key(
[email protected]ed31834b2013-07-09 08:32:4065 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
66 ASSERT_TRUE(key.get());
67
68 std::string data("Hello, World!");
69 std::string sha1 = base::SHA1HashString(data);
70 // Sign sha1 of the input data.
avidd373b8b2015-12-21 21:34:4371 std::vector<uint8_t> signature;
[email protected]ed31834b2013-07-09 08:32:4072 ASSERT_TRUE(crypto::SignatureCreator::Sign(
avidd373b8b2015-12-21 21:34:4373 key.get(), crypto::SignatureCreator::SHA1,
74 reinterpret_cast<const uint8_t*>(sha1.c_str()), sha1.size(), &signature));
[email protected]ed31834b2013-07-09 08:32:4075
avidd373b8b2015-12-21 21:34:4376 std::vector<uint8_t> public_key_info;
[email protected]ed31834b2013-07-09 08:32:4077 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
78
79 // Verify the input data.
[email protected]4b559b4d2011-04-14 17:37:1480 crypto::SignatureVerifier verifier;
[email protected]28ae8fe2009-06-05 18:25:0681 ASSERT_TRUE(verifier.VerifyInit(
davidben9c97a362016-03-03 16:18:2682 crypto::SignatureVerifier::RSA_PKCS1_SHA1, &signature.front(),
83 signature.size(), &public_key_info.front(), public_key_info.size()));
[email protected]28ae8fe2009-06-05 18:25:0684
avidd373b8b2015-12-21 21:34:4385 verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()),
[email protected]7eee7902009-06-06 06:06:3086 data.size());
87 ASSERT_TRUE(verifier.VerifyFinal());
88}
dougsteed0cf460ec2014-09-19 18:46:0989
90TEST(SignatureCreatorTest, SignSHA256DigestTest) {
91 // Do a verify round trip.
thakisd1a18472016-04-08 22:30:4192 std::unique_ptr<crypto::RSAPrivateKey> key_original(
dougsteed0cf460ec2014-09-19 18:46:0993 crypto::RSAPrivateKey::Create(1024));
94 ASSERT_TRUE(key_original.get());
95
avidd373b8b2015-12-21 21:34:4396 std::vector<uint8_t> key_info;
dougsteed0cf460ec2014-09-19 18:46:0997 key_original->ExportPrivateKey(&key_info);
thakisd1a18472016-04-08 22:30:4198 std::unique_ptr<crypto::RSAPrivateKey> key(
dougsteed0cf460ec2014-09-19 18:46:0999 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
100 ASSERT_TRUE(key.get());
101
102 std::string data("Hello, World!");
103 std::string sha256 = crypto::SHA256HashString(data);
104 // Sign sha256 of the input data.
avidd373b8b2015-12-21 21:34:43105 std::vector<uint8_t> signature;
dougsteed0cf460ec2014-09-19 18:46:09106 ASSERT_TRUE(crypto::SignatureCreator::Sign(
avidd373b8b2015-12-21 21:34:43107 key.get(), crypto::SignatureCreator::HashAlgorithm::SHA256,
108 reinterpret_cast<const uint8_t*>(sha256.c_str()), sha256.size(),
dougsteed0cf460ec2014-09-19 18:46:09109 &signature));
110
avidd373b8b2015-12-21 21:34:43111 std::vector<uint8_t> public_key_info;
dougsteed0cf460ec2014-09-19 18:46:09112 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
113
114 // Verify the input data.
115 crypto::SignatureVerifier verifier;
116 ASSERT_TRUE(verifier.VerifyInit(
davidben9c97a362016-03-03 16:18:26117 crypto::SignatureVerifier::RSA_PKCS1_SHA256, &signature.front(),
118 signature.size(), &public_key_info.front(), public_key_info.size()));
dougsteed0cf460ec2014-09-19 18:46:09119
avidd373b8b2015-12-21 21:34:43120 verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()),
dougsteed0cf460ec2014-09-19 18:46:09121 data.size());
122 ASSERT_TRUE(verifier.VerifyFinal());
123}