summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2017-06-10 12:58:54 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2017-06-10 12:58:54 +0700
commit00b8d26b9d7de162931ae6e01e39289975501de0 (patch)
tree7df8da4e43d09b462d706b431706183b84e913b0 /crypto
parentMark RFC5114 DH groups as Obsolete (diff)
downloadBouncyCastle.NET-ed25519-00b8d26b9d7de162931ae6e01e39289975501de0.tar.xz
Add result checks in DH agreements
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/crypto/agreement/DHAgreement.cs14
-rw-r--r--crypto/src/crypto/agreement/DHBasicAgreement.cs8
2 files changed, 13 insertions, 9 deletions
diff --git a/crypto/src/crypto/agreement/DHAgreement.cs b/crypto/src/crypto/agreement/DHAgreement.cs
index d214caafe..b5af104f9 100644
--- a/crypto/src/crypto/agreement/DHAgreement.cs
+++ b/crypto/src/crypto/agreement/DHAgreement.cs
@@ -81,13 +81,15 @@ namespace Org.BouncyCastle.Crypto.Agreement
 				throw new ArgumentNullException("message");
 
 			if (!pub.Parameters.Equals(dhParams))
-			{
 				throw new ArgumentException("Diffie-Hellman public key has wrong parameters.");
-			}
 
-			BigInteger p = dhParams.P;
+            BigInteger p = dhParams.P;
 
-			return message.ModPow(key.X, p).Multiply(pub.Y.ModPow(privateValue, p)).Mod(p);
-		}
-	}
+            BigInteger result = pub.Y.ModPow(privateValue, p);
+            if (result.Equals(BigInteger.One))
+                throw new InvalidOperationException("Shared key can't be 1");
+
+            return message.ModPow(key.X, p).Multiply(result).Mod(p);
+        }
+    }
 }
diff --git a/crypto/src/crypto/agreement/DHBasicAgreement.cs b/crypto/src/crypto/agreement/DHBasicAgreement.cs
index 75b5e9db5..d6f017e32 100644
--- a/crypto/src/crypto/agreement/DHBasicAgreement.cs
+++ b/crypto/src/crypto/agreement/DHBasicAgreement.cs
@@ -54,11 +54,13 @@ namespace Org.BouncyCastle.Crypto.Agreement
             DHPublicKeyParameters pub = (DHPublicKeyParameters)pubKey;
 
             if (!pub.Parameters.Equals(dhParams))
-            {
                 throw new ArgumentException("Diffie-Hellman public key has wrong parameters.");
-            }
 
-            return pub.Y.ModPow(key.X, dhParams.P);
+            BigInteger result = pub.Y.ModPow(key.X, dhParams.P);
+            if (result.Equals(BigInteger.One))
+                throw new InvalidOperationException("Shared key can't be 1");
+
+            return result;
         }
     }
 }