No such thing as a small change  
PerlMonks 
Re^3: How to get good primes for DH agreement ?by danaj (Friar) 
on Mar 22, 2019 at 18:42 UTC ( #1231575=note: print w/replies, xml )  Need Help?? 
Yes, using what is sometimes called the PRIMEINC algorithm does introduce a significant bias, as some values will be output much more frequently than some others. At the moment there is no known exploit for this with nontiny bit sizes, and some software uses it because it's not too hard to make it fast. Making the TRIVIAL algorithm (randomly select integers in the range and reject until it passes your primality test) fast takes more effort. I think it is much more likely that something else is a bigger issue, as rolling ones own crypto is full of pitfalls. Admittedly using someone else's crypto stuff also has pitfalls, but they are usually more nonobvious. Math::Prime::Util has a lot of routines for efficiently generating random primes. Relevant to DH, it has a routine for generating random nbit strong primes, but not currently safe primes. I've written one which will go in the next version. It's of course possible to do it by hand, such as something like: The advantage of the call is (1) it's ~10x faster, and (2) it's more convenient. You can also call out to something like openssl. Lastly, consider Crypt::DSA::GMP which follows the FIPS 1862 or FIPS 1864 standards for generating keys. It will give the <p,q,g> parameters you need.
In Section
Seekers of Perl Wisdom

