Add support for ChaCha20 with LibreSSL #9194
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for ChaCha20 with LibreSSL. Since cryptography's ChaCha20 uses OpenSSL's implementation (which uses a 64 bit counter + 64 bit nonce), and LibreSSL does the same, it is straightforward to use LibreSSL's API.
The only complication is that the current
_CipherContext
implementation assumes that the underlying cipher can be accessed through theEVP_CIPHER
API. This is not the case for LibreSSL's ChaCha20, which has a separate CRYPTO_chacha_20() API for it.In order to solve that, this PR makes
_CipherContext
an abstract class with two implementations,_CipherContextEVP
(which is the previous_CipherContext
) and_CipherContextChaCha
, which is a simple context that is used only for the LibreSSL+ChaCha20 combination.This means all of the code that uses
_CipherContext
remains the same, and we only have a single branching point isolated in thecreate_cipher_context()
function, which selects the correct context depending on the cipher+backend.It also leaves the option open in the future if we want to add BoringSSL's ChaCha20, since we can reuse the same
_CipherContextChaCha
for it (the API is almost the same, and we can deal with the differences in a C wrapper)