Block Ciphers

Step 3a: Creating a Key Object

/* Create a key object */

if ((status = B_CreateKeyObject (&aesKey)) != 0) break;

Step 3b: Setting the Key Data

Now you need to set the key size and pass the bytes of key data. According to the Reference Manual entry for AI_AES_CBCPad, the compatible KI type is KI_Item. A key anywhere from 1-255 bytes is supported. Here, you can use a random 24-byte key. For most applications, a 128-bit key should be sufficient.]

#define KEY_SIZE 24 /* number of bytes in the key */

ITEM aesKeyItem = {NULL, 0};

/* Step 3b: Set the key object with a random AES key */ aesKeyItem.len = KEY_SIZE;

aesKeyItem.data = T_malloc (rc6KeyItem.len);

if ((status = (aesKeyItem.data == NULL_PTR)) != 0) break;

At this point, you can write the key data to aesKeyItem.data. In the sample code, we fill aesKeyItem.data with random bytes:

if ((status = B_GenerateRandomBytes

(randomAlgorithm, aesKeyItem.data, aesKeyItem.len, (A_SURRENDER_CTX *)NULL_PTR)) != 0)

break;

if ((status = B_SetKeyInfo (aesKey, KI_Item, (POINTER)&aesKeyItem)) != 0) break;

Once you have passed in the key data, dispose of aesKeyItem, because it is no longer necessary. Crypto-C has already initialized the key object with the necessary data.

C h a p t e r 6 S y m m e t r i c - K e y O p e r a t i o n s

2 0 3

Page 225
Image 225
RSA Security 5.2.2 manual Creating a Key Object