rdrand.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Copyright © 2012, Intel Corporation. All rights reserved.
  2. Redistribution and use in source and binary forms, with or without
  3. modification, are permitted provided that the following conditions are met:
  4. - Redistributions of source code must retain the above copyright notice,
  5. this list of conditions and the following disclaimer.
  6. - Redistributions in binary form must reproduce the above copyright
  7. notice, this list of conditions and the following disclaimer in the
  8. documentation and/or other materials provided with the distribution.
  9. - Neither the name of Intel Corporation nor the names of its contributors
  10. may be used to endorse or promote products derived from this software
  11. without specific prior written permission.
  12. THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS" AND ANY EXPRESS OR
  13. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  15. EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  18. BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  19. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  20. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  21. POSSIBILITY OF SUCH DAMAGE. */
  22. /*! \file rdrand.h
  23. * \brief Public header for rdrand API.
  24. *
  25. * This is the public header for the rdrand API. It exposes the three public
  26. * APIs, which access the rdrand instruction for various data sizes.
  27. */
  28. #ifndef RDRAND_H
  29. #define RDRAND_H
  30. #include "config.h"
  31. #if HAVE_INTTYPES_H
  32. #include <inttypes.h>
  33. #endif
  34. #ifdef _MSC_VER
  35. /* MSVC specific */
  36. typedef unsigned __int16 uint16_t;
  37. typedef unsigned __int32 uint32_t;
  38. typedef unsigned __int64 uint64_t;
  39. #endif
  40. /*! \def RDRAND_SUCCESS
  41. * The rdrand call was successful, the hardware was ready, and a random
  42. * number was returned.
  43. */
  44. #define RDRAND_SUCCESS 1
  45. /*! \def RDRAND_NOT_READY
  46. * The rdrand call was unsuccessful, the hardware was not ready, and a
  47. * random number was not returned.
  48. */
  49. #define RDRAND_NOT_READY -1
  50. /*! \def RDRAND_SUPPORTED
  51. * The rdrand instruction is supported by the host hardware.
  52. */
  53. #define RDRAND_SUPPORTED -2
  54. /*! \def RDRAND_UNSUPPORTED
  55. * The rdrand instruction is unsupported by the host hardware.
  56. */
  57. #define RDRAND_UNSUPPORTED -3
  58. /*! \def RDRAND_SUPPORT_UNKNOWN
  59. * Whether or not the hardware supports the rdrand instruction is unknown
  60. */
  61. #define RDRAND_SUPPORT_UNKNOWN -4
  62. /*! \brief Calls rdrand for a 16-bit result.
  63. *
  64. * This function calls rdrand requesting a 16-bit result. By default, it will
  65. * perform only a single call to rdrand, returning success or failure. On
  66. * success, the data is written to memory pointed to by x. If the int retry is
  67. * true (non-zero), the function will enter a loop with count=10 until rdrand succeeds, at
  68. * which point it write the random data and return success, or fails This
  69. * function also ensures that rdrand is supported by the cpu or fails
  70. * gracefully.
  71. *
  72. * \param x pointer to memory to store the random result
  73. * \param retry int to determine whether or not to loop until rdrand succeeds
  74. * or until 10 failed attempts
  75. *
  76. * \return whether or not the call was successful, or supported at all
  77. */
  78. int rdrand_16(uint16_t* x, int retry);
  79. /*! \brief Calls rdrand for a 32-byte result.
  80. *
  81. * This function calls rdrand requesting a 32-bit result. By default, it will
  82. * perform only a single call to rdrand, returning success or failure. On
  83. * success, the data is written to memory pointed to by x. If the int retry is
  84. * true (non-zero), the function will enter a loop with count=10 until rdrand succeeds, at
  85. * which point it write the random data and return success, or fails This
  86. * function also ensures that rdrand is supported by the cpu or fails
  87. * gracefully.
  88. *
  89. * \param x pointer to memory to store the random result
  90. * \param retry int to determine whether or not to loop until rdrand succeeds
  91. * or until 10 failed attempts
  92. *
  93. * \return whether or not the call was successful, or supported at all
  94. */
  95. int rdrand_32(uint32_t* x, int retry);
  96. /*! \brief Calls rdrand for a 64-byte result.
  97. *
  98. * This function calls rdrand requesting a 64-byte result. By default, it will
  99. * perform only a single call to rdrand, returning success or failure. On
  100. * success, the data is written to memory pointed to by x. If the int retry is
  101. * true (non-zero), the function will enter a loop with count=10 until rdrand succeeds, at
  102. * which point it write the random data and return success, or fails This
  103. * function also ensures that rdrand is supported by the cpu or fails
  104. * gracefully.
  105. *
  106. * Calling rdrand_64 on a 32-bit system is inefficient as it makes two calls
  107. * to rdrand_32 to produce a single 64-bit value, using a shift to populate
  108. * the high bits. The physical construction of the DRNG allows you to do this
  109. * up to a 128-bit value while retaining multiplicative prediction resistance
  110. * (i.e., don't do this to generate numbers larger than 128 bits).
  111. *
  112. * \param x pointer to memory to store the random result
  113. * \param retry int to determine whether or not to loop until rdrand succeeds
  114. * or until 10 failed attempts
  115. *
  116. * \return whether or not the call was successful, or supported at all
  117. */
  118. int rdrand_64(uint64_t* x, int retry);
  119. /*! \brief Calls rdrand to obtain multiple 64-byte results.
  120. *
  121. * This function calls rdrand requesting multiple 64-byte results. On
  122. * success, the data is written to memory pointed to by x. This function
  123. * calls rdrand_64 and if any of those invocations fail, this function
  124. * fails. It returns the same values as rdrand_64.
  125. *
  126. * This function is inefficient on 32-bit systems.
  127. */
  128. int rdrand_get_n_64(unsigned int n, uint64_t* x);
  129. /*! \brief Calls rdrand to obtain multiple 32-byte results.
  130. *
  131. * This function calls rdrand requesting multiple 32-byte results. On
  132. * success, the data is written to memory pointed to by x. This function
  133. * calls rdrand_32 and if any of those invocations fail, this function
  134. * fails. It returns the same values as rdrand_32.
  135. */
  136. int rdrand_get_n_32(unsigned int n, uint32_t* x);
  137. /*! \brief Calls rdrand to fill a buffer of arbitrary size with random bytes.
  138. *
  139. * This function calls rdrand requesting multiple 64- or 32-bit results to
  140. * fill a buffer of arbitrary size.
  141. *
  142. * \param n size of the buffer to fill with random bytes
  143. * \param buffer pointer to memory to store the random result
  144. *
  145. * \return whether or not the call was successful, or supported at all
  146. */
  147. int rdrand_get_bytes(unsigned int n, unsigned char *buffer);
  148. #endif // RDRAND_H