Search in sources :

Example 11 with ECPublicKeyParameters

use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class ECDHCStagedAgreement method calculateNextPoint.

private ECPoint calculateNextPoint(ECPublicKeyParameters pubKey) {
    ECPublicKeyParameters pub = 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;
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) BigInteger(java.math.BigInteger) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)

Example 12 with ECPublicKeyParameters

use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class DSTU4145KeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    AsymmetricCipherKeyPair pair = super.generateKeyPair();
    ECPublicKeyParameters pub = (ECPublicKeyParameters) pair.getPublic();
    ECPrivateKeyParameters priv = (ECPrivateKeyParameters) pair.getPrivate();
    pub = new ECPublicKeyParameters(pub.getQ().negate(), pub.getParameters());
    return new AsymmetricCipherKeyPair(pub, priv);
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 13 with ECPublicKeyParameters

use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class ECKeyPairGenerator method generateKeyPair.

/**
 * Given the domain parameters this routine generates an EC key pair in accordance with X9.62
 * section 5.2.1 pages 26, 27.
 */
public AsymmetricCipherKeyPair generateKeyPair() {
    BigInteger n = params.getN();
    int nBitLength = n.bitLength();
    int minWeight = nBitLength >>> 2;
    BigInteger d;
    for (; ; ) {
        d = BigIntegers.createRandomBigInteger(nBitLength, random);
        if (d.compareTo(ONE) < 0 || (d.compareTo(n) >= 0)) {
            continue;
        }
        if (WNafUtil.getNafWeight(d) < minWeight) {
            continue;
        }
        break;
    }
    ECPoint Q = createBasePointMultiplier().multiply(params.getG(), d);
    return new AsymmetricCipherKeyPair(new ECPublicKeyParameters(Q, params), new ECPrivateKeyParameters(d, params));
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) BigInteger(java.math.BigInteger) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 14 with ECPublicKeyParameters

use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class ECIESKeyEncapsulation method encrypt.

/**
 * Generate and encapsulate a random session key.
 *
 * @param out    the output buffer for the encapsulated key.
 * @param outOff the offset for the output buffer.
 * @param keyLen the length of the session key.
 * @return the random session key.
 */
public CipherParameters encrypt(byte[] out, int outOff, int keyLen) throws IllegalArgumentException {
    if (!(key instanceof ECPublicKeyParameters)) {
        throw new IllegalArgumentException("Public key required for encryption");
    }
    ECPublicKeyParameters ecPubKey = (ECPublicKeyParameters) key;
    ECDomainParameters ecParams = ecPubKey.getParameters();
    ECCurve curve = ecParams.getCurve();
    BigInteger n = ecParams.getN();
    BigInteger h = ecParams.getH();
    // Generate the ephemeral key pair
    BigInteger r = BigIntegers.createRandomInRange(ONE, n, rnd);
    // Compute the static-ephemeral key agreement
    BigInteger rPrime = OldCofactorMode ? r.multiply(h).mod(n) : r;
    ECMultiplier basePointMultiplier = createBasePointMultiplier();
    ECPoint[] ghTilde = new ECPoint[] { basePointMultiplier.multiply(ecParams.getG(), r), ecPubKey.getQ().multiply(rPrime) };
    // NOTE: More efficient than normalizing each individually
    curve.normalizeAll(ghTilde);
    ECPoint gTilde = ghTilde[0], hTilde = ghTilde[1];
    // Encode the ephemeral public key
    byte[] C = gTilde.getEncoded(false);
    System.arraycopy(C, 0, out, outOff, C.length);
    // Encode the shared secret value
    byte[] PEH = hTilde.getAffineXCoord().getEncoded();
    return deriveKey(keyLen, C, PEH);
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ECMultiplier(com.github.zhenwei.core.math.ec.ECMultiplier) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)

Example 15 with ECPublicKeyParameters

use of com.github.zhenwei.core.crypto.params.ECPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        throw new IllegalStateException("EC Key Pair Generator not initialised");
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    ECPublicKeyParameters pub = (ECPublicKeyParameters) pair.getPublic();
    ECPrivateKeyParameters priv = (ECPrivateKeyParameters) pair.getPrivate();
    if (ecParams instanceof ECParameterSpec) {
        ECParameterSpec p = (ECParameterSpec) ecParams;
        BCECGOST3410PublicKey pubKey = new BCECGOST3410PublicKey(algorithm, pub, p);
        return new KeyPair(pubKey, new BCECGOST3410PrivateKey(algorithm, priv, pubKey, p));
    } else if (ecParams == null) {
        return new KeyPair(new BCECGOST3410PublicKey(algorithm, pub), new BCECGOST3410PrivateKey(algorithm, priv));
    } else {
        java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;
        BCECGOST3410PublicKey pubKey = new BCECGOST3410PublicKey(algorithm, pub, p);
        return new KeyPair(pubKey, new BCECGOST3410PrivateKey(algorithm, priv, pubKey, p));
    }
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Aggregations

ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)34 BigInteger (java.math.BigInteger)16 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)14 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)12 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)10 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)7 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)6 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)6 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)5 X9ECPoint (com.github.zhenwei.core.asn1.x9.X9ECPoint)5 IOException (java.io.IOException)5 ASN1BitString (com.github.zhenwei.core.asn1.ASN1BitString)4 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)4 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)4 ECParameterSpec (com.github.zhenwei.provider.jce.spec.ECParameterSpec)4 InvalidKeyException (java.security.InvalidKeyException)4 ECPoint (java.security.spec.ECPoint)4 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)3 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)3 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)3