diff options
author | buttercat1791 <mjjurkoic@gmail.com> | 2024-05-28 08:46:49 -0500 |
---|---|---|
committer | buttercat1791 <mjjurkoic@gmail.com> | 2024-05-28 08:46:49 -0500 |
commit | 9db92687fecd7528a6aa93e60bcbebc41342378c (patch) | |
tree | 3649f2bb6d87178eb1ac67d3c610c1ea9719bd99 /src | |
parent | b61ff680f2f16fe9b86da5f8c9dde78a926d8997 (diff) |
Validate secret key after generating it
Diffstat (limited to 'src')
-rw-r--r-- | src/signer/noscrypt_signer.cpp | 148 |
1 files changed, 110 insertions, 38 deletions
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 |