Search in sources :

Example 11 with KeyParameter

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

the class ISO9797Alg3Mac method init.

public void init(CipherParameters params) {
    reset();
    if (!(params instanceof KeyParameter || params instanceof ParametersWithIV)) {
        throw new IllegalArgumentException("params must be an instance of KeyParameter or ParametersWithIV");
    }
    // KeyParameter must contain a double or triple length DES key,
    // however the underlying cipher is a single DES. The middle and
    // right key are used only in the final step.
    KeyParameter kp;
    if (params instanceof KeyParameter) {
        kp = (KeyParameter) params;
    } else {
        kp = (KeyParameter) ((ParametersWithIV) params).getParameters();
    }
    KeyParameter key1;
    byte[] keyvalue = kp.getKey();
    if (keyvalue.length == 16) {
        // Double length DES key
        key1 = new KeyParameter(keyvalue, 0, 8);
        this.lastKey2 = new KeyParameter(keyvalue, 8, 8);
        this.lastKey3 = key1;
    } else if (keyvalue.length == 24) {
        // Triple length DES key
        key1 = new KeyParameter(keyvalue, 0, 8);
        this.lastKey2 = new KeyParameter(keyvalue, 8, 8);
        this.lastKey3 = new KeyParameter(keyvalue, 16, 8);
    } else {
        throw new IllegalArgumentException("Key must be either 112 or 168 bit long");
    }
    if (params instanceof ParametersWithIV) {
        cipher.init(true, new ParametersWithIV(key1, ((ParametersWithIV) params).getIV()));
    } else {
        cipher.init(true, key1);
    }
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 12 with KeyParameter

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

the class Poly1305 method setKey.

private void setKey(final byte[] key, final byte[] nonce) {
    if (key.length != 32) {
        throw new IllegalArgumentException("Poly1305 key must be 256 bits.");
    }
    if (cipher != null && (nonce == null || nonce.length != BLOCK_SIZE)) {
        throw new IllegalArgumentException("Poly1305 requires a 128 bit IV.");
    }
    // Extract r portion of key (and "clamp" the values)
    int t0 = Pack.littleEndianToInt(key, 0);
    int t1 = Pack.littleEndianToInt(key, 4);
    int t2 = Pack.littleEndianToInt(key, 8);
    int t3 = Pack.littleEndianToInt(key, 12);
    // NOTE: The masks perform the key "clamping" implicitly
    r0 = t0 & 0x03FFFFFF;
    r1 = ((t0 >>> 26) | (t1 << 6)) & 0x03FFFF03;
    r2 = ((t1 >>> 20) | (t2 << 12)) & 0x03FFC0FF;
    r3 = ((t2 >>> 14) | (t3 << 18)) & 0x03F03FFF;
    r4 = (t3 >>> 8) & 0x000FFFFF;
    // Precompute multipliers
    s1 = r1 * 5;
    s2 = r2 * 5;
    s3 = r3 * 5;
    s4 = r4 * 5;
    final byte[] kBytes;
    final int kOff;
    if (cipher == null) {
        kBytes = key;
        kOff = BLOCK_SIZE;
    } else {
        // Compute encrypted nonce
        kBytes = new byte[BLOCK_SIZE];
        kOff = 0;
        cipher.init(true, new KeyParameter(key, BLOCK_SIZE, BLOCK_SIZE));
        cipher.processBlock(nonce, 0, kBytes, 0);
    }
    k0 = Pack.littleEndianToInt(kBytes, kOff + 0);
    k1 = Pack.littleEndianToInt(kBytes, kOff + 4);
    k2 = Pack.littleEndianToInt(kBytes, kOff + 8);
    k3 = Pack.littleEndianToInt(kBytes, kOff + 12);
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 13 with KeyParameter

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

the class Poly1305 method init.

/**
 * Initialises the Poly1305 MAC.
 *
 * @param params if used with a block cipher, then a {@link ParametersWithIV} containing a 128 bit
 *               nonce and a {@link KeyParameter} with a 256 bit key complying to the {@link
 *               Poly1305KeyGenerator Poly1305 key format}, otherwise just the {@link
 *               KeyParameter}.
 */
public void init(CipherParameters params) throws IllegalArgumentException {
    byte[] nonce = null;
    if (cipher != null) {
        if (!(params instanceof ParametersWithIV)) {
            throw new IllegalArgumentException("Poly1305 requires an IV when used with a block cipher.");
        }
        ParametersWithIV ivParams = (ParametersWithIV) params;
        nonce = ivParams.getIV();
        params = ivParams.getParameters();
    }
    if (!(params instanceof KeyParameter)) {
        throw new IllegalArgumentException("Poly1305 requires a key.");
    }
    KeyParameter keyParams = (KeyParameter) params;
    setKey(keyParams.getKey(), nonce);
    reset();
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 14 with KeyParameter

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

the class SipHash method init.

public void init(CipherParameters params) throws IllegalArgumentException {
    if (!(params instanceof KeyParameter)) {
        throw new IllegalArgumentException("'params' must be an instance of KeyParameter");
    }
    KeyParameter keyParameter = (KeyParameter) params;
    byte[] key = keyParameter.getKey();
    if (key.length != 16) {
        throw new IllegalArgumentException("'params' must be a 128-bit key");
    }
    this.k0 = Pack.littleEndianToLong(key, 0);
    this.k1 = Pack.littleEndianToLong(key, 8);
    reset();
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 15 with KeyParameter

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

the class SkeinMac method init.

/**
 * Initialises the Skein digest with the provided parameters.<br> See {@link SkeinParameters} for
 * details on the parameterisation of the Skein hash function.
 *
 * @param params an instance of {@link SkeinParameters} or {@link KeyParameter}.
 */
public void init(CipherParameters params) throws IllegalArgumentException {
    SkeinParameters skeinParameters;
    if (params instanceof SkeinParameters) {
        skeinParameters = (SkeinParameters) params;
    } else if (params instanceof KeyParameter) {
        skeinParameters = new SkeinParameters.Builder().setKey(((KeyParameter) params).getKey()).build();
    } else {
        throw new IllegalArgumentException("Invalid parameter passed to Skein MAC init - " + params.getClass().getName());
    }
    if (skeinParameters.getKey() == null) {
        throw new IllegalArgumentException("Skein MAC requires a key parameter.");
    }
    engine.init(skeinParameters);
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) SkeinParameters(com.github.zhenwei.core.crypto.params.SkeinParameters)

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