package org.bouncycastle.openpgp.operator.bc;

import java.io.OutputStream;
import java.security.SecureRandom;
import org.bouncycastle.bcpg.AEADUtils;
import org.bouncycastle.bcpg.SymmetricKeyUtils;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.io.CipherOutputStream;
import org.bouncycastle.crypto.modes.AEADBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
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 BcPGPDataEncryptorBuilder implements PGPDataEncryptorBuilder {
    private int chunkSize;
    private int encAlgorithm;
    private SecureRandom random;
    private boolean withIntegrityPacket = true;
    private boolean isV5StyleAEAD = true;
    private int aeadAlgorithm = -1;

    /* JADX INFO: loaded from: classes3.dex */
    private class MyAeadDataEncryptor implements PGPAEADDataEncryptor {
        private final AEADBlockCipher c;
        private final boolean isV5StyleAEAD;
        private final byte[] iv;
        private final byte[] keyBytes;

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

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

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

        @Override // org.bouncycastle.openpgp.operator.PGPAEADDataEncryptor
        public int getChunkSize() {
            return BcPGPDataEncryptorBuilder.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 BcAEADUtil.PGPAeadOutputStream(this.isV5StyleAEAD, outputStream, this.c, new KeyParameter(this.keyBytes), this.iv, BcPGPDataEncryptorBuilder.this.encAlgorithm, BcPGPDataEncryptorBuilder.this.aeadAlgorithm, BcPGPDataEncryptorBuilder.this.chunkSize);
            } catch (Exception e) {
                throw new IllegalStateException("unable to process stream: " + e.getMessage());
            }
        }
    }

    /* JADX INFO: loaded from: classes2.dex */
    private class MyPGPDataEncryptor implements PGPDataEncryptor {
        private final BufferedBlockCipher c;

        MyPGPDataEncryptor(byte[] bArr) throws PGPException {
            try {
                this.c = BcUtil.createStreamCipher(true, BcImplProvider.createBlockCipher(BcPGPDataEncryptorBuilder.this.encAlgorithm), BcPGPDataEncryptorBuilder.this.withIntegrityPacket, bArr);
            } catch (IllegalArgumentException e) {
                throw new PGPException("invalid parameters: " + e.getMessage(), e);
            }
        }

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

        @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptor
        public PGPDigestCalculator getIntegrityCalculator() {
            if (BcPGPDataEncryptorBuilder.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 BcPGPDataEncryptorBuilder(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 BcPGPDataEncryptorBuilder setSecureRandom(SecureRandom secureRandom) {
        this.random = secureRandom;
        return this;
    }

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

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

    @Override // org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder
    public BcPGPDataEncryptorBuilder 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 BcPGPDataEncryptorBuilder setWithIntegrityPacket(boolean z) {
        this.withIntegrityPacket = z;
        return this;
    }
}
