diff --git a/hsm_utils/Makefile b/hsm_utils/Makefile index 4c5c72c..225a8f8 100644 --- a/hsm_utils/Makefile +++ b/hsm_utils/Makefile @@ -84,7 +84,7 @@ XLDLIBS= $(LIBPATH_SWORLD)/librqcard.a \ COMMON_OBJECTS = $(REFPATH)/nfutil.o $(REFPATH)/nfopt.o $(REFPATH)/getdate.o $(REFPATH)/report.o $(REFPATH)/report-usage.o $(REFPATH)/nftypes.o $(REFPATH)/tokenise.o -EXTRA_OBJECTS = mybignum.o +EXTRA_OBJECTS = my_hsm_bignum.o my_hsm_alloc.o COMMON_HEADERS= $(REFPATH)/nfutil.h $(REFPATH)/nfopt.h $(REFPATH)/nftypes.h $(REFPATH)/tokenise.h @@ -96,8 +96,11 @@ $(SRCPATH)/getdate.c: ; # Simple (non-threaded) programs ------------ -mybignum.o: mybignum.c - $(CC) $(CFLAGS) $(CPPFLAGS) -o mybignum.o -c mybignum.c +my_hsm_bignum.o: my_hsm_bignum.c + $(CC) $(CFLAGS) $(CPPFLAGS) -o my_hsm_bignum.o -c my_hsm_bignum.c + +my_hsm_alloc.o: my_hsm_alloc.c + $(CC) $(CFLAGS) $(CPPFLAGS) -o my_hsm_alloc.o -c my_hsm_alloc.c import_common_key: import_common_key.c $(EXTRA_OBJECTS) $(CC) $(CFLAGS) $(CPPFLAGS) -o import_common_key import_common_key.c $(COMMON_OBJECTS) $(EXTRA_OBJECTS) $(LDLIBS) diff --git a/hsm_utils/import_ecc_keypair.c b/hsm_utils/import_ecc_keypair.c index 37dc659..a3eaba5 100644 --- a/hsm_utils/import_ecc_keypair.c +++ b/hsm_utils/import_ecc_keypair.c @@ -22,11 +22,11 @@ #include "rqcard-applic.h" #include "rqcard-fips.h" -#include "ncthread-upcalls.h" +//#include "ncthread-upcalls.h" //#include "picky-upcalls.h" -#include "mybignum.h" - +#include "my_hsm_bignum.h" +#include "my_hsm_alloc.h" #define PRIV_KEY_FILE "./test_key/test-ecc-privkey.der" #define PUB_KEY_FILE "./test_key/test-ecc-pubkey.der" @@ -61,40 +61,10 @@ typedef struct } RSAPublicKeyData; -static void *my_malloc( size_t nbytes, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ); -static void *my_realloc( void *ptr, size_t nbytes, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ); -static void my_free( void *ptr, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ); - void PrintArray( char *pStr, const unsigned char *pData, int length ); int BN_bn2binWrapper( BIGNUM *bn, unsigned char **ptr, int *len ); int my_bin2bignumWrapper( NFast_AppHandle app, unsigned char *ptr, struct NFast_Bignum **nbn, int size ); -const NFast_MallocUpcalls my_malloc_upcalls = -{ - my_malloc, my_realloc, my_free -}; - -static void *my_malloc( size_t nbytes, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) -{ - return malloc( nbytes ); -} - -static void *my_realloc( void *ptr, size_t nbytes, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) -{ - return realloc( ptr, nbytes ); -} - -static void my_free( void *ptr, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) -{ - free( ptr ); -} - void PrintArray( char *pStr, const unsigned char *pData, int length ) { int i; diff --git a/hsm_utils/import_rsa_keypair.c b/hsm_utils/import_rsa_keypair.c index 3725460..d2c2159 100644 --- a/hsm_utils/import_rsa_keypair.c +++ b/hsm_utils/import_rsa_keypair.c @@ -20,11 +20,10 @@ #include "rqcard-applic.h" #include "rqcard-fips.h" -#include "ncthread-upcalls.h" //#include "picky-upcalls.h" -#include "mybignum.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" @@ -59,77 +58,8 @@ typedef struct } RSAPublicKeyData; -static void *my_malloc( size_t nbytes, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ); -static void *my_realloc( void *ptr, size_t nbytes, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ); -static void my_free( void *ptr, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ); - -int sbn_bin2bignum ( struct NFast_Bignum **ppBN_out, - struct NFast_Application *app, - const unsigned char *bin, const int size ); - void PrintArray( char *pStr, const unsigned char *pData, int length ); -const NFast_MallocUpcalls my_malloc_upcalls = -{ - my_malloc, my_realloc, my_free -}; - -static void *my_malloc( size_t nbytes, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) -{ - return malloc( nbytes ); -} - -static void *my_realloc( void *ptr, size_t nbytes, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) -{ - return realloc( ptr, nbytes ); -} - -static void my_free( void *ptr, - struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) -{ - free( ptr ); -} - -// bin データを NFastApp の BigNum データに変換する -int sbn_bin2bignum ( struct NFast_Bignum **ppBN_out, - struct NFast_Application *app, - const unsigned char *bin, const int size ) -{ - struct NFast_Bignum *pBN; - int len, i; - - len = size; - - if ( len > MAXBIGNUMBITS/4 ) return Status_OutOfRange; - - pBN = (struct NFast_Bignum *)NFastApp_Malloc( app, sizeof(struct NFast_Bignum), NULL, NULL ); - if ( !pBN ) return NOMEM; - - pBN->msb_first = 0; - pBN->msw_first = 0; - - for ( i = 0; i < len; i++ ) - pBN->bytes[i] = bin[len-1-i]; - - while ( (i & 3) != 0 ) - pBN->bytes[i++] = 0; - - pBN->nbytes = i; - - *ppBN_out = pBN; - -#if 0 - PrintArray( (char*)"bin2bn array", (const char*)pBN->bytes, pBN->nbytes ); -#endif - - return Status_OK; -} // sbn_bin2bignum - void PrintArray( char *pStr, const unsigned char *pData, int length ) { int i; diff --git a/hsm_utils/my_hsm_alloc.c b/hsm_utils/my_hsm_alloc.c new file mode 100644 index 0000000..23470d6 --- /dev/null +++ b/hsm_utils/my_hsm_alloc.c @@ -0,0 +1,39 @@ +/* +* my_hsm_alloc.c +*/ + +#include +#include +#include +#include +#include +#include + +#include "nfastapp.h" +#include "nfutil.h" +#include "my_hsm_alloc.h" + +/* --------------------- */ + +const NFast_MallocUpcalls my_malloc_upcalls = +{ + my_malloc, my_realloc, my_free +}; + +static void *my_malloc( size_t nbytes, + struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) +{ + return malloc( nbytes ); +} + +static void *my_realloc( void *ptr, size_t nbytes, + struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) +{ + return realloc( ptr, nbytes ); +} + +static void my_free( void *ptr, + struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) +{ + free( ptr ); +} \ No newline at end of file diff --git a/hsm_utils/my_hsm_alloc.h b/hsm_utils/my_hsm_alloc.h new file mode 100644 index 0000000..88bad9a --- /dev/null +++ b/hsm_utils/my_hsm_alloc.h @@ -0,0 +1,30 @@ +/* +* my_hsm_alloc.h +*/ + +#ifndef MY_HSM_ALLOC_H +#define MY_HSM_ALLOC_H + +#include "nfastapp.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern NFast_MallocUpcalls my_hsm_malloc_upcalls; + +void *my_hsm_malloc( size_t nbytes, + struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ); + +void *my_hsm_realloc( void *ptr, size_t nbytes, + struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ); + +void my_hsm_free( void *ptr, + struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ); + +#ifdef __cplusplus +} +#endif + +#endif // MY_HSM_ALLOC_H + diff --git a/hsm_utils/mybignum.c b/hsm_utils/my_hsm_bignum.c similarity index 100% rename from hsm_utils/mybignum.c rename to hsm_utils/my_hsm_bignum.c diff --git a/hsm_utils/mybignum.h b/hsm_utils/my_hsm_bignum.h similarity index 96% rename from hsm_utils/mybignum.h rename to hsm_utils/my_hsm_bignum.h index c5a9d78..6629b75 100644 --- a/hsm_utils/mybignum.h +++ b/hsm_utils/my_hsm_bignum.h @@ -22,8 +22,8 @@ * */ -#ifndef MY_BIGNUM_H -#define MY_BIGNUM_H +#ifndef MY_ALLOC_H +#define MY_ALLOC_H #include "nfastapp.h"