| RSA BSAFE Crypto-C |
Cryptographic Components for C |
| Search |
/* $Id: ecrand.c,v 1.12 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 generates a series of random values. You may want to rerun * this program using the same seed to prove that different random values * are generated each time. */ #include "bsafe.h" #include "demoutil.h" /* in samples/common/include */ #define NUMBER_OF_RANDOM_BYTES 64 B_ALGORITHM_METHOD *RANDOM_CHOOSER[] = { &AM_EC_RANDOM, &AM_SHA256, &AM_ENTROPY, (B_ALGORITHM_METHOD *)NULL_PTR /* * This fixes a problem that the IA64 compiler finds when seeding a * short chooser list */ #ifdef IA64_FORCE_LARGE IA64_FORCE_LARGE #endif }; #ifdef CRYPTOC_APP #define MAIN ecrandMain #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; A_EC_RANDOM_PARAMS ec_params; do { /* * The RSA_* demo code utilities are described in * common/include/demoutil.h. This procedure 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 up the algorithm object for 80 bits PRIME FIELD and SHA256 */ ec_params.strength = 80;; ec_params.prediction_resistance = 1; ec_params.string.data = NULL_PTR; ec_params.string.len = 0; ec_params.curve_type = EC_CT_PRIME_FIELD; ec_params.hashAlgorithm = AI_SHA256; ec_params.hashAlgParams = NULL_PTR; ec_params.mode = 0; /* Set the random algorithm object to EC_RAND */ if ((status = B_SetAlgorithmInfo (randomAlgorithm, AI_ECRandom, (POINTER)&ec_params)) != 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 obtain the random * seed. The method used here is for demonstration purposes only and * prompts for the value. Do not use this method when writing a real * application. */ 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); /* Pass the random seed and its length into B_RandomUpdate() */ if ((status = B_RandomUpdate (randomAlgorithm, randomSeed, randomSeedLen, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; /* * Prepare a buffer to receive the entropy Bytes and then call * 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 ("ecrand", status); /* 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 */