package defpackage;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.ProviderException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;

/* JADX INFO: renamed from: ᵴᵙˋ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C4658 implements InterfaceC5579 {

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final Object f17689;

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final /* synthetic */ int f17690 = 1;

    public C4658(String str) throws GeneralSecurityException {
        try {
            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
            this.f17689 = new C4658(str, keyStore);
        } catch (IOException e) {
            throw new GeneralSecurityException(e);
        }
    }

    public C4658(String str, KeyStore keyStore) throws InvalidKeyException {
        SecretKey secretKey = (SecretKey) keyStore.getKey(str, null);
        this.f17689 = secretKey;
        if (secretKey == null) {
            throw new InvalidKeyException("Keystore cannot load the key with ID: ".concat(str));
        }
    }

    @Override // defpackage.InterfaceC5579
    /* JADX INFO: renamed from: ʽˈˋ */
    public final byte[] mo1207(byte[] bArr, byte[] bArr2) throws BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
        int i = this.f17690;
        Object obj = this.f17689;
        switch (i) {
            case 0:
                if (bArr.length < 28) {
                    throw new BadPaddingException("ciphertext too short");
                }
                GCMParameterSpec gCMParameterSpec = new GCMParameterSpec(128, bArr, 0, 12);
                Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
                cipher.init(2, (SecretKey) obj, gCMParameterSpec);
                cipher.updateAAD(bArr2);
                return cipher.doFinal(bArr, 12, bArr.length - 12);
            default:
                C4658 c4658 = (C4658) obj;
                try {
                    return c4658.mo1207(bArr, bArr2);
                } catch (BadPaddingException e) {
                    throw e;
                } catch (GeneralSecurityException | ProviderException unused) {
                    try {
                        Thread.sleep((int) (Math.random() * 100.0d));
                        break;
                    } catch (InterruptedException unused2) {
                    }
                    return c4658.mo1207(bArr, bArr2);
                }
        }
    }

    @Override // defpackage.InterfaceC5579
    /* JADX INFO: renamed from: ᵲᵶᵳˈ */
    public final byte[] mo1208(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        int i = this.f17690;
        Object obj = this.f17689;
        switch (i) {
            case 0:
                if (bArr.length > 2147483619) {
                    C1685.m4558("plaintext too long");
                    return null;
                }
                byte[] bArr3 = new byte[bArr.length + 28];
                Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
                cipher.init(1, (SecretKey) obj);
                cipher.updateAAD(bArr2);
                if (cipher.doFinal(bArr, 0, bArr.length, bArr3, 12) != bArr.length + 16) {
                    C1685.m4558("encryption failed: bytesWritten is wrong");
                    return null;
                }
                byte[] iv = cipher.getIV();
                if (iv.length == 12) {
                    System.arraycopy(iv, 0, bArr3, 0, 12);
                    return bArr3;
                }
                C1685.m4558("IV has unexpected length");
                return null;
            default:
                C4658 c4658 = (C4658) obj;
                try {
                    return c4658.mo1208(bArr, bArr2);
                } catch (GeneralSecurityException | ProviderException unused) {
                    try {
                        Thread.sleep((int) (Math.random() * 100.0d));
                        break;
                    } catch (InterruptedException unused2) {
                    }
                    return c4658.mo1208(bArr, bArr2);
                }
        }
    }
}
