random.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2024 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #include "ndn-cxx/util/random.hpp"
24 
25 #include <openssl/err.h>
26 #include <openssl/rand.h>
27 
28 namespace ndn::random {
29 
30 uint32_t
32 {
33  uint32_t random;
34  generateSecureBytes({reinterpret_cast<uint8_t*>(&random), sizeof(random)});
35  return random;
36 }
37 
38 uint64_t
40 {
41  uint64_t random;
42  generateSecureBytes({reinterpret_cast<uint8_t*>(&random), sizeof(random)});
43  return random;
44 }
45 
46 void
47 generateSecureBytes(span<uint8_t> buf)
48 {
49  if (RAND_bytes(buf.data(), buf.size()) != 1) {
50  NDN_THROW(std::runtime_error("Failed to generate random bytes (error code " +
51  std::to_string(ERR_get_error()) + ")"));
52  }
53 }
54 
57 {
58  thread_local std::mt19937 rng = [] {
59  std::random_device rd;
60  // seed with 256 bits of entropy
61  std::seed_seq seeds{rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()};
62  return std::mt19937{seeds};
63  }();
64  return rng;
65 }
66 
67 uint32_t
69 {
70  thread_local std::uniform_int_distribution<uint32_t> distribution;
71  return distribution(getRandomNumberEngine());
72 }
73 
74 uint64_t
76 {
77  thread_local std::uniform_int_distribution<uint64_t> distribution;
78  return distribution(getRandomNumberEngine());
79 }
80 
81 } // namespace ndn::random
#define NDN_THROW(e)
Definition: exception.hpp:56
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:30
uint32_t generateSecureWord32()
Generate a cryptographically secure random integer in the range [0, 2^32).
Definition: random.cpp:31
uint32_t generateWord32()
Generate a non-cryptographically-secure random integer in the range [0, 2^32).
Definition: random.cpp:68
void generateSecureBytes(span< uint8_t > buf)
Fill buffer with cryptographically secure random bytes.
Definition: random.cpp:47
std::mt19937 RandomNumberEngine
Definition: random.hpp:56
uint64_t generateSecureWord64()
Generate a cryptographically secure random integer in the range [0, 2^64).
Definition: random.cpp:39
uint64_t generateWord64()
Generate a non-cryptographically-secure random integer in the range [0, 2^64).
Definition: random.cpp:75
RandomNumberEngine & getRandomNumberEngine()
Returns a reference to a thread-local instance of a properly seeded PRNG.
Definition: random.cpp:56