You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

144 lines
4.2 KiB

  1. /* Copyright (c) 2011 Stanford University.
  2. * Copyright (c) 2014-2015 Cryptography Research, Inc.
  3. * Released under the MIT License. See LICENSE.txt for license information.
  4. */
  5. /**
  6. * @file crandom.h
  7. * @author Mike Hamburg
  8. * @brief A miniature version of the (as of yet incomplete) crandom project.
  9. */
  10. #ifndef __GOLDI_CRANDOM_H__
  11. #define __GOLDI_CRANDOM_H__ 1
  12. #define _XOPEN_SOURCE 600
  13. #include <stdint.h> /* for uint64_t */
  14. #include <fcntl.h> /* for open */
  15. #include <errno.h> /* for returning errors after open */
  16. #include <stdlib.h> /* for abort */
  17. #include <string.h> /* for memcpy */
  18. #include <strings.h> /* for bzero */
  19. #include <unistd.h> /* for read */
  20. /**
  21. * @brief The state of a crandom generator.
  22. *
  23. * This object is opaque. It is not protected by a lock, and so must
  24. * not be accessed by multiple threads at the same time.
  25. */
  26. struct crandom_state_t {
  27. /** @privatesection */
  28. /* unsigned char seed[32]; */
  29. /* unsigned char buffer[96]; */
  30. unsigned char seedBuffer[32+96];
  31. uint64_t ctr;
  32. uint64_t magic;
  33. unsigned int fill;
  34. int reseed_countdown;
  35. int reseed_interval;
  36. int reseeds_mandatory;
  37. int randomfd;
  38. } __attribute__((aligned(16))) ;
  39. typedef struct crandom_state_t crandom_state_a_t[1];
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /**
  44. * Initialize a crandom state from the chosen file.
  45. *
  46. * This function initializes a state from a given state file, or
  47. * from a random device (eg. /dev/random or /dev/urandom).
  48. *
  49. * You must check the return value of this function.
  50. *
  51. * @param [out] state The crandom state variable to initalize.
  52. * @param [in] filename The name of the seed file or random device.
  53. * @param [in] reseed_interval The number of 96-byte blocks which can be
  54. * generated without reseeding. Suggest 10000.
  55. * @param [in] reseeds_mandatory If nonzero, call abort() if a reseed fails.
  56. * Suggest 1.
  57. *
  58. * @retval 0 Success.
  59. * @retval Nonzero An error to be interpreted by strerror().
  60. */
  61. int
  62. crandom_init_from_file (
  63. crandom_state_a_t state,
  64. const char *filename,
  65. int reseed_interval,
  66. int reseeds_mandatory
  67. ) __attribute__((warn_unused_result));
  68. /**
  69. * Initialize a crandom state from a buffer, for deterministic operation.
  70. *
  71. * This function is used to initialize a crandom state deterministically,
  72. * mainly for testing purposes. It can also be used to expand a secret
  73. * random value deterministically.
  74. *
  75. * @warning The crandom implementation is not guaranteed to be stable.
  76. * That is, a later release might produce a different random stream from
  77. * the same seed.
  78. *
  79. * @param [out] state The crandom state variable to initalize.
  80. * @param [in] initial_seed The seed value.
  81. */
  82. void
  83. crandom_init_from_buffer (
  84. crandom_state_a_t state,
  85. const char initial_seed[32]
  86. );
  87. /**
  88. * Fill the output buffer with random data.
  89. *
  90. * This function uses the given crandom state to produce pseudorandom data
  91. * in the output buffer.
  92. *
  93. * This function may perform reads from the state's random device if it needs
  94. * to reseed. This could block if that file is a blocking source, such as
  95. * a pipe or /dev/random on Linux. If reseeding fails and the state has
  96. * reseeds_mandatory set, this function will call abort(). Otherwise, it will
  97. * return an error code, but it will still randomize the buffer.
  98. *
  99. * If called on a corrupted, uninitialized or destroyed state, this function
  100. * will abort().
  101. *
  102. * @warning This function is not thread-safe with respect to the state. Don't
  103. * call it from multiple threads with the same state at the same time.
  104. *
  105. * @param [inout] state The crandom state to use for generation.
  106. * @param [out] output The buffer to fill with random data.
  107. * @param [in] length The length of the buffer.
  108. *
  109. * @retval 0 Success.
  110. * @retval Nonezero A non-mandatory reseed operation failed.
  111. */
  112. int
  113. crandom_generate (
  114. crandom_state_a_t state,
  115. unsigned char *output,
  116. unsigned long long length
  117. );
  118. /**
  119. * Destroy the random state. Further calls to crandom_generate() on that state
  120. * will abort().
  121. *
  122. * @param [inout] state The state to be destroyed.
  123. */
  124. void
  125. crandom_destroy (
  126. crandom_state_a_t state
  127. );
  128. #ifdef __cplusplus
  129. }; /* extern "C" */
  130. #endif
  131. #endif /* __GOLDI_CRANDOM_H__ */