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
@@ -8,6 +8,8 @@
8
8
  #include "crypto_verify_16.h"
9
9
  #include "utils.h"
10
10
 
11
+ static unsigned char _pad0[16];
12
+
11
13
  static inline void
12
14
  _u64_le_from_ull(unsigned char out[8U], unsigned long long x)
13
15
  {
@@ -23,7 +25,7 @@ _u64_le_from_ull(unsigned char out[8U], unsigned long long x)
23
25
 
24
26
  int
25
27
  crypto_aead_chacha20poly1305_encrypt(unsigned char *c,
26
- unsigned long long *clen,
28
+ unsigned long long *clen_p,
27
29
  const unsigned char *m,
28
30
  unsigned long long mlen,
29
31
  const unsigned char *ad,
@@ -40,8 +42,8 @@ crypto_aead_chacha20poly1305_encrypt(unsigned char *c,
40
42
  /* LCOV_EXCL_START */
41
43
  #ifdef ULONG_LONG_MAX
42
44
  if (mlen > ULONG_LONG_MAX - crypto_aead_chacha20poly1305_ABYTES) {
43
- if (clen != NULL) {
44
- *clen = 0ULL;
45
+ if (clen_p != NULL) {
46
+ *clen_p = 0ULL;
45
47
  }
46
48
  return -1;
47
49
  }
@@ -65,15 +67,69 @@ crypto_aead_chacha20poly1305_encrypt(unsigned char *c,
65
67
  crypto_onetimeauth_poly1305_final(&state, c + mlen);
66
68
  sodium_memzero(&state, sizeof state);
67
69
 
68
- if (clen != NULL) {
69
- *clen = mlen + crypto_aead_chacha20poly1305_ABYTES;
70
+ if (clen_p != NULL) {
71
+ *clen_p = mlen + crypto_aead_chacha20poly1305_ABYTES;
72
+ }
73
+ return 0;
74
+ }
75
+
76
+ int
77
+ crypto_aead_chacha20poly1305_ietf_encrypt(unsigned char *c,
78
+ unsigned long long *clen_p,
79
+ const unsigned char *m,
80
+ unsigned long long mlen,
81
+ const unsigned char *ad,
82
+ unsigned long long adlen,
83
+ const unsigned char *nsec,
84
+ const unsigned char *npub,
85
+ const unsigned char *k)
86
+ {
87
+ crypto_onetimeauth_poly1305_state state;
88
+ unsigned char block0[64U];
89
+ unsigned char slen[8U];
90
+
91
+ (void) nsec;
92
+ /* LCOV_EXCL_START */
93
+ #ifdef ULONG_LONG_MAX
94
+ if (mlen > ULONG_LONG_MAX - crypto_aead_chacha20poly1305_ABYTES) {
95
+ if (clen_p != NULL) {
96
+ *clen_p = 0ULL;
97
+ }
98
+ return -1;
99
+ }
100
+ #endif
101
+ /* LCOV_EXCL_STOP */
102
+
103
+ crypto_stream_chacha20_ietf(block0, sizeof block0, npub, k);
104
+ crypto_onetimeauth_poly1305_init(&state, block0);
105
+ sodium_memzero(block0, sizeof block0);
106
+
107
+ crypto_onetimeauth_poly1305_update(&state, ad, adlen);
108
+ crypto_onetimeauth_poly1305_update(&state, _pad0, (0x10 - adlen) & 0xf);
109
+
110
+ crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, npub, 1U, k);
111
+
112
+ crypto_onetimeauth_poly1305_update(&state, c, mlen);
113
+ crypto_onetimeauth_poly1305_update(&state, _pad0, (0x10 - mlen) & 0xf);
114
+
115
+ _u64_le_from_ull(slen, adlen);
116
+ crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
117
+
118
+ _u64_le_from_ull(slen, mlen);
119
+ crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
120
+
121
+ crypto_onetimeauth_poly1305_final(&state, c + mlen);
122
+ sodium_memzero(&state, sizeof state);
123
+
124
+ if (clen_p != NULL) {
125
+ *clen_p = mlen + crypto_aead_chacha20poly1305_ABYTES;
70
126
  }
71
127
  return 0;
72
128
  }
73
129
 
74
130
  int
75
131
  crypto_aead_chacha20poly1305_decrypt(unsigned char *m,
76
- unsigned long long *mlen,
132
+ unsigned long long *mlen_p,
77
133
  unsigned char *nsec,
78
134
  const unsigned char *c,
79
135
  unsigned long long clen,
@@ -86,11 +142,12 @@ crypto_aead_chacha20poly1305_decrypt(unsigned char *m,
86
142
  unsigned char block0[64U];
87
143
  unsigned char slen[8U];
88
144
  unsigned char mac[crypto_aead_chacha20poly1305_ABYTES];
145
+ unsigned long long mlen;
89
146
  int ret;
90
147
 
91
148
  (void) nsec;
92
- if (mlen != NULL) {
93
- *mlen = 0ULL;
149
+ if (mlen_p != NULL) {
150
+ *mlen_p = 0ULL;
94
151
  }
95
152
  if (clen < crypto_aead_chacha20poly1305_ABYTES) {
96
153
  return -1;
@@ -103,26 +160,84 @@ crypto_aead_chacha20poly1305_decrypt(unsigned char *m,
103
160
  _u64_le_from_ull(slen, adlen);
104
161
  crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
105
162
 
106
- crypto_onetimeauth_poly1305_update
107
- (&state, c, clen - crypto_aead_chacha20poly1305_ABYTES);
108
- _u64_le_from_ull(slen, clen - crypto_aead_chacha20poly1305_ABYTES);
163
+ mlen = clen - crypto_aead_chacha20poly1305_ABYTES;
164
+ crypto_onetimeauth_poly1305_update(&state, c, mlen);
165
+ _u64_le_from_ull(slen, mlen);
109
166
  crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
110
167
 
111
168
  crypto_onetimeauth_poly1305_final(&state, mac);
112
169
  sodium_memzero(&state, sizeof state);
113
170
 
114
171
  (void) sizeof(int[sizeof mac == 16U ? 1 : -1]);
115
- ret = crypto_verify_16(mac,
116
- c + clen - crypto_aead_chacha20poly1305_ABYTES);
172
+ ret = crypto_verify_16(mac, c + mlen);
117
173
  sodium_memzero(mac, sizeof mac);
118
174
  if (ret != 0) {
119
- memset(m, 0, clen - crypto_aead_chacha20poly1305_ABYTES);
175
+ memset(m, 0, mlen);
120
176
  return -1;
121
177
  }
122
178
  crypto_stream_chacha20_xor_ic
123
- (m, c, clen - crypto_aead_chacha20poly1305_ABYTES, npub, 1U, k);
124
- if (mlen != NULL) {
125
- *mlen = clen - crypto_aead_chacha20poly1305_ABYTES;
179
+ (m, c, mlen, npub, 1U, k);
180
+ if (mlen_p != NULL) {
181
+ *mlen_p = mlen;
182
+ }
183
+ return 0;
184
+ }
185
+
186
+ int
187
+ crypto_aead_chacha20poly1305_ietf_decrypt(unsigned char *m,
188
+ unsigned long long *mlen_p,
189
+ unsigned char *nsec,
190
+ const unsigned char *c,
191
+ unsigned long long clen,
192
+ const unsigned char *ad,
193
+ unsigned long long adlen,
194
+ const unsigned char *npub,
195
+ const unsigned char *k)
196
+ {
197
+ crypto_onetimeauth_poly1305_state state;
198
+ unsigned char block0[64U];
199
+ unsigned char slen[8U];
200
+ unsigned char mac[crypto_aead_chacha20poly1305_ABYTES];
201
+ unsigned long long mlen;
202
+ int ret;
203
+
204
+ (void) nsec;
205
+ if (mlen_p != NULL) {
206
+ *mlen_p = 0ULL;
207
+ }
208
+ if (clen < crypto_aead_chacha20poly1305_ABYTES) {
209
+ return -1;
210
+ }
211
+ crypto_stream_chacha20_ietf(block0, sizeof block0, npub, k);
212
+ crypto_onetimeauth_poly1305_init(&state, block0);
213
+ sodium_memzero(block0, sizeof block0);
214
+
215
+ crypto_onetimeauth_poly1305_update(&state, ad, adlen);
216
+ crypto_onetimeauth_poly1305_update(&state, _pad0, (0x10 - adlen) & 0xf);
217
+
218
+ mlen = clen - crypto_aead_chacha20poly1305_ABYTES;
219
+ crypto_onetimeauth_poly1305_update(&state, c, mlen);
220
+ crypto_onetimeauth_poly1305_update(&state, _pad0, (0x10 - mlen) & 0xf);
221
+
222
+ _u64_le_from_ull(slen, adlen);
223
+ crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
224
+
225
+ _u64_le_from_ull(slen, mlen);
226
+ crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
227
+
228
+ crypto_onetimeauth_poly1305_final(&state, mac);
229
+ sodium_memzero(&state, sizeof state);
230
+
231
+ (void) sizeof(int[sizeof mac == 16U ? 1 : -1]);
232
+ ret = crypto_verify_16(mac, c + mlen);
233
+ sodium_memzero(mac, sizeof mac);
234
+ if (ret != 0) {
235
+ memset(m, 0, mlen);
236
+ return -1;
237
+ }
238
+ crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, npub, 1U, k);
239
+ if (mlen_p != NULL) {
240
+ *mlen_p = mlen;
126
241
  }
127
242
  return 0;
128
243
  }
@@ -137,6 +252,11 @@ crypto_aead_chacha20poly1305_npubbytes(void) {
137
252
  return crypto_aead_chacha20poly1305_NPUBBYTES;
138
253
  }
139
254
 
255
+ size_t
256
+ crypto_aead_chacha20poly1305_ietf_npubbytes(void) {
257
+ return crypto_aead_chacha20poly1305_IETF_NPUBBYTES;
258
+ }
259
+
140
260
  size_t
141
261
  crypto_aead_chacha20poly1305_nsecbytes(void) {
142
262
  return crypto_aead_chacha20poly1305_NSECBYTES;
@@ -66,6 +66,7 @@ crypto_auth_hmacsha256_init(crypto_auth_hmacsha256_state *state,
66
66
  }
67
67
  crypto_hash_sha256_update(&state->octx, pad, 64);
68
68
 
69
+ sodium_memzero((void *) pad, sizeof pad);
69
70
  sodium_memzero((void *) khash, sizeof khash);
70
71
 
71
72
  return 0;
@@ -66,6 +66,7 @@ crypto_auth_hmacsha512_init(crypto_auth_hmacsha512_state *state,
66
66
  }
67
67
  crypto_hash_sha512_update(&state->octx, pad, 128);
68
68
 
69
+ sodium_memzero((void *) pad, sizeof pad);
69
70
  sodium_memzero((void *) khash, sizeof khash);
70
71
 
71
72
  return 0;
@@ -46,14 +46,14 @@ static const uint8_t blake2b_sigma[12][16] =
46
46
  /* LCOV_EXCL_START */
47
47
  static inline int blake2b_set_lastnode( blake2b_state *S )
48
48
  {
49
- S->f[1] = ~0ULL;
49
+ S->f[1] = -1;
50
50
  return 0;
51
51
  }
52
52
  /* LCOV_EXCL_STOP */
53
53
  #if 0
54
54
  static inline int blake2b_clear_lastnode( blake2b_state *S )
55
55
  {
56
- S->f[1] = 0ULL;
56
+ S->f[1] = 0;
57
57
  return 0;
58
58
  }
59
59
  #endif
@@ -62,7 +62,7 @@ static inline int blake2b_set_lastblock( blake2b_state *S )
62
62
  {
63
63
  if( S->last_node ) blake2b_set_lastnode( S );
64
64
 
65
- S->f[0] = ~0ULL;
65
+ S->f[0] = -1;
66
66
  return 0;
67
67
  }
68
68
  #if 0
@@ -70,7 +70,7 @@ static inline int blake2b_clear_lastblock( blake2b_state *S )
70
70
  {
71
71
  if( S->last_node ) blake2b_clear_lastnode( S );
72
72
 
73
- S->f[0] = 0ULL;
73
+ S->f[0] = 0;
74
74
  return 0;
75
75
  }
76
76
  #endif
@@ -418,11 +418,15 @@ int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen
418
418
  blake2b_state S[1];
419
419
 
420
420
  /* Verify parameters */
421
- if ( NULL == in ) return -1;
421
+ if ( NULL == in && inlen > 0 ) return -1;
422
422
 
423
423
  if ( NULL == out ) return -1;
424
424
 
425
- if( NULL == key ) keylen = 0;
425
+ if( NULL == key && keylen > 0 ) return -1;
426
+
427
+ if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
428
+
429
+ if( keylen > BLAKE2B_KEYBYTES ) return -1;
426
430
 
427
431
  if( keylen > 0 )
428
432
  {
@@ -46,7 +46,7 @@ crypto_generichash_primitive(void)
46
46
  size_t
47
47
  crypto_generichash_statebytes(void)
48
48
  {
49
- return sizeof(crypto_generichash_state);
49
+ return (sizeof(crypto_generichash_state) + (size_t) 63U) & ~(size_t) 63U;
50
50
  }
51
51
 
52
52
  int
@@ -40,6 +40,7 @@
40
40
  /* Avoid namespace collisions with BSD <sys/endian.h>. */
41
41
  #define be32dec _sha256_be32dec
42
42
  #define be32enc _sha256_be32enc
43
+ #define be64enc _sha256_be64enc
43
44
 
44
45
  static inline uint32_t
45
46
  be32dec(const void *pp)
@@ -53,7 +54,7 @@ be32dec(const void *pp)
53
54
  static inline void
54
55
  be32enc(void *pp, uint32_t x)
55
56
  {
56
- uint8_t * p = (uint8_t *)pp;
57
+ uint8_t *p = (uint8_t *)pp;
57
58
 
58
59
  p[3] = x & 0xff;
59
60
  p[2] = (x >> 8) & 0xff;
@@ -61,6 +62,21 @@ be32enc(void *pp, uint32_t x)
61
62
  p[0] = (x >> 24) & 0xff;
62
63
  }
63
64
 
65
+ static inline void
66
+ be64enc(void * pp, uint64_t x)
67
+ {
68
+ uint8_t * p = (uint8_t *)pp;
69
+
70
+ p[7] = x & 0xff;
71
+ p[6] = (x >> 8) & 0xff;
72
+ p[5] = (x >> 16) & 0xff;
73
+ p[4] = (x >> 24) & 0xff;
74
+ p[3] = (x >> 32) & 0xff;
75
+ p[2] = (x >> 40) & 0xff;
76
+ p[1] = (x >> 48) & 0xff;
77
+ p[0] = (x >> 56) & 0xff;
78
+ }
79
+
64
80
  static void
65
81
  be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
66
82
  {
@@ -206,9 +222,9 @@ SHA256_Pad(crypto_hash_sha256_state *state)
206
222
  unsigned char len[8];
207
223
  uint32_t r, plen;
208
224
 
209
- be32enc_vect(len, state->count, 8);
225
+ be64enc(len, state->count);
210
226
 
211
- r = (state->count[1] >> 3) & 0x3f;
227
+ r = (state->count >> 3) & 0x3f;
212
228
  plen = (r < 56) ? (56 - r) : (120 - r);
213
229
  crypto_hash_sha256_update(state, PAD, (unsigned long long) plen);
214
230
 
@@ -218,16 +234,13 @@ SHA256_Pad(crypto_hash_sha256_state *state)
218
234
  int
219
235
  crypto_hash_sha256_init(crypto_hash_sha256_state *state)
220
236
  {
221
- state->count[0] = state->count[1] = 0;
237
+ static const uint32_t sha256_initstate[8] = {
238
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
239
+ 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
240
+ };
222
241
 
223
- state->state[0] = 0x6A09E667;
224
- state->state[1] = 0xBB67AE85;
225
- state->state[2] = 0x3C6EF372;
226
- state->state[3] = 0xA54FF53A;
227
- state->state[4] = 0x510E527F;
228
- state->state[5] = 0x9B05688C;
229
- state->state[6] = 0x1F83D9AB;
230
- state->state[7] = 0x5BE0CD19;
242
+ state->count = (uint64_t) 0U;
243
+ memcpy(state->state, sha256_initstate, sizeof sha256_initstate);
231
244
 
232
245
  return 0;
233
246
  }
@@ -237,20 +250,13 @@ crypto_hash_sha256_update(crypto_hash_sha256_state *state,
237
250
  const unsigned char *in,
238
251
  unsigned long long inlen)
239
252
  {
240
- uint32_t bitlen[2];
241
253
  uint32_t r;
242
254
 
243
- r = (state->count[1] >> 3) & 0x3f;
244
-
245
- bitlen[1] = ((uint32_t)inlen) << 3;
246
- bitlen[0] = (uint32_t)(inlen >> 29);
247
-
248
- /* LCOV_EXCL_START */
249
- if ((state->count[1] += bitlen[1]) < bitlen[1]) {
250
- state->count[0]++;
255
+ if (inlen <= 0U) {
256
+ return 0;
251
257
  }
252
- /* LCOV_EXCL_STOP */
253
- state->count[0] += bitlen[0];
258
+ r = (state->count >> 3) & 0x3f;
259
+ state->count += (uint64_t)(inlen) << 3;
254
260
 
255
261
  if (inlen < 64 - r) {
256
262
  memcpy(&state->buf[r], in, inlen);
@@ -244,16 +244,15 @@ SHA512_Pad(crypto_hash_sha512_state *state)
244
244
  int
245
245
  crypto_hash_sha512_init(crypto_hash_sha512_state *state)
246
246
  {
247
- state->count[0] = state->count[1] = 0;
248
-
249
- state->state[0] = 0x6a09e667f3bcc908ULL;
250
- state->state[1] = 0xbb67ae8584caa73bULL;
251
- state->state[2] = 0x3c6ef372fe94f82bULL;
252
- state->state[3] = 0xa54ff53a5f1d36f1ULL;
253
- state->state[4] = 0x510e527fade682d1ULL;
254
- state->state[5] = 0x9b05688c2b3e6c1fULL;
255
- state->state[6] = 0x1f83d9abfb41bd6bULL;
256
- state->state[7] = 0x5be0cd19137e2179ULL;
247
+ static const uint64_t sha512_initstate[8] = {
248
+ 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
249
+ 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
250
+ 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
251
+ 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
252
+ };
253
+
254
+ state->count[0] = state->count[1] = (uint64_t) 0U;
255
+ memcpy(state->state, sha512_initstate, sizeof sha512_initstate);
257
256
 
258
257
  return 0;
259
258
  }
@@ -153,7 +153,8 @@ escrypt_r(escrypt_local_t * local, const uint8_t * passwd, size_t passwdlen,
153
153
  if (need > buflen || need < saltlen) {
154
154
  return NULL;
155
155
  }
156
- #if defined(HAVE_EMMINTRIN_H) || defined(_MSC_VER)
156
+ #if defined(HAVE_EMMINTRIN_H) || \
157
+ (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86)))
157
158
  escrypt_kdf =
158
159
  sodium_runtime_has_sse2() ? escrypt_kdf_sse : escrypt_kdf_nosse;
159
160
  #else
@@ -234,7 +235,8 @@ crypto_pwhash_scryptsalsa208sha256_ll(const uint8_t * passwd, size_t passwdlen,
234
235
  if (escrypt_init_local(&local)) {
235
236
  return -1; /* LCOV_EXCL_LINE */
236
237
  }
237
- #if defined(HAVE_EMMINTRIN_H) || defined(_MSC_VER)
238
+ #if defined(HAVE_EMMINTRIN_H) || \
239
+ (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86)))
238
240
  escrypt_kdf =
239
241
  sodium_runtime_has_sse2() ? escrypt_kdf_sse : escrypt_kdf_nosse;
240
242
  #else
@@ -31,6 +31,7 @@
31
31
  #define crypto_scrypt_H
32
32
 
33
33
  #include <stdint.h>
34
+ #include <stddef.h>
34
35
 
35
36
  #if SIZE_MAX > 0xffffffffULL
36
37
  # define ARCH_BITS 64