OpenVPN 3 Core Library
Loading...
Searching...
No Matches
mtrandapi.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
12// Non-cryptographic random number generator
13
14#ifndef OPENVPN_RANDOM_MTRANDAPI_H
15#define OPENVPN_RANDOM_MTRANDAPI_H
16
17#include <random>
18
22
23namespace openvpn {
24
25class MTRand : public WeakRandomAPI
26{
27 public:
28 OPENVPN_EXCEPTION(mtrand_error);
29
31 using rand_type = std::mt19937_64;
32
34 : rng(gen_seed(seed))
35 {
36 }
37
39 : rng(gen_seed())
40 {
41 }
42
43 MTRand(const rand_type::result_type seed)
44 : rng(seed)
45 {
46 }
47
48 // Random algorithm name
49 std::string name() const override
50 {
51 return "MTRand";
52 }
53
54 // Fill buffer with random bytes
55 void rand_bytes(unsigned char *buf, size_t size) override
56 {
57 while (size--)
58 *buf++ = rbs.get_byte(rng);
59 }
60
66 bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
67 {
68 rand_bytes(buf, size);
69 return true;
70 }
71
72 rand_type::result_type rand()
73 {
74 return rng();
75 }
76
77 private:
78 static rand_type::result_type gen_seed(RandomAPI &seed)
79 {
80 return seed.rand_get<rand_type::result_type>();
81 }
82
83 static rand_type::result_type gen_seed()
84 {
85 std::random_device rd;
86 RandomByteStore<decltype(rd)> rbs;
87 rand_type::result_type ret;
88 rbs.fill(ret, rd);
89 return ret;
90 }
91
94};
95
96} // namespace openvpn
97
98#endif
static rand_type::result_type gen_seed(RandomAPI &seed)
Definition mtrandapi.hpp:78
static rand_type::result_type gen_seed()
Definition mtrandapi.hpp:83
MTRand(RandomAPI &seed)
Definition mtrandapi.hpp:33
bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
Definition mtrandapi.hpp:66
rand_type rng
Definition mtrandapi.hpp:92
std::string name() const override
Get the name of the random number generation algorithm.
Definition mtrandapi.hpp:49
OPENVPN_EXCEPTION(mtrand_error)
std::mt19937_64 rand_type
Definition mtrandapi.hpp:31
void rand_bytes(unsigned char *buf, size_t size) override
Fill a buffer with random bytes.
Definition mtrandapi.hpp:55
MTRand(const rand_type::result_type seed)
Definition mtrandapi.hpp:43
rand_type::result_type rand()
Definition mtrandapi.hpp:72
RandomByteStore< rand_type > rbs
Definition mtrandapi.hpp:93
The smart pointer class.
Definition rc.hpp:119
Abstract base class for random number generators.
Definition randapi.hpp:39
T rand_get()
Create a data object filled with random bytes.
Definition randapi.hpp:86
unsigned char get_byte(RAND_TYPE &rng)
void fill(T &obj, RAND_TYPE &rng)
Abstract base class for pseudo random number generators.
Definition randapi.hpp:243
Implementation of the base classes for random number generators.
std::string ret