| RSA BSAFE Micro Edition Suite |
Streamlined security for mobile and embedded devices |
 
![]() |
/* $Id: r_asym_items.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 /* Modulus */ unsigned char modulus[] = { 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 */ unsigned char public_exponent[] = { 0x01, 0x00, 0x01 }; /* Private exponent */ unsigned char private_exponent[] = { 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 (p) */ unsigned char prime_0[] = { 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 (q) */ unsigned char prime_1[] = { 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 */ unsigned char exponent_0[] = { 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 */ unsigned char exponent_1[] = { 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 */ unsigned char coefficient[] = { 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 }; /* The flag to control loading of the CRT parameters of the RSA private key */ int no_crt = 0; /* Usage message */ static char *r_asym_usage[] = { "usage: r_asym_items [options]\n", "where options are:\n", " -string value - String to encrypt and decrypt\n", " -no_crt - Do not load the CRT parts of the private key\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); static int load_key_items(BIO *bio_err, R_PKEY *key); int main(int argc, char **argv) { int ret = R_ERROR_NONE; BIO *bio_out = NULL, *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; char *string = NULL; unsigned char buf[MAX_KEY_SIZE]; unsigned int len = MAX_KEY_SIZE; #ifdef NO_SOFTWARE_CRYPTO R_FIPS140_OPERATING_MODE_T operating_mode = FIPS140_MODE; #endif /* NO_SOFTWARE_CRYPTO */ /* * 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; } /* Set global default values */ no_crt = 0; /* 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); } else if (Strcmp(*argv, "-no_crt") == 0) { no_crt = 1; } #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, "Encryption data required\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; /* * Create a new R_PKEY object. (At this stage there is no key data stored * in the object). */ if ((ret = R_PKEY_new(pkey_ctx, R_PKEY_TYPE_RSA, &pkey)) != R_ERROR_NONE) { BIO_printf(bio_err, "Unable to create key object\n"); goto end; } /* Load the individual parts of the key against the R_PKEY object */ if ((ret = load_key_items(bio_err, pkey)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to load key data\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 private 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); } 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; /* * Create a new R_PKEY object. (At this stage there is no key data stored * in the object). */ if ((ret = R_PKEY_new(pkey_ctx, R_PKEY_TYPE_RSA, &pkey)) != R_ERROR_NONE) { BIO_printf(bio_err, "Unable to create key object\n"); goto end; } /* Load the individual parts of the key against the R_PKEY object */ if ((ret = load_key_items(bio_err, pkey)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to load key data\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); } static int load_key_items(BIO *bio_err, R_PKEY *pkey) { int ret = R_ERROR_NONE; int primes = 2; R_ITEM item; /* Add the number of primes to the R_PKEY object */ if ((ret = R_PKEY_set_info(pkey, R_PKEY_INFO_ID_NUM_PRIMES, &primes)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to set number of primes\n"); goto end; } /* Add the binary modulus data */ item.len = sizeof(modulus); item.data = modulus; if ((ret = R_PKEY_set_info(pkey, R_PKEY_INFO_ID_RSA_MOD, &item)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to set the modulus n\n"); goto end; } /* Add the binary public exponent (e) data */ item.len = sizeof(public_exponent); item.data = public_exponent; if ((ret = R_PKEY_set_info(pkey, R_PKEY_INFO_ID_RSA_EXP, &item)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to set the public exponent e\n"); goto end; } /* Add the binary private exponent (d) data */ item.len = sizeof(private_exponent); item.data = private_exponent; if ((ret = R_PKEY_set_info(pkey, R_PKEY_INFO_ID_RSA_PRIV_EXP, &item)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to set the private exponent d\n"); goto end; } /* * Add the binary data for the first prime factor (p: prime_0) */ if (!no_crt) { item.len = sizeof(prime_0); item.data = prime_0; if ((ret = R_PKEY_set_info(pkey, R_PKEY_INFO_ID_RSA_P, &item)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to set the factor p\n"); goto end; } /* * Add the binary data for the second prime factor (q: prime_1). (Note * that if the key is generated with more than 2 primes the identifier * R_PKEY_INFO_ID_RSA_PRIME_N(n) can be used to set the data against * the R_PKEY object.) */ item.len = sizeof(prime_1); item.data = prime_1; if ((ret = R_PKEY_set_info(pkey, R_PKEY_INFO_ID_RSA_Q, &item)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to set the factor q\n"); goto end; } /* Add the binary data for the first factor's dP exponent */ item.len = sizeof(exponent_0); item.data = exponent_0; if ((ret = R_PKEY_set_info(pkey, R_PKEY_INFO_ID_RSA_DMP1, &item)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to set the factor exponent dP\n"); goto end; } /* Add the binary data for the second factor's dQ exponent */ item.len = sizeof(exponent_1); item.data = exponent_1; if ((ret = R_PKEY_set_info(pkey, R_PKEY_INFO_ID_RSA_DMQ1, &item)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to set the factor exponent dQ\n"); goto end; } /* * Add the binary data for the second factor inverse qInv - coefficient * used in CRT */ item.len = sizeof(coefficient); item.data = coefficient; if ((ret = R_PKEY_set_info(pkey, R_PKEY_INFO_ID_RSA_IQMP, &item)) != R_ERROR_NONE) { BIO_printf(bio_err, "Failed to set the coefficient qInv\n"); } } end: return(ret); }