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.
 
 
 
 
 

141 lines
4.0 KiB

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