For Class B airports, I think the answer is realistically yes, due to traffic flows. How about Class C airports? Are there any legal requirements that it has to operate in 24/7, or is a convention?
The short answer is no. It's easy to find examples of class C towers that aren't 24/7, by checking in the Chart Supplement. Two examples are KXNA (as mentioned in the comments) which operates 1130-0500Z and KHSV, which operates 1200-0600Z. Note that the airspace changes when the tower is closed: KXNA becomes class G, and KHSV becomes class E.
As for class B, I would also assume that they're 24/7. I checked a few (KATL, KBOS, KSEA) in the Chart Supplement and they are indeed 24/7 but I don't feel like checking them all :-)