Search in sources :

Example 11 with ECDomainParameters

use of com.github.zhenwei.core.crypto.params.ECDomainParameters 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 12 with ECDomainParameters

use of com.github.zhenwei.core.crypto.params.ECDomainParameters in project LinLong-Java by zhenwei1108.

the class IESCipher method engineDoFinal.

// Finalisation methods
public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
    if (inputLen != 0) {
        buffer.write(input, inputOffset, inputLen);
    }
    final byte[] in = buffer.toByteArray();
    buffer.reset();
    // Convert parameters for use in IESEngine
    CipherParameters params = new IESWithCipherParameters(engineSpec.getDerivationV(), engineSpec.getEncodingV(), engineSpec.getMacKeySize(), engineSpec.getCipherKeySize());
    if (engineSpec.getNonce() != null) {
        params = new ParametersWithIV(params, engineSpec.getNonce());
    }
    final ECDomainParameters ecParams = ((ECKeyParameters) key).getParameters();
    final byte[] V;
    if (otherKeyParameter != null) {
        try {
            if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
                engine.init(true, otherKeyParameter, key, params);
            } else {
                engine.init(false, key, otherKeyParameter, params);
            }
            return engine.processBlock(in, 0, in.length);
        } catch (Exception e) {
            throw new BadBlockException("unable to process block", e);
        }
    }
    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
        // Generate the ephemeral key pair
        ECKeyPairGenerator gen = new ECKeyPairGenerator();
        gen.init(new ECKeyGenerationParameters(ecParams, random));
        final boolean usePointCompression = engineSpec.getPointCompression();
        EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(gen, new KeyEncoder() {

            public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
                return ((ECPublicKeyParameters) keyParameter).getQ().getEncoded(usePointCompression);
            }
        });
        // Encrypt the buffer
        try {
            engine.init(key, params, kGen);
            return engine.processBlock(in, 0, in.length);
        } catch (final Exception e) {
            throw new BadBlockException("unable to process block", e);
        }
    } else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE) {
        // Decrypt the buffer
        try {
            engine.init(key, params, new ECIESPublicKeyParser(ecParams));
            return engine.processBlock(in, 0, in.length);
        } catch (InvalidCipherTextException e) {
            throw new BadBlockException("unable to process block", e);
        }
    } else {
        throw new IllegalStateException("cipher not initialised");
    }
}
Also used : ECKeyPairGenerator(com.github.zhenwei.core.crypto.generators.ECKeyPairGenerator) EphemeralKeyPairGenerator(com.github.zhenwei.core.crypto.generators.EphemeralKeyPairGenerator) ECKeyParameters(com.github.zhenwei.core.crypto.params.ECKeyParameters) BadBlockException(com.github.zhenwei.provider.jcajce.provider.util.BadBlockException) KeyEncoder(com.github.zhenwei.core.crypto.KeyEncoder) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) BadBlockException(com.github.zhenwei.provider.jcajce.provider.util.BadBlockException) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) BadPaddingException(javax.crypto.BadPaddingException) IESWithCipherParameters(com.github.zhenwei.core.crypto.params.IESWithCipherParameters) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) ECIESPublicKeyParser(com.github.zhenwei.core.crypto.parsers.ECIESPublicKeyParser) IESWithCipherParameters(com.github.zhenwei.core.crypto.params.IESWithCipherParameters) ECKeyGenerationParameters(com.github.zhenwei.core.crypto.params.ECKeyGenerationParameters)

Example 13 with ECDomainParameters

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

Example 14 with ECDomainParameters

use of com.github.zhenwei.core.crypto.params.ECDomainParameters in project LinLong-Java by zhenwei1108.

the class EC5Util method getDomainParameters.

public static ECDomainParameters getDomainParameters(ProviderConfiguration configuration, ECParameterSpec params) {
    ECDomainParameters domainParameters;
    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 = ECUtil.getDomainParameters(configuration, convertSpec(params));
    }
    return domainParameters;
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters)

Example 15 with ECDomainParameters

use of com.github.zhenwei.core.crypto.params.ECDomainParameters in project LinLong-Java by zhenwei1108.

the class ECUtil method getDomainParameters.

public static ECDomainParameters getDomainParameters(ProviderConfiguration configuration, X962Parameters params) {
    ECDomainParameters domainParameters;
    if (params.isNamedCurve()) {
        ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
        X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
        if (ecP == null) {
            Map extraCurves = configuration.getAdditionalECParameters();
            ecP = (X9ECParameters) extraCurves.get(oid);
        }
        domainParameters = new ECNamedDomainParameters(oid, ecP);
    } else if (params.isImplicitlyCA()) {
        com.github.zhenwei.provider.jce.spec.ECParameterSpec iSpec = configuration.getEcImplicitlyCa();
        domainParameters = new ECDomainParameters(iSpec.getCurve(), iSpec.getG(), iSpec.getN(), iSpec.getH(), iSpec.getSeed());
    } else {
        X9ECParameters ecP = X9ECParameters.getInstance(params.getParameters());
        domainParameters = new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
    }
    return domainParameters;
}
Also used : ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) Map(java.util.Map) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Aggregations

ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)35 BigInteger (java.math.BigInteger)22 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)21 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)12 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)10 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)10 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)9 ECMultiplier (com.github.zhenwei.core.math.ec.ECMultiplier)9 ECParameterSpec (com.github.zhenwei.provider.jce.spec.ECParameterSpec)9 X9ECParameters (com.github.zhenwei.core.asn1.x9.X9ECParameters)7 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)6 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)6 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)5 X962Parameters (com.github.zhenwei.core.asn1.x9.X962Parameters)5 ECKeyGenerationParameters (com.github.zhenwei.core.crypto.params.ECKeyGenerationParameters)4 IOException (java.io.IOException)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)3 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)3 GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)3