package jk;

import android.security.keystore.KeyGenParameterSpec;
import android.util.Log;
import fk.m;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.util.Arrays;
import javax.crypto.KeyGenerator;
import nk.u;
import nk.w;

/* loaded from: classes3.dex */
public final class c implements m {

    /* renamed from: c, reason: collision with root package name */
    private static final String f25033c = "c";

    /* renamed from: a, reason: collision with root package name */
    private final String f25034a;

    /* renamed from: b, reason: collision with root package name */
    private KeyStore f25035b;

    /* loaded from: classes3.dex */
    public static final class b {

        /* renamed from: a, reason: collision with root package name */
        String f25036a = null;

        /* renamed from: b, reason: collision with root package name */
        KeyStore f25037b;

        public b() {
            this.f25037b = null;
            if (!c.c()) {
                throw new IllegalStateException("need Android Keystore on Android M or newer");
            }
            try {
                KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
                this.f25037b = keyStore;
                keyStore.load(null);
            } catch (IOException | GeneralSecurityException e10) {
                throw new IllegalStateException(e10);
            }
        }

        public c a() {
            return new c(this);
        }

        public b b(KeyStore keyStore) {
            if (keyStore == null) {
                throw new IllegalArgumentException("val cannot be null");
            }
            this.f25037b = keyStore;
            return this;
        }
    }

    static /* synthetic */ boolean c() {
        return f();
    }

    public static void d(String str) {
        if (new c().e(str)) {
            throw new IllegalArgumentException(String.format("cannot generate a new key %s because it already exists; please delete it with deleteKey() and try again", str));
        }
        String b10 = w.b("android-keystore://", str);
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "AndroidKeyStore");
        keyGenerator.init(new KeyGenParameterSpec.Builder(b10, 3).setKeySize(256).setBlockModes("GCM").setEncryptionPaddings("NoPadding").build());
        keyGenerator.generateKey();
    }

    private static boolean f() {
        return true;
    }

    private static fk.a g(fk.a aVar) {
        byte[] c10 = u.c(10);
        byte[] bArr = new byte[0];
        if (Arrays.equals(c10, aVar.b(aVar.a(c10, bArr), bArr))) {
            return aVar;
        }
        throw new KeyStoreException("cannot use Android Keystore: encryption/decryption of non-empty message and empty aad returns an incorrect result");
    }

    /* JADX WARN: Code restructure failed: missing block: B:13:0x001e, code lost:
    
        if (r3.toLowerCase(java.util.Locale.US).startsWith("android-keystore://") != false) goto L15;
     */
    @Override // fk.m
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public synchronized boolean a(String str) {
        String str2 = this.f25034a;
        boolean z10 = true;
        if (str2 != null && str2.equals(str)) {
            return true;
        }
        if (this.f25034a == null) {
        }
        z10 = false;
        return z10;
    }

    @Override // fk.m
    public synchronized fk.a b(String str) {
        String str2 = this.f25034a;
        if (str2 != null && !str2.equals(str)) {
            throw new GeneralSecurityException(String.format("this client is bound to %s, cannot load keys bound to %s", this.f25034a, str));
        }
        return g(new jk.b(w.b("android-keystore://", str), this.f25035b));
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public synchronized boolean e(String str) {
        String b10;
        b10 = w.b("android-keystore://", str);
        try {
        } catch (NullPointerException unused) {
            Log.w(f25033c, "Keystore is temporarily unavailable, wait 20ms, reinitialize Keystore and try again.");
            try {
                Thread.sleep(20L);
                KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
                this.f25035b = keyStore;
                keyStore.load(null);
            } catch (IOException e10) {
                throw new GeneralSecurityException(e10);
            } catch (InterruptedException unused2) {
            }
            return this.f25035b.containsAlias(b10);
        }
        return this.f25035b.containsAlias(b10);
    }

    public c() {
        this(new b());
    }

    private c(b bVar) {
        this.f25034a = bVar.f25036a;
        this.f25035b = bVar.f25037b;
    }
}
