OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "crypto/ec_signature_creator.h" | 5 #include "crypto/ec_signature_creator.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include <string> | 9 #include <string> |
8 #include <vector> | 10 #include <vector> |
9 | 11 |
10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
11 #include "crypto/ec_private_key.h" | 13 #include "crypto/ec_private_key.h" |
12 #include "crypto/signature_verifier.h" | 14 #include "crypto/signature_verifier.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 // TODO(rch): Add some exported keys from each to | 17 // TODO(rch): Add some exported keys from each to |
16 // test interop between NSS and OpenSSL. | 18 // test interop between NSS and OpenSSL. |
17 | 19 |
18 TEST(ECSignatureCreatorTest, BasicTest) { | 20 TEST(ECSignatureCreatorTest, BasicTest) { |
19 // Do a verify round trip. | 21 // Do a verify round trip. |
20 scoped_ptr<crypto::ECPrivateKey> key_original( | 22 scoped_ptr<crypto::ECPrivateKey> key_original( |
21 crypto::ECPrivateKey::Create()); | 23 crypto::ECPrivateKey::Create()); |
22 ASSERT_TRUE(key_original.get()); | 24 ASSERT_TRUE(key_original.get()); |
23 | 25 |
24 std::vector<uint8> key_info; | 26 std::vector<uint8_t> key_info; |
25 ASSERT_TRUE( | 27 ASSERT_TRUE( |
26 key_original->ExportEncryptedPrivateKey(std::string(), 1000, &key_info)); | 28 key_original->ExportEncryptedPrivateKey(std::string(), 1000, &key_info)); |
27 std::vector<uint8> pubkey_info; | 29 std::vector<uint8_t> pubkey_info; |
28 ASSERT_TRUE(key_original->ExportPublicKey(&pubkey_info)); | 30 ASSERT_TRUE(key_original->ExportPublicKey(&pubkey_info)); |
29 | 31 |
30 scoped_ptr<crypto::ECPrivateKey> key( | 32 scoped_ptr<crypto::ECPrivateKey> key( |
31 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 33 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
32 std::string(), key_info, pubkey_info)); | 34 std::string(), key_info, pubkey_info)); |
33 ASSERT_TRUE(key.get()); | 35 ASSERT_TRUE(key.get()); |
34 ASSERT_TRUE(key->key() != NULL); | 36 ASSERT_TRUE(key->key() != NULL); |
35 | 37 |
36 scoped_ptr<crypto::ECSignatureCreator> signer( | 38 scoped_ptr<crypto::ECSignatureCreator> signer( |
37 crypto::ECSignatureCreator::Create(key.get())); | 39 crypto::ECSignatureCreator::Create(key.get())); |
38 ASSERT_TRUE(signer.get()); | 40 ASSERT_TRUE(signer.get()); |
39 | 41 |
40 std::string data("Hello, World!"); | 42 std::string data("Hello, World!"); |
41 std::vector<uint8> signature; | 43 std::vector<uint8_t> signature; |
42 ASSERT_TRUE(signer->Sign(reinterpret_cast<const uint8*>(data.c_str()), | 44 ASSERT_TRUE(signer->Sign(reinterpret_cast<const uint8_t*>(data.c_str()), |
43 data.size(), | 45 data.size(), &signature)); |
44 &signature)); | |
45 | 46 |
46 std::vector<uint8> public_key_info; | 47 std::vector<uint8_t> public_key_info; |
47 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); | 48 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info)); |
48 | 49 |
49 // This is the algorithm ID for ECDSA with SHA-256. Parameters are ABSENT. | 50 // This is the algorithm ID for ECDSA with SHA-256. Parameters are ABSENT. |
50 // RFC 5758: | 51 // RFC 5758: |
51 // ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2) | 52 // ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2) |
52 // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 } | 53 // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 } |
53 // ... | 54 // ... |
54 // When the ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-SHA384, or | 55 // When the ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-SHA384, or |
55 // ecdsa-with-SHA512 algorithm identifier appears in the algorithm field | 56 // ecdsa-with-SHA512 algorithm identifier appears in the algorithm field |
56 // as an AlgorithmIdentifier, the encoding MUST omit the parameters | 57 // as an AlgorithmIdentifier, the encoding MUST omit the parameters |
57 // field. That is, the AlgorithmIdentifier SHALL be a SEQUENCE of one | 58 // field. That is, the AlgorithmIdentifier SHALL be a SEQUENCE of one |
58 // component, the OID ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with- | 59 // component, the OID ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with- |
59 // SHA384, or ecdsa-with-SHA512. | 60 // SHA384, or ecdsa-with-SHA512. |
60 // See also RFC 5480, Appendix A. | 61 // See also RFC 5480, Appendix A. |
61 const uint8 kECDSAWithSHA256AlgorithmID[] = { | 62 const uint8_t kECDSAWithSHA256AlgorithmID[] = { |
62 0x30, 0x0a, | 63 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, |
63 0x06, 0x08, | |
64 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, | |
65 }; | 64 }; |
66 crypto::SignatureVerifier verifier; | 65 crypto::SignatureVerifier verifier; |
67 ASSERT_TRUE(verifier.VerifyInit( | 66 ASSERT_TRUE(verifier.VerifyInit( |
68 kECDSAWithSHA256AlgorithmID, sizeof(kECDSAWithSHA256AlgorithmID), | 67 kECDSAWithSHA256AlgorithmID, sizeof(kECDSAWithSHA256AlgorithmID), |
69 &signature[0], signature.size(), | 68 &signature[0], signature.size(), |
70 &public_key_info.front(), public_key_info.size())); | 69 &public_key_info.front(), public_key_info.size())); |
71 | 70 |
72 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()), | 71 verifier.VerifyUpdate(reinterpret_cast<const uint8_t*>(data.c_str()), |
73 data.size()); | 72 data.size()); |
74 ASSERT_TRUE(verifier.VerifyFinal()); | 73 ASSERT_TRUE(verifier.VerifyFinal()); |
75 } | 74 } |
OLD | NEW |