Electron diffraction at crystal surfaces: I. Generalization of Darwin's dynamical theory