blob: cb2cf0022b1cd880b816101261d80ed3cc73f7ac [file] [log] [blame]
Yingdi Yuebfa4cb2014-06-17 15:28:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yuebfa4cb2014-06-17 15:28:53 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "security/signature-sha256-with-ecdsa.hpp"
Alexander Afanasyev574aa862017-01-10 19:53:28 -080023#include "security/verification-helpers.hpp"
Yingdi Yu6ab67812014-11-27 15:00:34 -080024#include "util/scheduler.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010025
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070026#include "boost-test.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010027#include "../identity-management-time-fixture.hpp"
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070028
29namespace ndn {
Alexander Afanasyev70244f42017-01-04 12:47:12 -080030namespace security {
Yingdi Yu6ab67812014-11-27 15:00:34 -080031namespace tests {
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070032
Alexander Afanasyev70244f42017-01-04 12:47:12 -080033using namespace ndn::tests;
34
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035class SignatureSha256EcdsaTimeFixture : public IdentityManagementTimeFixture
Yingdi Yu6ab67812014-11-27 15:00:34 -080036{
37public:
38 SignatureSha256EcdsaTimeFixture()
39 : scheduler(io)
40 {
41 }
42
43public:
44 Scheduler scheduler;
45};
46
Davide Pesaventoeee3e822016-11-26 19:19:34 +010047BOOST_AUTO_TEST_SUITE(Security)
48BOOST_FIXTURE_TEST_SUITE(TestSignatureSha256WithEcdsa, SignatureSha256EcdsaTimeFixture)
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070049
50const uint8_t sigInfo[] = {
Alexander Afanasyev70244f42017-01-04 12:47:12 -080051 0x16, 0x1b, // SignatureInfo
52 0x1b, 0x01, // SignatureType
53 0x03,
54 0x1c, 0x16, // KeyLocator
55 0x07, 0x14, // Name: /test/key/locator
56 0x08, 0x04,
57 0x74, 0x65, 0x73, 0x74,
58 0x08, 0x03,
59 0x6b, 0x65, 0x79,
60 0x08, 0x07,
61 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070062};
63
64const uint8_t sigValue[] = {
Alexander Afanasyev70244f42017-01-04 12:47:12 -080065 0x17, 0x40, // SignatureValue
66 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
67 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
68 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
69 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
70 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070071};
72
73
74BOOST_AUTO_TEST_CASE(Decoding)
75{
76 Block sigInfoBlock(sigInfo, sizeof(sigInfo));
77 Block sigValueBlock(sigValue, sizeof(sigValue));
78
79 Signature sig(sigInfoBlock, sigValueBlock);
Yingdi Yu4a557052014-07-09 16:40:37 -070080 BOOST_CHECK_NO_THROW(SignatureSha256WithEcdsa(sig));
81 BOOST_CHECK_NO_THROW(sig.getKeyLocator());
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070082}
83
84BOOST_AUTO_TEST_CASE(Encoding)
85{
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070086 Name name("/test/key/locator");
87 KeyLocator keyLocator(name);
88
Yingdi Yu4a557052014-07-09 16:40:37 -070089 SignatureSha256WithEcdsa sig(keyLocator);
90
91 BOOST_CHECK_NO_THROW(sig.getKeyLocator());
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070092
93 const Block& encodeSigInfoBlock = sig.getInfo();
94
95 Block sigInfoBlock(sigInfo, sizeof(sigInfo));
96
97 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
98 sigInfoBlock.wire() + sigInfoBlock.size(),
99 encodeSigInfoBlock.wire(),
100 encodeSigInfoBlock.wire() + encodeSigInfoBlock.size());
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -0700101
102 sig.setKeyLocator(Name("/test/another/key/locator"));
103
104 const Block& encodeSigInfoBlock2 = sig.getInfo();
105 BOOST_CHECK(sigInfoBlock != encodeSigInfoBlock2);
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700106}
107
Yingdi Yu6ab67812014-11-27 15:00:34 -0800108BOOST_AUTO_TEST_CASE(DataSignature)
109{
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700110 Identity identity = addIdentity("/SecurityTestSignatureSha256WithEcdsa/DataSignature", EcKeyParams());
Yingdi Yu6ab67812014-11-27 15:00:34 -0800111
112 Data testData("/SecurityTestSignatureSha256WithEcdsa/DataSignature/Data1");
113 char content[5] = "1234";
114 testData.setContent(reinterpret_cast<uint8_t*>(content), 5);
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800115 BOOST_CHECK_NO_THROW(m_keyChain.sign(testData, security::SigningInfo(identity)));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800116 Block dataBlock(testData.wireEncode().wire(), testData.wireEncode().size());
117
118 Data testData2;
119 testData2.wireDecode(dataBlock);
Alexander Afanasyev574aa862017-01-10 19:53:28 -0800120 BOOST_CHECK(verifySignature(testData2, identity.getDefaultKey()));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800121}
122
Yingdi Yu6ab67812014-11-27 15:00:34 -0800123BOOST_AUTO_TEST_CASE(InterestSignature)
124{
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700125 Identity identity = addIdentity("/SecurityTestSignatureSha256WithEcdsa/InterestSignature", EcKeyParams());
Yingdi Yu6ab67812014-11-27 15:00:34 -0800126
127 Interest interest("/SecurityTestSignatureSha256WithEcdsa/InterestSignature/Interest1");
128 Interest interest11("/SecurityTestSignatureSha256WithEcdsa/InterestSignature/Interest1");
129
130 scheduler.scheduleEvent(time::milliseconds(100), [&] {
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800131 BOOST_CHECK_NO_THROW(m_keyChain.sign(interest, security::SigningInfo(identity)));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800132 });
133
134 advanceClocks(time::milliseconds(100));
135 scheduler.scheduleEvent(time::milliseconds(100), [&] {
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800136 BOOST_CHECK_NO_THROW(m_keyChain.sign(interest11, security::SigningInfo(identity)));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800137 });
138
139 advanceClocks(time::milliseconds(100));
140
Yingdi Yu6ab67812014-11-27 15:00:34 -0800141 Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size());
142
143 Interest interest2;
144 interest2.wireDecode(interestBlock);
Alexander Afanasyev574aa862017-01-10 19:53:28 -0800145 BOOST_CHECK(verifySignature(interest2, identity.getDefaultKey()));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800146}
147
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100148BOOST_AUTO_TEST_SUITE_END() // TestSignatureSha256WithEcdsa
149BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700150
Yingdi Yu6ab67812014-11-27 15:00:34 -0800151} // namespace tests
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800152} // namespace security
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700153} // namespace ndn