Search in sources :

Example 6 with ECNamedCurveParameterSpec

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

the class BCECGOST3410_2012PublicKey method populateFromPubKeyInfo.

private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
    ASN1ObjectIdentifier algOid = info.getAlgorithm().getAlgorithm();
    ASN1BitString bits = info.getPublicKeyData();
    ASN1OctetString key;
    this.algorithm = "ECGOST3410-2012";
    try {
        key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
    } catch (IOException ex) {
        throw new IllegalArgumentException("error recovering public key");
    }
    byte[] keyEnc = key.getOctets();
    int fieldSize = 32;
    if (algOid.equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512)) {
        fieldSize = 64;
    }
    int keySize = 2 * fieldSize;
    byte[] x9Encoding = new byte[1 + keySize];
    x9Encoding[0] = 0x04;
    for (int i = 1; i <= fieldSize; ++i) {
        x9Encoding[i] = keyEnc[fieldSize - i];
        x9Encoding[i + fieldSize] = keyEnc[keySize - i];
    }
    this.gostParams = GOST3410PublicKeyAlgParameters.getInstance(info.getAlgorithm().getParameters());
    ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
    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(gostParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) EllipticCurve(java.security.spec.EllipticCurve) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) IOException(java.io.IOException) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 7 with ECNamedCurveParameterSpec

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

the class BCDSTU4145PrivateKey method populateFromPrivKeyInfo.

private void populateFromPrivKeyInfo(PrivateKeyInfo info) throws IOException {
    X962Parameters params = X962Parameters.getInstance(info.getPrivateKeyAlgorithm().getParameters());
    if (params.isNamedCurve()) {
        ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
        if (// DSTU Curve
        ecP == null) {
            ECDomainParameters gParam = DSTU4145NamedCurves.getByOID(oid);
            EllipticCurve ellipticCurve = EC5Util.convertCurve(gParam.getCurve(), gParam.getSeed());
            ecSpec = new ECNamedCurveSpec(oid.getId(), 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 {
        ASN1Sequence seq = ASN1Sequence.getInstance(params.getParameters());
        if (seq.getObjectAt(0) instanceof ASN1Integer) {
            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());
        } else {
            DSTU4145Params dstuParams = DSTU4145Params.getInstance(seq);
            com.github.zhenwei.provider.jce.spec.ECParameterSpec spec;
            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.getPrivateKeyAlgorithm().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.getPrivateKeyAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le)) {
                    reverseBytes(g_bytes);
                }
                spec = new com.github.zhenwei.provider.jce.spec.ECParameterSpec(curve, DSTU4145PointEncoder.decodePoint(curve, g_bytes), binary.getN());
            }
            EllipticCurve ellipticCurve = EC5Util.convertCurve(spec.getCurve(), spec.getSeed());
            this.ecSpec = new ECParameterSpec(ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.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 : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) DSTU4145ECBinary(com.github.zhenwei.core.asn1.ua.DSTU4145ECBinary) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) DSTU4145Params(com.github.zhenwei.core.asn1.ua.DSTU4145Params) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) 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) 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) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 8 with ECNamedCurveParameterSpec

use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec 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 9 with ECNamedCurveParameterSpec

use of com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec 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 10 with ECNamedCurveParameterSpec

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

the class JCEECPublicKey method populateFromPubKeyInfo.

private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
    AlgorithmIdentifier algID = info.getAlgorithm();
    if (algID.getAlgorithm().equals(CryptoProObjectIdentifiers.gostR3410_2001)) {
        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];
        }
        gostParams = GOST3410PublicKeyAlgParameters.getInstance(algID.getParameters());
        ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
        ECCurve curve = spec.getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
        this.q = curve.decodePoint(x9Encoding);
        ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
    } else {
        X962Parameters params = X962Parameters.getInstance(algID.getParameters());
        ECCurve curve;
        EllipticCurve ellipticCurve;
        if (params.isNamedCurve()) {
            ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) params.getParameters();
            X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
            curve = ecP.getCurve();
            ellipticCurve = EC5Util.convertCurve(curve, ecP.getSeed());
            ecSpec = new ECNamedCurveSpec(ECUtil.getCurveName(oid), ellipticCurve, EC5Util.convertPoint(ecP.getG()), ecP.getN(), ecP.getH());
        } else if (params.isImplicitlyCA()) {
            ecSpec = null;
            curve = WeGooProvider.CONFIGURATION.getEcImplicitlyCa().getCurve();
        } else {
            X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
            curve = ecP.getCurve();
            ellipticCurve = EC5Util.convertCurve(curve, ecP.getSeed());
            this.ecSpec = new ECParameterSpec(ellipticCurve, EC5Util.convertPoint(ecP.getG()), ecP.getN(), ecP.getH().intValue());
        }
        ASN1BitString bits = info.getPublicKeyData();
        byte[] data = bits.getBytes();
        ASN1OctetString key = new DEROctetString(data);
        // 
        if (data[0] == 0x04 && data[1] == data.length - 2 && (data[2] == 0x02 || data[2] == 0x03)) {
            int qLength = new X9IntegerConverter().getByteLength(curve);
            if (qLength >= data.length - 3) {
                try {
                    key = (ASN1OctetString) ASN1Primitive.fromByteArray(data);
                } catch (IOException ex) {
                    throw new IllegalArgumentException("error recovering public key");
                }
            }
        }
        X9ECPoint derQ = new X9ECPoint(curve, key);
        this.q = derQ.getPoint();
    }
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) X9IntegerConverter(com.github.zhenwei.core.asn1.x9.X9IntegerConverter) IOException(java.io.IOException) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) 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)

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