Skip to content

Commit bdca5a9

Browse files
authored
sodium: Use opaque objects for SHAKE state (#24351)
* sodium: Use opaque objects for SHAKE state * sodium: Make `sodium_crypto_xof_*shake*_update()` void * sodium: Fix error message phrasing when requesting invalid lengths from SHAKE * NEWS
1 parent 2bff391 commit bdca5a9

7 files changed

Lines changed: 406 additions & 301 deletions

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ PHP NEWS
7373
. Fixed infinite loop in sapi_header_op() for headers of 4 GiB or more.
7474
(Martin Mirchev)
7575

76+
- Sodium:
77+
. Pass sodium_crypto_xof_*() state via objects instead of by-ref strings.
78+
(timwolla)
79+
7680
- SPL:
7781
. Fixed bug GH-24264 (Unwind/GracefulExit discarded in spl_iterators.c).
7882
(Lazizbek Ergashev)

‎ext/sodium/libsodium.c‎

Lines changed: 267 additions & 244 deletions
Large diffs are not rendered by default.

‎ext/sodium/libsodium.stub.php‎

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -936,35 +936,67 @@ function sodium_ip2bin(string $ip): string {}
936936

937937
function sodium_crypto_xof_shake128(int $length, string $message): string {}
938938

939-
function sodium_crypto_xof_shake128_init(?int $domain = null): string {}
939+
function sodium_crypto_xof_shake128_init(?int $domain = null): SodiumCryptoXofShake128State {}
940940

941-
function sodium_crypto_xof_shake128_update(string &$state, string $message): true {}
941+
function sodium_crypto_xof_shake128_update(SodiumCryptoXofShake128State $state, string $message): void {}
942942

943-
function sodium_crypto_xof_shake128_squeeze(string &$state, int $length): string {}
943+
function sodium_crypto_xof_shake128_squeeze(SodiumCryptoXofShake128State $state, int $length): string {}
944+
945+
/**
946+
* @strict-properties
947+
* @not-serializable
948+
*/
949+
final class SodiumCryptoXofShake128State {
950+
951+
}
944952

945953
function sodium_crypto_xof_shake256(int $length, string $message): string {}
946954

947-
function sodium_crypto_xof_shake256_init(?int $domain = null): string {}
955+
function sodium_crypto_xof_shake256_init(?int $domain = null): SodiumCryptoXofShake256State {}
948956

949-
function sodium_crypto_xof_shake256_update(string &$state, string $message): true {}
957+
function sodium_crypto_xof_shake256_update(SodiumCryptoXofShake256State $state, string $message): void {}
958+
959+
function sodium_crypto_xof_shake256_squeeze(SodiumCryptoXofShake256State $state, int $length): string {}
960+
961+
/**
962+
* @strict-properties
963+
* @not-serializable
964+
*/
965+
final class SodiumCryptoXofShake256State {
950966

951-
function sodium_crypto_xof_shake256_squeeze(string &$state, int $length): string {}
967+
}
952968

953969
function sodium_crypto_xof_turboshake128(int $length, string $message): string {}
954970

955-
function sodium_crypto_xof_turboshake128_init(?int $domain = null): string {}
971+
function sodium_crypto_xof_turboshake128_init(?int $domain = null): SodiumCryptoXofTurboshake128State {}
956972

957-
function sodium_crypto_xof_turboshake128_update(string &$state, string $message): true {}
973+
function sodium_crypto_xof_turboshake128_update(SodiumCryptoXofTurboshake128State $state, string $message): void {}
958974

959-
function sodium_crypto_xof_turboshake128_squeeze(string &$state, int $length): string {}
975+
function sodium_crypto_xof_turboshake128_squeeze(SodiumCryptoXofTurboshake128State $state, int $length): string {}
976+
977+
/**
978+
* @strict-properties
979+
* @not-serializable
980+
*/
981+
final class SodiumCryptoXofTurboshake128State {
982+
983+
}
960984

961985
function sodium_crypto_xof_turboshake256(int $length, string $message): string {}
962986

963-
function sodium_crypto_xof_turboshake256_init(?int $domain = null): string {}
987+
function sodium_crypto_xof_turboshake256_init(?int $domain = null): SodiumCryptoXofTurboshake256State {}
964988

965-
function sodium_crypto_xof_turboshake256_update(string &$state, string $message): true {}
989+
function sodium_crypto_xof_turboshake256_update(SodiumCryptoXofTurboshake256State $state, string $message): void {}
990+
991+
function sodium_crypto_xof_turboshake256_squeeze(SodiumCryptoXofTurboshake256State $state, int $length): string {}
992+
993+
/**
994+
* @strict-properties
995+
* @not-serializable
996+
*/
997+
final class SodiumCryptoXofTurboshake256State {
966998

967-
function sodium_crypto_xof_turboshake256_squeeze(string &$state, int $length): string {}
999+
}
9681000
#endif
9691001

9701002
#ifdef crypto_kem_PUBLICKEYBYTES

‎ext/sodium/libsodium_arginfo.h‎

Lines changed: 86 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ext/sodium/tests/crypto_xof_shake128.phpt‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ try {
7878
echo $e->getMessage() . "\n";
7979
}
8080

81-
/* Error: bad state */
82-
try {
83-
$bad = "not_a_state";
84-
sodium_crypto_xof_shake128_update($bad, "test");
85-
} catch (SodiumException $e) {
86-
echo $e->getMessage() . "\n";
87-
}
8881
?>
8982
--EXPECT--
9083
string(64) "7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26"
@@ -97,8 +90,7 @@ string(64) "5881092dd818bf5cf8a3ddb793fbcba74097d5c526a6d35f97b83351940f2cc8"
9790
string(64) "5881092dd818bf5cf8a3ddb793fbcba74097d5c526a6d35f97b83351940f2cc8"
9891
string(64) "f3a3a89c329e644a7d2351744d9a28c953698b64102e912085ce1f6d79fa311e"
9992
bool(true)
100-
sodium_crypto_xof_shake128(): Argument #1 ($length) must be a positive integer
101-
sodium_crypto_xof_shake128(): Argument #1 ($length) must be a positive integer
93+
sodium_crypto_xof_shake128(): Argument #1 ($length) must be greater than 0
94+
sodium_crypto_xof_shake128(): Argument #1 ($length) must be greater than 0
10295
sodium_crypto_xof_shake128_init(): Argument #1 ($domain) must be between 0x01 and 0x7f
10396
sodium_crypto_xof_shake128_init(): Argument #1 ($domain) must be between 0x01 and 0x7f
104-
sodium_crypto_xof_shake128_update(): Argument #1 ($state) must have a correct state length

‎ext/sodium/tests/crypto_xof_shake256.phpt‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ try {
4949
echo $e->getMessage() . "\n";
5050
}
5151

52-
/* Error: bad state */
53-
try {
54-
$bad = "not_a_state";
55-
sodium_crypto_xof_shake256_update($bad, "test");
56-
} catch (SodiumException $e) {
57-
echo $e->getMessage() . "\n";
58-
}
59-
6052
/* Error: squeeze bad length */
6153
try {
6254
$state = sodium_crypto_xof_shake256_init();
@@ -75,6 +67,5 @@ bool(true)
7567
string(64) "483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739"
7668
string(64) "483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739"
7769
string(64) "1259d63c872d50fee4500685419f489966971c0a77e2058fab0e48dfb12d24bc"
78-
sodium_crypto_xof_shake256(): Argument #1 ($length) must be a positive integer
79-
sodium_crypto_xof_shake256_update(): Argument #1 ($state) must have a correct state length
80-
sodium_crypto_xof_shake256_squeeze(): Argument #2 ($length) must be a positive integer
70+
sodium_crypto_xof_shake256(): Argument #1 ($length) must be greater than 0
71+
sodium_crypto_xof_shake256_squeeze(): Argument #2 ($length) must be greater than 0

‎ext/sodium/tests/crypto_xof_turboshake.phpt‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@ try {
7272
echo $e->getMessage() . "\n";
7373
}
7474

75-
/* TurboSHAKE256 error: bad state */
76-
try {
77-
$bad = "not_a_state";
78-
sodium_crypto_xof_turboshake256_update($bad, "test");
79-
} catch (SodiumException $e) {
80-
echo $e->getMessage() . "\n";
81-
}
82-
8375
/* TurboSHAKE128 error: domain out of range */
8476
try {
8577
sodium_crypto_xof_turboshake128_init(0);
@@ -106,7 +98,6 @@ string(64) "0137a7e8cca0b99e9bdd557a7caac1f21d65bc2a2ccbde1e1f8c702352a2bf30"
10698
int(16)
10799
int(64)
108100
bool(true)
109-
sodium_crypto_xof_turboshake128(): Argument #1 ($length) must be a positive integer
110-
sodium_crypto_xof_turboshake256_update(): Argument #1 ($state) must have a correct state length
101+
sodium_crypto_xof_turboshake128(): Argument #1 ($length) must be greater than 0
111102
sodium_crypto_xof_turboshake128_init(): Argument #1 ($domain) must be between 0x01 and 0x7f
112103
sodium_crypto_xof_turboshake256_init(): Argument #1 ($domain) must be between 0x01 and 0x7f

0 commit comments

Comments
 (0)