rbnacl-libsodium 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +4 -0
  3. data/lib/rbnacl/libsodium/version.rb +1 -1
  4. data/vendor/libsodium/AUTHORS +3 -0
  5. data/vendor/libsodium/ChangeLog +21 -0
  6. data/vendor/libsodium/Makefile.in +9 -0
  7. data/vendor/libsodium/THANKS +3 -0
  8. data/vendor/libsodium/aclocal.m4 +1 -0
  9. data/vendor/libsodium/autom4te.cache/output.1 +919 -85
  10. data/vendor/libsodium/autom4te.cache/output.5 +18351 -0
  11. data/vendor/libsodium/autom4te.cache/requests +960 -725
  12. data/vendor/libsodium/autom4te.cache/traces.1 +289 -247
  13. data/vendor/libsodium/autom4te.cache/traces.5 +3032 -0
  14. data/vendor/libsodium/build-aux/ltmain.sh +70 -11
  15. data/vendor/libsodium/builds/msvc/properties/ARM.props +20 -0
  16. data/vendor/libsodium/builds/msvc/properties/ReleaseDEXE.props +1 -1
  17. data/vendor/libsodium/builds/msvc/version.h +2 -2
  18. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj +3 -1
  19. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters +13 -1
  20. data/vendor/libsodium/builds/msvc/vs2010/test/test.vcxproj +244 -241
  21. data/vendor/libsodium/builds/msvc/vs2010/test/test.vcxproj.filters +192 -189
  22. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj +2 -0
  23. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters +13 -1
  24. data/vendor/libsodium/builds/msvc/vs2012/test/test.vcxproj +244 -241
  25. data/vendor/libsodium/builds/msvc/vs2012/test/test.vcxproj.filters +192 -189
  26. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj +3 -1
  27. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters +13 -1
  28. data/vendor/libsodium/builds/msvc/vs2013/test/test.vcxproj +244 -241
  29. data/vendor/libsodium/builds/msvc/vs2013/test/test.vcxproj.filters +192 -189
  30. data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj +3 -1
  31. data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters +13 -1
  32. data/vendor/libsodium/builds/msvc/vs2015/test/test.vcxproj +244 -241
  33. data/vendor/libsodium/builds/msvc/vs2015/test/test.vcxproj.filters +192 -189
  34. data/vendor/libsodium/configure +918 -84
  35. data/vendor/libsodium/configure.ac +89 -15
  36. data/vendor/libsodium/dist-build/Makefile.am +6 -2
  37. data/vendor/libsodium/dist-build/Makefile.in +15 -2
  38. data/vendor/libsodium/dist-build/android-armv8-a.sh +4 -0
  39. data/vendor/libsodium/dist-build/android-build.sh +9 -9
  40. data/vendor/libsodium/dist-build/android-mips64.sh +4 -0
  41. data/vendor/libsodium/dist-build/android-x86-64.sh +4 -0
  42. data/vendor/libsodium/dist-build/emscripten.sh +3 -3
  43. data/vendor/libsodium/dist-build/ios.sh +5 -5
  44. data/vendor/libsodium/dist-build/nativeclient.sh +28 -0
  45. data/vendor/libsodium/examples/Makefile +21 -0
  46. data/vendor/libsodium/examples/auth.c +68 -0
  47. data/vendor/libsodium/examples/box.c +133 -0
  48. data/vendor/libsodium/examples/box_detached.c +132 -0
  49. data/vendor/libsodium/examples/generichash.c +80 -0
  50. data/vendor/libsodium/examples/generichash_stream.c +58 -0
  51. data/vendor/libsodium/examples/shorthash.c +58 -0
  52. data/vendor/libsodium/examples/sign.c +78 -0
  53. data/vendor/libsodium/examples/utils.h +106 -0
  54. data/vendor/libsodium/libsodium-uninstalled.pc.in +1 -1
  55. data/vendor/libsodium/libsodium.vcxproj +2 -0
  56. data/vendor/libsodium/libsodium.vcxproj.filters +6 -0
  57. data/vendor/libsodium/m4/ax_check_compile_flag.m4 +2 -2
  58. data/vendor/libsodium/m4/ax_check_define.m4 +92 -0
  59. data/vendor/libsodium/m4/ax_check_link_flag.m4 +3 -2
  60. data/vendor/libsodium/m4/libtool.m4 +111 -60
  61. data/vendor/libsodium/m4/ltoptions.m4 +1 -1
  62. data/vendor/libsodium/m4/ltsugar.m4 +1 -1
  63. data/vendor/libsodium/m4/ltversion.m4 +6 -6
  64. data/vendor/libsodium/m4/lt~obsolete.m4 +1 -1
  65. data/vendor/libsodium/msvc-scripts/Makefile.in +9 -0
  66. data/vendor/libsodium/msvc-scripts/process.bat +2 -2
  67. data/vendor/libsodium/src/Makefile.in +9 -0
  68. data/vendor/libsodium/src/libsodium/Makefile.am +27 -3
  69. data/vendor/libsodium/src/libsodium/Makefile.in +170 -63
  70. data/vendor/libsodium/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +852 -0
  71. data/vendor/libsodium/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c +137 -17
  72. data/vendor/libsodium/src/libsodium/crypto_auth/hmacsha256/cp/hmac_hmacsha256.c +1 -0
  73. data/vendor/libsodium/src/libsodium/crypto_auth/hmacsha512/cp/hmac_hmacsha512.c +1 -0
  74. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-ref.c +10 -6
  75. data/vendor/libsodium/src/libsodium/crypto_generichash/crypto_generichash.c +1 -1
  76. data/vendor/libsodium/src/libsodium/crypto_hash/sha256/cp/hash_sha256.c +29 -23
  77. data/vendor/libsodium/src/libsodium/crypto_hash/sha512/cp/hash_sha512.c +9 -10
  78. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c +4 -2
  79. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt.h +1 -0
  80. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c +4 -0
  81. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c +3 -0
  82. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c +2 -1
  83. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/fe_frombytes_curve25519_ref10.c +10 -10
  84. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/ref/api.h +10 -0
  85. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c +51 -0
  86. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/stream_chacha20_api.c +29 -0
  87. data/vendor/libsodium/src/libsodium/include/Makefile.am +6 -0
  88. data/vendor/libsodium/src/libsodium/include/Makefile.in +67 -40
  89. data/vendor/libsodium/src/libsodium/include/sodium.h +4 -0
  90. data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_aes256gcm.h +88 -0
  91. data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_chacha20poly1305.h +28 -2
  92. data/vendor/libsodium/src/libsodium/include/sodium/crypto_generichash_blake2b.h +0 -6
  93. data/vendor/libsodium/src/libsodium/include/sodium/crypto_hash_sha256.h +1 -1
  94. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_chacha20.h +23 -0
  95. data/vendor/libsodium/src/libsodium/include/sodium/export.h +8 -0
  96. data/vendor/libsodium/src/libsodium/include/sodium/randombytes_nativeclient.h +37 -0
  97. data/vendor/libsodium/src/libsodium/include/sodium/randombytes_salsa20_random.h +3 -2
  98. data/vendor/libsodium/src/libsodium/include/sodium/runtime.h +6 -0
  99. data/vendor/libsodium/src/libsodium/include/sodium/utils.h +15 -1
  100. data/vendor/libsodium/src/libsodium/randombytes/nativeclient/randombytes_nativeclient.c +49 -0
  101. data/vendor/libsodium/src/libsodium/randombytes/randombytes.c +11 -1
  102. data/vendor/libsodium/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +71 -45
  103. data/vendor/libsodium/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c +12 -2
  104. data/vendor/libsodium/src/libsodium/sodium/runtime.c +26 -3
  105. data/vendor/libsodium/src/libsodium/sodium/utils.c +86 -13
  106. data/vendor/libsodium/test/Makefile.in +9 -0
  107. data/vendor/libsodium/test/default/Makefile.am +130 -0
  108. data/vendor/libsodium/test/default/Makefile.in +197 -50
  109. data/vendor/libsodium/test/default/aead_aes256gcm.c +3197 -0
  110. data/vendor/libsodium/test/default/aead_aes256gcm.exp +1 -0
  111. data/vendor/libsodium/test/default/aead_chacha20poly1305.c +150 -17
  112. data/vendor/libsodium/test/default/aead_chacha20poly1305.exp +51 -0
  113. data/vendor/libsodium/test/default/chacha20.c +80 -5
  114. data/vendor/libsodium/test/default/chacha20.exp +11 -0
  115. data/vendor/libsodium/test/default/generichash.c +1332 -1
  116. data/vendor/libsodium/test/default/generichash.exp +1 -1
  117. data/vendor/libsodium/test/default/generichash2.c +3 -2
  118. data/vendor/libsodium/test/default/generichash3.c +2 -1
  119. data/vendor/libsodium/test/default/nacl-test-wrapper.sh +26 -0
  120. data/vendor/libsodium/test/default/randombytes.c +4 -0
  121. data/vendor/libsodium/test/default/scalarmult.c +33 -14
  122. data/vendor/libsodium/test/default/scalarmult.exp +4 -4
  123. data/vendor/libsodium/test/default/secretbox.c +9 -0
  124. data/vendor/libsodium/test/default/secretbox.exp +19 -0
  125. data/vendor/libsodium/test/default/secretbox_easy.c +40 -4
  126. data/vendor/libsodium/test/default/secretbox_easy.exp +58 -1
  127. data/vendor/libsodium/test/default/sodium_core.c +2 -0
  128. data/vendor/libsodium/test/default/sodium_utils.c +46 -7
  129. data/vendor/libsodium/test/default/sodium_utils.exp +5 -0
  130. data/vendor/libsodium/test/default/stream.c +1 -0
  131. metadata +28 -3
@@ -29,7 +29,7 @@ size_t crypto_aead_chacha20poly1305_abytes(void);
29
29
 
30
30
  SODIUM_EXPORT
31
31
  int crypto_aead_chacha20poly1305_encrypt(unsigned char *c,
32
- unsigned long long *clen,
32
+ unsigned long long *clen_p,
33
33
  const unsigned char *m,
34
34
  unsigned long long mlen,
35
35
  const unsigned char *ad,
@@ -40,7 +40,7 @@ int crypto_aead_chacha20poly1305_encrypt(unsigned char *c,
40
40
 
41
41
  SODIUM_EXPORT
42
42
  int crypto_aead_chacha20poly1305_decrypt(unsigned char *m,
43
- unsigned long long *mlen,
43
+ unsigned long long *mlen_p,
44
44
  unsigned char *nsec,
45
45
  const unsigned char *c,
46
46
  unsigned long long clen,
@@ -48,6 +48,32 @@ int crypto_aead_chacha20poly1305_decrypt(unsigned char *m,
48
48
  unsigned long long adlen,
49
49
  const unsigned char *npub,
50
50
  const unsigned char *k);
51
+
52
+ #define crypto_aead_chacha20poly1305_IETF_NPUBBYTES 12U
53
+ SODIUM_EXPORT
54
+ size_t crypto_aead_chacha20poly1305_ietf_npubbytes(void);
55
+
56
+ SODIUM_EXPORT
57
+ int crypto_aead_chacha20poly1305_ietf_encrypt(unsigned char *c,
58
+ unsigned long long *clen_p,
59
+ const unsigned char *m,
60
+ unsigned long long mlen,
61
+ const unsigned char *ad,
62
+ unsigned long long adlen,
63
+ const unsigned char *nsec,
64
+ const unsigned char *npub,
65
+ const unsigned char *k);
66
+
67
+ SODIUM_EXPORT
68
+ int crypto_aead_chacha20poly1305_ietf_decrypt(unsigned char *m,
69
+ unsigned long long *mlen_p,
70
+ unsigned char *nsec,
71
+ const unsigned char *c,
72
+ unsigned long long clen,
73
+ const unsigned char *ad,
74
+ unsigned long long adlen,
75
+ const unsigned char *npub,
76
+ const unsigned char *k);
51
77
  #ifdef __cplusplus
52
78
  }
53
79
  #endif
@@ -7,12 +7,6 @@
7
7
 
8
8
  #include "export.h"
9
9
 
10
- #if defined(_MSC_VER)
11
- # define CRYPTO_ALIGN(x) __declspec(align(x))
12
- #else
13
- # define CRYPTO_ALIGN(x) __attribute__((aligned(x)))
14
- #endif
15
-
16
10
  #ifdef __cplusplus
17
11
  # if __GNUC__
18
12
  # pragma GCC diagnostic ignored "-Wlong-long"
@@ -23,7 +23,7 @@ extern "C" {
23
23
 
24
24
  typedef struct crypto_hash_sha256_state {
25
25
  uint32_t state[8];
26
- uint32_t count[2];
26
+ uint64_t count;
27
27
  unsigned char buf[64];
28
28
  } crypto_hash_sha256_state;
29
29
  SODIUM_EXPORT
@@ -28,6 +28,8 @@ size_t crypto_stream_chacha20_keybytes(void);
28
28
  SODIUM_EXPORT
29
29
  size_t crypto_stream_chacha20_noncebytes(void);
30
30
 
31
+ /* ChaCha20 with a 64-bit nonce and a 64-bit counter, as originally designed */
32
+
31
33
  SODIUM_EXPORT
32
34
  int crypto_stream_chacha20(unsigned char *c, unsigned long long clen,
33
35
  const unsigned char *n, const unsigned char *k);
@@ -42,6 +44,27 @@ int crypto_stream_chacha20_xor_ic(unsigned char *c, const unsigned char *m,
42
44
  unsigned long long mlen,
43
45
  const unsigned char *n, uint64_t ic,
44
46
  const unsigned char *k);
47
+
48
+ /* ChaCha20 with a 96-bit nonce and a 32-bit counter (IETF) */
49
+
50
+ #define crypto_stream_chacha20_IETF_NONCEBYTES 12U
51
+ SODIUM_EXPORT
52
+ size_t crypto_stream_chacha20_ietf_noncebytes(void);
53
+
54
+ SODIUM_EXPORT
55
+ int crypto_stream_chacha20_ietf(unsigned char *c, unsigned long long clen,
56
+ const unsigned char *n, const unsigned char *k);
57
+
58
+ SODIUM_EXPORT
59
+ int crypto_stream_chacha20_ietf_xor(unsigned char *c, const unsigned char *m,
60
+ unsigned long long mlen, const unsigned char *n,
61
+ const unsigned char *k);
62
+
63
+ SODIUM_EXPORT
64
+ int crypto_stream_chacha20_ietf_xor_ic(unsigned char *c, const unsigned char *m,
65
+ unsigned long long mlen,
66
+ const unsigned char *n, uint32_t ic,
67
+ const unsigned char *k);
45
68
  #ifdef __cplusplus
46
69
  }
47
70
  #endif
@@ -29,4 +29,12 @@
29
29
  # endif
30
30
  #endif
31
31
 
32
+ #ifndef CRYPTO_ALIGN
33
+ # if defined(__INTEL_COMPILER) || defined(_MSC_VER)
34
+ # define CRYPTO_ALIGN(x) __declspec(align(x))
35
+ # else
36
+ # define CRYPTO_ALIGN(x) __attribute__((aligned(x)))
37
+ # endif
38
+ #endif
39
+
32
40
  #endif
@@ -0,0 +1,37 @@
1
+
2
+ #ifndef randombytes_nativeclient_H
3
+ #define randombytes_nativeclient_H
4
+
5
+ #ifdef __native_client__
6
+
7
+ #include <stddef.h>
8
+ #include <stdint.h>
9
+
10
+ #include "export.h"
11
+
12
+ #ifdef __cplusplus
13
+ extern "C" {
14
+ #endif
15
+
16
+ SODIUM_EXPORT
17
+ extern struct randombytes_implementation randombytes_nativeclient_implementation;
18
+
19
+ SODIUM_EXPORT
20
+ const char *randombytes_nativeclient_implementation_name(void);
21
+
22
+ SODIUM_EXPORT
23
+ uint32_t randombytes_nativeclient(void);
24
+
25
+ SODIUM_EXPORT
26
+ uint32_t randombytes_nativeclient_uniform(const uint32_t upper_bound);
27
+
28
+ SODIUM_EXPORT
29
+ void randombytes_nativeclient_buf(void * const buf, const size_t size);
30
+
31
+ #ifdef __cplusplus
32
+ }
33
+ #endif
34
+
35
+ #endif
36
+
37
+ #endif
@@ -4,8 +4,9 @@
4
4
 
5
5
  /*
6
6
  * THREAD SAFETY: randombytes_salsa20_random*() functions are
7
- * fork()-safe but not thread-safe.
8
- * Always wrap them in a mutex if you need thread safety.
7
+ * not thread-safe.
8
+ * Always wrap them in a mutex if you need thread safety,
9
+ * and call randombytes_stir() after fork()ing.
9
10
  */
10
11
 
11
12
  #include <stddef.h>
@@ -20,6 +20,12 @@ int sodium_runtime_has_sse2(void);
20
20
  SODIUM_EXPORT
21
21
  int sodium_runtime_has_sse3(void);
22
22
 
23
+ SODIUM_EXPORT
24
+ int sodium_runtime_has_pclmul(void);
25
+
26
+ SODIUM_EXPORT
27
+ int sodium_runtime_has_aesni(void);
28
+
23
29
  #ifdef __cplusplus
24
30
  }
25
31
  #endif
@@ -19,7 +19,8 @@ extern "C" {
19
19
  SODIUM_EXPORT
20
20
  void sodium_memzero(void * const pnt, const size_t len);
21
21
 
22
- /* WARNING: sodium_memcmp() must be used to verify if two secret keys
22
+ /*
23
+ * WARNING: sodium_memcmp() must be used to verify if two secret keys
23
24
  * are equal, in constant time.
24
25
  * It returns 0 if the keys are equal, and -1 if they differ.
25
26
  * This function is not designed for lexicographical comparisons.
@@ -27,6 +28,19 @@ void sodium_memzero(void * const pnt, const size_t len);
27
28
  SODIUM_EXPORT
28
29
  int sodium_memcmp(const void * const b1_, const void * const b2_, size_t len);
29
30
 
31
+ /*
32
+ * sodium_compare() returns -1 if b1_ < b2_, 1 if b1_ > b2_ and 0 if b1_ == b2_
33
+ * It is suitable for lexicographical comparisons, or to compare nonces
34
+ * and counters stored in little-endian format.
35
+ * However, it is slower than sodium_memcmp().
36
+ */
37
+ SODIUM_EXPORT
38
+ int sodium_compare(const unsigned char *b1_, const unsigned char *b2_,
39
+ size_t len);
40
+
41
+ SODIUM_EXPORT
42
+ void sodium_increment(unsigned char *n, const size_t nlen);
43
+
30
44
  SODIUM_EXPORT
31
45
  char *sodium_bin2hex(char * const hex, const size_t hex_maxlen,
32
46
  const unsigned char * const bin, const size_t bin_len);
@@ -0,0 +1,49 @@
1
+
2
+ #include <assert.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+
6
+ #ifdef __native_client__
7
+ # include <nacl/nacl_random.h>
8
+
9
+ # include "utils.h"
10
+ # include "randombytes.h"
11
+ # include "randombytes_nativeclient.h"
12
+
13
+ void
14
+ randombytes_nativeclient_buf(void * const buf, const size_t size)
15
+ {
16
+ size_t readnb;
17
+
18
+ if (nacl_secure_random(buf, size, &readnb) != 0) {
19
+ abort();
20
+ }
21
+ assert(readnb == size);
22
+ }
23
+
24
+ uint32_t
25
+ randombytes_nativeclient_random(void)
26
+ {
27
+ uint32_t r;
28
+
29
+ randombytes_nativeclient_buf(&r, sizeof r);
30
+
31
+ return r;
32
+ }
33
+
34
+ const char *
35
+ randombytes_nativeclient_implementation_name(void)
36
+ {
37
+ return "nativeclient";
38
+ }
39
+
40
+ struct randombytes_implementation randombytes_nativeclient_implementation = {
41
+ SODIUM_C99(.implementation_name =) randombytes_nativeclient_implementation_name,
42
+ SODIUM_C99(.random =) randombytes_nativeclient_random,
43
+ SODIUM_C99(.stir =) NULL,
44
+ SODIUM_C99(.uniform =) NULL,
45
+ SODIUM_C99(.buf =) randombytes_nativeclient_buf,
46
+ SODIUM_C99(.close =) NULL
47
+ };
48
+
49
+ #endif
@@ -12,9 +12,18 @@
12
12
  #include "randombytes.h"
13
13
  #include "randombytes_sysrandom.h"
14
14
 
15
+ #ifdef __native_client__
16
+ # include "randombytes_nativeclient.h"
17
+ #endif
18
+
15
19
  #ifndef __EMSCRIPTEN__
20
+ #ifdef __native_client__
21
+ static const randombytes_implementation *implementation =
22
+ &randombytes_nativeclient_implementation;
23
+ #else
16
24
  static const randombytes_implementation *implementation =
17
25
  &randombytes_sysrandom_implementation;
26
+ #endif
18
27
  #else
19
28
  static const randombytes_implementation *implementation = NULL;
20
29
  #endif
@@ -60,7 +69,8 @@ randombytes_stir(void)
60
69
  EM_ASM({
61
70
  if (Module.getRandomValue === undefined) {
62
71
  try {
63
- var crypto_ = ("object" === typeof window ? window : self).crypto,
72
+ var window_ = "object" === typeof window ? window : self,
73
+ crypto_ = typeof window_.crypto !== "undefined" ? window_.crypto : window_.msCrypto,
64
74
  randomValuesStandard = function() {
65
75
  var buf = new Uint32Array(1);
66
76
  crypto_.getRandomValues(buf);
@@ -42,12 +42,16 @@ BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
42
42
  #define SHA512_MIN_PAD_SIZE (1U + 16U)
43
43
  #define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1])
44
44
 
45
+ #if defined(__OpenBSD__) || defined(__CloudABI__)
46
+ # define HAVE_SAFE_ARC4RANDOM 1
47
+ #endif
48
+
45
49
  typedef struct Salsa20Random_ {
46
50
  unsigned char key[crypto_stream_salsa20_KEYBYTES];
47
51
  unsigned char rnd32[16U * SALSA20_RANDOM_BLOCK_SIZE];
48
52
  uint64_t nonce;
49
53
  size_t rnd32_outleft;
50
- #ifndef _MSC_VER
54
+ #ifdef HAVE_GETPID
51
55
  pid_t pid;
52
56
  #endif
53
57
  int random_data_source_fd;
@@ -65,27 +69,24 @@ static Salsa20Random stream = {
65
69
  static uint64_t
66
70
  sodium_hrtime(void)
67
71
  {
68
- struct timeval tv;
69
- uint64_t ts = (uint64_t) 0U;
70
- int ret;
72
+ uint64_t ts;
71
73
 
72
74
  #ifdef _WIN32
73
- struct _timeb tb;
74
-
75
+ {
76
+ struct _timeb tb;
75
77
  # pragma warning(push)
76
78
  # pragma warning(disable: 4996)
77
- _ftime(&tb);
79
+ _ftime(&tb);
78
80
  # pragma warning(pop)
79
- tv.tv_sec = (long) tb.time;
80
- tv.tv_usec = ((int) tb.millitm) * 1000;
81
- ret = 0;
81
+ ts = ((uint64_t) tb.time) * 1000000U + ((uint64_t) tb.millitm) * 1000U;
82
+ }
82
83
  #else
83
- ret = gettimeofday(&tv, NULL);
84
- #endif
85
- assert(ret == 0);
86
- if (ret == 0) {
87
- ts = (uint64_t) tv.tv_sec * 1000000U + (uint64_t) tv.tv_usec;
84
+ {
85
+ struct timeval tv;
86
+ assert(gettimeofday(&tv, NULL) == 0);
87
+ ts = ((uint64_t) tv.tv_sec) * 1000000U + (uint64_t) tv.tv_usec;
88
88
  }
89
+ #endif
89
90
  return ts;
90
91
  }
91
92
 
@@ -115,15 +116,16 @@ safe_read(const int fd, void * const buf_, size_t size)
115
116
  #endif
116
117
 
117
118
  #ifndef _WIN32
119
+ # ifndef HAVE_SAFE_ARC4RANDOM
118
120
  static int
119
121
  randombytes_salsa20_random_random_dev_open(void)
120
122
  {
121
123
  /* LCOV_EXCL_START */
122
124
  struct stat st;
123
125
  static const char *devices[] = {
124
- # ifndef USE_BLOCKING_RANDOM
126
+ # ifndef USE_BLOCKING_RANDOM
125
127
  "/dev/urandom",
126
- # endif
128
+ # endif
127
129
  "/dev/random", NULL
128
130
  };
129
131
  const char ** device = devices;
@@ -132,10 +134,16 @@ randombytes_salsa20_random_random_dev_open(void)
132
134
  do {
133
135
  fd = open(*device, O_RDONLY);
134
136
  if (fd != -1) {
135
- if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) {
136
- # if defined(F_SETFD) && defined(FD_CLOEXEC)
137
- (void) fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
137
+ if (fstat(fd, &st) == 0 &&
138
+ # ifdef S_ISNAM
139
+ (S_ISNAM(st.st_mode) || S_ISCHR(st.st_mode))
140
+ # else
141
+ S_ISCHR(st.st_mode)
138
142
  # endif
143
+ ) {
144
+ # if defined(F_SETFD) && defined(FD_CLOEXEC)
145
+ (void) fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
146
+ # endif
139
147
  return fd;
140
148
  }
141
149
  (void) close(fd);
@@ -149,8 +157,9 @@ randombytes_salsa20_random_random_dev_open(void)
149
157
  return -1;
150
158
  /* LCOV_EXCL_STOP */
151
159
  }
160
+ # endif
152
161
 
153
- #ifdef SYS_getrandom
162
+ # ifdef SYS_getrandom
154
163
  static int
155
164
  _randombytes_linux_getrandom(void * const buf, const size_t size)
156
165
  {
@@ -184,7 +193,7 @@ randombytes_linux_getrandom(void * const buf_, size_t size)
184
193
 
185
194
  return 0;
186
195
  }
187
- #endif
196
+ # endif
188
197
 
189
198
  static void
190
199
  randombytes_salsa20_random_init(void)
@@ -194,7 +203,11 @@ randombytes_salsa20_random_init(void)
194
203
  stream.nonce = sodium_hrtime();
195
204
  assert(stream.nonce != (uint64_t) 0U);
196
205
 
197
- # ifdef SYS_getrandom
206
+ # ifdef HAVE_SAFE_ARC4RANDOM
207
+ errno = errno_save;
208
+ # else
209
+
210
+ # ifdef SYS_getrandom
198
211
  {
199
212
  unsigned char fodder[16];
200
213
 
@@ -205,13 +218,14 @@ randombytes_salsa20_random_init(void)
205
218
  }
206
219
  stream.getrandom_available = 0;
207
220
  }
208
- # endif
221
+ # endif /* SYS_getrandom */
209
222
 
210
223
  if ((stream.random_data_source_fd =
211
224
  randombytes_salsa20_random_random_dev_open()) == -1) {
212
225
  abort(); /* LCOV_EXCL_LINE */
213
226
  }
214
227
  errno = errno_save;
228
+ # endif /* HAVE_SAFE_ARC4RANDOM */
215
229
  }
216
230
 
217
231
  #else /* _WIN32 */
@@ -224,6 +238,17 @@ randombytes_salsa20_random_init(void)
224
238
  }
225
239
  #endif
226
240
 
241
+ static void
242
+ randombytes_salsa20_random_rekey(const unsigned char * const mix)
243
+ {
244
+ unsigned char *key = stream.key;
245
+ size_t i;
246
+
247
+ for (i = (size_t) 0U; i < sizeof stream.key; i++) {
248
+ key[i] ^= mix[i];
249
+ }
250
+ }
251
+
227
252
  void
228
253
  randombytes_salsa20_random_stir(void)
229
254
  {
@@ -235,7 +260,6 @@ randombytes_salsa20_random_stir(void)
235
260
  unsigned char m0[crypto_auth_hmacsha512256_BYTES +
236
261
  2U * SHA512_BLOCK_SIZE - SHA512_MIN_PAD_SIZE];
237
262
  unsigned char *k0 = m0 + crypto_auth_hmacsha512256_BYTES;
238
- size_t i;
239
263
  size_t sizeof_k0 = sizeof m0 - crypto_auth_hmacsha512256_BYTES;
240
264
 
241
265
  memset(stream.rnd32, 0, sizeof stream.rnd32);
@@ -245,7 +269,10 @@ randombytes_salsa20_random_stir(void)
245
269
  stream.initialized = 1;
246
270
  }
247
271
  #ifndef _WIN32
248
- # ifdef SYS_getrandom
272
+
273
+ # ifdef HAVE_SAFE_ARC4RANDOM
274
+ arc4random_buf(m0, sizeof m0);
275
+ # elif defined(SYS_getrandom)
249
276
  if (stream.getrandom_available != 0) {
250
277
  if (randombytes_linux_getrandom(m0, sizeof m0) != 0) {
251
278
  abort(); /* LCOV_EXCL_LINE */
@@ -262,6 +289,7 @@ randombytes_salsa20_random_stir(void)
262
289
  abort(); /* LCOV_EXCL_LINE */
263
290
  }
264
291
  # endif
292
+
265
293
  #else /* _WIN32 */
266
294
  if (! RtlGenRandom((PVOID) m0, (ULONG) sizeof m0)) {
267
295
  abort(); /* LCOV_EXCL_LINE */
@@ -270,40 +298,29 @@ randombytes_salsa20_random_stir(void)
270
298
  COMPILER_ASSERT(sizeof stream.key == crypto_auth_hmacsha512256_BYTES);
271
299
  crypto_auth_hmacsha512256(stream.key, k0, sizeof_k0, s);
272
300
  COMPILER_ASSERT(sizeof stream.key <= sizeof m0);
273
- for (i = (size_t) 0U; i < sizeof stream.key; i++) {
274
- stream.key[i] ^= m0[i];
275
- }
301
+ randombytes_salsa20_random_rekey(m0);
276
302
  sodium_memzero(m0, sizeof m0);
303
+ #ifdef HAVE_GETPID
304
+ stream.pid = getpid();
305
+ #endif
277
306
  }
278
307
 
279
308
  static void
280
309
  randombytes_salsa20_random_stir_if_needed(void)
281
310
  {
282
- #ifdef _MSC_VER
311
+ #ifdef HAVE_GETPID
283
312
  if (stream.initialized == 0) {
284
313
  randombytes_salsa20_random_stir();
314
+ } else if (stream.pid != getpid()) {
315
+ abort();
285
316
  }
286
317
  #else
287
- const pid_t pid = getpid();
288
-
289
- if (stream.initialized == 0 || stream.pid != pid) {
290
- stream.pid = pid;
318
+ if (stream.initialized == 0) {
291
319
  randombytes_salsa20_random_stir();
292
320
  }
293
321
  #endif
294
322
  }
295
323
 
296
- static void
297
- randombytes_salsa20_random_rekey(const unsigned char * const mix)
298
- {
299
- unsigned char *key = stream.key;
300
- size_t i;
301
-
302
- for (i = (size_t) 0U; i < sizeof stream.key; i++) {
303
- key[i] ^= mix[i];
304
- }
305
- }
306
-
307
324
  static uint32_t
308
325
  randombytes_salsa20_random_getword(void)
309
326
  {
@@ -342,13 +359,22 @@ randombytes_salsa20_random_close(void)
342
359
  close(stream.random_data_source_fd) == 0) {
343
360
  stream.random_data_source_fd = -1;
344
361
  stream.initialized = 0;
362
+ # ifdef HAVE_GETPID
363
+ stream.pid = (pid_t) 0;
364
+ # endif
345
365
  ret = 0;
346
366
  }
367
+
368
+ # ifdef HAVE_SAFE_ARC4RANDOM
369
+ ret = 0;
370
+ # endif
371
+
347
372
  # ifdef SYS_getrandom
348
373
  if (stream.getrandom_available != 0) {
349
374
  ret = 0;
350
375
  }
351
376
  # endif
377
+
352
378
  #else /* _WIN32 */
353
379
  if (stream.initialized != 0) {
354
380
  stream.initialized = 0;