mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-02 22:46:30 +02:00
See ChangeLog: Fri Nov 19 17:15:20 CET 1999 Werner Koch
This commit is contained in:
parent
37f3c09edb
commit
2694bceb45
35 changed files with 422 additions and 289 deletions
173
mpi/mpiutil.c
173
mpi/mpiutil.c
|
@ -27,14 +27,7 @@
|
|||
#include "mpi.h"
|
||||
#include "mpi-internal.h"
|
||||
#include "memory.h"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
#ifdef M_DEBUG
|
||||
#undef mpi_alloc
|
||||
#undef mpi_alloc_secure
|
||||
#undef mpi_free
|
||||
#endif
|
||||
#include "g10lib.h"
|
||||
|
||||
/****************
|
||||
* Note: It was a bad idea to use the number of limbs to allocate
|
||||
|
@ -44,23 +37,14 @@
|
|||
* But mpi_alloc is used in a lot of places :-)
|
||||
*/
|
||||
MPI
|
||||
#ifdef M_DEBUG
|
||||
mpi_debug_alloc( unsigned nlimbs, const char *info )
|
||||
#else
|
||||
mpi_alloc( unsigned nlimbs )
|
||||
#endif
|
||||
{
|
||||
MPI a;
|
||||
|
||||
if( DBG_MEMORY )
|
||||
log_debug("mpi_alloc(%u)\n", nlimbs*BITS_PER_MPI_LIMB );
|
||||
#ifdef M_DEBUG
|
||||
a = m_debug_alloc( sizeof *a, info );
|
||||
a->d = nlimbs? mpi_debug_alloc_limb_space( nlimbs, 0, info ) : NULL;
|
||||
#else
|
||||
a = m_alloc( sizeof *a );
|
||||
a = g10_xmalloc( sizeof *a );
|
||||
a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 0 ) : NULL;
|
||||
#endif
|
||||
a->alloced = nlimbs;
|
||||
a->nlimbs = 0;
|
||||
a->sign = 0;
|
||||
|
@ -72,28 +56,19 @@ mpi_alloc( unsigned nlimbs )
|
|||
void
|
||||
mpi_m_check( MPI a )
|
||||
{
|
||||
m_check(a);
|
||||
m_check(a->d);
|
||||
g10_check_heap(a);
|
||||
g10_check_heap(a->d);
|
||||
}
|
||||
|
||||
MPI
|
||||
#ifdef M_DEBUG
|
||||
mpi_debug_alloc_secure( unsigned nlimbs, const char *info )
|
||||
#else
|
||||
mpi_alloc_secure( unsigned nlimbs )
|
||||
#endif
|
||||
{
|
||||
MPI a;
|
||||
|
||||
if( DBG_MEMORY )
|
||||
log_debug("mpi_alloc_secure(%u)\n", nlimbs*BITS_PER_MPI_LIMB );
|
||||
#ifdef M_DEBUG
|
||||
a = m_debug_alloc( sizeof *a, info );
|
||||
a->d = nlimbs? mpi_debug_alloc_limb_space( nlimbs, 1, info ) : NULL;
|
||||
#else
|
||||
a = m_alloc( sizeof *a );
|
||||
a = g10_xmalloc( sizeof *a );
|
||||
a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 1 ) : NULL;
|
||||
#endif
|
||||
a->alloced = nlimbs;
|
||||
a->flags = 1;
|
||||
a->nlimbs = 0;
|
||||
|
@ -103,90 +78,30 @@ mpi_alloc_secure( unsigned nlimbs )
|
|||
}
|
||||
|
||||
|
||||
#if 0
|
||||
static void *unused_limbs_5;
|
||||
static void *unused_limbs_32;
|
||||
static void *unused_limbs_64;
|
||||
#endif
|
||||
|
||||
mpi_ptr_t
|
||||
#ifdef M_DEBUG
|
||||
mpi_debug_alloc_limb_space( unsigned nlimbs, int secure, const char *info )
|
||||
#else
|
||||
mpi_alloc_limb_space( unsigned nlimbs, int secure )
|
||||
#endif
|
||||
{
|
||||
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 );
|
||||
#if 0
|
||||
if( !secure ) {
|
||||
if( nlimbs == 5 && unused_limbs_5 ) { /* DSA 160 bits */
|
||||
p = unused_limbs_5;
|
||||
unused_limbs_5 = *p;
|
||||
return p;
|
||||
}
|
||||
else if( nlimbs == 32 && unused_limbs_32 ) { /* DSA 1024 bits */
|
||||
p = unused_limbs_32;
|
||||
unused_limbs_32 = *p;
|
||||
return p;
|
||||
}
|
||||
else if( nlimbs == 64 && unused_limbs_64 ) { /* DSA 2*1024 bits */
|
||||
p = unused_limbs_64;
|
||||
unused_limbs_64 = *p;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef M_DEBUG
|
||||
p = secure? m_debug_alloc_secure(len, info):m_debug_alloc( len, info );
|
||||
#else
|
||||
p = secure? m_alloc_secure( len ):m_alloc( len );
|
||||
#endif
|
||||
p = secure? g10_xmalloc_secure( len ) : g10_xmalloc( len );
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void
|
||||
#ifdef M_DEBUG
|
||||
mpi_debug_free_limb_space( mpi_ptr_t a, const char *info )
|
||||
#else
|
||||
mpi_free_limb_space( mpi_ptr_t a )
|
||||
#endif
|
||||
{
|
||||
if( !a )
|
||||
return;
|
||||
if( DBG_MEMORY )
|
||||
log_debug("mpi_free_limb_space of size %lu\n", (ulong)m_size(a)*8 );
|
||||
log_debug("mpi_free_limb_space\n" );
|
||||
|
||||
#if 0
|
||||
if( !m_is_secure(a) ) {
|
||||
size_t nlimbs = m_size(a) / 4 ;
|
||||
void *p = a;
|
||||
|
||||
if( nlimbs == 5 ) { /* DSA 160 bits */
|
||||
*a = unused_limbs_5;
|
||||
unused_limbs_5 = a;
|
||||
return;
|
||||
}
|
||||
else if( nlimbs == 32 ) { /* DSA 1024 bits */
|
||||
*a = unused_limbs_32;
|
||||
unused_limbs_32 = a;
|
||||
return;
|
||||
}
|
||||
else if( nlimbs == 64 ) { /* DSA 2*1024 bits */
|
||||
*a = unused_limbs_64;
|
||||
unused_limbs_64 = a;
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
m_free(a);
|
||||
g10_free(a);
|
||||
}
|
||||
|
||||
|
||||
|
@ -202,33 +117,22 @@ mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs )
|
|||
|
||||
/****************
|
||||
* Resize the array of A to NLIMBS. the additional space is cleared
|
||||
* (set to 0) [done by m_realloc()]
|
||||
* (set to 0) [done by g10_realloc()]
|
||||
*/
|
||||
void
|
||||
#ifdef M_DEBUG
|
||||
mpi_debug_resize( MPI a, unsigned nlimbs, const char *info )
|
||||
#else
|
||||
mpi_resize( MPI a, unsigned nlimbs )
|
||||
#endif
|
||||
{
|
||||
if( nlimbs <= a->alloced )
|
||||
return; /* no need to do it */
|
||||
/* Note: a->secure is not used - instead the realloc functions
|
||||
* take care of it. Maybe we should drop a->secure completely
|
||||
* and rely on a mpi_is_secure function, which would be
|
||||
* a wrapper around m_is_secure
|
||||
* a wrapper around g10_is_secure
|
||||
*/
|
||||
#ifdef M_DEBUG
|
||||
if( a->d )
|
||||
a->d = m_debug_realloc(a->d, nlimbs * sizeof(mpi_limb_t), info );
|
||||
else
|
||||
a->d = m_debug_alloc_clear( nlimbs * sizeof(mpi_limb_t), info );
|
||||
#else
|
||||
if( a->d )
|
||||
a->d = m_realloc(a->d, nlimbs * sizeof(mpi_limb_t) );
|
||||
else
|
||||
a->d = m_alloc_clear( nlimbs * sizeof(mpi_limb_t) );
|
||||
#endif
|
||||
a->d = g10_xrealloc(a->d, nlimbs * sizeof(mpi_limb_t) );
|
||||
else /* FIXME: It may not be allocted in secure memory */
|
||||
a->d = g10_xcalloc( nlimbs , sizeof(mpi_limb_t) );
|
||||
a->alloced = nlimbs;
|
||||
}
|
||||
|
||||
|
@ -242,28 +146,20 @@ mpi_clear( MPI a )
|
|||
|
||||
|
||||
void
|
||||
#ifdef M_DEBUG
|
||||
mpi_debug_free( MPI a, const char *info )
|
||||
#else
|
||||
mpi_free( MPI a )
|
||||
#endif
|
||||
{
|
||||
if( !a )
|
||||
return;
|
||||
if( DBG_MEMORY )
|
||||
log_debug("mpi_free\n" );
|
||||
if( a->flags & 4 )
|
||||
m_free( a->d );
|
||||
g10_free( a->d );
|
||||
else {
|
||||
#ifdef M_DEBUG
|
||||
mpi_debug_free_limb_space(a->d, info);
|
||||
#else
|
||||
mpi_free_limb_space(a->d);
|
||||
#endif
|
||||
}
|
||||
if( a->flags & ~7 )
|
||||
log_bug("invalid flag value in mpi\n");
|
||||
m_free(a);
|
||||
g10_free(a);
|
||||
}
|
||||
|
||||
|
||||
|
@ -280,18 +176,10 @@ mpi_set_secure( MPI a )
|
|||
assert(!ap);
|
||||
return;
|
||||
}
|
||||
#ifdef M_DEBUG
|
||||
bp = mpi_debug_alloc_limb_space( a->nlimbs, 1, "set_secure" );
|
||||
#else
|
||||
bp = mpi_alloc_limb_space( a->nlimbs, 1 );
|
||||
#endif
|
||||
MPN_COPY( bp, ap, a->nlimbs );
|
||||
a->d = bp;
|
||||
#ifdef M_DEBUG
|
||||
mpi_debug_free_limb_space(ap, "set_secure");
|
||||
#else
|
||||
mpi_free_limb_space(ap);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
@ -299,21 +187,13 @@ MPI
|
|||
mpi_set_opaque( MPI a, void *p, int len )
|
||||
{
|
||||
if( !a ) {
|
||||
#ifdef M_DEBUG
|
||||
a = mpi_debug_alloc(0,"alloc_opaque");
|
||||
#else
|
||||
a = mpi_alloc(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
if( a->flags & 4 )
|
||||
m_free( a->d );
|
||||
g10_free( a->d );
|
||||
else {
|
||||
#ifdef M_DEBUG
|
||||
mpi_debug_free_limb_space(a->d, "alloc_opaque");
|
||||
#else
|
||||
mpi_free_limb_space(a->d);
|
||||
#endif
|
||||
}
|
||||
|
||||
a->d = p;
|
||||
|
@ -341,29 +221,20 @@ mpi_get_opaque( MPI a, int *len )
|
|||
* but copy it transparently.
|
||||
*/
|
||||
MPI
|
||||
#ifdef M_DEBUG
|
||||
mpi_debug_copy( MPI a, const char *info )
|
||||
#else
|
||||
mpi_copy( MPI a )
|
||||
#endif
|
||||
{
|
||||
int i;
|
||||
MPI b;
|
||||
|
||||
if( a && (a->flags & 4) ) {
|
||||
void *p = m_is_secure(a->d)? m_alloc_secure( a->nbits )
|
||||
: m_alloc( a->nbits );
|
||||
void *p = g10_is_secure(a->d)? g10_xmalloc_secure( a->nbits )
|
||||
: g10_xmalloc( a->nbits );
|
||||
memcpy( p, a->d, a->nbits );
|
||||
b = mpi_set_opaque( NULL, p, a->nbits );
|
||||
}
|
||||
else if( a ) {
|
||||
#ifdef M_DEBUG
|
||||
b = mpi_is_secure(a)? mpi_debug_alloc_secure( a->nlimbs, info )
|
||||
: mpi_debug_alloc( a->nlimbs, info );
|
||||
#else
|
||||
b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs )
|
||||
: mpi_alloc( a->nlimbs );
|
||||
#endif
|
||||
b->nlimbs = a->nlimbs;
|
||||
b->sign = a->sign;
|
||||
b->flags = a->flags;
|
||||
|
@ -388,8 +259,8 @@ mpi_alloc_like( MPI a )
|
|||
MPI b;
|
||||
|
||||
if( a && (a->flags & 4) ) {
|
||||
void *p = m_is_secure(a->d)? m_alloc_secure( a->nbits )
|
||||
: m_alloc( a->nbits );
|
||||
void *p = g10_is_secure(a->d)? g10_malloc_secure( a->nbits )
|
||||
: g10_malloc( a->nbits );
|
||||
memcpy( p, a->d, a->nbits );
|
||||
b = mpi_set_opaque( NULL, p, a->nbits );
|
||||
}
|
||||
|
@ -440,11 +311,7 @@ mpi_set_ui( MPI w, unsigned long u)
|
|||
MPI
|
||||
mpi_alloc_set_ui( unsigned long u)
|
||||
{
|
||||
#ifdef M_DEBUG
|
||||
MPI w = mpi_debug_alloc(1,"alloc_set_ui");
|
||||
#else
|
||||
MPI w = mpi_alloc(1);
|
||||
#endif
|
||||
w->d[0] = u;
|
||||
w->nlimbs = u? 1:0;
|
||||
w->sign = 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue