blob: 53df79696f502a02c718d29cf2531b0bb5fcd1f2 [file] [log] [blame]
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -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 Swaminathanc61cf192015-06-30 21:21:33 -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 Swaminathanc61cf192015-06-30 21:21:33 -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 Swaminathanc61cf192015-06-30 21:21:33 -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 Swaminathanc61cf192015-06-30 21:21:33 -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 Swaminathanc61cf192015-06-30 21:21:33 -070018 */
19
20#include "algo/aes.hpp"
21
22#include "boost-test.hpp"
23#include <algorithm>
24
25namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040026namespace nac {
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070027namespace algo {
28namespace tests {
29
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070030const uint8_t key[] = {0xdd, 0x60, 0x77, 0xec, 0xa9, 0x6b, 0x23, 0x1b,
31 0x40, 0x6b, 0x5a, 0xf8, 0x7d, 0x3d, 0x55, 0x32};
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070032
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070033// plaintext: AES-Encrypt-Test
34const uint8_t plaintext[] = { 0x41, 0x45, 0x53, 0x2d, 0x45, 0x6e, 0x63, 0x72,
35 0x79, 0x70, 0x74, 0x2d, 0x54, 0x65, 0x73, 0x74};
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070036
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070037// const uint8_t ciphertext_ecb[] = {0xcb, 0xe5, 0x6a, 0x80, 0x41, 0x24, 0x58, 0x23, 0x84, 0x14, 0x15,
38// 0x61, 0x80, 0xb9, 0x5e, 0xbd, 0xce, 0x32, 0xb4, 0xbe, 0xbc, 0x91,
39// 0x31, 0xd6, 0x19, 0x00, 0x80, 0x8b, 0xfa, 0x00, 0x05, 0x9c};
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070040
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070041const uint8_t initvector[] = {0x6f, 0x53, 0x7a, 0x65, 0x58, 0x6c, 0x65, 0x75,
42 0x44, 0x4c, 0x77, 0x35, 0x58, 0x63, 0x78, 0x6e};
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070043
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070044const uint8_t ciphertext_cbc_iv[] = {0xb7, 0x19, 0x5a, 0xbb, 0x23, 0xbf, 0x92, 0xb0,
45 0x95, 0xae, 0x74, 0xe9, 0xad, 0x72, 0x7c, 0x28,
46 0x6e, 0xc6, 0x73, 0xb5, 0x0b, 0x1a, 0x9e, 0xb9,
47 0x4d, 0xc5, 0xbd, 0x8b, 0x47, 0x1f, 0x43, 0x00};
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070048
49BOOST_AUTO_TEST_SUITE(TestAesAlgorithm)
50
51BOOST_AUTO_TEST_CASE(EncryptionDecryption)
52{
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070053 AesKeyParams params;
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070054 EncryptParams eparams(tlv::AlgorithmAesEcb, 16);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070055
Alexander Afanasyev867228e2016-10-17 16:54:55 -070056 DecryptKey<Aes> decryptKey(Buffer(key, sizeof(key)));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070057 EncryptKey<Aes> encryptKey = Aes::deriveEncryptKey(decryptKey.getKeyBits());
58
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070059 // check if loading key and key derivation
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070060 BOOST_CHECK_EQUAL_COLLECTIONS(encryptKey.getKeyBits().begin(),
61 encryptKey.getKeyBits().end(),
62 key, key + sizeof(key));
63 BOOST_CHECK_EQUAL_COLLECTIONS(decryptKey.getKeyBits().begin(),
64 decryptKey.getKeyBits().end(),
65 key, key + sizeof(key));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070066
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070067 // Comment out the test of AES_ECB because NAC no longer supports CBC.
68 // Compared with ECB, CBC is more secure.
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070069
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070070 // // encrypt data in AES_ECB
71 // Buffer cipherBuf = Aes::encrypt(key, sizeof(key), plaintext, sizeof(plaintext), eparams);
72 // BOOST_CHECK_EQUAL_COLLECTIONS(cipherBuf.begin(),
73 // cipherBuf.end(),
74 // ciphertext_ecb,
75 // ciphertext_ecb + sizeof(ciphertext_ecb));
76
77 // // decrypt data in AES_ECB
78 // Buffer recvBuf = Aes::decrypt(key, sizeof(key), cipherBuf.data(), cipherBuf.size(), eparams);
79 // BOOST_CHECK_EQUAL_COLLECTIONS(recvBuf.begin(),
80 // recvBuf.end(),
81 // plaintext,
82 // plaintext + sizeof(plaintext));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070083
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070084 // encrypt/decrypt data in AES_CBC with auto-generated IV
85 eparams.setAlgorithmType(tlv::AlgorithmAesCbc);
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070086 Buffer cipherBuf = Aes::encrypt(key, sizeof(key), plaintext, sizeof(plaintext), eparams);
87 Buffer recvBuf = Aes::decrypt(key, sizeof(key), cipherBuf.data(), cipherBuf.size(), eparams);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070088 BOOST_CHECK_EQUAL_COLLECTIONS(recvBuf.begin(), recvBuf.end(),
89 plaintext, plaintext + sizeof(plaintext));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070090
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070091 // encrypt data in AES_CBC with specified IV
92 eparams.setIV(initvector, 16);
93 cipherBuf = Aes::encrypt(key, sizeof(key), plaintext, sizeof(plaintext), eparams);
94 BOOST_CHECK_EQUAL_COLLECTIONS(cipherBuf.begin(), cipherBuf.end(),
95 ciphertext_cbc_iv, ciphertext_cbc_iv + sizeof(ciphertext_cbc_iv));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070096
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070097 // decrypt data in AES_CBC with specified IV
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070098 recvBuf = Aes::decrypt(key, sizeof(key), cipherBuf.data(), cipherBuf.size(), eparams);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070099 BOOST_CHECK_EQUAL_COLLECTIONS(recvBuf.begin(), recvBuf.end(),
100 plaintext, plaintext + sizeof(plaintext));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700101}
102
103BOOST_AUTO_TEST_SUITE_END()
104
105} // namespace tests
106} // namespace algo
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400107} // namespace nac
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700108} // namespace ndn