A SERVICE OF

logo

Chapter 5 Non-Cryptographic Operations 165
Generating Random Numbers
Generating Random Numbers
In the Introductory Example on page 9, we hard-coded the DES key. In an actual
application, you would use randomly-generated values. Crypto-C allows you to
generate a pseudo-random sequence of bytes using a pseudo-random number
generator (PRNG). These PRNGs are based on the message digests MD2, MD5, and
SHA1. This section shows how to use
AI_X962Random_V0, a SHA1-based pseudo-
random number generator. Its implementation can also be used as a model for the
MD2 and MD5 random number generators. This model should be used for most
random-number generation methods.
Note: There is also
AI_X931Random, which is a SHA1-based pseudo-random number
generator that allows multiple streams of randomness. It is intended
primarily for use with
AI_RSAStrongKeyGen, and should not be used for
general-purpose random-number generation. For an example of how to use
AI_X931Random, see Putting It All Together: An X9.31 Example on page 313.
Generating Random Numbers with SHA1
The example in this section corresponds to the file genbytes.c. This example, which
uses
AI_X962Random_V0, can easily be modified to use the PRNGs based on MD2 and
MD5,
AI_MD2Random and AI_MD5Random, respectively.
Step 1: Creating An Algorithm Object
Declare a variable to be B_ALGORITHM_OBJ. As defined in the function prototype in
Chapter 4 of the Reference Manual, its address is the argument for
B_CreateAlgorithmObject:
B_ALGORITHM_OBJ randomAlgorithm = (B_ALGORITHM_OBJ)NULL_PTR;
if ((status = B_CreateAlgorithmObject (&randomAlgorithm)) != 0)
break;