Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -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 | b263f15 | 2015-07-12 16:50:13 -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/transform/signer-filter.hpp" |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/security/key-params.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "ndn-cxx/security/transform/base64-decode.hpp" |
| 27 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
| 28 | #include "ndn-cxx/security/transform/private-key.hpp" |
| 29 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
| 30 | #include "ndn-cxx/security/verification-helpers.hpp" |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 32 | #include "tests/boost-test.hpp" |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 34 | namespace ndn::tests { |
| 35 | |
| 36 | using namespace ndn::security::transform; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(Security) |
| 39 | BOOST_AUTO_TEST_SUITE(Transform) |
| 40 | BOOST_AUTO_TEST_SUITE(TestSignerFilter) |
| 41 | |
| 42 | BOOST_AUTO_TEST_CASE(Rsa) |
| 43 | { |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 44 | const std::string publicKeyPkcs8 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 45 | "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n" |
| 46 | "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n" |
| 47 | "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n" |
| 48 | "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n" |
| 49 | "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n" |
| 50 | "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n" |
| 51 | "ywIDAQAB\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 52 | const std::string privateKeyPkcs1 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 53 | "MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT\n" |
| 54 | "sNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobws3iigohnM9yTK+KKiayPhIAm/+5H\n" |
| 55 | "GT6SgFJhYhqo1/upWdueojil6RP4/AgavHhopxlAVbk6G9VdVnlQcQ5Zv0OcGi73\n" |
| 56 | "c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9gZwIL5PuE9BiO6I39cL9z7EK1SfZh\n" |
| 57 | "OWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA9rH58ynaAix0tcR/nBMRLUX+e3rU\n" |
| 58 | "RHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0ywIDAQABAoIBADQkckOIl4IZMUTn\n" |
| 59 | "W8LFv6xOdkJwMKC8G6bsPRFbyY+HvC2TLt7epSvfS+f4AcYWaOPcDu2E49vt2sNr\n" |
| 60 | "cASly8hgwiRRAB3dHH9vcsboiTo8bi2RFvMqvjv9w3tK2yMxVDtmZamzrrnaV3YV\n" |
| 61 | "Q+5nyKo2F/PMDjQ4eUAKDOzjhBuKHsZBTFnA1MFNI+UKj5X4Yp64DFmKlxTX/U2b\n" |
| 62 | "wzVywo5hzx2Uhw51jmoLls4YUvMJXD0wW5ZtYRuPogXvXb/of9ef/20/wU11WFKg\n" |
| 63 | "Xb4gfR8zUXaXS1sXcnVm3+24vIs9dApUwykuoyjOqxWqcHRec2QT2FxVGkFEraze\n" |
| 64 | "CPa4rMECgYEA5Y8CywomIcTgerFGFCeMHJr8nQGqY2V/owFb3k9maczPnC9p4a9R\n" |
| 65 | "c5szLxA9FMYFxurQZMBWSEG2JS1HR2mnjigx8UKjYML/A+rvvjZOMe4M6Sy2ggh4\n" |
| 66 | "SkLZKpWTzjTe07ByM/j5v/SjNZhWAG7sw4/LmPGRQkwJv+KZhGojuOkCgYEA2cOF\n" |
| 67 | "T6cJRv6kvzTz9S0COZOVm+euJh/BXp7oAsAmbNfOpckPMzqHXy8/wpdKl6AAcB57\n" |
| 68 | "OuztlNfV1D7qvbz7JuRlYwQ0cEfBgbZPcz1p18HHDXhwn57ZPb8G33Yh9Omg0HNA\n" |
| 69 | "Imb4LsVuSqxA6NwSj7cpRekgTedrhLFPJ+Ydb5MCgYEAsM3Q7OjILcIg0t6uht9e\n" |
| 70 | "vrlwTsz1mtCV2co2I6crzdj9HeI2vqf1KAElDt6G7PUHhglcr/yjd8uEqmWRPKNX\n" |
| 71 | "ddnnfVZB10jYeP/93pac6z/Zmc3iU4yKeUe7U10ZFf0KkiiYDQd59CpLef/2XScS\n" |
| 72 | "HB0oRofnxRQjfjLc4muNT+ECgYEAlcDk06MOOTly+F8lCc1bA1dgAmgwFd2usDBd\n" |
| 73 | "Y07a3e0HGnGLN3Kfl7C5i0tZq64HvxLnMd2vgLVxQlXGPpdQrC1TH+XLXg+qnlZO\n" |
| 74 | "ivSH7i0/gx75bHvj75eH1XK65V8pDVDEoSPottllAIs21CxLw3N1ObOZWJm2EfmR\n" |
| 75 | "cuHICmsCgYAtFJ1idqMoHxES3mlRpf2JxyQudP3SCm2WpGmqVzhRYInqeatY5sUd\n" |
| 76 | "lPLHm/p77RT7EyxQHTlwn8FJPuM/4ZH1rQd/vB+Y8qAtYJCexDMsbvLW+Js+VOvk\n" |
| 77 | "jweEC0nrcL31j9mF0vz5E6tfRu4hhJ6L4yfWs0gSejskeVB/w8QY4g==\n"; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 78 | const uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 79 | |
| 80 | OBufferStream os1; |
| 81 | bufferSource(publicKeyPkcs8) >> base64Decode() >> streamSink(os1); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 82 | auto pubKey = os1.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 83 | |
| 84 | PrivateKey sKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 85 | sKey.loadPkcs1Base64({reinterpret_cast<const uint8_t*>(privateKeyPkcs1.data()), |
| 86 | privateKeyPkcs1.size()}); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 87 | |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 88 | BOOST_CHECK_THROW(SignerFilter(DigestAlgorithm::NONE, sKey), Error); |
| 89 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 90 | OBufferStream os2; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 91 | bufferSource(data) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 92 | auto sig = os2.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 93 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 94 | BOOST_TEST(security::verifySignature({data}, *sig, *pubKey)); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | BOOST_AUTO_TEST_CASE(Ecdsa) |
| 98 | { |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 99 | const std::string privateKeyPkcs1 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 100 | "MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC\n" |
| 101 | "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n" |
| 102 | "MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n" |
| 103 | "vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n" |
| 104 | "axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n" |
| 105 | "K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n" |
| 106 | "YyVRAgEBoUQDQgAEaG4WJuDAt0QkEM4t29KDUdzkQlMPGrqWzkWhgt9OGnwc6O7A\n" |
| 107 | "ZLPSrDyhwyrKS7XLRXml5DisQ93RvByll32y8A==\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 108 | const std::string publicKeyPkcs8 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 109 | "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n" |
| 110 | "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n" |
| 111 | "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n" |
| 112 | "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n" |
| 113 | "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n" |
| 114 | "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n" |
| 115 | "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n"; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 116 | const uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 117 | |
| 118 | OBufferStream os1; |
| 119 | bufferSource(publicKeyPkcs8) >> base64Decode() >> streamSink(os1); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 120 | auto pubKey = os1.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 121 | |
| 122 | PrivateKey sKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 123 | sKey.loadPkcs1Base64({reinterpret_cast<const uint8_t*>(privateKeyPkcs1.data()), |
| 124 | privateKeyPkcs1.size()}); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 125 | |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 126 | BOOST_CHECK_THROW(SignerFilter(DigestAlgorithm::NONE, sKey), Error); |
| 127 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 128 | OBufferStream os2; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 129 | bufferSource(data) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 130 | auto sig = os2.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 131 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 132 | BOOST_TEST(security::verifySignature({data}, *sig, *pubKey)); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 135 | BOOST_AUTO_TEST_SUITE(Hmac) |
| 136 | |
| 137 | // Test vectors from RFC 4231 |
Davide Pesavento | 93101df | 2021-06-15 18:33:09 -0400 | [diff] [blame] | 138 | // https://datatracker.ietf.org/doc/html/rfc4231#section-4 |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 139 | BOOST_AUTO_TEST_CASE(Rfc4231Test1) |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 140 | { |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 141 | // Test case 1 |
| 142 | const uint8_t rawKey[] = {0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 143 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
| 144 | const std::string data("Hi There"); |
| 145 | const uint8_t hmacSha224[] = {0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19, 0x68, 0x32, |
| 146 | 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f, 0x47, 0xb4, 0xb1, 0x16, |
| 147 | 0x99, 0x12, 0xba, 0x4f, 0x53, 0x68, 0x4b, 0x22}; |
| 148 | const uint8_t hmacSha256[] = {0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, |
| 149 | 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, |
| 150 | 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, |
| 151 | 0xcf, 0xf7}; |
| 152 | const uint8_t hmacSha384[] = {0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62, 0x6b, 0x08, |
| 153 | 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f, 0x15, 0xf9, 0xda, 0xdb, |
| 154 | 0xe4, 0x10, 0x1e, 0xc6, 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, |
| 155 | 0xc5, 0x9c, 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f, |
| 156 | 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6}; |
| 157 | const uint8_t hmacSha512[] = {0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d, 0x4f, 0xf0, |
| 158 | 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0, 0x23, 0x79, 0xf4, 0xe2, |
| 159 | 0xce, 0x4e, 0xc2, 0x78, 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, |
| 160 | 0x7c, 0xde, 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02, |
| 161 | 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4, 0xbe, 0x9d, |
| 162 | 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70, 0x2e, 0x69, 0x6c, 0x20, |
| 163 | 0x3a, 0x12, 0x68, 0x54}; |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 164 | |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 165 | PrivateKey key; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 166 | key.loadRaw(KeyType::HMAC, rawKey); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 167 | |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 168 | BOOST_CHECK_THROW(SignerFilter(DigestAlgorithm::NONE, key), Error); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 169 | |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 170 | OBufferStream os224; |
| 171 | bufferSource(data) >> signerFilter(DigestAlgorithm::SHA224, key) >> streamSink(os224); |
| 172 | auto sig = os224.buf(); |
| 173 | BOOST_CHECK_EQUAL_COLLECTIONS(sig->begin(), sig->end(), hmacSha224, hmacSha224 + sizeof(hmacSha224)); |
| 174 | |
| 175 | OBufferStream os256; |
| 176 | bufferSource(data) >> signerFilter(DigestAlgorithm::SHA256, key) >> streamSink(os256); |
| 177 | sig = os256.buf(); |
| 178 | BOOST_CHECK_EQUAL_COLLECTIONS(sig->begin(), sig->end(), hmacSha256, hmacSha256 + sizeof(hmacSha256)); |
| 179 | |
| 180 | OBufferStream os384; |
| 181 | bufferSource(data) >> signerFilter(DigestAlgorithm::SHA384, key) >> streamSink(os384); |
| 182 | sig = os384.buf(); |
| 183 | BOOST_CHECK_EQUAL_COLLECTIONS(sig->begin(), sig->end(), hmacSha384, hmacSha384 + sizeof(hmacSha384)); |
| 184 | |
| 185 | OBufferStream os512; |
| 186 | bufferSource(data) >> signerFilter(DigestAlgorithm::SHA512, key) >> streamSink(os512); |
| 187 | sig = os512.buf(); |
| 188 | BOOST_CHECK_EQUAL_COLLECTIONS(sig->begin(), sig->end(), hmacSha512, hmacSha512 + sizeof(hmacSha512)); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 191 | BOOST_AUTO_TEST_CASE(Rfc4231Test2) |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 192 | { |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 193 | // Test case 2 (HMAC-SHA-256 only) |
Davide Pesavento | 35c6379 | 2022-01-17 02:06:03 -0500 | [diff] [blame] | 194 | const uint8_t rawKey[] = {'J', 'e', 'f', 'e'}; |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 195 | const std::string data("what do ya want for nothing?"); |
| 196 | const uint8_t hmacSha256[] = {0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04, |
| 197 | 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08, |
| 198 | 0x9d, 0x27, 0x39, 0x83, 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, |
| 199 | 0x38, 0x43}; |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 200 | |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 201 | PrivateKey key; |
Davide Pesavento | 35c6379 | 2022-01-17 02:06:03 -0500 | [diff] [blame] | 202 | key.loadRaw(KeyType::HMAC, rawKey); |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 203 | |
| 204 | OBufferStream os256; |
| 205 | bufferSource(data) >> signerFilter(DigestAlgorithm::SHA256, key) >> streamSink(os256); |
| 206 | auto sig = os256.buf(); |
| 207 | BOOST_CHECK_EQUAL_COLLECTIONS(sig->begin(), sig->end(), hmacSha256, hmacSha256 + sizeof(hmacSha256)); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 210 | BOOST_AUTO_TEST_CASE(Rfc4231Test3) |
| 211 | { |
| 212 | // Test case 3 (HMAC-SHA-256 only) |
| 213 | const uint8_t rawKey[] = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 214 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}; |
| 215 | const uint8_t data[] = {0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 216 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 217 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 218 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 219 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd}; |
| 220 | const uint8_t hmacSha256[] = {0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, |
| 221 | 0xb8, 0xeb, 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, |
| 222 | 0x3e, 0xf8, 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, |
| 223 | 0x65, 0xfe}; |
| 224 | |
| 225 | PrivateKey key; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 226 | key.loadRaw(KeyType::HMAC, rawKey); |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 227 | |
| 228 | OBufferStream os256; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 229 | bufferSource(data) >> signerFilter(DigestAlgorithm::SHA256, key) >> streamSink(os256); |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 230 | auto sig = os256.buf(); |
| 231 | BOOST_CHECK_EQUAL_COLLECTIONS(sig->begin(), sig->end(), hmacSha256, hmacSha256 + sizeof(hmacSha256)); |
| 232 | } |
| 233 | |
| 234 | BOOST_AUTO_TEST_SUITE_END() // Hmac |
| 235 | |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 236 | BOOST_AUTO_TEST_CASE(InvalidKey) |
| 237 | { |
Davide Pesavento | 5e494c0 | 2019-09-29 16:30:19 -0400 | [diff] [blame] | 238 | PrivateKey key; |
| 239 | BOOST_CHECK_THROW(SignerFilter(DigestAlgorithm::SHA256, key), Error); |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 242 | BOOST_AUTO_TEST_SUITE_END() // TestSignerFilter |
| 243 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 244 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 245 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 246 | } // namespace ndn::tests |