package org.bouncycastle.openpgp.operator;

import org.bouncycastle.bcpg.InputStreamPacket;
import org.bouncycastle.bcpg.PublicKeyEncSessionPacket;
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes2.dex */
public abstract class AbstractPublicKeyDataDecryptorFactory implements PublicKeyDataDecryptorFactory {
    protected static void checkRange(int i, byte[] bArr) throws PGPException {
        if (i > bArr.length) {
            throw new PGPException("encoded length out of range");
        }
    }

    protected boolean containsSKAlg(int i) {
        return i != 6;
    }

    protected byte[] prependSKAlgorithmToSessionData(PublicKeyEncSessionPacket publicKeyEncSessionPacket, InputStreamPacket inputStreamPacket, byte[] bArr) throws PGPException {
        byte cipherAlgorithm;
        if (!containsSKAlg(publicKeyEncSessionPacket.getVersion())) {
            if (inputStreamPacket instanceof SymmetricEncIntegrityPacket) {
                SymmetricEncIntegrityPacket symmetricEncIntegrityPacket = (SymmetricEncIntegrityPacket) inputStreamPacket;
                if (symmetricEncIntegrityPacket.getVersion() == 2) {
                    cipherAlgorithm = (byte) (symmetricEncIntegrityPacket.getCipherAlgorithm() & 255);
                }
            }
            throw new PGPException("v6 PKESK packet MUST precede v2 SEIPD packet");
        }
        switch (publicKeyEncSessionPacket.getAlgorithm()) {
            case 25:
                cipherAlgorithm = publicKeyEncSessionPacket.getEncSessionKey()[0][33];
                break;
            case 26:
                cipherAlgorithm = publicKeyEncSessionPacket.getEncSessionKey()[0][57];
                break;
            default:
                return bArr;
        }
        return Arrays.prepend(bArr, cipherAlgorithm);
    }

    @Override // org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory
    public byte[] recoverSessionData(int i, byte[][] bArr) throws PGPException {
        return recoverSessionData(i, bArr, 3);
    }

    @Override // org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory
    public final byte[] recoverSessionData(PublicKeyEncSessionPacket publicKeyEncSessionPacket, InputStreamPacket inputStreamPacket) throws PGPException {
        return prependSKAlgorithmToSessionData(publicKeyEncSessionPacket, inputStreamPacket, recoverSessionData(publicKeyEncSessionPacket.getAlgorithm(), publicKeyEncSessionPacket.getEncSessionKey(), publicKeyEncSessionPacket.getVersion()));
    }
}
