RSA BSAFE Crypto-C

Cryptographic Components for C

Search

genbytes.c

/* $Id: genbytes.c,v 1.7 2004/12/03 02:08:40 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 generate a series of random values.  You may want
 *  to rerun this program using the same seed to prove that the same 
 *  random values are generated.  
 */

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

#define NUMBER_OF_RANDOM_BYTES 64

B_ALGORITHM_METHOD *RANDOM_CHOOSER[] = {
  &AM_SHA_RANDOM,
  (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 genbytesMain
#else
#define MAIN main
#endif

int MAIN(int argc, char *argv[])
{
  B_ALGORITHM_OBJ randomAlgorithm = (B_ALGORITHM_OBJ)NULL_PTR;
  unsigned char * randomSeed = NULL_PTR;
  unsigned int randomSeedLen = 0;
  unsigned char *randomByteBuffer = NULL_PTR;
  unsigned 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 ("Generating random bytes\n");
    RSA_PrintMessage ("=======================\n");

    /*  Create a random algorithm object */
    if ((status = B_CreateAlgorithmObject (&randomAlgorithm)) != 0)
      break;

    /*  Set the random algorithm object to SHA1 */
    if ((status = B_SetAlgorithmInfo (randomAlgorithm, AI_SHA1Random,
                                      NULL_PTR)) != 0)
      break;

    /*  Initialize the random algorithm. */
    if ((status = B_RandomInit (randomAlgorithm, RANDOM_CHOOSER,
                                (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /*  First, set aside memory to hold the random seed.  
                Then get the random seed.  The method used here prompts
                for the value, which is certainly not a method to use 
                when writing a real application.
                It is simply for illustrative purposes.  */
    randomSeedLen = 256;
    randomSeed = T_malloc (randomSeedLen);
    if (randomSeed == NULL_PTR) {
      status = RSA_DEMO_E_ALLOC;
      break;
    }

    T_memset (randomSeed, 0, randomSeedLen);

    RSA_PrintBuf ("Contents of randomSeed before seeding",
                  randomSeed, randomSeedLen);

    if ((status = RSA_GetCommand ((char *)randomSeed, randomSeedLen,
                                  "Enter a random seed")) != 0)
      break;

    RSA_PrintBuf ("Random Seed", randomSeed, randomSeedLen);

    /*  Now we have a random seed and its length.  Pass both
        into B_RandomUpdate.  */
    if ((status = B_RandomUpdate (randomAlgorithm, randomSeed, randomSeedLen,
                                  (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /*  Generate.  First, prepare a buffer for receiving the
        random bytes before calling B_GenerateRandomBytes.  */
    randomByteBuffer = T_malloc (NUMBER_OF_RANDOM_BYTES);
    if (randomByteBuffer == NULL_PTR) {
      status = RSA_DEMO_E_ALLOC;
      break;
    }

    T_memset (randomByteBuffer, 0, NUMBER_OF_RANDOM_BYTES);

    if ((status = B_GenerateRandomBytes (randomAlgorithm, randomByteBuffer,
                                         NUMBER_OF_RANDOM_BYTES,
                                         (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    RSA_PrintBuf ("Pseudo-random Bytes Generated", randomByteBuffer,
                  NUMBER_OF_RANDOM_BYTES);
  } while (0);

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

  /*  Remember to destroy all objects, and free all memory  */
  B_DestroyAlgorithmObject (&randomAlgorithm);

  if (randomSeed != NULL_PTR)
  {
    T_memset (randomSeed, 0, randomSeedLen);
    T_free (randomSeed);
  }

  if (randomByteBuffer != NULL_PTR)
    T_free (randomByteBuffer);

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

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