OpenVPN 3 Core Library
Loading...
Searching...
No Matches
randbytestore.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#ifndef OPENVPN_RANDOM_RANDBYTESTORE_H
13#define OPENVPN_RANDOM_RANDBYTESTORE_H
14
16
17namespace openvpn {
18
19template <typename RAND_TYPE>
21{
22 public:
23 static constexpr size_t SIZE = sizeof(typename RAND_TYPE::result_type);
24
25 unsigned char get_byte(RAND_TYPE &rng)
26 {
27 if (n_bytes == 0)
28 {
29 res.rt = rng();
30 n_bytes = SIZE;
31 }
32 unsigned char ret = res.bytes[0];
33 res.rt >>= 8;
34 --n_bytes;
35 return ret;
36 }
37
38 template <typename T>
39 void fill(T &obj, RAND_TYPE &rng)
40 {
41 unsigned char *data = reinterpret_cast<unsigned char *>(&obj);
42 for (size_t i = 0; i < sizeof(obj); ++i)
43 data[i] = get_byte(rng);
44 }
45
46 private:
47 union Result {
48 unsigned char bytes[SIZE];
49 typename RAND_TYPE::result_type rt;
50 };
51
53 unsigned int n_bytes = 0;
54};
55
56} // namespace openvpn
57#endif
unsigned char get_byte(RAND_TYPE &rng)
void fill(T &obj, RAND_TYPE &rng)
static constexpr size_t SIZE
std::string ret