aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar buttercat1791 <mjjurkoic@gmail.com>2024-05-28 08:46:49 -0500
committerLibravatar buttercat1791 <mjjurkoic@gmail.com>2024-05-28 08:46:49 -0500
commit9db92687fecd7528a6aa93e60bcbebc41342378c (patch)
tree3649f2bb6d87178eb1ac67d3c610c1ea9719bd99
parentb61ff680f2f16fe9b86da5f8c9dde78a926d8997 (diff)
Validate secret key after generating it
-rw-r--r--include/signer/noscrypt_signer.hpp18
-rw-r--r--src/signer/noscrypt_signer.cpp148
2 files changed, 123 insertions, 43 deletions
diff --git a/include/signer/noscrypt_signer.hpp b/include/signer/noscrypt_signer.hpp
index cdee3fd..b4ea5f7 100644
--- a/include/signer/noscrypt_signer.hpp
+++ b/include/signer/noscrypt_signer.hpp
@@ -35,10 +35,10 @@ public:
void sign(std::shared_ptr<data::Event> event) override;
private:
- std::shared_ptr<NCContext> noscryptContext;
+ std::shared_ptr<NCContext> _noscryptContext;
- std::string localPrivateKey;
- std::string localPublicKey;
+ std::string _localPrivateKey;
+ std::string _localPublicKey;
/**
* @brief Initializes the noscrypt library context into the class's `context` property.
@@ -46,8 +46,6 @@ private:
*/
std::shared_ptr<NCContext> _initNoscryptContext();
- void _logNoscryptResult(NCResult result);
-
/**
* @brief Generates a private/public key pair for local use.
* @returns The generated keypair of the form `[privateKey, publicKey]`, or a pair of empty
@@ -56,6 +54,16 @@ private:
* of this class.
*/
std::tuple<std::string, std::string> _createLocalKeypair();
+
+ #pragma region Logging
+
+ void _logNoscryptInitResult(NCResult result);
+
+ void _logNoscryptSecretKeyResult(NCResult result);
+
+ void _logNoscryptPublicKeyResult(NCResult result);
+
+ #pragma endregion
};
} // namespace signer
} // namespace nostr
diff --git a/src/signer/noscrypt_signer.cpp b/src/signer/noscrypt_signer.cpp
index 0e0ff89..fa391d1 100644
--- a/src/signer/noscrypt_signer.cpp
+++ b/src/signer/noscrypt_signer.cpp
@@ -13,20 +13,20 @@ nostr::signer::NoscryptSigner::NoscryptSigner(
{
plog::init(plog::debug, appender.get());
- this->noscryptContext = this->_initNoscryptContext();
- if (this->noscryptContext == nullptr)
+ this->_noscryptContext = this->_initNoscryptContext();
+ if (this->_noscryptContext == nullptr)
{
return;
}
const auto [privateKey, publicKey] = this->_createLocalKeypair();
- this->localPrivateKey = privateKey;
- this->localPublicKey = publicKey;
+ this->_localPrivateKey = privateKey;
+ this->_localPublicKey = publicKey;
};
nostr::signer::NoscryptSigner::~NoscryptSigner()
{
- NCDestroyContext(this->noscryptContext.get());
+ NCDestroyContext(this->_noscryptContext.get());
};
void nostr::signer::NoscryptSigner::receiveConnection(string connectionToken)
@@ -64,7 +64,7 @@ shared_ptr<NCContext> nostr::signer::NoscryptSigner::_initNoscryptContext()
generate_n(randomEntropy.get(), contextStructSize, [&]() { return dist(gen); });
NCResult result = NCInitContext(context.get(), randomEntropy.get());
- this->_logNoscryptResult(result);
+ this->_logNoscryptInitResult(result);
if (result != NC_SUCCESS)
{
@@ -74,35 +74,6 @@ shared_ptr<NCContext> nostr::signer::NoscryptSigner::_initNoscryptContext()
return context;
};
-void nostr::signer::NoscryptSigner::_logNoscryptResult(NCResult result)
-{
- switch (result) {
- case NC_SUCCESS:
- PLOG_INFO << "noscrypt - success";
- break;
-
- case E_NULL_PTR:
- PLOG_ERROR << "noscrypt - error: A null pointer was passed to the initializer.";
- break;
-
- case E_INVALID_ARG:
- PLOG_ERROR << "noscrypt - error: An invalid argument was passed to the initializer.";
- break;
-
- case E_INVALID_CONTEXT:
- PLOG_ERROR << "noscrypt - error: The NCContext struct is in an invalid state.";
- break;
-
- case E_ARGUMENT_OUT_OF_RANGE:
- PLOG_ERROR << "noscrypt - error: An initializer argument was outside the range of acceptable values.";
- break;
-
- case E_OPERATION_FAILED:
- PLOG_ERROR << "noscrypt - error";
- break;
- }
-};
-
/**
* @brief Generates a private/public key pair for local use.
* @returns The generated keypair of the form `[privateKey, publicKey]`, or a pair of empty
@@ -123,6 +94,16 @@ tuple<string, string> nostr::signer::NoscryptSigner::_createLocalKeypair()
uniform_int_distribution<> dist(0, NC_SEC_KEY_SIZE);
generate_n(secretKey.get()->key, NC_SEC_KEY_SIZE, [&]() { return dist(gen); });
+ // Check the validity of the secret key.
+ NCResult result = NCValidateSecretKey(this->_noscryptContext.get(), secretKey.get());
+ this->_logNoscryptSecretKeyResult(result);
+
+ if (result != NC_SUCCESS)
+ {
+ // Return empty strings if the secret key generation fails.
+ return make_tuple(string(), string());
+ }
+
// Convert the buffer into a hex string for a more human-friendly representation.
stringstream secretKeyStream;
for (int i = 0; i < NC_SEC_KEY_SIZE; i++)
@@ -134,14 +115,14 @@ tuple<string, string> nostr::signer::NoscryptSigner::_createLocalKeypair()
// Use noscrypt to derive the public key from its private counterpart.
unique_ptr<NCPublicKey> pubkey(new NCPublicKey);
NCResult result = NCGetPublicKey(
- this->noscryptContext.get(),
+ this->_noscryptContext.get(),
secretKey.get(),
pubkey.get());
- this->_logNoscryptResult(result);
+ this->_logNoscryptPublicKeyResult(result);
if (result != NC_SUCCESS)
{
- // Return empty strings if the key generation fails.
+ // Return empty strings if the pubkey generation fails.
return make_tuple(string(), string());
}
@@ -156,3 +137,94 @@ tuple<string, string> nostr::signer::NoscryptSigner::_createLocalKeypair()
return make_tuple(privateKey, publicKey);
};
+
+#pragma region Logging
+
+void nostr::signer::NoscryptSigner::_logNoscryptInitResult(NCResult result)
+{
+ switch (result) {
+ case NC_SUCCESS:
+ PLOG_INFO << "noscrypt - success";
+ break;
+
+ case E_NULL_PTR:
+ PLOG_ERROR << "noscrypt - error: A null pointer was passed to the initializer.";
+ break;
+
+ case E_INVALID_ARG:
+ PLOG_ERROR << "noscrypt - error: An invalid argument was passed to the initializer.";
+ break;
+
+ case E_INVALID_CONTEXT:
+ PLOG_ERROR << "noscrypt - error: The NCContext struct is in an invalid state.";
+ break;
+
+ case E_ARGUMENT_OUT_OF_RANGE:
+ PLOG_ERROR << "noscrypt - error: An initializer argument was outside the range of acceptable values.";
+ break;
+
+ case E_OPERATION_FAILED:
+ PLOG_ERROR << "noscrypt - error";
+ break;
+ }
+};
+
+void nostr::signer::NoscryptSigner::_logNoscryptSecretKeyResult(NCResult result)
+{
+ switch (result) {
+ case NC_SUCCESS:
+ PLOG_INFO << "noscrypt - success: Generated a valid secret key.";
+ break;
+
+ case E_NULL_PTR:
+ PLOG_ERROR << "noscrypt - error: A null pointer was passed to the secret key validation function.";
+ break;
+
+ case E_INVALID_ARG:
+ PLOG_ERROR << "noscrypt - error: An invalid argument was passed to the secret key validation function.";
+ break;
+
+ case E_INVALID_CONTEXT:
+ PLOG_ERROR << "noscrypt - error: The NCContext struct is in an invalid state.";
+ break;
+
+ case E_ARGUMENT_OUT_OF_RANGE:
+ PLOG_ERROR << "noscrypt - error: An argument was outside the range of acceptable values.";
+ break;
+
+ case E_OPERATION_FAILED:
+ PLOG_ERROR << "noscrypt - error: Failed to validate the generated secret key.";
+ break;
+ }
+};
+
+void nostr::signer::NoscryptSigner::_logNoscryptPublicKeyResult(NCResult result)
+{
+ switch (result) {
+ case NC_SUCCESS:
+ PLOG_INFO << "noscrypt - success: Generated a valid public key.";
+ break;
+
+ case E_NULL_PTR:
+ PLOG_ERROR << "noscrypt - error: A null pointer was passed to the public key generation function.";
+ break;
+
+ case E_INVALID_ARG:
+ PLOG_ERROR << "noscrypt - error: An invalid argument was passed to the public key generation function.";
+ break;
+
+ case E_INVALID_CONTEXT:
+ PLOG_ERROR << "noscrypt - error: The NCContext struct is in an invalid state.";
+ break;
+
+ case E_ARGUMENT_OUT_OF_RANGE:
+ PLOG_ERROR << "noscrypt - error: An argument was outside the range of acceptable values.";
+ break;
+
+ case E_OPERATION_FAILED:
+ PLOG_ERROR << "noscrypt - error: Failed to generate the public key from the secret key.";
+ break;
+ }
+};
+
+#pragma endregion