package org.bouncycastle.crypto.engines;

import org.bouncycastle.bcpg.sig.RevocationKeyTags;
import org.bouncycastle.bcpg.sig.RevocationReasonTags;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.OutputLengthException;
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.signers.PSSSigner;
import org.bouncycastle.util.Integers;
import org.bouncycastle.util.Pack;

/* JADX INFO: loaded from: classes3.dex */
public final class TwofishEngine implements BlockCipher {
    private static final int BLOCK_SIZE = 16;
    private static final int GF256_FDBK = 361;
    private static final int GF256_FDBK_2 = 180;
    private static final int GF256_FDBK_4 = 90;
    private static final int INPUT_WHITEN = 0;
    private static final int MAX_KEY_BITS = 256;
    private static final int MAX_ROUNDS = 16;
    private static final int OUTPUT_WHITEN = 4;
    private static final byte[][] P = {new byte[]{-87, 103, -77, -24, 4, -3, -93, 118, -102, -110, RevocationKeyTags.CLASS_DEFAULT, 120, -28, -35, -47, 56, 13, -58, 53, -104, 24, -9, -20, 108, 67, 117, 55, 38, -6, 19, -108, 72, -14, -48, -117, 48, -124, 84, -33, 35, 25, 91, 61, 89, -13, -82, -94, -126, 99, 1, -125, 46, -39, 81, -101, 124, -90, -21, -91, -66, 22, 12, -29, 97, -64, -116, 58, -11, 115, 44, 37, 11, -69, 78, -119, 107, 83, 106, -76, -15, -31, -26, -67, 69, -30, -12, -74, 102, -52, -107, 3, 86, -44, 28, 30, -41, -5, -61, -114, -75, -23, -49, -65, -70, -22, 119, 57, -81, 51, -55, 98, 113, -127, 121, 9, -83, 36, -51, -7, -40, -27, -59, -71, 77, 68, 8, -122, -25, -95, 29, -86, -19, 6, 112, -78, -46, 65, 123, -96, 17, 49, -62, 39, -112, RevocationReasonTags.USER_NO_LONGER_VALID, -10, 96, -1, -106, 92, -79, -85, -98, -100, 82, 27, 95, -109, 10, -17, -111, -123, 73, -18, 45, 79, -113, 59, 71, -121, 109, 70, -42, 62, 105, 100, 42, -50, -53, 47, -4, -105, 5, 122, -84, 127, -43, 26, 75, 14, -89, 90, 40, 20, 63, 41, -120, 60, 76, 2, -72, -38, -80, 23, 85, 31, -118, 125, 87, -57, -115, 116, -73, -60, -97, 114, 126, 21, 34, 18, 88, 7, -103, 52, 110, 80, -34, 104, 101, PSSSigner.TRAILER_IMPLICIT, -37, -8, -56, -88, 43, RevocationKeyTags.CLASS_SENSITIVE, -36, -2, 50, -92, -54, 16, 33, -16, -45, 93, 15, 0, 111, -99, 54, 66, 74, 94, -63, -32}, new byte[]{117, -13, -58, -12, -37, 123, -5, -56, 74, -45, -26, 107, 69, 125, -24, 75, -42, 50, -40, -3, 55, 113, -15, -31, 48, 15, -8, 27, -121, -6, 6, 63, 94, -70, -82, 91, -118, 0, PSSSigner.TRAILER_IMPLICIT, -99, 109, -63, -79, 14, RevocationKeyTags.CLASS_DEFAULT, 93, -46, -43, -96, -124, 7, 20, -75, -112, 44, -93, -78, 115, 76, 84, -110, 116, 54, 81, 56, -80, -67, 90, -4, 96, 98, -106, 108, 66, -9, 16, 124, 40, 39, -116, 19, -107, -100, -57, 36, 70, 59, 112, -54, -29, -123, -53, 17, -48, -109, -72, -90, -125, RevocationReasonTags.USER_NO_LONGER_VALID, -1, -97, 119, -61, -52, 3, 111, 8, -65, RevocationKeyTags.CLASS_SENSITIVE, -25, 43, -30, 121, 12, -86, -126, 65, 58, -22, -71, -28, -102, -92, -105, 126, -38, 122, 23, 102, -108, -95, 29, 61, -16, -34, -77, 11, 114, -89, 28, -17, -47, 83, 62, -113, 51, 38, 95, -20, 118, 42, 73, -127, -120, -18, 33, -60, 26, -21, -39, -59, 57, -103, -51, -83, 49, -117, 1, 24, 35, -35, 31, 78, 45, -7, 72, 79, -14, 101, -114, 120, 92, 88, 25, -115, -27, -104, 87, 103, 127, 5, 100, -81, 99, -74, -2, -11, -73, 60, -91, -50, -23, 104, 68, -32, 77, 67, 105, 41, 46, -84, 21, 89, -88, 10, -98, 110, 71, -33, 52, 53, 106, -49, -36, 34, -55, -64, -101, -119, -44, -19, -85, 18, -94, 13, 82, -69, 2, 47, -87, -41, 97, 30, -76, 80, 4, -10, -62, 22, 37, -122, 86, 85, 9, -66, -111}};
    private static final int P_00 = 1;
    private static final int P_01 = 0;
    private static final int P_02 = 0;
    private static final int P_03 = 1;
    private static final int P_04 = 1;
    private static final int P_10 = 0;
    private static final int P_11 = 0;
    private static final int P_12 = 1;
    private static final int P_13 = 1;
    private static final int P_14 = 0;
    private static final int P_20 = 1;
    private static final int P_21 = 1;
    private static final int P_22 = 0;
    private static final int P_23 = 0;
    private static final int P_24 = 0;
    private static final int P_30 = 0;
    private static final int P_31 = 1;
    private static final int P_32 = 1;
    private static final int P_33 = 0;
    private static final int P_34 = 1;
    private static final int ROUNDS = 16;
    private static final int ROUND_SUBKEYS = 8;
    private static final int RS_GF_FDBK = 333;
    private static final int SK_BUMP = 16843009;
    private static final int SK_ROTL = 9;
    private static final int SK_STEP = 33686018;
    private static final int TOTAL_SUBKEYS = 40;
    private int[] gSBox;
    private int[] gSubKeys;
    private boolean encrypting = false;
    private int[] gMDS0 = new int[256];
    private int[] gMDS1 = new int[256];
    private int[] gMDS2 = new int[256];
    private int[] gMDS3 = new int[256];
    private int k64Cnt = 0;
    private byte[] workingKey = null;

    public TwofishEngine() {
        CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(getAlgorithmName(), 256));
        for (int i = 0; i < 256; i++) {
            int i2 = P[0][i] & 255;
            int iMx_X = Mx_X(i2) & 255;
            int iMx_Y = Mx_Y(i2) & 255;
            int i3 = P[1][i] & 255;
            int[] iArr = {i2, i3};
            int[] iArr2 = {iMx_X, Mx_X(i3) & 255};
            int[] iArr3 = {iMx_Y, Mx_Y(i3) & 255};
            this.gMDS0[i] = iArr[1] | (iArr2[1] << 8) | (iArr3[1] << 16) | (iArr3[1] << 24);
            this.gMDS1[i] = iArr3[0] | (iArr3[0] << 8) | (iArr2[0] << 16) | (iArr[0] << 24);
            this.gMDS2[i] = (iArr3[1] << 24) | iArr2[1] | (iArr3[1] << 8) | (iArr[1] << 16);
            this.gMDS3[i] = (iArr[0] << 8) | iArr2[0] | (iArr3[0] << 16) | (iArr2[0] << 24);
        }
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    private int F32(int i, int[] iArr) {
        int i2;
        int i3;
        int iB0 = b0(i);
        int iB1 = b1(i);
        int iB2 = b2(i);
        int iB3 = b3(i);
        int i4 = iArr[0];
        int i5 = iArr[1];
        int i6 = iArr[2];
        int i7 = iArr[3];
        switch (3 & this.k64Cnt) {
            case 0:
                iB0 = (P[1][iB0] & 255) ^ b0(i7);
                iB1 = (P[0][iB1] & 255) ^ b1(i7);
                iB2 = (P[0][iB2] & 255) ^ b2(i7);
                iB3 = (P[1][iB3] & 255) ^ b3(i7);
                iB0 = b0(i6) ^ (P[1][iB0] & 255);
                iB1 = b1(i6) ^ (P[1][iB1] & 255);
                iB2 = b2(i6) ^ (P[0][iB2] & 255);
                iB3 = (P[0][iB3] & 255) ^ b3(i6);
                i2 = (this.gMDS0[(P[0][(P[0][iB0] & 255) ^ b0(i5)] & 255) ^ b0(i4)] ^ this.gMDS1[(P[0][(P[1][iB1] & 255) ^ b1(i5)] & 255) ^ b1(i4)]) ^ this.gMDS2[(P[1][(P[0][iB2] & 255) ^ b2(i5)] & 255) ^ b2(i4)];
                i3 = this.gMDS3[(P[1][(P[1][iB3] & 255) ^ b3(i5)] & 255) ^ b3(i4)];
                return i2 ^ i3;
            case 1:
                i2 = (this.gMDS0[(P[0][iB0] & 255) ^ b0(i4)] ^ this.gMDS1[(P[0][iB1] & 255) ^ b1(i4)]) ^ this.gMDS2[(P[1][iB2] & 255) ^ b2(i4)];
                i3 = this.gMDS3[(P[1][iB3] & 255) ^ b3(i4)];
                return i2 ^ i3;
            case 2:
                i2 = (this.gMDS0[(P[0][(P[0][iB0] & 255) ^ b0(i5)] & 255) ^ b0(i4)] ^ this.gMDS1[(P[0][(P[1][iB1] & 255) ^ b1(i5)] & 255) ^ b1(i4)]) ^ this.gMDS2[(P[1][(P[0][iB2] & 255) ^ b2(i5)] & 255) ^ b2(i4)];
                i3 = this.gMDS3[(P[1][(P[1][iB3] & 255) ^ b3(i5)] & 255) ^ b3(i4)];
                return i2 ^ i3;
            case 3:
                iB0 = b0(i6) ^ (P[1][iB0] & 255);
                iB1 = b1(i6) ^ (P[1][iB1] & 255);
                iB2 = b2(i6) ^ (P[0][iB2] & 255);
                iB3 = (P[0][iB3] & 255) ^ b3(i6);
                i2 = (this.gMDS0[(P[0][(P[0][iB0] & 255) ^ b0(i5)] & 255) ^ b0(i4)] ^ this.gMDS1[(P[0][(P[1][iB1] & 255) ^ b1(i5)] & 255) ^ b1(i4)]) ^ this.gMDS2[(P[1][(P[0][iB2] & 255) ^ b2(i5)] & 255) ^ b2(i4)];
                i3 = this.gMDS3[(P[1][(P[1][iB3] & 255) ^ b3(i5)] & 255) ^ b3(i4)];
                return i2 ^ i3;
            default:
                return 0;
        }
    }

    private int Fe32_0(int i) {
        return this.gSBox[(((i >>> 24) & 255) * 2) + 513] ^ ((this.gSBox[((i & 255) * 2) + 0] ^ this.gSBox[(((i >>> 8) & 255) * 2) + 1]) ^ this.gSBox[(((i >>> 16) & 255) * 2) + 512]);
    }

    private int Fe32_3(int i) {
        return this.gSBox[(((i >>> 16) & 255) * 2) + 513] ^ ((this.gSBox[(((i >>> 24) & 255) * 2) + 0] ^ this.gSBox[((i & 255) * 2) + 1]) ^ this.gSBox[(((i >>> 8) & 255) * 2) + 512]);
    }

    private int LFSR1(int i) {
        return ((i & 1) != 0 ? GF256_FDBK_2 : 0) ^ (i >> 1);
    }

    private int LFSR2(int i) {
        return ((i >> 2) ^ ((i & 2) != 0 ? GF256_FDBK_2 : 0)) ^ ((i & 1) != 0 ? GF256_FDBK_4 : 0);
    }

    private int Mx_X(int i) {
        return i ^ LFSR2(i);
    }

    private int Mx_Y(int i) {
        return LFSR2(i) ^ (LFSR1(i) ^ i);
    }

    private int RS_MDS_Encode(int i, int i2) {
        for (int i3 = 0; i3 < 4; i3++) {
            i2 = RS_rem(i2);
        }
        int iRS_rem = i ^ i2;
        for (int i4 = 0; i4 < 4; i4++) {
            iRS_rem = RS_rem(iRS_rem);
        }
        return iRS_rem;
    }

    private int RS_rem(int i) {
        int i2 = (i >>> 24) & 255;
        int i3 = ((i2 << 1) ^ ((i2 & 128) != 0 ? RS_GF_FDBK : 0)) & 255;
        int i4 = ((i2 >>> 1) ^ ((i2 & 1) != 0 ? 166 : 0)) ^ i3;
        return ((((i << 8) ^ (i4 << 24)) ^ (i3 << 16)) ^ (i4 << 8)) ^ i2;
    }

    private int b0(int i) {
        return i & 255;
    }

    private int b1(int i) {
        return (i >>> 8) & 255;
    }

    private int b2(int i) {
        return (i >>> 16) & 255;
    }

    private int b3(int i) {
        return (i >>> 24) & 255;
    }

    private void decryptBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
        int iLittleEndianToInt = Pack.littleEndianToInt(bArr, i) ^ this.gSubKeys[4];
        int iLittleEndianToInt2 = Pack.littleEndianToInt(bArr, i + 4) ^ this.gSubKeys[5];
        int iLittleEndianToInt3 = Pack.littleEndianToInt(bArr, i + 8) ^ this.gSubKeys[6];
        int iLittleEndianToInt4 = Pack.littleEndianToInt(bArr, i + 12) ^ this.gSubKeys[7];
        int i3 = 39;
        int i4 = 0;
        while (i4 < 16) {
            int iFe32_0 = Fe32_0(iLittleEndianToInt);
            int iFe32_3 = Fe32_3(iLittleEndianToInt2);
            int i5 = i3 - 1;
            int i6 = iLittleEndianToInt4 ^ (((iFe32_3 * 2) + iFe32_0) + this.gSubKeys[i3]);
            int i7 = iFe32_0 + iFe32_3;
            int i8 = i5 - 1;
            iLittleEndianToInt3 = Integers.rotateLeft(iLittleEndianToInt3, 1) ^ (i7 + this.gSubKeys[i5]);
            iLittleEndianToInt4 = Integers.rotateRight(i6, 1);
            int iFe32_1 = Fe32_0(iLittleEndianToInt3);
            int iFe32_4 = Fe32_3(iLittleEndianToInt4);
            int i9 = i8 - 1;
            int i10 = iLittleEndianToInt2 ^ (((iFe32_4 * 2) + iFe32_1) + this.gSubKeys[i8]);
            iLittleEndianToInt = Integers.rotateLeft(iLittleEndianToInt, 1) ^ ((iFe32_1 + iFe32_4) + this.gSubKeys[i9]);
            iLittleEndianToInt2 = Integers.rotateRight(i10, 1);
            i4 += 2;
            i3 = i9 - 1;
        }
        Pack.intToLittleEndian(this.gSubKeys[0] ^ iLittleEndianToInt3, bArr2, i2);
        Pack.intToLittleEndian(iLittleEndianToInt4 ^ this.gSubKeys[1], bArr2, i2 + 4);
        Pack.intToLittleEndian(this.gSubKeys[2] ^ iLittleEndianToInt, bArr2, i2 + 8);
        Pack.intToLittleEndian(this.gSubKeys[3] ^ iLittleEndianToInt2, bArr2, i2 + 12);
    }

    private void encryptBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
        int i3 = 0;
        int iLittleEndianToInt = Pack.littleEndianToInt(bArr, i) ^ this.gSubKeys[0];
        int iLittleEndianToInt2 = Pack.littleEndianToInt(bArr, i + 4) ^ this.gSubKeys[1];
        int iLittleEndianToInt3 = Pack.littleEndianToInt(bArr, i + 8) ^ this.gSubKeys[2];
        int iLittleEndianToInt4 = Pack.littleEndianToInt(bArr, i + 12) ^ this.gSubKeys[3];
        int i4 = 8;
        while (i3 < 16) {
            int iFe32_0 = Fe32_0(iLittleEndianToInt);
            int iFe32_3 = Fe32_3(iLittleEndianToInt2);
            int i5 = i4 + 1;
            iLittleEndianToInt3 = Integers.rotateRight(iLittleEndianToInt3 ^ ((iFe32_0 + iFe32_3) + this.gSubKeys[i4]), 1);
            int i6 = iFe32_0 + (iFe32_3 * 2);
            int i7 = i5 + 1;
            iLittleEndianToInt4 = Integers.rotateLeft(iLittleEndianToInt4, 1) ^ (i6 + this.gSubKeys[i5]);
            int iFe32_1 = Fe32_0(iLittleEndianToInt3);
            int iFe32_4 = Fe32_3(iLittleEndianToInt4);
            int i8 = i7 + 1;
            iLittleEndianToInt = Integers.rotateRight(iLittleEndianToInt ^ ((iFe32_1 + iFe32_4) + this.gSubKeys[i7]), 1);
            iLittleEndianToInt2 = Integers.rotateLeft(iLittleEndianToInt2, 1) ^ ((iFe32_1 + (iFe32_4 * 2)) + this.gSubKeys[i8]);
            i3 += 2;
            i4 = i8 + 1;
        }
        Pack.intToLittleEndian(this.gSubKeys[4] ^ iLittleEndianToInt3, bArr2, i2);
        Pack.intToLittleEndian(iLittleEndianToInt4 ^ this.gSubKeys[5], bArr2, i2 + 4);
        Pack.intToLittleEndian(this.gSubKeys[6] ^ iLittleEndianToInt, bArr2, i2 + 8);
        Pack.intToLittleEndian(this.gSubKeys[7] ^ iLittleEndianToInt2, bArr2, i2 + 12);
    }

    private void setKey(byte[] bArr) {
        int iB0;
        int iB1;
        int iB2;
        int iB3;
        int iB4;
        int iB5;
        int iB6;
        int iB7;
        int[] iArr = new int[4];
        int[] iArr2 = new int[4];
        int[] iArr3 = new int[4];
        this.gSubKeys = new int[40];
        for (int i = 0; i < this.k64Cnt; i++) {
            int i2 = i * 8;
            iArr[i] = Pack.littleEndianToInt(bArr, i2);
            iArr2[i] = Pack.littleEndianToInt(bArr, i2 + 4);
            iArr3[(this.k64Cnt - 1) - i] = RS_MDS_Encode(iArr[i], iArr2[i]);
        }
        for (int i3 = 0; i3 < 20; i3++) {
            int i4 = SK_STEP * i3;
            int iF32 = F32(i4, iArr);
            int iRotateLeft = Integers.rotateLeft(F32(i4 + 16843009, iArr2), 8);
            int i5 = iF32 + iRotateLeft;
            int i6 = i3 * 2;
            this.gSubKeys[i6] = i5;
            int i7 = i5 + iRotateLeft;
            this.gSubKeys[i6 + 1] = (i7 >>> 23) | (i7 << 9);
        }
        int i8 = iArr3[0];
        int i9 = iArr3[1];
        int i10 = iArr3[2];
        int i11 = iArr3[3];
        this.gSBox = new int[1024];
        for (int i12 = 0; i12 < 256; i12++) {
            switch (this.k64Cnt & 3) {
                case 0:
                    iB0 = (P[1][i12] & 255) ^ b0(i11);
                    iB1 = (P[0][i12] & 255) ^ b1(i11);
                    iB2 = (P[0][i12] & 255) ^ b2(i11);
                    iB3 = (P[1][i12] & 255) ^ b3(i11);
                    iB4 = (P[1][iB0] & 255) ^ b0(i10);
                    iB5 = (P[1][iB1] & 255) ^ b1(i10);
                    iB6 = (P[0][iB2] & 255) ^ b2(i10);
                    iB7 = (P[0][iB3] & 255) ^ b3(i10);
                    int i13 = i12 * 2;
                    this.gSBox[i13] = this.gMDS0[(P[0][(P[0][iB4] & 255) ^ b0(i9)] & 255) ^ b0(i8)];
                    this.gSBox[i13 + 1] = this.gMDS1[(P[0][(P[1][iB5] & 255) ^ b1(i9)] & 255) ^ b1(i8)];
                    this.gSBox[i13 + 512] = this.gMDS2[(P[1][(P[0][iB6] & 255) ^ b2(i9)] & 255) ^ b2(i8)];
                    this.gSBox[i13 + 513] = this.gMDS3[(P[1][(P[1][iB7] & 255) ^ b3(i9)] & 255) ^ b3(i8)];
                    break;
                case 1:
                    int i14 = i12 * 2;
                    this.gSBox[i14] = this.gMDS0[(P[0][i12] & 255) ^ b0(i8)];
                    this.gSBox[i14 + 1] = this.gMDS1[(P[0][i12] & 255) ^ b1(i8)];
                    this.gSBox[i14 + 512] = this.gMDS2[(P[1][i12] & 255) ^ b2(i8)];
                    this.gSBox[i14 + 513] = this.gMDS3[(P[1][i12] & 255) ^ b3(i8)];
                    break;
                case 2:
                    iB4 = i12;
                    iB5 = iB4;
                    iB6 = iB5;
                    iB7 = iB6;
                    int i15 = i12 * 2;
                    this.gSBox[i15] = this.gMDS0[(P[0][(P[0][iB4] & 255) ^ b0(i9)] & 255) ^ b0(i8)];
                    this.gSBox[i15 + 1] = this.gMDS1[(P[0][(P[1][iB5] & 255) ^ b1(i9)] & 255) ^ b1(i8)];
                    this.gSBox[i15 + 512] = this.gMDS2[(P[1][(P[0][iB6] & 255) ^ b2(i9)] & 255) ^ b2(i8)];
                    this.gSBox[i15 + 513] = this.gMDS3[(P[1][(P[1][iB7] & 255) ^ b3(i9)] & 255) ^ b3(i8)];
                    break;
                case 3:
                    iB0 = i12;
                    iB1 = iB0;
                    iB2 = iB1;
                    iB3 = iB2;
                    iB4 = (P[1][iB0] & 255) ^ b0(i10);
                    iB5 = (P[1][iB1] & 255) ^ b1(i10);
                    iB6 = (P[0][iB2] & 255) ^ b2(i10);
                    iB7 = (P[0][iB3] & 255) ^ b3(i10);
                    int i16 = i12 * 2;
                    this.gSBox[i16] = this.gMDS0[(P[0][(P[0][iB4] & 255) ^ b0(i9)] & 255) ^ b0(i8)];
                    this.gSBox[i16 + 1] = this.gMDS1[(P[0][(P[1][iB5] & 255) ^ b1(i9)] & 255) ^ b1(i8)];
                    this.gSBox[i16 + 512] = this.gMDS2[(P[1][(P[0][iB6] & 255) ^ b2(i9)] & 255) ^ b2(i8)];
                    this.gSBox[i16 + 513] = this.gMDS3[(P[1][(P[1][iB7] & 255) ^ b3(i9)] & 255) ^ b3(i8)];
                    break;
            }
        }
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public String getAlgorithmName() {
        return "Twofish";
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int getBlockSize() {
        return 16;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void init(boolean z, CipherParameters cipherParameters) {
        if (!(cipherParameters instanceof KeyParameter)) {
            throw new IllegalArgumentException("invalid parameter passed to Twofish init - " + cipherParameters.getClass().getName());
        }
        this.encrypting = z;
        this.workingKey = ((KeyParameter) cipherParameters).getKey();
        int length = this.workingKey.length * 8;
        switch (length) {
            case 128:
            case 192:
            case 256:
                CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(getAlgorithmName(), length, cipherParameters, Utils.getPurpose(z)));
                this.k64Cnt = this.workingKey.length / 8;
                setKey(this.workingKey);
                return;
            default:
                throw new IllegalArgumentException("Key length not 128/192/256 bits.");
        }
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int processBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
        if (this.workingKey == null) {
            throw new IllegalStateException("Twofish not initialised");
        }
        if (i + 16 > bArr.length) {
            throw new DataLengthException("input buffer too short");
        }
        if (i2 + 16 > bArr2.length) {
            throw new OutputLengthException("output buffer too short");
        }
        if (this.encrypting) {
            encryptBlock(bArr, i, bArr2, i2);
            return 16;
        }
        decryptBlock(bArr, i, bArr2, i2);
        return 16;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        if (this.workingKey != null) {
            setKey(this.workingKey);
        }
    }
}
