Search in sources :

Example 1 with ECKeyParameters

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

the class ECGOST2012SignatureSpi256 method engineInitVerify.

protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    ECKeyParameters param;
    if (publicKey instanceof ECPublicKey) {
        param = (ECKeyParameters) generatePublicKeyParameter(publicKey);
    } else {
        try {
            byte[] bytes = publicKey.getEncoded();
            publicKey = WeGooProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
            param = (ECKeyParameters) ECUtil.generatePublicKeyParameter(publicKey);
        } catch (Exception e) {
            throw new InvalidKeyException("cannot recognise key type in ECGOST-2012-256 signer");
        }
    }
    if (param.getParameters().getN().bitLength() > 256) {
        throw new InvalidKeyException("key out of range for ECGOST-2012-256");
    }
    digest.reset();
    signer.init(false, param);
}
Also used : ECKeyParameters(com.github.zhenwei.core.crypto.params.ECKeyParameters) ECPublicKey(com.github.zhenwei.provider.jce.interfaces.ECPublicKey) InvalidKeyException(java.security.InvalidKeyException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with ECKeyParameters

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

the class ECGOST2012SignatureSpi512 method engineInitSign.

protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    ECKeyParameters param;
    if (privateKey instanceof ECKey) {
        param = (ECKeyParameters) ECUtil.generatePrivateKeyParameter(privateKey);
    } else {
        throw new InvalidKeyException("cannot recognise key type in ECGOST-2012-512 signer");
    }
    if (param.getParameters().getN().bitLength() < 505) {
        throw new InvalidKeyException("key too weak for ECGOST-2012-512");
    }
    digest.reset();
    if (appRandom != null) {
        signer.init(true, new ParametersWithRandom(param, appRandom));
    } else {
        signer.init(true, param);
    }
}
Also used : ECKeyParameters(com.github.zhenwei.core.crypto.params.ECKeyParameters) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) ECKey(com.github.zhenwei.provider.jce.interfaces.ECKey) InvalidKeyException(java.security.InvalidKeyException)

Example 3 with ECKeyParameters

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

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

the class ECGOST2012SignatureSpi256 method engineInitSign.

protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    ECKeyParameters param;
    if (privateKey instanceof ECKey) {
        param = (ECKeyParameters) ECUtil.generatePrivateKeyParameter(privateKey);
    } else {
        throw new InvalidKeyException("cannot recognise key type in ECGOST-2012-256 signer");
    }
    if (param.getParameters().getN().bitLength() > 256) {
        throw new InvalidKeyException("key out of range for ECGOST-2012-256");
    }
    digest.reset();
    if (appRandom != null) {
        signer.init(true, new ParametersWithRandom(param, appRandom));
    } else {
        signer.init(true, param);
    }
}
Also used : ECKeyParameters(com.github.zhenwei.core.crypto.params.ECKeyParameters) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) ECKey(com.github.zhenwei.provider.jce.interfaces.ECKey) InvalidKeyException(java.security.InvalidKeyException)

Example 5 with ECKeyParameters

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

the class ECGOST2012SignatureSpi512 method engineInitVerify.

protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    ECKeyParameters param;
    if (publicKey instanceof ECPublicKey) {
        param = (ECKeyParameters) generatePublicKeyParameter(publicKey);
    } else {
        try {
            byte[] bytes = publicKey.getEncoded();
            publicKey = WeGooProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
            param = (ECKeyParameters) ECUtil.generatePublicKeyParameter(publicKey);
        } catch (Exception e) {
            throw new InvalidKeyException("cannot recognise key type in ECGOST-2012-512 signer");
        }
    }
    if (param.getParameters().getN().bitLength() < 505) {
        throw new InvalidKeyException("key too weak for ECGOST-2012-512");
    }
    digest.reset();
    signer.init(false, param);
}
Also used : ECKeyParameters(com.github.zhenwei.core.crypto.params.ECKeyParameters) ECPublicKey(com.github.zhenwei.provider.jce.interfaces.ECPublicKey) InvalidKeyException(java.security.InvalidKeyException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

ECKeyParameters (com.github.zhenwei.core.crypto.params.ECKeyParameters)5 InvalidKeyException (java.security.InvalidKeyException)5 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)2 ECKey (com.github.zhenwei.provider.jce.interfaces.ECKey)2 ECPublicKey (com.github.zhenwei.provider.jce.interfaces.ECPublicKey)2 SignatureException (java.security.SignatureException)2 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)1 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)1 KeyEncoder (com.github.zhenwei.core.crypto.KeyEncoder)1 ECKeyPairGenerator (com.github.zhenwei.core.crypto.generators.ECKeyPairGenerator)1 EphemeralKeyPairGenerator (com.github.zhenwei.core.crypto.generators.EphemeralKeyPairGenerator)1 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)1 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)1 ECKeyGenerationParameters (com.github.zhenwei.core.crypto.params.ECKeyGenerationParameters)1 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)1 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)1 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)1 ECIESPublicKeyParser (com.github.zhenwei.core.crypto.parsers.ECIESPublicKeyParser)1 BadBlockException (com.github.zhenwei.provider.jcajce.provider.util.BadBlockException)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1