rbnacl-libsodium 1.0.10 → 1.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +6 -2
  3. data/lib/rbnacl/libsodium/version.rb +1 -1
  4. data/vendor/libsodium/ChangeLog +24 -0
  5. data/vendor/libsodium/LICENSE +2 -0
  6. data/vendor/libsodium/Makefile.in +6 -1
  7. data/vendor/libsodium/aclocal.m4 +1 -0
  8. data/vendor/libsodium/autom4te.cache/output.1 +1896 -1136
  9. data/vendor/libsodium/autom4te.cache/output.7 +19837 -0
  10. data/vendor/libsodium/autom4te.cache/requests +1271 -1031
  11. data/vendor/libsodium/autom4te.cache/traces.1 +532 -491
  12. data/vendor/libsodium/autom4te.cache/traces.7 +3614 -0
  13. data/vendor/libsodium/builds/msvc/version.h +2 -2
  14. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj +22 -1
  15. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters +23 -20
  16. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj +13 -2
  17. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters +12 -9
  18. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj +12 -1
  19. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters +3 -0
  20. data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj +2 -1
  21. data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters +14 -11
  22. data/vendor/libsodium/configure +1887 -1127
  23. data/vendor/libsodium/configure.ac +44 -11
  24. data/vendor/libsodium/dist-build/Makefile.am +4 -2
  25. data/vendor/libsodium/dist-build/Makefile.in +9 -2
  26. data/vendor/libsodium/dist-build/android-armv8-a.sh +1 -1
  27. data/vendor/libsodium/dist-build/android-build.sh +40 -3
  28. data/vendor/libsodium/dist-build/android-mips64.sh +1 -1
  29. data/vendor/libsodium/dist-build/android-x86_64.sh +4 -0
  30. data/vendor/libsodium/dist-build/emscripten-symbols.def +25 -25
  31. data/vendor/libsodium/dist-build/emscripten.sh +69 -35
  32. data/vendor/libsodium/dist-build/msys2-win32.sh +8 -2
  33. data/vendor/libsodium/dist-build/msys2-win64.sh +8 -2
  34. data/vendor/libsodium/dist-build/{nativeclient.sh → nativeclient-pnacl.sh} +3 -4
  35. data/vendor/libsodium/dist-build/nativeclient-x86.sh +19 -0
  36. data/vendor/libsodium/dist-build/nativeclient-x86_64.sh +19 -0
  37. data/vendor/libsodium/logo.png +0 -0
  38. data/vendor/libsodium/m4/ax_pthread.m4 +485 -0
  39. data/vendor/libsodium/msvc-scripts/Makefile.in +5 -0
  40. data/vendor/libsodium/msvc-scripts/process.bat +2 -2
  41. data/vendor/libsodium/packaging/nuget/package.bat +3 -3
  42. data/vendor/libsodium/src/Makefile.in +5 -0
  43. data/vendor/libsodium/src/libsodium/Makefile.am +2 -1
  44. data/vendor/libsodium/src/libsodium/Makefile.in +13 -8
  45. data/vendor/libsodium/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +70 -87
  46. data/vendor/libsodium/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c +6 -0
  47. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-avx2.c +1 -1
  48. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-ref.c +9 -1
  49. data/vendor/libsodium/src/libsodium/crypto_hash/sha256/cp/hash_sha256.c +11 -4
  50. data/vendor/libsodium/src/libsodium/crypto_hash/sha512/cp/hash_sha512.c +14 -8
  51. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-encoding.c +0 -1
  52. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2.c +10 -6
  53. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c +2 -1
  54. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/donna_c64/curve25519_donna_c64.c +1 -1
  55. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.c +11 -11
  56. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.h +2 -2
  57. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_nsquare.S +3 -0
  58. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_pack.S +3 -0
  59. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder.S +830 -826
  60. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder_base.S +732 -728
  61. data/vendor/libsodium/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c +3 -0
  62. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/int128.h +1 -1
  63. data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/amd64_xmm6/stream_salsa20_amd64_xmm6.S +8 -0
  64. data/vendor/libsodium/src/libsodium/include/Makefile.in +5 -0
  65. data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_aes256gcm.h +1 -1
  66. data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_chacha20poly1305.h +1 -1
  67. data/vendor/libsodium/src/libsodium/include/sodium/crypto_auth.h +1 -1
  68. data/vendor/libsodium/src/libsodium/include/sodium/crypto_auth_hmacsha256.h +1 -1
  69. data/vendor/libsodium/src/libsodium/include/sodium/crypto_auth_hmacsha512.h +1 -1
  70. data/vendor/libsodium/src/libsodium/include/sodium/crypto_auth_hmacsha512256.h +1 -1
  71. data/vendor/libsodium/src/libsodium/include/sodium/crypto_box.h +3 -3
  72. data/vendor/libsodium/src/libsodium/include/sodium/crypto_box_curve25519xsalsa20poly1305.h +1 -1
  73. data/vendor/libsodium/src/libsodium/include/sodium/crypto_generichash.h +1 -1
  74. data/vendor/libsodium/src/libsodium/include/sodium/crypto_generichash_blake2b.h +1 -1
  75. data/vendor/libsodium/src/libsodium/include/sodium/crypto_hash.h +1 -1
  76. data/vendor/libsodium/src/libsodium/include/sodium/crypto_hash_sha256.h +1 -1
  77. data/vendor/libsodium/src/libsodium/include/sodium/crypto_hash_sha512.h +1 -1
  78. data/vendor/libsodium/src/libsodium/include/sodium/crypto_onetimeauth.h +1 -1
  79. data/vendor/libsodium/src/libsodium/include/sodium/crypto_onetimeauth_poly1305.h +1 -1
  80. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash.h +1 -1
  81. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash_argon2i.h +1 -1
  82. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash_scryptsalsa208sha256.h +1 -1
  83. data/vendor/libsodium/src/libsodium/include/sodium/crypto_secretbox.h +1 -1
  84. data/vendor/libsodium/src/libsodium/include/sodium/crypto_secretbox_xsalsa20poly1305.h +1 -1
  85. data/vendor/libsodium/src/libsodium/include/sodium/crypto_shorthash.h +1 -1
  86. data/vendor/libsodium/src/libsodium/include/sodium/crypto_shorthash_siphash24.h +1 -1
  87. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign.h +3 -3
  88. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign_ed25519.h +1 -1
  89. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign_edwards25519sha512batch.h +1 -1
  90. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream.h +1 -1
  91. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_aes128ctr.h +1 -1
  92. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_chacha20.h +1 -1
  93. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_salsa20.h +1 -1
  94. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_salsa2012.h +1 -1
  95. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_salsa208.h +1 -1
  96. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_xsalsa20.h +1 -1
  97. data/vendor/libsodium/src/libsodium/include/sodium/private/curve25519_ref10.h +2 -2
  98. data/vendor/libsodium/src/libsodium/include/sodium/private/mutex.h +7 -0
  99. data/vendor/libsodium/src/libsodium/include/sodium/randombytes.h +1 -1
  100. data/vendor/libsodium/src/libsodium/randombytes/nativeclient/randombytes_nativeclient.c +15 -4
  101. data/vendor/libsodium/src/libsodium/randombytes/randombytes.c +25 -15
  102. data/vendor/libsodium/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +72 -15
  103. data/vendor/libsodium/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c +43 -4
  104. data/vendor/libsodium/src/libsodium/sodium/core.c +131 -2
  105. data/vendor/libsodium/src/libsodium/sodium/runtime.c +5 -2
  106. data/vendor/libsodium/src/libsodium/sodium/utils.c +16 -8
  107. data/vendor/libsodium/test/Makefile.in +5 -0
  108. data/vendor/libsodium/test/default/Makefile.in +5 -0
  109. data/vendor/libsodium/test/default/generichash2.c +8 -2
  110. data/vendor/libsodium/test/default/nacl-test-wrapper.sh +19 -17
  111. data/vendor/libsodium/test/default/randombytes.c +4 -2
  112. data/vendor/libsodium/test/quirks/quirks.h +23 -2
  113. metadata +11 -15
  114. data/vendor/libsodium/README +0 -1
  115. data/vendor/libsodium/dist-build/android-x86-64.sh +0 -4
  116. data/vendor/libsodium/examples/Makefile +0 -21
  117. data/vendor/libsodium/examples/auth.c +0 -68
  118. data/vendor/libsodium/examples/box.c +0 -135
  119. data/vendor/libsodium/examples/box_detached.c +0 -134
  120. data/vendor/libsodium/examples/generichash.c +0 -80
  121. data/vendor/libsodium/examples/generichash_stream.c +0 -58
  122. data/vendor/libsodium/examples/shorthash.c +0 -58
  123. data/vendor/libsodium/examples/sign.c +0 -78
  124. data/vendor/libsodium/examples/utils.h +0 -110
@@ -0,0 +1,7 @@
1
+ #ifndef mutex_H
2
+ #define mutex_H 1
3
+
4
+ extern int sodium_crit_enter(void);
5
+ extern int sodium_crit_leave(void);
6
+
7
+ #endif
@@ -10,7 +10,7 @@
10
10
  #include "export.h"
11
11
 
12
12
  #ifdef __cplusplus
13
- # if __GNUC__
13
+ # ifdef __GNUC__
14
14
  # pragma GCC diagnostic ignored "-Wlong-long"
15
15
  # endif
16
16
  extern "C" {
@@ -4,7 +4,7 @@
4
4
  #include <stdlib.h>
5
5
 
6
6
  #ifdef __native_client__
7
- # include <nacl/nacl_random.h>
7
+ # include <irt.h>
8
8
 
9
9
  # include "utils.h"
10
10
  # include "randombytes.h"
@@ -13,12 +13,23 @@
13
13
  static void
14
14
  randombytes_nativeclient_buf(void * const buf, const size_t size)
15
15
  {
16
- size_t readnb;
16
+ unsigned char *buf_ = (unsigned char *) buf;
17
+ struct nacl_irt_random rand_intf;
18
+ size_t readnb = (size_t) 0U;
19
+ size_t toread = size;
17
20
 
18
- if (nacl_secure_random(buf, size, &readnb) != 0) {
21
+ if (nacl_interface_query(NACL_IRT_RANDOM_v0_1, &rand_intf,
22
+ sizeof rand_intf) != sizeof rand_intf) {
19
23
  abort();
20
24
  }
21
- assert(readnb == size);
25
+ while (toread > (size_t) 0U) {
26
+ if (rand_intf.get_random_bytes(buf_, size, &readnb) != 0 ||
27
+ readnb > size) {
28
+ abort();
29
+ }
30
+ toread -= readnb;
31
+ buf_ += readnb;
32
+ }
22
33
  }
23
34
 
24
35
  static uint32_t
@@ -20,18 +20,27 @@
20
20
  /* C++Builder defines a "random" macro */
21
21
  #undef random
22
22
 
23
- #ifndef __EMSCRIPTEN__
24
- #ifdef __native_client__
25
- static const randombytes_implementation *implementation =
26
- &randombytes_nativeclient_implementation;
27
- #else
28
- static const randombytes_implementation *implementation =
29
- &randombytes_sysrandom_implementation;
30
- #endif
23
+ static const randombytes_implementation *implementation;
24
+
25
+ #ifdef __EMSCRIPTEN__
26
+ # define RANDOMBYTES_DEFAULT_IMPLEMENTATION NULL
31
27
  #else
32
- static const randombytes_implementation *implementation = NULL;
28
+ # ifdef __native_client__
29
+ # define RANDOMBYTES_DEFAULT_IMPLEMENTATION &randombytes_nativeclient_implementation;
30
+ # else
31
+ # define RANDOMBYTES_DEFAULT_IMPLEMENTATION &randombytes_sysrandom_implementation;
32
+ # endif
33
33
  #endif
34
34
 
35
+ static void
36
+ randombytes_init_if_needed(void)
37
+ {
38
+ if (implementation == NULL) {
39
+ implementation = RANDOMBYTES_DEFAULT_IMPLEMENTATION;
40
+ randombytes_stir();
41
+ }
42
+ }
43
+
35
44
  int
36
45
  randombytes_set_implementation(randombytes_implementation *impl)
37
46
  {
@@ -44,6 +53,7 @@ const char *
44
53
  randombytes_implementation_name(void)
45
54
  {
46
55
  #ifndef __EMSCRIPTEN__
56
+ randombytes_init_if_needed();
47
57
  return implementation->implementation_name();
48
58
  #else
49
59
  return "js";
@@ -54,6 +64,7 @@ uint32_t
54
64
  randombytes_random(void)
55
65
  {
56
66
  #ifndef __EMSCRIPTEN__
67
+ randombytes_init_if_needed();
57
68
  return implementation->random();
58
69
  #else
59
70
  return EM_ASM_INT_V({
@@ -66,7 +77,8 @@ void
66
77
  randombytes_stir(void)
67
78
  {
68
79
  #ifndef __EMSCRIPTEN__
69
- if (implementation != NULL && implementation->stir != NULL) {
80
+ randombytes_init_if_needed();
81
+ if (implementation->stir != NULL) {
70
82
  implementation->stir();
71
83
  }
72
84
  #else
@@ -110,11 +122,8 @@ randombytes_uniform(const uint32_t upper_bound)
110
122
  uint32_t min;
111
123
  uint32_t r;
112
124
 
113
- #ifdef __EMSCRIPTEN__
114
- if (implementation != NULL && implementation->uniform != NULL) {
115
- return implementation->uniform(upper_bound);
116
- }
117
- #else
125
+ #ifndef __EMSCRIPTEN__
126
+ randombytes_init_if_needed();
118
127
  if (implementation->uniform != NULL) {
119
128
  return implementation->uniform(upper_bound);
120
129
  }
@@ -134,6 +143,7 @@ void
134
143
  randombytes_buf(void * const buf, const size_t size)
135
144
  {
136
145
  #ifndef __EMSCRIPTEN__
146
+ randombytes_init_if_needed();
137
147
  if (size > (size_t) 0U) {
138
148
  implementation->buf(buf, size);
139
149
  }
@@ -7,6 +7,7 @@
7
7
  #endif
8
8
  #ifdef __linux__
9
9
  # include <sys/syscall.h>
10
+ # include <poll.h>
10
11
  #endif
11
12
 
12
13
  #include <assert.h>
@@ -26,6 +27,7 @@
26
27
  #include "randombytes.h"
27
28
  #include "randombytes_salsa20_random.h"
28
29
  #include "utils.h"
30
+ #include "private/mutex.h"
29
31
 
30
32
  #ifdef _WIN32
31
33
  # include <windows.h>
@@ -128,6 +130,33 @@ safe_read(const int fd, void * const buf_, size_t size)
128
130
  #endif
129
131
 
130
132
  #ifndef _WIN32
133
+ # if defined(__linux__) && !defined(USE_BLOCKING_RANDOM)
134
+ static int
135
+ randombytes_block_on_dev_random(void)
136
+ {
137
+ struct pollfd pfd;
138
+ int fd;
139
+ int pret;
140
+
141
+ fd = open("/dev/random", O_RDONLY);
142
+ if (fd == -1) {
143
+ return 0;
144
+ }
145
+ pfd.fd = fd;
146
+ pfd.events = POLLIN;
147
+ pfd.revents = 0;
148
+ do {
149
+ pret = poll(&pfd, 1, -1);
150
+ } while (pret < 0 && (errno == EINTR || errno == EAGAIN));
151
+ if (pret != 1) {
152
+ (void) close(fd);
153
+ errno = EIO;
154
+ return -1;
155
+ }
156
+ return close(fd);
157
+ }
158
+ # endif
159
+
131
160
  # ifndef HAVE_SAFE_ARC4RANDOM
132
161
  static int
133
162
  randombytes_salsa20_random_random_dev_open(void)
@@ -143,6 +172,11 @@ randombytes_salsa20_random_random_dev_open(void)
143
172
  const char ** device = devices;
144
173
  int fd;
145
174
 
175
+ # if defined(__linux__) && !defined(USE_BLOCKING_RANDOM)
176
+ if (randombytes_block_on_dev_random() != 0) {
177
+ return -1;
178
+ }
179
+ # endif
146
180
  do {
147
181
  fd = open(*device, O_RDONLY);
148
182
  if (fd != -1) {
@@ -173,7 +207,7 @@ randombytes_salsa20_random_random_dev_open(void)
173
207
  }
174
208
  # endif
175
209
 
176
- # ifdef SYS_getrandom
210
+ # if defined(SYS_getrandom) && defined(__NR_getrandom)
177
211
  static int
178
212
  _randombytes_linux_getrandom(void * const buf, const size_t size)
179
213
  {
@@ -221,7 +255,7 @@ randombytes_salsa20_random_init(void)
221
255
  errno = errno_save;
222
256
  # else
223
257
 
224
- # ifdef SYS_getrandom
258
+ # if defined(SYS_getrandom) && defined(__NR_getrandom)
225
259
  {
226
260
  unsigned char fodder[16];
227
261
 
@@ -264,7 +298,7 @@ randombytes_salsa20_random_rekey(const unsigned char * const mix)
264
298
  }
265
299
 
266
300
  static void
267
- randombytes_salsa20_random_stir(void)
301
+ randombytes_salsa20_random_stir_unlocked(void)
268
302
  {
269
303
  /* constant to personalize the hash function */
270
304
  const unsigned char hsigma[crypto_generichash_KEYBYTES] = {
@@ -287,7 +321,7 @@ randombytes_salsa20_random_stir(void)
287
321
 
288
322
  # ifdef HAVE_SAFE_ARC4RANDOM
289
323
  arc4random_buf(m0, sizeof m0);
290
- # elif defined(SYS_getrandom)
324
+ # elif defined(SYS_getrandom) && defined(__NR_getrandom)
291
325
  if (stream.getrandom_available != 0) {
292
326
  if (randombytes_linux_getrandom(m0, sizeof m0) != 0) {
293
327
  abort(); /* LCOV_EXCL_LINE */
@@ -322,18 +356,30 @@ randombytes_salsa20_random_stir(void)
322
356
  #endif
323
357
  }
324
358
 
359
+ static void
360
+ randombytes_salsa20_random_stir(void)
361
+ {
362
+ if (sodium_crit_enter() != 0) {
363
+ abort();
364
+ }
365
+ randombytes_salsa20_random_stir_unlocked();
366
+ if (sodium_crit_leave() != 0) {
367
+ abort();
368
+ }
369
+ }
370
+
325
371
  static void
326
372
  randombytes_salsa20_random_stir_if_needed(void)
327
373
  {
328
374
  #ifdef HAVE_GETPID
329
375
  if (stream.initialized == 0) {
330
- randombytes_salsa20_random_stir();
376
+ randombytes_salsa20_random_stir_unlocked();
331
377
  } else if (stream.pid != getpid()) {
332
378
  abort();
333
379
  }
334
380
  #else
335
381
  if (stream.initialized == 0) {
336
- randombytes_salsa20_random_stir();
382
+ randombytes_salsa20_random_stir_unlocked();
337
383
  }
338
384
  #endif
339
385
  }
@@ -343,6 +389,9 @@ randombytes_salsa20_random_close(void)
343
389
  {
344
390
  int ret = -1;
345
391
 
392
+ if (sodium_crit_enter() != 0) {
393
+ abort();
394
+ }
346
395
  #ifndef _WIN32
347
396
  if (stream.random_data_source_fd != -1 &&
348
397
  close(stream.random_data_source_fd) == 0) {
@@ -358,7 +407,7 @@ randombytes_salsa20_random_close(void)
358
407
  ret = 0;
359
408
  # endif
360
409
 
361
- # ifdef SYS_getrandom
410
+ # if defined(SYS_getrandom) && defined(__NR_getrandom)
362
411
  if (stream.getrandom_available != 0) {
363
412
  ret = 0;
364
413
  }
@@ -370,6 +419,9 @@ randombytes_salsa20_random_close(void)
370
419
  ret = 0;
371
420
  }
372
421
  #endif
422
+ if (sodium_crit_leave() != 0) {
423
+ abort();
424
+ }
373
425
  return ret;
374
426
  }
375
427
 
@@ -379,6 +431,9 @@ randombytes_salsa20_random_buf(void * const buf, const size_t size)
379
431
  size_t i;
380
432
  int ret;
381
433
 
434
+ if (sodium_crit_enter() != 0) {
435
+ abort();
436
+ }
382
437
  randombytes_salsa20_random_stir_if_needed();
383
438
  COMPILER_ASSERT(sizeof stream.nonce == crypto_stream_salsa20_NONCEBYTES);
384
439
  #ifdef ULONG_LONG_MAX
@@ -394,14 +449,20 @@ randombytes_salsa20_random_buf(void * const buf, const size_t size)
394
449
  stream.nonce++;
395
450
  crypto_stream_salsa20_xor(stream.key, stream.key, sizeof stream.key,
396
451
  (unsigned char *) &stream.nonce, stream.key);
452
+ if (sodium_crit_leave() != 0) {
453
+ abort();
454
+ }
397
455
  }
398
456
 
399
457
  static uint32_t
400
- randombytes_salsa20_random_getword(void)
458
+ randombytes_salsa20_random(void)
401
459
  {
402
460
  uint32_t val;
403
461
  int ret;
404
462
 
463
+ if (sodium_crit_enter() != 0) {
464
+ abort();
465
+ }
405
466
  COMPILER_ASSERT(sizeof stream.rnd32 >= (sizeof stream.key) + (sizeof val));
406
467
  COMPILER_ASSERT(((sizeof stream.rnd32) - (sizeof stream.key))
407
468
  % sizeof val == (size_t) 0U);
@@ -420,16 +481,12 @@ randombytes_salsa20_random_getword(void)
420
481
  stream.rnd32_outleft -= sizeof val;
421
482
  memcpy(&val, &stream.rnd32[stream.rnd32_outleft], sizeof val);
422
483
  memset(&stream.rnd32[stream.rnd32_outleft], 0, sizeof val);
423
-
484
+ if (sodium_crit_leave() != 0) {
485
+ abort();
486
+ }
424
487
  return val;
425
488
  }
426
489
 
427
- static uint32_t
428
- randombytes_salsa20_random(void)
429
- {
430
- return randombytes_salsa20_random_getword();
431
- }
432
-
433
490
  static const char *
434
491
  randombytes_salsa20_implementation_name(void)
435
492
  {
@@ -7,6 +7,7 @@
7
7
  #endif
8
8
  #ifdef __linux__
9
9
  # include <sys/syscall.h>
10
+ # include <poll.h>
10
11
  #endif
11
12
 
12
13
  #include <assert.h>
@@ -24,6 +25,12 @@
24
25
  #include "utils.h"
25
26
 
26
27
  #ifdef _WIN32
28
+ /* `RtlGenRandom` is used over `CryptGenRandom` on Microsoft Windows based systems:
29
+ * - `CryptGenRandom` requires pulling in `CryptoAPI` which causes unnecessary
30
+ * memory overhead if this API is not being used for other purposes
31
+ * - `RtlGenRandom` is thus called directly instead. A detailed explanation
32
+ * can be found here: https://blogs.msdn.microsoft.com/michael_howard/2005/01/14/cryptographically-secure-random-number-on-windows-without-using-cryptoapi/
33
+ */
27
34
  # include <windows.h>
28
35
  # define RtlGenRandom SystemFunction036
29
36
  # if defined(__cplusplus)
@@ -107,6 +114,33 @@ safe_read(const int fd, void * const buf_, size_t size)
107
114
  #endif
108
115
 
109
116
  #ifndef _WIN32
117
+ # if defined(__linux__) && !defined(USE_BLOCKING_RANDOM)
118
+ static int
119
+ randombytes_block_on_dev_random(void)
120
+ {
121
+ struct pollfd pfd;
122
+ int fd;
123
+ int pret;
124
+
125
+ fd = open("/dev/random", O_RDONLY);
126
+ if (fd == -1) {
127
+ return 0;
128
+ }
129
+ pfd.fd = fd;
130
+ pfd.events = POLLIN;
131
+ pfd.revents = 0;
132
+ do {
133
+ pret = poll(&pfd, 1, -1);
134
+ } while (pret < 0 && (errno == EINTR || errno == EAGAIN));
135
+ if (pret != 1) {
136
+ (void) close(fd);
137
+ errno = EIO;
138
+ return -1;
139
+ }
140
+ return close(fd);
141
+ }
142
+ # endif
143
+
110
144
  static int
111
145
  randombytes_sysrandom_random_dev_open(void)
112
146
  {
@@ -121,6 +155,11 @@ randombytes_sysrandom_random_dev_open(void)
121
155
  const char ** device = devices;
122
156
  int fd;
123
157
 
158
+ # if defined(__linux__) && !defined(USE_BLOCKING_RANDOM)
159
+ if (randombytes_block_on_dev_random() != 0) {
160
+ return -1;
161
+ }
162
+ # endif
124
163
  do {
125
164
  fd = open(*device, O_RDONLY);
126
165
  if (fd != -1) {
@@ -150,7 +189,7 @@ randombytes_sysrandom_random_dev_open(void)
150
189
  /* LCOV_EXCL_STOP */
151
190
  }
152
191
 
153
- # ifdef SYS_getrandom
192
+ # if defined(SYS_getrandom) && defined(__NR_getrandom)
154
193
  static int
155
194
  _randombytes_linux_getrandom(void * const buf, const size_t size)
156
195
  {
@@ -191,7 +230,7 @@ randombytes_sysrandom_init(void)
191
230
  {
192
231
  const int errno_save = errno;
193
232
 
194
- # ifdef SYS_getrandom
233
+ # if defined(SYS_getrandom) && defined(__NR_getrandom)
195
234
  {
196
235
  unsigned char fodder[16];
197
236
 
@@ -248,7 +287,7 @@ randombytes_sysrandom_close(void)
248
287
  stream.initialized = 0;
249
288
  ret = 0;
250
289
  }
251
- # ifdef SYS_getrandom
290
+ # if defined(SYS_getrandom) && defined(__NR_getrandom)
252
291
  if (stream.getrandom_available != 0) {
253
292
  ret = 0;
254
293
  }
@@ -271,7 +310,7 @@ randombytes_sysrandom_buf(void * const buf, const size_t size)
271
310
  assert(size <= ULONG_LONG_MAX);
272
311
  #endif
273
312
  #ifndef _WIN32
274
- # ifdef SYS_getrandom
313
+ # if defined(SYS_getrandom) && defined(__NR_getrandom)
275
314
  if (stream.getrandom_available != 0) {
276
315
  if (randombytes_linux_getrandom(buf, size) != 0) {
277
316
  abort();
@@ -1,4 +1,12 @@
1
1
 
2
+ #include <string.h>
3
+ #include <time.h>
4
+ #ifdef HAVE_PTHREAD
5
+ # include <pthread.h>
6
+ #elif defined(_WIN32)
7
+ # include <windows.h>
8
+ #endif
9
+
2
10
  #include "core.h"
3
11
  #include "crypto_generichash.h"
4
12
  #include "crypto_onetimeauth.h"
@@ -8,9 +16,10 @@
8
16
  #include "randombytes.h"
9
17
  #include "runtime.h"
10
18
  #include "utils.h"
19
+ #include "private/mutex.h"
11
20
 
12
21
  #if !defined(_MSC_VER) && 0
13
- # warning This is unstable, untested, development code.
22
+ # warning *** This is unstable, untested, development code.
14
23
  # warning It might not compile. It might not work as expected.
15
24
  # warning It might be totally insecure.
16
25
  # warning Do not use this in production.
@@ -18,12 +27,24 @@
18
27
  # warning Alternatively, use the "stable" branch in the git repository.
19
28
  #endif
20
29
 
21
- static int initialized;
30
+ #if !defined(_MSC_VER) && (!defined(CONFIGURED) || CONFIGURED != 1)
31
+ # warning *** The library is being compiled using an undocumented method.
32
+ # warning This is not supported. It has not been tested, it might not
33
+ # warning work as expected, and performance is likely to be suboptimal.
34
+ #endif
35
+
36
+ static volatile int initialized;
22
37
 
23
38
  int
24
39
  sodium_init(void)
25
40
  {
41
+ if (sodium_crit_enter() != 0) {
42
+ return -1;
43
+ }
26
44
  if (initialized != 0) {
45
+ if (sodium_crit_leave() != 0) {
46
+ return -1;
47
+ }
27
48
  return 1;
28
49
  }
29
50
  _sodium_runtime_get_cpu_features();
@@ -35,6 +56,114 @@ sodium_init(void)
35
56
  _crypto_scalarmult_curve25519_pick_best_implementation();
36
57
  _crypto_stream_chacha20_pick_best_implementation();
37
58
  initialized = 1;
59
+ if (sodium_crit_leave() != 0) {
60
+ return -1;
61
+ }
62
+ return 0;
63
+ }
64
+
65
+ #if defined(HAVE_PTHREAD) && !defined(__EMSCRIPTEN__)
66
+
67
+ static pthread_mutex_t _sodium_lock = PTHREAD_MUTEX_INITIALIZER;
68
+
69
+ int
70
+ sodium_crit_enter(void)
71
+ {
72
+ return pthread_mutex_lock(&_sodium_lock);
73
+ }
74
+
75
+ int
76
+ sodium_crit_leave(void)
77
+ {
78
+ return pthread_mutex_unlock(&_sodium_lock);
79
+ }
80
+
81
+ #elif defined(_WIN32)
82
+
83
+ static CRITICAL_SECTION _sodium_lock;
84
+ static volatile LONG _sodium_lock_initialized;
85
+
86
+ int
87
+ _sodium_crit_init(void)
88
+ {
89
+ LONG status = 0L;
90
+
91
+ while ((status = InterlockedCompareExchange(&_sodium_lock_initialized,
92
+ 1L, 0L)) == 1L) {
93
+ Sleep(0);
94
+ }
95
+
96
+ switch (status) {
97
+ case 0L:
98
+ InitializeCriticalSection(&_sodium_lock);
99
+ return InterlockedExchange(&_sodium_lock_initialized, 2L) == 1L ? 0 : -1;
100
+ case 2L:
101
+ return 0;
102
+ default: /* should never be reached */
103
+ return -1;
104
+ }
105
+ }
106
+
107
+ int
108
+ sodium_crit_enter(void)
109
+ {
110
+ if (_sodium_crit_init() != 0) {
111
+ return -1;
112
+ }
113
+ EnterCriticalSection(&_sodium_lock);
38
114
 
39
115
  return 0;
40
116
  }
117
+
118
+ int
119
+ sodium_crit_leave(void)
120
+ {
121
+ LeaveCriticalSection(&_sodium_lock);
122
+
123
+ return 0;
124
+ }
125
+
126
+ #elif defined(__GNUC__) && !defined(__EMSCRIPTEN__) && !defined(__native_client__)
127
+
128
+ static volatile int _sodium_lock;
129
+
130
+ int
131
+ sodium_crit_enter(void)
132
+ {
133
+ # ifdef HAVE_NANOSLEEP
134
+ struct timespec q;
135
+ memset(&q, 0, sizeof q);
136
+ # endif
137
+ while (__sync_lock_test_and_set(&_sodium_lock, 1) != 0) {
138
+ # ifdef HAVE_NANOSLEEP
139
+ (void) nanosleep(&q, NULL);
140
+ # elif defined(__x86_64__) || defined(__i386__)
141
+ __asm__ __volatile__ ("pause");
142
+ # endif
143
+ }
144
+ return 0;
145
+ }
146
+
147
+ int
148
+ sodium_crit_leave(void)
149
+ {
150
+ __sync_lock_release(&_sodium_lock);
151
+
152
+ return 0;
153
+ }
154
+
155
+ #else
156
+
157
+ int
158
+ sodium_crit_enter(void)
159
+ {
160
+ return 0;
161
+ }
162
+
163
+ int
164
+ sodium_crit_leave(void)
165
+ {
166
+ return 0;
167
+ }
168
+
169
+ #endif