package o; import java.security.GeneralSecurityException; 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; /* loaded from: classes2.dex */ public final class OxE implements InterfaceC4280bVg { private static final ThreadLocal e = new ThreadLocal() { // from class: o.OxE.5 @Override // java.lang.ThreadLocal protected final /* synthetic */ Cipher initialValue() { return d(); } private static Cipher d() { try { return YYT.a.e("AES/GCM-SIV/NoPadding"); } catch (GeneralSecurityException e2) { throw new IllegalStateException(e2); } } }; private final SecretKey d; public OxE(byte[] bArr) throws GeneralSecurityException { ifG.e(bArr.length); this.d = new SecretKeySpec(bArr, "AES"); } @Override // o.InterfaceC4280bVg public final byte[] b(byte[] bArr, byte[] bArr2) throws GeneralSecurityException { if (bArr.length > 2147483619) { throw new GeneralSecurityException("plaintext too long"); } byte[] bArr3 = new byte[bArr.length + 28]; byte[] b = YTf.b(12); System.arraycopy(b, 0, bArr3, 0, 12); AlgorithmParameterSpec e2 = e(b, b.length); ThreadLocal threadLocal = e; threadLocal.get().init(1, this.d, e2); if (bArr2 != null && bArr2.length != 0) { threadLocal.get().updateAAD(bArr2); } int doFinal = threadLocal.get().doFinal(bArr, 0, bArr.length, bArr3, 12); if (doFinal == 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(doFinal - bArr.length))); } @Override // o.InterfaceC4280bVg public final byte[] d(byte[] bArr, byte[] bArr2) throws GeneralSecurityException { if (bArr.length < 28) { throw new GeneralSecurityException("ciphertext too short"); } AlgorithmParameterSpec e2 = e(bArr, 12); ThreadLocal threadLocal = e; threadLocal.get().init(2, this.d, e2); if (bArr2 != null && bArr2.length != 0) { threadLocal.get().updateAAD(bArr2); } return threadLocal.get().doFinal(bArr, 12, bArr.length - 12); } private static AlgorithmParameterSpec e(byte[] bArr, int i) throws GeneralSecurityException { try { Class.forName("javax.crypto.spec.GCMParameterSpec"); return new GCMParameterSpec(128, bArr, 0, i); } catch (ClassNotFoundException unused) { if ("The Android Project".equals(System.getProperty("java.vendor"))) { return new IvParameterSpec(bArr, 0, i); } throw new GeneralSecurityException("cannot use AES-GCM: javax.crypto.spec.GCMParameterSpec not found"); } } }