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 mbed TLS Cryptographic Random API defined in <mbedtls/ctr_drbg.h>
13// so that it can be used as the primary source of cryptographic entropy by
14// the OpenVPN core.
15
16#ifndef OPENVPN_MBEDTLS_UTIL_RAND_H
17#define OPENVPN_MBEDTLS_UTIL_RAND_H
18
19#include <mbedtls/entropy.h>
20#if MBEDTLS_VERSION_NUMBER < 0x03000000
21#include <mbedtls/entropy_poll.h>
22#endif
23#include <mbedtls/ctr_drbg.h>
24
27
28namespace openvpn {
29
31{
32 public:
33 OPENVPN_EXCEPTION(rand_error_mbedtls);
34
36
37
39 : entropy(std::move(entropy_source))
40 {
41 // Init RNG context
42 mbedtls_ctr_drbg_init(&ctx);
43
44 // Seed RNG
45 const int errnum = mbedtls_ctr_drbg_seed(&ctx, entropy_poll, entropy.get(), nullptr, 0);
46 if (errnum < 0)
47 throw MbedTLSException("mbedtls_ctr_drbg_seed", errnum);
48 }
49
54
56 {
57 // Free RNG context
58 mbedtls_ctr_drbg_free(&ctx);
59 }
60
61 // Random algorithm name
62 std::string name() const override
63 {
64 const std::string n = "mbedTLS-CTR_DRBG";
65 if (entropy)
66 return n + '+' + entropy->name();
67 else
68 return n;
69 }
70
71 // Fill buffer with random bytes
72 void rand_bytes(unsigned char *buf, size_t size) override
73 {
74 const int errnum = rndbytes(buf, size);
75 if (errnum < 0)
76 throw MbedTLSException("mbedtls_ctr_drbg_random", errnum);
77 }
78
79 // Like rand_bytes, but don't throw exception.
80 // Return true on successs, false on fail.
81 bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
82 {
83 return rndbytes(buf, size) >= 0;
84 }
85
92 mbedtls_ctr_drbg_context *get_ctr_drbg_ctx()
93 {
94 return &ctx;
95 }
96
97 private:
98 int rndbytes(unsigned char *buf, size_t size)
99 {
100 return mbedtls_ctr_drbg_random(&ctx, buf, size);
101 }
102
103 static int entropy_poll(void *arg, unsigned char *output, size_t len)
104 {
105 if (arg)
106 {
107 RandomAPI *entropy = (RandomAPI *)arg;
108 if (entropy->rand_bytes_noexcept(output, len))
109 return 0;
110 else
111 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
112 }
113 else
114 {
115#ifndef OPENVPN_DISABLE_MBEDTLS_PLATFORM_ENTROPY_POLL
116 size_t olen;
117 return mbedtls_platform_entropy_poll(nullptr, output, len, &olen);
118#else
119 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
120#endif
121 }
122 }
123
124 mbedtls_ctr_drbg_context ctx;
126};
127
128} // namespace openvpn
129
130#endif
OPENVPN_EXCEPTION(rand_error_mbedtls)
void rand_bytes(unsigned char *buf, size_t size) override
Fill a buffer with random bytes.
Definition rand.hpp:72
int rndbytes(unsigned char *buf, size_t size)
Definition rand.hpp:98
mbedtls_ctr_drbg_context ctx
Definition rand.hpp:124
MbedTLSRandom(StrongRandomAPI::Ptr entropy_source)
Definition rand.hpp:38
std::string name() const override
Get the name of the random number generation algorithm.
Definition rand.hpp:62
static int entropy_poll(void *arg, unsigned char *output, size_t len)
Definition rand.hpp:103
RandomAPI::Ptr entropy
Definition rand.hpp:125
mbedtls_ctr_drbg_context * get_ctr_drbg_ctx()
Definition rand.hpp:92
bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
Fill a buffer with random bytes without throwing exceptions.
Definition rand.hpp:81
RCPtr< MbedTLSRandom > Ptr
Definition rand.hpp:35
virtual ~MbedTLSRandom()
Definition rand.hpp:55
The smart pointer class.
Definition rc.hpp:119
T * get() const noexcept
Returns the raw pointer to the object T, or nullptr.
Definition rc.hpp:321
Abstract base class for random number generators.
Definition randapi.hpp:39
virtual std::string name() const =0
Get the name of the random number generation algorithm.
virtual bool rand_bytes_noexcept(unsigned char *buf, size_t size)=0
Fill a buffer with random bytes without throwing exceptions.
Abstract base class for cryptographically strong random number generators.
Definition randapi.hpp:228
Implementation of the base classes for random number generators.