blob: 1bfd89d0a764d13b8dd576f9b84162a3ecd6590d [file] [log] [blame]
[email protected]e4c18472012-01-25 00:56:431// 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
avidd373b8b2015-12-21 21:34:437#include <stdint.h>
8
thakisd1a18472016-04-08 22:30:419#include <memory>
[email protected]e4c18472012-01-25 00:56:4310#include <string>
11#include <vector>
12
[email protected]e4c18472012-01-25 00:56:4313#include "crypto/ec_private_key.h"
14#include "crypto/signature_verifier.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]012c2872013-10-25 17:26:0817// TODO(rch): Add some exported keys from each to
[email protected]e4c18472012-01-25 00:56:4318// test interop between NSS and OpenSSL.
[email protected]012c2872013-10-25 17:26:0819
[email protected]e4c18472012-01-25 00:56:4320TEST(ECSignatureCreatorTest, BasicTest) {
21 // Do a verify round trip.
thakisd1a18472016-04-08 22:30:4122 std::unique_ptr<crypto::ECPrivateKey> key_original(
[email protected]e4c18472012-01-25 00:56:4323 crypto::ECPrivateKey::Create());
davidben1c02c94c2017-01-04 13:54:3224 ASSERT_TRUE(key_original);
[email protected]e4c18472012-01-25 00:56:4325
avidd373b8b2015-12-21 21:34:4326 std::vector<uint8_t> key_info;
davidben1c02c94c2017-01-04 13:54:3227 ASSERT_TRUE(key_original->ExportPrivateKey(&key_info));
[email protected]e4c18472012-01-25 00:56:4328
thakisd1a18472016-04-08 22:30:4129 std::unique_ptr<crypto::ECPrivateKey> key(
davidben1c02c94c2017-01-04 13:54:3230 crypto::ECPrivateKey::CreateFromPrivateKeyInfo(key_info));
31 ASSERT_TRUE(key);
32 ASSERT_TRUE(key->key());
[email protected]e4c18472012-01-25 00:56:4333
thakisd1a18472016-04-08 22:30:4134 std::unique_ptr<crypto::ECSignatureCreator> signer(
[email protected]e4c18472012-01-25 00:56:4335 crypto::ECSignatureCreator::Create(key.get()));
davidben1c02c94c2017-01-04 13:54:3236 ASSERT_TRUE(signer);
[email protected]e4c18472012-01-25 00:56:4337
38 std::string data("Hello, World!");
avidd373b8b2015-12-21 21:34:4339 std::vector<uint8_t> signature;
40 ASSERT_TRUE(signer->Sign(reinterpret_cast<const uint8_t*>(data.c_str()),
41 data.size(), &signature));
[email protected]e4c18472012-01-25 00:56:4342
avidd373b8b2015-12-21 21:34:4343 std::vector<uint8_t> public_key_info;
[email protected]e4c18472012-01-25 00:56:4344 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
45
[email protected]e4c18472012-01-25 00:56:4346 crypto::SignatureVerifier verifier;
47 ASSERT_TRUE(verifier.VerifyInit(
davidben9c97a362016-03-03 16:18:2648 crypto::SignatureVerifier::ECDSA_SHA256, &signature[0], signature.size(),
[email protected]e4c18472012-01-25 00:56:4349 &public_key_info.front(), public_key_info.size()));
50
avidd373b8b2015-12-21 21:34:4351 verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()),
[email protected]e4c18472012-01-25 00:56:4352 data.size());
53 ASSERT_TRUE(verifier.VerifyFinal());
54}