# RFC: OpenSSL AEAD support

- Status: Implemented (PHP 7.1)
- Authors: Jakub Zelenka, bukka@php.net
- Date: 2016-01-02
- Version: 0.2
- Original: https://wiki.php.net/rfc/openssl_aead
- Page: https://externals.io/rfc/openssl_aead
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Include AEAD support to OpenSSL extension (closed)

Yes: 7 · No: 4

Voters: aeoris (Yes), ajf (No), bishop (Yes), bukka (Yes), bwoebi (No), galvao (Yes), gasolwu (No), leigh (Yes), mrook (Yes), ocramius (No), thorstenr (Yes)

## Discussions on the mailing list

- [\[RFC\] OpenSSL AEAD support](https://externals.io/message/90194.md) — started on 2016-01-06, 8 messages
- [\[RFC\]\[VOTE\] OpenSSL AEAD support](https://externals.io/message/90725.md) — started on 2016-01-20, 9 messages
- [Should we rethink the 50%+1 requirement for non-"language changes"?](https://externals.io/message/91020.md) — started on 2016-01-30, 14 messages

---

## Introduction

The PHP OpenSSL extension provides functions for data encryption (openssl\_encrypt) and decryption (openssl\_decrypt). These function works fine for all cipher algorithms (cipher + mode) except AEAD (Authenticated Encrypt with Associated Data) modes. These modes requires special handling in OpenSSL and a need for supplying resp. retrieving of the authenticated tag and optionally AAD (associated application data).

There are two AEAD modes supported by OpenSSL (up to version 1.0.2) - GCM (Galois Counter Mode) and CCM (Counter with CBC-MAC). Both of these modes currently fails on decryption as there is no way how to supply an authentication tag and internal OpenSSL API doesn't allow it to use it in the same way (e.g. there is no context finalization for CCM mode).

## Proposal

This RFC proposes adding extra parameters to the openssl\_encrypt resp. openssl\_decrypt for retrieving resp. supplying an authenticated tag and AAD. These parameters are optional and are used only for supported AEAD modes (GCM and CCM). If a tag is used for any modes that doesn't support AEAD, then the warning is triggered.

The parameters differs for each function.

#### Encryption

The new prototype for openssl\_encrypt is following:

```php
string openssl_encrypt ( string $data , string $method , string $password 
    [, int $options = 0 [, string $iv = "" [, string &$tag = NULL [, string $aad = "" [, int $tag_length = 16 ]]]])
```

##### New parameters description

- $tag - The authentication tag will be saved to the variable passed as a reference on successful encryption. If the encryption fails, then the variable is unchanged. The resulted tag length is the same as the length supplied in the $tag\_length parameter which default to 16.
- $aad - Additional authentication data.
- $tag\_length - The tag length can be set before the encryption and can be between 4 and 16 for GCM mode where it is the same like trimming the tag. On the other side the CCM has no such limits and also the resulted tag is different for each length.

#### Decryption

The new prototype for openssl\_decrypt is following:

```php
string openssl_decrypt ( string $data , string $method , string $password 
    [, int $options = 0 [, string $iv = "" [, string $tag = "" [, string $aad = "" ]]]] )
```

##### New parameters description

- $tag - The authentication tag that will be authenticated. If it's incorrect, then the authentication fails and the function returns FALSE.
- $aad - Additional authentication data.

## Backward Incompatible Changes

none

## Proposed PHP Version(s)

PHP 7.1

## RFC Impact

### To SAPIs

none

### To Existing Extensions

none

### To Opcache

none

### New Constants

none

## Open Issues

Tag length and parameters order.

## Unaffected PHP Functionality

The current encryption and decryption is unaffected. The new parameters are optional.

## Future Scope

Adding support for OCB mode once the extension supports OpenSSL 1.1

## Voting

Required 50%+1 majority for accepting this proposal.

**Vote: Include AEAD support to OpenSSL extension** — Yes: 7 · No: 4 (closed)

Voting started on January 20th at 17:00 UTC and ended on January 27th at 17:00 UTC.

## Patches and Tests

<https://github.com/php/php-src/pull/1716>

## Implementation

OpenSSL AEAD support has been implemented in PHP 7.1 with [commit 054b27d3](http://git.php.net/?p=php-src.git;a=commit;h=054b27d3), and is documented in the [openssl\_encrypt()](http://php.net/manual/function.openssl-encrypt.php) and [openssl\_decrypt()](http://php.net/manual/function.openssl-decrypt.php) man pages.

The PHP language specification is not affected by this RFC.

## References

- <https://bugs.php.net/bug.php?id=67304>

## Rejected Features

There has been discussion about introducing an object that would wrap the context and offered functions for setting tag, AAD, key, IV and making partials updates. However such functionality is already implemented in crypto extension and requires much more code (about extra 1000 lines) to address all possible exceptions. The main thing is that this is not contradicting to this proposal as it could easily co-exist as we will still have to keep openssl\_ecrypt and openssl\_decrypt working. This proposal is just about extending these two function for AEAD mode support.

It's been suggested that new functions openssl\_ecrypt\_aead and openss\_decrytp\_aead should be introduced instead of adding new parameters to prevent confusion for users that tries to use a tag with non AEAD modes. That has been rejected in favour of adding warning when tag supplied for non AEAD modes.
