Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2021 Regents of the University of California. |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/transform/block-cipher.hpp" |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
| 25 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
| 26 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 28 | #include "tests/boost-test.hpp" |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace security { |
| 32 | namespace transform { |
| 33 | namespace tests { |
| 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(Security) |
| 36 | BOOST_AUTO_TEST_SUITE(Transform) |
| 37 | BOOST_AUTO_TEST_SUITE(TestBlockCipher) |
| 38 | |
| 39 | BOOST_AUTO_TEST_CASE(AesCbc) |
| 40 | { |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 41 | const uint8_t key[] = { |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 42 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 43 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 44 | }; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 45 | const uint8_t iv[] = { |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 46 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 47 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 48 | }; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 49 | const uint8_t plainText[] = { |
| 50 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 51 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 52 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 53 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 54 | }; |
| 55 | // |
| 56 | // You can use the following shell one-liner to calculate the ciphertext: |
| 57 | // echo ${plaintext} | xxd -p -r | openssl enc -aes-128-cbc -K ${key} -iv ${iv} | xxd -i |
| 58 | // |
| 59 | const uint8_t cipherText[] = { |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 60 | 0x07, 0x4d, 0x32, 0x68, 0xc3, 0x40, 0x64, 0x43, |
| 61 | 0x1e, 0x66, 0x4c, 0x25, 0x66, 0x42, 0x0f, 0x59, |
| 62 | 0x0a, 0x51, 0x19, 0x07, 0x67, 0x5c, 0x0e, 0xfa, |
| 63 | 0xa6, 0x8c, 0xbb, 0xaf, 0xfd, 0xea, 0x47, 0xd4, |
| 64 | 0xc7, 0x2c, 0x12, 0x34, 0x79, 0xde, 0xec, 0xc8, |
| 65 | 0x75, 0x33, 0x8f, 0x6b, 0xd6, 0x55, 0xf3, 0xfa |
| 66 | }; |
| 67 | |
| 68 | // encrypt |
| 69 | OBufferStream os; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 70 | bufferSource(plainText) >> |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 71 | blockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT, |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 72 | key, sizeof(key), iv, sizeof(iv)) >> streamSink(os); |
| 73 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 74 | auto buf = os.buf(); |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL_COLLECTIONS(cipherText, cipherText + sizeof(cipherText), |
| 76 | buf->begin(), buf->end()); |
| 77 | |
| 78 | // decrypt |
| 79 | OBufferStream os2; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 80 | bufferSource(cipherText) >> |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 81 | blockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::DECRYPT, |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 82 | key, sizeof(key), iv, sizeof(iv)) >> streamSink(os2); |
| 83 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 84 | auto buf2 = os2.buf(); |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText), |
| 86 | buf2->begin(), buf2->end()); |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 87 | |
| 88 | // invalid key length |
| 89 | const uint8_t badKey[] = {0x00, 0x01, 0x02, 0x03}; |
| 90 | BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT, |
Davide Pesavento | eaa93f4 | 2017-09-17 00:21:00 -0400 | [diff] [blame] | 91 | badKey, sizeof(badKey), iv, sizeof(iv)), Error); |
| 92 | |
| 93 | // wrong iv length |
| 94 | const uint8_t badIv[] = {0x00, 0x01, 0x02, 0x03}; |
| 95 | BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT, |
| 96 | key, sizeof(key), badIv, sizeof(badIv)), Error); |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | BOOST_AUTO_TEST_CASE(InvalidAlgorithm) |
| 100 | { |
| 101 | BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::NONE, CipherOperator::ENCRYPT, |
| 102 | nullptr, 0, nullptr, 0), Error); |
Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_SUITE_END() // TestBlockCipher |
| 106 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 107 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 108 | |
| 109 | } // namespace tests |
| 110 | } // namespace transform |
| 111 | } // namespace security |
| 112 | } // namespace ndn |