Search in sources :

Example 21 with ECDomainParameters

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

the class BCDSTU4145PublicKey method populateFromPubKeyInfo.

private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
    ASN1BitString bits = info.getPublicKeyData();
    ASN1OctetString key;
    this.algorithm = "DSTU4145";
    try {
        key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
    } catch (IOException ex) {
        throw new IllegalArgumentException("error recovering public key");
    }
    byte[] keyEnc = key.getOctets();
    if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le)) {
        reverseBytes(keyEnc);
    }
    ASN1Sequence seq = ASN1Sequence.getInstance(info.getAlgorithm().getParameters());
    com.github.zhenwei.provider.jce.spec.ECParameterSpec spec = null;
    X9ECParameters x9Params = null;
    if (seq.getObjectAt(0) instanceof ASN1Integer) {
        x9Params = X9ECParameters.getInstance(seq);
        spec = new com.github.zhenwei.provider.jce.spec.ECParameterSpec(x9Params.getCurve(), x9Params.getG(), x9Params.getN(), x9Params.getH(), x9Params.getSeed());
    } else {
        dstuParams = DSTU4145Params.getInstance(seq);
        if (dstuParams.isNamedCurve()) {
            ASN1ObjectIdentifier curveOid = dstuParams.getNamedCurve();
            ECDomainParameters ecP = DSTU4145NamedCurves.getByOID(curveOid);
            spec = new ECNamedCurveParameterSpec(curveOid.getId(), ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
        } else {
            DSTU4145ECBinary binary = dstuParams.getECBinary();
            byte[] b_bytes = binary.getB();
            if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le)) {
                reverseBytes(b_bytes);
            }
            DSTU4145BinaryField field = binary.getField();
            ECCurve curve = new ECCurve.F2m(field.getM(), field.getK1(), field.getK2(), field.getK3(), binary.getA(), new BigInteger(1, b_bytes));
            byte[] g_bytes = binary.getG();
            if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le)) {
                reverseBytes(g_bytes);
            }
            spec = new com.github.zhenwei.provider.jce.spec.ECParameterSpec(curve, DSTU4145PointEncoder.decodePoint(curve, g_bytes), binary.getN());
        }
    }
    ECCurve curve = spec.getCurve();
    EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
    if (dstuParams != null) {
        ECPoint g = EC5Util.convertPoint(spec.getG());
        if (dstuParams.isNamedCurve()) {
            String name = dstuParams.getNamedCurve().getId();
            ecSpec = new ECNamedCurveSpec(name, ellipticCurve, g, spec.getN(), spec.getH());
        } else {
            ecSpec = new ECParameterSpec(ellipticCurve, g, spec.getN(), spec.getH().intValue());
        }
    } else {
        ecSpec = EC5Util.convertToSpec(x9Params);
    }
    // this.q = curve.createPoint(new BigInteger(1, x), new BigInteger(1, y), false);
    this.ecPublicKey = new ECPublicKeyParameters(DSTU4145PointEncoder.decodePoint(curve, keyEnc), EC5Util.getDomainParameters(null, ecSpec));
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec) DSTU4145ECBinary(com.github.zhenwei.core.asn1.ua.DSTU4145ECBinary) IOException(java.io.IOException) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) DSTU4145BinaryField(com.github.zhenwei.core.asn1.ua.DSTU4145BinaryField) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 22 with ECDomainParameters

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

the class KeyPairGeneratorSpi method initialize.

public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (params instanceof ECParameterSpec) {
        ECParameterSpec p = (ECParameterSpec) params;
        this.ecParams = params;
        param = new ECKeyGenerationParameters(new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH()), random);
        engine.init(param);
        initialised = true;
    } else if (params instanceof java.security.spec.ECParameterSpec) {
        java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) params;
        this.ecParams = params;
        ECCurve curve = EC5Util.convertCurve(p.getCurve());
        ECPoint g = EC5Util.convertPoint(curve, p.getGenerator());
        if (p instanceof DSTU4145ParameterSpec) {
            DSTU4145ParameterSpec dstuSpec = (DSTU4145ParameterSpec) p;
            param = new ECKeyGenerationParameters(new DSTU4145Parameters(new ECDomainParameters(curve, g, p.getOrder(), BigInteger.valueOf(p.getCofactor())), dstuSpec.getDKE()), random);
        } else {
            param = new ECKeyGenerationParameters(new ECDomainParameters(curve, g, p.getOrder(), BigInteger.valueOf(p.getCofactor())), random);
        }
        engine.init(param);
        initialised = true;
    } else if (params instanceof ECGenParameterSpec || params instanceof ECNamedCurveGenParameterSpec) {
        String curveName;
        if (params instanceof ECGenParameterSpec) {
            curveName = ((ECGenParameterSpec) params).getName();
        } else {
            curveName = ((ECNamedCurveGenParameterSpec) params).getName();
        }
        // ECDomainParameters ecP = ECGOST3410NamedCurves.getByName(curveName);
        ECDomainParameters ecP = DSTU4145NamedCurves.getByOID(new ASN1ObjectIdentifier(curveName));
        if (ecP == null) {
            throw new InvalidAlgorithmParameterException("unknown curve name: " + curveName);
        }
        this.ecParams = new ECNamedCurveSpec(curveName, ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
        java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;
        ECCurve curve = EC5Util.convertCurve(p.getCurve());
        ECPoint g = EC5Util.convertPoint(curve, p.getGenerator());
        param = new ECKeyGenerationParameters(new ECDomainParameters(curve, g, p.getOrder(), BigInteger.valueOf(p.getCofactor())), random);
        engine.init(param);
        initialised = true;
    } else if (params == null && WeGooProvider.CONFIGURATION.getEcImplicitlyCa() != null) {
        ECParameterSpec p = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
        this.ecParams = params;
        param = new ECKeyGenerationParameters(new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH()), random);
        engine.init(param);
        initialised = true;
    } else if (params == null && WeGooProvider.CONFIGURATION.getEcImplicitlyCa() == null) {
        throw new InvalidAlgorithmParameterException("null parameter passed but no implicitCA set");
    } else {
        throw new InvalidAlgorithmParameterException("parameter object not a ECParameterSpec: " + params.getClass().getName());
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) ECNamedCurveGenParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveGenParameterSpec) DSTU4145ParameterSpec(com.github.zhenwei.provider.jcajce.spec.DSTU4145ParameterSpec) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) DSTU4145Parameters(com.github.zhenwei.core.crypto.params.DSTU4145Parameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ECKeyGenerationParameters(com.github.zhenwei.core.crypto.params.ECKeyGenerationParameters) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 23 with ECDomainParameters

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

the class ECUtil method generatePrivateKeyParameter.

public static AsymmetricKeyParameter generatePrivateKeyParameter(PrivateKey key) throws InvalidKeyException {
    if (key instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) key;
        ECParameterSpec s = k.getParameters();
        if (s == null) {
            s = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
        }
        if (k.getParameters() instanceof ECNamedCurveParameterSpec) {
            String name = ((ECNamedCurveParameterSpec) k.getParameters()).getName();
            return new ECPrivateKeyParameters(k.getD(), new ECNamedDomainParameters(ECNamedCurveTable.getOID(name), s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        } else {
            return new ECPrivateKeyParameters(k.getD(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        }
    } else if (key instanceof java.security.interfaces.ECPrivateKey) {
        java.security.interfaces.ECPrivateKey privKey = (java.security.interfaces.ECPrivateKey) key;
        ECParameterSpec s = EC5Util.convertSpec(privKey.getParams());
        return new ECPrivateKeyParameters(privKey.getS(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    } else {
        // see if we can build a key from key.getEncoded()
        try {
            byte[] bytes = key.getEncoded();
            if (bytes == null) {
                throw new InvalidKeyException("no encoding for EC private key");
            }
            PrivateKey privateKey = WeGooProvider.getPrivateKey(PrivateKeyInfo.getInstance(bytes));
            if (privateKey instanceof java.security.interfaces.ECPrivateKey) {
                return ECUtil.generatePrivateKeyParameter(privateKey);
            }
        } catch (Exception e) {
            throw new InvalidKeyException("cannot identify EC private key: " + e.toString());
        }
    }
    throw new InvalidKeyException("can't identify EC private key.");
}
Also used : ECPrivateKey(com.github.zhenwei.provider.jce.interfaces.ECPrivateKey) ECPrivateKey(com.github.zhenwei.provider.jce.interfaces.ECPrivateKey) PrivateKey(java.security.PrivateKey) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) InvalidKeyException(java.security.InvalidKeyException) InvalidKeyException(java.security.InvalidKeyException) ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec)

Example 24 with ECDomainParameters

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

the class ECUtil method generatePublicKeyParameter.

public static AsymmetricKeyParameter generatePublicKeyParameter(PublicKey key) throws InvalidKeyException {
    if (key instanceof ECPublicKey) {
        ECPublicKey k = (ECPublicKey) key;
        ECParameterSpec s = k.getParameters();
        return new ECPublicKeyParameters(k.getQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    } else if (key instanceof java.security.interfaces.ECPublicKey) {
        java.security.interfaces.ECPublicKey pubKey = (java.security.interfaces.ECPublicKey) key;
        ECParameterSpec s = EC5Util.convertSpec(pubKey.getParams());
        return new ECPublicKeyParameters(EC5Util.convertPoint(pubKey.getParams(), pubKey.getW()), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    } else {
        // see if we can build a key from key.getEncoded()
        try {
            byte[] bytes = key.getEncoded();
            if (bytes == null) {
                throw new InvalidKeyException("no encoding for EC public key");
            }
            PublicKey publicKey = WeGooProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
            if (publicKey instanceof java.security.interfaces.ECPublicKey) {
                return ECUtil.generatePublicKeyParameter(publicKey);
            }
        } catch (Exception e) {
            throw new InvalidKeyException("cannot identify EC public key: " + e.toString());
        }
    }
    throw new InvalidKeyException("cannot identify EC public key.");
}
Also used : ECPublicKey(com.github.zhenwei.provider.jce.interfaces.ECPublicKey) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) PublicKey(java.security.PublicKey) ECPublicKey(com.github.zhenwei.provider.jce.interfaces.ECPublicKey) InvalidKeyException(java.security.InvalidKeyException) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) InvalidKeyException(java.security.InvalidKeyException)

Example 25 with ECDomainParameters

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

the class ECIESKeyEncapsulation method decrypt.

/**
 * Decrypt an encapsulated session key.
 *
 * @param in     the input buffer for the encapsulated key.
 * @param inOff  the offset for the input buffer.
 * @param inLen  the length of the encapsulated key.
 * @param keyLen the length of the session key.
 * @return the session key.
 */
public CipherParameters decrypt(byte[] in, int inOff, int inLen, int keyLen) throws IllegalArgumentException {
    if (!(key instanceof ECPrivateKeyParameters)) {
        throw new IllegalArgumentException("Private key required for encryption");
    }
    ECPrivateKeyParameters ecPrivKey = (ECPrivateKeyParameters) key;
    ECDomainParameters ecParams = ecPrivKey.getParameters();
    ECCurve curve = ecParams.getCurve();
    BigInteger n = ecParams.getN();
    BigInteger h = ecParams.getH();
    // Decode the ephemeral public key
    byte[] C = new byte[inLen];
    System.arraycopy(in, inOff, C, 0, inLen);
    // NOTE: Decoded points are already normalized (i.e in affine form)
    ECPoint gTilde = curve.decodePoint(C);
    // Compute the static-ephemeral key agreement
    ECPoint gHat = gTilde;
    if ((CofactorMode) || (OldCofactorMode)) {
        gHat = gHat.multiply(h);
    }
    BigInteger xHat = ecPrivKey.getD();
    if (CofactorMode) {
        xHat = xHat.multiply(ecParams.getHInv()).mod(n);
    }
    ECPoint hTilde = gHat.multiply(xHat).normalize();
    // Encode the shared secret value
    byte[] PEH = hTilde.getAffineXCoord().getEncoded();
    return deriveKey(keyLen, C, PEH);
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Aggregations

ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)35 BigInteger (java.math.BigInteger)22 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)21 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)12 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)10 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)10 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)9 ECMultiplier (com.github.zhenwei.core.math.ec.ECMultiplier)9 ECParameterSpec (com.github.zhenwei.provider.jce.spec.ECParameterSpec)9 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)7 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)6 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)6 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)5 X962Parameters (com.github.zhenwei.core.asn1.x9.X962Parameters)5 ECKeyGenerationParameters (com.github.zhenwei.core.crypto.params.ECKeyGenerationParameters)4 IOException (java.io.IOException)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)3 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)3 GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)3