Search in sources :

Example 6 with BlockCipher

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

the class NISTCTSBlockCipher method doFinal.

/**
 * Process the last block in the buffer.
 *
 * @param out the array the block currently being held is copied into.
 * @param outOff the offset at which the copying starts.
 * @return the number of output bytes copied to out.
 * @exception com.github.zhenwei.core.crypto.DataLengthException if there is insufficient space in out for
 * the output.
 * @exception IllegalStateException if the underlying cipher is not
 * initialised.
 * @exception com.github.zhenwei.core.crypto.InvalidCipherTextException if cipher text decrypts wrongly (in
 * case the exception will never get thrown).
 */
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    if (bufOff + outOff > out.length) {
        throw new OutputLengthException("output buffer to small in doFinal");
    }
    int blockSize = cipher.getBlockSize();
    int len = bufOff - blockSize;
    byte[] block = new byte[blockSize];
    if (forEncryption) {
        if (bufOff < blockSize) {
            throw new DataLengthException("need at least one block of input for NISTCTS");
        }
        if (bufOff > blockSize) {
            byte[] lastBlock = new byte[blockSize];
            if (this.type == CS2 || this.type == CS3) {
                cipher.processBlock(buf, 0, block, 0);
                System.arraycopy(buf, blockSize, lastBlock, 0, len);
                cipher.processBlock(lastBlock, 0, lastBlock, 0);
                if (this.type == CS2 && len == blockSize) {
                    System.arraycopy(block, 0, out, outOff, blockSize);
                    System.arraycopy(lastBlock, 0, out, outOff + blockSize, len);
                } else {
                    System.arraycopy(lastBlock, 0, out, outOff, blockSize);
                    System.arraycopy(block, 0, out, outOff + blockSize, len);
                }
            } else {
                System.arraycopy(buf, 0, block, 0, blockSize);
                cipher.processBlock(block, 0, block, 0);
                System.arraycopy(block, 0, out, outOff, len);
                System.arraycopy(buf, bufOff - len, lastBlock, 0, len);
                cipher.processBlock(lastBlock, 0, lastBlock, 0);
                System.arraycopy(lastBlock, 0, out, outOff + len, blockSize);
            }
        } else {
            cipher.processBlock(buf, 0, block, 0);
            System.arraycopy(block, 0, out, outOff, blockSize);
        }
    } else {
        if (bufOff < blockSize) {
            throw new DataLengthException("need at least one block of input for CTS");
        }
        byte[] lastBlock = new byte[blockSize];
        if (bufOff > blockSize) {
            if (this.type == CS3 || (this.type == CS2 && ((buf.length - bufOff) % blockSize) != 0)) {
                if (cipher instanceof CBCBlockCipher) {
                    BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
                    c.processBlock(buf, 0, block, 0);
                } else {
                    cipher.processBlock(buf, 0, block, 0);
                }
                for (int i = blockSize; i != bufOff; i++) {
                    lastBlock[i - blockSize] = (byte) (block[i - blockSize] ^ buf[i]);
                }
                System.arraycopy(buf, blockSize, block, 0, len);
                cipher.processBlock(block, 0, out, outOff);
                System.arraycopy(lastBlock, 0, out, outOff + blockSize, len);
            } else {
                BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
                c.processBlock(buf, bufOff - blockSize, lastBlock, 0);
                System.arraycopy(buf, 0, block, 0, blockSize);
                if (len != blockSize) {
                    System.arraycopy(lastBlock, len, block, len, blockSize - len);
                }
                cipher.processBlock(block, 0, block, 0);
                System.arraycopy(block, 0, out, outOff, blockSize);
                for (int i = 0; i != len; i++) {
                    lastBlock[i] ^= buf[i];
                }
                System.arraycopy(lastBlock, 0, out, outOff + blockSize, len);
            }
        } else {
            cipher.processBlock(buf, 0, block, 0);
            System.arraycopy(block, 0, out, outOff, blockSize);
        }
    }
    int offset = bufOff;
    reset();
    return offset;
}
Also used : BufferedBlockCipher(com.github.zhenwei.core.crypto.BufferedBlockCipher) BlockCipher(com.github.zhenwei.core.crypto.BlockCipher) DataLengthException(com.github.zhenwei.core.crypto.DataLengthException) OutputLengthException(com.github.zhenwei.core.crypto.OutputLengthException)

Example 7 with BlockCipher

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

the class OldCTSBlockCipher method doFinal.

/**
 * Process the last block in the buffer.
 *
 * @param out    the array the block currently being held is copied into.
 * @param outOff the offset at which the copying starts.
 * @return the number of output bytes copied to out.
 * @throws com.github.zhenwei.core.crypto.DataLengthException        if there is insufficient
 *                                                                   space in out for the output.
 * @throws IllegalStateException                                     if the underlying cipher is
 *                                                                   not initialised.
 * @throws com.github.zhenwei.core.crypto.InvalidCipherTextException if cipher text decrypts
 *                                                                   wrongly (in case the
 *                                                                   exception will never get
 *                                                                   thrown).
 */
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    if (bufOff + outOff > out.length) {
        throw new OutputLengthException("output buffer to small in doFinal");
    }
    int blockSize = cipher.getBlockSize();
    int len = bufOff - blockSize;
    byte[] block = new byte[blockSize];
    if (forEncryption) {
        cipher.processBlock(buf, 0, block, 0);
        if (bufOff < blockSize) {
            throw new DataLengthException("need at least one block of input for CTS");
        }
        for (int i = bufOff; i != buf.length; i++) {
            buf[i] = block[i - blockSize];
        }
        for (int i = blockSize; i != bufOff; i++) {
            buf[i] ^= block[i - blockSize];
        }
        if (cipher instanceof CBCBlockCipher) {
            BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
            c.processBlock(buf, blockSize, out, outOff);
        } else {
            cipher.processBlock(buf, blockSize, out, outOff);
        }
        System.arraycopy(block, 0, out, outOff + blockSize, len);
    } else {
        byte[] lastBlock = new byte[blockSize];
        if (cipher instanceof CBCBlockCipher) {
            BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
            c.processBlock(buf, 0, block, 0);
        } else {
            cipher.processBlock(buf, 0, block, 0);
        }
        for (int i = blockSize; i != bufOff; i++) {
            lastBlock[i - blockSize] = (byte) (block[i - blockSize] ^ buf[i]);
        }
        System.arraycopy(buf, blockSize, block, 0, len);
        cipher.processBlock(block, 0, out, outOff);
        System.arraycopy(lastBlock, 0, out, outOff + blockSize, len);
    }
    int offset = bufOff;
    reset();
    return offset;
}
Also used : BufferedBlockCipher(com.github.zhenwei.core.crypto.BufferedBlockCipher) BlockCipher(com.github.zhenwei.core.crypto.BlockCipher) DataLengthException(com.github.zhenwei.core.crypto.DataLengthException) OutputLengthException(com.github.zhenwei.core.crypto.OutputLengthException)

Aggregations

BlockCipher (com.github.zhenwei.core.crypto.BlockCipher)7 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)4 OutputLengthException (com.github.zhenwei.core.crypto.OutputLengthException)4 DataLengthException (com.github.zhenwei.core.crypto.DataLengthException)3 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)3 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)3 StreamBlockCipher (com.github.zhenwei.core.crypto.StreamBlockCipher)2 AESEngine (com.github.zhenwei.core.crypto.engines.AESEngine)2 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)1 BlowfishEngine (com.github.zhenwei.core.crypto.engines.BlowfishEngine)1 DESEngine (com.github.zhenwei.core.crypto.engines.DESEngine)1 DESedeEngine (com.github.zhenwei.core.crypto.engines.DESedeEngine)1 RC2Engine (com.github.zhenwei.core.crypto.engines.RC2Engine)1 SM4Engine (com.github.zhenwei.core.crypto.engines.SM4Engine)1 CMac (com.github.zhenwei.core.crypto.macs.CMac)1 CBCBlockCipher (com.github.zhenwei.core.crypto.modes.CBCBlockCipher)1 CFBBlockCipher (com.github.zhenwei.core.crypto.modes.CFBBlockCipher)1 OFBBlockCipher (com.github.zhenwei.core.crypto.modes.OFBBlockCipher)1 BlockCipherPadding (com.github.zhenwei.core.crypto.paddings.BlockCipherPadding)1 PKCS7Padding (com.github.zhenwei.core.crypto.paddings.PKCS7Padding)1