xref: /openbmc/bmcweb/include/ossl_random.hpp (revision 724985ff)
1 #pragma once
2 
3 #include "logging.hpp"
4 
5 #include <limits>
6 #include <string>
7 
8 namespace bmcweb
9 {
10 
11 struct OpenSSLGenerator
12 {
13     uint8_t operator()();
14 
maxbmcweb::OpenSSLGenerator15     static constexpr uint8_t max()
16     {
17         return std::numeric_limits<uint8_t>::max();
18     }
minbmcweb::OpenSSLGenerator19     static constexpr uint8_t min()
20     {
21         return std::numeric_limits<uint8_t>::min();
22     }
23 
errorbmcweb::OpenSSLGenerator24     bool error() const
25     {
26         return err;
27     }
28 
29     // all generators require this variable
30     using result_type = uint8_t;
31 
32   private:
33     // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function
34     static constexpr int opensslSuccess = 1;
35     bool err = false;
36 };
37 
38 std::string getRandomUUID();
39 
40 std::string getRandomIdOfLength(size_t length);
41 
42 bool constantTimeStringCompare(std::string_view a, std::string_view b);
43 struct ConstantTimeCompare
44 {
45     bool operator()(std::string_view a, std::string_view b) const;
46 };
47 
48 } // namespace bmcweb
49