blob: 1a509eb94b7b0d5f0efbb36f73c8061e0f58c3b7 [file] [log] [blame]
Yingdi Yuae734272015-07-04 17:38:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento8aad3722017-09-16 20:57:28 -04002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yuae734272015-07-04 17:38:48 -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 Pesavento8aad3722017-09-16 20:57:28 -040022#include "security/transform/digest-filter.hpp"
23
24#include "encoding/buffer-stream.hpp"
Davide Pesavento720f3ba2017-12-29 22:06:29 -050025#include "security/detail/openssl.hpp"
Yingdi Yuae734272015-07-04 17:38:48 -070026#include "security/transform/buffer-source.hpp"
27#include "security/transform/step-source.hpp"
Yingdi Yuae734272015-07-04 17:38:48 -070028#include "security/transform/stream-sink.hpp"
Yingdi Yuae734272015-07-04 17:38:48 -070029
30#include "boost-test.hpp"
31
32namespace ndn {
33namespace security {
34namespace transform {
35namespace tests {
36
37BOOST_AUTO_TEST_SUITE(Security)
38BOOST_AUTO_TEST_SUITE(Transform)
39BOOST_AUTO_TEST_SUITE(TestDigestFilter)
40
Davide Pesavento8aad3722017-09-16 20:57:28 -040041static const uint8_t in[] = {
42 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
43 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
44 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
45 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
46 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
47 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
48 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
49 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
50 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
51 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
52 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
53 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
54 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
55 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
56 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
57 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
58};
59static const uint8_t out[] = {
60 0x3e, 0x14, 0xfd, 0x66, 0x9a, 0x79, 0x80, 0x65,
61 0xc4, 0x0d, 0x61, 0xf8, 0x6a, 0xc7, 0x98, 0x29,
62 0xc0, 0x6b, 0x90, 0x8f, 0xbb, 0x19, 0xa0, 0x85,
63 0xf7, 0xfa, 0x7b, 0x2d, 0xd6, 0x8c, 0xd5, 0xa3
64};
65
Davide Pesavento720f3ba2017-12-29 22:06:29 -050066BOOST_AUTO_TEST_CASE(BufferInput)
Yingdi Yuae734272015-07-04 17:38:48 -070067{
Yingdi Yuae734272015-07-04 17:38:48 -070068 OBufferStream os;
Yingdi Yu99b2a002015-08-12 12:47:44 -070069 bufferSource(in, sizeof(in)) >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
Davide Pesavento720f3ba2017-12-29 22:06:29 -050070 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
Yingdi Yuae734272015-07-04 17:38:48 -070071}
72
Davide Pesavento720f3ba2017-12-29 22:06:29 -050073BOOST_AUTO_TEST_CASE(StepInput)
Yingdi Yuae734272015-07-04 17:38:48 -070074{
Yingdi Yuae734272015-07-04 17:38:48 -070075 StepSource source;
76 OBufferStream os;
Yingdi Yu99b2a002015-08-12 12:47:44 -070077 source >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
Yingdi Yuae734272015-07-04 17:38:48 -070078 source.write(in, 32);
79 source.write(in + 32, 1);
80 source.write(in + 33, 2);
81 source.write(in + 35, 3);
82 source.write(in + 38, 26);
83 source.write(in + 64, 64);
84 source.end();
Davide Pesavento720f3ba2017-12-29 22:06:29 -050085 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
Yingdi Yuae734272015-07-04 17:38:48 -070086}
87
Davide Pesavento720f3ba2017-12-29 22:06:29 -050088BOOST_AUTO_TEST_CASE(AlgorithmNone)
Yingdi Yuae734272015-07-04 17:38:48 -070089{
Davide Pesavento8aad3722017-09-16 20:57:28 -040090 BOOST_CHECK_THROW(DigestFilter{DigestAlgorithm::NONE}, Error);
Yingdi Yuae734272015-07-04 17:38:48 -070091}
92
Davide Pesavento720f3ba2017-12-29 22:06:29 -050093BOOST_AUTO_TEST_CASE(AlgorithmSha224)
94{
95 const uint8_t out[] = {
96 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47, 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4,
97 0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4, 0x2f,
98 };
99 OBufferStream os;
100 bufferSource("") >> digestFilter(DigestAlgorithm::SHA224) >> streamSink(os);
101 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
102}
103
104BOOST_AUTO_TEST_CASE(AlgorithmSha256)
105{
106 const uint8_t out[] = {
107 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
108 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
109 };
110 OBufferStream os;
111 bufferSource("") >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
112 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
113}
114
115BOOST_AUTO_TEST_CASE(AlgorithmSha384)
116{
117 const uint8_t out[] = {
118 0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a,
119 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda,
120 0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b,
121 };
122 OBufferStream os;
123 bufferSource("") >> digestFilter(DigestAlgorithm::SHA384) >> streamSink(os);
124 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
125}
126
127BOOST_AUTO_TEST_CASE(AlgorithmSha512)
128{
129 const uint8_t out[] = {
130 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,
131 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,
132 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,
133 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e,
134 };
135 OBufferStream os;
136 bufferSource("") >> digestFilter(DigestAlgorithm::SHA512) >> streamSink(os);
137 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
138}
139
140#if OPENSSL_VERSION_NUMBER >= 0x1010000fL && !defined(OPENSSL_NO_BLAKE2)
141BOOST_AUTO_TEST_CASE(AlgorithmBlake2b_512)
142{
143 const uint8_t out[] = {
144 0x78, 0x6a, 0x02, 0xf7, 0x42, 0x01, 0x59, 0x03, 0xc6, 0xc6, 0xfd, 0x85, 0x25, 0x52, 0xd2, 0x72,
145 0x91, 0x2f, 0x47, 0x40, 0xe1, 0x58, 0x47, 0x61, 0x8a, 0x86, 0xe2, 0x17, 0xf7, 0x1f, 0x54, 0x19,
146 0xd2, 0x5e, 0x10, 0x31, 0xaf, 0xee, 0x58, 0x53, 0x13, 0x89, 0x64, 0x44, 0x93, 0x4e, 0xb0, 0x4b,
147 0x90, 0x3a, 0x68, 0x5b, 0x14, 0x48, 0xb7, 0x55, 0xd5, 0x6f, 0x70, 0x1a, 0xfe, 0x9b, 0xe2, 0xce,
148 };
149 OBufferStream os;
150 bufferSource("") >> digestFilter(DigestAlgorithm::BLAKE2B_512) >> streamSink(os);
151 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
152}
153
154BOOST_AUTO_TEST_CASE(AlgorithmBlake2s_256)
155{
156 const uint8_t out[] = {
157 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94, 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
158 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e, 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
159 };
160 OBufferStream os;
161 bufferSource("") >> digestFilter(DigestAlgorithm::BLAKE2S_256) >> streamSink(os);
162 BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
163}
164#endif // OPENSSL_VERSION_NUMBER >= 0x1010000fL && !defined(OPENSSL_NO_BLAKE2)
165
Yingdi Yuae734272015-07-04 17:38:48 -0700166BOOST_AUTO_TEST_SUITE_END() // TestDigestFilter
167BOOST_AUTO_TEST_SUITE_END() // Transform
168BOOST_AUTO_TEST_SUITE_END() // Security
169
170} // namespace tests
171} // namespace transform
172} // namespace security
173} // namespace ndn