mirror of
https://github.com/rvtr/ctr_eFuse.git
synced 2025-11-02 00:11:04 -04:00
hsm_utils:mybignum追加(simblebignumまんま。デバッグ目的),
import_asymmetric_key.c -> import_rsa_keypair.c git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@101 ff987cc8-cf2f-4642-8568-d52cce064691
This commit is contained in:
parent
62daefb347
commit
dbb976ccb5
@ -82,7 +82,7 @@ XLDLIBS= $(LIBPATH_SWORLD)/librqcard.a \
|
|||||||
$(LIBPATH_NFLOG)/libnflog.a \
|
$(LIBPATH_NFLOG)/libnflog.a \
|
||||||
$(LIBPATH_CUTILS)/libcutils.a -lm
|
$(LIBPATH_CUTILS)/libcutils.a -lm
|
||||||
|
|
||||||
COMMON_OBJECTS = $(REFPATH)/simplecmd.o $(REFPATH)/simplebignum.o $(REFPATH)/nfutil.o $(REFPATH)/nfopt.o $(REFPATH)/getdate.o $(REFPATH)/report.o $(REFPATH)/report-usage.o $(REFPATH)/nftypes.o $(REFPATH)/tokenise.o
|
COMMON_OBJECTS = $(REFPATH)/simplecmd.o $(REFPATH)/simplebignum.o $(REFPATH)/nfutil.o $(REFPATH)/nfopt.o $(REFPATH)/getdate.o $(REFPATH)/report.o $(REFPATH)/report-usage.o $(REFPATH)/nftypes.o $(REFPATH)/tokenise.o mybignum.o
|
||||||
|
|
||||||
COMMON_HEADERS= $(REFPATH)/simplecmd.h $(REFPATH)/simplebignum.h $(REFPATH)/nfutil.h $(REFPATH)/nfopt.h $(REFPATH)/nftypes.h $(REFPATH)/tokenise.h
|
COMMON_HEADERS= $(REFPATH)/simplecmd.h $(REFPATH)/simplebignum.h $(REFPATH)/nfutil.h $(REFPATH)/nfopt.h $(REFPATH)/nftypes.h $(REFPATH)/tokenise.h
|
||||||
|
|
||||||
@ -94,11 +94,14 @@ $(SRCPATH)/getdate.c: ;
|
|||||||
|
|
||||||
# Simple (non-threaded) programs ------------
|
# Simple (non-threaded) programs ------------
|
||||||
|
|
||||||
|
mybignum.o: mybignum.c
|
||||||
|
$(CC) $(CFLAGS) $(CPPFLAGS) -o mybignum.o -c mybignum.c
|
||||||
|
|
||||||
import_common_key: import_common_key.c
|
import_common_key: import_common_key.c
|
||||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o import_common_key import_common_key.c $(COMMON_OBJECTS) $(LDLIBS)
|
$(CC) $(CFLAGS) $(CPPFLAGS) -o import_common_key import_common_key.c $(COMMON_OBJECTS) $(LDLIBS)
|
||||||
|
|
||||||
import_asymmetric_key: import_asymmetric_key.c
|
import_rsa_keypair: import_rsa_keypair.c
|
||||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o import_asymmetric_key import_asymmetric_key.c $(COMMON_OBJECTS) $(LDLIBS)
|
$(CC) $(CFLAGS) $(CPPFLAGS) -o import_rsa_keypair import_rsa_keypair.c $(COMMON_OBJECTS) $(LDLIBS)
|
||||||
|
|
||||||
# All single-threaded targets
|
# All single-threaded targets
|
||||||
|
|
||||||
|
|||||||
@ -23,8 +23,10 @@
|
|||||||
#include "simplebignum.h"
|
#include "simplebignum.h"
|
||||||
#include "ncthread-upcalls.h"
|
#include "ncthread-upcalls.h"
|
||||||
//#include "picky-upcalls.h"
|
//#include "picky-upcalls.h"
|
||||||
|
//#include "simplecmd.h"
|
||||||
|
|
||||||
|
#include "mybignum.h"
|
||||||
|
|
||||||
#include "simplecmd.h"
|
|
||||||
|
|
||||||
#define PRIV_KEY_FILE "/opt/nfast/work/rsa-priv-key2048.der"
|
#define PRIV_KEY_FILE "/opt/nfast/work/rsa-priv-key2048.der"
|
||||||
#define PUB_KEY_FILE "/opt/nfast/work/rsa-pub-key2048.der"
|
#define PUB_KEY_FILE "/opt/nfast/work/rsa-pub-key2048.der"
|
||||||
@ -72,30 +74,6 @@ int sbn_bin2bignum ( struct NFast_Bignum **ppBN_out,
|
|||||||
|
|
||||||
void PrintArray( char *pStr, const unsigned char *pData, int length );
|
void PrintArray( char *pStr, const unsigned char *pData, int length );
|
||||||
|
|
||||||
// bignum upcalls
|
|
||||||
int my_bignumreceiveupcall(struct NFast_Application *app,
|
|
||||||
struct NFast_Call_Context *cctx,
|
|
||||||
struct NFast_Transaction_Context *tctx,
|
|
||||||
M_Bignum *bignum, int nbytes,
|
|
||||||
const void *source,
|
|
||||||
int msbitfirst, int mswordfirst);
|
|
||||||
int my_bignumsendlenupcall( struct NFast_Application *app,
|
|
||||||
struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx,
|
|
||||||
const M_Bignum *bignum, int *nbytes_r );
|
|
||||||
int my_bignumsendupcall(struct NFast_Application *app,
|
|
||||||
struct NFast_Call_Context *cctx,
|
|
||||||
struct NFast_Transaction_Context *tctx,
|
|
||||||
const M_Bignum *bignum, int nbytes,
|
|
||||||
void *dest, int msbitfirst, int mswordfirst);
|
|
||||||
void my_bignumfreeupcall(struct NFast_Application *app,
|
|
||||||
struct NFast_Call_Context *cctx,
|
|
||||||
struct NFast_Transaction_Context *tctx,
|
|
||||||
M_Bignum *bignum);
|
|
||||||
int my_bignumformatupcall(struct NFast_Application *app,
|
|
||||||
struct NFast_Call_Context *cctx,
|
|
||||||
struct NFast_Transaction_Context *tctx,
|
|
||||||
int *msbitfirst_io, int *mswordfirst_io);
|
|
||||||
|
|
||||||
const NFast_MallocUpcalls my_malloc_upcalls =
|
const NFast_MallocUpcalls my_malloc_upcalls =
|
||||||
{
|
{
|
||||||
my_malloc, my_realloc, my_free
|
my_malloc, my_realloc, my_free
|
||||||
@ -119,97 +97,6 @@ static void my_free( void *ptr,
|
|||||||
free( ptr );
|
free( ptr );
|
||||||
}
|
}
|
||||||
|
|
||||||
int my_bignumreceiveupcall(struct NFast_Application *app,
|
|
||||||
struct NFast_Call_Context *cctx,
|
|
||||||
struct NFast_Transaction_Context *tctx,
|
|
||||||
M_Bignum *bignum, int nbytes,
|
|
||||||
const void *source,
|
|
||||||
int msbitfirst, int mswordfirst)
|
|
||||||
{
|
|
||||||
struct NFast_Bignum *pBN;
|
|
||||||
|
|
||||||
printf( "my_bignumreceiveupcall\n" );
|
|
||||||
|
|
||||||
if ( nbytes > MAXBIGNUMBITS/8 ) return Status_OutOfRange;
|
|
||||||
assert( (nbytes & 3)==0 );
|
|
||||||
|
|
||||||
pBN = (struct NFast_Bignum *)NFastApp_Malloc(app, sizeof(struct NFast_Bignum), cctx, tctx);
|
|
||||||
if ( !pBN ) return NOMEM;
|
|
||||||
|
|
||||||
nfutil_copybytes(pBN->bytes, (const unsigned char *)source,
|
|
||||||
nbytes, 0, 0);
|
|
||||||
|
|
||||||
pBN->msb_first = msbitfirst;
|
|
||||||
pBN->msw_first = mswordfirst;
|
|
||||||
pBN->nbytes=nbytes;
|
|
||||||
*bignum=pBN;
|
|
||||||
return Status_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
int my_bignumsendlenupcall( struct NFast_Application *app,
|
|
||||||
struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx,
|
|
||||||
const M_Bignum *bignum, int *nbytes_r )
|
|
||||||
{
|
|
||||||
printf( "my_bignumsendlenupcall\n" );
|
|
||||||
|
|
||||||
assert( ((*bignum)->nbytes & 3)==0 );
|
|
||||||
*nbytes_r= (*bignum)->nbytes;
|
|
||||||
|
|
||||||
return Status_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
int my_bignumsendupcall(struct NFast_Application *app,
|
|
||||||
struct NFast_Call_Context *cctx,
|
|
||||||
struct NFast_Transaction_Context *tctx,
|
|
||||||
const M_Bignum *bignum, int nbytes,
|
|
||||||
void *dest, int msbitfirst, int mswordfirst)
|
|
||||||
{
|
|
||||||
int swapends, swapwords;
|
|
||||||
struct NFast_Bignum *pBN = *bignum;
|
|
||||||
|
|
||||||
printf( "my_bignumsendupcall\n" );
|
|
||||||
|
|
||||||
assert( pBN->nbytes==nbytes );
|
|
||||||
|
|
||||||
/* Is format which we're sending in the same as that of the
|
|
||||||
bignumber?
|
|
||||||
(NB '!' used to constrain result to 0,1 range)
|
|
||||||
If not, work out which ends to swap.
|
|
||||||
*/
|
|
||||||
|
|
||||||
swapends = (!msbitfirst) ^ (!pBN->msb_first);
|
|
||||||
swapwords = (!mswordfirst) ^ (!pBN->msw_first);
|
|
||||||
nfutil_copybytes( (unsigned char *)dest, (*bignum)->bytes, nbytes,
|
|
||||||
swapends, swapwords );
|
|
||||||
return Status_OK;
|
|
||||||
}
|
|
||||||
void my_bignumfreeupcall(struct NFast_Application *app,
|
|
||||||
struct NFast_Call_Context *cctx,
|
|
||||||
struct NFast_Transaction_Context *tctx,
|
|
||||||
M_Bignum *bignum)
|
|
||||||
{
|
|
||||||
printf( "my_bignumfreeupcall\n" );
|
|
||||||
|
|
||||||
NFastApp_Free(app, (*bignum), cctx, tctx);
|
|
||||||
*bignum=NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int my_bignumformatupcall(struct NFast_Application *app,
|
|
||||||
struct NFast_Call_Context *cctx,
|
|
||||||
struct NFast_Transaction_Context *tctx,
|
|
||||||
int *msbitfirst_io, int *mswordfirst_io)
|
|
||||||
{
|
|
||||||
printf( "my_bignumformatupcall\n" );
|
|
||||||
|
|
||||||
/* Send to the module in little-endian format.
|
|
||||||
(This is not officially necessary. However, some
|
|
||||||
versions of the monitor (Maintenance mode) don't accept
|
|
||||||
big-endian bignums due to a bug) */
|
|
||||||
*msbitfirst_io=0;
|
|
||||||
*mswordfirst_io=0;
|
|
||||||
return Status_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
// bin データを NFastApp の BigNum データに変換する
|
// bin データを NFastApp の BigNum データに変換する
|
||||||
int sbn_bin2bignum ( struct NFast_Bignum **ppBN_out,
|
int sbn_bin2bignum ( struct NFast_Bignum **ppBN_out,
|
||||||
struct NFast_Application *app,
|
struct NFast_Application *app,
|
||||||
@ -390,11 +277,11 @@ int main( int argc, char *argv[] )
|
|||||||
// set bignum upcalls setting
|
// set bignum upcalls setting
|
||||||
result = NFastApp_SetBignumUpcalls(
|
result = NFastApp_SetBignumUpcalls(
|
||||||
handle,
|
handle,
|
||||||
sbn_bignumreceiveupcall,
|
my_bignumreceiveupcall,
|
||||||
sbn_bignumsendlenupcall,
|
my_bignumsendlenupcall,
|
||||||
sbn_bignumsendupcall,
|
my_bignumsendupcall,
|
||||||
sbn_bignumfreeupcall,
|
my_bignumfreeupcall,
|
||||||
sbn_bignumformatupcall,
|
my_bignumformatupcall,
|
||||||
NULL );
|
NULL );
|
||||||
if ( result != Status_OK )
|
if ( result != Status_OK )
|
||||||
{
|
{
|
||||||
283
hsm_utils/mybignum.c
Normal file
283
hsm_utils/mybignum.c
Normal file
@ -0,0 +1,283 @@
|
|||||||
|
/*
|
||||||
|
* SIMPLEBIGNUM.C
|
||||||
|
*
|
||||||
|
* Simple bignumber upcalls
|
||||||
|
*
|
||||||
|
* This example source code is provided for your information and
|
||||||
|
* assistance. See the file LICENCE.TXT for details and the
|
||||||
|
* terms and conditions of the licence which governs the use of the
|
||||||
|
* source code. By using such source code you will be accepting these
|
||||||
|
* terms and conditions. If you do not wish to accept these terms and
|
||||||
|
* conditions, DO NOT OPEN THE FILE OR USE THE SOURCE CODE.
|
||||||
|
*
|
||||||
|
* Note that there is NO WARRANTY.
|
||||||
|
*
|
||||||
|
* Copyright 2001 - 2002 nCipher Corporation Limited.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
#include "nfastapp.h"
|
||||||
|
#include "nfutil.h"
|
||||||
|
#include "mybignum.h"
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
int my_bignumreceiveupcall(struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx,
|
||||||
|
M_Bignum *bignum, int nbytes,
|
||||||
|
const void *source,
|
||||||
|
int msbitfirst, int mswordfirst)
|
||||||
|
{
|
||||||
|
struct NFast_Bignum *pBN;
|
||||||
|
|
||||||
|
if ( nbytes > MAXBIGNUMBITS/8 ) return Status_OutOfRange;
|
||||||
|
assert( (nbytes & 3)==0 );
|
||||||
|
|
||||||
|
pBN = (struct NFast_Bignum *)NFastApp_Malloc(app, sizeof(struct NFast_Bignum), cctx, tctx);
|
||||||
|
if ( !pBN ) return NOMEM;
|
||||||
|
|
||||||
|
nfutil_copybytes(pBN->bytes, (const unsigned char *)source,
|
||||||
|
nbytes, 0, 0);
|
||||||
|
|
||||||
|
pBN->msb_first = msbitfirst;
|
||||||
|
pBN->msw_first = mswordfirst;
|
||||||
|
pBN->nbytes=nbytes;
|
||||||
|
*bignum=pBN;
|
||||||
|
return Status_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
int my_bignumsendlenupcall(struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx,
|
||||||
|
const M_Bignum *bignum, int *nbytes_r)
|
||||||
|
{
|
||||||
|
assert( ((*bignum)->nbytes & 3)==0 );
|
||||||
|
*nbytes_r= (*bignum)->nbytes;
|
||||||
|
return Status_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
int my_bignumsendupcall(struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx,
|
||||||
|
const M_Bignum *bignum, int nbytes,
|
||||||
|
void *dest, int msbitfirst, int mswordfirst)
|
||||||
|
{
|
||||||
|
int swapends, swapwords;
|
||||||
|
struct NFast_Bignum *pBN = *bignum;
|
||||||
|
|
||||||
|
assert( pBN->nbytes==nbytes );
|
||||||
|
|
||||||
|
/* Is format which we're sending in the same as that of the
|
||||||
|
bignumber?
|
||||||
|
(NB '!' used to constrain result to 0,1 range)
|
||||||
|
If not, work out which ends to swap.
|
||||||
|
*/
|
||||||
|
|
||||||
|
swapends = (!msbitfirst) ^ (!pBN->msb_first);
|
||||||
|
swapwords = (!mswordfirst) ^ (!pBN->msw_first);
|
||||||
|
nfutil_copybytes( (unsigned char *)dest, (*bignum)->bytes, nbytes,
|
||||||
|
swapends, swapwords );
|
||||||
|
return Status_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
void my_bignumfreeupcall(struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx,
|
||||||
|
M_Bignum *bignum)
|
||||||
|
{
|
||||||
|
NFastApp_Free(app, (*bignum), cctx, tctx);
|
||||||
|
*bignum=NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
int my_bignumformatupcall(struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx,
|
||||||
|
int *msbitfirst_io, int *mswordfirst_io)
|
||||||
|
{
|
||||||
|
/* Send to the module in little-endian format.
|
||||||
|
(This is not officially necessary. However, some
|
||||||
|
versions of the monitor (Maintenance mode) don't accept
|
||||||
|
big-endian bignums due to a bug) */
|
||||||
|
*msbitfirst_io=0;
|
||||||
|
*mswordfirst_io=0;
|
||||||
|
return Status_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
NFast_BignumUpcalls sbn_upcalls = {
|
||||||
|
sbn_bignumreceiveupcall,
|
||||||
|
sbn_bignumsendlenupcall,
|
||||||
|
sbn_bignumsendupcall,
|
||||||
|
sbn_bignumfreeupcall,
|
||||||
|
sbn_bignumformatupcall
|
||||||
|
};
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
static int char2hex ( char c )
|
||||||
|
{
|
||||||
|
if ( c >= '0' && c <= '9' ) return c-'0';
|
||||||
|
if ( c >= 'A' && c <= 'F' ) return c-'A'+10;
|
||||||
|
if ( c >= 'a' && c <= 'f' ) return c-'a'+10;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
int my_char2bignum ( struct NFast_Bignum **ppBN_out,
|
||||||
|
const char *text,
|
||||||
|
struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx )
|
||||||
|
{
|
||||||
|
struct NFast_Bignum *pBN;
|
||||||
|
int d;
|
||||||
|
size_t len, i;
|
||||||
|
|
||||||
|
/* Strip leading whitespace */
|
||||||
|
|
||||||
|
while ( text[0] != 0 && isspace((unsigned char)text[0]) )
|
||||||
|
text++;
|
||||||
|
|
||||||
|
/* Strip trailing whitespace */
|
||||||
|
len=strlen(text);
|
||||||
|
while ( len > 0 && isspace((unsigned char)text[len-1]) )
|
||||||
|
len--;
|
||||||
|
|
||||||
|
if ( len > MAXBIGNUMBITS/4 ) return Status_OutOfRange;
|
||||||
|
|
||||||
|
pBN = (struct NFast_Bignum *)NFastApp_Malloc(app, sizeof(struct NFast_Bignum), cctx, tctx);
|
||||||
|
if ( !pBN ) return NOMEM;
|
||||||
|
|
||||||
|
pBN->msb_first = 0;
|
||||||
|
pBN->msw_first = 0;
|
||||||
|
|
||||||
|
/* Read in from the LS digit */
|
||||||
|
for ( i=0; i<len; i++ )
|
||||||
|
{
|
||||||
|
d = char2hex(text[len-1-i]);
|
||||||
|
if ( d < 0 ) return Status_Malformed;
|
||||||
|
if ( i & 1 )
|
||||||
|
pBN->bytes[i/2] |= (d << 4);
|
||||||
|
else
|
||||||
|
pBN->bytes[i/2] = d;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Pad to words if necessary */
|
||||||
|
i = (len+1)/2;
|
||||||
|
while ( (i & 3) != 0 )
|
||||||
|
pBN->bytes[i++] = 0;
|
||||||
|
|
||||||
|
assert(i <= INT_MAX);
|
||||||
|
pBN->nbytes=(int)i;
|
||||||
|
*ppBN_out=pBN;
|
||||||
|
return Status_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
static int getbyte ( const struct NFast_Bignum *pN, int pos )
|
||||||
|
{
|
||||||
|
/* Get a byte from a bignum, taking account of possible strange endianness */
|
||||||
|
if ( pos >= pN->nbytes ) return 0;
|
||||||
|
|
||||||
|
if ( pN->msb_first ) pos ^= 3; /* Big endian words */
|
||||||
|
|
||||||
|
if ( pN->msw_first )
|
||||||
|
{
|
||||||
|
pos = pN->nbytes-1-pos;
|
||||||
|
pos ^= 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pN->bytes[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
static int getbytelen ( const struct NFast_Bignum *pN )
|
||||||
|
{
|
||||||
|
int n=pN->nbytes-1;
|
||||||
|
while ( n >= 0 && getbyte(pN, n)==0 )
|
||||||
|
n--;
|
||||||
|
|
||||||
|
return n+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
int my_bignum2char ( char *buf, int buflen,
|
||||||
|
const struct NFast_Bignum *pBN,
|
||||||
|
struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx )
|
||||||
|
{
|
||||||
|
int i, d, pos, len;
|
||||||
|
static const char *hexdigits="0123456789ABCDEF";
|
||||||
|
|
||||||
|
len = pBN->nbytes;
|
||||||
|
|
||||||
|
pos = len*2+1;
|
||||||
|
if ( buflen < pos )
|
||||||
|
return Status_BufferFull;
|
||||||
|
|
||||||
|
buf[--pos] = 0;
|
||||||
|
|
||||||
|
for ( i=0; i<len; i++ )
|
||||||
|
{
|
||||||
|
d = getbyte(pBN,i);
|
||||||
|
buf[--pos] = hexdigits[d & 0xF];
|
||||||
|
buf[--pos] = hexdigits[(d>>4) & 0xF];
|
||||||
|
}
|
||||||
|
|
||||||
|
return Status_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
void my_printbignum ( FILE *f, const char *prefix, const struct NFast_Bignum *pBN )
|
||||||
|
{
|
||||||
|
char buf[MAXBIGNUMBITS/4+1];
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
rc=sbn_bignum2char(buf, sizeof(buf), pBN, NULL, NULL, NULL);
|
||||||
|
if ( rc != Status_OK ) strcpy(buf, "<invalid length>");
|
||||||
|
fprintf( f, "%s=\n %s\n", prefix, buf );
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------- */
|
||||||
|
|
||||||
|
int my_compare ( const struct NFast_Bignum *pA,
|
||||||
|
const struct NFast_Bignum *pB )
|
||||||
|
{
|
||||||
|
int i, aa, bb;
|
||||||
|
|
||||||
|
aa=getbytelen(pA);
|
||||||
|
bb=getbytelen(pB);
|
||||||
|
if ( aa != bb ) return (aa > bb) ? 1 : -1;
|
||||||
|
|
||||||
|
i=aa;
|
||||||
|
while ( i-- > 0 )
|
||||||
|
{
|
||||||
|
aa=getbyte(pA,i);
|
||||||
|
bb=getbyte(pB,i);
|
||||||
|
if ( aa != bb ) return (aa > bb) ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
163
hsm_utils/mybignum.h
Normal file
163
hsm_utils/mybignum.h
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
/** \file simplebignum.h Simple bignum support
|
||||||
|
*
|
||||||
|
* Illustrates simple easy-to-use bignumber format. This provides a
|
||||||
|
* definition of the \ref NFast_Bignum structure which can be used
|
||||||
|
* in applications which do not already have an equivalent structure
|
||||||
|
* defined.
|
||||||
|
*
|
||||||
|
* See also:
|
||||||
|
* - \ref nfastapp.h
|
||||||
|
* - \ref gsbignum
|
||||||
|
*/
|
||||||
|
/* Copyright 1999-2002 nCipher Corporation Limited.
|
||||||
|
*
|
||||||
|
* This example source code is provided for your information and
|
||||||
|
* assistance. See the file LICENCE.TXT for details and the
|
||||||
|
* terms and conditions of the licence which governs the use of the
|
||||||
|
* source code. By using such source code you will be accepting these
|
||||||
|
* terms and conditions. If you do not wish to accept these terms and
|
||||||
|
* conditions, DO NOT OPEN THE FILE OR USE THE SOURCE CODE.
|
||||||
|
*
|
||||||
|
* Note that there is NO WARRANTY.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MY_BIGNUM_H
|
||||||
|
#define MY_BIGNUM_H
|
||||||
|
|
||||||
|
#include "nfastapp.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MAXBIGNUMBITS
|
||||||
|
/** Maximum size of a bignum in bits */
|
||||||
|
#define MAXBIGNUMBITS 16384
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** Structure of a bignum
|
||||||
|
*
|
||||||
|
* \ref M_Bignum will be a pointer to this structure. */
|
||||||
|
struct NFast_Bignum {
|
||||||
|
/** Byte order
|
||||||
|
*
|
||||||
|
* If this is set then each 32-bit word in the bignum is big-endian
|
||||||
|
* (most-significant byte first); otherwise it is little-endian
|
||||||
|
* (least-significant byte first). */
|
||||||
|
int msb_first;
|
||||||
|
/** Word order
|
||||||
|
*
|
||||||
|
* If this is set then 32-bit words in the bignum are in big-endian order
|
||||||
|
* (most-significant word first); otherwise they are in little-endian
|
||||||
|
* order (least-significant words first).
|
||||||
|
*/
|
||||||
|
int msw_first;
|
||||||
|
/** Number of bytes */
|
||||||
|
int nbytes;
|
||||||
|
/** Bignum data
|
||||||
|
*
|
||||||
|
* Only the first \a nbytes are used. */
|
||||||
|
unsigned char bytes[MAXBIGNUMBITS/8];
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Bignum send & receive upcalls -------------------------- */
|
||||||
|
|
||||||
|
/* As well as being used directly as upcalls,
|
||||||
|
these can be used to create bignums from data blocks and
|
||||||
|
extract data from bignums.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** Bignum receive upcall
|
||||||
|
*
|
||||||
|
* See \ref NFast_BignumReceiveUpcall_t */
|
||||||
|
extern int my_bignumreceiveupcall(struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx,
|
||||||
|
M_Bignum *bignum, int nbytes,
|
||||||
|
const void *source,
|
||||||
|
int msbitfirst, int mswordfirst);
|
||||||
|
|
||||||
|
|
||||||
|
/** Bignum send-length upcall
|
||||||
|
*
|
||||||
|
* See \ref NFast_BignumSendLenUpcall_t */
|
||||||
|
extern int my_bignumsendlenupcall(struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx,
|
||||||
|
const M_Bignum *bignum, int *nbytes_r);
|
||||||
|
|
||||||
|
/** Bignum send upcall
|
||||||
|
*
|
||||||
|
* See \ref NFast_BignumSendUpcall_t */
|
||||||
|
extern int my_bignumsendupcall(struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx,
|
||||||
|
const M_Bignum *bignum, int nbytes,
|
||||||
|
void *dest, int msbitfirst, int mswordfirst);
|
||||||
|
|
||||||
|
|
||||||
|
/** Free bignum upcall
|
||||||
|
*
|
||||||
|
* See \ref NFast_BignumFreeUpcall_t */
|
||||||
|
extern void my_bignumfreeupcall(struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx,
|
||||||
|
M_Bignum *bignum);
|
||||||
|
|
||||||
|
/** Bignum format upcall
|
||||||
|
*
|
||||||
|
* See \ref NFast_BignumFormatUpcall_t */
|
||||||
|
extern int my_bignumformatupcall(struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx,
|
||||||
|
int *msbitfirst_io, int *mswordfirst_io);
|
||||||
|
|
||||||
|
/** Structure containing bignum upcalls
|
||||||
|
*
|
||||||
|
* See \ref NFastAppInitArgs and \ref NFAPP_IF_BIGNUM */
|
||||||
|
extern NFast_BignumUpcalls my_upcalls;
|
||||||
|
|
||||||
|
/* Bignum utility functions ----------------------------- */
|
||||||
|
|
||||||
|
/** Convert a hex string to a bignum
|
||||||
|
*
|
||||||
|
* \return Status code
|
||||||
|
*/
|
||||||
|
extern int my_char2bignum ( struct NFast_Bignum **ppBN_out,
|
||||||
|
const char *text,
|
||||||
|
struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx );
|
||||||
|
|
||||||
|
/** Convert a bignum to a hex string
|
||||||
|
*
|
||||||
|
* \return Status code
|
||||||
|
*/
|
||||||
|
extern int my_bignum2char ( char *buf, int buflen,
|
||||||
|
const struct NFast_Bignum *pBN,
|
||||||
|
struct NFast_Application *app,
|
||||||
|
struct NFast_Call_Context *cctx,
|
||||||
|
struct NFast_Transaction_Context *tctx );
|
||||||
|
|
||||||
|
/** Print a bignum in hex to a file
|
||||||
|
*
|
||||||
|
* Call ferror() to test for output errors.
|
||||||
|
*/
|
||||||
|
extern void my_printbignum ( FILE *f,
|
||||||
|
const char *prefix, const struct NFast_Bignum *pBN );
|
||||||
|
|
||||||
|
|
||||||
|
/** Compare two bignums
|
||||||
|
*
|
||||||
|
* \return -1, 0 or 1 if A\<B, A=B or A\>B
|
||||||
|
*/
|
||||||
|
extern int my_compare ( const struct NFast_Bignum *pA,
|
||||||
|
const struct NFast_Bignum *pB );
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user