OpenVPN 3 Core Library
Loading...
Searching...
No Matches
randapi.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
17#pragma once
18
19#include <cstdint>
20#include <limits>
21#include <string>
22#include <type_traits>
23
25#include <openvpn/common/rc.hpp>
28
29namespace openvpn {
30
38class RandomAPI : public RC<thread_unsafe_refcount>
39{
40 public:
46
51 virtual std::string name() const = 0;
52
58 virtual void rand_bytes(unsigned char *buf, size_t size) = 0;
59
67 virtual bool rand_bytes_noexcept(unsigned char *buf, size_t size) = 0;
68
74 template <typename T>
75 void rand_fill(T &obj)
76 {
77 rand_bytes(reinterpret_cast<unsigned char *>(&obj), sizeof(T));
78 }
79
85 template <typename T>
87 {
88 T ret;
90 return ret;
91 }
92
98 template <typename T>
100 {
101 T ret = rand_get<T>();
102 if constexpr (std::is_signed_v<T>)
103 {
104 // maps (T:min, -1) to (0, T:max) which is fine for random generation
105 ret &= std::numeric_limits<T>::max();
106 }
107 return ret;
108 }
109
116 template <typename T>
117 T randrange(const T end)
118 {
119 return rand_get_positive<T>() % end;
120 }
121
129 template <typename T>
130 T randrange(const T start, const T end)
131 {
132 if (start >= end)
133 return start;
134 return start + rand_get_positive<T>() % (end - start + 1);
135 }
136
146 std::uint32_t randrange32(const std::uint32_t end)
147 {
148 std::uint32_t r;
149 rand_fill(r);
150 return rand32_distribute(r, end);
151 }
152
162 std::uint32_t randrange32(const std::uint32_t start, const std::uint32_t end)
163 {
164 if (start >= end)
165 return start;
166 return start + randrange32(end - start + 1);
167 }
168
173 std::uint8_t randbyte()
174 {
175 std::uint8_t byte;
176 rand_fill(byte);
177 return byte;
178 }
179
184 bool randbool()
185 {
186 return bool(randbyte() & 1);
187 }
188
197 using result_type = unsigned int;
198 static constexpr result_type min()
199 {
200 return result_type(0);
201 }
202 static constexpr result_type max()
203 {
204 return ~result_type(0);
205 }
207 {
208 return rand_get<result_type>();
209 }
211
212 private:
213 friend class StrongRandomAPI;
214 friend class WeakRandomAPI;
215 RandomAPI() = default;
216};
217
226{
227 public:
233};
234
243{
244 public:
250};
251
252} // namespace openvpn
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition rc.hpp:908
Abstract base class for random number generators.
Definition randapi.hpp:39
unsigned int result_type
Definition randapi.hpp:197
T rand_get()
Create a data object filled with random bytes.
Definition randapi.hpp:86
virtual std::string name() const =0
Get the name of the random number generation algorithm.
T randrange(const T start, const T end)
Return a uniformly distributed random number in the range [start, end].
Definition randapi.hpp:130
virtual bool rand_bytes_noexcept(unsigned char *buf, size_t size)=0
Fill a buffer with random bytes without throwing exceptions.
std::uint8_t randbyte()
Return a random byte.
Definition randapi.hpp:173
void rand_fill(T &obj)
Fill a data object with random bytes.
Definition randapi.hpp:75
std::uint32_t randrange32(const std::uint32_t start, const std::uint32_t end)
Return a uniformly distributed random number in the range [start, end].
Definition randapi.hpp:162
result_type operator()()
Definition randapi.hpp:206
static constexpr result_type max()
Definition randapi.hpp:202
std::uint32_t randrange32(const std::uint32_t end)
Return a uniformly distributed random number in the range [0, end)
Definition randapi.hpp:146
T rand_get_positive()
Create a data object filled with random bytes, always >= 0 for signed types.
Definition randapi.hpp:99
static constexpr result_type min()
Definition randapi.hpp:198
virtual void rand_bytes(unsigned char *buf, size_t size)=0
Fill a buffer with random bytes.
bool randbool()
Return a random boolean.
Definition randapi.hpp:184
T randrange(const T end)
Return a uniformly distributed random number in the range [0, end)
Definition randapi.hpp:117
Abstract base class for cryptographically strong random number generators.
Definition randapi.hpp:226
Abstract base class for pseudo random number generators.
Definition randapi.hpp:243
std::uint32_t rand32_distribute(const std::uint32_t seed, const std::uint32_t end)
std::string ret