Skip to content

Commit 4e69828

Browse files
committed
added specific key factories for Falcon/Dilithium/Kyber
1 parent 4067865 commit 4e69828

File tree

12 files changed

+357
-135
lines changed

12 files changed

+357
-135
lines changed

prov/src/main/java/org/bouncycastle/jcajce/provider/util/AsymmetricAlgorithmProvider.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.bouncycastle.jcajce.provider.util;
22

3+
import java.security.KeyFactorySpi;
34
import java.util.Map;
45

56
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
67
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
8+
import org.bouncycastle.jcajce.provider.symmetric.AES;
79

810
public abstract class AsymmetricAlgorithmProvider
911
extends AlgorithmProvider
@@ -99,6 +101,23 @@ protected void addKeyPairGeneratorAlgorithm(
99101
}
100102
}
101103

104+
protected void addKeyFactoryAlgorithm(
105+
ConfigurableProvider provider,
106+
String algorithm,
107+
String className,
108+
ASN1ObjectIdentifier oid,
109+
AsymmetricKeyInfoConverter keyInfoConverter)
110+
{
111+
provider.addAlgorithm("KeyFactory." + algorithm, className);
112+
if (oid != null)
113+
{
114+
provider.addAlgorithm("Alg.Alias.KeyFactory." + oid, algorithm);
115+
provider.addAlgorithm("Alg.Alias.KeyFactory.OID." + oid, algorithm);
116+
117+
provider.addKeyInfoConverter(oid, keyInfoConverter);
118+
}
119+
}
120+
102121
protected void addKeyGeneratorAlgorithm(
103122
ConfigurableProvider provider,
104123
String algorithm,

prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/Dilithium.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.bouncycastle.jcajce.provider.util.AsymmetricAlgorithmProvider;
66
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
77
import org.bouncycastle.pqc.jcajce.provider.dilithium.DilithiumKeyFactorySpi;
8+
import org.bouncycastle.pqc.jcajce.provider.falcon.FalconKeyFactorySpi;
89

910
public class Dilithium
1011
{
@@ -20,6 +21,14 @@ public Mappings()
2021
public void configure(ConfigurableProvider provider)
2122
{
2223
provider.addAlgorithm("KeyFactory.DILITHIUM", PREFIX + "DilithiumKeyFactorySpi");
24+
25+
addKeyFactoryAlgorithm(provider, "DILITHIUM2", PREFIX + "DilithiumKeyFactorySpi$Base2", BCObjectIdentifiers.dilithium2, new DilithiumKeyFactorySpi.Base2());
26+
addKeyFactoryAlgorithm(provider, "DILITHIUM3", PREFIX + "DilithiumKeyFactorySpi$Base3", BCObjectIdentifiers.dilithium3, new DilithiumKeyFactorySpi.Base3());
27+
addKeyFactoryAlgorithm(provider, "DILITHIUM5", PREFIX + "DilithiumKeyFactorySpi$Base5", BCObjectIdentifiers.dilithium5, new DilithiumKeyFactorySpi.Base5());
28+
addKeyFactoryAlgorithm(provider, "DILITHIUM2-AES", PREFIX + "DilithiumKeyFactorySpi$Base2_AES", BCObjectIdentifiers.dilithium2_aes, new DilithiumKeyFactorySpi.Base2());
29+
addKeyFactoryAlgorithm(provider, "DILITHIUM3-AES", PREFIX + "DilithiumKeyFactorySpi$Base3_AES", BCObjectIdentifiers.dilithium3_aes, new DilithiumKeyFactorySpi.Base3());
30+
addKeyFactoryAlgorithm(provider, "DILITHIUM5-AES", PREFIX + "DilithiumKeyFactorySpi$Base5_AES", BCObjectIdentifiers.dilithium5_aes, new DilithiumKeyFactorySpi.Base5());
31+
2332
provider.addAlgorithm("KeyPairGenerator.DILITHIUM", PREFIX + "DilithiumKeyPairGeneratorSpi");
2433

2534
addKeyPairGeneratorAlgorithm(provider, "DILITHIUM2", PREFIX + "DilithiumKeyPairGeneratorSpi$Base2", BCObjectIdentifiers.dilithium2);
@@ -37,15 +46,6 @@ public void configure(ConfigurableProvider provider)
3746
addSignatureAlgorithm(provider, "DILITHIUM2-AES", PREFIX + "SignatureSpi$Base2_AES", BCObjectIdentifiers.dilithium2_aes);
3847
addSignatureAlgorithm(provider, "DILITHIUM3-AES", PREFIX + "SignatureSpi$Base3_AES", BCObjectIdentifiers.dilithium3_aes);
3948
addSignatureAlgorithm(provider, "DILITHIUM5-AES", PREFIX + "SignatureSpi$Base5_AES", BCObjectIdentifiers.dilithium5_aes);
40-
41-
AsymmetricKeyInfoConverter keyFact = new DilithiumKeyFactorySpi();
42-
43-
registerKeyFactoryOid(provider, BCObjectIdentifiers.dilithium2, "DILITHIUM", keyFact);
44-
registerKeyFactoryOid(provider, BCObjectIdentifiers.dilithium3, "DILITHIUM", keyFact);
45-
registerKeyFactoryOid(provider, BCObjectIdentifiers.dilithium5, "DILITHIUM", keyFact);
46-
registerKeyFactoryOid(provider, BCObjectIdentifiers.dilithium2_aes, "DILITHIUM", keyFact);
47-
registerKeyFactoryOid(provider, BCObjectIdentifiers.dilithium3_aes, "DILITHIUM", keyFact);
48-
registerKeyFactoryOid(provider, BCObjectIdentifiers.dilithium5_aes, "DILITHIUM", keyFact);
4949
}
5050
}
5151
}

prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/Falcon.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public Mappings()
2020
public void configure(ConfigurableProvider provider)
2121
{
2222
provider.addAlgorithm("KeyFactory.FALCON", PREFIX + "FalconKeyFactorySpi");
23+
24+
addKeyFactoryAlgorithm(provider, "FALCON-512", PREFIX + "FalconKeyFactorySpi$Falcon512", BCObjectIdentifiers.falcon_512, new FalconKeyFactorySpi.Falcon512());
25+
addKeyFactoryAlgorithm(provider, "FALCON-1024", PREFIX + "FalconKeyFactorySpi$Falcon1024", BCObjectIdentifiers.falcon_1024, new FalconKeyFactorySpi.Falcon1024());
26+
2327
provider.addAlgorithm("KeyPairGenerator.FALCON", PREFIX + "FalconKeyPairGeneratorSpi");
2428

2529
addKeyPairGeneratorAlgorithm(provider, "FALCON-512", PREFIX + "FalconKeyPairGeneratorSpi$Falcon512", BCObjectIdentifiers.falcon_512);
@@ -29,11 +33,6 @@ public void configure(ConfigurableProvider provider)
2933

3034
addSignatureAlgorithm(provider, "FALCON-512", PREFIX + "SignatureSpi$Falcon512", BCObjectIdentifiers.falcon_512);
3135
addSignatureAlgorithm(provider, "FALCON-1024", PREFIX + "SignatureSpi$Falcon1024", BCObjectIdentifiers.falcon_1024);
32-
33-
AsymmetricKeyInfoConverter keyFact = new FalconKeyFactorySpi();
34-
35-
registerKeyFactoryOid(provider, BCObjectIdentifiers.falcon_512, "FALCON", keyFact);
36-
registerKeyFactoryOid(provider, BCObjectIdentifiers.falcon_1024, "FALCON", keyFact);
3736
}
3837
}
3938
}

prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/Kyber.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public Mappings()
2020
public void configure(ConfigurableProvider provider)
2121
{
2222
provider.addAlgorithm("KeyFactory.KYBER", PREFIX + "KyberKeyFactorySpi");
23+
24+
addKeyFactoryAlgorithm(provider, "KYBER512", PREFIX + "KyberKeyFactorySpi$Kyber512", BCObjectIdentifiers.kyber512, new KyberKeyFactorySpi.Kyber512());
25+
addKeyFactoryAlgorithm(provider, "KYBER768", PREFIX + "KyberKeyFactorySpi$Kyber768", BCObjectIdentifiers.kyber768, new KyberKeyFactorySpi.Kyber768());
26+
addKeyFactoryAlgorithm(provider, "KYBER1024", PREFIX + "KyberKeyFactorySpi$Kyber1024", BCObjectIdentifiers.kyber1024, new KyberKeyFactorySpi.Kyber1024());
27+
addKeyFactoryAlgorithm(provider, "KYBER512-AES", PREFIX + "KyberKeyFactorySpi$Kyber512_AES", BCObjectIdentifiers.kyber512_aes, new KyberKeyFactorySpi.Kyber512_AES());
28+
addKeyFactoryAlgorithm(provider, "KYBER768-AES", PREFIX + "KyberKeyFactorySpi$Kyber768_AES", BCObjectIdentifiers.kyber768_aes, new KyberKeyFactorySpi.Kyber768_AES());
29+
addKeyFactoryAlgorithm(provider, "KYBER1024-AES", PREFIX + "KyberKeyFactorySpi$Kyber1024_AES", BCObjectIdentifiers.kyber1024_aes, new KyberKeyFactorySpi.Kyber1024_AES());
30+
2331
provider.addAlgorithm("KeyPairGenerator.KYBER", PREFIX + "KyberKeyPairGeneratorSpi");
2432

2533
addKeyPairGeneratorAlgorithm(provider, "KYBER512", PREFIX + "KyberKeyPairGeneratorSpi$Kyber512", BCObjectIdentifiers.kyber512);

prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/dilithium/DilithiumKeyFactorySpi.java

Lines changed: 77 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,48 @@
44
import java.security.InvalidKeyException;
55
import java.security.Key;
66
import java.security.KeyFactorySpi;
7+
import java.security.NoSuchAlgorithmException;
78
import java.security.PrivateKey;
89
import java.security.PublicKey;
910
import java.security.spec.InvalidKeySpecException;
1011
import java.security.spec.KeySpec;
1112
import java.security.spec.PKCS8EncodedKeySpec;
1213
import java.security.spec.X509EncodedKeySpec;
14+
import java.util.HashSet;
15+
import java.util.Set;
1316

17+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
1418
import org.bouncycastle.asn1.ASN1Primitive;
19+
import org.bouncycastle.asn1.bc.BCObjectIdentifiers;
1520
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
1621
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
1722
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
23+
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumParameters;
24+
import org.bouncycastle.pqc.jcajce.provider.util.BaseKeyFactorySpi;
1825

1926
public class DilithiumKeyFactorySpi
20-
extends KeyFactorySpi
21-
implements AsymmetricKeyInfoConverter
27+
extends BaseKeyFactorySpi
2228
{
23-
public PrivateKey engineGeneratePrivate(KeySpec keySpec)
24-
throws InvalidKeySpecException
25-
{
26-
if (keySpec instanceof PKCS8EncodedKeySpec)
27-
{
28-
// get the DER-encoded Key according to PKCS#8 from the spec
29-
byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded();
29+
private static final Set<ASN1ObjectIdentifier> keyOids = new HashSet<ASN1ObjectIdentifier>();
3030

31-
try
32-
{
33-
return generatePrivate(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)));
34-
}
35-
catch (Exception e)
36-
{
37-
throw new InvalidKeySpecException(e.toString());
38-
}
39-
}
40-
41-
throw new InvalidKeySpecException("Unsupported key specification: "
42-
+ keySpec.getClass() + ".");
31+
static
32+
{
33+
keyOids.add(BCObjectIdentifiers.dilithium2);
34+
keyOids.add(BCObjectIdentifiers.dilithium3);
35+
keyOids.add(BCObjectIdentifiers.dilithium5);
36+
keyOids.add(BCObjectIdentifiers.dilithium2_aes);
37+
keyOids.add(BCObjectIdentifiers.dilithium3_aes);
38+
keyOids.add(BCObjectIdentifiers.dilithium5_aes);
4339
}
4440

45-
public PublicKey engineGeneratePublic(KeySpec keySpec)
46-
throws InvalidKeySpecException
41+
public DilithiumKeyFactorySpi()
4742
{
48-
if (keySpec instanceof X509EncodedKeySpec)
49-
{
50-
// get the DER-encoded Key according to X.509 from the spec
51-
byte[] encKey = ((X509EncodedKeySpec)keySpec).getEncoded();
52-
53-
// decode the SubjectPublicKeyInfo data structure to the pki object
54-
try
55-
{
56-
return generatePublic(SubjectPublicKeyInfo.getInstance(encKey));
57-
}
58-
catch (Exception e)
59-
{
60-
throw new InvalidKeySpecException(e.toString());
61-
}
62-
}
43+
super(keyOids);
44+
}
6345

64-
throw new InvalidKeySpecException("Unknown key specification: " + keySpec + ".");
46+
public DilithiumKeyFactorySpi(ASN1ObjectIdentifier keyOid)
47+
{
48+
super(keyOid);
6549
}
6650

6751
public final KeySpec engineGetKeySpec(Key key, Class keySpec)
@@ -113,4 +97,58 @@ public PublicKey generatePublic(SubjectPublicKeyInfo keyInfo)
11397
{
11498
return new BCDilithiumPublicKey(keyInfo);
11599
}
100+
101+
public static class Base2
102+
extends DilithiumKeyFactorySpi
103+
{
104+
public Base2()
105+
{
106+
super(BCObjectIdentifiers.dilithium2);
107+
}
108+
}
109+
110+
public static class Base3
111+
extends DilithiumKeyFactorySpi
112+
{
113+
public Base3()
114+
{
115+
super(BCObjectIdentifiers.dilithium3);
116+
}
117+
}
118+
119+
public static class Base5
120+
extends DilithiumKeyFactorySpi
121+
{
122+
public Base5()
123+
{
124+
super(BCObjectIdentifiers.dilithium5);
125+
}
126+
}
127+
128+
public static class Base2_AES
129+
extends DilithiumKeyFactorySpi
130+
{
131+
public Base2_AES()
132+
{
133+
super(BCObjectIdentifiers.dilithium2_aes);
134+
}
135+
}
136+
137+
public static class Base3_AES
138+
extends DilithiumKeyFactorySpi
139+
{
140+
public Base3_AES()
141+
{
142+
super(BCObjectIdentifiers.dilithium3_aes);
143+
}
144+
}
145+
146+
public static class Base5_AES
147+
extends DilithiumKeyFactorySpi
148+
{
149+
public Base5_AES()
150+
{
151+
super(BCObjectIdentifiers.dilithium5_aes);
152+
}
153+
}
116154
}

prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/falcon/FalconKeyFactorySpi.java

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,35 @@
1010
import java.security.spec.KeySpec;
1111
import java.security.spec.PKCS8EncodedKeySpec;
1212
import java.security.spec.X509EncodedKeySpec;
13+
import java.util.HashSet;
14+
import java.util.Set;
1315

14-
import org.bouncycastle.asn1.ASN1Primitive;
16+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
17+
import org.bouncycastle.asn1.bc.BCObjectIdentifiers;
1518
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
1619
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
1720
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
21+
import org.bouncycastle.pqc.jcajce.provider.util.BaseKeyFactorySpi;
1822

1923
public class FalconKeyFactorySpi
20-
extends KeyFactorySpi
21-
implements AsymmetricKeyInfoConverter
24+
extends BaseKeyFactorySpi
2225
{
23-
public PrivateKey engineGeneratePrivate(KeySpec keySpec)
24-
throws InvalidKeySpecException
25-
{
26-
if (keySpec instanceof PKCS8EncodedKeySpec)
27-
{
28-
// get the DER-encoded Key according to PKCS#8 from the spec
29-
byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded();
26+
private static final Set<ASN1ObjectIdentifier> keyOids = new HashSet<ASN1ObjectIdentifier>();
3027

31-
try
32-
{
33-
return generatePrivate(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)));
34-
}
35-
catch (Exception e)
36-
{
37-
throw new InvalidKeySpecException(e.toString());
38-
}
39-
}
40-
41-
throw new InvalidKeySpecException("Unsupported key specification: "
42-
+ keySpec.getClass() + ".");
28+
static
29+
{
30+
keyOids.add(BCObjectIdentifiers.falcon_512);
31+
keyOids.add(BCObjectIdentifiers.falcon_1024);
4332
}
4433

45-
public PublicKey engineGeneratePublic(KeySpec keySpec)
46-
throws InvalidKeySpecException
34+
public FalconKeyFactorySpi()
4735
{
48-
if (keySpec instanceof X509EncodedKeySpec)
49-
{
50-
// get the DER-encoded Key according to X.509 from the spec
51-
byte[] encKey = ((X509EncodedKeySpec)keySpec).getEncoded();
52-
53-
// decode the SubjectPublicKeyInfo data structure to the pki object
54-
try
55-
{
56-
return generatePublic(SubjectPublicKeyInfo.getInstance(encKey));
57-
}
58-
catch (Exception e)
59-
{
60-
throw new InvalidKeySpecException(e.toString());
61-
}
62-
}
36+
super(keyOids);
37+
}
6338

64-
throw new InvalidKeySpecException("Unknown key specification: " + keySpec + ".");
39+
public FalconKeyFactorySpi(ASN1ObjectIdentifier keyOid)
40+
{
41+
super(keyOid);
6542
}
6643

6744
public final KeySpec engineGetKeySpec(Key key, Class keySpec)
@@ -113,4 +90,22 @@ public PublicKey generatePublic(SubjectPublicKeyInfo keyInfo)
11390
{
11491
return new BCFalconPublicKey(keyInfo);
11592
}
93+
94+
public static class Falcon512
95+
extends FalconKeyFactorySpi
96+
{
97+
public Falcon512()
98+
{
99+
super(BCObjectIdentifiers.falcon_512);
100+
}
101+
}
102+
103+
public static class Falcon1024
104+
extends FalconKeyFactorySpi
105+
{
106+
public Falcon1024()
107+
{
108+
super(BCObjectIdentifiers.falcon_1024);
109+
}
110+
}
116111
}

0 commit comments

Comments
 (0)