21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Table 7-2. Math operations supported by OpenSSL’s BIGNUM library (continued)<br />

Function Description Limitations Comments<br />

BN_ULONG BN_div_word(BIGNUM<br />

*a, BN_ULONG w);<br />

a = a÷w Returns the remainder.<br />

BN_ULONG BN_mod_word(BIGNUM<br />

*a, BN_ULONG w);<br />

return a mod w<br />

All of the above functions that return an int return 1 on success or 0 on failure. BN_<br />

div_word( ) and BN_mod_word( ) return their result. Note that the type BN_ULONG is simply<br />

a typedef for unsigned long.<br />

See Also<br />

Recipe 11.9<br />

7.5 Generating a Prime Number (Testing for<br />

Primality)<br />

<strong>Problem</strong><br />

You need to generate a random prime number or test to see if a number is prime.<br />

Solution<br />

Use the routines provided by your arbitrary-precision math library, or generate a random<br />

odd number and use the Rabin-Miller primality test to see whether the number<br />

generated is actually prime.<br />

Discussion<br />

Good arbitrary-precision math libraries have functions that can automatically generate<br />

primes and determine to a near certainty whether a number is prime. In addition,<br />

these libraries should have functionality that produces “safe” primes (that is, a<br />

prime whose value minus 1 divided by 2 is also prime). You should also be able to<br />

ask for a prime that gives a particular remainder when you divide that prime by a<br />

particular number. The last two pieces of functionality are useful for generating<br />

parameters for Diffie-Hellman key exchange.<br />

The OpenSSLfunctionality for generating and testing primes is discussed in Recipe<br />

7.4.<br />

The most common way primes are generated is by choosing a random odd number<br />

of the desired bit length from a secure pseudo-random source (we discuss pseudo-<br />

Generating a Prime Number (Testing for Primality) | 323<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!