rbnacl-libsodium 1.0.8 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +23 -0
  3. data/CHANGES.md +5 -0
  4. data/Gemfile +5 -2
  5. data/Rakefile +5 -0
  6. data/ext/rbnacl/extconf.rb +2 -1
  7. data/lib/rbnacl/libsodium.rb +8 -2
  8. data/lib/rbnacl/libsodium/version.rb +1 -1
  9. data/vendor/libsodium/AUTHORS +14 -0
  10. data/vendor/libsodium/ChangeLog +26 -0
  11. data/vendor/libsodium/LICENSE +1 -1
  12. data/vendor/libsodium/Makefile.am +1 -0
  13. data/vendor/libsodium/Makefile.in +9 -0
  14. data/vendor/libsodium/README.markdown +7 -0
  15. data/vendor/libsodium/aclocal.m4 +1 -0
  16. data/vendor/libsodium/appveyor.yml +25 -0
  17. data/vendor/libsodium/autom4te.cache/output.1 +640 -126
  18. data/vendor/libsodium/autom4te.cache/output.6 +19049 -0
  19. data/vendor/libsodium/autom4te.cache/requests +1151 -914
  20. data/vendor/libsodium/autom4te.cache/traces.1 +472 -426
  21. data/vendor/libsodium/autom4te.cache/traces.6 +3193 -0
  22. data/vendor/libsodium/builds/msvc/version.h +2 -2
  23. data/vendor/libsodium/builds/msvc/vs2010/libsodium.sln +50 -79
  24. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj +20 -8
  25. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters +208 -166
  26. data/vendor/libsodium/builds/msvc/vs2012/libsodium.sln +50 -79
  27. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj +20 -8
  28. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters +206 -164
  29. data/vendor/libsodium/builds/msvc/vs2013/libsodium.sln +52 -81
  30. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj +20 -8
  31. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters +206 -164
  32. data/vendor/libsodium/builds/msvc/vs2015/libsodium.sln +52 -81
  33. data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj +20 -8
  34. data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters +206 -164
  35. data/vendor/libsodium/configure +639 -125
  36. data/vendor/libsodium/configure.ac +94 -16
  37. data/vendor/libsodium/dist-build/Makefile.in +9 -0
  38. data/vendor/libsodium/dist-build/emscripten-symbols.def +370 -0
  39. data/vendor/libsodium/dist-build/emscripten.sh +9 -3
  40. data/vendor/libsodium/dist-build/generate-emscripten-symbols.sh +43 -0
  41. data/vendor/libsodium/libsodium-uninstalled.pc.in +1 -1
  42. data/vendor/libsodium/libsodium.pc.in +1 -1
  43. data/vendor/libsodium/libsodium.vcxproj +70 -66
  44. data/vendor/libsodium/libsodium.vcxproj.filters +204 -192
  45. data/vendor/libsodium/m4/ax_valgrind_check.m4 +190 -0
  46. data/vendor/libsodium/msvc-scripts/Makefile.in +9 -0
  47. data/vendor/libsodium/msvc-scripts/process.bat +2 -2
  48. data/vendor/libsodium/src/Makefile.in +9 -0
  49. data/vendor/libsodium/src/libsodium/Makefile.am +31 -6
  50. data/vendor/libsodium/src/libsodium/Makefile.in +238 -42
  51. data/vendor/libsodium/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +234 -38
  52. data/vendor/libsodium/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c +208 -118
  53. data/vendor/libsodium/src/libsodium/crypto_box/crypto_box_seal.c +2 -2
  54. data/vendor/libsodium/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/before_curve25519xsalsa20poly1305.c +1 -4
  55. data/vendor/libsodium/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c +1799 -1790
  56. data/vendor/libsodium/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.h +39 -39
  57. data/vendor/libsodium/src/libsodium/crypto_core/hchacha20/core_hchacha20.c +86 -0
  58. data/vendor/libsodium/src/libsodium/crypto_core/hchacha20/core_hchacha20.h +28 -0
  59. data/vendor/libsodium/src/libsodium/crypto_core/hsalsa20/ref2/core_hsalsa20.c +38 -46
  60. data/vendor/libsodium/src/libsodium/crypto_core/salsa20/ref/core_salsa20.c +47 -55
  61. data/vendor/libsodium/src/libsodium/crypto_core/salsa2012/ref/core_salsa2012.c +47 -55
  62. data/vendor/libsodium/src/libsodium/crypto_core/salsa208/ref/core_salsa208.c +47 -55
  63. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/generichash_blake2_api.c +7 -0
  64. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2-impl.h +0 -89
  65. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2.h +50 -141
  66. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-avx2.c +45 -0
  67. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-avx2.h +123 -0
  68. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-ref.c +3 -2
  69. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-sse41.c +2 -2
  70. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/{blake2b-round.h → blake2b-compress-sse41.h} +2 -28
  71. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-ssse3.c +2 -4
  72. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-ssse3.h +97 -0
  73. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-load-avx2.h +339 -0
  74. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-load-sse2.h +0 -2
  75. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-load-sse41.h +0 -2
  76. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-ref.c +29 -18
  77. data/vendor/libsodium/src/libsodium/crypto_hash/sha256/cp/hash_sha256.c +4 -43
  78. data/vendor/libsodium/src/libsodium/crypto_hash/sha512/cp/hash_sha512.c +3 -32
  79. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna.h +1 -20
  80. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna32.h +22 -41
  81. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna64.h +12 -39
  82. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c +2 -4
  83. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.h +1 -20
  84. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-core.c +570 -0
  85. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-core.h +198 -0
  86. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-encoding.c +444 -0
  87. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-encoding.h +32 -0
  88. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ref.c +229 -0
  89. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c +222 -0
  90. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-impl.h +40 -0
  91. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2.c +238 -0
  92. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2.h +251 -0
  93. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/blake2b-long.c +80 -0
  94. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/blake2b-long.h +8 -0
  95. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/blamka-round-ref.h +38 -0
  96. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/blamka-round-ssse3.h +117 -0
  97. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/pwhash_argon2i.c +164 -0
  98. data/vendor/libsodium/src/libsodium/crypto_pwhash/crypto_pwhash.c +106 -0
  99. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c +1 -1
  100. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt.h +4 -4
  101. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c +186 -186
  102. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c +2 -2
  103. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c +3 -2
  104. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/scrypt_platform.c +33 -33
  105. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c +253 -254
  106. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/donna_c64/curve25519_donna_c64.c +16 -17
  107. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/donna_c64/curve25519_donna_c64.h +1 -0
  108. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.c +11 -11
  109. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.h +1 -0
  110. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/consts_namespace.h +1 -1
  111. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe.h +3 -2
  112. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51.h +5 -3
  113. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_invert.c +41 -41
  114. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_mul.S +10 -2
  115. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_namespace.h +1 -1
  116. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_nsquare.S +4 -0
  117. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_pack.S +4 -0
  118. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe_frombytes_sandy2x.c +31 -32
  119. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder.S +4 -0
  120. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder.h +1 -1
  121. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder_base.S +4 -0
  122. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder_base.h +1 -1
  123. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder_base_namespace.h +1 -1
  124. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder_namespace.h +1 -1
  125. data/vendor/libsodium/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c +2 -6
  126. data/vendor/libsodium/src/libsodium/crypto_shorthash/siphash24/ref/shorthash_siphash24.c +8 -28
  127. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/open.c +75 -0
  128. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/afternm_aes128ctr.c +6 -6
  129. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/common.h +1 -18
  130. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/int128_aes128ctr.c +20 -20
  131. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/types.h +4 -4
  132. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/xor_afternm_aes128ctr.c +6 -6
  133. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c +56 -77
  134. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.h +1 -0
  135. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/vec/stream_chacha20_vec.h +1 -0
  136. data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/ref/stream_salsa20_ref.c +2 -8
  137. data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/ref/xor_salsa20_ref.c +2 -8
  138. data/vendor/libsodium/src/libsodium/crypto_stream/salsa2012/ref/stream_salsa2012.c +2 -8
  139. data/vendor/libsodium/src/libsodium/crypto_stream/salsa2012/ref/xor_salsa2012.c +2 -8
  140. data/vendor/libsodium/src/libsodium/crypto_stream/salsa208/ref/stream_salsa208.c +2 -8
  141. data/vendor/libsodium/src/libsodium/crypto_stream/salsa208/ref/xor_salsa208.c +2 -8
  142. data/vendor/libsodium/src/libsodium/crypto_stream/xsalsa20/ref/stream_xsalsa20.c +1 -5
  143. data/vendor/libsodium/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c +1 -5
  144. data/vendor/libsodium/src/libsodium/include/Makefile.am +3 -0
  145. data/vendor/libsodium/src/libsodium/include/Makefile.in +19 -8
  146. data/vendor/libsodium/src/libsodium/include/sodium.h +3 -0
  147. data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_aes256gcm.h +50 -0
  148. data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_chacha20poly1305.h +94 -22
  149. data/vendor/libsodium/src/libsodium/include/sodium/crypto_box_curve25519xsalsa20poly1305.h +6 -6
  150. data/vendor/libsodium/src/libsodium/include/sodium/crypto_core_hchacha20.h +35 -0
  151. data/vendor/libsodium/src/libsodium/include/sodium/crypto_generichash_blake2b.h +3 -0
  152. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash.h +89 -0
  153. data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash_argon2i.h +86 -0
  154. data/vendor/libsodium/src/libsodium/include/sodium/crypto_secretbox_xsalsa20poly1305.h +6 -6
  155. data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign_edwards25519sha512batch.h +0 -11
  156. data/vendor/libsodium/src/libsodium/include/sodium/runtime.h +3 -0
  157. data/vendor/libsodium/src/libsodium/randombytes/randombytes.c +3 -0
  158. data/vendor/libsodium/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +5 -1
  159. data/vendor/libsodium/src/libsodium/sodium/common.h +150 -0
  160. data/vendor/libsodium/src/libsodium/sodium/core.c +3 -1
  161. data/vendor/libsodium/src/libsodium/sodium/runtime.c +37 -19
  162. data/vendor/libsodium/src/libsodium/sodium/utils.c +18 -9
  163. data/vendor/libsodium/test/Makefile.in +9 -0
  164. data/vendor/libsodium/test/default/Makefile.am +10 -0
  165. data/vendor/libsodium/test/default/Makefile.in +53 -20
  166. data/vendor/libsodium/test/default/aead_aes256gcm.c +43 -17
  167. data/vendor/libsodium/test/default/aead_chacha20poly1305.c +179 -86
  168. data/vendor/libsodium/test/default/auth7.c +5 -5
  169. data/vendor/libsodium/test/default/box.c +4 -4
  170. data/vendor/libsodium/test/default/box2.c +1 -1
  171. data/vendor/libsodium/test/default/core6.c +1 -1
  172. data/vendor/libsodium/test/default/generichash.c +12 -1
  173. data/vendor/libsodium/test/default/generichash2.c +2 -2
  174. data/vendor/libsodium/test/default/generichash3.c +21 -0
  175. data/vendor/libsodium/test/default/pwhash.c +186 -168
  176. data/vendor/libsodium/test/default/pwhash.exp +11 -30
  177. data/vendor/libsodium/test/default/pwhash_scrypt.c +349 -0
  178. data/vendor/libsodium/test/default/pwhash_scrypt.exp +31 -0
  179. data/vendor/libsodium/test/default/secretbox.c +1 -1
  180. data/vendor/libsodium/test/default/secretbox2.c +1 -1
  181. data/vendor/libsodium/test/default/sign.c +15 -0
  182. data/vendor/libsodium/test/default/sodium_utils2.c +8 -3
  183. data/vendor/libsodium/test/default/sodium_utils3.c +4 -2
  184. data/vendor/libsodium/test/default/verify1.c +0 -4
  185. data/vendor/libsodium/test/quirks/quirks.h +3 -0
  186. metadata +37 -22
  187. data/vendor/libsodium/builds/msvc/vs2010/test/test.props +0 -43
  188. data/vendor/libsodium/builds/msvc/vs2010/test/test.runner.bat +0 -78
  189. data/vendor/libsodium/builds/msvc/vs2010/test/test.vcxproj +0 -244
  190. data/vendor/libsodium/builds/msvc/vs2010/test/test.vcxproj.filters +0 -192
  191. data/vendor/libsodium/builds/msvc/vs2012/test/test.props +0 -43
  192. data/vendor/libsodium/builds/msvc/vs2012/test/test.runner.bat +0 -78
  193. data/vendor/libsodium/builds/msvc/vs2012/test/test.vcxproj +0 -244
  194. data/vendor/libsodium/builds/msvc/vs2012/test/test.vcxproj.filters +0 -192
  195. data/vendor/libsodium/builds/msvc/vs2013/test/test.props +0 -43
  196. data/vendor/libsodium/builds/msvc/vs2013/test/test.runner.bat +0 -78
  197. data/vendor/libsodium/builds/msvc/vs2013/test/test.vcxproj +0 -244
  198. data/vendor/libsodium/builds/msvc/vs2013/test/test.vcxproj.filters +0 -192
  199. data/vendor/libsodium/builds/msvc/vs2015/test/test.props +0 -43
  200. data/vendor/libsodium/builds/msvc/vs2015/test/test.runner.bat +0 -78
  201. data/vendor/libsodium/builds/msvc/vs2015/test/test.vcxproj +0 -244
  202. data/vendor/libsodium/builds/msvc/vs2015/test/test.vcxproj.filters +0 -192
  203. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sysendian.h +0 -146
  204. data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/common_aes128ctr.c +0 -64
@@ -3,6 +3,7 @@
3
3
  * AES256-GCM, based on original code by Romain Dolbeau
4
4
  */
5
5
 
6
+ #include <errno.h>
6
7
  #include <stdint.h>
7
8
  #include <stdlib.h>
8
9
  #include <string.h>
@@ -21,6 +22,10 @@
21
22
 
22
23
  #include <immintrin.h>
23
24
 
25
+ #ifndef ENOSYS
26
+ # define ENOSYS ENXIO
27
+ #endif
28
+
24
29
  #if defined(__INTEL_COMPILER) || defined(_bswap64)
25
30
  #elif defined(_MSC_VER)
26
31
  # define _bswap64(a) _byteswap_uint64(a)
@@ -124,8 +129,8 @@ aesni_encrypt1(unsigned char *out, __m128i nv, const __m128i *rkeys)
124
129
  }
125
130
 
126
131
  /** multiple-blocks-at-once AES encryption with AES-NI ;
127
- on Haswell, aesenc as a latency of 7 and a througput of 1
128
- so the sequence of aesenc should be bubble-free, if you
132
+ on Haswell, aesenc as a latency of 7 and a throughput of 1
133
+ so the sequence of aesenc should be bubble-free if you
129
134
  have at least 8 blocks. Let's build an arbitratry-sized
130
135
  function */
131
136
  /* Step 1 : loading the nonce */
@@ -504,12 +509,13 @@ crypto_aead_aes256gcm_beforenm(crypto_aead_aes256gcm_state *ctx_,
504
509
  }
505
510
 
506
511
  int
507
- crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c, unsigned long long *clen,
508
- const unsigned char *m, unsigned long long mlen,
509
- const unsigned char *ad, unsigned long long adlen,
510
- const unsigned char *nsec,
511
- const unsigned char *npub,
512
- const crypto_aead_aes256gcm_state *ctx_)
512
+ crypto_aead_aes256gcm_encrypt_detached_afternm(unsigned char *c,
513
+ unsigned char *mac, unsigned long long *maclen_p,
514
+ const unsigned char *m, unsigned long long mlen,
515
+ const unsigned char *ad, unsigned long long adlen,
516
+ const unsigned char *nsec,
517
+ const unsigned char *npub,
518
+ const crypto_aead_aes256gcm_state *ctx_)
513
519
  {
514
520
  const __m128i rev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
515
521
  const context *ctx = (const context *) ctx_;
@@ -526,7 +532,7 @@ crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c, unsigned long long *clen
526
532
 
527
533
  (void) nsec;
528
534
  memcpy(H, ctx->H, sizeof H);
529
- if (mlen > 16ULL * (1ULL << 32)) {
535
+ if (mlen > 16ULL * ((1ULL << 32) - 2)) {
530
536
  abort(); /* LCOV_EXCL_LINE */
531
537
  }
532
538
  memcpy(&n2[0], npub, 3 * 4);
@@ -614,21 +620,40 @@ crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c, unsigned long long *clen
614
620
  addmul(accum, fb, 16, H);
615
621
 
616
622
  for (i = 0; i < 16; ++i) {
617
- c[i + mlen] = T[i] ^ accum[15 - i];
623
+ mac[i] = T[i] ^ accum[15 - i];
618
624
  }
619
- if (clen != NULL) {
620
- *clen = mlen + 16;
625
+ if (maclen_p != NULL) {
626
+ *maclen_p = 16;
621
627
  }
622
628
  return 0;
623
629
  }
624
630
 
625
631
  int
626
- crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen_p,
627
- unsigned char *nsec,
628
- const unsigned char *c, unsigned long long clen,
632
+ crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c, unsigned long long *clen_p,
633
+ const unsigned char *m, unsigned long long mlen,
629
634
  const unsigned char *ad, unsigned long long adlen,
635
+ const unsigned char *nsec,
630
636
  const unsigned char *npub,
631
637
  const crypto_aead_aes256gcm_state *ctx_)
638
+ {
639
+ int ret = crypto_aead_aes256gcm_encrypt_detached_afternm(c,
640
+ c + mlen, NULL,
641
+ m, mlen,
642
+ ad, adlen,
643
+ nsec, npub, ctx_);
644
+ if (clen_p != NULL) {
645
+ *clen_p = mlen + crypto_aead_aes256gcm_ABYTES;
646
+ }
647
+ return ret;
648
+ }
649
+
650
+ int
651
+ crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned char *nsec,
652
+ const unsigned char *c, unsigned long long clen,
653
+ const unsigned char *mac,
654
+ const unsigned char *ad, unsigned long long adlen,
655
+ const unsigned char *npub,
656
+ const crypto_aead_aes256gcm_state *ctx_)
632
657
  {
633
658
  const __m128i rev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
634
659
  const context *ctx = (const context *) ctx_;
@@ -645,20 +670,15 @@ crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen
645
670
  CRYPTO_ALIGN(16) unsigned char fb[16];
646
671
 
647
672
  (void) nsec;
648
- if (clen > 16ULL * (1ULL << 32) - 16ULL) {
673
+ if (clen > 16ULL * (1ULL << 32)) {
649
674
  abort(); /* LCOV_EXCL_LINE */
650
675
  }
651
- if (mlen_p != NULL) {
652
- *mlen_p = 0U;
653
- }
654
- if (clen < 16) {
655
- return -1;
656
- }
657
- mlen = clen - 16;
676
+ mlen = clen;
658
677
 
659
678
  memcpy(&n2[0], npub, 3 * 4);
660
679
  n2[3] = 0x01000000;
661
680
  aesni_encrypt1(T, _mm_load_si128((const __m128i *) n2), rkeys);
681
+
662
682
  {
663
683
  uint64_t x;
664
684
  x = _bswap64((uint64_t)(8 * adlen));
@@ -666,6 +686,7 @@ crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen
666
686
  x = _bswap64((uint64_t)(8 * mlen));
667
687
  memcpy(&fb[8], &x, sizeof x);
668
688
  }
689
+
669
690
  memcpy(H, ctx->H, sizeof H);
670
691
  Hv = _mm_shuffle_epi8(_mm_load_si128((const __m128i *) H), rev);
671
692
  _mm_store_si128((__m128i *) H, Hv);
@@ -752,6 +773,7 @@ crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen
752
773
  } \
753
774
  } \
754
775
  } while(0)
776
+
755
777
  n2[3] &= 0x00ffffff;
756
778
 
757
779
  COUNTER_INC2(n2);
@@ -762,9 +784,10 @@ crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen
762
784
  unsigned char d = 0;
763
785
 
764
786
  for (i = 0; i < 16; i++) {
765
- d |= (c[i + mlen] ^ (T[i] ^ accum[15 - i]));
787
+ d |= (mac[i] ^ (T[i] ^ accum[15 - i]));
766
788
  }
767
789
  if (d != 0) {
790
+ memset(m, 0, mlen);
768
791
  return -1;
769
792
  }
770
793
  }
@@ -773,10 +796,54 @@ crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen
773
796
  LOOPDRND128;
774
797
  LOOPDRMD128;
775
798
 
799
+ return 0;
800
+ }
801
+
802
+ int
803
+ crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen_p,
804
+ unsigned char *nsec,
805
+ const unsigned char *c, unsigned long long clen,
806
+ const unsigned char *ad, unsigned long long adlen,
807
+ const unsigned char *npub,
808
+ const crypto_aead_aes256gcm_state *ctx_)
809
+ {
810
+ unsigned long long mlen = 0ULL;
811
+ int ret = -1;
812
+
813
+ if (clen >= crypto_aead_aes256gcm_ABYTES) {
814
+ ret = crypto_aead_aes256gcm_decrypt_detached_afternm
815
+ (m, nsec, c, clen - crypto_aead_aes256gcm_ABYTES,
816
+ c + clen - crypto_aead_aes256gcm_ABYTES,
817
+ ad, adlen, npub, ctx_);
818
+ }
776
819
  if (mlen_p != NULL) {
820
+ if (ret == 0) {
821
+ mlen = clen - crypto_aead_aes256gcm_ABYTES;
822
+ }
777
823
  *mlen_p = mlen;
778
824
  }
779
- return 0;
825
+ return ret;
826
+ }
827
+
828
+ int
829
+ crypto_aead_aes256gcm_encrypt_detached(unsigned char *c,
830
+ unsigned char *mac,
831
+ unsigned long long *maclen_p,
832
+ const unsigned char *m,
833
+ unsigned long long mlen,
834
+ const unsigned char *ad,
835
+ unsigned long long adlen,
836
+ const unsigned char *nsec,
837
+ const unsigned char *npub,
838
+ const unsigned char *k)
839
+ {
840
+ CRYPTO_ALIGN(16) crypto_aead_aes256gcm_state ctx;
841
+
842
+ crypto_aead_aes256gcm_beforenm(&ctx, k);
843
+
844
+ return crypto_aead_aes256gcm_encrypt_detached_afternm
845
+ (c, mac, maclen_p, m, mlen, ad, adlen, nsec, npub,
846
+ (const crypto_aead_aes256gcm_state *) &ctx);
780
847
  }
781
848
 
782
849
  int
@@ -790,7 +857,7 @@ crypto_aead_aes256gcm_encrypt(unsigned char *c,
790
857
  const unsigned char *npub,
791
858
  const unsigned char *k)
792
859
  {
793
- crypto_aead_aes256gcm_state ctx;
860
+ CRYPTO_ALIGN(16) crypto_aead_aes256gcm_state ctx;
794
861
 
795
862
  crypto_aead_aes256gcm_beforenm(&ctx, k);
796
863
 
@@ -799,6 +866,26 @@ crypto_aead_aes256gcm_encrypt(unsigned char *c,
799
866
  (const crypto_aead_aes256gcm_state *) &ctx);
800
867
  }
801
868
 
869
+ int
870
+ crypto_aead_aes256gcm_decrypt_detached(unsigned char *m,
871
+ unsigned char *nsec,
872
+ const unsigned char *c,
873
+ unsigned long long clen,
874
+ const unsigned char *mac,
875
+ const unsigned char *ad,
876
+ unsigned long long adlen,
877
+ const unsigned char *npub,
878
+ const unsigned char *k)
879
+ {
880
+ CRYPTO_ALIGN(16) crypto_aead_aes256gcm_state ctx;
881
+
882
+ crypto_aead_aes256gcm_beforenm(&ctx, k);
883
+
884
+ return crypto_aead_aes256gcm_decrypt_detached_afternm
885
+ (m, nsec, c, clen, mac, ad, adlen, npub,
886
+ (const crypto_aead_aes256gcm_state *) &ctx);
887
+ }
888
+
802
889
  int
803
890
  crypto_aead_aes256gcm_decrypt(unsigned char *m,
804
891
  unsigned long long *mlen_p,
@@ -810,13 +897,13 @@ crypto_aead_aes256gcm_decrypt(unsigned char *m,
810
897
  const unsigned char *npub,
811
898
  const unsigned char *k)
812
899
  {
813
- crypto_aead_aes256gcm_state ctx;
900
+ CRYPTO_ALIGN(16) crypto_aead_aes256gcm_state ctx;
814
901
 
815
902
  crypto_aead_aes256gcm_beforenm(&ctx, k);
816
903
 
817
904
  return crypto_aead_aes256gcm_decrypt_afternm
818
905
  (m, mlen_p, nsec, c, clen, ad, adlen, npub,
819
- (const crypto_aead_aes256gcm_state *) &ctx);
906
+ (const crypto_aead_aes256gcm_state *) &ctx);
820
907
  }
821
908
 
822
909
  int
@@ -825,6 +912,125 @@ crypto_aead_aes256gcm_is_available(void)
825
912
  return sodium_runtime_has_pclmul() & sodium_runtime_has_aesni();
826
913
  }
827
914
 
915
+ #else
916
+
917
+ int
918
+ crypto_aead_aes256gcm_encrypt_detached(unsigned char *c,
919
+ unsigned char *mac,
920
+ unsigned long long *maclen_p,
921
+ const unsigned char *m,
922
+ unsigned long long mlen,
923
+ const unsigned char *ad,
924
+ unsigned long long adlen,
925
+ const unsigned char *nsec,
926
+ const unsigned char *npub,
927
+ const unsigned char *k)
928
+ {
929
+ errno = ENOSYS;
930
+ return -1;
931
+ }
932
+
933
+ int
934
+ crypto_aead_aes256gcm_encrypt(unsigned char *c, unsigned long long *clen_p,
935
+ const unsigned char *m, unsigned long long mlen,
936
+ const unsigned char *ad, unsigned long long adlen,
937
+ const unsigned char *nsec, const unsigned char *npub,
938
+ const unsigned char *k)
939
+ {
940
+ errno = ENOSYS;
941
+ return -1;
942
+ }
943
+
944
+ int
945
+ crypto_aead_aes256gcm_decrypt_detached(unsigned char *m,
946
+ unsigned char *nsec,
947
+ const unsigned char *c,
948
+ unsigned long long clen,
949
+ const unsigned char *mac,
950
+ const unsigned char *ad,
951
+ unsigned long long adlen,
952
+ const unsigned char *npub,
953
+ const unsigned char *k)
954
+ {
955
+ errno = ENOSYS;
956
+ return -1;
957
+ }
958
+
959
+ int
960
+ crypto_aead_aes256gcm_decrypt(unsigned char *m, unsigned long long *mlen_p,
961
+ unsigned char *nsec, const unsigned char *c,
962
+ unsigned long long clen, const unsigned char *ad,
963
+ unsigned long long adlen, const unsigned char *npub,
964
+ const unsigned char *k)
965
+ {
966
+ errno = ENOSYS;
967
+ return -1;
968
+ }
969
+
970
+ int
971
+ crypto_aead_aes256gcm_beforenm(crypto_aead_aes256gcm_state *ctx_,
972
+ const unsigned char *k)
973
+ {
974
+ errno = ENOSYS;
975
+ return -1;
976
+ }
977
+
978
+ int
979
+ crypto_aead_aes256gcm_encrypt_detached_afternm(unsigned char *c,
980
+ unsigned char *mac, unsigned long long *maclen_p,
981
+ const unsigned char *m, unsigned long long mlen,
982
+ const unsigned char *ad, unsigned long long adlen,
983
+ const unsigned char *nsec,
984
+ const unsigned char *npub,
985
+ const crypto_aead_aes256gcm_state *ctx_)
986
+ {
987
+ errno = ENOSYS;
988
+ return -1;
989
+ }
990
+
991
+ int
992
+ crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c, unsigned long long *clen_p,
993
+ const unsigned char *m, unsigned long long mlen,
994
+ const unsigned char *ad, unsigned long long adlen,
995
+ const unsigned char *nsec, const unsigned char *npub,
996
+ const crypto_aead_aes256gcm_state *ctx_)
997
+ {
998
+ errno = ENOSYS;
999
+ return -1;
1000
+ }
1001
+
1002
+ int
1003
+ crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned char *nsec,
1004
+ const unsigned char *c, unsigned long long clen,
1005
+ const unsigned char *mac,
1006
+ const unsigned char *ad, unsigned long long adlen,
1007
+ const unsigned char *npub,
1008
+ const crypto_aead_aes256gcm_state *ctx_)
1009
+ {
1010
+ errno = ENOSYS;
1011
+ return -1;
1012
+ }
1013
+
1014
+ int
1015
+ crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen_p,
1016
+ unsigned char *nsec,
1017
+ const unsigned char *c, unsigned long long clen,
1018
+ const unsigned char *ad, unsigned long long adlen,
1019
+ const unsigned char *npub,
1020
+ const crypto_aead_aes256gcm_state *ctx_)
1021
+ {
1022
+ errno = ENOSYS;
1023
+ return -1;
1024
+ }
1025
+
1026
+ int
1027
+ crypto_aead_aes256gcm_is_available(void)
1028
+ {
1029
+ return 0;
1030
+ }
1031
+
1032
+ #endif
1033
+
828
1034
  size_t
829
1035
  crypto_aead_aes256gcm_keybytes(void)
830
1036
  {
@@ -854,13 +1060,3 @@ crypto_aead_aes256gcm_statebytes(void)
854
1060
  {
855
1061
  return (sizeof(crypto_aead_aes256gcm_state) + (size_t) 15U) & ~(size_t) 15U;
856
1062
  }
857
-
858
- #else
859
-
860
- int
861
- crypto_aead_aes256gcm_is_available(void)
862
- {
863
- return 0;
864
- }
865
-
866
- #endif
@@ -1,4 +1,5 @@
1
1
 
2
+ #include <stdint.h>
2
3
  #include <stdlib.h>
3
4
  #include <limits.h>
4
5
  #include <string.h>
@@ -9,98 +10,98 @@
9
10
  #include "crypto_verify_16.h"
10
11
  #include "utils.h"
11
12
 
12
- static unsigned char _pad0[16];
13
+ #include "../../sodium/common.h"
13
14
 
14
- static inline void
15
- _u64_le_from_ull(unsigned char out[8U], unsigned long long x)
16
- {
17
- out[0] = (unsigned char) (x & 0xff); x >>= 8;
18
- out[1] = (unsigned char) (x & 0xff); x >>= 8;
19
- out[2] = (unsigned char) (x & 0xff); x >>= 8;
20
- out[3] = (unsigned char) (x & 0xff); x >>= 8;
21
- out[4] = (unsigned char) (x & 0xff); x >>= 8;
22
- out[5] = (unsigned char) (x & 0xff); x >>= 8;
23
- out[6] = (unsigned char) (x & 0xff); x >>= 8;
24
- out[7] = (unsigned char) (x & 0xff);
25
- }
15
+ static const unsigned char _pad0[16] = { 0 };
26
16
 
27
17
  int
28
- crypto_aead_chacha20poly1305_encrypt(unsigned char *c,
29
- unsigned long long *clen_p,
30
- const unsigned char *m,
31
- unsigned long long mlen,
32
- const unsigned char *ad,
33
- unsigned long long adlen,
34
- const unsigned char *nsec,
35
- const unsigned char *npub,
36
- const unsigned char *k)
18
+ crypto_aead_chacha20poly1305_encrypt_detached(unsigned char *c,
19
+ unsigned char *mac,
20
+ unsigned long long *maclen_p,
21
+ const unsigned char *m,
22
+ unsigned long long mlen,
23
+ const unsigned char *ad,
24
+ unsigned long long adlen,
25
+ const unsigned char *nsec,
26
+ const unsigned char *npub,
27
+ const unsigned char *k)
37
28
  {
38
29
  crypto_onetimeauth_poly1305_state state;
39
30
  unsigned char block0[64U];
40
31
  unsigned char slen[8U];
41
32
 
42
33
  (void) nsec;
43
- /* LCOV_EXCL_START */
44
- #ifdef ULONG_LONG_MAX
45
- if (mlen > ULONG_LONG_MAX - crypto_aead_chacha20poly1305_ABYTES) {
46
- if (clen_p != NULL) {
47
- *clen_p = 0ULL;
48
- }
49
- return -1;
50
- }
51
- #endif
52
- /* LCOV_EXCL_STOP */
53
-
54
34
  crypto_stream_chacha20(block0, sizeof block0, npub, k);
55
35
  crypto_onetimeauth_poly1305_init(&state, block0);
56
36
  sodium_memzero(block0, sizeof block0);
57
37
 
58
38
  crypto_onetimeauth_poly1305_update(&state, ad, adlen);
59
- _u64_le_from_ull(slen, adlen);
39
+ STORE64_LE(slen, (uint64_t) adlen);
60
40
  crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
61
41
 
62
42
  crypto_stream_chacha20_xor_ic(c, m, mlen, npub, 1U, k);
63
43
 
64
44
  crypto_onetimeauth_poly1305_update(&state, c, mlen);
65
- _u64_le_from_ull(slen, mlen);
45
+ STORE64_LE(slen, (uint64_t) mlen);
66
46
  crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
67
47
 
68
- crypto_onetimeauth_poly1305_final(&state, c + mlen);
48
+ crypto_onetimeauth_poly1305_final(&state, mac);
69
49
  sodium_memzero(&state, sizeof state);
70
50
 
71
- if (clen_p != NULL) {
72
- *clen_p = mlen + crypto_aead_chacha20poly1305_ABYTES;
51
+ if (maclen_p != NULL) {
52
+ *maclen_p = crypto_aead_chacha20poly1305_ABYTES;
73
53
  }
74
54
  return 0;
75
55
  }
76
56
 
77
57
  int
78
- crypto_aead_chacha20poly1305_ietf_encrypt(unsigned char *c,
79
- unsigned long long *clen_p,
80
- const unsigned char *m,
81
- unsigned long long mlen,
82
- const unsigned char *ad,
83
- unsigned long long adlen,
84
- const unsigned char *nsec,
85
- const unsigned char *npub,
86
- const unsigned char *k)
58
+ crypto_aead_chacha20poly1305_encrypt(unsigned char *c,
59
+ unsigned long long *clen_p,
60
+ const unsigned char *m,
61
+ unsigned long long mlen,
62
+ const unsigned char *ad,
63
+ unsigned long long adlen,
64
+ const unsigned char *nsec,
65
+ const unsigned char *npub,
66
+ const unsigned char *k)
67
+ {
68
+ unsigned long long clen = 0ULL;
69
+ int ret;
70
+
71
+ if (mlen > UINT64_MAX - crypto_aead_chacha20poly1305_ABYTES) {
72
+ abort(); /* LCOV_EXCL_LINE */
73
+ }
74
+ ret = crypto_aead_chacha20poly1305_encrypt_detached(c,
75
+ c + mlen, NULL,
76
+ m, mlen,
77
+ ad, adlen,
78
+ nsec, npub, k);
79
+ if (clen_p != NULL) {
80
+ if (ret == 0) {
81
+ clen = mlen + crypto_aead_chacha20poly1305_ABYTES;
82
+ }
83
+ *clen_p = clen;
84
+ }
85
+ return ret;
86
+ }
87
+
88
+ int
89
+ crypto_aead_chacha20poly1305_ietf_encrypt_detached(unsigned char *c,
90
+ unsigned char *mac,
91
+ unsigned long long *maclen_p,
92
+ const unsigned char *m,
93
+ unsigned long long mlen,
94
+ const unsigned char *ad,
95
+ unsigned long long adlen,
96
+ const unsigned char *nsec,
97
+ const unsigned char *npub,
98
+ const unsigned char *k)
87
99
  {
88
100
  crypto_onetimeauth_poly1305_state state;
89
101
  unsigned char block0[64U];
90
102
  unsigned char slen[8U];
91
103
 
92
104
  (void) nsec;
93
- /* LCOV_EXCL_START */
94
- #ifdef ULONG_LONG_MAX
95
- if (mlen > ULONG_LONG_MAX - crypto_aead_chacha20poly1305_ABYTES) {
96
- if (clen_p != NULL) {
97
- *clen_p = 0ULL;
98
- }
99
- return -1;
100
- }
101
- #endif
102
- /* LCOV_EXCL_STOP */
103
-
104
105
  crypto_stream_chacha20_ietf(block0, sizeof block0, npub, k);
105
106
  crypto_onetimeauth_poly1305_init(&state, block0);
106
107
  sodium_memzero(block0, sizeof block0);
@@ -113,102 +114,148 @@ crypto_aead_chacha20poly1305_ietf_encrypt(unsigned char *c,
113
114
  crypto_onetimeauth_poly1305_update(&state, c, mlen);
114
115
  crypto_onetimeauth_poly1305_update(&state, _pad0, (0x10 - mlen) & 0xf);
115
116
 
116
- _u64_le_from_ull(slen, adlen);
117
+ STORE64_LE(slen, (uint64_t) adlen);
117
118
  crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
118
119
 
119
- _u64_le_from_ull(slen, mlen);
120
+ STORE64_LE(slen, (uint64_t) mlen);
120
121
  crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
121
122
 
122
- crypto_onetimeauth_poly1305_final(&state, c + mlen);
123
+ crypto_onetimeauth_poly1305_final(&state, mac);
123
124
  sodium_memzero(&state, sizeof state);
124
125
 
125
- if (clen_p != NULL) {
126
- *clen_p = mlen + crypto_aead_chacha20poly1305_ABYTES;
126
+ if (maclen_p != NULL) {
127
+ *maclen_p = crypto_aead_chacha20poly1305_ietf_ABYTES;
127
128
  }
128
129
  return 0;
129
130
  }
130
131
 
131
132
  int
132
- crypto_aead_chacha20poly1305_decrypt(unsigned char *m,
133
- unsigned long long *mlen_p,
134
- unsigned char *nsec,
135
- const unsigned char *c,
136
- unsigned long long clen,
137
- const unsigned char *ad,
138
- unsigned long long adlen,
139
- const unsigned char *npub,
140
- const unsigned char *k)
133
+ crypto_aead_chacha20poly1305_ietf_encrypt(unsigned char *c,
134
+ unsigned long long *clen_p,
135
+ const unsigned char *m,
136
+ unsigned long long mlen,
137
+ const unsigned char *ad,
138
+ unsigned long long adlen,
139
+ const unsigned char *nsec,
140
+ const unsigned char *npub,
141
+ const unsigned char *k)
142
+ {
143
+ unsigned long long clen = 0ULL;
144
+ int ret;
145
+
146
+ if (mlen > UINT64_MAX - crypto_aead_chacha20poly1305_ietf_ABYTES) {
147
+ abort(); /* LCOV_EXCL_LINE */
148
+ }
149
+ ret = crypto_aead_chacha20poly1305_ietf_encrypt_detached(c,
150
+ c + mlen, NULL,
151
+ m, mlen,
152
+ ad, adlen,
153
+ nsec, npub, k);
154
+ if (clen_p != NULL) {
155
+ if (ret == 0) {
156
+ clen = mlen + crypto_aead_chacha20poly1305_ietf_ABYTES;
157
+ }
158
+ *clen_p = clen;
159
+ }
160
+ return ret;
161
+ }
162
+
163
+ int
164
+ crypto_aead_chacha20poly1305_decrypt_detached(unsigned char *m,
165
+ unsigned char *nsec,
166
+ const unsigned char *c,
167
+ unsigned long long clen,
168
+ const unsigned char *mac,
169
+ const unsigned char *ad,
170
+ unsigned long long adlen,
171
+ const unsigned char *npub,
172
+ const unsigned char *k)
141
173
  {
142
174
  crypto_onetimeauth_poly1305_state state;
143
175
  unsigned char block0[64U];
144
176
  unsigned char slen[8U];
145
- unsigned char mac[crypto_aead_chacha20poly1305_ABYTES];
177
+ unsigned char computed_mac[crypto_aead_chacha20poly1305_ABYTES];
146
178
  unsigned long long mlen;
147
179
  int ret;
148
180
 
149
181
  (void) nsec;
150
- if (mlen_p != NULL) {
151
- *mlen_p = 0ULL;
152
- }
153
- if (clen < crypto_aead_chacha20poly1305_ABYTES) {
154
- return -1;
155
- }
156
182
  crypto_stream_chacha20(block0, sizeof block0, npub, k);
157
183
  crypto_onetimeauth_poly1305_init(&state, block0);
158
184
  sodium_memzero(block0, sizeof block0);
159
185
 
160
186
  crypto_onetimeauth_poly1305_update(&state, ad, adlen);
161
- _u64_le_from_ull(slen, adlen);
187
+ STORE64_LE(slen, (uint64_t) adlen);
162
188
  crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
163
189
 
164
- mlen = clen - crypto_aead_chacha20poly1305_ABYTES;
190
+ mlen = clen;
165
191
  crypto_onetimeauth_poly1305_update(&state, c, mlen);
166
- _u64_le_from_ull(slen, mlen);
192
+ STORE64_LE(slen, (uint64_t) mlen);
167
193
  crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
168
194
 
169
- crypto_onetimeauth_poly1305_final(&state, mac);
195
+ crypto_onetimeauth_poly1305_final(&state, computed_mac);
170
196
  sodium_memzero(&state, sizeof state);
171
197
 
172
- (void) sizeof(int[sizeof mac == 16U ? 1 : -1]);
173
- ret = crypto_verify_16(mac, c + mlen);
174
- sodium_memzero(mac, sizeof mac);
198
+ (void) sizeof(int[sizeof computed_mac == 16U ? 1 : -1]);
199
+ ret = crypto_verify_16(computed_mac, mac);
200
+ sodium_memzero(computed_mac, sizeof computed_mac);
175
201
  if (ret != 0) {
176
202
  memset(m, 0, mlen);
177
203
  return -1;
178
204
  }
179
- crypto_stream_chacha20_xor_ic
180
- (m, c, mlen, npub, 1U, k);
205
+ crypto_stream_chacha20_xor_ic(m, c, mlen, npub, 1U, k);
206
+
207
+ return 0;
208
+ }
209
+
210
+ int
211
+ crypto_aead_chacha20poly1305_decrypt(unsigned char *m,
212
+ unsigned long long *mlen_p,
213
+ unsigned char *nsec,
214
+ const unsigned char *c,
215
+ unsigned long long clen,
216
+ const unsigned char *ad,
217
+ unsigned long long adlen,
218
+ const unsigned char *npub,
219
+ const unsigned char *k)
220
+ {
221
+ unsigned long long mlen = 0ULL;
222
+ int ret = -1;
223
+
224
+ if (clen >= crypto_aead_chacha20poly1305_ABYTES) {
225
+ ret = crypto_aead_chacha20poly1305_decrypt_detached
226
+ (m, nsec,
227
+ c, clen - crypto_aead_chacha20poly1305_ABYTES,
228
+ c + clen - crypto_aead_chacha20poly1305_ABYTES,
229
+ ad, adlen, npub, k);
230
+ }
181
231
  if (mlen_p != NULL) {
232
+ if (ret == 0) {
233
+ mlen = clen - crypto_aead_chacha20poly1305_ABYTES;
234
+ }
182
235
  *mlen_p = mlen;
183
236
  }
184
- return 0;
237
+ return ret;
185
238
  }
186
239
 
187
240
  int
188
- crypto_aead_chacha20poly1305_ietf_decrypt(unsigned char *m,
189
- unsigned long long *mlen_p,
190
- unsigned char *nsec,
191
- const unsigned char *c,
192
- unsigned long long clen,
193
- const unsigned char *ad,
194
- unsigned long long adlen,
195
- const unsigned char *npub,
196
- const unsigned char *k)
241
+ crypto_aead_chacha20poly1305_ietf_decrypt_detached(unsigned char *m,
242
+ unsigned char *nsec,
243
+ const unsigned char *c,
244
+ unsigned long long clen,
245
+ const unsigned char *mac,
246
+ const unsigned char *ad,
247
+ unsigned long long adlen,
248
+ const unsigned char *npub,
249
+ const unsigned char *k)
197
250
  {
198
251
  crypto_onetimeauth_poly1305_state state;
199
252
  unsigned char block0[64U];
200
253
  unsigned char slen[8U];
201
- unsigned char mac[crypto_aead_chacha20poly1305_ABYTES];
254
+ unsigned char computed_mac[crypto_aead_chacha20poly1305_ietf_ABYTES];
202
255
  unsigned long long mlen;
203
256
  int ret;
204
257
 
205
258
  (void) nsec;
206
- if (mlen_p != NULL) {
207
- *mlen_p = 0ULL;
208
- }
209
- if (clen < crypto_aead_chacha20poly1305_ABYTES) {
210
- return -1;
211
- }
212
259
  crypto_stream_chacha20_ietf(block0, sizeof block0, npub, k);
213
260
  crypto_onetimeauth_poly1305_init(&state, block0);
214
261
  sodium_memzero(block0, sizeof block0);
@@ -216,31 +263,79 @@ crypto_aead_chacha20poly1305_ietf_decrypt(unsigned char *m,
216
263
  crypto_onetimeauth_poly1305_update(&state, ad, adlen);
217
264
  crypto_onetimeauth_poly1305_update(&state, _pad0, (0x10 - adlen) & 0xf);
218
265
 
219
- mlen = clen - crypto_aead_chacha20poly1305_ABYTES;
266
+ mlen = clen;
220
267
  crypto_onetimeauth_poly1305_update(&state, c, mlen);
221
268
  crypto_onetimeauth_poly1305_update(&state, _pad0, (0x10 - mlen) & 0xf);
222
269
 
223
- _u64_le_from_ull(slen, adlen);
270
+ STORE64_LE(slen, (uint64_t) adlen);
224
271
  crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
225
272
 
226
- _u64_le_from_ull(slen, mlen);
273
+ STORE64_LE(slen, (uint64_t) mlen);
227
274
  crypto_onetimeauth_poly1305_update(&state, slen, sizeof slen);
228
275
 
229
- crypto_onetimeauth_poly1305_final(&state, mac);
276
+ crypto_onetimeauth_poly1305_final(&state, computed_mac);
230
277
  sodium_memzero(&state, sizeof state);
231
278
 
232
- (void) sizeof(int[sizeof mac == 16U ? 1 : -1]);
233
- ret = crypto_verify_16(mac, c + mlen);
234
- sodium_memzero(mac, sizeof mac);
279
+ (void) sizeof(int[sizeof computed_mac == 16U ? 1 : -1]);
280
+ ret = crypto_verify_16(computed_mac, mac);
281
+ sodium_memzero(computed_mac, sizeof computed_mac);
235
282
  if (ret != 0) {
236
283
  memset(m, 0, mlen);
237
284
  return -1;
238
285
  }
239
286
  crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, npub, 1U, k);
287
+
288
+ return 0;
289
+ }
290
+
291
+ int
292
+ crypto_aead_chacha20poly1305_ietf_decrypt(unsigned char *m,
293
+ unsigned long long *mlen_p,
294
+ unsigned char *nsec,
295
+ const unsigned char *c,
296
+ unsigned long long clen,
297
+ const unsigned char *ad,
298
+ unsigned long long adlen,
299
+ const unsigned char *npub,
300
+ const unsigned char *k)
301
+ {
302
+ unsigned long long mlen = 0ULL;
303
+ int ret = -1;
304
+
305
+ if (clen >= crypto_aead_chacha20poly1305_ietf_ABYTES) {
306
+ ret = crypto_aead_chacha20poly1305_ietf_decrypt_detached
307
+ (m, nsec,
308
+ c, clen - crypto_aead_chacha20poly1305_ietf_ABYTES,
309
+ c + clen - crypto_aead_chacha20poly1305_ietf_ABYTES,
310
+ ad, adlen, npub, k);
311
+ }
240
312
  if (mlen_p != NULL) {
313
+ if (ret == 0) {
314
+ mlen = clen - crypto_aead_chacha20poly1305_ietf_ABYTES;
315
+ }
241
316
  *mlen_p = mlen;
242
317
  }
243
- return 0;
318
+ return ret;
319
+ }
320
+
321
+ size_t
322
+ crypto_aead_chacha20poly1305_ietf_keybytes(void) {
323
+ return crypto_aead_chacha20poly1305_ietf_KEYBYTES;
324
+ }
325
+
326
+ size_t
327
+ crypto_aead_chacha20poly1305_ietf_npubbytes(void) {
328
+ return crypto_aead_chacha20poly1305_ietf_NPUBBYTES;
329
+ }
330
+
331
+ size_t
332
+ crypto_aead_chacha20poly1305_ietf_nsecbytes(void) {
333
+ return crypto_aead_chacha20poly1305_ietf_NSECBYTES;
334
+ }
335
+
336
+ size_t
337
+ crypto_aead_chacha20poly1305_ietf_abytes(void) {
338
+ return crypto_aead_chacha20poly1305_ietf_ABYTES;
244
339
  }
245
340
 
246
341
  size_t
@@ -253,11 +348,6 @@ crypto_aead_chacha20poly1305_npubbytes(void) {
253
348
  return crypto_aead_chacha20poly1305_NPUBBYTES;
254
349
  }
255
350
 
256
- size_t
257
- crypto_aead_chacha20poly1305_ietf_npubbytes(void) {
258
- return crypto_aead_chacha20poly1305_IETF_NPUBBYTES;
259
- }
260
-
261
351
  size_t
262
352
  crypto_aead_chacha20poly1305_nsecbytes(void) {
263
353
  return crypto_aead_chacha20poly1305_NSECBYTES;