No it's not legal, but they do it to prevent employees from stealing from the company and/or taking huge loans and escaping the country. It's more of a leash around your neck. You totally have the right to keep yours with you at all times, but that might eventually create tension at your work (trust issues).