Skip to content

Commit 98184af

Browse files
committed
Improve ristretto255 scalarmult exception messages
These fail due to bad inputs, not internal errors.
1 parent 31d0aff commit 98184af

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

ext/sodium/libsodium.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,7 +2585,7 @@ PHP_FUNCTION(sodium_crypto_scalarmult_ristretto255)
25852585
q = zend_string_alloc(crypto_scalarmult_ristretto255_BYTES, 0);
25862586
if (crypto_scalarmult_ristretto255((unsigned char *) ZSTR_VAL(q), n, p) != 0) {
25872587
zend_string_efree(q);
2588-
zend_throw_exception(sodium_exception_ce, "internal error", 0);
2588+
zend_throw_exception(sodium_exception_ce, "Result is identity element", 0);
25892589
RETURN_THROWS();
25902590
}
25912591
ZSTR_VAL(q)[crypto_scalarmult_ristretto255_BYTES] = 0;
@@ -2612,7 +2612,7 @@ PHP_FUNCTION(sodium_crypto_scalarmult_ristretto255_base)
26122612
q = zend_string_alloc(crypto_scalarmult_ristretto255_BYTES, 0);
26132613
if (crypto_scalarmult_ristretto255_base((unsigned char *) ZSTR_VAL(q), n) != 0) {
26142614
zend_string_efree(q);
2615-
zend_throw_exception(sodium_exception_ce, "internal error", 0);
2615+
zend_argument_error(sodium_exception_ce, 1, "must not be zero", 0);
26162616
RETURN_THROWS();
26172617
}
26182618
ZSTR_VAL(q)[crypto_scalarmult_BYTES] = 0;

ext/sodium/tests/crypto_core_ristretto255.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ bool(false)
128128
bool(false)
129129
string(64) "3066f82a1a747d45120d1740f14358531a8f04bbffe6a819f86dfe50f44a0a46"
130130
bool(true)
131-
internal error
131+
Result is identity element
132132
bool(true)
133133
bool(false)
134134
bool(true)

ext/sodium/tests/crypto_scalarmult_ristretto255.phpt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ if (!defined('SODIUM_CRYPTO_CORE_RISTRETTO255_HASHBYTES')) print "skip libsodium
1010
<?php
1111
$b = sodium_hex2bin("e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76");
1212
$n = str_repeat("\0", SODIUM_CRYPTO_SCALARMULT_RISTRETTO255_SCALARBYTES);
13-
for ($i = 0; $i < 16; $i++, sodium_increment($n)) {
14-
try {
15-
$p = sodium_crypto_scalarmult_ristretto255_base($n);
16-
$p2 = sodium_crypto_scalarmult_ristretto255($n, $b);
17-
} catch (SodiumException $ex) {
18-
echo $ex->getMessage(), "\n";
19-
continue;
20-
}
13+
14+
try {
15+
$p = sodium_crypto_scalarmult_ristretto255_base($n);
16+
} catch (SodiumException $ex) {
17+
echo $ex->getMessage(), "\n";
18+
}
19+
try {
20+
$p2 = sodium_crypto_scalarmult_ristretto255($n, $b);
21+
} catch (SodiumException $ex) {
22+
echo $ex->getMessage(), "\n";
23+
}
24+
25+
for ($i = 1; $i < 16; $i++) {
26+
sodium_increment($n);
27+
$p = sodium_crypto_scalarmult_ristretto255_base($n);
28+
$p2 = sodium_crypto_scalarmult_ristretto255($n, $b);
2129
var_dump(sodium_bin2hex($p));
2230
assert($p === $p2);
2331
}
@@ -30,7 +38,8 @@ try {
3038

3139
?>
3240
--EXPECT--
33-
internal error
41+
sodium_crypto_scalarmult_ristretto255_base(): Argument #1 ($n) must not be zero
42+
Result is identity element
3443
string(64) "e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76"
3544
string(64) "6a493210f7499cd17fecb510ae0cea23a110e8d5b901f8acadd3095c73a3b919"
3645
string(64) "94741f5d5d52755ece4f23f044ee27d5d1ea1e2bd196b462166b16152a9d0259"

0 commit comments

Comments
 (0)