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