Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Java: Reuse Cipher in AesGcmJce #208

Closed
kelunik opened this issue May 15, 2019 · 4 comments
Closed

Java: Reuse Cipher in AesGcmJce #208

kelunik opened this issue May 15, 2019 · 4 comments
Assignees

Comments

@kelunik
Copy link
Contributor

kelunik commented May 15, 2019

Currently, AesGcmJce calls instance() for every cryptographic operation, which in turn calls EngineFactory.CIPHER.getInstance(), which is rather slow.

According to the Java documentation, Cipher objects are reusable, as all state is dropped when they're re-initialized. As far as I can tell, these Cipher instances could be stored in a ThreadLocal allowing them to be reused from the same thread at least, which seems to be rather common, e.g. decoding multiple database rows sequentially with a fixed application key.

@thaidn
Copy link
Contributor

thaidn commented Jul 23, 2019

Thanks for the suggestion.

@bleichen found that one of the dangerous features in JCA is that the default behaviour of Cipher.doFinal is to reinitialize the cipher with the same parameters as the last encryption. For AES-GCM this behaviour would imply that the authentication key is leaked, if the Cipher object is reused in two encryptions without an explicit initialization of the IV.

Our implementation always explicitly initializes the IV though, so I think what your suggestion should work.

@bleichen what do you think?

@bleichen
Copy link
Contributor

bleichen commented Jul 24, 2019 via email

@thaidn thaidn self-assigned this Sep 17, 2019
thaidn added a commit that referenced this issue Oct 3, 2019
@thaidn
Copy link
Contributor

thaidn commented Oct 3, 2019

This is fixed, and will be included in 1.3.0.

Thanks so much for the suggestion! It was indeed much faster.

@thaidn thaidn closed this as completed Oct 3, 2019
@kelunik
Copy link
Contributor Author

kelunik commented Oct 3, 2019

@thaidn: That's very good news! Thank you for your work on this.

thaidn added a commit that referenced this issue Oct 16, 2019
copybara-service bot pushed a commit to tink-crypto/tink-java that referenced this issue Jan 23, 2024
This is an implementation of AEAD using the ChaCha20Poly1305 provided by the JCE.

We use the same optimization as for AES-GCM, see discussion in tink-crypto/tink#208.

The goal is to later add a call to this to the key manager, with a fall-back to the Tink implementation if the JCE doesn't support it.

PiperOrigin-RevId: 600808913
Change-Id: I80fd44e2db0e9bdddfa11d05540a6e91f9b97ef3
morambro pushed a commit that referenced this issue Jan 26, 2024
This is an implementation of AEAD using the ChaCha20Poly1305 provided by the JCE.

We use the same optimization as for AES-GCM, see discussion in #208.

The goal is to later add a call to this to the key manager, with a fall-back to the Tink implementation if the JCE doesn't support it.

PiperOrigin-RevId: 600808913
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants