
Introductory Example
18 RSA BSAFE Crypto-C Developer’s Guide
For now, we declare:
For a stream cipher, the length of the encrypted (output) data is equal to the length of
the input data. So we allocate
dataToEncryptLen
bytes with T_malloc:
The previous code sample uses the Crypto-C routine
T_malloc. Crypto-C supplies its
own memory management routines to increase code portability and to meet the
special requirements of handling encrypted data. The Crypto-C memory
management routines reside in the file
tstdlib.c; make sure this file is compiled and
linked in. These routines are described in Chapter 4 of the Reference Manual and in
“Memory-Management Routines” on page 122 of this manual.
In our example, the
T_malloc routine from tstdlib.c returns a pointer to the
allocated memory. If, for some reason, it cannot allocate memory (for example, when
there is not enough memory available),
T_malloc will return NULL_PTR. It is
imperative to always check the return value of
T_malloc, even if you are allocating
only a small number of bytes.
T_malloc also sets an unsigned char * variable; it is a
good idea to initialize this variable to
NULL_PTR. See “Step 6: Destroy” on page 20 for
more information.
The third argument to
B_EncryptUpdate is a pointer to an unsigned int.
B_EncryptUpdate returns a value indicating how many bytes it placed into the output
buffer. It will place this value at the address specified by the pointer to the
unsigned
int
. Make the proper declaration:
Crypto-C might not encrypt all the input data during a call to
B_EncryptUpdate. Any
unprocessed data will be saved in a buffer inside the algorithm object created by
Crypto-C and encrypted during a subsequent call to Update (see “Multiple Updates”
on page 29) or during the call to
B_EncryptFinal (see “Step 5: Final” on page 19). This
is why it is important to keep track of how many bytes Crypto-C wrote to the output
buffer.
The fourth argument to
B_EncryptUpdate is the size of the output buffer. The Update
unsigned char *encryptedData = NULL_PTR;
encryptedData = T_malloc (dataToEncryptLen);
if ((status = (encryptedData == NULL_PTR)) != 0)
break;
unsigned int outputLenUpdate;