Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 4 | * |
| 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 Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 23 | #include "security/verification-helpers.hpp" |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 24 | #include "util/scheduler.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 25 | |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 26 | #include "boost-test.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 27 | #include "../identity-management-time-fixture.hpp" |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame^] | 28 | #include "make-interest-data.hpp" |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 31 | namespace security { |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 32 | namespace tests { |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 34 | using namespace ndn::tests; |
| 35 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 36 | class SignatureSha256EcdsaTimeFixture : public IdentityManagementTimeFixture |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 37 | { |
| 38 | public: |
| 39 | SignatureSha256EcdsaTimeFixture() |
| 40 | : scheduler(io) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | public: |
| 45 | Scheduler scheduler; |
| 46 | }; |
| 47 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 48 | BOOST_AUTO_TEST_SUITE(Security) |
| 49 | BOOST_FIXTURE_TEST_SUITE(TestSignatureSha256WithEcdsa, SignatureSha256EcdsaTimeFixture) |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 50 | |
| 51 | const uint8_t sigInfo[] = { |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 52 | 0x16, 0x1b, // SignatureInfo |
| 53 | 0x1b, 0x01, // SignatureType |
| 54 | 0x03, |
| 55 | 0x1c, 0x16, // KeyLocator |
| 56 | 0x07, 0x14, // Name: /test/key/locator |
| 57 | 0x08, 0x04, |
| 58 | 0x74, 0x65, 0x73, 0x74, |
| 59 | 0x08, 0x03, |
| 60 | 0x6b, 0x65, 0x79, |
| 61 | 0x08, 0x07, |
| 62 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | const uint8_t sigValue[] = { |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 66 | 0x17, 0x40, // SignatureValue |
| 67 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 68 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 69 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 70 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 71 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | |
| 75 | BOOST_AUTO_TEST_CASE(Decoding) |
| 76 | { |
| 77 | Block sigInfoBlock(sigInfo, sizeof(sigInfo)); |
| 78 | Block sigValueBlock(sigValue, sizeof(sigValue)); |
| 79 | |
| 80 | Signature sig(sigInfoBlock, sigValueBlock); |
Davide Pesavento | 8a8c01b | 2018-03-11 00:07:52 -0500 | [diff] [blame] | 81 | BOOST_CHECK_NO_THROW(SignatureSha256WithEcdsa{sig}); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 82 | BOOST_CHECK_NO_THROW(sig.getKeyLocator()); |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | BOOST_AUTO_TEST_CASE(Encoding) |
| 86 | { |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 87 | Name name("/test/key/locator"); |
| 88 | KeyLocator keyLocator(name); |
| 89 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 90 | SignatureSha256WithEcdsa sig(keyLocator); |
| 91 | |
| 92 | BOOST_CHECK_NO_THROW(sig.getKeyLocator()); |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 93 | |
| 94 | const Block& encodeSigInfoBlock = sig.getInfo(); |
| 95 | |
| 96 | Block sigInfoBlock(sigInfo, sizeof(sigInfo)); |
| 97 | |
| 98 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(), |
| 99 | sigInfoBlock.wire() + sigInfoBlock.size(), |
| 100 | encodeSigInfoBlock.wire(), |
| 101 | encodeSigInfoBlock.wire() + encodeSigInfoBlock.size()); |
Alexander Afanasyev | 1c6976d | 2014-07-13 11:40:50 -0700 | [diff] [blame] | 102 | |
| 103 | sig.setKeyLocator(Name("/test/another/key/locator")); |
| 104 | |
| 105 | const Block& encodeSigInfoBlock2 = sig.getInfo(); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 106 | BOOST_CHECK_NE(sigInfoBlock, encodeSigInfoBlock2); |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE(DataSignature) |
| 110 | { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 111 | Identity identity = addIdentity("/SecurityTestSignatureSha256WithEcdsa/DataSignature", EcKeyParams()); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 112 | |
| 113 | Data testData("/SecurityTestSignatureSha256WithEcdsa/DataSignature/Data1"); |
| 114 | char content[5] = "1234"; |
| 115 | testData.setContent(reinterpret_cast<uint8_t*>(content), 5); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 116 | BOOST_CHECK_NO_THROW(m_keyChain.sign(testData, security::SigningInfo(identity))); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 117 | Block dataBlock(testData.wireEncode().wire(), testData.wireEncode().size()); |
| 118 | |
| 119 | Data testData2; |
| 120 | testData2.wireDecode(dataBlock); |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 121 | BOOST_CHECK(verifySignature(testData2, identity.getDefaultKey())); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 124 | BOOST_AUTO_TEST_CASE(InterestSignature) |
| 125 | { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 126 | Identity identity = addIdentity("/SecurityTestSignatureSha256WithEcdsa/InterestSignature", EcKeyParams()); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 127 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame^] | 128 | auto interest = makeInterest("/SecurityTestSignatureSha256WithEcdsa/InterestSignature/Interest1"); |
| 129 | auto interest11 = makeInterest("/SecurityTestSignatureSha256WithEcdsa/InterestSignature/Interest1"); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 130 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 131 | scheduler.scheduleEvent(100_ms, [&] { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame^] | 132 | m_keyChain.sign(*interest, security::SigningInfo(identity)); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 133 | }); |
| 134 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 135 | advanceClocks(100_ms); |
| 136 | scheduler.scheduleEvent(100_ms, [&] { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame^] | 137 | m_keyChain.sign(*interest11, security::SigningInfo(identity)); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 138 | }); |
| 139 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 140 | advanceClocks(100_ms); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 141 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame^] | 142 | Block interestBlock(interest->wireEncode().wire(), interest->wireEncode().size()); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 143 | |
| 144 | Interest interest2; |
| 145 | interest2.wireDecode(interestBlock); |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 146 | BOOST_CHECK(verifySignature(interest2, identity.getDefaultKey())); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 149 | BOOST_AUTO_TEST_SUITE_END() // TestSignatureSha256WithEcdsa |
| 150 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 151 | |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 152 | } // namespace tests |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 153 | } // namespace security |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 154 | } // namespace ndn |