aboutsummaryrefslogtreecommitdiff
path: root/src/cryptography/nostr_secure_rng.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/cryptography/nostr_secure_rng.cpp')
-rw-r--r--src/cryptography/nostr_secure_rng.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/cryptography/nostr_secure_rng.cpp b/src/cryptography/nostr_secure_rng.cpp
new file mode 100644
index 0000000..9d92514
--- /dev/null
+++ b/src/cryptography/nostr_secure_rng.cpp
@@ -0,0 +1,46 @@
+#include <plog/Init.h>
+#include <plog/Log.h>
+
+#include <openssl/evp.h>
+#include <openssl/rand.h>
+#include <openssl/crypto.h>
+
+#include "nostr_secure_rng.hpp"
+
+using namespace std;
+using namespace nostr::cryptography;
+
+void NostrSecureRng::fill(void* buffer, size_t length)
+{
+ if (RAND_bytes((uint8_t*)buffer, length) != 1)
+ {
+ //TODO throw runtime exception
+ PLOG_ERROR << "Failed to generate random bytes";
+ }
+}
+
+inline void NostrSecureRng::fill(vector<uint8_t>& buffer)
+{
+ fill(buffer.data(), buffer.size());
+}
+
+void NostrSecureRng::reseed(uint32_t bufferSize)
+{
+ int rc = RAND_load_file("/dev/random", bufferSize);
+
+ if (rc != bufferSize)
+ {
+ PLOG_WARNING << "Failed to reseed the RNG with /dev/random, falling back to /dev/urandom.";
+ RAND_poll();
+ }
+}
+
+void NostrSecureRng::zero(void* buffer, size_t length)
+{
+ OPENSSL_cleanse(buffer, length);
+}
+
+inline void NostrSecureRng::zero(vector<uint8_t>& buffer)
+{
+ zero(buffer.data(), buffer.size());
+} \ No newline at end of file