Skip to content

Commit 4e5c949

Browse files
committed
ext/sodium: Test the KEM internal-error paths
An invalid ML-KEM public key encoding and a small-order X25519 ciphertext component deterministically fail inside libsodium, exercising the memzero-on-error branches. ML-KEM768 decapsulation never fails (implicit rejection), so it has no such test. Also drop the cross-family length check, which duplicated existing coverage.
1 parent 83d9421 commit 4e5c949

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

ext/sodium/tests/crypto_kem.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ try {
9191
} catch (Throwable $e) {
9292
echo $e::class, ': ', $e->getMessage(), "\n";
9393
}
94+
/* Error: a public key with an invalid ML-KEM encoding is rejected */
95+
try {
96+
sodium_crypto_kem_enc(str_repeat("\xff", SODIUM_CRYPTO_KEM_PUBLICKEYBYTES));
97+
} catch (Throwable $e) {
98+
echo $e::class, ': ', $e->getMessage(), "\n";
99+
}
100+
/* Error: a ciphertext whose X25519 component is a small-order point is rejected */
101+
$small_order_ciphertext = substr($ciphertext, 0, SODIUM_CRYPTO_KEM_CIPHERTEXTBYTES - 32)
102+
. str_repeat("\0", 32);
103+
try {
104+
sodium_crypto_kem_dec($small_order_ciphertext, $secret_key);
105+
} catch (Throwable $e) {
106+
echo $e::class, ': ', $e->getMessage(), "\n";
107+
}
94108
?>
95109
--EXPECT--
96110
crypto_kem (X-Wing):
@@ -117,3 +131,5 @@ SodiumException: sodium_crypto_kem_dec(): Argument #2 ($secret_key) must be SODI
117131
SodiumException: sodium_crypto_kem_secretkey(): Argument #1 ($key_pair) must be SODIUM_CRYPTO_KEM_KEYPAIRBYTES bytes long
118132
SodiumException: sodium_crypto_kem_publickey(): Argument #1 ($key_pair) must be SODIUM_CRYPTO_KEM_KEYPAIRBYTES bytes long
119133
SodiumException: sodium_crypto_kem_seed_keypair(): Argument #1 ($seed) must be SODIUM_CRYPTO_KEM_SEEDBYTES bytes long
134+
SodiumException: internal error
135+
SodiumException: internal error

ext/sodium/tests/crypto_kem_mlkem768.phpt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,6 @@ try {
6161
} catch (Throwable $e) {
6262
echo $e::class, ': ', $e->getMessage(), "\n";
6363
}
64-
/* Error: an ML-KEM768 public key is not a valid X-Wing public key */
65-
if (defined('SODIUM_CRYPTO_KEM_PUBLICKEYBYTES')) {
66-
try {
67-
sodium_crypto_kem_enc($public_key);
68-
} catch (Throwable $e) {
69-
echo $e::class, ': ', $e->getMessage(), "\n";
70-
}
71-
} else {
72-
echo "SodiumException: sodium_crypto_kem_enc(): Argument #1 (\$public_key) must be SODIUM_CRYPTO_KEM_PUBLICKEYBYTES bytes long\n";
73-
}
7464
/* Error: truncated ciphertext */
7565
try {
7666
sodium_crypto_kem_mlkem768_dec(substr($ciphertext, 0, -1), $secret_key);
@@ -101,6 +91,12 @@ try {
10191
} catch (Throwable $e) {
10292
echo $e::class, ': ', $e->getMessage(), "\n";
10393
}
94+
/* Error: a public key with an invalid ML-KEM encoding is rejected */
95+
try {
96+
sodium_crypto_kem_mlkem768_enc(str_repeat("\xff", SODIUM_CRYPTO_KEM_MLKEM768_PUBLICKEYBYTES));
97+
} catch (Throwable $e) {
98+
echo $e::class, ': ', $e->getMessage(), "\n";
99+
}
104100
?>
105101
--EXPECT--
106102
crypto_kem_mlkem768:
@@ -122,9 +118,9 @@ bool(true)
122118
bool(true)
123119
bool(true)
124120
SodiumException: sodium_crypto_kem_mlkem768_enc(): Argument #1 ($public_key) must be SODIUM_CRYPTO_KEM_MLKEM768_PUBLICKEYBYTES bytes long
125-
SodiumException: sodium_crypto_kem_enc(): Argument #1 ($public_key) must be SODIUM_CRYPTO_KEM_PUBLICKEYBYTES bytes long
126121
SodiumException: sodium_crypto_kem_mlkem768_dec(): Argument #1 ($ciphertext) must be SODIUM_CRYPTO_KEM_MLKEM768_CIPHERTEXTBYTES bytes long
127122
SodiumException: sodium_crypto_kem_mlkem768_dec(): Argument #2 ($secret_key) must be SODIUM_CRYPTO_KEM_MLKEM768_SECRETKEYBYTES bytes long
128123
SodiumException: sodium_crypto_kem_mlkem768_secretkey(): Argument #1 ($key_pair) must be SODIUM_CRYPTO_KEM_MLKEM768_KEYPAIRBYTES bytes long
129124
SodiumException: sodium_crypto_kem_mlkem768_publickey(): Argument #1 ($key_pair) must be SODIUM_CRYPTO_KEM_MLKEM768_KEYPAIRBYTES bytes long
130125
SodiumException: sodium_crypto_kem_mlkem768_seed_keypair(): Argument #1 ($seed) must be SODIUM_CRYPTO_KEM_MLKEM768_SEEDBYTES bytes long
126+
SodiumException: internal error

0 commit comments

Comments
 (0)