Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 273ea01 | 2022-02-20 17:50:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -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/digest-filter.hpp" |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
| 26 | #include "ndn-cxx/security/transform/step-source.hpp" |
| 27 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "tests/boost-test.hpp" |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 80d671f | 2022-06-08 04:04:52 -0400 | [diff] [blame] | 31 | #include <openssl/evp.h> |
| 32 | |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 33 | namespace ndn { |
| 34 | namespace security { |
| 35 | namespace transform { |
| 36 | namespace tests { |
| 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(Security) |
| 39 | BOOST_AUTO_TEST_SUITE(Transform) |
| 40 | BOOST_AUTO_TEST_SUITE(TestDigestFilter) |
| 41 | |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 42 | static const uint8_t in[] = { |
| 43 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 44 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 45 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 46 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 47 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 48 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 49 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 50 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 51 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 52 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 53 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 54 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 55 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 56 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 57 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 58 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 59 | }; |
| 60 | static const uint8_t out[] = { |
| 61 | 0x3e, 0x14, 0xfd, 0x66, 0x9a, 0x79, 0x80, 0x65, |
| 62 | 0xc4, 0x0d, 0x61, 0xf8, 0x6a, 0xc7, 0x98, 0x29, |
| 63 | 0xc0, 0x6b, 0x90, 0x8f, 0xbb, 0x19, 0xa0, 0x85, |
| 64 | 0xf7, 0xfa, 0x7b, 0x2d, 0xd6, 0x8c, 0xd5, 0xa3 |
| 65 | }; |
| 66 | |
Davide Pesavento | 720f3ba | 2017-12-29 22:06:29 -0500 | [diff] [blame] | 67 | BOOST_AUTO_TEST_CASE(BufferInput) |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 68 | { |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 69 | OBufferStream os; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 70 | bufferSource(in) >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os); |
Davide Pesavento | 720f3ba | 2017-12-29 22:06:29 -0500 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Davide Pesavento | 720f3ba | 2017-12-29 22:06:29 -0500 | [diff] [blame] | 74 | BOOST_AUTO_TEST_CASE(StepInput) |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 75 | { |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 76 | StepSource source; |
| 77 | OBufferStream os; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 78 | source >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 79 | source.write({in, 32}); |
| 80 | source.write({in + 32, 1}); |
| 81 | source.write({in + 33, 2}); |
| 82 | source.write({in + 35, 3}); |
| 83 | source.write({in + 38, 26}); |
| 84 | source.write({in + 64, 64}); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 85 | source.end(); |
Davide Pesavento | 720f3ba | 2017-12-29 22:06:29 -0500 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Davide Pesavento | 720f3ba | 2017-12-29 22:06:29 -0500 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(AlgorithmNone) |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 90 | { |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 91 | BOOST_CHECK_THROW(DigestFilter{DigestAlgorithm::NONE}, Error); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Davide Pesavento | 720f3ba | 2017-12-29 22:06:29 -0500 | [diff] [blame] | 94 | BOOST_AUTO_TEST_CASE(AlgorithmSha224) |
| 95 | { |
| 96 | const uint8_t out[] = { |
| 97 | 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47, 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, |
| 98 | 0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4, 0x2f, |
| 99 | }; |
| 100 | OBufferStream os; |
| 101 | bufferSource("") >> digestFilter(DigestAlgorithm::SHA224) >> streamSink(os); |
| 102 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
| 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_CASE(AlgorithmSha256) |
| 106 | { |
| 107 | const uint8_t out[] = { |
| 108 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, |
| 109 | 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, |
| 110 | }; |
| 111 | OBufferStream os; |
| 112 | bufferSource("") >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os); |
| 113 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
| 114 | } |
| 115 | |
| 116 | BOOST_AUTO_TEST_CASE(AlgorithmSha384) |
| 117 | { |
| 118 | const uint8_t out[] = { |
| 119 | 0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a, |
| 120 | 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, |
| 121 | 0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b, |
| 122 | }; |
| 123 | OBufferStream os; |
| 124 | bufferSource("") >> digestFilter(DigestAlgorithm::SHA384) >> streamSink(os); |
| 125 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
| 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(AlgorithmSha512) |
| 129 | { |
| 130 | const uint8_t out[] = { |
| 131 | 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, |
| 132 | 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, |
| 133 | 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, |
| 134 | 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e, |
| 135 | }; |
| 136 | OBufferStream os; |
| 137 | bufferSource("") >> digestFilter(DigestAlgorithm::SHA512) >> streamSink(os); |
| 138 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
| 139 | } |
| 140 | |
Davide Pesavento | 273ea01 | 2022-02-20 17:50:02 -0500 | [diff] [blame] | 141 | #ifndef OPENSSL_NO_BLAKE2 |
Davide Pesavento | 720f3ba | 2017-12-29 22:06:29 -0500 | [diff] [blame] | 142 | BOOST_AUTO_TEST_CASE(AlgorithmBlake2b_512) |
| 143 | { |
| 144 | const uint8_t out[] = { |
| 145 | 0x78, 0x6a, 0x02, 0xf7, 0x42, 0x01, 0x59, 0x03, 0xc6, 0xc6, 0xfd, 0x85, 0x25, 0x52, 0xd2, 0x72, |
| 146 | 0x91, 0x2f, 0x47, 0x40, 0xe1, 0x58, 0x47, 0x61, 0x8a, 0x86, 0xe2, 0x17, 0xf7, 0x1f, 0x54, 0x19, |
| 147 | 0xd2, 0x5e, 0x10, 0x31, 0xaf, 0xee, 0x58, 0x53, 0x13, 0x89, 0x64, 0x44, 0x93, 0x4e, 0xb0, 0x4b, |
| 148 | 0x90, 0x3a, 0x68, 0x5b, 0x14, 0x48, 0xb7, 0x55, 0xd5, 0x6f, 0x70, 0x1a, 0xfe, 0x9b, 0xe2, 0xce, |
| 149 | }; |
| 150 | OBufferStream os; |
| 151 | bufferSource("") >> digestFilter(DigestAlgorithm::BLAKE2B_512) >> streamSink(os); |
| 152 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
| 153 | } |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE(AlgorithmBlake2s_256) |
| 156 | { |
| 157 | const uint8_t out[] = { |
| 158 | 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94, 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c, |
| 159 | 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e, 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9, |
| 160 | }; |
| 161 | OBufferStream os; |
| 162 | bufferSource("") >> digestFilter(DigestAlgorithm::BLAKE2S_256) >> streamSink(os); |
| 163 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
| 164 | } |
Davide Pesavento | 273ea01 | 2022-02-20 17:50:02 -0500 | [diff] [blame] | 165 | #endif // !OPENSSL_NO_BLAKE2 |
Davide Pesavento | 720f3ba | 2017-12-29 22:06:29 -0500 | [diff] [blame] | 166 | |
Davide Pesavento | e80d116 | 2018-09-08 19:23:09 -0400 | [diff] [blame] | 167 | BOOST_AUTO_TEST_CASE(AlgorithmSha3_224) |
| 168 | { |
| 169 | const uint8_t out[] = { |
| 170 | 0x6b, 0x4e, 0x03, 0x42, 0x36, 0x67, 0xdb, 0xb7, 0x3b, 0x6e, 0x15, 0x45, 0x4f, 0x0e, 0xb1, 0xab, |
| 171 | 0xd4, 0x59, 0x7f, 0x9a, 0x1b, 0x07, 0x8e, 0x3f, 0x5b, 0x5a, 0x6b, 0xc7, |
| 172 | }; |
| 173 | OBufferStream os; |
| 174 | bufferSource("") >> digestFilter(DigestAlgorithm::SHA3_224) >> streamSink(os); |
| 175 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
| 176 | } |
| 177 | |
| 178 | BOOST_AUTO_TEST_CASE(AlgorithmSha3_256) |
| 179 | { |
| 180 | const uint8_t out[] = { |
| 181 | 0xa7, 0xff, 0xc6, 0xf8, 0xbf, 0x1e, 0xd7, 0x66, 0x51, 0xc1, 0x47, 0x56, 0xa0, 0x61, 0xd6, 0x62, |
| 182 | 0xf5, 0x80, 0xff, 0x4d, 0xe4, 0x3b, 0x49, 0xfa, 0x82, 0xd8, 0x0a, 0x4b, 0x80, 0xf8, 0x43, 0x4a, |
| 183 | }; |
| 184 | OBufferStream os; |
| 185 | bufferSource("") >> digestFilter(DigestAlgorithm::SHA3_256) >> streamSink(os); |
| 186 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
| 187 | } |
| 188 | |
| 189 | BOOST_AUTO_TEST_CASE(AlgorithmSha3_384) |
| 190 | { |
| 191 | const uint8_t out[] = { |
| 192 | 0x0c, 0x63, 0xa7, 0x5b, 0x84, 0x5e, 0x4f, 0x7d, 0x01, 0x10, 0x7d, 0x85, 0x2e, 0x4c, 0x24, 0x85, |
| 193 | 0xc5, 0x1a, 0x50, 0xaa, 0xaa, 0x94, 0xfc, 0x61, 0x99, 0x5e, 0x71, 0xbb, 0xee, 0x98, 0x3a, 0x2a, |
| 194 | 0xc3, 0x71, 0x38, 0x31, 0x26, 0x4a, 0xdb, 0x47, 0xfb, 0x6b, 0xd1, 0xe0, 0x58, 0xd5, 0xf0, 0x04, |
| 195 | }; |
| 196 | OBufferStream os; |
| 197 | bufferSource("") >> digestFilter(DigestAlgorithm::SHA3_384) >> streamSink(os); |
| 198 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
| 199 | } |
| 200 | |
| 201 | BOOST_AUTO_TEST_CASE(AlgorithmSha3_512) |
| 202 | { |
| 203 | const uint8_t out[] = { |
| 204 | 0xa6, 0x9f, 0x73, 0xcc, 0xa2, 0x3a, 0x9a, 0xc5, 0xc8, 0xb5, 0x67, 0xdc, 0x18, 0x5a, 0x75, 0x6e, |
| 205 | 0x97, 0xc9, 0x82, 0x16, 0x4f, 0xe2, 0x58, 0x59, 0xe0, 0xd1, 0xdc, 0xc1, 0x47, 0x5c, 0x80, 0xa6, |
| 206 | 0x15, 0xb2, 0x12, 0x3a, 0xf1, 0xf5, 0xf9, 0x4c, 0x11, 0xe3, 0xe9, 0x40, 0x2c, 0x3a, 0xc5, 0x58, |
| 207 | 0xf5, 0x00, 0x19, 0x9d, 0x95, 0xb6, 0xd3, 0xe3, 0x01, 0x75, 0x85, 0x86, 0x28, 0x1d, 0xcd, 0x26, |
| 208 | }; |
| 209 | OBufferStream os; |
| 210 | bufferSource("") >> digestFilter(DigestAlgorithm::SHA3_512) >> streamSink(os); |
| 211 | BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end()); |
| 212 | } |
Davide Pesavento | e80d116 | 2018-09-08 19:23:09 -0400 | [diff] [blame] | 213 | |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 214 | BOOST_AUTO_TEST_SUITE_END() // TestDigestFilter |
| 215 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 216 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 217 | |
| 218 | } // namespace tests |
| 219 | } // namespace transform |
| 220 | } // namespace security |
| 221 | } // namespace ndn |