package fa;

import java.security.GeneralSecurityException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import javax.crypto.Cipher;
import ma.j0;
import ma.p0;
import xyz.stream.utils.f0;

/* JADX INFO: loaded from: classes2.dex */
public final class q implements da.a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final byte[] f26024a;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final pa.c f26026c;

    public q(byte[] bArr, ta.a aVar, int i10) {
        this.f26026c = com.bumptech.glide.g.a(pa.a.R(pa.b.b(bArr.length), f0.e(bArr, da.e.f25073b)));
        this.f26024a = aVar.b();
        this.f26025b = i10;
    }

    @Override // da.a
    public final byte[] a(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        if (bArr == null) {
            throw new NullPointerException("plaintext is null");
        }
        int i10 = this.f26025b;
        int i11 = i10 + 12;
        byte[] bArrA = j0.a(i11);
        byte[] bArrCopyOf = Arrays.copyOf(bArrA, i10);
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArrA, i10, i11);
        k kVar = new k(c(bArrCopyOf));
        byte[] bArr3 = this.f26024a;
        int length = bArr3.length + i10 + bArrCopyOfRange.length;
        if (bArrCopyOfRange.length != 12) {
            throw new GeneralSecurityException("iv is wrong size");
        }
        AlgorithmParameterSpec algorithmParameterSpecA = c.a(0, bArrCopyOfRange.length, bArrCopyOfRange);
        Cipher cipher = (Cipher) c.f25988a.get();
        cipher.init(1, kVar.f26017a, algorithmParameterSpecA);
        if (bArr2 != null && bArr2.length != 0) {
            cipher.updateAAD(bArr2);
        }
        int outputSize = cipher.getOutputSize(bArr.length);
        if (outputSize > Integer.MAX_VALUE - length) {
            throw new GeneralSecurityException("plaintext too long");
        }
        byte[] bArr4 = new byte[length + outputSize];
        if (cipher.doFinal(bArr, 0, bArr.length, bArr4, length) != outputSize) {
            throw new GeneralSecurityException("not enough data written");
        }
        System.arraycopy(bArr3, 0, bArr4, 0, bArr3.length);
        System.arraycopy(bArrA, 0, bArr4, bArr3.length, bArrA.length);
        return bArr4;
    }

    @Override // da.a
    public final byte[] b(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        if (bArr == null) {
            throw new NullPointerException("ciphertext is null");
        }
        int length = bArr.length;
        byte[] bArr3 = this.f26024a;
        int length2 = bArr3.length;
        int i10 = this.f26025b;
        if (length < length2 + i10 + 28) {
            throw new GeneralSecurityException("ciphertext too short");
        }
        if (!p0.b(bArr3, bArr)) {
            throw new GeneralSecurityException("Decryption failed (OutputPrefix mismatch).");
        }
        int length3 = bArr3.length + i10;
        k kVar = new k(c(Arrays.copyOfRange(bArr, bArr3.length, length3)));
        int i11 = length3 + 12;
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, length3, i11);
        if (bArrCopyOfRange.length != 12) {
            throw new GeneralSecurityException("iv is wrong size");
        }
        if (bArr.length < length3 + 28) {
            throw new GeneralSecurityException("ciphertext too short");
        }
        AlgorithmParameterSpec algorithmParameterSpecA = c.a(0, bArrCopyOfRange.length, bArrCopyOfRange);
        Cipher cipher = (Cipher) c.f25988a.get();
        cipher.init(2, kVar.f26017a, algorithmParameterSpecA);
        if (bArr2 != null && bArr2.length != 0) {
            cipher.updateAAD(bArr2);
        }
        return cipher.doFinal(bArr, i11, bArr.length - i11);
    }

    public final byte[] c(byte[] bArr) throws GeneralSecurityException {
        byte[] bArr2 = {0, 1, 88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        byte[] bArr3 = {0, 2, 88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        if (bArr.length > 12 || bArr.length < 8) {
            throw new GeneralSecurityException("invalid salt size");
        }
        System.arraycopy(bArr, 0, bArr2, 4, bArr.length);
        System.arraycopy(bArr, 0, bArr3, 4, bArr.length);
        byte[] bArr4 = new byte[32];
        pa.c cVar = this.f26026c;
        System.arraycopy(cVar.e(16, bArr2), 0, bArr4, 0, 16);
        System.arraycopy(cVar.e(16, bArr3), 0, bArr4, 16, 16);
        return bArr4;
    }
}
