Block Ciphers
198 RSA BSAFE Crypto-C Developers Guide
Step 3: Init
The next step is to make a call to B_EncryptInit. To do this, you need a key object.
You will first create a key object, and then set the key data.
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_RC6_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
rc6KeyItem.data
. In the sample code, we
fill
rc6KeyItem.data
with random bytes:
Once you have passed in the key data, dispose of
rc6KeyItem
, because it is no longer
necessary. Crypto-C has already initialized the key object with the necessary data.
B_KEY_OBJ rc6Key = (B_KEY_OBJ)NULL_PTR;
if ((status = B_CreateKeyObject (&rc6Key)) != 0)
break;
#define KEY_SIZE 24 /* number of bytes in the key */
ITEM rc6KeyItem = {NULL, 0};
/* Step 3b: Set the key object with a random RC6 key */
rc6KeyItem.len = KEY_SIZE;
rc6KeyItem.data = T_malloc (rc6KeyItem.len);
if ((status = (rc6KeyItem.data == NULL_PTR)) != 0)
break;
if ((status = B_GenerateRandomBytes
(randomAlgorithm, rc6KeyItem.data, rc6KeyItem.len,
(A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;
if ((status = B_SetKeyInfo (rc6Key, KI_Item, (POINTER)&rc6KeyItem)) != 0)
break;