package org.bouncycastle.openpgp.operator;

import java.io.IOException;
import java.math.BigInteger;
import org.bouncycastle.bcpg.ContainedPacket;
import org.bouncycastle.bcpg.MPInteger;
import org.bouncycastle.bcpg.PublicKeyEncSessionPacket;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.util.Properties;

/* JADX INFO: loaded from: classes2.dex */
public abstract class PublicKeyKeyEncryptionMethodGenerator implements PGPKeyEncryptionMethodGenerator {
    public static final String SESSION_KEY_OBFUSCATION_PROPERTY = "org.bouncycastle.openpgp.session_key_obfuscation";
    public static final long WILDCARD = 0;
    public static final byte[] WILDCARD_FINGERPRINT = new byte[0];
    public static final long WILDCARD_KEYID = 0;
    private final PGPPublicKey pubKey;
    protected boolean sessionKeyObfuscation;
    protected boolean useWildcardRecipient;

    protected PublicKeyKeyEncryptionMethodGenerator(PGPPublicKey pGPPublicKey) {
        switch (pGPPublicKey.getAlgorithm()) {
            case 1:
            case 2:
            case 16:
            case 18:
            case 20:
            case 25:
            case 26:
                this.pubKey = pGPPublicKey;
                this.sessionKeyObfuscation = getSessionKeyObfuscationDefault();
                return;
            case 3:
                throw new IllegalArgumentException("Can't use an RSA_SIGN key for encryption.");
            case 17:
                throw new IllegalArgumentException("Can't use DSA for encryption.");
            case 19:
                throw new IllegalArgumentException("Can't use ECDSA for encryption.");
            case 22:
            case 27:
            case 28:
                throw new IllegalArgumentException("Can't use EdDSA for encryption.");
            default:
                throw new IllegalArgumentException("unknown asymmetric algorithm: " + pGPPublicKey.getAlgorithm());
        }
    }

    private void addCheckSum(byte[] bArr, int i) {
        int i2 = 0;
        while (i != bArr.length - 2) {
            i2 += bArr[i] & 255;
            i++;
        }
        bArr[bArr.length - 2] = (byte) (i2 >> 8);
        bArr[bArr.length - 1] = (byte) i2;
    }

    private byte[] convertToEncodedMPI(byte[] bArr) throws PGPException {
        try {
            return new MPInteger(new BigInteger(1, bArr)).getEncoded();
        } catch (IOException e) {
            throw new PGPException("Invalid MPI encoding: " + e.getMessage(), e);
        }
    }

    protected static byte[] getSessionInfo(byte[] bArr, byte b, byte[] bArr2) {
        int length = bArr.length + bArr2.length + (b == 0 ? 1 : 2);
        byte[] bArr3 = new byte[length];
        System.arraycopy(bArr, 0, bArr3, 0, bArr.length);
        bArr3[bArr.length] = (byte) ((length - bArr.length) - 1);
        if (b != 0) {
            bArr3[bArr.length + 1] = b;
        }
        System.arraycopy(bArr2, 0, bArr3, length - bArr2.length, bArr2.length);
        return bArr3;
    }

    private static boolean getSessionKeyObfuscationDefault() {
        return !Properties.isOverrideSetTo(SESSION_KEY_OBFUSCATION_PROPERTY, false);
    }

    protected byte[] createSessionInfo(byte b, byte[] bArr) {
        if (b == 0) {
            byte[] bArr2 = new byte[bArr.length + 2];
            System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
            addCheckSum(bArr2, 0);
            return bArr2;
        }
        byte[] bArr3 = new byte[bArr.length + 3];
        bArr3[0] = b;
        System.arraycopy(bArr, 0, bArr3, 1, bArr.length);
        addCheckSum(bArr3, 1);
        return bArr3;
    }

    public byte[][] encodeEncryptedSessionInfo(byte[] bArr) throws PGPException {
        switch (this.pubKey.getAlgorithm()) {
            case 1:
            case 2:
                return new byte[][]{convertToEncodedMPI(bArr)};
            case 16:
            case 20:
                int length = bArr.length / 2;
                byte[] bArr2 = new byte[length];
                int length2 = bArr.length / 2;
                byte[] bArr3 = new byte[length2];
                System.arraycopy(bArr, 0, bArr2, 0, length);
                System.arraycopy(bArr, length, bArr3, 0, length2);
                return new byte[][]{convertToEncodedMPI(bArr2), convertToEncodedMPI(bArr3)};
            case 18:
            case 25:
            case 26:
                return new byte[][]{bArr};
            default:
                throw new PGPException("unknown asymmetric algorithm: " + this.pubKey.getAlgorithm());
        }
    }

    protected abstract byte[] encryptSessionInfo(PGPPublicKey pGPPublicKey, byte[] bArr, byte b, boolean z) throws PGPException;

    @Override // org.bouncycastle.openpgp.operator.PGPKeyEncryptionMethodGenerator
    public ContainedPacket generate(PGPDataEncryptorBuilder pGPDataEncryptorBuilder, byte[] bArr) throws PGPException {
        byte[] fingerprint;
        int version;
        if (pGPDataEncryptorBuilder.getAeadAlgorithm() <= 0 || pGPDataEncryptorBuilder.isV5StyleAEAD()) {
            return PublicKeyEncSessionPacket.createV3PKESKPacket(this.useWildcardRecipient ? 0L : this.pubKey.getKeyID(), this.pubKey.getAlgorithm(), encodeEncryptedSessionInfo(encryptSessionInfo(this.pubKey, bArr, (byte) pGPDataEncryptorBuilder.getAlgorithm(), true)));
        }
        if (this.useWildcardRecipient) {
            fingerprint = WILDCARD_FINGERPRINT;
            version = 0;
        } else {
            fingerprint = this.pubKey.getFingerprint();
            version = this.pubKey.getVersion();
        }
        return PublicKeyEncSessionPacket.createV6PKESKPacket(version, fingerprint, this.pubKey.getAlgorithm(), encodeEncryptedSessionInfo(encryptSessionInfo(this.pubKey, bArr, (byte) pGPDataEncryptorBuilder.getAlgorithm(), false)));
    }

    public PublicKeyKeyEncryptionMethodGenerator setSessionKeyObfuscation(boolean z) {
        this.sessionKeyObfuscation = z;
        return this;
    }

    @Deprecated
    public PublicKeyKeyEncryptionMethodGenerator setUseWildcardKeyID(boolean z) {
        return setUseWildcardRecipient(z);
    }

    public PublicKeyKeyEncryptionMethodGenerator setUseWildcardRecipient(boolean z) {
        this.useWildcardRecipient = z;
        return this;
    }
}
