package u2;

import F2.b0;
import com.google.crypto.tink.shaded.protobuf.AbstractC0472a;
import com.google.crypto.tink.shaded.protobuf.AbstractC0479h;
import com.google.crypto.tink.shaded.protobuf.B;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.util.concurrent.atomic.AtomicReference;
import t2.InterfaceC1024a;

/* JADX INFO: loaded from: classes.dex */
public final class v implements InterfaceC1024a {

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final z2.b f10377b;

    public v(b0 b0Var, z2.b bVar) {
        this.f10376a = b0Var;
        this.f10377b = bVar;
    }

    @Override // t2.InterfaceC1024a
    public final byte[] a(byte[] bArr, byte[] bArr2) {
        AbstractC0472a abstractC0472aG;
        b0 b0Var = this.f10376a;
        AtomicReference atomicReference = t2.n.f9997a;
        synchronized (t2.n.class) {
            try {
                A2.f fVar = ((t2.e) t2.n.f9997a.get()).a(b0Var.B()).f9977a;
                Class cls = fVar.f32c;
                if (!fVar.f31b.keySet().contains(cls) && !Void.class.equals(cls)) {
                    throw new IllegalArgumentException("Given internalKeyMananger " + fVar.toString() + " does not support primitive class " + cls.getName());
                }
                if (!((Boolean) t2.n.f9999c.get(b0Var.B())).booleanValue()) {
                    throw new GeneralSecurityException("newKey-operation not permitted for key type " + b0Var.B());
                }
                AbstractC0479h abstractC0479hC = b0Var.C();
                try {
                    A2.e eVarD = fVar.d();
                    AbstractC0472a abstractC0472aI = eVarD.i(abstractC0479hC);
                    eVarD.j(abstractC0472aI);
                    abstractC0472aG = eVarD.g(abstractC0472aI);
                } catch (B e2) {
                    throw new GeneralSecurityException("Failures parsing proto of type ".concat(((Class) fVar.d().f29k).getName()), e2);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        byte[] bArrE = abstractC0472aG.e();
        byte[] bArrA = this.f10377b.a(bArrE, f10375c);
        byte[] bArrA2 = ((InterfaceC1024a) t2.n.d(this.f10376a.B(), bArrE)).a(bArr, bArr2);
        return ByteBuffer.allocate(bArrA.length + 4 + bArrA2.length).putInt(bArrA.length).put(bArrA).put(bArrA2).array();
    }

    @Override // t2.InterfaceC1024a
    public final byte[] b(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        try {
            ByteBuffer byteBufferWrap = ByteBuffer.wrap(bArr);
            int i = byteBufferWrap.getInt();
            if (i <= 0 || i > bArr.length - 4) {
                throw new GeneralSecurityException("invalid ciphertext");
            }
            byte[] bArr3 = new byte[i];
            byteBufferWrap.get(bArr3, 0, i);
            byte[] bArr4 = new byte[byteBufferWrap.remaining()];
            byteBufferWrap.get(bArr4, 0, byteBufferWrap.remaining());
            return ((InterfaceC1024a) t2.n.d(this.f10376a.B(), this.f10377b.b(bArr3, f10375c))).b(bArr4, bArr2);
        } catch (IndexOutOfBoundsException e2) {
            e = e2;
            throw new GeneralSecurityException("invalid ciphertext", e);
        } catch (NegativeArraySizeException e6) {
            e = e6;
            throw new GeneralSecurityException("invalid ciphertext", e);
        } catch (BufferUnderflowException e7) {
            e = e7;
            throw new GeneralSecurityException("invalid ciphertext", e);
        }
    }
}
