Skip to content

Commit 6c6a58e

Browse files
committed
Allow passing $tag for non-authenticated encryption
openssl_encrypt() currently throws a warning if the $tag out parameter is passed for a non-authenticated cipher. This violates the principle that a function should behave the same if a parameter is not passed, and if the default value is passed for the parameter. I believe this warning should simply be dropped and the $tag be populated with null, as is already the case. Otherwise, it is not possible to use openssl_encrypt() in generic wrapper APIs, that are compatible with both authenticated and non-authenticated encryption. Closes GH-6333.
1 parent d9058b6 commit 6c6a58e

File tree

4 files changed

+7
-13
lines changed

4 files changed

+7
-13
lines changed

ext/openssl/openssl.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6744,8 +6744,6 @@ PHP_OPENSSL_API zend_string* php_openssl_encrypt(char *data, size_t data_len, ch
67446744
}
67456745
} else if (tag) {
67466746
ZEND_TRY_ASSIGN_REF_NULL(tag);
6747-
php_error_docref(NULL, E_WARNING,
6748-
"The authenticated tag cannot be provided for cipher that doesn not support AEAD");
67496747
} else if (mode.is_aead) {
67506748
php_error_docref(NULL, E_WARNING, "A tag should be provided when using AEAD mode");
67516749
zend_string_release_ex(outbuf, 0);

ext/openssl/tests/openssl_decrypt_basic.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ var_dump(rtrim($output));
2828
$encrypted = openssl_encrypt($data, "bf-ecb", $password, OPENSSL_DONT_ZERO_PAD_KEY);
2929
$output = openssl_decrypt($encrypted, "bf-ecb", $password, OPENSSL_DONT_ZERO_PAD_KEY);
3030
var_dump($output);
31+
32+
// It's okay to pass $tag for a non-authenticated cipher.
33+
// It will be populated with null in that case.
34+
openssl_encrypt($data, $method, $password, 0, $iv, $tag);
35+
var_dump($tag);
36+
3137
?>
3238
--EXPECT--
3339
string(45) "openssl_encrypt() and openssl_decrypt() tests"
3440
string(45) "openssl_encrypt() and openssl_decrypt() tests"
3541
string(45) "openssl_encrypt() and openssl_decrypt() tests"
3642
string(45) "openssl_encrypt() and openssl_decrypt() tests"
43+
NULL

ext/openssl/tests/openssl_decrypt_error.phpt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ var_dump(openssl_decrypt(array(), $method, $password));
2323
var_dump(openssl_decrypt($encrypted, array(), $password));
2424
var_dump(openssl_decrypt($encrypted, $method, array()));
2525

26-
// invalid using of an authentication tag
27-
var_dump(openssl_encrypt($data, $method, $password, 0, $iv, $wrong));
2826
?>
2927
--EXPECTF--
3028
Warning: openssl_encrypt(): Using an empty Initialization Vector (iv) is potentially insecure and not recommended in %s on line %d
@@ -53,6 +51,3 @@ NULL
5351

5452
Warning: openssl_decrypt() expects parameter 3 to be string, array given in %s on line %d
5553
NULL
56-
57-
Warning: openssl_encrypt(): The authenticated tag cannot be provided for cipher that doesn not support AEAD in %s on line %d
58-
string(44) "yof6cPPH4mLee6TOc0YQSrh4dvywMqxGUyjp0lV6+aM="

ext/openssl/tests/openssl_encrypt_error.phpt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ var_dump(openssl_encrypt($arr, $method, $object));
2121
var_dump(openssl_encrypt($data, $arr, $object));
2222
var_dump(openssl_encrypt($data, $method, $arr));
2323

24-
// invalid using of an authentication tag
25-
var_dump(openssl_encrypt($data, $method, $password, 0, $iv, $wrong));
26-
2724
// padding of the key is disabled
2825
var_dump(openssl_encrypt($data, $method, $password, OPENSSL_DONT_ZERO_PAD_KEY, $iv));
2926
?>
@@ -49,8 +46,5 @@ NULL
4946
Warning: openssl_encrypt() expects parameter 3 to be string, array given in %s on line %d
5047
NULL
5148

52-
Warning: openssl_encrypt(): The authenticated tag cannot be provided for cipher that doesn not support AEAD in %s on line %d
53-
string(44) "iPR4HulskuaP5Z6me5uImk6BqVyJG73+63tkPauVZYk="
54-
5549
Warning: openssl_encrypt(): Key length cannot be set for the cipher method in %s on line %d
5650
bool(false)

0 commit comments

Comments
 (0)