package org.bouncycastle.crypto.digests;

import org.bouncycastle.asn1.BERTags;
import org.bouncycastle.asn1.eac.CertificateBody;
import org.bouncycastle.asn1.x509.DisplayText;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.SecretKeyPacket;
import org.bouncycastle.bcpg.sig.RevocationKeyTags;
import org.bouncycastle.crypto.CryptoServicePurpose;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.crypto.ExtendedDigest;
import org.bouncycastle.math.Primes;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Memoable;
import org.bouncycastle.util.Pack;

/* JADX INFO: loaded from: classes2.dex */
public final class WhirlpoolDigest implements ExtendedDigest, Memoable {
    private static final int BITCOUNT_ARRAY_SIZE = 32;
    private static final int BYTE_LENGTH = 64;
    private static final int DIGEST_LENGTH_BYTES = 64;
    private static final int REDUCTION_POLYNOMIAL = 285;
    private static final int ROUNDS = 10;
    private long[] _K;
    private long[] _L;
    private short[] _bitCount;
    private long[] _block;
    private byte[] _buffer;
    private int _bufferPos;
    private long[] _hash;
    private final long[] _rc;
    private long[] _state;
    private final CryptoServicePurpose purpose;
    private static final int[] SBOX = {24, 35, 198, 232, 135, 184, 1, 79, 54, 166, 210, 245, 121, 111, 145, 82, 96, 188, 155, 142, 163, 12, 123, 53, 29, BERTags.FLAGS, 215, 194, 46, 75, SecretKeyPacket.USAGE_SHA1, 87, 21, 119, 55, 229, 159, 240, 74, 218, 88, 201, 41, 10, 177, 160, PublicKeyAlgorithmTags.EXPERIMENTAL_8, 133, 189, 93, 16, 244, 203, 62, 5, PublicKeyAlgorithmTags.EXPERIMENTAL_4, 228, 39, 65, 139, 167, 125, 149, 216, 251, 238, 124, PublicKeyAlgorithmTags.EXPERIMENTAL_3, 221, 23, 71, 158, 202, 45, 191, 7, 173, 90, 131, 51, 99, 2, 170, 113, DisplayText.DISPLAY_TEXT_MAXIMUM_SIZE, 25, 73, 217, 242, 227, 91, 136, 154, 38, 50, 176, 233, 15, 213, 128, 190, 205, 52, 72, 255, 122, 144, 95, 32, PublicKeyAlgorithmTags.EXPERIMENTAL_5, 26, 174, 180, 84, 147, 34, 100, 241, 115, 18, 64, 8, 195, 236, 219, 161, 141, 61, 151, 0, 207, 43, 118, 130, 214, 27, 181, 175, PublicKeyAlgorithmTags.EXPERIMENTAL_7, 80, 69, 243, 48, 239, 63, 85, 162, 234, 101, 186, 47, 192, 222, 28, SecretKeyPacket.USAGE_AEAD, 77, 146, 117, 6, 138, 178, 230, 14, 31, 98, 212, 168, 150, 249, 197, 37, 89, 132, 114, 57, 76, 94, 120, 56, 140, 209, 165, 226, 97, 179, 33, 156, 30, 67, 199, 252, 4, 81, 153, PublicKeyAlgorithmTags.EXPERIMENTAL_10, 13, 250, 223, 126, 36, 59, 171, 206, 17, 143, 78, 183, 235, 60, 129, 148, 247, 185, 19, 44, Primes.SMALL_FACTOR_LIMIT, 231, PublicKeyAlgorithmTags.EXPERIMENTAL_11, 196, 3, 86, 68, CertificateBody.profileType, 169, 42, 187, 193, 83, 220, 11, 157, PublicKeyAlgorithmTags.EXPERIMENTAL_9, 49, 116, 246, 70, 172, 137, 20, 225, 22, 58, PublicKeyAlgorithmTags.EXPERIMENTAL_6, 9, 112, 182, 208, 237, 204, 66, 152, 164, 40, 92, 248, 134};
    private static final long[] C0 = new long[256];
    private static final long[] C1 = new long[256];
    private static final long[] C2 = new long[256];
    private static final long[] C3 = new long[256];
    private static final long[] C4 = new long[256];
    private static final long[] C5 = new long[256];
    private static final long[] C6 = new long[256];
    private static final long[] C7 = new long[256];
    private static final short[] EIGHT = new short[32];

    static {
        EIGHT[31] = 8;
        for (int i = 0; i < 256; i++) {
            int i2 = SBOX[i];
            int iMulX = mulX(i2);
            int iMulX2 = mulX(iMulX);
            int i3 = iMulX2 ^ i2;
            int iMulX3 = mulX(iMulX2);
            int i4 = iMulX3 ^ i2;
            C0[i] = packIntoLong(i2, i2, iMulX2, i2, iMulX3, i3, iMulX, i4);
            C1[i] = packIntoLong(i4, i2, i2, iMulX2, i2, iMulX3, i3, iMulX);
            C2[i] = packIntoLong(iMulX, i4, i2, i2, iMulX2, i2, iMulX3, i3);
            C3[i] = packIntoLong(i3, iMulX, i4, i2, i2, iMulX2, i2, iMulX3);
            C4[i] = packIntoLong(iMulX3, i3, iMulX, i4, i2, i2, iMulX2, i2);
            C5[i] = packIntoLong(i2, iMulX3, i3, iMulX, i4, i2, i2, iMulX2);
            C6[i] = packIntoLong(iMulX2, i2, iMulX3, i3, iMulX, i4, i2, i2);
            C7[i] = packIntoLong(i2, iMulX2, i2, iMulX3, i3, iMulX, i4, i2);
        }
    }

    public WhirlpoolDigest() {
        this(CryptoServicePurpose.ANY);
    }

    public WhirlpoolDigest(CryptoServicePurpose cryptoServicePurpose) {
        this._rc = new long[11];
        this._buffer = new byte[64];
        this._bufferPos = 0;
        this._bitCount = new short[32];
        this._hash = new long[8];
        this._K = new long[8];
        this._L = new long[8];
        this._block = new long[8];
        this._state = new long[8];
        this._rc[0] = 0;
        for (int i = 1; i <= 10; i++) {
            int i2 = (i - 1) * 8;
            this._rc[i] = (((((((C0[i2] & (-72057594037927936L)) ^ (C1[i2 + 1] & 71776119061217280L)) ^ (C2[i2 + 2] & 280375465082880L)) ^ (C3[i2 + 3] & 1095216660480L)) ^ (C4[i2 + 4] & 4278190080L)) ^ (C5[i2 + 5] & 16711680)) ^ (C6[i2 + 6] & 65280)) ^ (C7[i2 + 7] & 255);
        }
        this.purpose = cryptoServicePurpose;
        CryptoServicesRegistrar.checkConstraints(Utils.getDefaultProperties(this, getDigestSize(), cryptoServicePurpose));
    }

    public WhirlpoolDigest(WhirlpoolDigest whirlpoolDigest) {
        this._rc = new long[11];
        this._buffer = new byte[64];
        this._bufferPos = 0;
        this._bitCount = new short[32];
        this._hash = new long[8];
        this._K = new long[8];
        this._L = new long[8];
        this._block = new long[8];
        this._state = new long[8];
        this.purpose = whirlpoolDigest.purpose;
        reset(whirlpoolDigest);
        CryptoServicesRegistrar.checkConstraints(Utils.getDefaultProperties(this, getDigestSize(), this.purpose));
    }

    private byte[] copyBitLength() {
        byte[] bArr = new byte[32];
        for (int i = 0; i < 32; i++) {
            bArr[i] = (byte) (this._bitCount[i] & 255);
        }
        return bArr;
    }

    private void finish() {
        byte[] bArrCopyBitLength = copyBitLength();
        byte[] bArr = this._buffer;
        int i = this._bufferPos;
        bArr[i] = (byte) (bArr[i] | RevocationKeyTags.CLASS_DEFAULT);
        int i2 = this._bufferPos + 1;
        this._bufferPos = i2;
        if (i2 == this._buffer.length) {
            processFilledBuffer(this._buffer, 0);
        }
        if (this._bufferPos > 32) {
            while (this._bufferPos != 0) {
                update((byte) 0);
            }
        }
        while (this._bufferPos <= 32) {
            update((byte) 0);
        }
        System.arraycopy(bArrCopyBitLength, 0, this._buffer, 32, bArrCopyBitLength.length);
        processFilledBuffer(this._buffer, 0);
    }

    private void increment() {
        int i = 0;
        for (int length = this._bitCount.length - 1; length >= 0; length--) {
            int i2 = (this._bitCount[length] & 255) + EIGHT[length] + i;
            i = i2 >>> 8;
            this._bitCount[length] = (short) (i2 & 255);
        }
    }

    private static int mulX(int i) {
        return ((-(i >>> 7)) & REDUCTION_POLYNOMIAL) ^ (i << 1);
    }

    private static long packIntoLong(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
        return (((((((((long) i2) << 48) ^ (((long) i) << 56)) ^ (((long) i3) << 40)) ^ (((long) i4) << 32)) ^ (((long) i5) << 24)) ^ (((long) i6) << 16)) ^ (((long) i7) << 8)) ^ ((long) i8);
    }

    private void processFilledBuffer(byte[] bArr, int i) {
        Pack.bigEndianToLong(this._buffer, 0, this._block);
        processBlock();
        this._bufferPos = 0;
        Arrays.fill(this._buffer, (byte) 0);
    }

    @Override // org.bouncycastle.util.Memoable
    public Memoable copy() {
        return new WhirlpoolDigest(this);
    }

    @Override // org.bouncycastle.crypto.Digest
    public int doFinal(byte[] bArr, int i) {
        finish();
        Pack.longToBigEndian(this._hash, bArr, i);
        reset();
        return getDigestSize();
    }

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

    @Override // org.bouncycastle.crypto.ExtendedDigest
    public int getByteLength() {
        return 64;
    }

    @Override // org.bouncycastle.crypto.Digest
    public int getDigestSize() {
        return 64;
    }

    protected void processBlock() {
        long[] jArr;
        char c;
        char c2;
        long[] jArr2;
        for (int i = 0; i < 8; i++) {
            long[] jArr3 = this._state;
            long j = this._block[i];
            long[] jArr4 = this._K;
            long j2 = this._hash[i];
            jArr4[i] = j2;
            jArr3[i] = j ^ j2;
        }
        for (int i2 = 1; i2 <= 10; i2++) {
            int i3 = 0;
            while (true) {
                jArr = this._L;
                c = ' ';
                c2 = '(';
                if (i3 >= 8) {
                    break;
                }
                jArr[i3] = 0;
                long[] jArr5 = this._L;
                jArr5[i3] = C0[((int) (this._K[(i3 + 0) & 7] >>> 56)) & 255] ^ jArr5[i3];
                long[] jArr6 = this._L;
                jArr6[i3] = jArr6[i3] ^ C1[((int) (this._K[(i3 - 1) & 7] >>> 48)) & 255];
                long[] jArr7 = this._L;
                jArr7[i3] = jArr7[i3] ^ C2[((int) (this._K[(i3 - 2) & 7] >>> 40)) & 255];
                long[] jArr8 = this._L;
                jArr8[i3] = jArr8[i3] ^ C3[((int) (this._K[(i3 - 3) & 7] >>> 32)) & 255];
                long[] jArr9 = this._L;
                jArr9[i3] = jArr9[i3] ^ C4[((int) (this._K[(i3 - 4) & 7] >>> 24)) & 255];
                long[] jArr10 = this._L;
                jArr10[i3] = jArr10[i3] ^ C5[((int) (this._K[(i3 - 5) & 7] >>> 16)) & 255];
                long[] jArr11 = this._L;
                jArr11[i3] = jArr11[i3] ^ C6[((int) (this._K[(i3 - 6) & 7] >>> 8)) & 255];
                long[] jArr12 = this._L;
                jArr12[i3] = jArr12[i3] ^ C7[((int) this._K[(i3 - 7) & 7]) & 255];
                i3++;
            }
            System.arraycopy(jArr, 0, this._K, 0, this._K.length);
            long[] jArr13 = this._K;
            jArr13[0] = jArr13[0] ^ this._rc[i2];
            int i4 = 0;
            while (true) {
                jArr2 = this._L;
                if (i4 < 8) {
                    jArr2[i4] = this._K[i4];
                    long[] jArr14 = this._L;
                    jArr14[i4] = jArr14[i4] ^ C0[((int) (this._state[(i4 + 0) & 7] >>> 56)) & 255];
                    long[] jArr15 = this._L;
                    jArr15[i4] = jArr15[i4] ^ C1[((int) (this._state[(i4 - 1) & 7] >>> 48)) & 255];
                    long[] jArr16 = this._L;
                    jArr16[i4] = jArr16[i4] ^ C2[((int) (this._state[(i4 - 2) & 7] >>> c2)) & 255];
                    long[] jArr17 = this._L;
                    jArr17[i4] = jArr17[i4] ^ C3[((int) (this._state[(i4 - 3) & 7] >>> c)) & 255];
                    long[] jArr18 = this._L;
                    jArr18[i4] = jArr18[i4] ^ C4[((int) (this._state[(i4 - 4) & 7] >>> 24)) & 255];
                    long[] jArr19 = this._L;
                    jArr19[i4] = jArr19[i4] ^ C5[((int) (this._state[(i4 - 5) & 7] >>> 16)) & 255];
                    long[] jArr20 = this._L;
                    jArr20[i4] = jArr20[i4] ^ C6[((int) (this._state[(i4 - 6) & 7] >>> 8)) & 255];
                    long[] jArr21 = this._L;
                    jArr21[i4] = jArr21[i4] ^ C7[((int) this._state[(i4 - 7) & 7]) & 255];
                    i4++;
                    c = ' ';
                    c2 = '(';
                }
            }
            System.arraycopy(jArr2, 0, this._state, 0, this._state.length);
        }
        for (int i5 = 0; i5 < 8; i5++) {
            long[] jArr22 = this._hash;
            jArr22[i5] = jArr22[i5] ^ (this._state[i5] ^ this._block[i5]);
        }
    }

    @Override // org.bouncycastle.crypto.Digest
    public void reset() {
        this._bufferPos = 0;
        Arrays.fill(this._bitCount, (short) 0);
        Arrays.fill(this._buffer, (byte) 0);
        Arrays.fill(this._hash, 0L);
        Arrays.fill(this._K, 0L);
        Arrays.fill(this._L, 0L);
        Arrays.fill(this._block, 0L);
        Arrays.fill(this._state, 0L);
    }

    @Override // org.bouncycastle.util.Memoable
    public void reset(Memoable memoable) {
        WhirlpoolDigest whirlpoolDigest = (WhirlpoolDigest) memoable;
        System.arraycopy(whirlpoolDigest._rc, 0, this._rc, 0, this._rc.length);
        System.arraycopy(whirlpoolDigest._buffer, 0, this._buffer, 0, this._buffer.length);
        this._bufferPos = whirlpoolDigest._bufferPos;
        System.arraycopy(whirlpoolDigest._bitCount, 0, this._bitCount, 0, this._bitCount.length);
        System.arraycopy(whirlpoolDigest._hash, 0, this._hash, 0, this._hash.length);
        System.arraycopy(whirlpoolDigest._K, 0, this._K, 0, this._K.length);
        System.arraycopy(whirlpoolDigest._L, 0, this._L, 0, this._L.length);
        System.arraycopy(whirlpoolDigest._block, 0, this._block, 0, this._block.length);
        System.arraycopy(whirlpoolDigest._state, 0, this._state, 0, this._state.length);
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte b) {
        this._buffer[this._bufferPos] = b;
        int i = this._bufferPos + 1;
        this._bufferPos = i;
        if (i == this._buffer.length) {
            processFilledBuffer(this._buffer, 0);
        }
        increment();
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte[] bArr, int i, int i2) {
        while (i2 > 0) {
            update(bArr[i]);
            i++;
            i2--;
        }
    }
}
