Repository navigation
ChaCha20Poly1305 support - #3680
Conversation
|
@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
left a comment
There was a problem hiding this comment.
First pass, didn't review the core code :D
|
|
||
| .. module:: cryptography.hazmat.primitives.ciphers.aead | ||
|
|
||
| Authenticated encryption with additional data allows the user to guarantee |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Is 12 bytes enough for random nonces?
There was a problem hiding this comment.
It's a bit low, but we do it for GCM as well. Suggestion?
| >>> key = ChaCha20Poly1305.generate_key() | ||
| >>> chacha = ChaCha20Poly1305(key) | ||
| >>> nonce = os.urandom(12) | ||
| >>> ct, tag = chacha.encrypt(nonce, data, aad) |
There was a problem hiding this comment.
Should we just define the output as ct + tag? That's what go appears to do.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
(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 |
There was a problem hiding this comment.
This last parameter seems like it can be dropped.
|
|
||
| class ChaCha20Poly1305(object): | ||
| def __init__(self, key): | ||
| if len(key) != 32: |
| key = binascii.unhexlify(vector["key"]) | ||
| nonce = binascii.unhexlify(vector["nonce"]) | ||
| if vector["ad"].startswith(b"\""): | ||
| aad = vector["ad"].strip(b"\"") |
There was a problem hiding this comment.
This shouldn't be a strip, just a [1:-1]` I think.
| if vector["ad"].startswith(b"\""): | ||
| aad = vector["ad"].strip(b"\"") | ||
| else: | ||
| aad = binascii.unhexlify(vector["ad"].strip(b"\"")) |
There was a problem hiding this comment.
strip doesn't do anything here does it?
|
Calling a real cryptographer: @lvh.
At 96-bits you can expect a repeat with 50% probability at 2^48 nonces. So
if you cap how much shit you encrypt with the key maybe it's fine?
…On Sun, Jun 4, 2017 at 11:12 PM, Paul Kehrer ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In docs/hazmat/primitives/aead.rst
<#3680 (comment)>:
> +
+ The ChaCha20Poly1305 construction is defined in :rfc:`7539` section 2.8.
+ It is a stream cipher combined with a MAC that offers strong integrity
+ guarantees.
+
+ :param bytes key: A 32-byte key. This **must** be kept secret.
+
+ .. doctest::
+
+ >>> import os
+ >>> from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
+ >>> data = b"a secret message"
+ >>> aad = b"authenticated but unencrypted data"
+ >>> key = ChaCha20Poly1305.generate_key()
+ >>> chacha = ChaCha20Poly1305(key)
+ >>> nonce = os.urandom(12)
It's a bit low, but we do it for GCM as well. Suggestion?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3680 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAADBK7R5E8ULJUnP2DQtURwqItcVyHbks5sA3I7gaJpZM4NvlqL>
.
--
"I disapprove of what you say, but I will defend to the death your right to
say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
"The people's good is the highest law." -- Cicero
GPG Key fingerprint: D1B3 ADC0 E023 8CA6
|
|
Counterpoint, both Go, Ring, and BoringSSL's AEAD APIs just concatenate
them.
…On Sun, Jun 4, 2017 at 11:14 PM, Paul Kehrer ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In docs/hazmat/primitives/aead.rst
<#3680 (comment)>:
> + The ChaCha20Poly1305 construction is defined in :rfc:`7539` section 2.8.
+ It is a stream cipher combined with a MAC that offers strong integrity
+ guarantees.
+
+ :param bytes key: A 32-byte key. This **must** be kept secret.
+
+ .. doctest::
+
+ >>> import os
+ >>> from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
+ >>> data = b"a secret message"
+ >>> aad = b"authenticated but unencrypted data"
+ >>> key = ChaCha20Poly1305.generate_key()
+ >>> chacha = ChaCha20Poly1305(key)
+ >>> nonce = os.urandom(12)
+ >>> ct, tag = chacha.encrypt(nonce, data, aad)
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).
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3680 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAADBDgesAM-8_q0TRmDtseGoW7uDKU2ks5sA3J8gaJpZM4NvlqL>
.
--
"I disapprove of what you say, but I will defend to the death your right to
say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
"The people's good is the highest law." -- Cicero
GPG Key fingerprint: D1B3 ADC0 E023 8CA6
|
| >>> aad = b"authenticated but unencrypted data" | ||
| >>> key = ChaCha20Poly1305.generate_key() | ||
| >>> chacha = ChaCha20Poly1305(key) | ||
| >>> nonce = os.urandom(12) |
| >>> key = ChaCha20Poly1305.generate_key() | ||
| >>> chacha = ChaCha20Poly1305(key) | ||
| >>> nonce = os.urandom(12) | ||
| >>> ct, tag = chacha.encrypt(nonce, data, aad) |
There was a problem hiding this comment.
(moving my comment); BoringSSL, ring, and Go all do the concat
|
|
||
| .. classmethod:: generate_key() | ||
|
|
||
| Generates a random ChaCha20Poly1305 key using ``os.urandom``. |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Write the last param as operation == _ENCRYPT so it's clear what's going on
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
check_bytes should be private, add a leading underscore
| self._key, nonce, tag, data, associated_data | ||
| ) | ||
|
|
||
| def _check_vars(self, nonce, data, associated_data): |
| [b"0" * 12, b"data", object()] | ||
| ] | ||
| ) | ||
| def test_vars_not_bytes_encrypt(self, nonce, data, associated_data): |
| def test_boringssl_vectors(self, vector, backend): | ||
| key = binascii.unhexlify(vector["key"]) | ||
| nonce = binascii.unhexlify(vector["nonce"]) | ||
| if vector["ad"].startswith(b"\""): |
There was a problem hiding this comment.
If you write the quotes as ' you don't need to escape
| aad = binascii.unhexlify(vector["ad"]) | ||
| tag = binascii.unhexlify(vector["tag"]) | ||
| if vector["in"].startswith(b"\""): | ||
| pt = vector["in"].strip(b"\"") |
There was a problem hiding this comment.
swap the strip out for slicing here.
|
`int(operation == _ENCRYPT)` then. My real point is that this fucntion
takes an int, masquerasing as a bool, and our constants aren't obviously
tied to it.
…On Mon, Jun 5, 2017 at 8:15 PM, Paul Kehrer ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/cryptography/hazmat/backends/openssl/chacha20poly1305.py
<#3680 (comment)>:
> + res = backend._lib.EVP_CIPHER_CTX_ctrl(
+ ctx, backend._lib.EVP_CTRL_AEAD_SET_IVLEN, len(nonce),
+ backend._ffi.NULL
+ )
+ backend.openssl_assert(res != 0)
+ if operation == _DECRYPT:
+ if tag is None or len(tag) != 16:
+ raise ValueError("Tag must be 16 bytes")
+
+ res = backend._lib.EVP_CIPHER_CTX_ctrl(
+ ctx, backend._lib.EVP_CTRL_AEAD_SET_TAG, len(tag), tag
+ )
+ backend.openssl_assert(res != 0)
+
+ res = backend._lib.EVP_CipherInit_ex(
+ ctx, backend._ffi.NULL, backend._ffi.NULL, key, nonce, operation
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.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3680 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAADBG4y6vR_OKC-MsOYOq1YNLXIUGv2ks5sBJo8gaJpZM4NvlqL>
.
--
"I disapprove of what you say, but I will defend to the death your right to
say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
"The people's good is the highest law." -- Cicero
GPG Key fingerprint: D1B3 ADC0 E023 8CA6
|
|
outstanding questions remain on the generation of the nonce for the doctest example and whether we should concat or not. |
reaperhulk
left a comment
There was a problem hiding this comment.
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(): |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
mmmm. is there a way you'd prefer to handle it?
There was a problem hiding this comment.
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.
18d758f to
2f2d06b
Compare
|
D'oh, coverage isn't showing files and we do have it pinned here. 😢 |
if it's not supported.
2f2d06b to
aa404f9
Compare
|
|
||
| Reuse of a ``nonce`` with a given ``key`` compromises the security | ||
| of any message with that ``nonce`` and ``key`` pair. | ||
|
|
There was a problem hiding this comment.
This needs like a sentence of prose :-)
| :returns bytes: The ciphertext bytes with the 16 byte tag appended. | ||
|
|
||
| .. method:: decrypt(nonce, data, associated_data) | ||
|
|
| ``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 |
| backend._ffi.NULL, | ||
| backend._ffi.NULL, | ||
| backend._ffi.NULL, | ||
| operation |
There was a problem hiding this comment.
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: |
| ) | ||
| backend.openssl_assert(res != 0) | ||
| if operation == _DECRYPT: | ||
| assert len(tag) == 16 |
| skip_message="Requires OpenSSL without ChaCha20Poly1305 support" | ||
| ) | ||
| @pytest.mark.requires_backend_interface(interface=CipherBackend) | ||
| def test_chacha20poly1305_unsupported_on_older_openssl(backend): |
There was a problem hiding this comment.
mmmm. is there a way you'd prefer to handle it?
| def __init__(self, key): | ||
| if not backend.chacha20poly1305_supported(): | ||
| raise exceptions.UnsupportedAlgorithm( | ||
| "ChaCha20Poly1305 is not supported by this version of OpenSSL." |
There was a problem hiding this comment.
Should probably provide the second "reason" argument.
Fixes #3667