Yes, in most if not all jurisdictions, essential cookies are legal. Their purpose is to improve user experience and they’re not supposed to be used for anything beyond that. So essential cookies are generally considered part of the machinery that makes the modern internet work.