package org.bouncycastle.crypto.encodings;

import java.math.BigInteger;
import org.bouncycastle.bcpg.sig.RevocationKeyTags;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.params.RSAKeyParameters;

/* JADX INFO: loaded from: classes.dex */
public class ISO9796d1Encoding implements AsymmetricBlockCipher {
    private int bitSize;
    private AsymmetricBlockCipher engine;
    private boolean forEncryption;
    private BigInteger modulus;
    private int padBits = 0;
    private static final BigInteger SIXTEEN = BigInteger.valueOf(16);
    private static final BigInteger SIX = BigInteger.valueOf(6);
    private static byte[] shadows = {14, 3, 5, 8, 9, 4, 2, 15, 0, 13, 11, 6, 7, 10, 12, 1};
    private static byte[] inverse = {8, 15, 6, 1, 5, 2, 11, 12, 3, 4, 13, 10, 14, 9, 0, 7};

    public ISO9796d1Encoding(AsymmetricBlockCipher asymmetricBlockCipher) {
        this.engine = asymmetricBlockCipher;
    }

    private static byte[] convertOutputDecryptOnly(BigInteger bigInteger) {
        byte[] byteArray = bigInteger.toByteArray();
        if (byteArray[0] != 0) {
            return byteArray;
        }
        int length = byteArray.length - 1;
        byte[] bArr = new byte[length];
        System.arraycopy(byteArray, 1, bArr, 0, length);
        return bArr;
    }

    private byte[] decodeBlock(byte[] bArr, int i, int i2) throws InvalidCipherTextException {
        byte[] bArrProcessBlock = this.engine.processBlock(bArr, i, i2);
        int i3 = (this.bitSize + 13) / 16;
        BigInteger bigInteger = new BigInteger(1, bArrProcessBlock);
        if (!bigInteger.mod(SIXTEEN).equals(SIX)) {
            if (!this.modulus.subtract(bigInteger).mod(SIXTEEN).equals(SIX)) {
                throw new InvalidCipherTextException("resulting integer iS or (modulus - iS) is not congruent to 6 mod 16");
            }
            bigInteger = this.modulus.subtract(bigInteger);
        }
        byte[] bArrConvertOutputDecryptOnly = convertOutputDecryptOnly(bigInteger);
        if ((bArrConvertOutputDecryptOnly[bArrConvertOutputDecryptOnly.length - 1] & 15) != 6) {
            throw new InvalidCipherTextException("invalid forcing byte in block");
        }
        bArrConvertOutputDecryptOnly[bArrConvertOutputDecryptOnly.length - 1] = (byte) (((bArrConvertOutputDecryptOnly[bArrConvertOutputDecryptOnly.length - 1] & 255) >>> 4) | (inverse[(bArrConvertOutputDecryptOnly[bArrConvertOutputDecryptOnly.length - 2] & 255) >> 4] << 4));
        bArrConvertOutputDecryptOnly[0] = (byte) ((shadows[(bArrConvertOutputDecryptOnly[1] & 255) >>> 4] << 4) | shadows[bArrConvertOutputDecryptOnly[1] & 15]);
        int i4 = 1;
        int i5 = 0;
        boolean z = false;
        for (int length = bArrConvertOutputDecryptOnly.length - 1; length >= bArrConvertOutputDecryptOnly.length - (i3 * 2); length -= 2) {
            int i6 = (shadows[(bArrConvertOutputDecryptOnly[length] & 255) >>> 4] << 4) | shadows[bArrConvertOutputDecryptOnly[length] & 15];
            int i7 = length - 1;
            if (((bArrConvertOutputDecryptOnly[i7] ^ i6) & 255) != 0) {
                if (z) {
                    throw new InvalidCipherTextException("invalid tsums in block");
                }
                i4 = (bArrConvertOutputDecryptOnly[i7] ^ i6) & 255;
                z = true;
                i5 = i7;
            }
        }
        bArrConvertOutputDecryptOnly[i5] = 0;
        int length2 = (bArrConvertOutputDecryptOnly.length - i5) / 2;
        byte[] bArr2 = new byte[length2];
        for (int i8 = 0; i8 < length2; i8++) {
            bArr2[i8] = bArrConvertOutputDecryptOnly[(i8 * 2) + i5 + 1];
        }
        this.padBits = i4 - 1;
        return bArr2;
    }

    private byte[] encodeBlock(byte[] bArr, int i, int i2) throws InvalidCipherTextException {
        int i3 = (this.bitSize + 7) / 8;
        byte[] bArr2 = new byte[i3];
        int i4 = 1;
        int i5 = this.padBits + 1;
        int i6 = (this.bitSize + 13) / 16;
        int i7 = 0;
        while (i7 < i6) {
            if (i7 > i6 - i2) {
                int i8 = i6 - i7;
                System.arraycopy(bArr, (i + i2) - i8, bArr2, i3 - i6, i8);
            } else {
                System.arraycopy(bArr, i, bArr2, i3 - (i7 + i2), i2);
            }
            i7 += i2;
        }
        for (int i9 = i3 - (i6 * 2); i9 != i3; i9 += 2) {
            byte b = bArr2[(i3 - i6) + (i9 / 2)];
            bArr2[i9] = (byte) ((shadows[(b & 255) >>> 4] << 4) | shadows[b & 15]);
            bArr2[i9 + 1] = b;
        }
        int i10 = i3 - (i2 * 2);
        bArr2[i10] = (byte) (bArr2[i10] ^ i5);
        int i11 = i3 - 1;
        bArr2[i11] = (byte) ((bArr2[i11] << 4) | 6);
        int i12 = 8 - ((this.bitSize - 1) % 8);
        if (i12 != 8) {
            bArr2[0] = (byte) (bArr2[0] & (255 >>> i12));
            bArr2[0] = (byte) ((128 >>> i12) | bArr2[0]);
            i4 = 0;
        } else {
            bArr2[0] = 0;
            bArr2[1] = (byte) (bArr2[1] | RevocationKeyTags.CLASS_DEFAULT);
        }
        return this.engine.processBlock(bArr2, i4, i3 - i4);
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public int getInputBlockSize() {
        int inputBlockSize = this.engine.getInputBlockSize();
        return this.forEncryption ? (inputBlockSize + 1) / 2 : inputBlockSize;
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public int getOutputBlockSize() {
        int outputBlockSize = this.engine.getOutputBlockSize();
        return this.forEncryption ? outputBlockSize : (outputBlockSize + 1) / 2;
    }

    public int getPadBits() {
        return this.padBits;
    }

    public AsymmetricBlockCipher getUnderlyingCipher() {
        return this.engine;
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public void init(boolean z, CipherParameters cipherParameters) {
        RSAKeyParameters rSAKeyParameters = cipherParameters instanceof ParametersWithRandom ? (RSAKeyParameters) ((ParametersWithRandom) cipherParameters).getParameters() : (RSAKeyParameters) cipherParameters;
        this.engine.init(z, cipherParameters);
        this.modulus = rSAKeyParameters.getModulus();
        this.bitSize = this.modulus.bitLength();
        this.forEncryption = z;
    }

    @Override // org.bouncycastle.crypto.AsymmetricBlockCipher
    public byte[] processBlock(byte[] bArr, int i, int i2) throws InvalidCipherTextException {
        return this.forEncryption ? encodeBlock(bArr, i, i2) : decodeBlock(bArr, i, i2);
    }

    public void setPadBits(int i) {
        if (i > 7) {
            throw new IllegalArgumentException("padBits > 7");
        }
        this.padBits = i;
    }
}
