Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clarify Key.getAlgorithm/Cipher.getInstance usage #455

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
* <p>Other possibly implementations might use a Key-Derivation Function to derive a unique key per
* record.
*
* <p>This class is only as strong as the security of the wrapping/unwrapping keys' underlying
* cryptographic algorithm. We recommend using an AES or RSA key, though other algorithms may also
* be used.
*
* @author Greg Rubin
*/
public class WrappedRawMaterials extends AbstractRawMaterials {
Expand Down Expand Up @@ -117,6 +121,7 @@ protected SecretKey initEnvelopeKey() throws GeneralSecurityException {
throw new IllegalStateException("No private decryption key provided.");
}
byte[] encryptedKey = Base64.decode(description.get(ENVELOPE_KEY));
// The wrapping/unwrapping keys can be of any algorithm of the user's choice, so no check is needed/possible here.
String wrappingAlgorithm = unwrappingKey.getAlgorithm();
if (description.containsKey(KEY_WRAPPING_ALGORITHM)) {
wrappingAlgorithm = description.get(KEY_WRAPPING_ALGORITHM);
Expand All @@ -128,13 +133,16 @@ protected SecretKey initEnvelopeKey() throws GeneralSecurityException {
? generateContentKey(description.get(CONTENT_KEY_ALGORITHM))
: generateContentKey(DEFAULT_ALGORITHM);

// The wrapping/unwrapping keys can be of any algorithm of the user's choice, so no check is needed/possible here.
String wrappingAlg =
description.containsKey(KEY_WRAPPING_ALGORITHM)
? description.get(KEY_WRAPPING_ALGORITHM)
: getTransformation(wrappingKey.getAlgorithm());
String contentKeyAlg = key.getAlgorithm();

byte[] encryptedKey = wrapKey(key, wrappingAlg);
description.put(ENVELOPE_KEY, Base64.encodeToString(encryptedKey));
description.put(CONTENT_KEY_ALGORITHM, key.getAlgorithm());
description.put(CONTENT_KEY_ALGORITHM, contentKeyAlg);
description.put(KEY_WRAPPING_ALGORITHM, wrappingAlg);
setMaterialDescription(description);
return key;
Expand All @@ -147,6 +155,7 @@ public byte[] wrapKey(SecretKey key, String wrappingAlg)
if (wrappingKey instanceof DelegatedKey) {
return ((DelegatedKey) wrappingKey).wrap(key, null, wrappingAlg);
} else {
// The wrapping key can be of any algorithm of the user's choice, so no check is needed/possible here.
Cipher cipher = Cipher.getInstance(wrappingAlg);
cipher.init(Cipher.WRAP_MODE, wrappingKey, Utils.getRng());
byte[] encryptedKey = cipher.wrap(key);
Expand All @@ -167,6 +176,7 @@ protected SecretKey unwrapKey(
null,
wrappingAlgorithm);
} else {
// The unwrapping key can be of any algorithm of the user's choice, so no check is needed/possible here.
Cipher cipher = Cipher.getInstance(wrappingAlgorithm);

// This can be of the form "AES/256" as well as "AES" e.g.,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
* <p>This is generally a more secure way of encrypting data than with the {@link
* SymmetricStaticProvider}.
*
* <p>This class is only as strong as the security of the wrapping/unwrapping keys' underlying
* cryptographic algorithm. We recommend using an AES or RSA key, though other algorithms may also
* be used.
*
* @see WrappedRawMaterials
* @author Greg Rubin
*/
Expand Down