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 AppleRandom() = default;
32
33 std::string name() const override
34 {
35 return "AppleRandom";
36 }
37
38 // Fill buffer with random bytes
39 void rand_bytes(unsigned char *buf, size_t size) override
40 {
41 if (!rndbytes(buf, size))
42 throw rand_error_apple("rand_bytes");
43 }
44
45 // Like rand_bytes, but don't throw exception.
46 // Return true on successs, false on fail.
47 bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
48 {
49 return rndbytes(buf, size);
50 }
51
52 private:
53 bool rndbytes(unsigned char *buf, size_t size)
54 {
55 return SecRandomCopyBytes(kSecRandomDefault, size, buf) ? false : true;
56 }
57};
58} // namespace openvpn
59
60#endif
bool rndbytes(unsigned char *buf, size_t size)
Definition rand.hpp:53
bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
Fill a buffer with random bytes without throwing exceptions.
Definition rand.hpp:47
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:39
std::string name() const override
Get the name of the random number generation algorithm.
Definition rand.hpp:33
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:228
Implementation of the base classes for random number generators.