summary refs log tree commit diff
path: root/crypto/src/security/PrivateKeyFactory.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-06-06 20:08:57 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-06-06 20:08:57 +0700
commitbea54d9e469082225cd81ad28d55a0bce75b1402 (patch)
treeca5f135f763ef3177cf1c33639ab5573e0722402 /crypto/src/security/PrivateKeyFactory.cs
parentMerge remote-tracking branch 'origin/master' (diff)
downloadBouncyCastle.NET-ed25519-bea54d9e469082225cd81ad28d55a0bce75b1402.tar.xz
Improve EdDSA/XDH key validation
Diffstat (limited to 'crypto/src/security/PrivateKeyFactory.cs')
-rw-r--r--crypto/src/security/PrivateKeyFactory.cs20
1 files changed, 8 insertions, 12 deletions
diff --git a/crypto/src/security/PrivateKeyFactory.cs b/crypto/src/security/PrivateKeyFactory.cs
index dfc73c2cd..408c8b6a0 100644
--- a/crypto/src/security/PrivateKeyFactory.cs
+++ b/crypto/src/security/PrivateKeyFactory.cs
@@ -174,26 +174,26 @@ namespace Org.BouncyCastle.Security
             }
             else if (algOid.Equals(EdECObjectIdentifiers.id_X25519))
             {
-                return new X25519PrivateKeyParameters(GetRawKey(keyInfo, X25519PrivateKeyParameters.KeySize), 0);
+                return new X25519PrivateKeyParameters(GetRawKey(keyInfo));
             }
             else if (algOid.Equals(EdECObjectIdentifiers.id_X448))
             {
-                return new X448PrivateKeyParameters(GetRawKey(keyInfo, X448PrivateKeyParameters.KeySize), 0);
+                return new X448PrivateKeyParameters(GetRawKey(keyInfo));
             }
             else if (algOid.Equals(EdECObjectIdentifiers.id_Ed25519))
             {
-                return new Ed25519PrivateKeyParameters(GetRawKey(keyInfo, Ed25519PrivateKeyParameters.KeySize), 0);
+                return new Ed25519PrivateKeyParameters(GetRawKey(keyInfo));
             }
             else if (algOid.Equals(EdECObjectIdentifiers.id_Ed448))
             {
-                return new Ed448PrivateKeyParameters(GetRawKey(keyInfo, Ed448PrivateKeyParameters.KeySize), 0);
+                return new Ed448PrivateKeyParameters(GetRawKey(keyInfo));
             }
             else if (algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512)
                      || algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256))
             {
                 Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(keyInfo.PrivateKeyAlgorithm.Parameters);
-                ECGost3410Parameters ecSpec = null;
-                BigInteger d = null;
+                ECGost3410Parameters ecSpec;
+                BigInteger d;
                 Asn1Object p = keyInfo.PrivateKeyAlgorithm.Parameters.ToAsn1Object();
                 if (p is Asn1Sequence && (Asn1Sequence.GetInstance(p).Count == 2 || Asn1Sequence.GetInstance(p).Count == 3))
                 {
@@ -280,13 +280,9 @@ namespace Org.BouncyCastle.Security
             }
         }
 
-        private static byte[] GetRawKey(PrivateKeyInfo keyInfo, int expectedSize)
+        private static byte[] GetRawKey(PrivateKeyInfo keyInfo)
         {
-            byte[] result = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets();
-            if (expectedSize != result.Length)
-                throw new SecurityUtilityException("private key encoding has incorrect length");
-
-            return result;
+            return Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets();
         }
 
         public static AsymmetricKeyParameter DecryptKey(