package org.bouncycastle.openpgp.operator.jcajce;

import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Provider;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import org.bouncycastle.bcpg.AEADUtils;
import org.bouncycastle.bcpg.SymmetricKeyUtils;
import org.bouncycastle.jcajce.io.CipherOutputStream;
import org.bouncycastle.jcajce.util.DefaultJcaJceHelper;
import org.bouncycastle.jcajce.util.NamedJcaJceHelper;
import org.bouncycastle.jcajce.util.ProviderJcaJceHelper;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.operator.PGPAEADDataEncryptor;
import org.bouncycastle.openpgp.operator.PGPDataEncryptor;
import org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder;
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class JcePGPDataEncryptorBuilder implements PGPDataEncryptorBuilder {
    private int chunkSize;
    private final int encAlgorithm;
    private SecureRandom random;
    private OperatorHelper helper = new OperatorHelper(new DefaultJcaJceHelper());
    private JceAEADUtil aeadHelper = new JceAEADUtil(this.helper);
    private boolean withIntegrityPacket = true;
    private int aeadAlgorithm = -1;
    private boolean isV5StyleAEAD = true;

    private class MyAeadDataEncryptor implements PGPAEADDataEncryptor {
        private final Cipher c;
        private final boolean isV5StyleAEAD;
        private final byte[] iv;
        private final byte[] keyBytes;

        MyAeadDataEncryptor(byte[] bArr) throws PGPException {
            this.isV5StyleAEAD = bArr.length == SymmetricKeyUtils.getKeyLengthInOctets(JcePGPDataEncryptorBuilder.this.encAlgorithm);
            if (this.isV5StyleAEAD) {
                this.keyBytes = bArr;
                this.iv = new byte[AEADUtils.getIVLength((byte) JcePGPDataEncryptorBuilder.this.aeadAlgorithm)];
                JcePGPDataEncryptorBuilder.this.getSecureRandom().nextBytes(this.iv);
            } else {
                byte[][] bArrSplitMessageKeyAndIv = AEADUtils.splitMessageKeyAndIv(bArr, JcePGPDataEncryptorBuilder.this.encAlgorithm, JcePGPDataEncryptorBuilder.this.aeadAlgorithm);
                this.keyBytes = bArrSplitMessageKeyAndIv[0];
                this.iv = bArrSplitMessageKeyAndIv[1];
            }
            this.c = JcePGPDataEncryptorBuilder.this.aeadHelper.createAEADCipher(JcePGPDataEncryptorBuilder.this.encAlgorithm, JcePGPDataEncryptorBuilder.this.aeadAlgorithm);
        }

        @Override // org.bouncycastle.openpgp.operator.PGPAEADDataEncryptor
        public int getAEADAlgorithm() {
            return JcePGPDataEncryptorBuilder.this.aeadAlgorithm;
        }

        @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptor
        public int getBlockSize() {
            return this.c.getBlockSize();
        }

        @Override // org.bouncycastle.openpgp.operator.PGPAEADDataEncryptor
        public int getChunkSize() {
            return JcePGPDataEncryptorBuilder.this.chunkSize;
        }

        @Override // org.bouncycastle.openpgp.operator.PGPAEADDataEncryptor
        public byte[] getIV() {
            return Arrays.clone(this.iv);
        }

        @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptor
        public PGPDigestCalculator getIntegrityCalculator() {
            return null;
        }

        @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptor
        public OutputStream getOutputStream(OutputStream outputStream) {
            try {
                return new JceAEADUtil.PGPAeadOutputStream(this.isV5StyleAEAD, outputStream, this.c, JcaJcePGPUtil.makeSymmetricKey(JcePGPDataEncryptorBuilder.this.encAlgorithm, this.keyBytes), this.iv, JcePGPDataEncryptorBuilder.this.encAlgorithm, JcePGPDataEncryptorBuilder.this.aeadAlgorithm, JcePGPDataEncryptorBuilder.this.chunkSize);
            } catch (Exception e) {
                throw new IllegalStateException("unable to process stream: " + e.getMessage());
            }
        }
    }

    private class MyPGPDataEncryptor implements PGPDataEncryptor {
        private final Cipher c;

        MyPGPDataEncryptor(byte[] bArr) throws PGPException {
            this.c = JcePGPDataEncryptorBuilder.this.helper.createStreamCipher(JcePGPDataEncryptorBuilder.this.encAlgorithm, JcePGPDataEncryptorBuilder.this.withIntegrityPacket);
            try {
                boolean z = JcePGPDataEncryptorBuilder.this.withIntegrityPacket;
                Cipher cipher = this.c;
                if (z) {
                    cipher.init(1, JcaJcePGPUtil.makeSymmetricKey(JcePGPDataEncryptorBuilder.this.encAlgorithm, bArr), new IvParameterSpec(new byte[this.c.getBlockSize()]));
                } else {
                    cipher.init(1, JcaJcePGPUtil.makeSymmetricKey(JcePGPDataEncryptorBuilder.this.encAlgorithm, bArr));
                }
            } catch (InvalidAlgorithmParameterException e) {
                throw new PGPException("imvalid algorithm parameter: " + e.getMessage(), e);
            } catch (InvalidKeyException e2) {
                throw new PGPException("invalid key: " + e2.getMessage(), e2);
            }
        }

        @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptor
        public int getBlockSize() {
            return this.c.getBlockSize();
        }

        @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptor
        public PGPDigestCalculator getIntegrityCalculator() {
            if (JcePGPDataEncryptorBuilder.this.withIntegrityPacket) {
                return new SHA1PGPDigestCalculator();
            }
            return null;
        }

        @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptor
        public OutputStream getOutputStream(OutputStream outputStream) {
            return new CipherOutputStream(outputStream, this.c);
        }
    }

    public JcePGPDataEncryptorBuilder(int i) {
        this.encAlgorithm = i;
        if (i == 0) {
            throw new IllegalArgumentException("null cipher specified");
        }
    }

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public PGPDataEncryptor build(byte[] bArr) throws PGPException {
        return this.aeadAlgorithm > 0 ? new MyAeadDataEncryptor(bArr) : new MyPGPDataEncryptor(bArr);
    }

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public int getAeadAlgorithm() {
        return this.aeadAlgorithm;
    }

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public int getAlgorithm() {
        return this.encAlgorithm;
    }

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public int getChunkSize() {
        return this.chunkSize;
    }

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public SecureRandom getSecureRandom() {
        if (this.random == null) {
            this.random = new SecureRandom();
        }
        return this.random;
    }

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public boolean isV5StyleAEAD() {
        return this.isV5StyleAEAD;
    }

    public JcePGPDataEncryptorBuilder setProvider(String str) {
        this.helper = new OperatorHelper(new NamedJcaJceHelper(str));
        this.aeadHelper = new JceAEADUtil(this.helper);
        return this;
    }

    public JcePGPDataEncryptorBuilder setProvider(Provider provider) {
        this.helper = new OperatorHelper(new ProviderJcaJceHelper(provider));
        this.aeadHelper = new JceAEADUtil(this.helper);
        return this;
    }

    public JcePGPDataEncryptorBuilder setSecureRandom(SecureRandom secureRandom) {
        this.random = secureRandom;
        return this;
    }

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public JcePGPDataEncryptorBuilder setUseV5AEAD() {
        this.isV5StyleAEAD = true;
        return this;
    }

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public JcePGPDataEncryptorBuilder setUseV6AEAD() {
        this.isV5StyleAEAD = false;
        return this;
    }

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public JcePGPDataEncryptorBuilder setWithAEAD(int i, int i2) {
        if (this.encAlgorithm != 7 && this.encAlgorithm != 8 && this.encAlgorithm != 9 && this.encAlgorithm != 11 && this.encAlgorithm != 12 && this.encAlgorithm != 13) {
            throw new IllegalStateException("AEAD algorithms can only be used with AES and Camellia");
        }
        if (i2 < 6) {
            throw new IllegalArgumentException("minimum chunkSize is 6");
        }
        this.aeadAlgorithm = i;
        this.chunkSize = i2 - 6;
        return this;
    }

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public JcePGPDataEncryptorBuilder setWithIntegrityPacket(boolean z) {
        this.withIntegrityPacket = z;
        return this;
    }
}
