[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 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/ec_signature_creator.h" | ||||
6 | |||||
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 7 | #include <stdint.h> |
8 | |||||
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 9 | #include <memory> |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 10 | #include <string> |
11 | #include <vector> | ||||
12 | |||||
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 13 | #include "crypto/ec_private_key.h" |
14 | #include "crypto/signature_verifier.h" | ||||
15 | #include "testing/gtest/include/gtest/gtest.h" | ||||
16 | |||||
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 17 | // TODO(rch): Add some exported keys from each to |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 18 | // test interop between NSS and OpenSSL. |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 19 | |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 20 | TEST(ECSignatureCreatorTest, BasicTest) { |
21 | // Do a verify round trip. | ||||
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 22 | std::unique_ptr<crypto::ECPrivateKey> key_original( |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 23 | crypto::ECPrivateKey::Create()); |
davidben | 1c02c94c | 2017-01-04 13:54:32 | [diff] [blame] | 24 | ASSERT_TRUE(key_original); |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 25 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 26 | std::vector<uint8_t> key_info; |
davidben | 1c02c94c | 2017-01-04 13:54:32 | [diff] [blame] | 27 | ASSERT_TRUE(key_original->ExportPrivateKey(&key_info)); |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 28 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 29 | std::unique_ptr<crypto::ECPrivateKey> key( |
davidben | 1c02c94c | 2017-01-04 13:54:32 | [diff] [blame] | 30 | crypto::ECPrivateKey::CreateFromPrivateKeyInfo(key_info)); |
31 | ASSERT_TRUE(key); | ||||
32 | ASSERT_TRUE(key->key()); | ||||
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 33 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 34 | std::unique_ptr<crypto::ECSignatureCreator> signer( |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 35 | crypto::ECSignatureCreator::Create(key.get())); |
davidben | 1c02c94c | 2017-01-04 13:54:32 | [diff] [blame] | 36 | ASSERT_TRUE(signer); |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 37 | |
38 | std::string data("Hello, World!"); | ||||
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 39 | std::vector<uint8_t> signature; |
40 | ASSERT_TRUE(signer->Sign(reinterpret_cast<const uint8_t*>(data.c_str()), | ||||
41 | data.size(), &signature)); | ||||
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 42 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 43 | std::vector<uint8_t> public_key_info; |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 44 | ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
45 | |||||
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 46 | crypto::SignatureVerifier verifier; |
47 | ASSERT_TRUE(verifier.VerifyInit( | ||||
davidben | 9c97a36 | 2016-03-03 16:18:26 | [diff] [blame] | 48 | crypto::SignatureVerifier::ECDSA_SHA256, &signature[0], signature.size(), |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 49 | &public_key_info.front(), public_key_info.size())); |
50 | |||||
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 51 | verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()), |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 52 | data.size()); |
53 | ASSERT_TRUE(verifier.VerifyFinal()); | ||||
54 | } |