Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4481,7 +4481,7 @@ PHP_FUNCTION(openssl_encrypt)
zend_string *ret;
zval *tag = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|lszsl", &data, &data_len, &method, &method_len,
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|lszs!l", &data, &data_len, &method, &method_len,
&password, &password_len, &options, &iv, &iv_len, &tag, &aad, &aad_len, &tag_len) == FAILURE) {
RETURN_THROWS();
}
Expand All @@ -4503,7 +4503,7 @@ PHP_FUNCTION(openssl_decrypt)
size_t data_len, method_len, password_len, iv_len = 0, tag_len = 0, aad_len = 0;
zend_string *ret;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|lss!s", &data, &data_len, &method, &method_len,
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|lss!s!", &data, &data_len, &method, &method_len,
&password, &password_len, &options, &iv, &iv_len, &tag, &tag_len, &aad, &aad_len) == FAILURE) {
RETURN_THROWS();
}
Expand Down
4 changes: 2 additions & 2 deletions ext/openssl/openssl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,9 @@ function openssl_digest(string $data, string $digest_algo, bool $binary = false)
/**
* @param string $tag
*/
function openssl_encrypt(#[\SensitiveParameter] string $data, string $cipher_algo, #[\SensitiveParameter] string $passphrase, int $options = 0, string $iv = "", &$tag = null, string $aad = "", int $tag_length = 16): string|false {}
function openssl_encrypt(#[\SensitiveParameter] string $data, string $cipher_algo, #[\SensitiveParameter] string $passphrase, int $options = 0, string $iv = "", &$tag = null, ?string $aad = "", int $tag_length = 16): string|false {}

function openssl_decrypt(string $data, string $cipher_algo, #[\SensitiveParameter] string $passphrase, int $options = 0, string $iv = "", ?string $tag = null, string $aad = ""): string|false {}
function openssl_decrypt(string $data, string $cipher_algo, #[\SensitiveParameter] string $passphrase, int $options = 0, string $iv = "", ?string $tag = null, ?string $aad = ""): string|false {}

function openssl_cipher_iv_length(string $cipher_algo): int|false {}

Expand Down
6 changes: 3 additions & 3 deletions ext/openssl/openssl_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions ext/openssl/openssl_backend_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1637,10 +1637,14 @@ void php_openssl_load_cipher_mode(struct php_openssl_cipher_mode *mode, const EV
{
int cipher_mode = EVP_CIPHER_mode(cipher_type);
memset(mode, 0, sizeof(struct php_openssl_cipher_mode));

switch (cipher_mode) {
case EVP_CIPH_GCM_MODE:
case EVP_CIPH_CCM_MODE:
/* We check for EVP_CIPH_OCB_MODE, because LibreSSL does not support it. */
/* We check for EVP_CIPH_SIV_MODE and EVP_CIPH_SIV_MODE, because LibreSSL does not support it. */
#ifdef EVP_CIPH_SIV_MODE
case EVP_CIPH_SIV_MODE:
#endif
#ifdef EVP_CIPH_OCB_MODE
case EVP_CIPH_OCB_MODE:
/* For OCB mode, explicitly set the tag length even when decrypting,
Expand Down Expand Up @@ -1797,7 +1801,9 @@ zend_result php_openssl_cipher_update(const EVP_CIPHER *cipher_type,
return FAILURE;
}

if (mode->is_aead && !EVP_CipherUpdate(cipher_ctx, NULL, &i, (const unsigned char *) aad, (int) aad_len)) {
/* Only pass AAD to OpenSSL if caller provided it.
This makes NULL mean zero AAD items, while "" with len 0 means one empty AAD item. */
if (mode->is_aead && aad != NULL && !EVP_CipherUpdate(cipher_ctx, NULL, &i, (const unsigned char *)aad, (int)aad_len)) {
Comment on lines +1804 to +1806
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for SIV or is this for all modes. I thought that empty string won't make difference but I guess it might.

In general allowing null should be fine for master so it's not really a problem. Just want to know the reasoning as I haven't checked this propertly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only SIV produces different outputs for aad = null and add = "". Other modes produce the same output.
From my understanding this is because in SIV mode aad could be a vector list. Thus null here means an empty vector list. And I don't believe we need to support multiple vectors as aad. Because then we would need to change $aad to string|array.

Copy link
Member

@bukka bukka Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok. It would be good to check if skipping actually works for ccm, gcm and ocb (using null argument). It might make sense to potentially limit it just to SIV and treat null as "" for other modes.

php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Setting of additional application data failed");
return FAILURE;
Expand Down
47 changes: 47 additions & 0 deletions ext/openssl/tests/gh20851_aad_empty.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you actually change it to a single test for encryption and decryption that will look like https://github.com/jordikroon/php-src/blob/ca12f45a76f3242953e7e27a9b64e509e9f518e2/ext/openssl/tests/openssl_decrypt_gcm.phpt
and
https://github.com/jordikroon/php-src/blob/ca12f45a76f3242953e7e27a9b64e509e9f518e2/ext/openssl/tests/openssl_encrypt_gcm.phpt

Please name it in a similar way and use the standard test vectors. It will also match the ccm and ocb tests...

openssl: AES-256-SIV AEAD tag and AAD roundtrip
--EXTENSIONS--
openssl
--FILE--
<?php
$algo = 'aes-256-siv';
$key = str_repeat('1', 64);
$tag = '';
$aad = '';
$input = 'Hello world!';

$ciphertext = openssl_encrypt(
'Hello world!',
$algo,
$key,
OPENSSL_RAW_DATA,
'', // IV is empty for this cipher in PHP
$tag, // gets filled with the SIV
$aad,
16
);

echo 'input: ' . $input . PHP_EOL;
echo 'tag: ' . bin2hex($tag) . PHP_EOL;
echo 'ciphertext: ' . bin2hex($ciphertext) . PHP_EOL;
echo 'combined: ' . bin2hex($tag . $ciphertext) . PHP_EOL;

$dec = openssl_decrypt(
$ciphertext,
$algo,
$key,
OPENSSL_RAW_DATA,
'',
$tag,
$aad
);

echo 'decrypted: ' . var_export($dec, true) . PHP_EOL;
?>
--EXPECTF--
input: Hello world!
tag: f6c98e3e785947502a09994d2757f9c1
ciphertext: a430a41a9bc089fa45ad27be
combined: f6c98e3e785947502a09994d2757f9c1a430a41a9bc089fa45ad27be
decrypted: 'Hello world!'

47 changes: 47 additions & 0 deletions ext/openssl/tests/gh20851_aad_null.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
openssl: AES-256-SIV AEAD tag and AAD roundtrip
--EXTENSIONS--
openssl
--FILE--
<?php
$algo = 'aes-256-siv';
$key = str_repeat('1', 64);
$tag = '';
$aad = null;
$input = 'Hello world!';

$ciphertext = openssl_encrypt(
'Hello world!',
$algo,
$key,
OPENSSL_RAW_DATA,
'', // IV is empty for this cipher in PHP
$tag, // gets filled with the SIV
$aad,
16
);

echo 'input: ' . $input . PHP_EOL;
echo 'tag: ' . bin2hex($tag) . PHP_EOL;
echo 'ciphertext: ' . bin2hex($ciphertext) . PHP_EOL;
echo 'combined: ' . bin2hex($tag . $ciphertext) . PHP_EOL;

$dec = openssl_decrypt(
$ciphertext,
$algo,
$key,
OPENSSL_RAW_DATA,
'',
$tag,
$aad
);

echo 'decrypted: ' . var_export($dec, true) . PHP_EOL;
?>
--EXPECTF--
input: Hello world!
tag: c06f0df087e2784c5560ce5d0b378311
ciphertext: 72fffba74d7bc3ddcceeb6d1
combined: c06f0df087e2784c5560ce5d0b37831172fffba74d7bc3ddcceeb6d1
decrypted: 'Hello world!'

Loading