Search in sources :

Example 6 with GOST3410PublicKeyAlgParameters

use of com.github.zhenwei.core.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)

Example 7 with GOST3410PublicKeyAlgParameters

use of com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters 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 8 with GOST3410PublicKeyAlgParameters

use of com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project LinLong-Java by zhenwei1108.

the class BCECGOST3410PublicKey method getEncoded.

public byte[] getEncoded() {
    ASN1Encodable params;
    SubjectPublicKeyInfo info;
    params = getGostParams();
    if (params == null) {
        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, new X9ECPoint(EC5Util.convertPoint(curve, ecSpec.getGenerator()), withCompression), ecSpec.getOrder(), BigInteger.valueOf(ecSpec.getCofactor()), ecSpec.getCurve().getSeed());
            params = new X962Parameters(ecP);
        }
    }
    BigInteger bX = this.ecPublicKey.getQ().getAffineXCoord().toBigInteger();
    BigInteger bY = this.ecPublicKey.getQ().getAffineYCoord().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;
    }
    return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
}
Also used : X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) IOException(java.io.IOException) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 9 with GOST3410PublicKeyAlgParameters

use of com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters 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 10 with GOST3410PublicKeyAlgParameters

use of com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters in project LinLong-Java by zhenwei1108.

the class BCGOST3410PrivateKey method getEncoded.

/**
 * Return a PKCS8 representation of the key. The sequence returned represents a full
 * PrivateKeyInfo object.
 *
 * @return a PKCS8 representation of the key.
 */
public byte[] getEncoded() {
    PrivateKeyInfo info;
    byte[] keyEnc = this.getX().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) {
            info = new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()))), new DEROctetString(keyBytes));
        } else {
            info = new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94), new DEROctetString(keyBytes));
        }
        return info.getEncoded(ASN1Encoding.DER);
    } 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) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)12 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)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 IOException (java.io.IOException)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 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)5 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)5 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)4 EllipticCurve (java.security.spec.EllipticCurve)4 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)3 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)3 DSAParameter (com.github.zhenwei.core.asn1.x509.DSAParameter)3 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)3 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)3