mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
use only one implementation of decryption, call from all input parameter variants via inline functions
This commit is contained in:
parent
dcfed9bb12
commit
337bf554f5
@ -100,35 +100,7 @@ AuthenticatedEncryption::ResultType AuthenticatedEncryption::encrypt(const Memor
|
||||
return AUTH_ENCRYPT_OK;
|
||||
}
|
||||
|
||||
AuthenticatedEncryption::ResultType AuthenticatedEncryption::decrypt(const MemoryBin* encryptedMessage, MemoryBin** message) const
|
||||
{
|
||||
assert(message && encryptedMessage);
|
||||
std::shared_lock<std::shared_mutex> _lock(mWorkingMutex);
|
||||
|
||||
if (!mEncryptionKey) {
|
||||
return AUTH_NO_KEY;
|
||||
}
|
||||
|
||||
size_t decryptSize = encryptedMessage->size() - crypto_secretbox_MACBYTES;
|
||||
//unsigned char* decryptBuffer = (unsigned char*)malloc(decryptSize);
|
||||
auto mm = MemoryManager::getInstance();
|
||||
//ObfusArray* decryptedData = new ObfusArray(decryptSize);
|
||||
auto decryptedData = mm->getFreeMemory(decryptSize);
|
||||
unsigned char nonce[crypto_secretbox_NONCEBYTES];
|
||||
// we use a hardcoded value for nonce
|
||||
// TODO: use a dynamic value, save it along with the other parameters
|
||||
memset(nonce, 31, crypto_secretbox_NONCEBYTES);
|
||||
|
||||
if (crypto_secretbox_open_easy(*decryptedData, *encryptedMessage, encryptedMessage->size(), nonce, *mEncryptionKey)) {
|
||||
mm->releaseMemory(decryptedData);
|
||||
return AUTH_DECRYPT_MESSAGE_FAILED;
|
||||
}
|
||||
*message = decryptedData;
|
||||
|
||||
return AUTH_DECRYPT_OK;
|
||||
}
|
||||
|
||||
AuthenticatedEncryption::ResultType AuthenticatedEncryption::decrypt(const std::vector<unsigned char>& encryptedMessage, MemoryBin** message) const
|
||||
AuthenticatedEncryption::ResultType AuthenticatedEncryption::decrypt(const unsigned char* encryptedMessage, size_t encryptedMessageSize, MemoryBin** message) const
|
||||
{
|
||||
assert(message);
|
||||
std::shared_lock<std::shared_mutex> _lock(mWorkingMutex);
|
||||
@ -137,7 +109,7 @@ AuthenticatedEncryption::ResultType AuthenticatedEncryption::decrypt(const std::
|
||||
return AUTH_NO_KEY;
|
||||
}
|
||||
|
||||
size_t decryptSize = encryptedMessage.size() - crypto_secretbox_MACBYTES;
|
||||
size_t decryptSize = encryptedMessageSize - crypto_secretbox_MACBYTES;
|
||||
//unsigned char* decryptBuffer = (unsigned char*)malloc(decryptSize);
|
||||
auto mm = MemoryManager::getInstance();
|
||||
//ObfusArray* decryptedData = new ObfusArray(decryptSize);
|
||||
@ -147,7 +119,7 @@ AuthenticatedEncryption::ResultType AuthenticatedEncryption::decrypt(const std::
|
||||
// TODO: use a dynamic value, save it along with the other parameters
|
||||
memset(nonce, 31, crypto_secretbox_NONCEBYTES);
|
||||
|
||||
if (crypto_secretbox_open_easy(*decryptedData, encryptedMessage.data(), encryptedMessage.size(), nonce, *mEncryptionKey)) {
|
||||
if (crypto_secretbox_open_easy(*decryptedData, encryptedMessage, encryptedMessageSize, nonce, *mEncryptionKey)) {
|
||||
mm->releaseMemory(decryptedData);
|
||||
return AUTH_DECRYPT_MESSAGE_FAILED;
|
||||
}
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
|
||||
|
||||
#include "../SingletonManager/MemoryManager.h"
|
||||
|
||||
#include <shared_mutex>
|
||||
#include <vector>
|
||||
|
||||
@ -62,12 +61,16 @@ public:
|
||||
|
||||
ResultType encrypt(const MemoryBin* message, MemoryBin** encryptedMessage) const;
|
||||
|
||||
ResultType decrypt(const MemoryBin* encryptedMessage, MemoryBin** message) const;
|
||||
inline ResultType decrypt(const MemoryBin* encryptedMessage, MemoryBin** message) const {
|
||||
return decrypt(encryptedMessage->data(), encryptedMessage->size(), message);
|
||||
}
|
||||
//! \brief same as the other decrypt only in other format
|
||||
//! \param encryptedMessage format from Poco Binary Data from DB, like returned from model/table/user for encrypted private key
|
||||
//!
|
||||
//! double code, I don't know how to prevent without unnecessary copy of encryptedMessage
|
||||
ResultType decrypt(const std::vector<unsigned char>& encryptedMessage, MemoryBin** message) const;
|
||||
inline ResultType decrypt(const std::vector<unsigned char>& encryptedMessage, MemoryBin** message) const {
|
||||
return decrypt(encryptedMessage.data(), encryptedMessage.size(), message);
|
||||
}
|
||||
//! \brief raw decrypt function, actual implementation
|
||||
ResultType decrypt(const unsigned char* encryptedMessage, size_t encryptedMessageSize, MemoryBin** message) const;
|
||||
|
||||
static const char* getErrorMessage(ResultType type);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user