summary refs log tree commit diff
path: root/crypto/src/pqc
diff options
context:
space:
mode:
authorDavid Hook <david.hook@keyfactor.com>2022-11-08 12:03:20 +1100
committerDavid Hook <david.hook@keyfactor.com>2022-11-08 12:03:20 +1100
commitca0885f6900a239067f317e6605059db3ebd892f (patch)
tree8743088c2302b40fb6a71873c8555536ecd60ba2 /crypto/src/pqc
parentMerge remote-tracking branch 'refs/remotes/origin/master' (diff)
downloadBouncyCastle.NET-ed25519-ca0885f6900a239067f317e6605059db3ebd892f.tar.xz
added raw encoding for Dilithium keys
Diffstat (limited to 'crypto/src/pqc')
-rw-r--r--crypto/src/pqc/crypto/utils/PublicKeyFactory.cs56
-rw-r--r--crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs3
2 files changed, 40 insertions, 19 deletions
diff --git a/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs b/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs
index 9eea279b1..792dc6f40 100644
--- a/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs
+++ b/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs
@@ -266,20 +266,28 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
             {
                 DilithiumParameters dilithiumParams = PqcUtilities.DilithiumParamsLookup(keyInfo.AlgorithmID.Algorithm);
 
-                Asn1Object obj = keyInfo.ParsePublicKey();
-                if (obj is Asn1Sequence)
+                try
                 {
-                    Asn1Sequence keySeq = Asn1Sequence.GetInstance(obj);
+                    Asn1Object obj = keyInfo.ParsePublicKey();
+                    if (obj is Asn1Sequence)
+                    {
+                        Asn1Sequence keySeq = Asn1Sequence.GetInstance(obj);
 
-                    return new DilithiumPublicKeyParameters(dilithiumParams,
-                        Asn1OctetString.GetInstance(keySeq[0]).GetOctets(),
-                        Asn1OctetString.GetInstance(keySeq[1]).GetOctets());
+                        return new DilithiumPublicKeyParameters(dilithiumParams,
+                            Asn1OctetString.GetInstance(keySeq[0]).GetOctets(),
+                            Asn1OctetString.GetInstance(keySeq[1]).GetOctets());
+                    }
+                    else
+                    {
+                        byte[] encKey = Asn1OctetString.GetInstance(obj).GetOctets();
+
+                        return new DilithiumPublicKeyParameters(dilithiumParams, encKey);
+                    }
                 }
-                else
+                catch (Exception e)
                 {
-                    byte[] encKey = Asn1OctetString.GetInstance(obj).GetOctets();
-
-                    return new DilithiumPublicKeyParameters(dilithiumParams, encKey);
+                    // raw encoding
+                    return new DilithiumPublicKeyParameters(dilithiumParams, keyInfo.PublicKeyData.GetOctets());
                 }
             }
         }
@@ -316,17 +324,31 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
             {
                 FalconParameters falconParams = PqcUtilities.FalconParamsLookup(keyInfo.AlgorithmID.Algorithm);
 
-                Asn1Object obj = keyInfo.ParsePublicKey();
-                if (obj is Asn1Sequence)
+                try
                 {
-                    byte[] keyEnc = Asn1OctetString.GetInstance(Asn1Sequence.GetInstance(obj)[0]).GetOctets();
+                    Asn1Object obj = keyInfo.ParsePublicKey();
+                    if (obj is Asn1Sequence)
+                    {
+                        byte[] keyEnc = Asn1OctetString.GetInstance(Asn1Sequence.GetInstance(obj)[0]).GetOctets();
 
-                    return new FalconPublicKeyParameters(falconParams, keyEnc);
+                        return new FalconPublicKeyParameters(falconParams, keyEnc);
+                    }
+                    else
+                    {
+                        // header byte + h
+                        byte[] keyEnc = Asn1OctetString.GetInstance(obj).GetOctets();
+
+                        if (keyEnc[0] != (byte)(0x00 + falconParams.LogN))
+                        {
+                            throw new ArgumentException("byte[] enc of Falcon h value not tagged correctly");
+                        }
+                        return new FalconPublicKeyParameters(falconParams, Arrays.CopyOfRange(keyEnc, 1, keyEnc.Length));
+                    }
                 }
-                else
+                catch (Exception e)
                 {
-                    // header byte + h
-                    byte[] keyEnc = Asn1OctetString.GetInstance(obj).GetOctets();
+                    // raw encoding
+                    byte[] keyEnc = keyInfo.PublicKeyData.GetOctets();
 
                     if (keyEnc[0] != (byte)(0x00 + falconParams.LogN))
                     {
diff --git a/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs
index f532cfdae..2b16cb260 100644
--- a/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs
+++ b/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs
@@ -125,8 +125,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
                 AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(
                     PqcUtilities.DilithiumOidLookup(dilithiumPublicKeyParameters.Parameters));
             
-                return new SubjectPublicKeyInfo(algorithmIdentifier,
-                    new DerOctetString(Arrays.Concatenate(dilithiumPublicKeyParameters.Rho, dilithiumPublicKeyParameters.T1)));
+                return new SubjectPublicKeyInfo(algorithmIdentifier, Arrays.Concatenate(dilithiumPublicKeyParameters.Rho, dilithiumPublicKeyParameters.T1));
             }
             if (publicKey is BikePublicKeyParameters bikePublicKeyParameters)
             {