Search in sources :

Example 1 with ECParameterSpec

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

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        throw new IllegalStateException("DSTU Key Pair Generator not initialised");
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    ECPublicKeyParameters pub = (ECPublicKeyParameters) pair.getPublic();
    ECPrivateKeyParameters priv = (ECPrivateKeyParameters) pair.getPrivate();
    if (ecParams instanceof ECParameterSpec) {
        ECParameterSpec p = (ECParameterSpec) ecParams;
        BCDSTU4145PublicKey pubKey = new BCDSTU4145PublicKey(algorithm, pub, p);
        return new KeyPair(pubKey, new BCDSTU4145PrivateKey(algorithm, priv, pubKey, p));
    } else if (ecParams == null) {
        return new KeyPair(new BCDSTU4145PublicKey(algorithm, pub), new BCDSTU4145PrivateKey(algorithm, priv));
    } else {
        java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;
        BCDSTU4145PublicKey pubKey = new BCDSTU4145PublicKey(algorithm, pub, p);
        return new KeyPair(pubKey, new BCDSTU4145PrivateKey(algorithm, priv, pubKey, p));
    }
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 2 with ECParameterSpec

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

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        throw new IllegalStateException("EC Key Pair Generator not initialised");
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    ECPublicKeyParameters pub = (ECPublicKeyParameters) pair.getPublic();
    ECPrivateKeyParameters priv = (ECPrivateKeyParameters) pair.getPrivate();
    if (ecParams instanceof ECParameterSpec) {
        ECParameterSpec p = (ECParameterSpec) ecParams;
        BCECGOST3410PublicKey pubKey = new BCECGOST3410PublicKey(algorithm, pub, p);
        return new KeyPair(pubKey, new BCECGOST3410PrivateKey(algorithm, priv, pubKey, p));
    } else if (ecParams == null) {
        return new KeyPair(new BCECGOST3410PublicKey(algorithm, pub), new BCECGOST3410PrivateKey(algorithm, priv));
    } else {
        java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;
        BCECGOST3410PublicKey pubKey = new BCECGOST3410PublicKey(algorithm, pub, p);
        return new KeyPair(pubKey, new BCECGOST3410PrivateKey(algorithm, priv, pubKey, p));
    }
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 3 with ECParameterSpec

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

the class KeyPairGeneratorSpi method initialize.

public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (params instanceof GOST3410ParameterSpec) {
        GOST3410ParameterSpec gostParams = (GOST3410ParameterSpec) params;
        init(gostParams, random);
    } else 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());
        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();
        }
        init(new GOST3410ParameterSpec(curveName), random);
    } 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) ECPoint(com.github.zhenwei.core.math.ec.ECPoint) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) GOST3410ParameterSpec(com.github.zhenwei.provider.jcajce.spec.GOST3410ParameterSpec) ECKeyGenerationParameters(com.github.zhenwei.core.crypto.params.ECKeyGenerationParameters)

Example 4 with ECParameterSpec

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

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        throw new IllegalStateException("EC Key Pair Generator not initialised");
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    ECPublicKeyParameters pub = (ECPublicKeyParameters) pair.getPublic();
    ECPrivateKeyParameters priv = (ECPrivateKeyParameters) pair.getPrivate();
    if (ecParams instanceof ECParameterSpec) {
        ECParameterSpec p = (ECParameterSpec) ecParams;
        BCECGOST3410_2012PublicKey pubKey = new BCECGOST3410_2012PublicKey(algorithm, pub, p);
        return new KeyPair(pubKey, new BCECGOST3410_2012PrivateKey(algorithm, priv, pubKey, p));
    } else if (ecParams == null) {
        return new KeyPair(new BCECGOST3410_2012PublicKey(algorithm, pub), new BCECGOST3410_2012PrivateKey(algorithm, priv));
    } else {
        java.security.spec.ECParameterSpec p = (java.security.spec.ECParameterSpec) ecParams;
        BCECGOST3410_2012PublicKey pubKey = new BCECGOST3410_2012PublicKey(algorithm, pub, p);
        return new KeyPair(pubKey, new BCECGOST3410_2012PrivateKey(algorithm, priv, pubKey, p));
    }
}
Also used : ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 5 with ECParameterSpec

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

the class KeyFactorySpi method engineGetKeySpec.

protected KeySpec engineGetKeySpec(Key key, Class spec) throws InvalidKeySpecException {
    if ((spec.isAssignableFrom(KeySpec.class) || spec.isAssignableFrom(java.security.spec.ECPublicKeySpec.class)) && key instanceof ECPublicKey) {
        ECPublicKey k = (ECPublicKey) key;
        if (k.getParams() != null) {
            return new java.security.spec.ECPublicKeySpec(k.getW(), k.getParams());
        } else {
            ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
            return new java.security.spec.ECPublicKeySpec(k.getW(), EC5Util.convertSpec(EC5Util.convertCurve(implicitSpec.getCurve(), implicitSpec.getSeed()), implicitSpec));
        }
    } else if ((spec.isAssignableFrom(KeySpec.class) || spec.isAssignableFrom(java.security.spec.ECPrivateKeySpec.class)) && key instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) key;
        if (k.getParams() != null) {
            return new java.security.spec.ECPrivateKeySpec(k.getS(), k.getParams());
        } else {
            ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
            return new java.security.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(EC5Util.convertCurve(implicitSpec.getCurve(), implicitSpec.getSeed()), implicitSpec));
        }
    } else if (spec.isAssignableFrom(com.github.zhenwei.provider.jce.spec.ECPublicKeySpec.class) && key instanceof ECPublicKey) {
        ECPublicKey k = (ECPublicKey) key;
        if (k.getParams() != null) {
            return new com.github.zhenwei.provider.jce.spec.ECPublicKeySpec(EC5Util.convertPoint(k.getParams(), k.getW()), EC5Util.convertSpec(k.getParams()));
        } else {
            ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
            return new com.github.zhenwei.provider.jce.spec.ECPublicKeySpec(EC5Util.convertPoint(k.getParams(), k.getW()), implicitSpec);
        }
    } else if (spec.isAssignableFrom(com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec.class) && key instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) key;
        if (k.getParams() != null) {
            return new com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(k.getParams()));
        } else {
            ECParameterSpec implicitSpec = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
            return new com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec(k.getS(), implicitSpec);
        }
    } else if (spec.isAssignableFrom(OpenSSHPublicKeySpec.class) && key instanceof ECPublicKey) {
        if (key instanceof BCECPublicKey) {
            BCECPublicKey bcPk = (BCECPublicKey) key;
            ECParameterSpec sc = bcPk.getParameters();
            try {
                return new OpenSSHPublicKeySpec(OpenSSHPublicKeyUtil.encodePublicKey(new ECPublicKeyParameters(bcPk.getQ(), new ECDomainParameters(sc.getCurve(), sc.getG(), sc.getN(), sc.getH(), sc.getSeed()))));
            } catch (IOException e) {
                throw new IllegalArgumentException("unable to produce encoding: " + e.getMessage());
            }
        } else {
            throw new IllegalArgumentException("invalid key type: " + key.getClass().getName());
        }
    } else if (spec.isAssignableFrom(OpenSSHPrivateKeySpec.class) && key instanceof ECPrivateKey) {
        if (key instanceof BCECPrivateKey) {
            try {
                return new OpenSSHPrivateKeySpec(PrivateKeyInfo.getInstance(key.getEncoded()).parsePrivateKey().toASN1Primitive().getEncoded());
            } catch (IOException e) {
                throw new IllegalArgumentException("cannot encoded key: " + e.getMessage());
            }
        } else {
            throw new IllegalArgumentException("invalid key type: " + key.getClass().getName());
        }
    }
    return super.engineGetKeySpec(key, spec);
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) OpenSSHPrivateKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPrivateKeySpec) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) OpenSSHPublicKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec) OpenSSHPrivateKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPrivateKeySpec) KeySpec(java.security.spec.KeySpec) ECPrivateKeySpec(com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec) ECPublicKeySpec(com.github.zhenwei.provider.jce.spec.ECPublicKeySpec) IOException(java.io.IOException) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ECPublicKeySpec(com.github.zhenwei.provider.jce.spec.ECPublicKeySpec) ECPublicKey(java.security.interfaces.ECPublicKey) ECPrivateKeySpec(com.github.zhenwei.provider.jce.spec.ECPrivateKeySpec) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) OpenSSHPublicKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec)

Aggregations

ECParameterSpec (com.github.zhenwei.provider.jce.spec.ECParameterSpec)10 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)7 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)5 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)5 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)3 ECKeyGenerationParameters (com.github.zhenwei.core.crypto.params.ECKeyGenerationParameters)3 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)3 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)3 ECNamedCurveGenParameterSpec (com.github.zhenwei.provider.jce.spec.ECNamedCurveGenParameterSpec)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 KeyPair (java.security.KeyPair)3 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)3 GOST3410ParameterSpec (com.github.zhenwei.provider.jcajce.spec.GOST3410ParameterSpec)2 InvalidKeyException (java.security.InvalidKeyException)2 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 X500Name (com.github.zhenwei.core.asn1.x500.X500Name)1 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)1 DSTU4145Parameters (com.github.zhenwei.core.crypto.params.DSTU4145Parameters)1 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)1 RSAKeyParameters (com.github.zhenwei.core.crypto.params.RSAKeyParameters)1