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 else
135 return start + rand_get_positive<T>() % (end - start + 1);
136 }
137
147 std::uint32_t randrange32(const std::uint32_t end)
148 {
149 std::uint32_t r;
150 rand_fill(r);
151 return rand32_distribute(r, end);
152 }
153
163 std::uint32_t randrange32(const std::uint32_t start, const std::uint32_t end)
164 {
165 if (start >= end)
166 return start;
167 else
168 return start + randrange32(end - start + 1);
169 }
170
175 std::uint8_t randbyte()
176 {
177 std::uint8_t byte;
178 rand_fill(byte);
179 return byte;
180 }
181
186 bool randbool()
187 {
188 return bool(randbyte() & 1);
189 }
190
199 typedef unsigned int result_type;
200 static constexpr result_type min()
201 {
202 return result_type(0);
203 }
204 static constexpr result_type max()
205 {
206 return ~result_type(0);
207 }
209 {
210 return rand_get<result_type>();
211 }
213
214 private:
215 friend class StrongRandomAPI;
216 friend class WeakRandomAPI;
217 RandomAPI() = default;
218};
219
228{
229 public:
235};
236
245{
246 public:
252};
253
254} // namespace openvpn
The smart pointer class.
Definition rc.hpp:119
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition rc.hpp:912
Abstract base class for random number generators.
Definition randapi.hpp:39
RCPtr< RandomAPI > Ptr
Smart pointer type for managing the ownership of RandomAPI objects.
Definition randapi.hpp:45
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:175
void rand_fill(T &obj)
Fill a data object with random bytes.
Definition randapi.hpp:75
unsigned int result_type
Definition randapi.hpp:199
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:163
result_type operator()()
Definition randapi.hpp:208
static constexpr result_type max()
Definition randapi.hpp:204
std::uint32_t randrange32(const std::uint32_t end)
Return a uniformly distributed random number in the range [0, end)
Definition randapi.hpp:147
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:200
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:186
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:228
RCPtr< StrongRandomAPI > Ptr
Smart pointer type for managing the ownership of StrongRandomAPI objects.
Definition randapi.hpp:234
Abstract base class for pseudo random number generators.
Definition randapi.hpp:245
RCPtr< WeakRandomAPI > Ptr
Smart pointer type for managing the ownership of WeakRandomAPI objects.
Definition randapi.hpp:251
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
std::uint32_t rand32_distribute(const std::uint32_t seed, const std::uint32_t end)
std::string ret