Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/key-params.hpp" |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 25 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
| 27 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 28 | namespace ndn::tests { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 30 | BOOST_AUTO_TEST_SUITE(Security) |
| 31 | BOOST_AUTO_TEST_SUITE(TestKeyParams) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 33 | BOOST_AUTO_TEST_CASE(Rsa) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 34 | { |
| 35 | RsaKeyParams params; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 36 | BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::RSA); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 37 | BOOST_CHECK_EQUAL(params.getKeySize(), 2048); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 38 | BOOST_CHECK_EQUAL(params.getKeyIdType(), KeyIdType::RANDOM); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 39 | |
Davide Pesavento | 648ae9e | 2018-09-08 15:40:02 -0400 | [diff] [blame] | 40 | RsaKeyParams params2(4096, KeyIdType::SHA256); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 41 | BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::RSA); |
Davide Pesavento | 648ae9e | 2018-09-08 15:40:02 -0400 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(params2.getKeySize(), 4096); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 43 | BOOST_CHECK_EQUAL(params2.getKeyIdType(), KeyIdType::SHA256); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 44 | |
Davide Pesavento | 648ae9e | 2018-09-08 15:40:02 -0400 | [diff] [blame] | 45 | BOOST_CHECK_THROW(RsaKeyParams(1024), KeyParams::Error); |
Davide Pesavento | bbe7942 | 2022-06-01 00:55:02 -0400 | [diff] [blame] | 46 | BOOST_CHECK_THROW(RsaKeyParams(2000), KeyParams::Error); |
| 47 | BOOST_CHECK_THROW(RsaKeyParams(16500), KeyParams::Error); |
| 48 | BOOST_CHECK_THROW(RsaKeyParams(20480), KeyParams::Error); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 49 | |
| 50 | name::Component keyId("keyId"); |
| 51 | RsaKeyParams params4(keyId); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(params4.getKeyType(), KeyType::RSA); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(params4.getKeySize(), 2048); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(params4.getKeyIdType(), KeyIdType::USER_SPECIFIED); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(params4.getKeyId(), keyId); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 58 | BOOST_AUTO_TEST_CASE(Ec) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 59 | { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 60 | EcKeyParams params; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::EC); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(params.getKeySize(), 256); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(params.getKeyIdType(), KeyIdType::RANDOM); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 64 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 65 | EcKeyParams params2(384, KeyIdType::SHA256); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::EC); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 67 | BOOST_CHECK_EQUAL(params2.getKeySize(), 384); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(params2.getKeyIdType(), KeyIdType::SHA256); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 69 | |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 70 | BOOST_CHECK_THROW(EcKeyParams(64), KeyParams::Error); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 71 | |
| 72 | name::Component keyId("keyId"); |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 73 | EcKeyParams params4(keyId); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(params4.getKeyType(), KeyType::EC); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(params4.getKeySize(), 256); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(params4.getKeyIdType(), KeyIdType::USER_SPECIFIED); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 77 | BOOST_CHECK_EQUAL(params4.getKeyId(), keyId); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 80 | BOOST_AUTO_TEST_CASE(Aes) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 81 | { |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 82 | name::Component keyId("keyId"); |
| 83 | AesKeyParams params(keyId); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::AES); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(params.getKeySize(), 128); |
| 86 | BOOST_CHECK_EQUAL(params.getKeyIdType(), KeyIdType::USER_SPECIFIED); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(params.getKeyId(), keyId); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 88 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 89 | AesKeyParams params2(keyId, 192); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 90 | BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::AES); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL(params2.getKeySize(), 192); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(params2.getKeyIdType(), KeyIdType::USER_SPECIFIED); |
| 93 | BOOST_CHECK_EQUAL(params2.getKeyId(), keyId); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 94 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 95 | AesKeyParams params3(keyId, 256); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(params3.getKeyType(), KeyType::AES); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(params3.getKeySize(), 256); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(params3.getKeyIdType(), KeyIdType::USER_SPECIFIED); |
| 99 | BOOST_CHECK_EQUAL(params3.getKeyId(), keyId); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 100 | |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 101 | BOOST_CHECK_THROW(AesKeyParams(keyId, 64), KeyParams::Error); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 102 | |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 103 | AesKeyParams params4; |
| 104 | BOOST_CHECK_EQUAL(params4.getKeyType(), KeyType::AES); |
| 105 | BOOST_CHECK_EQUAL(params4.getKeySize(), 128); |
| 106 | BOOST_CHECK_EQUAL(params4.getKeyIdType(), KeyIdType::RANDOM); |
| 107 | |
| 108 | AesKeyParams params5(192); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 109 | BOOST_CHECK_EQUAL(params5.getKeyType(), KeyType::AES); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(params5.getKeySize(), 192); |
| 111 | BOOST_CHECK_EQUAL(params5.getKeyIdType(), KeyIdType::RANDOM); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 114 | BOOST_AUTO_TEST_CASE(Hmac) |
| 115 | { |
| 116 | name::Component keyId("keyId"); |
| 117 | HmacKeyParams params(keyId); |
| 118 | BOOST_CHECK_EQUAL(params.getKeyType(), KeyType::HMAC); |
| 119 | BOOST_CHECK_EQUAL(params.getKeySize(), 256); |
| 120 | BOOST_CHECK_EQUAL(params.getKeyIdType(), KeyIdType::USER_SPECIFIED); |
| 121 | BOOST_CHECK_EQUAL(params.getKeyId(), keyId); |
| 122 | |
| 123 | HmacKeyParams params2(keyId, 384); |
| 124 | BOOST_CHECK_EQUAL(params2.getKeyType(), KeyType::HMAC); |
| 125 | BOOST_CHECK_EQUAL(params2.getKeySize(), 384); |
| 126 | BOOST_CHECK_EQUAL(params2.getKeyIdType(), KeyIdType::USER_SPECIFIED); |
| 127 | BOOST_CHECK_EQUAL(params2.getKeyId(), keyId); |
| 128 | |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 129 | BOOST_CHECK_THROW(HmacKeyParams(keyId, 0), KeyParams::Error); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 130 | BOOST_CHECK_THROW(HmacKeyParams(keyId, 300), KeyParams::Error); // not a multiple of 8 |
| 131 | |
| 132 | HmacKeyParams params3; |
| 133 | BOOST_CHECK_EQUAL(params3.getKeyType(), KeyType::HMAC); |
| 134 | BOOST_CHECK_EQUAL(params3.getKeySize(), 256); |
| 135 | BOOST_CHECK_EQUAL(params3.getKeyIdType(), KeyIdType::RANDOM); |
| 136 | |
| 137 | HmacKeyParams params4(1024); |
| 138 | BOOST_CHECK_EQUAL(params4.getKeyType(), KeyType::HMAC); |
| 139 | BOOST_CHECK_EQUAL(params4.getKeySize(), 1024); |
| 140 | BOOST_CHECK_EQUAL(params4.getKeyIdType(), KeyIdType::RANDOM); |
| 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_CASE(KeyTypeToString) |
| 144 | { |
| 145 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyType::NONE), "NONE"); |
| 146 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyType::RSA), "RSA"); |
| 147 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyType::EC), "EC"); |
| 148 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyType::AES), "AES"); |
| 149 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyType::HMAC), "HMAC"); |
| 150 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<KeyType>(12345)), "12345"); |
| 151 | } |
| 152 | |
| 153 | BOOST_AUTO_TEST_CASE(KeyIdTypeToString) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 154 | { |
Davide Pesavento | 77d9e81 | 2019-06-03 22:05:54 -0400 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyIdType::USER_SPECIFIED), "USER-SPECIFIED"); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyIdType::SHA256), "SHA256"); |
| 157 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(KeyIdType::RANDOM), "RANDOM"); |
| 158 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<KeyIdType>(12345)), "12345"); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 161 | BOOST_AUTO_TEST_SUITE_END() // TestKeyParams |
| 162 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 163 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 164 | } // namespace ndn::tests |