blob: f051b06f0011e88919e6ddba9c8a4cc5053d4f0d [file] [log] [blame]
Yingdi Yu7036ce22014-06-19 18:53:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc3dfc242017-09-14 20:18:48 -04002/*
Davide Pesaventobbe79422022-06-01 00:55:02 -04003 * Copyright (c) 2013-2022 Regents of the University of California.
Yingdi Yu7036ce22014-06-19 18:53:37 -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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/key-params.hpp"
Yingdi Yu7036ce22014-06-19 18:53:37 -070023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Yingdi Yu7036ce22014-06-19 18:53:37 -070025
Yingdi Yuc08d7d62015-07-16 21:05:11 -070026#include <boost/lexical_cast.hpp>
27
Yingdi Yu7036ce22014-06-19 18:53:37 -070028namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Yingdi Yu7036ce22014-06-19 18:53:37 -070030
Davide Pesaventoeee3e822016-11-26 19:19:34 +010031BOOST_AUTO_TEST_SUITE(Security)
32BOOST_AUTO_TEST_SUITE(TestKeyParams)
Yingdi Yu7036ce22014-06-19 18:53:37 -070033
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_CASE(Rsa)
Yingdi Yu7036ce22014-06-19 18:53:37 -070035{
36 RsaKeyParams params;
Yingdi Yu99b2a002015-08-12 12:47:44 -070037 BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::RSA);
Yingdi Yu7036ce22014-06-19 18:53:37 -070038 BOOST_CHECK_EQUAL(params.getKeySize(), 2048);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050039 BOOST_CHECK_EQUAL(params.getKeyIdType(), KeyIdType::RANDOM);
Yingdi Yu7036ce22014-06-19 18:53:37 -070040
Davide Pesavento648ae9e2018-09-08 15:40:02 -040041 RsaKeyParams params2(4096, KeyIdType::SHA256);
Yingdi Yu99b2a002015-08-12 12:47:44 -070042 BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::RSA);
Davide Pesavento648ae9e2018-09-08 15:40:02 -040043 BOOST_CHECK_EQUAL(params2.getKeySize(), 4096);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050044 BOOST_CHECK_EQUAL(params2.getKeyIdType(), KeyIdType::SHA256);
Yingdi Yu7036ce22014-06-19 18:53:37 -070045
Davide Pesavento648ae9e2018-09-08 15:40:02 -040046 BOOST_CHECK_THROW(RsaKeyParams(1024), KeyParams::Error);
Davide Pesaventobbe79422022-06-01 00:55:02 -040047 BOOST_CHECK_THROW(RsaKeyParams(2000), KeyParams::Error);
48 BOOST_CHECK_THROW(RsaKeyParams(16500), KeyParams::Error);
49 BOOST_CHECK_THROW(RsaKeyParams(20480), KeyParams::Error);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070050
51 name::Component keyId("keyId");
52 RsaKeyParams params4(keyId);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050053 BOOST_CHECK_EQUAL(params4.getKeyType(), KeyType::RSA);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070054 BOOST_CHECK_EQUAL(params4.getKeySize(), 2048);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050055 BOOST_CHECK_EQUAL(params4.getKeyIdType(), KeyIdType::USER_SPECIFIED);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070056 BOOST_CHECK_EQUAL(params4.getKeyId(), keyId);
Yingdi Yu7036ce22014-06-19 18:53:37 -070057}
58
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070059BOOST_AUTO_TEST_CASE(Ec)
Yingdi Yu7036ce22014-06-19 18:53:37 -070060{
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070061 EcKeyParams params;
Yingdi Yu99b2a002015-08-12 12:47:44 -070062 BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::EC);
Yingdi Yu7036ce22014-06-19 18:53:37 -070063 BOOST_CHECK_EQUAL(params.getKeySize(), 256);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050064 BOOST_CHECK_EQUAL(params.getKeyIdType(), KeyIdType::RANDOM);
Yingdi Yu7036ce22014-06-19 18:53:37 -070065
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070066 EcKeyParams params2(384, KeyIdType::SHA256);
Yingdi Yu99b2a002015-08-12 12:47:44 -070067 BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::EC);
Yingdi Yu7036ce22014-06-19 18:53:37 -070068 BOOST_CHECK_EQUAL(params2.getKeySize(), 384);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050069 BOOST_CHECK_EQUAL(params2.getKeyIdType(), KeyIdType::SHA256);
Yingdi Yu7036ce22014-06-19 18:53:37 -070070
Laqin Fan0fe72ea2019-05-22 16:42:59 -050071 BOOST_CHECK_THROW(EcKeyParams(64), KeyParams::Error);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070072
73 name::Component keyId("keyId");
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070074 EcKeyParams params4(keyId);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050075 BOOST_CHECK_EQUAL(params4.getKeyType(), KeyType::EC);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070076 BOOST_CHECK_EQUAL(params4.getKeySize(), 256);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050077 BOOST_CHECK_EQUAL(params4.getKeyIdType(), KeyIdType::USER_SPECIFIED);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070078 BOOST_CHECK_EQUAL(params4.getKeyId(), keyId);
Yingdi Yu7036ce22014-06-19 18:53:37 -070079}
80
Davide Pesaventoeee3e822016-11-26 19:19:34 +010081BOOST_AUTO_TEST_CASE(Aes)
Yingdi Yu7036ce22014-06-19 18:53:37 -070082{
Yingdi Yuc08d7d62015-07-16 21:05:11 -070083 name::Component keyId("keyId");
84 AesKeyParams params(keyId);
Yingdi Yu99b2a002015-08-12 12:47:44 -070085 BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::AES);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070086 BOOST_CHECK_EQUAL(params.getKeySize(), 128);
87 BOOST_CHECK_EQUAL(params.getKeyIdType(), KeyIdType::USER_SPECIFIED);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050088 BOOST_CHECK_EQUAL(params.getKeyId(), keyId);
Yingdi Yu7036ce22014-06-19 18:53:37 -070089
Yingdi Yuc08d7d62015-07-16 21:05:11 -070090 AesKeyParams params2(keyId, 192);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050091 BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::AES);
Yingdi Yuc08d7d62015-07-16 21:05:11 -070092 BOOST_CHECK_EQUAL(params2.getKeySize(), 192);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050093 BOOST_CHECK_EQUAL(params2.getKeyIdType(), KeyIdType::USER_SPECIFIED);
94 BOOST_CHECK_EQUAL(params2.getKeyId(), keyId);
Yingdi Yu7036ce22014-06-19 18:53:37 -070095
Yingdi Yuc08d7d62015-07-16 21:05:11 -070096 AesKeyParams params3(keyId, 256);
Yingdi Yu99b2a002015-08-12 12:47:44 -070097 BOOST_CHECK_EQUAL(params3.getKeyType(), KeyType::AES);
Yingdi Yu7036ce22014-06-19 18:53:37 -070098 BOOST_CHECK_EQUAL(params3.getKeySize(), 256);
Laqin Fan0fe72ea2019-05-22 16:42:59 -050099 BOOST_CHECK_EQUAL(params3.getKeyIdType(), KeyIdType::USER_SPECIFIED);
100 BOOST_CHECK_EQUAL(params3.getKeyId(), keyId);
Yingdi Yu7036ce22014-06-19 18:53:37 -0700101
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500102 BOOST_CHECK_THROW(AesKeyParams(keyId, 64), KeyParams::Error);
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700103
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500104 AesKeyParams params4;
105 BOOST_CHECK_EQUAL(params4.getKeyType(), KeyType::AES);
106 BOOST_CHECK_EQUAL(params4.getKeySize(), 128);
107 BOOST_CHECK_EQUAL(params4.getKeyIdType(), KeyIdType::RANDOM);
108
109 AesKeyParams params5(192);
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700110 BOOST_CHECK_EQUAL(params5.getKeyType(), KeyType::AES);
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500111 BOOST_CHECK_EQUAL(params5.getKeySize(), 192);
112 BOOST_CHECK_EQUAL(params5.getKeyIdType(), KeyIdType::RANDOM);
Yingdi Yu7036ce22014-06-19 18:53:37 -0700113}
114
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500115BOOST_AUTO_TEST_CASE(Hmac)
116{
117 name::Component keyId("keyId");
118 HmacKeyParams params(keyId);
119 BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::HMAC);
120 BOOST_CHECK_EQUAL(params.getKeySize(), 256);
121 BOOST_CHECK_EQUAL(params.getKeyIdType(), KeyIdType::USER_SPECIFIED);
122 BOOST_CHECK_EQUAL(params.getKeyId(), keyId);
123
124 HmacKeyParams params2(keyId, 384);
125 BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::HMAC);
126 BOOST_CHECK_EQUAL(params2.getKeySize(), 384);
127 BOOST_CHECK_EQUAL(params2.getKeyIdType(), KeyIdType::USER_SPECIFIED);
128 BOOST_CHECK_EQUAL(params2.getKeyId(), keyId);
129
Davide Pesavento5e494c02019-09-29 16:30:19 -0400130 BOOST_CHECK_THROW(HmacKeyParams(keyId, 0), KeyParams::Error);
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500131 BOOST_CHECK_THROW(HmacKeyParams(keyId, 300), KeyParams::Error); // not a multiple of 8
132
133 HmacKeyParams params3;
134 BOOST_CHECK_EQUAL(params3.getKeyType(), KeyType::HMAC);
135 BOOST_CHECK_EQUAL(params3.getKeySize(), 256);
136 BOOST_CHECK_EQUAL(params3.getKeyIdType(), KeyIdType::RANDOM);
137
138 HmacKeyParams params4(1024);
139 BOOST_CHECK_EQUAL(params4.getKeyType(), KeyType::HMAC);
140 BOOST_CHECK_EQUAL(params4.getKeySize(), 1024);
141 BOOST_CHECK_EQUAL(params4.getKeyIdType(), KeyIdType::RANDOM);
142}
143
144BOOST_AUTO_TEST_CASE(KeyTypeToString)
145{
146 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyType::NONE), "NONE");
147 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyType::RSA), "RSA");
148 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyType::EC), "EC");
149 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyType::AES), "AES");
150 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyType::HMAC), "HMAC");
151 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<KeyType>(12345)), "12345");
152}
153
154BOOST_AUTO_TEST_CASE(KeyIdTypeToString)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700155{
Davide Pesavento77d9e812019-06-03 22:05:54 -0400156 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyIdType::USER_SPECIFIED), "USER-SPECIFIED");
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700157 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyIdType::SHA256), "SHA256");
158 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyIdType::RANDOM), "RANDOM");
159 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<KeyIdType>(12345)), "12345");
Yingdi Yu7036ce22014-06-19 18:53:37 -0700160}
161
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100162BOOST_AUTO_TEST_SUITE_END() // TestKeyParams
163BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu7036ce22014-06-19 18:53:37 -0700164
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800165} // namespace tests
Yingdi Yu7036ce22014-06-19 18:53:37 -0700166} // namespace ndn