Skip to content

Commit 4bcd6b6

Browse files
committed
added SLHDSA provider struct
1 parent ccd21ee commit 4bcd6b6

File tree

15 files changed

+1413
-17
lines changed

15 files changed

+1413
-17
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.bouncycastle.jcajce.interfaces;
2+
3+
import java.security.Key;
4+
5+
import org.bouncycastle.jcajce.spec.SLHDSAParameterSpec;
6+
7+
public interface SLHDSAKey
8+
extends Key
9+
{
10+
/**
11+
* Return the parameters for this key.
12+
*
13+
* @return a SLHDSAParameterSpec
14+
*/
15+
SLHDSAParameterSpec getParameterSpec();
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.bouncycastle.jcajce.interfaces;
2+
3+
import java.security.PrivateKey;
4+
5+
public interface SLHDSAPrivateKey
6+
extends PrivateKey, SLHDSAKey
7+
{
8+
/**
9+
* Return the public key corresponding to this private key.
10+
*
11+
* @return a SLH-DSA Public Key
12+
*/
13+
SLHDSAPublicKey getPublicKey();
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.bouncycastle.jcajce.interfaces;
2+
3+
import java.security.PublicKey;
4+
5+
public interface SLHDSAPublicKey
6+
extends PublicKey, SLHDSAKey
7+
{
8+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.bouncycastle.jcajce.provider.asymmetric;
2+
3+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
4+
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
5+
import org.bouncycastle.jcajce.provider.asymmetric.slhdsa.SLHDSAKeyFactorySpi;
6+
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
7+
import org.bouncycastle.jcajce.provider.util.AsymmetricAlgorithmProvider;
8+
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
9+
10+
public class SLHDSA
11+
{
12+
private static final String PREFIX = "org.bouncycastle.jcajce.provider.asymmetric" + ".slhdsa.";
13+
14+
public static class Mappings
15+
extends AsymmetricAlgorithmProvider
16+
{
17+
public Mappings()
18+
{
19+
}
20+
21+
public void configure(ConfigurableProvider provider)
22+
{
23+
provider.addAlgorithm("KeyFactory.SLH-DSA", PREFIX + "SLHDSAKeyFactorySpi");
24+
provider.addAlgorithm("KeyPairGenerator.SLH-DSA", PREFIX + "SLHDSAKeyPairGeneratorSpi");
25+
26+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHA2-128S", PREFIX + "SLHDSAKeyPairGeneratorSpi$Sha2_128s", NISTObjectIdentifiers.id_slh_dsa_sha2_128s);
27+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHA2-128F", PREFIX + "SLHDSAKeyPairGeneratorSpi$Sha2_128f", NISTObjectIdentifiers.id_slh_dsa_sha2_128f);
28+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHA2-192S", PREFIX + "SLHDSAKeyPairGeneratorSpi$Sha2_192s", NISTObjectIdentifiers.id_slh_dsa_sha2_192s);
29+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHA2-192F", PREFIX + "SLHDSAKeyPairGeneratorSpi$Sha2_192f", NISTObjectIdentifiers.id_slh_dsa_sha2_192f);
30+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHA2-256S", PREFIX + "SLHDSAKeyPairGeneratorSpi$Sha2_256s", NISTObjectIdentifiers.id_slh_dsa_sha2_256s);
31+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHA2-256F", PREFIX + "SLHDSAKeyPairGeneratorSpi$Sha2_256f", NISTObjectIdentifiers.id_slh_dsa_sha2_256f);
32+
33+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHAKE-128S", PREFIX + "SLHDSAKeyPairGeneratorSpi$Shake_128s", NISTObjectIdentifiers.id_slh_dsa_shake_128s);
34+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHAKE-128F", PREFIX + "SLHDSAKeyPairGeneratorSpi$Shake_128f", NISTObjectIdentifiers.id_slh_dsa_shake_128f);
35+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHAKE-192S", PREFIX + "SLHDSAKeyPairGeneratorSpi$Shake_192s", NISTObjectIdentifiers.id_slh_dsa_shake_192s);
36+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHAKE-192F", PREFIX + "SLHDSAKeyPairGeneratorSpi$Shake_192f", NISTObjectIdentifiers.id_slh_dsa_shake_192f);
37+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHAKE-256S", PREFIX + "SLHDSAKeyPairGeneratorSpi$Shake_256s", NISTObjectIdentifiers.id_slh_dsa_shake_256s);
38+
addKeyPairGeneratorAlgorithm(provider, "SLH-DSA-SHAKE-256F", PREFIX + "SLHDSAKeyPairGeneratorSpi$Shake_256f", NISTObjectIdentifiers.id_slh_dsa_shake_256f);
39+
40+
addSignatureAlgorithm(provider, "SLH-DSA", PREFIX + "SignatureSpi$Direct", (ASN1ObjectIdentifier)null);
41+
42+
ASN1ObjectIdentifier[] nistOids = new ASN1ObjectIdentifier[]
43+
{
44+
NISTObjectIdentifiers.id_slh_dsa_sha2_128s,
45+
NISTObjectIdentifiers.id_slh_dsa_sha2_128f,
46+
NISTObjectIdentifiers.id_slh_dsa_shake_128s,
47+
NISTObjectIdentifiers.id_slh_dsa_shake_128f,
48+
NISTObjectIdentifiers.id_slh_dsa_sha2_192s,
49+
NISTObjectIdentifiers.id_slh_dsa_sha2_192f,
50+
NISTObjectIdentifiers.id_slh_dsa_shake_192s,
51+
NISTObjectIdentifiers.id_slh_dsa_shake_192f,
52+
NISTObjectIdentifiers.id_slh_dsa_sha2_256s,
53+
NISTObjectIdentifiers.id_slh_dsa_sha2_256f,
54+
NISTObjectIdentifiers.id_slh_dsa_shake_256s,
55+
NISTObjectIdentifiers.id_slh_dsa_shake_256f
56+
};
57+
58+
for (int i = 0; i != nistOids.length; i++)
59+
{
60+
provider.addAlgorithm("Alg.Alias.Signature." + nistOids[i], "SLH-DSA");
61+
provider.addAlgorithm("Alg.Alias.Signature.OID." + nistOids[i], "SLH-DSA");
62+
}
63+
64+
AsymmetricKeyInfoConverter keyFact = new SLHDSAKeyFactorySpi();
65+
66+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_128s, "SLH-DSA", keyFact);
67+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_128f, "SLH-DSA", keyFact);
68+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_192s, "SLH-DSA", keyFact);
69+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_192f, "SLH-DSA", keyFact);
70+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_256s, "SLH-DSA", keyFact);
71+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_256f, "SLH-DSA", keyFact);
72+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_128s, "SLH-DSA", keyFact);
73+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_128f, "SLH-DSA", keyFact);
74+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_192s, "SLH-DSA", keyFact);
75+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_192f, "SLH-DSA", keyFact);
76+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_256s, "SLH-DSA", keyFact);
77+
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_256f, "SLH-DSA", keyFact);
78+
}
79+
}
80+
}

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/SPHINCSPlus.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
44
import org.bouncycastle.asn1.bc.BCObjectIdentifiers;
5-
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
65
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
76
import org.bouncycastle.jcajce.provider.util.AsymmetricAlgorithmProvider;
87
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
@@ -76,21 +75,7 @@ public void configure(ConfigurableProvider provider)
7675
provider.addAlgorithm("Alg.Alias.Signature.SPHINCS+", "SPHINCSPLUS");
7776

7877
AsymmetricKeyInfoConverter keyFact = new SPHINCSPlusKeyFactorySpi();
79-
80-
// registerOid(provider, BCObjectIdentifiers.sphincsPlus, "SPHINCSPLUS", keyFact);
81-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_128s, "SPHINCSPLUS", keyFact);
82-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_128f, "SPHINCSPLUS", keyFact);
83-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_192s, "SPHINCSPLUS", keyFact);
84-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_192f, "SPHINCSPLUS", keyFact);
85-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_256s, "SPHINCSPLUS", keyFact);
86-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_sha2_256f, "SPHINCSPLUS", keyFact);
87-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_128s, "SPHINCSPLUS", keyFact);
88-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_128f, "SPHINCSPLUS", keyFact);
89-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_192s, "SPHINCSPLUS", keyFact);
90-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_192f, "SPHINCSPLUS", keyFact);
91-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_256s, "SPHINCSPLUS", keyFact);
92-
registerKeyFactoryOid(provider, NISTObjectIdentifiers.id_slh_dsa_shake_256f, "SPHINCSPLUS", keyFact);
93-
78+
9479
registerKeyFactoryOid(provider, BCObjectIdentifiers.sphincsPlus_sha2_128s_r3, "SPHINCSPLUS", keyFact);
9580
registerKeyFactoryOid(provider, BCObjectIdentifiers.sphincsPlus_sha2_128f_r3, "SPHINCSPLUS", keyFact);
9681
registerKeyFactoryOid(provider, BCObjectIdentifiers.sphincsPlus_shake_128s_r3, "SPHINCSPLUS", keyFact);
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package org.bouncycastle.jcajce.provider.asymmetric.slhdsa;
2+
3+
import java.io.IOException;
4+
import java.io.ObjectInputStream;
5+
import java.io.ObjectOutputStream;
6+
7+
import org.bouncycastle.asn1.ASN1Set;
8+
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
9+
import org.bouncycastle.jcajce.interfaces.SLHDSAPrivateKey;
10+
import org.bouncycastle.jcajce.interfaces.SLHDSAPublicKey;
11+
import org.bouncycastle.jcajce.spec.SLHDSAParameterSpec;
12+
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPrivateKeyParameters;
13+
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPublicKeyParameters;
14+
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
15+
import org.bouncycastle.pqc.crypto.util.PrivateKeyInfoFactory;
16+
import org.bouncycastle.util.Arrays;
17+
import org.bouncycastle.util.Strings;
18+
19+
public class BCSLHDSAPrivateKey
20+
implements SLHDSAPrivateKey
21+
{
22+
private static final long serialVersionUID = 1L;
23+
24+
private transient SPHINCSPlusPrivateKeyParameters params;
25+
private transient ASN1Set attributes;
26+
27+
public BCSLHDSAPrivateKey(
28+
SPHINCSPlusPrivateKeyParameters params)
29+
{
30+
this.params = params;
31+
}
32+
33+
public BCSLHDSAPrivateKey(PrivateKeyInfo keyInfo)
34+
throws IOException
35+
{
36+
init(keyInfo);
37+
}
38+
39+
private void init(PrivateKeyInfo keyInfo)
40+
throws IOException
41+
{
42+
this.attributes = keyInfo.getAttributes();
43+
this.params = (SPHINCSPlusPrivateKeyParameters)PrivateKeyFactory.createKey(keyInfo);
44+
}
45+
46+
/**
47+
* Compare this SPHINCS-256 private key with another object.
48+
*
49+
* @param o the other object
50+
* @return the result of the comparison
51+
*/
52+
public boolean equals(Object o)
53+
{
54+
if (o == this)
55+
{
56+
return true;
57+
}
58+
59+
if (o instanceof BCSLHDSAPrivateKey)
60+
{
61+
BCSLHDSAPrivateKey otherKey = (BCSLHDSAPrivateKey)o;
62+
63+
return Arrays.areEqual(params.getEncoded(), otherKey.params.getEncoded());
64+
}
65+
66+
return false;
67+
}
68+
69+
public int hashCode()
70+
{
71+
return Arrays.hashCode(params.getEncoded());
72+
}
73+
74+
/**
75+
* @return name of the algorithm - "SLH-DSA"
76+
*/
77+
public final String getAlgorithm()
78+
{
79+
return "SLH-DSA" + "-" + Strings.toUpperCase(params.getParameters().getName());
80+
}
81+
82+
public byte[] getEncoded()
83+
{
84+
85+
try
86+
{
87+
PrivateKeyInfo pki = PrivateKeyInfoFactory.createPrivateKeyInfo(params, attributes);
88+
89+
return pki.getEncoded();
90+
}
91+
catch (IOException e)
92+
{
93+
return null;
94+
}
95+
}
96+
97+
public SLHDSAPublicKey getPublicKey()
98+
{
99+
return new BCSLHDSAPublicKey(new SPHINCSPlusPublicKeyParameters(params.getParameters(), params.getPublicKey()));
100+
}
101+
102+
public SLHDSAParameterSpec getParameterSpec()
103+
{
104+
return SLHDSAParameterSpec.fromName(params.getParameters().getName());
105+
}
106+
107+
public String getFormat()
108+
{
109+
return "PKCS#8";
110+
}
111+
112+
SPHINCSPlusPrivateKeyParameters getKeyParams()
113+
{
114+
return params;
115+
}
116+
117+
private void readObject(
118+
ObjectInputStream in)
119+
throws IOException, ClassNotFoundException
120+
{
121+
in.defaultReadObject();
122+
123+
byte[] enc = (byte[])in.readObject();
124+
125+
init(PrivateKeyInfo.getInstance(enc));
126+
}
127+
128+
private void writeObject(
129+
ObjectOutputStream out)
130+
throws IOException
131+
{
132+
out.defaultWriteObject();
133+
134+
out.writeObject(this.getEncoded());
135+
}
136+
}

0 commit comments

Comments
 (0)