blob: d462c93967403070f181ed8c61bfaab11fcf798c [file] [log] [blame]
Prashanthc0029b62015-04-27 14:00:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California
4 *
5 * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
6 * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
7 *
8 * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
9 * 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 *
12 * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * 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
17 * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "encrypted-content.hpp"
21
22#include "boost-test.hpp"
23#include <algorithm>
24
25namespace ndn {
26namespace gep {
27namespace 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
49const uint8_t encrypted_noiv[] = {
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{
75 EncryptedContent content;
76 BOOST_CHECK_EQUAL(content.getAlgorithmType(), -1);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070077 BOOST_CHECK_EQUAL((content.getPayload()).size(), 0);
78 BOOST_CHECK_EQUAL((content.getInitialVector()).size(), 0);
Prashanthc0029b62015-04-27 14:00:08 -070079 BOOST_CHECK_EQUAL(content.hasKeyLocator(), false);
80 BOOST_CHECK_THROW(content.getKeyLocator(), EncryptedContent::Error);
81
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070082 Buffer payload(message, sizeof(message));
83 //Buffer initialVector(iv, sizeof(iv));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -070084
Prashanthc0029b62015-04-27 14:00:08 -070085 KeyLocator keyLocator("test/key/locator");
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070086 EncryptedContent sha256RsaContent(tlv::AlgorithmRsaOaep, keyLocator,
87 message, sizeof(message), iv, sizeof(iv));
88 const Buffer& contentPayload = sha256RsaContent.getPayload();
89 const Buffer& contentInitialVector = sha256RsaContent.getInitialVector();
Prashanthc0029b62015-04-27 14:00:08 -070090
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070091 BOOST_CHECK_EQUAL(sha256RsaContent.getAlgorithmType(), tlv::AlgorithmRsaOaep);
92 BOOST_CHECK_EQUAL_COLLECTIONS(contentPayload.begin(),
93 contentPayload.end(),
94 payload.begin(),
95 payload.end());
96 BOOST_CHECK_EQUAL_COLLECTIONS(contentInitialVector.begin(),
97 contentInitialVector.end(),
98 iv,
99 iv + sizeof(iv));
Prashanthc0029b62015-04-27 14:00:08 -0700100 BOOST_CHECK_EQUAL(sha256RsaContent.hasKeyLocator(), true);
101 BOOST_CHECK_NO_THROW(sha256RsaContent.getKeyLocator());
102 BOOST_CHECK_EQUAL(sha256RsaContent.getKeyLocator().getName(), Name("test/key/locator"));
103
104 Block encryptedBlock(encrypted, sizeof(encrypted));
105 const Block& encoded = sha256RsaContent.wireEncode();
106
107 BOOST_CHECK_EQUAL_COLLECTIONS(encryptedBlock.wire(),
108 encryptedBlock.wire() + encryptedBlock.size(),
109 encoded.wire(),
110 encoded.wire() + encoded.size());
111
112 sha256RsaContent = EncryptedContent(encryptedBlock);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700113 const Buffer& contentPayloadBlock = sha256RsaContent.getPayload();
114 const Buffer& contentInitialVectorBlock = sha256RsaContent.getInitialVector();
Prashanthc0029b62015-04-27 14:00:08 -0700115
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700116 BOOST_CHECK_EQUAL(sha256RsaContent.getAlgorithmType(), tlv::AlgorithmRsaOaep);
Prashanthc0029b62015-04-27 14:00:08 -0700117 BOOST_CHECK_EQUAL(sha256RsaContent.hasKeyLocator(), true);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700118 BOOST_CHECK_EQUAL_COLLECTIONS(contentPayloadBlock.begin(),
119 contentPayloadBlock.end(),
120 payload.begin(),
121 payload.end());
122 BOOST_CHECK_EQUAL_COLLECTIONS(contentInitialVectorBlock.begin(),
123 contentInitialVectorBlock.end(),
124 iv,
125 iv + sizeof(iv));
Prashanthc0029b62015-04-27 14:00:08 -0700126 BOOST_CHECK_NO_THROW(sha256RsaContent.getKeyLocator());
127 BOOST_CHECK_EQUAL(sha256RsaContent.getKeyLocator().getName(), Name("test/key/locator"));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700128
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700129 sha256RsaContent = EncryptedContent(tlv::AlgorithmRsaOaep, keyLocator,
130 message, sizeof(message));
131 const Buffer& contentPayloadRecovered = sha256RsaContent.getPayload();
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700132
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700133 BOOST_CHECK_EQUAL(sha256RsaContent.getAlgorithmType(), tlv::AlgorithmRsaOaep);
134 BOOST_CHECK_EQUAL_COLLECTIONS(contentPayloadRecovered.begin(),
135 contentPayloadRecovered.end(),
136 payload.begin(),
137 payload.end());
138 BOOST_CHECK_EQUAL((sha256RsaContent.getInitialVector()).size(), 0);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700139 BOOST_CHECK_EQUAL(sha256RsaContent.hasKeyLocator(), true);
140 BOOST_CHECK_NO_THROW(sha256RsaContent.getKeyLocator());
141 BOOST_CHECK_EQUAL(sha256RsaContent.getKeyLocator().getName(), Name("test/key/locator"));
142
143 encryptedBlock = Block(encrypted_noiv, sizeof(encrypted_noiv));
144 const Block& encodedNoIV = sha256RsaContent.wireEncode();
145
146 BOOST_CHECK_EQUAL_COLLECTIONS(encryptedBlock.wire(),
147 encryptedBlock.wire() + encryptedBlock.size(),
148 encodedNoIV.wire(),
149 encodedNoIV.wire() + encodedNoIV.size());
150
151 sha256RsaContent = EncryptedContent(encryptedBlock);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700152 const Buffer& contentPayloadNoIV = sha256RsaContent.getPayload();
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700153
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700154 BOOST_CHECK_EQUAL(sha256RsaContent.getAlgorithmType(), tlv::AlgorithmRsaOaep);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700155 BOOST_CHECK_EQUAL(sha256RsaContent.hasKeyLocator(), true);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700156 BOOST_CHECK_EQUAL_COLLECTIONS(contentPayloadNoIV.begin(),
157 contentPayloadNoIV.end(),
158 payload.begin(),
159 payload.end());
160 BOOST_CHECK_EQUAL((sha256RsaContent.getInitialVector()).size(), 0);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700161 BOOST_CHECK_NO_THROW(sha256RsaContent.getKeyLocator());
162 BOOST_CHECK_EQUAL(sha256RsaContent.getKeyLocator().getName(), Name("test/key/locator"));
163
Prashanthc0029b62015-04-27 14:00:08 -0700164}
165
166BOOST_AUTO_TEST_CASE(ConstructorError)
167{
168 const uint8_t error1[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700169 0x1f, 0x30, // Wrong EncryptedContent (0x82, 0x24)
Prashanthc0029b62015-04-27 14:00:08 -0700170 0x1c, 0x16, // KeyLocator
171 0x07, 0x14, // Name
172 0x08, 0x04,
173 0x74, 0x65, 0x73, 0x74,
174 0x08, 0x03,
175 0x6b, 0x65, 0x79,
176 0x08, 0x07,
177 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
178 0x83, 0x01, // EncryptedAlgorithm
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700179 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700180 0x85, 0x0a, // InitialVector
181 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
Prashanthc0029b62015-04-27 14:00:08 -0700182 0x84, 0x07, // EncryptedPayload
183 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
184 };
185 Block errorBlock1(error1, sizeof(error1));
186 BOOST_CHECK_THROW(EncryptedContent info(errorBlock1), EncryptedContent::Error);
187
188 const uint8_t error2[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700189 0x82, 0x30, // EncryptedContent
Prashanthc0029b62015-04-27 14:00:08 -0700190 0x1d, 0x16, // Wrong KeyLocator (0x1c, 0x16)
191 0x07, 0x14, // Name
192 0x08, 0x04,
193 0x74, 0x65, 0x73, 0x74,
194 0x08, 0x03,
195 0x6b, 0x65, 0x79,
196 0x08, 0x07,
197 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
198 0x83, 0x01, // EncryptedAlgorithm
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700199 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700200 0x85, 0x0a, // InitialVector
201 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
Prashanthc0029b62015-04-27 14:00:08 -0700202 0x84, 0x07, // EncryptedPayload
203 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
204 };
205 Block errorBlock2(error2, sizeof(error2));
206 BOOST_CHECK_THROW(EncryptedContent info(errorBlock2), EncryptedContent::Error);
207
208 const uint8_t error3[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700209 0x82, 0x30, // EncryptedContent
Prashanthc0029b62015-04-27 14:00:08 -0700210 0x1c, 0x16, // KeyLocator
211 0x07, 0x14, // Name
212 0x08, 0x04,
213 0x74, 0x65, 0x73, 0x74,
214 0x08, 0x03,
215 0x6b, 0x65, 0x79,
216 0x08, 0x07,
217 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
218 0x1d, 0x01, // Wrong EncryptedAlgorithm (0x83, 0x01)
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700219 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700220 0x85, 0x0a, // InitialVector
221 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
Prashanthc0029b62015-04-27 14:00:08 -0700222 0x84, 0x07, // EncryptedPayload
223 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
224 };
225 Block errorBlock3(error3, sizeof(error3));
226 BOOST_CHECK_THROW(EncryptedContent info(errorBlock3), EncryptedContent::Error);
227
228 const uint8_t error4[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700229 0x82, 0x30, // EncryptedContent
Prashanthc0029b62015-04-27 14:00:08 -0700230 0x1c, 0x16, // KeyLocator
231 0x07, 0x14, // Name
232 0x08, 0x04,
233 0x74, 0x65, 0x73, 0x74, // 'test'
234 0x08, 0x03,
235 0x6b, 0x65, 0x79, // 'key'
236 0x08, 0x07,
237 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator'
238 0x83, 0x01, // EncryptedAlgorithm
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700239 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700240 0x1f, 0x0a, // InitialVector (0x84, 0x0a)
241 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
242 0x84, 0x07, // EncryptedPayload
Prashanthc0029b62015-04-27 14:00:08 -0700243 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
244 };
245 Block errorBlock4(error4, sizeof(error4));
246 BOOST_CHECK_THROW(EncryptedContent info(errorBlock4), EncryptedContent::Error);
247
248 const uint8_t error5[] = {
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700249 0x82, 0x30, // EncryptedContent
250 0x1c, 0x16, // KeyLocator
251 0x07, 0x14, // Name
252 0x08, 0x04,
253 0x74, 0x65, 0x73, 0x74, // 'test'
254 0x08, 0x03,
255 0x6b, 0x65, 0x79, // 'key'
256 0x08, 0x07,
257 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, // 'locator'
258 0x83, 0x01, // EncryptedAlgorithm
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700259 0x03,
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700260 0x85, 0x0a, // InitialVector
261 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d, 0x62, 0x69, 0x74, 0x73,
262 0x21, 0x07, // EncryptedPayload (0x85, 0x07)
263 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74
Prashanthc0029b62015-04-27 14:00:08 -0700264 };
265 Block errorBlock5(error5, sizeof(error5));
266 BOOST_CHECK_THROW(EncryptedContent info(errorBlock5), EncryptedContent::Error);
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700267
268 const uint8_t error6[] = {
269 0x82, 0x00 // Empty EncryptedContent
270 };
271 Block errorBlock6(error6, sizeof(error6));
272 BOOST_CHECK_THROW(EncryptedContent info(errorBlock6), EncryptedContent::Error);
Prashanthc0029b62015-04-27 14:00:08 -0700273}
274
275BOOST_AUTO_TEST_CASE(SetterGetter)
276{
277 EncryptedContent content;
278 BOOST_CHECK_EQUAL(content.getAlgorithmType(), -1);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700279 BOOST_CHECK_EQUAL((content.getPayload()).size(), 0);
280 BOOST_CHECK_EQUAL((content.getInitialVector()).size(), 0);
Prashanthc0029b62015-04-27 14:00:08 -0700281 BOOST_CHECK_EQUAL(content.hasKeyLocator(), false);
282 BOOST_CHECK_THROW(content.getKeyLocator(), EncryptedContent::Error);
283
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700284 content.setAlgorithmType(tlv::AlgorithmRsaOaep);
285 BOOST_CHECK_EQUAL(content.getAlgorithmType(), tlv::AlgorithmRsaOaep);
286 BOOST_CHECK_EQUAL((content.getPayload()).size(), 0);
287 BOOST_CHECK_EQUAL(content.getInitialVector().size(), 0);
Prashanthc0029b62015-04-27 14:00:08 -0700288 BOOST_CHECK_EQUAL(content.hasKeyLocator(), false);
289
290 KeyLocator keyLocator("/test/key/locator");
291 content.setKeyLocator(keyLocator);
292 BOOST_CHECK_EQUAL(content.hasKeyLocator(), true);
293 BOOST_CHECK_NO_THROW(content.getKeyLocator());
294 BOOST_CHECK_EQUAL(content.getKeyLocator().getName(), Name("/test/key/locator"));
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700295 BOOST_CHECK_EQUAL((content.getPayload()).size(), 0);
296 BOOST_CHECK_EQUAL((content.getInitialVector()).size(), 0);
Prashanthc0029b62015-04-27 14:00:08 -0700297
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700298 content.setPayload(message, sizeof(message));
Prashanthc0029b62015-04-27 14:00:08 -0700299
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700300 const Buffer& contentPayload = content.getPayload();
301 BOOST_CHECK_EQUAL_COLLECTIONS(contentPayload.begin(),
302 contentPayload.end(),
303 message,
304 message + sizeof(message));
Prashanthc0029b62015-04-27 14:00:08 -0700305
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700306 content.setInitialVector(iv, sizeof(iv));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700307
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700308 const Buffer& contentInitialVector = content.getInitialVector();
309 BOOST_CHECK_EQUAL_COLLECTIONS(contentInitialVector.begin(),
310 contentInitialVector.end(),
311 iv,
312 iv + sizeof(iv));
Prashanth Swaminathanb1b95962015-07-06 13:13:08 -0700313
Prashanthc0029b62015-04-27 14:00:08 -0700314 const Block& encoded = content.wireEncode();
315 Block contentBlock(encrypted, sizeof(encrypted));
316
317 BOOST_CHECK_EQUAL_COLLECTIONS(contentBlock.wire(),
318 contentBlock.wire() + contentBlock.size(),
319 encoded.wire(),
320 encoded.wire() + encoded.size());
321}
322BOOST_AUTO_TEST_SUITE_END()
323
324} // namespace tests
325} // namespace gep
326} // namespace ndn