summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-05-04 16:29:36 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-05-04 16:29:36 +0700
commit7bb7b5e01054bc88dd33b54ece72990e01a28109 (patch)
tree246c808459785ecf7653b7605eeb6bc9942b75d3
parentReference field directly (diff)
downloadBouncyCastle.NET-ed25519-7bb7b5e01054bc88dd33b54ece72990e01a28109.tar.xz
Update Dilithium PrivateKeyInfo code from bc-java
-rw-r--r--crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs38
-rw-r--r--crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs7
-rw-r--r--crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs31
3 files changed, 38 insertions, 38 deletions
diff --git a/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs b/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs
index 469fbf0fe..d140bb4a2 100644
--- a/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs
+++ b/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs
@@ -167,29 +167,25 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
                 if (version != 0)
                     throw new IOException("unknown private key version: " + version);
 
-                if (keyInfo.PublicKeyData != null)
-                {
-                    Asn1Sequence pubKey = Asn1Sequence.GetInstance(keyInfo.PublicKeyData.GetOctets());
-                    return new DilithiumPrivateKeyParameters(spParams,
-                        DerBitString.GetInstance(keyEnc[1]).GetOctets(),
-                        DerBitString.GetInstance(keyEnc[2]).GetOctets(),
-                        DerBitString.GetInstance(keyEnc[3]).GetOctets(),
-                        DerBitString.GetInstance(keyEnc[4]).GetOctets(),
-                        DerBitString.GetInstance(keyEnc[5]).GetOctets(),
-                        DerBitString.GetInstance(keyEnc[6]).GetOctets(),
-                        Asn1OctetString.GetInstance(pubKey[1]).GetOctets()); // encT1
-                }
-                else
+                byte[] t1 = null;
+
+                DerBitString publicKeyData = keyInfo.PublicKeyData;
+                if (publicKeyData != null)
                 {
-                    return new DilithiumPrivateKeyParameters(spParams,
-                        DerBitString.GetInstance(keyEnc[1]).GetOctets(),
-                        DerBitString.GetInstance(keyEnc[2]).GetOctets(),
-                        DerBitString.GetInstance(keyEnc[3]).GetOctets(),
-                        DerBitString.GetInstance(keyEnc[4]).GetOctets(),
-                        DerBitString.GetInstance(keyEnc[5]).GetOctets(),
-                        DerBitString.GetInstance(keyEnc[6]).GetOctets(),
-                        null);
+                    var pubParams = PqcPublicKeyFactory.DilithiumConverter.GetPublicKeyParameters(spParams,
+                        publicKeyData);
+
+                    t1 = pubParams.GetT1();
                 }
+
+                return new DilithiumPrivateKeyParameters(spParams,
+                    DerBitString.GetInstance(keyEnc[1]).GetOctets(),
+                    DerBitString.GetInstance(keyEnc[2]).GetOctets(),
+                    DerBitString.GetInstance(keyEnc[3]).GetOctets(),
+                    DerBitString.GetInstance(keyEnc[4]).GetOctets(),
+                    DerBitString.GetInstance(keyEnc[5]).GetOctets(),
+                    DerBitString.GetInstance(keyEnc[6]).GetOctets(),
+                    t1); // encT1
             }
             if (algOid.Equals(BCObjectIdentifiers.falcon_512) ||
                 algOid.Equals(BCObjectIdentifiers.falcon_1024))
diff --git a/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs
index 5e11c6d1d..ad079d5c6 100644
--- a/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs
+++ b/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs
@@ -157,12 +157,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
                 AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(
                     PqcUtilities.DilithiumOidLookup(dilithiumPrivateKeyParameters.Parameters));
 
-                Asn1EncodableVector vPub = new Asn1EncodableVector(2);
-                vPub.Add(new DerOctetString(dilithiumPrivateKeyParameters.Rho));
-                vPub.Add(new DerOctetString(dilithiumPrivateKeyParameters.T1));
+                DilithiumPublicKeyParameters pubParams = dilithiumPrivateKeyParameters.GetPublicKeyParameters();
 
-                return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes,
-                    new DerSequence(vPub).GetEncoded());
+                return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, pubParams.GetEncoded());
             }
             if (privateKey is BikePrivateKeyParameters bikePrivateKeyParameters)
             {
diff --git a/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs b/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs
index b72bb8666..cb0bffb70 100644
--- a/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs
+++ b/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs
@@ -187,7 +187,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
             return converter.GetPublicKeyParameters(keyInfo, defaultParams);
         }
 
-        private abstract class SubjectPublicKeyInfoConverter
+        internal abstract class SubjectPublicKeyInfoConverter
         {
             internal abstract AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams);
         }
@@ -280,6 +280,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
                 return new PicnicPublicKeyParameters(picnicParams, keyEnc);
             }
         }
+
         [Obsolete("Will be removed")]
         private class SikeConverter
             : SubjectPublicKeyInfoConverter
@@ -293,21 +294,27 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
                 return new SikePublicKeyParameters(sikeParams, keyEnc);
             }
         }
-        private class DilithiumConverter
+
+        internal class DilithiumConverter
             : SubjectPublicKeyInfoConverter
         {
-            internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams)
+            internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo,
+                object defaultParams)
             {
-                DilithiumParameters dilithiumParams = PqcUtilities.DilithiumParamsLookup(keyInfo.AlgorithmID.Algorithm);
+                var dilithiumParameters = PqcUtilities.DilithiumParamsLookup(keyInfo.AlgorithmID.Algorithm);
+
+                return GetPublicKeyParameters(dilithiumParameters, keyInfo.PublicKeyData);
+            }
 
+            internal static DilithiumPublicKeyParameters GetPublicKeyParameters(DilithiumParameters dilithiumParameters,
+                DerBitString publicKeyData)
+            {
                 try
                 {
-                    Asn1Object obj = keyInfo.ParsePublicKey();
-                    if (obj is Asn1Sequence)
+                    Asn1Object obj = Asn1Object.FromByteArray(publicKeyData.GetOctets());
+                    if (obj is Asn1Sequence keySeq)
                     {
-                        Asn1Sequence keySeq = Asn1Sequence.GetInstance(obj);
-
-                        return new DilithiumPublicKeyParameters(dilithiumParams,
+                        return new DilithiumPublicKeyParameters(dilithiumParameters,
                             Asn1OctetString.GetInstance(keySeq[0]).GetOctets(),
                             Asn1OctetString.GetInstance(keySeq[1]).GetOctets());
                     }
@@ -315,13 +322,13 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities
                     {
                         byte[] encKey = Asn1OctetString.GetInstance(obj).GetOctets();
 
-                        return new DilithiumPublicKeyParameters(dilithiumParams, encKey);
+                        return new DilithiumPublicKeyParameters(dilithiumParameters, encKey);
                     }
                 }
                 catch (Exception)
                 {
-                    // raw encoding
-                    return new DilithiumPublicKeyParameters(dilithiumParams, keyInfo.PublicKeyData.GetOctets());
+                    // we're a raw encoding
+                    return new DilithiumPublicKeyParameters(dilithiumParameters, publicKeyData.GetOctets());
                 }
             }
         }