Skip to content

Commit c6bd363

Browse files
Set provider ID correctly in the encrypted data key.
1 parent 7e2dde6 commit c6bd363

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ private static byte[] specToBytes(final GCMParameterSpec spec) {
5555
return baos.toByteArray();
5656
}
5757

58-
private static GCMParameterSpec bytesToSpec(final byte[] data, final int offset) throws GeneralSecurityException {
58+
private static GCMParameterSpec bytesToSpec(final byte[] data, final int offset) throws InvalidKeyException {
5959
final ByteArrayInputStream bais = new ByteArrayInputStream(data, offset, data.length - offset);
6060
try (final DataInputStream dis = new DataInputStream(bais)) {
6161
final int tagLen = dis.readInt();
6262
final int nonceLen = dis.readInt();
6363

64-
if(tagLen != TAG_LENGTH) {
64+
if (tagLen != TAG_LENGTH) {
6565
throw new InvalidKeyException(String.format("Authentication tag length must be %s", TAG_LENGTH));
6666
}
6767

68-
if(nonceLen != NONCE_LENGTH) {
68+
if (nonceLen != NONCE_LENGTH) {
6969
throw new InvalidKeyException(String.format("Initialization vector (IV) length must be %s", NONCE_LENGTH));
7070
}
7171

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ abstract Cipher buildUnwrappingCipher(Key key, byte[] extraInfo, int offset,
7575
* Encrypts the given key, incorporating the given keyName and encryptionContext.
7676
* @param key The key to encrypt.
7777
* @param keyName A UTF-8 encoded representing a name for the key.
78+
* @param keyNamespace A UTF-8 encoded value that namespaces the key.
7879
* @param encryptionContext A key-value mapping of arbitrary, non-secret, UTF-8 encoded strings used
7980
* during encryption and decryption to provide additional authenticated data (AAD).
8081
* @return The encrypted data key.
8182
*/
82-
public EncryptedDataKey encryptKey(final byte[] key, final String keyName,
83+
public EncryptedDataKey encryptKey(final byte[] key, final String keyName, final String keyNamespace,
8384
final Map<String, String> encryptionContext) {
8485

8586
final byte[] keyNameBytes = keyName.getBytes(KEY_NAME_ENCODING);
@@ -93,7 +94,7 @@ public EncryptedDataKey encryptKey(final byte[] key, final String keyName,
9394
System.arraycopy(keyNameBytes, 0, provInfo, 0, keyNameBytes.length);
9495
System.arraycopy(wData.extraInfo, 0, provInfo, keyNameBytes.length, wData.extraInfo.length);
9596

96-
return new KeyBlob(keyName, provInfo, encryptedKey);
97+
return new KeyBlob(keyNamespace, provInfo, encryptedKey);
9798
} catch (final GeneralSecurityException gsex) {
9899
throw new AwsCryptoException(gsex);
99100
}
@@ -103,7 +104,7 @@ public EncryptedDataKey encryptKey(final byte[] key, final String keyName,
103104
* Decrypts the given encrypted data key.
104105
*
105106
* @param edk The encrypted data key.
106-
* @param keyName A UTF-8 encoded representing a name for the key.
107+
* @param keyName A UTF-8 encoded String representing a name for the key.
107108
* @param encryptionContext A key-value mapping of arbitrary, non-secret, UTF-8 encoded strings used
108109
* during encryption and decryption to provide additional authenticated data (AAD).
109110
* @return The decrypted key.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public DataKey<JceMasterKey> generateDataKey(final CryptoAlgorithm algorithm,
111111
final Map<String, String> encryptionContext) {
112112
final byte[] rawKey = new byte[algorithm.getDataKeyLength()];
113113
rnd.nextBytes(rawKey);
114-
EncryptedDataKey encryptedDataKey = jceKeyCipher_.encryptKey(rawKey, keyId_, encryptionContext);
114+
EncryptedDataKey encryptedDataKey = jceKeyCipher_.encryptKey(rawKey, keyId_, providerName_, encryptionContext);
115115
return new DataKey<>(new SecretKeySpec(rawKey, algorithm.getDataKeyAlgo()),
116116
encryptedDataKey.getEncryptedDataKey(), encryptedDataKey.getProviderInformation(), this);
117117
}
@@ -129,7 +129,7 @@ public DataKey<JceMasterKey> encryptDataKey(final CryptoAlgorithm algorithm,
129129
throw new IllegalArgumentException("Incorrect key algorithm. Expected " + key.getAlgorithm()
130130
+ " but got " + algorithm.getKeyAlgo());
131131
}
132-
EncryptedDataKey encryptedDataKey = jceKeyCipher_.encryptKey(key.getEncoded(), keyId_, encryptionContext);
132+
EncryptedDataKey encryptedDataKey = jceKeyCipher_.encryptKey(key.getEncoded(), keyId_, providerName_, encryptionContext);
133133
return new DataKey<>(key, encryptedDataKey.getEncryptedDataKey(), encryptedDataKey.getProviderInformation(), this);
134134
}
135135

0 commit comments

Comments
 (0)