package M3;

import F.I;
import X3.b0;
import com.google.crypto.tink.shaded.protobuf.AbstractC1253a;
import com.google.crypto.tink.shaded.protobuf.AbstractC1261i;
import com.google.crypto.tink.shaded.protobuf.C;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.util.concurrent.atomic.AtomicReference;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final byte[] f5104c = new byte[0];

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final b0 f5105a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final R3.c f5106b;

    public v(b0 b0Var, R3.c cVar) {
        this.f5105a = b0Var;
        this.f5106b = cVar;
    }

    @Override // L3.a
    public final byte[] a(byte[] bArr, byte[] bArr2) {
        AbstractC1253a abstractC1253aA;
        b0 b0Var = this.f5105a;
        AtomicReference atomicReference = L3.n.f4186a;
        synchronized (L3.n.class) {
            try {
                S3.d dVar = ((L3.e) L3.n.f4186a.get()).a(b0Var.A()).f4166a;
                Class cls = dVar.f6225c;
                if (!dVar.f6224b.keySet().contains(cls) && !Void.class.equals(cls)) {
                    throw new IllegalArgumentException("Given internalKeyMananger " + dVar.toString() + " does not support primitive class " + cls.getName());
                }
                if (!((Boolean) L3.n.f4188c.get(b0Var.A())).booleanValue()) {
                    throw new GeneralSecurityException("newKey-operation not permitted for key type " + b0Var.A());
                }
                AbstractC1261i abstractC1261iB = b0Var.B();
                try {
                    I iD = dVar.d();
                    AbstractC1253a abstractC1253aH = iD.h(abstractC1261iB);
                    iD.m(abstractC1253aH);
                    abstractC1253aA = iD.a(abstractC1253aH);
                } catch (C e6) {
                    throw new GeneralSecurityException("Failures parsing proto of type ".concat(((Class) dVar.d().f1203a).getName()), e6);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        byte[] bArrD = abstractC1253aA.d();
        byte[] bArrA = this.f5106b.a(bArrD, f5104c);
        byte[] bArrA2 = ((L3.a) L3.n.d(this.f5105a.A(), bArrD)).a(bArr, bArr2);
        return ByteBuffer.allocate(bArrA.length + 4 + bArrA2.length).putInt(bArrA.length).put(bArrA).put(bArrA2).array();
    }

    @Override // L3.a
    public final byte[] b(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        try {
            ByteBuffer byteBufferWrap = ByteBuffer.wrap(bArr);
            int i6 = byteBufferWrap.getInt();
            if (i6 <= 0 || i6 > bArr.length - 4) {
                throw new GeneralSecurityException("invalid ciphertext");
            }
            byte[] bArr3 = new byte[i6];
            byteBufferWrap.get(bArr3, 0, i6);
            byte[] bArr4 = new byte[byteBufferWrap.remaining()];
            byteBufferWrap.get(bArr4, 0, byteBufferWrap.remaining());
            return ((L3.a) L3.n.d(this.f5105a.A(), this.f5106b.b(bArr3, f5104c))).b(bArr4, bArr2);
        } catch (IndexOutOfBoundsException e6) {
            e = e6;
            throw new GeneralSecurityException("invalid ciphertext", e);
        } catch (NegativeArraySizeException e7) {
            e = e7;
            throw new GeneralSecurityException("invalid ciphertext", e);
        } catch (BufferUnderflowException e8) {
            e = e8;
            throw new GeneralSecurityException("invalid ciphertext", e);
        }
    }
}
