blob: 4460be7ae38bceaa00ca74b3ddac4380e7dc471e [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu21157162014-02-28 13:02:34 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu21157162014-02-28 13:02:34 -080020 */
21
Yingdi Yu21157162014-02-28 13:02:34 -080022#include "security/key-chain.hpp"
23#include "security/validator.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070024#include "security/cryptopp.hpp"
Yingdi Yu6ab67812014-11-27 15:00:34 -080025#include "identity-management-fixture.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026#include "boost-test.hpp"
27
Yingdi Yu21157162014-02-28 13:02:34 -080028namespace ndn {
29
Yingdi Yu6ab67812014-11-27 15:00:34 -080030BOOST_FIXTURE_TEST_SUITE(SecurityTestDigestSha256, security::IdentityManagementFixture)
Yingdi Yu21157162014-02-28 13:02:34 -080031
Yingdi Yu4a557052014-07-09 16:40:37 -070032std::string SHA256_RESULT("a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4");
Yingdi Yu21157162014-02-28 13:02:34 -080033
Yingdi Yu4a557052014-07-09 16:40:37 -070034BOOST_AUTO_TEST_CASE(Sha256)
Yingdi Yu21157162014-02-28 13:02:34 -080035{
36 using namespace CryptoPP;
37
38 char content[6] = "1234\n";
39 ConstBufferPtr buf = crypto::sha256(reinterpret_cast<uint8_t*>(content), 5);
Yingdi Yu4a557052014-07-09 16:40:37 -070040 std::string result;
Yingdi Yu21157162014-02-28 13:02:34 -080041 StringSource(buf->buf(), buf->size(), true, new HexEncoder(new StringSink(result), false));
42
Yingdi Yu4a557052014-07-09 16:40:37 -070043 BOOST_CHECK_EQUAL(SHA256_RESULT, result);
Yingdi Yu21157162014-02-28 13:02:34 -080044}
45
Yingdi Yu6ab67812014-11-27 15:00:34 -080046BOOST_AUTO_TEST_CASE(DataSignature)
Yingdi Yu21157162014-02-28 13:02:34 -080047{
48 using namespace CryptoPP;
49
50 Name name("/TestSignatureSha/Basic");
51 Data testData(name);
52 char content[5] = "1234";
53 testData.setContent(reinterpret_cast<uint8_t*>(content), 5);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070054
Yingdi Yu6ab67812014-11-27 15:00:34 -080055 m_keyChain.signWithSha256(testData);
Yingdi Yu21157162014-02-28 13:02:34 -080056
57 testData.wireEncode();
58
Yingdi Yubf6a2812014-06-17 15:32:11 -070059 DigestSha256 sig(testData.getSignature());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070060
Yingdi Yu4a557052014-07-09 16:40:37 -070061 BOOST_CHECK(Validator::verifySignature(testData, sig));
62
63 BOOST_CHECK_THROW(sig.getKeyLocator(), ndn::SignatureInfo::Error);
Yingdi Yu21157162014-02-28 13:02:34 -080064}
65
Yingdi Yu6ab67812014-11-27 15:00:34 -080066BOOST_AUTO_TEST_CASE(InterestSignature)
67{
68 Name name("/SecurityTestDigestSha256/InterestSignature/Interest1");
69 Interest testInterest(name);
70
71 m_keyChain.signWithSha256(testInterest);
72 testInterest.wireEncode();
73 const Name& signedName = testInterest.getName();
74
75 Signature signature(signedName[signed_interest::POS_SIG_INFO].blockFromValue(),
76 signedName[signed_interest::POS_SIG_VALUE].blockFromValue());
77 DigestSha256 sig(signature);
78 BOOST_CHECK(Validator::verifySignature(testInterest, sig));
79}
80
81
Yingdi Yu21157162014-02-28 13:02:34 -080082BOOST_AUTO_TEST_SUITE_END()
83
84} // namespace ndn