prng-testhelper.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*############################################################################
  2. # Copyright 2016 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 <random>
  29. /// Return status for Prng Generate function
  30. typedef enum {
  31. kPrngNoErr = 0, //!< no error
  32. kPrngErr = -999, //!< unspecified error
  33. kPrngNotImpl, //!< not implemented error
  34. kPrngBadArgErr //!< incorrect arg to function
  35. } PrngStatus;
  36. /// Pseudo random number generator (prng) class.
  37. class Prng {
  38. public:
  39. Prng() : seed_(1) { set_seed(seed_); }
  40. ~Prng() {}
  41. /// Retrieve seed
  42. unsigned int get_seed() const { return seed_; }
  43. /// Set seed for random number generator
  44. void set_seed(unsigned int val) {
  45. seed_ = val;
  46. generator_.seed(seed_);
  47. }
  48. /// Generates random number
  49. static int __STDCALL Generate(unsigned int* random_data, int num_bits,
  50. void* user_data) {
  51. unsigned int num_bytes = num_bits / CHAR_BIT;
  52. unsigned int num_words = num_bytes / sizeof(unsigned int);
  53. unsigned int extra_bytes = num_bytes % sizeof(unsigned int);
  54. if (!random_data) {
  55. return kPrngBadArgErr;
  56. }
  57. if (num_bits <= 0) {
  58. return kPrngBadArgErr;
  59. }
  60. Prng* myprng = (Prng*)user_data;
  61. std::uniform_int_distribution<> dis(0x0, 0xffff);
  62. if (num_words > 0) {
  63. for (unsigned int n = 0; n < num_words; n++) {
  64. random_data[n] =
  65. (dis(myprng->generator_) << 16) + dis(myprng->generator_);
  66. }
  67. }
  68. if (extra_bytes > 0) {
  69. unsigned int data =
  70. (dis(myprng->generator_) << 16) + dis(myprng->generator_);
  71. unsigned char* byte_data = (unsigned char*)&data;
  72. unsigned char* random_bytes = (unsigned char*)&random_data[num_words];
  73. for (unsigned int n = 0; n < extra_bytes; n++) {
  74. random_bytes[n] = byte_data[n];
  75. }
  76. }
  77. return kPrngNoErr;
  78. }
  79. private:
  80. unsigned int seed_;
  81. std::mt19937 generator_;
  82. };
  83. #endif // EPID_COMMON_TESTHELPER_PRNG_TESTHELPER_H_