package Y3;

import J0.C0343a0;
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import javax.crypto.Cipher;

/* JADX INFO: loaded from: classes3.dex */
public final class c implements L3.a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f8802a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Object f8803b;

    public c(byte[] bArr, int i6) throws GeneralSecurityException {
        this.f8802a = i6;
        switch (i6) {
            case 1:
                this.f8803b = new N3.d(bArr, 0);
                return;
            case 2:
                this.f8803b = new N3.d(bArr, 1);
                return;
            default:
                if (!A0.a.b(2)) {
                    throw new GeneralSecurityException("Can not use AES-GCM in FIPS-mode, as BoringCrypto module is not available.");
                }
                this.f8803b = new N3.b(bArr);
                return;
        }
    }

    @Override // L3.a
    public final byte[] a(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        switch (this.f8802a) {
            case 0:
                byte[] bArrA = s.a(12);
                N3.b bVar = (N3.b) this.f8803b;
                bVar.getClass();
                if (bArrA.length != 12) {
                    throw new GeneralSecurityException("iv is wrong size");
                }
                if (bArr.length > 2147483619) {
                    throw new GeneralSecurityException("plaintext too long");
                }
                boolean z6 = bVar.f5269b;
                byte[] bArr3 = new byte[z6 ? bArr.length + 28 : bArr.length + 16];
                if (z6) {
                    System.arraycopy(bArrA, 0, bArr3, 0, 12);
                }
                AlgorithmParameterSpec algorithmParameterSpecA = N3.b.a(bArrA);
                C0343a0 c0343a0 = N3.b.f5267c;
                ((Cipher) c0343a0.get()).init(1, bVar.f5268a, algorithmParameterSpecA);
                if (bArr2 != null && bArr2.length != 0) {
                    ((Cipher) c0343a0.get()).updateAAD(bArr2);
                }
                int iDoFinal = ((Cipher) c0343a0.get()).doFinal(bArr, 0, bArr.length, bArr3, z6 ? 12 : 0);
                if (iDoFinal == bArr.length + 16) {
                    return bArr3;
                }
                throw new GeneralSecurityException(Z0.o.j(iDoFinal - bArr.length, "encryption failed; GCM tag must be 16 bytes, but got only ", " bytes"));
            case 1:
                ByteBuffer byteBufferAllocate = ByteBuffer.allocate(bArr.length + 28);
                byte[] bArrA2 = s.a(12);
                byteBufferAllocate.put(bArrA2);
                ((N3.d) this.f8803b).b(byteBufferAllocate, bArrA2, bArr, bArr2);
                return byteBufferAllocate.array();
            default:
                ByteBuffer byteBufferAllocate2 = ByteBuffer.allocate(bArr.length + 40);
                byte[] bArrA3 = s.a(24);
                byteBufferAllocate2.put(bArrA3);
                ((N3.d) this.f8803b).b(byteBufferAllocate2, bArrA3, bArr, bArr2);
                return byteBufferAllocate2.array();
        }
    }

    @Override // L3.a
    public final byte[] b(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        switch (this.f8802a) {
            case 0:
                byte[] bArrCopyOf = Arrays.copyOf(bArr, 12);
                N3.b bVar = (N3.b) this.f8803b;
                bVar.getClass();
                if (bArrCopyOf.length != 12) {
                    throw new GeneralSecurityException("iv is wrong size");
                }
                boolean z6 = bVar.f5269b;
                if (bArr.length < (z6 ? 28 : 16)) {
                    throw new GeneralSecurityException("ciphertext too short");
                }
                if (z6 && !ByteBuffer.wrap(bArrCopyOf).equals(ByteBuffer.wrap(bArr, 0, 12))) {
                    throw new GeneralSecurityException("iv does not match prepended iv");
                }
                AlgorithmParameterSpec algorithmParameterSpecA = N3.b.a(bArrCopyOf);
                C0343a0 c0343a0 = N3.b.f5267c;
                ((Cipher) c0343a0.get()).init(2, bVar.f5268a, algorithmParameterSpecA);
                if (bArr2 != null && bArr2.length != 0) {
                    ((Cipher) c0343a0.get()).updateAAD(bArr2);
                }
                int i6 = z6 ? 12 : 0;
                int length = bArr.length;
                if (z6) {
                    length -= 12;
                }
                return ((Cipher) c0343a0.get()).doFinal(bArr, i6, length);
            case 1:
                if (bArr.length < 28) {
                    throw new GeneralSecurityException("ciphertext too short");
                }
                return ((N3.d) this.f8803b).a(ByteBuffer.wrap(bArr, 12, bArr.length - 12), Arrays.copyOf(bArr, 12), bArr2);
            default:
                if (bArr.length < 40) {
                    throw new GeneralSecurityException("ciphertext too short");
                }
                return ((N3.d) this.f8803b).a(ByteBuffer.wrap(bArr, 24, bArr.length - 24), Arrays.copyOf(bArr, 24), bArr2);
        }
    }
}
