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/ec_signature_creator.h" 6 7 #include <stdint.h> 8 9 #include <memory> 10 #include <string> 11 #include <vector> 12 13 #include "crypto/ec_private_key.h" 14 #include "crypto/signature_verifier.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 // TODO(rch): Add some exported keys from each to 18 // test interop between NSS and OpenSSL. 19 TEST(ECSignatureCreatorTest,BasicTest)20TEST(ECSignatureCreatorTest, BasicTest) { 21 // Do a verify round trip. 22 std::unique_ptr<crypto::ECPrivateKey> key_original( 23 crypto::ECPrivateKey::Create()); 24 ASSERT_TRUE(key_original); 25 26 std::vector<uint8_t> key_info; 27 ASSERT_TRUE(key_original->ExportPrivateKey(&key_info)); 28 29 std::unique_ptr<crypto::ECPrivateKey> key( 30 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(key_info)); 31 ASSERT_TRUE(key); 32 ASSERT_TRUE(key->key()); 33 34 std::unique_ptr<crypto::ECSignatureCreator> signer( 35 crypto::ECSignatureCreator::Create(key.get())); 36 ASSERT_TRUE(signer); 37 38 std::string data("Hello, World!"); 39 std::vector<uint8_t> signature; 40 ASSERT_TRUE(signer->Sign(base::as_bytes(base::make_span(data)), &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(crypto::SignatureVerifier::ECDSA_SHA256, 47 signature, public_key_info)); 48 49 verifier.VerifyUpdate(base::as_bytes(base::make_span(data))); 50 ASSERT_TRUE(verifier.VerifyFinal()); 51 } 52