Working during residency is called "moonlighting" and it's definetely legal, but not all residency programs allow it. Some, however, will encourage it. It is a major factor for some people when choosing a residency as the supplemental income can be substantial.