Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 1 | /* -*- 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 "algo/aes.hpp" |
| 21 | |
| 22 | #include "boost-test.hpp" |
| 23 | #include <algorithm> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace gep { |
| 27 | namespace algo { |
| 28 | namespace tests { |
| 29 | |
| 30 | const uint8_t key[] = { |
| 31 | 0xdd, 0x60, 0x77, 0xec, 0xa9, 0x6b, 0x23, 0x1b, |
| 32 | 0x40, 0x6b, 0x5a, 0xf8, 0x7d, 0x3d, 0x55, 0x32 |
| 33 | }; |
| 34 | |
| 35 | const uint8_t plaintext[] = { // plaintext: AES-Encrypt-Test |
| 36 | 0x41, 0x45, 0x53, 0x2d, 0x45, 0x6e, 0x63, 0x72, |
| 37 | 0x79, 0x70, 0x74, 0x2d, 0x54, 0x65, 0x73, 0x74 |
| 38 | }; |
| 39 | |
| 40 | const uint8_t ciphertext_ecb[] = { |
| 41 | 0xcb, 0xe5, 0x6a, 0x80, 0x41, 0x24, 0x58, 0x23, |
| 42 | 0x84, 0x14, 0x15, 0x61, 0x80, 0xb9, 0x5e, 0xbd, |
| 43 | 0xce, 0x32, 0xb4, 0xbe, 0xbc, 0x91, 0x31, 0xd6, |
| 44 | 0x19, 0x00, 0x80, 0x8b, 0xfa, 0x00, 0x05, 0x9c |
| 45 | }; |
| 46 | |
| 47 | const uint8_t initvector[] = { |
| 48 | 0x6f, 0x53, 0x7a, 0x65, 0x58, 0x6c, 0x65, 0x75, |
| 49 | 0x44, 0x4c, 0x77, 0x35, 0x58, 0x63, 0x78, 0x6e |
| 50 | }; |
| 51 | |
| 52 | const uint8_t ciphertext_cbc_iv[] = { |
| 53 | 0xb7, 0x19, 0x5a, 0xbb, 0x23, 0xbf, 0x92, 0xb0, |
| 54 | 0x95, 0xae, 0x74, 0xe9, 0xad, 0x72, 0x7c, 0x28, |
| 55 | 0x6e, 0xc6, 0x73, 0xb5, 0x0b, 0x1a, 0x9e, 0xb9, |
| 56 | 0x4d, 0xc5, 0xbd, 0x8b, 0x47, 0x1f, 0x43, 0x00 |
| 57 | }; |
| 58 | |
| 59 | BOOST_AUTO_TEST_SUITE(TestAesAlgorithm) |
| 60 | |
| 61 | BOOST_AUTO_TEST_CASE(EncryptionDecryption) |
| 62 | { |
| 63 | RandomNumberGenerator rng; |
| 64 | AesKeyParams params; |
| 65 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 66 | EncryptParams eparams(tlv::AlgorithmAesEcb, 16); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 67 | |
| 68 | DecryptKey<Aes> decryptKey(std::move(Buffer(key, sizeof(key)))); |
| 69 | EncryptKey<Aes> encryptKey = Aes::deriveEncryptKey(decryptKey.getKeyBits()); |
| 70 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 71 | // check if loading key and key derivation |
| 72 | BOOST_CHECK_EQUAL_COLLECTIONS(encryptKey.getKeyBits().begin(), encryptKey.getKeyBits().end(), key, key + sizeof(key)); |
| 73 | BOOST_CHECK_EQUAL_COLLECTIONS(decryptKey.getKeyBits().begin(), decryptKey.getKeyBits().end(), key, key + sizeof(key)); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 74 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 75 | // encrypt data in AES_ECB |
| 76 | Buffer cipherBuf = Aes::encrypt(key, sizeof(key), plaintext, sizeof(plaintext), eparams); |
| 77 | BOOST_CHECK_EQUAL_COLLECTIONS(cipherBuf.begin(), cipherBuf.end(), |
| 78 | ciphertext_ecb, ciphertext_ecb + sizeof(ciphertext_ecb)); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 79 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 80 | // decrypt data in AES_ECB |
| 81 | Buffer recvBuf = Aes::decrypt(key, sizeof(key), cipherBuf.buf(), cipherBuf.size(), eparams); |
| 82 | BOOST_CHECK_EQUAL_COLLECTIONS(recvBuf.begin(), recvBuf.end(), |
| 83 | plaintext, plaintext + sizeof(plaintext)); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 84 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 85 | // encrypt/decrypt data in AES_CBC with auto-generated IV |
| 86 | eparams.setAlgorithmType(tlv::AlgorithmAesCbc); |
| 87 | cipherBuf = Aes::encrypt(key, sizeof(key), plaintext, sizeof(plaintext), eparams); |
| 88 | recvBuf = Aes::decrypt(key, sizeof(key), cipherBuf.buf(), cipherBuf.size(), eparams); |
| 89 | BOOST_CHECK_EQUAL_COLLECTIONS(recvBuf.begin(), recvBuf.end(), |
| 90 | plaintext, plaintext + sizeof(plaintext)); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 91 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 92 | // encrypt data in AES_CBC with specified IV |
| 93 | eparams.setIV(initvector, 16); |
| 94 | cipherBuf = Aes::encrypt(key, sizeof(key), plaintext, sizeof(plaintext), eparams); |
| 95 | BOOST_CHECK_EQUAL_COLLECTIONS(cipherBuf.begin(), cipherBuf.end(), |
| 96 | ciphertext_cbc_iv, ciphertext_cbc_iv + sizeof(ciphertext_cbc_iv)); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 97 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 98 | // decrypt data in AES_CBC with specified IV |
| 99 | recvBuf = Aes::decrypt(key, sizeof(key), cipherBuf.buf(), cipherBuf.size(), eparams); |
| 100 | BOOST_CHECK_EQUAL_COLLECTIONS(recvBuf.begin(), recvBuf.end(), |
| 101 | plaintext, plaintext + sizeof(plaintext)); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_SUITE_END() |
| 105 | |
| 106 | } // namespace tests |
| 107 | } // namespace algo |
| 108 | } // namespace gep |
| 109 | } // namespace ndn |