Chapter 6 Symmetric-Key Operations 203
Block Ciphers
Step 3a: Creating a Key Object
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.]
At this point, you can write the key data to
aesKeyItem.data
. In the sample code, we
fill
aesKeyItem.data
with random bytes:
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.
/* Create a key object */
if ((status = B_CreateKeyObject (&aesKey)) != 0)
break;
#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;
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;