Search in sources :

Example 1 with ECNamedCurveParameterSpec

use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec in project LinLong-Java by zhenwei1108.

the class EC5Util method convertToSpec.

public static ECParameterSpec convertToSpec(X962Parameters params, ECCurve curve) {
    ECParameterSpec ecSpec;
    EllipticCurve ellipticCurve;
    if (params.isNamedCurve()) {
        ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) params.getParameters();
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
        if (ecP == null) {
            Map additionalECParameters = WeGooProvider.CONFIGURATION.getAdditionalECParameters();
            if (!additionalECParameters.isEmpty()) {
                ecP = (X9ECParameters) additionalECParameters.get(oid);
            }
        }
        ellipticCurve = EC5Util.convertCurve(curve, ecP.getSeed());
        ecSpec = new ECNamedCurveSpec(ECUtil.getCurveName(oid), ellipticCurve, convertPoint(ecP.getG()), ecP.getN(), ecP.getH());
    } else if (params.isImplicitlyCA()) {
        ecSpec = null;
    } else {
        ASN1Sequence pSeq = ASN1Sequence.getInstance(params.getParameters());
        if (pSeq.size() > 3) {
            X9ECParameters ecP = X9ECParameters.getInstance(pSeq);
            ellipticCurve = EC5Util.convertCurve(curve, ecP.getSeed());
            if (ecP.getH() != null) {
                ecSpec = new ECParameterSpec(ellipticCurve, convertPoint(ecP.getG()), ecP.getN(), ecP.getH().intValue());
            } else {
                ecSpec = new ECParameterSpec(ellipticCurve, convertPoint(ecP.getG()), ecP.getN(), // TODO: not strictly correct... need to fix the test data...
                1);
            }
        } else // GOST parameters
        {
            GOST3410PublicKeyAlgParameters gostParams = GOST3410PublicKeyAlgParameters.getInstance(pSeq);
            ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
            curve = spec.getCurve();
            ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
            ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
        }
    }
    return ecSpec;
}
Also used : ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) ECParameterSpec(java.security.spec.ECParameterSpec) EllipticCurve(java.security.spec.EllipticCurve) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec) HashMap(java.util.HashMap) Map(java.util.Map) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 2 with ECNamedCurveParameterSpec

use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec in project LinLong-Java by zhenwei1108.

the class ECUtil method getDomainParameters.

public static ECDomainParameters getDomainParameters(ProviderConfiguration configuration, com.github.zhenwei.provider.jce.spec.ECParameterSpec params) {
    ECDomainParameters domainParameters;
    if (params instanceof ECNamedCurveParameterSpec) {
        ECNamedCurveParameterSpec nParams = (ECNamedCurveParameterSpec) params;
        ASN1ObjectIdentifier nameOid = ECUtil.getNamedCurveOid(nParams.getName());
        domainParameters = new ECNamedDomainParameters(nameOid, nParams.getCurve(), nParams.getG(), nParams.getN(), nParams.getH(), nParams.getSeed());
    } else if (params == null) {
        com.github.zhenwei.provider.jce.spec.ECParameterSpec iSpec = configuration.getEcImplicitlyCa();
        domainParameters = new ECDomainParameters(iSpec.getCurve(), iSpec.getG(), iSpec.getN(), iSpec.getH(), iSpec.getSeed());
    } else {
        domainParameters = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH(), params.getSeed());
    }
    return domainParameters;
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 3 with ECNamedCurveParameterSpec

use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec in project LinLong-Java by zhenwei1108.

the class BCECGOST3410PrivateKey method populateFromPrivKeyInfo.

private void populateFromPrivKeyInfo(PrivateKeyInfo info) throws IOException {
    AlgorithmIdentifier pkAlg = info.getPrivateKeyAlgorithm();
    ASN1Encodable pkParams = pkAlg.getParameters();
    ASN1Primitive p = pkParams.toASN1Primitive();
    if (p instanceof ASN1Sequence && (ASN1Sequence.getInstance(p).size() == 2 || ASN1Sequence.getInstance(p).size() == 3)) {
        GOST3410PublicKeyAlgParameters gParams = GOST3410PublicKeyAlgParameters.getInstance(pkParams);
        gostParams = gParams;
        ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gParams.getPublicKeyParamSet()));
        ECCurve curve = spec.getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
        ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(gParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
        ASN1Encodable privKey = info.parsePrivateKey();
        if (privKey instanceof ASN1Integer) {
            this.d = ASN1Integer.getInstance(privKey).getPositiveValue();
        } else {
            byte[] encVal = ASN1OctetString.getInstance(privKey).getOctets();
            byte[] dVal = new byte[encVal.length];
            for (int i = 0; i != encVal.length; i++) {
                dVal[i] = encVal[encVal.length - 1 - i];
            }
            this.d = new BigInteger(1, dVal);
        }
    } else {
        // for backwards compatibility
        X962Parameters params = X962Parameters.getInstance(pkParams);
        if (params.isNamedCurve()) {
            ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
            X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
            if (ecP == null) {
                throw new IllegalStateException();
            }
            String curveName = ECUtil.getCurveName(oid);
            EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
            ecSpec = new ECNamedCurveSpec(curveName, ellipticCurve, EC5Util.convertPoint(ecP.getG()), ecP.getN(), ecP.getH());
        } else if (params.isImplicitlyCA()) {
            ecSpec = null;
        } else {
            X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
            EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
            this.ecSpec = new ECParameterSpec(ellipticCurve, EC5Util.convertPoint(ecP.getG()), ecP.getN(), ecP.getH().intValue());
        }
        ASN1Encodable privKey = info.parsePrivateKey();
        if (privKey instanceof ASN1Integer) {
            ASN1Integer derD = ASN1Integer.getInstance(privKey);
            this.d = derD.getValue();
        } else {
            com.github.zhenwei.core.asn1.sec.ECPrivateKey ec = com.github.zhenwei.core.asn1.sec.ECPrivateKey.getInstance(privKey);
            this.d = ec.getKey();
            this.publicKey = ec.getPublicKey();
        }
    }
}
Also used : X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) ASN1Primitive(com.github.zhenwei.core.asn1.ASN1Primitive) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 4 with ECNamedCurveParameterSpec

use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec in project LinLong-Java by zhenwei1108.

the class BCECGOST3410PublicKey method populateFromPubKeyInfo.

private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
    ASN1BitString bits = info.getPublicKeyData();
    ASN1OctetString key;
    this.algorithm = "ECGOST3410";
    try {
        key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
    } catch (IOException ex) {
        throw new IllegalArgumentException("error recovering public key");
    }
    byte[] keyEnc = key.getOctets();
    byte[] x9Encoding = new byte[65];
    x9Encoding[0] = 0x04;
    for (int i = 1; i <= 32; ++i) {
        x9Encoding[i] = keyEnc[32 - i];
        x9Encoding[i + 32] = keyEnc[64 - i];
    }
    ASN1ObjectIdentifier paramOID;
    if (info.getAlgorithm().getParameters() instanceof ASN1ObjectIdentifier) {
        paramOID = ASN1ObjectIdentifier.getInstance(info.getAlgorithm().getParameters());
        gostParams = paramOID;
    } else {
        GOST3410PublicKeyAlgParameters params = GOST3410PublicKeyAlgParameters.getInstance(info.getAlgorithm().getParameters());
        gostParams = params;
        paramOID = params.getPublicKeyParamSet();
    }
    ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(paramOID));
    ECCurve curve = spec.getCurve();
    EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
    this.ecPublicKey = new ECPublicKeyParameters(curve.decodePoint(x9Encoding), ECUtil.getDomainParameters(null, spec));
    this.ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(paramOID), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) IOException(java.io.IOException) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint) EllipticCurve(java.security.spec.EllipticCurve) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 5 with ECNamedCurveParameterSpec

use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec in project LinLong-Java by zhenwei1108.

the class BCECGOST3410_2012PrivateKey method populateFromPrivKeyInfo.

private void populateFromPrivKeyInfo(PrivateKeyInfo info) throws IOException {
    ASN1Primitive p = info.getPrivateKeyAlgorithm().getParameters().toASN1Primitive();
    if (p instanceof ASN1Sequence && (ASN1Sequence.getInstance(p).size() == 2 || ASN1Sequence.getInstance(p).size() == 3)) {
        gostParams = GOST3410PublicKeyAlgParameters.getInstance(info.getPrivateKeyAlgorithm().getParameters());
        ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
        ECCurve curve = spec.getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
        ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
        ASN1OctetString privEnc = info.getPrivateKey();
        if (privEnc.getOctets().length == 32 || privEnc.getOctets().length == 64) {
            this.d = new BigInteger(1, Arrays.reverse(privEnc.getOctets()));
        } else {
            ASN1Encodable privKey = info.parsePrivateKey();
            if (privKey instanceof ASN1Integer) {
                this.d = ASN1Integer.getInstance(privKey).getPositiveValue();
            } else {
                byte[] encVal = ASN1OctetString.getInstance(privKey).getOctets();
                this.d = new BigInteger(1, Arrays.reverse(encVal));
            }
        }
    } else {
        // for backwards compatibility
        X962Parameters params = X962Parameters.getInstance(info.getPrivateKeyAlgorithm().getParameters());
        if (params.isNamedCurve()) {
            ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
            X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
            if (// GOST Curve
            ecP == null) {
                X9ECParameters gParam = ECGOST3410NamedCurves.getByOIDX9(oid);
                EllipticCurve ellipticCurve = EC5Util.convertCurve(gParam.getCurve(), gParam.getSeed());
                ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(oid), ellipticCurve, EC5Util.convertPoint(gParam.getG()), gParam.getN(), gParam.getH());
            } else {
                EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
                ecSpec = new ECNamedCurveSpec(ECUtil.getCurveName(oid), ellipticCurve, EC5Util.convertPoint(ecP.getG()), ecP.getN(), ecP.getH());
            }
        } else if (params.isImplicitlyCA()) {
            ecSpec = null;
        } else {
            X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
            EllipticCurve ellipticCurve = EC5Util.convertCurve(ecP.getCurve(), ecP.getSeed());
            this.ecSpec = new ECParameterSpec(ellipticCurve, EC5Util.convertPoint(ecP.getG()), ecP.getN(), ecP.getH().intValue());
        }
        ASN1Encodable privKey = info.parsePrivateKey();
        if (privKey instanceof ASN1Integer) {
            ASN1Integer derD = ASN1Integer.getInstance(privKey);
            this.d = derD.getValue();
        } else {
            com.github.zhenwei.core.asn1.sec.ECPrivateKey ec = com.github.zhenwei.core.asn1.sec.ECPrivateKey.getInstance(privKey);
            this.d = ec.getKey();
            this.publicKey = ec.getPublicKey();
        }
    }
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) ASN1Primitive(com.github.zhenwei.core.asn1.ASN1Primitive) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Aggregations

ECNamedCurveParameterSpec (com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec)10 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)9 ECNamedCurveSpec (com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)8 EllipticCurve (java.security.spec.EllipticCurve)8 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)7 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)6 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)6 ECParameterSpec (java.security.spec.ECParameterSpec)6 ASN1BitString (com.github.zhenwei.core.asn1.ASN1BitString)5 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)5 X9ECPoint (com.github.zhenwei.core.asn1.x9.X9ECPoint)5 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)4 X962Parameters (com.github.zhenwei.core.asn1.x9.X962Parameters)4 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)4 IOException (java.io.IOException)4 BigInteger (java.math.BigInteger)4 ECPoint (java.security.spec.ECPoint)4 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)3 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)3 GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)3