Skip to content

Commit 66b3838

Browse files
Changing encryptKey and decryptKey methods to use key bytes instead of SecretKey
1 parent 28ec657 commit 66b3838

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/main/java/com/amazonaws/encryptionsdk/internal/JceKeyCipher.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,15 @@ abstract Cipher buildUnwrappingCipher(Key key, byte[] extraInfo, int offset,
8181
* during encryption and decryption to provide additional authenticated data (AAD).
8282
* @return The encrypted data key.
8383
*/
84-
public EncryptedDataKey encryptKey(final SecretKey key, final String keyName,
84+
public EncryptedDataKey encryptKey(final byte[] key, final String keyName,
8585
final Map<String, String> encryptionContext) {
8686

87-
final byte[] keyBytes = key.getEncoded();
8887
final byte[] keyNameBytes = keyName.getBytes(KEY_NAME_ENCODING);
8988

9089
try {
9190
final JceKeyCipher.WrappingData wData = buildWrappingCipher(wrappingKey, encryptionContext);
9291
final Cipher cipher = wData.cipher;
93-
final byte[] encryptedKey = cipher.doFinal(keyBytes);
92+
final byte[] encryptedKey = cipher.doFinal(key);
9493

9594
final byte[] provInfo = new byte[keyNameBytes.length + wData.extraInfo.length];
9695
System.arraycopy(keyNameBytes, 0, provInfo, 0, keyNameBytes.length);
@@ -105,27 +104,20 @@ public EncryptedDataKey encryptKey(final SecretKey key, final String keyName,
105104
/**
106105
* Decrypts the given encrypted data key.
107106
*
108-
* @param algorithm The algorithm that encrypted the data key.
109107
* @param edk The encrypted data key.
110108
* @param keyName A UTF-8 encoded representing a name for the key.
111109
* @param encryptionContext A key-value mapping of arbitrary, non-secret, UTF-8 encoded strings used
112110
* during encryption and decryption to provide additional authenticated data (AAD).
113111
* @return The decrypted key.
114112
* @throws GeneralSecurityException If a problem occurred decrypting the key.
115113
*/
116-
public SecretKey decryptKey(final CryptoAlgorithm algorithm, final EncryptedDataKey edk, final String keyName,
114+
public byte[] decryptKey(final EncryptedDataKey edk, final String keyName,
117115
final Map<String, String> encryptionContext) throws GeneralSecurityException {
118116
final byte[] keyNameBytes = keyName.getBytes(KEY_NAME_ENCODING);
119117

120118
final Cipher cipher = buildUnwrappingCipher(unwrappingKey, edk.getProviderInformation(),
121119
keyNameBytes.length, encryptionContext);
122-
final byte[] rawKey = cipher.doFinal(edk.getEncryptedDataKey());
123-
if (rawKey.length != algorithm.getDataKeyLength()) {
124-
// Something's wrong here. Assume that the decryption is invalid.
125-
return null;
126-
}
127-
128-
return new SecretKeySpec(rawKey, algorithm.getDataKeyAlgo());
120+
return cipher.doFinal(edk.getEncryptedDataKey());
129121
}
130122

131123
static class WrappingData {

src/main/java/com/amazonaws/encryptionsdk/jce/JceMasterKey.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ public DataKey<JceMasterKey> generateDataKey(final CryptoAlgorithm algorithm,
110110
final Map<String, String> encryptionContext) {
111111
final byte[] rawKey = new byte[algorithm.getDataKeyLength()];
112112
rnd.nextBytes(rawKey);
113-
EncryptedDataKey encryptedDataKey = jceKeyCipher_.encryptKey(new SecretKeySpec(rawKey, algorithm.getDataKeyAlgo()), keyId_,
114-
encryptionContext);
113+
EncryptedDataKey encryptedDataKey = jceKeyCipher_.encryptKey(rawKey, keyId_, encryptionContext);
115114
return new DataKey<>(new SecretKeySpec(rawKey, algorithm.getDataKeyAlgo()),
116115
encryptedDataKey.getEncryptedDataKey(), encryptedDataKey.getProviderInformation(), this);
117116
}
@@ -129,7 +128,7 @@ public DataKey<JceMasterKey> encryptDataKey(final CryptoAlgorithm algorithm,
129128
throw new IllegalArgumentException("Incorrect key algorithm. Expected " + key.getAlgorithm()
130129
+ " but got " + algorithm.getKeyAlgo());
131130
}
132-
EncryptedDataKey encryptedDataKey = jceKeyCipher_.encryptKey(key, keyId_, encryptionContext);
131+
EncryptedDataKey encryptedDataKey = jceKeyCipher_.encryptKey(key.getEncoded(), keyId_, encryptionContext);
133132
return new DataKey<>(key, encryptedDataKey.getEncryptedDataKey(), encryptedDataKey.getProviderInformation(), this);
134133
}
135134

@@ -144,10 +143,12 @@ public DataKey<JceMasterKey> decryptDataKey(final CryptoAlgorithm algorithm,
144143
try {
145144
if (edk.getProviderId().equals(getProviderId())
146145
&& arrayPrefixEquals(edk.getProviderInformation(), keyIdBytes_, keyIdBytes_.length)) {
147-
final SecretKey decryptedKey = jceKeyCipher_.decryptKey(algorithm, edk, keyId_, encryptionContext);
146+
final byte[] decryptedKey = jceKeyCipher_.decryptKey(edk, keyId_, encryptionContext);
148147

149-
if(decryptedKey != null) {
150-
return new DataKey<>(decryptedKey, edk.getEncryptedDataKey(), edk.getProviderInformation(), this);
148+
// Validate that the decrypted key length is as expected
149+
if (decryptedKey.length == algorithm.getDataKeyLength()) {
150+
return new DataKey<>(new SecretKeySpec(decryptedKey, algorithm.getDataKeyAlgo()),
151+
edk.getEncryptedDataKey(), edk.getProviderInformation(), this);
151152
}
152153
}
153154
} catch (final Exception ex) {

0 commit comments

Comments
 (0)