xref: /openbmc/bmcweb/include/ossl_random.hpp (revision c160ae72)
1 #pragma once
2 
3 #include "logging.hpp"
4 
5 #include <openssl/rand.h>
6 
7 #include <iostream>
8 #include <limits>
9 #include <string>
10 
11 namespace bmcweb
12 {
13 
14 struct OpenSSLGenerator
15 {
16     uint8_t operator()()
17     {
18         uint8_t index = 0;
19         int rc = RAND_bytes(&index, sizeof(index));
20         if (rc != opensslSuccess)
21         {
22             BMCWEB_LOG_ERROR("Cannot get random number");
23             err = true;
24         }
25 
26         return index;
27     }
28 
29     static constexpr uint8_t max()
30     {
31         return std::numeric_limits<uint8_t>::max();
32     }
33     static constexpr uint8_t min()
34     {
35         return std::numeric_limits<uint8_t>::min();
36     }
37 
38     bool error() const
39     {
40         return err;
41     }
42 
43     // all generators require this variable
44     using result_type = uint8_t;
45 
46   private:
47     // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function
48     static constexpr int opensslSuccess = 1;
49     bool err = false;
50 };
51 
52 std::string getRandomUUID();
53 
54 } // namespace bmcweb
55