blob: 6b7bbcbd0afa3bb1422b7e6f8e77ec26e19d4391 [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 *
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
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 {
31namespace gep {
32namespace 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
73} // namespace gep
74} // namespace ndn