-
Notifications
You must be signed in to change notification settings - Fork 8k
Properly initialize AEAD cipher flags in OpenSSL backend #20853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1637,6 +1637,13 @@ 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)); | ||
|
|
||
| #if defined(EVP_CIPH_FLAG_AEAD_CIPHER) | ||
| if (EVP_CIPHER_flags(cipher_type) & EVP_CIPH_FLAG_AEAD_CIPHER) { | ||
| php_openssl_set_aead_flags(mode); | ||
| } | ||
| #endif | ||
|
|
||
| switch (cipher_mode) { | ||
| case EVP_CIPH_GCM_MODE: | ||
| case EVP_CIPH_CCM_MODE: | ||
|
|
@@ -1797,7 +1804,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
+1807
to
+1809
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| php_openssl_store_errors(); | ||
| php_error_docref(NULL, E_WARNING, "Setting of additional application data failed"); | ||
| return FAILURE; | ||
|
|
||
| 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 = ''; | ||
| $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!' | ||
|
|
| 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!' | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to add
EVP_CIPH_SIV_MODEto the switch if exists and set the right mode if there are some differences.