package org.bouncycastle.crypto.signers;

import java.security.SecureRandom;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.Signer;
import org.bouncycastle.crypto.Xof;
import org.bouncycastle.crypto.digests.Prehash;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.params.RSABlindingParameters;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class PSSSigner implements Signer {
    public static final byte TRAILER_IMPLICIT = -68;
    private byte[] block;
    private AsymmetricBlockCipher cipher;
    private Digest contentDigest1;
    private Digest contentDigest2;
    private int emBits;
    private int hLen;
    private byte[] mDash;
    private Digest mgfDigest;
    private int mgfhLen;
    private SecureRandom random;
    private int sLen;
    private boolean sSet;
    private byte[] salt;
    private byte trailer;

    public PSSSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, int i) {
        this(asymmetricBlockCipher, digest, i, TRAILER_IMPLICIT);
    }

    public PSSSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, int i, byte b) {
        this(asymmetricBlockCipher, digest, digest, i, b);
    }

    public PSSSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, Digest digest2, int i) {
        this(asymmetricBlockCipher, digest, digest2, i, TRAILER_IMPLICIT);
    }

    public PSSSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, Digest digest2, int i, byte b) {
        this(asymmetricBlockCipher, digest, digest, digest2, i, b);
    }

    private PSSSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, Digest digest2, Digest digest3, int i, byte b) {
        this.cipher = asymmetricBlockCipher;
        this.contentDigest1 = digest;
        this.contentDigest2 = digest2;
        this.mgfDigest = digest3;
        this.hLen = digest2.getDigestSize();
        this.mgfhLen = digest3.getDigestSize();
        this.sSet = false;
        this.sLen = i;
        this.salt = new byte[i];
        this.mDash = new byte[i + 8 + this.hLen];
        this.trailer = b;
    }

    private PSSSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, Digest digest2, Digest digest3, byte[] bArr, byte b) {
        this.cipher = asymmetricBlockCipher;
        this.contentDigest1 = digest;
        this.contentDigest2 = digest2;
        this.mgfDigest = digest3;
        this.hLen = digest2.getDigestSize();
        this.mgfhLen = digest3.getDigestSize();
        this.sSet = true;
        this.sLen = bArr.length;
        this.salt = bArr;
        this.mDash = new byte[this.sLen + 8 + this.hLen];
        this.trailer = b;
    }

    public PSSSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, Digest digest2, byte[] bArr) {
        this(asymmetricBlockCipher, digest, digest2, bArr, TRAILER_IMPLICIT);
    }

    public PSSSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, Digest digest2, byte[] bArr, byte b) {
        this(asymmetricBlockCipher, digest, digest, digest2, bArr, b);
    }

    public PSSSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, byte[] bArr) {
        this(asymmetricBlockCipher, digest, digest, bArr, TRAILER_IMPLICIT);
    }

    private void ItoOSP(int i, byte[] bArr) {
        bArr[0] = (byte) (i >>> 24);
        bArr[1] = (byte) (i >>> 16);
        bArr[2] = (byte) (i >>> 8);
        bArr[3] = (byte) (i >>> 0);
    }

    private void clearBlock(byte[] bArr) {
        for (int i = 0; i != bArr.length; i++) {
            bArr[i] = 0;
        }
    }

    public static PSSSigner createRawSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest) {
        return new PSSSigner(asymmetricBlockCipher, Prehash.forDigest(digest), digest, digest, digest.getDigestSize(), TRAILER_IMPLICIT);
    }

    public static PSSSigner createRawSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, Digest digest2, int i, byte b) {
        return new PSSSigner(asymmetricBlockCipher, Prehash.forDigest(digest), digest, digest2, i, b);
    }

    public static PSSSigner createRawSigner(AsymmetricBlockCipher asymmetricBlockCipher, Digest digest, Digest digest2, byte[] bArr, byte b) {
        return new PSSSigner(asymmetricBlockCipher, Prehash.forDigest(digest), digest, digest2, bArr, b);
    }

    private byte[] maskGenerator(byte[] bArr, int i, int i2, int i3) {
        if (!(this.mgfDigest instanceof Xof)) {
            return maskGeneratorFunction1(bArr, i, i2, i3);
        }
        byte[] bArr2 = new byte[i3];
        this.mgfDigest.update(bArr, i, i2);
        ((Xof) this.mgfDigest).doFinal(bArr2, 0, i3);
        return bArr2;
    }

    private byte[] maskGeneratorFunction1(byte[] bArr, int i, int i2, int i3) {
        byte[] bArr2 = new byte[i3];
        byte[] bArr3 = new byte[this.mgfhLen];
        byte[] bArr4 = new byte[4];
        this.mgfDigest.reset();
        int i4 = 0;
        while (i4 < i3 / this.mgfhLen) {
            ItoOSP(i4, bArr4);
            this.mgfDigest.update(bArr, i, i2);
            this.mgfDigest.update(bArr4, 0, 4);
            this.mgfDigest.doFinal(bArr3, 0);
            System.arraycopy(bArr3, 0, bArr2, this.mgfhLen * i4, this.mgfhLen);
            i4++;
        }
        if (this.mgfhLen * i4 < i3) {
            ItoOSP(i4, bArr4);
            this.mgfDigest.update(bArr, i, i2);
            this.mgfDigest.update(bArr4, 0, 4);
            this.mgfDigest.doFinal(bArr3, 0);
            System.arraycopy(bArr3, 0, bArr2, this.mgfhLen * i4, i3 - (i4 * this.mgfhLen));
        }
        return bArr2;
    }

    @Override // org.bouncycastle.crypto.Signer
    public byte[] generateSignature() throws DataLengthException, CryptoException {
        if (this.contentDigest1.getDigestSize() != this.hLen) {
            throw new IllegalStateException();
        }
        this.contentDigest1.doFinal(this.mDash, (this.mDash.length - this.hLen) - this.sLen);
        if (this.sLen != 0) {
            if (!this.sSet) {
                this.random.nextBytes(this.salt);
            }
            System.arraycopy(this.salt, 0, this.mDash, this.mDash.length - this.sLen, this.sLen);
        }
        int i = this.hLen;
        byte[] bArr = new byte[i];
        this.contentDigest2.update(this.mDash, 0, this.mDash.length);
        this.contentDigest2.doFinal(bArr, 0);
        this.block[(((this.block.length - this.sLen) - 1) - this.hLen) - 1] = 1;
        System.arraycopy(this.salt, 0, this.block, ((this.block.length - this.sLen) - this.hLen) - 1, this.sLen);
        byte[] bArrMaskGenerator = maskGenerator(bArr, 0, i, (this.block.length - this.hLen) - 1);
        int i2 = 0;
        while (true) {
            int length = bArrMaskGenerator.length;
            byte[] bArr2 = this.block;
            if (i2 == length) {
                System.arraycopy(bArr, 0, bArr2, (this.block.length - this.hLen) - 1, this.hLen);
                int length2 = 255 >>> ((this.block.length * 8) - this.emBits);
                byte[] bArr3 = this.block;
                bArr3[0] = (byte) (length2 & bArr3[0]);
                this.block[this.block.length - 1] = this.trailer;
                byte[] bArrProcessBlock = this.cipher.processBlock(this.block, 0, this.block.length);
                clearBlock(this.block);
                return bArrProcessBlock;
            }
            bArr2[i2] = (byte) (bArr2[i2] ^ bArrMaskGenerator[i2]);
            i2++;
        }
    }

    @Override // org.bouncycastle.crypto.Signer
    public void init(boolean z, CipherParameters cipherParameters) {
        CipherParameters parameters;
        RSAKeyParameters publicKey;
        if (cipherParameters instanceof ParametersWithRandom) {
            ParametersWithRandom parametersWithRandom = (ParametersWithRandom) cipherParameters;
            parameters = parametersWithRandom.getParameters();
            this.random = parametersWithRandom.getRandom();
        } else {
            if (z) {
                this.random = CryptoServicesRegistrar.getSecureRandom();
            }
            parameters = cipherParameters;
        }
        if (parameters instanceof RSABlindingParameters) {
            publicKey = ((RSABlindingParameters) parameters).getPublicKey();
            this.cipher.init(z, cipherParameters);
        } else {
            publicKey = (RSAKeyParameters) parameters;
            this.cipher.init(z, parameters);
        }
        this.emBits = publicKey.getModulus().bitLength() - 1;
        if (this.emBits < (this.hLen * 8) + (this.sLen * 8) + 9) {
            throw new IllegalArgumentException("key too small for specified hash and salt lengths");
        }
        this.block = new byte[(this.emBits + 7) / 8];
        reset();
    }

    @Override // org.bouncycastle.crypto.Signer
    public void reset() {
        this.contentDigest1.reset();
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte b) {
        this.contentDigest1.update(b);
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte[] bArr, int i, int i2) {
        this.contentDigest1.update(bArr, i, i2);
    }

    @Override // org.bouncycastle.crypto.Signer
    public boolean verifySignature(byte[] bArr) {
        byte[] bArr2;
        if (this.contentDigest1.getDigestSize() != this.hLen) {
            throw new IllegalStateException();
        }
        this.contentDigest1.doFinal(this.mDash, (this.mDash.length - this.hLen) - this.sLen);
        try {
            byte[] bArrProcessBlock = this.cipher.processBlock(bArr, 0, bArr.length);
            Arrays.fill(this.block, 0, this.block.length - bArrProcessBlock.length, (byte) 0);
            System.arraycopy(bArrProcessBlock, 0, this.block, this.block.length - bArrProcessBlock.length, bArrProcessBlock.length);
            int length = 255 >>> ((this.block.length * 8) - this.emBits);
            if ((255 & this.block[0]) != (this.block[0] & length) || this.block[this.block.length - 1] != this.trailer) {
                clearBlock(this.block);
                return false;
            }
            byte[] bArrMaskGenerator = maskGenerator(this.block, (this.block.length - this.hLen) - 1, this.hLen, (this.block.length - this.hLen) - 1);
            int i = 0;
            while (true) {
                int length2 = bArrMaskGenerator.length;
                bArr2 = this.block;
                if (i == length2) {
                    break;
                }
                bArr2[i] = (byte) (bArr2[i] ^ bArrMaskGenerator[i]);
                i++;
            }
            bArr2[0] = (byte) (length & bArr2[0]);
            int i2 = 0;
            while (true) {
                int length3 = ((this.block.length - this.hLen) - this.sLen) - 2;
                byte[] bArr3 = this.block;
                if (i2 == length3) {
                    if (bArr3[((this.block.length - this.hLen) - this.sLen) - 2] != 1) {
                        clearBlock(this.block);
                        return false;
                    }
                    if (this.sSet) {
                        System.arraycopy(this.salt, 0, this.mDash, this.mDash.length - this.sLen, this.sLen);
                    } else {
                        System.arraycopy(this.block, ((this.block.length - this.sLen) - this.hLen) - 1, this.mDash, this.mDash.length - this.sLen, this.sLen);
                    }
                    this.contentDigest2.update(this.mDash, 0, this.mDash.length);
                    this.contentDigest2.doFinal(this.mDash, this.mDash.length - this.hLen);
                    int length4 = (this.block.length - this.hLen) - 1;
                    for (int length5 = this.mDash.length - this.hLen; length5 != this.mDash.length; length5++) {
                        if ((this.block[length4] ^ this.mDash[length5]) != 0) {
                            clearBlock(this.mDash);
                            clearBlock(this.block);
                            return false;
                        }
                        length4++;
                    }
                    clearBlock(this.mDash);
                    clearBlock(this.block);
                    return true;
                }
                if (bArr3[i2] != 0) {
                    clearBlock(this.block);
                    return false;
                }
                i2++;
            }
        } catch (Exception e) {
            return false;
        }
    }
}
