summaryrefslogtreecommitdiff
path: root/include/ossl_random.hpp
diff options
context:
space:
mode:
authorEd Tanous <edtanous@google.com>2023-06-28 21:28:38 +0300
committerEd Tanous <ed@tanous.net>2023-07-12 22:48:32 +0300
commit2c6ffdb08b2207ff7c31041f77cc3755508d45c4 (patch)
tree5b2b58102221318866a06a11751d2efa76782fe4 /include/ossl_random.hpp
parent9eef578458f6dc3587bd27039f1ebb367041000b (diff)
downloadbmcweb-2c6ffdb08b2207ff7c31041f77cc3755508d45c4.tar.xz
Use openssl random number generator
We already have a generator class. We should use it. Wrap this into a function that can be unit tested, and add unit tests. Note, some files also needed to change name, because random.hpp conflicts with the built in random, and causes circular build problems. This commit changes it to ossl_random. Tested: Unit tests pass. Now has coverage. Redfish service validator passes. Change-Id: I5f8eee1af5f4843a352c6fd0e26d67fd3320ef53 Signed-off-by: Ed Tanous <edtanous@google.com>
Diffstat (limited to 'include/ossl_random.hpp')
-rw-r--r--include/ossl_random.hpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/include/ossl_random.hpp b/include/ossl_random.hpp
new file mode 100644
index 0000000000..2cbec849a9
--- /dev/null
+++ b/include/ossl_random.hpp
@@ -0,0 +1,52 @@
+#pragma once
+
+#include <openssl/rand.h>
+
+#include <iostream>
+#include <limits>
+#include <string>
+
+namespace bmcweb
+{
+
+struct OpenSSLGenerator
+{
+ uint8_t operator()()
+ {
+ uint8_t index = 0;
+ int rc = RAND_bytes(&index, sizeof(index));
+ if (rc != opensslSuccess)
+ {
+ std::cerr << "Cannot get random number\n";
+ err = true;
+ }
+
+ return index;
+ }
+
+ static constexpr uint8_t max()
+ {
+ return std::numeric_limits<uint8_t>::max();
+ }
+ static constexpr uint8_t min()
+ {
+ return std::numeric_limits<uint8_t>::min();
+ }
+
+ bool error() const
+ {
+ return err;
+ }
+
+ // all generators require this variable
+ using result_type = uint8_t;
+
+ private:
+ // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function
+ static constexpr int opensslSuccess = 1;
+ bool err = false;
+};
+
+std::string getRandomUUID();
+
+} // namespace bmcweb