Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 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 "util/random.hpp" |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 23 | #include "security/detail/openssl.hpp" |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 25 | #include "boost-test.hpp" |
| 26 | |
| 27 | #include <boost/mpl/vector.hpp> |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 28 | #include <cmath> |
| 29 | |
| 30 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 31 | namespace tests { |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 32 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(Util) |
| 34 | BOOST_AUTO_TEST_SUITE(TestRandom) |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 35 | |
| 36 | class PseudoRandomWord32 |
| 37 | { |
| 38 | public: |
| 39 | static uint32_t |
| 40 | generate() |
| 41 | { |
| 42 | return random::generateWord32(); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | class PseudoRandomWord64 |
| 47 | { |
| 48 | public: |
| 49 | static uint64_t |
| 50 | generate() |
| 51 | { |
| 52 | return random::generateWord64(); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | class SecureRandomWord32 |
| 57 | { |
| 58 | public: |
| 59 | static uint32_t |
| 60 | generate() |
| 61 | { |
| 62 | return random::generateSecureWord32(); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | class SecureRandomWord64 |
| 67 | { |
| 68 | public: |
| 69 | static uint64_t |
| 70 | generate() |
| 71 | { |
| 72 | return random::generateSecureWord64(); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | typedef boost::mpl::vector<PseudoRandomWord32, |
| 77 | PseudoRandomWord64, |
| 78 | SecureRandomWord32, |
| 79 | SecureRandomWord64> RandomGenerators; |
| 80 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 81 | |
| 82 | static double |
| 83 | getDeviation(const std::vector<uint32_t>& counts, size_t size) |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 84 | { |
| 85 | // Kolmogorov-Smirnov Goodness-of-Fit Test |
| 86 | // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm |
| 87 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 88 | std::vector<double> edf(counts.size(), 0.0); |
| 89 | double probability = 0.0; |
| 90 | for (size_t i = 0; i < counts.size(); i++) { |
| 91 | probability += 1.0 * counts[i] / size; |
| 92 | edf[i] = probability; |
| 93 | } |
| 94 | |
| 95 | double t = 0.0; |
| 96 | for (size_t i = 0; i < counts.size(); i++) { |
| 97 | t = std::max(t, std::abs(edf[i] - (i * 1.0 / counts.size()))); |
| 98 | } |
| 99 | |
| 100 | return t; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | BOOST_AUTO_TEST_CASE_TEMPLATE(GoodnessOfFit, RandomGenerator, RandomGenerators) |
| 105 | { |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 106 | const size_t MAX_BINS = 32; |
| 107 | const uint32_t MAX_ITERATIONS = 35; |
| 108 | |
| 109 | std::vector<uint32_t> counts(MAX_BINS, 0); |
| 110 | |
| 111 | for (uint32_t i = 0; i < MAX_ITERATIONS; i++) { |
| 112 | counts[RandomGenerator::generate() % MAX_BINS]++; |
| 113 | } |
| 114 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 115 | // Check if it is uniform distribution with confidence 0.95 |
| 116 | // http://dlc.erieri.com/onlinetextbook/index.cfm?fuseaction=textbook.appendix&FileName=Table7 |
| 117 | BOOST_WARN_LE(getDeviation(counts, MAX_ITERATIONS), 0.230); |
| 118 | } |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 119 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 120 | BOOST_AUTO_TEST_CASE(GenerateRandomBytes) |
| 121 | { |
| 122 | // Kolmogorov-Smirnov Goodness-of-Fit Test |
| 123 | // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm |
| 124 | |
| 125 | uint8_t buf[1024] = {0}; |
| 126 | random::generateSecureBytes(buf, sizeof(buf)); |
| 127 | |
| 128 | std::vector<uint32_t> counts(256, 0); |
| 129 | |
| 130 | for (size_t i = 0; i < sizeof(buf); i++) { |
| 131 | counts[buf[i]]++; |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Check if it is uniform distribution with confidence 0.95 |
| 135 | // http://dlc.erieri.com/onlinetextbook/index.cfm?fuseaction=textbook.appendix&FileName=Table7 |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 136 | BOOST_WARN_LE(getDeviation(counts, sizeof(buf)), 0.230); |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 139 | // This fixture uses OpenSSL routines to set a dummy random generator that always fails |
| 140 | class FailRandMethodFixture |
| 141 | { |
| 142 | public: |
| 143 | FailRandMethodFixture() |
| 144 | : m_dummyRandMethod{&FailRandMethodFixture::seed, |
| 145 | &FailRandMethodFixture::bytes, |
| 146 | &FailRandMethodFixture::cleanup, |
| 147 | &FailRandMethodFixture::add, |
| 148 | &FailRandMethodFixture::pseudorand, |
| 149 | &FailRandMethodFixture::status} |
| 150 | { |
| 151 | m_origRandMethod = RAND_get_rand_method(); |
| 152 | RAND_set_rand_method(&m_dummyRandMethod); |
| 153 | } |
| 154 | |
| 155 | ~FailRandMethodFixture() |
| 156 | { |
| 157 | RAND_set_rand_method(m_origRandMethod); |
| 158 | } |
| 159 | |
| 160 | private: // RAND_METHOD callbacks |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 161 | #if OPENSSL_VERSION_NUMBER < 0x1010000fL |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 162 | static void |
| 163 | seed(const void* buf, int num) |
| 164 | { |
| 165 | } |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 166 | #else |
| 167 | static int |
| 168 | seed(const void* buf, int num) |
| 169 | { |
| 170 | return 0; |
| 171 | } |
| 172 | #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 173 | |
| 174 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 175 | bytes(unsigned char* buf, int num) |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 176 | { |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static void |
| 181 | cleanup() |
| 182 | { |
| 183 | } |
| 184 | |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 185 | #if OPENSSL_VERSION_NUMBER < 0x1010000fL |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 186 | static void |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 187 | add(const void* buf, int num, double entropy) |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 188 | { |
| 189 | } |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 190 | #else |
| 191 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 192 | add(const void* buf, int num, double entropy) |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 193 | { |
| 194 | return 0; |
| 195 | } |
| 196 | #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 197 | |
| 198 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 199 | pseudorand(unsigned char* buf, int num) |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 200 | { |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int |
| 205 | status() |
| 206 | { |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | private: |
| 211 | const RAND_METHOD* m_origRandMethod; |
| 212 | RAND_METHOD m_dummyRandMethod; |
| 213 | }; |
| 214 | |
| 215 | BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture) |
| 216 | { |
| 217 | uint8_t buf[1024] = {0}; |
| 218 | BOOST_CHECK_THROW(random::generateSecureBytes(buf, sizeof(buf)), std::runtime_error); |
| 219 | } |
| 220 | |
| 221 | BOOST_AUTO_TEST_SUITE_END() // TestRandom |
| 222 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 223 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 224 | } // namespace tests |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 225 | } // namespace ndn |