var_dump($msg === $msg2);
var_dump(sodium_crypto_aead_chacha20poly1305_decrypt($ciphertext, 'x' . $ad, $nonce, $key));
try {
- // Switched order
- $msg2 = sodium_crypto_aead_chacha20poly1305_decrypt($ciphertext, $ad, $key, $nonce);
- var_dump(false);
+ // Switched order
+ $msg2 = sodium_crypto_aead_chacha20poly1305_decrypt($ciphertext, $ad, $key, $nonce);
+ var_dump(false);
} catch (SodiumException $ex) {
- var_dump(true);
+ var_dump(true);
}
echo "aead_chacha20poly1305_ietf:\n";
if (SODIUM_LIBRARY_MAJOR_VERSION > 7 ||
- (SODIUM_LIBRARY_MAJOR_VERSION == 7 &&
- SODIUM_LIBRARY_MINOR_VERSION >= 6)) {
- $msg = random_bytes(random_int(1, 1000));
- $nonce = random_bytes(SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES);
- $key = sodium_crypto_aead_chacha20poly1305_ietf_keygen();
- $ad = random_bytes(random_int(1, 1000));
+ (SODIUM_LIBRARY_MAJOR_VERSION == 7 &&
+ SODIUM_LIBRARY_MINOR_VERSION >= 6)) {
+ $msg = random_bytes(random_int(1, 1000));
+ $nonce = random_bytes(SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES);
+ $key = sodium_crypto_aead_chacha20poly1305_ietf_keygen();
+ $ad = random_bytes(random_int(1, 1000));
- $ciphertext = sodium_crypto_aead_chacha20poly1305_ietf_encrypt($msg, $ad, $nonce, $key);
- $msg2 = sodium_crypto_aead_chacha20poly1305_ietf_decrypt($ciphertext, $ad, $nonce, $key);
- var_dump($ciphertext !== $msg);
- var_dump($msg === $msg2);
- var_dump(sodium_crypto_aead_chacha20poly1305_ietf_decrypt($ciphertext, 'x' . $ad, $nonce, $key));
- try {
- // Switched order
- $msg2 = sodium_crypto_aead_chacha20poly1305_ietf_decrypt($ciphertext, $ad, $key, $nonce);
- var_dump(false);
- } catch (SodiumException $ex) {
- var_dump(true);
- }
+ $ciphertext = sodium_crypto_aead_chacha20poly1305_ietf_encrypt($msg, $ad, $nonce, $key);
+ $msg2 = sodium_crypto_aead_chacha20poly1305_ietf_decrypt($ciphertext, $ad, $nonce, $key);
+ var_dump($ciphertext !== $msg);
+ var_dump($msg === $msg2);
+ var_dump(sodium_crypto_aead_chacha20poly1305_ietf_decrypt($ciphertext, 'x' . $ad, $nonce, $key));
+ try {
+ // Switched order
+ $msg2 = sodium_crypto_aead_chacha20poly1305_ietf_decrypt($ciphertext, $ad, $key, $nonce);
+ var_dump(false);
+ } catch (SodiumException $ex) {
+ var_dump(true);
+ }
} else {
- var_dump(true);
- var_dump(true);
- var_dump(false);
- var_dump(true);
+ var_dump(true);
+ var_dump(true);
+ var_dump(false);
+ var_dump(true);
}
echo "aead_xchacha20poly1305_ietf:\n";
if (SODIUM_LIBRARY_MAJOR_VERSION > 9 ||
- (SODIUM_LIBRARY_MAJOR_VERSION == 9 &&
- SODIUM_LIBRARY_MINOR_VERSION >= 4)) {
- $msg = random_bytes(random_int(1, 1000));
- $nonce = random_bytes(SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES);
- $key = sodium_crypto_aead_xchacha20poly1305_ietf_keygen();
- $ad = random_bytes(random_int(1, 1000));
+ (SODIUM_LIBRARY_MAJOR_VERSION == 9 &&
+ SODIUM_LIBRARY_MINOR_VERSION >= 4)) {
+ $msg = random_bytes(random_int(1, 1000));
+ $nonce = random_bytes(SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES);
+ $key = sodium_crypto_aead_xchacha20poly1305_ietf_keygen();
+ $ad = random_bytes(random_int(1, 1000));
- $ciphertext = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt($msg, $ad, $nonce, $key);
- $msg2 = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, $ad, $nonce, $key);
- var_dump($ciphertext !== $msg);
- var_dump($msg === $msg2);
- var_dump(sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, 'x' . $ad, $nonce, $key));
- try {
- // Switched order
- $msg2 = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, $ad, $key, $nonce);
- var_dump(false);
- } catch (SodiumException $ex) {
- var_dump(true);
- }
+ $ciphertext = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt($msg, $ad, $nonce, $key);
+ $msg2 = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, $ad, $nonce, $key);
+ var_dump($ciphertext !== $msg);
+ var_dump($msg === $msg2);
+ var_dump(sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, 'x' . $ad, $nonce, $key));
+ try {
+ // Switched order
+ $msg2 = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, $ad, $key, $nonce);
+ var_dump(false);
+ } catch (SodiumException $ex) {
+ var_dump(true);
+ }
} else {
- var_dump(true);
- var_dump(true);
- var_dump(false);
- var_dump(true);
+ var_dump(true);
+ var_dump(true);
+ var_dump(false);
+ var_dump(true);
}
echo "aead_aes256gcm:\n";
if (sodium_crypto_aead_aes256gcm_is_available()) {
- $msg = random_bytes(random_int(1, 1000));
- $nonce = random_bytes(SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES);
- $ad = random_bytes(random_int(1, 1000));
- $key = sodium_crypto_aead_aes256gcm_keygen();
- $ciphertext = sodium_crypto_aead_aes256gcm_encrypt($msg, $ad, $nonce, $key);
- $msg2 = sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $ad, $nonce, $key);
- var_dump($ciphertext !== $msg);
- var_dump($msg === $msg2);
- var_dump(sodium_crypto_aead_aes256gcm_decrypt($ciphertext, 'x' . $ad, $nonce, $key));
- try {
- // Switched order
- $msg2 = sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $ad, $key, $nonce);
- var_dump(false);
- } catch (SodiumException $ex) {
- var_dump(true);
- }
+ $msg = random_bytes(random_int(1, 1000));
+ $nonce = random_bytes(SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES);
+ $ad = random_bytes(random_int(1, 1000));
+ $key = sodium_crypto_aead_aes256gcm_keygen();
+ $ciphertext = sodium_crypto_aead_aes256gcm_encrypt($msg, $ad, $nonce, $key);
+ $msg2 = sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $ad, $nonce, $key);
+ var_dump($ciphertext !== $msg);
+ var_dump($msg === $msg2);
+ var_dump(sodium_crypto_aead_aes256gcm_decrypt($ciphertext, 'x' . $ad, $nonce, $key));
+ try {
+ // Switched order
+ $msg2 = sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $ad, $key, $nonce);
+ var_dump(false);
+ } catch (SodiumException $ex) {
+ var_dump(true);
+ }
} else {
- var_dump(true);
- var_dump(true);
- var_dump(false);
- var_dump(true);
+ var_dump(true);
+ var_dump(true);
+ var_dump(false);
+ var_dump(true);
}
?>
--EXPECT--
$bad_key = random_bytes(SODIUM_CRYPTO_AUTH_KEYBYTES - 1);
try {
- $mac = sodium_crypto_auth($msg, $bad_key);
- echo 'Fail!', PHP_EOL;
+ $mac = sodium_crypto_auth($msg, $bad_key);
+ echo 'Fail!', PHP_EOL;
} catch (SodiumException $ex) {
echo $ex->getMessage(), PHP_EOL;
}
// Let's flip a bit pseudo-randomly
$badmsg = $msg;
$badmsg[$i=mt_rand(0, 999)] = \chr(
- \ord($msg[$i]) ^ (
- 1 << mt_rand(0, 7)
- )
+ \ord($msg[$i]) ^ (
+ 1 << mt_rand(0, 7)
+ )
);
var_dump(sodium_crypto_auth_verify($mac, $badmsg, $key));
$bob_box_publickey = sodium_crypto_box_publickey($bob_box_kp);
$alice_to_bob_kp = sodium_crypto_box_keypair_from_secretkey_and_publickey(
- $alice_box_secretkey,
- $bob_box_publickey
+ $alice_box_secretkey,
+ $bob_box_publickey
);
$msg = "Here is another message, to be signed using Alice's secret key, and " .
"since they are derived from a fixed seeds";
$ciphertext = sodium_crypto_box(
- $msg,
- $message_nonce,
- $alice_to_bob_kp
+ $msg,
+ $message_nonce,
+ $alice_to_bob_kp
);
try {
$ciphertext = sodium_crypto_box(
- $msg,
- $message_nonce,
- substr($alice_to_bob_kp, 1)
+ $msg,
+ $message_nonce,
+ substr($alice_to_bob_kp, 1)
);
} catch (SodiumException $ex) {
- echo $ex->getMessage(), PHP_EOL;
+ echo $ex->getMessage(), PHP_EOL;
}
sodium_memzero($alice_box_kp);
$bob_box_secretkey = sodium_crypto_box_secretkey($bob_box_kp);
$bob_to_alice_kp = sodium_crypto_box_keypair_from_secretkey_and_publickey(
- $bob_box_secretkey,
- $alice_box_publickey
+ $bob_box_secretkey,
+ $alice_box_publickey
);
$plaintext = sodium_crypto_box_open(
- $ciphertext,
- $message_nonce,
- $bob_to_alice_kp
+ $ciphertext,
+ $message_nonce,
+ $bob_to_alice_kp
);
var_dump($msg === $plaintext);
$alice_to_bob_message_nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
$alice_to_bob_ciphertext = sodium_crypto_box('Hi, this is Alice',
- $alice_to_bob_message_nonce,
- $alice_to_bob_kp);
+ $alice_to_bob_message_nonce,
+ $alice_to_bob_kp);
$alice_message_decrypted_by_bob = sodium_crypto_box_open($alice_to_bob_ciphertext,
- $alice_to_bob_message_nonce,
- $bob_to_alice_kp);
+ $alice_to_bob_message_nonce,
+ $bob_to_alice_kp);
$bob_to_alice_message_nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
$bob_to_alice_ciphertext = sodium_crypto_box('Hi Alice! This is Bob',
- $bob_to_alice_message_nonce,
- $bob_to_alice_kp);
+ $bob_to_alice_message_nonce,
+ $bob_to_alice_kp);
$bob_message_decrypted_by_alice = sodium_crypto_box_open($bob_to_alice_ciphertext,
- $bob_to_alice_message_nonce,
- $alice_to_bob_kp);
+ $bob_to_alice_message_nonce,
+ $alice_to_bob_kp);
var_dump($alice_message_decrypted_by_bob);
var_dump($bob_message_decrypted_by_alice);
if (SODIUM_LIBRARY_MAJOR_VERSION > 7 ||
- (SODIUM_LIBRARY_MAJOR_VERSION == 7 &&
- SODIUM_LIBRARY_MINOR_VERSION >= 5)) {
- $anonymous_message_to_alice = sodium_crypto_box_seal('Anonymous message',
- $alice_publickey);
+ (SODIUM_LIBRARY_MAJOR_VERSION == 7 &&
+ SODIUM_LIBRARY_MINOR_VERSION >= 5)) {
+ $anonymous_message_to_alice = sodium_crypto_box_seal('Anonymous message',
+ $alice_publickey);
- $decrypted_message = sodium_crypto_box_seal_open($anonymous_message_to_alice,
- $alice_kp);
+ $decrypted_message = sodium_crypto_box_seal_open($anonymous_message_to_alice,
+ $alice_kp);
} else {
- $decrypted_message = 'Anonymous message';
+ $decrypted_message = 'Anonymous message';
}
var_dump($decrypted_message);
$msg = sodium_hex2bin(
- '7375f4094f1151640bd853cb13dbc1a0ee9e13b0287a89d34fa2f6732be9de13f88457553d'.
- '768347116522d6d32c9cb353ef07aa7c83bd129b2bb5db35b28334c935b24f2639405a0604'
+ '7375f4094f1151640bd853cb13dbc1a0ee9e13b0287a89d34fa2f6732be9de13f88457553d'.
+ '768347116522d6d32c9cb353ef07aa7c83bd129b2bb5db35b28334c935b24f2639405a0604'
);
$kp = sodium_hex2bin(
- '36a6c2b96a650d80bf7e025e0f58f3d636339575defb370801a54213bd54582d'.
- '5aecbcf7866e7a4d58a6c1317e2b955f54ecbe2fcbbf7d262c10636ed524480c'
+ '36a6c2b96a650d80bf7e025e0f58f3d636339575defb370801a54213bd54582d'.
+ '5aecbcf7866e7a4d58a6c1317e2b955f54ecbe2fcbbf7d262c10636ed524480c'
);
var_dump(sodium_crypto_box_seal_open($msg, $kp));
?>
$exp = bin2hex($q);
var_dump($exp);
$act = bin2hex(
- sodium_crypto_generichash('msg2', '0123456789abcdef', 64)
+ sodium_crypto_generichash('msg2', '0123456789abcdef', 64)
);
var_dump($act);
var_dump($exp === $act);
try {
- $hash = sodium_crypto_generichash('test', '', 128);
+ $hash = sodium_crypto_generichash('test', '', 128);
} catch (SodiumException $ex) {
- var_dump(true);
+ var_dump(true);
}
?>
--EXPECT--
var_dump(sodium_bin2hex($q));
try {
- sodium_crypto_scalarmult(substr($n, 1), $p);
+ sodium_crypto_scalarmult(substr($n, 1), $p);
} catch (SodiumException $ex) {
- var_dump(true);
+ var_dump(true);
}
?>
--EXPECT--
var_dump($y);
try {
- sodium_crypto_secretbox('test', substr($nonce, 1), $key);
+ sodium_crypto_secretbox('test', substr($nonce, 1), $key);
} catch (SodiumException $ex) {
- var_dump(true);
+ var_dump(true);
}
?>
echo bin2hex($h3) . "\n";
try {
- sodium_crypto_shorthash($m1, $k1 . $k2);
+ sodium_crypto_shorthash($m1, $k1 . $k2);
} catch (SodiumException $ex) {
- var_dump(true);
+ var_dump(true);
}
?>
--EXPECT--
$signature = sodium_crypto_sign_detached($msg, $alice_secretkey);
var_dump(strlen($signature) === SODIUM_CRYPTO_SIGN_BYTES);
var_dump(sodium_crypto_sign_verify_detached($signature,
- $msg, $alice_publickey));
+ $msg, $alice_publickey));
var_dump(sodium_crypto_sign_verify_detached($signature,
- $msg . "\0", $alice_publickey));
+ $msg . "\0", $alice_publickey));
$calc_pubkey = sodium_crypto_sign_publickey_from_secretkey($alice_secretkey);
var_dump(sodium_memcmp($calc_pubkey, $alice_publickey) === 0);
var_dump($curve25519key === sodium_hex2bin("381b2be5e3d38820deb1243fb58b4be654da30dd3ccde492cb88f937eb489363"));
try {
- sodium_crypto_sign($msg, substr($alice_secretkey, 1));
+ sodium_crypto_sign($msg, substr($alice_secretkey, 1));
} catch (SodiumException $ex) {
- var_dump(true);
+ var_dump(true);
}
?>
--EXPECT--
var_dump($stream6 === $stream);
try {
- sodium_crypto_stream($len, substr($nonce, 1), $key);
+ sodium_crypto_stream($len, substr($nonce, 1), $key);
} catch (SodiumException $ex) {
- var_dump(true);
+ var_dump(true);
}
?>
<?php
function do_memzero($x) {
- sodium_memzero($x);
+ sodium_memzero($x);
}
$x = 42;
$notStr = 123;
try {
- sodium_increment($notStr);
+ sodium_increment($notStr);
} catch (SodiumException $e) {
- echo $e->getMessage(), "\n";
+ echo $e->getMessage(), "\n";
}
$str = "abc";
$notStr = 123;
try {
- sodium_add($notStr, $addStr);
+ sodium_add($notStr, $addStr);
} catch (SodiumException $e) {
- echo $e->getMessage(), "\n";
+ echo $e->getMessage(), "\n";
}
$str = "abc";
--TEST--
-Check for libsodium presence
+Check for sodium presence
--SKIPIF--
<?php if (!extension_loaded("sodium")) print "skip"; ?>
--FILE--
<?php
-echo "libsodium extension is available";
+echo "sodium extension is available";
/*
- you can add regression tests for your extension here
+ you can add regression tests for your extension here
the output of your test code has to be equal to the
text in the --EXPECT-- section below for the tests
to pass, differences between the output and the
expected text are interpreted as failure
- see php5/README.TESTING for further information on
+ see php7/README.TESTING for further information on
writing regression tests
*/
?>
--EXPECT--
-libsodium extension is available
+sodium extension is available
$hash = sodium_crypto_pwhash_str
($passwd, SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
- SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE);
+ SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE);
var_dump(substr($hash, 0, strlen(SODIUM_CRYPTO_PWHASH_STRPREFIX)) ===
- SODIUM_CRYPTO_PWHASH_STRPREFIX);
+ SODIUM_CRYPTO_PWHASH_STRPREFIX);
$testHash = '$argon2i$v=19$m=4096,t=3,p=1$MzE4ODFiZWFlMjAzOWUAAA$FWUV6tsyJ32qThiLi1cCsLIbf3dIOG/RwXcTzt536KY';
$c = sodium_crypto_pwhash_str_verify($testHash, $passwd);
var_dump(bin2hex($v));
if (SODIUM_LIBRARY_MAJOR_VERSION > 7 ||
- (SODIUM_LIBRARY_MAJOR_VERSION == 7 &&
- SODIUM_LIBRARY_MINOR_VERSION >= 6)) {
- $v_1 = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
- $v_2 = ""."\x02\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
- $v_1 .= '';
- var_dump(sodium_compare($v_1, $v_2));
- sodium_increment($v_1);
- var_dump(sodium_compare($v_1, $v_2));
- sodium_increment($v_1);
- var_dump(sodium_compare($v_1, $v_2));
+ (SODIUM_LIBRARY_MAJOR_VERSION == 7 &&
+ SODIUM_LIBRARY_MINOR_VERSION >= 6)) {
+ $v_1 = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
+ $v_2 = ""."\x02\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
+ $v_1 .= '';
+ var_dump(sodium_compare($v_1, $v_2));
+ sodium_increment($v_1);
+ var_dump(sodium_compare($v_1, $v_2));
+ sodium_increment($v_1);
+ var_dump(sodium_compare($v_1, $v_2));
} else {
- // Dummy test results for libsodium < 1.0.4
- var_dump(-1, 0, 1);
+ // Dummy test results for libsodium < 1.0.4
+ var_dump(-1, 0, 1);
}
$str = 'stdClass';
$str_unpadded = sodium_unpad($str_padded, 16);
var_dump($str_unpadded == $str);
+
+if (defined('SODIUM_BASE64_VARIANT_ORIGINAL')) {
+ for ($i = 0; $i < 100; $i++) {
+ $bin = $i == 0 ? '' : random_bytes($i);
+ $b64 = base64_encode($bin);
+ $b64_ = sodium_bin2base64($bin, SODIUM_BASE64_VARIANT_ORIGINAL);
+ if ($b64 !== $b64_) {
+ echo "frombin[$b64] != frombin_[$b64_]\n";
+ }
+ $bin_ = sodium_base642bin($b64, SODIUM_BASE64_VARIANT_ORIGINAL);
+ if ($bin !== $bin_) {
+ echo "frombase64([$b64]) != frombase64_[$b64]\n";
+ }
+ $bin_ = sodium_base642bin(" $b64\n", SODIUM_BASE64_VARIANT_ORIGINAL, " \n");
+ if ($bin !== $bin_) {
+ echo "frombase64([$b64]) != frombase64_([ $b64\\n])\n";
+ }
+ }
+ try {
+ var_dump(sodium_base642bin('O1R', SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING));
+ } catch (Exception $e) {
+ var_dump('base64("O1R") case passed');
+ }
+ try {
+ var_dump(sodium_base642bin('O1', SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING));
+ } catch (Exception $e) {
+ var_dump('base64("O1") case passed');
+ }
+ try {
+ var_dump(sodium_base642bin('O', SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING));
+ } catch (Exception $e) {
+ var_dump('base64("O") case passed');
+ }
+}
+
?>
--EXPECT--
0
}
string(32) "78797a80000000000000000000000000"
bool(true)
+string(25) "base64("O1R") case passed"
+string(24) "base64("O1") case passed"
+string(23) "base64("O") case passed"