Skip to content

Commit a9dcbfc

Browse files
committed
added key translation for Dilithium and Falcon
1 parent 0b72cc5 commit a9dcbfc

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.bouncycastle.pqc.asn1.PQCObjectIdentifiers;
3131
import org.bouncycastle.pqc.jcajce.provider.dilithium.DilithiumKeyFactorySpi;
3232
import org.bouncycastle.pqc.jcajce.provider.falcon.FalconKeyFactorySpi;
33+
import org.bouncycastle.pqc.jcajce.provider.kyber.KyberKeyFactorySpi;
3334
import org.bouncycastle.pqc.jcajce.provider.lms.LMSKeyFactorySpi;
3435
import org.bouncycastle.pqc.jcajce.provider.mceliece.McElieceCCA2KeyFactorySpi;
3536
import org.bouncycastle.pqc.jcajce.provider.mceliece.McElieceKeyFactorySpi;
@@ -322,6 +323,12 @@ private void loadPQCKeys()
322323
addKeyInfoConverter(BCObjectIdentifiers.dilithium2_aes, new DilithiumKeyFactorySpi());
323324
addKeyInfoConverter(BCObjectIdentifiers.dilithium3_aes, new DilithiumKeyFactorySpi());
324325
addKeyInfoConverter(BCObjectIdentifiers.dilithium5_aes, new DilithiumKeyFactorySpi());
326+
addKeyInfoConverter(BCObjectIdentifiers.kyber512, new KyberKeyFactorySpi());
327+
addKeyInfoConverter(BCObjectIdentifiers.kyber768, new KyberKeyFactorySpi());
328+
addKeyInfoConverter(BCObjectIdentifiers.kyber1024, new KyberKeyFactorySpi());
329+
addKeyInfoConverter(BCObjectIdentifiers.kyber512_aes, new KyberKeyFactorySpi());
330+
addKeyInfoConverter(BCObjectIdentifiers.kyber768_aes, new KyberKeyFactorySpi());
331+
addKeyInfoConverter(BCObjectIdentifiers.kyber1024_aes, new KyberKeyFactorySpi());
325332
}
326333

327334
public void setParameter(String parameterName, Object parameter)

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.pqc.jcajce.provider.dilithium;
22

33
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
45
import java.security.InvalidKeyException;
56
import java.security.NoSuchAlgorithmException;
67
import java.security.PrivateKey;
@@ -9,6 +10,7 @@
910
import java.security.SignatureException;
1011
import java.security.spec.AlgorithmParameterSpec;
1112

13+
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
1214
import org.bouncycastle.crypto.CipherParameters;
1315
import org.bouncycastle.crypto.params.ParametersWithRandom;
1416
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumParameters;
@@ -45,26 +47,30 @@ protected SignatureSpi(DilithiumSigner signer, DilithiumParameters parameters)
4547
protected void engineInitVerify(PublicKey publicKey)
4648
throws InvalidKeyException
4749
{
48-
if (publicKey instanceof BCDilithiumPublicKey)
50+
if (!(publicKey instanceof BCDilithiumPublicKey))
4951
{
50-
BCDilithiumPublicKey key = (BCDilithiumPublicKey)publicKey;
51-
CipherParameters param = key.getKeyParams();
52-
53-
if (parameters != null)
52+
try
5453
{
55-
String canonicalAlg = Strings.toUpperCase(parameters.getName());
56-
if (!canonicalAlg.equals(key.getAlgorithm()))
57-
{
58-
throw new InvalidKeyException("signature configured for " + canonicalAlg);
59-
}
54+
publicKey = new BCDilithiumPublicKey(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()));
55+
}
56+
catch (Exception e)
57+
{
58+
throw new InvalidKeyException("unknown public key passed to Dilithium: " + e.getMessage(), e);
6059
}
61-
62-
signer.init(false, param);
6360
}
64-
else
61+
62+
BCDilithiumPublicKey key = (BCDilithiumPublicKey)publicKey;
63+
64+
if (parameters != null)
6565
{
66-
throw new InvalidKeyException("unknown public key passed to Dilithium");
66+
String canonicalAlg = Strings.toUpperCase(parameters.getName());
67+
if (!canonicalAlg.equals(key.getAlgorithm()))
68+
{
69+
throw new InvalidKeyException("signature configured for " + canonicalAlg);
70+
}
6771
}
72+
73+
signer.init(false, key.getKeyParams());
6874
}
6975

7076
protected void engineInitSign(PrivateKey privateKey, SecureRandom random)

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.pqc.jcajce.provider.falcon;
22

33
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
45
import java.security.InvalidKeyException;
56
import java.security.NoSuchAlgorithmException;
67
import java.security.PrivateKey;
@@ -9,11 +10,13 @@
910
import java.security.SignatureException;
1011
import java.security.spec.AlgorithmParameterSpec;
1112

13+
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
1214
import org.bouncycastle.crypto.CipherParameters;
1315
import org.bouncycastle.crypto.params.ParametersWithRandom;
1416
import org.bouncycastle.pqc.crypto.falcon.FalconParameters;
1517
import org.bouncycastle.pqc.crypto.falcon.FalconPrivateKeyParameters;
1618
import org.bouncycastle.pqc.crypto.falcon.FalconSigner;
19+
import org.bouncycastle.pqc.jcajce.provider.dilithium.BCDilithiumPublicKey;
1720
import org.bouncycastle.util.Strings;
1821

1922
public class SignatureSpi
@@ -45,26 +48,30 @@ protected SignatureSpi(FalconSigner signer, FalconParameters parameters)
4548
protected void engineInitVerify(PublicKey publicKey)
4649
throws InvalidKeyException
4750
{
48-
if (publicKey instanceof BCFalconPublicKey)
51+
if (!(publicKey instanceof BCFalconPublicKey))
4952
{
50-
BCFalconPublicKey key = (BCFalconPublicKey)publicKey;
51-
CipherParameters param = key.getKeyParams();
52-
53-
if (parameters != null)
53+
try
5454
{
55-
String canonicalAlg = Strings.toUpperCase(parameters.getName());
56-
if (!canonicalAlg.equals(key.getAlgorithm()))
57-
{
58-
throw new InvalidKeyException("signature configured for " + canonicalAlg);
59-
}
55+
publicKey = new BCFalconPublicKey(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()));
56+
}
57+
catch (Exception e)
58+
{
59+
throw new InvalidKeyException("unknown public key passed to Falcon: " + e.getMessage(), e);
6060
}
61-
62-
signer.init(false, param);
6361
}
64-
else
62+
63+
BCFalconPublicKey key = (BCFalconPublicKey)publicKey;
64+
65+
if (parameters != null)
6566
{
66-
throw new InvalidKeyException("unknown public key passed to Falcon");
67+
String canonicalAlg = Strings.toUpperCase(parameters.getName());
68+
if (!canonicalAlg.equals(key.getAlgorithm()))
69+
{
70+
throw new InvalidKeyException("signature configured for " + canonicalAlg);
71+
}
6772
}
73+
74+
signer.init(false, key.getKeyParams());
6875
}
6976

7077
protected void engineInitSign(PrivateKey privateKey, SecureRandom random)

0 commit comments

Comments
 (0)