Search in sources :

Example 16 with KeyParameter

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

the class VMPCMac method init.

public void init(CipherParameters params) throws IllegalArgumentException {
    if (!(params instanceof ParametersWithIV)) {
        throw new IllegalArgumentException("VMPC-MAC Init parameters must include an IV");
    }
    ParametersWithIV ivParams = (ParametersWithIV) params;
    KeyParameter key = (KeyParameter) ivParams.getParameters();
    if (!(ivParams.getParameters() instanceof KeyParameter)) {
        throw new IllegalArgumentException("VMPC-MAC Init parameters must include a key");
    }
    this.workingIV = ivParams.getIV();
    if (workingIV == null || workingIV.length < 1 || workingIV.length > 768) {
        throw new IllegalArgumentException("VMPC-MAC requires 1 to 768 bytes of IV");
    }
    this.workingKey = key.getKey();
    reset();
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 17 with KeyParameter

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

the class CTRSP800DRBG method CTR_DRBG_Update.

private void CTR_DRBG_Update(byte[] seed, byte[] key, byte[] v) {
    byte[] temp = new byte[seed.length];
    byte[] outputBlock = new byte[_engine.getBlockSize()];
    int i = 0;
    int outLen = _engine.getBlockSize();
    _engine.init(true, new KeyParameter(expandKey(key)));
    while (i * outLen < seed.length) {
        addOneTo(v);
        _engine.processBlock(v, 0, outputBlock, 0);
        int bytesToCopy = ((temp.length - i * outLen) > outLen) ? outLen : (temp.length - i * outLen);
        System.arraycopy(outputBlock, 0, temp, i * outLen, bytesToCopy);
        ++i;
    }
    XOR(temp, seed, temp, 0);
    System.arraycopy(temp, 0, key, 0, key.length);
    System.arraycopy(temp, key.length, v, 0, v.length);
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 18 with KeyParameter

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

the class CTRSP800DRBG method BCC.

/*
   * 1. chaining_value = 0^outlen
   *    . Comment: Set the first chaining value to outlen zeros.
   * 2. n = len (data)/outlen.
   * 3. Starting with the leftmost bits of data, split the data into n blocks of outlen bits
   *    each, forming block(1) to block(n).
   * 4. For i = 1 to n do
   * 4.1 input_block = chaining_value ^ block(i) .
   * 4.2 chaining_value = Block_Encrypt (Key, input_block).
   * 5. output_block = chaining_value.
   * 6. Return output_block.
   */
private void BCC(byte[] bccOut, byte[] k, byte[] iV, byte[] data) {
    int outlen = _engine.getBlockSize();
    // initial values = 0
    byte[] chainingValue = new byte[outlen];
    int n = data.length / outlen;
    byte[] inputBlock = new byte[outlen];
    _engine.init(true, new KeyParameter(expandKey(k)));
    _engine.processBlock(iV, 0, chainingValue, 0);
    for (int i = 0; i < n; i++) {
        XOR(inputBlock, chainingValue, data, i * outlen);
        _engine.processBlock(inputBlock, 0, chainingValue, 0);
    }
    System.arraycopy(chainingValue, 0, bccOut, 0, bccOut.length);
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 19 with KeyParameter

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

the class HMacSP800DRBG method generate.

/**
 * Populate a passed in array with random data.
 *
 * @param output              output array for generated bits.
 * @param additionalInput     additional input to be added to the DRBG in this step.
 * @param predictionResistant true if a reseed should be forced, false otherwise.
 * @return number of bits generated, -1 if a reseed required.
 */
public int generate(byte[] output, byte[] additionalInput, boolean predictionResistant) {
    int numberOfBits = output.length * 8;
    if (numberOfBits > MAX_BITS_REQUEST) {
        throw new IllegalArgumentException("Number of bits per request limited to " + MAX_BITS_REQUEST);
    }
    if (_reseedCounter > RESEED_MAX) {
        return -1;
    }
    if (predictionResistant) {
        reseed(additionalInput);
        additionalInput = null;
    }
    // 2.
    if (additionalInput != null) {
        hmac_DRBG_Update(additionalInput);
    }
    // 3.
    byte[] rv = new byte[output.length];
    int m = output.length / _V.length;
    _hMac.init(new KeyParameter(_K));
    for (int i = 0; i < m; i++) {
        _hMac.update(_V, 0, _V.length);
        _hMac.doFinal(_V, 0);
        System.arraycopy(_V, 0, rv, i * _V.length, _V.length);
    }
    if (m * _V.length < rv.length) {
        _hMac.update(_V, 0, _V.length);
        _hMac.doFinal(_V, 0);
        System.arraycopy(_V, 0, rv, m * _V.length, rv.length - (m * _V.length));
    }
    hmac_DRBG_Update(additionalInput);
    _reseedCounter++;
    System.arraycopy(rv, 0, output, 0, output.length);
    return numberOfBits;
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 20 with KeyParameter

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

the class HMacDSAKCalculator method nextK.

public BigInteger nextK() {
    byte[] t = new byte[BigIntegers.getUnsignedByteLength(n)];
    for (; ; ) {
        int tOff = 0;
        while (tOff < t.length) {
            hMac.update(V, 0, V.length);
            hMac.doFinal(V, 0);
            int len = Math.min(t.length - tOff, V.length);
            System.arraycopy(V, 0, t, tOff, len);
            tOff += len;
        }
        BigInteger k = bitsToInt(t);
        if (k.compareTo(ZERO) > 0 && k.compareTo(n) < 0) {
            return k;
        }
        hMac.update(V, 0, V.length);
        hMac.update((byte) 0x00);
        hMac.doFinal(K, 0);
        hMac.init(new KeyParameter(K));
        hMac.update(V, 0, V.length);
        hMac.doFinal(V, 0);
    }
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) BigInteger(java.math.BigInteger)

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