-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathencryption.cpp
More file actions
217 lines (159 loc) · 7 KB
/
Copy pathencryption.cpp
File metadata and controls
217 lines (159 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "encryption.h"
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/aes.h>
#include <random>
#include <cassert>
bool generateRSAKeys(secure::string& psPrivKey, std::string& psPubKey)
{
RSA *prKeyPair = RSA_new();
BIGNUM *pbnExponent = BN_new();
BN_set_word(pbnExponent, RSA_F4);
int nRes = RSA_generate_key_ex (prKeyPair, 4096, pbnExponent, nullptr);
if (nRes == 0)
{
RSA_free(prKeyPair);
BN_clear_free(pbnExponent);
CRYPTO_cleanup_all_ex_data();
return false;
}
BIO *pri = BIO_new(BIO_s_mem());
BIO *pub = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(pri, prKeyPair, nullptr, nullptr, 0, nullptr, nullptr);
PEM_write_bio_RSAPublicKey(pub, prKeyPair);
size_t nPriLen = BIO_pending(pri);
size_t nPubLen = BIO_pending(pub);
psPubKey.resize(nPubLen + 1);
psPrivKey.resize(nPriLen + 1);
std::fill(psPubKey.begin(), psPubKey.end(), 0);
std::fill(psPrivKey.begin(), psPrivKey.end(), 0);
BIO_read(pub, &psPubKey[0], nPubLen);
BIO_read(pri, &psPrivKey[0], nPriLen);
RSA_free(prKeyPair);
BIO_free_all(pri);
BIO_free_all(pub);
BN_clear_free(pbnExponent);
CRYPTO_cleanup_all_ex_data();
return nRes != 0;
}
bool encryptRSAKey(const secure::string& psKeyData, const secure::string& psPassword, std::vector<unsigned char>& pvchSalt, int& pnDeriveIterations, std::vector<unsigned char>& pvchCryptedKey)
{
pvchSalt.resize(8);
if (! RAND_pseudo_bytes(&pvchSalt[0], pvchSalt.size()))
return false;
unsigned char chKey[32];
unsigned char chIV[32];
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int> uni(700000, 2000000);
pnDeriveIterations = uni(rng);
EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &pvchSalt[0], (const unsigned char *)&psPassword[0], psPassword.size(), pnDeriveIterations, chKey, chIV);
// max ciphertext len for a n bytes of plaintext is
// n + AES_BLOCK_SIZE - 1 bytes
int nLen = psKeyData.size();
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
pvchCryptedKey = std::vector<unsigned char> (nCLen);
EVP_CIPHER_CTX ctx;
bool fOk = true;
EVP_CIPHER_CTX_init(&ctx);
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), nullptr, chKey, chIV) != 0;
if (fOk) fOk = EVP_EncryptUpdate(&ctx, &pvchCryptedKey[0], &nCLen, (const unsigned char*)&psKeyData[0], nLen) != 0;
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, &pvchCryptedKey[0] + nCLen, &nFLen) != 0;
EVP_CIPHER_CTX_cleanup(&ctx);
if (!fOk) return false;
pvchCryptedKey.resize(nCLen + nFLen);
return true;
}
bool decryptRSAKey(const std::vector<unsigned char>& pvchCryptedKey, const secure::string& psPassword, const std::vector<unsigned char>& pvchSalt, int pnDeriveIterations, secure::string& psDecryptedKey)
{
unsigned char chKey[32];
unsigned char chIV[32];
EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &pvchSalt[0],
(const unsigned char *)&psPassword[0], psPassword.size(), pnDeriveIterations, chKey, chIV);
// plaintext will always be equal to or lesser than length of ciphertext
int nLen = pvchCryptedKey.size();
int nPLen = nLen, nFLen = 0;
psDecryptedKey.resize(nLen);
EVP_CIPHER_CTX ctx;
bool fOk = true;
EVP_CIPHER_CTX_init(&ctx);
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), nullptr, chKey, chIV) != 0;
if (fOk) fOk = EVP_DecryptUpdate(&ctx, (unsigned char*)&psDecryptedKey[0], &nPLen, &pvchCryptedKey[0], nLen) != 0;
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (unsigned char*)&psDecryptedKey[0] + nPLen, &nFLen) != 0;
EVP_CIPHER_CTX_cleanup(&ctx);
if (!fOk) return false;
psDecryptedKey.resize(nPLen + nFLen);
return true;
}
bool legacyDecryptKey(const std::vector<unsigned char>& pvchCryptedKey, const secure::string& psPassword, const std::vector<unsigned char>& pvchSalt, int pnDeriveIterations, ripple::SecretKey& prsSecret)
{
using namespace ripple;
unsigned char chKey[32];
unsigned char chIV[32];
EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &pvchSalt[0],
(const unsigned char *)&psPassword[0], psPassword.size(), pnDeriveIterations, chKey, chIV);
// plaintext will always be equal to or lesser than length of ciphertext
int nLen = pvchCryptedKey.size();
int nPLen = nLen, nFLen = 0;
secure::secret decryptedKey;
decryptedKey.resize(nLen);
EVP_CIPHER_CTX ctx;
bool fOk = true;
EVP_CIPHER_CTX_init(&ctx);
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), nullptr, chKey, chIV) != 0;
if (fOk) fOk = EVP_DecryptUpdate(&ctx, &decryptedKey[0], &nPLen, &pvchCryptedKey[0], nLen) != 0;
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, &decryptedKey[0] + nPLen, &nFLen) != 0;
EVP_CIPHER_CTX_cleanup(&ctx);
if (!fOk) return false;
decryptedKey.resize(nPLen + nFLen);
prsSecret = SecretKey(Slice(decryptedKey.data(), decryptedKey.size()));
return true;
}
//// Asymmetric encryption
bool encryptSecretKey(const ripple::SecretKey& prsSecret, const std::string& psEncryptionKey, std::vector<unsigned char>& pvchEncryptedKey)
{
BIO* bio = BIO_new_mem_buf((const void*)&psEncryptionKey[0], -1) ; // -1: assume string is null terminated
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL) ; // NO NL
// Load the RSA key from the BIO
RSA* rsa_pub_key = PEM_read_bio_RSAPublicKey(bio, nullptr, nullptr, nullptr);
if(!rsa_pub_key)
{
BIO_free(bio);
CRYPTO_cleanup_all_ex_data();
return false;
}
pvchEncryptedKey.resize(RSA_size(rsa_pub_key));
std::fill(pvchEncryptedKey.begin(), pvchEncryptedKey.end(), 0);
std::vector<unsigned char> vguard(pvchEncryptedKey.size(), 0);
int nRes = RSA_public_encrypt(prsSecret.size(), prsSecret.data(), &pvchEncryptedKey[0], rsa_pub_key, RSA_PKCS1_PADDING);
assert(vguard != pvchEncryptedKey);
BIO_free(bio);
CRYPTO_cleanup_all_ex_data();
return (nRes > 0) && (nRes == RSA_size(rsa_pub_key));
}
bool decryptSecretKey(const std::vector<unsigned char>& pvchEncryptedSecret, const secure::string& psDecryptionKey, ripple::SecretKey& prsSecretKey)
{
using namespace ripple;
BIO *bio = BIO_new_mem_buf( (const void*)&psDecryptionKey[0], -1 );
//BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); // NO NL
RSA* rsa_priv_key = PEM_read_bio_RSAPrivateKey(bio, nullptr, nullptr, nullptr);
if (!rsa_priv_key)
{
BIO_free( bio ) ;
CRYPTO_cleanup_all_ex_data();
return false;
}
int nRsaLen = RSA_size(rsa_priv_key);
secure::secret secretData;
secretData.resize(nRsaLen);
std::fill(secretData.begin(), secretData.end(), 0);
int nResLen = RSA_private_decrypt(secretData.size(), &pvchEncryptedSecret[0], &secretData[0], rsa_priv_key, RSA_PKCS1_PADDING);
BIO_free( bio ) ;
CRYPTO_cleanup_all_ex_data();
if (nResLen < 0)
return false;
prsSecretKey = SecretKey(Slice(&secretData[0], nResLen));
return true;
}