From 1d7042f104003bc600f4d7878c2f48d72781c2e1 Mon Sep 17 00:00:00 2001 From: kubodera_yuichi Date: Tue, 29 Dec 2009 03:54:06 +0000 Subject: [PATCH] =?UTF-8?q?hsm=5Futils=E3=81=8B=E3=82=89=E3=81=AEmy=5Fallo?= =?UTF-8?q?c,=20my=5Fbignum=E3=82=92=E8=BC=B8=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@125 ff987cc8-cf2f-4642-8568-d52cce064691 --- Makefile | 3 +- cr_hsm_alloc.c | 49 +++++++ cr_hsm_alloc.h | 30 ++++ cr_hsm_bignum.c | 359 ++++++++++++++++++++++++++++++++++++++++++++++++ cr_hsm_bignum.h | 178 ++++++++++++++++++++++++ cr_hsm_code.c | 2 +- 6 files changed, 619 insertions(+), 2 deletions(-) create mode 100644 cr_hsm_alloc.c create mode 100644 cr_hsm_alloc.h create mode 100644 cr_hsm_bignum.c create mode 100644 cr_hsm_bignum.h diff --git a/Makefile b/Makefile index d18b153..fb38342 100644 --- a/Makefile +++ b/Makefile @@ -85,7 +85,8 @@ KEYS_C = cr_eFuse_privKey_prod.c cr_eFuse_pubKey_prod.c \ cr_NintendoCTR2_priv_dummy_dev.c SRCS = main.c cr_generate_id.c cr_id_util.c cr_keyPair.c \ - cr_device_cert.c cr_enc_id.c cr_alloc.c cr_hsm_code.c + cr_device_cert.c cr_enc_id.c cr_alloc.c \ + cr_hsm_code.c cr_hsm_alloc.c cr_hsm_bignum.c OBJS = $(notdir $(SRCS:.c=.o)) diff --git a/cr_hsm_alloc.c b/cr_hsm_alloc.c new file mode 100644 index 0000000..bbe770d --- /dev/null +++ b/cr_hsm_alloc.c @@ -0,0 +1,49 @@ +/* +* my_hsm_alloc.c +*/ + +#include +#include +#include +#include +#include +#include + +#ifdef USE_HSM + +#include "nfastapp.h" +#include "nfutil.h" +#include "my_hsm_alloc.h" + +/* --------------------- */ + +const NFast_MallocUpcalls my_hsm_malloc_upcalls = +{ + my_hsm_malloc, my_hsm_realloc, my_hsm_free +}; + +/* --------------------- */ + +void *my_hsm_malloc( size_t nbytes, + struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) +{ + return malloc( nbytes ); +} + +/* --------------------- */ + +void *my_hsm_realloc( void *ptr, size_t nbytes, + struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) +{ + return realloc( ptr, nbytes ); +} + +/* --------------------- */ + +void my_hsm_free( void *ptr, + struct NFast_Call_Context *cctx, struct NFast_Transaction_Context *tctx ) +{ + free( ptr ); +} + +#endif // HSM diff --git a/cr_hsm_alloc.h b/cr_hsm_alloc.h new file mode 100644 index 0000000..91a39ff --- /dev/null +++ b/cr_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 const 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/cr_hsm_bignum.c b/cr_hsm_bignum.c new file mode 100644 index 0000000..b535776 --- /dev/null +++ b/cr_hsm_bignum.c @@ -0,0 +1,359 @@ +/* +* 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 +#include +#include +#include +#include +#include + +#ifdef USE_HSM + +#include "nfastapp.h" +#include "nfutil.h" +#include "my_hsm_bignum.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 my_upcalls = { + my_bignumreceiveupcall, + my_bignumsendlenupcall, + my_bignumsendupcall, + my_bignumfreeupcall, + my_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; ibytes[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; +} + +/* --------------------- */ + +// 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 */ + 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>4) & 0xF]; + } + + return Status_OK; +} + +/* --------------------- */ + +int my_bignum2bin ( unsigned char *buf, int buflen, + struct NFast_Application *app, + const struct NFast_Bignum *pBN ) +{ + int i, pos, len; + + len = pBN->nbytes; + pos = len; + if ( buflen < pos ) + return Status_BufferFull; + + for ( i = 0; i < len; i++ ) + { + buf[--pos] = getbyte( pBN, i ); + } + + return Status_OK; +} // my_bignum2bin + +/* --------------------- */ + +int my_bignumCopy( struct NFast_Bignum **dst, + const struct NFast_Bignum *src, + struct NFast_Application *app ) +{ + struct NFast_Bignum *pBN; + pBN = (struct NFast_Bignum *)NFastApp_Malloc( app, sizeof(struct NFast_Bignum), NULL, NULL ); + if ( !pBN ) return NOMEM; + + pBN->msb_first = src->msb_first; + pBN->msw_first = src->msw_first; + pBN->nbytes = src->nbytes; + memcpy( pBN->bytes, src->bytes, src->nbytes ); + + *dst = pBN; + + return Status_OK; +} + +/* --------------------- */ + +void my_printbignum ( FILE *f, const char *prefix, const struct NFast_Bignum *pBN ) +{ + char buf[MAXBIGNUMBITS/4+1]; + int rc; + + rc = my_bignum2char(buf, sizeof(buf), pBN, NULL, NULL, NULL); + if ( rc != Status_OK ) strcpy(buf, ""); + 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; +} + +#endif // HSM diff --git a/cr_hsm_bignum.h b/cr_hsm_bignum.h new file mode 100644 index 0000000..c029d8a --- /dev/null +++ b/cr_hsm_bignum.h @@ -0,0 +1,178 @@ +/** \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_ALLOC_H +#define MY_ALLOC_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 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 + */ +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 ); + +// convert NFast_Bignum to binary +int my_bignum2bin ( unsigned char *buf, int buflen, + struct NFast_Application *app, + const struct NFast_Bignum *pBN ); + +// NFast_Bignum copy +int my_bignumCopy( struct NFast_Bignum **dst, + const struct NFast_Bignum *src, + struct NFast_Application *app ); + +/** 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 + */ +extern int my_compare ( const struct NFast_Bignum *pA, + const struct NFast_Bignum *pB ); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/cr_hsm_code.c b/cr_hsm_code.c index 4e8c2b8..415fdd3 100644 --- a/cr_hsm_code.c +++ b/cr_hsm_code.c @@ -128,7 +128,7 @@ #include "rqcard-applic.h" #include "rqcard-fips.h" // nShield optional -#include "simplebignum.h" +#include "cr_hsm_bignum.h" // TORIAEZU : nFast variables NFast_AppHandle hsmHandle;