blob: e7339031ea75486e3ba92839e3f78b4a6be7834f [file] [log] [blame]
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -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
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -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.
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -07007 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04008 * NAC is free software: you can redistribute it and/or modify it under the terms
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -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;
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -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/>.
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070018 */
19
Yingdi Yu3decf4e2015-11-02 12:33:31 -080020#include "algo/encryptor.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070021#include "encrypted-content.hpp"
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070022#include "algo/aes.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070023#include "algo/rsa.hpp"
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070024
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070025#include "boost-test.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070026#include <boost/mpl/list.hpp>
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070027#include <algorithm>
28
29namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040030namespace nac {
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070031namespace algo {
32namespace tests {
33
34BOOST_AUTO_TEST_SUITE(TestEncryptor)
35
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070036class TestDataAesCbc
37{
38public:
39 TestDataAesCbc()
40 : keyName("/test")
41 , encryptParams(tlv::AlgorithmAesCbc)
42 {
43 const uint8_t raw_content[] = {
44 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
45 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
46 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x73
47 };
48 plainText = Buffer(raw_content, sizeof(raw_content));
49
50 const uint8_t aes_key[] = {
51 0xdd, 0x60, 0x77, 0xec, 0xa9, 0x6b, 0x23, 0x1b,
52 0x40, 0x6b, 0x5a, 0xf8, 0x7d, 0x3d, 0x55, 0x32
53 };
54 key = Buffer(aes_key, sizeof(aes_key));
55
56 const uint8_t iv[] = {
57 0x73, 0x6f, 0x6d, 0x65, 0x72, 0x61, 0x6e, 0x64,
58 0x6f, 0x6d, 0x76, 0x65, 0x63, 0x74, 0x6f, 0x72
59 };
60
61 encryptParams.setIV(iv, sizeof(iv));
62
63 const uint8_t encrypted_content[] = {
64 0x15, 0x43, // Content
65 0x82, 0x41, // EncryptedContent
66 0x1c, 0x08, // KeyLocator /test
67 0x07, 0x06,
68 0x08, 0x04, 0x74, 0x65, 0x73, 0x74,
69 0x83, 0x01, // EncryptedAlgorithm
70 0x01, // AlgorithmAesCbc
71 0x85, 0x10,
72 0x73, 0x6f, 0x6d, 0x65, 0x72, 0x61, 0x6e, 0x64,
73 0x6f, 0x6d, 0x76, 0x65, 0x63, 0x74, 0x6f, 0x72,
74 0x84, 0x20, // EncryptedPayLoad
75 0x6a, 0x6b, 0x58, 0x9c, 0x30, 0x3b, 0xd9, 0xa6,
76 0xed, 0xd2, 0x12, 0xef, 0x29, 0xad, 0xc3, 0x60,
77 0x1f, 0x1b, 0x6b, 0xc7, 0x03, 0xff, 0x53, 0x52,
78 0x82, 0x6d, 0x82, 0x73, 0x05, 0xf9, 0x03, 0xdc
79 };
80 encryptedContent = Buffer(encrypted_content, sizeof(encrypted_content));
81 }
82
83public:
84 Buffer plainText;
85 Buffer key;
86 Name keyName;
87 EncryptParams encryptParams;
88 Buffer encryptedContent;
89};
90
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070091typedef boost::mpl::list<TestDataAesCbc> EncryptorAesTestInputs;
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070092
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070093BOOST_AUTO_TEST_CASE_TEMPLATE(ContentSymmetricEncrypt, T, EncryptorAesTestInputs)
94{
95 T input;
96
97 Data data;
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070098 encryptData(data,
99 input.plainText.data(),
100 input.plainText.size(),
101 input.keyName,
102 input.key.data(),
103 input.key.size(),
104 input.encryptParams);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700105
Yingdi Yu3decf4e2015-11-02 12:33:31 -0800106 BOOST_CHECK_EQUAL(data.getName(), Name("/FOR").append(input.keyName));
107
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700108 BOOST_CHECK_EQUAL_COLLECTIONS(input.encryptedContent.begin(),
109 input.encryptedContent.end(),
110 data.getContent().wire(),
111 data.getContent().wire() + data.getContent().size());
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700112
113 EncryptedContent content(data.getContent().blockFromValue());
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700114 const Buffer& decryptedOutput = Aes::decrypt(input.key.data(),
115 input.key.size(),
116 content.getPayload().data(),
117 content.getPayload().size(),
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700118 input.encryptParams);
119
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700120 BOOST_CHECK_EQUAL_COLLECTIONS(input.plainText.begin(),
121 input.plainText.end(),
122 decryptedOutput.begin(),
123 decryptedOutput.end());
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700124}
125
126class TestDataRsaOaep
127{
128public:
129 TestDataRsaOaep()
130 : type(tlv::AlgorithmRsaOaep)
131 {
132 }
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700133
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700134public:
135 tlv::AlgorithmTypeValue type;
136};
137
138class TestDataRsaPkcs
139{
140public:
141 TestDataRsaPkcs()
142 : type(tlv::AlgorithmRsaPkcs)
143 {
144 }
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700145
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700146public:
147 tlv::AlgorithmTypeValue type;
148};
149
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700150typedef boost::mpl::list<TestDataRsaOaep, TestDataRsaPkcs> EncryptorRsaTestInputs;
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700151
152BOOST_AUTO_TEST_CASE_TEMPLATE(ContentAsymmetricEncryptSmall, T, EncryptorRsaTestInputs)
153{
154 T type;
155
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700156 const uint8_t raw_content[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
157 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
158 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x73};
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700159
160 Data data;
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700161 RsaKeyParams rsaParams(1024);
162
163 Name keyName("test");
164
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700165 DecryptKey<Rsa> decryptKey = Rsa::generateKey(rsaParams);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700166 EncryptKey<Rsa> encryptKey = Rsa::deriveEncryptKey(decryptKey.getKeyBits());
167
168 Buffer eKey = encryptKey.getKeyBits();
169 Buffer dKey = decryptKey.getKeyBits();
170
171 EncryptParams encryptParams(type.type);
172
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700173 encryptData(data, raw_content, sizeof(raw_content), keyName, eKey.data(), eKey.size(), encryptParams);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700174
Yingdi Yu3decf4e2015-11-02 12:33:31 -0800175 BOOST_CHECK_EQUAL(data.getName(), Name("/FOR").append(keyName));
176
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700177 Block dataContent = data.getContent();
178 dataContent.parse();
179 BOOST_CHECK_EQUAL(dataContent.elements_size(), 1);
180
181 EncryptedContent extractContent(data.getContent().blockFromValue());
182 BOOST_CHECK_EQUAL(extractContent.getKeyLocator().getName(), keyName);
183 BOOST_CHECK_EQUAL(extractContent.getInitialVector().size(), 0);
184 BOOST_CHECK_EQUAL(extractContent.getAlgorithmType(), type.type);
185
186 const Buffer& recovered = extractContent.getPayload();
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700187 Buffer decrypted =
188 Rsa::decrypt(dKey.data(), dKey.size(), recovered.data(), recovered.size());
189 BOOST_CHECK_EQUAL_COLLECTIONS(raw_content,
190 raw_content + sizeof(raw_content),
191 decrypted.begin(),
192 decrypted.end());
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700193}
194
195BOOST_AUTO_TEST_CASE_TEMPLATE(ContentAsymmetricEncryptLarge, T, EncryptorRsaTestInputs)
196{
197 T type;
198
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700199 const uint8_t large_content[] =
200 {0x73, 0x5a, 0xbd, 0x47, 0x0c, 0xfe, 0xf8, 0x7d, 0x2e, 0x17, 0xaa, 0x11, 0x6f, 0x23, 0xc5,
201 0x10, 0x23, 0x36, 0x88, 0xc4, 0x2a, 0x0f, 0x9a, 0x72, 0x54, 0x31, 0xa8, 0xb3, 0x51, 0x18,
202 0x9f, 0x0e, 0x1b, 0x93, 0x62, 0xd9, 0xc4, 0xf5, 0xf4, 0x3d, 0x61, 0x9a, 0xca, 0x05, 0x65,
203 0x6b, 0xc6, 0x41, 0xf9, 0xd5, 0x1c, 0x67, 0xc1, 0xd0, 0xd5, 0x6f, 0x7b, 0x70, 0xb8, 0x8f,
204 0xdb, 0x19, 0x68, 0x7c, 0xe0, 0x2d, 0x04, 0x49, 0xa9, 0xa2, 0x77, 0x4e, 0xfc, 0x60, 0x0d,
205 0x7c, 0x1b, 0x93, 0x6c, 0xd2, 0x61, 0xc4, 0x6b, 0x01, 0xe9, 0x12, 0x28, 0x6d, 0xf5, 0x78,
206 0xe9, 0x99, 0x0b, 0x9c, 0x4f, 0x90, 0x34, 0x3e, 0x06, 0x92, 0x57, 0xe3, 0x7a, 0x8f, 0x13,
207 0xc7, 0xf3, 0xfe, 0xf0, 0xe2, 0x59, 0x48, 0x15, 0xb9, 0xdb, 0x77, 0x07, 0x1d, 0x6d, 0xb5,
208 0x65, 0x17, 0xdf, 0x76, 0x6f, 0xb5, 0x43, 0xde, 0x71, 0xac, 0xf1, 0x22, 0xbf, 0xb2, 0xe5,
209 0xd9, 0x22, 0xf1, 0x67, 0x76, 0x71, 0x0c, 0xff, 0x99, 0x7b, 0x94, 0x9b, 0x24, 0x20, 0x80,
210 0xe3, 0xcc, 0x06, 0x4a, 0xed, 0xdf, 0xec, 0x50, 0xd5, 0x87, 0x3d, 0xa0, 0x7d, 0x9c, 0xe5,
211 0x13, 0x10, 0x98, 0x14, 0xc3, 0x90, 0x10, 0xd9, 0x25, 0x9a, 0x59, 0xe9, 0x37, 0x26, 0xfd,
212 0x87, 0xd7, 0xf4, 0xf9, 0x11, 0x91, 0xad, 0x5c, 0x00, 0x95, 0xf5, 0x2b, 0x37, 0xf7, 0x4e,
213 0xb4, 0x4b, 0x42, 0x7c, 0xb3, 0xad, 0xd6, 0x33, 0x5f, 0x0b, 0x84, 0x57, 0x7f, 0xa7, 0x07,
214 0x73, 0x37, 0x4b, 0xab, 0x2e, 0xfb, 0xfe, 0x1e, 0xcb, 0xb6, 0x4a, 0xc1, 0x21, 0x5f, 0xec,
215 0x92, 0xb7, 0xac, 0x97, 0x75, 0x20, 0xc9, 0xd8, 0x9e, 0x93, 0xd5, 0x12, 0x7a, 0x64, 0xb9,
216 0x4c, 0xed, 0x49, 0x87, 0x44, 0x5b, 0x4f, 0x90, 0x34, 0x3e, 0x06, 0x92, 0x57, 0xe3, 0x7a,
217 0x8f, 0x13, 0xc7, 0xf3, 0xfe, 0xf0, 0xe2, 0x59, 0x48, 0x15, 0xb9, 0xdb, 0x77, 0x07, 0x1d,
218 0x6d, 0xb5, 0x65, 0x17, 0xdf, 0x76, 0x6f, 0xb5, 0x43, 0xde, 0x71, 0xac, 0xf1, 0x22, 0xbf,
219 0xb2, 0xe5, 0xd9};
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700220
221 Data data;
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700222 RsaKeyParams rsaParams(1024);
223
224 Name keyName("test");
225
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700226 DecryptKey<Rsa> decryptKey = Rsa::generateKey(rsaParams);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700227 EncryptKey<Rsa> encryptKey = Rsa::deriveEncryptKey(decryptKey.getKeyBits());
228
229 Buffer eKey = encryptKey.getKeyBits();
230 Buffer dKey = decryptKey.getKeyBits();
231
232 EncryptParams encryptParams(type.type);
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700233 encryptData(data, large_content, sizeof(large_content), keyName, eKey.data(), eKey.size(), encryptParams);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700234
Yingdi Yu3decf4e2015-11-02 12:33:31 -0800235 BOOST_CHECK_EQUAL(data.getName(), Name("/FOR").append(keyName));
236
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700237 Block largeDataContent = data.getContent();
238 largeDataContent.parse();
239 BOOST_CHECK_EQUAL(largeDataContent.elements_size(), 2);
240
241 Block::element_const_iterator it = largeDataContent.elements_begin();
242
243 BOOST_CHECK(it != largeDataContent.elements_end());
244 Block nonceContent(*it);
245 BOOST_CHECK_EQUAL(nonceContent.type(), tlv::EncryptedContent);
246 EncryptedContent encryptedNonce(nonceContent);
247 BOOST_CHECK_EQUAL(encryptedNonce.getKeyLocator().getName(), keyName);
248 BOOST_CHECK_EQUAL(encryptedNonce.getInitialVector().size(), 0);
249 BOOST_CHECK_EQUAL(encryptedNonce.getAlgorithmType(), type.type);
250
251 it++;
252 BOOST_CHECK(it != largeDataContent.elements_end());
253 Block payloadContent(*it);
254 BOOST_CHECK_EQUAL(payloadContent.type(), tlv::EncryptedContent);
255 EncryptedContent encryptedPayload(payloadContent);
256 Name nonceKeyName = keyName.append("nonce");
257 BOOST_CHECK_EQUAL(encryptedPayload.getKeyLocator().getName(), nonceKeyName);
258 BOOST_CHECK_EQUAL(encryptedPayload.getInitialVector().size(), 16);
259 BOOST_CHECK_EQUAL(encryptedPayload.getAlgorithmType(), tlv::AlgorithmAesCbc);
260
261 it++;
262 BOOST_CHECK(it == largeDataContent.elements_end());
263
264 const Buffer& bufferNonce = encryptedNonce.getPayload();
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700265 Buffer nonce =
266 Rsa::decrypt(dKey.data(), dKey.size(), bufferNonce.data(), bufferNonce.size());
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700267
268 encryptParams.setAlgorithmType(tlv::AlgorithmAesCbc);
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700269 encryptParams.setIV(encryptedPayload.getInitialVector().data(),
270 encryptedPayload.getInitialVector().size());
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700271 const Buffer& bufferPayload = encryptedPayload.getPayload();
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700272 Buffer largePayload =
273 Aes::decrypt(nonce.data(), nonce.size(), bufferPayload.data(), bufferPayload.size(), encryptParams);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700274
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700275 BOOST_CHECK_EQUAL_COLLECTIONS(large_content,
276 large_content + sizeof(large_content),
277 largePayload.begin(),
278 largePayload.end());
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700279}
280
281BOOST_AUTO_TEST_SUITE_END()
282
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700283} // namespace tests
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700284} // namespace algo
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400285} // namespace nac
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700286} // namespace ndn