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
@@ -3185,6 +3185,11 @@ tv(void)
3185
3185
  printf("Verification of test vector #%u with a truncated tag failed\n",
3186
3186
  (unsigned int) i);
3187
3187
  }
3188
+ if (i == 0 && crypto_aead_aes256gcm_decrypt(NULL, NULL,
3189
+ NULL, ciphertext, ciphertext_len,
3190
+ ad, ad_len, nonce, key) != 0) {
3191
+ printf("Verification of test vector #%u's tag failed\n", (unsigned int) i);
3192
+ }
3188
3193
  if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len,
3189
3194
  NULL, ciphertext, ciphertext_len,
3190
3195
  ad, ad_len, nonce, key) != 0) {
@@ -3231,6 +3236,7 @@ main(void)
3231
3236
  assert(crypto_aead_aes256gcm_npubbytes() == crypto_aead_aes256gcm_NPUBBYTES);
3232
3237
  assert(crypto_aead_aes256gcm_abytes() == crypto_aead_aes256gcm_ABYTES);
3233
3238
  assert(crypto_aead_aes256gcm_statebytes() >= sizeof(crypto_aead_aes256gcm_state));
3239
+ assert(crypto_aead_aes256gcm_messagebytes_max() == crypto_aead_aes256gcm_MESSAGEBYTES_MAX);
3234
3240
  printf("OK\n");
3235
3241
 
3236
3242
  return 0;
@@ -66,6 +66,10 @@ tv(void)
66
66
  printf("m != m2\n");
67
67
  }
68
68
  memset(m2, 0, m2len);
69
+ assert(crypto_aead_chacha20poly1305_decrypt_detached(NULL, NULL,
70
+ c, MLEN, mac,
71
+ ad, ADLEN,
72
+ nonce, firstkey) == 0);
69
73
  if (crypto_aead_chacha20poly1305_decrypt_detached(m2, NULL,
70
74
  c, MLEN, mac,
71
75
  ad, ADLEN,
@@ -163,6 +167,12 @@ tv(void)
163
167
  assert(crypto_aead_chacha20poly1305_keybytes() > 0U);
164
168
  assert(crypto_aead_chacha20poly1305_npubbytes() > 0U);
165
169
  assert(crypto_aead_chacha20poly1305_nsecbytes() == 0U);
170
+ assert(crypto_aead_chacha20poly1305_messagebytes_max() > 0U);
171
+ assert(crypto_aead_chacha20poly1305_messagebytes_max() == crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX);
172
+ assert(crypto_aead_chacha20poly1305_keybytes() == crypto_aead_chacha20poly1305_KEYBYTES);
173
+ assert(crypto_aead_chacha20poly1305_nsecbytes() == crypto_aead_chacha20poly1305_NSECBYTES);
174
+ assert(crypto_aead_chacha20poly1305_npubbytes() == crypto_aead_chacha20poly1305_NPUBBYTES);
175
+ assert(crypto_aead_chacha20poly1305_abytes() == crypto_aead_chacha20poly1305_ABYTES);
166
176
 
167
177
  return 0;
168
178
  }
@@ -239,6 +249,10 @@ tv_ietf(void)
239
249
  printf("m != m2\n");
240
250
  }
241
251
  memset(m2, 0, m2len);
252
+ assert(crypto_aead_chacha20poly1305_ietf_decrypt_detached(NULL, NULL,
253
+ c, MLEN, mac,
254
+ ad, ADLEN,
255
+ nonce, firstkey) == 0);
242
256
  if (crypto_aead_chacha20poly1305_ietf_decrypt_detached(m2, NULL,
243
257
  c, MLEN, mac,
244
258
  ad, ADLEN,
@@ -338,10 +352,12 @@ tv_ietf(void)
338
352
  assert(crypto_aead_chacha20poly1305_ietf_npubbytes() > crypto_aead_chacha20poly1305_npubbytes());
339
353
  assert(crypto_aead_chacha20poly1305_ietf_nsecbytes() == 0U);
340
354
  assert(crypto_aead_chacha20poly1305_ietf_nsecbytes() == crypto_aead_chacha20poly1305_nsecbytes());
355
+ assert(crypto_aead_chacha20poly1305_ietf_messagebytes_max() == crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX);
341
356
  assert(crypto_aead_chacha20poly1305_IETF_KEYBYTES == crypto_aead_chacha20poly1305_ietf_KEYBYTES);
342
357
  assert(crypto_aead_chacha20poly1305_IETF_NSECBYTES == crypto_aead_chacha20poly1305_ietf_NSECBYTES);
343
358
  assert(crypto_aead_chacha20poly1305_IETF_NPUBBYTES == crypto_aead_chacha20poly1305_ietf_NPUBBYTES);
344
359
  assert(crypto_aead_chacha20poly1305_IETF_ABYTES == crypto_aead_chacha20poly1305_ietf_ABYTES);
360
+ assert(crypto_aead_chacha20poly1305_IETF_MESSAGEBYTES_MAX == crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX);
345
361
 
346
362
  return 0;
347
363
  }
@@ -29,6 +29,7 @@ tv(void)
29
29
  = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 };
30
30
  unsigned char *c = (unsigned char *) sodium_malloc(CLEN);
31
31
  unsigned char *detached_c = (unsigned char *) sodium_malloc(MLEN);
32
+ unsigned char *key2 = (unsigned char *) sodium_malloc(crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
32
33
  unsigned char *mac = (unsigned char *) sodium_malloc(crypto_aead_xchacha20poly1305_ietf_ABYTES);
33
34
  unsigned char *m2 = (unsigned char *) sodium_malloc(MLEN);
34
35
  unsigned long long found_clen;
@@ -39,8 +40,8 @@ tv(void)
39
40
  assert(sizeof MESSAGE - 1U == MLEN);
40
41
  memcpy(m, MESSAGE, MLEN);
41
42
  crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,
42
- ad, ADLEN,
43
- NULL, nonce, firstkey);
43
+ ad, ADLEN,
44
+ NULL, nonce, firstkey);
44
45
  if (found_clen != MLEN + crypto_aead_xchacha20poly1305_ietf_abytes()) {
45
46
  printf("found_clen is not properly set\n");
46
47
  }
@@ -52,10 +53,10 @@ tv(void)
52
53
  }
53
54
  printf("\n");
54
55
  crypto_aead_xchacha20poly1305_ietf_encrypt_detached(detached_c,
55
- mac, &found_maclen,
56
- m, MLEN,
57
- ad, ADLEN,
58
- NULL, nonce, firstkey);
56
+ mac, &found_maclen,
57
+ m, MLEN,
58
+ ad, ADLEN,
59
+ NULL, nonce, firstkey);
59
60
  if (found_maclen != crypto_aead_xchacha20poly1305_ietf_abytes()) {
60
61
  printf("found_maclen is not properly set\n");
61
62
  }
@@ -64,7 +65,7 @@ tv(void)
64
65
  }
65
66
 
66
67
  if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN, ad,
67
- ADLEN, nonce, firstkey) != 0) {
68
+ ADLEN, nonce, firstkey) != 0) {
68
69
  printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed\n");
69
70
  }
70
71
  if (m2len != MLEN) {
@@ -75,9 +76,9 @@ tv(void)
75
76
  }
76
77
  memset(m2, 0, m2len);
77
78
  if (crypto_aead_xchacha20poly1305_ietf_decrypt_detached(m2, NULL,
78
- c, MLEN, mac,
79
- ad, ADLEN,
80
- nonce, firstkey) != 0) {
79
+ c, MLEN, mac,
80
+ ad, ADLEN,
81
+ nonce, firstkey) != 0) {
81
82
  printf("crypto_aead_xchacha20poly1305_ietf_decrypt_detached() failed\n");
82
83
  }
83
84
  if (memcmp(m, m2, MLEN) != 0) {
@@ -87,14 +88,14 @@ tv(void)
87
88
  for (i = 0U; i < CLEN; i++) {
88
89
  c[i] ^= (i + 1U);
89
90
  if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, NULL, NULL, c, CLEN,
90
- ad, ADLEN, nonce, firstkey)
91
+ ad, ADLEN, nonce, firstkey)
91
92
  == 0 || memcmp(m, m2, MLEN) == 0) {
92
93
  printf("message can be forged\n");
93
94
  }
94
95
  c[i] ^= (i + 1U);
95
96
  }
96
97
  crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,
97
- NULL, 0U, NULL, nonce, firstkey);
98
+ NULL, 0U, NULL, nonce, firstkey);
98
99
  if (found_clen != CLEN) {
99
100
  printf("clen is not properly set (adlen=0)\n");
100
101
  }
@@ -106,7 +107,7 @@ tv(void)
106
107
  }
107
108
  printf("\n");
108
109
  if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN,
109
- NULL, 0U, nonce, firstkey) != 0) {
110
+ NULL, 0U, nonce, firstkey) != 0) {
110
111
  printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n");
111
112
  }
112
113
  if (m2len != MLEN) {
@@ -138,7 +139,7 @@ tv(void)
138
139
 
139
140
  memcpy(c, m, MLEN);
140
141
  crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, c, MLEN,
141
- NULL, 0U, NULL, nonce, firstkey);
142
+ NULL, 0U, NULL, nonce, firstkey);
142
143
  if (found_clen != CLEN) {
143
144
  printf("clen is not properly set (adlen=0)\n");
144
145
  }
@@ -151,7 +152,7 @@ tv(void)
151
152
  printf("\n");
152
153
 
153
154
  if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN,
154
- NULL, 0U, nonce, firstkey) != 0) {
155
+ NULL, 0U, nonce, firstkey) != 0) {
155
156
  printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n");
156
157
  }
157
158
  if (m2len != MLEN) {
@@ -161,8 +162,15 @@ tv(void)
161
162
  printf("m != c (adlen=0)\n");
162
163
  }
163
164
 
165
+ crypto_aead_xchacha20poly1305_ietf_keygen(key2);
166
+ if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN,
167
+ NULL, 0U, nonce, key2) == 0) {
168
+ printf("crypto_aead_xchacha20poly1305_ietf_decrypt() with a wrong key should have failed\n");
169
+ }
170
+
164
171
  sodium_free(c);
165
172
  sodium_free(detached_c);
173
+ sodium_free(key2);
166
174
  sodium_free(mac);
167
175
  sodium_free(m2);
168
176
  sodium_free(m);
@@ -171,10 +179,12 @@ tv(void)
171
179
  assert(crypto_aead_xchacha20poly1305_ietf_npubbytes() == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
172
180
  assert(crypto_aead_xchacha20poly1305_ietf_nsecbytes() == 0U);
173
181
  assert(crypto_aead_xchacha20poly1305_ietf_nsecbytes() == crypto_aead_xchacha20poly1305_ietf_NSECBYTES);
182
+ assert(crypto_aead_xchacha20poly1305_ietf_messagebytes_max() == crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX);
174
183
  assert(crypto_aead_xchacha20poly1305_IETF_KEYBYTES == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
175
184
  assert(crypto_aead_xchacha20poly1305_IETF_NSECBYTES == crypto_aead_xchacha20poly1305_ietf_NSECBYTES);
176
185
  assert(crypto_aead_xchacha20poly1305_IETF_NPUBBYTES == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
177
186
  assert(crypto_aead_xchacha20poly1305_IETF_ABYTES == crypto_aead_xchacha20poly1305_ietf_ABYTES);
187
+ assert(crypto_aead_xchacha20poly1305_IETF_MESSAGEBYTES_MAX == crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX);
178
188
 
179
189
  return 0;
180
190
  }
@@ -19,6 +19,7 @@ int
19
19
  main(void)
20
20
  {
21
21
  crypto_auth_hmacsha512_state st;
22
+ crypto_auth_hmacsha256_state st256;
22
23
  size_t i;
23
24
 
24
25
  assert(crypto_auth_hmacsha512_statebytes() ==
@@ -52,6 +53,18 @@ main(void)
52
53
  printf("\n");
53
54
  }
54
55
 
56
+ memset(a2, 0, sizeof a2);
57
+ crypto_auth_hmacsha256_init(&st256, key2, sizeof key2);
58
+ crypto_auth_hmacsha256_update(&st256, NULL, 0U);
59
+ crypto_auth_hmacsha256_update(&st256, c, 1U);
60
+ crypto_auth_hmacsha256_update(&st256, c, sizeof c - 2U);
61
+ crypto_auth_hmacsha256_final(&st256, a2);
62
+ for (i = 0; i < sizeof a2; ++i) {
63
+ printf(",0x%02x", (unsigned int) a2[i]);
64
+ if (i % 8 == 7)
65
+ printf("\n");
66
+ }
67
+
55
68
  assert(crypto_auth_bytes() > 0U);
56
69
  assert(crypto_auth_keybytes() > 0U);
57
70
  assert(strcmp(crypto_auth_primitive(), "hmacsha512256") == 0);
@@ -20,3 +20,11 @@
20
20
  ,0x31,0x8a,0x9a,0x0b,0x3b,0x78,0x60,0xa4
21
21
  ,0x31,0x6f,0x72,0x9b,0x8d,0x30,0x0f,0x15
22
22
  ,0x9b,0x2f,0x60,0x93,0xa8,0x60,0xc1,0xed
23
+ ,0x62,0x27,0xe4,0xce,0x7c,0x7f,0xe7,0xa4
24
+ ,0xba,0x9e,0x2a,0xc3,0x42,0xc3,0x5d,0x24
25
+ ,0x03,0x3e,0x38,0x8c,0x9b,0xdc,0x29,0x9b
26
+ ,0x4a,0x50,0x50,0xf6,0x71,0x70,0xf4,0x83
27
+ ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
28
+ ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
29
+ ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
30
+ ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
@@ -87,6 +87,7 @@ main(void)
87
87
  assert(crypto_box_zerobytes() > 0U);
88
88
  assert(crypto_box_boxzerobytes() > 0U);
89
89
  assert(crypto_box_macbytes() > 0U);
90
+ assert(crypto_box_messagebytes_max() > 0U);
90
91
  assert(strcmp(crypto_box_primitive(), "curve25519xsalsa20poly1305") == 0);
91
92
  assert(crypto_box_curve25519xsalsa20poly1305_seedbytes() ==
92
93
  crypto_box_seedbytes());
@@ -104,6 +105,8 @@ main(void)
104
105
  crypto_box_boxzerobytes());
105
106
  assert(crypto_box_curve25519xsalsa20poly1305_macbytes() ==
106
107
  crypto_box_macbytes());
108
+ assert(crypto_box_curve25519xsalsa20poly1305_messagebytes_max() ==
109
+ crypto_box_messagebytes_max());
107
110
 
108
111
  return 0;
109
112
  }
@@ -64,6 +64,8 @@ main(void)
64
64
  assert(ret == -1);
65
65
 
66
66
  memset(m, 0, sizeof m);
67
+ ret = crypto_box_beforenm(k, small_order_p, bobsk);
68
+ assert(ret == -1);
67
69
  ret = crypto_box_beforenm(k, alicepk, bobsk);
68
70
  assert(ret == 0);
69
71
  if (crypto_box_open_afternm(m, c, 163, nonce, k) == 0) {
@@ -64,11 +64,8 @@ main(void)
64
64
  }
65
65
  printf("\n");
66
66
  c[randombytes_uniform(crypto_box_MACBYTES)]++;
67
- ret =
68
- crypto_box_open_easy(c, c, crypto_box_MACBYTES, nonce, bobpk, alicesk);
67
+ ret = crypto_box_open_easy(c, c, crypto_box_MACBYTES, nonce, bobpk, alicesk);
69
68
  assert(ret == -1);
70
69
 
71
- assert(crypto_box_easy(c, m, SIZE_MAX - 1U, nonce, bobpk, alicesk) == -1);
72
-
73
70
  return 0;
74
71
  }
@@ -88,10 +88,10 @@ main(void)
88
88
 
89
89
  memset(m2, 0, m2_size);
90
90
 
91
- if (crypto_box_easy_afternm(c, m, SIZE_MAX - 1U, nonce, k1) == 0) {
91
+ if (crypto_box_easy_afternm(c, m, 0, nonce, k1) != 0) {
92
92
  printf(
93
- "crypto_box_easy_afternm() with a short ciphertext should have "
94
- "failed\n");
93
+ "crypto_box_easy_afternm() with a null ciphertext should have "
94
+ "worked\n");
95
95
  }
96
96
  crypto_box_easy_afternm(c, m, (unsigned long long) mlen, nonce, k1);
97
97
  if (crypto_box_open_easy_afternm(
@@ -113,6 +113,10 @@ main(void)
113
113
  ret = crypto_box_detached(c, mac, m, (unsigned long long) mlen, nonce,
114
114
  alicepk, bobsk);
115
115
  assert(ret == 0);
116
+ if (crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen, nonce,
117
+ small_order_p, alicesk) != -1) {
118
+ printf("crypto_box_open_detached() with a weak key passed\n");
119
+ }
116
120
  if (crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen, nonce,
117
121
  bobpk, alicesk) != 0) {
118
122
  printf("crypto_box_open_detached() failed\n");
@@ -75,7 +75,7 @@ void tv(void)
75
75
  crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 1U, key);
76
76
  sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
77
77
  printf("[%s]\n", out_hex);
78
- };
78
+ }
79
79
 
80
80
  static
81
81
  void tv_ietf(void)
@@ -163,7 +163,7 @@ void tv_ietf(void)
163
163
  crypto_stream_chacha20_ietf_xor_ic(out, out, sizeof out, nonce, 1U, key);
164
164
  sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
165
165
  printf("[%s]\n", out_hex);
166
- };
166
+ }
167
167
 
168
168
  int
169
169
  main(void)
@@ -172,8 +172,15 @@ main(void)
172
172
  tv_ietf();
173
173
 
174
174
  assert(crypto_stream_chacha20_keybytes() > 0U);
175
+ assert(crypto_stream_chacha20_keybytes() == crypto_stream_chacha20_KEYBYTES);
175
176
  assert(crypto_stream_chacha20_noncebytes() > 0U);
177
+ assert(crypto_stream_chacha20_noncebytes() == crypto_stream_chacha20_NONCEBYTES);
178
+ assert(crypto_stream_chacha20_messagebytes_max() == crypto_stream_chacha20_MESSAGEBYTES_MAX);
179
+ assert(crypto_stream_chacha20_ietf_keybytes() > 0U);
180
+ assert(crypto_stream_chacha20_ietf_keybytes() == crypto_stream_chacha20_ietf_KEYBYTES);
176
181
  assert(crypto_stream_chacha20_ietf_noncebytes() > 0U);
182
+ assert(crypto_stream_chacha20_ietf_noncebytes() == crypto_stream_chacha20_ietf_NONCEBYTES);
183
+ assert(crypto_stream_chacha20_ietf_messagebytes_max() == crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX);
177
184
 
178
185
  return 0;
179
186
  }
@@ -30,7 +30,53 @@
30
30
 
31
31
  int xmain(void);
32
32
 
33
- #ifndef BROWSER_TESTS
33
+ #ifdef BENCHMARKS
34
+
35
+ # include <sys/time.h>
36
+
37
+ # ifndef ITERATIONS
38
+ # define ITERATIONS 128
39
+ # endif
40
+
41
+ static unsigned long long now(void)
42
+ {
43
+ struct timeval tp;
44
+ unsigned long long now;
45
+
46
+ if (gettimeofday(&tp, NULL) != 0) {
47
+ abort();
48
+ }
49
+ now = ((unsigned long long) tp.tv_sec * 1000000ULL) +
50
+ (unsigned long long) tp.tv_usec;
51
+
52
+ return now;
53
+ }
54
+
55
+ int main(void)
56
+ {
57
+ unsigned long long ts_start;
58
+ unsigned long long ts_end;
59
+ unsigned int i;
60
+
61
+ if (sodium_init() != 0) {
62
+ return 99;
63
+ }
64
+ randombytes_set_implementation(&randombytes_salsa20_implementation);
65
+ ts_start = now();
66
+ for (i = 0; i < ITERATIONS; i++) {
67
+ if (xmain() != 0) {
68
+ abort();
69
+ }
70
+ }
71
+ ts_end = now();
72
+ printf("%llu\n", 1000000ULL * (ts_end - ts_start) / ITERATIONS);
73
+
74
+ return 0;
75
+ }
76
+
77
+ #define printf(...) do { } while(0)
78
+
79
+ #elif !defined(BROWSER_TESTS)
34
80
 
35
81
  FILE *fp_res;
36
82
 
@@ -0,0 +1,226 @@
1
+ #define TEST_NAME "codecs"
2
+ #include "cmptest.h"
3
+
4
+ int
5
+ main(void)
6
+ {
7
+ unsigned char buf1[1000];
8
+ char buf3[33];
9
+ unsigned char buf4[4];
10
+ const char *b64;
11
+ char *b64_;
12
+ const char *b64_end;
13
+ unsigned char *bin;
14
+ unsigned char *bin_padded;
15
+ const char *hex;
16
+ const char *hex_end;
17
+ size_t b64_len;
18
+ size_t bin_len, bin_len2;
19
+ unsigned int i;
20
+
21
+ printf("%s\n",
22
+ sodium_bin2hex(buf3, 33U, (const unsigned char *) "0123456789ABCDEF",
23
+ 16U));
24
+ hex = "Cafe : 6942";
25
+ sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len,
26
+ &hex_end);
27
+ printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
28
+ buf4[0], buf4[1], buf4[2], buf4[3]);
29
+ printf("dt1: %ld\n", (long) (hex_end - hex));
30
+
31
+ hex = "Cafe : 6942";
32
+ sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, NULL);
33
+ printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
34
+ buf4[0], buf4[1], buf4[2], buf4[3]);
35
+
36
+ hex = "deadbeef";
37
+ if (sodium_hex2bin(buf1, 1U, hex, 8U, NULL, &bin_len, &hex_end) != -1) {
38
+ printf("sodium_hex2bin() overflow not detected\n");
39
+ }
40
+ printf("dt2: %ld\n", (long) (hex_end - hex));
41
+
42
+ hex = "de:ad:be:eff";
43
+ if (sodium_hex2bin(buf1, 4U, hex, 12U, ":", &bin_len, &hex_end) != -1) {
44
+ printf(
45
+ "sodium_hex2bin() with an odd input length and a short output "
46
+ "buffer\n");
47
+ }
48
+ printf("dt3: %ld\n", (long) (hex_end - hex));
49
+
50
+ hex = "de:ad:be:eff";
51
+ if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
52
+ &bin_len, &hex_end) != -1) {
53
+ printf("sodium_hex2bin() with an odd input length\n");
54
+ }
55
+ printf("dt4: %ld\n", (long) (hex_end - hex));
56
+
57
+ hex = "de:ad:be:eff";
58
+ if (sodium_hex2bin(buf1, sizeof buf1, hex, 13U, ":",
59
+ &bin_len, &hex_end) != -1) {
60
+ printf("sodium_hex2bin() with an odd input length (2)\n");
61
+ }
62
+ printf("dt5: %ld\n", (long) (hex_end - hex));
63
+
64
+ hex = "de:ad:be:eff";
65
+ if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
66
+ &bin_len, NULL) != -1) {
67
+ printf("sodium_hex2bin() with an odd input length and no end pointer\n");
68
+ }
69
+
70
+ hex = "de:ad:be:ef*";
71
+ if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
72
+ &bin_len, &hex_end) != 0) {
73
+ printf("sodium_hex2bin() with an extra character and an end pointer\n");
74
+ }
75
+ printf("dt6: %ld\n", (long) (hex_end - hex));
76
+
77
+ hex = "de:ad:be:ef*";
78
+ if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
79
+ &bin_len, NULL) != -1) {
80
+ printf("sodium_hex2bin() with an extra character and no end pointer\n");
81
+ }
82
+
83
+ printf("%s\n",
84
+ sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
85
+ 21U, sodium_base64_VARIANT_ORIGINAL));
86
+ printf("%s\n",
87
+ sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
88
+ 22U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
89
+ printf("%s\n",
90
+ sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
91
+ 21U, sodium_base64_VARIANT_URLSAFE));
92
+ printf("%s\n",
93
+ sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
94
+ 22U, sodium_base64_VARIANT_URLSAFE_NO_PADDING));
95
+ printf("%s\n",
96
+ sodium_bin2base64(buf3, 1U, NULL,
97
+ 0U, sodium_base64_VARIANT_ORIGINAL));
98
+ printf("%s\n",
99
+ sodium_bin2base64(buf3, 5U, (const unsigned char *) "a",
100
+ 1U, sodium_base64_VARIANT_ORIGINAL));
101
+ printf("%s\n",
102
+ sodium_bin2base64(buf3, 5U, (const unsigned char *) "ab",
103
+ 2U, sodium_base64_VARIANT_ORIGINAL));
104
+ printf("%s\n",
105
+ sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
106
+ 3U, sodium_base64_VARIANT_ORIGINAL));
107
+ printf("%s\n",
108
+ sodium_bin2base64(buf3, 1U, NULL,
109
+ 0U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
110
+ printf("%s\n",
111
+ sodium_bin2base64(buf3, 3U, (const unsigned char *) "a",
112
+ 1U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
113
+ printf("%s\n",
114
+ sodium_bin2base64(buf3, 4U, (const unsigned char *) "ab",
115
+ 2U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
116
+ printf("%s\n",
117
+ sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
118
+ 3U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
119
+
120
+ b64 = "VGhpcyBpcyBhIGpvdXJu" "\n" "ZXkgaW50by" " " "Bzb3VuZA==";
121
+ memset(buf4, '*', sizeof buf4);
122
+ assert(sodium_base642bin(buf4, sizeof buf4, b64, strlen(b64), "\n\r ", &bin_len,
123
+ &b64_end, sodium_base64_VARIANT_ORIGINAL) == -1);
124
+ buf4[bin_len] = 0;
125
+ printf("[%s]\n", (const char *) buf4);
126
+ printf("[%s]\n", b64_end);
127
+
128
+ memset(buf1, '*', sizeof buf1);
129
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), "\n\r ", &bin_len,
130
+ &b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
131
+ buf1[bin_len] = 0;
132
+ printf("[%s]\n", (const char *) buf1);
133
+ assert(*b64_end == 0);
134
+
135
+ memset(buf1, '*', sizeof buf1);
136
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, &bin_len,
137
+ &b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
138
+ buf1[bin_len] = 0;
139
+ printf("[%s]\n", (const char *) buf1);
140
+ printf("[%s]\n", b64_end);
141
+
142
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
143
+ &b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
144
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
145
+ &b64_end, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
146
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
147
+ &b64_end, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
148
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
149
+ &b64_end, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
150
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
151
+ &b64_end, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
152
+
153
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
154
+ NULL, sodium_base64_VARIANT_ORIGINAL) == -1);
155
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
156
+ NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
157
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
158
+ NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
159
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
160
+ NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == -1);
161
+ assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
162
+ NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == -1);
163
+
164
+ assert(sodium_base642bin(NULL, (size_t) 10U, "a=", (size_t) 2U, NULL, NULL, NULL,
165
+ sodium_base64_VARIANT_URLSAFE) == -1);
166
+ assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, NULL, NULL, NULL,
167
+ sodium_base64_VARIANT_URLSAFE) == -1);
168
+ assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, "~", NULL, NULL,
169
+ sodium_base64_VARIANT_URLSAFE) == -1);
170
+ assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, "*", NULL, NULL,
171
+ sodium_base64_VARIANT_URLSAFE) == -1);
172
+ assert(sodium_base642bin(NULL, (size_t) 10U, "a==", (size_t) 3U, NULL, NULL, NULL,
173
+ sodium_base64_VARIANT_URLSAFE) == -1);
174
+ assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, NULL, NULL, NULL,
175
+ sodium_base64_VARIANT_URLSAFE) == -1);
176
+ assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, "~", NULL, NULL,
177
+ sodium_base64_VARIANT_URLSAFE) == -1);
178
+ assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, "*", NULL, NULL,
179
+ sodium_base64_VARIANT_URLSAFE) == -1);
180
+
181
+ assert(sodium_base642bin(buf1, sizeof buf1, "O1R", (size_t) 3U, NULL, NULL, NULL,
182
+ sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
183
+ assert(sodium_base642bin(buf1, sizeof buf1, "O1Q", (size_t) 3U, NULL, NULL, NULL,
184
+ sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
185
+ assert(sodium_base642bin(buf1, sizeof buf1, "O1", (size_t) 2U, NULL, NULL, NULL,
186
+ sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
187
+ assert(sodium_base642bin(buf1, sizeof buf1, "Ow", (size_t) 2U, NULL, NULL, NULL,
188
+ sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
189
+ assert(sodium_base642bin(buf1, sizeof buf1, "O", (size_t) 1U, NULL, NULL, NULL,
190
+ sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
191
+
192
+ assert(sodium_base642bin(buf1, sizeof buf1, "kaw", (size_t) 3U, NULL, NULL, NULL,
193
+ sodium_base64_VARIANT_ORIGINAL) == -1);
194
+ assert(sodium_base642bin(buf1, sizeof buf1, "kQ*", (size_t) 3U, "@", NULL, NULL,
195
+ sodium_base64_VARIANT_ORIGINAL) == -1);
196
+ assert(sodium_base642bin(buf1, sizeof buf1, "kQ*", (size_t) 3U, "*", NULL, NULL,
197
+ sodium_base64_VARIANT_ORIGINAL) == -1);
198
+ assert(sodium_base642bin(buf1, sizeof buf1, "kaw=**", (size_t) 6U, "*", NULL, NULL,
199
+ sodium_base64_VARIANT_ORIGINAL) == 0);
200
+ assert(sodium_base642bin(buf1, sizeof buf1, "kaw*=*", (size_t) 6U, "~*", NULL, NULL,
201
+ sodium_base64_VARIANT_ORIGINAL) == 0);
202
+ assert(sodium_base642bin(buf1, sizeof buf1, "ka*w*=*", (size_t) 7U, "*~", NULL, NULL,
203
+ sodium_base64_VARIANT_ORIGINAL) == 0);
204
+
205
+ for (i = 0; i < 1000; i++) {
206
+ assert(sizeof buf1 >= 100);
207
+ bin_len = (size_t) randombytes_uniform(100);
208
+ bin = (unsigned char *) sodium_malloc(bin_len);
209
+ b64_len = (bin_len + 2U) / 3U * 4U + 1U;
210
+ assert(b64_len == sodium_base64_encoded_len(bin_len, sodium_base64_VARIANT_URLSAFE));
211
+ b64_ = (char *) sodium_malloc(b64_len);
212
+ randombytes_buf(bin, bin_len);
213
+ memcpy(buf1, bin, bin_len);
214
+ b64 = sodium_bin2base64(b64_, b64_len, bin, bin_len,
215
+ sodium_base64_VARIANT_URLSAFE);
216
+ assert(b64 != NULL);
217
+ assert(sodium_base642bin(bin, bin_len + 10, b64, b64_len,
218
+ NULL, NULL, &b64_end,
219
+ sodium_base64_VARIANT_URLSAFE) == 0);
220
+ assert(b64_end == &b64[b64_len - 1]);
221
+ assert(memcmp(bin, buf1, bin_len) == 0);
222
+ sodium_free(bin);
223
+ sodium_free(b64_);
224
+ }
225
+ return 0;
226
+ }