Roll blink 153402:153409
[chromium-blink-merge.git] / crypto / ec_signature_creator_unittest.cc
blobb34022b1861ec89e39b98870a600652af8ded17b
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.
5 #include "crypto/ec_signature_creator.h"
7 #include <string>
8 #include <vector>
10 #include "base/memory/scoped_ptr.h"
11 #include "crypto/ec_private_key.h"
12 #include "crypto/signature_verifier.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 #if defined(USE_OPENSSL)
16 // Once ECSignatureCreator is implemented for OpenSSL, remove this #if block.
17 // TODO(rch): When that happens, also add some exported keys from each to
18 // test interop between NSS and OpenSSL.
19 TEST(ECSignatureCreatorTest, OpenSSLStub) {
20 scoped_ptr<crypto::ECSignatureCreator> signer(
21 crypto::ECSignatureCreator::Create(NULL));
22 ASSERT_TRUE(signer.get());
23 EXPECT_FALSE(signer->Sign(NULL, 0, NULL));
25 #else
26 TEST(ECSignatureCreatorTest, BasicTest) {
27 // Do a verify round trip.
28 scoped_ptr<crypto::ECPrivateKey> key_original(
29 crypto::ECPrivateKey::Create());
30 ASSERT_TRUE(key_original.get());
32 std::vector<uint8> key_info;
33 ASSERT_TRUE(
34 key_original->ExportEncryptedPrivateKey(std::string(), 1000, &key_info));
35 std::vector<uint8> pubkey_info;
36 ASSERT_TRUE(key_original->ExportPublicKey(&pubkey_info));
38 scoped_ptr<crypto::ECPrivateKey> key(
39 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
40 std::string(), key_info, pubkey_info));
41 ASSERT_TRUE(key.get());
42 ASSERT_TRUE(key->key() != NULL);
44 scoped_ptr<crypto::ECSignatureCreator> signer(
45 crypto::ECSignatureCreator::Create(key.get()));
46 ASSERT_TRUE(signer.get());
48 std::string data("Hello, World!");
49 std::vector<uint8> signature;
50 ASSERT_TRUE(signer->Sign(reinterpret_cast<const uint8*>(data.c_str()),
51 data.size(),
52 &signature));
54 std::vector<uint8> public_key_info;
55 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
57 // This is the algorithm ID for SHA-256 with EC encryption.
58 const uint8 kECDSAWithSHA256AlgorithmID[] = {
59 0x30, 0x0c,
60 0x06, 0x08,
61 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02,
62 0x05, 0x00
64 crypto::SignatureVerifier verifier;
65 ASSERT_TRUE(verifier.VerifyInit(
66 kECDSAWithSHA256AlgorithmID, sizeof(kECDSAWithSHA256AlgorithmID),
67 &signature[0], signature.size(),
68 &public_key_info.front(), public_key_info.size()));
70 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()),
71 data.size());
72 ASSERT_TRUE(verifier.VerifyFinal());
74 #endif // !defined(USE_OPENSSL)