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

import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/* JADX INFO: loaded from: classes2.dex */
public final class r22 implements q22 {

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

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

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

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

    public r22(byte[] bArr) throws GeneralSecurityException {
        h92.a(bArr.length);
        SecretKeySpec secretKeySpec = new SecretKeySpec(bArr, "AES");
        this.f15970a = secretKeySpec;
        if (!ih1.z(1)) {
            throw new GeneralSecurityException("Can not use AES-CMAC in FIPS-mode.");
        }
        Cipher cipher = (Cipher) f15969d.get();
        cipher.init(1, secretKeySpec);
        byte[] bArrE = ih1.E(cipher.doFinal(new byte[16]));
        this.f15971b = bArrE;
        this.f15972c = ih1.E(bArrE);
    }

    @Override // com.google.android.gms.internal.ads.q22, com.google.android.gms.internal.pal.ia
    public final byte[] a(int i10, byte[] bArr) throws GeneralSecurityException {
        byte[] bArrM0;
        if (i10 > 16) {
            throw new InvalidAlgorithmParameterException("outputLength too large, max is 16 bytes");
        }
        SecretKeySpec secretKeySpec = this.f15970a;
        if (!ih1.z(1)) {
            throw new GeneralSecurityException("Can not use AES-CMAC in FIPS-mode.");
        }
        Cipher cipher = (Cipher) f15969d.get();
        cipher.init(1, secretKeySpec);
        int length = bArr.length;
        int i11 = length != 0 ? 1 + ((length - 1) >> 4) : 1;
        int i12 = i11 - 1;
        int i13 = i12 * 16;
        if (i11 * 16 == length) {
            bArrM0 = ih1.m0(bArr, i13, this.f15971b);
        } else {
            byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, i13, 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;
            if (bArrCopyOf.length != 16) {
                throw new IllegalArgumentException("The lengths of x and y should match.");
            }
            bArrM0 = ih1.m0(bArrCopyOf, 0, this.f15972c);
        }
        byte[] bArr2 = new byte[16];
        byte[] bArr3 = new byte[16];
        for (int i14 = 0; i14 < i12; i14++) {
            int i15 = i14 * 16;
            for (int i16 = 0; i16 < 16; i16++) {
                bArr3[i16] = (byte) (bArr2[i16] ^ bArr[i16 + i15]);
            }
            if (cipher.doFinal(bArr3, 0, 16, bArr2) != 16) {
                throw new IllegalStateException("Cipher didn't write full block");
            }
        }
        for (int i17 = 0; i17 < 16; i17++) {
            bArr3[i17] = (byte) (bArr2[i17] ^ bArrM0[i17]);
        }
        if (cipher.doFinal(bArr3, 0, 16, bArr2) == 16) {
            return i10 == 16 ? bArr2 : Arrays.copyOf(bArr2, i10);
        }
        throw new IllegalStateException("Cipher didn't write full block");
    }
}
