mirror of
https://github.com/rvtr/ctr_eFuse.git
synced 2025-11-02 00:11:04 -04:00
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@155 ff987cc8-cf2f-4642-8568-d52cce064691
1012 lines
30 KiB
C
1012 lines
30 KiB
C
/* ====================================================================
|
||
* 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>
|
||
#include <cr_alloc.h>
|
||
|
||
#ifdef USE_HSM
|
||
#include "cr_hsm_code.h"
|
||
#include "cr_hsm_alloc.h"
|
||
#include "cr_hsm_bignum.h"
|
||
#include "cr_generate_id.h"
|
||
#include "cr_generate_id_private.h"
|
||
|
||
// nShield
|
||
#include "nfastapp.h"
|
||
#include "nfkm.h"
|
||
#include "rqcard-applic.h"
|
||
#include "rqcard-fips.h"
|
||
|
||
// nFast variables
|
||
NFast_AppHandle hsmHandle;
|
||
NFastApp_Connection hsmConnection;
|
||
NFKM_WorldInfo *hsmWorld = NULL; // allocate
|
||
|
||
#ifdef ENCRYPT_AES
|
||
M_KeyID hsmAesKeyidDev, hsmAesKeyidProd;
|
||
const NFKM_KeyIdent hsmAesKeyidentDev = { (char*)"simple", (char*)"efuse-aes-dev" };
|
||
const NFKM_KeyIdent hsmAesKeyidentProd = { (char*)"simple", (char*)"efuse-aes-prod" };
|
||
|
||
static int hsm_aes_load_key( NFKM_KeyIdent keyident, M_KeyID *keyid );
|
||
#else // !ENCRYPT_AES
|
||
M_KeyID hsmRsaPrivkeyidDev, hsmRsaPubkeyidDev, hsmRsaPrivkeyidProd, hsmRsaPubkeyidProd;
|
||
const NFKM_KeyIdent hsmRsaKeyidentDev = { (char*)"simple", (char*)"efuse-rsa-priv-dev" };
|
||
const NFKM_KeyIdent hsmRsaKeyidentProd = { (char*)"simple", (char*)"efuse-rsa-priv-prod" };
|
||
|
||
static int hsm_rsa_load_keypair( NFKM_KeyIdent keyident, M_KeyID *privKeyid, M_KeyID *pubKeyid );
|
||
#endif // ENCRYPT_AES
|
||
|
||
// ECDSA key
|
||
M_KeyID hsmEcdsaPrivkeyidDev, hsmEcdsaPubkeyidDev;
|
||
M_KeyID hsmEcdsaPrivkeyidProd, hsmEcdsaPubkeyidProd;
|
||
|
||
const NFKM_KeyIdent hsmEcdsaPrivkeyidentDev = { (char*)"simple", (char*)"nct2-priv-dev" };
|
||
const NFKM_KeyIdent hsmEcdsaPubkeyidentDev = { (char*)"simple", (char*)"nct2-pub-dev" };
|
||
const NFKM_KeyIdent hsmEcdsaPrivkeyidentProd = { (char*)"simple", (char*)"nct2-priv-prod" };
|
||
const NFKM_KeyIdent hsmEcdsaPubkeyidentProd = { (char*)"simple", (char*)"nct2-pub-prod" };
|
||
int hsm_ecdsa_load_keypair( NFKM_KeyIdent privKeyident, M_KeyID *privKeyid,
|
||
NFKM_KeyIdent pubKeyident, M_KeyID *pubKeyid );
|
||
|
||
#define EXE_AUTH_OK
|
||
// init HSM
|
||
int hsm_initialize( void )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
|
||
// HSM <20>̃<EFBFBD><CC83>Z<EFBFBD>b<EFBFBD>g<EFBFBD><67><EFBFBD><EFBFBD>
|
||
#ifdef EXE_AUTH_OK
|
||
ret_code = hsm_reset_module();
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
#endif
|
||
|
||
// init HSM
|
||
ret_code = NFastApp_Init( &hsmHandle, my_hsm_malloc, my_hsm_realloc, my_hsm_free, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// set BigNum upcalls for HSM
|
||
ret_code = NFastApp_SetBignumUpcalls(
|
||
hsmHandle,
|
||
my_bignumreceiveupcall,
|
||
my_bignumsendlenupcall,
|
||
my_bignumsendupcall,
|
||
my_bignumfreeupcall,
|
||
my_bignumformatupcall,
|
||
NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// connect HSM
|
||
ret_code = NFastApp_Connect( hsmHandle, &hsmConnection, 0 /* flag */, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// get NFKM info
|
||
ret_code = NFKM_getinfo( hsmHandle, &hsmWorld, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
#ifdef ENCRYPT_AES
|
||
// load aes dev key
|
||
ret_code = hsm_aes_load_key( hsmAesKeyidentDev, &hsmAesKeyidDev );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// load aes prod key
|
||
ret_code = hsm_aes_load_key( hsmAesKeyidentProd, &hsmAesKeyidProd );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
#else // !ENCRYPT_AES
|
||
// load rsa dev keypair
|
||
ret_code = hsm_rsa_load_keypair( hsmRsaKeyidentDev, &hsmRsaPrivkeyidDev, &hsmRsaPubkeyidDev );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// load rsa prod keypair
|
||
ret_code = hsm_rsa_load_keypair( hsmRsaKeyidentProd, &hsmRsaPrivkeyidProd, &hsmRsaPubkeyidProd );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
#endif // ENCRYPT_AES
|
||
|
||
// load ecdsa dev keypair
|
||
ret_code = hsm_ecdsa_load_keypair( hsmEcdsaPrivkeyidentDev, &hsmEcdsaPrivkeyidDev,
|
||
hsmEcdsaPubkeyidentDev, &hsmEcdsaPubkeyidDev );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// load ecdsa prod keypair
|
||
ret_code = hsm_ecdsa_load_keypair( hsmEcdsaPrivkeyidentProd, &hsmEcdsaPrivkeyidProd,
|
||
hsmEcdsaPubkeyidentProd, &hsmEcdsaPubkeyidProd );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
return ret_code;
|
||
} // hsm_initialize
|
||
|
||
int hsm_finalize( void )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
|
||
// void
|
||
NFKM_freeinfo( hsmHandle, &hsmWorld, NULL );
|
||
|
||
ret_code = NFastApp_Disconnect( hsmConnection, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
}
|
||
|
||
// void
|
||
NFastApp_Finish( hsmHandle, NULL );
|
||
|
||
return ret_code;
|
||
} // hsm_finalize
|
||
|
||
int hsm_reset_module( void )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
NFast_AppHandle handle;
|
||
NFastApp_Connection connection;
|
||
M_Command cmd;
|
||
M_Reply reply;
|
||
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
// init HSM
|
||
ret_code = NFastApp_InitEx( &handle, NULL, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[<5B>h<EFBFBD>Őڑ<C590><DA91><EFBFBD><EFBFBD>Ȃ<EFBFBD><C882>ƁAClerUnit, RetryFailedModule<6C>R<EFBFBD>}<7D><><EFBFBD>h<EFBFBD><68><EFBFBD><EFBFBD><EFBFBD>s<EFBFBD>ł<EFBFBD><C582>Ȃ<EFBFBD>
|
||
ret_code = NFastApp_Connect( handle, &connection, NFastApp_ConnectionFlags_Privileged, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// <20><><EFBFBD>W<EFBFBD><57><EFBFBD>[<5B><><EFBFBD>̏<EFBFBD><CC8F>Ԋm<D48A>F
|
||
cmd.cmd = Cmd_NewEnquiry;
|
||
cmd.args.newenquiry.version = EnqVer_Six;
|
||
cmd.args.newenquiry.module = HSM_MODULE_ID;
|
||
ret_code = NFastApp_Transact( connection, NULL, &cmd, &reply, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// <20><><EFBFBD>W<EFBFBD><57><EFBFBD>[<5B><><EFBFBD><EFBFBD> failed state <20>łȂ<C582><C882>Ȃ<EFBFBD><C882>ȉ<EFBFBD><C889>͕K<CD95>v<EFBFBD>Ȃ<EFBFBD><C882>̂ő<CC82><C591>I<EFBFBD><49>
|
||
if ( reply.reply.newenquiry.flags & Cmd_NewEnquiry_Reply_flags_Failed )
|
||
{
|
||
NFastApp_Free_Reply( handle, NULL, NULL, &reply );
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
// failed state <20><><EFBFBD><EFBFBD><EFBFBD>̕<EFBFBD><CC95><EFBFBD><EFBFBD>v<EFBFBD><76>
|
||
// <20>K<EFBFBD><4B> Status_OK <20><><EFBFBD>Ԃ<EFBFBD><D482>Ă<EFBFBD><C482><EFBFBD><EFBFBD>̂ŐM<C590>p<EFBFBD>ł<EFBFBD><C582>Ȃ<EFBFBD>
|
||
cmd.cmd = Cmd_RetryFailedModule;
|
||
cmd.args.retryfailedmodule.module = HSM_MODULE_ID;
|
||
ret_code = NFastApp_Transact( connection, NULL, &cmd, &reply, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
} // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
|
||
NFastApp_Free_Reply( handle, NULL, NULL, &reply );
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
// <20><><EFBFBD>W<EFBFBD><57><EFBFBD>[<5B><><EFBFBD>̃<EFBFBD><CC83>Z<EFBFBD>b<EFBFBD>g
|
||
cmd.cmd = Cmd_ClearUnit;
|
||
cmd.args.clearunit.module = HSM_MODULE_ID;
|
||
ret_code = NFastApp_Transact( connection, NULL, &cmd, &reply, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
NFastApp_Free_Reply( handle, NULL, NULL, &reply );
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );;
|
||
|
||
// <20><><EFBFBD>W<EFBFBD><57><EFBFBD>[<5B><><EFBFBD>̏<EFBFBD><CC8F>Ԃ<EFBFBD><D482>ēx<C493>m<EFBFBD>F
|
||
cmd.cmd = Cmd_NewEnquiry;
|
||
cmd.args.newenquiry.module = HSM_MODULE_ID;
|
||
cmd.args.newenquiry.version = EnqVer_Six;
|
||
ret_code = NFastApp_Transact( connection, NULL, &cmd, &reply, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// <20>܂<EFBFBD> failed state <20>Ȃ<EFBFBD><C882>A<CE81>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>邵<EFBFBD><E982B5><EFBFBD>Ȃ<EFBFBD>
|
||
if ( reply.reply.newenquiry.flags & Cmd_NewEnquiry_Reply_flags_Failed )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return Status_HardwareFailed;
|
||
}
|
||
|
||
NFastApp_Free_Reply( handle, NULL, NULL, &reply );
|
||
|
||
// <20>ؒf
|
||
ret_code = NFastApp_Disconnect( connection, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
// <20>n<EFBFBD><6E><EFBFBD>h<EFBFBD><68><EFBFBD>j<EFBFBD><6A>
|
||
NFastApp_Finish( handle, NULL );
|
||
|
||
return ret_code;
|
||
} // hsm_reset_module
|
||
|
||
int hsm_generate_random( unsigned char *buf, int bytes )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
M_Command cmd;
|
||
M_Reply reply;
|
||
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
// <20>R<EFBFBD>}<7D><><EFBFBD>h<EFBFBD>ɒl<C992><6C><EFBFBD>Z<EFBFBD>b<EFBFBD>g<EFBFBD><67><EFBFBD><EFBFBD>
|
||
cmd.cmd = Cmd_GenerateRandom;
|
||
cmd.args.generaterandom.lenbytes = bytes;
|
||
|
||
// <20><><EFBFBD>ߔ<EFBFBD><DF94>s
|
||
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
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 ret_code = CR_GENID_SUCCESS;
|
||
|
||
M_Command cmd;
|
||
M_Reply reply;
|
||
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
// <20>R<EFBFBD>}<7D><><EFBFBD>h<EFBFBD>ɒl<C992><6C><EFBFBD>Z<EFBFBD>b<EFBFBD>g<EFBFBD><67><EFBFBD><EFBFBD>
|
||
cmd.cmd = Cmd_GetRTC;
|
||
cmd.args.getrtc.module = HSM_MODULE_ID;
|
||
|
||
// <20><><EFBFBD>ߔ<EFBFBD><DF94>s
|
||
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
*time = (int)reply.reply.getrtc.time.currenttimelow;
|
||
|
||
NFastApp_Free_Command( hsmHandle, NULL, NULL, &cmd );
|
||
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
|
||
|
||
return CR_GENID_SUCCESS;
|
||
} // hsm_get_rtc
|
||
|
||
#ifdef ENCRYPT_AES
|
||
|
||
int hsm_aes_load_key( NFKM_KeyIdent keyident, M_KeyID *keyid )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
NFKM_Key *keyinfo = NULL;
|
||
NFKM_ModuleInfo *moduleinfo = NULL;
|
||
M_ByteBlock *blobPtr = NULL;
|
||
|
||
// find key
|
||
ret_code = NFKM_findkey( hsmHandle, keyident, &keyinfo, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
// get usable Module
|
||
moduleinfo = hsmWorld->modules[0];
|
||
ret_code = NFKM_getusablemodule( hsmWorld, HSM_MODULE_ID, &moduleinfo );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
// load key blob
|
||
blobPtr = &keyinfo->privblob;
|
||
ret_code = NFKM_cmd_loadblob( hsmHandle, hsmConnection,
|
||
moduleinfo->module, blobPtr,
|
||
0, keyid, "loading aes key blob", NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
end:
|
||
if ( keyinfo != NULL )
|
||
NFKM_freekey( hsmHandle, keyinfo, NULL );
|
||
|
||
return ret_code;
|
||
} // hsm_aes_load_key
|
||
|
||
int hsm_aes_encrypt( unsigned char *dst_buf, unsigned char *org_buf, int size, u8 bonding_option, u8 *pIV )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
|
||
M_KeyID keyid;
|
||
M_Command cmd;
|
||
M_Reply reply;
|
||
M_IV enc_iv;
|
||
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
// key set
|
||
keyid = bonding_option ? hsmAesKeyidDev : hsmAesKeyidProd;
|
||
|
||
// iv set
|
||
enc_iv.mech = Mech_RijndaelmCBCpNONE;
|
||
memcpy( enc_iv.iv.generic128.iv.bytes, pIV, sizeof( enc_iv.iv.generic128.iv.bytes ) );
|
||
|
||
// encrypt command set
|
||
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.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 )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
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, u8 bonding_option, u8 *pIV )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
|
||
M_KeyID keyid;
|
||
M_Command cmd;
|
||
M_Reply reply;
|
||
M_IV dec_iv;
|
||
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
// key set
|
||
keyid = bonding_option ? hsmAesKeyidDev : hsmAesKeyidProd;
|
||
|
||
// iv set
|
||
dec_iv.mech = Mech_RijndaelmCBCpNONE;
|
||
memcpy( dec_iv.iv.generic128.iv.bytes, pIV, sizeof( dec_iv.iv.generic128.iv.bytes ) );
|
||
|
||
// decyrpt
|
||
cmd.cmd = Cmd_Decrypt;
|
||
cmd.args.decrypt.flags = 0;
|
||
cmd.args.decrypt.key = keyid;
|
||
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 )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
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
|
||
|
||
#else // !ENCRYPT_AES
|
||
|
||
|
||
int hsm_rsa_load_keypair( NFKM_KeyIdent keyident, M_KeyID *privKeyid, M_KeyID *pubKeyid )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
NFKM_Key *keyinfo = NULL;
|
||
NFKM_ModuleInfo *moduleinfo = NULL;
|
||
M_ByteBlock *blobPtr = NULL;
|
||
|
||
// find key
|
||
ret_code = NFKM_findkey( hsmHandle, keyident, &keyinfo, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
// get usable Module
|
||
moduleinfo = hsmWorld->modules[0];
|
||
ret_code = NFKM_getusablemodule( hsmWorld, HSM_MODULE_ID, &moduleinfo );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
// load private key blob
|
||
blobPtr = &keyinfo->privblob;
|
||
ret_code = NFKM_cmd_loadblob( hsmHandle, hsmConnection,
|
||
moduleinfo->module, blobPtr,
|
||
0, privKeyid,
|
||
"loading priv key blob", NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
// load public key blob
|
||
blobPtr = &keyinfo->pubblob;
|
||
ret_code = NFKM_cmd_loadblob( hsmHandle, hsmConnection,
|
||
moduleinfo->module, blobPtr,
|
||
0, pubKeyid,
|
||
"loading pub key blob", NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
end:
|
||
NFKM_freekey( hsmHandle, keyinfo, NULL );
|
||
|
||
return ret_code;
|
||
} // hsm_rsa_load_keypair
|
||
|
||
int hsm_rsa_encrypt( unsigned char *dst_buf, unsigned char *org_buf, int size, u8 bonding_option )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
|
||
M_KeyID keyid;
|
||
M_Command cmd;
|
||
M_Reply reply;
|
||
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
// key set
|
||
keyid = bonding_option ? hsmRsaPubkeyidDev : hsmRsaPubkeyidProd;
|
||
|
||
// encrypt command set
|
||
cmd.cmd = Cmd_Encrypt;
|
||
cmd.args.encrypt.flags = 0;
|
||
cmd.args.encrypt.key = keyid;
|
||
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 )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
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, u8 bonding_option )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
|
||
M_KeyID keyid;
|
||
M_Command cmd;
|
||
M_Reply reply;
|
||
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
// key set
|
||
keyid = bonding_option ? hsmRsaPrivkeyidDev : hsmRsaPrivkeyidProd;
|
||
|
||
// decyrpt command set
|
||
cmd.cmd = Cmd_Decrypt;
|
||
cmd.args.decrypt.flags = 0;
|
||
cmd.args.decrypt.key = keyid;
|
||
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 )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
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 // !ENCRYPT_AES
|
||
|
||
int hsm_ecdsa_load_keypair( NFKM_KeyIdent privKeyident, M_KeyID *privKeyid,
|
||
NFKM_KeyIdent pubKeyident, M_KeyID *pubKeyid )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
NFKM_Key *keyinfo = NULL;
|
||
NFKM_ModuleInfo *moduleinfo = NULL;
|
||
M_ByteBlock *blobptr = NULL;
|
||
|
||
// get usable Module
|
||
moduleinfo = hsmWorld->modules[0];
|
||
ret_code = NFKM_getusablemodule( hsmWorld, HSM_MODULE_ID, &moduleinfo );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
// priv
|
||
if ( &privKeyident != NULL )
|
||
{
|
||
// find key
|
||
ret_code = NFKM_findkey( hsmHandle, privKeyident, &keyinfo, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
// load key blob
|
||
blobptr = &(keyinfo->privblob);
|
||
ret_code = NFKM_cmd_loadblob( hsmHandle, hsmConnection,
|
||
moduleinfo->module, blobptr,
|
||
0, privKeyid,
|
||
"loading priv key blob", NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
NFKM_freekey( hsmHandle, keyinfo, NULL );
|
||
keyinfo = NULL;
|
||
}
|
||
else
|
||
{
|
||
*privKeyid = 0;
|
||
} // priv
|
||
|
||
// pub
|
||
if ( &pubKeyident != NULL )
|
||
{
|
||
// find key
|
||
ret_code = NFKM_findkey( hsmHandle, pubKeyident, &keyinfo, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
|
||
// load key blob
|
||
blobptr = &(keyinfo->pubblob);
|
||
ret_code = NFKM_cmd_loadblob( hsmHandle, hsmConnection,
|
||
moduleinfo->module, blobptr,
|
||
0, pubKeyid,
|
||
"loading pub key blob", NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
goto end;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
*pubKeyid = 0;
|
||
} // pub
|
||
|
||
end:
|
||
NFKM_freekey( hsmHandle, keyinfo, NULL );
|
||
|
||
return ret_code;
|
||
} // hsm_ecdsa_load_keypair
|
||
|
||
int hsm_ecdsa_sign( unsigned char *sign_buf, unsigned char *data_buf, unsigned char bonding_option )
|
||
{
|
||
int ret_code = CR_GENID_SUCCESS;
|
||
|
||
M_KeyID privKeyid, pubKeyid;
|
||
M_Command cmd;
|
||
M_Reply reply;
|
||
unsigned char *rPtr, *sPtr;
|
||
int rLen, sLen;
|
||
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
// key set
|
||
privKeyid = bonding_option ? hsmEcdsaPrivkeyidDev : hsmEcdsaPrivkeyidProd;
|
||
pubKeyid = bonding_option ? hsmEcdsaPubkeyidDev : hsmEcdsaPubkeyidProd;
|
||
|
||
// sign command set
|
||
cmd.cmd = Cmd_Sign;
|
||
cmd.args.sign.flags = 0; // Cmd_Sign_Args_flags_given_iv_present;
|
||
cmd.args.sign.key = privKeyid;
|
||
cmd.args.sign.mech = HSM_SIGN_MECH;
|
||
#if 1
|
||
cmd.args.sign.plain.type = PlainTextType_Hash32;
|
||
cmd.args.sign.plain.data.hash32.data = *(M_Hash32*)data_buf;
|
||
#else
|
||
cmd.args.sign.plain.type = PlainTextType_Hash;
|
||
cmd.args.sign.plain.data.hash.data = *(M_Hash*)data_buf;
|
||
#endif
|
||
|
||
// sign command issue
|
||
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
#if 1
|
||
// signature bignum -> bin
|
||
rLen = reply.reply.sign.sig.data.ecdsa.r->nbytes;
|
||
rPtr = (unsigned char*)cr_mem_malloc( rLen );
|
||
my_bignum2bin ( rPtr, rLen, hsmHandle, reply.reply.sign.sig.data.ecdsa.r );
|
||
sLen = reply.reply.sign.sig.data.ecdsa.s->nbytes;
|
||
sPtr = (unsigned char*)cr_mem_malloc( sLen );
|
||
my_bignum2bin ( sPtr, sLen, hsmHandle, reply.reply.sign.sig.data.ecdsa.s );
|
||
#endif
|
||
|
||
#if 0
|
||
DEBUG_PRINT_ARRAY( (char*)"sig r(HSM)", (const char *)rPtr, rLen );
|
||
DEBUG_PRINT_ARRAY( (char*)"sig s(HSM)", (const char *)sPtr, sLen );
|
||
#endif
|
||
|
||
|
||
// verify
|
||
#if 1
|
||
struct NFast_Bignum *rBn, *sBn;
|
||
my_bignumCopy( &rBn, reply.reply.sign.sig.data.ecdsa.r, hsmHandle );
|
||
my_bignumCopy( &sBn, reply.reply.sign.sig.data.ecdsa.s, hsmHandle );
|
||
|
||
//NFastApp_Free_Command( handle, NULL, NULL, &cmd );
|
||
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
|
||
memset( &cmd, 0, sizeof( cmd ) );
|
||
memset( &reply, 0, sizeof( reply ) );
|
||
|
||
cmd.cmd = Cmd_Verify;
|
||
cmd.args.verify.flags = 0;
|
||
cmd.args.verify.key = pubKeyid;
|
||
cmd.args.verify.mech = HSM_SIGN_MECH;
|
||
#if 1
|
||
cmd.args.verify.plain.type = PlainTextType_Hash32;
|
||
cmd.args.verify.plain.data.hash32.data = *(M_Hash32*)data_buf;
|
||
#else
|
||
cmd.args.verify.plain.type = PlainTextType_Hash;
|
||
cmd.args.verify.plain.data.hash.data = *(M_Hash*)data_buf;
|
||
#endif
|
||
|
||
cmd.args.verify.sig.mech = HSM_SIGN_MECH;
|
||
cmd.args.verify.sig.data.ecdsa.r = rBn;
|
||
cmd.args.verify.sig.data.ecdsa.s = sBn;
|
||
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
ret_code = reply.status;
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||
return ret_code;
|
||
}
|
||
|
||
NFastApp_Free( hsmHandle, rBn, NULL, NULL );
|
||
NFastApp_Free( hsmHandle, sBn, NULL, NULL );
|
||
#endif // verify
|
||
|
||
// copy sign
|
||
int i;
|
||
memset( sign_buf, 0, 60 );
|
||
if ( rLen == 32 )
|
||
memcpy( sign_buf, &rPtr[2], 0x1E );
|
||
else
|
||
{
|
||
for ( i = 0; i < rLen; i++ )
|
||
sign_buf[ 30 - i - 1 ] = rPtr[ rLen - i - 1 ];
|
||
}
|
||
|
||
if ( sLen == 32 )
|
||
memcpy( &sign_buf[30], &sPtr[2], 0x1E );
|
||
else
|
||
{
|
||
for ( i = 0; i < sLen; i++ )
|
||
sign_buf[ 60 - i - 1 ] = sPtr[ sLen - i - 1 ];
|
||
}
|
||
|
||
//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 );
|
||
cr_mem_free( rPtr );
|
||
cr_mem_free( sPtr );
|
||
|
||
return CR_GENID_SUCCESS;
|
||
} // hsm_ecdsa_sign
|
||
|
||
#endif // USE_HSM
|