Don't use algorithm parameters if missing salt or iteration count.

The PBEKeySpec constructor doesn't allow an empty salt or iteration count,
and throws an exception if it sees one.

Bug: 38161557
Test: cts -m CtsLibcoreTestCases

(cherry picked from commit 65832e311cb5fda062d79599b149232b47294fea)

Change-Id: I69d05471af364f69793e83268c826d24ac82052e
Merged-In: I23fa5d10003637584f856738940f54bddb0657dc
Merged-In: I585d00f30e8848563d74d3f244f073d91d5db268
diff --git a/bcprov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java b/bcprov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java
index 59b715a..63d7b35 100644
--- a/bcprov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java
+++ b/bcprov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java
@@ -642,14 +642,17 @@
             {
                 pbeSpec = (PBEParameterSpec)params;
                 // BEGIN android-added
-                // At this point, k.getParam() == null, so the key hasn't been generated. Recreate
-                // the BCPBEKey with specs from algorithm parameters as to generate the key.
-                k = new BCPBEKey(k.getAlgorithm(), k.getOID(), k.getType(), k.getDigest(),
-                        k.getKeySize(), k.getIvSize(),
-                        new PBEKeySpec(
-                                k.getPassword(), pbeSpec.getSalt(), pbeSpec.getIterationCount(),
-                                k.getKeySize()),
-                        null /* CipherParameters */);
+                // At this point, k.getParam() == null, so the key hasn't been generated.  If
+                // the parameters have non-default values, recreate the BCPBEKey from algorithm
+                // parameters as to generate the key.
+                if ((pbeSpec.getSalt().length != 0) && (pbeSpec.getIterationCount() > 0)) {
+                    k = new BCPBEKey(k.getAlgorithm(), k.getOID(), k.getType(), k.getDigest(),
+                            k.getKeySize(), k.getIvSize(),
+                            new PBEKeySpec(
+                                    k.getPassword(), pbeSpec.getSalt(), pbeSpec.getIterationCount(),
+                                    k.getKeySize()),
+                            null /* CipherParameters */);
+                }
                 // END android-added
                 param = PBE.Util.makePBEParameters(k, params, cipher.getUnderlyingCipher().getAlgorithmName());
             }