ECPoint q2u = ECAlgorithms.cleanPoint(curve, Q2U.getQ()); ECPoint q1v = ECAlgorithms.cleanPoint(curve, Q1V.getQ()); ECPoint q2v = ECAlgorithms.cleanPoint(curve, Q2V.getQ());
/** * Decrypt an EC pair producing the original EC point. * * @param pair the EC point pair to process. * @return the result of the Elgamal process. */ public ECPoint decrypt(ECPair pair) { if (key == null) { throw new IllegalStateException("ECElGamalDecryptor not initialised"); } ECCurve curve = key.getParameters().getCurve(); ECPoint tmp = ECAlgorithms.cleanPoint(curve, pair.getX()).multiply(key.getD()); return ECAlgorithms.cleanPoint(curve, pair.getY()).subtract(tmp).normalize(); } }
private ECPoint calculateU(SM2KeyExchangePublicParameters otherPub) { ECDomainParameters params = staticKey.getParameters(); ECPoint p1 = ECAlgorithms.cleanPoint(params.getCurve(), otherPub.getStaticPublicKey().getQ()); ECPoint p2 = ECAlgorithms.cleanPoint(params.getCurve(), otherPub.getEphemeralPublicKey().getQ()); BigInteger x1 = reduce(ephemeralPubPoint.getAffineXCoord().toBigInteger()); BigInteger x2 = reduce(p2.getAffineXCoord().toBigInteger()); BigInteger tA = staticKey.getD().add(x1.multiply(ephemeralKey.getD())); BigInteger k1 = ecParams.getH().multiply(tA).mod(ecParams.getN()); BigInteger k2 = k1.multiply(x2).mod(ecParams.getN()); return ECAlgorithms.sumOfTwoMultiplies(p1, k1, p2, k2).normalize(); }
public byte[] calculateAgreement( CipherParameters pubKey) { ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey; ECDomainParameters params = key.getParameters(); if (!params.equals(pub.getParameters())) { throw new IllegalStateException("ECVKO public key has wrong domain parameters"); } BigInteger hd = params.getH().multiply(ukm).multiply(key.getD()).mod(params.getN()); // 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()) { throw new IllegalStateException("Infinity is not a valid agreement value for ECVKO"); } return fromPoint(P); }
public BigInteger calculateAgreement( CipherParameters pubKey) { ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey; ECDomainParameters params = key.getParameters(); if (!params.equals(pub.getParameters())) { throw new IllegalStateException("ECDHC public key has wrong domain parameters"); } BigInteger hd = params.getH().multiply(key.getD()).mod(params.getN()); // 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()) { throw new IllegalStateException("Infinity is not a valid agreement value for ECDHC"); } return P.getAffineXCoord().toBigInteger(); } }
ECPoint Q = ECAlgorithms.cleanPoint(params.getCurve(), pub.getQ()); if (Q.isInfinity())
/** * Transform an existing cipher text pair using the ElGamal algorithm. Note: it is assumed this * transform has been initialised with the same public key that was used to create the original * cipher text. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECFixedTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = this.k.mod(n); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getX())), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getY())) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * Transform an existing cipher test pair using the ElGamal algorithm. Note: it is assumed this * transform has been initialised with the same public key that was used to create the original * cipher text. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECNewRandomnessTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = ECUtil.generateK(n, random); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getX())), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getY())) }; ec.getCurve().normalizeAll(gamma_phi); lastK = k; return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * Process a single EC point using the basic ElGamal algorithm. * * @param point the EC point to process. * @return the result of the Elgamal process. */ public ECPair encrypt(ECPoint point) { if (key == null) { throw new IllegalStateException("ECElGamalEncryptor not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger k = ECUtil.generateK(ec.getN(), random); ECMultiplier basePointMultiplier = createBasePointMultiplier(); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), point)) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }
/** * Transform an existing cipher text pair using the ElGamal algorithm. Note: the input cipherText will * need to be preserved in order to complete the transformation to the new public key. * * @param cipherText the EC point to process. * @return returns a new ECPair representing the result of the process. */ public ECPair transform(ECPair cipherText) { if (key == null) { throw new IllegalStateException("ECNewPublicKeyTransform not initialised"); } ECDomainParameters ec = key.getParameters(); BigInteger n = ec.getN(); ECMultiplier basePointMultiplier = createBasePointMultiplier(); BigInteger k = ECUtil.generateK(n, random); ECPoint[] gamma_phi = new ECPoint[]{ basePointMultiplier.multiply(ec.getG(), k), key.getQ().multiply(k).add(ECAlgorithms.cleanPoint(ec.getCurve(), cipherText.getY())) }; ec.getCurve().normalizeAll(gamma_phi); return new ECPair(gamma_phi[0], gamma_phi[1]); }