B_EncryptFinal
AI_RC4

Introductory Example

function must know the size of the buffer. The Update function will not attempt to place data into unallocated memory; instead, it returns an error if it needs to place more bytes into the buffer than are allocated. In our example, we will use dataToEncryptLen as our output data size.

The seventh argument is a Manual, the description of

random algorithm. Recall that in Chapter 2 of the Reference states:

You may pass (B_ALGORITHM_OBJ)NULL_PTR for all randomAlgorithm arguments.

That is exactly what we will supply in our example.

For the eighth argument, once again we pass a properly cast NULL_PTR as the surrender context. When we put this all together, our Update call is:

if ((status = B_EncryptUpdate

(rc4Encrypter, encryptedData, &outputLenUpdate, dataToEncryptLen, dataToEncrypt, dataToEncryptLen, (B_ALGORITHM_OBJ)NULL_PTR,

(A_SURRENDER_CTX *)NULL_PTR)) != 0) break;

encryptedDataLen = outputLenUpdate + outputLenFinal

Note the warning in the Reference Manual Chapter 2 entry for AI_RC4:

Due to the nature of the RC4 algorithm, security is compromised if multiple data blocks are encrypted with the same RC4 key. Therefore, B_EncryptUpdate cannot be called after B_EncryptFinal. This is because after a call to B_EncryptFinal and B_DecryptFinal, the state of the algorithm object is reset to the state in which it was following the call to B_EncryptInit and B_DecryptInit. To begin an encryption operation for a new data block, you must call B_EncryptInit and supply a new key.

This simply means that you should not use the same key for two different encryption sessions.

Step 5: Final

finalizes the encryption process by encrypting any data that B_EncryptUpdate could not. See Chapter 4 of the Reference Manual for the function’s description and prototype:

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

1 9

Page 41
Image 41
RSA Security 5.2.2 manual Final, If status = BEncryptUpdate