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.

BIGNUM bn;<br />

void BN_init(&bn);<br />

If you’re dynamically allocating a BIGNUM object, OpenSSLprovides a function that<br />

allocates and initializes in one fell swoop:<br />

BIGNUM *bn = BN_new( );<br />

You should not use malloc( ) to allocate a BIGNUM object because you are likely to<br />

confuse the library (it may believe that your object is unallocated).<br />

If you would like to deallocate a BIGNUM object that was allocated using BN_new( ),<br />

pass it to BN_free( ).<br />

In addition, for security purposes, you may wish to zero out the memory used by a<br />

BIGNUM object before you deallocate it. If so, pass it to BN_clear( ), which explicitly<br />

overwrites all memory in use by a BIGNUM context. You can also zero and free in one<br />

operation by passing the object to BIGNUM_clear_free( ).<br />

void BN_free(BIGNUM *bn);<br />

void BN_clear(BIGNUM *bn);<br />

void BN_clear_free(BIGNUM *bn);<br />

Some operations may require you to allocate BN_CTX objects. These objects are<br />

scratch space for temporary values. You should always create BN_CTX objects dynamically<br />

by calling BN_CTX_new( ), which will return a dynamically allocated and initialized<br />

BN_CTX object. When you’re done with a BN_CTX object, destroy it by passing it to<br />

BN_CTX_free( ).<br />

BN_CTX *BN_CTX_new(void);<br />

int BN_CTX_free(BN_CTX *c);<br />

Assigning to BIGNUM objects<br />

Naturally, we’ll want to assign numerical values to BIGNUM objects. The easiest way to<br />

do this is to copy another number. OpenSSLprovides a way to allocate a new BIGNUM<br />

object and copy a second BIGNUM object all at once:<br />

BIGNUM *BN_dup(BIGNUM *bn_to_copy);<br />

In addition, if you already have an allocated context, you can just call BN_copy( ),<br />

which has the following signature:<br />

BIGNUM *BN_copy(BIGNUM *destination_bn, BIGNUM *src_bn);<br />

This function returns destination_bn on success.<br />

You can assign the value 0 to a BIGNUM object with the following function:<br />

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

You can also use BN_clear( ), which will write over the old value first.<br />

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