package org.bouncycastle.bcpg;

import java.io.IOException;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class AEADEncDataPacket extends InputStreamPacket implements AEADAlgorithmTags, BCPGHeaderObject {
    public static final int VERSION_1 = 1;
    private final byte aeadAlgorithm;
    private final byte algorithm;
    private final byte chunkSize;
    private final byte[] iv;
    private final byte version;

    public AEADEncDataPacket(int i, int i2, int i3, byte[] bArr) {
        super(null, 20);
        this.version = (byte) 1;
        this.algorithm = (byte) i;
        this.aeadAlgorithm = (byte) i2;
        this.chunkSize = (byte) i3;
        this.iv = Arrays.clone(bArr);
    }

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

    public AEADEncDataPacket(BCPGInputStream bCPGInputStream, boolean z) throws IOException {
        super(bCPGInputStream, 20, z);
        this.version = (byte) bCPGInputStream.read();
        if (this.version != 1) {
            throw new UnsupportedPacketVersionException("Unknown AEAD packet version: " + ((int) this.version));
        }
        this.algorithm = (byte) bCPGInputStream.read();
        this.aeadAlgorithm = (byte) bCPGInputStream.read();
        this.chunkSize = (byte) bCPGInputStream.read();
        try {
            this.iv = new byte[AEADUtils.getIVLength(this.aeadAlgorithm)];
            bCPGInputStream.readFully(this.iv);
        } catch (IllegalArgumentException e) {
            throw new MalformedPacketException("Unknown AEAD algorithm ID: " + ((int) this.aeadAlgorithm), e);
        }
    }

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

    public static int getIVLength(byte b) {
        return AEADUtils.getIVLength(b);
    }

    @Override // org.bouncycastle.bcpg.BCPGHeaderObject
    public void encode(BCPGOutputStream bCPGOutputStream) throws IOException {
        bCPGOutputStream.write(1);
        bCPGOutputStream.write(getAlgorithm());
        bCPGOutputStream.write(getAEADAlgorithm());
        bCPGOutputStream.write(getChunkSize());
        bCPGOutputStream.write(this.iv);
    }

    public byte[] getAAData() {
        return createAAData(getVersion(), getAlgorithm(), getAEADAlgorithm(), getChunkSize());
    }

    public byte getAEADAlgorithm() {
        return this.aeadAlgorithm;
    }

    public byte getAlgorithm() {
        return this.algorithm;
    }

    public int getChunkSize() {
        return this.chunkSize;
    }

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

    @Override // org.bouncycastle.bcpg.BCPGHeaderObject
    public int getType() {
        return 20;
    }

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