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 unsigned char get_byte(RAND_TYPE &rng)
24 {
25 if (n_bytes == 0)
26 {
27 res = rng();
28 n_bytes = sizeof(res);
29 }
30 const unsigned char ret = static_cast<unsigned char>(res);
31 res >>= 8;
32 --n_bytes;
33 return ret;
34 }
35
36 template <typename T>
37 void fill(T &obj, RAND_TYPE &rng)
38 {
39 unsigned char *data = reinterpret_cast<unsigned char *>(&obj);
40 for (size_t i = 0; i < sizeof(obj); ++i)
41 data[i] = get_byte(rng);
42 }
43
44 private:
45 typename RAND_TYPE::result_type res = 0;
46 unsigned int n_bytes = 0;
47};
48
49} // namespace openvpn
50#endif
RAND_TYPE::result_type res
unsigned char get_byte(RAND_TYPE &rng)
void fill(T &obj, RAND_TYPE &rng)