Random.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Random_H
  16. #define Random_H
  17. #include "memory/Allocator.h"
  18. #include "exception/Err.h"
  19. #include "util/log/Log.h"
  20. #include "crypto/random/seed/RandomSeed.h"
  21. #include "util/Linker.h"
  22. Linker_require("crypto/random/Random.c")
  23. #include <stdint.h>
  24. typedef struct Random Random_t;
  25. void Random_bytes(Random_t* rand, uint8_t* location, uint64_t count);
  26. void Random_bytes_fromRust(Random_t* rand, uint8_t* location, uint64_t count);
  27. /**
  28. * Get random Base32 text, great for password material.
  29. *
  30. * @param output the buffer to write the output to.
  31. * @param length the number of bytes to write.
  32. */
  33. void Random_base32(struct Random* rand, uint8_t* output, uint32_t length);
  34. void Random_addRandom(struct Random* rand, uint32_t randomNumber);
  35. static inline void Random_longs(struct Random* rand, uint64_t* location, uint64_t count)
  36. {
  37. Random_bytes(rand, (uint8_t*) location, count*8);
  38. }
  39. static inline void Random_ints(struct Random* rand, uint32_t* location, uint64_t count)
  40. {
  41. Random_bytes(rand, (uint8_t*) location, count*4);
  42. }
  43. static inline int8_t Random_int8(struct Random* rand)
  44. {
  45. int8_t ret;
  46. Random_bytes(rand, (uint8_t*)&ret, 1);
  47. return ret;
  48. }
  49. static inline uint8_t Random_uint8(struct Random* rand)
  50. {
  51. uint8_t ret;
  52. Random_bytes(rand, (uint8_t*)&ret, 1);
  53. return ret;
  54. }
  55. static inline int16_t Random_int16(struct Random* rand)
  56. {
  57. int16_t ret;
  58. Random_bytes(rand, (uint8_t*)&ret, 2);
  59. return ret;
  60. }
  61. static inline uint16_t Random_uint16(struct Random* rand)
  62. {
  63. uint16_t ret;
  64. Random_bytes(rand, (uint8_t*)&ret, 2);
  65. return ret;
  66. }
  67. static inline int32_t Random_int32(struct Random* rand)
  68. {
  69. int32_t ret;
  70. Random_bytes(rand, (uint8_t*)&ret, 4);
  71. return ret;
  72. }
  73. static inline uint32_t Random_uint32(struct Random* rand)
  74. {
  75. uint32_t ret;
  76. Random_bytes(rand, (uint8_t*)&ret, 4);
  77. return ret;
  78. }
  79. static inline int64_t Random_int64(struct Random* rand)
  80. {
  81. int64_t ret;
  82. Random_longs(rand, (uint64_t*)&ret, 1);
  83. return ret;
  84. }
  85. static inline uint64_t Random_uint64(struct Random* rand)
  86. {
  87. uint64_t ret;
  88. Random_longs(rand, &ret, 1);
  89. return ret;
  90. }
  91. Err_DEFUN Random_newWithSeed(
  92. struct Random** out,
  93. struct Allocator* alloc,
  94. struct Log* logger,
  95. RandomSeed_t* seed);
  96. Err_DEFUN Random_new(
  97. struct Random** out,
  98. struct Allocator* alloc,
  99. struct Log* logger);
  100. #endif