33package software .amazon .encryption .s3 .materials ;
44
55import software .amazon .encryption .s3 .S3EncryptionClientException ;
6+ import software .amazon .encryption .s3 .algorithms .AlgorithmSuite ;
67import software .amazon .encryption .s3 .internal .CryptoFactory ;
78
89import javax .crypto .Cipher ;
@@ -117,8 +118,7 @@ public byte[] encryptDataKey(SecureRandom secureRandom,
117118
118119 // Create a pseudo-data key with the content encryption appended to the data key
119120 byte [] dataKey = materials .plaintextDataKey ();
120- byte [] dataCipherName = materials .algorithmSuite ().cipherName ().getBytes (
121- StandardCharsets .UTF_8 );
121+ byte [] dataCipherName = AlgorithmSuite .ALG_AES_256_GCM_IV12_TAG16_NO_KDF .cipherName ().getBytes (StandardCharsets .UTF_8 );
122122 byte [] pseudoDataKey = new byte [1 + dataKey .length + dataCipherName .length ];
123123
124124 pseudoDataKey [0 ] = (byte )dataKey .length ;
@@ -146,7 +146,8 @@ private byte[] parsePseudoDataKey(DecryptionMaterials materials, byte[] pseudoDa
146146 throw new S3EncryptionClientException ("Invalid key length (" + dataKeyLengthBytes + ") in encrypted data key" );
147147 }
148148
149- int dataCipherNameLength = pseudoDataKey .length - dataKeyLengthBytes - 1 ;
149+ // int dataCipherNameLength = pseudoDataKey.length - dataKeyLengthBytes - 1;
150+ int dataCipherNameLength = AlgorithmSuite .ALG_AES_256_GCM_IV12_TAG16_NO_KDF .cipherName ().getBytes (StandardCharsets .UTF_8 ).length ;
150151 if (dataCipherNameLength <= 0 ) {
151152 throw new S3EncryptionClientException ("Invalid data cipher name length (" + dataCipherNameLength + ") in encrypted data key" );
152153 }
@@ -156,7 +157,7 @@ private byte[] parsePseudoDataKey(DecryptionMaterials materials, byte[] pseudoDa
156157 System .arraycopy (pseudoDataKey , 1 , dataKey , 0 , dataKeyLengthBytes );
157158 System .arraycopy (pseudoDataKey , 1 + dataKeyLengthBytes , dataCipherName , 0 , dataCipherNameLength );
158159
159- byte [] expectedDataCipherName = materials . algorithmSuite () .cipherName ().getBytes (StandardCharsets .UTF_8 );
160+ byte [] expectedDataCipherName = AlgorithmSuite . ALG_AES_256_GCM_IV12_TAG16_NO_KDF .cipherName ().getBytes (StandardCharsets .UTF_8 );
160161 if (!Arrays .equals (expectedDataCipherName , dataCipherName )) {
161162 throw new S3EncryptionClientException ("The data cipher does not match the data cipher used for encryption. The object may be altered or corrupted" );
162163 }
0 commit comments