Skip to content

Commit ff89da0

Browse files
committed
Merge branch 'add-kem-hqc-jdk21' into 'main'
Add kem hqc jdk21 See merge request root/bc-java!102
2 parents bafc09a + e948e5c commit ff89da0

File tree

15 files changed

+653
-24
lines changed

15 files changed

+653
-24
lines changed

prov/src/main/java/org/bouncycastle/jce/provider/BouncyCastleProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public final class BouncyCastleProvider extends Provider
130130

131131
private static final String[] ASYMMETRIC_CIPHERS =
132132
{
133-
"DSA", "DH", "EC", "RSA", "GOST", "ECGOST", "ElGamal", "DSTU4145", "GM", "EdEC", "LMS", "SPHINCSPlus", "Dilithium", "Falcon", "NTRU", "CONTEXT", "SLHDSA", "MLDSA", "MLKEM"
133+
"DSA", "DH", "EC", "RSA", "GOST", "ECGOST", "ElGamal", "DSTU4145", "GM", "EdEC", "LMS", "SPHINCSPlus",
134+
"Dilithium", "Falcon", "NTRU", "CONTEXT", "SLHDSA", "MLDSA", "MLKEM", "HQC"
134135
};
135136

136137
/*

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class BouncyCastlePQCProvider
3232
private static final Map keyInfoConverters = new HashMap();
3333

3434
/*
35-
* Configurable symmetric ciphers
36-
*/
35+
* Configurable symmetric ciphers
36+
*/
3737
private static final String ALGORITHM_PACKAGE = "org.bouncycastle.pqc.jcajce.provider.";
3838
private static final String[] ALGORITHMS =
3939
{
@@ -118,7 +118,7 @@ public void addAlgorithm(String key, String value, Map<String, String> attribute
118118
addAttributes(key, attributes);
119119
}
120120

121-
public void addAlgorithm(String type, ASN1ObjectIdentifier oid, String className)
121+
public void addAlgorithm(String type, ASN1ObjectIdentifier oid, String className)
122122
{
123123
if (!containsKey(type + "." + className))
124124
{
@@ -151,7 +151,7 @@ public AsymmetricKeyInfoConverter getKeyInfoConverter(ASN1ObjectIdentifier oid)
151151

152152
public void addAttributes(String key, Map<String, String> attributeMap)
153153
{
154-
for (Iterator it = attributeMap.keySet().iterator(); it.hasNext();)
154+
for (Iterator it = attributeMap.keySet().iterator(); it.hasNext(); )
155155
{
156156
String attributeName = (String)it.next();
157157
String attributeKey = key + " " + attributeName;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,37 @@ public Mappings()
2020
public void configure(ConfigurableProvider provider)
2121
{
2222
provider.addAlgorithm("KeyFactory.HQC", PREFIX + "HQCKeyFactorySpi");
23+
provider.addAlgorithm("Alg.Alias.KeyFactory.HQC", "HQC");
24+
addKeyFactoryAlgorithm(provider, "HQC128", PREFIX + "HQCKeyFactorySpi$HQC128", BCObjectIdentifiers.hqc128, new HQCKeyFactorySpi.HQC128());
25+
addKeyFactoryAlgorithm(provider, "HQC192", PREFIX + "HQCKeyFactorySpi$HQC192", BCObjectIdentifiers.hqc192, new HQCKeyFactorySpi.HQC192());
26+
addKeyFactoryAlgorithm(provider, "HQC256", PREFIX + "HQCKeyFactorySpi$HQC256", BCObjectIdentifiers.hqc256, new HQCKeyFactorySpi.HQC256());
27+
2328
provider.addAlgorithm("KeyPairGenerator.HQC", PREFIX + "HQCKeyPairGeneratorSpi");
29+
provider.addAlgorithm("Alg.Alias.KeyPairGenerator.HQC", "HQC");
30+
addKeyPairGeneratorAlgorithm(provider, "HQC128", PREFIX + "HQCKeyPairGeneratorSpi$HQC128", BCObjectIdentifiers.hqc128);
31+
addKeyPairGeneratorAlgorithm(provider, "HQC192", PREFIX + "HQCKeyPairGeneratorSpi$HQC192", BCObjectIdentifiers.hqc192);
32+
addKeyPairGeneratorAlgorithm(provider, "HQC256", PREFIX + "HQCKeyPairGeneratorSpi$HQC256", BCObjectIdentifiers.hqc256);
2433

2534
provider.addAlgorithm("KeyGenerator.HQC", PREFIX + "HQCKeyGeneratorSpi");
35+
addKeyGeneratorAlgorithm(provider, "HQC128", PREFIX + "HQCKeyGeneratorSpi$HQC128", BCObjectIdentifiers.hqc128);
36+
addKeyGeneratorAlgorithm(provider, "HQC192", PREFIX + "HQCKeyGeneratorSpi$HQC192", BCObjectIdentifiers.hqc192);
37+
addKeyGeneratorAlgorithm(provider, "HQC256", PREFIX + "HQCKeyGeneratorSpi$HQC256", BCObjectIdentifiers.hqc256);
2638

2739
AsymmetricKeyInfoConverter keyFact = new HQCKeyFactorySpi();
2840

2941
provider.addAlgorithm("Cipher.HQC", PREFIX + "HQCCipherSpi$Base");
42+
provider.addAlgorithm("Alg.Alias.Cipher.HQC", "HQC");
3043
provider.addAlgorithm("Alg.Alias.Cipher." + BCObjectIdentifiers.pqc_kem_hqc, "HQC");
3144

3245
addCipherAlgorithm(provider, "HQC128", PREFIX + "HQCCipherSpi$HQC128", BCObjectIdentifiers.hqc128);
3346
addCipherAlgorithm(provider, "HQC192", PREFIX + "HQCCipherSpi$HQC192", BCObjectIdentifiers.hqc192);
3447
addCipherAlgorithm(provider, "HQC256", PREFIX + "HQCCipherSpi$HQC256", BCObjectIdentifiers.hqc256);
3548

3649
registerOid(provider, BCObjectIdentifiers.pqc_kem_hqc, "HQC", keyFact);
50+
provider.addKeyInfoConverter(BCObjectIdentifiers.hqc128, keyFact);
51+
provider.addKeyInfoConverter(BCObjectIdentifiers.hqc192, keyFact);
52+
provider.addKeyInfoConverter(BCObjectIdentifiers.hqc256, keyFact);
3753
}
3854
}
3955
}
56+

prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/frodo/FrodoCipherSpi.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
import org.bouncycastle.jcajce.spec.KEMParameterSpec;
2828
import org.bouncycastle.pqc.crypto.frodo.FrodoKEMExtractor;
2929
import org.bouncycastle.pqc.crypto.frodo.FrodoKEMGenerator;
30-
import org.bouncycastle.pqc.crypto.hqc.HQCKEMGenerator;
31-
import org.bouncycastle.pqc.jcajce.provider.hqc.BCHQCPrivateKey;
32-
import org.bouncycastle.pqc.jcajce.provider.hqc.BCHQCPublicKey;
3330
import org.bouncycastle.pqc.jcajce.provider.util.WrapUtil;
3431
import org.bouncycastle.util.Arrays;
3532
import org.bouncycastle.util.Exceptions;

prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/hqc/HQCKeyFactorySpi.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,44 @@
33
import java.io.IOException;
44
import java.security.InvalidKeyException;
55
import java.security.Key;
6-
import java.security.KeyFactorySpi;
76
import java.security.PrivateKey;
87
import java.security.PublicKey;
98
import java.security.spec.InvalidKeySpecException;
109
import java.security.spec.KeySpec;
1110
import java.security.spec.PKCS8EncodedKeySpec;
1211
import java.security.spec.X509EncodedKeySpec;
12+
import java.util.HashSet;
13+
import java.util.Set;
1314

15+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
1416
import org.bouncycastle.asn1.ASN1Primitive;
17+
import org.bouncycastle.asn1.bc.BCObjectIdentifiers;
1518
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
1619
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
17-
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
20+
import org.bouncycastle.pqc.jcajce.provider.util.BaseKeyFactorySpi;
1821

1922
public class HQCKeyFactorySpi
20-
extends KeyFactorySpi
21-
implements AsymmetricKeyInfoConverter
23+
extends BaseKeyFactorySpi
2224
{
25+
private static final Set<ASN1ObjectIdentifier> keyOids = new HashSet<ASN1ObjectIdentifier>();
26+
27+
static
28+
{
29+
keyOids.add(BCObjectIdentifiers.hqc128);
30+
keyOids.add(BCObjectIdentifiers.hqc192);
31+
keyOids.add(BCObjectIdentifiers.hqc256);
32+
}
33+
34+
public HQCKeyFactorySpi()
35+
{
36+
super(keyOids);
37+
}
38+
39+
public HQCKeyFactorySpi(ASN1ObjectIdentifier keyOids)
40+
{
41+
super(keyOids);
42+
}
43+
2344
public PrivateKey engineGeneratePrivate(KeySpec keySpec)
2445
throws InvalidKeySpecException
2546
{
@@ -113,4 +134,31 @@ public PublicKey generatePublic(SubjectPublicKeyInfo keyInfo)
113134
{
114135
return new BCHQCPublicKey(keyInfo);
115136
}
137+
138+
public static class HQC128
139+
extends HQCKeyFactorySpi
140+
{
141+
public HQC128()
142+
{
143+
super(BCObjectIdentifiers.hqc128);
144+
}
145+
}
146+
147+
public static class HQC192
148+
extends HQCKeyFactorySpi
149+
{
150+
public HQC192()
151+
{
152+
super(BCObjectIdentifiers.hqc192);
153+
}
154+
}
155+
156+
public static class HQC256
157+
extends HQCKeyFactorySpi
158+
{
159+
public HQC256()
160+
{
161+
super(BCObjectIdentifiers.hqc256);
162+
}
163+
}
116164
}

prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/hqc/HQCKeyGeneratorSpi.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,62 @@
1515
import org.bouncycastle.jcajce.spec.KEMGenerateSpec;
1616
import org.bouncycastle.pqc.crypto.hqc.HQCKEMExtractor;
1717
import org.bouncycastle.pqc.crypto.hqc.HQCKEMGenerator;
18+
import org.bouncycastle.pqc.crypto.hqc.HQCParameters;
19+
import org.bouncycastle.pqc.jcajce.spec.HQCParameterSpec;
1820
import org.bouncycastle.util.Arrays;
1921

2022
public class HQCKeyGeneratorSpi
21-
extends KeyGeneratorSpi
23+
extends KeyGeneratorSpi
2224
{
2325
private KEMGenerateSpec genSpec;
2426
private SecureRandom random;
2527
private KEMExtractSpec extSpec;
28+
private HQCParameters hqcParameters;
29+
30+
public HQCKeyGeneratorSpi()
31+
{
32+
this(null);
33+
}
34+
35+
public HQCKeyGeneratorSpi(HQCParameters hqcParameters)
36+
{
37+
this.hqcParameters = hqcParameters;
38+
}
2639

2740
protected void engineInit(SecureRandom secureRandom)
2841
{
2942
throw new UnsupportedOperationException("Operation not supported");
3043
}
3144

3245
protected void engineInit(AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom)
33-
throws InvalidAlgorithmParameterException
46+
throws InvalidAlgorithmParameterException
3447
{
3548
this.random = secureRandom;
3649
if (algorithmParameterSpec instanceof KEMGenerateSpec)
3750
{
3851
this.genSpec = (KEMGenerateSpec)algorithmParameterSpec;
3952
this.extSpec = null;
53+
if (hqcParameters != null)
54+
{
55+
String canonicalAlgName = HQCParameterSpec.fromName(hqcParameters.getName()).getName();
56+
if (!canonicalAlgName.equals(genSpec.getPublicKey().getAlgorithm()))
57+
{
58+
throw new InvalidAlgorithmParameterException("key generator locked to " + canonicalAlgName);
59+
}
60+
}
4061
}
4162
else if (algorithmParameterSpec instanceof KEMExtractSpec)
4263
{
4364
this.genSpec = null;
4465
this.extSpec = (KEMExtractSpec)algorithmParameterSpec;
66+
if (hqcParameters != null)
67+
{
68+
String canonicalAlgName = HQCParameterSpec.fromName(hqcParameters.getName()).getName();
69+
if (!canonicalAlgName.equals(extSpec.getPrivateKey().getAlgorithm()))
70+
{
71+
throw new InvalidAlgorithmParameterException("key generator locked to " + canonicalAlgName);
72+
}
73+
}
4574
}
4675
else
4776
{
@@ -91,4 +120,31 @@ protected SecretKey engineGenerateKey()
91120
return rv;
92121
}
93122
}
123+
124+
public static class HQC128
125+
extends HQCKeyGeneratorSpi
126+
{
127+
public HQC128()
128+
{
129+
super(HQCParameters.hqc128);
130+
}
131+
}
132+
133+
public static class HQC192
134+
extends HQCKeyGeneratorSpi
135+
{
136+
public HQC192()
137+
{
138+
super(HQCParameters.hqc192);
139+
}
140+
}
141+
142+
public static class HQC256
143+
extends HQCKeyGeneratorSpi
144+
{
145+
public HQC256()
146+
{
147+
super(HQCParameters.hqc256);
148+
}
149+
}
94150
}

prov/src/main/java/org/bouncycastle/pqc/jcajce/provider/hqc/HQCKeyPairGeneratorSpi.java

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.security.InvalidAlgorithmParameterException;
44
import java.security.KeyPair;
5+
import java.security.NoSuchAlgorithmException;
56
import java.security.SecureRandom;
67
import java.security.spec.AlgorithmParameterSpec;
78
import java.util.HashMap;
@@ -19,7 +20,7 @@
1920
import org.bouncycastle.util.Strings;
2021

2122
public class HQCKeyPairGeneratorSpi
22-
extends java.security.KeyPairGenerator
23+
extends java.security.KeyPairGenerator
2324
{
2425
private static Map parameters = new HashMap();
2526

@@ -45,17 +46,22 @@ public HQCKeyPairGeneratorSpi()
4546
super("HQC");
4647
}
4748

49+
protected HQCKeyPairGeneratorSpi(HQCParameterSpec paramSpec)
50+
{
51+
super(Strings.toUpperCase(paramSpec.getName()));
52+
}
53+
4854
public void initialize(
49-
int strength,
50-
SecureRandom random)
55+
int strength,
56+
SecureRandom random)
5157
{
5258
throw new IllegalArgumentException("use AlgorithmParameterSpec");
5359
}
5460

5561
public void initialize(
56-
AlgorithmParameterSpec params,
57-
SecureRandom random)
58-
throws InvalidAlgorithmParameterException
62+
AlgorithmParameterSpec params,
63+
SecureRandom random)
64+
throws InvalidAlgorithmParameterException
5965
{
6066
String name = getNameFromParams(params);
6167

@@ -101,4 +107,34 @@ public KeyPair generateKeyPair()
101107

102108
return new KeyPair(new BCHQCPublicKey(pub), new BCHQCPrivateKey(priv));
103109
}
110+
111+
public static class HQC128
112+
extends HQCKeyPairGeneratorSpi
113+
{
114+
public HQC128()
115+
throws NoSuchAlgorithmException
116+
{
117+
super(HQCParameterSpec.hqc128);
118+
}
119+
}
120+
121+
public static class HQC192
122+
extends HQCKeyPairGeneratorSpi
123+
{
124+
public HQC192()
125+
throws NoSuchAlgorithmException
126+
{
127+
super(HQCParameterSpec.hqc192);
128+
}
129+
}
130+
131+
public static class HQC256
132+
extends HQCKeyPairGeneratorSpi
133+
{
134+
public HQC256()
135+
throws NoSuchAlgorithmException
136+
{
137+
super(HQCParameterSpec.hqc256);
138+
}
139+
}
104140
}

0 commit comments

Comments
 (0)