mirror of
https://github.com/rvtr/ctr_eFuse.git
synced 2025-11-02 00:11:04 -04:00
hsm_utils:bin2bignumを切り出し
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@105 ff987cc8-cf2f-4642-8568-d52cce064691
This commit is contained in:
parent
88951ece8e
commit
db8f08ddee
@ -66,10 +66,6 @@ static void *my_realloc( void *ptr, size_t nbytes,
|
||||
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 =
|
||||
@ -95,41 +91,6 @@ static void my_free( void *ptr,
|
||||
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;
|
||||
@ -427,50 +388,50 @@ int main( int argc, char *argv[] )
|
||||
struct NFast_Bignum *eBn = NULL;
|
||||
{
|
||||
// p
|
||||
result = sbn_bin2bignum( &pBn, handle, pPtr, pLen );
|
||||
result = my_bin2bignum( &pBn, handle, pPtr, pLen );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( p )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( p )\n", result );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// q
|
||||
result = sbn_bin2bignum( &qBn, handle, qPtr, qLen );
|
||||
result = my_bin2bignum( &qBn, handle, qPtr, qLen );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( q )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( q )\n", result );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// dmp1
|
||||
result = sbn_bin2bignum( &dmp1Bn, handle, dmp1Ptr, dmp1Len );
|
||||
result = my_bin2bignum( &dmp1Bn, handle, dmp1Ptr, dmp1Len );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( dmp1 )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( dmp1 )\n", result );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// dmq1
|
||||
result = sbn_bin2bignum( &dmq1Bn, handle, dmq1Ptr, dmq1Len );
|
||||
result = my_bin2bignum( &dmq1Bn, handle, dmq1Ptr, dmq1Len );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( dmq1 )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( dmq1 )\n", result );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// iqmp
|
||||
result = sbn_bin2bignum( &iqmpBn, handle, iqmpPtr, iqmpLen );
|
||||
result = my_bin2bignum( &iqmpBn, handle, iqmpPtr, iqmpLen );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( iqmp )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( iqmp )\n", result );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// e
|
||||
result = sbn_bin2bignum( &eBn, handle, ePtr, eLen );
|
||||
result = my_bin2bignum( &eBn, handle, ePtr, eLen );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( e )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( e )\n", result );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,10 +65,6 @@ 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 );
|
||||
|
||||
@ -95,41 +91,6 @@ static void my_free( void *ptr,
|
||||
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;
|
||||
@ -427,50 +388,50 @@ int main( int argc, char *argv[] )
|
||||
struct NFast_Bignum *eBn = NULL;
|
||||
{
|
||||
// p
|
||||
result = sbn_bin2bignum( &pBn, handle, pPtr, pLen );
|
||||
result = my_bin2bignum( &pBn, handle, pPtr, pLen );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( p )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( p )\n", result );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// q
|
||||
result = sbn_bin2bignum( &qBn, handle, qPtr, qLen );
|
||||
result = my_bin2bignum( &qBn, handle, qPtr, qLen );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( q )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( q )\n", result );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// dmp1
|
||||
result = sbn_bin2bignum( &dmp1Bn, handle, dmp1Ptr, dmp1Len );
|
||||
result = my_bin2bignum( &dmp1Bn, handle, dmp1Ptr, dmp1Len );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( dmp1 )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( dmp1 )\n", result );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// dmq1
|
||||
result = sbn_bin2bignum( &dmq1Bn, handle, dmq1Ptr, dmq1Len );
|
||||
result = my_bin2bignum( &dmq1Bn, handle, dmq1Ptr, dmq1Len );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( dmq1 )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( dmq1 )\n", result );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// iqmp
|
||||
result = sbn_bin2bignum( &iqmpBn, handle, iqmpPtr, iqmpLen );
|
||||
result = my_bin2bignum( &iqmpBn, handle, iqmpPtr, iqmpLen );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( iqmp )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( iqmp )\n", result );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// e
|
||||
result = sbn_bin2bignum( &eBn, handle, ePtr, eLen );
|
||||
result = my_bin2bignum( &eBn, handle, ePtr, eLen );
|
||||
if ( result != Status_OK )
|
||||
{
|
||||
printf( "error(%d) : sbn_bin2bignum( e )\n", result );
|
||||
printf( "error(%d) : my_bin2bignum( e )\n", result );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -118,12 +118,12 @@ int my_bignumformatupcall(struct NFast_Application *app,
|
||||
return Status_OK;
|
||||
}
|
||||
|
||||
NFast_BignumUpcalls my_upcalls = {
|
||||
my_bignumreceiveupcall,
|
||||
my_bignumsendlenupcall,
|
||||
my_bignumsendupcall,
|
||||
my_bignumfreeupcall,
|
||||
my_bignumformatupcall
|
||||
NFast_BignumUpcalls sbn_upcalls = {
|
||||
sbn_bignumreceiveupcall,
|
||||
sbn_bignumsendlenupcall,
|
||||
sbn_bignumsendupcall,
|
||||
sbn_bignumfreeupcall,
|
||||
sbn_bignumformatupcall
|
||||
};
|
||||
|
||||
/* --------------------- */
|
||||
@ -190,6 +190,39 @@ int my_char2bignum ( struct NFast_Bignum **ppBN_out,
|
||||
|
||||
/* --------------------- */
|
||||
|
||||
// bin データを NFast_Bignum データに変換する
|
||||
int my_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;
|
||||
|
||||
return Status_OK;
|
||||
} // my_bin2bignum
|
||||
|
||||
/* --------------------- */
|
||||
|
||||
static int getbyte ( const struct NFast_Bignum *pN, int pos )
|
||||
{
|
||||
/* Get a byte from a bignum, taking account of possible strange endianness */
|
||||
@ -253,7 +286,7 @@ void my_printbignum ( FILE *f, const char *prefix, const struct NFast_Bignum *pB
|
||||
char buf[MAXBIGNUMBITS/4+1];
|
||||
int rc;
|
||||
|
||||
rc = my_bignum2char(buf, sizeof(buf), pBN, NULL, NULL, NULL);
|
||||
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 );
|
||||
}
|
||||
|
||||
@ -130,6 +130,11 @@ extern int my_char2bignum ( struct NFast_Bignum **ppBN_out,
|
||||
struct NFast_Call_Context *cctx,
|
||||
struct NFast_Transaction_Context *tctx );
|
||||
|
||||
// convert binary to NFast_Bignum
|
||||
extern int my_bin2bignum ( struct NFast_Bignum **ppBN_out,
|
||||
struct NFast_Application *app,
|
||||
const unsigned char *bin, const int size );
|
||||
|
||||
/** Convert a bignum to a hex string
|
||||
*
|
||||
* \return Status code
|
||||
|
||||
Loading…
Reference in New Issue
Block a user