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 gep (Group-based Encryption Protocol for NDN). |
| 6 | * See AUTHORS.md for complete list of gep authors and contributors. |
| 7 | * |
| 8 | * gep 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 | * gep 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 | * gep, 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 "rsa.hpp" |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 22 | #include "error.hpp" |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | namespace gep { |
| 26 | namespace algo { |
| 27 | |
| 28 | using namespace CryptoPP; |
| 29 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 30 | static Buffer |
| 31 | transform(SimpleProxyFilter* filter, const uint8_t* data, size_t dataLen) |
| 32 | { |
| 33 | OBufferStream obuf; |
| 34 | filter->Attach(new FileSink(obuf)); |
| 35 | |
| 36 | StringSource pipe(data, dataLen, true, filter); |
| 37 | return *(obuf.buf()); |
| 38 | } |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 39 | |
| 40 | DecryptKey<Rsa> |
| 41 | Rsa::generateKey(RandomNumberGenerator& rng, RsaKeyParams& params) |
| 42 | { |
| 43 | RSA::PrivateKey privateKey; |
| 44 | privateKey.GenerateRandomWithKeySize(rng, params.getKeySize()); |
| 45 | |
| 46 | OBufferStream obuf; |
| 47 | privateKey.Save(FileSink(obuf).Ref()); |
| 48 | |
| 49 | DecryptKey<Rsa> decryptKey(std::move(*obuf.buf())); |
| 50 | return decryptKey; |
| 51 | } |
| 52 | |
| 53 | EncryptKey<Rsa> |
| 54 | Rsa::deriveEncryptKey(const Buffer& keyBits) |
| 55 | { |
| 56 | RSA::PrivateKey privateKey; |
| 57 | |
| 58 | ByteQueue keyQueue; |
| 59 | keyQueue.LazyPut(keyBits.get(), keyBits.size()); |
| 60 | privateKey.Load(keyQueue); |
| 61 | |
| 62 | RSA::PublicKey publicKey(privateKey); |
| 63 | |
| 64 | OBufferStream obuf; |
| 65 | publicKey.Save(FileSink(obuf).Ref()); |
| 66 | |
| 67 | EncryptKey<Rsa> encryptKey(std::move(*obuf.buf())); |
| 68 | return encryptKey; |
| 69 | } |
| 70 | |
| 71 | Buffer |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 72 | Rsa::decrypt(const uint8_t* key, size_t keyLen, |
| 73 | const uint8_t* payload, size_t payloadLen, |
| 74 | const EncryptParams& params) |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 75 | { |
| 76 | AutoSeededRandomPool rng; |
| 77 | RSA::PrivateKey privateKey; |
| 78 | |
| 79 | ByteQueue keyQueue; |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 80 | keyQueue.LazyPut(key, keyLen); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 81 | privateKey.Load(keyQueue); |
| 82 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 83 | switch (params.getAlgorithmType()) { |
| 84 | case tlv::AlgorithmRsaPkcs: { |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 85 | RSAES_PKCS1v15_Decryptor decryptor_pkcs1v15(privateKey); |
| 86 | PK_DecryptorFilter* filter_pkcs1v15 = new PK_DecryptorFilter(rng, decryptor_pkcs1v15); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 87 | return transform(filter_pkcs1v15, payload, payloadLen); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 88 | } |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 89 | case tlv::AlgorithmRsaOaep: { |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 90 | RSAES_OAEP_SHA_Decryptor decryptor_oaep_sha(privateKey); |
| 91 | PK_DecryptorFilter* filter_oaep_sha = new PK_DecryptorFilter(rng, decryptor_oaep_sha); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 92 | return transform(filter_oaep_sha, payload, payloadLen); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 93 | } |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 94 | default: |
| 95 | throw Error("unsupported padding scheme"); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | Buffer |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 100 | Rsa::encrypt(const uint8_t* key, size_t keyLen, |
| 101 | const uint8_t* payload, size_t payloadLen, |
| 102 | const EncryptParams& params) |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 103 | { |
| 104 | AutoSeededRandomPool rng; |
| 105 | RSA::PublicKey publicKey; |
| 106 | |
| 107 | ByteQueue keyQueue; |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 108 | keyQueue.LazyPut(key, keyLen); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 109 | publicKey.Load(keyQueue); |
| 110 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 111 | switch (params.getAlgorithmType()) { |
| 112 | case tlv::AlgorithmRsaPkcs: { |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 113 | RSAES_PKCS1v15_Encryptor encryptor_pkcs1v15(publicKey); |
| 114 | PK_EncryptorFilter* filter_pkcs1v15 = new PK_EncryptorFilter(rng, encryptor_pkcs1v15); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 115 | return transform(filter_pkcs1v15, payload, payloadLen); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 116 | } |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 117 | case tlv::AlgorithmRsaOaep: { |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 118 | RSAES_OAEP_SHA_Encryptor encryptor_oaep_sha(publicKey); |
| 119 | PK_EncryptorFilter* filter_oaep_sha = new PK_EncryptorFilter(rng, encryptor_oaep_sha); |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 120 | return transform(filter_oaep_sha, payload, payloadLen); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 121 | } |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 122 | default: |
| 123 | throw Error("unsupported padding scheme"); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 127 | } // namespace algo |
| 128 | } // namespace gep |
| 129 | } // namespace ndn |