Skip to content

[WIP] GCM support - #176

Merged
dreid merged 25 commits into
pyca:masterfrom
reaperhulk:gcm-support
Dec 4, 2013
Merged

dreid merged 25 commits into
pyca:masterfrom
reaperhulk:gcm-support

Conversation

@reaperhulk

Copy link
Copy Markdown
Member

Rebased and rewritten. I'm not happy with this API so let's discuss how to make it better! Here's what encryption/decryption looks like with the current model:

import binascii
key = binascii.unhexlify(b"0" * 32)
iv = binascii.unhexlify(b"0" * 24)
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
cipher = Cipher(algorithms.AES(key), modes.GCM(iv))
encryptor = cipher.encryptor()
encryptor.add_data(b"authenticated but encrypted payload")
ct = encryptor.update(b"a secret message") + encryptor.finalize()
tag = encryptor.tag
cipher = Cipher(algorithms.AES(key), modes.GCM(iv, tag))
decryptor = cipher.decryptor()
decryptor.add_data(b"authenticated but encrypted payload")
decryptor.update(ct) + decryptor.finalize()

When calling encryptor() or decryptor() you will get an AEADCipherContext object that has the methods from CipherContext but also adds add_data and tag.

Note that you must obtain the tag from the AEADCipherContext object and build a new GCM object (this time passing the tag) if you want to do an encrypt/decrypt cycle.

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 guess GCM isn't available with other ciphers?

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.

Not in OpenSSL.

—
Sent from Mailbox for iPhone

On Thu, Oct 24, 2013 at 11:40 AM, Alex Gaynor [email protected]
wrote:

@@ -153,6 +153,11 @@ def _register_default_ciphers(self):
mode_cls,
GetCipherByName("des-ede3-{mode.name}")
)

@reaperhulk

Copy link
Copy Markdown
Member Author

This comment is outdated.

This PR has now been rebased and reworked to fit in the new model we use for _CipherContexts in the backend. Outstanding items to discuss:

  • To decrypt data you must set a tag on the GCM object, but when encrypting the tag is set on the _AEADCipherContext. This is ugly, but setting it back on the mode object also seems ugly.
  • The "AEAD" interfaces in the OpenSSL backend are hardcoded to 128-bit block GCM. OpenSSL only supports AES with GCM or CCM as AEAD modes at this time. CCM is a 2 pass AEAD mode that requires you to supply the length of the data to encrypt before beginning encryption so we'll have to figure out how that fits in this model. Simplest solution would be to leave it as is and then when we implement CCM check the mode in the context creation.
  • Do we want to run the full GCM test vector suite? It adds over 47,000 tests and right now it looks like it's going to cause Travis to time out our pypy builds until the coverage speedups Alex did for pypy get released.

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 method duplicates a ton of logic from the superclass, and never calls the parent's implementation.

@alex

alex commented Oct 25, 2013

Copy link
Copy Markdown
Member

Can we split the vector loading and tests for that into a seperate patch?

@reaperhulk

Copy link
Copy Markdown
Member Author

Yeah we certainly can. This patch is still very much WIP overall, but I'll break the vector loader + tests out and submit them as a finalized PR tomorrow (assuming I actually think they're done).

@alex

alex commented Oct 25, 2013

Copy link
Copy Markdown
Member

Great!

On Thu, Oct 24, 2013 at 9:18 PM, Paul Kehrer [email protected]:

Yeah we certainly can. This patch is still very much WIP overall, but I'll
break the vector loader + tests out and submit them as a finalized PR
tomorrow (assuming I actually think they're done).

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/176#issuecomment-27060163
.

"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: 125F 5C67 DFE9 4084

Comment thread cryptography/primitives/interfaces.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When does it get? When does it set?

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 is one of the ugly parts of the PR right now (and the comment is inaccurate). At the moment if you want to decrypt you must set the tag in the GCM object. When encrypting, however, after calling finalize() it will set the tag on the context.

One possible solution would be to remove tag from the GCM object entirely and require users to get/set tag on the AEADContext. This would keep the tag location consistent and allow users to use their BlockCipher object as a factory for both encryption and decryption contexts. The GCM object itself would be reduced to only holding the IV though.

aes = ciphers.AES(b"0" * 16)
gcm = modes.GCM(b"0" * 12)
bc = BlockCipher(aes, gcm)
enc = bc.encryptor()
enc.add_data(b"test")
enc.finalize()
tag = enc.tag
dec = bc.decryptor()
dec.tag = tag
dec.add_data(b"test")
dec.finalize()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, abstractproperty, nevermind.

@alex

alex commented Nov 14, 2013

Copy link
Copy Markdown
Member

I think this is now ready to be rebased on master, all the dependent patches are in?

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 323f5f34467166166cad571fd0feb138dc923b07 on reaperhulk:gcm-support into 838ad7d on pyca:master.

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 doesn't look right. If we just malloc block_size // 8, there's not enough bytes for this. Probably no slicing is needed at all? just buffer(...)[:]?

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling ed51a9ee673e508086b109eb8f7e4698c2548a58 on reaperhulk:gcm-support into 838ad7d on pyca:master.

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.

The _tag properly shouldn't exist on _CipherContext IMO.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 893b4bde18fc8f5251a86ff46d11f4cedc5c3081 on reaperhulk:gcm-support into 838ad7d on pyca:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 358cc37a585bf7b79bae388cfb3d54b6602e73cd on reaperhulk:gcm-support into 8cf523e on pyca:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling c89d78561fd41c82792c4aaadd1ecee1460f7211 on reaperhulk:gcm-support into 9050172 on pyca:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 5b828b1 on reaperhulk:gcm-support into bdb6deb on pyca:master.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think you need this interface. Or the concrete implementation of it. Something either provides AEADCipherContext or AEADCipherContext & AEADEncryptionContext (maybe there is a better name for that which will relieve us of the apparent need for symmetry here?)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 5578c66 on reaperhulk:gcm-support into bdb6deb on pyca:master.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AEADDecryptionContext is no more?

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.

Oops, will remove.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling cd28a7c on reaperhulk:gcm-support into bdb6deb on pyca:master.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Referenced recommendations should be provide links.

Clarify that 96-bit IV is only recommended in performance critical
situations...otherwise feel free to use something longer.
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 6728437 on reaperhulk:gcm-support into bdb6deb on pyca:master.

dreid added a commit that referenced this pull request Dec 4, 2013
@dreid
dreid merged commit 3029fe4 into pyca:master Dec 4, 2013
@reaperhulk
reaperhulk deleted the gcm-support branch December 12, 2013 20:31
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 12, 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.

4 participants