package org.bouncycastle.bcpg;

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

/* JADX INFO: loaded from: classes.dex */
public class SecretKeyPacket extends ContainedPacket implements PublicKeyAlgorithmTags {
    public static final int MAX_S2K_ENCODING_LEN = 8192;
    public static final int USAGE_AEAD = 253;
    public static final int USAGE_CHECKSUM = 255;
    public static final int USAGE_NONE = 0;
    public static final int USAGE_SHA1 = 254;
    private int aeadAlgorithm;
    private int encAlgorithm;
    private byte[] iv;
    private PublicKeyPacket pubKeyPacket;
    private S2K s2k;
    private int s2kUsage;
    private byte[] secKeyData;

    SecretKeyPacket(int i, BCPGInputStream bCPGInputStream) throws IOException {
        this(i, bCPGInputStream, false);
    }

    SecretKeyPacket(int i, BCPGInputStream bCPGInputStream, boolean z) throws IOException {
        super(i, z);
        this.pubKeyPacket = this instanceof SecretSubkeyPacket ? new PublicSubkeyPacket(bCPGInputStream, z) : new PublicKeyPacket(bCPGInputStream, z);
        int version = this.pubKeyPacket.getVersion();
        this.s2kUsage = bCPGInputStream.read();
        if (version == 5 || (version == 6 && this.s2kUsage != 0)) {
            bCPGInputStream.read();
        }
        this.encAlgorithm = (this.s2kUsage == 255 || this.s2kUsage == 254 || this.s2kUsage == 253) ? bCPGInputStream.read() : this.s2kUsage;
        if (this.s2kUsage == 253) {
            this.aeadAlgorithm = bCPGInputStream.read();
        }
        if (version == 6 && (this.s2kUsage == 254 || this.s2kUsage == 253)) {
            byte[] bArr = new byte[sanitizeLength(bCPGInputStream.read(), 8192, "S2K length octet")];
            bCPGInputStream.readFully(bArr);
            this.s2k = new S2K(new ByteArrayInputStream(bArr));
        } else if (this.s2kUsage == 255 || this.s2kUsage == 254 || this.s2kUsage == 253) {
            this.s2k = new S2K(bCPGInputStream);
        }
        if (this.s2kUsage == 253) {
            try {
                this.iv = new byte[AEADUtils.getIVLength(this.aeadAlgorithm)];
                bCPGInputStream.readFully(this.iv);
            } catch (IllegalArgumentException e) {
                throw new MalformedPacketException("Unknown AEAD algorithm", e);
            }
        } else if ((this.s2k == null || this.s2k.getType() != 101 || this.s2k.getProtectionMode() != 1) && this.s2kUsage != 0 && this.iv == null) {
            if (this.encAlgorithm < 7) {
                this.iv = new byte[8];
            } else {
                this.iv = new byte[16];
            }
            bCPGInputStream.readFully(this.iv, 0, this.iv.length);
        }
        if (version != 5) {
            this.secKeyData = bCPGInputStream.readAll();
            return;
        }
        long j = (((long) bCPGInputStream.read()) << 8) | (((long) bCPGInputStream.read()) << 24) | (((long) bCPGInputStream.read()) << 16) | ((long) bCPGInputStream.read());
        this.secKeyData = new byte[sanitizeLength((int) ((this.s2kUsage == 255 || this.s2kUsage == 0) ? j + 2 : j), 2097152, "Key octet count")];
        bCPGInputStream.readFully(this.secKeyData);
    }

    SecretKeyPacket(int i, PublicKeyPacket publicKeyPacket, int i2, int i3, int i4, S2K s2k, byte[] bArr, byte[] bArr2) {
        super(i, publicKeyPacket.hasNewPacketFormat());
        this.pubKeyPacket = publicKeyPacket;
        this.encAlgorithm = i2;
        this.aeadAlgorithm = i3;
        this.s2kUsage = i4;
        this.s2k = s2k;
        this.iv = Arrays.clone(bArr);
        this.secKeyData = bArr2;
        if (s2k != null && s2k.getType() == 4 && i4 != 253) {
            throw new IllegalArgumentException("Argon2 is only used with AEAD (S2K usage octet 253)");
        }
        if (publicKeyPacket.getVersion() == 6 && i4 == 255) {
            throw new IllegalArgumentException("Version 6 keys MUST NOT use S2K usage USAGE_CHECKSUM");
        }
    }

    SecretKeyPacket(int i, PublicKeyPacket publicKeyPacket, int i2, S2K s2k, byte[] bArr, byte[] bArr2) {
        this(i, publicKeyPacket, i2, 0, i2 != 0 ? 255 : 0, s2k, bArr, bArr2);
    }

    SecretKeyPacket(BCPGInputStream bCPGInputStream) throws IOException {
        this(5, bCPGInputStream);
    }

    SecretKeyPacket(BCPGInputStream bCPGInputStream, boolean z) throws IOException {
        this(5, bCPGInputStream, z);
    }

    public SecretKeyPacket(PublicKeyPacket publicKeyPacket, int i, int i2, int i3, S2K s2k, byte[] bArr, byte[] bArr2) {
        this(5, publicKeyPacket, i, i2, i3, s2k, bArr, bArr2);
    }

    public SecretKeyPacket(PublicKeyPacket publicKeyPacket, int i, int i2, S2K s2k, byte[] bArr, byte[] bArr2) {
        this(5, publicKeyPacket, i, 0, i2, s2k, bArr, bArr2);
    }

    public SecretKeyPacket(PublicKeyPacket publicKeyPacket, int i, S2K s2k, byte[] bArr, byte[] bArr2) {
        this(5, publicKeyPacket, i, s2k, bArr, bArr2);
    }

    private byte[] encodeConditionalParameters() throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        if (this.s2kUsage == 255 || this.s2kUsage == 254 || this.s2kUsage == 253) {
            byteArrayOutputStream.write(this.encAlgorithm);
            if (this.s2kUsage == 253) {
                byteArrayOutputStream.write(this.aeadAlgorithm);
            }
            byte[] encoded = this.s2k.getEncoded();
            if (this.pubKeyPacket.getVersion() == 6) {
                byteArrayOutputStream.write(encoded.length);
            }
            byteArrayOutputStream.write(encoded);
        }
        if (this.iv != null) {
            byteArrayOutputStream.write(this.iv);
        }
        return byteArrayOutputStream.toByteArray();
    }

    @Override // org.bouncycastle.bcpg.ContainedPacket
    public void encode(BCPGOutputStream bCPGOutputStream) throws IOException {
        bCPGOutputStream.writePacket(hasNewPacketFormat(), getPacketTag(), getEncodedContents());
    }

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

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

    public byte[] getEncodedContents() throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BCPGOutputStream bCPGOutputStream = new BCPGOutputStream(byteArrayOutputStream);
        bCPGOutputStream.write(this.pubKeyPacket.getEncodedContents());
        bCPGOutputStream.write(this.s2kUsage);
        byte[] bArrEncodeConditionalParameters = encodeConditionalParameters();
        if (this.pubKeyPacket.getVersion() == 5 || (this.pubKeyPacket.getVersion() == 6 && this.s2kUsage != 0)) {
            bCPGOutputStream.write(bArrEncodeConditionalParameters.length);
        }
        bCPGOutputStream.write(bArrEncodeConditionalParameters);
        if (this.secKeyData != null && this.secKeyData.length > 0) {
            if (this.pubKeyPacket.getVersion() == 5) {
                int length = this.secKeyData.length;
                if (this.s2kUsage == 255 || this.s2kUsage == 0) {
                    length -= 2;
                }
                StreamUtil.write4OctetLength(bCPGOutputStream, length);
            }
            bCPGOutputStream.write(this.secKeyData);
        }
        bCPGOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    }

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

    public PublicKeyPacket getPublicKeyPacket() {
        return this.pubKeyPacket;
    }

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

    public int getS2KUsage() {
        return this.s2kUsage;
    }

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