package defpackage;

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

/* JADX INFO: renamed from: ᵖᵛᵢ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C2432 implements InterfaceC3608 {

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final byte[] f9471;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final byte[] f9472;

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final InterfaceC2925 f9473;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public static final byte[] f9469 = new byte[16];

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public static final byte[] f9468 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public static final C0140 f9470 = new C0140(4);

    public C2432(byte[] bArr, C2855 c2855) throws InvalidKeyException {
        if (!AbstractC2704.m6538(1)) {
            C1685.m4558("Can not use AES-SIV in FIPS-mode.");
            throw null;
        }
        if (bArr.length != 32 && bArr.length != 64) {
            throw new InvalidKeyException(AbstractC3589.m8110(new StringBuilder("invalid key size: "), bArr.length, " bytes; key must have 32 or 64 bytes"));
        }
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, 0, bArr.length / 2);
        this.f9471 = Arrays.copyOfRange(bArr, bArr.length / 2, bArr.length);
        this.f9473 = ˌᵨʽʙ.ᵮᵣᵗˈ(C5537.m11158(C0429.m1828(bArrCopyOfRange.length), new ʽˎˇˌ(21, C2855.m6836(bArrCopyOfRange))));
        this.f9472 = c2855.m6837();
    }

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public static void m6168(int i) throws GeneralSecurityException {
        if (i > 126) {
            throw new GeneralSecurityException(AbstractC0793.m2876(i, "Too many associated datas: ", " > 126"));
        }
    }

    @Override // defpackage.InterfaceC3608
    /* JADX INFO: renamed from: ʽˈˋ */
    public final byte[] mo2136(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        byte[][] bArr3 = {bArr2};
        m6168(1);
        int length = bArr.length;
        byte[] bArr4 = this.f9472;
        if (length < bArr4.length + 16) {
            C1685.m4558("Ciphertext too short.");
            return null;
        }
        if (!AbstractC0184.m1341(bArr4, bArr)) {
            C1685.m4558("Decryption failed (OutputPrefix mismatch).");
            return null;
        }
        Cipher cipher = (Cipher) f9470.get();
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, bArr4.length, bArr4.length + 16);
        byte[] bArr5 = (byte[]) bArrCopyOfRange.clone();
        bArr5[8] = (byte) (bArr5[8] & 127);
        bArr5[12] = (byte) (bArr5[12] & 127);
        cipher.init(2, new SecretKeySpec(this.f9471, "AES"), new IvParameterSpec(bArr5));
        int length2 = bArr4.length + 16;
        int length3 = bArr.length - length2;
        byte[] bArrDoFinal = cipher.doFinal(bArr, length2, length3);
        if (length3 == 0 && bArrDoFinal == null && "The Android Project".equals(System.getProperty("java.vendor"))) {
            bArrDoFinal = new byte[0];
        }
        byte[][] bArr6 = (byte[][]) Arrays.copyOf(bArr3, 2);
        bArr6[1] = bArrDoFinal;
        if (MessageDigest.isEqual(bArrCopyOfRange, m6169(bArr6))) {
            return bArrDoFinal;
        }
        throw new AEADBadTagException("Integrity check failed.");
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final byte[] m6169(byte[]... bArr) {
        byte[] bArrCopyOf;
        int length = bArr.length;
        InterfaceC2925 interfaceC2925 = this.f9473;
        if (length == 0) {
            return interfaceC2925.mo4740(16, f9468);
        }
        byte[] bArrMo4740 = interfaceC2925.mo4740(16, f9469);
        for (int i = 0; i < bArr.length - 1; i++) {
            byte[] bArr2 = bArr[i];
            if (bArr2 == null) {
                bArr2 = new byte[0];
            }
            bArrMo4740 = ᵷˆᵣ.ˏːˆ(ᵷᵸˁ.ᵵᵽ(bArrMo4740), interfaceC2925.mo4740(16, bArr2));
        }
        byte[] bArr3 = bArr[bArr.length - 1];
        if (bArr3.length >= 16) {
            if (bArr3.length < bArrMo4740.length) {
                ᵕᵩʽᵲ.ᵮᵣᵗˈ("xorEnd requires a.length >= b.length");
                return null;
            }
            int length2 = bArr3.length - bArrMo4740.length;
            bArrCopyOf = Arrays.copyOf(bArr3, bArr3.length);
            for (int i2 = 0; i2 < bArrMo4740.length; i2++) {
                int i3 = length2 + i2;
                bArrCopyOf[i3] = (byte) (bArrCopyOf[i3] ^ bArrMo4740[i2]);
            }
        } else {
            if (bArr3.length >= 16) {
                ᵕᵩʽᵲ.ᵮᵣᵗˈ("x must be smaller than a block.");
                return null;
            }
            byte[] bArrCopyOf2 = Arrays.copyOf(bArr3, 16);
            bArrCopyOf2[bArr3.length] = -128;
            bArrCopyOf = ᵷˆᵣ.ˏːˆ(bArrCopyOf2, ᵷᵸˁ.ᵵᵽ(bArrMo4740));
        }
        return interfaceC2925.mo4740(16, bArrCopyOf);
    }

    @Override // defpackage.InterfaceC3608
    /* JADX INFO: renamed from: ᵲᵶᵳˈ */
    public final byte[] mo2137(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        byte[][] bArr3 = {bArr2};
        m6168(1);
        int length = bArr.length;
        byte[] bArr4 = this.f9472;
        if (length > 2147483631 - bArr4.length) {
            C1685.m4558("plaintext too long");
            return null;
        }
        Cipher cipher = (Cipher) f9470.get();
        byte[][] bArr5 = (byte[][]) Arrays.copyOf(bArr3, 2);
        bArr5[1] = bArr;
        byte[] bArrM6169 = m6169(bArr5);
        byte[] bArr6 = (byte[]) bArrM6169.clone();
        bArr6[8] = (byte) (bArr6[8] & 127);
        bArr6[12] = (byte) (bArr6[12] & 127);
        cipher.init(1, new SecretKeySpec(this.f9471, "AES"), new IvParameterSpec(bArr6));
        byte[] bArrCopyOf = Arrays.copyOf(bArr4, bArr4.length + bArrM6169.length + bArr.length);
        System.arraycopy(bArrM6169, 0, bArrCopyOf, bArr4.length, bArrM6169.length);
        if (cipher.doFinal(bArr, 0, bArr.length, bArrCopyOf, bArr4.length + bArrM6169.length) == bArr.length) {
            return bArrCopyOf;
        }
        C1685.m4558("not enough data written");
        return null;
    }
}
