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 typedef std::mt19937_64 rand_type;
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 if (!rndbytes(buf, size))
58 throw mtrand_error("rand_bytes failed");
59 }
60
61 // Like rand_bytes, but don't throw exception.
62 // Return true on successs, false on fail.
63 bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
64 {
65 return rndbytes(buf, size);
66 }
67
68 rand_type::result_type rand()
69 {
70 return rng();
71 }
72
73 private:
74 bool rndbytes(unsigned char *buf, size_t size)
75 {
76 while (size--)
77 *buf++ = rbs.get_byte(rng);
78 return true;
79 }
80
81 static rand_type::result_type gen_seed(RandomAPI &seed)
82 {
83 return seed.rand_get<rand_type::result_type>();
84 }
85
86 static rand_type::result_type gen_seed()
87 {
88 std::random_device rd;
89 RandomByteStore<decltype(rd)> rbs;
90 rand_type::result_type ret;
91 rbs.fill(ret, rd);
92 return ret;
93 }
94
97};
98
99} // namespace openvpn
100
101#endif
static rand_type::result_type gen_seed(RandomAPI &seed)
Definition mtrandapi.hpp:81
static rand_type::result_type gen_seed()
Definition mtrandapi.hpp:86
MTRand(RandomAPI &seed)
Definition mtrandapi.hpp:33
RCPtr< MTRand > Ptr
Definition mtrandapi.hpp:30
bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
Fill a buffer with random bytes without throwing exceptions.
Definition mtrandapi.hpp:63
rand_type rng
Definition mtrandapi.hpp:95
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:68
bool rndbytes(unsigned char *buf, size_t size)
Definition mtrandapi.hpp:74
RandomByteStore< rand_type > rbs
Definition mtrandapi.hpp:96
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:245
Implementation of the base classes for random number generators.
std::string ret