Search in sources :

Example 1 with OAEPEncoding

use of com.github.zhenwei.core.crypto.encodings.OAEPEncoding in project LinLong-Java by zhenwei1108.

the class CipherSpi method initFromSpec.

private void initFromSpec(OAEPParameterSpec pSpec) throws NoSuchPaddingException {
    MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) pSpec.getMGFParameters();
    Digest digest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
    if (digest == null) {
        throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: " + mgfParams.getDigestAlgorithm());
    }
    cipher = new OAEPEncoding(new ElGamalEngine(), digest, ((PSource.PSpecified) pSpec.getPSource()).getValue());
    paramSpec = pSpec;
}
Also used : Digest(com.github.zhenwei.core.crypto.Digest) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OAEPEncoding(com.github.zhenwei.core.crypto.encodings.OAEPEncoding) ElGamalEngine(com.github.zhenwei.core.crypto.engines.ElGamalEngine) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 2 with OAEPEncoding

use of com.github.zhenwei.core.crypto.encodings.OAEPEncoding in project LinLong-Java by zhenwei1108.

the class CipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key instanceof DHPublicKey) {
        param = ElGamalUtil.generatePublicKeyParameter((PublicKey) key);
    } else if (key instanceof DHPrivateKey) {
        param = ElGamalUtil.generatePrivateKeyParameter((PrivateKey) key);
    } else {
        throw new InvalidKeyException("unknown key type passed to ElGamal");
    }
    if (params instanceof OAEPParameterSpec) {
        OAEPParameterSpec spec = (OAEPParameterSpec) params;
        paramSpec = params;
        if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1") && !spec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
            throw new InvalidAlgorithmParameterException("unknown mask generation function specified");
        }
        if (!(spec.getMGFParameters() instanceof MGF1ParameterSpec)) {
            throw new InvalidAlgorithmParameterException("unkown MGF parameters");
        }
        Digest digest = DigestFactory.getDigest(spec.getDigestAlgorithm());
        if (digest == null) {
            throw new InvalidAlgorithmParameterException("no match on digest algorithm: " + spec.getDigestAlgorithm());
        }
        MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) spec.getMGFParameters();
        Digest mgfDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
        if (mgfDigest == null) {
            throw new InvalidAlgorithmParameterException("no match on MGF digest algorithm: " + mgfParams.getDigestAlgorithm());
        }
        cipher = new OAEPEncoding(new ElGamalEngine(), digest, mgfDigest, ((PSource.PSpecified) spec.getPSource()).getValue());
    } else if (params != null) {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    if (random != null) {
        param = new ParametersWithRandom(param, random);
    }
    switch(opmode) {
        case javax.crypto.Cipher.ENCRYPT_MODE:
        case javax.crypto.Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case javax.crypto.Cipher.DECRYPT_MODE:
        case javax.crypto.Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            throw new InvalidParameterException("unknown opmode " + opmode + " passed to ElGamal");
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DHPublicKey(javax.crypto.interfaces.DHPublicKey) Digest(com.github.zhenwei.core.crypto.Digest) PublicKey(java.security.PublicKey) DHPublicKey(javax.crypto.interfaces.DHPublicKey) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) InvalidKeyException(java.security.InvalidKeyException) ElGamalEngine(com.github.zhenwei.core.crypto.engines.ElGamalEngine) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) InvalidParameterException(java.security.InvalidParameterException) OAEPEncoding(com.github.zhenwei.core.crypto.encodings.OAEPEncoding) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 3 with OAEPEncoding

use of com.github.zhenwei.core.crypto.encodings.OAEPEncoding in project LinLong-Java by zhenwei1108.

the class CipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (params == null || params instanceof OAEPParameterSpec) {
        if (key instanceof RSAPublicKey) {
            if (privateKeyOnly && opmode == Cipher.ENCRYPT_MODE) {
                throw new InvalidKeyException("mode 1 requires RSAPrivateKey");
            }
            param = RSAUtil.generatePublicKeyParameter((RSAPublicKey) key);
        } else if (key instanceof RSAPrivateKey) {
            if (publicKeyOnly && opmode == Cipher.ENCRYPT_MODE) {
                throw new InvalidKeyException("mode 2 requires RSAPublicKey");
            }
            param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey) key);
        } else {
            throw new InvalidKeyException("unknown key type passed to RSA");
        }
        if (params != null) {
            OAEPParameterSpec spec = (OAEPParameterSpec) params;
            paramSpec = params;
            if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1") && !spec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
                throw new InvalidAlgorithmParameterException("unknown mask generation function specified");
            }
            if (!(spec.getMGFParameters() instanceof MGF1ParameterSpec)) {
                throw new InvalidAlgorithmParameterException("unkown MGF parameters");
            }
            Digest digest = DigestFactory.getDigest(spec.getDigestAlgorithm());
            if (digest == null) {
                throw new InvalidAlgorithmParameterException("no match on digest algorithm: " + spec.getDigestAlgorithm());
            }
            MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) spec.getMGFParameters();
            Digest mgfDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
            if (mgfDigest == null) {
                throw new InvalidAlgorithmParameterException("no match on MGF digest algorithm: " + mgfParams.getDigestAlgorithm());
            }
            cipher = new OAEPEncoding(new RSABlindedEngine(), digest, mgfDigest, ((PSource.PSpecified) spec.getPSource()).getValue());
        }
    } else {
        throw new InvalidAlgorithmParameterException("unknown parameter type: " + params.getClass().getName());
    }
    if (!(cipher instanceof RSABlindedEngine)) {
        if (random != null) {
            param = new ParametersWithRandom(param, random);
        } else {
            param = new ParametersWithRandom(param, CryptoServicesRegistrar.getSecureRandom());
        }
    }
    bOut.reset();
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            throw new InvalidParameterException("unknown opmode " + opmode + " passed to RSA");
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) Digest(com.github.zhenwei.core.crypto.Digest) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) InvalidKeyException(java.security.InvalidKeyException) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) InvalidParameterException(java.security.InvalidParameterException) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSABlindedEngine(com.github.zhenwei.core.crypto.engines.RSABlindedEngine) OAEPEncoding(com.github.zhenwei.core.crypto.encodings.OAEPEncoding) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 4 with OAEPEncoding

use of com.github.zhenwei.core.crypto.encodings.OAEPEncoding in project LinLong-Java by zhenwei1108.

the class CipherSpi method initFromSpec.

private void initFromSpec(OAEPParameterSpec pSpec) throws NoSuchPaddingException {
    MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) pSpec.getMGFParameters();
    Digest digest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
    if (digest == null) {
        throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: " + mgfParams.getDigestAlgorithm());
    }
    cipher = new OAEPEncoding(new RSABlindedEngine(), digest, ((PSource.PSpecified) pSpec.getPSource()).getValue());
    paramSpec = pSpec;
}
Also used : Digest(com.github.zhenwei.core.crypto.Digest) RSABlindedEngine(com.github.zhenwei.core.crypto.engines.RSABlindedEngine) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OAEPEncoding(com.github.zhenwei.core.crypto.encodings.OAEPEncoding) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Aggregations

Digest (com.github.zhenwei.core.crypto.Digest)4 OAEPEncoding (com.github.zhenwei.core.crypto.encodings.OAEPEncoding)4 MGF1ParameterSpec (java.security.spec.MGF1ParameterSpec)4 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)2 ElGamalEngine (com.github.zhenwei.core.crypto.engines.ElGamalEngine)2 RSABlindedEngine (com.github.zhenwei.core.crypto.engines.RSABlindedEngine)2 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 InvalidParameterException (java.security.InvalidParameterException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)2 PublicKey (java.security.PublicKey)1 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 DHPrivateKey (javax.crypto.interfaces.DHPrivateKey)1 DHPublicKey (javax.crypto.interfaces.DHPublicKey)1