summary refs log tree commit diff
path: root/crypto/src/pqc
diff options
context:
space:
mode:
authorDavid Hook <david.hook@keyfactor.com>2023-07-23 20:34:35 +1000
committerDavid Hook <david.hook@keyfactor.com>2023-07-23 20:34:35 +1000
commit46590f15c9646b26e6d5ad8c10a083d25106bb58 (patch)
treef023e6c1ede20f00da42a3e560697e7b5f7f0341 /crypto/src/pqc
parentfixed encapsulation length (diff)
downloadBouncyCastle.NET-ed25519-46590f15c9646b26e6d5ad8c10a083d25106bb58.tar.xz
adjusted HQC and BIKE keys for latest BC Java changes
Diffstat (limited to 'crypto/src/pqc')
-rw-r--r--crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs35
-rw-r--r--crypto/src/pqc/crypto/utils/PqcSubjectPublicKeyInfoFactory.cs4
2 files changed, 31 insertions, 8 deletions
diff --git a/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs b/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs
index 146827034..77af73b41 100644
--- a/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs
+++ b/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs
@@ -403,11 +403,22 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
         {
             internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams)
             {
-                byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets();
+                try
+                {
+                    byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets();
 
-                BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.Algorithm.Algorithm);
+                    BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.Algorithm.Algorithm);
 
-                return new BikePublicKeyParameters(bikeParams, keyEnc);
+                    return new BikePublicKeyParameters(bikeParams, keyEnc);
+                }
+                catch (Exception e)
+                {
+                    byte[] keyEnc = keyInfo.PublicKeyData.GetOctets();
+
+                    BikeParameters bikeParams = PqcUtilities.BikeParamsLookup(keyInfo.Algorithm.Algorithm);
+
+                    return new BikePublicKeyParameters(bikeParams, keyEnc);
+                }
             }
         }
 
@@ -415,11 +426,23 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
         {
             internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams)
             {
-                byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets();
+                try
+                {
+                    byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets();
+
+                    HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.Algorithm.Algorithm);
+
+                    return new HqcPublicKeyParameters(hqcParams, keyEnc);
+                }
+                catch (Exception e)
+                {
+                    // raw encoding
+                    byte[] keyEnc = keyInfo.PublicKeyData.GetOctets();
 
-                HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.Algorithm.Algorithm);
+                    HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.Algorithm.Algorithm);
 
-                return new HqcPublicKeyParameters(hqcParams, keyEnc);
+                    return new HqcPublicKeyParameters(hqcParams, keyEnc);
+                }
             }
         }
     }
diff --git a/crypto/src/pqc/crypto/utils/PqcSubjectPublicKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/PqcSubjectPublicKeyInfoFactory.cs
index 946aca963..7b1142607 100644
--- a/crypto/src/pqc/crypto/utils/PqcSubjectPublicKeyInfoFactory.cs
+++ b/crypto/src/pqc/crypto/utils/PqcSubjectPublicKeyInfoFactory.cs
@@ -141,7 +141,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
                 AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(
                     PqcUtilities.BikeOidLookup(bikePublicKeyParameters.Parameters));
 
-                return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding));
+                return new SubjectPublicKeyInfo(algorithmIdentifier, encoding);
             }
             if (publicKey is HqcPublicKeyParameters hqcPublicKeyParameters)
             {
@@ -150,7 +150,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
                 AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(
                     PqcUtilities.HqcOidLookup(hqcPublicKeyParameters.Parameters));
 
-                return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding));
+                return new SubjectPublicKeyInfo(algorithmIdentifier, encoding);
             }
 
             throw new ArgumentException("Class provided no convertible: " + Platform.GetTypeName(publicKey));