Skip to content

Commit 55d71b3

Browse files
committed
moved to slhdsa package.
further work on kyber to lm-kem conversion (sob, sob, ...)
1 parent f47ff7a commit 55d71b3

File tree

13 files changed

+227
-120
lines changed

13 files changed

+227
-120
lines changed

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumParameters;
3737
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumPrivateKeyParameters;
3838
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumPublicKeyParameters;
39-
import org.bouncycastle.pqc.crypto.mlkem.MLKEMParameters;
40-
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPrivateKeyParameters;
4139
import org.bouncycastle.pqc.crypto.falcon.FalconParameters;
4240
import org.bouncycastle.pqc.crypto.falcon.FalconPrivateKeyParameters;
4341
import org.bouncycastle.pqc.crypto.frodo.FrodoParameters;
@@ -46,6 +44,8 @@
4644
import org.bouncycastle.pqc.crypto.hqc.HQCPrivateKeyParameters;
4745
import org.bouncycastle.pqc.crypto.lms.HSSPrivateKeyParameters;
4846
import org.bouncycastle.pqc.crypto.lms.LMSPrivateKeyParameters;
47+
import org.bouncycastle.pqc.crypto.mlkem.MLKEMParameters;
48+
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPrivateKeyParameters;
4949
import org.bouncycastle.pqc.crypto.newhope.NHPrivateKeyParameters;
5050
import org.bouncycastle.pqc.crypto.ntru.NTRUParameters;
5151
import org.bouncycastle.pqc.crypto.ntru.NTRUPrivateKeyParameters;
@@ -59,6 +59,8 @@
5959
import org.bouncycastle.pqc.crypto.rainbow.RainbowPrivateKeyParameters;
6060
import org.bouncycastle.pqc.crypto.saber.SABERParameters;
6161
import org.bouncycastle.pqc.crypto.saber.SABERPrivateKeyParameters;
62+
import org.bouncycastle.pqc.crypto.slhdsa.SLHDSAParameters;
63+
import org.bouncycastle.pqc.crypto.slhdsa.SLHDSAPrivateKeyParameters;
6264
import org.bouncycastle.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters;
6365
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusParameters;
6466
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPrivateKeyParameters;
@@ -173,19 +175,7 @@ else if (algOID.equals(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig))
173175
return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
174176
}
175177
}
176-
else if (algOID.on(BCObjectIdentifiers.sphincsPlus) || algOID.on(BCObjectIdentifiers.sphincsPlus_interop) ||
177-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_sha2_128s) ||
178-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_sha2_128f) ||
179-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_sha2_192s) ||
180-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_sha2_192f) ||
181-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_sha2_256s) ||
182-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_sha2_256f) ||
183-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_shake_128s) ||
184-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_shake_128f) ||
185-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_shake_192s) ||
186-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_shake_192f) ||
187-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_shake_256s) ||
188-
algOID.equals(NISTObjectIdentifiers.id_slh_dsa_shake_256f))
178+
else if (algOID.on(BCObjectIdentifiers.sphincsPlus) || algOID.on(BCObjectIdentifiers.sphincsPlus_interop))
189179
{
190180
SPHINCSPlusParameters spParams = Utils.sphincsPlusParamsLookup(algOID);
191181

@@ -202,6 +192,23 @@ else if (algOID.on(BCObjectIdentifiers.sphincsPlus) || algOID.on(BCObjectIdentif
202192
return new SPHINCSPlusPrivateKeyParameters(spParams, ASN1OctetString.getInstance(obj).getOctets());
203193
}
204194
}
195+
else if (Utils.shldsaParams.containsKey(algOID))
196+
{
197+
SLHDSAParameters spParams = Utils.slhdsaParamsLookup(algOID);
198+
199+
ASN1Encodable obj = keyInfo.parsePrivateKey();
200+
if (obj instanceof ASN1Sequence)
201+
{
202+
SPHINCSPLUSPrivateKey spKey = SPHINCSPLUSPrivateKey.getInstance(obj);
203+
SPHINCSPLUSPublicKey publicKey = spKey.getPublicKey();
204+
return new SLHDSAPrivateKeyParameters(spParams, spKey.getSkseed(), spKey.getSkprf(),
205+
publicKey.getPkseed(), publicKey.getPkroot());
206+
}
207+
else
208+
{
209+
return new SLHDSAPrivateKeyParameters(spParams, ASN1OctetString.getInstance(obj).getOctets());
210+
}
211+
}
205212
else if (algOID.on(BCObjectIdentifiers.picnic))
206213
{
207214
byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@
2525
import org.bouncycastle.pqc.crypto.cmce.CMCEPrivateKeyParameters;
2626
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumPrivateKeyParameters;
2727
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumPublicKeyParameters;
28-
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPrivateKeyParameters;
2928
import org.bouncycastle.pqc.crypto.falcon.FalconPrivateKeyParameters;
3029
import org.bouncycastle.pqc.crypto.frodo.FrodoPrivateKeyParameters;
3130
import org.bouncycastle.pqc.crypto.hqc.HQCPrivateKeyParameters;
3231
import org.bouncycastle.pqc.crypto.lms.Composer;
3332
import org.bouncycastle.pqc.crypto.lms.HSSPrivateKeyParameters;
3433
import org.bouncycastle.pqc.crypto.lms.LMSPrivateKeyParameters;
34+
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPrivateKeyParameters;
3535
import org.bouncycastle.pqc.crypto.newhope.NHPrivateKeyParameters;
3636
import org.bouncycastle.pqc.crypto.ntru.NTRUPrivateKeyParameters;
3737
import org.bouncycastle.pqc.crypto.ntruprime.NTRULPRimePrivateKeyParameters;
3838
import org.bouncycastle.pqc.crypto.ntruprime.SNTRUPrimePrivateKeyParameters;
3939
import org.bouncycastle.pqc.crypto.picnic.PicnicPrivateKeyParameters;
4040
import org.bouncycastle.pqc.crypto.rainbow.RainbowPrivateKeyParameters;
4141
import org.bouncycastle.pqc.crypto.saber.SABERPrivateKeyParameters;
42+
import org.bouncycastle.pqc.crypto.slhdsa.SLHDSAPrivateKeyParameters;
4243
import org.bouncycastle.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters;
4344
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPrivateKeyParameters;
4445
import org.bouncycastle.pqc.crypto.xmss.BDS;
@@ -136,11 +137,19 @@ else if (privateKey instanceof HSSPrivateKeyParameters)
136137
else if (privateKey instanceof SPHINCSPlusPrivateKeyParameters)
137138
{
138139
SPHINCSPlusPrivateKeyParameters params = (SPHINCSPlusPrivateKeyParameters)privateKey;
139-
140+
140141
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.sphincsPlusOidLookup(params.getParameters()));
141142

142143
return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(params.getEncoded()), attributes, params.getPublicKey());
143144
}
145+
else if (privateKey instanceof SLHDSAPrivateKeyParameters)
146+
{
147+
SLHDSAPrivateKeyParameters params = (SLHDSAPrivateKeyParameters)privateKey;
148+
149+
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.slhdsaOidLookup(params.getParameters()));
150+
151+
return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(params.getEncoded()), attributes, params.getPublicKey());
152+
}
144153
else if (privateKey instanceof PicnicPrivateKeyParameters)
145154
{
146155
PicnicPrivateKeyParameters params = (PicnicPrivateKeyParameters)privateKey;

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

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import org.bouncycastle.pqc.crypto.cmce.CMCEPublicKeyParameters;
3333
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumParameters;
3434
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumPublicKeyParameters;
35-
import org.bouncycastle.pqc.crypto.mlkem.MLKEMParameters;
36-
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPublicKeyParameters;
3735
import org.bouncycastle.pqc.crypto.falcon.FalconParameters;
3836
import org.bouncycastle.pqc.crypto.falcon.FalconPublicKeyParameters;
3937
import org.bouncycastle.pqc.crypto.frodo.FrodoParameters;
@@ -42,6 +40,8 @@
4240
import org.bouncycastle.pqc.crypto.hqc.HQCPublicKeyParameters;
4341
import org.bouncycastle.pqc.crypto.lms.HSSPublicKeyParameters;
4442
import org.bouncycastle.pqc.crypto.lms.LMSPublicKeyParameters;
43+
import org.bouncycastle.pqc.crypto.mlkem.MLKEMParameters;
44+
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPublicKeyParameters;
4545
import org.bouncycastle.pqc.crypto.newhope.NHPublicKeyParameters;
4646
import org.bouncycastle.pqc.crypto.ntru.NTRUParameters;
4747
import org.bouncycastle.pqc.crypto.ntru.NTRUPublicKeyParameters;
@@ -55,6 +55,8 @@
5555
import org.bouncycastle.pqc.crypto.rainbow.RainbowPublicKeyParameters;
5656
import org.bouncycastle.pqc.crypto.saber.SABERParameters;
5757
import org.bouncycastle.pqc.crypto.saber.SABERPublicKeyParameters;
58+
import org.bouncycastle.pqc.crypto.slhdsa.SLHDSAParameters;
59+
import org.bouncycastle.pqc.crypto.slhdsa.SLHDSAPublicKeyParameters;
5860
import org.bouncycastle.pqc.crypto.sphincs.SPHINCSPublicKeyParameters;
5961
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusParameters;
6062
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPublicKeyParameters;
@@ -88,18 +90,6 @@ public class PublicKeyFactory
8890
converters.put(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig, new LMSConverter());
8991
converters.put(PQCObjectIdentifiers.mcElieceCca2, new McElieceCCA2Converter());
9092
converters.put(BCObjectIdentifiers.sphincsPlus, new SPHINCSPlusConverter());
91-
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_128s, new SPHINCSPlusConverter());
92-
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_128f, new SPHINCSPlusConverter());
93-
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_192s, new SPHINCSPlusConverter());
94-
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_192f, new SPHINCSPlusConverter());
95-
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_256s, new SPHINCSPlusConverter());
96-
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_256f, new SPHINCSPlusConverter());
97-
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_128s, new SPHINCSPlusConverter());
98-
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_128f, new SPHINCSPlusConverter());
99-
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_192s, new SPHINCSPlusConverter());
100-
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_192f, new SPHINCSPlusConverter());
101-
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_256s, new SPHINCSPlusConverter());
102-
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_256f, new SPHINCSPlusConverter());
10393

10494
converters.put(BCObjectIdentifiers.sphincsPlus_sha2_128s_r3, new SPHINCSPlusConverter());
10595
converters.put(BCObjectIdentifiers.sphincsPlus_sha2_128f_r3, new SPHINCSPlusConverter());
@@ -230,6 +220,19 @@ public class PublicKeyFactory
230220
converters.put(BCObjectIdentifiers.rainbow_V_classic, new RainbowConverter());
231221
converters.put(BCObjectIdentifiers.rainbow_V_circumzenithal, new RainbowConverter());
232222
converters.put(BCObjectIdentifiers.rainbow_V_compressed, new RainbowConverter());
223+
224+
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_128s, new SLHDSAConverter());
225+
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_128f, new SLHDSAConverter());
226+
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_192s, new SLHDSAConverter());
227+
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_192f, new SLHDSAConverter());
228+
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_256s, new SLHDSAConverter());
229+
converters.put(NISTObjectIdentifiers.id_slh_dsa_sha2_256f, new SLHDSAConverter());
230+
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_128s, new SLHDSAConverter());
231+
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_128f, new SLHDSAConverter());
232+
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_192s, new SLHDSAConverter());
233+
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_192f, new SLHDSAConverter());
234+
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_256s, new SLHDSAConverter());
235+
converters.put(NISTObjectIdentifiers.id_slh_dsa_shake_256f, new SLHDSAConverter());
233236
}
234237

235238
/**
@@ -708,6 +711,31 @@ AsymmetricKeyParameter getPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Obje
708711
}
709712
}
710713

714+
private static class SLHDSAConverter
715+
extends SubjectPublicKeyInfoConverter
716+
{
717+
AsymmetricKeyParameter getPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Object defaultParams)
718+
throws IOException
719+
{
720+
try
721+
{
722+
byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePublicKey()).getOctets();
723+
724+
SLHDSAParameters spParams = Utils.slhdsaParamsLookup(keyInfo.getAlgorithm().getAlgorithm());
725+
726+
return new SLHDSAPublicKeyParameters(spParams, Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
727+
}
728+
catch (Exception e)
729+
{
730+
byte[] keyEnc = keyInfo.getPublicKeyData().getOctets();
731+
732+
SLHDSAParameters spParams = Utils.slhdsaParamsLookup(keyInfo.getAlgorithm().getAlgorithm());
733+
734+
return new SLHDSAPublicKeyParameters(spParams, keyEnc);
735+
}
736+
}
737+
}
738+
711739
private static class RainbowConverter
712740
extends SubjectPublicKeyInfoConverter
713741
{

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@
1919
import org.bouncycastle.pqc.crypto.bike.BIKEPublicKeyParameters;
2020
import org.bouncycastle.pqc.crypto.cmce.CMCEPublicKeyParameters;
2121
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumPublicKeyParameters;
22-
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPublicKeyParameters;
2322
import org.bouncycastle.pqc.crypto.falcon.FalconPublicKeyParameters;
2423
import org.bouncycastle.pqc.crypto.frodo.FrodoPublicKeyParameters;
2524
import org.bouncycastle.pqc.crypto.hqc.HQCPublicKeyParameters;
2625
import org.bouncycastle.pqc.crypto.lms.Composer;
2726
import org.bouncycastle.pqc.crypto.lms.HSSPublicKeyParameters;
2827
import org.bouncycastle.pqc.crypto.lms.LMSPublicKeyParameters;
28+
import org.bouncycastle.pqc.crypto.mlkem.MLKEMPublicKeyParameters;
2929
import org.bouncycastle.pqc.crypto.newhope.NHPublicKeyParameters;
3030
import org.bouncycastle.pqc.crypto.ntru.NTRUPublicKeyParameters;
3131
import org.bouncycastle.pqc.crypto.ntruprime.NTRULPRimePublicKeyParameters;
3232
import org.bouncycastle.pqc.crypto.ntruprime.SNTRUPrimePublicKeyParameters;
3333
import org.bouncycastle.pqc.crypto.picnic.PicnicPublicKeyParameters;
3434
import org.bouncycastle.pqc.crypto.rainbow.RainbowPublicKeyParameters;
3535
import org.bouncycastle.pqc.crypto.saber.SABERPublicKeyParameters;
36+
import org.bouncycastle.pqc.crypto.slhdsa.SLHDSAPublicKeyParameters;
3637
import org.bouncycastle.pqc.crypto.sphincs.SPHINCSPublicKeyParameters;
3738
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPublicKeyParameters;
3839
import org.bouncycastle.pqc.crypto.xmss.XMSSMTPublicKeyParameters;
@@ -100,6 +101,15 @@ else if (publicKey instanceof HSSPublicKeyParameters)
100101
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
101102
return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(encoding));
102103
}
104+
else if (publicKey instanceof SLHDSAPublicKeyParameters)
105+
{
106+
SLHDSAPublicKeyParameters params = (SLHDSAPublicKeyParameters)publicKey;
107+
108+
byte[] encoding = params.getEncoded();
109+
110+
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.slhdsaOidLookup(params.getParameters()));
111+
return new SubjectPublicKeyInfo(algorithmIdentifier, encoding);
112+
}
103113
else if (publicKey instanceof SPHINCSPlusPublicKeyParameters)
104114
{
105115
SPHINCSPlusPublicKeyParameters params = (SPHINCSPlusPublicKeyParameters)publicKey;

0 commit comments

Comments
 (0)