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

import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.util.Arrays;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/* JADX INFO: loaded from: classes2.dex */
public final class yd implements b7 {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final List f21384c = Arrays.asList(64);

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final byte[] f21385d = new byte[16];

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

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

    public yd(byte[] bArr) throws GeneralSecurityException {
        if (!q9.y(1)) {
            throw new GeneralSecurityException("Can not use AES-SIV in FIPS-mode.");
        }
        int length = bArr.length;
        if (!f21384c.contains(Integer.valueOf(length))) {
            throw new InvalidKeyException(defpackage.e.e("invalid key size: ", length, " bytes; key must have 64 bytes"));
        }
        int i10 = length >> 1;
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, 0, i10);
        this.f21387b = Arrays.copyOfRange(bArr, i10, length);
        this.f21386a = new i5(bArrCopyOfRange);
    }

    @Override // com.google.android.gms.internal.pal.b7
    public final byte[] zza(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        byte[] bArrH0;
        if (bArr.length > 2147483631) {
            throw new GeneralSecurityException("plaintext too long");
        }
        Cipher cipher = (Cipher) de.f20763e.a("AES/CTR/NoPadding");
        byte[][] bArr3 = {bArr2, bArr};
        i5 i5Var = this.f21386a;
        byte[] bArrA = i5Var.a(16, f21385d);
        for (int i10 = 0; i10 <= 0; i10++) {
            byte[] bArr4 = bArr3[i10];
            if (bArr4 == null) {
                bArr4 = new byte[0];
            }
            bArrA = q9.h0(q9.P(bArrA), i5Var.a(16, bArr4));
        }
        byte[] bArr5 = bArr3[1];
        int length = bArr5.length;
        if (length >= 16) {
            int length2 = bArrA.length;
            if (length < length2) {
                throw new IllegalArgumentException("xorEnd requires a.length >= b.length");
            }
            int i11 = length - length2;
            bArrH0 = Arrays.copyOf(bArr5, length);
            for (int i12 = 0; i12 < bArrA.length; i12++) {
                int i13 = i11 + i12;
                bArrH0[i13] = (byte) (bArrH0[i13] ^ bArrA[i12]);
            }
        } else {
            int length3 = bArr5.length;
            if (length3 >= 16) {
                throw new IllegalArgumentException("x must be smaller than a block.");
            }
            byte[] bArrCopyOf = Arrays.copyOf(bArr5, 16);
            bArrCopyOf[length3] = -128;
            bArrH0 = q9.h0(bArrCopyOf, q9.P(bArrA));
        }
        byte[] bArrA2 = i5Var.a(16, bArrH0);
        byte[] bArr6 = (byte[]) bArrA2.clone();
        bArr6[8] = (byte) (bArr6[8] & 127);
        bArr6[12] = (byte) (bArr6[12] & 127);
        cipher.init(1, new SecretKeySpec(this.f21387b, "AES"), new IvParameterSpec(bArr6));
        return q9.Y(bArrA2, cipher.doFinal(bArr));
    }
}
