package p138.p176.p278.p279.p290;

import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import p138.p176.p177.p242.p246.p251.C4303;
import p138.p176.p278.p279.InterfaceC5176;

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

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final SecretKey f15867;

    public C5504(byte[] bArr) throws InvalidAlgorithmParameterException {
        C5554.m8920(bArr.length);
        this.f15867 = new SecretKeySpec(bArr, "AES");
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static AlgorithmParameterSpec m8872(byte[] bArr, int i, int i2) throws GeneralSecurityException {
        try {
            Class.forName("javax.crypto.spec.GCMParameterSpec");
            return new GCMParameterSpec(128, bArr, i, i2);
        } catch (ClassNotFoundException unused) {
            if (C4303.m7167()) {
                return new IvParameterSpec(bArr, i, i2);
            }
            throw new GeneralSecurityException("cannot use AES-GCM: javax.crypto.spec.GCMParameterSpec not found");
        }
    }

    @Override // p138.p176.p278.p279.InterfaceC5176
    /* JADX INFO: renamed from: ֏ */
    public byte[] mo8647(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        if (bArr.length > 2147483619) {
            throw new GeneralSecurityException("plaintext too long");
        }
        byte[] bArr3 = new byte[bArr.length + 12 + 16];
        byte[] bArrM8914 = C5548.m8914(12);
        System.arraycopy(bArrM8914, 0, bArr3, 0, 12);
        Cipher cipherM8904 = C5534.f15933.m8904("AES/GCM/NoPadding");
        cipherM8904.init(1, this.f15867, m8872(bArrM8914, 0, bArrM8914.length));
        if (bArr2 != null && bArr2.length != 0) {
            cipherM8904.updateAAD(bArr2);
        }
        int iDoFinal = cipherM8904.doFinal(bArr, 0, bArr.length, bArr3, 12);
        if (iDoFinal == bArr.length + 16) {
            return bArr3;
        }
        throw new GeneralSecurityException(String.format("encryption failed; GCM tag must be %s bytes, but got only %s bytes", 16, Integer.valueOf(iDoFinal - bArr.length)));
    }

    @Override // p138.p176.p278.p279.InterfaceC5176
    /* JADX INFO: renamed from: ؠ */
    public byte[] mo8648(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        if (bArr.length < 28) {
            throw new GeneralSecurityException("ciphertext too short");
        }
        AlgorithmParameterSpec algorithmParameterSpecM8872 = m8872(bArr, 0, 12);
        Cipher cipherM8904 = C5534.f15933.m8904("AES/GCM/NoPadding");
        cipherM8904.init(2, this.f15867, algorithmParameterSpecM8872);
        if (bArr2 != null && bArr2.length != 0) {
            cipherM8904.updateAAD(bArr2);
        }
        return cipherM8904.doFinal(bArr, 12, bArr.length - 12);
    }
}
