package U3;

import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import javax.crypto.Cipher;
import n4.u0;

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

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

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

    public c(int i, byte[] bArr) throws GeneralSecurityException {
        this.f6698a = i;
        switch (i) {
            case 1:
                this.f6699b = new J3.f(0, bArr);
                return;
            case 2:
                this.f6699b = new J3.f(1, bArr);
                return;
            default:
                if (!I1.a.c(2)) {
                    throw new GeneralSecurityException("Can not use AES-GCM in FIPS-mode, as BoringCrypto module is not available.");
                }
                this.f6699b = new J3.c(bArr);
                return;
        }
    }

    @Override // H3.a
    public final byte[] a(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        switch (this.f6698a) {
            case u0.f15495q /* 0 */:
                byte[] bArrA = n.a(12);
                J3.c cVar = (J3.c) this.f6699b;
                cVar.getClass();
                if (bArrA.length != 12) {
                    throw new GeneralSecurityException("iv is wrong size");
                }
                if (bArr.length > 2147483619) {
                    throw new GeneralSecurityException("plaintext too long");
                }
                boolean z8 = cVar.f3471b;
                byte[] bArr3 = new byte[z8 ? bArr.length + 28 : bArr.length + 16];
                if (z8) {
                    System.arraycopy(bArrA, 0, bArr3, 0, 12);
                }
                AlgorithmParameterSpec algorithmParameterSpecA = J3.c.a(bArrA);
                J3.b bVar = J3.c.f3469c;
                ((Cipher) bVar.get()).init(1, cVar.f3470a, algorithmParameterSpecA);
                if (bArr2 != null && bArr2.length != 0) {
                    ((Cipher) bVar.get()).updateAAD(bArr2);
                }
                int iDoFinal = ((Cipher) bVar.get()).doFinal(bArr, 0, bArr.length, bArr3, z8 ? 12 : 0);
                if (iDoFinal == bArr.length + 16) {
                    return bArr3;
                }
                throw new GeneralSecurityException(I1.a.k(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 = n.a(12);
                byteBufferAllocate.put(bArrA2);
                ((J3.f) this.f6699b).b(byteBufferAllocate, bArrA2, bArr, bArr2);
                return byteBufferAllocate.array();
            default:
                ByteBuffer byteBufferAllocate2 = ByteBuffer.allocate(bArr.length + 40);
                byte[] bArrA3 = n.a(24);
                byteBufferAllocate2.put(bArrA3);
                ((J3.f) this.f6699b).b(byteBufferAllocate2, bArrA3, bArr, bArr2);
                return byteBufferAllocate2.array();
        }
    }

    @Override // H3.a
    public final byte[] b(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        switch (this.f6698a) {
            case u0.f15495q /* 0 */:
                byte[] bArrCopyOf = Arrays.copyOf(bArr, 12);
                J3.c cVar = (J3.c) this.f6699b;
                cVar.getClass();
                if (bArrCopyOf.length != 12) {
                    throw new GeneralSecurityException("iv is wrong size");
                }
                boolean z8 = cVar.f3471b;
                if (bArr.length < (z8 ? 28 : 16)) {
                    throw new GeneralSecurityException("ciphertext too short");
                }
                if (z8 && !ByteBuffer.wrap(bArrCopyOf).equals(ByteBuffer.wrap(bArr, 0, 12))) {
                    throw new GeneralSecurityException("iv does not match prepended iv");
                }
                AlgorithmParameterSpec algorithmParameterSpecA = J3.c.a(bArrCopyOf);
                J3.b bVar = J3.c.f3469c;
                ((Cipher) bVar.get()).init(2, cVar.f3470a, algorithmParameterSpecA);
                if (bArr2 != null && bArr2.length != 0) {
                    ((Cipher) bVar.get()).updateAAD(bArr2);
                }
                int i = z8 ? 12 : 0;
                int length = bArr.length;
                if (z8) {
                    length -= 12;
                }
                return ((Cipher) bVar.get()).doFinal(bArr, i, length);
            case 1:
                if (bArr.length < 28) {
                    throw new GeneralSecurityException("ciphertext too short");
                }
                return ((J3.f) this.f6699b).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 ((J3.f) this.f6699b).a(ByteBuffer.wrap(bArr, 24, bArr.length - 24), Arrays.copyOf(bArr, 24), bArr2);
        }
    }
}
