summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
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;
         }
     }
 }