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.

RSA. For example, public key certificates encode the public exponent within<br />

them, and it is almost universally one of these three values. These numbers are<br />

common because it’s fast to multiply other numbers with these numbers, particularly<br />

in hardware. This number is stored in the RSA object, and it is used for<br />

both encryption and decryption operations.<br />

cb<br />

Callback function; when called, it allows for monitoring the progress of generating<br />

a prime. It is passed directly to the function’s internal call to BN_generate_<br />

prime( ), as discussed in Recipe 7.4.<br />

cb_arg<br />

Application-specific argument that is passed directly to the callback function, if<br />

one is specified.<br />

If you need to generate an “n-bit” key manually, you can do so as follows:<br />

1. Choose two random primes p and q, both of length n/2, using the techniques<br />

discussed in Recipe 7.5. Ideally, both primes will have their two most significant<br />

bits set to ensure that the public key (derived from these primes) is exactly n bits<br />

long.<br />

2. Compute n, the product of p and q. This is the public key.<br />

3. Compute d, the inverse of the chosen exponent, modulo (p –1)× (q – 1). This is<br />

generally done using the extended Euclidean algorithm, which is outside the<br />

scope of this book. See the Handbook of Applied Cryptography by Alfred J. Menezes,<br />

Paul C. Van Oorschot, and Scott A. Vanstone for a good discussion of the<br />

extended Euclidean algorithm.<br />

4. Optionally, precompute some values that will significantly speed up private key<br />

operations (decryption and signing): d mod (p – 1), d mod (q – 1), and the<br />

inverse of q mod p (again using the extended Euclidean algorithm).<br />

Here’s an example, using the OpenSSLBIGNUM library, of computing all the values<br />

you need for a key, given two primes p and q:<br />

#include <br />

typedef struct {<br />

BIGNUM *n;<br />

unsigned long e; /* This number should generally be small. */<br />

} RSA_PUBKEY;<br />

typedef struct {<br />

BIGNUM *n;<br />

BIGNUM *d; /* The actual private key. */<br />

/* These aren't necessary, but speed things up if used. If you do use them,<br />

you don't need to keep n or d around. */<br />

BIGNUM *p;<br />

BIGNUM *q;<br />

328 | 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!