Search in sources :

Example 1 with IESParameterSpec

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

the class IESCipher method engineInit.

public void engineInit(int opmode, Key key, AlgorithmParameterSpec engineSpec, SecureRandom random) throws InvalidAlgorithmParameterException, InvalidKeyException {
    // Use default parameters (including cipher key size) if none are specified
    if (engineSpec == null) {
        byte[] nonce = null;
        if (ivLength != 0 && opmode == Cipher.ENCRYPT_MODE) {
            nonce = new byte[ivLength];
            random.nextBytes(nonce);
        }
        this.engineSpec = IESUtil.guessParameterSpec(engine.getCipher(), nonce);
    } else if (engineSpec instanceof IESParameterSpec) {
        this.engineSpec = (IESParameterSpec) engineSpec;
    } else {
        throw new InvalidAlgorithmParameterException("must be passed IES parameters");
    }
    byte[] nonce = this.engineSpec.getNonce();
    if (ivLength != 0 && (nonce == null || nonce.length != ivLength)) {
        throw new InvalidAlgorithmParameterException("NONCE in IES Parameters needs to be " + ivLength + " bytes long");
    }
    // Parse the recipient's key
    if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE) {
        if (key instanceof DHPublicKey) {
            this.key = DHUtil.generatePublicKeyParameter((PublicKey) key);
        } else if (key instanceof IESKey) {
            IESKey ieKey = (IESKey) key;
            this.key = DHUtil.generatePublicKeyParameter(ieKey.getPublic());
            this.otherKeyParameter = DHUtil.generatePrivateKeyParameter(ieKey.getPrivate());
        } else {
            throw new InvalidKeyException("must be passed recipient's public DH key for encryption");
        }
    } else if (opmode == Cipher.DECRYPT_MODE || opmode == Cipher.UNWRAP_MODE) {
        if (key instanceof DHPrivateKey) {
            this.key = DHUtil.generatePrivateKeyParameter((PrivateKey) key);
        } else if (key instanceof IESKey) {
            IESKey ieKey = (IESKey) key;
            this.otherKeyParameter = DHUtil.generatePublicKeyParameter(ieKey.getPublic());
            this.key = DHUtil.generatePrivateKeyParameter(ieKey.getPrivate());
        } else {
            throw new InvalidKeyException("must be passed recipient's private DH key for decryption");
        }
    } else {
        throw new InvalidKeyException("must be passed EC key");
    }
    this.random = random;
    this.state = opmode;
    buffer.reset();
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DHPublicKey(javax.crypto.interfaces.DHPublicKey) DHPublicKey(javax.crypto.interfaces.DHPublicKey) PublicKey(java.security.PublicKey) IESParameterSpec(com.github.zhenwei.provider.jce.spec.IESParameterSpec) IESKey(com.github.zhenwei.provider.jce.interfaces.IESKey) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with IESParameterSpec

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

the class IESCipher method engineInit.

public void engineInit(int opmode, Key key, AlgorithmParameterSpec engineSpec, SecureRandom random) throws InvalidAlgorithmParameterException, InvalidKeyException {
    otherKeyParameter = null;
    // Use default parameters (including cipher key size) if none are specified
    if (engineSpec == null) {
        byte[] nonce = null;
        if (ivLength != 0 && opmode == Cipher.ENCRYPT_MODE) {
            nonce = new byte[ivLength];
            random.nextBytes(nonce);
        }
        this.engineSpec = IESUtil.guessParameterSpec(engine.getCipher(), nonce);
    } else if (engineSpec instanceof IESParameterSpec) {
        this.engineSpec = (IESParameterSpec) engineSpec;
    } else {
        throw new InvalidAlgorithmParameterException("must be passed IES parameters");
    }
    byte[] nonce = this.engineSpec.getNonce();
    if (ivLength != 0 && (nonce == null || nonce.length != ivLength)) {
        throw new InvalidAlgorithmParameterException("NONCE in IES Parameters needs to be " + ivLength + " bytes long");
    }
    // Parse the recipient's key
    if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE) {
        if (key instanceof PublicKey) {
            this.key = ECUtils.generatePublicKeyParameter((PublicKey) key);
        } else if (key instanceof IESKey) {
            IESKey ieKey = (IESKey) key;
            this.key = ECUtils.generatePublicKeyParameter(ieKey.getPublic());
            this.otherKeyParameter = ECUtil.generatePrivateKeyParameter(ieKey.getPrivate());
        } else {
            throw new InvalidKeyException("must be passed recipient's public EC key for encryption");
        }
    } else if (opmode == Cipher.DECRYPT_MODE || opmode == Cipher.UNWRAP_MODE) {
        if (key instanceof PrivateKey) {
            this.key = ECUtil.generatePrivateKeyParameter((PrivateKey) key);
        } else if (key instanceof IESKey) {
            IESKey ieKey = (IESKey) key;
            this.otherKeyParameter = ECUtils.generatePublicKeyParameter(ieKey.getPublic());
            this.key = ECUtil.generatePrivateKeyParameter(ieKey.getPrivate());
        } else {
            throw new InvalidKeyException("must be passed recipient's private EC key for decryption");
        }
    } else {
        throw new InvalidKeyException("must be passed EC key");
    }
    this.random = random;
    this.state = opmode;
    buffer.reset();
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) IESParameterSpec(com.github.zhenwei.provider.jce.spec.IESParameterSpec) IESKey(com.github.zhenwei.provider.jce.interfaces.IESKey) InvalidKeyException(java.security.InvalidKeyException)

Example 3 with IESParameterSpec

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

the class AlgorithmParametersSpi method engineInit.

protected void engineInit(byte[] params) throws IOException {
    try {
        ASN1Sequence s = (ASN1Sequence) ASN1Primitive.fromByteArray(params);
        if (s.size() > 5) {
            throw new IOException("sequence too big");
        }
        byte[] derivationV = null;
        byte[] encodingV = null;
        BigInteger macKeySize = null;
        BigInteger keySize = null;
        byte[] nonce = null;
        boolean pointCompression = false;
        for (Enumeration en = s.getObjects(); en.hasMoreElements(); ) {
            Object o = en.nextElement();
            if (o instanceof ASN1TaggedObject) {
                ASN1TaggedObject t = ASN1TaggedObject.getInstance(o);
                if (t.getTagNo() == 0) {
                    derivationV = ASN1OctetString.getInstance(t, false).getOctets();
                } else if (t.getTagNo() == 1) {
                    encodingV = ASN1OctetString.getInstance(t, false).getOctets();
                }
            } else if (o instanceof ASN1Integer) {
                macKeySize = ASN1Integer.getInstance(o).getValue();
            } else if (o instanceof ASN1Sequence) {
                ASN1Sequence seq = ASN1Sequence.getInstance(o);
                keySize = ASN1Integer.getInstance(seq.getObjectAt(0)).getValue();
                nonce = ASN1OctetString.getInstance(seq.getObjectAt(1)).getOctets();
            } else if (o instanceof ASN1Boolean) {
                pointCompression = ASN1Boolean.getInstance(o).isTrue();
            }
        }
        if (keySize != null) {
            this.currentSpec = new IESParameterSpec(derivationV, encodingV, macKeySize.intValue(), keySize.intValue(), nonce, pointCompression);
        } else {
            this.currentSpec = new IESParameterSpec(derivationV, encodingV, macKeySize.intValue(), -1, null, pointCompression);
        }
    } catch (ClassCastException e) {
        throw new IOException("Not a valid IES Parameter encoding.");
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new IOException("Not a valid IES Parameter encoding.");
    }
}
Also used : Enumeration(java.util.Enumeration) ASN1TaggedObject(com.github.zhenwei.core.asn1.ASN1TaggedObject) IESParameterSpec(com.github.zhenwei.provider.jce.spec.IESParameterSpec) IOException(java.io.IOException) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) BigInteger(java.math.BigInteger) ASN1TaggedObject(com.github.zhenwei.core.asn1.ASN1TaggedObject) DERTaggedObject(com.github.zhenwei.core.asn1.DERTaggedObject) ASN1Boolean(com.github.zhenwei.core.asn1.ASN1Boolean)

Aggregations

IESParameterSpec (com.github.zhenwei.provider.jce.spec.IESParameterSpec)3 IESKey (com.github.zhenwei.provider.jce.interfaces.IESKey)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 PublicKey (java.security.PublicKey)2 ASN1Boolean (com.github.zhenwei.core.asn1.ASN1Boolean)1 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)1 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)1 ASN1TaggedObject (com.github.zhenwei.core.asn1.ASN1TaggedObject)1 DERTaggedObject (com.github.zhenwei.core.asn1.DERTaggedObject)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 PrivateKey (java.security.PrivateKey)1 Enumeration (java.util.Enumeration)1 DHPrivateKey (javax.crypto.interfaces.DHPrivateKey)1 DHPublicKey (javax.crypto.interfaces.DHPublicKey)1