Random.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Random_H
  16. #define Random_H
  17. #include "memory/Allocator.h"
  18. #include "exception/Except.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. struct Random;
  25. void Random_bytes(struct Random* rand, uint8_t* location, uint64_t count);
  26. /**
  27. * Get random Base32 text, great for password material.
  28. *
  29. * @param output the buffer to write the output to.
  30. * @param length the number of bytes to write.
  31. */
  32. void Random_base32(struct Random* rand, uint8_t* output, uint32_t length);
  33. void Random_addRandom(struct Random* rand, uint32_t randomNumber);
  34. static inline void Random_longs(struct Random* rand, uint64_t* location, uint64_t count)
  35. {
  36. Random_bytes(rand, (uint8_t*) location, count*8);
  37. }
  38. static inline void Random_ints(struct Random* rand, uint32_t* location, uint64_t count)
  39. {
  40. Random_bytes(rand, (uint8_t*) location, count*4);
  41. }
  42. static inline int16_t Random_int8(struct Random* rand)
  43. {
  44. int8_t ret;
  45. Random_bytes(rand, (uint8_t*)&ret, 1);
  46. return ret;
  47. }
  48. static inline uint16_t Random_uint8(struct Random* rand)
  49. {
  50. uint8_t ret;
  51. Random_bytes(rand, (uint8_t*)&ret, 1);
  52. return ret;
  53. }
  54. static inline int16_t Random_int16(struct Random* rand)
  55. {
  56. int16_t ret;
  57. Random_bytes(rand, (uint8_t*)&ret, 2);
  58. return ret;
  59. }
  60. static inline uint16_t Random_uint16(struct Random* rand)
  61. {
  62. uint16_t ret;
  63. Random_bytes(rand, (uint8_t*)&ret, 2);
  64. return ret;
  65. }
  66. static inline int32_t Random_int32(struct Random* rand)
  67. {
  68. int32_t ret;
  69. Random_bytes(rand, (uint8_t*)&ret, 4);
  70. return ret;
  71. }
  72. static inline uint32_t Random_uint32(struct Random* rand)
  73. {
  74. uint32_t ret;
  75. Random_bytes(rand, (uint8_t*)&ret, 4);
  76. return ret;
  77. }
  78. static inline int64_t Random_int64(struct Random* rand)
  79. {
  80. int64_t ret;
  81. Random_longs(rand, (uint64_t*)&ret, 1);
  82. return ret;
  83. }
  84. static inline uint64_t Random_uint64(struct Random* rand)
  85. {
  86. uint64_t ret;
  87. Random_longs(rand, &ret, 1);
  88. return ret;
  89. }
  90. struct Random* Random_newWithSeed(struct Allocator* alloc,
  91. struct Log* logger,
  92. struct RandomSeed* seed,
  93. struct Except* eh);
  94. struct Random* Random_new(struct Allocator* alloc, struct Log* logger, struct Except* eh);
  95. #endif