package o; import com.google.common.primitives.UnsignedBytes; import java.security.GeneralSecurityException; import java.util.Arrays; import javax.crypto.AEADBadTagException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import o.uAf; /* loaded from: classes2.dex */ public final class IYa implements InterfaceC4280bVg { private final int b; private final byte[] d; private final byte[] h; private final SecretKeySpec j; private static uAf.RVV c = uAf.RVV.ALGORITHM_NOT_FIPS; private static final ThreadLocal e = new ThreadLocal() { // from class: o.IYa.4 @Override // java.lang.ThreadLocal protected final /* synthetic */ Cipher initialValue() { return e(); } private static Cipher e() { try { return YYT.a.e("AES/ECB/NOPADDING"); } catch (GeneralSecurityException e2) { throw new IllegalStateException(e2); } } }; private static final ThreadLocal a = new ThreadLocal() { // from class: o.IYa.1 @Override // java.lang.ThreadLocal protected final /* synthetic */ Cipher initialValue() { return b(); } private static Cipher b() { try { return YYT.a.e("AES/CTR/NOPADDING"); } catch (GeneralSecurityException e2) { throw new IllegalStateException(e2); } } }; public IYa(byte[] bArr, int i) throws GeneralSecurityException { if (!c.d()) { throw new GeneralSecurityException("Can not use AES-EAX in FIPS-mode."); } if (i != 12 && i != 16) { throw new IllegalArgumentException("IV size should be either 12 or 16 bytes"); } this.b = i; ifG.e(bArr.length); SecretKeySpec secretKeySpec = new SecretKeySpec(bArr, "AES"); this.j = secretKeySpec; Cipher cipher = e.get(); cipher.init(1, secretKeySpec); byte[] e2 = e(cipher.doFinal(new byte[16])); this.d = e2; this.h = e(e2); } private static byte[] e(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; } private static byte[] e(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] & UnsignedBytes.MAX_VALUE) >>> 7)); i = i2; } bArr2[15] = (byte) (((bArr[0] >> 7) & 135) ^ (bArr[15] << 1)); return bArr2; } private byte[] d(Cipher cipher, int i, byte[] bArr, int i2, int i3) throws IllegalBlockSizeException, BadPaddingException { byte[] bArr2; byte[] bArr3 = new byte[16]; bArr3[15] = (byte) i; if (i3 == 0) { return cipher.doFinal(e(bArr3, this.d)); } byte[] doFinal = cipher.doFinal(bArr3); int i4 = 0; while (i3 - i4 > 16) { for (int i5 = 0; i5 < 16; i5++) { doFinal[i5] = (byte) (doFinal[i5] ^ bArr[(i2 + i4) + i5]); } doFinal = cipher.doFinal(doFinal); i4 += 16; } byte[] copyOfRange = Arrays.copyOfRange(bArr, i4 + i2, i2 + i3); if (copyOfRange.length == 16) { bArr2 = e(copyOfRange, this.d); } else { byte[] copyOf = Arrays.copyOf(this.h, 16); for (int i6 = 0; i6 < copyOfRange.length; i6++) { copyOf[i6] = (byte) (copyOf[i6] ^ copyOfRange[i6]); } copyOf[copyOfRange.length] = (byte) (copyOf[copyOfRange.length] ^ 128); bArr2 = copyOf; } return cipher.doFinal(e(doFinal, bArr2)); } @Override // o.InterfaceC4280bVg public final byte[] b(byte[] bArr, byte[] bArr2) throws GeneralSecurityException { int length = bArr.length; int i = this.b; if (length > 2147483631 - i) { throw new GeneralSecurityException("plaintext too long"); } byte[] bArr3 = new byte[bArr.length + i + 16]; byte[] b = YTf.b(i); System.arraycopy(b, 0, bArr3, 0, this.b); Cipher cipher = e.get(); cipher.init(1, this.j); byte[] d = d(cipher, 0, b, 0, b.length); byte[] bArr4 = bArr2 == null ? new byte[0] : bArr2; byte[] d2 = d(cipher, 1, bArr4, 0, bArr4.length); Cipher cipher2 = a.get(); cipher2.init(1, this.j, new IvParameterSpec(d)); cipher2.doFinal(bArr, 0, bArr.length, bArr3, this.b); byte[] d3 = d(cipher, 2, bArr3, this.b, bArr.length); int length2 = bArr.length; int i2 = this.b; for (int i3 = 0; i3 < 16; i3++) { bArr3[length2 + i2 + i3] = (byte) ((d2[i3] ^ d[i3]) ^ d3[i3]); } return bArr3; } @Override // o.InterfaceC4280bVg public final byte[] d(byte[] bArr, byte[] bArr2) throws GeneralSecurityException { int length = (bArr.length - this.b) - 16; if (length < 0) { throw new GeneralSecurityException("ciphertext too short"); } Cipher cipher = e.get(); cipher.init(1, this.j); byte[] d = d(cipher, 0, bArr, 0, this.b); if (bArr2 == null) { bArr2 = new byte[0]; } byte[] bArr3 = bArr2; byte[] d2 = d(cipher, 1, bArr3, 0, bArr3.length); byte[] d3 = d(cipher, 2, bArr, this.b, length); int length2 = bArr.length; byte b = 0; for (int i = 0; i < 16; i++) { b = (byte) (b | (((bArr[(length2 - 16) + i] ^ d2[i]) ^ d[i]) ^ d3[i])); } if (b != 0) { throw new AEADBadTagException("tag mismatch"); } Cipher cipher2 = a.get(); cipher2.init(1, this.j, new IvParameterSpec(d)); return cipher2.doFinal(bArr, this.b, length); } }