ec_signature_creator_unittest.cc 1.71 KB
Newer Older
1 2 3 4 5 6
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "crypto/ec_signature_creator.h"

7 8
#include <stdint.h>

thakis's avatar
thakis committed
9
#include <memory>
10 11 12 13 14 15 16
#include <string>
#include <vector>

#include "crypto/ec_private_key.h"
#include "crypto/signature_verifier.h"
#include "testing/gtest/include/gtest/gtest.h"

17
// TODO(rch): Add some exported keys from each to
18
// test interop between NSS and OpenSSL.
19

20 21
TEST(ECSignatureCreatorTest, BasicTest) {
  // Do a verify round trip.
thakis's avatar
thakis committed
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

thakis's avatar
thakis committed
29
  std::unique_ptr<crypto::ECPrivateKey> key(
30 31 32
      crypto::ECPrivateKey::CreateFromPrivateKeyInfo(key_info));
  ASSERT_TRUE(key);
  ASSERT_TRUE(key->key());
33

thakis's avatar
thakis committed
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 40 41
  std::vector<uint8_t> signature;
  ASSERT_TRUE(signer->Sign(reinterpret_cast<const uint8_t*>(data.c_str()),
                           data.size(), &signature));
42

43
  std::vector<uint8_t> public_key_info;
44 45 46 47
  ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));

  crypto::SignatureVerifier verifier;
  ASSERT_TRUE(verifier.VerifyInit(
48
      crypto::SignatureVerifier::ECDSA_SHA256, &signature[0], signature.size(),
49 50
      &public_key_info.front(), public_key_info.size()));

51
  verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()),
52 53 54
                        data.size());
  ASSERT_TRUE(verifier.VerifyFinal());
}