Search in sources :

Example 26 with KeyParameter

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

the class PKCS5S2ParametersGenerator method generateDerivedKey.

private byte[] generateDerivedKey(int dkLen) {
    int hLen = hMac.getMacSize();
    int l = (dkLen + hLen - 1) / hLen;
    byte[] iBuf = new byte[4];
    byte[] outBytes = new byte[l * hLen];
    int outPos = 0;
    CipherParameters param = new KeyParameter(password);
    hMac.init(param);
    for (int i = 1; i <= l; i++) {
        // Increment the value in 'iBuf'
        int pos = 3;
        while (++iBuf[pos] == 0) {
            --pos;
        }
        F(salt, iterationCount, iBuf, outBytes, outPos);
        outPos += hLen;
    }
    return outBytes;
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 27 with KeyParameter

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

the class HKDFBytesGenerator method init.

public void init(DerivationParameters param) {
    if (!(param instanceof HKDFParameters)) {
        throw new IllegalArgumentException("HKDF parameters required for HKDFBytesGenerator");
    }
    HKDFParameters params = (HKDFParameters) param;
    if (params.skipExtract()) {
        // use IKM directly as PRK
        hMacHash.init(new KeyParameter(params.getIKM()));
    } else {
        hMacHash.init(extract(params.getSalt(), params.getIKM()));
    }
    info = params.getInfo();
    generatedBytes = 0;
    currentT = new byte[hashLen];
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) HKDFParameters(com.github.zhenwei.core.crypto.params.HKDFParameters)

Example 28 with KeyParameter

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

the class Salsa20Engine method init.

/**
 * initialise a Salsa20 cipher.
 *
 * @param forEncryption whether or not we are for encryption.
 * @param params        the parameters required to set up the cipher.
 * @throws IllegalArgumentException if the params argument is inappropriate.
 */
public void init(boolean forEncryption, CipherParameters params) {
    if (!(params instanceof ParametersWithIV)) {
        throw new IllegalArgumentException(getAlgorithmName() + " Init parameters must include an IV");
    }
    ParametersWithIV ivParams = (ParametersWithIV) params;
    byte[] iv = ivParams.getIV();
    if (iv == null || iv.length != getNonceSize()) {
        throw new IllegalArgumentException(getAlgorithmName() + " requires exactly " + getNonceSize() + " bytes of IV");
    }
    CipherParameters keyParam = ivParams.getParameters();
    if (keyParam == null) {
        if (!initialised) {
            throw new IllegalStateException(getAlgorithmName() + " KeyParameter can not be null for first initialisation");
        }
        setKey(null, iv);
    } else if (keyParam instanceof KeyParameter) {
        setKey(((KeyParameter) keyParam).getKey(), iv);
    } else {
        throw new IllegalArgumentException(getAlgorithmName() + " Init parameters must contain a KeyParameter (or null for re-init)");
    }
    reset();
    initialised = true;
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 29 with KeyParameter

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

the class TEAEngine method init.

/**
 * initialise
 *
 * @param forEncryption whether or not we are for encryption.
 * @param params        the parameters required to set up the cipher.
 * @throws IllegalArgumentException if the params argument is inappropriate.
 */
public void init(boolean forEncryption, CipherParameters params) {
    if (!(params instanceof KeyParameter)) {
        throw new IllegalArgumentException("invalid parameter passed to TEA init - " + params.getClass().getName());
    }
    _forEncryption = forEncryption;
    _initialised = true;
    KeyParameter p = (KeyParameter) params;
    setKey(p.getKey());
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 30 with KeyParameter

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

the class ThreefishEngine method init.

/**
 * Initialise the engine.
 *
 * @param params an instance of {@link TweakableBlockCipherParameters}, or {@link KeyParameter}
 *               (to use a 0 tweak)
 */
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    final byte[] keyBytes;
    final byte[] tweakBytes;
    if (params instanceof TweakableBlockCipherParameters) {
        TweakableBlockCipherParameters tParams = (TweakableBlockCipherParameters) params;
        keyBytes = tParams.getKey().getKey();
        tweakBytes = tParams.getTweak();
    } else if (params instanceof KeyParameter) {
        keyBytes = ((KeyParameter) params).getKey();
        tweakBytes = null;
    } else {
        throw new IllegalArgumentException("Invalid parameter passed to Threefish init - " + params.getClass().getName());
    }
    long[] keyWords = null;
    long[] tweakWords = null;
    if (keyBytes != null) {
        if (keyBytes.length != this.blocksizeBytes) {
            throw new IllegalArgumentException("Threefish key must be same size as block (" + blocksizeBytes + " bytes)");
        }
        keyWords = new long[blocksizeWords];
        for (int i = 0; i < keyWords.length; i++) {
            keyWords[i] = bytesToWord(keyBytes, i * 8);
        }
    }
    if (tweakBytes != null) {
        if (tweakBytes.length != TWEAK_SIZE_BYTES) {
            throw new IllegalArgumentException("Threefish tweak must be " + TWEAK_SIZE_BYTES + " bytes");
        }
        tweakWords = new long[] { bytesToWord(tweakBytes, 0), bytesToWord(tweakBytes, 8) };
    }
    init(forEncryption, keyWords, tweakWords);
}
Also used : TweakableBlockCipherParameters(com.github.zhenwei.core.crypto.params.TweakableBlockCipherParameters) 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