package com.google.android.gms.internal.ads;

import java.security.GeneralSecurityException;
import javax.crypto.AEADBadTagException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/* JADX INFO: loaded from: classes2.dex */
public final class p82 implements qv1 {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final com.google.android.gms.common.api.internal.x0 f15302e = new com.google.android.gms.common.api.internal.x0(8);

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final SecretKeySpec f15305c;

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

    public p82(byte[] bArr, int i10, byte[] bArr2) throws GeneralSecurityException {
        if (!ih1.z(1)) {
            throw new GeneralSecurityException("Can not use AES-EAX in FIPS-mode.");
        }
        if (i10 != 12 && i10 != 16) {
            throw new IllegalArgumentException("IV size should be either 12 or 16 bytes");
        }
        this.f15306d = i10;
        int length = bArr.length;
        h92.a(length);
        this.f15305c = new SecretKeySpec(bArr, "AES");
        this.f15304b = ih1.W(o22.z(p22.b(length), jc1.d(bArr)));
        this.f15303a = bArr2;
    }

    public static p82 a(vw1 vw1Var) throws GeneralSecurityException {
        if (!ih1.z(1)) {
            throw new GeneralSecurityException("Can not use AES-EAX in FIPS-mode.");
        }
        vw1Var.getClass();
        return new p82(vw1Var.f17786c.h(), vw1Var.f17785b.f9303b, vw1Var.f17787d.b());
    }

    public final byte[] b(int i10, int i11, int i12, byte[] bArr) {
        byte[] bArr2 = new byte[i12 + 16];
        bArr2[15] = (byte) i10;
        System.arraycopy(bArr, i11, bArr2, 16, i12);
        return this.f15304b.a(16, bArr2);
    }

    @Override // com.google.android.gms.internal.ads.qv1
    public final byte[] zza(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        int length = bArr.length;
        byte[] bArr3 = this.f15303a;
        int length2 = bArr3.length;
        int i10 = this.f15306d;
        int i11 = ((length - length2) - i10) - 16;
        if (i11 < 0) {
            throw new GeneralSecurityException("ciphertext too short");
        }
        if (!q12.c(bArr3, bArr)) {
            throw new GeneralSecurityException("Decryption failed (OutputPrefix mismatch).");
        }
        byte[] bArrB = b(0, length2, i10, bArr);
        if (bArr2 == null) {
            bArr2 = new byte[0];
        }
        byte[] bArrB2 = b(1, 0, bArr2.length, bArr2);
        byte[] bArrB3 = b(2, length2 + i10, i11, bArr);
        int i12 = length - 16;
        byte b10 = 0;
        for (int i13 = 0; i13 < 16; i13++) {
            b10 = (byte) (b10 | (((bArr[i12 + i13] ^ bArrB2[i13]) ^ bArrB[i13]) ^ bArrB3[i13]));
        }
        if (b10 != 0) {
            throw new AEADBadTagException("tag mismatch");
        }
        Cipher cipher = (Cipher) f15302e.get();
        cipher.init(1, this.f15305c, new IvParameterSpec(bArrB));
        return cipher.doFinal(bArr, bArr3.length + i10, i11);
    }
}
