Search in sources :

Example 6 with KeyParameter

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

the class GCMSIVBlockCipher method deriveKeys.

/**
 * Derive Keys.
 *
 * @param pKey the keyGeneration key
 */
private void deriveKeys(final KeyParameter pKey) {
    /* Create the buffers */
    final byte[] myIn = new byte[BUFLEN];
    final byte[] myOut = new byte[BUFLEN];
    final byte[] myResult = new byte[BUFLEN];
    final byte[] myEncKey = new byte[pKey.getKey().length];
    /* Prepare for encryption */
    System.arraycopy(theNonce, 0, myIn, BUFLEN - NONCELEN, NONCELEN);
    theCipher.init(true, pKey);
    /* Derive authentication key */
    int myOff = 0;
    theCipher.processBlock(myIn, 0, myOut, 0);
    System.arraycopy(myOut, 0, myResult, myOff, HALFBUFLEN);
    myIn[0]++;
    myOff += HALFBUFLEN;
    theCipher.processBlock(myIn, 0, myOut, 0);
    System.arraycopy(myOut, 0, myResult, myOff, HALFBUFLEN);
    /* Derive encryption key */
    myIn[0]++;
    myOff = 0;
    theCipher.processBlock(myIn, 0, myOut, 0);
    System.arraycopy(myOut, 0, myEncKey, myOff, HALFBUFLEN);
    myIn[0]++;
    myOff += HALFBUFLEN;
    theCipher.processBlock(myIn, 0, myOut, 0);
    System.arraycopy(myOut, 0, myEncKey, myOff, HALFBUFLEN);
    /* If we have a 32byte key */
    if (myEncKey.length == BUFLEN << 1) {
        /* Derive remainder of encryption key */
        myIn[0]++;
        myOff += HALFBUFLEN;
        theCipher.processBlock(myIn, 0, myOut, 0);
        System.arraycopy(myOut, 0, myEncKey, myOff, HALFBUFLEN);
        myIn[0]++;
        myOff += HALFBUFLEN;
        theCipher.processBlock(myIn, 0, myOut, 0);
        System.arraycopy(myOut, 0, myEncKey, myOff, HALFBUFLEN);
    }
    /* Initialise the Cipher */
    theCipher.init(true, new KeyParameter(myEncKey));
    /* Initialise the multiplier */
    fillReverse(myResult, 0, BUFLEN, myOut);
    mulX(myOut);
    theMultiplier.init(myOut);
    theFlags |= INIT;
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 7 with KeyParameter

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

the class KGCMBlockCipher method init.

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    this.forEncryption = forEncryption;
    KeyParameter engineParam;
    if (params instanceof AEADParameters) {
        AEADParameters param = (AEADParameters) params;
        byte[] iv = param.getNonce();
        int diff = this.iv.length - iv.length;
        Arrays.fill(this.iv, (byte) 0);
        System.arraycopy(iv, 0, this.iv, diff, iv.length);
        initialAssociatedText = param.getAssociatedText();
        int macSizeBits = param.getMacSize();
        if (macSizeBits < MIN_MAC_BITS || macSizeBits > (blockSize << 3) || (macSizeBits & 7) != 0) {
            throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
        }
        macSize = macSizeBits >>> 3;
        engineParam = param.getKey();
        if (initialAssociatedText != null) {
            processAADBytes(initialAssociatedText, 0, initialAssociatedText.length);
        }
    } else if (params instanceof ParametersWithIV) {
        ParametersWithIV param = (ParametersWithIV) params;
        byte[] iv = param.getIV();
        int diff = this.iv.length - iv.length;
        Arrays.fill(this.iv, (byte) 0);
        System.arraycopy(iv, 0, this.iv, diff, iv.length);
        initialAssociatedText = null;
        // Set default mac size
        macSize = blockSize;
        engineParam = (KeyParameter) param.getParameters();
    } else {
        throw new IllegalArgumentException("Invalid parameter passed");
    }
    // TODO Nonce re-use check (sample code from GCMBlockCipher)
    // if (forEncryption)
    // {
    // if (nonce != null && Arrays.areEqual(nonce, newNonce))
    // {
    // if (keyParam == null)
    // {
    // throw new IllegalArgumentException("cannot reuse nonce for GCM encryption");
    // }
    // if (lastKey != null && Arrays.areEqual(lastKey, keyParam.getKey()))
    // {
    // throw new IllegalArgumentException("cannot reuse nonce for GCM encryption");
    // }
    // }
    // }
    this.macBlock = new byte[blockSize];
    ctrEngine.init(true, new ParametersWithIV(engineParam, this.iv));
    engine.init(true, engineParam);
}
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 8 with KeyParameter

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

the class OCBBlockCipher method init.

public void init(boolean forEncryption, CipherParameters parameters) throws IllegalArgumentException {
    boolean oldForEncryption = this.forEncryption;
    this.forEncryption = forEncryption;
    this.macBlock = null;
    KeyParameter keyParameter;
    byte[] N;
    if (parameters instanceof AEADParameters) {
        AEADParameters aeadParameters = (AEADParameters) parameters;
        N = aeadParameters.getNonce();
        initialAssociatedText = aeadParameters.getAssociatedText();
        int macSizeBits = aeadParameters.getMacSize();
        if (macSizeBits < 64 || macSizeBits > 128 || macSizeBits % 8 != 0) {
            throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
        }
        macSize = macSizeBits / 8;
        keyParameter = aeadParameters.getKey();
    } else if (parameters instanceof ParametersWithIV) {
        ParametersWithIV parametersWithIV = (ParametersWithIV) parameters;
        N = parametersWithIV.getIV();
        initialAssociatedText = null;
        macSize = 16;
        keyParameter = (KeyParameter) parametersWithIV.getParameters();
    } else {
        throw new IllegalArgumentException("invalid parameters passed to OCB");
    }
    this.hashBlock = new byte[16];
    this.mainBlock = new byte[forEncryption ? BLOCK_SIZE : (BLOCK_SIZE + macSize)];
    if (N == null) {
        N = new byte[0];
    }
    if (N.length > 15) {
        throw new IllegalArgumentException("IV must be no more than 15 bytes");
    }
    if (keyParameter != null) {
        // hashCipher always used in forward mode
        hashCipher.init(true, keyParameter);
        mainCipher.init(forEncryption, keyParameter);
        KtopInput = null;
    } else if (oldForEncryption != forEncryption) {
        throw new IllegalArgumentException("cannot change encrypting state without providing key.");
    }
    this.L_Asterisk = new byte[16];
    hashCipher.processBlock(L_Asterisk, 0, L_Asterisk, 0);
    this.L_Dollar = OCB_double(L_Asterisk);
    this.L = new Vector();
    this.L.addElement(OCB_double(L_Dollar));
    /*
     * NONCE-DEPENDENT AND PER-ENCRYPTION/DECRYPTION INITIALISATION
     */
    int bottom = processNonce(N);
    int bits = bottom % 8, bytes = bottom / 8;
    if (bits == 0) {
        System.arraycopy(Stretch, bytes, OffsetMAIN_0, 0, 16);
    } else {
        for (int i = 0; i < 16; ++i) {
            int b1 = Stretch[bytes] & 0xff;
            int b2 = Stretch[++bytes] & 0xff;
            this.OffsetMAIN_0[i] = (byte) ((b1 << bits) | (b2 >>> (8 - bits)));
        }
    }
    this.hashBlockPos = 0;
    this.mainBlockPos = 0;
    this.hashBlockCount = 0;
    this.mainBlockCount = 0;
    this.OffsetHASH = new byte[16];
    this.Sum = new byte[16];
    System.arraycopy(this.OffsetMAIN_0, 0, this.OffsetMAIN, 0, 16);
    this.Checksum = new byte[16];
    if (initialAssociatedText != null) {
        processAADBytes(initialAssociatedText, 0, initialAssociatedText.length);
    }
}
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) Vector(java.util.Vector)

Example 9 with KeyParameter

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

the class ChaCha20Poly1305 method init.

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    KeyParameter initKeyParam;
    byte[] initNonce;
    CipherParameters chacha20Params;
    if (params instanceof AEADParameters) {
        AEADParameters aeadParams = (AEADParameters) params;
        int macSizeBits = aeadParams.getMacSize();
        if ((MAC_SIZE * 8) != macSizeBits) {
            throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
        }
        initKeyParam = aeadParams.getKey();
        initNonce = aeadParams.getNonce();
        chacha20Params = new ParametersWithIV(initKeyParam, initNonce);
        this.initialAAD = aeadParams.getAssociatedText();
    } else if (params instanceof ParametersWithIV) {
        ParametersWithIV ivParams = (ParametersWithIV) params;
        initKeyParam = (KeyParameter) ivParams.getParameters();
        initNonce = ivParams.getIV();
        chacha20Params = ivParams;
        this.initialAAD = null;
    } else {
        throw new IllegalArgumentException("invalid parameters passed to ChaCha20Poly1305");
    }
    // Validate key
    if (null == initKeyParam) {
        if (State.UNINITIALIZED == state) {
            throw new IllegalArgumentException("Key must be specified in initial init");
        }
    } else {
        if (KEY_SIZE != initKeyParam.getKey().length) {
            throw new IllegalArgumentException("Key must be 256 bits");
        }
    }
    // Validate nonce
    if (null == initNonce || NONCE_SIZE != initNonce.length) {
        throw new IllegalArgumentException("Nonce must be 96 bits");
    }
    // Check for encryption with reused nonce
    if (State.UNINITIALIZED != state && forEncryption && Arrays.areEqual(nonce, initNonce)) {
        if (null == initKeyParam || Arrays.areEqual(key, initKeyParam.getKey())) {
            throw new IllegalArgumentException("cannot reuse nonce for ChaCha20Poly1305 encryption");
        }
    }
    if (null != initKeyParam) {
        System.arraycopy(initKeyParam.getKey(), 0, key, 0, KEY_SIZE);
    }
    System.arraycopy(initNonce, 0, nonce, 0, NONCE_SIZE);
    chacha20.init(true, chacha20Params);
    this.state = forEncryption ? State.ENC_INIT : State.DEC_INIT;
    reset(true, false);
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) 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 10 with KeyParameter

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

the class GCFBBlockCipher method calculateByte.

protected byte calculateByte(byte b) {
    if (counter > 0 && counter % 1024 == 0) {
        BlockCipher base = cfbEngine.getUnderlyingCipher();
        base.init(false, key);
        byte[] nextKey = new byte[32];
        base.processBlock(C, 0, nextKey, 0);
        base.processBlock(C, 8, nextKey, 8);
        base.processBlock(C, 16, nextKey, 16);
        base.processBlock(C, 24, nextKey, 24);
        key = new KeyParameter(nextKey);
        base.init(true, key);
        byte[] iv = cfbEngine.getCurrentIV();
        base.processBlock(iv, 0, iv, 0);
        cfbEngine.init(forEncryption, new ParametersWithIV(key, iv));
    }
    counter++;
    return cfbEngine.calculateByte(b);
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) BlockCipher(com.github.zhenwei.core.crypto.BlockCipher) StreamBlockCipher(com.github.zhenwei.core.crypto.StreamBlockCipher) 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