Search in sources :

Example 66 with KeyParameter

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

the class GOST28147Mac method recursiveInit.

private void recursiveInit(CipherParameters params) throws IllegalArgumentException {
    if (params == null) {
        return;
    }
    CipherParameters child = null;
    if (params instanceof ParametersWithSBox) {
        ParametersWithSBox param = (ParametersWithSBox) params;
        // 
        // Set the S-Box
        // 
        System.arraycopy(param.getSBox(), 0, this.S, 0, param.getSBox().length);
        child = param.getParameters();
    } else if (params instanceof KeyParameter) {
        workingKey = generateWorkingKey(((KeyParameter) params).getKey());
    } else if (params instanceof ParametersWithIV) {
        ParametersWithIV p = (ParametersWithIV) params;
        System.arraycopy(p.getIV(), 0, mac, 0, mac.length);
        // don't skip the initial CM5Func
        macIV = p.getIV();
        child = p.getParameters();
    } else {
        throw new IllegalArgumentException("invalid parameter passed to GOST28147 init - " + params.getClass().getName());
    }
    recursiveInit(child);
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) ParametersWithSBox(com.github.zhenwei.core.crypto.params.ParametersWithSBox) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 67 with KeyParameter

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

the class KGMac method init.

/**
 * Initialises the GMAC - requires a {@link ParametersWithIV} providing a {@link KeyParameter} and
 * a nonce.
 */
public void init(final CipherParameters params) throws IllegalArgumentException {
    if (params instanceof ParametersWithIV) {
        final ParametersWithIV param = (ParametersWithIV) params;
        final byte[] iv = param.getIV();
        final KeyParameter keyParam = (KeyParameter) param.getParameters();
        // GCM is always operated in encrypt mode to calculate MAC
        cipher.init(true, new AEADParameters(keyParam, macSizeBits, iv));
    } else {
        throw new IllegalArgumentException("KGMAC requires ParametersWithIV");
    }
}
Also used : 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 68 with KeyParameter

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

the class KMAC method init.

public void init(CipherParameters params) throws IllegalArgumentException {
    KeyParameter kParam = (KeyParameter) params;
    this.key = Arrays.clone(kParam.getKey());
    this.initialised = true;
    reset();
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 69 with KeyParameter

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

the class GOST28147Engine method init.

/**
 * initialise an GOST28147 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 ParametersWithSBox) {
        ParametersWithSBox param = (ParametersWithSBox) params;
        // 
        // Set the S-Box
        // 
        byte[] sBox = param.getSBox();
        if (sBox.length != Sbox_Default.length) {
            throw new IllegalArgumentException("invalid S-box passed to GOST28147 init");
        }
        this.S = Arrays.clone(sBox);
        // 
        if (param.getParameters() != null) {
            workingKey = generateWorkingKey(forEncryption, ((KeyParameter) param.getParameters()).getKey());
        }
    } else if (params instanceof KeyParameter) {
        workingKey = generateWorkingKey(forEncryption, ((KeyParameter) params).getKey());
    } else if (params != null) {
        throw new IllegalArgumentException("invalid parameter passed to GOST28147 init - " + params.getClass().getName());
    }
}
Also used : ParametersWithSBox(com.github.zhenwei.core.crypto.params.ParametersWithSBox) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 70 with KeyParameter

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

the class Grainv1Engine method init.

/**
 * Initialize a Grain v1 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) throws IllegalArgumentException {
    /**
     * Grain encryption and decryption is completely symmetrical, so the
     * 'forEncryption' is irrelevant.
     */
    if (!(params instanceof ParametersWithIV)) {
        throw new IllegalArgumentException("Grain v1 Init parameters must include an IV");
    }
    ParametersWithIV ivParams = (ParametersWithIV) params;
    byte[] iv = ivParams.getIV();
    if (iv == null || iv.length != 8) {
        throw new IllegalArgumentException("Grain v1 requires exactly 8 bytes of IV");
    }
    if (!(ivParams.getParameters() instanceof KeyParameter)) {
        throw new IllegalArgumentException("Grain v1 Init parameters must include a key");
    }
    KeyParameter key = (KeyParameter) ivParams.getParameters();
    /**
     * Initialize variables.
     */
    workingIV = new byte[key.getKey().length];
    workingKey = new byte[key.getKey().length];
    lfsr = new int[STATE_SIZE];
    nfsr = new int[STATE_SIZE];
    out = new byte[2];
    System.arraycopy(iv, 0, workingIV, 0, iv.length);
    System.arraycopy(key.getKey(), 0, workingKey, 0, key.getKey().length);
    reset();
}
Also used : 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