randombytes.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <assert.h>
  2. #include <limits.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #ifdef __EMSCRIPTEN__
  7. # include <emscripten.h>
  8. #endif
  9. #include "core.h"
  10. #include "crypto_stream_chacha20.h"
  11. #include "randombytes.h"
  12. #ifndef RANDOMBYTES_CUSTOM_IMPLEMENTATION
  13. # ifdef RANDOMBYTES_DEFAULT_IMPLEMENTATION
  14. # include "randombytes_internal.h"
  15. # endif
  16. # include "randombytes_sysrandom.h"
  17. #endif
  18. #include "private/common.h"
  19. /* C++Builder defines a "random" macro */
  20. #undef random
  21. static const randombytes_implementation *implementation;
  22. #ifndef RANDOMBYTES_DEFAULT_IMPLEMENTATION
  23. # ifdef __EMSCRIPTEN__
  24. # define RANDOMBYTES_DEFAULT_IMPLEMENTATION NULL
  25. # else
  26. # define RANDOMBYTES_DEFAULT_IMPLEMENTATION &randombytes_sysrandom_implementation;
  27. # endif
  28. #endif
  29. static void
  30. randombytes_init_if_needed(void)
  31. {
  32. if (implementation == NULL) {
  33. implementation = RANDOMBYTES_DEFAULT_IMPLEMENTATION;
  34. randombytes_stir();
  35. }
  36. }
  37. int
  38. randombytes_set_implementation(randombytes_implementation *impl)
  39. {
  40. implementation = impl;
  41. return 0;
  42. }
  43. const char *
  44. randombytes_implementation_name(void)
  45. {
  46. #ifndef __EMSCRIPTEN__
  47. randombytes_init_if_needed();
  48. return implementation->implementation_name();
  49. #else
  50. return "js";
  51. #endif
  52. }
  53. uint32_t
  54. randombytes_random(void)
  55. {
  56. #ifndef __EMSCRIPTEN__
  57. randombytes_init_if_needed();
  58. return implementation->random();
  59. #else
  60. return EM_ASM_INT_V({
  61. return Module.getRandomValue();
  62. });
  63. #endif
  64. }
  65. void
  66. randombytes_stir(void)
  67. {
  68. #ifndef __EMSCRIPTEN__
  69. randombytes_init_if_needed();
  70. if (implementation->stir != NULL) {
  71. implementation->stir();
  72. }
  73. #else
  74. EM_ASM({
  75. if (Module.getRandomValue === undefined) {
  76. try {
  77. var window_ = 'object' === typeof window ? window : self;
  78. var crypto_ = typeof window_.crypto !== 'undefined' ? window_.crypto : window_.msCrypto;
  79. var randomValuesStandard = function() {
  80. var buf = new Uint32Array(1);
  81. crypto_.getRandomValues(buf);
  82. return buf[0] >>> 0;
  83. };
  84. randomValuesStandard();
  85. Module.getRandomValue = randomValuesStandard;
  86. } catch (e) {
  87. try {
  88. var crypto = require('crypto');
  89. var randomValueNodeJS = function() {
  90. var buf = crypto['randomBytes'](4);
  91. return (buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]) >>> 0;
  92. };
  93. randomValueNodeJS();
  94. Module.getRandomValue = randomValueNodeJS;
  95. } catch (e) {
  96. throw 'No secure random number generator found';
  97. }
  98. }
  99. }
  100. });
  101. #endif
  102. }
  103. uint32_t
  104. randombytes_uniform(const uint32_t upper_bound)
  105. {
  106. uint32_t min;
  107. uint32_t r;
  108. #ifndef __EMSCRIPTEN__
  109. randombytes_init_if_needed();
  110. if (implementation->uniform != NULL) {
  111. return implementation->uniform(upper_bound);
  112. }
  113. #endif
  114. if (upper_bound < 2) {
  115. return 0;
  116. }
  117. min = (1U + ~upper_bound) % upper_bound; /* = 2**32 mod upper_bound */
  118. do {
  119. r = randombytes_random();
  120. } while (r < min);
  121. /* r is now clamped to a set whose size mod upper_bound == 0
  122. * the worst case (2**31+1) requires ~ 2 attempts */
  123. return r % upper_bound;
  124. }
  125. void
  126. randombytes_buf(void * const buf, const size_t size)
  127. {
  128. #ifndef __EMSCRIPTEN__
  129. randombytes_init_if_needed();
  130. if (size > (size_t) 0U) {
  131. implementation->buf(buf, size);
  132. }
  133. #else
  134. unsigned char *p = (unsigned char *) buf;
  135. size_t i;
  136. for (i = (size_t) 0U; i < size; i++) {
  137. p[i] = (unsigned char) randombytes_random();
  138. }
  139. #endif
  140. }
  141. void
  142. randombytes_buf_deterministic(void * const buf, const size_t size,
  143. const unsigned char seed[randombytes_SEEDBYTES])
  144. {
  145. static const unsigned char nonce[crypto_stream_chacha20_ietf_NONCEBYTES] = {
  146. 'L', 'i', 'b', 's', 'o', 'd', 'i', 'u', 'm', 'D', 'R', 'G'
  147. };
  148. COMPILER_ASSERT(randombytes_SEEDBYTES == crypto_stream_chacha20_ietf_KEYBYTES);
  149. #if SIZE_MAX > 0x4000000000ULL
  150. COMPILER_ASSERT(randombytes_BYTES_MAX <= 0x4000000000ULL);
  151. if (size > 0x4000000000ULL) {
  152. sodium_misuse();
  153. }
  154. #endif
  155. crypto_stream_chacha20_ietf((unsigned char *) buf, (unsigned long long) size,
  156. nonce, seed);
  157. }
  158. size_t
  159. randombytes_seedbytes(void)
  160. {
  161. return randombytes_SEEDBYTES;
  162. }
  163. int
  164. randombytes_close(void)
  165. {
  166. if (implementation != NULL && implementation->close != NULL) {
  167. return implementation->close();
  168. }
  169. return 0;
  170. }
  171. void
  172. randombytes(unsigned char * const buf, const unsigned long long buf_len)
  173. {
  174. assert(buf_len <= SIZE_MAX);
  175. randombytes_buf(buf, (size_t) buf_len);
  176. }