What are possible caveats when generating a group for use as parameters for Diffie-Hellman key exchange? As reusing a widely used group for Diffie-Hellman key exchanges might lead to far easier third-party key discovery through precomputation for that specific group, I would like to know what can possibly go wrong when generating custom groups for use as Diffie-Hellman parameters.

Here is what I currently believe to be true about the requirements. Please correct me or explain further if this is wrong or not detailed enough:


A field is needed for the Diffie-Hellman key exchange to work.
Rings without zero divisors are also integral domains. Integral domains that are finite are also fields. For some reason, a group with a prime modulus is also a finite ring without zero divisors and therefore a finite integral domain and therefore also a field.
To be hard to attack, the modulus needs to be large enough and one needs to be sure that the modulus is actually prime and not just pseudo prime.
One requirement that you don't have listed is that the generator $g$ needs to generate a subgroup that's of a large prime order; here's what can go wrong if that is not true:


If the order of $g$ (which we call $q$) has a factor $r$, then the attacker can, hearing $g^x$, determine $x \bmod r$ in $O(\sqrt{r})$ time.  If $r$ isn't large, this immediately implies that you're leaking data
Worse yet, if the order $r$ is smooth (has no large factors), that makes the DLOG problem easy.


One strategy is, as mandragoe suggests, is look for a prime $p$ with $(p-1)/2$ prime as well.  There is certainly a lot to like about this strategy; you can use (say) $g=2$, which can make the computation easier (and if the order of $g$ happens to be $2q$, all that means is that the attacker can learn the lsbit of your DH private values, which really doesn't tell him much, and you can avoid even that by making sure that $p \equiv 7 \pmod{8}$.

On the other hand, it is also a comparatively costly option, as looking for a value $p$ that meets both conditions means going through a lot of candidates.  Now, if you're doing this computation only rarely (say, once a day), that's really not that big of a deal; however if you're coining a fresh group for every exchange, it is certainly more expensive that you need.

An alternative approach is:


Search for a 256 bit prime $q$
Once you have that, scan through 2048 bit values $kq+1$ for a prime; when you find it, call it $p$
For $g$, select an arbitrary value $x$ (2 works), and compute $x^k \bmod p$; if that value is not 1, use it for $g$.


The order of the generator $g$ will be $q$, which is a sufficiently large prime that we don't need to worry about attacks that take $O(\sqrt{q})$ time.

The expensive operation is the search for $p$, which is no more expensive than any other prime search for that size.