There appears to be a bug when attempting to use PBKDF2 with AES encryption in approved only mode, where the PBEWITH* Cipher/SecretKeyFactory algorithm family is unavailable.
Example code:
import org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.SecureRandom;
var keyAlgo = "PBKDF2WITHHMACSHA512";
var cipherAlgo = "2.16.840.1.101.3.4.1.42";
var provider = new BouncyCastleFipsProvider();
var random = SecureRandom.getInstance("SHA1PRNG");
var password = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".toCharArray();
var pbeKeySpec = new PBEKeySpec(password);
var factory = SecretKeyFactory.getInstance(keyAlgo, provider);
var key = factory.generateSecret(pbeKeySpec);
var salt = new byte[16];
random.nextBytes(salt);
var iv = new byte[16];
random.nextBytes(iv);
var parameterSpec = new PBEParameterSpec(salt, 1000, new IvParameterSpec(iv));
var encryptCipher = Cipher.getInstance(cipherAlgo, provider);
var decryptCipher = Cipher.getInstance(cipherAlgo, provider);
encryptCipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
decryptCipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
var encryptedBytes = encryptCipher.doFinal("testing".getBytes());
var decryptedBytes = decryptCipher.doFinal(encryptedBytes);
var decryptedStr = new String(decryptedBytes, "UTF-8");
System.out.println(decryptedStr);
The above produced the following exception on decryption:
javax.crypto.BadPaddingException: Error finalising cipher data: pad block corrupted
The reason for this is because in BaseCipher.doEngineInit() it's ignoring the IV specified in parameterSpec and generates a new, random IV.
The bug can be fixed easily enough by pulling the IvParamterSpec out of PBEParameterSpec with getParameterSpec() either in BaseCipher or in the ParametersCreator that BaseCipher calls.
There appears to be a bug when attempting to use PBKDF2 with AES encryption in approved only mode, where the PBEWITH* Cipher/SecretKeyFactory algorithm family is unavailable.
Example code:
The above produced the following exception on decryption:
The reason for this is because in
BaseCipher.doEngineInit()it's ignoring the IV specified inparameterSpecand generates a new, random IV.The bug can be fixed easily enough by pulling the
IvParamterSpecout ofPBEParameterSpecwithgetParameterSpec()either inBaseCipheror in theParametersCreatorthatBaseCiphercalls.