Search in sources :

Example 6 with GOST3410PublicKeyAlgParameters

use of org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters 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 7 with GOST3410PublicKeyAlgParameters

use of org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project LinLong-Java by zhenwei1108.

the class BCGOST3410PublicKey method getEncoded.

public byte[] getEncoded() {
    SubjectPublicKeyInfo info;
    byte[] keyEnc = this.getY().toByteArray();
    byte[] keyBytes;
    if (keyEnc[0] == 0) {
        keyBytes = new byte[keyEnc.length - 1];
    } else {
        keyBytes = new byte[keyEnc.length];
    }
    for (int i = 0; i != keyBytes.length; i++) {
        // must be little endian
        keyBytes[i] = keyEnc[keyEnc.length - 1 - i];
    }
    try {
        if (gost3410Spec instanceof GOST3410ParameterSpec) {
            if (gost3410Spec.getEncryptionParamSetOID() != null) {
                info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getEncryptionParamSetOID()))), new DEROctetString(keyBytes));
            } else {
                info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()))), new DEROctetString(keyBytes));
            }
        } else {
            info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94), new DEROctetString(keyBytes));
        }
        return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
    } catch (IOException e) {
        return null;
    }
}
Also used : GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) IOException(java.io.IOException) GOST3410ParameterSpec(com.github.zhenwei.provider.jce.spec.GOST3410ParameterSpec) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 8 with GOST3410PublicKeyAlgParameters

use of org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project robovm by robovm.

the class JCEECPublicKey method populateFromPubKeyInfo.

private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
    // BEGIN android-removed
    // if (info.getAlgorithmId().getObjectId().equals(CryptoProObjectIdentifiers.gostR3410_2001))
    // {
    //     DERBitString 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[]          x = new byte[32];
    //     byte[]          y = new byte[32];
    //
    //     for (int i = 0; i != x.length; i++)
    //     {
    //         x[i] = keyEnc[32 - 1 - i];
    //     }
    //
    //     for (int i = 0; i != y.length; i++)
    //     {
    //         y[i] = keyEnc[64 - 1 - i];
    //     }
    //
    //     gostParams = new GOST3410PublicKeyAlgParameters((ASN1Sequence)info.getAlgorithmId().getParameters());
    //
    //     ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
    //
    //     ECCurve curve = spec.getCurve();
    //     EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
    //
    //     this.q = curve.createPoint(new BigInteger(1, x), new BigInteger(1, y), false);
    //
    //     ecSpec = new ECNamedCurveSpec(
    //             ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()),
    //             ellipticCurve,
    //             new ECPoint(
    //                     spec.getG().getX().toBigInteger(),
    //                     spec.getG().getY().toBigInteger()),
    //                     spec.getN(), spec.getH());
    //
    // }
    // else
    // END android-removed
    {
        X962Parameters params = new X962Parameters((ASN1Primitive) info.getAlgorithmId().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, new ECPoint(ecP.getG().getX().toBigInteger(), ecP.getG().getY().toBigInteger()), ecP.getN(), ecP.getH());
        } else if (params.isImplicitlyCA()) {
            ecSpec = null;
            curve = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa().getCurve();
        } else {
            X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
            curve = ecP.getCurve();
            ellipticCurve = EC5Util.convertCurve(curve, ecP.getSeed());
            this.ecSpec = new ECParameterSpec(ellipticCurve, new ECPoint(ecP.getG().getX().toBigInteger(), ecP.getG().getY().toBigInteger()), ecP.getN(), ecP.getH().intValue());
        }
        DERBitString 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(org.bouncycastle.asn1.ASN1OctetString) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) X9IntegerConverter(org.bouncycastle.asn1.x9.X9IntegerConverter) DERBitString(org.bouncycastle.asn1.DERBitString) IOException(java.io.IOException) X9ECPoint(org.bouncycastle.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint) DEROctetString(org.bouncycastle.asn1.DEROctetString) X962Parameters(org.bouncycastle.asn1.x9.X962Parameters) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) X9ECPoint(org.bouncycastle.asn1.x9.X9ECPoint) ECCurve(org.bouncycastle.math.ec.ECCurve) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(org.bouncycastle.jce.spec.ECNamedCurveSpec)

Example 9 with GOST3410PublicKeyAlgParameters

use of org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project robovm by robovm.

the class JCEECPublicKey method getEncoded.

public byte[] getEncoded() {
    ASN1Encodable params;
    SubjectPublicKeyInfo info;
    // BEGIN android-removed
    // if (algorithm.equals("ECGOST3410"))
    // {
    //     if (gostParams != null)
    //     {
    //         params = gostParams;
    //     }
    //     else
    //     {
    //         if (ecSpec instanceof ECNamedCurveSpec)
    //         {
    //             params = new GOST3410PublicKeyAlgParameters(
    //                            ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec)ecSpec).getName()),
    //                            CryptoProObjectIdentifiers.gostR3411_94_CryptoProParamSet);
    //         }
    //         else
    //         {   // strictly speaking this may not be applicable...
    //             ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
    //
    //             X9ECParameters ecP = new X9ECParameters(
    //                 curve,
    //                 EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression),
    //                 ecSpec.getOrder(),
    //                 BigInteger.valueOf(ecSpec.getCofactor()),
    //                 ecSpec.getCurve().getSeed());
    //
    //             params = new X962Parameters(ecP);
    //         }
    //     }
    //
    //     BigInteger      bX = this.q.getX().toBigInteger();
    //     BigInteger      bY = this.q.getY().toBigInteger();
    //     byte[]          encKey = new byte[64];
    //
    //     extractBytes(encKey, 0, bX);
    //     extractBytes(encKey, 32, bY);
    //
    //     try
    //     {
    //         info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_2001, params), new DEROctetString(encKey));
    //     }
    //     catch (IOException e)
    //     {
    //         return null;
    //     }
    // }
    // else
    // END android-removed
    {
        if (ecSpec instanceof ECNamedCurveSpec) {
            ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveSpec) ecSpec).getName());
            if (curveOid == null) {
                curveOid = new ASN1ObjectIdentifier(((ECNamedCurveSpec) ecSpec).getName());
            }
            params = new X962Parameters(curveOid);
        } else if (ecSpec == null) {
            params = new X962Parameters(DERNull.INSTANCE);
        } else {
            ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
            X9ECParameters ecP = new X9ECParameters(curve, EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression), ecSpec.getOrder(), BigInteger.valueOf(ecSpec.getCofactor()), ecSpec.getCurve().getSeed());
            params = new X962Parameters(ecP);
        }
        ECCurve curve = this.engineGetQ().getCurve();
        ASN1OctetString p = (ASN1OctetString) new X9ECPoint(curve.createPoint(this.getQ().getX().toBigInteger(), this.getQ().getY().toBigInteger(), withCompression)).toASN1Primitive();
        info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), p.getOctets());
    }
    return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
}
Also used : X962Parameters(org.bouncycastle.asn1.x9.X962Parameters) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) X9ECPoint(org.bouncycastle.asn1.x9.X9ECPoint) ECCurve(org.bouncycastle.math.ec.ECCurve) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(org.bouncycastle.jce.spec.ECNamedCurveSpec) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 10 with GOST3410PublicKeyAlgParameters

use of org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project jmulticard by ctt-gob-es.

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 = BouncyCastleProvider.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(org.bouncycastle.asn1.ASN1Sequence) ECParameterSpec(java.security.spec.ECParameterSpec) EllipticCurve(java.security.spec.EllipticCurve) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) GOST3410PublicKeyAlgParameters(org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters) ECNamedCurveParameterSpec(org.bouncycastle.jce.spec.ECNamedCurveParameterSpec) HashMap(java.util.HashMap) Map(java.util.Map) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(org.bouncycastle.jce.spec.ECNamedCurveSpec)

Aggregations

GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)12 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)10 IOException (java.io.IOException)10 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)9 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)8 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)8 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)7 X962Parameters (com.github.zhenwei.core.asn1.x9.X962Parameters)7 X9ECPoint (com.github.zhenwei.core.asn1.x9.X9ECPoint)7 ECNamedCurveSpec (com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)7 BigInteger (java.math.BigInteger)7 EllipticCurve (java.security.spec.EllipticCurve)6 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)5 ECParameterSpec (java.security.spec.ECParameterSpec)5 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)5 ECNamedCurveSpec (org.bouncycastle.jce.spec.ECNamedCurveSpec)5 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)4 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)4 ECPoint (java.security.spec.ECPoint)4 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)4