package org.bouncycastle.bcpg;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.SecureRandom;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.util.Integers;

/* JADX INFO: loaded from: classes2.dex */
public class S2K extends BCPGObject {
    public static final int ARGON_2 = 4;
    private static final int EXPBIAS = 6;
    public static final int GNU_DUMMY_S2K = 101;
    public static final int GNU_PROTECTION_MODE_DIVERT_TO_CARD = 2;
    public static final int GNU_PROTECTION_MODE_INTERNAL = 3;
    public static final int GNU_PROTECTION_MODE_NO_PRIVATE_KEY = 1;
    public static final int SALTED = 1;
    public static final int SALTED_AND_ITERATED = 3;
    public static final int SIMPLE = 0;
    int algorithm;
    int itCount;
    byte[] iv;
    int memorySizeExponent;
    int parallelism;
    int passes;
    int protectionMode;
    int type;

    /* JADX INFO: loaded from: classes.dex */
    public static class Argon2Params {
        private final int memSizeExp;
        private final int parallelism;
        private final int passes;
        private final byte[] salt;

        public Argon2Params() {
            this(CryptoServicesRegistrar.getSecureRandom());
        }

        public Argon2Params(int i, int i2, int i3, SecureRandom secureRandom) {
            this(mineSalt(secureRandom), i, i2, i3);
        }

        public Argon2Params(SecureRandom secureRandom) {
            this(1, 4, 21, secureRandom);
        }

        public Argon2Params(byte[] bArr, int i, int i2, int i3) {
            if (bArr.length != 16) {
                throw new IllegalArgumentException("Argon2 uses 16 bytes of salt");
            }
            if ((i > 255) || (i < 1)) {
                throw new IllegalArgumentException("Passes MUST be an integer value from 1 to 255.");
            }
            if (i2 < 1 || i2 > 255) {
                throw new IllegalArgumentException("Parallelism MUST be an integer value from 1 to 255.");
            }
            if (i3 < 35 - Integers.numberOfLeadingZeros(i2 - 1) || i3 > 30) {
                throw new IllegalArgumentException("Memory size exponent MUST be an integer value from 3 + bitlen(parallelism - 1) to 30.");
            }
            this.salt = bArr;
            this.passes = i;
            this.parallelism = i2;
            this.memSizeExp = i3;
        }

        public static Argon2Params memoryConstrainedParameters() {
            return new Argon2Params(3, 4, 16, CryptoServicesRegistrar.getSecureRandom());
        }

        private static byte[] mineSalt(SecureRandom secureRandom) {
            byte[] bArr = new byte[16];
            secureRandom.nextBytes(bArr);
            return bArr;
        }

        public static Argon2Params universallyRecommendedParameters() {
            return new Argon2Params(1, 4, 21, CryptoServicesRegistrar.getSecureRandom());
        }

        public int getMemSizeExp() {
            return this.memSizeExp;
        }

        public int getParallelism() {
            return this.parallelism;
        }

        public int getPasses() {
            return this.passes;
        }

        public byte[] getSalt() {
            return this.salt;
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static class GNUDummyParams {
        private final int protectionMode;

        private GNUDummyParams(int i) {
            this.protectionMode = i;
        }

        public static GNUDummyParams divertToCard() {
            return new GNUDummyParams(2);
        }

        public static GNUDummyParams internal() {
            return new GNUDummyParams(3);
        }

        public static GNUDummyParams noPrivateKey() {
            return new GNUDummyParams(1);
        }

        public int getProtectionMode() {
            return this.protectionMode;
        }
    }

    public S2K(int i) {
        this.itCount = -1;
        this.passes = -1;
        this.protectionMode = -1;
        this.type = 0;
        this.algorithm = i;
    }

    public S2K(int i, byte[] bArr) {
        this.itCount = -1;
        this.passes = -1;
        this.protectionMode = -1;
        this.type = 1;
        this.algorithm = i;
        this.iv = bArr;
    }

    public S2K(int i, byte[] bArr, int i2) {
        this.itCount = -1;
        this.passes = -1;
        this.protectionMode = -1;
        this.type = 3;
        this.algorithm = i;
        this.iv = bArr;
        if (i2 >= 256 && i2 <= 65536) {
            throw new IllegalArgumentException("invalid itCount");
        }
        this.itCount = i2;
    }

    S2K(InputStream inputStream) throws IOException {
        this.itCount = -1;
        this.passes = -1;
        this.protectionMode = -1;
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        this.type = dataInputStream.read();
        switch (this.type) {
            case 0:
                this.algorithm = dataInputStream.read();
                return;
            case 1:
                this.algorithm = dataInputStream.read();
                this.iv = new byte[8];
                dataInputStream.readFully(this.iv, 0, this.iv.length);
                return;
            case 3:
                this.algorithm = dataInputStream.read();
                this.iv = new byte[8];
                dataInputStream.readFully(this.iv, 0, this.iv.length);
                this.itCount = dataInputStream.read();
                return;
            case 4:
                this.iv = new byte[16];
                dataInputStream.readFully(this.iv);
                this.passes = dataInputStream.read();
                this.parallelism = dataInputStream.read();
                this.memorySizeExponent = dataInputStream.read();
                return;
            case 101:
                this.algorithm = dataInputStream.read();
                dataInputStream.read();
                dataInputStream.read();
                dataInputStream.read();
                this.protectionMode = dataInputStream.read();
                return;
            default:
                throw new UnsupportedPacketVersionException("Invalid S2K type: " + this.type);
        }
    }

    public S2K(Argon2Params argon2Params) {
        this.itCount = -1;
        this.passes = -1;
        this.protectionMode = -1;
        this.type = 4;
        this.iv = argon2Params.getSalt();
        this.passes = argon2Params.getPasses();
        this.parallelism = argon2Params.getParallelism();
        this.memorySizeExponent = argon2Params.getMemSizeExp();
    }

    public S2K(GNUDummyParams gNUDummyParams) {
        this.itCount = -1;
        this.passes = -1;
        this.protectionMode = -1;
        this.type = 101;
        this.protectionMode = gNUDummyParams.getProtectionMode();
    }

    public static S2K argon2S2K(Argon2Params argon2Params) {
        return new S2K(argon2Params);
    }

    public static S2K gnuDummyS2K(GNUDummyParams gNUDummyParams) {
        return new S2K(gNUDummyParams);
    }

    public static S2K saltedAndIteratedS2K(int i, byte[] bArr, int i2) {
        return new S2K(i, bArr, i2);
    }

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

    public static S2K simpleS2K(int i) {
        return new S2K(i);
    }

    private void writeOneOctetOrThrow(BCPGOutputStream bCPGOutputStream, int i, String str) throws IOException {
        if ((i & (-256)) != 0) {
            throw new IllegalStateException(str + " not encodable");
        }
        bCPGOutputStream.write(i);
    }

    @Override // org.bouncycastle.bcpg.BCPGObject
    public void encode(BCPGOutputStream bCPGOutputStream) throws IOException {
        int i;
        int i2;
        String str;
        bCPGOutputStream.write(this.type);
        switch (this.type) {
            case 0:
                i = this.algorithm;
                bCPGOutputStream.write(i);
                return;
            case 1:
                bCPGOutputStream.write(this.algorithm);
                bCPGOutputStream.write(this.iv);
                return;
            case 3:
                bCPGOutputStream.write(this.algorithm);
                bCPGOutputStream.write(this.iv);
                i2 = this.itCount;
                str = "Iteration count";
                writeOneOctetOrThrow(bCPGOutputStream, i2, str);
                return;
            case 4:
                bCPGOutputStream.write(this.iv);
                writeOneOctetOrThrow(bCPGOutputStream, this.passes, "Passes");
                writeOneOctetOrThrow(bCPGOutputStream, this.parallelism, "Parallelism");
                i2 = this.memorySizeExponent;
                str = "Memory size exponent";
                writeOneOctetOrThrow(bCPGOutputStream, i2, str);
                return;
            case 101:
                bCPGOutputStream.write(this.algorithm);
                bCPGOutputStream.write(71);
                bCPGOutputStream.write(78);
                bCPGOutputStream.write(85);
                i = this.protectionMode;
                bCPGOutputStream.write(i);
                return;
            default:
                throw new IllegalStateException("Unknown S2K type " + this.type);
        }
    }

    public int getHashAlgorithm() {
        return this.algorithm;
    }

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

    public long getIterationCount() {
        int i = this.itCount;
        int i2 = this.itCount;
        return i >= 256 ? i2 : ((i2 & 15) + 16) << ((this.itCount >> 4) + 6);
    }

    public int getMemorySizeExponent() {
        return this.memorySizeExponent;
    }

    public int getParallelism() {
        return this.parallelism;
    }

    public int getPasses() {
        return this.passes;
    }

    public int getProtectionMode() {
        return this.protectionMode;
    }

    public int getType() {
        return this.type;
    }
}
