scclib
Stable Cloud Computing C++ Library
random.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_CRYPTO_RANDOM_H
32 #define _SCC_CRYPTO_RANDOM_H
33 
34 #include <climits>
35 #include <string>
36 
37 namespace scc::crypto {
38 
58 {
59 public:
60  RandomEngine() {}
61 
63  static void* ctx();
64 
66  static void lock();
67 
69  static void unlock();
70 
72  static void random_seed();
73 
75  static void seed(const void *, int len);
77  static void seed(const std::string& buf)
78  {
79  seed((void*)buf.data(), buf.size());
80  }
81 
83  static void rand_bytes(void *, int len);
85  static uint32_t rand_uint32()
86  {
87  uint32_t r;
88  rand_bytes(&r, sizeof(uint32_t));
89  return r;
90  }
92  static int32_t rand_int32()
93  {
94  int32_t r;
95  rand_bytes(&r, sizeof(int32_t));
96  return r;
97  }
99  static uint64_t rand_uint64()
100  {
101  uint64_t r;
102  rand_bytes(&r, sizeof(uint64_t));
103  return r;
104  }
106  static int64_t rand_int64()
107  {
108  int64_t r;
109  rand_bytes(&r, sizeof(int64_t));
110  return r;
111  }
112 
114  typedef int result_type;
117  {
118  return rand_uint32();
119  }
121  constexpr int min() { return 0; }
123  constexpr int max() { return UINT_MAX; }
124 };
125 
131 {
132  bool m_lk;
133 public:
134  RandomEngineLocker() : m_lk(false)
135  {
136  lock();
137  }
139  {
140  unlock();
141  }
143  void lock()
144  {
145  if (!m_lk)
146  {
148  m_lk = true;
149  }
150  }
152  void unlock()
153  {
154  if (m_lk)
155  {
157  m_lk = false;
158  }
159  }
160 };
164 } // namespace
165 
166 #endif
Helper to safely lock the engine.
Definition: random.h:131
void lock()
Lock the engine.
Definition: random.h:143
void unlock()
Unlock the engine.
Definition: random.h:152
The random number generator is initialized with random entropy on first use.
Definition: random.h:58
int result_type
Required for UniformRandomBitGenerator interface.
Definition: random.h:114
static int64_t rand_int64()
Generate random long word.
Definition: random.h:106
static void lock()
Lock the number generator while the opaque reference is in use.
static void random_seed()
Generate a random seed and seed the generator with it.
int operator()()
Required for UniformRandomBitGenerator interface.
Definition: random.h:116
static void seed(const void *, int len)
Seed the generator.
static uint32_t rand_uint32()
Generate random word.
Definition: random.h:85
constexpr int max()
Required for UniformRandomBitGenerator interface.
Definition: random.h:123
constexpr int min()
Required for UniformRandomBitGenerator interface.
Definition: random.h:121
static void * ctx()
Opaque reference to the number generator.
static uint64_t rand_uint64()
Generate random long word.
Definition: random.h:99
static int32_t rand_int32()
Generate random word.
Definition: random.h:92
static void seed(const std::string &buf)
Seed the generator.
Definition: random.h:77
static void unlock()
Unlock the number generator.
static void rand_bytes(void *, int len)
Generate random bytes.