diff options
Diffstat (limited to 'mpi')
-rw-r--r-- | mpi/ChangeLog | 8 | ||||
-rw-r--r-- | mpi/mpi-mpow.c | 14 | ||||
-rw-r--r-- | mpi/mpicoder.c | 73 | ||||
-rw-r--r-- | mpi/mpiutil.c | 16 |
4 files changed, 73 insertions, 38 deletions
diff --git a/mpi/ChangeLog b/mpi/ChangeLog index 125fa1a68..2d2ec8bdb 100644 --- a/mpi/ChangeLog +++ b/mpi/ChangeLog @@ -1,3 +1,11 @@ +Mon Jan 24 13:04:28 CET 2000 Werner Koch <[email protected]> + + * mpiutil.c: Removed all memory debugging code. + + * mpicoder.c (gcry_mpi_aprint): New. + + * Replaced all m_ memory functions by g10_ ones. + Fri Dec 31 14:06:56 CET 1999 Werner Koch <[email protected]> * mpi-bit.c (gcry_mpi_get_nbits): New. diff --git a/mpi/mpi-mpow.c b/mpi/mpi-mpow.c index a8c561dd1..556f7e116 100644 --- a/mpi/mpi-mpow.c +++ b/mpi/mpi-mpow.c @@ -45,16 +45,16 @@ static int build_index( MPI *exparray, int k, int i, int t ) { int j, bitno; - int index = 0; + int idx = 0; bitno = t-i; for(j=k-1; j >= 0; j-- ) { - index <<= 1; + idx <<= 1; if( mpi_test_bit( exparray[j], bitno ) ) - index |= 1; + idx |= 1; } - /*log_debug("t=%d i=%d index=%d\n", t, i, index );*/ - return index; + /*log_debug("t=%d i=%d idx=%d\n", t, i, idx );*/ + return idx; } /**************** @@ -87,7 +87,7 @@ mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI m) assert(t); assert( k < 10 ); - G = m_alloc_clear( (1<<k) * sizeof *G ); + G = g10_xcalloc( (1<<k) , sizeof *G ); #ifdef USE_BARRETT barrett_y = init_barrett( m, &barrett_k, &barrett_r1, &barrett_r2 ); #endif @@ -128,7 +128,7 @@ mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI m) #endif for(i=0; i < (1<<k); i++ ) mpi_free(G[i]); - m_free(G); + g10_free(G); } diff --git a/mpi/mpicoder.c b/mpi/mpicoder.c index 753c17673..ca805f28b 100644 --- a/mpi/mpicoder.c +++ b/mpi/mpicoder.c @@ -221,8 +221,8 @@ do_get_buffer( MPI a, unsigned *nbytes, int *sign, int force_secure ) if( sign ) *sign = a->sign; *nbytes = a->nlimbs * BYTES_PER_MPI_LIMB; - p = buffer = force_secure || mpi_is_secure(a) ? m_alloc_secure( *nbytes) - : m_alloc( *nbytes ); + p = buffer = force_secure || mpi_is_secure(a) ? g10_xmalloc_secure( *nbytes) + : g10_xmalloc( *nbytes ); for(i=a->nlimbs-1; i >= 0; i-- ) { alimb = a->d[i]; @@ -340,6 +340,7 @@ gcry_mpi_scan( struct gcry_mpi **ret_mpi, enum gcry_mpi_format format, /* TODO: add a way to allocate the MPI in secure memory * Hmmm: maybe it is better to retrieve this information from * the provided buffer. */ + #warning secure memory is not used here. if( format == GCRYMPI_FMT_STD ) { const byte *s = buffer; @@ -458,7 +459,7 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, } if( n > len && buffer ) { - m_free(tmp); + g10_free(tmp); return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ } if( buffer ) { @@ -468,7 +469,24 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, memcpy( s, tmp, n-extra ); } - m_free(tmp); + g10_free(tmp); + *nbytes = n; + return 0; + } + else if( format == GCRYMPI_FMT_USG ) { + unsigned int n = (nbits + 7)/8; + + /* we ignore the sign for this format */ + /* FIXME: for performance reasons we should put this into + * mpi_aprint becuase we can then use the buffer directly */ + if( n > len && buffer ) + return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ + if( buffer ) { + char *tmp; + tmp = mpi_get_buffer( a, &n, NULL ); + memcpy( buffer, tmp, n ); + g10_free(tmp); + } *nbytes = n; return 0; } @@ -488,7 +506,7 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, tmp = mpi_get_buffer( a, &n, NULL ); memcpy( s+2, tmp, n ); - m_free(tmp); + g10_free(tmp); } *nbytes = n+2; return 0; @@ -508,7 +526,7 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, } if( n+4 > len && buffer ) { - m_free(tmp); + g10_free(tmp); return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ } if( buffer ) { @@ -522,7 +540,7 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, memcpy( s, tmp, n-extra ); } - m_free(tmp); + g10_free(tmp); *nbytes = 4+n; return 0; } @@ -534,10 +552,10 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, tmp = mpi_get_buffer( a, &n, NULL ); if( !n || (*tmp & 0x80) ) - extra=1; + extra=2; - if( 2*n+3+1 > len && buffer ) { - m_free(tmp); + if( 2*n + extra + !!a->sign + 1 > len && buffer ) { + g10_free(tmp); return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ } if( buffer ) { @@ -559,17 +577,38 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, *nbytes = (char*)s - buffer; } else { - *nbytes = n; - if( a->sign ) - ++*nbytes; - if( extra ) - *nbytes += 2; - ++*nbytes; /* terminating Nul */ + *nbytes = 2*n + extra + !!a->sign + 1; } - m_free(tmp); + g10_free(tmp); return 0; } else return GCRYERR_INV_ARG; } +/**************** + * Like gcry_mpi_print but this function allocates the buffer itself. + * The caller has to supply the address of a pointer. nbytes may be + * NULL. + */ +int +gcry_mpi_aprint( enum gcry_mpi_format format, void **buffer, size_t *nbytes, + struct gcry_mpi *a ) +{ + size_t n; + int rc; + + *buffer = NULL; + rc = gcry_mpi_print( format, NULL, &n, a ); + if( rc ) + return rc; + *buffer = mpi_is_secure(a) ? g10_xmalloc_secure( n ) : g10_xmalloc( n ); + rc = gcry_mpi_print( format, *buffer, &n, a ); + if( rc ) { + g10_free(*buffer); + *buffer = NULL; + } + return rc; +} + + diff --git a/mpi/mpiutil.c b/mpi/mpiutil.c index 58d06970d..2dad135d4 100644 --- a/mpi/mpiutil.c +++ b/mpi/mpiutil.c @@ -1,5 +1,5 @@ /* mpiutil.ac - Utility functions for MPI - * Copyright (C) 1998 Free Software Foundation, Inc. + * Copyright (C) 1998, 2000 Free Software Foundation, Inc. * * This file is part of GnuPG. * @@ -41,8 +41,6 @@ mpi_alloc( unsigned nlimbs ) { MPI a; - if( DBG_MEMORY ) - log_debug("mpi_alloc(%u)\n", nlimbs*BITS_PER_MPI_LIMB ); a = g10_xmalloc( sizeof *a ); a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 0 ) : NULL; a->alloced = nlimbs; @@ -64,8 +62,6 @@ mpi_alloc_secure( unsigned nlimbs ) { MPI a; - if( DBG_MEMORY ) - log_debug("mpi_alloc_secure(%u)\n", nlimbs*BITS_PER_MPI_LIMB ); a = g10_xmalloc( sizeof *a ); a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 1 ) : NULL; a->alloced = nlimbs; @@ -83,9 +79,6 @@ mpi_alloc_limb_space( unsigned nlimbs, int secure ) size_t len = nlimbs * sizeof(mpi_limb_t); mpi_ptr_t p; - if( DBG_MEMORY ) - log_debug("mpi_alloc_limb_space(%u)\n", (unsigned)len*8 ); - p = secure? g10_xmalloc_secure( len ) : g10_xmalloc( len ); return p; @@ -96,9 +89,6 @@ mpi_free_limb_space( mpi_ptr_t a ) { if( !a ) return; - if( DBG_MEMORY ) - log_debug("mpi_free_limb_space\n" ); - g10_free(a); } @@ -147,8 +137,6 @@ mpi_free( MPI a ) { if( !a ) return; - if( DBG_MEMORY ) - log_debug("mpi_free\n" ); if( a->flags & 4 ) g10_free( a->d ); else { @@ -385,7 +373,7 @@ gcry_mpi_randomize( GCRY_MPI w, char *p = mpi_is_secure(w) ? gcry_random_bytes( (nbits+7)/8, level ) : gcry_random_bytes_secure( (nbits+7)/8, level ); mpi_set_buffer( w, p, (nbits+7)/8, 0 ); - m_free(p); + g10_free(p); } |