Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 5 | * 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 8 | * NAC is free software: you can redistribute it and/or modify it under the terms |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 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 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 12 | * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 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 |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 17 | * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "algo/aes.hpp" |
| 21 | |
| 22 | #include "boost-test.hpp" |
| 23 | #include <algorithm> |
| 24 | |
| 25 | namespace ndn { |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 26 | namespace nac { |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 27 | namespace algo { |
| 28 | namespace tests { |
| 29 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 30 | const uint8_t key[] = {0xdd, 0x60, 0x77, 0xec, 0xa9, 0x6b, 0x23, 0x1b, |
| 31 | 0x40, 0x6b, 0x5a, 0xf8, 0x7d, 0x3d, 0x55, 0x32}; |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 32 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 33 | // plaintext: AES-Encrypt-Test |
| 34 | const uint8_t plaintext[] = { 0x41, 0x45, 0x53, 0x2d, 0x45, 0x6e, 0x63, 0x72, |
| 35 | 0x79, 0x70, 0x74, 0x2d, 0x54, 0x65, 0x73, 0x74}; |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 36 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 37 | // 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 40 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 41 | const uint8_t initvector[] = {0x6f, 0x53, 0x7a, 0x65, 0x58, 0x6c, 0x65, 0x75, |
| 42 | 0x44, 0x4c, 0x77, 0x35, 0x58, 0x63, 0x78, 0x6e}; |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 43 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 44 | const 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 48 | |
| 49 | BOOST_AUTO_TEST_SUITE(TestAesAlgorithm) |
| 50 | |
| 51 | BOOST_AUTO_TEST_CASE(EncryptionDecryption) |
| 52 | { |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 53 | AesKeyParams params; |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 54 | EncryptParams eparams(tlv::AlgorithmAesEcb, 16); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | 867228e | 2016-10-17 16:54:55 -0700 | [diff] [blame] | 56 | DecryptKey<Aes> decryptKey(Buffer(key, sizeof(key))); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 57 | EncryptKey<Aes> encryptKey = Aes::deriveEncryptKey(decryptKey.getKeyBits()); |
| 58 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 59 | // check if loading key and key derivation |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 60 | 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 66 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 67 | // Comment out the test of AES_ECB because NAC no longer supports CBC. |
| 68 | // Compared with ECB, CBC is more secure. |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 69 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 70 | // // 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 83 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 84 | // encrypt/decrypt data in AES_CBC with auto-generated IV |
| 85 | eparams.setAlgorithmType(tlv::AlgorithmAesCbc); |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 86 | 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 Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL_COLLECTIONS(recvBuf.begin(), recvBuf.end(), |
| 89 | plaintext, plaintext + sizeof(plaintext)); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 90 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 91 | // 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 96 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 97 | // decrypt data in AES_CBC with specified IV |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 98 | recvBuf = Aes::decrypt(key, sizeof(key), cipherBuf.data(), cipherBuf.size(), eparams); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL_COLLECTIONS(recvBuf.begin(), recvBuf.end(), |
| 100 | plaintext, plaintext + sizeof(plaintext)); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | BOOST_AUTO_TEST_SUITE_END() |
| 104 | |
| 105 | } // namespace tests |
| 106 | } // namespace algo |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 107 | } // namespace nac |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 108 | } // namespace ndn |