From fe709df0545fabef95a62a9226f86b339499d8e2 Mon Sep 17 00:00:00 2001 From: Joseph Donofry <joedonofry@gmail.com> Date: Sun, 12 May 2019 21:46:04 -0400 Subject: [PATCH] Remove explicit pointer use in crypto utils --- include/mtxclient/crypto/client.hpp | 10 ---------- include/mtxclient/crypto/utils.hpp | 12 ++++++++++++ lib/crypto/utils.cpp | 16 ++++++---------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/include/mtxclient/crypto/client.hpp b/include/mtxclient/crypto/client.hpp index 2f9e35f47..1c3828789 100644 --- a/include/mtxclient/crypto/client.hpp +++ b/include/mtxclient/crypto/client.hpp @@ -68,16 +68,6 @@ private: std::string msg_; }; -//! Create a uint8_t buffer which is initialized with random bytes. -inline BinaryBuf -create_buffer(std::size_t nbytes) -{ - auto buf = BinaryBuf(nbytes); - randombytes_buf(buf.data(), buf.size()); - - return buf; -} - template<class T> std::string pickle(typename T::olm_type *object, const std::string &key) diff --git a/include/mtxclient/crypto/utils.hpp b/include/mtxclient/crypto/utils.hpp index 1e38e28c5..f8af46f75 100644 --- a/include/mtxclient/crypto/utils.hpp +++ b/include/mtxclient/crypto/utils.hpp @@ -9,6 +9,8 @@ #include <openssl/hmac.h> #include <openssl/sha.h> +#include <sodium.h> + #include <boost/algorithm/string.hpp> namespace mtx { @@ -20,6 +22,16 @@ using BinaryBuf = std::vector<uint8_t>; const std::string HEADER_LINE("-----BEGIN MEGOLM SESSION DATA-----"); const std::string TRAILER_LINE("-----END MEGOLM SESSION DATA-----"); +//! Create a uint8_t buffer which is initialized with random bytes. +inline BinaryBuf +create_buffer(std::size_t nbytes) +{ + auto buf = BinaryBuf(nbytes); + randombytes_buf(buf.data(), buf.size()); + + return buf; +} + //! Simple wrapper around the OpenSSL PKCS5_PBKDF2_HMAC function BinaryBuf PBKDF2_HMAC_SHA_512(const std::string pass, const BinaryBuf salt, uint32_t iterations); diff --git a/lib/crypto/utils.cpp b/lib/crypto/utils.cpp index 7664fe0c0..283383539 100644 --- a/lib/crypto/utils.cpp +++ b/lib/crypto/utils.cpp @@ -32,7 +32,7 @@ AES_CTR_256_Encrypt(const std::string plaintext, const BinaryBuf aes256Key, Bina int ciphertext_len; - unsigned char *cipher = new unsigned char[plaintext.size()]; + BinaryBuf encrypted = create_buffer(plaintext.size()); uint8_t *iv_data = iv.data(); // need to set bit 63 to 0 @@ -51,7 +51,7 @@ AES_CTR_256_Encrypt(const std::string plaintext, const BinaryBuf aes256Key, Bina * EVP_EncryptUpdate can be called multiple times if necessary */ if (1 != EVP_EncryptUpdate(ctx, - cipher, + encrypted.data(), &len, reinterpret_cast<const unsigned char *>(&plaintext.c_str()[0]), plaintext.size())) { @@ -62,7 +62,7 @@ AES_CTR_256_Encrypt(const std::string plaintext, const BinaryBuf aes256Key, Bina /* Finalise the encryption. Further ciphertext bytes may be written at * this stage. */ - if (1 != EVP_EncryptFinal_ex(ctx, cipher + len, &len)) { + if (1 != EVP_EncryptFinal_ex(ctx, encrypted.data() + len, &len)) { // handleErrors(); } @@ -71,8 +71,6 @@ AES_CTR_256_Encrypt(const std::string plaintext, const BinaryBuf aes256Key, Bina /* Clean up */ EVP_CIPHER_CTX_free(ctx); - BinaryBuf encrypted(reinterpret_cast<uint8_t *>(cipher), cipher + ciphertext_len); - delete[] cipher; return encrypted; } @@ -85,7 +83,7 @@ AES_CTR_256_Decrypt(const std::string ciphertext, const BinaryBuf aes256Key, Bin int plaintext_len; - unsigned char *plaintext = new unsigned char[ciphertext.size()]; + BinaryBuf decrypted = create_buffer(ciphertext.size()); /* Create and initialise the context */ if (!(ctx = EVP_CIPHER_CTX_new())) { @@ -105,7 +103,7 @@ AES_CTR_256_Decrypt(const std::string ciphertext, const BinaryBuf aes256Key, Bin * EVP_DecryptUpdate can be called multiple times if necessary */ if (1 != EVP_DecryptUpdate(ctx, - plaintext, + decrypted.data(), &len, reinterpret_cast<const unsigned char *>(&ciphertext.data()[0]), ciphertext.size())) { @@ -116,7 +114,7 @@ AES_CTR_256_Decrypt(const std::string ciphertext, const BinaryBuf aes256Key, Bin /* Finalise the decryption. Further plaintext bytes may be written at * this stage. */ - if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) { + if (1 != EVP_DecryptFinal_ex(ctx, decrypted.data() + len, &len)) { // handleErrors(); } plaintext_len += len; @@ -124,8 +122,6 @@ AES_CTR_256_Decrypt(const std::string ciphertext, const BinaryBuf aes256Key, Bin /* Clean up */ EVP_CIPHER_CTX_free(ctx); - BinaryBuf decrypted(reinterpret_cast<uint8_t *>(plaintext), plaintext + plaintext_len); - delete[] plaintext; return decrypted; } -- GitLab