Low-energy behavior of gluons and gravitons from gauge invariance