Search in sources :

Example 46 with KeyParameter

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

the class BaseStreamCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    this.pbeSpec = null;
    this.pbeAlgorithm = null;
    this.engineParams = null;
    // 
    if (!(key instanceof SecretKey)) {
        throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
    }
    if (key instanceof PKCS12Key) {
        PKCS12Key k = (PKCS12Key) key;
        pbeSpec = (PBEParameterSpec) params;
        if (k instanceof PKCS12KeyWithParameters && pbeSpec == null) {
            pbeSpec = new PBEParameterSpec(((PKCS12KeyWithParameters) k).getSalt(), ((PKCS12KeyWithParameters) k).getIterationCount());
        }
        param = Util.makePBEParameters(k.getEncoded(), PKCS12, digest, keySizeInBits, ivLength * 8, pbeSpec, cipher.getAlgorithmName());
    } else if (key instanceof BCPBEKey) {
        BCPBEKey k = (BCPBEKey) key;
        if (k.getOID() != null) {
            pbeAlgorithm = k.getOID().getId();
        } else {
            pbeAlgorithm = k.getAlgorithm();
        }
        if (k.getParam() != null) {
            param = k.getParam();
            pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
        } else if (params instanceof PBEParameterSpec) {
            param = Util.makePBEParameters(k, params, cipher.getAlgorithmName());
            pbeSpec = (PBEParameterSpec) params;
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
        if (k.getIvSize() != 0) {
            ivParam = (ParametersWithIV) param;
        }
    } else if (params == null) {
        if (digest > 0) {
            throw new InvalidKeyException("Algorithm requires a PBE key");
        }
        param = new KeyParameter(key.getEncoded());
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
        ivParam = (ParametersWithIV) param;
    } else {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
        SecureRandom ivRandom = random;
        if (ivRandom == null) {
            ivRandom = CryptoServicesRegistrar.getSecureRandom();
        }
        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
            byte[] iv = new byte[ivLength];
            ivRandom.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV) param;
        } else {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }
    try {
        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");
        }
    } catch (Exception e) {
        throw new InvalidKeyException(e.getMessage());
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidParameterException(java.security.InvalidParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) DataLengthException(com.github.zhenwei.core.crypto.DataLengthException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) ShortBufferException(javax.crypto.ShortBufferException) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) InvalidParameterException(java.security.InvalidParameterException) SecretKey(javax.crypto.SecretKey) IvParameterSpec(javax.crypto.spec.IvParameterSpec) PKCS12Key(com.github.zhenwei.provider.jcajce.PKCS12Key) PKCS12KeyWithParameters(com.github.zhenwei.provider.jcajce.PKCS12KeyWithParameters) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 47 with KeyParameter

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

the class BaseWrapCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key instanceof BCPBEKey) {
        BCPBEKey k = (BCPBEKey) key;
        if (params instanceof PBEParameterSpec) {
            param = Util.makePBEParameters(k, params, wrapEngine.getAlgorithmName());
        } else if (k.getParam() != null) {
            param = k.getParam();
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    } else {
        param = new KeyParameter(key.getEncoded());
    }
    if (params instanceof IvParameterSpec) {
        IvParameterSpec ivSpec = (IvParameterSpec) params;
        this.iv = ivSpec.getIV();
        param = new ParametersWithIV(param, iv);
    }
    if (params instanceof GOST28147WrapParameterSpec) {
        GOST28147WrapParameterSpec spec = (GOST28147WrapParameterSpec) params;
        byte[] sBox = spec.getSBox();
        if (sBox != null) {
            param = new ParametersWithSBox(param, sBox);
        }
        param = new ParametersWithUKM(param, spec.getUKM());
    }
    if (param instanceof KeyParameter && ivSize != 0) {
        if (opmode == Cipher.WRAP_MODE || opmode == Cipher.ENCRYPT_MODE) {
            iv = new byte[ivSize];
            random.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
        }
    }
    if (random != null) {
        param = new ParametersWithRandom(param, random);
    }
    try {
        switch(opmode) {
            case Cipher.WRAP_MODE:
                wrapEngine.init(true, param);
                this.wrapStream = null;
                this.forWrapping = true;
                break;
            case Cipher.UNWRAP_MODE:
                wrapEngine.init(false, param);
                this.wrapStream = null;
                this.forWrapping = false;
                break;
            case Cipher.ENCRYPT_MODE:
                wrapEngine.init(true, param);
                this.wrapStream = new ErasableOutputStream();
                this.forWrapping = true;
                break;
            case Cipher.DECRYPT_MODE:
                wrapEngine.init(false, param);
                this.wrapStream = new ErasableOutputStream();
                this.forWrapping = false;
                break;
            default:
                throw new InvalidParameterException("Unknown mode parameter passed to init.");
        }
    } catch (Exception e) {
        throw new InvalidKeyOrParametersException(e.getMessage(), e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) GOST28147WrapParameterSpec(com.github.zhenwei.provider.jcajce.spec.GOST28147WrapParameterSpec) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidParameterException(java.security.InvalidParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) InvalidParameterException(java.security.InvalidParameterException) ParametersWithUKM(com.github.zhenwei.core.crypto.params.ParametersWithUKM) ParametersWithSBox(com.github.zhenwei.core.crypto.params.ParametersWithSBox) IvParameterSpec(javax.crypto.spec.IvParameterSpec) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 48 with KeyParameter

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

the class TLSKDF method hmac_hash.

private static void hmac_hash(Mac mac, byte[] secret, byte[] seed, byte[] out) {
    mac.init(new KeyParameter(secret));
    byte[] a = seed;
    int size = mac.getMacSize();
    int iterations = (out.length + size - 1) / size;
    byte[] buf = new byte[mac.getMacSize()];
    byte[] buf2 = new byte[mac.getMacSize()];
    for (int i = 0; i < iterations; i++) {
        mac.update(a, 0, a.length);
        mac.doFinal(buf, 0);
        a = buf;
        mac.update(a, 0, a.length);
        mac.update(seed, 0, seed.length);
        mac.doFinal(buf2, 0);
        System.arraycopy(buf2, 0, out, (size * i), Math.min(size, out.length - (size * i)));
    }
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 49 with KeyParameter

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

the class Seed method prg.

static void prg(byte[] r, int rOff, long rlen, byte[] key, int keyOff) {
    byte[] nonce = new byte[8];
    StreamCipher cipher = new ChaChaEngine(12);
    cipher.init(true, new ParametersWithIV(new KeyParameter(key, keyOff, 32), nonce));
    cipher.processBytes(r, rOff, (int) rlen, r, rOff);
// crypto_stream_chacha12(r, rlen, nonce, key);
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) ChaChaEngine(com.github.zhenwei.core.crypto.engines.ChaChaEngine) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) StreamCipher(com.github.zhenwei.core.crypto.StreamCipher)

Example 50 with KeyParameter

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

the class BcPasswordRecipientInfoGenerator method generateEncryptedBytes.

public byte[] generateEncryptedBytes(AlgorithmIdentifier keyEncryptionAlgorithm, byte[] derivedKey, GenericKey contentEncryptionKey) throws CMSException {
    byte[] contentEncryptionKeySpec = ((KeyParameter) CMSUtils.getBcKey(contentEncryptionKey)).getKey();
    Wrapper keyEncryptionCipher = EnvelopedDataHelper.createRFC3211Wrapper(keyEncryptionAlgorithm.getAlgorithm());
    keyEncryptionCipher.init(true, new ParametersWithIV(new KeyParameter(derivedKey), ASN1OctetString.getInstance(keyEncryptionAlgorithm.getParameters()).getOctets()));
    return keyEncryptionCipher.wrap(contentEncryptionKeySpec, 0, contentEncryptionKeySpec.length);
}
Also used : Wrapper(com.github.zhenwei.core.crypto.Wrapper) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Aggregations

KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)91 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)41 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)15 AEADParameters (com.github.zhenwei.core.crypto.params.AEADParameters)10 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)6 ParametersWithSBox (com.github.zhenwei.core.crypto.params.ParametersWithSBox)6 RC2Parameters (com.github.zhenwei.core.crypto.params.RC2Parameters)6 BigInteger (java.math.BigInteger)6 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)4 StreamCipher (com.github.zhenwei.core.crypto.StreamCipher)4 PKCS5S2ParametersGenerator (com.github.zhenwei.core.crypto.generators.PKCS5S2ParametersGenerator)4 HMac (com.github.zhenwei.core.crypto.macs.HMac)4 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)4 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)4 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 IvParameterSpec (javax.crypto.spec.IvParameterSpec)4 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 RC5Parameters (com.github.zhenwei.core.crypto.params.RC5Parameters)3 CMSException (com.github.zhenwei.pkix.cms.CMSException)3