package p138.p176.p278.p279.p290;

import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.AEADBadTagException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import p138.p176.p278.p279.InterfaceC5176;

/* JADX INFO: renamed from: ދ.ހ.ހ.֏.ޞ.ށ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class C5502 implements InterfaceC5176 {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final byte[] f15862;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final byte[] f15863;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final SecretKeySpec f15864;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final int f15865;

    public C5502(byte[] bArr, int i) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
        if (i != 12 && i != 16) {
            throw new IllegalArgumentException("IV size should be either 12 or 16 bytes");
        }
        this.f15865 = i;
        C5554.m8920(bArr.length);
        this.f15864 = new SecretKeySpec(bArr, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/NOPADDING");
        cipher.init(1, this.f15864);
        byte[] bArrM8869 = m8869(cipher.doFinal(new byte[16]));
        this.f15862 = bArrM8869;
        this.f15863 = m8869(bArrM8869);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static byte[] m8869(byte[] bArr) {
        byte[] bArr2 = new byte[16];
        int i = 0;
        while (i < 15) {
            int i2 = i + 1;
            bArr2[i] = (byte) (((bArr[i] << 1) ^ ((bArr[i2] & 255) >>> 7)) & 255);
            i = i2;
        }
        bArr2[15] = (byte) ((bArr[15] << 1) ^ ((bArr[0] & 128) != 0 ? 135 : 0));
        return bArr2;
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public static byte[] m8870(byte[] bArr, byte[] bArr2) {
        int length = bArr.length;
        byte[] bArr3 = new byte[length];
        for (int i = 0; i < length; i++) {
            bArr3[i] = (byte) (bArr[i] ^ bArr2[i]);
        }
        return bArr3;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final byte[] m8871(Cipher cipher, int i, byte[] bArr, int i2, int i3) throws BadPaddingException, IllegalBlockSizeException {
        byte[] bArrM8870;
        byte[] bArr2 = new byte[16];
        bArr2[15] = (byte) i;
        if (i3 == 0) {
            return cipher.doFinal(m8870(bArr2, this.f15862));
        }
        byte[] bArrDoFinal = cipher.doFinal(bArr2);
        int i4 = 0;
        while (i3 - i4 > 16) {
            for (int i5 = 0; i5 < 16; i5++) {
                bArrDoFinal[i5] = (byte) (bArrDoFinal[i5] ^ bArr[(i2 + i4) + i5]);
            }
            bArrDoFinal = cipher.doFinal(bArrDoFinal);
            i4 += 16;
        }
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, i4 + i2, i2 + i3);
        if (bArrCopyOfRange.length == 16) {
            bArrM8870 = m8870(bArrCopyOfRange, this.f15862);
        } else {
            byte[] bArrCopyOf = Arrays.copyOf(this.f15863, 16);
            for (int i6 = 0; i6 < bArrCopyOfRange.length; i6++) {
                bArrCopyOf[i6] = (byte) (bArrCopyOf[i6] ^ bArrCopyOfRange[i6]);
            }
            bArrCopyOf[bArrCopyOfRange.length] = (byte) (bArrCopyOf[bArrCopyOfRange.length] ^ 128);
            bArrM8870 = bArrCopyOf;
        }
        return cipher.doFinal(m8870(bArrDoFinal, bArrM8870));
    }

    @Override // p138.p176.p278.p279.InterfaceC5176
    /* JADX INFO: renamed from: ֏ */
    public byte[] mo8647(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        int length = bArr.length;
        int i = this.f15865;
        if (length > (Integer.MAX_VALUE - i) - 16) {
            throw new GeneralSecurityException("plaintext too long");
        }
        byte[] bArr3 = new byte[bArr.length + i + 16];
        byte[] bArrM8914 = C5548.m8914(i);
        System.arraycopy(bArrM8914, 0, bArr3, 0, this.f15865);
        Cipher cipher = Cipher.getInstance("AES/ECB/NOPADDING");
        cipher.init(1, this.f15864);
        byte[] bArrM8871 = m8871(cipher, 0, bArrM8914, 0, bArrM8914.length);
        byte[] bArr4 = bArr2 == null ? new byte[0] : bArr2;
        byte[] bArrM88712 = m8871(cipher, 1, bArr4, 0, bArr4.length);
        Cipher cipher2 = Cipher.getInstance("AES/CTR/NOPADDING");
        cipher2.init(1, this.f15864, new IvParameterSpec(bArrM8871));
        cipher2.doFinal(bArr, 0, bArr.length, bArr3, this.f15865);
        byte[] bArrM88713 = m8871(cipher, 2, bArr3, this.f15865, bArr.length);
        int length2 = bArr.length + this.f15865;
        for (int i2 = 0; i2 < 16; i2++) {
            bArr3[length2 + i2] = (byte) ((bArrM88712[i2] ^ bArrM8871[i2]) ^ bArrM88713[i2]);
        }
        return bArr3;
    }

    @Override // p138.p176.p278.p279.InterfaceC5176
    /* JADX INFO: renamed from: ؠ */
    public byte[] mo8648(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        int length = (bArr.length - this.f15865) - 16;
        if (length < 0) {
            throw new GeneralSecurityException("ciphertext too short");
        }
        Cipher cipher = Cipher.getInstance("AES/ECB/NOPADDING");
        cipher.init(1, this.f15864);
        byte[] bArrM8871 = m8871(cipher, 0, bArr, 0, this.f15865);
        if (bArr2 == null) {
            bArr2 = new byte[0];
        }
        byte[] bArr3 = bArr2;
        byte[] bArrM88712 = m8871(cipher, 1, bArr3, 0, bArr3.length);
        byte[] bArrM88713 = m8871(cipher, 2, bArr, this.f15865, length);
        int length2 = bArr.length - 16;
        byte b = 0;
        for (int i = 0; i < 16; i++) {
            b = (byte) (b | (((bArr[length2 + i] ^ bArrM88712[i]) ^ bArrM8871[i]) ^ bArrM88713[i]));
        }
        if (b != 0) {
            throw new AEADBadTagException("tag mismatch");
        }
        Cipher cipher2 = Cipher.getInstance("AES/CTR/NOPADDING");
        cipher2.init(1, this.f15864, new IvParameterSpec(bArrM8871));
        return cipher2.doFinal(bArr, this.f15865, length);
    }
}
