Skip to content

Commit 7de1357

Browse files
committed
properly initialize AEAD cipher flags in OpenSSL backend
1 parent cb63e4f commit 7de1357

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

ext/openssl/openssl_backend_common.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,13 @@ void php_openssl_load_cipher_mode(struct php_openssl_cipher_mode *mode, const EV
16371637
{
16381638
int cipher_mode = EVP_CIPHER_mode(cipher_type);
16391639
memset(mode, 0, sizeof(struct php_openssl_cipher_mode));
1640+
1641+
#if defined(EVP_CIPH_FLAG_AEAD_CIPHER)
1642+
if (EVP_CIPHER_flags(cipher_type) & EVP_CIPH_FLAG_AEAD_CIPHER) {
1643+
php_openssl_set_aead_flags(mode);
1644+
}
1645+
#endif
1646+
16401647
switch (cipher_mode) {
16411648
case EVP_CIPH_GCM_MODE:
16421649
case EVP_CIPH_CCM_MODE:

ext/openssl/tests/gh20851.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
openssl: AES-256-SIV AEAD tag and AAD roundtrip
3+
--EXTENSIONS--
4+
openssl
5+
--FILE--
6+
<?php
7+
$algo = 'aes-256-siv';
8+
$key = str_repeat('1', 64);
9+
$tag = '';
10+
$input = 'Hello world!';
11+
12+
$ciphertext = openssl_encrypt(
13+
'Hello world!',
14+
$algo,
15+
$key,
16+
OPENSSL_RAW_DATA,
17+
'', // IV is empty for this cipher in PHP
18+
$tag // gets filled with the SIV
19+
);
20+
21+
echo 'input: ' . $input . PHP_EOL;
22+
echo 'tag: ' . bin2hex($tag) . PHP_EOL;
23+
echo 'ciphertext: ' . bin2hex($ciphertext) . PHP_EOL;
24+
echo 'combined: ' . bin2hex($tag . $ciphertext) . PHP_EOL;
25+
26+
$dec = openssl_decrypt(
27+
$ciphertext,
28+
$algo,
29+
$key,
30+
OPENSSL_RAW_DATA,
31+
'',
32+
$tag
33+
);
34+
35+
echo 'decrypted: ' . var_export($dec, true) . PHP_EOL;
36+
?>
37+
--EXPECTF--
38+
input: Hello world!
39+
tag: f6c98e3e785947502a09994d2757f9c1
40+
ciphertext: a430a41a9bc089fa45ad27be
41+
combined: f6c98e3e785947502a09994d2757f9c1a430a41a9bc089fa45ad27be
42+
decrypted: 'Hello world!'
43+

0 commit comments

Comments
 (0)