OpenVPN 3 Core Library
Loading...
Searching...
No Matches
rand.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// Wrap the OpenSSL Cryptographic Random API defined in <openssl/rand.h>
13// so that it can be used as the primary source of cryptographic entropy by
14// the OpenVPN core.
15
16#ifndef OPENVPN_OPENSSL_UTIL_RAND_H
17#define OPENVPN_OPENSSL_UTIL_RAND_H
18
19#include <openssl/rand.h>
20
23
24namespace openvpn {
26{
27 public:
28 OPENVPN_EXCEPTION(rand_error_openssl);
29
31
32 OpenSSLRandom() = default;
33
34 std::string name() const override
35 {
36 return "OpenSSLRandom";
37 }
38
39 // Fill buffer with random bytes
40 void rand_bytes(unsigned char *buf, size_t size) override
41 {
42 if (!rndbytes(buf, size))
43 throw rand_error_openssl("rand_bytes");
44 }
45
46 // Like rand_bytes, but don't throw exception.
47 // Return true on successs, false on fail.
48 bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
49 {
50 return rndbytes(buf, size);
51 }
52
53 private:
54 bool rndbytes(unsigned char *buf, size_t size)
55 {
56 return is_safe_conversion<int>(size) ? RAND_bytes(buf, static_cast<int>(size)) == 1 : false;
57 }
58};
59} // namespace openvpn
60
61#endif
void rand_bytes(unsigned char *buf, size_t size) override
Fill a buffer with random bytes.
Definition rand.hpp:40
OPENVPN_EXCEPTION(rand_error_openssl)
bool rndbytes(unsigned char *buf, size_t size)
Definition rand.hpp:54
std::string name() const override
Get the name of the random number generation algorithm.
Definition rand.hpp:34
RCPtr< OpenSSLRandom > Ptr
Definition rand.hpp:30
bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
Fill a buffer with random bytes without throwing exceptions.
Definition rand.hpp:48
The smart pointer class.
Definition rc.hpp:119
Abstract base class for cryptographically strong random number generators.
Definition randapi.hpp:228
Implementation of the base classes for random number generators.