Introductory Example

static unsigned char rc4KeyData[] = {

0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10

};

ITEM rc4KeyItem; rc4KeyItem.data = rc4KeyData; rc4KeyItem.len = sizeof(rc4keyData);

Now we can complete the call to B_SetKeyInfo:

if ((status = B_SetKeyInfo

(rc4Key, KI_Item, (POINTER)&rc4KeyItem)) != 0) break;

As with algorithm objects, once you have set a key object, you should not set it again. If you need another key object, you should create a new one.

Note: In a real application, for security reasons, you might want to zeroize and free your key data immediately after setting the key.

Now that we have created and set our key object, rc4Key, we can pass it as the second argument to B_EncryptInit.

Selecting an Algorithm Chooser

The third argument to B_EncryptInit is an algorithm chooser; this is a structure that specifies which algorithm methods to link in. An algorithm method (AM) is the underlying code that actually performs the cryptographic operation. Because many AIs can perform more than one cryptographic function (for example, AI_RC4 can perform encryption and decryption), an application often has a choice of which underlying algorithm methods need to be linked in.

An algorithm chooser lists all the AMs the application will use; only these AMs will be linked in. Crypto-C comes with a demonstration application containing the algorithm chooser DEMO_ALGORITHM_CHOOSER. You can use this algorithm chooser in any Crypto-C application as long as the module which defines it (choosc.c) is compiled and linked in. However, DEMO_ALGORITHM_CHOOSER will link in all the algorithm methods available, even though an application might use only two or three.

A developer can write an algorithm chooser for the specific application to make the executable image smaller. See “Algorithm Choosers” on page 116 in this manual for

C h a p t e r 2 Q u i c k S t a r t

1 5

Page 37
Image 37
RSA Security 5.2.2 manual Selecting an Algorithm Chooser, Now we can complete the call to BSetKeyInfo