| RSA BSAFE Micro Edition Suite |
Streamlined security for mobile and embedded devices |
 
![]() |
/* $Id: r_random.c,v 1.27 2005/02/08 05:57:17 jmckee Exp $ */ /* * Copyright (C) 1998-2003 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. * * */ /* * A source of pseudo random numbers is required for various aspects of the * security protocol and components included in this product. Failure to * appropriately seed the Pseudo Random Number Generator (PRNG) will * seriously impact the security provided. Your application should provide this * random seed. * * The exact requirements for this seeding process may depend upon your * application and the environment for which your application is designed. * See RFC 1750 - Randomness Recommendations for Security. */ #include "r_prod.h" #include "cryp_mod.h" /* Usage message */ static char *r_random_usage[] = { "usage: r_random [options]\n", "where options are:\n", " -seed value - Seed value to use (\"12345678\" default)\n", " -amount value - Amount of random data to be generated (32 Bytes default)\n", #ifdef NO_SOFTWARE_CRYPTO " -no_fips140 - Use non FIPS140 operating mode\n", " -fips140_ssl - Use FIPS140 SSL operating mode\n", #endif /* NO_SOFTWARE_CRYPTO */ " -help - Print this help menu\n", NULL }; /* * Main sample program entry point. * * @param argc [In] The number of arguments typed on the command line. * @param argv [In] The array of individual arguments from the command line. * * @returns R_ERROR_NONE indicates success.<br> * See @ref R_ERROR_IDS for valid values. */ int main(int argc, char **argv) { int ret = R_ERROR_NONE; BIO *bio_out = NULL; BIO *bio_err = NULL; R_RES_LIST *res_list; R_LIB_CTX *lib_ctx = NULL; R_CR_CTX *ctx = NULL; R_CR *rand_obj = NULL; char *seed; unsigned char *buf = NULL; unsigned int len; #ifdef NO_SOFTWARE_CRYPTO R_FIPS140_OPERATING_MODE_T operating_mode = FIPS140_MODE; #endif /* NO_SOFTWARE_CRYPTO */ /* Set the default values */ len = 32; seed = "12345678"; res_list = PRODUCT_DEFAULT_RESOURCE_LIST(); /* * Create BIOs to stdout and stderr. BIOs are the Basic Input/Output * mechanism provided by RSA and are recommended for all input and output * from applications. */ bio_out = BIO_new_fp(stdout, BIO_NOCLOSE); bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); if ((bio_out == NULL) || (bio_err == NULL)) { ret = R_ERROR_ALLOC_FAILURE; goto end; } /* Skip the program name */ argc--; argv++; /* Parse the command line parameters */ while (argc >= 1) { if (Strcmp(*argv, "-seed") == 0) { if (--argc < 1) { goto bad; } seed = *(++argv); } else if (Strcmp(*argv, "-amount") == 0) { if (--argc < 1) { goto bad; } len = atoi(*(++argv)); } #ifdef NO_SOFTWARE_CRYPTO else if (Strcmp(*argv, "-no_fips140") == 0) { operating_mode = NON_FIPS140_MODE; } else if (Strcmp(*argv, "-fips140_ssl") == 0) { operating_mode = FIPS140_SSL_MODE; } #endif /* NO_SOFTWARE_CRYPTO */ else if (Strcmp(*argv, "-help") == 0) { goto bad; } else { BIO_printf(bio_err, "Unknown option %s\n", *argv); goto bad; } argc--; argv++; } if (0) { char **pp; bad: for (pp = r_random_usage; (*pp != NULL); pp++) { BIO_printf(bio_err, *pp); } goto end; } /* * Create the library context to provide access to an implementation with * minimal features */ #ifdef NO_SOFTWARE_CRYPTO /* * For FIPS140 shared library builds set the operating mode required * first */ switch (operating_mode) { case FIPS140_MODE: CRYPTOC_FIPS140_enable_fips140_operating_mode(); break; case NON_FIPS140_MODE: CRYPTOC_FIPS140_enable_non_fips140_operating_mode(); break; case FIPS140_SSL_MODE: CRYPTOC_FIPS140_enable_fips140_ssl_operating_mode(); break; } #endif /* NO_SOFTWARE_CRYPTO */ if ((ret = PRODUCT_LIBRARY_NEW(res_list, R_RES_FLAG_DEF, &lib_ctx)) != R_ERROR_NONE) { BIO_printf(bio_err, "Unable to create library context\n"); goto end; } /* * Create a new cryptographic context. All cryptographic operations * require a cryptographic context in order to access the cryptographic * functionality. */ if ((ret = R_CR_CTX_new(lib_ctx, R_RES_FLAG_DEF, &ctx)) != R_ERROR_NONE) { BIO_printf(bio_err, "Unable to create cryptographic context\n"); goto end; } /* Create a new cryptographic object for random data generation */ if ((ret = R_CR_new(ctx, R_CR_TYPE_RANDOM, R_CR_ID_RANDOM, R_CR_SUB_NONE, &rand_obj)) != R_ERROR_NONE) { BIO_printf(bio_err, "Unable to create cryptographic object\n"); goto end; } /* Seed the random object */ if ((ret = R_CR_random_seed(rand_obj, (unsigned char *) seed, Strlen((char *) seed))) != R_ERROR_NONE) { BIO_printf(bio_err, "Unable to seed cryptographic object\n"); goto end; } /* Generate the random data into an allocated buffer */ if ((buf = (unsigned char *) Malloc(len)) == NULL) { BIO_printf(bio_err, "Unable to allocate output buffer\n"); ret = R_ERROR_ALLOC_FAILURE; goto end; } if ((ret = R_CR_random_bytes(rand_obj, len, buf, &len)) != R_ERROR_NONE) { BIO_printf(bio_err, "Unable to generate random data\n"); goto end; } /* Print the generated random data */ BIO_printf(bio_out, "GENERATED RANDOM DATA:\n"); BIO_dump(bio_out, buf, len); end: /* * Clean up. Report errors if there is an output stream using both the * error and the string representation. Destroy the dynamically allocated * objects and return an exit code. */ if ((ret != R_ERROR_NONE) && (bio_err != NULL)) { #ifndef NO_CRYPTO_ERR BIO_printf(bio_err, "ERROR: (%d) %s\n", ret, R_LIB_CTX_get_error_string(lib_ctx, R_RES_MOD_ID_LIBRARY, ret)); #else /* NO_CRYPTO_ERR */ BIO_printf(bio_err, "ERROR: (%d)\n", ret); #endif /* NO_CRYPTO_ERR */ } if (buf != NULL) { Free(buf); } if (rand_obj != NULL) { R_CR_free(rand_obj); } if (ctx != NULL) { R_CR_CTX_free(ctx); } if (lib_ctx != NULL) { PRODUCT_LIBRARY_FREE(lib_ctx); } if (bio_err != NULL) { BIO_free(bio_err); } if (bio_out != NULL) { BIO_free(bio_out); } return(R_ERROR_EXIT_CODE(ret)); }