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

import java.io.Serializable;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/* JADX INFO: loaded from: classes2.dex */
public final class i5 implements ia {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public Serializable f20983a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public Object f20984b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public Serializable f20985c;

    public i5(int i10) {
        if (i10 != 1) {
            this.f20983a = new ArrayList();
            this.f20984b = new ArrayList();
            this.f20985c = new ArrayList();
        } else {
            this.f20983a = new ArrayList();
            this.f20984b = fa.f20911b;
            this.f20985c = null;
        }
    }

    @Override // com.google.android.gms.internal.pal.ia
    public final byte[] a(int i10, byte[] bArr) throws GeneralSecurityException {
        byte[] bArrH0;
        if (i10 > 16) {
            throw new InvalidAlgorithmParameterException("outputLength too large, max is 16 bytes");
        }
        if (!q9.y(1)) {
            throw new GeneralSecurityException("Can not use AES-CMAC in FIPS-mode.");
        }
        Cipher cipher = (Cipher) de.f20763e.a("AES/ECB/NoPadding");
        cipher.init(1, (SecretKey) this.f20983a);
        int length = bArr.length;
        int iMax = Math.max(1, (int) Math.ceil(((double) length) / 16.0d));
        if (iMax * 16 == length) {
            bArrH0 = q9.m0(bArr, (iMax - 1) * 16, 0, (byte[]) this.f20984b, 16);
        } else {
            byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, (iMax - 1) * 16, length);
            int length2 = bArrCopyOfRange.length;
            if (length2 >= 16) {
                throw new IllegalArgumentException("x must be smaller than a block.");
            }
            byte[] bArrCopyOf = Arrays.copyOf(bArrCopyOfRange, 16);
            bArrCopyOf[length2] = -128;
            bArrH0 = q9.h0(bArrCopyOf, (byte[]) this.f20985c);
        }
        byte[] bArrDoFinal = new byte[16];
        for (int i11 = 0; i11 < iMax - 1; i11++) {
            bArrDoFinal = cipher.doFinal(q9.m0(bArrDoFinal, 0, i11 * 16, bArr, 16));
        }
        return Arrays.copyOf(cipher.doFinal(q9.h0(bArrH0, bArrDoFinal)), i10);
    }

    public final ha b() {
        if (((ArrayList) this.f20983a) == null) {
            throw new IllegalStateException("cannot call build() twice");
        }
        Integer num = (Integer) this.f20985c;
        if (num != null) {
            int iIntValue = num.intValue();
            ArrayList arrayList = (ArrayList) this.f20983a;
            int size = arrayList.size();
            int i10 = 0;
            while (i10 < size) {
                int i11 = i10 + 1;
                if (((ga) arrayList.get(i10)).f20939b != iIntValue) {
                    i10 = i11;
                }
            }
            throw new GeneralSecurityException("primary key ID is not present in entries");
        }
        ha haVar = new ha((fa) this.f20984b, Collections.unmodifiableList((ArrayList) this.f20983a), (Integer) this.f20985c);
        this.f20983a = null;
        return haVar;
    }

    /* JADX WARN: Type inference failed for: r3v7, types: [byte[], java.io.Serializable] */
    public i5(byte[] bArr) throws GeneralSecurityException {
        je.a(bArr.length);
        this.f20983a = new SecretKeySpec(bArr, "AES");
        if (q9.y(1)) {
            Cipher cipher = (Cipher) de.f20763e.a("AES/ECB/NoPadding");
            cipher.init(1, (SecretKey) this.f20983a);
            byte[] bArrP = q9.P(cipher.doFinal(new byte[16]));
            this.f20984b = bArrP;
            this.f20985c = q9.P(bArrP);
            return;
        }
        throw new GeneralSecurityException("Can not use AES-CMAC in FIPS-mode.");
    }
}
