prng-testhelper.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*############################################################################
  2. # Copyright 2016-2017 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \file
  18. * \brief Pseudo random number generator interface.
  19. */
  20. #ifndef EPID_COMMON_TESTHELPER_PRNG_TESTHELPER_H_
  21. #define EPID_COMMON_TESTHELPER_PRNG_TESTHELPER_H_
  22. #if defined(_WIN32) || defined(_WIN64)
  23. #define __STDCALL __stdcall
  24. #else
  25. #define __STDCALL
  26. #endif
  27. #include <limits.h> // for CHAR_BIT
  28. #include <stdint.h>
  29. #include <random>
  30. #include <vector>
  31. extern "C" {
  32. #include "epid/common/types.h"
  33. }
  34. /// Return status for Prng Generate function
  35. typedef enum {
  36. kPrngNoErr = 0, //!< no error
  37. kPrngErr = -999, //!< unspecified error
  38. kPrngNotImpl, //!< not implemented error
  39. kPrngBadArgErr //!< incorrect arg to function
  40. } PrngStatus;
  41. /// Pseudo random number generator (prng) class.
  42. class Prng {
  43. public:
  44. Prng() : seed_(1) { set_seed(seed_); }
  45. ~Prng() {}
  46. /// Retrieve seed
  47. unsigned int get_seed() const { return seed_; }
  48. /// Set seed for random number generator
  49. void set_seed(unsigned int val) {
  50. seed_ = val;
  51. generator_.seed(seed_);
  52. }
  53. /// Generates random number
  54. static int __STDCALL Generate(unsigned int* random_data, int num_bits,
  55. void* user_data) {
  56. unsigned int num_bytes = num_bits / CHAR_BIT;
  57. unsigned int extra_bits = num_bits % CHAR_BIT;
  58. unsigned char* random_bytes = reinterpret_cast<unsigned char*>(random_data);
  59. if (!random_data) {
  60. return kPrngBadArgErr;
  61. }
  62. if (num_bits <= 0) {
  63. return kPrngBadArgErr;
  64. }
  65. if (0 != extra_bits) {
  66. num_bytes += 1;
  67. }
  68. Prng* myprng = (Prng*)user_data;
  69. for (unsigned int n = 0; n < num_bytes; n++) {
  70. random_bytes[n] =
  71. static_cast<unsigned char>(myprng->generator_() & 0x000000ff);
  72. }
  73. return kPrngNoErr;
  74. }
  75. private:
  76. unsigned int seed_;
  77. std::mt19937 generator_;
  78. };
  79. // BitSupplier implementation returns pre-defined bytes.
  80. class StaticPrng {
  81. public:
  82. StaticPrng(ConstOctStr bytes, size_t length)
  83. : bytes_((uint8_t const*)bytes, (uint8_t const*)bytes + length) {}
  84. ~StaticPrng() {}
  85. /// Generates random number
  86. static int __STDCALL Generate(unsigned int* random_data, int num_bits,
  87. void* user_data) {
  88. unsigned int num_bytes = num_bits / CHAR_BIT;
  89. if (!random_data) {
  90. return kPrngBadArgErr;
  91. }
  92. if (num_bits <= 0) {
  93. return kPrngBadArgErr;
  94. }
  95. StaticPrng* myprng = (StaticPrng*)user_data;
  96. for (size_t i = 0; i < num_bytes; i++) {
  97. random_data[i] = myprng->bytes_[i % myprng->bytes_.size()];
  98. }
  99. return kPrngNoErr;
  100. }
  101. private:
  102. std::vector<uint8_t> bytes_;
  103. };
  104. #endif // EPID_COMMON_TESTHELPER_PRNG_TESTHELPER_H_