Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@anubhavi25
Copy link
Contributor

…by using thread local instances of Cipher

Signed-off-by: Anurag [email protected]

@anubhavi25 anubhavi25 changed the title DE25851: Made Encrpytor#encrypt thread safe and improved performance … DE25851: Made Encryptor#encrypt thread safe and improved performance … Mar 27, 2017
@FrankGasparovic FrankGasparovic force-pushed the DE25851 branch 2 times, most recently from ddf4272 to 7c980c7 Compare March 27, 2017 21:39
Copy link
Collaborator

@irinaepshteyn irinaepshteyn left a comment

Choose a reason for hiding this comment

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

Done reviewing. Please review my comments.

// Rigorous checks to make sure we've absolutely got the correct instance (with correct alg/key/iv/...)
if (cachedCipher != null && cachedCipher.isCipherValid(iv, cipherMode, this.secretKeySpec)) {
return cachedCipher.cipher;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Caching cipher seems like unnecessary overhead. isCipherValid does comparison on cipherMode (ENCRYPT_MODE or DECRYPT_MODE) and on IV. If the same thread is used continuously to encrypt (or decrypt), the cached cipher will be thrown away each time as new IV is generated for every encryption/decryption operation. If same thread is used to encrypt and then decrypt, cached cypher will be thrown away as mode is different. I can't come up with the use case when cached cypher is actually used.

Copy link
Contributor Author

@anubhavi25 anubhavi25 Mar 28, 2017

Choose a reason for hiding this comment

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

Apart from maybe allowing flexibility to change the algorithm and not having to change this code, I agree with you. For now, I've changed it to just cache the Cipher instance thread-locally (agnostic of Cipher#init).

CIPHER_THREAD_LOCAL.set(new CachedCipher(cipherMode, this.secretKeySpec, cipher));
return cipher;
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException
| InvalidKeyException e) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's better to catch a Throwable here and wrap anything that could go wrong with CipherInitializationFailureException

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

throw new SymmetricKeyValidationException("Encryption key must be string of length " + KEY_LENGTH_IN_BYTES);
}
this.encryptionKey = encryptionKey;
this.secretKeySpec = new SecretKeySpec(encryptionKey.getBytes(), ALGO);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could unload key length validation to SecretKeySpec(byte[] key, int offset, int len, String algorithm) constructor. It throws IllegalArgumentException if algorithm is null or key is null, empty, or too short, i.e. key.length-offset<len.

Copy link
Contributor Author

@anubhavi25 anubhavi25 Mar 28, 2017

Choose a reason for hiding this comment

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

Agreed. In the case that the key is longer than 16 bytes, the first 16 will be used (since the offset is 0)

…by using thread local instances of Cipher

Signed-off-by: Anurag <[email protected]>
@anubhavi25
Copy link
Contributor Author

@anubhavi25 anubhavi25 dismissed irinaepshteyn’s stale review March 28, 2017 02:24

Incorporated PR feedback and squashed changes

Copy link
Collaborator

@irinaepshteyn irinaepshteyn left a comment

Choose a reason for hiding this comment

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

Ready to merge

@FrankGasparovic FrankGasparovic merged commit 0f1f20c into develop Mar 28, 2017
@FrankGasparovic FrankGasparovic deleted the DE25851 branch March 28, 2017 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants