rdrand.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <stdint.h>
  31. /*! \def RDRAND_SUCCESS
  32. * The rdrand call was successful, the hardware was ready, and a random
  33. * number was returned.
  34. */
  35. #define RDRAND_SUCCESS 1
  36. /*! \def RDRAND_NOT_READY
  37. * The rdrand call was unsuccessful, the hardware was not ready, and a
  38. * random number was not returned.
  39. */
  40. #define RDRAND_NOT_READY -1
  41. /*! \def RDRAND_SUPPORTED
  42. * The rdrand instruction is supported by the host hardware.
  43. */
  44. #define RDRAND_SUPPORTED -2
  45. /*! \def RDRAND_UNSUPPORTED
  46. * The rdrand instruction is unsupported by the host hardware.
  47. */
  48. #define RDRAND_UNSUPPORTED -3
  49. /*! \def RDRAND_SUPPORT_UNKNOWN
  50. * Whether or not the hardware supports the rdrand instruction is unknown
  51. */
  52. #define RDRAND_SUPPORT_UNKNOWN -4
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. /*! \brief Calls rdrand for a 16-bit result.
  57. *
  58. * This function calls rdrand requesting a 16-bit result. By default, it will
  59. * perform only a single call to rdrand, returning success or failure. On
  60. * success, the data is written to memory pointed to by x. If the int retry is
  61. * true (non-zero), the function will enter a loop with count=10 until rdrand succeeds, at
  62. * which point it write the random data and return success, or fails This
  63. * function also ensures that rdrand is supported by the cpu or fails
  64. * gracefully.
  65. *
  66. * \param x pointer to memory to store the random result
  67. * \param retry int to determine whether or not to loop until rdrand succeeds
  68. * or until 10 failed attempts
  69. *
  70. * \return whether or not the call was successful, or supported at all
  71. */
  72. int rdrand_16(uint16_t* x, int retry);
  73. /*! \brief Calls rdrand for a 32-byte result.
  74. *
  75. * This function calls rdrand requesting a 32-bit result. By default, it will
  76. * perform only a single call to rdrand, returning success or failure. On
  77. * success, the data is written to memory pointed to by x. If the int retry is
  78. * true (non-zero), the function will enter a loop with count=10 until rdrand succeeds, at
  79. * which point it write the random data and return success, or fails This
  80. * function also ensures that rdrand is supported by the cpu or fails
  81. * gracefully.
  82. *
  83. * \param x pointer to memory to store the random result
  84. * \param retry int to determine whether or not to loop until rdrand succeeds
  85. * or until 10 failed attempts
  86. *
  87. * \return whether or not the call was successful, or supported at all
  88. */
  89. int rdrand_32(uint32_t* x, int retry);
  90. /*! \brief Calls rdrand for a 64-byte result.
  91. *
  92. * This function calls rdrand requesting a 64-byte result. By default, it will
  93. * perform only a single call to rdrand, returning success or failure. On
  94. * success, the data is written to memory pointed to by x. If the int retry is
  95. * true (non-zero), the function will enter a loop with count=10 until rdrand succeeds, at
  96. * which point it write the random data and return success, or fails This
  97. * function also ensures that rdrand is supported by the cpu or fails
  98. * gracefully.
  99. *
  100. * \param x pointer to memory to store the random result
  101. * \param retry int to determine whether or not to loop until rdrand succeeds
  102. * or until 10 failed attempts
  103. *
  104. * \return whether or not the call was successful, or supported at all
  105. */
  106. int rdrand_64(uint64_t* x, int retry);
  107. /*! \brief Calls rdrand to obtain multiple 64-byte results.
  108. *
  109. * This function calls rdrand requesting multiple 64-byte results. On
  110. * success, the data is written to memory pointed to by x. This function
  111. * calls rdrand_64 and if any of those invocations fail, this function
  112. * fails. It returns the same values as rdrand_64.
  113. */
  114. int rdrand_get_n_64(unsigned int n, uint64_t* x);
  115. /*! \brief Calls rdrand to obtain multiple 32-byte results.
  116. *
  117. * This function calls rdrand requesting multiple 32-byte results. On
  118. * success, the data is written to memory pointed to by x. This function
  119. * calls rdrand_32 and if any of those invocations fail, this function
  120. * fails. It returns the same values as rdrand_32.
  121. */
  122. int rdrand_get_n_32(unsigned int n, uint32_t* x);
  123. /*! \brief Calls rdrand to fill a buffer of arbitrary size with random bytes.
  124. *
  125. * This function calls rdrand requesting multiple 64- or 32-bit results to
  126. * fill a buffer of arbitrary size.
  127. *
  128. * \param n size of the buffer to fill with random bytes
  129. * \param buffer pointer to memory to store the random result
  130. *
  131. * \return whether or not the call was successful, or supported at all
  132. */
  133. int rdrand_get_bytes(unsigned int n, unsigned char *buffer);
  134. #ifdef __cplusplus
  135. };
  136. #endif
  137. #endif // RDRAND_H