Skip to content

Commit 73b7df4

Browse files
committed
Cut the prov test run time: run the prov test task's per-class forks eight wide as every other test task already does, add a write-side org.bouncycastle.bcfks.store_it_count property (Properties.BCFKS_STORE_IT_COUNT) so the BCFKS keystore's default PBKDF2-HMAC-SHA512 iteration count can be lowered the way the PKCS#12 one can, set it low in the Gradle and Ant test harnesses alongside the PKCS#12 property with a BCFKSStoreTest case covering the honoured, out-of-range and explicit-PBKDFConfig cases, and generate the XMSS and XMSS^MT test keys at small tree heights wherever the test does not depend on the height.
1 parent 32f10fd commit 73b7df4

11 files changed

Lines changed: 228 additions & 40 deletions

File tree

‎ant/bc+-build.xml‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,9 +1082,11 @@
10821082
</fileset>
10831083
</classpath>
10841084
<sysproperty key="test.java.version.prefix" value="${env.JAVA_VERSION_PREFIX}" />
1085-
<!-- the PKCS#12 keystore writes with a 600,000 iteration PBE by default, several
1086-
seconds per store; the tests are not measuring the KDF, so write cheap files -->
1085+
<!-- the PKCS#12 keystore writes with a 600,000 iteration PBE by default and BCFKS
1086+
with a 51,200 iteration PBKDF2, several seconds per store; the tests are not
1087+
measuring the KDF, so write cheap files -->
10871088
<sysproperty key="org.bouncycastle.pkcs12.store_it_count" value="4096" />
1089+
<sysproperty key="org.bouncycastle.bcfks.store_it_count" value="1024" />
10881090

10891091
<formatter type="xml" />
10901092
<batchtest todir="${artifacts.reports.xml.dir}" unless="testcase">
@@ -1114,9 +1116,11 @@
11141116
</fileset>
11151117
</classpath>
11161118

1117-
<!-- the PKCS#12 keystore writes with a 600,000 iteration PBE by default, several
1118-
seconds per store; the tests are not measuring the KDF, so write cheap files -->
1119+
<!-- the PKCS#12 keystore writes with a 600,000 iteration PBE by default and BCFKS
1120+
with a 51,200 iteration PBKDF2, several seconds per store; the tests are not
1121+
measuring the KDF, so write cheap files -->
11191122
<sysproperty key="org.bouncycastle.pkcs12.store_it_count" value="4096" />
1123+
<sysproperty key="org.bouncycastle.bcfks.store_it_count" value="1024" />
11201124

11211125
<formatter type="xml" />
11221126
<test name="${testcase}" todir="${artifacts.reports.xml.dir}" if="testcase" />

‎build.gradle‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,13 @@ configure(subprojects.findAll {it.name != 'bom'}) {
312312
junitXml.outputLocation = layout.buildDirectory.dir("test-results")
313313
}
314314

315-
// The PKCS#12 keystore writes with a 600,000 iteration PBE by default, which is
316-
// several seconds per store and dominates the wall time of any suite that touches
317-
// a keystore. The tests are not measuring the KDF, so write cheap files here. Runs
318-
// after testFull's systemProperties assignment above, so it survives it.
315+
// The PKCS#12 keystore writes with a 600,000 iteration PBE by default, and BCFKS
316+
// with a 51,200 iteration PBKDF2-HMAC-SHA512, which is several seconds per store and
317+
// dominates the wall time of any suite that touches a keystore. The tests are not
318+
// measuring the KDF, so write cheap files here. Runs after testFull's
319+
// systemProperties assignment above, so it survives it.
319320
systemProperty 'org.bouncycastle.pkcs12.store_it_count', '4096'
321+
systemProperty 'org.bouncycastle.bcfks.store_it_count', '1024'
320322
}
321323

322324
// Bundle the project LICENSE into META-INF/ of every jar produced.

‎core/src/main/java/org/bouncycastle/util/Properties.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,23 @@ public class Properties
262262
*/
263263
public static final String BCFKS_MAX_SCRYPT_MEMORY = "org.bouncycastle.bcfks.max_scrypt_memory";
264264

265+
/**
266+
* The PBKDF2 iteration count the BCFKS keystore uses when <b>writing</b> a file through the
267+
* plain {@code KeyStore.store(OutputStream, char[])} path - the write-side counterpart of
268+
* {@link #BCFKS_MAX_IT_COUNT}, which only bounds what is accepted on load, and the BCFKS
269+
* analogue of {@link #PKCS12_STORE_IT_COUNT}. Default 51,200 (PBKDF2-HMAC-SHA512), applied to
270+
* the integrity MAC key and to the key-encryption keys of the entries. A caller supplying a
271+
* {@code BCFKSLoadStoreParameter} with its own {@code PBKDFConfig} is unaffected.
272+
* <p>
273+
* Lowering this trades password-cracking resistance for store/load time, and is only worth
274+
* doing where something other than the passphrase carries the confidentiality of the file.
275+
* A value outside 1 .. 5,000,000 is ignored and the default used, so a mistyped property fails
276+
* towards the default rather than towards a file with no PBE work in it; the upper bound is
277+
* the {@link #BCFKS_MAX_IT_COUNT} default, so a file written under this property can always
278+
* be read back. Read via {@link #asInteger(String, int)}.
279+
*/
280+
public static final String BCFKS_STORE_IT_COUNT = "org.bouncycastle.bcfks.store_it_count";
281+
265282
/**
266283
* Upper bound on the PBKDF2 iteration count honoured when BC takes that count from an
267284
* untrusted encoding: decrypting a PBES2-protected PKCS#8 / PEM private key or PKCS#12

‎core/src/main/jdk1.4/org/bouncycastle/util/Properties.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class Properties
4949
public static final String DSA_MAX_SIZE = "org.bouncycastle.dsa.max_size";
5050
public static final String BCFKS_MAX_IT_COUNT = "org.bouncycastle.bcfks.max_it_count";
5151
public static final String BCFKS_MAX_SCRYPT_MEMORY = "org.bouncycastle.bcfks.max_scrypt_memory";
52+
public static final String BCFKS_STORE_IT_COUNT = "org.bouncycastle.bcfks.store_it_count";
5253
public static final String PBE_MAX_ITERATION_COUNT = "org.bouncycastle.pbe.max_iteration_count";
5354
public static final String PBE_MAX_SCRYPT_MEMORY = "org.bouncycastle.pbe.max_scrypt_memory";
5455
public static final String PKMAC_MAX_ITERATION_COUNT = "org.bouncycastle.pkmac.max_iteration_count";

‎docs/releasenotes.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ <h3>2.1.2 Defects Fixed</h3>
153153

154154
<h3>2.1.3 Additional Features and Functionality</h3>
155155
<ul>
156+
<li>The BCFKS keystore now honours a write-side iteration count property, org.bouncycastle.bcfks.store_it_count (Properties.BCFKS_STORE_IT_COUNT), the counterpart of the PKCS#12 org.bouncycastle.pkcs12.store_it_count added earlier in this cycle: it sets the PBKDF2-HMAC-SHA512 iteration count used for the integrity MAC key, the store encryption and the entry key-encryption keys when a store is written through KeyStore.store(OutputStream, char[]) without a BCFKSLoadStoreParameter naming its own KDF. The default remains 51,200; a value outside 1 .. 5,000,000 is ignored so that a mistyped property cannot produce a file with no PBE work in it, and the upper bound is the default read-side cap, so a file written under the property can always be read back. A BCFKSLoadStoreParameter with an explicit PBKDFConfig is unaffected.</li>
156157
<li>The LMS / HSS hash-based signature implementation (RFC 8554) has been promoted from org.bouncycastle.pqc.crypto.lms into org.bouncycastle.crypto, following ML-KEM, ML-DSA and SLH-DSA: the key parameter classes are now org.bouncycastle.crypto.params.LMS* / HSS* (LMSigParameters, LMOtsParameters, LMSParameters, LMSKeyParameters, LMSPublicKeyParameters, LMSPrivateKeyParameters, LMSKeyGenerationParameters and the four HSS equivalents), the key pair generators are org.bouncycastle.crypto.generators.LMSKeyPairGenerator / HSSKeyPairGenerator, and LMSSigner, HSSSigner and the LMSContextBasedSigner / LMSContextBasedVerifier interfaces are in org.bouncycastle.crypto.signers, with LMSContext and the engine in org.bouncycastle.crypto.signers.lms. org.bouncycastle.crypto.util.PublicKeyFactory / PrivateKeyFactory / SubjectPublicKeyInfoFactory / PrivateKeyInfoFactory now cover id-alg-hss-lms-hashsig, so a caller no longer has to reach for the org.bouncycastle.pqc.crypto.util copies for LMS, and the BC provider's LMS KeyFactory, KeyPairGenerator and Signature services use the promoted classes throughout. Two behavioural differences in the promoted copy: LMSSigner and HSSSigner implement org.bouncycastle.crypto.Signer (they buffer the message through update() and sign or verify it on generateSignature() / verifySignature(byte[])) rather than the org.bouncycastle.pqc.crypto.MessageSigner interface, though the one-shot generateSignature(byte[]) and verifySignature(byte[], byte[]) methods remain; and an exhausted key raises the new org.bouncycastle.crypto.ExhaustedPrivateKeyException, which org.bouncycastle.pqc.crypto.ExhaustedPrivateKeyException now extends, so a catch of the new type catches both while a catch of the old one does not see a throw from the promoted code. All encodings are unchanged - the two implementations produce byte-identical X.509, PKCS#8 and signature encodings and each verifies what the other signed, which the new org.bouncycastle.pqc.crypto.test.LMSPromotionCompatibilityTest asserts across the parameter-set matrix. The engine package follows the ML-DSA and SLH-DSA shape: its public classes are LMSEngine, the operations the key classes are built on, LMSContext, and LMSSignature as the opaque form of the chaining signatures an HSS private key carries; the one-time key and signature classes, seed derivation and encoding helpers stay package-private, as do the index-management methods of the key classes, so the stateful index cannot be moved from outside the key.</li>
157158
<li>org.bouncycastle.pqc.crypto.lms is deprecated in favour of the promoted implementation above and is scheduled for removal in the next release; it is otherwise unchanged and still carries its own test suite for this release. The two implementations hold their signature index in independent objects, so a single stateful private key must be driven through one of them and not both. In the JCE layer, org.bouncycastle.pqc.jcajce.spec.LMSKeyGenParameterSpec and the already-deprecated LMSParameterSpec gain a constructor and accessors taking the promoted types - getLMSigParameters() and getLMOtsParameters() - and their existing constructor, getSigParams() and getOtsParams(), which name the org.bouncycastle.pqc.crypto.lms types, are deprecated alongside the package rather than repointed, so existing callers are unaffected.</li>
158159
<li>The new org.bouncycastle.pkcs12.store_it_count property sets the PBE iteration count the PKCS12 keystore uses when writing a file, the write-side counterpart of the existing org.bouncycastle.pkcs12.max_it_count, which only bounds what is accepted on load. The default is unchanged - 600,000 for the key and certificate encryption, twice that for the integrity MAC - but until now there was no way to ask for anything else, and the count was raised from 51,200 in 1.85, which multiplies the cost of writing or reading a keystore by about twelve. Lowering it trades password-cracking resistance for store and load time, and is only worth doing where something other than the passphrase carries the confidentiality of the file: a machine-generated high-entropy password, or a keystore held somewhere already protected. It is deliberately an operator setting rather than a per-call one, so a deployment that wants cheaper files sets it once at startup. A value outside 1 .. 2,500,000 is ignored and the default used, the upper bound keeping the doubled MAC count inside the 5,000,000 that org.bouncycastle.pkcs12.max_it_count defaults to, so a file written under this property can always be read back. A keystore loaded from an existing file still keeps and rewrites the MAC iteration count it arrived with. The same change corrects a long standing divergence in the legacy Ant distributions, whose hand maintained copies of the keystore SPIs had never been brought forward: the jdk13 and jdk14 jars were writing PKCS12 files at 1,024 iterations - the figure PKCS#12 shipped with in the 1990s - and their PBMAC1 keystore at 51,200, rather than the count the modern jars use, and neither took its MAC count from a file it had loaded. All three now share the default and honour the new property.</li>

‎prov/build.gradle‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ configurations {
390390

391391

392392
test {
393+
// Every AllTest* class already runs in its own JVM (forkEvery = 1 from the root build), so
394+
// the forks share no System/Security property state; run them alongside each other as the
395+
// other modules' test tasks and prov's own test8 / test11 / test15 / test17 / test25 do.
396+
maxParallelForks = 8;
393397
jvmArgs = ['-Dtest.java.version.prefix=any']
394398
}
395399

‎prov/src/main/java/org/bouncycastle/jcajce/provider/keystore/bcfks/BcFKSKeyStoreSpi.java‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@
109109
class BcFKSKeyStoreSpi
110110
extends KeyStoreSpi
111111
{
112+
/**
113+
* The PBKDF2-HMAC-SHA512 iteration count written by default, for the integrity MAC key and
114+
* the entry key-encryption keys.
115+
*/
116+
private static final int DEFAULT_STORE_IT_COUNT = 50 * 1024;
117+
118+
/**
119+
* Largest count {@link Properties#BCFKS_STORE_IT_COUNT} may name - the default read-side cap
120+
* {@link Properties#BCFKS_MAX_IT_COUNT} applies, so a file BC writes is one BC can read back.
121+
*/
122+
private static final int MAX_STORE_IT_COUNT = 5000000;
123+
112124
private static final Map<String, ASN1ObjectIdentifier> oidMap = new HashMap<String, ASN1ObjectIdentifier>();
113125
private static final Map<ASN1ObjectIdentifier, String> publicAlgMap = new HashMap<ASN1ObjectIdentifier, String>();
114126

@@ -1661,14 +1673,33 @@ private KeyDerivationFunc generatePkbdAlgorithmIdentifier(ASN1ObjectIdentifier d
16611673

16621674
if (PKCSObjectIdentifiers.id_PBKDF2.equals(derivationAlgorithm))
16631675
{
1664-
return new KeyDerivationFunc(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(pbkdSalt, 50 * 1024, keySizeInBytes, new AlgorithmIdentifier(PKCSObjectIdentifiers.id_hmacWithSHA512, DERNull.INSTANCE)));
1676+
return new KeyDerivationFunc(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(pbkdSalt, getStoreIterationCount(), keySizeInBytes, new AlgorithmIdentifier(PKCSObjectIdentifiers.id_hmacWithSHA512, DERNull.INSTANCE)));
16651677
}
16661678
else
16671679
{
16681680
throw new IllegalStateException("unknown derivation algorithm: " + derivationAlgorithm);
16691681
}
16701682
}
16711683

1684+
/**
1685+
* The PBKDF2 iteration count to write with on the default path - the integrity MAC key and
1686+
* the entry key-encryption keys when no {@link BCFKSLoadStoreParameter} names a KDF -
1687+
* {@link #DEFAULT_STORE_IT_COUNT} unless {@link Properties#BCFKS_STORE_IT_COUNT} names a
1688+
* usable value. A value outside 1 .. {@link #MAX_STORE_IT_COUNT} is ignored, so a mistyped
1689+
* property fails towards the default rather than towards a file with no PBE work in it.
1690+
*/
1691+
private static int getStoreIterationCount()
1692+
{
1693+
int itCount = Properties.asInteger(Properties.BCFKS_STORE_IT_COUNT, DEFAULT_STORE_IT_COUNT);
1694+
1695+
if (itCount < 1 || itCount > MAX_STORE_IT_COUNT)
1696+
{
1697+
return DEFAULT_STORE_IT_COUNT;
1698+
}
1699+
1700+
return itCount;
1701+
}
1702+
16721703
public static class Std
16731704
extends BcFKSKeyStoreSpi
16741705
{

‎prov/src/main/jdk1.4/org/bouncycastle/jcajce/provider/keystore/bcfks/BcFKSKeyStoreSpi.java‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@
9494
class BcFKSKeyStoreSpi
9595
extends KeyStoreSpi
9696
{
97+
/**
98+
* The PBKDF2-HMAC-SHA512 iteration count written by default, for the integrity MAC key and
99+
* the entry key-encryption keys.
100+
*/
101+
private static final int DEFAULT_STORE_IT_COUNT = 50 * 1024;
102+
103+
/**
104+
* Largest count {@link Properties#BCFKS_STORE_IT_COUNT} may name - the default read-side cap
105+
* {@link Properties#BCFKS_MAX_IT_COUNT} applies, so a file BC writes is one BC can read back.
106+
*/
107+
private static final int MAX_STORE_IT_COUNT = 5000000;
108+
97109
private static final Map<String, ASN1ObjectIdentifier> oidMap = new HashMap<String, ASN1ObjectIdentifier>();
98110
private static final Map<ASN1ObjectIdentifier, String> publicAlgMap = new HashMap<ASN1ObjectIdentifier, String>();
99111

@@ -1187,14 +1199,33 @@ private KeyDerivationFunc generatePkbdAlgorithmIdentifier(ASN1ObjectIdentifier d
11871199

11881200
if (PKCSObjectIdentifiers.id_PBKDF2.equals(derivationAlgorithm))
11891201
{
1190-
return new KeyDerivationFunc(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(pbkdSalt, 50 * 1024, keySizeInBytes, new AlgorithmIdentifier(PKCSObjectIdentifiers.id_hmacWithSHA512, DERNull.INSTANCE)));
1202+
return new KeyDerivationFunc(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(pbkdSalt, getStoreIterationCount(), keySizeInBytes, new AlgorithmIdentifier(PKCSObjectIdentifiers.id_hmacWithSHA512, DERNull.INSTANCE)));
11911203
}
11921204
else
11931205
{
11941206
throw new IllegalStateException("unknown derivation algorithm: " + derivationAlgorithm);
11951207
}
11961208
}
11971209

1210+
/**
1211+
* The PBKDF2 iteration count to write with on the default path - the integrity MAC key and
1212+
* the entry key-encryption keys when no {@link BCFKSLoadStoreParameter} names a KDF -
1213+
* {@link #DEFAULT_STORE_IT_COUNT} unless {@link Properties#BCFKS_STORE_IT_COUNT} names a
1214+
* usable value. A value outside 1 .. {@link #MAX_STORE_IT_COUNT} is ignored, so a mistyped
1215+
* property fails towards the default rather than towards a file with no PBE work in it.
1216+
*/
1217+
private static int getStoreIterationCount()
1218+
{
1219+
int itCount = Properties.asInteger(Properties.BCFKS_STORE_IT_COUNT, DEFAULT_STORE_IT_COUNT);
1220+
1221+
if (itCount < 1 || itCount > MAX_STORE_IT_COUNT)
1222+
{
1223+
return DEFAULT_STORE_IT_COUNT;
1224+
}
1225+
1226+
return itCount;
1227+
}
1228+
11981229
public static class Std
11991230
extends BcFKSKeyStoreSpi
12001231
{

0 commit comments

Comments
 (0)