Commit c9ae5a0eb45 for php
commit c9ae5a0eb456cd7add5932752d01d97e33a142fe
Author: Christophe B <bourelly.christophe@gmail.com>
Date: Thu Dec 3 22:13:29 2026 +0100
Add AES-GCM-SIV AEAD support (RFC 8452) (#21874)
Extends the AEAD switch in php_openssl_load_cipher_mode() to also
recognize EVP_CIPH_GCM_SIV_MODE alongside the SIV/OCB cases added by
GH-20853. GCM-SIV (OpenSSL >= 3.2) uses the standard EVP_CTRL_AEAD_*_TAG
controls and takes a single AAD input, so aad_supports_vector stays false.
LibreSSL does not define EVP_CIPH_GCM_SIV_MODE, hence the #ifdef guard.
GCM-SIV only takes a 96-bit nonce, but OpenSSL accepts any
EVP_CTRL_AEAD_SET_IVLEN for it and reads 12 bytes from the IV buffer, so a
shorter IV was an out-of-bounds read. php_openssl_validate_iv() now rejects
any GCM-SIV IV that is not exactly 12 bytes instead of taking the
variable-length AEAD path.
Tests use RFC 8452 Appendix C.2 vectors and cover the tag, tampering and
IV length failure paths.
diff --git a/NEWS b/NEWS
index a35e2c83915..46f3219d12f 100644
--- a/NEWS
+++ b/NEWS
@@ -139,6 +139,9 @@ PHP NEWS
(ndossche)
. Fixed JIT ignoring exceptions from typed property set hooks. (David Carlier)
+- OpenSSL:
+ . Added AES-GCM-SIV support. (Rakdos8)
+
- PDO:
. Fixed PDOStatement::getColumnMeta() reading out of bounds for an invalid
column index. (Ilia Alshanetsky)
diff --git a/ext/openssl/openssl_backend_common.c b/ext/openssl/openssl_backend_common.c
index 75106130602..cced6f46815 100644
--- a/ext/openssl/openssl_backend_common.c
+++ b/ext/openssl/openssl_backend_common.c
@@ -1686,10 +1686,13 @@ void php_openssl_load_cipher_mode(struct php_openssl_cipher_mode *mode, const EV
switch (cipher_mode) {
case EVP_CIPH_GCM_MODE:
case EVP_CIPH_CCM_MODE:
- /* We check for EVP_CIPH_SIV_MODE and EVP_CIPH_SIV_MODE, because older OpenSSL and LibreSSL do not support them. */
+ /* We check for EVP_CIPH_SIV_MODE and EVP_CIPH_GCM_SIV_MODE, because older OpenSSL and LibreSSL do not support them. */
#ifdef EVP_CIPH_SIV_MODE
case EVP_CIPH_SIV_MODE:
#endif
+#ifdef EVP_CIPH_GCM_SIV_MODE
+ case EVP_CIPH_GCM_SIV_MODE:
+#endif
#ifdef EVP_CIPH_OCB_MODE
case EVP_CIPH_OCB_MODE:
/* For OCB mode, explicitly set the tag length even when decrypting,
@@ -1701,6 +1704,11 @@ void php_openssl_load_cipher_mode(struct php_openssl_cipher_mode *mode, const EV
mode->is_single_run_aead = cipher_mode == EVP_CIPH_CCM_MODE;
#ifdef EVP_CIPH_SIV_MODE
mode->aad_supports_vector = cipher_mode == EVP_CIPH_SIV_MODE;
+#endif
+#ifdef EVP_CIPH_GCM_SIV_MODE
+ /* GCM-SIV only takes a 96-bit nonce, but OpenSSL accepts any IV length for it and reads
+ * 12 bytes from the IV buffer, see https://github.com/openssl/openssl/commit/b8997895c2e8. */
+ mode->aead_fixed_iv_len = cipher_mode == EVP_CIPH_GCM_SIV_MODE;
#endif
break;
#ifdef NID_chacha20_poly1305
@@ -1719,6 +1727,12 @@ zend_result php_openssl_validate_iv(const char **piv, size_t *piv_len, size_t iv
char *iv_new;
if (mode->is_aead) {
+ if (mode->aead_fixed_iv_len && *piv_len != iv_required_len) {
+ php_error_docref(NULL, E_WARNING,
+ "IV passed is %zd bytes long, cipher expects an IV of precisely %zd bytes",
+ *piv_len, iv_required_len);
+ return FAILURE;
+ }
if (EVP_CIPHER_CTX_ctrl(cipher_ctx, mode->aead_ivlen_flag, *piv_len, NULL) <= 0) {
php_error_docref(NULL, E_WARNING, "Setting of IV length for AEAD mode failed");
return FAILURE;
diff --git a/ext/openssl/php_openssl_backend.h b/ext/openssl/php_openssl_backend.h
index 1e43fd79c6b..ae154f89537 100644
--- a/ext/openssl/php_openssl_backend.h
+++ b/ext/openssl/php_openssl_backend.h
@@ -359,6 +359,7 @@ struct php_openssl_cipher_mode {
bool set_tag_length_always;
bool set_tag_length_when_encrypting;
bool aad_supports_vector;
+ bool aead_fixed_iv_len;
int aead_get_tag_flag;
int aead_set_tag_flag;
int aead_ivlen_flag;
diff --git a/ext/openssl/tests/cipher_tests.inc b/ext/openssl/tests/cipher_tests.inc
index 9d17e847bfa..ffc57ad2155 100644
--- a/ext/openssl/tests/cipher_tests.inc
+++ b/ext/openssl/tests/cipher_tests.inc
@@ -213,6 +213,33 @@ $php_openssl_cipher_tests = array(
'21ad626dc866cc539f2d0e34b6824fc3',
),
),
+ /* RFC 8452 Appendix C.2 vectors for AES-256-GCM-SIV. */
+ 'aes-256-gcm-siv' => array(
+ array(
+ 'key' => '0100000000000000000000000000000000000000000000000000000000000000',
+ 'iv' => '030000000000000000000000',
+ 'aad' => '',
+ 'tag' => '07f5f4169bbf55a8400cd47ea6fd400f',
+ 'pt' => '',
+ 'ct' => '',
+ ),
+ array(
+ 'key' => '0100000000000000000000000000000000000000000000000000000000000000',
+ 'iv' => '030000000000000000000000',
+ 'aad' => '',
+ 'tag' => '843122130f7364b761e0b97427e3df28',
+ 'pt' => '0100000000000000',
+ 'ct' => 'c2ef328e5c71c83b',
+ ),
+ array(
+ 'key' => '0100000000000000000000000000000000000000000000000000000000000000',
+ 'iv' => '030000000000000000000000',
+ 'aad' => '01',
+ 'tag' => '91213f267e3b452f02d01ae33e4ec854',
+ 'pt' => '0200000000000000',
+ 'ct' => '1de22967237a8132',
+ ),
+ ),
'chacha20-poly1305' => array(
array(
'key' => '808182838485868788898a8b8c8d8e8f' .
diff --git a/ext/openssl/tests/openssl_decrypt_gcm_siv.phpt b/ext/openssl/tests/openssl_decrypt_gcm_siv.phpt
new file mode 100644
index 00000000000..0173748a9c9
--- /dev/null
+++ b/ext/openssl/tests/openssl_decrypt_gcm_siv.phpt
@@ -0,0 +1,69 @@
+--TEST--
+openssl_decrypt() with GCM-SIV cipher algorithm tests
+--EXTENSIONS--
+openssl
+--SKIPIF--
+<?php
+if (!in_array('aes-256-gcm-siv', openssl_get_cipher_methods())) {
+ die("skip aes-256-gcm-siv not available (requires OpenSSL >= 3.2)");
+}
+?>
+--FILE--
+<?php
+require_once __DIR__ . "/cipher_tests.inc";
+$method = 'aes-256-gcm-siv';
+$tests = openssl_get_cipher_tests($method);
+
+foreach ($tests as $idx => $test) {
+ echo "TEST $idx\n";
+ $pt = openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
+ $test['iv'], $test['tag'], $test['aad']);
+ var_dump($test['pt'] === $pt);
+}
+
+// Use the last vector (non-empty pt + AAD) for tampering checks.
+echo "TEST WRONGTAG\n";
+$bad_tag = $test['tag'];
+$bad_tag[0] = chr(ord($bad_tag[0]) ^ 0x01);
+var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
+ $test['iv'], $bad_tag, $test['aad']));
+
+echo "TEST WRONGCT\n";
+$bad_ct = $test['ct'];
+$bad_ct[0] = chr(ord($bad_ct[0]) ^ 0x01);
+var_dump(openssl_decrypt($bad_ct, $method, $test['key'], OPENSSL_RAW_DATA,
+ $test['iv'], $test['tag'], $test['aad']));
+
+echo "TEST WRONGNONCE\n";
+$bad_iv = $test['iv'];
+$bad_iv[0] = chr(ord($bad_iv[0]) ^ 0x01);
+var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
+ $bad_iv, $test['tag'], $test['aad']));
+
+echo "TEST WRONGAAD\n";
+var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
+ $test['iv'], $test['tag'], 'unexpected aad'));
+
+echo "TEST SHORTNONCE\n";
+var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA,
+ substr($test['iv'], 0, 11), $test['tag'], $test['aad']));
+?>
+--EXPECTF--
+TEST 0
+bool(true)
+TEST 1
+bool(true)
+TEST 2
+bool(true)
+TEST WRONGTAG
+bool(false)
+TEST WRONGCT
+bool(false)
+TEST WRONGNONCE
+bool(false)
+TEST WRONGAAD
+bool(false)
+TEST SHORTNONCE
+
+Warning: openssl_decrypt(): IV passed is 11 bytes long, cipher expects an IV of precisely 12 bytes in %s on line %d
+bool(false)
diff --git a/ext/openssl/tests/openssl_encrypt_gcm_siv.phpt b/ext/openssl/tests/openssl_encrypt_gcm_siv.phpt
new file mode 100644
index 00000000000..d9cccf29e07
--- /dev/null
+++ b/ext/openssl/tests/openssl_encrypt_gcm_siv.phpt
@@ -0,0 +1,60 @@
+--TEST--
+openssl_encrypt() with GCM-SIV cipher algorithm tests
+--EXTENSIONS--
+openssl
+--SKIPIF--
+<?php
+if (!in_array('aes-256-gcm-siv', openssl_get_cipher_methods())) {
+ die("skip aes-256-gcm-siv not available (requires OpenSSL >= 3.2)");
+}
+?>
+--FILE--
+<?php
+require_once __DIR__ . "/cipher_tests.inc";
+$method = 'aes-256-gcm-siv';
+$tests = openssl_get_cipher_tests($method);
+
+foreach ($tests as $idx => $test) {
+ echo "TEST $idx\n";
+ $ct = openssl_encrypt($test['pt'], $method, $test['key'], OPENSSL_RAW_DATA,
+ $test['iv'], $tag, $test['aad'], strlen($test['tag']));
+ var_dump($test['ct'] === $ct);
+ var_dump($test['tag'] === $tag);
+}
+
+// Failing when IV is not 12 bytes
+var_dump(openssl_encrypt('data', $method, str_repeat('x', 32), 0, '', $tag));
+var_dump(openssl_encrypt('data', $method, str_repeat('x', 32), 0, 'x', $tag));
+var_dump(openssl_encrypt('data', $method, str_repeat('x', 32), 0, str_repeat('x', 13), $tag));
+
+// Failing to retrieve tag (max is 16 bytes)
+var_dump(openssl_encrypt('data', $method, str_repeat('x', 32), 0, str_repeat('x', 12), $tag, '', 20));
+
+// Failing when no tag supplied
+var_dump(openssl_encrypt('data', $method, str_repeat('x', 32), 0, str_repeat('x', 12)));
+?>
+--EXPECTF--
+TEST 0
+bool(true)
+bool(true)
+TEST 1
+bool(true)
+bool(true)
+TEST 2
+bool(true)
+bool(true)
+
+Warning: openssl_encrypt(): IV passed is 0 bytes long, cipher expects an IV of precisely 12 bytes in %s on line %d
+bool(false)
+
+Warning: openssl_encrypt(): IV passed is 1 bytes long, cipher expects an IV of precisely 12 bytes in %s on line %d
+bool(false)
+
+Warning: openssl_encrypt(): IV passed is 13 bytes long, cipher expects an IV of precisely 12 bytes in %s on line %d
+bool(false)
+
+Warning: openssl_encrypt(): Retrieving verification tag failed in %s on line %d
+bool(false)
+
+Warning: openssl_encrypt(): A tag should be provided when using AEAD mode in %s on line %d
+bool(false)