RSA BSAFE Micro Edition Suite

Streamlined security for mobile and embedded devices

Search  Print

r_dgst.c

/* $Id: r_dgst.c,v 1.22 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.
 *
 *
 */

/*
 * @file r_dgst.c
 * This sample demonstrates how to perform digest operations.
 *
 * For example, to:
 *
 * Digest a string using MD5:
 * r_dgst -alg MD5 -string "hello world"
 */

#include "r_prod.h"
#include "cryp_mod.h"


/* Usage message */
static char *r_dgst_usage[] =
{
    "usage: r_dgst [options]\n",
    "where options are:\n",
    " -alg value      - Digest algorithm, one of SHA1 (default), SHA256,"
    "                   SHA384, SHA512 or MD5\n",
    " -string value   - String to digest\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.
 *           See @ref R_ERROR_IDS for valid values.
 */
int main(int argc, char *argv[])
{
    int               ret         = R_ERROR_NONE;
    int               dgst_alg;
    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              *dgst_obj   = NULL;
    char              *string;
    char              *str;
    unsigned char     dgst_buf[R_CR_DIGEST_MAX_LEN];
    unsigned int      dgst_len;
#ifdef NO_SOFTWARE_CRYPTO
    R_FIPS140_OPERATING_MODE_T  operating_mode = FIPS140_MODE;
#endif /* NO_SOFTWARE_CRYPTO */

    /* Set the default values for the digest operation */
    dgst_alg   = R_CR_ID_SHA1;
    string     = NULL;

    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;
    }

    /* Bypass the program name from the argument list */
    argc--;
    argv++;

    /* Parse the command line parameters */
    while (argc >= 1)
    {
        if (Strcmp(*argv, "-alg") == 0)
        {
            if (--argc < 1)
            {
                goto bad;
            }

            str = *(++argv);

            if (Strcmp(str, "SHA1") == 0)
            {
                dgst_alg = R_CR_ID_SHA1;
            }
            else if (Strcmp(str, "SHA256") == 0)
            {
                dgst_alg = R_CR_ID_SHA256;
            }
            else if (Strcmp(str, "SHA384") == 0)
            {
                dgst_alg = R_CR_ID_SHA384;
            }
            else if (Strcmp(str, "SHA512") == 0)
            {
                dgst_alg = R_CR_ID_SHA512;
            }
            else if (Strcmp(str, "MD5") == 0)
            {
                dgst_alg = R_CR_ID_MD5;
            }
            else
            {
                BIO_printf(bio_err, "Unknown algorithm\n");
                ret = R_ERROR_BAD_PARAMETER;
                goto bad;
            }
        }
        else if (Strcmp(*argv, "-string") == 0)
        {
            if (--argc < 1)
            {
                goto bad;
            }

            string = *(++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++;
    }

    /* Validate the command line options */
    if (string == NULL)
    {
        BIO_printf(bio_err,"No data to digest\n");
        goto bad;
    }

    if (0)
    {
        char **pp;

bad:
        for (pp=r_dgst_usage; (*pp != NULL); pp++)
        {
            BIO_printf(bio_err, *pp);
        }

        goto end;
    }

    /*
     * Create the library context to provide access to all configurable aspects
     * of the library
     */
#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 */
    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 digest cryptographic object using the algorithm
     * specified on the command line
     */
    if ((ret = R_CR_new(ctx, R_CR_TYPE_DIGEST, dgst_alg, R_CR_SUB_NONE,
        &dgst_obj)) != R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Unable to create cryptographic object\n");
        goto end;
    }

    /* Initialize the digest object */
    if ((ret = R_CR_digest_init(dgst_obj)) != R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Digest init failure\n");
        goto end;
    }

    /*
     * Digest the data. The data is available in a single block so only one
     * digest update call is required. If the message consists of multiple
     * parts then multiple calls to R_CR_digest_update() must be made.
     */
    if ((ret = R_CR_digest_update(dgst_obj, (unsigned char *)string,
        Strlen(string))) != R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Digest update failure\n");
        goto end;
    }

    /* Return the digest calculated on the data */
    if ((ret = R_CR_digest_final(dgst_obj, dgst_buf, &dgst_len)) !=
        R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Digest final failure\n");
        goto end;
    }

     /* Print the result of the string digest */
    BIO_printf(bio_out, "STRING DIGEST RESULT:\n");
    BIO_dump(bio_out, dgst_buf, dgst_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 (dgst_obj != NULL)
    {
        R_CR_free(dgst_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));
}


Copyright (c) 1999-2005 RSA Security Inc. All rights reserved. 072-001001-2100-001-000 - 2.1