summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Hook <david.hook@keyfactor.com>2022-10-15 12:42:52 +1100
committerDavid Hook <david.hook@keyfactor.com>2022-10-15 12:42:52 +1100
commit4fc8d6b0e5cb5b386b03ee199523efb74ac316da (patch)
tree27dafe8834bbf5e7472126fffa6356093c4e89af
parentRefactoring in Pqc.Crypto.Bike (performance) (diff)
downloadBouncyCastle.NET-ed25519-4fc8d6b0e5cb5b386b03ee199523efb74ac316da.tar.xz
conflict resolution
-rw-r--r--crypto/src/BouncyCastle.Crypto.csproj2
-rw-r--r--crypto/src/asn1/bc/BCObjectIdentifiers.cs9
-rw-r--r--crypto/src/pqc/crypto/utils/PqcUtilities.cs22
-rw-r--r--crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs8
-rw-r--r--crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs10
-rw-r--r--crypto/src/pqc/crypto/utils/PublicKeyFactory.cs17
-rw-r--r--crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs11
7 files changed, 78 insertions, 1 deletions
diff --git a/crypto/src/BouncyCastle.Crypto.csproj b/crypto/src/BouncyCastle.Crypto.csproj
index ee977cce8..9786cd985 100644
--- a/crypto/src/BouncyCastle.Crypto.csproj
+++ b/crypto/src/BouncyCastle.Crypto.csproj
@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
-    <TargetFrameworks>net6.0;netstandard2.0;net451</TargetFrameworks>
+    <TargetFrameworks>net6.0;netstandard2.0;net462</TargetFrameworks>
     <RootNamespace>Org.BouncyCastle</RootNamespace>
     <AssemblyOriginatorKeyFile>..\..\BouncyCastle.snk</AssemblyOriginatorKeyFile>
     <SignAssembly>true</SignAssembly>
diff --git a/crypto/src/asn1/bc/BCObjectIdentifiers.cs b/crypto/src/asn1/bc/BCObjectIdentifiers.cs
index 424d9b6f3..f3933af87 100644
--- a/crypto/src/asn1/bc/BCObjectIdentifiers.cs
+++ b/crypto/src/asn1/bc/BCObjectIdentifiers.cs
@@ -226,5 +226,14 @@ namespace Org.BouncyCastle.Asn1.BC
         public static readonly DerObjectIdentifier bike128 = pqc_kem_bike.Branch("1");
         public static readonly DerObjectIdentifier bike192 = pqc_kem_bike.Branch("2");
         public static readonly DerObjectIdentifier bike256 = pqc_kem_bike.Branch("3");
+
+        /**
+         * HQC
+         */
+        public static readonly DerObjectIdentifier pqc_kem_hqc = bc_kem.Branch("9");
+
+        public static readonly DerObjectIdentifier hqc128 = pqc_kem_hqc.Branch("1");
+        public static readonly DerObjectIdentifier hqc192 = pqc_kem_hqc.Branch("2");
+        public static readonly DerObjectIdentifier hqc256 = pqc_kem_hqc.Branch("3");
     }
 }
diff --git a/crypto/src/pqc/crypto/utils/PqcUtilities.cs b/crypto/src/pqc/crypto/utils/PqcUtilities.cs
index d0873d702..8d73333c9 100644
--- a/crypto/src/pqc/crypto/utils/PqcUtilities.cs
+++ b/crypto/src/pqc/crypto/utils/PqcUtilities.cs
@@ -8,6 +8,7 @@ using Org.BouncyCastle.Pqc.Crypto.Cmce;
 using Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium;
 using Org.BouncyCastle.Pqc.Crypto.Crystals.Kyber;
 using Org.BouncyCastle.Pqc.Crypto.Falcon;
+using Org.BouncyCastle.Pqc.Crypto.Hqc;
 using Org.BouncyCastle.Pqc.Crypto.Picnic;
 using Org.BouncyCastle.Pqc.Crypto.Saber;
 using Org.BouncyCastle.Pqc.Crypto.Sike;
@@ -41,6 +42,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
         private readonly static Dictionary<BikeParameters, DerObjectIdentifier> bikeOids = new Dictionary<BikeParameters, DerObjectIdentifier>();
         private readonly static Dictionary<DerObjectIdentifier, BikeParameters> bikeParams = new Dictionary<DerObjectIdentifier, BikeParameters>();
 
+        private readonly static Dictionary<HqcParameters, DerObjectIdentifier> hqcOids = new Dictionary<HqcParameters, DerObjectIdentifier>();
+        private readonly static Dictionary<DerObjectIdentifier, HqcParameters> hqcParams = new Dictionary<DerObjectIdentifier, HqcParameters>();
+
         static PqcUtilities()
         {
             // CMCE
@@ -173,6 +177,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
             bikeParams[BCObjectIdentifiers.bike128] = BikeParameters.bike128;
             bikeParams[BCObjectIdentifiers.bike192] = BikeParameters.bike192;
             bikeParams[BCObjectIdentifiers.bike256] = BikeParameters.bike256;
+
+            hqcOids[HqcParameters.hqc128] = BCObjectIdentifiers.hqc128;
+            hqcOids[HqcParameters.hqc192] = BCObjectIdentifiers.hqc192;
+            hqcOids[HqcParameters.hqc256] = BCObjectIdentifiers.hqc256;
+
+            hqcParams[BCObjectIdentifiers.hqc128] = HqcParameters.hqc128;
+            hqcParams[BCObjectIdentifiers.hqc192] = HqcParameters.hqc192;
+            hqcParams[BCObjectIdentifiers.hqc256] = HqcParameters.hqc256;
         }
 
         public static DerObjectIdentifier McElieceOidLookup(CmceParameters parameters)
@@ -263,5 +275,15 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
         {
             return bikeParams[oid];
         }
+
+        internal static DerObjectIdentifier HqcOidLookup(HqcParameters parameters)
+        {
+            return hqcOids[parameters];
+        }
+
+        internal static HqcParameters HqcParamsLookup(DerObjectIdentifier oid)
+        {
+            return hqcParams[oid];
+        }
     }
 }
diff --git a/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs b/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs
index 937242903..7b0039d63 100644
--- a/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs
+++ b/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs
@@ -14,6 +14,7 @@ using Org.BouncyCastle.Pqc.Crypto.Cmce;
 using Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium;
 using Org.BouncyCastle.Pqc.Crypto.Crystals.Kyber;
 using Org.BouncyCastle.Pqc.Crypto.Falcon;
+using Org.BouncyCastle.Pqc.Crypto.Hqc;
 using Org.BouncyCastle.Pqc.Crypto.Lms;
 using Org.BouncyCastle.Pqc.Crypto.Picnic;
 using Org.BouncyCastle.Pqc.Crypto.Saber;
@@ -121,6 +122,13 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
 
                 return new BikePrivateKeyParameters(bikeParams, h0, h1, sigma);
             }
+            if (algOID.On(BCObjectIdentifiers.pqc_kem_hqc))
+            {
+                byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets();
+                HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.PrivateKeyAlgorithm.Algorithm);
+
+                return new HqcPrivateKeyParameters(hqcParams, keyEnc);
+            }
             if (algOID.Equals(BCObjectIdentifiers.kyber512)
                 || algOID.Equals(BCObjectIdentifiers.kyber512_aes)
                 || algOID.Equals(BCObjectIdentifiers.kyber768)
diff --git a/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs
index 61b02f009..e4ae51361 100644
--- a/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs
+++ b/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs
@@ -14,6 +14,7 @@ using Org.BouncyCastle.Pqc.Crypto.Picnic;
 using Org.BouncyCastle.Pqc.Crypto.Saber;
 using Org.BouncyCastle.Pqc.Crypto.Sike;
 using Org.BouncyCastle.Pqc.Crypto.Bike;
+using Org.BouncyCastle.Pqc.Crypto.Hqc;
 using Org.BouncyCastle.Pqc.Crypto.SphincsPlus;
 using Org.BouncyCastle.Utilities;
 
@@ -179,6 +180,15 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
                 AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PqcUtilities.BikeOidLookup(parameters.Parameters));
                 return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes);
             }
+            if (privateKey is HqcPrivateKeyParameters)
+            {
+                HqcPrivateKeyParameters parameters = (HqcPrivateKeyParameters)privateKey;
+
+                byte[] encoding = parameters.GetEncoded();
+
+                AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PqcUtilities.HqcOidLookup(parameters.Parameters));
+                return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes);
+            }
 
             throw new ArgumentException("Class provided is not convertible: " + Platform.GetTypeName(privateKey));
         }
diff --git a/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs b/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs
index 3f352bf04..53aa28c36 100644
--- a/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs
+++ b/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs
@@ -14,6 +14,7 @@ using Org.BouncyCastle.Pqc.Crypto.Cmce;
 using Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium;
 using Org.BouncyCastle.Pqc.Crypto.Crystals.Kyber;
 using Org.BouncyCastle.Pqc.Crypto.Falcon;
+using Org.BouncyCastle.Pqc.Crypto.Hqc;
 using Org.BouncyCastle.Pqc.Crypto.Picnic;
 using Org.BouncyCastle.Pqc.Crypto.Saber;
 using Org.BouncyCastle.Pqc.Crypto.Sike;
@@ -98,6 +99,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
             converters[BCObjectIdentifiers.bike128] = new BikeConverter();
             converters[BCObjectIdentifiers.bike192] = new BikeConverter();
             converters[BCObjectIdentifiers.bike256] = new BikeConverter();
+
+            converters[BCObjectIdentifiers.hqc128] = new HqcConverter();
+            converters[BCObjectIdentifiers.hqc192] = new HqcConverter();
+            converters[BCObjectIdentifiers.hqc256] = new HqcConverter();
         }
         
         /// <summary> Create a public key from a SubjectPublicKeyInfo encoding</summary>
@@ -304,5 +309,17 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
                 return new BikePublicKeyParameters(bikeParams, keyEnc);
             }
         }
+
+        private class HqcConverter : SubjectPublicKeyInfoConverter
+        {
+            internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams)
+            {
+                byte[] keyEnc = DerOctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets();
+
+                HqcParameters hqcParams = PqcUtilities.HqcParamsLookup(keyInfo.AlgorithmID.Algorithm);
+
+                return new HqcPublicKeyParameters(hqcParams, keyEnc);
+            }
+        }
     }
 }
\ No newline at end of file
diff --git a/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs
index a919a71e5..47cb3e4cb 100644
--- a/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs
+++ b/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs
@@ -10,6 +10,7 @@ using Org.BouncyCastle.Pqc.Crypto.Cmce;
 using Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium;
 using Org.BouncyCastle.Pqc.Crypto.Crystals.Kyber;
 using Org.BouncyCastle.Pqc.Crypto.Falcon;
+using Org.BouncyCastle.Pqc.Crypto.Hqc;
 using Org.BouncyCastle.Pqc.Crypto.Picnic;
 using Org.BouncyCastle.Pqc.Crypto.Saber;
 using Org.BouncyCastle.Pqc.Crypto.Sike;
@@ -129,6 +130,16 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
 
                 return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding));
             }
+            if (publicKey is HqcPublicKeyParameters)
+            {
+                HqcPublicKeyParameters parameters = (HqcPublicKeyParameters)publicKey;
+
+
+                byte[] encoding = parameters.GetEncoded();
+                AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PqcUtilities.HqcOidLookup(parameters.Parameters));
+
+                return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding));
+            }
 
             throw new ArgumentException("Class provided no convertible: " + Platform.GetTypeName(publicKey));
         }