Chapter 7 Public-Key Operations 301

Performing Elliptic Curve Operations
Step 5: Final
Step 6: Destroy
Destroy all objects that are no longer needed. Also, be sure to zeroize and free any allocated memory when it is no longer needed.
unsigned int maxEncryptedDataLen;
unsigned int outputLenUpdate;
maxEncryptedDataLen = 21 + (2 * fieldElementLen) + dataToEncryptLen;
encryptedData = T_malloc(maxEncryptedDataLen);
if ((status = (encryptedData == NULL_PTR)) != 0)
break;
if ((status = B_EncryptUpdate
(ecESEncrypt, encryptedData, &outputLenUpdate,
maxEncryptedDataLen, dataToEncrypt, dataToEncryptLen,
(B_ALGORITHM_OBJ)NULL_PTR,
(A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;
unsigned int outputLenFinal, outputLenTotal;
if ((status = B_EncryptFinal
(ecESEncrypt, encryptedData + outputLenUpdate,
&outputLenFinal, maxEncryptedDataLen - outputLenUpdate,
randomAlgorithm, (A_SURRENDER_CTX *)NULL_PTR)) != 0)
break;
outputLenTotal = outputLenUpdate + outputLenFinal;
B_DestroyAlgorithmObject (&ecESEncrypt);
B_DestroyKeyObject (&publicKey);
T_free (encryptedData);