blob: f6722b9d43a178c92976dfa23ebd1c2a9e78e70a [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev130076f2017-12-05 11:09:33 -05002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080020 */
21
22#include "random.hpp"
Yingdi Yu0b537b92015-08-27 14:39:34 -070023#include "../security/detail/openssl.hpp"
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080024
Davide Pesavento1cd9f6e2016-10-08 00:26:36 +020025#include <random>
Alexander Afanasyev75088672014-07-14 11:58:30 -070026
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080027namespace ndn {
28namespace random {
29
Alexander Afanasyev75088672014-07-14 11:58:30 -070030uint32_t
31generateSecureWord32()
32{
Yingdi Yu0b537b92015-08-27 14:39:34 -070033 uint32_t random;
34 generateSecureBytes(reinterpret_cast<uint8_t*>(&random), sizeof(random));
35 return random;
Alexander Afanasyev75088672014-07-14 11:58:30 -070036}
37
38uint64_t
39generateSecureWord64()
40{
41 uint64_t random;
Yingdi Yu0b537b92015-08-27 14:39:34 -070042 generateSecureBytes(reinterpret_cast<uint8_t*>(&random), sizeof(random));
Alexander Afanasyev75088672014-07-14 11:58:30 -070043 return random;
44}
45
Yingdi Yu0b537b92015-08-27 14:39:34 -070046void
47generateSecureBytes(uint8_t* bytes, size_t size)
48{
49 if (RAND_bytes(bytes, size) != 1) {
50 BOOST_THROW_EXCEPTION(std::runtime_error("Failed to generate random bytes (error code " +
Alexander Afanasyev130076f2017-12-05 11:09:33 -050051 to_string(ERR_get_error()) + ")"));
Yingdi Yu0b537b92015-08-27 14:39:34 -070052 }
53}
54
Davide Pesavento1cd9f6e2016-10-08 00:26:36 +020055static std::mt19937&
Alexander Afanasyev75088672014-07-14 11:58:30 -070056getRandomGenerator()
57{
Davide Pesavento1cd9f6e2016-10-08 00:26:36 +020058 static std::mt19937 rng{std::random_device{}()};
59 return rng;
Alexander Afanasyev75088672014-07-14 11:58:30 -070060}
61
62uint32_t
63generateWord32()
64{
Davide Pesavento1cd9f6e2016-10-08 00:26:36 +020065 static std::uniform_int_distribution<uint32_t> distribution;
Alexander Afanasyev75088672014-07-14 11:58:30 -070066 return distribution(getRandomGenerator());
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080067}
68
Yingdi Yu17bc3012014-02-10 17:37:12 -080069uint64_t
70generateWord64()
71{
Davide Pesavento1cd9f6e2016-10-08 00:26:36 +020072 static std::uniform_int_distribution<uint64_t> distribution;
Alexander Afanasyev75088672014-07-14 11:58:30 -070073 return distribution(getRandomGenerator());
Yingdi Yu17bc3012014-02-10 17:37:12 -080074}
75
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080076} // namespace random
77} // namespace ndn