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.

There’s a similar function for assigning the value 1:<br />

int BN_one(BIGNUM *bn);<br />

You can also assign any nonnegative value that fits in an unsigned long using the<br />

function BN_set_word( ):<br />

int BN_set_word(BIGNUM *bn, unsigned long value);<br />

The previous three functions return 1 on success.<br />

If you need to assign a positive number that is too large to represent as an unsigned<br />

long, you can represent it in binary as a sequence of bytes and have OpenSSLconvert<br />

the binary buffer to a BIGNUM object. Note that the bytes must be in order from<br />

most significant to least significant. That is, you can’t just point OpenSSLat memory<br />

containing a 64-bit long long (__int64 on Windows) on a little-endian machine,<br />

because the bytes will be backwards. Once your buffer is in the right format, you can<br />

use the function BN_bin2bn( ), which has the following signature:<br />

BIGNUM *BN_bin2bn(unsigned char *buf, int len, BIGNUM *c);<br />

This function has the following arguments:<br />

buf<br />

Buffer containing the binary representation to be converted.<br />

len<br />

Length of the buffer in bits. It does not need to be a multiple of eight. Extra bits<br />

in the buffer will be ignored.<br />

c<br />

BIGNUM object to be loaded with the value from the binary representation. This<br />

may be specified as NULL, in which case a new BIGNUM object will be dynamically<br />

allocated. The new BIGNUM object will be returned if one is allocated; otherwise,<br />

the specified BIGNUM object will be returned.<br />

None of the previously mentioned techniques allows us to represent a negative number.<br />

The simplest technique is to get the corresponding positive integer, then use the<br />

following macro that takes a pointer to a BIGNUM object and negates it (i.e., multiplies<br />

by –1):<br />

#define BN_negate(x) ((x)->neg = (!((x)->neg)) & 1)<br />

Getting BIGNUM objects with random values<br />

Before you can get BIGNUM objects with random values, you need to have seeded the<br />

OpenSSLrandom number generator. (With newer versions of OpenSSL, the generator<br />

will be seeded for you on most platforms; see Recipe 11.9).<br />

One common thing to want to do is generate a random prime number. The API for<br />

this is somewhat complex:<br />

BIGNUM *BN_generate_prime(BIGNUM *ret, int num, int safe, BIGNUM *add, BIGNUM *rem,<br />

void (*callback)(int, int, void *), void *cb_arg);<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Manipulating Big Numbers | 317

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

Saved successfully!

Ooh no, something went wrong!