Message Digests

The following example BER-encodes the preceeding sample digest:

#define DIGEST_LEN 20

#define ALG_ID_LEN DIGEST_LEN + 18

ITEM *sha1AlgInfoBER;

unsigned char digestInfoBER[ALG_ID_LEN]; unsigned int digestInfoBERLen;

if ((status = B_GetAlgorithmInfo

((POINTER *)&sha1AlgInfoBER, digester, AI_SHA1_BER)) != 0) break;

if ((status = B_EncodeDigestInfo

(digestInfoBER, &digestInfoBERLen, ALG_ID_LEN, sha1AlgInfoBER, digestedData, digestedDataLen)) != 0)

break;

To decode BER-encoded information, call B_DecodeDigestInfo. Simply pass the addresses you need; Crypto-C will fill the ITEMs for you:

ITEM retrievedAlgorithmID;

ITEM retrievedDigest;

if ((status = B_DecodeDigestInfo (&retrievedAlgorithmID, &retrievedDigest, digestInfoBER, digestInfoBERLen)) != 0)

break;

Note: When you create an RSA digital signature as specified in PKCS #1, the digestInfo is the data which is encrypted with the RSA private key.

Saving the State of a Digest Algorithm Object

Saved State

The sample program in samples/hashalg/mdigsv.c demonstrates various ways to go about digesting data. As shown in “Creating a Digest” on page 152, one could collect the data to digest in one buffer and present it with one call to B_DigestUpdate() and a call to B_DigestFinal() to retrieve the digest. The DigestDataAll helper function in mdigsv.c shows this scenario. Alternatively, multiple calls could be made to

1 5 6

R S A B S A F E C r y p t o - C D e v e l o p e r ’s G u i d e

Page 178
Image 178
RSA Security 5.2.2 manual Saving the State of a Digest Algorithm Object, Saved State