rbnacl-libsodium 1.0.13 → 1.0.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (201) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +5 -0
  3. data/lib/rbnacl/libsodium.rb +1 -0
  4. data/lib/rbnacl/libsodium/version.rb +1 -1
  5. data/vendor/libsodium/AUTHORS +2 -2
  6. data/vendor/libsodium/ChangeLog +64 -0
  7. data/vendor/libsodium/Makefile.am +1 -0
  8. data/vendor/libsodium/README.markdown +1 -3
  9. data/vendor/libsodium/THANKS +13 -3
  10. data/vendor/libsodium/builds/Makefile.am +65 -0
  11. data/vendor/libsodium/builds/msvc/resource.rc +2 -2
  12. data/vendor/libsodium/builds/msvc/version.h +3 -3
  13. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj +168 -173
  14. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters +382 -394
  15. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj +168 -173
  16. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters +382 -394
  17. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj +168 -173
  18. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters +382 -394
  19. data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj +168 -173
  20. data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters +382 -394
  21. data/vendor/libsodium/builds/msvc/vs2017/libsodium/libsodium.vcxproj +168 -173
  22. data/vendor/libsodium/builds/msvc/vs2017/libsodium/libsodium.vcxproj.filters +382 -394
  23. data/vendor/libsodium/configure.ac +59 -10
  24. data/vendor/libsodium/contrib/Findsodium.cmake +2 -0
  25. data/vendor/libsodium/dist-build/emscripten-symbols.def +72 -32
  26. data/vendor/libsodium/dist-build/emscripten.sh +33 -6
  27. data/vendor/libsodium/dist-build/generate-emscripten-symbols.sh +52 -38
  28. data/vendor/libsodium/dist-build/ios.sh +2 -2
  29. data/vendor/libsodium/libsodium.vcxproj +168 -173
  30. data/vendor/libsodium/libsodium.vcxproj.filters +220 -235
  31. data/vendor/libsodium/m4/ax_check_catchable_abrt.m4 +57 -0
  32. data/vendor/libsodium/m4/ax_check_gnu_make.m4 +2 -2
  33. data/vendor/libsodium/m4/ax_pthread.m4 +2 -2
  34. data/vendor/libsodium/msvc-scripts/process.bat +3 -3
  35. data/vendor/libsodium/packaging/dotnet-core/README.md +5 -5
  36. data/vendor/libsodium/packaging/dotnet-core/libsodium.props +5 -2
  37. data/vendor/libsodium/packaging/dotnet-core/prepare.py +13 -31
  38. data/vendor/libsodium/packaging/dotnet-core/recipes/linux-x64 +4 -0
  39. data/vendor/libsodium/packaging/nuget/package.config +1 -1
  40. data/vendor/libsodium/src/libsodium/Makefile.am +11 -12
  41. data/vendor/libsodium/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +11 -4
  42. data/vendor/libsodium/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c +17 -4
  43. data/vendor/libsodium/src/libsodium/crypto_aead/xchacha20poly1305/sodium/aead_xchacha20poly1305.c +8 -1
  44. data/vendor/libsodium/src/libsodium/crypto_box/crypto_box.c +6 -0
  45. data/vendor/libsodium/src/libsodium/crypto_box/crypto_box_easy.c +5 -4
  46. data/vendor/libsodium/src/libsodium/crypto_box/curve25519xchacha20poly1305/box_curve25519xchacha20poly1305.c +11 -4
  47. data/vendor/libsodium/src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305.c +6 -0
  48. data/vendor/libsodium/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c +78 -17
  49. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-ref.c +74 -64
  50. data/vendor/libsodium/src/libsodium/crypto_hash/sha512/cp/hash_sha512_cp.c +2 -0
  51. data/vendor/libsodium/src/libsodium/crypto_kx/crypto_kx.c +7 -0
  52. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna32.h +1 -1
  53. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna64.h +1 -0
  54. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c +17 -17
  55. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-core.c +53 -48
  56. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-core.h +37 -11
  57. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-encoding.c +23 -179
  58. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx2.c +4 -12
  59. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx512f.c +244 -0
  60. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ref.c +3 -12
  61. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c +4 -12
  62. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2.c +1 -5
  63. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2.h +1 -1
  64. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/blamka-round-avx512f.h +145 -0
  65. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/pwhash_argon2i.c +91 -14
  66. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/pwhash_argon2id.c +35 -14
  67. data/vendor/libsodium/src/libsodium/crypto_pwhash/crypto_pwhash.c +41 -4
  68. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c +31 -17
  69. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt.h +4 -0
  70. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c +5 -1
  71. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c +30 -1
  72. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c +8 -0
  73. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/scalarmult_curve25519.c +3 -3
  74. data/vendor/libsodium/src/libsodium/crypto_secretbox/crypto_secretbox.c +6 -0
  75. data/vendor/libsodium/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c +5 -4
  76. data/vendor/libsodium/src/libsodium/crypto_secretbox/xchacha20poly1305/secretbox_xchacha20poly1305.c +11 -4
  77. data/vendor/libsodium/src/libsodium/crypto_secretbox/xsalsa20poly1305/secretbox_xsalsa20poly1305.c +6 -0
  78. data/vendor/libsodium/src/libsodium/crypto_secretstream/xchacha20poly1305/secretstream_xchacha20poly1305.c +311 -0
  79. data/vendor/libsodium/src/libsodium/crypto_sign/crypto_sign.c +6 -0
  80. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/ed25519_ref10.h +3 -0
  81. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/keypair.c +9 -2
  82. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/obsolete.c +1 -1
  83. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/open.c +6 -5
  84. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/sign_ed25519.c +6 -0
  85. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-avx2.c +3 -2
  86. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-ssse3.c +3 -2
  87. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/dolbeau/u0.h +1 -1
  88. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/ref/chacha20_ref.c +3 -2
  89. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/stream_chacha20.c +12 -0
  90. data/vendor/libsodium/src/libsodium/crypto_stream/crypto_stream.c +6 -0
  91. data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/stream_salsa20.c +7 -1
  92. data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/xmm6int/u0.h +1 -1
  93. data/vendor/libsodium/src/libsodium/crypto_stream/salsa2012/stream_salsa2012.c +6 -0
  94. data/vendor/libsodium/src/libsodium/crypto_stream/salsa208/stream_salsa208.c +6 -0
  95. data/vendor/libsodium/src/libsodium/crypto_stream/xchacha20/stream_xchacha20.c +6 -0
  96. data/vendor/libsodium/src/libsodium/crypto_stream/xsalsa20/stream_xsalsa20.c +6 -0
  97. data/vendor/libsodium/src/libsodium/crypto_verify/sodium/verify.c +66 -29
  98. data/vendor/libsodium/src/libsodium/include/Makefile.am +1 -1
  99. data/vendor/libsodium/src/libsodium/include/sodium.h +2 -2
  100. data/vendor/libsodium/src/libsodium/include/sodium/core.h +9 -0
  101. data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_aes256gcm.h +26 -0
  102. data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_chacha20poly1305.h +16 -4
  103. data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_xchacha20poly1305.h +10 -4
  104. data/vendor/libsodium/src/libsodium/include/sodium/crypto_box.h +4 -0
  105. data/vendor/libsodium/src/libsodium/include/sodium/crypto_box_curve25519xchacha20poly1305.h +6 -0
  106. data/vendor/libsodium/src/libsodium/include/sodium/crypto_box_curve25519xsalsa20poly1305.h +24 -15
  107. data/vendor/libsodium/src/libsodium/include/sodium/crypto_core_salsa208.h +8 -4
  108. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash.h +40 -18
  109. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash_argon2i.h +8 -2
  110. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash_argon2id.h +8 -2
  111. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash_scryptsalsa208sha256.h +11 -3
  112. data/vendor/libsodium/src/libsodium/include/sodium/crypto_secretbox.h +4 -0
  113. data/vendor/libsodium/src/libsodium/include/sodium/crypto_secretbox_xchacha20poly1305.h +6 -0
  114. data/vendor/libsodium/src/libsodium/include/sodium/crypto_secretbox_xsalsa20poly1305.h +17 -8
  115. data/vendor/libsodium/src/libsodium/include/sodium/crypto_secretstream_xchacha20poly1305.h +102 -0
  116. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign.h +4 -0
  117. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign_ed25519.h +4 -0
  118. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign_edwards25519sha512batch.h +1 -0
  119. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream.h +4 -0
  120. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_chacha20.h +10 -0
  121. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_salsa20.h +4 -0
  122. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_salsa2012.h +4 -0
  123. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_salsa208.h +15 -5
  124. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_xchacha20.h +4 -0
  125. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_xsalsa20.h +4 -0
  126. data/vendor/libsodium/src/libsodium/include/sodium/export.h +9 -0
  127. data/vendor/libsodium/src/libsodium/include/sodium/private/common.h +11 -0
  128. data/vendor/libsodium/src/libsodium/include/sodium/private/curve25519_ref10.h +2 -30
  129. data/vendor/libsodium/src/libsodium/include/sodium/randombytes.h +2 -0
  130. data/vendor/libsodium/src/libsodium/include/sodium/runtime.h +12 -9
  131. data/vendor/libsodium/src/libsodium/include/sodium/utils.h +36 -0
  132. data/vendor/libsodium/src/libsodium/randombytes/nativeclient/randombytes_nativeclient.c +3 -2
  133. data/vendor/libsodium/src/libsodium/randombytes/randombytes.c +18 -14
  134. data/vendor/libsodium/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +11 -10
  135. data/vendor/libsodium/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c +20 -8
  136. data/vendor/libsodium/src/libsodium/sodium/codecs.c +333 -0
  137. data/vendor/libsodium/src/libsodium/sodium/core.c +36 -4
  138. data/vendor/libsodium/src/libsodium/sodium/runtime.c +18 -0
  139. data/vendor/libsodium/src/libsodium/sodium/utils.c +87 -100
  140. data/vendor/libsodium/test/constcheck.sh +19 -0
  141. data/vendor/libsodium/test/default/Makefile.am +39 -7
  142. data/vendor/libsodium/test/default/aead_aes256gcm.c +6 -0
  143. data/vendor/libsodium/test/default/aead_chacha20poly1305.c +16 -0
  144. data/vendor/libsodium/test/default/aead_xchacha20poly1305.c +25 -15
  145. data/vendor/libsodium/test/default/auth.c +13 -0
  146. data/vendor/libsodium/test/default/auth.exp +8 -0
  147. data/vendor/libsodium/test/default/box.c +3 -0
  148. data/vendor/libsodium/test/default/box2.c +2 -0
  149. data/vendor/libsodium/test/default/box_easy.c +1 -4
  150. data/vendor/libsodium/test/default/box_easy2.c +7 -3
  151. data/vendor/libsodium/test/default/chacha20.c +9 -2
  152. data/vendor/libsodium/test/default/cmptest.h +47 -1
  153. data/vendor/libsodium/test/default/codecs.c +226 -0
  154. data/vendor/libsodium/test/default/codecs.exp +28 -0
  155. data/vendor/libsodium/test/default/core3.c +42 -15
  156. data/vendor/libsodium/test/default/ed25519_convert.c +17 -0
  157. data/vendor/libsodium/test/default/kdf.c +8 -4
  158. data/vendor/libsodium/test/default/keygen.c +5 -2
  159. data/vendor/libsodium/test/default/kx.c +30 -0
  160. data/vendor/libsodium/test/default/metamorphic.c +187 -0
  161. data/vendor/libsodium/test/default/metamorphic.exp +1 -0
  162. data/vendor/libsodium/test/default/misuse.c +145 -0
  163. data/vendor/libsodium/test/default/misuse.exp +0 -0
  164. data/vendor/libsodium/test/default/onetimeauth.c +2 -0
  165. data/vendor/libsodium/test/default/{pwhash.c → pwhash_argon2i.c} +133 -106
  166. data/vendor/libsodium/test/default/{pwhash.exp → pwhash_argon2i.exp} +0 -1
  167. data/vendor/libsodium/test/default/pwhash_argon2id.c +254 -144
  168. data/vendor/libsodium/test/default/pwhash_argon2id.exp +2 -3
  169. data/vendor/libsodium/test/default/pwhash_scrypt.c +61 -18
  170. data/vendor/libsodium/test/default/pwhash_scrypt.exp +6 -0
  171. data/vendor/libsodium/test/default/randombytes.c +8 -3
  172. data/vendor/libsodium/test/default/secretbox.c +8 -0
  173. data/vendor/libsodium/test/default/secretbox2.c +5 -0
  174. data/vendor/libsodium/test/default/secretbox_easy.c +19 -1
  175. data/vendor/libsodium/test/default/secretbox_easy.exp +2 -0
  176. data/vendor/libsodium/test/default/secretbox_easy2.c +4 -0
  177. data/vendor/libsodium/test/default/secretstream.c +280 -0
  178. data/vendor/libsodium/test/default/secretstream.exp +1 -0
  179. data/vendor/libsodium/test/default/sign.c +31 -0
  180. data/vendor/libsodium/test/default/sodium_core.c +23 -1
  181. data/vendor/libsodium/test/default/sodium_core.exp +1 -1
  182. data/vendor/libsodium/test/default/sodium_utils.c +45 -56
  183. data/vendor/libsodium/test/default/sodium_utils.exp +0 -8
  184. data/vendor/libsodium/test/default/stream.c +2 -0
  185. data/vendor/libsodium/test/default/stream2.c +1 -0
  186. data/vendor/libsodium/test/default/xchacha20.c +58 -7
  187. metadata +21 -18
  188. data/vendor/libsodium/dist-build/emscripten-wasm.sh +0 -132
  189. data/vendor/libsodium/m4/pkg.m4 +0 -214
  190. data/vendor/libsodium/packaging/dotnet-core/desktop.targets +0 -16
  191. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/nacl/afternm_aes128ctr.c +0 -174
  192. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/nacl/beforenm_aes128ctr.c +0 -66
  193. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/nacl/common.h +0 -766
  194. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/nacl/consts.h +0 -28
  195. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/nacl/consts_aes128ctr.c +0 -28
  196. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/nacl/int128.h +0 -50
  197. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/nacl/int128_aes128ctr.c +0 -149
  198. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/nacl/stream_aes128ctr_nacl.c +0 -31
  199. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/nacl/xor_afternm_aes128ctr.c +0 -195
  200. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/stream_aes128ctr.c +0 -19
  201. data/vendor/libsodium/src/libsodium/include/sodium/crypto_stream_aes128ctr.h +0 -65
@@ -222,9 +222,11 @@ crypto_hash_sha512_update(crypto_hash_sha512_state *state,
222
222
 
223
223
  bitlen[1] = ((uint64_t) inlen) << 3;
224
224
  bitlen[0] = ((uint64_t) inlen) >> 61;
225
+ /* LCOV_EXCL_START */
225
226
  if ((state->count[1] += bitlen[1]) < bitlen[1]) {
226
227
  state->count[0]++;
227
228
  }
229
+ /* LCOV_EXCL_STOP */
228
230
  state->count[0] += bitlen[0];
229
231
  if (inlen < 128 - r) {
230
232
  for (i = 0; i < inlen; i++) {
@@ -1,6 +1,7 @@
1
1
 
2
2
  #include <stddef.h>
3
3
 
4
+ #include "core.h"
4
5
  #include "crypto_generichash.h"
5
6
  #include "crypto_kx.h"
6
7
  #include "crypto_scalarmult.h"
@@ -47,6 +48,9 @@ crypto_kx_client_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES],
47
48
  if (tx == NULL) {
48
49
  tx = rx;
49
50
  }
51
+ if (rx == NULL) {
52
+ sodium_misuse(); /* LCOV_EXCL_LINE */
53
+ }
50
54
  if (crypto_scalarmult(q, client_sk, server_pk) != 0) {
51
55
  return -1;
52
56
  }
@@ -85,6 +89,9 @@ crypto_kx_server_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES],
85
89
  if (tx == NULL) {
86
90
  tx = rx;
87
91
  }
92
+ if (rx == NULL) {
93
+ sodium_misuse(); /* LCOV_EXCL_LINE */
94
+ }
88
95
  if (crypto_scalarmult(q, server_sk, client_pk) != 0) {
89
96
  return -1;
90
97
  }
@@ -28,7 +28,7 @@ typedef struct poly1305_state_internal_t {
28
28
  static void
29
29
  poly1305_init(poly1305_state_internal_t *st, const unsigned char key[32])
30
30
  {
31
- /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
31
+ /* r &= 0xffffffc0ffffffc0ffffffc0fffffff - wiped after finalization */
32
32
  st->r[0] = (LOAD32_LE(&key[0])) & 0x3ffffff;
33
33
  st->r[1] = (LOAD32_LE(&key[3]) >> 2) & 0x3ffff03;
34
34
  st->r[2] = (LOAD32_LE(&key[6]) >> 4) & 0x3ffc0ff;
@@ -46,6 +46,7 @@ poly1305_init(poly1305_state_internal_t *st, const unsigned char key[32])
46
46
  t0 = LOAD64_LE(&key[0]);
47
47
  t1 = LOAD64_LE(&key[8]);
48
48
 
49
+ /* wiped after finalization */
49
50
  st->r[0] = (t0) &0xffc0fffffff;
50
51
  st->r[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffff;
51
52
  st->r[2] = ((t1 >> 24)) & 0x00ffffffc0f;
@@ -40,14 +40,14 @@ enum poly1305_state_flags_t {
40
40
  poly1305_final_shift8 = 4,
41
41
  poly1305_final_shift16 = 8,
42
42
  poly1305_final_r2_r = 16, /* use [r^2,r] for the final block */
43
- poly1305_final_r_1 = 32, /* use [r,1] for the final block */
43
+ poly1305_final_r_1 = 32 /* use [r,1] for the final block */
44
44
  };
45
45
 
46
46
  typedef struct poly1305_state_internal_t {
47
47
  union {
48
48
  uint64_t h[3];
49
49
  uint32_t hh[10];
50
- }; /* 40 bytes */
50
+ } H; /* 40 bytes */
51
51
  uint32_t R[5]; /* 20 bytes */
52
52
  uint32_t R2[5]; /* 20 bytes */
53
53
  uint32_t R4[5]; /* 20 bytes */
@@ -120,9 +120,9 @@ poly1305_init_ext(poly1305_state_internal_t *st, const unsigned char key[32],
120
120
  bytes = ~(unsigned long long) 0;
121
121
  }
122
122
  /* H = 0 */
123
- _mm_storeu_si128((xmmi *) (void *) &st->hh[0], _mm_setzero_si128());
124
- _mm_storeu_si128((xmmi *) (void *) &st->hh[4], _mm_setzero_si128());
125
- _mm_storeu_si128((xmmi *) (void *) &st->hh[8], _mm_setzero_si128());
123
+ _mm_storeu_si128((xmmi *) (void *) &st->H.hh[0], _mm_setzero_si128());
124
+ _mm_storeu_si128((xmmi *) (void *) &st->H.hh[4], _mm_setzero_si128());
125
+ _mm_storeu_si128((xmmi *) (void *) &st->H.hh[8], _mm_setzero_si128());
126
126
 
127
127
  /* clamp key */
128
128
  memcpy(&t0, key, 8);
@@ -242,9 +242,9 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m,
242
242
  bytes -= 32;
243
243
  st->flags |= poly1305_started;
244
244
  } else {
245
- T0 = _mm_loadu_si128((const xmmi *) (const void *) &st->hh[0]);
246
- T1 = _mm_loadu_si128((const xmmi *) (const void *) &st->hh[4]);
247
- T2 = _mm_loadu_si128((const xmmi *) (const void *) &st->hh[8]);
245
+ T0 = _mm_loadu_si128((const xmmi *) (const void *) &st->H.hh[0]);
246
+ T1 = _mm_loadu_si128((const xmmi *) (const void *) &st->H.hh[4]);
247
+ T2 = _mm_loadu_si128((const xmmi *) (const void *) &st->H.hh[8]);
248
248
  H0 = _mm_shuffle_epi32(T0, _MM_SHUFFLE(1, 1, 0, 0));
249
249
  H1 = _mm_shuffle_epi32(T0, _MM_SHUFFLE(3, 3, 2, 2));
250
250
  H2 = _mm_shuffle_epi32(T1, _MM_SHUFFLE(1, 1, 0, 0));
@@ -684,9 +684,9 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m,
684
684
  T4 = _mm_shuffle_epi32(H4, _MM_SHUFFLE(0, 0, 2, 0));
685
685
  T0 = _mm_unpacklo_epi64(T0, T1);
686
686
  T1 = _mm_unpacklo_epi64(T2, T3);
687
- _mm_storeu_si128((xmmi *) (void *) &st->hh[0], T0);
688
- _mm_storeu_si128((xmmi *) (void *) &st->hh[4], T1);
689
- _mm_storel_epi64((xmmi *) (void *) &st->hh[8], T4);
687
+ _mm_storeu_si128((xmmi *) (void *) &st->H.hh[0], T0);
688
+ _mm_storeu_si128((xmmi *) (void *) &st->H.hh[4], T1);
689
+ _mm_storel_epi64((xmmi *) (void *) &st->H.hh[8], T4);
690
690
  } else {
691
691
  uint32_t t0, t1, t2, t3, t4, b;
692
692
  uint64_t h0, h1, h2, g0, g1, g2, c, nc;
@@ -755,9 +755,9 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m,
755
755
  h1 = (h1 & nc) | (g1 & c);
756
756
  h2 = (h2 & nc) | (g2 & c);
757
757
 
758
- st->h[0] = h0;
759
- st->h[1] = h1;
760
- st->h[2] = h2;
758
+ st->H.h[0] = h0;
759
+ st->H.h[1] = h1;
760
+ st->H.h[2] = h2;
761
761
  }
762
762
  }
763
763
 
@@ -833,9 +833,9 @@ poly1305_finish_ext(poly1305_state_internal_t *st, const unsigned char *m,
833
833
  poly1305_blocks(st, NULL, 32);
834
834
  }
835
835
 
836
- h0 = st->h[0];
837
- h1 = st->h[1];
838
- h2 = st->h[2];
836
+ h0 = st->H.h[0];
837
+ h1 = st->H.h[1];
838
+ h2 = st->H.h[2];
839
839
 
840
840
  /* pad */
841
841
  h0 = ((h0) | (h1 << 44));
@@ -36,28 +36,6 @@
36
36
 
37
37
  static fill_segment_fn fill_segment = fill_segment_ref;
38
38
 
39
- /***************Instance and Position constructors**********/
40
- void
41
- init_block_value(block *b, uint8_t in)
42
- {
43
- memset(b->v, in, sizeof(b->v));
44
- }
45
-
46
- void
47
- copy_block(block *dst, const block *src)
48
- {
49
- memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_QWORDS_IN_BLOCK);
50
- }
51
-
52
- void
53
- xor_block(block *dst, const block *src)
54
- {
55
- int i;
56
- for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
57
- dst->v[i] ^= src->v[i];
58
- }
59
- }
60
-
61
39
  static void
62
40
  load_block(block *dst, const void *input)
63
41
  {
@@ -105,6 +83,7 @@ allocate_memory(block_region **region, uint32_t m_cost)
105
83
  if (!*region) {
106
84
  return ARGON2_MEMORY_ALLOCATION_ERROR; /* LCOV_EXCL_LINE */
107
85
  }
86
+ (*region)->base = (*region)->memory = NULL;
108
87
 
109
88
  #if defined(MAP_ANON) && defined(HAVE_MMAP)
110
89
  if ((base = mmap(NULL, memory_size, PROT_READ | PROT_WRITE,
@@ -154,12 +133,18 @@ static void clear_memory(argon2_instance_t *instance, int clear);
154
133
  static void
155
134
  clear_memory(argon2_instance_t *instance, int clear)
156
135
  {
157
- if (instance->region != NULL && clear) {
158
- /* LCOV_EXCL_START */
159
- sodium_memzero(instance->region->memory,
160
- sizeof(block) * instance->memory_blocks);
161
- /* LCOV_EXCL_STOP */
136
+ /* LCOV_EXCL_START */
137
+ if (clear) {
138
+ if (instance->region != NULL) {
139
+ sodium_memzero(instance->region->memory,
140
+ sizeof(block) * instance->memory_blocks);
141
+ }
142
+ if (instance->pseudo_rands != NULL) {
143
+ sodium_memzero(instance->pseudo_rands,
144
+ sizeof(uint64_t) * instance->segment_length);
145
+ }
162
146
  }
147
+ /* LCOV_EXCL_STOP */
163
148
  }
164
149
 
165
150
  /* Deallocates memory
@@ -170,7 +155,7 @@ static void free_memory(block_region *memory);
170
155
  static void
171
156
  free_memory(block_region *region)
172
157
  {
173
- if (region->base) {
158
+ if (region && region->base) {
174
159
  #if defined(MAP_ANON) && defined(HAVE_MMAP)
175
160
  if (munmap(region->base, region->size)) {
176
161
  return; /* LCOV_EXCL_LINE */
@@ -182,6 +167,19 @@ free_memory(block_region *region)
182
167
  free(region);
183
168
  }
184
169
 
170
+ void
171
+ free_instance(argon2_instance_t *instance, int flags)
172
+ {
173
+ /* Clear memory */
174
+ clear_memory(instance, flags & ARGON2_FLAG_CLEAR_MEMORY);
175
+
176
+ /* Deallocate the memory */
177
+ free(instance->pseudo_rands);
178
+ instance->pseudo_rands = NULL;
179
+ free_memory(instance->region);
180
+ instance->region = NULL;
181
+ }
182
+
185
183
  void
186
184
  finalize(const argon2_context *context, argon2_instance_t *instance)
187
185
  {
@@ -212,11 +210,7 @@ finalize(const argon2_context *context, argon2_instance_t *instance)
212
210
  ARGON2_BLOCK_SIZE); /* clear blockhash_bytes */
213
211
  }
214
212
 
215
- /* Clear memory */
216
- clear_memory(instance, context->flags & ARGON2_FLAG_CLEAR_PASSWORD);
217
-
218
- /* Deallocate the memory */
219
- free_memory(instance->region);
213
+ free_instance(instance, context->flags);
220
214
  }
221
215
  }
222
216
 
@@ -292,14 +286,13 @@ index_alpha(const argon2_instance_t *instance,
292
286
  return absolute_position;
293
287
  }
294
288
 
295
- int
289
+ void
296
290
  fill_memory_blocks(argon2_instance_t *instance)
297
291
  {
298
- int result;
299
292
  uint32_t r, s;
300
293
 
301
294
  if (instance == NULL || instance->lanes == 0) {
302
- return ARGON2_OK; /* LCOV_EXCL_LINE */
295
+ return; /* LCOV_EXCL_LINE */
303
296
  }
304
297
 
305
298
  for (r = 0; r < instance->passes; ++r) {
@@ -313,14 +306,10 @@ fill_memory_blocks(argon2_instance_t *instance)
313
306
  position.lane = l;
314
307
  position.slice = (uint8_t) s;
315
308
  position.index = 0;
316
- result = fill_segment(instance, position);
317
- if (ARGON2_OK != result) {
318
- return result; /* LCOV_EXCL_LINE */
319
- }
309
+ fill_segment(instance, position);
320
310
  }
321
311
  }
322
312
  }
323
- return ARGON2_OK;
324
313
  }
325
314
 
326
315
  int
@@ -510,10 +499,12 @@ initial_hash(uint8_t *blockhash, argon2_context *context, argon2_type type)
510
499
  crypto_generichash_blake2b_update(
511
500
  &BlakeHash, (const uint8_t *) context->pwd, context->pwdlen);
512
501
 
502
+ /* LCOV_EXCL_START */
513
503
  if (context->flags & ARGON2_FLAG_CLEAR_PASSWORD) {
514
- sodium_memzero(context->pwd, context->pwdlen); /* LCOV_EXCL_LINE */
515
- context->pwdlen = 0; /* LCOV_EXCL_LINE */
504
+ sodium_memzero(context->pwd, context->pwdlen);
505
+ context->pwdlen = 0;
516
506
  }
507
+ /* LCOV_EXCL_STOP */
517
508
  }
518
509
 
519
510
  STORE32_LE(value, context->saltlen);
@@ -527,8 +518,8 @@ initial_hash(uint8_t *blockhash, argon2_context *context, argon2_type type)
527
518
  STORE32_LE(value, context->secretlen);
528
519
  crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
529
520
 
521
+ /* LCOV_EXCL_START */
530
522
  if (context->secret != NULL) {
531
- /* LCOV_EXCL_START */
532
523
  crypto_generichash_blake2b_update(
533
524
  &BlakeHash, (const uint8_t *) context->secret, context->secretlen);
534
525
 
@@ -536,18 +527,18 @@ initial_hash(uint8_t *blockhash, argon2_context *context, argon2_type type)
536
527
  sodium_memzero(context->secret, context->secretlen);
537
528
  context->secretlen = 0;
538
529
  }
539
- /* LCOV_EXCL_STOP */
540
530
  }
531
+ /* LCOV_EXCL_STOP */
541
532
 
542
533
  STORE32_LE(value, context->adlen);
543
534
  crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
544
535
 
536
+ /* LCOV_EXCL_START */
545
537
  if (context->ad != NULL) {
546
- /* LCOV_EXCL_START */
547
538
  crypto_generichash_blake2b_update(
548
539
  &BlakeHash, (const uint8_t *) context->ad, context->adlen);
549
- /* LCOV_EXCL_STOP */
550
540
  }
541
+ /* LCOV_EXCL_STOP */
551
542
 
552
543
  crypto_generichash_blake2b_final(&BlakeHash, blockhash,
553
544
  ARGON2_PREHASH_DIGEST_LENGTH);
@@ -559,13 +550,20 @@ initialize(argon2_instance_t *instance, argon2_context *context)
559
550
  uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH];
560
551
  int result = ARGON2_OK;
561
552
 
562
- if (instance == NULL || context == NULL)
553
+ if (instance == NULL || context == NULL) {
563
554
  return ARGON2_INCORRECT_PARAMETER;
555
+ }
564
556
 
565
557
  /* 1. Memory allocation */
566
558
 
559
+ if ((instance->pseudo_rands = (uint64_t *)
560
+ malloc(sizeof(uint64_t) * instance->segment_length)) == NULL) {
561
+ return ARGON2_MEMORY_ALLOCATION_ERROR;
562
+ }
563
+
567
564
  result = allocate_memory(&(instance->region), instance->memory_blocks);
568
565
  if (ARGON2_OK != result) {
566
+ free_instance(instance, context->flags);
569
567
  return result;
570
568
  }
571
569
 
@@ -591,6 +589,13 @@ int
591
589
  argon2_pick_best_implementation(void)
592
590
  {
593
591
  /* LCOV_EXCL_START */
592
+ #if defined(HAVE_AVX512FINTRIN_H) && defined(HAVE_AVX2INTRIN_H) && \
593
+ defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)
594
+ if (sodium_runtime_has_avx512f()) {
595
+ fill_segment = fill_segment_avx512f;
596
+ return 0;
597
+ }
598
+ #endif
594
599
  #if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_TMMINTRIN_H) && \
595
600
  defined(HAVE_SMMINTRIN_H)
596
601
  if (sodium_runtime_has_avx2()) {
@@ -14,6 +14,8 @@
14
14
  #ifndef argon2_core_H
15
15
  #define argon2_core_H
16
16
 
17
+ #include <string.h>
18
+
17
19
  #include "argon2.h"
18
20
 
19
21
  /*************************Argon2 internal
@@ -28,6 +30,7 @@ enum argon2_ctx_constants {
28
30
  ARGON2_QWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 8,
29
31
  ARGON2_OWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 16,
30
32
  ARGON2_HWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 32,
33
+ ARGON2_512BIT_WORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 64,
31
34
 
32
35
  /* Number of pseudo-random values generated by one call to Blake in Argon2i
33
36
  to
@@ -60,13 +63,28 @@ typedef struct block_region_ {
60
63
  /*****************Functions that work with the block******************/
61
64
 
62
65
  /* Initialize each byte of the block with @in */
63
- void init_block_value(block *b, uint8_t in);
66
+ static inline void
67
+ init_block_value(block *b, uint8_t in)
68
+ {
69
+ memset(b->v, in, sizeof(b->v));
70
+ }
64
71
 
65
72
  /* Copy block @src to block @dst */
66
- void copy_block(block *dst, const block *src);
73
+ static inline void
74
+ copy_block(block *dst, const block *src)
75
+ {
76
+ memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_QWORDS_IN_BLOCK);
77
+ }
67
78
 
68
79
  /* XOR @src onto @dst bytewise */
69
- void xor_block(block *dst, const block *src);
80
+ static inline void
81
+ xor_block(block *dst, const block *src)
82
+ {
83
+ int i;
84
+ for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
85
+ dst->v[i] ^= src->v[i];
86
+ }
87
+ }
70
88
 
71
89
  /*
72
90
  * Argon2 instance: memory pointer, number of passes, amount of memory, type,
@@ -76,6 +94,7 @@ void xor_block(block *dst, const block *src);
76
94
  */
77
95
  typedef struct Argon2_instance_t {
78
96
  block_region *region; /* Memory region pointer */
97
+ uint64_t *pseudo_rands;
79
98
  uint32_t passes; /* Number of passes */
80
99
  uint32_t memory_blocks; /* Number of blocks in memory */
81
100
  uint32_t segment_length;
@@ -162,6 +181,11 @@ void fill_first_blocks(uint8_t *blockhash, const argon2_instance_t *instance);
162
181
  */
163
182
  int initialize(argon2_instance_t *instance, argon2_context *context);
164
183
 
184
+ /*
185
+ * Deallocates memory. Used on error path.
186
+ */
187
+ void free_instance(argon2_instance_t *instance, int flags);
188
+
165
189
  /*
166
190
  * XORing the last block of each lane, hashing it, making the tag. Deallocates
167
191
  * the memory.
@@ -182,15 +206,17 @@ void finalize(const argon2_context *context, argon2_instance_t *instance);
182
206
  * @param position Current position
183
207
  * @pre all block pointers must be valid
184
208
  */
185
- typedef int (*fill_segment_fn)(const argon2_instance_t *instance,
186
- argon2_position_t position);
209
+ typedef void (*fill_segment_fn)(const argon2_instance_t *instance,
210
+ argon2_position_t position);
187
211
  int argon2_pick_best_implementation(void);
188
- int fill_segment_avx2(const argon2_instance_t *instance,
189
- argon2_position_t position);
190
- int fill_segment_ssse3(const argon2_instance_t *instance,
212
+ void fill_segment_avx512f(const argon2_instance_t *instance,
213
+ argon2_position_t position);
214
+ void fill_segment_avx2(const argon2_instance_t *instance,
191
215
  argon2_position_t position);
192
- int fill_segment_ref(const argon2_instance_t *instance,
193
- argon2_position_t position);
216
+ void fill_segment_ssse3(const argon2_instance_t *instance,
217
+ argon2_position_t position);
218
+ void fill_segment_ref(const argon2_instance_t *instance,
219
+ argon2_position_t position);
194
220
 
195
221
  /*
196
222
  * Function that fills the entire memory t_cost times based on the first two
@@ -198,6 +224,6 @@ int fill_segment_ref(const argon2_instance_t *instance,
198
224
  * @param instance Pointer to the current instance
199
225
  * @return Zero if successful, -1 if memory failed to allocate
200
226
  */
201
- int fill_memory_blocks(argon2_instance_t *instance);
227
+ void fill_memory_blocks(argon2_instance_t *instance);
202
228
 
203
229
  #endif