Skip to content

Commit b065841

Browse files
committed
Make every BCPQC key report an algorithm name its provider registers - SNTRUPrime keys had the sibling NTRULPRime name, BIKE and CMCE keys their parameter-set name against family-only registrations (their parameter-set locked ciphers now check the key's own parameter set), and SPHINCS-256 is now an alias of SPHINCS256 - sweeping every registered KeyPairGenerator for the KeyFactory round trip by the key's own name.
1 parent c3413f8 commit b065841

14 files changed

Lines changed: 291 additions & 19 deletions

File tree

‎docs/releasenotes.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ <h3>2.1.2 Defects Fixed</h3>
117117
<li>Five parameter-set specific KeyPairGenerator implementations in the BCPQC provider ignored the parameter set named by the algorithm and generated keys for their family's default one instead, so KeyPairGenerator.getInstance("HQC-256", "BCPQC").generateKeyPair() returned an hqc-128 key pair. The protected constructor each parameter-set specific subclass calls took the parameter set, but used it only to name the JCA algorithm, leaving the generator uninitialised; generateKeyPair() then took the uninitialised branch, which is hard-coded to the family default. A caller selecting a parameter set by name, and not additionally calling initialize() with the matching AlgorithmParameterSpec, therefore received keys at the family's default security level with nothing reporting the substitution - for HQC, level 1 keys where level 3 or level 5 was asked for. HQCKeyPairGeneratorSpi recorded nothing at all; MayoKeyPairGeneratorSpi, AIMerKeyPairGeneratorSpi, SQIsignKeyPairGeneratorSpi and SnovaKeyPairGeneratorSpi stored the parameter set in a field that no other code then read. All five now initialise from their own parameter set in that constructor, as MLKEMKeyPairGeneratorSpi and MLDSAKeyPairGeneratorSpi in the BC provider already did, and each additionally refuses an initialize(AlgorithmParameterSpec, ...) naming a different parameter set with InvalidAlgorithmParameterException("key pair generator locked to ..."), matching the ML-KEM generator and the lock the HQC KeyGenerator (the KEM side) has always applied. The family-level generators ("HQC", "Mayo", "AIMer", "SQIsign", "Snova") are unchanged, still defaulting as they did and still accepting any parameter set of their family, and an explicitly initialised parameter-set specific generator was correct before and is correct now. This is the KeyPairGenerator counterpart of the parameter-set specific KeyFactory defect above.</li>
118118
<li>Following on from the parameter-set specific KeyPairGenerator defect above, a sweep of every PQC family that registers such generators found six more - SLH-DSA in the BC provider, and QRUOV, FAEST, HAETAE, SDitH and MQOM in BCPQC - whose named generators produced the right parameter set but would accept an initialize(AlgorithmParameterSpec, ...) naming a different one, silently re-pointing a generator obtained as, say, SLH-DSA-SHAKE-256F at a level 1 parameter set. All six now refuse it with InvalidAlgorithmParameterException("key pair generator locked to ..."), as ML-KEM, ML-DSA, Falcon, NTRU+, SMAUG-T, UOV, Classic McEliece and FrodoKEM already did. <b>This is a behavioural change for a caller that deliberately re-initialised a named generator</b>, which now has to obtain the generator for the parameter set it wants (or use the family-level generator, which continues to accept any parameter set of its family). Separately, SLHDSAKeyPairGeneratorSpi built its JCA algorithm name by prefixing "SLH-DSA-" to a parameter set name that already carried that prefix, so getAlgorithm() on a generator obtained as "SLH-DSA-SHA2-128S" returned "SLH-DSA-SLH-DSA-SHA2-128S"; it now returns the name it was requested under, as the ML-DSA generator does. Keys, encodings and signatures were never affected by that.</li>
119119
<li>The AIMer, SMAUG-T and NTRU+ parameter set names were not registered as algorithm aliases in the BCPQC provider, so getInstance(spec.getName()) - the natural way to turn an AlgorithmParameterSpec into a service, and what the other sixteen PQC families accept - failed with NoSuchAlgorithmException for those three. The spellings differ from the registered names only in punctuation: AIMerParameterSpec names carry no hyphen ("aimer128f" against the registered "AIMer-128f"), SmaugTParameterSpec uses an underscore ("SMAUGT_MODE1" against "SMAUGT-MODE1") and NTRUPlusParameterSpec no hyphen before the size ("NTRU+KEM768" against "NTRU+KEM-768"), so the mismatch was easy to hit and gave no hint as to the spelling wanted. Each parameter set name is now an alias of its algorithm across every service the family registers - KeyFactory, KeyPairGenerator and Signature for AIMer, KeyFactory, KeyPairGenerator, KeyGenerator and Cipher for SMAUG-T and NTRU+. The registered names are unchanged and remain the canonical ones; JCA algorithm lookup is case insensitive, so both spellings resolve in any case.</li>
120+
<li>A key's getAlgorithm() in the BCPQC provider did not always name a service the provider registers, so the standard JCA round trip - KeyFactory.getInstance(key.getAlgorithm(), "BCPQC") given the key's own X509EncodedKeySpec / PKCS8EncodedKeySpec - failed for four families, found by sweeping every KeyPairGenerator the provider registers. The Streamlined NTRU Prime keys reported "NTRULPRime", the sibling NTRU LPRime family's name, so the lookup resolved to the wrong family's KeyFactory and generatePublic / generatePrivate failed with an InvalidKeySpecException carrying a ClassCastException; unlike most of the key classes these hardcode the family name directly, which is how the copy-paste escaped the parameter-set sweeps above, and they now report "SNTRUPrime". The BIKE and classic McEliece keys reported their parameter-set name in upper case ("BIKE128", "MCELIECE8192128F") where those families register family-level services only, so the KeyFactory lookup itself failed with NoSuchAlgorithmException; they now report "BIKE" and "CMCE", the names their families register for their KeyFactory and KeyPairGenerator and what their own javadoc already promised, matching the other family-only KEM families (the parameter set remains available from every key through getParameterSpec()). The two families' parameter-set locked Cipher services ("BIKE128", "mceliece348864", ...) had enforced their lock by comparing the parameter-set name against getAlgorithm(); they now check the key's own parameter set, so the lock is unchanged in effect - a key of the cipher's parameter set is accepted, any other set, including an f-variant against its plain cipher, is refused with the same InvalidKeyException. SPHINCS-256 keys report "SPHINCS-256", the algorithm's published name, where its services are registered as "SPHINCS256"; the keys are unchanged - they have reported that name since the algorithm was added - and "SPHINCS-256" is now an alias of the SPHINCS256 KeyFactory and KeyPairGenerator, so the name a key reports resolves there too.</li>
120121
<li>Four PQC families disagreed with themselves about how a parameter set is spelled, because their KeyPairGenerator took its JCA algorithm name from the lightweight parameters object rather than from the name it was registered under. Falcon's generator reported getAlgorithm() as "falcon-1024" where it is registered as FALCON-1024 and its keys report FALCON-1024; SMAUG-T's reported "smaugt_mode1" where it is registered as SMAUGT-MODE1; and NTRU+ reported NTRU+KEM768 where it was registered as NTRU+KEM-768. Each family now uses one spelling throughout - generator, parameter spec, generated keys and the "key pair generator locked to ..." message - and each generator's getAlgorithm() returns the name it was obtained under: FALCON-512 / FALCON-1024, SMAUGT-MODE1 / MODE3 / MODE5 / MODET, and NTRU+KEM768 / NTRU+KEM864 / NTRU+KEM1152. The SMAUG-T parameter set names (SmaugTParameterSpec.getName(), and the algorithm the keys report) therefore move from the underscored SMAUGT_MODE1 form to the hyphenated SMAUGT-MODE1 form, and the NTRU+ services are now registered under the un-hyphenated NTRU+KEM768 form that its parameter sets already used. <b>Both previous spellings continue to resolve</b> - SMAUGT_MODE1 and NTRU+KEM-768 are registered as aliases across every service of their families - so existing getInstance calls keep working; only getAlgorithm() and the parameter set name change. SLH-DSA is corrected the same way in the entry above.</li>
121122
<li>The JCA/JCE provider classes for the pre-standardisation SPHINCS+ and Kyber have been removed from the BCPQC provider hierarchy: org.bouncycastle.pqc.jcajce.provider.sphincsplus and .kyber, their SPHINCSPlus and Kyber Mappings classes, and the unused org.bouncycastle.jcajce.provider.asymmetric.SPHINCSPlus mappings alongside them. Neither was listed in BouncyCastlePQCProvider's algorithm set or in BouncyCastleProvider's, so none of the services they described - "SPHINCS+-SHA2-128S", the BCPQC-side "ML-KEM-512", and the rest - had been obtainable through either provider; they were superseded by SLH-DSA and ML-KEM in the BC provider, which are registered and are what callers should use. The corresponding exports have been dropped from both module-info descriptors. The lightweight implementations are untouched, as is the unrelated org.bouncycastle.pqc.legacy.sphincsplus package.</li>
122123
<li>The parameter-set specific HashML-DSA Signature services in the BC provider refused a pure ML-DSA key of their own parameter set, so a caller who obtained a key as "ML-DSA-65" and a signature as "ML-DSA-65-WITH-SHA512" was met with InvalidKeyException("signature configured for ML-DSA-65-WITH-SHA512"). FIPS 204 sec. 5 defines one key generation algorithm per parameter set and the keys it produces carry no commitment to the pure mode over the pre-hash one, so the pure key was a perfectly good HashML-DSA key; the SPIs were comparing the key's algorithm name against their own, which differ by the "-WITH-SHA512" suffix. It particularly bit certificate-sourced keys, which carry the pure OID (RFC 9881), and the provider was already inconsistent about it - the unparameterised "HASH-ML-DSA" and "HASH-ML-DSA-EXTERNAL-HASH" services accepted pure keys all along. The comparison is now against the parameter set rather than the name, so a pure key is accepted wherever its parameter set matches. Signatures are unchanged: HashMLDSASigner derives the same SHA-512 pre-hash for a pure and a pre-hash key of the same parameter set, so the bytes produced are identical either way. <b>The tolerance is one way only</b> - a key that names a HashML-DSA parameter set has been narrowed to the pre-hash mode and is still refused by the pure ML-DSA services - and a key of any other parameter set is still refused in both directions (github #2397).</li>

‎prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/SPHINCS.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public void configure(ConfigurableProvider provider)
2121
{
2222
provider.addAlgorithm("KeyFactory.SPHINCS256", PREFIX + "Sphincs256KeyFactorySpi");
2323
provider.addAlgorithm("KeyPairGenerator.SPHINCS256", PREFIX + "Sphincs256KeyPairGeneratorSpi");
24+
// the name the keys themselves report - so getInstance(key.getAlgorithm()) resolves
25+
provider.addAlgorithm("Alg.Alias.KeyFactory.SPHINCS-256", "SPHINCS256");
26+
provider.addAlgorithm("Alg.Alias.KeyPairGenerator.SPHINCS-256", "SPHINCS256");
2427

2528
addSignatureAlgorithm(provider, "SHA512", "SPHINCS256", PREFIX + "SignatureSpi$withSha512", PQCObjectIdentifiers.sphincs256_with_SHA512);
2629
addSignatureAlgorithm(provider, "SHA3-512", "SPHINCS256", PREFIX + "SignatureSpi$withSha3_512", PQCObjectIdentifiers.sphincs256_with_SHA3_512);

‎prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/bike/BCBIKEPrivateKey.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.bouncycastle.pqc.jcajce.spec.BIKEParameterSpec;
1414
import org.bouncycastle.pqc.legacy.bike.BIKEPrivateKeyParameters;
1515
import org.bouncycastle.util.Arrays;
16-
import org.bouncycastle.util.Strings;
1716

1817
public class BCBIKEPrivateKey
1918
implements PrivateKey, BIKEKey
@@ -72,11 +71,11 @@ public int hashCode()
7271
}
7372

7473
/**
75-
* @return name of the algorithm - "BIKE[128|192|256]"
74+
* @return name of the algorithm - "BIKE"
7675
*/
7776
public final String getAlgorithm()
7877
{
79-
return Strings.toUpperCase(params.getParameters().getName());
78+
return "BIKE";
8079
}
8180

8281
public byte[] getEncoded()

‎prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/bike/BCBIKEPublicKey.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.bouncycastle.pqc.jcajce.interfaces.BIKEKey;
1313
import org.bouncycastle.pqc.jcajce.spec.BIKEParameterSpec;
1414
import org.bouncycastle.util.Arrays;
15-
import org.bouncycastle.util.Strings;
1615

1716
public class BCBIKEPublicKey
1817
implements PublicKey, BIKEKey
@@ -72,7 +71,7 @@ public int hashCode()
7271
*/
7372
public final String getAlgorithm()
7473
{
75-
return Strings.toUpperCase(params.getParameters().getName());
74+
return "BIKE";
7675
}
7776

7877
public byte[] getEncoded()

‎prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/bike/BIKECipherSpi.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,14 @@ else if (opmode == Cipher.UNWRAP_MODE)
182182

183183
if (bikeParameters != null)
184184
{
185-
String canonicalAlgName = Strings.toUpperCase(bikeParameters.getName());
186-
if (!canonicalAlgName.equals(key.getAlgorithm()))
185+
// the keys report the family name "BIKE", so the parameter-set lock has to look at
186+
// the key's own parameter set rather than at getAlgorithm()
187+
BIKEParameters keyParameters = (opmode == Cipher.WRAP_MODE)
188+
? wrapKey.getKeyParams().getParameters()
189+
: unwrapKey.getKeyParams().getParameters();
190+
if (!bikeParameters.getName().equals(keyParameters.getName()))
187191
{
188-
throw new InvalidKeyException("cipher locked to " + canonicalAlgName + " " + key.getAlgorithm());
192+
throw new InvalidKeyException("cipher locked to " + Strings.toUpperCase(bikeParameters.getName()));
189193
}
190194
}
191195
}

‎prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/cmce/BCCMCEPrivateKey.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.bouncycastle.pqc.jcajce.interfaces.CMCEKey;
1515
import org.bouncycastle.pqc.jcajce.spec.CMCEParameterSpec;
1616
import org.bouncycastle.util.Arrays;
17-
import org.bouncycastle.util.Strings;
1817

1918
/**
2019
* @deprecated the standardised Classic McEliece KEM (ISO/IEC 18033-2:2006/Amd 2:2026) is now provided under org.bouncycastle.crypto and org.bouncycastle.jcajce (see org.bouncycastle.crypto.params.CMCEParameters and org.bouncycastle.jcajce.spec.CMCEParameterSpec). This is the legacy NIST round 3 (non-pc, incl. mceliece348864) implementation, retained for backwards compatibility.
@@ -85,7 +84,7 @@ private BCCMCEPublicKey getPublicKey()
8584
*/
8685
public final String getAlgorithm()
8786
{
88-
return Strings.toUpperCase(params.getParameters().getName());
87+
return "CMCE";
8988
}
9089

9190
public byte[] getEncoded()

‎prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/cmce/BCCMCEPublicKey.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.bouncycastle.pqc.jcajce.interfaces.CMCEKey;
1313
import org.bouncycastle.pqc.jcajce.spec.CMCEParameterSpec;
1414
import org.bouncycastle.util.Arrays;
15-
import org.bouncycastle.util.Strings;
1615

1716
/**
1817
* @deprecated the standardised Classic McEliece KEM (ISO/IEC 18033-2:2006/Amd 2:2026) is now provided under org.bouncycastle.crypto and org.bouncycastle.jcajce (see org.bouncycastle.crypto.params.CMCEParameters and org.bouncycastle.jcajce.spec.CMCEParameterSpec). This is the legacy NIST round 3 (non-pc, incl. mceliece348864) implementation, retained for backwards compatibility.
@@ -76,7 +75,7 @@ public int hashCode()
7675
*/
7776
public final String getAlgorithm()
7877
{
79-
return Strings.toUpperCase(params.getParameters().getName());
78+
return "CMCE";
8079
}
8180

8281
public byte[] getEncoded()

‎prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/cmce/CMCECipherSpi.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,14 @@ else if (opmode == Cipher.UNWRAP_MODE)
180180

181181
if (cmceParameters != null)
182182
{
183-
String canonicalAlgName = Strings.toUpperCase(cmceParameters.getName());
184-
if (!canonicalAlgName.equals(key.getAlgorithm()))
183+
// the keys report the family name "CMCE", so the parameter-set lock has to look at
184+
// the key's own parameter set rather than at getAlgorithm()
185+
CMCEParameters keyParameters = (opmode == Cipher.WRAP_MODE)
186+
? wrapKey.getKeyParams().getParameters()
187+
: unwrapKey.getKeyParams().getParameters();
188+
if (!cmceParameters.getName().equals(keyParameters.getName()))
185189
{
186-
throw new InvalidKeyException("cipher locked to " + canonicalAlgName);
190+
throw new InvalidKeyException("cipher locked to " + Strings.toUpperCase(cmceParameters.getName()));
187191
}
188192
}
189193
}

‎prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/ntruprime/BCSNTRUPrimePrivateKey.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ private BCSNTRUPrimePublicKey getPublicKey()
7676
}
7777

7878
/**
79-
* @return name of the algorithm - "NTRULPRime"
79+
* @return name of the algorithm - "SNTRUPrime"
8080
*/
8181
public final String getAlgorithm()
8282
{
83-
return "NTRULPRime";
83+
return "SNTRUPrime";
8484
}
8585

8686
public byte[] getEncoded()

‎prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/ntruprime/BCSNTRUPrimePublicKey.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ public int hashCode()
6767
}
6868

6969
/**
70-
* @return name of the algorithm - "NTRULPRime"
70+
* @return name of the algorithm - "SNTRUPrime"
7171
*/
7272
public final String getAlgorithm()
7373
{
74-
return "NTRULPRime";
74+
return "SNTRUPrime";
7575
}
7676

7777
public byte[] getEncoded()

0 commit comments

Comments
 (0)