ec_signature_creator_unittest.cc 1.6 KB

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