ctr_eFuse/cr_hsm_code.c
kubodera_yuichi 8c5597c7eb HSM初期化の時にビルドスイッチによって読み出す鍵を切り替えるよう変更
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@24 ff987cc8-cf2f-4642-8568-d52cce064691
2009-12-22 06:30:10 +00:00

739 lines
23 KiB
C
Raw Blame History

/* ====================================================================
* Copyright (c) 1998-2008 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@openssl.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#ifdef USE_HSM
#include "cr_hsm_code.h"
#include "cr_generate_id.h"
// nShield
#include "nfastapp.h"
#include "nfkm.h"
#include "rqcard-applic.h"
#include "rqcard-fips.h"
// nShield optional
#include "simplebignum.h"
// TORIAEZU : nFast variables
NFast_AppHandle hsmHandle;
NFastApp_Connection hsmConnection;
NFKM_WorldInfo *hsmWorld = NULL; // allocate
RQCard hsmCard;
RQCard_FIPS hsmFips;
M_KeyID hsmLtid;
NFKM_ModuleInfo *hsmModuleinfo = NULL;
M_ByteBlock *hsmBlobptr = NULL;
#ifdef ENCRYPT_AES
M_KeyID hsmAeskeyid;
const NFKM_KeyIdent hsmAeskeyident = { (char*)"simple", (char*)"aes-test-key" };
NFKM_Key *hsmAeskeyinfo = NULL; // allocate
static int hsm_aes_load_key( void );
#else // !ENCRYPT_AES
M_KeyID hsmRsaPrivkeyid, hsmRsaPubkeyid;
const NFKM_KeyIdent hsmRsakeyident = { (char*)"simple", (char*)"rsa-priv-key-2048" };
NFKM_Key *hsmRsakeyinfo = NULL; // allocate
static int hsm_rsa_load_keypair( void );
#endif // ENCRYPT_AES
// init HSM
int hsm_initialize( void )
{
int ret_code = 0;
// init HSM
ret_code = NFastApp_InitEx( &hsmHandle, NULL, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : NFastApp_InitEx\n", ret_code );
return ret_code;
}
// set BigNum upcalls for HSM
ret_code = NFastApp_SetBignumUpcalls(
hsmHandle,
sbn_bignumreceiveupcall,
sbn_bignumsendlenupcall,
sbn_bignumsendupcall,
sbn_bignumfreeupcall,
sbn_bignumformatupcall,
NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : NFastApp_SetBignumUpcalls\n", ret_code );
return ret_code;
}
// connect HSM
// arg3 is flag
// NFastApp_ConnectionFlags_Privileged flag need to run by nfast user.
ret_code = NFastApp_Connect( hsmHandle, &hsmConnection, 0, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : NFastApp_Connect\n", ret_code );
return ret_code;
}
// get NFKM info
ret_code = NFKM_getinfo( hsmHandle, &hsmWorld, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : NFKM_getinfo\n", ret_code );
return ret_code;
}
// init Card-Loading Lib(RQCard)
ret_code = RQCard_init( &hsmCard, hsmHandle, hsmConnection, hsmWorld, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : RQCard_init\n", ret_code );
return ret_code;
}
// init FIPS state
ret_code = RQCard_fips_init( &hsmCard, &hsmFips );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : RQCard_fips_init\n", ret_code );
return ret_code;
}
// select Card-Auth UI
ret_code = RQCard_ui_scroll( &hsmCard );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : RQCard_ui_scroll\n", ret_code );
return ret_code;
}
#ifdef ENCRYPT_AES
ret_code = hsm_aes_load_key();
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : hsm_aes_load_key\n", ret_code );
return ret_code;
}
#else // !ENCRYPT_AES
ret_code = hsm_rsa_load_keypair();
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : hsm_rsa_load_keypair\n", ret_code );
return ret_code;
}
#endif // ENCRYPT_AES
return CR_GENID_SUCCESS;
} // hsm_initialize
void hsm_finalize( void )
{
RQCard_fips_free( &hsmCard, &hsmFips );
RQCard_destroy( &hsmCard );
NFKM_freekey( hsmHandle, hsmAeskeyinfo, NULL );
NFKM_freeinfo( hsmHandle, &hsmWorld, NULL );
NFastApp_Disconnect( hsmConnection, NULL );
NFastApp_Finish( hsmHandle, NULL );
} // hsm_finalize
int hsm_generate_random( unsigned char *buf, int bytes )
{
int ret_code;
M_Command cmd;
M_Reply reply;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
cmd.cmd = Cmd_GenerateRandom;
cmd.args.generaterandom.lenbytes = bytes;
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : generate random\n", ret_code );
return ret_code;
}
ret_code = reply.status;
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : generate random reply\n", ret_code );
return ret_code;
}
// buffer copy
memcpy( buf, reply.reply.generaterandom.data.ptr, bytes );
NFastApp_Free_Command( hsmHandle, NULL, NULL, &cmd );
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
return CR_GENID_SUCCESS;
} // hsm_generate_rand
int hsm_get_rtc( time_t *time )
{
int result;
M_Command cmd;
M_Reply reply;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
cmd.cmd = Cmd_GetRTC;
cmd.args.getrtc.module = HSM_MODULE_ID;
result = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( result != CR_GENID_SUCCESS )
{
printf( "error(%d) : get rtc(transaction)\n", result );
return result;
}
result = reply.status;
if ( result != CR_GENID_SUCCESS )
{
printf( "error(%d) : get rtc(reply status)\n", result );
return result;
}
*time = (int)reply.reply.getrtc.time.currenttimelow;
return CR_GENID_SUCCESS;
} // hsm_get_rtc
int hsm_aes_load_key( void )
{
int ret_code = 0;
// find key
ret_code = NFKM_findkey( hsmHandle, hsmAeskeyident, &hsmAeskeyinfo, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : NFKM_findkey\n", ret_code );
return ret_code;
}
// if Key_flags_ProtectionCardSet is enable, eventloop is an essential.
if ( hsmAeskeyinfo->flags & Key_flags_ProtectionCardSet )
{
// load specific OCS
ret_code = RQCard_logic_ocs_specific( &hsmCard, &hsmAeskeyinfo->cardset, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : RQCard_logic_ocs_specific\n", ret_code );
return ret_code;
}
// use specific Module
ret_code = RQCard_whichmodule_specific( &hsmCard, HSM_MODULE_ID, &hsmLtid );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : RQCard_whichmodule_specific\n", ret_code );
return ret_code;
}
// eventloop
ret_code = hsmCard.uf->eventloop( &hsmCard );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : Card eventloop\n", ret_code );
return ret_code;
}
}
// get usable Module
hsmModuleinfo = hsmWorld->modules[0];
ret_code = NFKM_getusablemodule( hsmWorld, HSM_MODULE_ID, &hsmModuleinfo );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : NFKM_getusablemodule\n", ret_code );
return ret_code;
}
// load key blob
if ( hsmAeskeyinfo->pubblob.len )
hsmBlobptr = &hsmAeskeyinfo->pubblob;
else
hsmBlobptr = &hsmAeskeyinfo->privblob;
ret_code = NFKM_cmd_loadblob( hsmHandle, hsmConnection,
hsmModuleinfo->module, hsmBlobptr,
hsmLtid, &hsmAeskeyid,
"loading key blob", NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : NFKM_cmd_loadblob\n", ret_code );
return ret_code;
}
return CR_GENID_SUCCESS;
} // hsm_aes_load_key
int hsm_rsa_load_keypair( void )
{
int ret_code = 0;
// find key
ret_code = NFKM_findkey( hsmHandle, hsmRsakeyident, &hsmRsakeyinfo, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : NFKM_findkey\n", ret_code );
return ret_code;
}
// if Key_flags_ProtectionCardSet is enable, eventloop is an essential.
if ( hsmRsakeyinfo->flags & Key_flags_ProtectionCardSet )
{
// load specific OCS
ret_code = RQCard_logic_ocs_specific( &hsmCard, &hsmRsakeyinfo->cardset, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : RQCard_logic_ocs_specific\n", ret_code );
return ret_code;
}
// use specific Module
ret_code = RQCard_whichmodule_specific( &hsmCard, HSM_MODULE_ID, &hsmLtid );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : RQCard_whichmodule_specific\n", ret_code );
return ret_code;
}
// eventloop
ret_code = hsmCard.uf->eventloop( &hsmCard );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : Card eventloop\n", ret_code );
return ret_code;
}
}
// get usable Module
hsmModuleinfo = hsmWorld->modules[0];
ret_code = NFKM_getusablemodule( hsmWorld, HSM_MODULE_ID, &hsmModuleinfo );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : NFKM_getusablemodule\n", ret_code );
return ret_code;
}
// load key blob
printf( "pubblob.len : %d, privblob.len : %d\n",
(int)hsmRsakeyinfo->pubblob.len, (int)hsmRsakeyinfo->privblob.len );
hsmBlobptr = &hsmRsakeyinfo->privblob;
ret_code = NFKM_cmd_loadblob( hsmHandle, hsmConnection,
hsmModuleinfo->module, hsmBlobptr,
hsmLtid, &hsmRsaPrivkeyid,
"loading priv-key blob", NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : NFKM_cmd_loadblob(priv-key)\n", ret_code );
return ret_code;
}
printf( "RSA private key object load : ok\n" );
hsmBlobptr = &hsmRsakeyinfo->pubblob;
ret_code = NFKM_cmd_loadblob( hsmHandle, hsmConnection,
hsmModuleinfo->module, hsmBlobptr,
hsmLtid, &hsmRsaPubkeyid,
"loading pub-key blob", NULL );
if ( ret_code != Status_OK )
{
printf( "error(%d) : NFKM_cmd_loadblob(pub-key)\n", ret_code );
}
printf( "RSA public key object load : ok\n" );
// get key info
{
M_Command cmd;
M_Reply reply;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
cmd.cmd = Cmd_GetKeyInfo;
cmd.args.getkeyinfo.key = hsmRsaPrivkeyid;
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != Status_OK )
{
printf( "error(%d) : FastApp_Transact(Cmd_GetKeyInfo)\n", ret_code );
}
}
#if 0
// get & set key acl
{
int i, j;
M_Command cmd;
M_Reply reply;
M_ACL newACL;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
// GetACL
cmd.cmd = Cmd_GetACL;
cmd.args.getacl.key = hsmRsaPrivkeyid;
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != Status_OK )
{
printf( "error(%d) : FastApp_Transact(Cmd_GetKeyInfo)\n", ret_code );
}
printf( "n_groups : %d\n", reply.reply.getacl.acl.n_groups );
for ( i = 0; i < reply.reply.getacl.acl.n_groups; i++ )
{
printf( "group[%d] : %d actions ok.\n", i, reply.reply.getacl.acl.groups[i].n_actions );
for ( j = 0; j < reply.reply.getacl.acl.groups[i].n_actions; j++ )
{
if ( reply.reply.getacl.acl.groups[i].actions[j].type == 1 ) // Act_OpPermissions
printf( "OpPermissions : %08X\n",
(int)reply.reply.getacl.acl.groups[i].actions[j].details.oppermissions.perms );
}
}
newACL = reply.reply.getacl.acl;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
// find cardsets
NFKM_CardSet *cardset = NULL;
ret_code = NFKM_findcardset( hsmHandle, &(hsmRsakeyinfo->cardset), &cardset, NULL );
if ( ret_code != Status_OK )
{
printf( "error(%d) : NFKM_findcardset\n", ret_code );
}
// make new ACL
NFKM_MakeACLParams map;
NFKM_MakeBlobsParams mbp;
M_ACL newACL;
memset( &map, 0, sizeof( map ) );
map.f = NFKM_NKF_RecoveryEnabled | NFKM_NKF_ProtectionCardSet;
map.op_base = (NFKM_DEFOPPERMS_SIGN | NFKM_DEFOPPERMS_VERIFY |
NFKM_DEFOPPERMS_ENCRYPT | NFKM_DEFOPPERMS_DECRYPT );
map.cs = cardset;
ret_code = NFKM_newkey_makeaclx( hsmHandle, hsmConnection, hsmWorld, &map,
&newACL, NULL );
if ( ret_code != Status_OK )
{
printf( "error(%d) : NFKM_newkey_makeaclx\n", ret_code );
}
// SetACL
cmd.cmd = Cmd_SetACL;
cmd.args.setacl.key = hsmRsaPrivkeyid;
cmd.args.setacl.newacl = newACL;
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != Status_OK )
{
printf( "error(%d) : set acl(transaction)\n", ret_code );
}
ret_code = reply.status;
if ( ret_code != Status_OK )
{
printf( "error(%d) : set acl(reply status)\n", ret_code );
}
}
#endif
return CR_GENID_SUCCESS;
} // hsm_rsa_load_keypair
int hsm_aes_encrypt( unsigned char *dst_buf, unsigned char *org_buf, int size )
{
int ret_code = 0;
M_Command cmd;
M_Reply reply;
M_IV enc_iv;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
// iv set
enc_iv.mech = Mech_RijndaelmCBCpNONE;
memset( enc_iv.iv.generic128.iv.bytes, 0, sizeof( enc_iv.iv.generic128.iv.bytes ) );
// encrypt command set
cmd.cmd = Cmd_Encrypt;
cmd.args.encrypt.key = hsmAeskeyid;
cmd.args.encrypt.mech = Mech_RijndaelmCBCpNONE;
cmd.args.encrypt.plain.type = PlainTextType_Bytes;
cmd.args.encrypt.plain.data.bytes.data.len = size;
cmd.args.encrypt.plain.data.bytes.data.ptr = org_buf;
cmd.args.encrypt.flags = Cmd_Encrypt_Args_flags_given_iv_present;
cmd.args.encrypt.given_iv = &enc_iv;
// encrypt command issue
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : Encryption by HSM\n", ret_code );
return ret_code;
}
ret_code = reply.status;
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : Encrypt Reply Status by HSM\n", ret_code );
return ret_code;
}
// buffer copy
memcpy( dst_buf, reply.reply.encrypt.cipher.data.generic128.cipher.ptr, size );
//NFastApp_Free_Command( hsmHandle, NULL, NULL, &cmd ); // <20><><EFBFBD>̂<EFBFBD><CC82>A<EFBFBD>{<7B>[<5B>g<EFBFBD><67><EFBFBD><EFBFBD>
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
return CR_GENID_SUCCESS;
} // hsm_aes_encrypt
int hsm_aes_decrypt( unsigned char *dst_buf, unsigned char *org_buf, int size )
{
int ret_code = 0;
M_Command cmd;
M_Reply reply;
M_IV dec_iv;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
// iv set
dec_iv.mech = Mech_RijndaelmCBCpNONE;
memset( dec_iv.iv.generic128.iv.bytes, 0, sizeof( dec_iv.iv.generic128.iv.bytes ) );
// decyrpt
cmd.cmd = Cmd_Decrypt;
cmd.args.decrypt.flags = 0;
cmd.args.decrypt.key = hsmAeskeyid;
cmd.args.decrypt.mech = Mech_RijndaelmCBCpNONE;
cmd.args.decrypt.cipher.mech = Mech_RijndaelmCBCpNONE;
cmd.args.decrypt.cipher.data.generic128.cipher.len = size;
cmd.args.decrypt.cipher.data.generic128.cipher.ptr = org_buf;
cmd.args.decrypt.cipher.iv = dec_iv.iv;
cmd.args.decrypt.reply_type = PlainTextType_Bytes;
// decrypt command issue
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : Decryption by HSM\n", ret_code );
return ret_code;
}
ret_code = reply.status;
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : Decrypt Reply Status by HSM\n", ret_code );
return ret_code;
}
// buffer copy
memcpy( dst_buf, reply.reply.decrypt.plain.data.bytes.data.ptr, size );
//NFastApp_Free_Command( hsmHandle, NULL, NULL, &cmd ); // <20><><EFBFBD>̂<EFBFBD><CC82>A<EFBFBD>{<7B>[<5B>g<EFBFBD><67><EFBFBD><EFBFBD>
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
return CR_GENID_SUCCESS;
} // hsm_aes_decrypt
int hsm_rsa_encrypt( unsigned char *dst_buf, unsigned char *org_buf, int size )
{
int ret_code = 0;
M_Command cmd;
M_Reply reply;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
// encrypt command set
cmd.cmd = Cmd_Encrypt;
cmd.args.encrypt.flags = 0;
cmd.args.encrypt.key = hsmRsaPubkeyid;
cmd.args.encrypt.mech = Mech_RSApPKCS1;
cmd.args.encrypt.plain.type = PlainTextType_Bytes;
cmd.args.encrypt.plain.data.bytes.data.len = size;
cmd.args.encrypt.plain.data.bytes.data.ptr = org_buf;
// encrypt command issue
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : Encryption by HSM\n", ret_code );
return ret_code;
}
ret_code = reply.status;
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : Encrypt Reply Status by HSM\n", ret_code );
return ret_code;
}
// buffer copy
memcpy( dst_buf, reply.reply.decrypt.plain.data.bytes.data.ptr, size );
//NFastApp_Free_Command( hsmHandle, NULL, NULL, &cmd ); // <20><><EFBFBD>̂<EFBFBD><CC82>A<EFBFBD>{<7B>[<5B>g<EFBFBD><67><EFBFBD><EFBFBD>
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
return CR_GENID_SUCCESS;
} // hsm_rsa_encrypt
int hsm_rsa_decrypt( unsigned char *dst_buf, unsigned char *org_buf, int size )
{
int ret_code = 0;
M_Command cmd;
M_Reply reply;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
// decyrpt command set
cmd.cmd = Cmd_Decrypt;
cmd.args.decrypt.flags = 0;
cmd.args.decrypt.key = hsmRsaPrivkeyid;
cmd.args.decrypt.mech = Mech_Any;
cmd.args.decrypt.cipher.mech = Mech_RSApPKCS1;
cmd.args.decrypt.cipher.data.generic128.cipher.len = size;
cmd.args.decrypt.cipher.data.generic128.cipher.ptr = org_buf;
cmd.args.decrypt.reply_type = PlainTextType_Bytes;
// decrypt command issue
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : Decryption by HSM\n", ret_code );
return ret_code;
}
ret_code = reply.status;
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error(%d) : Decrypt Reply Status by HSM\n", ret_code );
return ret_code;
}
// buffer copy
memcpy( dst_buf, reply.reply.decrypt.plain.data.bytes.data.ptr, size );
//NFastApp_Free_Command( hsmHandle, NULL, NULL, &cmd ); // <20><><EFBFBD>̂<EFBFBD><CC82>A<EFBFBD>{<7B>[<5B>g<EFBFBD><67><EFBFBD><EFBFBD>
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
return CR_GENID_SUCCESS;
} // hsm_rsa_decrypt
#endif // USE_HSM