diff options
author | Werner Koch <wk@gnupg.org> | 2001-05-31 14:16:54 +0000 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2001-05-31 14:16:54 +0000 |
commit | 8bfa2df2135a1bd9823067debf0c8799bac4a936 (patch) | |
tree | b91075d39633de4f762dca14ae2323053ded62c8 /mpi/mpiutil.c | |
parent | 08e3fbb986bed2f39cc5dca806faca9654c3a6df (diff) | |
download | libgcrypt-8bfa2df2135a1bd9823067debf0c8799bac4a936.tar.gz |
The first libgcrypt only release.
Diffstat (limited to 'mpi/mpiutil.c')
-rw-r--r-- | mpi/mpiutil.c | 102 |
1 files changed, 44 insertions, 58 deletions
diff --git a/mpi/mpiutil.c b/mpi/mpiutil.c index c38c0083..6946e20a 100644 --- a/mpi/mpiutil.c +++ b/mpi/mpiutil.c @@ -1,5 +1,5 @@ /* mpiutil.ac - Utility functions for MPI - * Copyright (C) 1998, 2000 Free Software Foundation, Inc. + * Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc. * * This file is part of Libgcrypt. * @@ -36,11 +36,11 @@ * But mpi_alloc is used in a lot of places :-) */ MPI -mpi_alloc( unsigned nlimbs ) +_gcry_mpi_alloc( unsigned nlimbs ) { MPI a; - a = g10_xmalloc( sizeof *a ); + a = gcry_xmalloc( sizeof *a ); a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 0 ) : NULL; a->alloced = nlimbs; a->nlimbs = 0; @@ -50,18 +50,18 @@ mpi_alloc( unsigned nlimbs ) } void -mpi_m_check( MPI a ) +_gcry_mpi_m_check( MPI a ) { - g10_check_heap(a); - g10_check_heap(a->d); + _gcry_check_heap(a); + _gcry_check_heap(a->d); } MPI -mpi_alloc_secure( unsigned nlimbs ) +_gcry_mpi_alloc_secure( unsigned nlimbs ) { MPI a; - a = g10_xmalloc( sizeof *a ); + a = gcry_xmalloc( sizeof *a ); a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 1 ) : NULL; a->alloced = nlimbs; a->flags = 1; @@ -73,27 +73,27 @@ mpi_alloc_secure( unsigned nlimbs ) mpi_ptr_t -mpi_alloc_limb_space( unsigned nlimbs, int secure ) +_gcry_mpi_alloc_limb_space( unsigned nlimbs, int secure ) { size_t len = nlimbs * sizeof(mpi_limb_t); mpi_ptr_t p; - p = secure? g10_xmalloc_secure( len ) : g10_xmalloc( len ); + p = secure? gcry_xmalloc_secure( len ) : gcry_xmalloc( len ); return p; } void -mpi_free_limb_space( mpi_ptr_t a ) +_gcry_mpi_free_limb_space( mpi_ptr_t a ) { if( !a ) return; - g10_free(a); + gcry_free(a); } void -mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs ) +_gcry_mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs ) { mpi_free_limb_space(a->d); a->d = ap; @@ -104,27 +104,27 @@ 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 g10_realloc()] + * (set to 0) [done by gcry_realloc()] */ void -mpi_resize( MPI a, unsigned nlimbs ) +_gcry_mpi_resize( MPI a, unsigned nlimbs ) { 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 g10_is_secure + * a wrapper around gcry_is_secure */ if( a->d ) - a->d = g10_xrealloc(a->d, nlimbs * sizeof(mpi_limb_t) ); + a->d = gcry_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->d = gcry_xcalloc( nlimbs , sizeof(mpi_limb_t) ); a->alloced = nlimbs; } void -mpi_clear( MPI a ) +_gcry_mpi_clear( MPI a ) { a->nlimbs = 0; a->flags = 0; @@ -132,18 +132,18 @@ mpi_clear( MPI a ) void -mpi_free( MPI a ) +_gcry_mpi_free( MPI a ) { if( !a ) return; if( a->flags & 4 ) - g10_free( a->d ); + gcry_free( a->d ); else { mpi_free_limb_space(a->d); } if( a->flags & ~7 ) log_bug("invalid flag value in mpi\n"); - g10_free(a); + gcry_free(a); } static void @@ -174,7 +174,7 @@ gcry_mpi_set_opaque( MPI a, void *p, unsigned int nbits ) } if( a->flags & 4 ) - g10_free( a->d ); + gcry_free( a->d ); else { mpi_free_limb_space(a->d); } @@ -204,14 +204,14 @@ gcry_mpi_get_opaque( MPI a, unsigned int *nbits ) * but copy it transparently. */ MPI -mpi_copy( MPI a ) +_gcry_mpi_copy( MPI a ) { int i; MPI b; if( a && (a->flags & 4) ) { - void *p = g10_is_secure(a->d)? g10_xmalloc_secure( (a->sign+7)/8 ) - : g10_xmalloc( (a->sign+7)/8 ); + void *p = gcry_is_secure(a->d)? gcry_xmalloc_secure( (a->sign+7)/8 ) + : gcry_xmalloc( (a->sign+7)/8 ); memcpy( p, a->d, (a->sign+7)/8 ); b = gcry_mpi_set_opaque( NULL, p, a->sign ); } @@ -232,18 +232,18 @@ mpi_copy( MPI a ) /**************** * This function allocates an MPI which is optimized to hold - * a value as large as the one given in the arhgument and allocates it + * a value as large as the one given in the argument and allocates it * with the same flags as A. */ MPI -mpi_alloc_like( MPI a ) +_gcry_mpi_alloc_like( MPI a ) { MPI b; if( a && (a->flags & 4) ) { int n = (a->sign+7)/8; - void *p = g10_is_secure(a->d)? g10_malloc_secure( n ) - : g10_malloc( n ); + void *p = gcry_is_secure(a->d)? gcry_malloc_secure( n ) + : gcry_malloc( n ); memcpy( p, a->d, n ); b = gcry_mpi_set_opaque( NULL, p, a->sign ); } @@ -261,7 +261,7 @@ mpi_alloc_like( MPI a ) void -mpi_set( MPI w, MPI u) +_gcry_mpi_set( MPI w, MPI u) { mpi_ptr_t wp, up; mpi_size_t usize = u->nlimbs; @@ -278,7 +278,7 @@ mpi_set( MPI w, MPI u) void -mpi_set_ui( MPI w, unsigned long u) +_gcry_mpi_set_ui( MPI w, unsigned long u) { RESIZE_IF_NEEDED(w, 1); w->d[0] = u; @@ -289,7 +289,7 @@ mpi_set_ui( MPI w, unsigned long u) MPI -mpi_alloc_set_ui( unsigned long u) +_gcry_mpi_alloc_set_ui( unsigned long u) { MPI w = mpi_alloc(1); w->d[0] = u; @@ -300,7 +300,7 @@ mpi_alloc_set_ui( unsigned long u) void -mpi_swap( MPI a, MPI b) +_gcry_mpi_swap( MPI a, MPI b) { struct gcry_mpi tmp; @@ -311,34 +311,34 @@ mpi_swap( MPI a, MPI b) GCRY_MPI gcry_mpi_new( unsigned int nbits ) { - return mpi_alloc( (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB ); + return _gcry_mpi_alloc( (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB ); } GCRY_MPI gcry_mpi_snew( unsigned int nbits ) { - return mpi_alloc_secure( (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB ); + return _gcry_mpi_alloc_secure( (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB ); } void gcry_mpi_release( GCRY_MPI a ) { - mpi_free( a ); + _gcry_mpi_free( a ); } GCRY_MPI gcry_mpi_copy( const GCRY_MPI a ) { - return mpi_copy( (GCRY_MPI)a ); + return _gcry_mpi_copy( (GCRY_MPI)a ); } GCRY_MPI gcry_mpi_set( GCRY_MPI w, const GCRY_MPI u ) { if( !w ) - w = mpi_alloc( mpi_get_nlimbs(u) ); - mpi_set( w, (GCRY_MPI)u ); + w = _gcry_mpi_alloc( mpi_get_nlimbs(u) ); + _gcry_mpi_set( w, (GCRY_MPI)u ); return w; } @@ -346,33 +346,20 @@ GCRY_MPI gcry_mpi_set_ui( GCRY_MPI w, unsigned long u ) { if( !w ) - w = mpi_alloc(1); - mpi_set_ui( w, u ); + w = _gcry_mpi_alloc(1); + _gcry_mpi_set_ui( w, u ); return w; } -int -gcry_mpi_cmp( const GCRY_MPI u, const GCRY_MPI v ) -{ - return mpi_cmp( (GCRY_MPI)u, (GCRY_MPI)v ); -} - -int -gcry_mpi_cmp_ui( const GCRY_MPI u, unsigned long v ) -{ - return mpi_cmp_ui( (GCRY_MPI)u, v ); -} - - void gcry_mpi_randomize( GCRY_MPI w, unsigned int nbits, enum gcry_random_level level ) { 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 ); - g10_free(p); + _gcry_mpi_set_buffer( w, p, (nbits+7)/8, 0 ); + gcry_free(p); } @@ -406,4 +393,3 @@ gcry_mpi_get_flag( GCRY_MPI a, enum gcry_mpi_flag flag ) } } - |