Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 5 | * 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 8 | * NAC is free software: you can redistribute it and/or modify it under the terms |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 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 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 12 | * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 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 |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 17 | * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 20 | #include "algo/rsa.hpp" |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 21 | #include "algo/encrypt-params.hpp" |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 22 | #include "boost-test.hpp" |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 23 | #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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | #include <string> |
| 29 | |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 30 | namespace ndn { |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 31 | namespace nac { |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 32 | namespace algo { |
| 33 | namespace tests { |
| 34 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 35 | // plaintext: RSA-Encrypt-Test |
| 36 | const uint8_t plainText[] = { 0x52, 0x53, 0x41, 0x2d, 0x45, 0x6e, 0x63, 0x72, |
| 37 | 0x79, 0x70, 0x74, 0x2d, 0x54, 0x65, 0x73, 0x74}; |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 38 | |
| 39 | BOOST_AUTO_TEST_SUITE(TestRsaAlgorithm) |
| 40 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 41 | BOOST_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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 55 | BOOST_AUTO_TEST_CASE(EncryptionDecryption) |
| 56 | { |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 57 | RsaKeyParams params; |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 58 | DecryptKey<Rsa> sKey = Rsa::generateKey(params); |
| 59 | EncryptKey<Rsa> pKey = Rsa::deriveEncryptKey(sKey.getKeyBits()); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 60 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 61 | 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | BOOST_AUTO_TEST_SUITE_END() |
| 70 | |
| 71 | } // namespace tests |
| 72 | } // namespace algo |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 73 | } // namespace nac |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 74 | } // namespace ndn |