1--TEST-- 2Check for libsodium stream 3--EXTENSIONS-- 4sodium 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 12$len = 100; 13$stream = sodium_crypto_stream_xchacha20($len, $nonce, $key); 14var_dump(strlen($stream)); 15 16$stream2 = sodium_crypto_stream_xchacha20($len, $nonce, $key); 17 18$nonce = random_bytes(SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES); 19$stream3 = sodium_crypto_stream_xchacha20($len, $nonce, $key); 20 21$key = sodium_crypto_stream_keygen(); 22$stream4 = sodium_crypto_stream_xchacha20($len, $nonce, $key); 23 24var_dump($stream === $stream2); 25var_dump($stream !== $stream3); 26var_dump($stream !== $stream4); 27var_dump($stream2 !== $stream3); 28var_dump($stream2 !== $stream4); 29var_dump($stream3 !== $stream4); 30 31$stream5 = sodium_crypto_stream_xchacha20_xor($stream, $nonce, $key); 32var_dump($stream5 !== $stream); 33$stream6 = sodium_crypto_stream_xchacha20_xor($stream5, $nonce, $key); 34 35var_dump($stream6 === $stream); 36 37// New test (with Initial Counter feature): 38$n2 = random_bytes(SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES); 39$left = str_repeat("\x01", 64); 40$right = str_repeat("\xfe", 64); 41 42// All at once: 43$stream7_unified = sodium_crypto_stream_xchacha20_xor($left . $right, $n2, $key); 44 45// Piecewise, with initial counter: 46$stream7_left = sodium_crypto_stream_xchacha20_xor_ic($left, $n2, 0, $key); 47$stream7_right = sodium_crypto_stream_xchacha20_xor_ic($right, $n2, 1, $key); 48$stream7_concat = $stream7_left . $stream7_right; 49 50var_dump(strlen($stream7_concat)); 51var_dump($stream7_unified === $stream7_concat); 52 53try { 54 sodium_crypto_stream_xchacha20(-1, $nonce, $key); 55} catch (SodiumException $ex) { 56 echo $ex->getMessage(), "\n"; 57} 58try { 59 sodium_crypto_stream_xchacha20($len, substr($nonce, 1), $key); 60} catch (SodiumException $ex) { 61 echo $ex->getMessage(), "\n"; 62} 63try { 64 sodium_crypto_stream_xchacha20($len, $nonce, substr($key, 1)); 65} catch (SodiumException $ex) { 66 echo $ex->getMessage(), "\n"; 67} 68try { 69 sodium_crypto_stream_xchacha20_xor($stream, substr($nonce, 1), $key); 70} catch (SodiumException $ex) { 71 echo $ex->getMessage(), "\n"; 72} 73try { 74 sodium_crypto_stream_xchacha20_xor($stream, $nonce, substr($key, 1)); 75} catch (SodiumException $ex) { 76 echo $ex->getMessage(), "\n"; 77} 78 79?> 80--EXPECT-- 81int(100) 82bool(true) 83bool(true) 84bool(true) 85bool(true) 86bool(true) 87bool(true) 88bool(true) 89bool(true) 90int(128) 91bool(true) 92sodium_crypto_stream_xchacha20(): Argument #1 ($length) must be greater than 0 93sodium_crypto_stream_xchacha20(): Argument #2 ($nonce) must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long 94sodium_crypto_stream_xchacha20(): Argument #3 ($key) must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long 95sodium_crypto_stream_xchacha20_xor(): Argument #2 ($nonce) must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long 96sodium_crypto_stream_xchacha20_xor(): Argument #3 ($key) must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long 97