mirror of
https://github.com/OpenVPN/openvpn3.git
synced 2024-09-20 04:02:15 +02:00
random: factor out rand32_distribute() from RandomAPI::randrange32()
This is done so that rand32_distribute() can be used for hash seeds as well. Signed-off-by: James Yonan <james@openvpn.net>
This commit is contained in:
parent
90123495a5
commit
60501b4513
@ -30,6 +30,7 @@
|
||||
#include <openvpn/common/size.hpp>
|
||||
#include <openvpn/common/rc.hpp>
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/random/randistrib.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
|
||||
@ -85,16 +86,6 @@ namespace openvpn {
|
||||
return rand_get_positive<T>() % end;
|
||||
}
|
||||
|
||||
// Return a uniformly distributed random number in the range [0, end).
|
||||
// This version is strictly 32-bit only and optimizes by avoiding
|
||||
// integer division.
|
||||
std::uint32_t randrange32(const std::uint32_t end)
|
||||
{
|
||||
std::uint32_t r;
|
||||
rand_fill(r);
|
||||
return (std::uint64_t(r) * end) >> 32;
|
||||
}
|
||||
|
||||
// Return a uniformly distributed random number in the range [start, end].
|
||||
template <typename T>
|
||||
T randrange(const T start, const T end)
|
||||
@ -105,6 +96,27 @@ namespace openvpn {
|
||||
return start + rand_get_positive<T>() % (end - start + 1);
|
||||
}
|
||||
|
||||
// Return a uniformly distributed random number in the range [0, end).
|
||||
// This version is strictly 32-bit only and optimizes by avoiding
|
||||
// integer division.
|
||||
std::uint32_t randrange32(const std::uint32_t end)
|
||||
{
|
||||
std::uint32_t r;
|
||||
rand_fill(r);
|
||||
return rand32_distribute(r, end);
|
||||
}
|
||||
|
||||
// Return a uniformly distributed random number in the range [start, end].
|
||||
// This version is strictly 32-bit only and optimizes by avoiding
|
||||
// integer division.
|
||||
std::uint32_t randrange32(const std::uint32_t start, const std::uint32_t end)
|
||||
{
|
||||
if (start >= end)
|
||||
return start;
|
||||
else
|
||||
return start + randrange32(end - start + 1);
|
||||
}
|
||||
|
||||
// Throw an exception if algorithm is not crypto-strength.
|
||||
// Be sure to always call this method before using an rng
|
||||
// for crypto purposes.
|
||||
|
37
openvpn/random/randistrib.hpp
Normal file
37
openvpn/random/randistrib.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2019 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace openvpn {
|
||||
|
||||
// Return a uniformly distributed random number in the range [0, end)
|
||||
// using seed as a random seed. This version is strictly 32-bit only
|
||||
// and optimizes by avoiding integer division.
|
||||
inline std::uint32_t rand32_distribute(const std::uint32_t seed,
|
||||
const std::uint32_t end)
|
||||
{
|
||||
return (std::uint64_t(seed) * end) >> 32;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user