OpenVPN 3 Core Library
Loading...
Searching...
No Matches
skew.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#pragma once
13
14#include <algorithm>
15
16#include <openvpn/time/time.hpp>
18
19namespace openvpn {
20
22{
23 // Skew factors (+/- %).
24 // Pass these to skew() via factor parameter.
25 enum
26 {
27 PCT_50 = 0,
28 PCT_25 = 1,
33 };
34
35 // Skew a duration by some random flux.
36 static Time::Duration skew(const Time::Duration &dur, const unsigned int factor, RandomAPI &prng)
37 {
38 const std::uint32_t bms = static_cast<uint32_t>(std::min(dur.to_binary_ms() >> factor, oulong(0x40000000))); // avoid 32-bit overflow in next step
39 const int flux = int(prng.randrange32(bms)) - int(bms / 2);
40 return dur + flux;
41 }
42};
43
44} // namespace openvpn
Abstract base class for random number generators.
Definition randapi.hpp:39
std::uint32_t randrange32(const std::uint32_t end)
Return a uniformly distributed random number in the range [0, end)
Definition randapi.hpp:147
unsigned long oulong
Definition olong.hpp:24
Implementation of the base classes for random number generators.
static Time::Duration skew(const Time::Duration &dur, const unsigned int factor, RandomAPI &prng)
Definition skew.hpp:36