Search in sources :

Example 1 with CipherParameters

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

the class CCMBlockCipher method init.

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    this.forEncryption = forEncryption;
    CipherParameters cipherParameters;
    if (params instanceof AEADParameters) {
        AEADParameters param = (AEADParameters) params;
        nonce = param.getNonce();
        initialAssociatedText = param.getAssociatedText();
        macSize = getMacSize(forEncryption, param.getMacSize());
        cipherParameters = param.getKey();
    } else if (params instanceof ParametersWithIV) {
        ParametersWithIV param = (ParametersWithIV) params;
        nonce = param.getIV();
        initialAssociatedText = null;
        macSize = getMacSize(forEncryption, 64);
        cipherParameters = param.getParameters();
    } else {
        throw new IllegalArgumentException("invalid parameters passed to CCM: " + params.getClass().getName());
    }
    // NOTE: Very basic support for key re-use, but no performance gain from it
    if (cipherParameters != null) {
        keyParam = cipherParameters;
    }
    if (nonce == null || nonce.length < 7 || nonce.length > 13) {
        throw new IllegalArgumentException("nonce must have length from 7 to 13 octets");
    }
    reset();
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) AEADParameters(com.github.zhenwei.core.crypto.params.AEADParameters)

Example 2 with CipherParameters

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

the class ChaCha20Poly1305 method init.

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    KeyParameter initKeyParam;
    byte[] initNonce;
    CipherParameters chacha20Params;
    if (params instanceof AEADParameters) {
        AEADParameters aeadParams = (AEADParameters) params;
        int macSizeBits = aeadParams.getMacSize();
        if ((MAC_SIZE * 8) != macSizeBits) {
            throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
        }
        initKeyParam = aeadParams.getKey();
        initNonce = aeadParams.getNonce();
        chacha20Params = new ParametersWithIV(initKeyParam, initNonce);
        this.initialAAD = aeadParams.getAssociatedText();
    } else if (params instanceof ParametersWithIV) {
        ParametersWithIV ivParams = (ParametersWithIV) params;
        initKeyParam = (KeyParameter) ivParams.getParameters();
        initNonce = ivParams.getIV();
        chacha20Params = ivParams;
        this.initialAAD = null;
    } else {
        throw new IllegalArgumentException("invalid parameters passed to ChaCha20Poly1305");
    }
    // Validate key
    if (null == initKeyParam) {
        if (State.UNINITIALIZED == state) {
            throw new IllegalArgumentException("Key must be specified in initial init");
        }
    } else {
        if (KEY_SIZE != initKeyParam.getKey().length) {
            throw new IllegalArgumentException("Key must be 256 bits");
        }
    }
    // Validate nonce
    if (null == initNonce || NONCE_SIZE != initNonce.length) {
        throw new IllegalArgumentException("Nonce must be 96 bits");
    }
    // Check for encryption with reused nonce
    if (State.UNINITIALIZED != state && forEncryption && Arrays.areEqual(nonce, initNonce)) {
        if (null == initKeyParam || Arrays.areEqual(key, initKeyParam.getKey())) {
            throw new IllegalArgumentException("cannot reuse nonce for ChaCha20Poly1305 encryption");
        }
    }
    if (null != initKeyParam) {
        System.arraycopy(initKeyParam.getKey(), 0, key, 0, KEY_SIZE);
    }
    System.arraycopy(initNonce, 0, nonce, 0, NONCE_SIZE);
    chacha20.init(true, chacha20Params);
    this.state = forEncryption ? State.ENC_INIT : State.DEC_INIT;
    reset(true, false);
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) AEADParameters(com.github.zhenwei.core.crypto.params.AEADParameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 3 with CipherParameters

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

the class PSSSigner method init.

public void init(boolean forSigning, CipherParameters param) {
    CipherParameters params;
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom p = (ParametersWithRandom) param;
        params = p.getParameters();
        random = p.getRandom();
    } else {
        params = param;
        if (forSigning) {
            random = CryptoServicesRegistrar.getSecureRandom();
        }
    }
    RSAKeyParameters kParam;
    if (params instanceof RSABlindingParameters) {
        kParam = ((RSABlindingParameters) params).getPublicKey();
        // pass on random
        cipher.init(forSigning, param);
    } else {
        kParam = (RSAKeyParameters) params;
        cipher.init(forSigning, params);
    }
    emBits = kParam.getModulus().bitLength() - 1;
    if (emBits < (8 * hLen + 8 * sLen + 9)) {
        throw new IllegalArgumentException("key too small for specified hash and salt lengths");
    }
    block = new byte[(emBits + 7) / 8];
    reset();
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) RSABlindingParameters(com.github.zhenwei.core.crypto.params.RSABlindingParameters) RSAKeyParameters(com.github.zhenwei.core.crypto.params.RSAKeyParameters)

Example 4 with CipherParameters

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

the class SignatureSpi method engineInitVerify.

protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    CipherParameters param;
    if (publicKey instanceof BCDSTU4145PublicKey) {
        param = ((BCDSTU4145PublicKey) publicKey).engineGetKeyParameters();
        digest = new GOST3411Digest(expandSbox(((BCDSTU4145PublicKey) publicKey).getSbox()));
    } else {
        param = ECUtil.generatePublicKeyParameter(publicKey);
        digest = new GOST3411Digest(expandSbox(DSTU4145Params.getDefaultDKE()));
    }
    signer.init(false, param);
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) GOST3411Digest(com.github.zhenwei.core.crypto.digests.GOST3411Digest)

Example 5 with CipherParameters

use of com.github.zhenwei.core.crypto.CipherParameters 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);
    }
    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());
    }
    DHParameters dhParams = ((DHKeyParameters) key).getParameters();
    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
        DHKeyPairGenerator gen = new DHKeyPairGenerator();
        gen.init(new DHKeyGenerationParameters(random, dhParams));
        EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(gen, new KeyEncoder() {

            public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
                byte[] Vloc = new byte[(((DHKeyParameters) keyParameter).getParameters().getP().bitLength() + 7) / 8];
                byte[] Vtmp = BigIntegers.asUnsignedByteArray(((DHPublicKeyParameters) keyParameter).getY());
                if (Vtmp.length > Vloc.length) {
                    throw new IllegalArgumentException("Senders's public key longer than expected.");
                } else {
                    System.arraycopy(Vtmp, 0, Vloc, Vloc.length - Vtmp.length, Vtmp.length);
                }
                return Vloc;
            }
        });
        // Encrypt the buffer
        try {
            engine.init(key, params, kGen);
            return engine.processBlock(in, 0, in.length);
        } catch (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 DHIESPublicKeyParser(((DHKeyParameters) key).getParameters()));
            return engine.processBlock(in, 0, in.length);
        } catch (InvalidCipherTextException e) {
            throw new BadBlockException("unable to process block", e);
        }
    } else {
        throw new IllegalStateException("IESCipher not initialised");
    }
}
Also used : EphemeralKeyPairGenerator(com.github.zhenwei.core.crypto.generators.EphemeralKeyPairGenerator) DHKeyParameters(com.github.zhenwei.core.crypto.params.DHKeyParameters) BadBlockException(com.github.zhenwei.provider.jcajce.provider.util.BadBlockException) KeyEncoder(com.github.zhenwei.core.crypto.KeyEncoder) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) DHPublicKeyParameters(com.github.zhenwei.core.crypto.params.DHPublicKeyParameters) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) DHKeyPairGenerator(com.github.zhenwei.core.crypto.generators.DHKeyPairGenerator) DHKeyGenerationParameters(com.github.zhenwei.core.crypto.params.DHKeyGenerationParameters) 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) DHIESPublicKeyParser(com.github.zhenwei.core.crypto.parsers.DHIESPublicKeyParser) IESWithCipherParameters(com.github.zhenwei.core.crypto.params.IESWithCipherParameters)

Aggregations

CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)69 InvalidKeyException (java.security.InvalidKeyException)21 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)20 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)20 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)15 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)10 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)7 AEADParameters (com.github.zhenwei.core.crypto.params.AEADParameters)6 PublicKey (java.security.PublicKey)6 InvalidParameterException (java.security.InvalidParameterException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)5 ShortBufferException (javax.crypto.ShortBufferException)5 IvParameterSpec (javax.crypto.spec.IvParameterSpec)5 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)4 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)4 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)4 ParametersWithSBox (com.github.zhenwei.core.crypto.params.ParametersWithSBox)4 BadPaddingException (javax.crypto.BadPaddingException)4 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)4