summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2017-06-09 15:06:20 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2017-06-09 15:06:20 +0700
commita7e03daa1cb6604a945133427c7c9cfc5e08720c (patch)
tree303f61ef7f10a4ae59e677205b73807c2617678e
parentAdd validation to DSA public key constructor (diff)
downloadBouncyCastle.NET-ed25519-a7e03daa1cb6604a945133427c7c9cfc5e08720c.tar.xz
Add validation to DH public key constructor
-rw-r--r--crypto/src/crypto/parameters/DHPublicKeyParameters.cs29
1 files changed, 21 insertions, 8 deletions
diff --git a/crypto/src/crypto/parameters/DHPublicKeyParameters.cs b/crypto/src/crypto/parameters/DHPublicKeyParameters.cs
index e79375f71..1704c47dc 100644
--- a/crypto/src/crypto/parameters/DHPublicKeyParameters.cs
+++ b/crypto/src/crypto/parameters/DHPublicKeyParameters.cs
@@ -8,6 +8,25 @@ namespace Org.BouncyCastle.Crypto.Parameters
     public class DHPublicKeyParameters
 		: DHKeyParameters
     {
+        private static BigInteger Validate(BigInteger y, DHParameters dhParams)
+        {
+            if (y == null)
+                throw new ArgumentNullException("y");
+
+            // TLS check
+            if (y.CompareTo(BigInteger.Two) < 0 || y.CompareTo(dhParams.P.Subtract(BigInteger.Two)) > 0)
+                throw new ArgumentException("invalid DH public key", "y");
+
+            // we can't validate without Q.
+            if (dhParams.Q != null
+                && !y.ModPow(dhParams.Q, dhParams.P).Equals(BigInteger.One))
+            {
+                throw new ArgumentException("y value does not appear to be in correct group", "y");
+            }
+
+            return y;
+        }
+
         private readonly BigInteger y;
 
 		public DHPublicKeyParameters(
@@ -15,10 +34,7 @@ namespace Org.BouncyCastle.Crypto.Parameters
             DHParameters	parameters)
 			: base(false, parameters)
         {
-			if (y == null)
-				throw new ArgumentNullException("y");
-
-			this.y = y;
+			this.y = Validate(y, parameters);
         }
 
 		public DHPublicKeyParameters(
@@ -27,10 +43,7 @@ namespace Org.BouncyCastle.Crypto.Parameters
 		    DerObjectIdentifier	algorithmOid)
 			: base(false, parameters, algorithmOid)
         {
-			if (y == null)
-				throw new ArgumentNullException("y");
-
-			this.y = y;
+            this.y = Validate(y, parameters);
         }
 
         public BigInteger Y