package sa;

import com.google.android.gms.common.api.internal.x0;
import com.google.android.gms.measurement.internal.c2;
import com.google.crypto.tink.config.internal.TinkFipsUtil$AlgorithmFipsCompatibility;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.AEADBadTagException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import ma.p0;
import xyz.stream.utils.f0;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final byte[] f32718d = new byte[16];

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final byte[] f32719e = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final x0 f32720f = new x0(22);

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

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

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

    public d(byte[] bArr, ta.a aVar) throws GeneralSecurityException {
        if (!TinkFipsUtil$AlgorithmFipsCompatibility.f23814a.a()) {
            throw new GeneralSecurityException("Can not use AES-SIV in FIPS-mode.");
        }
        if (bArr.length != 32 && bArr.length != 64) {
            throw new InvalidKeyException(u.a.b(new StringBuilder("invalid key size: "), bArr.length, " bytes; key must have 32 or 64 bytes"));
        }
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, 0, bArr.length / 2);
        this.f32722b = Arrays.copyOfRange(bArr, bArr.length / 2, bArr.length);
        this.f32721a = com.bumptech.glide.g.a(pa.a.R(pa.b.b(bArrCopyOfRange.length), f0.e(bArrCopyOfRange, da.e.f25073b)));
        this.f32723c = aVar.b();
    }

    public static void d(int i10) throws GeneralSecurityException {
        if (i10 > 126) {
            throw new GeneralSecurityException(defpackage.e.e("Too many associated datas: ", i10, " > 126"));
        }
    }

    @Override // da.b
    public final byte[] a(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        byte[][] bArr3 = {bArr2};
        d(1);
        int length = bArr.length;
        byte[] bArr4 = this.f32723c;
        if (length > 2147483631 - bArr4.length) {
            throw new GeneralSecurityException("plaintext too long");
        }
        Cipher cipher = (Cipher) f32720f.get();
        byte[][] bArr5 = (byte[][]) Arrays.copyOf(bArr3, 2);
        bArr5[1] = bArr;
        byte[] bArrC = c(bArr5);
        byte[] bArr6 = (byte[]) bArrC.clone();
        bArr6[8] = (byte) (bArr6[8] & 127);
        bArr6[12] = (byte) (bArr6[12] & 127);
        cipher.init(1, new SecretKeySpec(this.f32722b, "AES"), new IvParameterSpec(bArr6));
        byte[] bArrCopyOf = Arrays.copyOf(bArr4, bArr4.length + bArrC.length + bArr.length);
        System.arraycopy(bArrC, 0, bArrCopyOf, bArr4.length, bArrC.length);
        if (cipher.doFinal(bArr, 0, bArr.length, bArrCopyOf, bArr4.length + bArrC.length) == bArr.length) {
            return bArrCopyOf;
        }
        throw new GeneralSecurityException("not enough data written");
    }

    @Override // da.b
    public final byte[] b(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        byte[][] bArr3 = {bArr2};
        d(1);
        int length = bArr.length;
        byte[] bArr4 = this.f32723c;
        if (length < bArr4.length + 16) {
            throw new GeneralSecurityException("Ciphertext too short.");
        }
        if (!p0.b(bArr4, bArr)) {
            throw new GeneralSecurityException("Decryption failed (OutputPrefix mismatch).");
        }
        Cipher cipher = (Cipher) f32720f.get();
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, bArr4.length, bArr4.length + 16);
        byte[] bArr5 = (byte[]) bArrCopyOfRange.clone();
        bArr5[8] = (byte) (bArr5[8] & 127);
        bArr5[12] = (byte) (bArr5[12] & 127);
        cipher.init(2, new SecretKeySpec(this.f32722b, "AES"), new IvParameterSpec(bArr5));
        int length2 = bArr4.length + 16;
        int length3 = bArr.length - length2;
        byte[] bArrDoFinal = cipher.doFinal(bArr, length2, length3);
        if (length3 == 0 && bArrDoFinal == null && "The Android Project".equals(System.getProperty("java.vendor"))) {
            bArrDoFinal = new byte[0];
        }
        byte[][] bArr6 = (byte[][]) Arrays.copyOf(bArr3, 2);
        bArr6[1] = bArrDoFinal;
        if (MessageDigest.isEqual(bArrCopyOfRange, c(bArr6))) {
            return bArrDoFinal;
        }
        throw new AEADBadTagException("Integrity check failed.");
    }

    public final byte[] c(byte[]... bArr) {
        byte[] bArrP;
        int length = bArr.length;
        pa.c cVar = this.f32721a;
        if (length == 0) {
            return cVar.e(16, f32719e);
        }
        byte[] bArrE = cVar.e(16, f32718d);
        for (int i10 = 0; i10 < bArr.length - 1; i10++) {
            byte[] bArr2 = bArr[i10];
            if (bArr2 == null) {
                bArr2 = new byte[0];
            }
            bArrE = c2.p(com.bumptech.glide.f.c(bArrE), cVar.e(16, bArr2));
        }
        byte[] bArr3 = bArr[bArr.length - 1];
        if (bArr3.length >= 16) {
            if (bArr3.length < bArrE.length) {
                throw new IllegalArgumentException("xorEnd requires a.length >= b.length");
            }
            int length2 = bArr3.length - bArrE.length;
            bArrP = Arrays.copyOf(bArr3, bArr3.length);
            for (int i11 = 0; i11 < bArrE.length; i11++) {
                int i12 = length2 + i11;
                bArrP[i12] = (byte) (bArrP[i12] ^ bArrE[i11]);
            }
        } else {
            if (bArr3.length >= 16) {
                throw new IllegalArgumentException("x must be smaller than a block.");
            }
            byte[] bArrCopyOf = Arrays.copyOf(bArr3, 16);
            bArrCopyOf[bArr3.length] = -128;
            bArrP = c2.p(bArrCopyOf, com.bumptech.glide.f.c(bArrE));
        }
        return cVar.e(16, bArrP);
    }
}
