package org.bouncycastle.openpgp;

import java.io.IOException;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.bcpg.AEADEncDataPacket;
import org.bouncycastle.bcpg.BCPGHeaderObject;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.SymmetricEncDataPacket;
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator;
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.openpgp.operator.PGPKeyEncryptionMethodGenerator;
import org.bouncycastle.util.io.TeeOutputStream;

/* JADX INFO: loaded from: classes.dex */
public class PGPEncryptedDataGenerator implements SymmetricKeyAlgorithmTags, StreamGenerator {
    public static final int S2K_SHA1 = 2;
    public static final int S2K_SHA224 = 11;
    public static final int S2K_SHA256 = 8;
    public static final int S2K_SHA384 = 9;
    public static final int S2K_SHA512 = 10;
    private OutputStream cOut;
    private PGPDataEncryptorBuilder dataEncryptorBuilder;
    private int defAlgorithm;
    private PGPDigestCalculator digestCalc;
    private boolean forceSessionKey;
    private OutputStream genOut;
    private List<PGPKeyEncryptionMethodGenerator> methods;
    private BCPGOutputStream pOut;
    private SecureRandom rand;
    private byte[] salt;
    private SessionKeyExtractionCallback sessionKeyExtractionCallback;
    private boolean useOldFormat;

    /* JADX INFO: loaded from: classes3.dex */
    private static class ClosableBCPGOutputStream extends BCPGOutputStream {
        public ClosableBCPGOutputStream(OutputStream outputStream, BCPGHeaderObject bCPGHeaderObject, long j) throws IOException {
            super(outputStream, bCPGHeaderObject.getType(), j);
            bCPGHeaderObject.encode(this);
        }

        public ClosableBCPGOutputStream(OutputStream outputStream, BCPGHeaderObject bCPGHeaderObject, long j, boolean z) throws IOException {
            super(outputStream, bCPGHeaderObject.getType(), j, z);
            bCPGHeaderObject.encode(this);
        }

        public ClosableBCPGOutputStream(OutputStream outputStream, BCPGHeaderObject bCPGHeaderObject, byte[] bArr) throws IOException {
            super(outputStream, bCPGHeaderObject.getType(), bArr);
            bCPGHeaderObject.encode(this);
        }

        @Override // org.bouncycastle.bcpg.BCPGOutputStream, java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            finish();
        }
    }

    /* JADX INFO: loaded from: classes2.dex */
    public interface SessionKeyExtractionCallback {
        void extractSessionKey(PGPSessionKey pGPSessionKey);
    }

    public PGPEncryptedDataGenerator(PGPDataEncryptorBuilder pGPDataEncryptorBuilder) {
        this(pGPDataEncryptorBuilder, false);
    }

    public PGPEncryptedDataGenerator(PGPDataEncryptorBuilder pGPDataEncryptorBuilder, boolean z) {
        this.useOldFormat = false;
        this.salt = new byte[32];
        this.methods = new ArrayList();
        this.forceSessionKey = true;
        this.sessionKeyExtractionCallback = null;
        this.dataEncryptorBuilder = pGPDataEncryptorBuilder;
        this.useOldFormat = z;
        this.defAlgorithm = this.dataEncryptorBuilder.getAlgorithm();
        this.rand = this.dataEncryptorBuilder.getSecureRandom();
        this.rand.nextBytes(this.salt);
    }

    private OutputStream open(OutputStream outputStream, long j, byte[] bArr) throws IllegalStateException, IOException, PGPException {
        byte[] key;
        byte[] bArrDeriveMessageKeyAndIv;
        BCPGHeaderObject symmetricEncDataPacket;
        BCPGHeaderObject bCPGHeaderObjectCreateVersion2Packet;
        int length;
        if (this.cOut != null) {
            throw new IllegalStateException("generator already in open state");
        }
        if (this.methods.size() == 0) {
            throw new IllegalStateException("no encryption methods specified");
        }
        this.pOut = new BCPGOutputStream(outputStream, !this.useOldFormat);
        boolean z = !this.forceSessionKey && this.methods.size() == 1 && (this.methods.get(0) instanceof PBEKeyEncryptionMethodGenerator);
        boolean zIsV5StyleAEAD = this.dataEncryptorBuilder.isV5StyleAEAD();
        if (this.dataEncryptorBuilder.getAeadAlgorithm() == -1 || zIsV5StyleAEAD) {
            key = z ? ((PBEKeyEncryptionMethodGenerator) this.methods.get(0)).getKey(this.defAlgorithm) : PGPUtil.makeRandomKey(this.defAlgorithm, this.rand);
            bArrDeriveMessageKeyAndIv = key;
        } else {
            key = PGPUtil.makeRandomKey(this.defAlgorithm, this.rand);
            bArrDeriveMessageKeyAndIv = AEADUtil.deriveMessageKeyAndIv(this.dataEncryptorBuilder.getAeadAlgorithm(), this.defAlgorithm, key, this.salt, SymmetricEncIntegrityPacket.createAAData(2, this.defAlgorithm, this.dataEncryptorBuilder.getAeadAlgorithm(), this.dataEncryptorBuilder.getChunkSize()));
        }
        if (this.sessionKeyExtractionCallback != null) {
            this.sessionKeyExtractionCallback.extractSessionKey(new PGPSessionKey(this.defAlgorithm, key));
        }
        PGPDataEncryptor pGPDataEncryptorBuild = this.dataEncryptorBuilder.build(bArrDeriveMessageKeyAndIv);
        this.digestCalc = pGPDataEncryptorBuild.getIntegrityCalculator();
        for (int i = 0; i < this.methods.size(); i++) {
            this.pOut.writePacket(this.methods.get(i).generate(this.dataEncryptorBuilder, key));
        }
        try {
            if (pGPDataEncryptorBuild instanceof PGPAEADDataEncryptor) {
                PGPAEADDataEncryptor pGPAEADDataEncryptor = (PGPAEADDataEncryptor) pGPDataEncryptorBuild;
                if (zIsV5StyleAEAD) {
                    byte[] iv = pGPAEADDataEncryptor.getIV();
                    bCPGHeaderObjectCreateVersion2Packet = new AEADEncDataPacket(this.defAlgorithm, pGPAEADDataEncryptor.getAEADAlgorithm(), pGPAEADDataEncryptor.getChunkSize(), iv);
                    length = iv.length;
                } else {
                    bCPGHeaderObjectCreateVersion2Packet = SymmetricEncIntegrityPacket.createVersion2Packet(this.defAlgorithm, pGPAEADDataEncryptor.getAEADAlgorithm(), pGPAEADDataEncryptor.getChunkSize(), this.salt);
                    length = this.salt.length;
                }
                long j2 = length;
                if (bArr == null) {
                    long chunkSize = 1 << (pGPAEADDataEncryptor.getChunkSize() + 6);
                    this.pOut = new ClosableBCPGOutputStream(outputStream, bCPGHeaderObjectCreateVersion2Packet, j + ((((j + chunkSize) - 1) / chunkSize) * 16) + 16 + 4 + j2);
                } else {
                    this.pOut = new ClosableBCPGOutputStream(outputStream, bCPGHeaderObjectCreateVersion2Packet, bArr);
                }
                OutputStream outputStream2 = pGPDataEncryptorBuild.getOutputStream(this.pOut);
                this.cOut = outputStream2;
                this.genOut = outputStream2;
            } else {
                if (this.digestCalc != null) {
                    symmetricEncDataPacket = SymmetricEncIntegrityPacket.createVersion1Packet();
                    if (this.useOldFormat) {
                        throw new PGPException("symmetric-enc-integrity packets not supported in old PGP format");
                    }
                } else {
                    symmetricEncDataPacket = new SymmetricEncDataPacket();
                }
                BCPGHeaderObject bCPGHeaderObject = symmetricEncDataPacket;
                this.pOut = bArr == null ? new ClosableBCPGOutputStream(outputStream, bCPGHeaderObject, this.digestCalc == null ? j + ((long) pGPDataEncryptorBuild.getBlockSize()) + 2 : 22 + j + ((long) pGPDataEncryptorBuild.getBlockSize()) + 2 + 1, this.useOldFormat) : new ClosableBCPGOutputStream(outputStream, bCPGHeaderObject, bArr);
                OutputStream outputStream3 = pGPDataEncryptorBuild.getOutputStream(this.pOut);
                this.cOut = outputStream3;
                this.genOut = outputStream3;
                if (this.digestCalc != null) {
                    this.genOut = new TeeOutputStream(this.digestCalc.getOutputStream(), this.cOut);
                }
                int blockSize = pGPDataEncryptorBuild.getBlockSize() + 2;
                byte[] bArr2 = new byte[blockSize];
                this.rand.nextBytes(bArr2);
                bArr2[blockSize - 1] = bArr2[blockSize - 3];
                bArr2[blockSize - 2] = bArr2[blockSize - 4];
                this.genOut.write(bArr2);
            }
            return new WrappedGeneratorStream(this.genOut, this);
        } catch (IOException e) {
            throw e;
        } catch (Exception e2) {
            throw new PGPException("Exception creating cipher", e2);
        }
    }

    public void addMethod(PGPKeyEncryptionMethodGenerator pGPKeyEncryptionMethodGenerator) {
        this.methods.add(pGPKeyEncryptionMethodGenerator);
    }

    @Override // org.bouncycastle.openpgp.StreamGenerator
    public void close() throws IOException {
        if (this.cOut != null) {
            if (this.digestCalc != null) {
                BCPGOutputStream bCPGOutputStream = new BCPGOutputStream(this.genOut, 19, 20L);
                bCPGOutputStream.finish();
                bCPGOutputStream.flush();
                this.cOut.write(this.digestCalc.getDigest());
            }
            this.cOut.close();
            this.cOut = null;
            this.pOut = null;
        }
    }

    public OutputStream open(OutputStream outputStream, long j) throws IOException, PGPException {
        return open(outputStream, j, null);
    }

    public OutputStream open(OutputStream outputStream, byte[] bArr) throws IOException, PGPException {
        return open(outputStream, 0L, bArr);
    }

    public void setForceSessionKey(boolean z) {
        this.forceSessionKey = z;
    }

    public void setSessionKeyExtractionCallback(SessionKeyExtractionCallback sessionKeyExtractionCallback) {
        this.sessionKeyExtractionCallback = sessionKeyExtractionCallback;
    }
}
