prng.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 implementation.
  19. */
  20. #include <time.h>
  21. #include <ippcp.h>
  22. #include <stdlib.h>
  23. #include "src/prng.h"
  24. EpidStatus PrngCreate(void** prng) {
  25. // Security note:
  26. // Random number generator used in the samples not claimed to be a
  27. // cryptographically secure pseudo-random number generator.
  28. EpidStatus sts = kEpidErr;
  29. int prng_ctx_size = 0;
  30. IppsPRNGState* prng_ctx = NULL;
  31. int seed_ctx_size = 0;
  32. IppsBigNumState* seed_ctx = NULL;
  33. time_t seed_value;
  34. if (!prng) return kEpidBadArgErr;
  35. if (ippStsNoErr != ippsPRNGGetSize(&prng_ctx_size)) return kEpidErr;
  36. if (ippStsNoErr !=
  37. ippsBigNumGetSize((sizeof(seed_value) + 3) / 4, &seed_ctx_size))
  38. return kEpidErr;
  39. do {
  40. prng_ctx = (IppsPRNGState*)calloc(1, prng_ctx_size);
  41. if (!prng_ctx) {
  42. sts = kEpidNoMemErr;
  43. break;
  44. }
  45. if (ippStsNoErr != ippsPRNGInit(sizeof(seed_value) * 8, prng_ctx)) {
  46. sts = kEpidErr;
  47. break;
  48. }
  49. // seed PRNG
  50. seed_ctx = (IppsBigNumState*)calloc(1, seed_ctx_size);
  51. if (!seed_ctx) {
  52. sts = kEpidNoMemErr;
  53. break;
  54. }
  55. if (ippStsNoErr != ippsBigNumInit((sizeof(seed_value) + 3) / 4, seed_ctx)) {
  56. sts = kEpidErr;
  57. break;
  58. }
  59. time(&seed_value);
  60. if (ippStsNoErr !=
  61. ippsSetOctString_BN((void*)&seed_value, sizeof(seed_value), seed_ctx)) {
  62. sts = kEpidErr;
  63. break;
  64. }
  65. if (ippStsNoErr != ippsPRNGSetSeed(seed_ctx, prng_ctx)) {
  66. sts = kEpidErr;
  67. break;
  68. }
  69. *prng = prng_ctx;
  70. prng_ctx = NULL;
  71. sts = kEpidNoErr;
  72. } while (0);
  73. if (seed_ctx) free(seed_ctx);
  74. if (prng_ctx) free(prng_ctx);
  75. return sts;
  76. }
  77. void PrngDelete(void** prng) {
  78. if (prng && *prng) {
  79. free(*prng);
  80. *prng = NULL;
  81. }
  82. }
  83. // simple wrapper to hide IPP implementation.
  84. int __STDCALL PrngGen(unsigned int* rand_data, int num_bits, void* user_data) {
  85. return ippsPRNGen(rand_data, num_bits, (IppsPRNGState*)user_data);
  86. }