crypto_stream_xchacha20.phpt 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. --TEST--
  2. Check for libsodium stream
  3. --EXTENSIONS--
  4. sodium
  5. --SKIPIF--
  6. <?php if (!defined('SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES')) print "skip"; ?>
  7. --FILE--
  8. <?php
  9. $nonce = random_bytes(SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES);
  10. $key = sodium_crypto_stream_xchacha20_keygen();
  11. $len = 100;
  12. $stream = sodium_crypto_stream_xchacha20($len, $nonce, $key);
  13. var_dump(strlen($stream));
  14. $stream2 = sodium_crypto_stream_xchacha20($len, $nonce, $key);
  15. $nonce = random_bytes(SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES);
  16. $stream3 = sodium_crypto_stream_xchacha20($len, $nonce, $key);
  17. $key = sodium_crypto_stream_keygen();
  18. $stream4 = sodium_crypto_stream_xchacha20($len, $nonce, $key);
  19. var_dump($stream === $stream2);
  20. var_dump($stream !== $stream3);
  21. var_dump($stream !== $stream4);
  22. var_dump($stream2 !== $stream3);
  23. var_dump($stream2 !== $stream4);
  24. var_dump($stream3 !== $stream4);
  25. $stream5 = sodium_crypto_stream_xchacha20_xor($stream, $nonce, $key);
  26. var_dump($stream5 !== $stream);
  27. $stream6 = sodium_crypto_stream_xchacha20_xor($stream5, $nonce, $key);
  28. var_dump($stream6 === $stream);
  29. try {
  30. sodium_crypto_stream_xchacha20(-1, $nonce, $key);
  31. } catch (SodiumException $ex) {
  32. echo $ex->getMessage(), "\n";
  33. }
  34. try {
  35. sodium_crypto_stream_xchacha20($len, substr($nonce, 1), $key);
  36. } catch (SodiumException $ex) {
  37. echo $ex->getMessage(), "\n";
  38. }
  39. try {
  40. sodium_crypto_stream_xchacha20($len, $nonce, substr($key, 1));
  41. } catch (SodiumException $ex) {
  42. echo $ex->getMessage(), "\n";
  43. }
  44. try {
  45. sodium_crypto_stream_xchacha20_xor($stream, substr($nonce, 1), $key);
  46. } catch (SodiumException $ex) {
  47. echo $ex->getMessage(), "\n";
  48. }
  49. try {
  50. sodium_crypto_stream_xchacha20_xor($stream, $nonce, substr($key, 1));
  51. } catch (SodiumException $ex) {
  52. echo $ex->getMessage(), "\n";
  53. }
  54. ?>
  55. --EXPECT--
  56. int(100)
  57. bool(true)
  58. bool(true)
  59. bool(true)
  60. bool(true)
  61. bool(true)
  62. bool(true)
  63. bool(true)
  64. bool(true)
  65. sodium_crypto_stream_xchacha20(): Argument #1 ($length) must be greater than 0
  66. sodium_crypto_stream_xchacha20(): Argument #2 ($nonce) must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long
  67. sodium_crypto_stream_xchacha20(): Argument #3 ($key) must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long
  68. sodium_crypto_stream_xchacha20_xor(): Argument #2 ($nonce) must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long
  69. sodium_crypto_stream_xchacha20_xor(): Argument #3 ($key) must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long