blob: cd857c1b234aca0dbe83307ef8b4296cc550117c [file] [log] [blame]
Yingdi Yu87516612015-07-10 18:03:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento6158f472017-08-11 18:55:09 -04002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Yingdi Yu87516612015-07-10 18:03:52 -07004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/transform/block-cipher.hpp"
Davide Pesavento6158f472017-08-11 18:55:09 -040023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#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 Yu87516612015-07-10 18:03:52 -070027
Davide Pesavento7e780642018-11-24 15:51:34 -050028#include "tests/boost-test.hpp"
Yingdi Yu87516612015-07-10 18:03:52 -070029
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030namespace ndn::tests {
31
32using namespace ndn::security::transform;
Yingdi Yu87516612015-07-10 18:03:52 -070033
34BOOST_AUTO_TEST_SUITE(Security)
35BOOST_AUTO_TEST_SUITE(Transform)
36BOOST_AUTO_TEST_SUITE(TestBlockCipher)
37
38BOOST_AUTO_TEST_CASE(AesCbc)
39{
Davide Pesavento6158f472017-08-11 18:55:09 -040040 const uint8_t key[] = {
Yingdi Yu87516612015-07-10 18:03:52 -070041 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
42 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
43 };
Davide Pesavento6158f472017-08-11 18:55:09 -040044 const uint8_t iv[] = {
Yingdi Yu87516612015-07-10 18:03:52 -070045 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
46 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
47 };
Davide Pesavento6158f472017-08-11 18:55:09 -040048 const uint8_t plainText[] = {
49 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
50 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
51 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
52 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
53 };
54 //
55 // You can use the following shell one-liner to calculate the ciphertext:
56 // echo ${plaintext} | xxd -p -r | openssl enc -aes-128-cbc -K ${key} -iv ${iv} | xxd -i
57 //
58 const uint8_t cipherText[] = {
Yingdi Yu87516612015-07-10 18:03:52 -070059 0x07, 0x4d, 0x32, 0x68, 0xc3, 0x40, 0x64, 0x43,
60 0x1e, 0x66, 0x4c, 0x25, 0x66, 0x42, 0x0f, 0x59,
61 0x0a, 0x51, 0x19, 0x07, 0x67, 0x5c, 0x0e, 0xfa,
62 0xa6, 0x8c, 0xbb, 0xaf, 0xfd, 0xea, 0x47, 0xd4,
63 0xc7, 0x2c, 0x12, 0x34, 0x79, 0xde, 0xec, 0xc8,
64 0x75, 0x33, 0x8f, 0x6b, 0xd6, 0x55, 0xf3, 0xfa
65 };
66
67 // encrypt
68 OBufferStream os;
Davide Pesavento35c63792022-01-17 02:06:03 -050069 bufferSource(plainText)
70 >> blockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT, key, iv)
71 >> streamSink(os);
Yingdi Yu87516612015-07-10 18:03:52 -070072
Davide Pesavento6158f472017-08-11 18:55:09 -040073 auto buf = os.buf();
Yingdi Yu87516612015-07-10 18:03:52 -070074 BOOST_CHECK_EQUAL_COLLECTIONS(cipherText, cipherText + sizeof(cipherText),
75 buf->begin(), buf->end());
76
77 // decrypt
78 OBufferStream os2;
Davide Pesavento35c63792022-01-17 02:06:03 -050079 bufferSource(cipherText)
80 >> blockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::DECRYPT, key, iv)
81 >> streamSink(os2);
Yingdi Yu87516612015-07-10 18:03:52 -070082
Davide Pesavento6158f472017-08-11 18:55:09 -040083 auto buf2 = os2.buf();
Yingdi Yu87516612015-07-10 18:03:52 -070084 BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
85 buf2->begin(), buf2->end());
Davide Pesavento8aad3722017-09-16 20:57:28 -040086
87 // invalid key length
88 const uint8_t badKey[] = {0x00, 0x01, 0x02, 0x03};
Davide Pesavento35c63792022-01-17 02:06:03 -050089 BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT, badKey, iv),
90 Error);
Davide Pesaventoeaa93f42017-09-17 00:21:00 -040091
92 // wrong iv length
93 const uint8_t badIv[] = {0x00, 0x01, 0x02, 0x03};
Davide Pesavento35c63792022-01-17 02:06:03 -050094 BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT, key, badIv),
95 Error);
Davide Pesavento8aad3722017-09-16 20:57:28 -040096}
97
98BOOST_AUTO_TEST_CASE(InvalidAlgorithm)
99{
Davide Pesavento35c63792022-01-17 02:06:03 -0500100 BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::NONE, CipherOperator::DECRYPT, {}, {}), Error);
101 BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::NONE, CipherOperator::ENCRYPT, {}, {}), Error);
Yingdi Yu87516612015-07-10 18:03:52 -0700102}
103
104BOOST_AUTO_TEST_SUITE_END() // TestBlockCipher
105BOOST_AUTO_TEST_SUITE_END() // Transform
106BOOST_AUTO_TEST_SUITE_END() // Security
107
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400108} // namespace ndn::tests