[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame^] | 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 | |
| 5 | #include "crypto/ec_signature_creator.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 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" |
| 14 | |
| 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_FALSE(signer.get()); |
| 23 | } |
| 24 | #else |
| 25 | TEST(ECSignatureCreatorTest, BasicTest) { |
| 26 | // Do a verify round trip. |
| 27 | scoped_ptr<crypto::ECPrivateKey> key_original( |
| 28 | crypto::ECPrivateKey::Create()); |
| 29 | ASSERT_TRUE(key_original.get()); |
| 30 | |
| 31 | std::vector<uint8> key_info; |
| 32 | ASSERT_TRUE(key_original->ExportEncryptedPrivateKey("", 1000, &key_info)); |
| 33 | std::vector<uint8> pubkey_info; |
| 34 | ASSERT_TRUE(key_original->ExportPublicKey(&pubkey_info)); |
| 35 | |
| 36 | scoped_ptr<crypto::ECPrivateKey> key( |
| 37 | crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo("", key_info, |
| 38 | pubkey_info)); |
| 39 | ASSERT_TRUE(key.get()); |
| 40 | ASSERT_TRUE(key->key() != NULL); |
| 41 | |
| 42 | scoped_ptr<crypto::ECSignatureCreator> signer( |
| 43 | crypto::ECSignatureCreator::Create(key.get())); |
| 44 | ASSERT_TRUE(signer.get()); |
| 45 | |
| 46 | std::string data("Hello, World!"); |
| 47 | std::vector<uint8> signature; |
| 48 | ASSERT_TRUE(signer->Sign(reinterpret_cast<const uint8*>(data.c_str()), |
| 49 | data.size(), |
| 50 | &signature)); |
| 51 | |
| 52 | std::vector<uint8> public_key_info; |
| 53 | ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
| 54 | |
| 55 | // This is the algorithm ID for SHA-1 with EC encryption. |
| 56 | const uint8 kECDSAWithSHA1AlgorithmID[] = { |
| 57 | 0x30, 0x0b, |
| 58 | 0x06, 0x07, |
| 59 | 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, |
| 60 | 0x05, 0x00 |
| 61 | }; |
| 62 | crypto::SignatureVerifier verifier; |
| 63 | ASSERT_TRUE(verifier.VerifyInit( |
| 64 | kECDSAWithSHA1AlgorithmID, sizeof(kECDSAWithSHA1AlgorithmID), |
| 65 | &signature.front(), signature.size(), |
| 66 | &public_key_info.front(), public_key_info.size())); |
| 67 | |
| 68 | verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), |
| 69 | data.size()); |
| 70 | ASSERT_TRUE(verifier.VerifyFinal()); |
| 71 | } |
| 72 | #endif // !defined(USE_OPENSSL) |