Skip to content

Commit 2482506

Browse files
committed
Dynamically allocating memory for session key
1 parent e8b2ac7 commit 2482506

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

libs/libencrypt/src/encryption.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ EVP_PKEY* Encryption::loadPublicKey(const std::string& filename) {
6262
* @param publicKey The public key used for encryption
6363
* @return std::string Encrypted session key
6464
*/
65-
std::string Encryption::encryptSessionKey(const unsigned char* sessionKey, size_t keySize, EVP_PKEY* publicKey) {
65+
std::string Encryption::encryptSessionKey(std::vector<unsigned char>& sessionKey, EVP_PKEY* publicKey) {
6666
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(publicKey, NULL);
6767
if (!ctx) {
6868
std::cerr << "Failed to create EVP_PKEY_CTX" << std::endl;
@@ -82,14 +82,15 @@ std::string Encryption::encryptSessionKey(const unsigned char* sessionKey, size_
8282
}
8383

8484
size_t outLen;
85-
if (EVP_PKEY_encrypt(ctx, NULL, &outLen, sessionKey, keySize) <= 0) {
85+
size_t keySize = sessionKey.size();
86+
if (EVP_PKEY_encrypt(ctx, NULL, &outLen, sessionKey.data(), keySize) <= 0) {
8687
std::cerr << "EVP_PKEY_encrypt (determine length) failed" << std::endl;
8788
EVP_PKEY_CTX_free(ctx);
8889
return "";
8990
}
9091

9192
std::vector<unsigned char> out(outLen);
92-
if (EVP_PKEY_encrypt(ctx, out.data(), &outLen, sessionKey, keySize) <= 0) {
93+
if (EVP_PKEY_encrypt(ctx, out.data(), &outLen, sessionKey.data(), keySize) <= 0) {
9394
std::cerr << "EVP_PKEY_encrypt failed" << std::endl;
9495
EVP_PKEY_CTX_free(ctx);
9596
return "";
@@ -133,15 +134,15 @@ std::string Encryption::base64Encode(const unsigned char* buffer, size_t length)
133134
* @return std::string The encrypted ciphertext.
134135
* Returns an empty string if there is an error during encryption.
135136
*/
136-
std::string Encryption::encryptData(const std::string& plaintext, const unsigned char* sessionKey, const unsigned char* iv) {
137+
std::string Encryption::encryptData(const std::string& plaintext, std::vector<unsigned char>& sessionKey, const unsigned char* iv) {
137138
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
138139
if (!ctx) {
139140
std::cerr << "Failed to create EVP_CIPHER_CTX" << std::endl;
140141
return "";
141142
}
142143

143144
// Initialize the encryption operation with AES-128-CBC
144-
if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, sessionKey, iv) != 1) {
145+
if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, sessionKey.data(), iv) != 1) {
145146
std::cerr << "EVP_EncryptInit_ex failed" << std::endl;
146147
EVP_CIPHER_CTX_free(ctx);
147148
return "";
@@ -189,8 +190,8 @@ bool Encryption::encryptFile(const std::string& publicKeyFile, std::string& file
189190

190191
OpenSSL_add_all_algorithms();
191192
ERR_load_crypto_strings();
192-
unsigned char sessionKey[sessionKeySize];
193-
generateSessionKey(sessionKey, sizeof(sessionKey));
193+
std::vector<unsigned char> sessionKey(sessionKeySize);
194+
generateSessionKey(sessionKey.data(), sessionKey.size());
194195

195196
//load public key
196197
EVP_PKEY* publicKey = loadPublicKey(publicKeyFile);
@@ -211,7 +212,7 @@ bool Encryption::encryptFile(const std::string& publicKeyFile, std::string& file
211212
file.close();
212213

213214
// Encrypt session key
214-
std::string encryptedSessionKey = encryptSessionKey(sessionKey, sizeof(sessionKey), publicKey);
215+
std::string encryptedSessionKey = encryptSessionKey(sessionKey, publicKey);
215216
if (encryptedSessionKey.empty()) {
216217
EVP_PKEY_free(publicKey);
217218
return false;

libs/libencrypt/src/encryption.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Encryption {
5050
* @param publicKey The public key used for encryption
5151
* @return std::string Encrypted session key
5252
*/
53-
static std::string encryptSessionKey(const unsigned char* sessionKey, size_t keySize, EVP_PKEY* publicKey);
53+
static std::string encryptSessionKey(std::vector<unsigned char>& sessionKey, EVP_PKEY* publicKey);
5454

5555
/**
5656
* @brief
@@ -70,7 +70,7 @@ class Encryption {
7070
* @return std::string The encrypted ciphertext.
7171
* Returns an empty string if there is an error during encryption.
7272
*/
73-
static std::string encryptData(const std::string& plaintext, const unsigned char* sessionKey, const unsigned char* iv);
73+
static std::string encryptData(const std::string& plaintext, std::vector<unsigned char>& sessionKey, const unsigned char* iv);
7474

7575
/**
7676
* @brief Encrypts a file using the provided public key.

0 commit comments

Comments
 (0)