package J0;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import javax.crypto.AEADBadTagException;

/* JADX INFO: renamed from: J0.b, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public abstract class AbstractC0345b {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public Object f2978a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Object f2979b;

    public AbstractC0345b(byte[] bArr) throws GeneralSecurityException {
        if (!A0.a.a(1)) {
            throw new GeneralSecurityException("Can not use ChaCha20Poly1305 in FIPS-mode.");
        }
        this.f2978a = g(bArr, 1);
        this.f2979b = g(bArr, 0);
    }

    public static byte[] f(byte[] bArr, ByteBuffer byteBuffer) {
        int length = bArr.length % 16 == 0 ? bArr.length : (bArr.length + 16) - (bArr.length % 16);
        int iRemaining = byteBuffer.remaining();
        int i6 = iRemaining % 16;
        int i7 = (i6 == 0 ? iRemaining : (iRemaining + 16) - i6) + length;
        ByteBuffer byteBufferOrder = ByteBuffer.allocate(i7 + 16).order(ByteOrder.LITTLE_ENDIAN);
        byteBufferOrder.put(bArr);
        byteBufferOrder.position(length);
        byteBufferOrder.put(byteBuffer);
        byteBufferOrder.position(i7);
        byteBufferOrder.putLong(bArr.length);
        byteBufferOrder.putLong(iRemaining);
        return byteBufferOrder.array();
    }

    public byte[] a(ByteBuffer byteBuffer, byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        if (byteBuffer.remaining() < 16) {
            throw new GeneralSecurityException("ciphertext too short");
        }
        int iPosition = byteBuffer.position();
        byte[] bArr3 = new byte[16];
        byteBuffer.position(byteBuffer.limit() - 16);
        byteBuffer.get(bArr3);
        byteBuffer.position(iPosition);
        byteBuffer.limit(byteBuffer.limit() - 16);
        if (bArr2 == null) {
            bArr2 = new byte[0];
        }
        try {
            byte[] bArr4 = new byte[32];
            ((F1.L) this.f2979b).b(bArr, 0).get(bArr4);
            if (!MessageDigest.isEqual(A4.g.v(bArr4, f(bArr2, byteBuffer)), bArr3)) {
                throw new GeneralSecurityException("invalid MAC");
            }
            byteBuffer.position(iPosition);
            F1.L l = (F1.L) this.f2978a;
            l.getClass();
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(byteBuffer.remaining());
            l.p(bArr, byteBufferAllocate, byteBuffer);
            return byteBufferAllocate.array();
        } catch (GeneralSecurityException e6) {
            throw new AEADBadTagException(e6.toString());
        }
    }

    public void b(ByteBuffer byteBuffer, byte[] bArr, byte[] bArr2, byte[] bArr3) throws GeneralSecurityException {
        if (byteBuffer.remaining() < bArr2.length + 16) {
            throw new IllegalArgumentException("Given ByteBuffer output is too small");
        }
        int iPosition = byteBuffer.position();
        F1.L l = (F1.L) this.f2978a;
        l.getClass();
        if (byteBuffer.remaining() < bArr2.length) {
            throw new IllegalArgumentException("Given ByteBuffer output is too small");
        }
        l.p(bArr, byteBuffer, ByteBuffer.wrap(bArr2));
        byteBuffer.position(iPosition);
        byteBuffer.limit(byteBuffer.limit() - 16);
        if (bArr3 == null) {
            bArr3 = new byte[0];
        }
        byte[] bArr4 = new byte[32];
        ((F1.L) this.f2979b).b(bArr, 0).get(bArr4);
        byte[] bArrV = A4.g.v(bArr4, f(bArr3, byteBuffer));
        byteBuffer.limit(byteBuffer.limit() + 16);
        byteBuffer.put(bArrV);
    }

    public abstract int[] c(int i6);

    public int[] d(int i6, int i7) {
        if (i6 < 0 || i7 < 0 || i6 == i7) {
            return null;
        }
        int[] iArr = (int[]) this.f2979b;
        iArr[0] = i6;
        iArr[1] = i7;
        return iArr;
    }

    public String e() {
        String str = (String) this.f2978a;
        if (str != null) {
            return str;
        }
        kotlin.jvm.internal.o.n("text");
        throw null;
    }

    public abstract F1.L g(byte[] bArr, int i6);

    public abstract int[] h(int i6);

    public AbstractC0345b() {
        this.f2979b = new int[2];
    }
}
