Multiple Updates

/* If there was an error in the above while loop, break out of the do-while construct. */

if (status != 0) break;

/* Call B_EncryptFinal once after all Updates. */ if ((status = B_EncryptFinal

(encryptionObject, blockOfEncryptedData, &outputLenFinal, UPDATE_OUTPUT_SIZE, (B_ALGORITHM_OBJ)NULL_PTR, (A_SURRENDER_CTX *)NULL_PTR)) != 0)

break;

/* Save the encrypted data. */ if ((status = AppendDataToFile

(outputFile, blockOfEncryptedData, outputLenFinal)) != 0)

break;

totalBytesSoFar += outputLenFinal; } while (0);

/* Free up any memory allocated, save it to a file or print it out first if you need to save it. */

T_memset (dataToEncrypt, 0, sizeof (dataToEncrypt));

In the preceeding code example, we took dataToEncryptLen bytes of data to encrypt and passed them to B_EncryptUpdate. The number of bytes of output may or may not be dataToEncryptLen; check outputLenUpdate to see. If fewer than dataToEncryptLen bytes were output, the as-yet-unencrypted input waits in a buffer.

Notice that we did not allocate memory but used the stack; we did this by declaring our buffers to be arrays of unsigned char. This means that the operating system will do the allocating and freeing.

Also notice the call to T_memset, another memory management routine from tstdlib.c. The T_memset routine sets all the bytes of a buffer to a particular value; in this case, it wrote a 0 to every byte in dataToEncrypt. T_memset is described in Chapter 4 of the Reference Manual. When memory is freed, whether by a call to T_free or automatically by the operating system, the data still exists at that location; the operating system has simply marked that area as available for use. For security, overwrite any memory that held sensitive data when you are done with it. This prevents attackers from reconstructing secrets by examining your computer’s memory.

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

3 1

Page 53
Image 53
RSA Security 5.2.2 manual Multiple Updates