Skip to content

Commit 0ea89a4

Browse files
committed
added encoding preservation for ML-DSA, ML-KEM, added property for triggering seed only PrivateKeyInfo generation, relates to github #1969.
1 parent 2a4ec8a commit 0ea89a4

File tree

5 files changed

+251
-27
lines changed

5 files changed

+251
-27
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/util/PrivateKeyInfoFactory.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.bouncycastle.pqc.legacy.crypto.mceliece.McElieceCCA2PrivateKeyParameters;
5454
import org.bouncycastle.pqc.legacy.crypto.qtesla.QTESLAPrivateKeyParameters;
5555
import org.bouncycastle.util.Pack;
56+
import org.bouncycastle.util.Properties;
5657

5758
/**
5859
* Factory to create ASN.1 private key info objects from lightweight private keys.
@@ -247,18 +248,17 @@ else if (privateKey instanceof MLKEMPrivateKeyParameters)
247248
MLKEMPrivateKeyParameters params = (MLKEMPrivateKeyParameters)privateKey;
248249

249250
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.mlkemOidLookup(params.getParameters()));
250-
251-
return new PrivateKeyInfo(algorithmIdentifier, getBasicPQCEncoding(params.getSeed(), params.getEncoded()), attributes);
252-
// byte[] seed = params.getSeed();
253-
//
254-
// if (seed == null)
255-
// {
256-
// return new PrivateKeyInfo(algorithmIdentifier, params.getEncoded(), attributes);
257-
// }
258-
// else
259-
// {
260-
// return new PrivateKeyInfo(algorithmIdentifier, seed, attributes);
261-
// }
251+
252+
byte[] seed = params.getSeed();
253+
if (Properties.isOverrideSet("org.bouncycastle.mlkem.seedOnly"))
254+
{
255+
if (seed == null) // very difficult to imagine, but...
256+
{
257+
throw new IOException("no seed available");
258+
}
259+
return new PrivateKeyInfo(algorithmIdentifier, seed, attributes);
260+
}
261+
return new PrivateKeyInfo(algorithmIdentifier, getBasicPQCEncoding(seed, params.getEncoded()), attributes);
262262
}
263263
else if (privateKey instanceof NTRULPRimePrivateKeyParameters)
264264
{
@@ -297,20 +297,16 @@ else if (privateKey instanceof MLDSAPrivateKeyParameters)
297297

298298
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.mldsaOidLookup(params.getParameters()));
299299

300+
byte[] seed = params.getSeed();
301+
if (Properties.isOverrideSet("org.bouncycastle.mldsa.seedOnly"))
302+
{
303+
if (seed == null) // very difficult to imagine, but...
304+
{
305+
throw new IOException("no seed available");
306+
}
307+
return new PrivateKeyInfo(algorithmIdentifier, seed, attributes);
308+
}
300309
return new PrivateKeyInfo(algorithmIdentifier, getBasicPQCEncoding(params.getSeed(), params.getEncoded()), attributes);
301-
// byte[] seed = params.getSeed();
302-
// if (seed == null)
303-
// {
304-
// MLDSAPublicKeyParameters pubParams = params.getPublicKeyParameters();
305-
//
306-
// return new PrivateKeyInfo(algorithmIdentifier, params.getEncoded(), attributes, pubParams.getEncoded());
307-
// }
308-
// else
309-
// {
310-
// MLDSAPublicKeyParameters pubParams = params.getPublicKeyParameters();
311-
//
312-
// return new PrivateKeyInfo(algorithmIdentifier, seed, attributes, pubParams.getEncoded());
313-
// }
314310
}
315311
else if (privateKey instanceof DilithiumPrivateKeyParameters)
316312
{

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/mldsa/BCMLDSAPrivateKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public BCMLDSAPrivateKey(PrivateKeyInfo keyInfo)
4444
private void init(PrivateKeyInfo keyInfo)
4545
throws IOException
4646
{
47+
this.encoding = keyInfo.getEncoded();
4748
init((MLDSAPrivateKeyParameters)PrivateKeyFactory.createKey(keyInfo), keyInfo.getAttributes());
4849
}
4950

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/mlkem/BCMLKEMPrivateKey.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class BCMLKEMPrivateKey
2525
private transient MLKEMPrivateKeyParameters params;
2626
private transient String algorithm;
2727
private transient ASN1Set attributes;
28+
private transient byte[] priorEncoding;
2829

2930
public BCMLKEMPrivateKey(
3031
MLKEMPrivateKeyParameters params)
@@ -43,6 +44,7 @@ private void init(PrivateKeyInfo keyInfo)
4344
throws IOException
4445
{
4546
this.attributes = keyInfo.getAttributes();
47+
this.priorEncoding = keyInfo.getPrivateKey().getOctets();
4648
this.params = (MLKEMPrivateKeyParameters)PrivateKeyFactory.createKey(keyInfo);
4749
this.algorithm = Strings.toUpperCase(MLKEMParameterSpec.fromName(params.getParameters().getName()).getName());
4850
}
@@ -89,6 +91,11 @@ public byte[] getEncoded()
8991
{
9092
PrivateKeyInfo pki = PrivateKeyInfoFactory.createPrivateKeyInfo(params, attributes);
9193

94+
if (priorEncoding != null)
95+
{
96+
pki = new PrivateKeyInfo(pki.getPrivateKeyAlgorithm(), priorEncoding, attributes);
97+
}
98+
9299
return pki.getEncoded();
93100
}
94101
catch (IOException e)

0 commit comments

Comments
 (0)