Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California |
| 4 | * |
| 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 | |
| 20 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
| 21 | #include "algo/encrypt-params.hpp" |
| 22 | #include "algo/rsa.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | #include <algorithm> |
| 26 | #include <string> |
| 27 | |
| 28 | using namespace CryptoPP; |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace gep { |
| 32 | namespace algo { |
| 33 | namespace tests { |
| 34 | |
| 35 | const std::string privateKey = { |
| 36 | "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMLY2w1PmsuZNvZ4" |
| 37 | "rJs1pESLrxF1Xlk9Zg4Sc0r2HIEn/eme8f7cOxXq8OtxIjowEfjceHGvfc7YG1Nw" |
| 38 | "LDh+ka4Jh6QtYqPEL9GHfrBeufynd0g2PAPVXySBvOJr/Isk+4/Fsj5ihrIPgrQ5" |
| 39 | "wTBBuLYjDgwPppC/+vddsr5wu5bbAgMBAAECgYBYmRLB8riIa5q6aBTUXofbQ0jP" |
| 40 | "v3avTWPicjFKnK5JbE3gtQ2Evc+AH9x8smzF2KXTayy5RPsH2uxR/GefKK5EkWbB" |
| 41 | "mLwWDJ5/QPlLK1STxPs8B/89mp8sZkZ1AxnSHhV/a3dRcK1rVamVcqPMdFyM5PfX" |
| 42 | "/apL3MlL6bsq2FipAQJBAOp7EJuEs/qAjh8hgyV2acLdsokUEwXH4gCK6+KQW8XS" |
| 43 | "xFWAG4IbbLfq1HwEpHC2hJSzifCQGoPAxYBRgSK+h6sCQQDUuqF04o06+Qpe4A/W" |
| 44 | "pWCBGE33+CD4lBtaeoIagsAs/lgcFmXiJZ4+4PhyIORmwFgql9ZDFHSpl8rAYsfk" |
| 45 | "dz2RAkEAtUKpFe/BybYzJ3Galg0xuMf0ye7QvblExjKeIqiBqS1DRO0hVrSomIxZ" |
| 46 | "8f0MuWz+lI0t5t8fABa3FnjrINa0vQJBAJeZKNaTXPJZ5/oU0zS0RkG5gFbmjRiY" |
| 47 | "86VXCMC7zRhDaacajyDKjithR6yNpDdVe39fFWJYgYsakXLo8mruTwECQGqywoy9" |
| 48 | "epf1flKx4YCCrw+qRKmbkcXWcpFV32EG2K2D1GsxkuXv/b3qO67Uxx1Arxp9o8dl" |
| 49 | "k34WfzApRjNjho0=" |
| 50 | }; |
| 51 | |
| 52 | const std::string publicKey = { |
| 53 | "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDC2NsNT5rLmTb2eKybNaREi68R" |
| 54 | "dV5ZPWYOEnNK9hyBJ/3pnvH+3DsV6vDrcSI6MBH43Hhxr33O2BtTcCw4fpGuCYek" |
| 55 | "LWKjxC/Rh36wXrn8p3dINjwD1V8kgbzia/yLJPuPxbI+YoayD4K0OcEwQbi2Iw4M" |
| 56 | "D6aQv/r3XbK+cLuW2wIDAQAB" |
| 57 | }; |
| 58 | |
| 59 | const uint8_t plaintext[] = { // plaintext: RSA-Encrypt-Test |
| 60 | 0x52, 0x53, 0x41, 0x2d, 0x45, 0x6e, 0x63, 0x72, |
| 61 | 0x79, 0x70, 0x74, 0x2d, 0x54, 0x65, 0x73, 0x74 |
| 62 | }; |
| 63 | |
| 64 | const uint8_t ciphertext[] = { |
| 65 | 0x33, 0xfb, 0x32, 0xd4, 0x2d, 0x45, 0x75, 0x3f, 0x34, 0xde, 0x3b, |
| 66 | 0xaa, 0x80, 0x5f, 0x74, 0x6f, 0xf0, 0x3f, 0x01, 0x31, 0xdd, 0x2b, |
| 67 | 0x85, 0x02, 0x1b, 0xed, 0x2d, 0x16, 0x1b, 0x96, 0xe5, 0x77, 0xde, |
| 68 | 0xcd, 0x44, 0xe5, 0x3c, 0x32, 0xb6, 0x9a, 0xa9, 0x5d, 0xaa, 0x4b, |
| 69 | 0x94, 0xe2, 0xac, 0x4a, 0x4e, 0xf5, 0x35, 0x21, 0xd0, 0x03, 0x4a, |
| 70 | 0xa7, 0x53, 0xae, 0x13, 0x08, 0x63, 0x38, 0x2c, 0x92, 0xe3, 0x44, |
| 71 | 0x64, 0xbf, 0x33, 0x84, 0x8e, 0x51, 0x9d, 0xb9, 0x85, 0x83, 0xf6, |
| 72 | 0x8e, 0x09, 0xc1, 0x72, 0xb9, 0x90, 0x5d, 0x48, 0x63, 0xec, 0xd0, |
| 73 | 0xcc, 0xfa, 0xab, 0x44, 0x2b, 0xaa, 0xa6, 0xb6, 0xca, 0xec, 0x2b, |
| 74 | 0x5f, 0xbe, 0x77, 0xa5, 0x52, 0xeb, 0x0a, 0xaa, 0xf2, 0x2a, 0x19, |
| 75 | 0x62, 0x80, 0x14, 0x87, 0x42, 0x35, 0xd0, 0xb6, 0xa3, 0x47, 0x4e, |
| 76 | 0xb6, 0x1a, 0x88, 0xa3, 0x16, 0xb2, 0x19 |
| 77 | }; |
| 78 | |
| 79 | BOOST_AUTO_TEST_SUITE(TestRsaAlgorithm) |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(EncryptionDecryption) |
| 82 | { |
| 83 | RandomNumberGenerator rng; |
| 84 | RsaKeyParams params; |
| 85 | EncryptParams eparams(ENCRYPT_MODE_RSA, PADDING_SCHEME_OAEP_SHA, 0); |
| 86 | |
| 87 | OBufferStream privateKeyBuffer, publicKeyBuffer; |
| 88 | StringSource privPipe(privateKey, true, |
| 89 | new Base64Decoder(new FileSink(privateKeyBuffer))); |
| 90 | StringSource publPipe(publicKey, true, |
| 91 | new Base64Decoder(new FileSink(publicKeyBuffer))); |
| 92 | |
| 93 | DecryptKey<Rsa> decryptKey(std::move(*(privateKeyBuffer.buf()))); |
| 94 | EncryptKey<Rsa> encryptKey = Rsa::deriveEncryptKey(decryptKey.getKeyBits()); |
| 95 | |
| 96 | Buffer encodedPublic = *(publicKeyBuffer.buf()); |
| 97 | Buffer derivedPublicKey = encryptKey.getKeyBits(); |
| 98 | |
| 99 | BOOST_CHECK_EQUAL_COLLECTIONS(encodedPublic.begin(), |
| 100 | encodedPublic.end(), |
| 101 | derivedPublicKey.begin(), |
| 102 | derivedPublicKey.end()); |
| 103 | |
| 104 | Buffer plainBuf(plaintext, sizeof(plaintext)); |
| 105 | Buffer encryptBuf = Rsa::encrypt(encryptKey.getKeyBits(), plainBuf, eparams); |
| 106 | Buffer recvBuf = Rsa::decrypt(decryptKey.getKeyBits(), encryptBuf, eparams); |
| 107 | |
| 108 | BOOST_CHECK_EQUAL_COLLECTIONS(plaintext, |
| 109 | plaintext + sizeof(plaintext), |
| 110 | recvBuf.begin(), |
| 111 | recvBuf.end()); |
| 112 | |
| 113 | Buffer cipherBuf(ciphertext, sizeof(ciphertext)); |
| 114 | Buffer convBuf = Rsa::decrypt(decryptKey.getKeyBits(), cipherBuf, eparams); |
| 115 | |
| 116 | BOOST_CHECK_EQUAL_COLLECTIONS(plaintext, |
| 117 | plaintext + sizeof(plaintext), |
| 118 | convBuf.begin(), |
| 119 | convBuf.end()); |
| 120 | } |
| 121 | |
| 122 | BOOST_AUTO_TEST_SUITE_END() |
| 123 | |
| 124 | } // namespace tests |
| 125 | } // namespace algo |
| 126 | } // namespace gep |
| 127 | } // namespace ndn |