diff options
author | buttercat1791 <mjjurkoic@gmail.com> | 2024-08-11 19:16:06 -0500 |
---|---|---|
committer | buttercat1791 <mjjurkoic@gmail.com> | 2024-08-11 19:16:06 -0500 |
commit | bc80ca660f96424ba8b2bd9ea60ad52239d7552a (patch) | |
tree | 993062561eda83c840d144f3e6ae0c1de9e78da9 /src | |
parent | fcb5037960347865492ffb6f591fc9db165a4d5f (diff) |
Handle base64 encoding and decoding
Diffstat (limited to 'src')
-rw-r--r-- | src/cryptography/noscrypt_cipher.cpp (renamed from src/signer/noscrypt_cipher.cpp) | 44 | ||||
-rw-r--r-- | src/signer/noscrypt_cipher.hpp | 160 | ||||
-rw-r--r-- | src/signer/noscrypt_signer.cpp | 1 |
3 files changed, 42 insertions, 163 deletions
diff --git a/src/signer/noscrypt_cipher.cpp b/src/cryptography/noscrypt_cipher.cpp index d751261..ae9aaf0 100644 --- a/src/signer/noscrypt_cipher.cpp +++ b/src/cryptography/noscrypt_cipher.cpp @@ -1,12 +1,12 @@ #include <plog/Init.h> #include <plog/Log.h> -#include <noscryptutil.h> +#include <openssl/evp.h> #include <openssl/rand.h> -#include "noscrypt_cipher.hpp" +#include "cryptography/noscrypt_cipher.hpp" -using namespace nostr::signer; +using namespace nostr::cryptography; using namespace std; static void _printNoscryptError(NCResult result, const std::string funcName, int lineNum) @@ -143,3 +143,41 @@ result = this->_cipher.setInput(inputBuffer); return string(output.begin(), output.end()); } + +string NoscryptCipher::naiveEncodeBase64(const std::string& str) +{ + // Compute base64 size and allocate a string buffer of that size. + const size_t encodedSize = NoscryptCipher::base64EncodedSize(str.size()); + unsigned char* encodedData = new unsigned char[encodedSize]; + + // Encode the input string to base64. + EVP_EncodeBlock(encodedData, (const unsigned char*)str.data(), str.size()); + + // Construct the encoded string from the buffer. + string encodedStr((char*)encodedData); + + // Zero out the buffer and delete the pointer. + memset(encodedData, 0, encodedSize); + delete [] encodedData; + + return encodedStr; +} + +string NoscryptCipher::naiveDecodeBase64(const string& str) +{ + // Compute the size of the decoded string and allocate a buffer of that size. + const size_t decodedSize = NoscryptCipher::base64DecodedSize(str.size()); + unsigned char* decodedData = new unsigned char[decodedSize]; + + // Decode the input string from base64. + EVP_DecodeBlock(decodedData, (const unsigned char*)str.data(), str.size()); + + // Construct the decoded string from the buffer. + string decodedStr((char*)decodedData); + + // Zero out the buffer and delete the pointer. + memset(decodedData, 0, decodedSize); + delete [] decodedData; + + return decodedStr; +}; diff --git a/src/signer/noscrypt_cipher.hpp b/src/signer/noscrypt_cipher.hpp deleted file mode 100644 index ffe4bec..0000000 --- a/src/signer/noscrypt_cipher.hpp +++ /dev/null @@ -1,160 +0,0 @@ - -#include <memory> - -#include <noscrypt.h> -#include <noscryptutil.h> - -namespace nostr -{ -namespace signer -{ -class NoscryptCipherContext -{ -private: - NCUtilCipherContext* _cipher; - -public: - - NoscryptCipherContext(uint32_t version, uint32_t mode) - { - /* - * Create a new cipher context with the specified - * version and mode that will live for the duration of the - * instance. - * - * The user is expected to use the noscryptutil mode for - * setting encryption/decryption modes. - * - * The cipher will zero out the memory when it is freed. - * - * For decryption, by default the mac is verified before - * decryption occurs. - * - * NOTE: The ciper is set to reusable mode, so encrypt/decrypt - * can be called multiple times although it's not recommended, - * its just the more predictable way for users to handle it. - */ - - _cipher = NCUtilCipherAlloc( - version, - mode | NC_UTIL_CIPHER_ZERO_ON_FREE | NC_UTIL_CIPHER_REUSEABLE - ); - - //TODO, may fail to allocate memory. - } - - ~NoscryptCipherContext() - { - //Free the cipher context (will also zero any data/pointers) - NCUtilCipherFree(_cipher); - } - - NCResult update( - const std::shared_ptr<const NCContext> libContext, - const std::shared_ptr<const NCSecretKey> localKey, - const std::shared_ptr<const NCPublicKey> remoteKey - ) const - { - return NCUtilCipherUpdate(_cipher, libContext.get(), localKey.get(), remoteKey.get()); - } - - NCResult setIV(std::vector<uint8_t>& iv) const - { - return NCUtilCipherSetProperty(_cipher, NC_ENC_SET_IV, iv.data(), (uint32_t)iv.size()); - } - - size_t ivSize() const - { - NCResult size = NCUtilCipherGetIvSize(_cipher); - - if (size <= 0) - { - //TODO Implement error handling - return 0; - } - - return size; - } - - NCResult outputSize() const - { - return NCUtilCipherGetOutputSize(_cipher); - } - - uint32_t flags() const - { - NCResult result = NCUtilCipherGetFlags(_cipher); - - if (result <= 0) - { - //TODO Implement error handling - return 0; - } - - return (uint32_t)result; - } - - NCResult readOutput(std::vector<uint8_t>& output) const - { - return NCUtilCipherReadOutput(_cipher, output.data(), (uint32_t)output.size()); - } - - NCResult setInput(const std::vector<uint8_t>& input) const - { - /* - * Assign and validate input string. Init can be only called multiple times - * without side effects when the reusable flag is set. (currently set) - */ - - return NCUtilCipherInit(_cipher, input.data(), input.size()); - } -}; - -class NoscryptCipher -{ - -private: - 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; - } -}; -} // namespace signer -} // namespace nostr diff --git a/src/signer/noscrypt_signer.cpp b/src/signer/noscrypt_signer.cpp index 3cf4b6f..2b87703 100644 --- a/src/signer/noscrypt_signer.cpp +++ b/src/signer/noscrypt_signer.cpp @@ -17,6 +17,7 @@ using namespace nostr::data; using namespace nostr::service; using namespace nostr::signer; +using namespace nostr::cryptography; using namespace std; #pragma region Constructors and Destructors |