aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/signer/noscrypt_cipher.cpp17
-rw-r--r--src/signer/noscrypt_cipher.hpp106
2 files changed, 64 insertions, 59 deletions
diff --git a/src/signer/noscrypt_cipher.cpp b/src/signer/noscrypt_cipher.cpp
index 7f499a8..d20bdd6 100644
--- a/src/signer/noscrypt_cipher.cpp
+++ b/src/signer/noscrypt_cipher.cpp
@@ -2,7 +2,7 @@
#include <plog/Log.h>
#include <noscryptutil.h>
-#include <openssl/evp.h>
+#include <openssl/rand.h>
#include "noscrypt_cipher.hpp"
@@ -44,10 +44,9 @@ static void _printNoscryptError(NCResult result, const std::string funcName, int
#define LOG_NC_ERROR(result) _printNoscryptError(result, __func__, __LINE__)
-NoscryptCipher::NoscryptCipher(uint32_t version, uint32_t mode)
+NoscryptCipher::NoscryptCipher(uint32_t version, uint32_t mode) :
+ _cipher(version, mode)
{
- this->_cipher = NoscryptCipherContext(version, mode);
-
/*
* We can know what iv size we need for the cipher now and allocate
* a buffer just to save some allocations and code during the
@@ -58,10 +57,10 @@ NoscryptCipher::NoscryptCipher(uint32_t version, uint32_t mode)
if ((mode & NC_UTIL_CIPHER_MODE) == NC_UTIL_CIPHER_MODE_ENCRYPT)
{
//Resize the vector to the size of the current cipher
- this->_ivBuffer.resize(&this->_cipher.ivSize());
+ this->_ivBuffer.resize(this->_cipher.ivSize());
//Safe to assign the iv to the context now and it will maintain a pointer to the buffer
- this->_cipher.setIV(&this->_ivBuffer);
+ this->_cipher.setIV(this->_ivBuffer);
}
}
@@ -83,7 +82,7 @@ std::string NoscryptCipher::update(
//Safely convert the string to a vector of bytes (allocates and copies, so maybe speed up later)
const vector<uint8_t> inputBuffer(input.begin(), input.end());
- result = this->_cipher.setInput(&inputBuffer);
+ result = this->_cipher.setInput(inputBuffer);
if (result != NC_SUCCESS)
{
LOG_NC_ERROR(result);
@@ -133,9 +132,9 @@ std::string NoscryptCipher::update(
}
//Alloc vector for reading input data (maybe only alloc once)
- const vector<uint8_t> output(outputSize);
+ vector<uint8_t> output(outputSize);
- result = this->_cipher.readOutput(&output);
+ result = this->_cipher.readOutput(output);
if (result != outputSize)
{
LOG_NC_ERROR(result);
diff --git a/src/signer/noscrypt_cipher.hpp b/src/signer/noscrypt_cipher.hpp
index 8fd5ad4..3a901be 100644
--- a/src/signer/noscrypt_cipher.hpp
+++ b/src/signer/noscrypt_cipher.hpp
@@ -8,51 +8,11 @@ namespace nostr
{
namespace signer
{
- class NoscryptCipher
+ class NoscryptCipherContext
{
- public:
- NoscryptCipher(uint32_t version, uint32_t mode);
-
- ~NoscryptCipher();
-
- /*
- * @brief Performs the cipher operation on the input data. Depending on the mode
- * the cipher was initialized as, this will either encrypt or decrypt the data.
- * @param libContext The noscrypt library context.
- * @param localKey The local secret key used to encrypt/decrypt the data.
- * @param remoteKey The remote public key used to encrypt/decrypt the data.
- * @param input The data to encrypt/decrypt.
- * @returns The opposite of the input data.
- * @remark This cipher function follows the nostr nips format and will use do it's
- * best to
- */
- std::string update(
- const std::shared_ptr<const NCContext> libContext,
- const std::shared_ptr<const NCSecretKey> localKey,
- const std::shared_ptr<const NCPublicKey> remoteKey,
- const std::string input
- );
-
- static std::string naiveEncodeBase64(const std::string& str)
- {
- //TODO Implement base64 encoding
- return str;
- }
-
- static std::string naiveDecodeBase64(const std::string& str)
- {
- //TODO Implement base64 decoding
- return str;
- }
-
private:
+ NCUtilCipherContext* _cipher;
- NoscryptCipherContext _cipher;
- std::vector<uint8_t> _ivBuffer;
- };
-
- class NoscryptCipherContext
- {
public:
NoscryptCipherContext(uint32_t version, uint32_t mode)
@@ -79,6 +39,8 @@ namespace nostr
version,
mode | NC_UTIL_CIPHER_ZERO_ON_FREE | NC_UTIL_CIPHER_REUSEABLE
);
+
+ //TODO, may fail to allocate memory.
}
~NoscryptCipherContext()
@@ -88,15 +50,15 @@ namespace nostr
}
NCResult update(
- const shared_ptr<const NCContext>& libContext,
- const shared_ptr<const NCSecretKey>& localKey,
- const shared_ptr<const NCPublicKey>& remoteKey
- )
+ const std::shared_ptr<const NCContext> libContext,
+ const std::shared_ptr<const NCSecretKey> localKey,
+ const std::shared_ptr<const NCPublicKey> remoteKey
+ ) const
{
- return NCUtilCipherUpdate(this->_cipher, libContext.get(), localKey.get(), remoteKey.get());
+ return NCUtilCipherUpdate(_cipher, libContext.get(), localKey.get(), remoteKey.get());
}
- NCResult setIV(const std::vector<uint8_t>& iv)
+ NCResult setIV(std::vector<uint8_t>& iv) const
{
return NCUtilCipherSetProperty(_cipher, NC_ENC_SET_IV, iv.data(), (uint32_t)iv.size());
}
@@ -132,7 +94,7 @@ namespace nostr
return (uint32_t)result;
}
- NCResult readOutput(const std::vector<uint8_t>& output) const
+ NCResult readOutput(std::vector<uint8_t>& output) const
{
return NCUtilCipherReadOutput(_cipher, output.data(), (uint32_t)output.size());
}
@@ -146,9 +108,53 @@ namespace nostr
return NCUtilCipherInit(_cipher, input.data(), input.size());
}
+ };
+
+ class NoscryptCipher
+ {
private:
- NCUtilCipherContext* _cipher;
+ const NoscryptCipherContext _cipher;
+ /*
+ * Stores the initialziation vector (aka nonce for nip44) for the cipher.
+ * Noscrypt needs a memory buffer to store the iv, as it only holds pointers.
+ *
+ * This buffer must always point to valid memory after the cipher is created.
+ */
+ std::vector<uint8_t> _ivBuffer;
+
+ public:
+ NoscryptCipher(uint32_t version, uint32_t mode);
+
+ /*
+ * @brief Performs the cipher operation on the input data. Depending on the mode
+ * the cipher was initialized as, this will either encrypt or decrypt the data.
+ * @param libContext The noscrypt library context.
+ * @param localKey The local secret key used to encrypt/decrypt the data.
+ * @param remoteKey The remote public key used to encrypt/decrypt the data.
+ * @param input The data to encrypt/decrypt.
+ * @returns The opposite of the input data.
+ * @remark This cipher function follows the nostr nips format and will use do it's
+ * best to
+ */
+ std::string update(
+ const std::shared_ptr<const NCContext> libContext,
+ const std::shared_ptr<const NCSecretKey> localKey,
+ const std::shared_ptr<const NCPublicKey> remoteKey,
+ const std::string& input
+ );
+
+ static std::string naiveEncodeBase64(const std::string& str)
+ {
+ //TODO Implement base64 encoding
+ return str;
+ }
+
+ static std::string naiveDecodeBase64(const std::string& str)
+ {
+ //TODO Implement base64 decoding
+ return str;
+ }
};
}
}