ctr_eFuse/hsm_utils/import_rsa_keypair.c
kubodera_yuichi ebd20afe39 hsm_utils:RSA鍵インポートプログラム整理
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@115 ff987cc8-cf2f-4642-8568-d52cce064691
2009-12-28 01:54:41 +00:00

634 lines
17 KiB
C
Raw Blame History

// import key (+ encrypt, decrypt) test for nShield
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// openssl
#include <openssl/err.h>
#include <openssl/bn.h>
#include <openssl/sha.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/aes.h>
#include <openssl/pem.h>
#include "nfastapp.h"
#include "nfkm.h"
#include "rqcard-applic.h"
#include "rqcard-fips.h"
//#include "picky-upcalls.h"
#include "my_hsm_bignum.h"
#include "my_hsm_alloc.h"
#define PRIV_KEY_FILE "/opt/nfast/work/rsa-priv-key2048.der"
#define PUB_KEY_FILE "/opt/nfast/work/rsa-pub-key2048.der"
#define MODULE_ID 1
#define DATA_LEN 256 // bytes
// RSA private key data
typedef struct
{
struct NFast_Bignum *p;
struct NFast_Bignum *q;
struct NFast_Bignum *dmp1;
struct NFast_Bignum *dmq1;
struct NFast_Bignum *iqmp;
struct NFast_Bignum *e;
}
RSAPrivateKeyDataBn;
// RSA public key data
typedef struct
{
struct NFast_Bignum *e;
struct NFast_Bignum *n;
}
RSAPublicKeyData;
// global variable
NFast_AppHandle handle;
NFastApp_Connection nc;
NFKM_WorldInfo *world = NULL;
RQCard card;
RQCard_FIPS fips;
M_KeyID ltid = 0; // the cardset loaded into the module
NFKM_CardSet *cardset = NULL;
// function
int importRSAPrivate( void );
void PrintArray( char *pStr, const unsigned char *pData, int length );
int importRSAPrivate( NFKM_KeyIdent keyident )
{
RSA *privkey = NULL;
FILE *fp;
unsigned char *pPtr, *qPtr, *dmp1Ptr, *dmq1Ptr, *iqmpPtr, *ePtr;
int pLen, qLen, dmp1Len, dmq1Len, iqmpLen, eLen;
M_Command cmd;
M_Reply reply;
NFKM_MakeACLParams map;
NFKM_MakeBlobsParams mbp;
NFKM_Key reg_key;
RSAPrivateKeyDataBn *privBn = NULL;
pPtr = qPtr = dmp1Ptr = dmq1Ptr = iqmpPtr = ePtr = NULL;
pLen = qLen = dmp1Len = dmq1Len = iqmpLen = eLen = 0;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
memset( &map, 0, sizeof( map ) );
memset( &mbp, 0, sizeof( mbp ) );
memset( &reg_key, 0, sizeof( reg_key ) );
// key data open
fp = fopen( PRIV_KEY_FILE, "rb" );
if ( !fp )
{
printf( "error : open %s file\n", PRIV_KEY_FILE );
return 1;
}
privkey = d2i_RSAPrivateKey_fp( fp, NULL );
if ( !privkey )
{
printf( "error : d2i_RSAPrivateKey_fp\n" );
return 1;
}
fclose( fp );
#if 0
printf( "RSA(p) : %d bytes\n", BN_num_bytes( privkey->p ) );
printf( "RSA(q) : %d bytes\n", BN_num_bytes( privkey->q ) );
printf( "RSA(dmp1) : %d bytes\n", BN_num_bytes( privkey->dmp1 ) );
printf( "RSA(dmq1) : %d bytes\n", BN_num_bytes( privkey->dmq1 ) );
printf( "RSA(iqmp) : %d bytes\n", BN_num_bytes( privkey->iqmp ) );
printf( "RSA(e) : %d bytes\n", BN_num_bytes( privkey->e ) );
#endif
// RSA priv key <20>̍\<5C><><EFBFBD>v<EFBFBD>f<EFBFBD><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EA82BC><EFBFBD>o<EFBFBD>C<EFBFBD>i<EFBFBD><69><EFBFBD>ɕϊ<C995>
{
// p
pLen = BN_num_bytes( privkey->p );
pPtr = (char *)malloc( pLen );
if ( pLen != BN_bn2bin( privkey->p, pPtr ) )
{
printf( "BN_bn2bin failed!(p)\n" );
return 1;
}
// q
qLen = BN_num_bytes( privkey->q );
qPtr = (char *)malloc( qLen );
if ( qLen != BN_bn2bin( privkey->q, qPtr ) )
{
printf( "BN_bn2bin failed!(q)\n" );
return 1;
}
// dmp1
dmp1Len = BN_num_bytes( privkey->dmp1 );
dmp1Ptr = (char *)malloc( dmp1Len );
if ( dmp1Len != BN_bn2bin( privkey->dmp1, dmp1Ptr ) )
{
printf( "BN_bn2bin failed!(dmp1)\n" );
return 1;
}
// dmq1
dmq1Len = BN_num_bytes( privkey->dmq1 );
dmq1Ptr = (char *)malloc( dmq1Len );
if ( dmq1Len != BN_bn2bin( privkey->dmq1, dmq1Ptr ) )
{
printf( "BN_bn2bin failed!(dmq1)\n" );
return 1;
}
// iqmp
iqmpLen = BN_num_bytes( privkey->iqmp );
iqmpPtr = (char *)malloc( iqmpLen );
if ( iqmpLen != BN_bn2bin( privkey->iqmp, iqmpPtr ) )
{
printf( "BN_bn2bin failed!(dmq1)\n" );
return 1;
}
// e
eLen = BN_num_bytes( privkey->e );
ePtr = (char *)malloc( eLen );
if ( eLen != BN_bn2bin( privkey->e, ePtr ) )
{
printf( "BN_bn2bin failed!(e)\n" );
return 1;
}
} // rsa bignum(openssl) -> bin
// <20>o<EFBFBD>C<EFBFBD>i<EFBFBD><69><EFBFBD><EFBFBD>HSM<53><4D>Bignum<75>ɕϊ<C995>
{
my_bin2bignum( &(privBn->p), handle, pPtr, pLen );
my_bin2bignum( &(privBn->q), handle, qPtr, qLen );
my_bin2bignum( &(privBn->dmp1), handle, dmp1Ptr, dmp1Len );
my_bin2bignum( &(privBn->dmq1), handle, dmq1Ptr, dmq1Len );
my_bin2bignum( &(privBn->iqmp), handle, iqmpPtr, iqmpLen );
my_bin2bignum( &(privBn->e), handle, ePtr, eLen );
free( pPtr );
free( qPtr );
free( dmp1Ptr );
free( dmq1Ptr );
free( iqmpPtr );
free( ePtr );
}
#if 0
printf( "RSA(p) : 0x%08X\n", (unsigned int)privBn->p );
printf( "RSA(q) : 0x%08X\n", (unsigned int)privBn->q );
printf( "RSA(dmp1) : 0x%08X\n", (unsigned int)privBn->dmp1 );
printf( "RSA(dmq1) : 0x%08X\n", (unsigned int)privBn->dmq1 );
printf( "RSA(iqmp) : 0x%08X\n", (unsigned int)privBn->iqmp );
printf( "RSA(e) : 0x%08X\n", (unsigned int)privBn->e );
#endif
// make ACL
if ( cardset != NULL )
map.f = NFKM_NKF_RecoveryEnabled | NFKM_NKF_ProtectionCardSet;
else
map.f = NFKM_NKF_RecoveryEnabled | NFKM_NKF_ProtectionModule;
// <20><EFBFBD><E996A7><EFBFBD>ɂ<EFBFBD> DECRYPT <20><> SIGN
// <20><><EFBFBD>J<EFBFBD><4A><EFBFBD>ɂ<EFBFBD> ENCRYPT <20><> VERIFY <20><><EFBFBD><EFBFBD><EFBFBD>Z<EFBFBD>b<EFBFBD>g<EFBFBD>ł<EFBFBD><C582>Ȃ<EFBFBD><C882>H<EFBFBD>H
map.op_base = NFKM_DEFOPPERMS_DECRYPT | NFKM_DEFOPPERMS_SIGN;
map.cs = cardset;
result = NFKM_newkey_makeaclx( handle, nc, world, &map, &(cmd.args.import.acl), NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_newkey_makeaclx\n", result );
return result;
}
printf( "import.\n" );
// import key
cmd.cmd = Cmd_Import;
cmd.args.import.module = MODULE_ID;
cmd.args.import.data.type = KeyType_RSAPrivate;
cmd.args.import.data.data.rsaprivate.p = pBn;
cmd.args.import.data.data.rsaprivate.q = qBn;
cmd.args.import.data.data.rsaprivate.dmp1 = dmp1Bn;
cmd.args.import.data.data.rsaprivate.dmq1 = dmq1Bn;
cmd.args.import.data.data.rsaprivate.iqmp = iqmpBn;
cmd.args.import.data.data.rsaprivate.e = eBn;
result = NFastApp_Transact( nc, NULL, &cmd, &reply, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : Cmd_Import\n", result );
return 1;
}
result = reply.status;
if ( result != Status_OK )
{
printf( "error(%d) : Cmd_Import(reply)\n", result );
return 1;
}
printf( "keyid : 0x%08X\n", (unsigned int)reply.reply.import.key );
// make blobs
reg_key.v = Key__maxversion; // TORIAEZU Version Max (8)
reg_key.name = keyident.ident;
reg_key.appname = keyident.appname;
reg_key.ident = keyident.ident;
time( &(reg_key.gentime) );
mbp.f = map.f;
mbp.kpriv = reply.reply.import.key;
mbp.lt = ltid;
mbp.cs = cardset;
result = NFKM_newkey_makeblobsx( handle, nc, world, &mbp, &reg_key, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_newkey_makeblobsx\n", result );
return 1;
}
printf( "recordkey.\n" );
// record key to disk
result = NFKM_recordkey( handle, &reg_key, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_recordkey\n", result );
return 1;
}
// destroy key
result = NFKM_cmd_destroy( handle, nc, 0, reply.reply.import.key, "destroy import key", NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_cmd_destroy\n", result );
return 1;
}
printf( "record key success.\n" );
} // import_rsa_private
void PrintArray( char *pStr, const unsigned char *pData, int length )
{
int i;
printf( "%s(%d bytes)", pStr, length );
for ( i = 0; i < length; i++ )
{
if ( (i % 16) == 0 ) printf( "\n" );
printf( "%02X ", pData[ i ] );
}
printf( "\n" );
} // PrintArray
int main( int argc, char *argv[] )
{
int i;
int result = 0;
int rand_size = 80;
M_Command cmd;
M_Reply reply;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
M_KeyID keyid;
NFKM_Key *keyinfo;
if ( argc == 2 )
rand_size = atoi( argv[1] );
// init nFast
result = NFastApp_InitEx( &handle, NULL, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFastApp_InitEx\n", result );
return 0;
}
// connecting to hardserver
result = NFastApp_Connect( handle, &nc, 0, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFastApp_Connect\n", result );
return 0;
}
// set bignum upcalls setting
result = NFastApp_SetBignumUpcalls(
handle,
my_bignumreceiveupcall,
my_bignumsendlenupcall,
my_bignumsendupcall,
my_bignumfreeupcall,
my_bignumformatupcall,
NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFastApp_SetBignumUpcalls\n", result );
return 0;
}
// NFKM getinfo
result = NFKM_getinfo( handle, &world, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_getinfo\n", result );
return 0;
}
// init card-loading lib
result = RQCard_init( &card, handle, nc, world, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : RQCard_init\n", result );
return 0;
}
// init FIPS state
result = RQCard_fips_init( &card, &fips );
if ( result != Status_OK )
{
printf( "error(%d) : RQCard_fips_init\n", result );
return 0;
}
// ui select
result = RQCard_ui_default( &card );
if ( result != Status_OK )
{
printf( "error(%d) : RQCard_ui_default\n", result );
return 0;
}
// get strict-FIPS authorization
#if 0
NFKM_FIPS140AuthHandle fipsHandle;
M_SlotID slotId;
result = RQCard_fips_get( &fips, 1, &fipsHandle, &slotId );
if ( result != Status_OK )
{
printf( "error(%d) : RQCard_fips_get\n", result );
return 0;
}
if ( fipsHandle == NULL )
{
printf( "this sworld isn't strict-FIPS.\n" );
}
#endif
#if 0
// list cardsets
int card_num;
NFKM_CardSetIdent *cardident = NULL;
result = NFKM_listcardsets( handle, &card_num, &cardident, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_listcardsets\n", result );
return 0;
}
// find cardsets
result = NFKM_findcardset( handle, cardident, &cardset, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_findcardset\n", result );
return 0;
}
// load cardset
result = RQCard_logic_ocs_specific( &card, &(cardset->hkltu), "Load Cardset" );
if ( result != Status_OK )
{
printf( "error(%d) : RQCard_logic_ocs_specific\n", result );
return 0;
}
// use specific module : #1
// important!! : if you set resultplace=NULL, abort. (possibility is 100%)
result = RQCard_whichmodule_specific( &card, world->modules[0]->module, &ltid );
if ( result != Status_OK )
{
printf( "error(%d) : RQCard_whichmodule_specific\n", result );
}
// wait event loop
result = card.uf->eventloop( &card );
if ( result != Status_OK )
{
printf( "error(%d) : card module event loop\n", result );
}
#endif
// get usable module
NFKM_ModuleInfo *moduleinfo = world->modules[0];
result = NFKM_getusablemodule( world, MODULE_ID, &moduleinfo );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_getusablemodule\n", result );
}
// RSA privkey <20>̃C<CC83><43><EFBFBD>|<7C>[<5B>g
NFKM_KeyIdent keyident = { (char*)"simple", (char*)"rsa-import-privkey" };
result = importRSAPrivate( keyident );
if ( result != Status_OK )
{
printf( "error(%d) : importRSAPrivate\n", result );
}
// list key
#if 0
int key_num;
NFKM_KeyIdent *keylist = NULL;
result = NFKM_listkeys( handle, &key_num, &keylist, "simple", NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_listkeys\n", result );
}
NFKM_KeyIdent **tkp = &keylist;
for ( i = 0; i < key_num; i++ )
{
printf( "appname : %s, ident : %s\n", tkp[i]->appname, tkp[i]->ident );
}
#endif
// find key
NFKM_KeyIdent ki_v = { (char*)"simple", (char*)"rsa-import-privkey" };
printf( "appname : %s, ident : %s\n", ki_v.appname, ki_v.ident );
result = NFKM_findkey( handle, ki_v, &keyinfo, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_findkey\n", result );
}
// load blob
M_ByteBlock *blobptr;
if ( keyinfo->pubblob.len)
blobptr = &keyinfo->pubblob;
else
{
blobptr = &keyinfo->privblob;
}
result = NFKM_cmd_loadblob( handle, nc,
moduleinfo->module, blobptr, ltid, &keyid, "loading key blob", NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFKM_cmd_loadblob\n", result );
}
printf( "key ID : %u\n", (unsigned int)keyid );
// get key info
cmd.cmd = Cmd_GetKeyInfo;
cmd.args.getkeyinfo.key = keyid;
result = NFastApp_Transact( nc, NULL, &cmd, &reply, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : FastApp_Transact(Cmd_GetKeyInfo)\n", result );
}
// if type == 30 then Rijndael(AES)
printf( "keytype : %d\n", reply.reply.getkeyinfo.type );
// encrypt & dectypt test
{
M_ByteBlock enc_input, dec_input;
M_ByteBlock enc_output, dec_output;
M_IV base_iv, enc_iv, dec_iv;
// data setting
enc_input.len = DATA_LEN;
enc_input.ptr = (unsigned char*)malloc( DATA_LEN );
for ( i = 0; i < enc_input.len; i++ )
enc_input.ptr[i] = i;
base_iv.mech = Mech_RijndaelmCBCpNONE;
for ( i = 0; i < 16; i++ )
base_iv.iv.generic128.iv.bytes[i] = i;
enc_iv = base_iv;
dec_iv = base_iv;
// encrypt : my ver
cmd.cmd = Cmd_Encrypt;
cmd.args.encrypt.key = keyid;
cmd.args.encrypt.mech = Mech_RijndaelmCBCpNONE;
cmd.args.encrypt.plain.type = PlainTextType_Bytes;
cmd.args.encrypt.plain.data.bytes.data = enc_input;
cmd.args.encrypt.flags = Cmd_Encrypt_Args_flags_given_iv_present;
cmd.args.encrypt.given_iv = &enc_iv;
result = NFastApp_Transact( nc, NULL, &cmd, &reply, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : FastApp_Transact(Cmd_Encrypt)\n", result );
}
result = reply.status;
if ( result != Status_OK )
{
printf( "error(%d) : reply.status(Cmd_Encrypt)\n", result );
}
enc_output.len = reply.reply.encrypt.cipher.data.generic128.cipher.len;
if ( enc_output.len != DATA_LEN )
{
printf( "error : output data size isn't %d bytes(Cmd_Encrypt)\n", (int)enc_output.len );
}
enc_output.ptr = (unsigned char*)malloc( enc_output.len );
memcpy( enc_output.ptr,
reply.reply.encrypt.cipher.data.generic128.cipher.ptr,
enc_output.len );
printf( "encrypt ok\n" );
dec_input.len = enc_output.len;
dec_input.ptr = (unsigned char*)malloc( dec_input.len );
memcpy( dec_input.ptr, enc_output.ptr, DATA_LEN );
NFastApp_Free_Reply( handle, NULL, NULL, &reply );
// decrypt : my ver
cmd.cmd = Cmd_Decrypt;
cmd.args.decrypt.flags = 0;
cmd.args.decrypt.key = keyid;
cmd.args.decrypt.mech = Mech_RSApPKCS1;
cmd.args.decrypt.cipher.mech = Mech_RSApPKCS1;
cmd.args.decrypt.cipher.data.generic128.cipher = dec_input;
cmd.args.decrypt.cipher.iv = dec_iv.iv;
cmd.args.decrypt.reply_type = PlainTextType_Bytes;
result = NFastApp_Transact( nc, NULL, &cmd, &reply, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : FastApp_Transact(Cmd_Decrypt)\n", result );
}
result = reply.status;
if ( result != Status_OK )
{
printf( "error(%d) : reply.status(Cmd_Decrypt)\n", result );
}
dec_output.len = reply.reply.decrypt.plain.data.bytes.data.len;
if ( dec_output.len != DATA_LEN )
{
printf( "error : output size isn't %d bytes(Cmd_Decrypt)\n", (int)enc_output.len );
}
dec_output.ptr = (unsigned char*)malloc( dec_output.len );
memcpy( dec_output.ptr,
reply.reply.decrypt.plain.data.bytes.data.ptr,
dec_output.len );
printf( "decrypt ok\n" );
NFastApp_Free_Reply( handle, NULL, NULL, &reply );
// key destroy
memset( &cmd, 0, sizeof( cmd ) ); // fail if NFastApp_Free_Command
cmd.cmd = Cmd_Destroy;
cmd.args.destroy.key = keyid;
result = NFastApp_Transact( nc, NULL, &cmd, &reply, NULL );
if ( result != Status_OK )
{
printf( "error(%d) : NFastApp_Transact(Cmd_Destroy)\n", result );
}
NFastApp_Free_Reply( handle, NULL, NULL, &reply );
// data show
printf( "enc_input : (%d bytes)", (int)enc_input.len );
for ( i = 0; i < enc_input.len; i++ )
{
if ( i % 16 == 0 )
printf( "\n" );
printf( "%02X ", enc_input.ptr[i] );
}
printf( "\n" );
printf( "\nenc_output : (%d bytes)", (int)enc_output.len );
for ( i = 0; i < enc_output.len; i++ )
{
if ( i % 16 == 0 )
printf( "\n" );
printf( "%02X ", enc_output.ptr[i] );
}
printf( "\n" );
printf( "\ndec_output : (%d bytes)", (int)dec_output.len );
for ( i = 0; i < dec_output.len; i++ )
{
if ( i % 16 == 0 )
printf( "\n" );
printf( "%02X ", dec_output.ptr[i] );
}
printf( "\n" );
} // encrypt & decrypt
// end processing
RQCard_fips_free( &card, &fips );
RQCard_destroy( &card );
NFKM_freekey( handle, keyinfo, NULL );
NFKM_freeinfo( handle, &world, NULL );
NFastApp_Disconnect( nc, NULL );
NFastApp_Finish( handle, NULL );
return 0;
} // main