RSA BSAFE Micro Edition Suite

Streamlined security for mobile and embedded devices

Search  Print

r_asym_buf.c

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

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

/* The maximum key buffer size for this sample */
#define MAX_KEY_SIZE 1024

/* The DER encoded RSA key stored in binary format */
static unsigned char rsa_der_data[] =
{

    /* Sequence Of, Total Length, Int 0 */
    0x30, 0x82, 0x02, 0x5D, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,

    /* Modulus */
    0x00, 0xe0, 0xe0, 0x6c, 0x8d, 0xbe, 0xb2, 0x8b,
    0xc9, 0xf3, 0xa6, 0x3d, 0xa1, 0x12, 0xea, 0xf7,
    0x99, 0xf7, 0x3d, 0x3e, 0xfa, 0xa3, 0xb1, 0xe2,
    0x42, 0x95, 0x71, 0xb5, 0x71, 0xd2, 0x32, 0x7a,
    0xda, 0x10, 0x40, 0xe2, 0x5b, 0x09, 0x74, 0x69,
    0x08, 0x78, 0x46, 0x37, 0x71, 0x34, 0x3e, 0x69,
    0xa7, 0x37, 0x6d, 0xf8, 0x70, 0x1d, 0xaa, 0xa5,
    0x34, 0xb0, 0x33, 0xa3, 0x43, 0xac, 0x4d, 0xeb,
    0x41, 0x5e, 0x0a, 0x8a, 0xfd, 0xa6, 0x0a, 0x4b,
    0x09, 0x7f, 0x5a, 0x18, 0xf2, 0x9e, 0xc2, 0x22,
    0xa6, 0x6b, 0x9a, 0x69, 0x73, 0x22, 0xd5, 0x37,
    0xc9, 0x63, 0xb0, 0x88, 0xf5, 0x60, 0x5d, 0x99,
    0x16, 0x33, 0x54, 0x53, 0x30, 0xed, 0x35, 0xde,
    0x0c, 0x87, 0x3b, 0x54, 0xba, 0x59, 0x22, 0x3e,
    0xb2, 0x79, 0x90, 0x96, 0x61, 0xdb, 0xf3, 0x4a,
    0x37, 0x18, 0x4c, 0x7f, 0xa8, 0xca, 0xee, 0xd6,
    0x31,

    /* Public Exponent. Value is 65537 (0x1001) */
    0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x81,

    /* Private Exponent */
    0x00,
    0x6b, 0x1f, 0x1d, 0x36, 0xec, 0x77, 0x7b, 0x15,
    0xa9, 0xc6, 0x30, 0x27, 0x71, 0xae, 0x92, 0x62,
    0x3a, 0x9f, 0x67, 0x47, 0xd8, 0x00, 0x9d, 0xca,
    0xa0, 0x0b, 0xf9, 0xa6, 0x0d, 0xbe, 0x54, 0x3d,
    0x5a, 0x6e, 0xbe, 0x25, 0x25, 0xbc, 0xd9, 0x67,
    0xda, 0x7b, 0x80, 0x5f, 0xa1, 0xc6, 0x75, 0x67,
    0xdd, 0x84, 0xba, 0x4b, 0x16, 0x26, 0xba, 0xe9,
    0xfd, 0x61, 0xab, 0xcd, 0x49, 0xe0, 0x18, 0x47,
    0x37, 0x9f, 0x56, 0x08, 0x2d, 0xd9, 0x16, 0x81,
    0xff, 0x7d, 0xd0, 0x7e, 0x01, 0x8f, 0xd4, 0x84,
    0xd3, 0xe8, 0xeb, 0x27, 0x48, 0xc3, 0x6c, 0xdc,
    0xa9, 0x01, 0xb7, 0xe5, 0x24, 0x28, 0xd1, 0x6c,
    0x67, 0x03, 0xa7, 0x63, 0xfb, 0xfa, 0x79, 0xd8,
    0x08, 0x6a, 0xe1, 0xde, 0x3d, 0x12, 0x7a, 0x36,
    0x20, 0x25, 0x01, 0xd1, 0x08, 0x11, 0x0c, 0xcd,
    0x80, 0x44, 0x3c, 0xfd, 0xc5, 0xc4, 0xdb, 0xd1,

    /* Prime.0 */
    0x02, 0x41,
    0x00,
    0xf1, 0x6b, 0xdd, 0x2f, 0xdd, 0xd8, 0xdf, 0x80,
    0x30, 0xe6, 0x9c, 0xd3, 0x4e, 0x46, 0x5e, 0x9f,
    0x42, 0x62, 0xb1, 0x66, 0x86, 0x57, 0x1b, 0xca,
    0x87, 0x9c, 0xcf, 0xfd, 0x1c, 0xb6, 0x26, 0x76,
    0x95, 0x35, 0xbf, 0x0b, 0xfb, 0x51, 0xaf, 0x0f,
    0x46, 0x1c, 0x5e, 0xcb, 0x82, 0xa0, 0x83, 0xbf,
    0x46, 0xc9, 0x3b, 0xd6, 0x4e, 0x7a, 0x5d, 0xbf,
    0x03, 0x05, 0x69, 0x27, 0x31, 0x6d, 0x65, 0xbd,

    /* Prime.1 */
    0x02, 0x41,
    0x00,
    0xee, 0x74, 0xcb, 0xa3, 0xd0, 0x90, 0x2d, 0x8a,
    0xe9, 0xe7, 0x10, 0xdd, 0xb4, 0x65, 0x2e, 0x91,
    0x22, 0x09, 0x52, 0x72, 0xab, 0xbd, 0x32, 0x31,
    0x4e, 0xd7, 0xd0, 0x2b, 0x4b, 0x13, 0x57, 0x20,
    0x6b, 0xf9, 0xa4, 0x57, 0xb1, 0x47, 0x59, 0x67,
    0x86, 0xa6, 0x8c, 0x2c, 0xc1, 0xf3, 0x8b, 0xba,
    0x8a, 0x6b, 0xb1, 0x62, 0x5d, 0x43, 0x5a, 0x71,
    0xdb, 0xd0, 0x33, 0x43, 0x97, 0x99, 0x17, 0x85,

    /* Prime Exponent.0 */
    0x02, 0x41,
    0x00,
    0xa6, 0x35, 0xdc, 0xd2, 0x57, 0xaa, 0x38, 0x35,
    0xc9, 0x74, 0xfc, 0x03, 0x7e, 0xa0, 0x74, 0x04,
    0xb1, 0x6f, 0xc1, 0x33, 0x14, 0xca, 0x64, 0x17,
    0xcb, 0xc5, 0xea, 0x6c, 0x18, 0x98, 0x4f, 0x62,
    0xd4, 0xd7, 0x6b, 0xf0, 0x93, 0xd6, 0x68, 0xef,
    0xdb, 0x15, 0x2d, 0x2e, 0x6f, 0x80, 0x93, 0x33,
    0xdd, 0x48, 0x2e, 0x2a, 0x1d, 0x5d, 0xa1, 0xad,
    0x20, 0x27, 0x59, 0x7d, 0xe2, 0x49, 0xaf, 0x01,

    /* Prime Exponent.1 */
    0x02, 0x40,
    0xcf, 0xf1, 0x9c, 0x30, 0x33, 0xcd, 0xb7, 0x59,
    0x7f, 0x96, 0x57, 0xf7, 0xee, 0xbb, 0x99, 0xbb,
    0x48, 0xa2, 0x36, 0x7a, 0xf7, 0x57, 0x1a, 0xf1,
    0x32, 0xdf, 0x32, 0x92, 0xbe, 0x7a, 0x94, 0x2d,
    0x1a, 0xdb, 0xed, 0xbb, 0xe7, 0x45, 0xe0, 0x2a,
    0x4e, 0x9a, 0xe8, 0x7c, 0x93, 0x7a, 0x4e, 0x2c,
    0x93, 0x4f, 0x4c, 0xb6, 0x09, 0xbc, 0x95, 0x9f,
    0xda, 0xdf, 0x9a, 0x04, 0xe4, 0xab, 0xc5, 0x7d,

    /* Coefficient */
    0x02, 0x40,
    0xe9, 0x3b, 0x10, 0x6e, 0xdc, 0xa4, 0x09, 0x86,
    0x84, 0xec, 0x40, 0x9e, 0x69, 0xad, 0x1e, 0x60,
    0xaf, 0xb7, 0x6d, 0xdc, 0x7c, 0x22, 0xad, 0x55,
    0x03, 0x18, 0xba, 0xc1, 0xe1, 0xd2, 0xcd, 0xcd,
    0xf9, 0xdc, 0x42, 0xd3, 0x2b, 0x6d, 0xf4, 0xfe,
    0xf2, 0x9f, 0x62, 0x8c, 0x8a, 0x4f, 0x76, 0x0d,
    0xf8, 0x34, 0x83, 0x62, 0xee, 0x4d, 0xbf, 0x88,
    0x1d, 0xe0, 0x65, 0x45, 0xaf, 0xec, 0x71, 0x6b
};

/* Usage message */
static char *r_asym_usage[] =
{
    "usage: r_asym_buf [options]\n",
    "where options are:\n",
    " -string value   - String to encrypt and decrypt\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
};

static int encrypt_data(BIO *bio_err, R_CR_CTX *ctx, R_PKEY_CTX *pkey_ctx,
    unsigned char *string, unsigned char *buf, unsigned int *mlen);
static int decrypt_data(BIO *bio_err, R_CR_CTX *ctx, R_PKEY_CTX *pkey_ctx,
    unsigned char *data, unsigned int dlen, unsigned char *buf,
    unsigned int *mlen);

/*
 * Main sample 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 = PRODUCT_DEFAULT_RESOURCE_LIST();
    R_LIB_CTX *lib_ctx = NULL;
    R_CR_CTX *ctx = NULL;
    R_PKEY_CTX *pkey_ctx = NULL;
    unsigned char buf[MAX_KEY_SIZE];
    unsigned int len = MAX_KEY_SIZE;
    char *string;
#ifdef NO_SOFTWARE_CRYPTO
    R_FIPS140_OPERATING_MODE_T  operating_mode = FIPS140_MODE;
#endif /* NO_SOFTWARE_CRYPTO */

    /* Set the default values */
    string = NULL;

    /*
     * 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, "-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, "Data must be supplied for encryption\n");
        goto bad;
    }

    if (0)
    {
        char **pp;

bad:
        for (pp = r_asym_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 asymmetric key context */

    if ((ret = R_PKEY_CTX_new(lib_ctx, R_RES_FLAG_DEF, R_PKEY_TYPE_RSA,
        &pkey_ctx)) != R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Unable to create key context\n");
        goto end;
    }

    /* Encrypt the string with the public key */

    if ((ret = encrypt_data(bio_err, ctx, pkey_ctx, (unsigned char *)string,
        buf, &len)) != R_ERROR_NONE)
    {
        goto end;
    }

    /* Print the encrypted data */
    BIO_printf(bio_out, "ENCRYPTED MESSAGE:\n");
    BIO_dump(bio_out, buf, len);

    /* Retrieve the decrypted string with the private key */

    if ((ret = decrypt_data(bio_err, ctx, pkey_ctx, buf, len, buf, &len)) !=
        R_ERROR_NONE)
    {
        goto end;
    }

    /* Print the decrypted data */
    BIO_printf(bio_out, "DECRYPTED MESSAGE:\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 (pkey_ctx != NULL)
    {
        R_PKEY_CTX_free(pkey_ctx);
    }

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

static int encrypt_data(BIO *bio_err, R_CR_CTX *ctx, R_PKEY_CTX *pkey_ctx,
    unsigned char *string, unsigned char *buf, unsigned int *mlen)
{
    int ret = R_ERROR_NONE;
    R_CR *enc_obj = NULL;
    R_PKEY *pkey = NULL;
    unsigned int cons;
    unsigned char *tmp;

    /*
     * Create an R_PKEY object from the binary key data. Extract the public key
     * information from the binary data and load into a key object. This
     * function will move the data pointer to the end of the buffer, so pass in
     * a copy in case the key data is required later.
     */

    tmp = rsa_der_data;
    if ((ret = R_PKEY_from_binary(pkey_ctx, R_PKEY_FL_BY_REFERENCE,
        R_PKEY_TYPE_RSA, sizeof(rsa_der_data), tmp, &cons, &pkey)) !=
        R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Unable to read public key from buffer\n");
        goto end;
    }

    /*
     * Create a new asymmetric cryptographic object initialized for
     * RSA public encryption with PKCS #1 padding
     */

    if ((ret = R_CR_new(ctx, R_CR_TYPE_ASYM, R_CR_ID_RSA_PKCS1,
        R_CR_SUB_PUB_ENC, &enc_obj)) != R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Unable to create cryptographic object\n");
        goto end;
    }

    /* Load the RSA public key into the cryptographic object */

    if ((ret = R_CR_asym_encrypt_init(enc_obj, pkey)) != R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Unable to initialize cryptographic object\n");
        goto end;
    }

    /* Encrypt the data */

    if ((ret = R_CR_asym_encrypt(enc_obj, string, Strlen((char *)string), buf,
        mlen)) != R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Unable to encrypt data\n");
        goto end;
    }

end:
    /*
     * Clean up. Destroy any dynamically allocated objects and return an
     * error code.
     */

    if (enc_obj != NULL)
    {
        R_CR_free(enc_obj);
    }

    if (pkey != NULL)
    {
        R_PKEY_free(pkey);
    }

    return(ret);
}

/*
 * Performs the asymmetric decryption operation to retrieve a string value.
 *
 * @param bio_err  [In]      The BIO for diagnostic error information.
 * @param ctx      [In]      The cryptographic context reference.
 * @param pkey_ctx [In]      The asymmetric Key context reference.
 * @param data     [In]      The data to decrypt.
 * @param dlen     [In]      The length of <i>data</i>.
 * @param buf      [In, Out] The buffer where the decrypted data is stored.
 * @param mlen     [In, Out] The length of <i>buf</i> consumed.
 */
static int decrypt_data(BIO *bio_err, R_CR_CTX *ctx, R_PKEY_CTX *pkey_ctx,
    unsigned char *data, unsigned int dlen, unsigned char *buf,
    unsigned int *mlen)
{
    int ret = R_ERROR_NONE;
    R_CR *dec_obj = NULL;
    R_PKEY *pkey = NULL;
    unsigned int cons;
    unsigned char *tmp;

    /*
     * Create an R_PKEY object from the binary key data. Extract the public key
     * information from the binary data and load into a key object. This
     * function will move the data pointer to the end of the buffer, so pass in
     * a copy in case the key data is required later.
     */

    tmp = rsa_der_data;
    if ((ret = R_PKEY_from_binary(pkey_ctx, R_PKEY_FL_BY_REFERENCE,
        R_PKEY_TYPE_RSA, sizeof(rsa_der_data), tmp, &cons, &pkey)) !=
        R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Unable to read private key from buffer\n");
        goto end;
    }

    /*
     * Create a new asymmetric cryptographic object initialized for
     * RSA private decryption with PKCS #1 padding
     */

    if ((ret = R_CR_new(ctx, R_CR_TYPE_ASYM, R_CR_ID_RSA_PKCS1,
        R_CR_SUB_PRIV_DEC, &dec_obj)) != R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Unable to create cryptographic object\n");
        goto end;
    }

    /* Load the RSA private key into the cryptographic object */

    if ((ret = R_CR_asym_decrypt_init(dec_obj, pkey)) != R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Unable to initialize cryptographic object\n");
        goto end;
    }

    /* Decrypt the data */

    if ((ret = R_CR_asym_decrypt(dec_obj, data, dlen, buf, mlen)) !=
        R_ERROR_NONE)
    {
        BIO_printf(bio_err, "Unable to decrypt data\n");
        goto end;
    }

end:
    /*
     * Clean up. Destroy any dynamically allocated objects and return an
     * error code.
     */

    if (dec_obj != NULL)
    {
        R_CR_free(dec_obj);
    }

    if (pkey != NULL)
    {
        R_PKEY_free(pkey);
    }

    return(ret);
}


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