Skip to content

Commit 32f1a1d

Browse files
committed
Merge branch '1985-pbe-generator' into 'main'
1985 pbe generator See merge request root/bc-java!70
2 parents 9706d9a + b641d60 commit 32f1a1d

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

pkix/src/test/java/org/bouncycastle/cms/test/GOSTR3410_2012_256CmsSignVerifyDetached.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private static boolean verifyDetached(byte[] data, byte[] detachedCms,
128128
PKIXBuilderParameters params = new PKIXBuilderParameters(trustAnchors, constraints);
129129

130130
params.setDate(new Date(x509Certificate.getNotAfter().getTime() - 5000L));
131-
131+
132132
JcaCertStoreBuilder certStoreBuilder = new JcaCertStoreBuilder();
133133
certStoreBuilder.addCertificate(signerCert);
134134

prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ else if (modeName.startsWith("PGPCFB"))
448448
{
449449
throw new NoSuchAlgorithmException("no mode support for " + modeName);
450450
}
451-
451+
452452
ivLength = baseEngine.getBlockSize();
453453
cipher = new BufferedGenericBlockCipher(
454454
new PGPCFBBlockCipher(baseEngine, inlineIV));
@@ -821,14 +821,16 @@ else if (!(key instanceof RepeatedSecretKeySpec))
821821
param = null;
822822
}
823823

824-
AlgorithmParameterSpec params;
824+
AlgorithmParameterSpec params = paramSpec;
825825
if (paramSpec instanceof PBEParameterSpec)
826826
{
827827
params = ((PBEParameterSpec)paramSpec).getParameterSpec();
828-
}
829-
else
830-
{
831-
params = paramSpec;
828+
// If params.getIv() returns an empty byte array, ivParam will be assigned an IV generated by PBE.Util.makePBEParameters
829+
// according to RFC 7292. This behavior is intended for Jasypt users who choose to use NoIvGenerator.
830+
if (params instanceof IvParameterSpec && ((IvParameterSpec)params).getIV().length == 0)
831+
{
832+
params = paramSpec;
833+
}
832834
}
833835

834836
if (params instanceof AEADParameterSpec)

prov/src/test/java/org/bouncycastle/jce/provider/test/PBETest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,43 @@ private void testExtendedPBEParameterSpec()
564564

565565
isTrue(Arrays.areEqual(input, decryptedBytes));
566566
}
567+
568+
private void testNoIvPBEParameterSpec()
569+
throws Exception
570+
{
571+
String cipherAlgo = "PBEWITHSHA256AND256BITAES-CBC-BC";
572+
573+
SecureRandom random = new FixedSecureRandom(Hex.decode(
574+
"000102030405060708090a0b0c0d0e0f"
575+
+ "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"));
576+
577+
char[] password = "abcdefghijklmnop".toCharArray();
578+
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
579+
580+
SecretKeyFactory factory = SecretKeyFactory.getInstance(
581+
"PBEWITHSHA256AND256BITAES-CBC-BC",
582+
"BC");
583+
SecretKey key = factory.generateSecret(pbeKeySpec);
584+
585+
byte[] salt = new byte[16];
586+
random.nextBytes(salt);
587+
// simulate the situation for issue #1985
588+
byte[] iv = new byte[0];
589+
590+
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 1000, new IvParameterSpec(iv));
591+
592+
Cipher encryptCipher = Cipher.getInstance(cipherAlgo, "BC");
593+
Cipher decryptCipher = Cipher.getInstance(cipherAlgo, "BC");
594+
595+
encryptCipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec);
596+
decryptCipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec);
597+
598+
byte[] input = Strings.toByteArray("testing");
599+
byte[] encryptedBytes = encryptCipher.doFinal(input);
600+
byte[] decryptedBytes = decryptCipher.doFinal(encryptedBytes);
601+
602+
isTrue(Arrays.areEqual(input, decryptedBytes));
603+
}
567604

568605
public void performTest()
569606
throws Exception
@@ -710,7 +747,7 @@ public void performTest()
710747
}
711748

712749
testExtendedPBEParameterSpec();
713-
750+
testNoIvPBEParameterSpec();
714751
testPKCS12Interop();
715752

716753
testPBEHMac("PBEWithHMacSHA1", hMac1);

0 commit comments

Comments
 (0)