rbnacl-libsodium 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) 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 +0 -3
  5. data/vendor/libsodium/ChangeLog +24 -0
  6. data/vendor/libsodium/THANKS +2 -0
  7. data/vendor/libsodium/autom4te.cache/output.1 +35 -147
  8. data/vendor/libsodium/autom4te.cache/output.3 +35 -147
  9. data/vendor/libsodium/autom4te.cache/requests +127 -127
  10. data/vendor/libsodium/autom4te.cache/traces.1 +244 -254
  11. data/vendor/libsodium/autom4te.cache/traces.3 +111 -113
  12. data/vendor/libsodium/builds/msvc/properties/Common.props +3 -1
  13. data/vendor/libsodium/builds/msvc/properties/Release.props +1 -3
  14. data/vendor/libsodium/builds/msvc/version.h +3 -3
  15. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj +1 -11
  16. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters +1 -37
  17. data/vendor/libsodium/builds/msvc/vs2010/test/test.vcxproj +10 -1
  18. data/vendor/libsodium/builds/msvc/vs2010/test/test.vcxproj.filters +10 -1
  19. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj +1 -11
  20. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters +1 -37
  21. data/vendor/libsodium/builds/msvc/vs2012/test/test.vcxproj +10 -1
  22. data/vendor/libsodium/builds/msvc/vs2012/test/test.vcxproj.filters +10 -1
  23. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj +1 -11
  24. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters +1 -37
  25. data/vendor/libsodium/builds/msvc/vs2013/test/test.vcxproj +10 -1
  26. data/vendor/libsodium/builds/msvc/vs2013/test/test.vcxproj.filters +10 -1
  27. data/vendor/libsodium/configure +33 -145
  28. data/vendor/libsodium/configure.ac +22 -23
  29. data/vendor/libsodium/dist-build/android-build.sh +1 -0
  30. data/vendor/libsodium/dist-build/emscripten.sh +2 -2
  31. data/vendor/libsodium/dist-build/ios.sh +1 -0
  32. data/vendor/libsodium/libsodium.pc.in +1 -1
  33. data/vendor/libsodium/libsodium.vcxproj +1 -5
  34. data/vendor/libsodium/libsodium.vcxproj.filters +1 -13
  35. data/vendor/libsodium/msvc-scripts/process.bat +3 -3
  36. data/vendor/libsodium/msvc-scripts/sodium.props +4 -1
  37. data/vendor/libsodium/src/libsodium/Makefile.am +1 -10
  38. data/vendor/libsodium/src/libsodium/Makefile.in +5 -79
  39. data/vendor/libsodium/src/libsodium/crypto_auth/hmacsha256/cp/verify_hmacsha256.c +3 -1
  40. data/vendor/libsodium/src/libsodium/crypto_auth/hmacsha512/cp/verify_hmacsha512.c +3 -1
  41. data/vendor/libsodium/src/libsodium/crypto_auth/hmacsha512256/cp/verify_hmacsha512256.c +3 -1
  42. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c +6 -0
  43. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/pow225521.h +2 -2
  44. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/keypair.c +46 -3
  45. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/open.c +2 -10
  46. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/sign.c +7 -1
  47. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/sign_ed25519_api.c +18 -0
  48. data/vendor/libsodium/src/libsodium/include/Makefile.am +0 -1
  49. data/vendor/libsodium/src/libsodium/include/Makefile.in +0 -1
  50. data/vendor/libsodium/src/libsodium/include/sodium.h +0 -1
  51. data/vendor/libsodium/src/libsodium/include/sodium/crypto_onetimeauth_poly1305.h +1 -2
  52. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash_scryptsalsa208sha256.h +5 -0
  53. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign_ed25519.h +15 -0
  54. data/vendor/libsodium/src/libsodium/include/sodium/utils.h +54 -3
  55. data/vendor/libsodium/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +22 -7
  56. data/vendor/libsodium/src/libsodium/sodium/core.c +2 -0
  57. data/vendor/libsodium/src/libsodium/sodium/utils.c +305 -31
  58. data/vendor/libsodium/test/default/Makefile.am +18 -16
  59. data/vendor/libsodium/test/default/Makefile.in +141 -76
  60. data/vendor/libsodium/test/default/ed25519_convert.c +48 -0
  61. data/vendor/libsodium/test/default/ed25519_convert.exp +3 -0
  62. data/vendor/libsodium/test/default/sign.c +15 -4
  63. data/vendor/libsodium/test/default/sodium_utils2.c +70 -0
  64. data/vendor/libsodium/test/default/sodium_utils2.exp +3 -0
  65. data/vendor/libsodium/test/default/sodium_utils3.c +55 -0
  66. data/vendor/libsodium/test/default/sodium_utils3.exp +2 -0
  67. metadata +8 -24
  68. data/vendor/libsodium/src/libsodium/crypto_auth/try.c +0 -119
  69. data/vendor/libsodium/src/libsodium/crypto_box/try.c +0 -195
  70. data/vendor/libsodium/src/libsodium/crypto_hash/try.c +0 -76
  71. data/vendor/libsodium/src/libsodium/crypto_scalarmult/try.c +0 -125
  72. data/vendor/libsodium/src/libsodium/crypto_secretbox/try.c +0 -129
  73. data/vendor/libsodium/src/libsodium/crypto_sign/try.c +0 -87
  74. data/vendor/libsodium/src/libsodium/crypto_stream/aes256estream/hongjun/aes-table-be.h +0 -273
  75. data/vendor/libsodium/src/libsodium/crypto_stream/aes256estream/hongjun/aes-table-le.h +0 -274
  76. data/vendor/libsodium/src/libsodium/crypto_stream/aes256estream/hongjun/aes-table.h +0 -62
  77. data/vendor/libsodium/src/libsodium/crypto_stream/aes256estream/hongjun/aes256-ctr.c +0 -239
  78. data/vendor/libsodium/src/libsodium/crypto_stream/aes256estream/hongjun/aes256.h +0 -171
  79. data/vendor/libsodium/src/libsodium/crypto_stream/aes256estream/hongjun/api.h +0 -13
  80. data/vendor/libsodium/src/libsodium/crypto_stream/aes256estream/hongjun/ecrypt-sync.h +0 -27
  81. data/vendor/libsodium/src/libsodium/crypto_stream/aes256estream/stream_aes256estream_api.c +0 -16
  82. data/vendor/libsodium/src/libsodium/crypto_stream/try.c +0 -122
  83. data/vendor/libsodium/src/libsodium/crypto_verify/try.c +0 -76
  84. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_aes256estream.h +0 -67
  85. data/vendor/libsodium/src/libsodium/sodium/compat.c +0 -361
  86. data/vendor/libsodium/test/default/stream5.c +0 -29
  87. data/vendor/libsodium/test/default/stream5.exp +0 -1
  88. data/vendor/libsodium/test/default/stream6.c +0 -54
  89. data/vendor/libsodium/test/default/stream6.exp +0 -17
@@ -1,9 +1,11 @@
1
1
  #include "api.h"
2
2
  #include "crypto_verify_32.h"
3
+ #include "utils.h"
3
4
 
4
5
  int crypto_auth_verify(const unsigned char *h,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
5
6
  {
6
7
  unsigned char correct[32];
7
8
  crypto_auth(correct,in,inlen,k);
8
- return crypto_verify_32(h,correct);
9
+ return crypto_verify_32(h,correct) | (-(h - correct == 0)) |
10
+ sodium_memcmp(correct,h,32);
9
11
  }
@@ -1,10 +1,12 @@
1
1
  #include "api.h"
2
2
  #include "crypto_verify_64.h"
3
+ #include "utils.h"
3
4
 
4
5
  int crypto_auth_verify(const unsigned char *h, const unsigned char *in,
5
6
  unsigned long long inlen, const unsigned char *k)
6
7
  {
7
8
  unsigned char correct[64];
8
9
  crypto_auth(correct,in,inlen,k);
9
- return crypto_verify_64(h,correct);
10
+ return crypto_verify_64(h,correct) | (-(h - correct == 0)) |
11
+ sodium_memcmp(correct,h,64);
10
12
  }
@@ -1,10 +1,12 @@
1
1
  #include "api.h"
2
2
  #include "crypto_verify_32.h"
3
+ #include "utils.h"
3
4
 
4
5
  int crypto_auth_verify(const unsigned char *h, const unsigned char *in,
5
6
  unsigned long long inlen, const unsigned char *k)
6
7
  {
7
8
  unsigned char correct[32];
8
9
  crypto_auth(correct,in,inlen,k);
9
- return crypto_verify_32(h,correct);
10
+ return crypto_verify_32(h,correct) | (-(h - correct == 0)) |
11
+ sodium_memcmp(correct,h,32);
10
12
  }
@@ -61,6 +61,12 @@ crypto_pwhash_scryptsalsa208sha256_strbytes(void)
61
61
  return crypto_pwhash_scryptsalsa208sha256_STRBYTES;
62
62
  }
63
63
 
64
+ const char *
65
+ crypto_pwhash_scryptsalsa208sha256_strprefix(void)
66
+ {
67
+ return crypto_pwhash_scryptsalsa208sha256_STRPREFIX;
68
+ }
69
+
64
70
  size_t
65
71
  crypto_pwhash_scryptsalsa208sha256_opslimit_interactive(void)
66
72
  {
@@ -50,7 +50,7 @@
50
50
  /* qhasm: z2 = z1^2^1 */
51
51
  /* asm 1: fe_sq(>z2=fe#1,<z1=fe#11); for (i = 1;i < 1;++i) fe_sq(>z2=fe#1,>z2=fe#1); */
52
52
  /* asm 2: fe_sq(>z2=t0,<z1=z); for (i = 1;i < 1;++i) fe_sq(>z2=t0,>z2=t0); */
53
- fe_sq(t0,z); for (i = 1;i < 1;++i) fe_sq(t0,t0);
53
+ fe_sq(t0,z); /* for (i = 1;i < 1;++i) fe_sq(t0,t0); */
54
54
 
55
55
  /* qhasm: z8 = z2^2^2 */
56
56
  /* asm 1: fe_sq(>z8=fe#2,<z2=fe#1); for (i = 1;i < 2;++i) fe_sq(>z8=fe#2,>z8=fe#2); */
@@ -70,7 +70,7 @@ fe_mul(t0,t0,t1);
70
70
  /* qhasm: z22 = z11^2^1 */
71
71
  /* asm 1: fe_sq(>z22=fe#3,<z11=fe#1); for (i = 1;i < 1;++i) fe_sq(>z22=fe#3,>z22=fe#3); */
72
72
  /* asm 2: fe_sq(>z22=t2,<z11=t0); for (i = 1;i < 1;++i) fe_sq(>z22=t2,>z22=t2); */
73
- fe_sq(t2,t0); for (i = 1;i < 1;++i) fe_sq(t2,t2);
73
+ fe_sq(t2,t0); /* for (i = 1;i < 1;++i) fe_sq(t2,t2); */
74
74
 
75
75
  /* qhasm: z_5_0 = z9*z22 */
76
76
  /* asm 1: fe_mul(>z_5_0=fe#2,<z9=fe#2,<z22=fe#3); */
@@ -2,8 +2,11 @@
2
2
  #include <string.h>
3
3
 
4
4
  #include "api.h"
5
- #include "randombytes.h"
6
5
  #include "crypto_hash_sha512.h"
6
+ #include "crypto_scalarmult_curve25519.h"
7
+ #include "randombytes.h"
8
+ #include "utils.h"
9
+ #include "fe.h"
7
10
  #include "ge.h"
8
11
 
9
12
  int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk,
@@ -27,7 +30,47 @@ int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk,
27
30
  int crypto_sign_keypair(unsigned char *pk, unsigned char *sk)
28
31
  {
29
32
  unsigned char seed[32];
33
+ int ret;
34
+
35
+ randombytes(seed, sizeof seed);
36
+ ret = crypto_sign_seed_keypair(pk, sk, seed);
37
+ sodium_memzero(seed, sizeof seed);
38
+
39
+ return ret;
40
+ }
41
+
42
+ int crypto_sign_ed25519_pk_to_curve25519(unsigned char *curve25519_pk,
43
+ const unsigned char *ed25519_pk)
44
+ {
45
+ ge_p3 A;
46
+ fe x;
47
+ fe one_minus_y;
30
48
 
31
- randombytes(seed,32);
32
- return crypto_sign_seed_keypair(pk,sk,seed);
49
+ ge_frombytes_negate_vartime(&A, ed25519_pk);
50
+ fe_1(one_minus_y);
51
+ fe_sub(one_minus_y, one_minus_y, A.Y);
52
+ fe_invert(one_minus_y, one_minus_y);
53
+ fe_1(x);
54
+ fe_add(x, x, A.Y);
55
+ fe_mul(x, x, one_minus_y);
56
+ fe_tobytes(curve25519_pk, x);
57
+
58
+ return 0;
59
+ }
60
+
61
+ int crypto_sign_ed25519_sk_to_curve25519(unsigned char *curve25519_sk,
62
+ const unsigned char *ed25519_sk)
63
+ {
64
+ unsigned char h[crypto_hash_sha512_BYTES];
65
+
66
+ crypto_hash_sha512(h, ed25519_sk,
67
+ crypto_sign_ed25519_SECRETKEYBYTES -
68
+ crypto_sign_ed25519_PUBLICKEYBYTES);
69
+ h[0] &= 248;
70
+ h[31] &= 127;
71
+ h[31] |= 64;
72
+ memcpy(curve25519_sk, h, crypto_scalarmult_curve25519_BYTES);
73
+ sodium_memzero(h, sizeof h);
74
+
75
+ return 0;
33
76
  }
@@ -43,16 +43,8 @@ crypto_sign_verify_detached(const unsigned char *sig, const unsigned char *m,
43
43
  ge_double_scalarmult_vartime(&R, h, &A, sig + 32);
44
44
  ge_tobytes(rcheck, &R);
45
45
 
46
- if (crypto_verify_32(rcheck, sig) != 0) {
47
- return -1;
48
- }
49
- if (sig == rcheck) {
50
- return -1;
51
- }
52
- if (sodium_memcmp(sig, rcheck, 32) != 0) {
53
- return -1;
54
- }
55
- return 0;
46
+ return crypto_verify_32(rcheck, sig) | (-(rcheck - sig == 0)) |
47
+ sodium_memcmp(sig, rcheck, 32);
56
48
  }
57
49
 
58
50
  int
@@ -5,6 +5,7 @@
5
5
  #include "crypto_hash_sha512.h"
6
6
  #include "ge.h"
7
7
  #include "sc.h"
8
+ #include "utils.h"
8
9
 
9
10
  int
10
11
  crypto_sign_detached(unsigned char *sig, unsigned long long *siglen,
@@ -44,6 +45,9 @@ crypto_sign_detached(unsigned char *sig, unsigned long long *siglen,
44
45
  sc_reduce(hram);
45
46
  sc_muladd(sig + 32, hram, az, nonce);
46
47
 
48
+ sodium_memzero(az, sizeof az);
49
+ sodium_memzero(nonce, sizeof nonce);
50
+
47
51
  if (siglen != NULL) {
48
52
  *siglen = 64U;
49
53
  }
@@ -59,7 +63,9 @@ crypto_sign(unsigned char *sm, unsigned long long *smlen,
59
63
 
60
64
  if (crypto_sign_detached(sm, &siglen, m, mlen, sk) != 0 ||
61
65
  siglen > crypto_sign_ed25519_BYTES) {
62
- *smlen = 0;
66
+ if (smlen != NULL) {
67
+ *smlen = 0;
68
+ }
63
69
  memset(sm, 0, mlen + crypto_sign_ed25519_BYTES);
64
70
  return -1;
65
71
  }
@@ -1,3 +1,6 @@
1
+
2
+ #include <string.h>
3
+
1
4
  #include "crypto_sign_ed25519.h"
2
5
 
3
6
  size_t
@@ -19,3 +22,18 @@ size_t
19
22
  crypto_sign_ed25519_secretkeybytes(void) {
20
23
  return crypto_sign_ed25519_SECRETKEYBYTES;
21
24
  }
25
+
26
+ int
27
+ crypto_sign_ed25519_sk_to_seed(unsigned char *seed, const unsigned char *sk)
28
+ {
29
+ memmove(seed, sk, crypto_sign_ed25519_SEEDBYTES);
30
+ return 0;
31
+ }
32
+
33
+ int
34
+ crypto_sign_ed25519_sk_to_pk(unsigned char *pk, const unsigned char *sk)
35
+ {
36
+ memmove(pk, sk + crypto_sign_ed25519_SEEDBYTES,
37
+ crypto_sign_ed25519_PUBLICKEYBYTES);
38
+ return 0;
39
+ }
@@ -32,7 +32,6 @@ SODIUM_EXPORT = \
32
32
  sodium/crypto_sign_edwards25519sha512batch.h \
33
33
  sodium/crypto_stream.h \
34
34
  sodium/crypto_stream_aes128ctr.h \
35
- sodium/crypto_stream_aes256estream.h \
36
35
  sodium/crypto_stream_chacha20.h \
37
36
  sodium/crypto_stream_salsa20.h \
38
37
  sodium/crypto_stream_salsa2012.h \
@@ -325,7 +325,6 @@ SODIUM_EXPORT = \
325
325
  sodium/crypto_sign_edwards25519sha512batch.h \
326
326
  sodium/crypto_stream.h \
327
327
  sodium/crypto_stream_aes128ctr.h \
328
- sodium/crypto_stream_aes256estream.h \
329
328
  sodium/crypto_stream_chacha20.h \
330
329
  sodium/crypto_stream_salsa20.h \
331
330
  sodium/crypto_stream_salsa2012.h \
@@ -32,7 +32,6 @@
32
32
  #include <sodium/crypto_sign_ed25519.h>
33
33
  #include <sodium/crypto_stream.h>
34
34
  #include <sodium/crypto_stream_aes128ctr.h>
35
- #include <sodium/crypto_stream_aes256estream.h>
36
35
  #include <sodium/crypto_stream_chacha20.h>
37
36
  #include <sodium/crypto_stream_salsa20.h>
38
37
  #include <sodium/crypto_stream_salsa2012.h>
@@ -54,9 +54,8 @@ const char *crypto_onetimeauth_poly1305_implementation_name(void);
54
54
  SODIUM_EXPORT
55
55
  int crypto_onetimeauth_poly1305_set_implementation(crypto_onetimeauth_poly1305_implementation *impl);
56
56
 
57
- SODIUM_EXPORT
58
57
  crypto_onetimeauth_poly1305_implementation *
59
- crypto_onetimeauth_pick_best_implementation(void);
58
+ crypto_onetimeauth_pick_best_implementation(void);
60
59
 
61
60
  SODIUM_EXPORT
62
61
  int crypto_onetimeauth_poly1305(unsigned char *out,
@@ -2,6 +2,7 @@
2
2
  #define crypto_pwhash_scryptsalsa208sha256_H
3
3
 
4
4
  #include <stddef.h>
5
+ #include <stdint.h>
5
6
 
6
7
  #include "export.h"
7
8
 
@@ -20,6 +21,10 @@ size_t crypto_pwhash_scryptsalsa208sha256_saltbytes(void);
20
21
  SODIUM_EXPORT
21
22
  size_t crypto_pwhash_scryptsalsa208sha256_strbytes(void);
22
23
 
24
+ #define crypto_pwhash_scryptsalsa208sha256_STRPREFIX "$7$"
25
+ SODIUM_EXPORT
26
+ const char *crypto_pwhash_scryptsalsa208sha256_strprefix(void);
27
+
23
28
  #define crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE 524288ULL
24
29
  SODIUM_EXPORT
25
30
  size_t crypto_pwhash_scryptsalsa208sha256_opslimit_interactive(void);
@@ -57,6 +57,21 @@ SODIUM_EXPORT
57
57
  int crypto_sign_ed25519_seed_keypair(unsigned char *pk, unsigned char *sk,
58
58
  const unsigned char *seed);
59
59
 
60
+ SODIUM_EXPORT
61
+ int crypto_sign_ed25519_pk_to_curve25519(unsigned char *curve25519_pk,
62
+ const unsigned char *ed25519_pk);
63
+
64
+ SODIUM_EXPORT
65
+ int crypto_sign_ed25519_sk_to_curve25519(unsigned char *curve25519_sk,
66
+ const unsigned char *ed25519_sk);
67
+
68
+ SODIUM_EXPORT
69
+ int crypto_sign_ed25519_sk_to_seed(unsigned char *seed,
70
+ const unsigned char *sk);
71
+
72
+ SODIUM_EXPORT
73
+ int crypto_sign_ed25519_sk_to_pk(unsigned char *pk, const unsigned char *sk);
74
+
60
75
  #ifdef __cplusplus
61
76
  }
62
77
  #endif
@@ -16,12 +16,14 @@ extern "C" {
16
16
  # define _SODIUM_C99(X) X
17
17
  #endif
18
18
 
19
- unsigned char *_sodium_alignedcalloc(unsigned char ** const unaligned_p,
20
- const size_t len);
21
-
22
19
  SODIUM_EXPORT
23
20
  void sodium_memzero(void * const pnt, const size_t len);
24
21
 
22
+ /* WARNING: sodium_memcmp() must be used to verify if two secret keys
23
+ * are equal, in constant time.
24
+ * It returns 0 if the keys are equal, and -1 if they differ.
25
+ * This function is not designed for lexicographical comparisons.
26
+ */
25
27
  SODIUM_EXPORT
26
28
  int sodium_memcmp(const void * const b1_, const void * const b2_, size_t len);
27
29
 
@@ -41,6 +43,55 @@ int sodium_mlock(void * const addr, const size_t len);
41
43
  SODIUM_EXPORT
42
44
  int sodium_munlock(void * const addr, const size_t len);
43
45
 
46
+ /* WARNING: sodium_malloc() and sodium_allocarray() are not general-purpose
47
+ * allocation functions.
48
+ *
49
+ * They return a pointer to a region filled with 0xd0 bytes, immediately
50
+ * followed by a guard page.
51
+ * As a result, accessing a single byte after the requested allocation size
52
+ * will intentionally trigger a segmentation fault.
53
+ *
54
+ * A canary and an additional guard page placed before the beginning of the
55
+ * region may also kill the process if a buffer underflow is detected.
56
+ *
57
+ * The memory layout is:
58
+ * [unprotected region size (read only)][guard page (no access)][unprotected pages (read/write)][guard page (no access)]
59
+ * With the layout of the unprotected pages being:
60
+ * [optional padding][16-bytes canary][user region]
61
+ *
62
+ * However:
63
+ * - These functions are significantly slower than standard functions
64
+ * - Each allocation requires 3 or 4 additional pages
65
+ * - The returned address will not be aligned if the allocation size is not
66
+ * a multiple of the required alignment. For this reason, these functions
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.
71
+ */
72
+
73
+ SODIUM_EXPORT
74
+ void *sodium_malloc(const size_t size);
75
+
76
+ SODIUM_EXPORT
77
+ void *sodium_allocarray(size_t count, size_t size);
78
+
79
+ SODIUM_EXPORT
80
+ void sodium_free(void *ptr);
81
+
82
+ SODIUM_EXPORT
83
+ int sodium_mprotect_noaccess(void *ptr);
84
+
85
+ SODIUM_EXPORT
86
+ int sodium_mprotect_readonly(void *ptr);
87
+
88
+ SODIUM_EXPORT
89
+ int sodium_mprotect_readwrite(void *ptr);
90
+
91
+ /* -------- */
92
+
93
+ int _sodium_alloc_init(void);
94
+
44
95
  #ifdef __cplusplus
45
96
  }
46
97
  #endif
@@ -41,7 +41,7 @@ BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
41
41
 
42
42
  typedef struct Salsa20Random_ {
43
43
  unsigned char key[crypto_stream_salsa20_KEYBYTES];
44
- unsigned char rnd32[SALSA20_RANDOM_BLOCK_SIZE];
44
+ unsigned char rnd32[16U * SALSA20_RANDOM_BLOCK_SIZE];
45
45
  uint64_t nonce;
46
46
  size_t rnd32_outleft;
47
47
  #ifndef _MSC_VER
@@ -67,7 +67,7 @@ sodium_hrtime(void)
67
67
  #ifdef _WIN32
68
68
  struct _timeb tb;
69
69
 
70
- _ftime(&tb);
70
+ _ftime_s(&tb);
71
71
  tv.tv_sec = (long) tb.time;
72
72
  tv.tv_usec = ((int) tb.millitm) * 1000;
73
73
  ret = 0;
@@ -214,14 +214,26 @@ randombytes_salsa20_random_stir_if_needed(void)
214
214
  #endif
215
215
  }
216
216
 
217
+ static void
218
+ randombytes_salsa20_random_rekey(const unsigned char * const mix)
219
+ {
220
+ unsigned char *key = stream.key;
221
+ size_t i;
222
+
223
+ for (i = (size_t) 0U; i < sizeof stream.key; i++) {
224
+ key[i] ^= mix[i];
225
+ }
226
+ }
227
+
217
228
  static uint32_t
218
229
  randombytes_salsa20_random_getword(void)
219
230
  {
220
231
  uint32_t val;
221
232
  int ret;
222
233
 
223
- COMPILER_ASSERT(sizeof stream.rnd32 >= sizeof val);
224
- COMPILER_ASSERT(sizeof stream.rnd32 % sizeof val == (size_t) 0U);
234
+ COMPILER_ASSERT(sizeof stream.rnd32 >= (sizeof stream.key) + (sizeof val));
235
+ COMPILER_ASSERT(((sizeof stream.rnd32) - (sizeof stream.key))
236
+ % sizeof val == (size_t) 0U);
225
237
  if (stream.rnd32_outleft <= (size_t) 0U) {
226
238
  randombytes_salsa20_random_stir_if_needed();
227
239
  COMPILER_ASSERT(sizeof stream.nonce == crypto_stream_salsa20_NONCEBYTES);
@@ -230,11 +242,13 @@ randombytes_salsa20_random_getword(void)
230
242
  (unsigned char *) &stream.nonce,
231
243
  stream.key);
232
244
  assert(ret == 0);
245
+ stream.rnd32_outleft = (sizeof stream.rnd32) - (sizeof stream.key);
246
+ randombytes_salsa20_random_rekey(&stream.rnd32[stream.rnd32_outleft]);
233
247
  stream.nonce++;
234
- stream.rnd32_outleft = sizeof stream.rnd32;
235
248
  }
236
249
  stream.rnd32_outleft -= sizeof val;
237
250
  memcpy(&val, &stream.rnd32[stream.rnd32_outleft], sizeof val);
251
+ memset(&stream.rnd32[stream.rnd32_outleft], 0, sizeof val);
238
252
 
239
253
  return val;
240
254
  }
@@ -278,10 +292,11 @@ randombytes_salsa20_random_buf(void * const buf, const size_t size)
278
292
  assert(size <= ULONG_LONG_MAX);
279
293
  #endif
280
294
  ret = crypto_stream_salsa20((unsigned char *) buf, (unsigned long long) size,
281
- (unsigned char *) &stream.nonce,
282
- stream.key);
295
+ (unsigned char *) &stream.nonce, stream.key);
283
296
  assert(ret == 0);
284
297
  stream.nonce++;
298
+ crypto_stream_salsa20_xor(stream.key, stream.key, sizeof stream.key,
299
+ (unsigned char *) &stream.nonce, stream.key);
285
300
  }
286
301
 
287
302
  /*
@@ -3,6 +3,7 @@
3
3
  #include "crypto_onetimeauth.h"
4
4
  #include "randombytes.h"
5
5
  #include "runtime.h"
6
+ #include "utils.h"
6
7
 
7
8
  static int initialized;
8
9
 
@@ -17,6 +18,7 @@ sodium_init(void)
17
18
  return -1;
18
19
  }
19
20
  randombytes_stir();
21
+ _sodium_alloc_init();
20
22
  initialized = 1;
21
23
 
22
24
  return 0;