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

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Arrays;

/* JADX INFO: loaded from: classes2.dex */
public final class v8 implements d7 {

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

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

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

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

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

    public v8(wc wcVar, w8 w8Var, o3 o3Var, t8 t8Var) {
        this.f21286a = wcVar;
        this.f21287b = w8Var;
        this.f21289d = o3Var;
        this.f21288c = t8Var;
    }

    @Override // com.google.android.gms.internal.pal.d7
    public final byte[] zza(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        byte[] bArrH0;
        byte[] bArr3 = bArr2 == null ? new byte[0] : bArr2;
        wc wcVar = this.f21286a;
        w8 w8Var = this.f21287b;
        o3 o3Var = this.f21289d;
        t8 t8Var = this.f21288c;
        m5 m5VarA = w8Var.a(wcVar.s().l());
        byte[] bArr4 = (byte[]) m5VarA.f21082c;
        byte[] bArr5 = (byte[]) m5VarA.f21081b;
        byte[] bArrY = q9.Y(y8.f21380n, w8Var.zzb(), o3Var.g(), t8Var.zzb());
        byte[] bArr6 = y8.f21378l;
        byte[] bArr7 = u8.f21246d;
        byte[] bArr8 = y8.f21381o;
        Charset charset = StandardCharsets.UTF_8;
        byte[] bArrY2 = q9.Y(y8.f21367a, o3Var.i(q9.Y(bArr8, bArrY, "psk_id_hash".getBytes(charset), bArr7), bArr6), o3Var.i(q9.Y(bArr8, bArrY, "info_hash".getBytes(charset), bArr3), bArr6));
        byte[] bArrI = o3Var.i(q9.Y(bArr8, bArrY, "secret".getBytes(charset), bArr7), bArr5);
        int iZza = t8Var.zza();
        byte[] bArrH = o3Var.h(bArrI, iZza, y8.c("key", bArrY2, bArrY, iZza));
        byte[] bArrH2 = o3Var.h(bArrI, 12, y8.c("base_nonce", bArrY2, bArrY, 12));
        BigInteger bigInteger = BigInteger.ONE;
        u8 u8Var = new u8(bArr4, bArrH, bArrH2, bigInteger.shiftLeft(96).subtract(bigInteger), t8Var);
        byte[] bArr9 = f21285e;
        synchronized (u8Var) {
            try {
                byte[] byteArray = u8Var.f21249c.toByteArray();
                int length = byteArray.length;
                if (length != 12) {
                    if (length > 13) {
                        throw new GeneralSecurityException("integer too large");
                    }
                    if (length != 13) {
                        byte[] bArr10 = new byte[12];
                        System.arraycopy(byteArray, 0, bArr10, 12 - length, length);
                        byteArray = bArr10;
                    } else {
                        if (byteArray[0] != 0) {
                            throw new GeneralSecurityException("integer too large");
                        }
                        byteArray = Arrays.copyOfRange(byteArray, 1, 13);
                    }
                }
                bArrH0 = q9.h0(bArrH2, byteArray);
                if (u8Var.f21249c.compareTo(u8Var.f21248b) >= 0) {
                    throw new GeneralSecurityException("message limit reached");
                }
                u8Var.f21249c = u8Var.f21249c.add(bigInteger);
            } catch (Throwable th) {
                throw th;
            }
        }
        return q9.Y(bArr4, u8Var.f21247a.a(bArrH, bArrH0, bArr, bArr9));
    }
}
