blob: ac259659dc83e8a11fd6dca58c1d457c2a26d765 [file] [log] [blame]
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07004 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04005 * 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 Swaminathanc61cf192015-06-30 21:21:33 -07007 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04008 * NAC is free software: you can redistribute it and/or modify it under the terms
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07009 * 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 Afanasyev9091d832018-04-18 17:21:08 -040012 * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070013 * 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 Afanasyev9091d832018-04-18 17:21:08 -040017 * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070018 */
19
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070020#include "algo/rsa.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070021#include "algo/encrypt-params.hpp"
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070022#include "boost-test.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070023#include <ndn-cxx/security/key-params.hpp>
24#include <ndn-cxx/security/transform/private-key.hpp>
25#include <ndn-cxx/security/transform/public-key.hpp>
26#include <ndn-cxx/encoding/buffer-stream.hpp>
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070027#include <algorithm>
28#include <string>
29
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070030namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040031namespace nac {
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070032namespace algo {
33namespace tests {
34
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070035// plaintext: RSA-Encrypt-Test
36const uint8_t plainText[] = { 0x52, 0x53, 0x41, 0x2d, 0x45, 0x6e, 0x63, 0x72,
37 0x79, 0x70, 0x74, 0x2d, 0x54, 0x65, 0x73, 0x74};
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070038
39BOOST_AUTO_TEST_SUITE(TestRsaAlgorithm)
40
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070041BOOST_AUTO_TEST_CASE(TransformEncryptionDecryption)
42{
43 RsaKeyParams params;
44 auto sKey = security::transform::generatePrivateKey(params);
45 security::transform::PublicKey pKey;
46 ConstBufferPtr pKeyBits = sKey->derivePublicKey();
47 pKey.loadPkcs8(pKeyBits->data(), pKeyBits->size());
48
49 auto cipherText = pKey.encrypt(plainText, sizeof(plainText));
50 auto decrypted = sKey->decrypt(cipherText->data(), cipherText->size());
51 BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
52 decrypted->begin(), decrypted->end());
53}
54
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070055BOOST_AUTO_TEST_CASE(EncryptionDecryption)
56{
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070057 RsaKeyParams params;
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070058 DecryptKey<Rsa> sKey = Rsa::generateKey(params);
59 EncryptKey<Rsa> pKey = Rsa::deriveEncryptKey(sKey.getKeyBits());
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070060
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070061 auto cipherText = Rsa::encrypt(pKey.getKeyBits().data(), pKey.getKeyBits().size(),
62 plainText, sizeof(plainText));
63 auto decrypted = Rsa::decrypt(sKey.getKeyBits().data(), sKey.getKeyBits().size(),
64 cipherText.data(), cipherText.size());
65 BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
66 decrypted.begin(), decrypted.end());
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070067}
68
69BOOST_AUTO_TEST_SUITE_END()
70
71} // namespace tests
72} // namespace algo
Alexander Afanasyev9091d832018-04-18 17:21:08 -040073} // namespace nac
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070074} // namespace ndn