Yingdi Yu | 8751661 | 2015-07-10 18:03:52 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
| 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 | |
| 22 | #include "security/transform/block-cipher.hpp" |
| 23 | #include "security/transform/buffer-source.hpp" |
| 24 | #include "security/transform/stream-sink.hpp" |
| 25 | #include "encoding/buffer-stream.hpp" |
| 26 | #include <iostream> |
| 27 | |
| 28 | #include "boost-test.hpp" |
| 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 | { |
| 41 | uint8_t key[] = { |
| 42 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 43 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 44 | }; |
| 45 | |
| 46 | uint8_t plainText[] = { |
| 47 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 48 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 49 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 50 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 51 | }; |
| 52 | |
| 53 | uint8_t iv[] = { |
| 54 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 55 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * Cipher text can be generated using: |
| 60 | * |
| 61 | * { |
| 62 | * using namespace CryptoPP; |
| 63 | * CBC_Mode<AES>::Encryption aes(key, sizeof(key), iv); |
| 64 | * StringSource(plainText, sizeof(plainText), true, |
| 65 | * new StreamTransformationFilter(aes, |
| 66 | * new HexEncoder(new FileSink(std::cerr), false))); |
| 67 | * } |
| 68 | */ |
| 69 | uint8_t cipherText[] = { |
| 70 | 0x07, 0x4d, 0x32, 0x68, 0xc3, 0x40, 0x64, 0x43, |
| 71 | 0x1e, 0x66, 0x4c, 0x25, 0x66, 0x42, 0x0f, 0x59, |
| 72 | 0x0a, 0x51, 0x19, 0x07, 0x67, 0x5c, 0x0e, 0xfa, |
| 73 | 0xa6, 0x8c, 0xbb, 0xaf, 0xfd, 0xea, 0x47, 0xd4, |
| 74 | 0xc7, 0x2c, 0x12, 0x34, 0x79, 0xde, 0xec, 0xc8, |
| 75 | 0x75, 0x33, 0x8f, 0x6b, 0xd6, 0x55, 0xf3, 0xfa |
| 76 | }; |
| 77 | |
| 78 | // encrypt |
| 79 | OBufferStream os; |
| 80 | bufferSource(plainText, sizeof(plainText)) >> |
| 81 | blockCipher(BlockCipherAlgorithm::AES_CBC, |
| 82 | CipherOperator::ENCRYPT, |
| 83 | key, sizeof(key), iv, sizeof(iv)) >> streamSink(os); |
| 84 | |
| 85 | ConstBufferPtr buf = os.buf(); |
| 86 | BOOST_CHECK_EQUAL_COLLECTIONS(cipherText, cipherText + sizeof(cipherText), |
| 87 | buf->begin(), buf->end()); |
| 88 | |
| 89 | // decrypt |
| 90 | OBufferStream os2; |
| 91 | bufferSource(cipherText, sizeof(cipherText)) >> |
| 92 | blockCipher(BlockCipherAlgorithm::AES_CBC, |
| 93 | CipherOperator::DECRYPT, |
| 94 | key, sizeof(key), iv, sizeof(iv)) >> streamSink(os2); |
| 95 | |
| 96 | ConstBufferPtr buf2 = os2.buf(); |
| 97 | BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText), |
| 98 | buf2->begin(), buf2->end()); |
| 99 | } |
| 100 | |
| 101 | BOOST_AUTO_TEST_SUITE_END() // TestBlockCipher |
| 102 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 103 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 104 | |
| 105 | } // namespace tests |
| 106 | } // namespace transform |
| 107 | } // namespace security |
| 108 | } // namespace ndn |