randombytes.c 5.3 KB

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