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

import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final y82 f16818a;

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

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

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

    public t82(o82 o82Var, b92 b92Var, int i10, byte[] bArr) {
        this.f16818a = o82Var;
        this.f16819b = b92Var;
        this.f16820c = i10;
        this.f16821d = bArr;
    }

    public static t82 a(ow1 ow1Var) {
        byte[] bArrH = ow1Var.f15185c.h();
        uw1 uw1Var = ow1Var.f15184b;
        o82 o82Var = new o82(bArrH, uw1Var.f17406c);
        String strValueOf = String.valueOf(uw1Var.f17409f);
        a92 a92Var = new a92("HMAC".concat(strValueOf), new SecretKeySpec(ow1Var.f15186d.h(), "HMAC"), 0);
        int i10 = uw1Var.f17407d;
        return new t82(o82Var, new b92(a92Var, i10), i10, ow1Var.f15187e.b());
    }

    @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.f16821d;
        int length2 = bArr3.length;
        int i10 = this.f16820c;
        if (length < i10 + length2) {
            throw new GeneralSecurityException("Decryption failed (ciphertext too short).");
        }
        if (!q12.c(bArr3, bArr)) {
            throw new GeneralSecurityException("Decryption failed (OutputPrefix mismatch).");
        }
        int i11 = length - i10;
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, length2, i11);
        byte[] bArrCopyOfRange2 = Arrays.copyOfRange(bArr, i11, length);
        if (bArr2 == null) {
            bArr2 = new byte[0];
        }
        byte[] bArrG = ih1.G(bArr2, bArrCopyOfRange, Arrays.copyOf(ByteBuffer.allocate(8).putLong(((long) bArr2.length) * 8).array(), 8));
        b92 b92Var = (b92) this.f16819b;
        byte[] bArr4 = b92Var.f9455d;
        int length3 = bArr4.length;
        int i12 = b92Var.f9453b;
        q22 q22Var = b92Var.f9452a;
        byte[] bArr5 = b92Var.f9454c;
        if (!MessageDigest.isEqual(length3 > 0 ? ih1.G(bArr5, q22Var.a(i12, ih1.G(bArrG, bArr4))) : ih1.G(bArr5, q22Var.a(i12, bArrG)), bArrCopyOfRange2)) {
            throw new GeneralSecurityException("invalid MAC");
        }
        o82 o82Var = (o82) this.f16818a;
        o82Var.getClass();
        int length4 = bArrCopyOfRange.length;
        int i13 = o82Var.f14959b;
        if (length4 < i13) {
            throw new GeneralSecurityException("ciphertext too short");
        }
        byte[] bArr6 = new byte[i13];
        System.arraycopy(bArrCopyOfRange, 0, bArr6, 0, i13);
        int i14 = length4 - i13;
        byte[] bArr7 = new byte[i14];
        Cipher cipher = (Cipher) o82.f14957d.get();
        byte[] bArr8 = new byte[o82Var.f14960c];
        System.arraycopy(bArr6, 0, bArr8, 0, i13);
        cipher.init(2, o82Var.f14958a, new IvParameterSpec(bArr8));
        if (cipher.doFinal(bArrCopyOfRange, i13, i14, bArr7, 0) == i14) {
            return bArr7;
        }
        throw new GeneralSecurityException("stored output's length does not match input's length");
    }
}
