added clean point sanity check
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.params.ParametersWithUKM;
+import org.bouncycastle.math.ec.ECAlgorithms;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.BigIntegers;
@@ -52,7 +53,14 @@ public int getFieldSize()
BigInteger hd = params.getH().multiply(ukm).multiply(key.getD()).mod(params.getN());
- ECPoint P = pub.getQ().multiply(hd).normalize();
+ // Always perform calculations on the exact curve specified by our private key's parameters
+ ECPoint pubPoint = ECAlgorithms.cleanPoint(params.getCurve(), pub.getQ());
+ if (pubPoint.isInfinity())
+ {
+ throw new IllegalStateException("Infinity is not a valid public key for ECDHC");
+ }
+
+ ECPoint P = pubPoint.multiply(hd).normalize();
if (P.isInfinity())
{
