From 337bf554f599dbe725a83d97d94c1a00cce2d552 Mon Sep 17 00:00:00 2001 From: Dario Date: Wed, 24 Jun 2020 10:20:10 +0200 Subject: [PATCH] use only one implementation of decryption, call from all input parameter variants via inline functions --- src/cpp/Crypto/AuthenticatedEncryption.cpp | 34 ++-------------------- src/cpp/Crypto/AuthenticatedEncryption.h | 13 +++++---- 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/src/cpp/Crypto/AuthenticatedEncryption.cpp b/src/cpp/Crypto/AuthenticatedEncryption.cpp index 0a1625991..fe95cd06c 100644 --- a/src/cpp/Crypto/AuthenticatedEncryption.cpp +++ b/src/cpp/Crypto/AuthenticatedEncryption.cpp @@ -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 _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& encryptedMessage, MemoryBin** message) const +AuthenticatedEncryption::ResultType AuthenticatedEncryption::decrypt(const unsigned char* encryptedMessage, size_t encryptedMessageSize, MemoryBin** message) const { assert(message); std::shared_lock _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; } diff --git a/src/cpp/Crypto/AuthenticatedEncryption.h b/src/cpp/Crypto/AuthenticatedEncryption.h index e3a920ec9..634c73a5d 100644 --- a/src/cpp/Crypto/AuthenticatedEncryption.h +++ b/src/cpp/Crypto/AuthenticatedEncryption.h @@ -3,7 +3,6 @@ #include "../SingletonManager/MemoryManager.h" - #include #include @@ -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& encryptedMessage, MemoryBin** message) const; + inline ResultType decrypt(const std::vector& 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);