package org.bouncycastle.crypto.engines;

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;

/* JADX INFO: loaded from: classes2.dex */
public class RC6Engine implements BlockCipher {
    private static final int LGW = 5;
    private static final int P32 = -1209970333;
    private static final int Q32 = -1640531527;
    private static final int _noRounds = 20;
    private static final int bytesPerWord = 4;
    private static final int wordSize = 32;
    private int[] _S = null;
    private boolean forEncryption;

    private int bytesToWord(byte[] bArr, int i) {
        int i2 = 0;
        for (int i3 = 3; i3 >= 0; i3--) {
            i2 = (i2 << 8) + (bArr[i3 + i] & 255);
        }
        return i2;
    }

    private int decryptBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
        int iBytesToWord = bytesToWord(bArr, i);
        int iBytesToWord2 = bytesToWord(bArr, i + 4);
        int iBytesToWord3 = bytesToWord(bArr, i + 8);
        int iBytesToWord4 = bytesToWord(bArr, i + 12);
        int i3 = iBytesToWord3 - this._S[43];
        int iRotateRight = iBytesToWord - this._S[42];
        int i4 = 20;
        while (i4 >= 1) {
            int iRotateLeft = rotateLeft(((iRotateRight * 2) + 1) * iRotateRight, 5);
            int iRotateLeft2 = rotateLeft(((i3 * 2) + 1) * i3, 5);
            int i5 = i4 * 2;
            int iRotateRight2 = rotateRight(iBytesToWord2 - this._S[i5 + 1], iRotateLeft) ^ iRotateLeft2;
            i4--;
            int i6 = iRotateRight;
            iRotateRight = rotateRight(iBytesToWord4 - this._S[i5], iRotateLeft2) ^ iRotateLeft;
            iBytesToWord4 = i3;
            i3 = iRotateRight2;
            iBytesToWord2 = i6;
        }
        int i7 = iBytesToWord4 - this._S[1];
        int i8 = iBytesToWord2 - this._S[0];
        wordToBytes(iRotateRight, bArr2, i2);
        wordToBytes(i8, bArr2, i2 + 4);
        wordToBytes(i3, bArr2, i2 + 8);
        wordToBytes(i7, bArr2, i2 + 12);
        return 16;
    }

    private int encryptBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
        int iBytesToWord = bytesToWord(bArr, i);
        int iBytesToWord2 = bytesToWord(bArr, i + 4);
        int iBytesToWord3 = bytesToWord(bArr, i + 8);
        int iBytesToWord4 = bytesToWord(bArr, i + 12);
        int i3 = iBytesToWord2 + this._S[0];
        int i4 = iBytesToWord4 + this._S[1];
        int i5 = 1;
        while (i5 <= 20) {
            int iRotateLeft = rotateLeft(((i3 * 2) + 1) * i3, 5);
            int iRotateLeft2 = rotateLeft(((i4 * 2) + 1) * i4, 5);
            int i6 = i5 * 2;
            int iRotateLeft3 = rotateLeft(iBytesToWord ^ iRotateLeft, iRotateLeft2) + this._S[i6];
            int iRotateLeft4 = rotateLeft(iBytesToWord3 ^ iRotateLeft2, iRotateLeft) + this._S[i6 + 1];
            i5++;
            iBytesToWord3 = i4;
            i4 = iRotateLeft3;
            iBytesToWord = i3;
            i3 = iRotateLeft4;
        }
        int i7 = iBytesToWord + this._S[42];
        int i8 = iBytesToWord3 + this._S[43];
        wordToBytes(i7, bArr2, i2);
        wordToBytes(i3, bArr2, i2 + 4);
        wordToBytes(i8, bArr2, i2 + 8);
        wordToBytes(i4, bArr2, i2 + 12);
        return 16;
    }

    private int rotateLeft(int i, int i2) {
        return (i >>> (-i2)) | (i << i2);
    }

    private int rotateRight(int i, int i2) {
        return (i << (-i2)) | (i >>> i2);
    }

    private void setKey(byte[] bArr) {
        int[] iArr;
        int length = (bArr.length + 3) / 4;
        int length2 = ((bArr.length + 4) - 1) / 4;
        int[] iArr2 = new int[length2];
        for (int length3 = bArr.length - 1; length3 >= 0; length3--) {
            int i = length3 / 4;
            iArr2[i] = (iArr2[i] << 8) + (bArr[length3] & 255);
        }
        this._S = new int[44];
        this._S[0] = P32;
        int i2 = 1;
        while (true) {
            int length4 = this._S.length;
            iArr = this._S;
            if (i2 >= length4) {
                break;
            }
            iArr[i2] = this._S[i2 - 1] + Q32;
            i2++;
        }
        int length5 = length2 > iArr.length ? length2 * 3 : this._S.length * 3;
        int length6 = 0;
        int iRotateLeft = 0;
        int iRotateLeft2 = 0;
        int i3 = 0;
        for (int i4 = 0; i4 < length5; i4++) {
            int[] iArr3 = this._S;
            iRotateLeft = rotateLeft(this._S[length6] + iRotateLeft + iRotateLeft2, 3);
            iArr3[length6] = iRotateLeft;
            iRotateLeft2 = rotateLeft(iArr2[i3] + iRotateLeft + iRotateLeft2, iRotateLeft2 + iRotateLeft);
            iArr2[i3] = iRotateLeft2;
            length6 = (length6 + 1) % this._S.length;
            i3 = (i3 + 1) % length2;
        }
    }

    private void wordToBytes(int i, byte[] bArr, int i2) {
        for (int i3 = 0; i3 < 4; i3++) {
            bArr[i3 + i2] = (byte) i;
            i >>>= 8;
        }
    }

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

    @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 RC6 init - " + cipherParameters.getClass().getName());
        }
        this.forEncryption = z;
        byte[] key = ((KeyParameter) cipherParameters).getKey();
        setKey(key);
        CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(getAlgorithmName(), key.length * 8, cipherParameters, Utils.getPurpose(z)));
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int processBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
        int blockSize = getBlockSize();
        if (this._S == null) {
            throw new IllegalStateException("RC6 engine not initialised");
        }
        if (i + blockSize > bArr.length) {
            throw new DataLengthException("input buffer too short");
        }
        if (blockSize + i2 <= bArr2.length) {
            return this.forEncryption ? encryptBlock(bArr, i, bArr2, i2) : decryptBlock(bArr, i, bArr2, i2);
        }
        throw new OutputLengthException("output buffer too short");
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
    }
}
