blob: a2fce03abf5e298b9f16051ec13e1a685afcd4da [file] [log] [blame]
Prashanthc0029b62015-04-27 14:00:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Prashanthc0029b62015-04-27 14:00:08 -07004 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04005 * This file is part of NAC (Name-Based Access Control for NDN).
6 * See AUTHORS.md for complete list of NAC authors and contributors.
Prashanthc0029b62015-04-27 14:00:08 -07007 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04008 * NAC is free software: you can redistribute it and/or modify it under the terms
Prashanthc0029b62015-04-27 14:00:08 -07009 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -040012 * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
Prashanthc0029b62015-04-27 14:00:08 -070013 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
Alexander Afanasyev9091d832018-04-18 17:21:08 -040017 * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Prashanthc0029b62015-04-27 14:00:08 -070018 */
19
20#include "encrypted-content.hpp"
21
22#include "boost-test.hpp"
23#include <algorithm>
24
25namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040026namespace nac {
Prashanthc0029b62015-04-27 14:00:08 -070027namespace tests {
28
29BOOST_AUTO_TEST_SUITE(TestEncryptedContent)
30
31const uint8_t encrypted[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700320x82, 0x30, // EncryptedContent
33 0x1c, 0x16, // KeyLocator
34 0x07, 0x14, // Name
35 0x08, 0x04,
36 0x74, 0x65, 0x73, 0x74, // 'test'
37 0x08, 0x03,
38 0x6b, 0x65, 0x79, // 'key'
39 0x08, 0x07,
40 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator'
41 0x83, 0x01, // EncryptedAlgorithm
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070042 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070043 0x85, 0x0a, // InitialVector
44 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
45 0x84, 0x07, // EncryptedPayload
46 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
47};
48
Yingdi Yu416dae02016-03-09 15:50:53 -080049const uint8_t encryptedNoIv[] = {
Prashanthc0029b62015-04-27 14:00:08 -0700500x82, 0x24, // EncryptedContent
51 0x1c, 0x16, // KeyLocator
52 0x07, 0x14, // Name
53 0x08, 0x04,
54 0x74, 0x65, 0x73, 0x74, // 'test'
55 0x08, 0x03,
56 0x6b, 0x65, 0x79, // 'key'
57 0x08, 0x07,
58 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator'
59 0x83, 0x01, // EncryptedAlgorithm
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070060 0x03,
Prashanthc0029b62015-04-27 14:00:08 -070061 0x84, 0x07, // EncryptedPayload
62 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
63};
64
65const uint8_t message[] = {
66 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
67};
68
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070069const uint8_t iv[] = {
70 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73
71};
72
Prashanthc0029b62015-04-27 14:00:08 -070073BOOST_AUTO_TEST_CASE(Constructor)
74{
Yingdi Yu416dae02016-03-09 15:50:53 -080075 // checking default setting.
Prashanthc0029b62015-04-27 14:00:08 -070076 EncryptedContent content;
77 BOOST_CHECK_EQUAL(content.getAlgorithmType(), -1);
Yingdi Yu416dae02016-03-09 15:50:53 -080078 BOOST_CHECK_EQUAL(content.getPayload().size(), 0);
79 BOOST_CHECK_EQUAL(content.getInitialVector().size(), 0);
Prashanthc0029b62015-04-27 14:00:08 -070080 BOOST_CHECK_EQUAL(content.hasKeyLocator(), false);
81 BOOST_CHECK_THROW(content.getKeyLocator(), EncryptedContent::Error);
82
Yingdi Yu416dae02016-03-09 15:50:53 -080083 // check an encrypted content with IV
84 EncryptedContent rsaOaepContent(tlv::AlgorithmRsaOaep,
85 KeyLocator("test/key/locator"),
86 message, sizeof(message),
87 iv, sizeof(iv));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070088
Yingdi Yu416dae02016-03-09 15:50:53 -080089 BOOST_CHECK_EQUAL(rsaOaepContent.getAlgorithmType(), tlv::AlgorithmRsaOaep);
90 BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContent.getPayload().begin(),
91 rsaOaepContent.getPayload().end(),
92 message,
93 message + sizeof(message));
94 BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContent.getInitialVector().begin(),
95 rsaOaepContent.getInitialVector().end(),
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070096 iv,
97 iv + sizeof(iv));
Yingdi Yu416dae02016-03-09 15:50:53 -080098 BOOST_CHECK_EQUAL(rsaOaepContent.hasKeyLocator(), true);
99 BOOST_CHECK_NO_THROW(rsaOaepContent.getKeyLocator());
100 BOOST_CHECK_EQUAL(rsaOaepContent.getKeyLocator().getName(), Name("test/key/locator"));
Prashanthc0029b62015-04-27 14:00:08 -0700101
Yingdi Yu416dae02016-03-09 15:50:53 -0800102 // encoding
Prashanthc0029b62015-04-27 14:00:08 -0700103 Block encryptedBlock(encrypted, sizeof(encrypted));
Yingdi Yu416dae02016-03-09 15:50:53 -0800104 const Block& encoded = rsaOaepContent.wireEncode();
Prashanthc0029b62015-04-27 14:00:08 -0700105
106 BOOST_CHECK_EQUAL_COLLECTIONS(encryptedBlock.wire(),
107 encryptedBlock.wire() + encryptedBlock.size(),
108 encoded.wire(),
109 encoded.wire() + encoded.size());
110
Yingdi Yu416dae02016-03-09 15:50:53 -0800111 // decoding
112 EncryptedContent rsaOaepContent2(encryptedBlock);
113 BOOST_CHECK_EQUAL(rsaOaepContent2.getAlgorithmType(), tlv::AlgorithmRsaOaep);
114 BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContent2.getPayload().begin(),
115 rsaOaepContent2.getPayload().end(),
116 message,
117 message + sizeof(message));
118 BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContent2.getInitialVector().begin(),
119 rsaOaepContent2.getInitialVector().end(),
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700120 iv,
121 iv + sizeof(iv));
Yingdi Yu416dae02016-03-09 15:50:53 -0800122 BOOST_CHECK_EQUAL(rsaOaepContent2.hasKeyLocator(), true);
123 BOOST_CHECK_NO_THROW(rsaOaepContent2.getKeyLocator());
124 BOOST_CHECK_EQUAL(rsaOaepContent2.getKeyLocator().getName(), Name("test/key/locator"));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700125
Yingdi Yu416dae02016-03-09 15:50:53 -0800126
127 // Check no IV case
128 EncryptedContent rsaOaepContentNoIv(tlv::AlgorithmRsaOaep,
129 KeyLocator("test/key/locator"),
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700130 message, sizeof(message));
Yingdi Yu416dae02016-03-09 15:50:53 -0800131 BOOST_CHECK_EQUAL(rsaOaepContentNoIv.getAlgorithmType(), tlv::AlgorithmRsaOaep);
132 BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContentNoIv.getPayload().begin(),
133 rsaOaepContentNoIv.getPayload().end(),
134 message,
135 message + sizeof(message));
136 BOOST_CHECK_EQUAL(rsaOaepContentNoIv.getInitialVector().size(), 0);
137 BOOST_CHECK_EQUAL(rsaOaepContentNoIv.hasKeyLocator(), true);
138 BOOST_CHECK_NO_THROW(rsaOaepContentNoIv.getKeyLocator());
139 BOOST_CHECK_EQUAL(rsaOaepContentNoIv.getKeyLocator().getName(), Name("test/key/locator"));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700140
Yingdi Yu416dae02016-03-09 15:50:53 -0800141 // encoding
142 Block encryptedBlock2(encryptedNoIv, sizeof(encryptedNoIv));
143 const Block& encodedNoIV = rsaOaepContentNoIv.wireEncode();
144 BOOST_CHECK_EQUAL_COLLECTIONS(encryptedBlock2.wire(),
145 encryptedBlock2.wire() + encryptedBlock2.size(),
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700146 encodedNoIV.wire(),
147 encodedNoIV.wire() + encodedNoIV.size());
148
Yingdi Yu416dae02016-03-09 15:50:53 -0800149 // decoding
150 EncryptedContent rsaOaepContentNoIv2(encryptedBlock2);
151 BOOST_CHECK_EQUAL(rsaOaepContentNoIv2.getAlgorithmType(), tlv::AlgorithmRsaOaep);
152 BOOST_CHECK_EQUAL_COLLECTIONS(rsaOaepContentNoIv2.getPayload().begin(),
153 rsaOaepContentNoIv2.getPayload().end(),
154 message,
155 message + sizeof(message));
156 BOOST_CHECK_EQUAL(rsaOaepContentNoIv2.getInitialVector().size(), 0);
157 BOOST_CHECK_EQUAL(rsaOaepContentNoIv2.hasKeyLocator(), true);
158 BOOST_CHECK_NO_THROW(rsaOaepContentNoIv2.getKeyLocator());
159 BOOST_CHECK_EQUAL(rsaOaepContentNoIv2.getKeyLocator().getName(), Name("test/key/locator"));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700160
Prashanthc0029b62015-04-27 14:00:08 -0700161}
162
163BOOST_AUTO_TEST_CASE(ConstructorError)
164{
165 const uint8_t error1[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700166 0x1f, 0x30, // Wrong EncryptedContent (0x82, 0x24)
Prashanthc0029b62015-04-27 14:00:08 -0700167 0x1c, 0x16, // KeyLocator
168 0x07, 0x14, // Name
169 0x08, 0x04,
170 0x74, 0x65, 0x73, 0x74,
171 0x08, 0x03,
172 0x6b, 0x65, 0x79,
173 0x08, 0x07,
174 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
175 0x83, 0x01, // EncryptedAlgorithm
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700176 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700177 0x85, 0x0a, // InitialVector
178 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
Prashanthc0029b62015-04-27 14:00:08 -0700179 0x84, 0x07, // EncryptedPayload
180 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
181 };
182 Block errorBlock1(error1, sizeof(error1));
183 BOOST_CHECK_THROW(EncryptedContent info(errorBlock1), EncryptedContent::Error);
184
185 const uint8_t error2[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700186 0x82, 0x30, // EncryptedContent
Prashanthc0029b62015-04-27 14:00:08 -0700187 0x1d, 0x16, // Wrong KeyLocator (0x1c, 0x16)
188 0x07, 0x14, // Name
189 0x08, 0x04,
190 0x74, 0x65, 0x73, 0x74,
191 0x08, 0x03,
192 0x6b, 0x65, 0x79,
193 0x08, 0x07,
194 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
195 0x83, 0x01, // EncryptedAlgorithm
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700196 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700197 0x85, 0x0a, // InitialVector
198 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
Prashanthc0029b62015-04-27 14:00:08 -0700199 0x84, 0x07, // EncryptedPayload
200 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
201 };
202 Block errorBlock2(error2, sizeof(error2));
203 BOOST_CHECK_THROW(EncryptedContent info(errorBlock2), EncryptedContent::Error);
204
205 const uint8_t error3[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700206 0x82, 0x30, // EncryptedContent
Prashanthc0029b62015-04-27 14:00:08 -0700207 0x1c, 0x16, // KeyLocator
208 0x07, 0x14, // Name
209 0x08, 0x04,
210 0x74, 0x65, 0x73, 0x74,
211 0x08, 0x03,
212 0x6b, 0x65, 0x79,
213 0x08, 0x07,
214 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
215 0x1d, 0x01, // Wrong EncryptedAlgorithm (0x83, 0x01)
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700216 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700217 0x85, 0x0a, // InitialVector
218 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
Prashanthc0029b62015-04-27 14:00:08 -0700219 0x84, 0x07, // EncryptedPayload
220 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
221 };
222 Block errorBlock3(error3, sizeof(error3));
223 BOOST_CHECK_THROW(EncryptedContent info(errorBlock3), EncryptedContent::Error);
224
225 const uint8_t error4[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700226 0x82, 0x30, // EncryptedContent
Prashanthc0029b62015-04-27 14:00:08 -0700227 0x1c, 0x16, // KeyLocator
228 0x07, 0x14, // Name
229 0x08, 0x04,
230 0x74, 0x65, 0x73, 0x74, // 'test'
231 0x08, 0x03,
232 0x6b, 0x65, 0x79, // 'key'
233 0x08, 0x07,
234 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator'
235 0x83, 0x01, // EncryptedAlgorithm
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700236 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700237 0x1f, 0x0a, // InitialVector (0x84, 0x0a)
238 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
239 0x84, 0x07, // EncryptedPayload
Prashanthc0029b62015-04-27 14:00:08 -0700240 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
241 };
242 Block errorBlock4(error4, sizeof(error4));
243 BOOST_CHECK_THROW(EncryptedContent info(errorBlock4), EncryptedContent::Error);
244
245 const uint8_t error5[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700246 0x82, 0x30, // EncryptedContent
247 0x1c, 0x16, // KeyLocator
248 0x07, 0x14, // Name
249 0x08, 0x04,
250 0x74, 0x65, 0x73, 0x74, // 'test'
251 0x08, 0x03,
252 0x6b, 0x65, 0x79, // 'key'
253 0x08, 0x07,
254 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator'
255 0x83, 0x01, // EncryptedAlgorithm
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700256 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700257 0x85, 0x0a, // InitialVector
258 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
259 0x21, 0x07, // EncryptedPayload (0x85, 0x07)
260 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
Prashanthc0029b62015-04-27 14:00:08 -0700261 };
262 Block errorBlock5(error5, sizeof(error5));
263 BOOST_CHECK_THROW(EncryptedContent info(errorBlock5), EncryptedContent::Error);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700264
265 const uint8_t error6[] = {
266 0x82, 0x00 // Empty EncryptedContent
267 };
268 Block errorBlock6(error6, sizeof(error6));
269 BOOST_CHECK_THROW(EncryptedContent info(errorBlock6), EncryptedContent::Error);
Prashanthc0029b62015-04-27 14:00:08 -0700270}
271
272BOOST_AUTO_TEST_CASE(SetterGetter)
273{
274 EncryptedContent content;
275 BOOST_CHECK_EQUAL(content.getAlgorithmType(), -1);
Yingdi Yu416dae02016-03-09 15:50:53 -0800276 BOOST_CHECK_EQUAL(content.getPayload().size(), 0);
277 BOOST_CHECK_EQUAL(content.getInitialVector().size(), 0);
Prashanthc0029b62015-04-27 14:00:08 -0700278 BOOST_CHECK_EQUAL(content.hasKeyLocator(), false);
279 BOOST_CHECK_THROW(content.getKeyLocator(), EncryptedContent::Error);
280
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700281 content.setAlgorithmType(tlv::AlgorithmRsaOaep);
282 BOOST_CHECK_EQUAL(content.getAlgorithmType(), tlv::AlgorithmRsaOaep);
Yingdi Yu416dae02016-03-09 15:50:53 -0800283 BOOST_CHECK_EQUAL(content.getPayload().size(), 0);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700284 BOOST_CHECK_EQUAL(content.getInitialVector().size(), 0);
Prashanthc0029b62015-04-27 14:00:08 -0700285 BOOST_CHECK_EQUAL(content.hasKeyLocator(), false);
286
287 KeyLocator keyLocator("/test/key/locator");
288 content.setKeyLocator(keyLocator);
289 BOOST_CHECK_EQUAL(content.hasKeyLocator(), true);
290 BOOST_CHECK_NO_THROW(content.getKeyLocator());
291 BOOST_CHECK_EQUAL(content.getKeyLocator().getName(), Name("/test/key/locator"));
Yingdi Yu416dae02016-03-09 15:50:53 -0800292 BOOST_CHECK_EQUAL(content.getPayload().size(), 0);
293 BOOST_CHECK_EQUAL(content.getInitialVector().size(), 0);
Prashanthc0029b62015-04-27 14:00:08 -0700294
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700295 content.setPayload(message, sizeof(message));
Yingdi Yu416dae02016-03-09 15:50:53 -0800296 BOOST_CHECK_EQUAL_COLLECTIONS(content.getPayload().begin(),
297 content.getPayload().end(),
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700298 message,
299 message + sizeof(message));
Prashanthc0029b62015-04-27 14:00:08 -0700300
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700301 content.setInitialVector(iv, sizeof(iv));
Yingdi Yu416dae02016-03-09 15:50:53 -0800302 BOOST_CHECK_EQUAL_COLLECTIONS(content.getInitialVector().begin(),
303 content.getInitialVector().end(),
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700304 iv,
305 iv + sizeof(iv));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700306
Prashanthc0029b62015-04-27 14:00:08 -0700307 const Block& encoded = content.wireEncode();
308 Block contentBlock(encrypted, sizeof(encrypted));
Prashanthc0029b62015-04-27 14:00:08 -0700309 BOOST_CHECK_EQUAL_COLLECTIONS(contentBlock.wire(),
310 contentBlock.wire() + contentBlock.size(),
311 encoded.wire(),
312 encoded.wire() + encoded.size());
313}
314BOOST_AUTO_TEST_SUITE_END()
315
316} // namespace tests
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400317} // namespace nac
Prashanthc0029b62015-04-27 14:00:08 -0700318} // namespace ndn