package org.bouncycastle.crypto.engines;

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Pack;

/* JADX INFO: loaded from: classes2.dex */
public class Grain128AEADEngine extends AEADBaseEngine {
    private static final int STATE_SIZE = 4;
    private final int[] authAcc;
    private final int[] authSr;
    private final int[] lfsr;
    private final int[] nfsr;
    private byte[] workingIV;
    private byte[] workingKey;

    public Grain128AEADEngine() {
        this.algorithmName = "Grain-128 AEAD";
        this.KEY_SIZE = 16;
        this.IV_SIZE = 12;
        this.MAC_SIZE = 8;
        this.lfsr = new int[4];
        this.nfsr = new int[4];
        this.authAcc = new int[2];
        this.authSr = new int[2];
        setInnerMembers(AEADBaseEngine.ProcessingBufferType.Immediate, AEADBaseEngine.AADOperatorType.Stream, AEADBaseEngine.DataOperatorType.StreamCipher);
    }

    private void absorbAadData(byte[] bArr, int i, int i2) {
        for (int i3 = 0; i3 < i2; i3++) {
            byte b = bArr[i + i3];
            for (int i4 = 0; i4 < 8; i4++) {
                shift();
                updateInternalState((b >> i4) & 1);
            }
        }
    }

    private int getByteKeyStream() {
        int output = getOutput();
        shift();
        return output;
    }

    private int getOutput() {
        int i = this.nfsr[0] >>> 2;
        int i2 = this.nfsr[0] >>> 12;
        int i3 = this.nfsr[0] >>> 15;
        int i4 = this.nfsr[1] >>> 4;
        int i5 = this.nfsr[1] >>> 13;
        int i6 = this.nfsr[2];
        int i7 = this.nfsr[2] >>> 9;
        int i8 = this.nfsr[2] >>> 25;
        int i9 = this.nfsr[2] >>> 31;
        int i10 = this.lfsr[0] >>> 8;
        int i11 = this.lfsr[0] >>> 13;
        int i12 = this.lfsr[0] >>> 20;
        int i13 = this.lfsr[1] >>> 10;
        int i14 = this.lfsr[1] >>> 28;
        int i15 = this.lfsr[2] >>> 15;
        return (((((((((((this.lfsr[2] >>> 30) & (i2 & i9)) ^ ((((i12 & i11) ^ (i10 & i2)) ^ (i9 & i13)) ^ (i14 & i15))) ^ (this.lfsr[2] >>> 29)) ^ i) ^ i3) ^ i4) ^ i5) ^ i6) ^ i7) ^ i8) & 1;
    }

    private int getOutputLFSR() {
        int i = this.lfsr[0];
        int i2 = this.lfsr[0] >>> 7;
        int i3 = this.lfsr[1] >>> 6;
        int i4 = this.lfsr[2] >>> 6;
        return (((((i ^ i2) ^ i3) ^ i4) ^ (this.lfsr[2] >>> 17)) ^ this.lfsr[3]) & 1;
    }

    private int getOutputNFSR() {
        int i = this.nfsr[0];
        int i2 = this.nfsr[0] >>> 3;
        int i3 = this.nfsr[0] >>> 11;
        int i4 = this.nfsr[0] >>> 13;
        int i5 = this.nfsr[0] >>> 17;
        int i6 = this.nfsr[0] >>> 18;
        int i7 = this.nfsr[0] >>> 22;
        int i8 = this.nfsr[0] >>> 24;
        int i9 = this.nfsr[0] >>> 25;
        int i10 = this.nfsr[0] >>> 26;
        int i11 = this.nfsr[0] >>> 27;
        int i12 = this.nfsr[1] >>> 8;
        int i13 = this.nfsr[1] >>> 16;
        int i14 = this.nfsr[1] >>> 24;
        int i15 = this.nfsr[1] >>> 27;
        int i16 = this.nfsr[1] >>> 29;
        int i17 = this.nfsr[2] >>> 1;
        int i18 = this.nfsr[2] >>> 3;
        int i19 = this.nfsr[2] >>> 4;
        int i20 = this.nfsr[2] >>> 6;
        int i21 = this.nfsr[2] >>> 14;
        int i22 = this.nfsr[2] >>> 18;
        int i23 = this.nfsr[2] >>> 20;
        int i24 = this.nfsr[2] >>> 24;
        int i25 = this.nfsr[2] >>> 27;
        int i26 = this.nfsr[2] >>> 28;
        int i27 = this.nfsr[2] >>> 29;
        return (((((((((((this.nfsr[3] ^ ((i14 ^ (i ^ i10)) ^ i25)) ^ (i2 & i18)) ^ (i3 & i4)) ^ (i5 & i6)) ^ (i11 & i15)) ^ (i12 & i13)) ^ (i16 & i17)) ^ (i19 & i23)) ^ ((i7 & i8) & i9)) ^ ((i20 & i21) & i22)) ^ (((i24 & i26) & i27) & (this.nfsr[2] >>> 31))) & 1;
    }

    private void initGrain(int[] iArr) {
        for (int i = 0; i < 2; i++) {
            for (int i2 = 0; i2 < 32; i2++) {
                iArr[i] = iArr[i] | (getByteKeyStream() << i2);
            }
        }
    }

    private void shift() {
        shift(this.nfsr, (getOutputNFSR() ^ this.lfsr[0]) & 1);
        shift(this.lfsr, getOutputLFSR() & 1);
    }

    private void shift(int[] iArr, int i) {
        iArr[0] = (iArr[0] >>> 1) | (iArr[1] << 31);
        iArr[1] = (iArr[1] >>> 1) | (iArr[2] << 31);
        iArr[2] = (iArr[2] >>> 1) | (iArr[3] << 31);
        iArr[3] = (i << 31) | (iArr[3] >>> 1);
    }

    private void updateInternalState(int i) {
        int i2 = -i;
        int[] iArr = this.authAcc;
        iArr[0] = iArr[0] ^ (this.authSr[0] & i2);
        int[] iArr2 = this.authAcc;
        iArr2[1] = (i2 & this.authSr[1]) ^ iArr2[1];
        int byteKeyStream = getByteKeyStream();
        this.authSr[0] = (this.authSr[0] >>> 1) | (this.authSr[1] << 31);
        this.authSr[1] = (byteKeyStream << 31) | (this.authSr[1] >>> 1);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public /* bridge */ /* synthetic */ int doFinal(byte[] bArr, int i) throws InvalidCipherTextException, IllegalStateException {
        return super.doFinal(bArr, i);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    protected void finishAAD(AEADBaseEngine.State state, boolean z) {
        finishAAD1(state);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public /* bridge */ /* synthetic */ String getAlgorithmName() {
        return super.getAlgorithmName();
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    public /* bridge */ /* synthetic */ int getIVBytesSize() {
        return super.getIVBytesSize();
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    public /* bridge */ /* synthetic */ int getKeyBytesSize() {
        return super.getKeyBytesSize();
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public /* bridge */ /* synthetic */ byte[] getMac() {
        return super.getMac();
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public /* bridge */ /* synthetic */ int getOutputSize(int i) {
        return super.getOutputSize(i);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public int getUpdateOutputSize(int i) {
        return getTotalBytesForUpdate(i);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public /* bridge */ /* synthetic */ void init(boolean z, CipherParameters cipherParameters) {
        super.init(z, cipherParameters);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    protected void init(byte[] bArr, byte[] bArr2) throws IllegalArgumentException {
        this.workingIV = new byte[16];
        this.workingKey = bArr;
        System.arraycopy(bArr2, 0, this.workingIV, 0, this.IV_SIZE);
        this.workingIV[12] = -1;
        this.workingIV[13] = -1;
        this.workingIV[14] = -1;
        this.workingIV[15] = 127;
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public /* bridge */ /* synthetic */ void processAADByte(byte b) {
        super.processAADByte(b);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public /* bridge */ /* synthetic */ void processAADBytes(byte[] bArr, int i, int i2) {
        super.processAADBytes(bArr, i, i2);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    protected void processBufferAAD(byte[] bArr, int i) {
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    protected void processBufferDecrypt(byte[] bArr, int i, byte[] bArr2, int i2) {
        int len = this.dataOperator.getLen();
        for (int i3 = 0; i3 < len; i3++) {
            byte b = bArr[i + i3];
            byte byteKeyStream = 0;
            for (int i4 = 0; i4 < 8; i4++) {
                byteKeyStream = (byte) (byteKeyStream | ((((b >> i4) & 1) ^ getByteKeyStream()) << i4));
                updateInternalState((byteKeyStream >> i4) & 1);
            }
            bArr2[i2 + i3] = byteKeyStream;
        }
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    protected void processBufferEncrypt(byte[] bArr, int i, byte[] bArr2, int i2) {
        int len = this.dataOperator.getLen();
        for (int i3 = 0; i3 < len; i3++) {
            byte b = bArr[i + i3];
            byte byteKeyStream = 0;
            for (int i4 = 0; i4 < 8; i4++) {
                int i5 = (b >> i4) & 1;
                byteKeyStream = (byte) (byteKeyStream | ((getByteKeyStream() ^ i5) << i4));
                updateInternalState(i5);
            }
            bArr2[i2 + i3] = byteKeyStream;
        }
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public /* bridge */ /* synthetic */ int processByte(byte b, byte[] bArr, int i) throws DataLengthException {
        return super.processByte(b, bArr, i);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public /* bridge */ /* synthetic */ int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws DataLengthException {
        return super.processBytes(bArr, i, i2, bArr2, i3);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    protected void processFinalAAD() {
        int i;
        int len = this.aadOperator.getLen();
        byte[] bytes = ((AEADBaseEngine.StreamAADOperator) this.aadOperator).getBytes();
        int i2 = 5;
        byte[] bArr = new byte[5];
        if (len < 128) {
            i = 4;
            bArr[4] = (byte) len;
        } else {
            int i3 = len;
            do {
                i2--;
                bArr[i2] = (byte) i3;
                i3 >>>= 8;
            } while (i3 != 0);
            int i4 = 5 - i2;
            int i5 = i2 - 1;
            bArr[i5] = (byte) (128 | i4);
            i = i5;
        }
        absorbAadData(bArr, i, 5 - i);
        absorbAadData(bytes, 0, len);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    protected void processFinalBlock(byte[] bArr, int i) {
        int[] iArr = this.authAcc;
        iArr[0] = iArr[0] ^ this.authSr[0];
        int[] iArr2 = this.authAcc;
        iArr2[1] = iArr2[1] ^ this.authSr[1];
        Pack.intToLittleEndian(this.authAcc, this.mac, 0);
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine, org.bouncycastle.crypto.modes.AEADCipher
    public /* bridge */ /* synthetic */ void reset() {
        super.reset();
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    protected void reset(boolean z) {
        super.reset(z);
        Pack.littleEndianToInt(this.workingKey, 0, this.nfsr);
        Pack.littleEndianToInt(this.workingIV, 0, this.lfsr);
        Arrays.clear(this.authAcc);
        Arrays.clear(this.authSr);
        for (int i = 0; i < 320; i++) {
            int output = getOutput();
            shift(this.nfsr, ((getOutputNFSR() ^ this.lfsr[0]) ^ output) & 1);
            shift(this.lfsr, (output ^ getOutputLFSR()) & 1);
        }
        for (int i2 = 0; i2 < 8; i2++) {
            for (int i3 = 0; i3 < 8; i3++) {
                int output2 = getOutput();
                shift(this.nfsr, (((getOutputNFSR() ^ this.lfsr[0]) ^ output2) ^ (this.workingKey[i2] >> i3)) & 1);
                shift(this.lfsr, ((output2 ^ getOutputLFSR()) ^ (this.workingKey[i2 + 8] >> i3)) & 1);
            }
        }
        initGrain(this.authAcc);
        initGrain(this.authSr);
    }
}
