package org.bouncycastle.bcpg;

import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;

/* JADX INFO: loaded from: classes.dex */
public class SymmetricKeyEncSessionPacket extends ContainedPacket {
    public static final int VERSION_4 = 4;
    public static final int VERSION_5 = 5;
    public static final int VERSION_6 = 6;
    private int aeadAlgorithm;
    private byte[] authTag;
    private int encAlgorithm;
    private byte[] iv;
    private S2K s2k;
    private byte[] s2kBytes;
    private byte[] secKeyData;
    private int version;

    private SymmetricKeyEncSessionPacket(int i, int i2, int i3, byte[] bArr, S2K s2k, byte[] bArr2, byte[] bArr3) {
        super(3);
        this.version = i;
        this.encAlgorithm = i2;
        this.aeadAlgorithm = i3;
        this.s2k = s2k;
        this.secKeyData = bArr2;
        int iVLength = AEADUtils.getIVLength(i3);
        if (iVLength != bArr.length) {
            throw new IllegalArgumentException("Mismatched AEAD IV length. Expected " + iVLength + ", got " + bArr.length);
        }
        this.iv = bArr;
        int authTagLength = AEADUtils.getAuthTagLength(i3);
        if (authTagLength != bArr3.length) {
            throw new IllegalArgumentException("Mismatched AEAD AuthTag length. Expected " + authTagLength + ", got " + bArr3.length);
        }
        this.authTag = bArr3;
    }

    public SymmetricKeyEncSessionPacket(int i, S2K s2k, byte[] bArr) {
        super(3);
        this.version = 4;
        this.encAlgorithm = i;
        this.s2k = s2k;
        this.secKeyData = bArr;
    }

    public SymmetricKeyEncSessionPacket(BCPGInputStream bCPGInputStream) throws IOException {
        this(bCPGInputStream, false);
    }

    public SymmetricKeyEncSessionPacket(BCPGInputStream bCPGInputStream, boolean z) throws IOException {
        int iVLength;
        super(3, z);
        this.version = bCPGInputStream.read();
        if (this.version == 4) {
            this.encAlgorithm = bCPGInputStream.read();
            this.s2k = new S2K(bCPGInputStream);
            this.secKeyData = bCPGInputStream.readAll();
            return;
        }
        if (this.version != 5 && this.version != 6) {
            throw new UnsupportedPacketVersionException("Unsupported PGP symmetric-key encrypted session key packet version encountered: " + this.version);
        }
        int i = this.version == 6 ? bCPGInputStream.read() : 0;
        this.encAlgorithm = bCPGInputStream.read();
        this.aeadAlgorithm = bCPGInputStream.read();
        if (this.version == 6) {
            iVLength = (i - 3) - bCPGInputStream.read();
        } else {
            try {
                iVLength = AEADUtils.getIVLength(this.aeadAlgorithm);
            } catch (IllegalArgumentException e) {
                throw new MalformedPacketException(e.getMessage(), e);
            }
        }
        if (iVLength < 0) {
            throw new MalformedPacketException("IV length cannot be negative.");
        }
        this.s2k = new S2K(bCPGInputStream);
        this.iv = new byte[iVLength];
        if (bCPGInputStream.read(this.iv) != this.iv.length) {
            throw new EOFException("Premature end of stream.");
        }
        try {
            int authTagLength = AEADUtils.getAuthTagLength(this.aeadAlgorithm);
            this.authTag = new byte[authTagLength];
            byte[] all = bCPGInputStream.readAll();
            if (all.length - authTagLength < 0) {
                throw new MalformedPacketException("AuthTagLen exceeds session key data.");
            }
            this.secKeyData = new byte[all.length - authTagLength];
            System.arraycopy(all, 0, this.secKeyData, 0, this.secKeyData.length);
            System.arraycopy(all, this.secKeyData.length, this.authTag, 0, authTagLength);
        } catch (IllegalArgumentException e2) {
            throw new MalformedPacketException("Unknown AEAD algorithm.", e2);
        }
    }

    public static byte[] createAAData(int i, int i2, int i3) {
        return new byte[]{-61, (byte) (i & 255), (byte) (i2 & 255), (byte) (i3 & 255)};
    }

    public static SymmetricKeyEncSessionPacket createV4Packet(int i, S2K s2k, byte[] bArr) {
        return new SymmetricKeyEncSessionPacket(i, s2k, bArr);
    }

    public static SymmetricKeyEncSessionPacket createV5Packet(int i, int i2, byte[] bArr, S2K s2k, byte[] bArr2, byte[] bArr3) {
        return new SymmetricKeyEncSessionPacket(5, i, i2, bArr, s2k, bArr2, bArr3);
    }

    public static SymmetricKeyEncSessionPacket createV6Packet(int i, int i2, byte[] bArr, S2K s2k, byte[] bArr2, byte[] bArr3) {
        return new SymmetricKeyEncSessionPacket(6, i, i2, bArr, s2k, bArr2, bArr3);
    }

    @Override // org.bouncycastle.bcpg.ContainedPacket
    public void encode(BCPGOutputStream bCPGOutputStream) throws IOException {
        int length;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BCPGOutputStream bCPGOutputStream2 = this.version == 4 ? new BCPGOutputStream(byteArrayOutputStream) : new BCPGOutputStream((OutputStream) byteArrayOutputStream, true);
        bCPGOutputStream2.write(this.version);
        if (this.version == 4) {
            bCPGOutputStream2.write(this.encAlgorithm);
            bCPGOutputStream2.writeObject(this.s2k);
            if (this.secKeyData != null && this.secKeyData.length > 0) {
                bCPGOutputStream2.write(this.secKeyData);
            }
        } else {
            if (this.version == 6) {
                length = this.s2k.getEncoded().length;
                bCPGOutputStream2.write(length + 3 + this.iv.length);
            } else {
                length = 0;
            }
            bCPGOutputStream2.write(this.encAlgorithm);
            bCPGOutputStream2.write(this.aeadAlgorithm);
            if (this.version == 6) {
                bCPGOutputStream2.write(length);
            }
            bCPGOutputStream2.writeObject(this.s2k);
            bCPGOutputStream2.write(this.iv);
            if (this.secKeyData != null && this.secKeyData.length > 0) {
                bCPGOutputStream2.write(this.secKeyData);
            }
            bCPGOutputStream2.write(this.authTag);
        }
        bCPGOutputStream2.close();
        bCPGOutputStream.writePacket(hasNewPacketFormat(), 3, byteArrayOutputStream.toByteArray());
    }

    public byte[] getAAData() {
        return createAAData(getVersion(), getEncAlgorithm(), getAeadAlgorithm());
    }

    public int getAeadAlgorithm() {
        return this.aeadAlgorithm;
    }

    public byte[] getAuthTag() {
        return this.authTag;
    }

    public int getEncAlgorithm() {
        return this.encAlgorithm;
    }

    public byte[] getIv() {
        return this.iv;
    }

    public S2K getS2K() {
        return this.s2k;
    }

    public byte[] getSecKeyData() {
        return this.secKeyData;
    }

    public int getVersion() {
        return this.version;
    }
}
