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.

andomness in depth in Recipe 11.1). Generally, the output of the random number<br />

generator will have the first and last bits set. Setting the last bit ensures that the number<br />

is odd; no even numbers are primes. Setting the first bit ensures that the generated<br />

number really is of the desired bit length.<br />

When generating RSA keys, people usually set the first two bits of all their potential<br />

primes. That way, if you multiply two primes of the same bit length together, they’ll<br />

produce a result that’s exactly twice the bit length. When people talk about the “bit<br />

length of an RSA key,” they’re generally talking about the size of such a product.<br />

For determining whether a number is prime, most people use the Rabin-Miller test,<br />

which can determine primality with high probability. Every time you run the Rabin-<br />

Miller test and the test reports the number “may be prime,” the actual probability of<br />

the number being prime increases dramatically. By the time you’ve run five iterations<br />

and have received “may be prime” every time, the odds of the random value’s<br />

not being prime aren’t worth worrying about.<br />

If you are generating a prime number for use in Diffie-Hellman key exchange (i.e., a<br />

“safe” prime), you should test the extra conditions before you even check to see if<br />

the number itself is prime because doing so will speed up tests.<br />

We provide the following code that implements Rabin-Miller on top of the OpenSSL<br />

BIGNUM library, which almost seems worthless, because if you’re using OpenSSL, it<br />

already contains this test as an API function (again, see Recipe 7.4). However, the<br />

OpenSSLBIGNUM API is straightforward. It should be easy to take this code and<br />

translate it to work with whatever package you’re using for arbitrary precision math.<br />

Do note, though, that any library you use is likely already to have a<br />

function that performs this work for you.<br />

In this code, we explicitly attempt division for the first 100 primes, although we recommend<br />

trying more primes than that. (OpenSSLitself tries 2,048, a widely recommended<br />

number.) We omit the additional primes for space reasons, but you can find<br />

a list of those primes on this book’s web site. In addition, we use spc_rand( ) to get a<br />

random binary value. See Recipe 11.2 for a discussion of this function.<br />

#include <br />

#include <br />

#define NUMBER_ITERS 5<br />

#define NUMBER_PRIMES 100<br />

static unsigned long primes[NUMBER_PRIMES] = {<br />

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,<br />

59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131,<br />

137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,<br />

227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311,<br />

324 | Chapter 7: Public Key Cryptography<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!