Skip to content

ChaCha20Poly1305 support - #3680

Merged
alex merged 14 commits into
pyca:masterfrom
reaperhulk:chacha20poly1305
Jun 8, 2017
Merged

alex merged 14 commits into
pyca:masterfrom
reaperhulk:chacha20poly1305

Conversation

@reaperhulk

Copy link
Copy Markdown
Member

Fixes #3667

@mention-bot

Copy link
Copy Markdown

@reaperhulk, thanks for your PR! By analyzing the history of the files in this pull request, we identified @alex, @public and @palaviv to be potential reviewers.

@alex alex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass, didn't review the core code :D

Comment thread docs/hazmat/primitives/aead.rst Outdated

.. module:: cryptography.hazmat.primitives.ciphers.aead

Authenticated encryption with additional data allows the user to guarantee

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the initialism in parens.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd dumb the description down "Authenticated encryption with additional data are encryption schemes which provide both confidentiality and integrity for their ciphertext, and also support providing integrity for additional data which is not encrypted"

>>> aad = b"authenticated but unencrypted data"
>>> key = ChaCha20Poly1305.generate_key()
>>> chacha = ChaCha20Poly1305(key)
>>> nonce = os.urandom(12)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 12 bytes enough for random nonces?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit low, but we do it for GCM as well. Suggestion?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paging @lvh to the courtesy phone.

Comment thread docs/hazmat/primitives/aead.rst Outdated
>>> key = ChaCha20Poly1305.generate_key()
>>> chacha = ChaCha20Poly1305(key)
>>> nonce = os.urandom(12)
>>> ct, tag = chacha.encrypt(nonce, data, aad)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just define the output as ct + tag? That's what go appears to do.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then take that as input to decrypt? I'm a bit reluctant to concatenate when we already have them as separate things (and the spec has no opinion, unfortunately).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(moving my comment); BoringSSL, ring, and Go all do the concat

additional_data):
return chacha20poly1305.decrypt(
self, key, nonce, tag, data, additional_data,
chacha20poly1305._DECRYPT

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This last parameter seems like it can be dropped.


class ChaCha20Poly1305(object):
def __init__(self, key):
if len(key) != 32:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if key is bytes I guess.

Comment thread tests/hazmat/primitives/test_aead.py Outdated
key = binascii.unhexlify(vector["key"])
nonce = binascii.unhexlify(vector["nonce"])
if vector["ad"].startswith(b"\""):
aad = vector["ad"].strip(b"\"")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be a strip, just a [1:-1]` I think.

Comment thread tests/hazmat/primitives/test_aead.py Outdated
if vector["ad"].startswith(b"\""):
aad = vector["ad"].strip(b"\"")
else:
aad = binascii.unhexlify(vector["ad"].strip(b"\""))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strip doesn't do anything here does it?

@alex

alex commented Jun 5, 2017 via email

Copy link
Copy Markdown
Member

@alex

alex commented Jun 5, 2017 via email

Copy link
Copy Markdown
Member

>>> aad = b"authenticated but unencrypted data"
>>> key = ChaCha20Poly1305.generate_key()
>>> chacha = ChaCha20Poly1305(key)
>>> nonce = os.urandom(12)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paging @lvh to the courtesy phone.

Comment thread docs/hazmat/primitives/aead.rst Outdated
>>> key = ChaCha20Poly1305.generate_key()
>>> chacha = ChaCha20Poly1305(key)
>>> nonce = os.urandom(12)
>>> ct, tag = chacha.encrypt(nonce, data, aad)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(moving my comment); BoringSSL, ring, and Go all do the concat

Comment thread docs/hazmat/primitives/aead.rst Outdated

.. classmethod:: generate_key()

Generates a random ChaCha20Poly1305 key using ``os.urandom``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to mention the how? Maybe start the description with "securely" :-)



def _chacha20poly1305_setup(backend, key, nonce, tag, operation):
evp_cipher = backend._lib.EVP_get_cipherbyname(b"chacha20-poly1305")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably needs to check that it's actually supported :-)

backend.openssl_assert(res != 0)

res = backend._lib.EVP_CipherInit_ex(
ctx, backend._ffi.NULL, backend._ffi.NULL, key, nonce, operation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write the last param as operation == _ENCRYPT so it's clear what's going on

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make the change but I definitely disagree about that being more clear. Understanding the new form requires knowing that a bool isn't a real thing in Python.


class ChaCha20Poly1305(object):
def __init__(self, key):
utils.check_bytes("key", key)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check_bytes should be private, add a leading underscore

self._key, nonce, tag, data, associated_data
)

def _check_vars(self, nonce, data, associated_data):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/vars/params/ I think

Comment thread tests/hazmat/primitives/test_aead.py Outdated
[b"0" * 12, b"data", object()]
]
)
def test_vars_not_bytes_encrypt(self, nonce, data, associated_data):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/vars/params/

Comment thread tests/hazmat/primitives/test_aead.py Outdated
def test_boringssl_vectors(self, vector, backend):
key = binascii.unhexlify(vector["key"])
nonce = binascii.unhexlify(vector["nonce"])
if vector["ad"].startswith(b"\""):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you write the quotes as ' you don't need to escape

Comment thread tests/hazmat/primitives/test_aead.py Outdated
aad = binascii.unhexlify(vector["ad"])
tag = binascii.unhexlify(vector["tag"])
if vector["in"].startswith(b"\""):
pt = vector["in"].strip(b"\"")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swap the strip out for slicing here.

@alex

alex commented Jun 6, 2017 via email

Copy link
Copy Markdown
Member

@reaperhulk

Copy link
Copy Markdown
Member Author

outstanding questions remain on the generation of the nonce for the doctest example and whether we should concat or not.

@reaperhulk reaperhulk left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still some outstanding review feedback + the new comments I just added that we should discuss as well.


class ChaCha20Poly1305(object):
def __init__(self, key):
if not backend.chacha20poly1305_supported():

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach has some drawbacks in the tests.

skip_message="Requires OpenSSL without ChaCha20Poly1305 support"
)
@pytest.mark.requires_backend_interface(interface=CipherBackend)
def test_chacha20poly1305_unsupported_on_older_openssl(backend):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use the supported mark we need to parametrize with backend even though we never use the arg. Additionally, we have to supply an interface to requires_backend_interface even though there is no interface associated with ChaCha20Poly1305 support. So this test file has a lot of pointless arguments that exist only to make the supported mark skip things on the right versions of OpenSSL.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmmm. is there a way you'd prefer to handle it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really have a better answer right now, it's just something to be aware of until we (eventually) change this. In this PR we're using CipherBackend but in X25519 it's DHBackend...just because they're vaguely related I guess.

@reaperhulk
reaperhulk force-pushed the chacha20poly1305 branch 2 times, most recently from 18d758f to 2f2d06b Compare June 7, 2017 05:38
@reaperhulk

Copy link
Copy Markdown
Member Author

D'oh, coverage isn't showing files and we do have it pinned here. 😢


Reuse of a ``nonce`` with a given ``key`` compromises the security
of any message with that ``nonce`` and ``key`` pair.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs like a sentence of prose :-)

:returns bytes: The ciphertext bytes with the 16 byte tag appended.

.. method:: decrypt(nonce, data, associated_data)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Comment thread docs/hazmat/primitives/aead.rst Outdated
``None`` if none was passed during encryption.
:returns bytes: The original plaintext.
:raises cryptography.exceptions.InvalidTag: If the authentication tag
doesn't match this exception will be raised. This will occur when

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't match what :-)

backend._ffi.NULL,
backend._ffi.NULL,
backend._ffi.NULL,
operation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about operation == _ENCRYPT.

utils._check_bytes("nonce", nonce)
utils._check_bytes("data", data)
utils._check_bytes("associated_data", associated_data)
if not len(nonce) == 12:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!=

)
backend.openssl_assert(res != 0)
if operation == _DECRYPT:
assert len(tag) == 16

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is reachable.

skip_message="Requires OpenSSL without ChaCha20Poly1305 support"
)
@pytest.mark.requires_backend_interface(interface=CipherBackend)
def test_chacha20poly1305_unsupported_on_older_openssl(backend):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmmm. is there a way you'd prefer to handle it?

@alex alex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last comment I think!

def __init__(self, key):
if not backend.chacha20poly1305_supported():
raise exceptions.UnsupportedAlgorithm(
"ChaCha20Poly1305 is not supported by this version of OpenSSL."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably provide the second "reason" argument.

@alex
alex merged commit 7e53d91 into pyca:master Jun 8, 2017
@alex alex added this to the Twentieth release milestone Jun 8, 2017
@reaperhulk
reaperhulk deleted the chacha20poly1305 branch June 8, 2017 04:09
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Add ChaCha20Poly1305 AEAD Construction

3 participants