rbnacl-libsodium 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) 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/ChangeLog +20 -0
  5. data/vendor/libsodium/LICENSE +1 -1
  6. data/vendor/libsodium/Makefile.in +8 -4
  7. data/vendor/libsodium/THANKS +3 -0
  8. data/vendor/libsodium/autom4te.cache/output.1 +96 -57
  9. data/vendor/libsodium/autom4te.cache/output.3 +96 -57
  10. data/vendor/libsodium/autom4te.cache/requests +544 -544
  11. data/vendor/libsodium/autom4te.cache/traces.1 +386 -367
  12. data/vendor/libsodium/autom4te.cache/traces.3 +222 -213
  13. data/vendor/libsodium/builds/msvc/version.h +2 -2
  14. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.props +1 -1
  15. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.props +1 -1
  16. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.props +1 -1
  17. data/vendor/libsodium/configure +95 -56
  18. data/vendor/libsodium/configure.ac +26 -11
  19. data/vendor/libsodium/dist-build/Makefile.am +1 -0
  20. data/vendor/libsodium/dist-build/Makefile.in +2 -0
  21. data/vendor/libsodium/dist-build/emscripten.sh +48 -10
  22. data/vendor/libsodium/dist-build/ios.sh +59 -7
  23. data/vendor/libsodium/libsodium-uninstalled.pc.in +6 -0
  24. data/vendor/libsodium/msvc-scripts/Makefile.in +1 -0
  25. data/vendor/libsodium/msvc-scripts/process.bat +2 -2
  26. data/vendor/libsodium/msvc-scripts/sodium.props +2 -2
  27. data/vendor/libsodium/src/Makefile.in +1 -0
  28. data/vendor/libsodium/src/libsodium/Makefile.in +1 -0
  29. data/vendor/libsodium/src/libsodium/crypto_box/crypto_box_easy.c +46 -4
  30. data/vendor/libsodium/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/box_curve25519xsalsa20poly1305.c +13 -2
  31. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/scrypt_platform.c +2 -2
  32. data/vendor/libsodium/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c +8 -2
  33. data/vendor/libsodium/src/libsodium/crypto_sign/crypto_sign.c +6 -6
  34. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/keypair.c +3 -1
  35. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/open.c +13 -6
  36. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/sign.c +8 -8
  37. data/vendor/libsodium/src/libsodium/crypto_sign/edwards25519sha512batch/ref/sign_edwards25519sha512batch.c +10 -6
  38. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c +2 -2
  39. data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/ref/stream_salsa20_ref.c +2 -2
  40. data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/ref/xor_salsa20_ref.c +2 -2
  41. data/vendor/libsodium/src/libsodium/crypto_stream/salsa2012/ref/stream_salsa2012.c +11 -5
  42. data/vendor/libsodium/src/libsodium/crypto_stream/salsa2012/ref/xor_salsa2012.c +11 -5
  43. data/vendor/libsodium/src/libsodium/crypto_stream/salsa208/ref/stream_salsa208.c +11 -5
  44. data/vendor/libsodium/src/libsodium/crypto_stream/salsa208/ref/xor_salsa208.c +11 -5
  45. data/vendor/libsodium/src/libsodium/crypto_stream/xsalsa20/ref/stream_xsalsa20.c +5 -1
  46. data/vendor/libsodium/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c +5 -1
  47. data/vendor/libsodium/src/libsodium/include/Makefile.in +1 -0
  48. data/vendor/libsodium/src/libsodium/include/sodium.h +44 -44
  49. data/vendor/libsodium/src/libsodium/include/sodium/crypto_box.h +61 -36
  50. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash_scryptsalsa208sha256.h +0 -10
  51. data/vendor/libsodium/src/libsodium/include/sodium/crypto_secretbox.h +20 -18
  52. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign.h +3 -3
  53. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign_ed25519.h +3 -3
  54. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign_edwards25519sha512batch.h +2 -2
  55. data/vendor/libsodium/src/libsodium/include/sodium/randombytes.h +1 -1
  56. data/vendor/libsodium/src/libsodium/include/sodium/utils.h +10 -3
  57. data/vendor/libsodium/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +9 -2
  58. data/vendor/libsodium/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c +9 -2
  59. data/vendor/libsodium/src/libsodium/sodium/utils.c +97 -72
  60. data/vendor/libsodium/test/Makefile.in +1 -0
  61. data/vendor/libsodium/test/default/Makefile.am +9 -2
  62. data/vendor/libsodium/test/default/Makefile.in +38 -78
  63. data/vendor/libsodium/test/default/box_easy2.c +55 -10
  64. data/vendor/libsodium/test/default/box_easy2.exp +3 -0
  65. data/vendor/libsodium/test/default/cmptest.h +4 -0
  66. data/vendor/libsodium/test/default/{pre.js → pre.js.inc} +10 -4
  67. data/vendor/libsodium/test/default/pwhash.c +6 -4
  68. data/vendor/libsodium/test/default/scalarmult6.c +0 -2
  69. data/vendor/libsodium/test/default/sign.c +1 -1
  70. data/vendor/libsodium/test/default/sodium_utils2.c +5 -2
  71. data/vendor/libsodium/test/default/sodium_utils3.c +3 -1
  72. metadata +5 -4
@@ -32,22 +32,10 @@ size_t crypto_box_publickeybytes(void);
32
32
  SODIUM_EXPORT
33
33
  size_t crypto_box_secretkeybytes(void);
34
34
 
35
- #define crypto_box_BEFORENMBYTES crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES
36
- SODIUM_EXPORT
37
- size_t crypto_box_beforenmbytes(void);
38
-
39
35
  #define crypto_box_NONCEBYTES crypto_box_curve25519xsalsa20poly1305_NONCEBYTES
40
36
  SODIUM_EXPORT
41
37
  size_t crypto_box_noncebytes(void);
42
38
 
43
- #define crypto_box_ZEROBYTES crypto_box_curve25519xsalsa20poly1305_ZEROBYTES
44
- SODIUM_EXPORT
45
- size_t crypto_box_zerobytes(void);
46
-
47
- #define crypto_box_BOXZEROBYTES crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES
48
- SODIUM_EXPORT
49
- size_t crypto_box_boxzerobytes(void);
50
-
51
39
  #define crypto_box_MACBYTES crypto_box_curve25519xsalsa20poly1305_MACBYTES
52
40
  SODIUM_EXPORT
53
41
  size_t crypto_box_macbytes(void);
@@ -63,30 +51,6 @@ int crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk,
63
51
  SODIUM_EXPORT
64
52
  int crypto_box_keypair(unsigned char *pk, unsigned char *sk);
65
53
 
66
- SODIUM_EXPORT
67
- int crypto_box_beforenm(unsigned char *k, const unsigned char *pk,
68
- const unsigned char *sk);
69
-
70
- SODIUM_EXPORT
71
- int crypto_box_afternm(unsigned char *c, const unsigned char *m,
72
- unsigned long long mlen, const unsigned char *n,
73
- const unsigned char *k);
74
-
75
- SODIUM_EXPORT
76
- int crypto_box_open_afternm(unsigned char *m, const unsigned char *c,
77
- unsigned long long clen, const unsigned char *n,
78
- const unsigned char *k);
79
-
80
- SODIUM_EXPORT
81
- int crypto_box(unsigned char *c, const unsigned char *m,
82
- unsigned long long mlen, const unsigned char *n,
83
- const unsigned char *pk, const unsigned char *sk);
84
-
85
- SODIUM_EXPORT
86
- int crypto_box_open(unsigned char *m, const unsigned char *c,
87
- unsigned long long clen, const unsigned char *n,
88
- const unsigned char *pk, const unsigned char *sk);
89
-
90
54
  SODIUM_EXPORT
91
55
  int crypto_box_easy(unsigned char *c, const unsigned char *m,
92
56
  unsigned long long mlen, const unsigned char *n,
@@ -110,6 +74,67 @@ int crypto_box_open_detached(unsigned char *m, const unsigned char *c,
110
74
  const unsigned char *n,
111
75
  const unsigned char *pk,
112
76
  const unsigned char *sk);
77
+
78
+ /* -- Precomputation interface -- */
79
+
80
+ #define crypto_box_BEFORENMBYTES crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES
81
+ SODIUM_EXPORT
82
+ size_t crypto_box_beforenmbytes(void);
83
+
84
+ SODIUM_EXPORT
85
+ int crypto_box_beforenm(unsigned char *k, const unsigned char *pk,
86
+ const unsigned char *sk);
87
+
88
+ SODIUM_EXPORT
89
+ int crypto_box_easy_afternm(unsigned char *c, const unsigned char *m,
90
+ unsigned long long mlen, const unsigned char *n,
91
+ const unsigned char *k);
92
+
93
+ SODIUM_EXPORT
94
+ int crypto_box_open_easy_afternm(unsigned char *m, const unsigned char *c,
95
+ unsigned long long clen, const unsigned char *n,
96
+ const unsigned char *k);
97
+
98
+ SODIUM_EXPORT
99
+ int crypto_box_detached_afternm(unsigned char *c, unsigned char *mac,
100
+ const unsigned char *m, unsigned long long mlen,
101
+ const unsigned char *n, const unsigned char *k);
102
+
103
+ SODIUM_EXPORT
104
+ int crypto_box_open_detached_afternm(unsigned char *m, const unsigned char *c,
105
+ const unsigned char *mac,
106
+ unsigned long long clen, const unsigned char *n,
107
+ const unsigned char *k);
108
+
109
+ /* -- NaCl compatibility interface ; Requires padding -- */
110
+
111
+ #define crypto_box_ZEROBYTES crypto_box_curve25519xsalsa20poly1305_ZEROBYTES
112
+ SODIUM_EXPORT
113
+ size_t crypto_box_zerobytes(void);
114
+
115
+ #define crypto_box_BOXZEROBYTES crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES
116
+ SODIUM_EXPORT
117
+ size_t crypto_box_boxzerobytes(void);
118
+
119
+ SODIUM_EXPORT
120
+ int crypto_box(unsigned char *c, const unsigned char *m,
121
+ unsigned long long mlen, const unsigned char *n,
122
+ const unsigned char *pk, const unsigned char *sk);
123
+
124
+ SODIUM_EXPORT
125
+ int crypto_box_open(unsigned char *m, const unsigned char *c,
126
+ unsigned long long clen, const unsigned char *n,
127
+ const unsigned char *pk, const unsigned char *sk);
128
+
129
+ SODIUM_EXPORT
130
+ int crypto_box_afternm(unsigned char *c, const unsigned char *m,
131
+ unsigned long long mlen, const unsigned char *n,
132
+ const unsigned char *k);
133
+
134
+ SODIUM_EXPORT
135
+ int crypto_box_open_afternm(unsigned char *m, const unsigned char *c,
136
+ unsigned long long clen, const unsigned char *n,
137
+ const unsigned char *k);
113
138
  #ifdef __cplusplus
114
139
  }
115
140
  #endif
@@ -72,14 +72,4 @@ int crypto_pwhash_scryptsalsa208sha256_ll(const uint8_t * passwd, size_t passwdl
72
72
  }
73
73
  #endif
74
74
 
75
- /* Backward compatibility with version 0.5.0 */
76
-
77
- #define crypto_pwhash_scryptxsalsa208sha256_SALTBYTES crypto_pwhash_scryptsalsa208sha256_SALTBYTES
78
- #define crypto_pwhash_scryptxsalsa208sha256_saltbytes crypto_pwhash_scryptsalsa208sha256_saltbytes
79
- #define crypto_pwhash_scryptxsalsa208sha256_STRBYTES crypto_pwhash_scryptsalsa208sha256_STRBYTES
80
- #define crypto_pwhash_scryptxsalsa208sha256_strbytes crypto_pwhash_scryptsalsa208sha256_strbytes
81
- #define crypto_pwhash_scryptxsalsa208sha256 crypto_pwhash_scryptsalsa208sha256
82
- #define crypto_pwhash_scryptxsalsa208sha256_str crypto_pwhash_scryptsalsa208sha256_str
83
- #define crypto_pwhash_scryptxsalsa208sha256_str_verify crypto_pwhash_scryptsalsa208sha256_str_verify
84
-
85
75
  #endif
@@ -21,14 +21,6 @@ size_t crypto_secretbox_keybytes(void);
21
21
  SODIUM_EXPORT
22
22
  size_t crypto_secretbox_noncebytes(void);
23
23
 
24
- #define crypto_secretbox_ZEROBYTES crypto_secretbox_xsalsa20poly1305_ZEROBYTES
25
- SODIUM_EXPORT
26
- size_t crypto_secretbox_zerobytes(void);
27
-
28
- #define crypto_secretbox_BOXZEROBYTES crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES
29
- SODIUM_EXPORT
30
- size_t crypto_secretbox_boxzerobytes(void);
31
-
32
24
  #define crypto_secretbox_MACBYTES crypto_secretbox_xsalsa20poly1305_MACBYTES
33
25
  SODIUM_EXPORT
34
26
  size_t crypto_secretbox_macbytes(void);
@@ -37,16 +29,6 @@ size_t crypto_secretbox_macbytes(void);
37
29
  SODIUM_EXPORT
38
30
  const char *crypto_secretbox_primitive(void);
39
31
 
40
- SODIUM_EXPORT
41
- int crypto_secretbox(unsigned char *c, const unsigned char *m,
42
- unsigned long long mlen, const unsigned char *n,
43
- const unsigned char *k);
44
-
45
- SODIUM_EXPORT
46
- int crypto_secretbox_open(unsigned char *m, const unsigned char *c,
47
- unsigned long long clen, const unsigned char *n,
48
- const unsigned char *k);
49
-
50
32
  SODIUM_EXPORT
51
33
  int crypto_secretbox_easy(unsigned char *c, const unsigned char *m,
52
34
  unsigned long long mlen, const unsigned char *n,
@@ -71,6 +53,26 @@ int crypto_secretbox_open_detached(unsigned char *m,
71
53
  unsigned long long clen,
72
54
  const unsigned char *n,
73
55
  const unsigned char *k);
56
+
57
+ /* -- NaCl compatibility interface ; Requires padding -- */
58
+
59
+ #define crypto_secretbox_ZEROBYTES crypto_secretbox_xsalsa20poly1305_ZEROBYTES
60
+ SODIUM_EXPORT
61
+ size_t crypto_secretbox_zerobytes(void);
62
+
63
+ #define crypto_secretbox_BOXZEROBYTES crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES
64
+ SODIUM_EXPORT
65
+ size_t crypto_secretbox_boxzerobytes(void);
66
+
67
+ SODIUM_EXPORT
68
+ int crypto_secretbox(unsigned char *c, const unsigned char *m,
69
+ unsigned long long mlen, const unsigned char *n,
70
+ const unsigned char *k);
71
+
72
+ SODIUM_EXPORT
73
+ int crypto_secretbox_open(unsigned char *m, const unsigned char *c,
74
+ unsigned long long clen, const unsigned char *n,
75
+ const unsigned char *k);
74
76
  #ifdef __cplusplus
75
77
  }
76
78
  #endif
@@ -48,17 +48,17 @@ SODIUM_EXPORT
48
48
  int crypto_sign_keypair(unsigned char *pk, unsigned char *sk);
49
49
 
50
50
  SODIUM_EXPORT
51
- int crypto_sign(unsigned char *sm, unsigned long long *smlen,
51
+ int crypto_sign(unsigned char *sm, unsigned long long *smlen_p,
52
52
  const unsigned char *m, unsigned long long mlen,
53
53
  const unsigned char *sk);
54
54
 
55
55
  SODIUM_EXPORT
56
- int crypto_sign_open(unsigned char *m, unsigned long long *mlen,
56
+ int crypto_sign_open(unsigned char *m, unsigned long long *mlen_p,
57
57
  const unsigned char *sm, unsigned long long smlen,
58
58
  const unsigned char *pk);
59
59
 
60
60
  SODIUM_EXPORT
61
- int crypto_sign_detached(unsigned char *sig, unsigned long long *siglen,
61
+ int crypto_sign_detached(unsigned char *sig, unsigned long long *siglen_p,
62
62
  const unsigned char *m, unsigned long long mlen,
63
63
  const unsigned char *sk);
64
64
 
@@ -28,18 +28,18 @@ SODIUM_EXPORT
28
28
  size_t crypto_sign_ed25519_secretkeybytes(void);
29
29
 
30
30
  SODIUM_EXPORT
31
- int crypto_sign_ed25519(unsigned char *sm, unsigned long long *smlen,
31
+ int crypto_sign_ed25519(unsigned char *sm, unsigned long long *smlen_p,
32
32
  const unsigned char *m, unsigned long long mlen,
33
33
  const unsigned char *sk);
34
34
 
35
35
  SODIUM_EXPORT
36
- int crypto_sign_ed25519_open(unsigned char *m, unsigned long long *mlen,
36
+ int crypto_sign_ed25519_open(unsigned char *m, unsigned long long *mlen_p,
37
37
  const unsigned char *sm, unsigned long long smlen,
38
38
  const unsigned char *pk);
39
39
 
40
40
  SODIUM_EXPORT
41
41
  int crypto_sign_ed25519_detached(unsigned char *sig,
42
- unsigned long long *siglen,
42
+ unsigned long long *siglen_p,
43
43
  const unsigned char *m,
44
44
  unsigned long long mlen,
45
45
  const unsigned char *sk);
@@ -36,14 +36,14 @@ size_t crypto_sign_edwards25519sha512batch_secretkeybytes(void);
36
36
 
37
37
  SODIUM_EXPORT
38
38
  int crypto_sign_edwards25519sha512batch(unsigned char *sm,
39
- unsigned long long *smlen,
39
+ unsigned long long *smlen_p,
40
40
  const unsigned char *m,
41
41
  unsigned long long mlen,
42
42
  const unsigned char *sk);
43
43
 
44
44
  SODIUM_EXPORT
45
45
  int crypto_sign_edwards25519sha512batch_open(unsigned char *m,
46
- unsigned long long *mlen,
46
+ unsigned long long *mlen_p,
47
47
  const unsigned char *sm,
48
48
  unsigned long long smlen,
49
49
  const unsigned char *pk);
@@ -46,7 +46,7 @@ int randombytes_set_implementation(randombytes_implementation *impl);
46
46
  SODIUM_EXPORT
47
47
  const char *randombytes_implementation_name(void);
48
48
 
49
- /* -- Compatibility layer with NaCl -- */
49
+ /* -- NaCl compatibility interface -- */
50
50
 
51
51
  SODIUM_EXPORT
52
52
  void randombytes(unsigned char * const buf, const unsigned long long buf_len);
@@ -65,9 +65,16 @@ int sodium_munlock(void * const addr, const size_t len);
65
65
  * - The returned address will not be aligned if the allocation size is not
66
66
  * a multiple of the required alignment. For this reason, these functions
67
67
  * are designed to store data, such as secret keys and messages.
68
- * They should not be used to store pointers mixed with other types
69
- * in portable code unless extreme care is taken to ensure correct
70
- * pointers alignment.
68
+ *
69
+ * sodium_malloc() can be used to allocate any libsodium data structure,
70
+ * with the exception of crypto_generichash_state.
71
+ *
72
+ * The crypto_generichash_state structure is packed and its length is
73
+ * either 357 or 361 bytes. For this reason, when using sodium_malloc() to
74
+ * allocate a crypto_generichash_state structure, padding must be added in
75
+ * order to ensure proper alignment:
76
+ * state = sodium_malloc((sizeof(crypto_generichash_state)
77
+ * + (size_t) 63U) & ~(size_t) 63U);
71
78
  */
72
79
 
73
80
  SODIUM_EXPORT
@@ -94,7 +94,7 @@ safe_read(const int fd, void * const buf_, size_t count)
94
94
  assert(count > (size_t) 0U);
95
95
  do {
96
96
  while ((readnb = read(fd, buf, count)) < (ssize_t) 0 &&
97
- errno == EINTR); /* LCOV_EXCL_LINE */
97
+ (errno == EINTR || errno == EAGAIN)); /* LCOV_EXCL_LINE */
98
98
  if (readnb < (ssize_t) 0) {
99
99
  return readnb; /* LCOV_EXCL_LINE */
100
100
  }
@@ -125,15 +125,22 @@ randombytes_salsa20_random_random_dev_open(void)
125
125
  int fd;
126
126
 
127
127
  do {
128
- if ((fd = open(*device, O_RDONLY)) != -1) {
128
+ fd = open(*device, O_RDONLY);
129
+ if (fd != -1) {
129
130
  if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) {
131
+ # if defined(F_SETFD) && defined(FD_CLOEXEC)
132
+ (void) fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
133
+ # endif
130
134
  return fd;
131
135
  }
132
136
  (void) close(fd);
137
+ } else if (errno == EINTR) {
138
+ continue;
133
139
  }
134
140
  device++;
135
141
  } while (*device != NULL);
136
142
 
143
+ errno = EIO;
137
144
  return -1;
138
145
  /* LCOV_EXCL_STOP */
139
146
  }
@@ -83,7 +83,7 @@ safe_read(const int fd, void * const buf_, size_t count)
83
83
  assert(count > (size_t) 0U);
84
84
  do {
85
85
  while ((readnb = read(fd, buf, count)) < (ssize_t) 0 &&
86
- errno == EINTR); /* LCOV_EXCL_LINE */
86
+ (errno == EINTR || errno == EAGAIN)); /* LCOV_EXCL_LINE */
87
87
  if (readnb < (ssize_t) 0) {
88
88
  return readnb; /* LCOV_EXCL_LINE */
89
89
  }
@@ -114,15 +114,22 @@ randombytes_sysrandom_random_dev_open(void)
114
114
  int fd;
115
115
 
116
116
  do {
117
- if ((fd = open(*device, O_RDONLY)) != -1) {
117
+ fd = open(*device, O_RDONLY);
118
+ if (fd != -1) {
118
119
  if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) {
120
+ # if defined(F_SETFD) && defined(FD_CLOEXEC)
121
+ (void) fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
122
+ # endif
119
123
  return fd;
120
124
  }
121
125
  (void) close(fd);
126
+ } else if (errno == EINTR) {
127
+ continue;
122
128
  }
123
129
  device++;
124
130
  } while (*device != NULL);
125
131
 
132
+ errno = EIO;
126
133
  return -1;
127
134
  /* LCOV_EXCL_STOP */
128
135
  }
@@ -32,7 +32,7 @@
32
32
  #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
33
33
  # define MAP_ANON MAP_ANONYMOUS
34
34
  #endif
35
- #if defined(_WIN32) || defined(MAP_ANON) || defined(HAVE_POSIX_MEMALIGN)
35
+ #if defined(_WIN32) || (defined(MAP_ANON) && defined(HAVE_MMAP)) || defined(HAVE_POSIX_MEMALIGN)
36
36
  # define HAVE_ALIGNED_MALLOC
37
37
  #endif
38
38
  #if defined(HAVE_MPROTECT) && !(defined(PROT_NONE) && defined(PROT_READ) && defined(PROT_WRITE))
@@ -172,10 +172,33 @@ sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
172
172
  return ret;
173
173
  }
174
174
 
175
+ int
176
+ _sodium_alloc_init(void)
177
+ {
178
+ #ifdef HAVE_ALIGNED_MALLOC
179
+ # if defined(_SC_PAGESIZE)
180
+ long page_size_ = sysconf(_SC_PAGESIZE);
181
+ if (page_size_ > 0L) {
182
+ page_size = (size_t) page_size_;
183
+ }
184
+ # elif defined(_WIN32)
185
+ SYSTEM_INFO si;
186
+ GetSystemInfo(&si);
187
+ page_size = (size_t) si.dwPageSize;
188
+ # endif
189
+ if (page_size < CANARY_SIZE || page_size < sizeof(size_t)) {
190
+ abort(); /* LCOV_EXCL_LINE */
191
+ }
192
+ #endif
193
+ randombytes_buf(canary, sizeof canary);
194
+
195
+ return 0;
196
+ }
197
+
175
198
  int
176
199
  sodium_mlock(void * const addr, const size_t len)
177
200
  {
178
- #ifdef MADV_DONTDUMP
201
+ #if defined(MADV_DONTDUMP) && defined(HAVE_MADVISE)
179
202
  (void) madvise(addr, len, MADV_DONTDUMP);
180
203
  #endif
181
204
  #ifdef HAVE_MLOCK
@@ -192,7 +215,7 @@ int
192
215
  sodium_munlock(void * const addr, const size_t len)
193
216
  {
194
217
  sodium_memzero(addr, len);
195
- #ifdef MADV_DODUMP
218
+ #if defined(MADV_DODUMP) && defined(HAVE_MADVISE)
196
219
  (void) madvise(addr, len, MADV_DODUMP);
197
220
  #endif
198
221
  #ifdef HAVE_MLOCK
@@ -205,45 +228,14 @@ sodium_munlock(void * const addr, const size_t len)
205
228
  #endif
206
229
  }
207
230
 
208
- int
209
- _sodium_alloc_init(void)
210
- {
211
- #if defined(_SC_PAGESIZE)
212
- long page_size_ = sysconf(_SC_PAGESIZE);
213
- if (page_size_ > 0L) {
214
- page_size = (size_t) page_size_;
215
- }
216
- #elif defined(_WIN32)
217
- SYSTEM_INFO si;
218
- GetSystemInfo(&si);
219
- page_size = (size_t) si.dwPageSize;
220
- #endif
221
- if (page_size < CANARY_SIZE) {
222
- abort(); /* LCOV_EXCL_LINE */
223
- }
224
- randombytes_buf(canary, sizeof canary);
225
-
226
- return 0;
227
- }
228
-
229
- static inline size_t
230
- _page_round(const size_t size)
231
- {
232
- const size_t page_mask = page_size - 1U;
233
-
234
- return (size + page_mask) & ~page_mask;
235
- }
236
-
237
231
  static int
238
232
  _mprotect_noaccess(void *ptr, size_t size)
239
233
  {
240
- #if defined(HAVE_MPROTECT) && defined(HAVE_PAGE_PROTECTION)
234
+ #ifdef HAVE_MPROTECT
241
235
  return mprotect(ptr, size, PROT_NONE);
242
236
  #elif defined(_WIN32)
243
- {
244
- DWORD old;
245
- return -(VirtualProtect(ptr, size, PAGE_NOACCESS, &old) == 0);
246
- }
237
+ DWORD old;
238
+ return -(VirtualProtect(ptr, size, PAGE_NOACCESS, &old) == 0);
247
239
  #else
248
240
  errno = ENOSYS;
249
241
  return -1;
@@ -253,13 +245,11 @@ _mprotect_noaccess(void *ptr, size_t size)
253
245
  static int
254
246
  _mprotect_readonly(void *ptr, size_t size)
255
247
  {
256
- #if defined(HAVE_MPROTECT) && defined(HAVE_PAGE_PROTECTION)
248
+ #ifdef HAVE_MPROTECT
257
249
  return mprotect(ptr, size, PROT_READ);
258
250
  #elif defined(_WIN32)
259
- {
260
- DWORD old;
261
- return -(VirtualProtect(ptr, size, PAGE_READONLY, &old) == 0);
262
- }
251
+ DWORD old;
252
+ return -(VirtualProtect(ptr, size, PAGE_READONLY, &old) == 0);
263
253
  #else
264
254
  errno = ENOSYS;
265
255
  return -1;
@@ -269,66 +259,72 @@ _mprotect_readonly(void *ptr, size_t size)
269
259
  static int
270
260
  _mprotect_readwrite(void *ptr, size_t size)
271
261
  {
272
- #if defined(HAVE_MPROTECT) && defined(HAVE_PAGE_PROTECTION)
262
+ #ifdef HAVE_MPROTECT
273
263
  return mprotect(ptr, size, PROT_READ | PROT_WRITE);
274
264
  #elif defined(_WIN32)
275
- {
276
- DWORD old;
277
- return -(VirtualProtect(ptr, size, PAGE_READWRITE, &old) == 0);
278
- }
265
+ DWORD old;
266
+ return -(VirtualProtect(ptr, size, PAGE_READWRITE, &old) == 0);
279
267
  #else
280
268
  errno = ENOSYS;
281
269
  return -1;
282
270
  #endif
283
271
  }
284
272
 
273
+ #ifdef HAVE_ALIGNED_MALLOC
274
+
285
275
  static void
286
276
  _out_of_bounds(void)
287
277
  {
288
- #ifdef SIGSEGV
278
+ # ifdef SIGSEGV
289
279
  raise(SIGSEGV);
290
- #elif defined(SIGKILL)
280
+ # elif defined(SIGKILL)
291
281
  raise(SIGKILL);
292
- #endif
282
+ # endif
293
283
  abort();
294
- } /* LCOV_EXCL_LINE */
284
+ } /* LCOV_EXCL_LINE */
285
+
286
+ static inline size_t
287
+ _page_round(const size_t size)
288
+ {
289
+ const size_t page_mask = page_size - 1U;
290
+
291
+ return (size + page_mask) & ~page_mask;
292
+ }
295
293
 
296
294
  static __attribute__((malloc)) unsigned char *
297
295
  _alloc_aligned(const size_t size)
298
296
  {
299
297
  void *ptr;
300
298
 
301
- #ifdef MAP_ANON
299
+ # if defined(MAP_ANON) && defined(HAVE_MMAP)
302
300
  if ((ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
303
301
  MAP_ANON | MAP_PRIVATE | MAP_NOCORE, -1, 0)) == MAP_FAILED) {
304
302
  ptr = NULL; /* LCOV_EXCL_LINE */
305
303
  } /* LCOV_EXCL_LINE */
306
- #elif defined(HAVE_POSIX_MEMALIGN)
304
+ # elif defined(HAVE_POSIX_MEMALIGN)
307
305
  if (posix_memalign(&ptr, page_size, size) != 0) {
308
306
  ptr = NULL; /* LCOV_EXCL_LINE */
309
307
  } /* LCOV_EXCL_LINE */
310
- #elif defined(_WIN32)
308
+ # elif defined(_WIN32)
311
309
  ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
312
- #elif !defined(HAVE_ALIGNED_MALLOC)
313
- ptr = malloc(size);
314
- #else
315
- # error Bug
316
- #endif
310
+ # else
311
+ # error Bug
312
+ # endif
317
313
  return (unsigned char *) ptr;
318
314
  }
319
315
 
320
316
  static void
321
317
  _free_aligned(unsigned char * const ptr, const size_t size)
322
318
  {
323
- #ifdef MAP_ANON
319
+ # if defined(MAP_ANON) && defined(HAVE_MMAP)
324
320
  (void) munmap(ptr, size);
325
- #elif defined(HAVE_POSIX_MEMALIGN)
321
+ # elif defined(HAVE_POSIX_MEMALIGN)
326
322
  free(ptr);
327
- #elif defined(_WIN32)
323
+ # elif defined(_WIN32)
328
324
  VirtualFree(ptr, 0U, MEM_RELEASE);
329
- #else
330
- free(ptr);
331
- #endif
325
+ # else
326
+ # error Bug
327
+ # endif
332
328
  }
333
329
 
334
330
  static unsigned char *
@@ -347,6 +343,15 @@ _unprotected_ptr_from_user_ptr(const void *ptr)
347
343
  return (unsigned char *) unprotected_ptr_u;
348
344
  }
349
345
 
346
+ #endif /* HAVE_ALIGNED_MALLOC */
347
+
348
+ #ifndef HAVE_ALIGNED_MALLOC
349
+ static __attribute__((malloc)) void *
350
+ _sodium_malloc(const size_t size)
351
+ {
352
+ return malloc(size);
353
+ }
354
+ #else
350
355
  static __attribute__((malloc)) void *
351
356
  _sodium_malloc(const size_t size)
352
357
  {
@@ -373,9 +378,9 @@ _sodium_malloc(const size_t size)
373
378
  }
374
379
  unprotected_ptr = base_ptr + page_size * 2U;
375
380
  _mprotect_noaccess(base_ptr + page_size, page_size);
376
- #ifndef HAVE_PAGE_PROTECTION
381
+ # ifndef HAVE_PAGE_PROTECTION
377
382
  memcpy(unprotected_ptr + unprotected_size, canary, sizeof canary);
378
- #endif
383
+ # endif
379
384
  _mprotect_noaccess(unprotected_ptr + unprotected_size, page_size);
380
385
  sodium_mlock(unprotected_ptr, unprotected_size);
381
386
  canary_ptr = unprotected_ptr + _page_round(size_with_canary) -
@@ -388,6 +393,7 @@ _sodium_malloc(const size_t size)
388
393
 
389
394
  return user_ptr;
390
395
  }
396
+ #endif /* !HAVE_ALIGNED_MALLOC */
391
397
 
392
398
  __attribute__((malloc)) void *
393
399
  sodium_malloc(const size_t size)
@@ -416,6 +422,13 @@ sodium_allocarray(size_t count, size_t size)
416
422
  return sodium_malloc(total_size);
417
423
  }
418
424
 
425
+ #ifndef HAVE_ALIGNED_MALLOC
426
+ void
427
+ sodium_free(void *ptr)
428
+ {
429
+ free(ptr);
430
+ }
431
+ #else
419
432
  void
420
433
  sodium_free(void *ptr)
421
434
  {
@@ -429,24 +442,35 @@ sodium_free(void *ptr)
429
442
  return;
430
443
  }
431
444
  canary_ptr = ((unsigned char *) ptr) - sizeof canary;
432
- if (sodium_memcmp(canary_ptr, canary, sizeof canary) != 0) {
433
- _out_of_bounds();
434
- }
435
445
  unprotected_ptr = _unprotected_ptr_from_user_ptr(ptr);
436
446
  base_ptr = unprotected_ptr - page_size * 2U;
437
447
  memcpy(&unprotected_size, base_ptr, sizeof unprotected_size);
438
448
  total_size = page_size + page_size + unprotected_size + page_size;
439
449
  _mprotect_readwrite(base_ptr, total_size);
440
- #ifndef HAVE_PAGE_PROTECTION
450
+ if (sodium_memcmp(canary_ptr, canary, sizeof canary) != 0) {
451
+ _out_of_bounds();
452
+ }
453
+ # ifndef HAVE_PAGE_PROTECTION
441
454
  if (sodium_memcmp(unprotected_ptr + unprotected_size,
442
455
  canary, sizeof canary) != 0) {
443
456
  _out_of_bounds();
444
457
  }
445
- #endif
458
+ # endif
446
459
  sodium_munlock(unprotected_ptr, unprotected_size);
447
460
  _free_aligned(base_ptr, total_size);
448
461
  }
462
+ #endif /* HAVE_ALIGNED_MALLOC */
449
463
 
464
+ #ifndef HAVE_PAGE_PROTECTION
465
+ static int
466
+ _sodium_mprotect(void *ptr, int (*cb)(void *ptr, size_t size))
467
+ {
468
+ (void) ptr;
469
+ (void) cb;
470
+ errno = ENOSYS;
471
+ return -1;
472
+ }
473
+ #else
450
474
  static int
451
475
  _sodium_mprotect(void *ptr, int (*cb)(void *ptr, size_t size))
452
476
  {
@@ -460,6 +484,7 @@ _sodium_mprotect(void *ptr, int (*cb)(void *ptr, size_t size))
460
484
 
461
485
  return cb(unprotected_ptr, unprotected_size);
462
486
  }
487
+ #endif
463
488
 
464
489
  int
465
490
  sodium_mprotect_noaccess(void *ptr)