RSA BSAFE Crypto-C

Cryptographic Components for C

Search

sha512.c

/* $Id: sha512.c,v 1.6 2004/12/03 02:08:36 sparki Exp $ */
/*
 * Copyright (C) 1998-2004 RSA Security Inc.
 *
 * This file shall only be used to demonstrate how to interface to an
 * RSA Security Inc. licensed development product.
 *
 * You have a royalty-free right to use, reproduce and distribute this
 * demonstration file, provided that you agree that RSA Security Inc.
 * has no warranty, implied or otherwise, or liability for this
 * demonstration file (including any modified version).  This software
 * is provided "as is" without warranties or representations of any
 * kind. RSA Security disclaims all conditions and warranties, statutory
 * and otherwise, both express and implied, with respect to the software,
 * its quality and performance, including but not limited to, all
 * implied warranties of merchantability, fitness for a particular
 * purpose, title and noninfringement of third party rights. Without
 * limiting the foregoing, RSA Security does not warrant that the
 * software is error-free or that errors in the product will be
 * corrected. You agree that RSA Security shall not be liable for any
 * direct, indirect, incidental, special, consequential, punitive or
 * other damages whatsoever resulting from your use of this software
 * or any modified version.
 *
 *
 */

/*  This program will prompt the user for some input to digest with SHA_512.
 *  You may want to rerun this program using the same input data to prove to
 *  yourself that you get the same digest.  A slight change to the data
 *  will result in a totally different digest.
 */

#include "bsafe.h"
#include "demoutil.h"  /* in samples/common/include */

/*  SHA512 will produce a digest length of 64 bytes */
#define DIGEST_LEN 64
#define BUF_LEN 256         /* arbitrary */

B_ALGORITHM_METHOD *DIGEST_CHOOSER[] = {
  &AM_SHA512,
  (B_ALGORITHM_METHOD *)NULL_PTR
/* This will fix a problem that the IA64 compiler finds when *
 * seeing a short chooser list */
#ifdef IA64_FORCE_LARGE
                IA64_FORCE_LARGE
#endif
};

#ifdef CRYPTOC_APP
#define MAIN mdigestMain
#else
#define MAIN main
#endif

int MAIN(int argc, char *argv[])
{
  B_ALGORITHM_OBJ digester = (B_ALGORITHM_OBJ)NULL_PTR;
  unsigned char dataToDigest[BUF_LEN];
  unsigned int dataToDigestLen;
  unsigned char digestedData[DIGEST_LEN];
  unsigned int digestedDataLen;

  int status;

  do {
    /* The RSA_* demo code utilities are described in
       common/include/demoutil.h.  This procedure simply checks the
       command-line arguments for input or output options. */
    if ((status = RSA_SetOptions (argc, argv)) != 0)
      break;

    RSA_PrintMessage ("Message Digest using SHA 512\n");
    RSA_PrintMessage ("============================\n");

    /*  Create an algorithm object */
    if ((status = B_CreateAlgorithmObject (&digester)) != 0)
      break;

    /*  Set the algorithm object to SHA512.  This could also be set
        to AI_SHA256 or AI_SHA384, in which case the AM in the chooser
        would need to change and the DIGESTLEN would need to change.
        For AI_SHA256 the digest length is 32, for AI_SHA384 it should
        be 48.*/
    if ((status = B_SetAlgorithmInfo (digester, AI_SHA512, NULL_PTR)) != 0)
      break;

    /*  Initialize the digest algorithm and then prompt the user for input. */
    if ((status = B_DigestInit (digester, (B_KEY_OBJ)NULL_PTR, DIGEST_CHOOSER,
                                (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    do {
      status = RSA_GetCommand ((char *)dataToDigest, sizeof (dataToDigest),
                               "Enter data to digest");
    } while (status != 0);

    dataToDigestLen = T_strlen ((char *)dataToDigest);

    RSA_PrintBuf ("Data to Digest", dataToDigest, dataToDigestLen);

    /*  Update the digest from the user's input */
    if ((status = B_DigestUpdate (digester, dataToDigest, dataToDigestLen,
                                  (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /*  Finalize the digest */
    if ((status = B_DigestFinal (digester, digestedData, &digestedDataLen,
                                 DIGEST_LEN,
                                 (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    RSA_PrintBuf ("Digested Data", digestedData, digestedDataLen);
  } while (0);

  if (status != 0)
    RSA_PrintError ("mdigest", status);

  /*  Destroy all objects and free up any allocated memory  */
  B_DestroyAlgorithmObject (&digester);

  T_memset (dataToDigest, 0, BUF_LEN);

  return (status);
} /*  end main  */

Copyright (c) 1999-2005 RSA Security Inc. All rights reserved. 068-001001-6210-001-000 - 6.2.1