Skip to content

Commit 6adfe9d

Browse files
committed
Fix error messages for sodium_crypto_stream_xchacha20
And test them. Also adjust the constant check in the test so that it actually runs.
1 parent f7f1f7f commit 6adfe9d

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

ext/sodium/libsodium.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1503,15 +1503,15 @@ PHP_FUNCTION(sodium_crypto_stream_xchacha20)
15031503
RETURN_THROWS();
15041504
}
15051505
if (ciphertext_len <= 0 || ciphertext_len >= SIZE_MAX) {
1506-
zend_argument_error(sodium_exception_ce, 1, "length must be greater than 0");
1506+
zend_argument_error(sodium_exception_ce, 1, "must be greater than 0");
15071507
RETURN_THROWS();
15081508
}
15091509
if (nonce_len != crypto_stream_xchacha20_NONCEBYTES) {
1510-
zend_argument_error(sodium_exception_ce, 2, "nonce must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long");
1510+
zend_argument_error(sodium_exception_ce, 2, "must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long");
15111511
RETURN_THROWS();
15121512
}
15131513
if (key_len != crypto_stream_xchacha20_KEYBYTES) {
1514-
zend_argument_error(sodium_exception_ce, 3, "key must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long");
1514+
zend_argument_error(sodium_exception_ce, 3, "must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long");
15151515
RETURN_THROWS();
15161516
}
15171517
ciphertext = zend_string_checked_alloc((size_t) ciphertext_len, 0);
@@ -1545,11 +1545,11 @@ PHP_FUNCTION(sodium_crypto_stream_xchacha20_xor)
15451545
RETURN_THROWS();
15461546
}
15471547
if (nonce_len != crypto_stream_xchacha20_NONCEBYTES) {
1548-
zend_argument_error(sodium_exception_ce, 2, "nonce must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long");
1548+
zend_argument_error(sodium_exception_ce, 2, "must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long");
15491549
RETURN_THROWS();
15501550
}
15511551
if (key_len != crypto_stream_xchacha20_KEYBYTES) {
1552-
zend_argument_error(sodium_exception_ce, 3, "key must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long");
1552+
zend_argument_error(sodium_exception_ce, 3, "must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long");
15531553
RETURN_THROWS();
15541554
}
15551555
ciphertext_len = msg_len;

ext/sodium/tests/crypto_stream_xchacha20.phpt

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--TEST--
22
Check for libsodium stream
33
--SKIPIF--
4-
<?php if (!extension_loaded("sodium") || !defined('CRYPTO_STREAM_XCHACHA20_KEYBYTES')) print "skip"; ?>
4+
<?php if (!extension_loaded("sodium") || !defined('SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES')) print "skip"; ?>
55
--FILE--
66
<?php
77
$nonce = random_bytes(SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES);
@@ -32,10 +32,30 @@ $stream6 = sodium_crypto_stream_xchacha20_xor($stream5, $nonce, $key);
3232

3333
var_dump($stream6 === $stream);
3434

35+
try {
36+
sodium_crypto_stream_xchacha20(-1, $nonce, $key);
37+
} catch (SodiumException $ex) {
38+
echo $ex->getMessage(), "\n";
39+
}
3540
try {
3641
sodium_crypto_stream_xchacha20($len, substr($nonce, 1), $key);
3742
} catch (SodiumException $ex) {
38-
var_dump(true);
43+
echo $ex->getMessage(), "\n";
44+
}
45+
try {
46+
sodium_crypto_stream_xchacha20($len, $nonce, substr($key, 1));
47+
} catch (SodiumException $ex) {
48+
echo $ex->getMessage(), "\n";
49+
}
50+
try {
51+
sodium_crypto_stream_xchacha20_xor($stream, substr($nonce, 1), $key);
52+
} catch (SodiumException $ex) {
53+
echo $ex->getMessage(), "\n";
54+
}
55+
try {
56+
sodium_crypto_stream_xchacha20_xor($stream, $nonce, substr($key, 1));
57+
} catch (SodiumException $ex) {
58+
echo $ex->getMessage(), "\n";
3959
}
4060

4161
?>
@@ -49,4 +69,8 @@ bool(true)
4969
bool(true)
5070
bool(true)
5171
bool(true)
52-
bool(true)
72+
sodium_crypto_stream_xchacha20(): Argument #1 ($length) must be greater than 0
73+
sodium_crypto_stream_xchacha20(): Argument #2 ($nonce) must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long
74+
sodium_crypto_stream_xchacha20(): Argument #3 ($key) must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long
75+
sodium_crypto_stream_xchacha20_xor(): Argument #2 ($nonce) must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long
76+
sodium_crypto_stream_xchacha20_xor(): Argument #3 ($key) must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long

0 commit comments

Comments
 (0)