blob: f5fe848e245d1e9f305476bef9e1b3e35a25751d [file] [log] [blame]
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07001/* -*- 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 Swaminathand5b3eae2015-07-09 15:37:05 -070022#include "error.hpp"
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070023
24namespace ndn {
25namespace gep {
26namespace algo {
27
28using namespace CryptoPP;
29
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070030static Buffer
31transform(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 Swaminathanc61cf192015-06-30 21:21:33 -070039
40DecryptKey<Rsa>
41Rsa::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
53EncryptKey<Rsa>
54Rsa::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
71Buffer
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070072Rsa::decrypt(const uint8_t* key, size_t keyLen,
73 const uint8_t* payload, size_t payloadLen,
74 const EncryptParams& params)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070075{
76 AutoSeededRandomPool rng;
77 RSA::PrivateKey privateKey;
78
79 ByteQueue keyQueue;
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070080 keyQueue.LazyPut(key, keyLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070081 privateKey.Load(keyQueue);
82
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070083 switch (params.getAlgorithmType()) {
84 case tlv::AlgorithmRsaPkcs: {
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070085 RSAES_PKCS1v15_Decryptor decryptor_pkcs1v15(privateKey);
86 PK_DecryptorFilter* filter_pkcs1v15 = new PK_DecryptorFilter(rng, decryptor_pkcs1v15);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070087 return transform(filter_pkcs1v15, payload, payloadLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070088 }
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070089 case tlv::AlgorithmRsaOaep: {
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070090 RSAES_OAEP_SHA_Decryptor decryptor_oaep_sha(privateKey);
91 PK_DecryptorFilter* filter_oaep_sha = new PK_DecryptorFilter(rng, decryptor_oaep_sha);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070092 return transform(filter_oaep_sha, payload, payloadLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070093 }
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070094 default:
95 throw Error("unsupported padding scheme");
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070096 }
97}
98
99Buffer
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700100Rsa::encrypt(const uint8_t* key, size_t keyLen,
101 const uint8_t* payload, size_t payloadLen,
102 const EncryptParams& params)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700103{
104 AutoSeededRandomPool rng;
105 RSA::PublicKey publicKey;
106
107 ByteQueue keyQueue;
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700108 keyQueue.LazyPut(key, keyLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700109 publicKey.Load(keyQueue);
110
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700111 switch (params.getAlgorithmType()) {
112 case tlv::AlgorithmRsaPkcs: {
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700113 RSAES_PKCS1v15_Encryptor encryptor_pkcs1v15(publicKey);
114 PK_EncryptorFilter* filter_pkcs1v15 = new PK_EncryptorFilter(rng, encryptor_pkcs1v15);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700115 return transform(filter_pkcs1v15, payload, payloadLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700116 }
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700117 case tlv::AlgorithmRsaOaep: {
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700118 RSAES_OAEP_SHA_Encryptor encryptor_oaep_sha(publicKey);
119 PK_EncryptorFilter* filter_oaep_sha = new PK_EncryptorFilter(rng, encryptor_oaep_sha);
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700120 return transform(filter_oaep_sha, payload, payloadLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700121 }
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -0700122 default:
123 throw Error("unsupported padding scheme");
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700124 }
125}
126
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700127} // namespace algo
128} // namespace gep
129} // namespace ndn