blob: 66361fb493c724cb71c0e2a7f87711afe8da1528 [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"
22
23namespace ndn {
24namespace gep {
25namespace algo {
26
27using namespace CryptoPP;
28
29Buffer
30crypt(SimpleProxyFilter* filter, const Buffer& data);
31
32DecryptKey<Rsa>
33Rsa::generateKey(RandomNumberGenerator& rng, RsaKeyParams& params)
34{
35 RSA::PrivateKey privateKey;
36 privateKey.GenerateRandomWithKeySize(rng, params.getKeySize());
37
38 OBufferStream obuf;
39 privateKey.Save(FileSink(obuf).Ref());
40
41 DecryptKey<Rsa> decryptKey(std::move(*obuf.buf()));
42 return decryptKey;
43}
44
45EncryptKey<Rsa>
46Rsa::deriveEncryptKey(const Buffer& keyBits)
47{
48 RSA::PrivateKey privateKey;
49
50 ByteQueue keyQueue;
51 keyQueue.LazyPut(keyBits.get(), keyBits.size());
52 privateKey.Load(keyQueue);
53
54 RSA::PublicKey publicKey(privateKey);
55
56 OBufferStream obuf;
57 publicKey.Save(FileSink(obuf).Ref());
58
59 EncryptKey<Rsa> encryptKey(std::move(*obuf.buf()));
60 return encryptKey;
61}
62
63Buffer
64Rsa::decrypt(const Buffer& keyBits, const Buffer& encryptedData, const EncryptParams& params)
65{
66 AutoSeededRandomPool rng;
67 RSA::PrivateKey privateKey;
68
69 ByteQueue keyQueue;
70 keyQueue.LazyPut(keyBits.data(), keyBits.size());
71 privateKey.Load(keyQueue);
72
73 switch (params.getPaddingScheme()) {
74 case PADDING_SCHEME_PKCS1v15:
75 {
76 RSAES_PKCS1v15_Decryptor decryptor_pkcs1v15(privateKey);
77 PK_DecryptorFilter* filter_pkcs1v15 = new PK_DecryptorFilter(rng, decryptor_pkcs1v15);
78 return crypt(filter_pkcs1v15, encryptedData);
79 }
80
81 case PADDING_SCHEME_OAEP_SHA:
82 {
83 RSAES_OAEP_SHA_Decryptor decryptor_oaep_sha(privateKey);
84 PK_DecryptorFilter* filter_oaep_sha = new PK_DecryptorFilter(rng, decryptor_oaep_sha);
85 return crypt(filter_oaep_sha, encryptedData);
86 }
87
88 default:
89 throw Error("unsupported padding scheme");
90 }
91}
92
93Buffer
94Rsa::encrypt(const Buffer& keyBits, const Buffer& plainData, const EncryptParams& params)
95{
96 AutoSeededRandomPool rng;
97 RSA::PublicKey publicKey;
98
99 ByteQueue keyQueue;
100 keyQueue.LazyPut(keyBits.data(), keyBits.size());
101 publicKey.Load(keyQueue);
102
103 switch (params.getPaddingScheme()) {
104 case PADDING_SCHEME_PKCS1v15:
105 {
106 RSAES_PKCS1v15_Encryptor encryptor_pkcs1v15(publicKey);
107 PK_EncryptorFilter* filter_pkcs1v15 = new PK_EncryptorFilter(rng, encryptor_pkcs1v15);
108 return crypt(filter_pkcs1v15, plainData);
109 }
110
111 case PADDING_SCHEME_OAEP_SHA:
112 {
113 RSAES_OAEP_SHA_Encryptor encryptor_oaep_sha(publicKey);
114 PK_EncryptorFilter* filter_oaep_sha = new PK_EncryptorFilter(rng, encryptor_oaep_sha);
115 return crypt(filter_oaep_sha, plainData);
116 }
117
118 default:
119 throw Error("unsupported padding scheme");
120 }
121}
122
123Buffer
124crypt(SimpleProxyFilter* filter, const Buffer& data)
125{
126 OBufferStream obuf;
127 filter->Attach(new FileSink(obuf));
128
129 StringSource pipe(data.get(), data.size(), true, filter);
130 return *(obuf.buf());
131}
132
133} // namespace algo
134} // namespace gep
135} // namespace ndn