diff options
author | Werner Koch <wk@gnupg.org> | 1999-12-08 22:03:03 +0100 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 1999-12-08 22:03:03 +0100 |
commit | 4555c0be941cef55486b7a1644d9b70f4ea50c77 (patch) | |
tree | 1a10627bf31163f9aef5040724c53f75e8daf3e6 /mpi | |
parent | See ChangeLog: Fri Nov 19 17:15:20 CET 1999 Werner Koch (diff) | |
download | gnupg2-4555c0be941cef55486b7a1644d9b70f4ea50c77.tar.xz gnupg2-4555c0be941cef55486b7a1644d9b70f4ea50c77.zip |
See ChangeLog: Wed Dec 8 21:58:32 CET 1999 Werner Koch
Diffstat (limited to 'mpi')
-rw-r--r-- | mpi/ChangeLog | 33 | ||||
-rw-r--r-- | mpi/Makefile.am | 5 | ||||
-rw-r--r-- | mpi/g10m.c | 95 | ||||
-rw-r--r-- | mpi/mpi-bit.c | 11 | ||||
-rw-r--r-- | mpi/mpi-pow.c | 4 | ||||
-rw-r--r-- | mpi/mpicoder.c | 338 | ||||
-rw-r--r-- | mpi/mpiutil.c | 106 |
7 files changed, 356 insertions, 236 deletions
diff --git a/mpi/ChangeLog b/mpi/ChangeLog index 3a0fca1ad..81fec2a00 100644 --- a/mpi/ChangeLog +++ b/mpi/ChangeLog @@ -1,3 +1,36 @@ +Wed Dec 8 21:58:32 CET 1999 Werner Koch <wk@gnupg.de> + + * Makefile.am (INCLUDES): Add ../gcrypt. + + * g10m.c : Removed. + + * mpicoder.c (mpi_write): Removed. + (mpi_read): Removed. + (gcry_mpi_scan): New. Taken from ../gcrypt/mpiapi.c. + (gcry_mpi_print): Ditto. + + * mpi-pow.c (mpi_powm): Renamed to ... + (gcry_mpi_powm): ... this. + + * mpiutil.c (gcry_mpi_new): New as a wrapper around the old function. + Taken from ../gcrypt/mpiapi.c. + (gcry_mpi_snew): Ditto. + (gcry_mpi_release): Ditto. + (gcry_mpi_copy): Ditto. + (gcry_mpi_set): Ditto. + (gcry_mpi_set_ui): Ditto. + (gcry_mpi_cmp): Ditto. + (gcry_mpi_cmp_ui): Ditto. + (gcry_mpi_randomize): Ditto. + + * mpicoder.c (mpi_print): Removed the nbit_info kludge. + * mpi-bits.c (mpi_get_nbits): Replaced the is_protected stuff by + checking whether it is an opaque mpi and then returns it's length + in bits. + * mpiutil.c (mpi_set_opaque): Changed the interface to take a number + of bits for the length. Adjusted all users. + (mpi_get_opaque): Ditto. + Fri Nov 19 17:15:20 CET 1999 Werner Koch <wk@gnupg.de> * mpicoder.c (g10_log_mpidump): Add a temporary workaround diff --git a/mpi/Makefile.am b/mpi/Makefile.am index ef9816aa5..98ad5fcc3 100644 --- a/mpi/Makefile.am +++ b/mpi/Makefile.am @@ -1,7 +1,7 @@ ## Process this file with automake to produce Makefile.in -INCLUDES = -I$(top_srcdir)/include +INCLUDES = -I$(top_srcdir)/gcrypt CFLAGS = @CFLAGS@ @MPI_OPT_FLAGS@ SFLAGS = @MPI_SFLAGS@ @@ -32,8 +32,7 @@ libmpi_la_SOURCES = longlong.h \ mpih-cmp.c \ mpih-div.c \ mpih-mul.c \ - mpiutil.c \ - g10m.c + mpiutil.c # Note this objects are actually links, the sourcefiles are # distributed by special code in dist-hook diff --git a/mpi/g10m.c b/mpi/g10m.c deleted file mode 100644 index c43e067a7..000000000 --- a/mpi/g10m.c +++ /dev/null @@ -1,95 +0,0 @@ -/* g10m.c - Wrapper for MPI - * Copyright (C) 1998 Free Software Foundation, Inc. - * - * This file is part of GnuPG. - * - * GnuPG is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * GnuPG is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#include <config.h> -#include <stdio.h> -#include <stdlib.h> -#include "mpi.h" -#include "util.h" - -/* FIXME: The modules should use functions from libgcrypt */ - -const char *g10m_revision_string(int dummy) { return "$Revision$"; } - -MPI -g10m_new( unsigned nbits ) -{ - return mpi_alloc( (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB ); -} - -MPI -g10m_new_secure( unsigned nbits ) -{ - return mpi_alloc_secure( (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB ); -} - -void -g10m_release( MPI a ) -{ - mpi_free(a); -} - -void -g10m_resize( MPI a, unsigned nbits ) -{ - mpi_resize( a, (nbits+BITS_PER_MPI_LIMB-1) / BITS_PER_MPI_LIMB ); -} - -MPI g10m_copy( MPI a ) { return mpi_copy( a ); } -void g10m_swap( MPI a, MPI b) { mpi_swap( a, b ); } -void g10m_set( MPI w, MPI u) { mpi_set( w, u ); } -void g10m_set_ui( MPI w, ulong u ) { mpi_set_ui( w, u ); } - -int g10m_cmp( MPI u, MPI v ) { return mpi_cmp( u, v ); } -int g10m_cmp_ui( MPI u, ulong v ) { return mpi_cmp_ui( u, v ); } - -void g10m_add(MPI w, MPI u, MPI v) { mpi_add( w, u, v ); } -void g10m_add_ui(MPI w, MPI u, ulong v ) { mpi_add_ui( w, u, v ); } -void g10m_sub( MPI w, MPI u, MPI v) { mpi_sub( w, u, v ); } -void g10m_sub_ui(MPI w, MPI u, ulong v ) { mpi_sub_ui( w, u, v ); } - -void g10m_mul( MPI w, MPI u, MPI v) { mpi_mul( w, u, v ); } -void g10m_mulm( MPI w, MPI u, MPI v, MPI m) { mpi_mulm( w, u, v, m ); } -void g10m_mul_2exp( MPI w, MPI u, ulong cnt) { mpi_mul_2exp( w, u, cnt ); } -void g10m_mul_ui(MPI w, MPI u, ulong v ) { mpi_mul_ui( w, u, v ); } - -void g10m_fdiv_q( MPI q, MPI d, MPI r ) { mpi_fdiv_q( q, d, r ); } - -void g10m_powm( MPI r, MPI b, MPI e, MPI m) { mpi_powm( r, b, e, m ); } - -int g10m_gcd( MPI g, MPI a, MPI b ) { return mpi_gcd( g, a, b ); } -int g10m_invm( MPI x, MPI u, MPI v ) { mpi_invm( x, u, v ); return 0; } - -unsigned g10m_get_nbits( MPI a ) { return mpi_get_nbits( a ); } - -unsigned -g10m_get_size( MPI a ) -{ - return mpi_get_nlimbs( a ) * BITS_PER_MPI_LIMB; -} - - -void -g10m_set_buffer( MPI a, const char *buffer, unsigned nbytes, int sign ) -{ - mpi_set_buffer( a, buffer, nbytes, sign ); -} - - diff --git a/mpi/mpi-bit.c b/mpi/mpi-bit.c index f1eff8636..45ca029ed 100644 --- a/mpi/mpi-bit.c +++ b/mpi/mpi-bit.c @@ -55,7 +55,7 @@ __clz_tab[] = void mpi_normalize( MPI a ) { - if( mpi_is_protected(a) ) + if( mpi_is_opaque(a) ) return; for( ; a->nlimbs && !a->d[a->nlimbs-1]; a->nlimbs-- ) @@ -67,16 +67,13 @@ mpi_normalize( MPI a ) /**************** * Return the number of bits in A. */ -unsigned +unsigned int mpi_get_nbits( MPI a ) { unsigned n; - if( mpi_is_protected(a) ) { - n = mpi_get_nbit_info(a); - if( !n ) - n = a->nlimbs * BITS_PER_MPI_LIMB; - return n; + if( mpi_is_opaque(a) ) { + return a->sign; /* which holds the number of bits */ } mpi_normalize( a ); diff --git a/mpi/mpi-pow.c b/mpi/mpi-pow.c index e8d55f9b9..a57eff87a 100644 --- a/mpi/mpi-pow.c +++ b/mpi/mpi-pow.c @@ -30,16 +30,16 @@ #include <config.h> #include <stdio.h> #include <stdlib.h> +#include <assert.h> #include "mpi-internal.h" #include "longlong.h" -#include <assert.h> /**************** * RES = BASE ^ EXP mod MOD */ void -mpi_powm( MPI res, MPI base, MPI exp, MPI mod) +gcry_mpi_powm( MPI res, MPI base, MPI exp, MPI mod) { mpi_ptr_t rp, ep, mp, bp; mpi_size_t esize, msize, bsize, rsize; diff --git a/mpi/mpicoder.c b/mpi/mpicoder.c index 25d37ec41..352468cbd 100644 --- a/mpi/mpicoder.c +++ b/mpi/mpicoder.c @@ -26,110 +26,11 @@ #include "mpi.h" #include "mpi-internal.h" -#include "iobuf.h" #include "memory.h" -#include "util.h" - -#ifdef M_DEBUG - #undef mpi_read -#endif +#include "g10lib.h" #define MAX_EXTERN_MPI_BITS 16384 -/**************** - * write an mpi to out. - */ -int -mpi_write( IOBUF out, MPI a ) -{ - return -1; - #warning Function is disabled - #if 0 - int rc; - unsigned nbits = mpi_get_nbits(a); - byte *p, *buf; - unsigned n; - - if( nbits > MAX_EXTERN_MPI_BITS ) - log_bug("mpi_encode: mpi too large (%u bits)\n", nbits); - - iobuf_put(out, (nbits >>8) ); - iobuf_put(out, (nbits) ); - - p = buf = mpi_get_buffer( a, &n, NULL ); - rc = iobuf_write( out, p, n ); - m_free(buf); - return rc; - #endif -} - - -/**************** - * Read an external representation of an mpi and return the MPI - * The external format is a 16 bit unsigned value stored in network byte order, - * giving the number of bits for the following integer. The integer is stored - * with MSB first (left padded with zeroes to align on a byte boundary). - */ -MPI -#ifdef M_DEBUG -mpi_debug_read(IOBUF inp, unsigned *ret_nread, int secure, const char *info) -#else -mpi_read(IOBUF inp, unsigned *ret_nread, int secure) -#endif -{ - return NULL; - #warning Function is disabled - #if 0 - int c, i, j; - unsigned nbits, nbytes, nlimbs, nread=0; - mpi_limb_t a; - MPI val = MPI_NULL; - - if( (c = iobuf_get(inp)) == -1 ) - goto leave; - nbits = c << 8; - if( (c = iobuf_get(inp)) == -1 ) - goto leave; - nbits |= c; - if( nbits > MAX_EXTERN_MPI_BITS ) { - log_error("mpi too large (%u bits)\n", nbits); - goto leave; - } - nread = 2; - - nbytes = (nbits+7) / 8; - nlimbs = (nbytes+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB; - #ifdef M_DEBUG - val = secure? mpi_debug_alloc_secure( nlimbs, info ) - : mpi_debug_alloc( nlimbs, info ); - #else - val = secure? mpi_alloc_secure( nlimbs ) - : mpi_alloc( nlimbs ); - #endif - i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; - i %= BYTES_PER_MPI_LIMB; - val->nbits = nbits; - j= val->nlimbs = nlimbs; - val->sign = 0; - for( ; j > 0; j-- ) { - a = 0; - for(; i < BYTES_PER_MPI_LIMB; i++ ) { - a <<= 8; - a |= iobuf_get(inp) & 0xff; nread++; - } - i = 0; - val->d[j-1] = a; - } - - leave: - if( nread > *ret_nread ) - log_bug("mpi crosses packet border"); - else - *ret_nread = nread; - return val; - #endif -} - MPI mpi_read_from_buffer(byte *buffer, unsigned *ret_nread, int secure) @@ -155,7 +56,6 @@ mpi_read_from_buffer(byte *buffer, unsigned *ret_nread, int secure) : mpi_alloc( nlimbs ); i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; i %= BYTES_PER_MPI_LIMB; - val->nbits = nbits; j= val->nlimbs = nlimbs; val->sign = 0; for( ; j > 0; j-- ) { @@ -264,13 +164,9 @@ mpi_print( FILE *fp, MPI a, int mode ) if( a == MPI_NULL ) return fprintf(fp, "[MPI_NULL]"); if( !mode ) { - unsigned n1, n2; + unsigned int n1; n1 = mpi_get_nbits(a); - n2 = mpi_get_nbit_info(a); - if( n2 && n2 != n1 ) - n += fprintf(fp, "[%u bits (%u)]", n1, n2 ); - else - n += fprintf(fp, "[%u bits]", n1); + n += fprintf(fp, "[%u bits]", n1); } else { if( a->sign ) @@ -454,3 +350,231 @@ mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign ) assert( i == nlimbs ); } + + +int +gcry_mpi_scan( struct gcry_mpi **ret_mpi, enum gcry_mpi_format format, + const char *buffer, size_t *nbytes ) +{ + struct gcry_mpi *a = NULL; + unsigned int len; + + len = nbytes? *nbytes : strlen(buffer); + + /* TODO: add a way to allocate the MPI in secure memory + * Hmmm: maybe it is better to retrieve this information from + * the provided buffer. */ + if( format == GCRYMPI_FMT_STD ) { + const byte *s = buffer; + + a = mpi_alloc( (len+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ); + if( len ) { /* not zero */ + a->sign = *s & 0x80; + if( a->sign ) { + /* FIXME: we have to convert from 2compl to magnitude format */ + mpi_free(a); + return GCRYERR_INTERNAL; + } + else + mpi_set_buffer( a, s, len, 0 ); + } + if( ret_mpi ) + *ret_mpi = a; + else + mpi_free(a); + return 0; + } + else if( format == GCRYMPI_FMT_USG ) { + a = mpi_alloc( (len+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ); + if( len ) /* not zero */ + mpi_set_buffer( a, buffer, len, 0 ); + if( ret_mpi ) + *ret_mpi = a; + else + mpi_free(a); + return 0; + } + else if( format == GCRYMPI_FMT_PGP ) { + a = mpi_read_from_buffer( (char*)buffer, &len, 0 ); + if( nbytes ) + *nbytes = len; + if( ret_mpi ) + *ret_mpi = a; + else + mpi_free(a); + return a? 0 : GCRYERR_INV_OBJ; + } + else if( format == GCRYMPI_FMT_SSH ) { + const byte *s = buffer; + size_t n; + + if( len < 4 ) + return GCRYERR_TOO_SHORT; + n = s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]; + s += 4; len -= 4; + if( n > len ) + return GCRYERR_TOO_LARGE; /* or should it be too_short */ + + a = mpi_alloc( (n+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ); + if( len ) { /* not zero */ + a->sign = *s & 0x80; + if( a->sign ) { + /* FIXME: we have to convert from 2compl to magnitude format */ + mpi_free(a); + return GCRYERR_INTERNAL; + } + else + mpi_set_buffer( a, s, n, 0 ); + } + if( nbytes ) + *nbytes = n+4; + if( ret_mpi ) + *ret_mpi = a; + else + mpi_free(a); + return 0; + } + else if( format == GCRYMPI_FMT_HEX ) { + if( nbytes ) + return GCRYERR_INV_ARG; /* can only handle C strings for now */ + a = mpi_alloc(0); + if( mpi_fromstr( a, buffer ) ) + return GCRYERR_INV_OBJ; + if( ret_mpi ) + *ret_mpi = a; + else + mpi_free(a); + return 0; + } + else + return GCRYERR_INV_ARG; +} + +/**************** + * Write a using format into buffer which has a length of *NBYTES. + * Returns the number of bytes actually written in nbytes. + */ +int +gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, + struct gcry_mpi *a ) +{ + unsigned int nbits = mpi_get_nbits(a); + size_t len; + + if( !nbytes ) + return GCRYERR_INV_ARG; + + len = *nbytes; + if( format == GCRYMPI_FMT_STD ) { + byte *s = buffer; + char *tmp; + int extra = 0; + unsigned int n; + + if( a->sign ) + return GCRYERR_INTERNAL; /* can't handle it yet */ + + tmp = mpi_get_buffer( a, &n, NULL ); + if( n && (*tmp & 0x80) ) { + n++; + extra=1; + } + + if( n > len ) { + m_free(tmp); + return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ + } + if( extra ) + *s++ = 0; + + memcpy( s, tmp, n-extra ); + m_free(tmp); + *nbytes = n; + return 0; + } + else if( format == GCRYMPI_FMT_PGP ) { + unsigned int n = (nbits + 7)/8; + byte *s = buffer; + char *tmp; + + if( a->sign ) + return GCRYERR_INV_ARG; /* pgp format can only handle unsigned */ + + if( n+2 > len ) + return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ + s[0] = nbits >> 8; + s[1] = nbits; + + tmp = mpi_get_buffer( a, &n, NULL ); + memcpy( s+2, tmp, n ); + m_free(tmp); + *nbytes = n+2; + return 0; + } + else if( format == GCRYMPI_FMT_SSH ) { + byte *s = buffer; + char *tmp; + int extra = 0; + unsigned int n; + + if( a->sign ) + return GCRYERR_INTERNAL; /* can't handle it yet */ + + tmp = mpi_get_buffer( a, &n, NULL ); + if( n && (*tmp & 0x80) ) { + n++; + extra=1; + } + + if( n+4 > len ) { + m_free(tmp); + return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ + } + *s++ = n >> 24; + *s++ = n >> 16; + *s++ = n >> 8; + *s++ = n; + if( extra ) + *s++ = 0; + + memcpy( s, tmp, n-extra ); + m_free(tmp); + *nbytes = 4+n; + return 0; + } + else if( format == GCRYMPI_FMT_HEX ) { + byte *s = buffer; + byte *tmp; + int i; + int extra = 0; + unsigned int n=0; + + tmp = mpi_get_buffer( a, &n, NULL ); + if( !n || (*tmp & 0x80) ) + extra=1; + + if( 2*n+3+1 > len ) { + m_free(tmp); + return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ + } + if( a->sign ) + *s++ = '-'; + if( extra ) { + *s++ = '0'; + *s++ = '0'; + } + + for(i=0; i < n; i++ ) { + unsigned int c = tmp[i]; + *s++ = (c >> 4) < 10? '0'+(c>>4) : 'A'+(c>>4)-10 ; + c &= 15; + *s++ = c < 10? '0'+c : 'A'+c-10 ; + } + *s++ = 0; + *nbytes = (char*)s - buffer; + return 0; + } + else + return GCRYERR_INV_ARG; +} + diff --git a/mpi/mpiutil.c b/mpi/mpiutil.c index 317940b5f..381db4804 100644 --- a/mpi/mpiutil.c +++ b/mpi/mpiutil.c @@ -24,10 +24,10 @@ #include <string.h> #include <assert.h> +#include "g10lib.h" #include "mpi.h" #include "mpi-internal.h" #include "memory.h" -#include "g10lib.h" /**************** * Note: It was a bad idea to use the number of limbs to allocate @@ -49,7 +49,6 @@ mpi_alloc( unsigned nlimbs ) a->nlimbs = 0; a->sign = 0; a->flags = 0; - a->nbits = 0; return a; } @@ -73,7 +72,6 @@ mpi_alloc_secure( unsigned nlimbs ) a->flags = 1; a->nlimbs = 0; a->sign = 0; - a->nbits = 0; return a; } @@ -140,7 +138,6 @@ void mpi_clear( MPI a ) { a->nlimbs = 0; - a->nbits = 0; a->flags = 0; } @@ -162,7 +159,6 @@ mpi_free( MPI a ) g10_free(a); } - void mpi_set_secure( MPI a ) { @@ -184,7 +180,7 @@ mpi_set_secure( MPI a ) MPI -mpi_set_opaque( MPI a, void *p, int len ) +mpi_set_opaque( MPI a, void *p, unsigned int nbits ) { if( !a ) { a = mpi_alloc(0); @@ -199,19 +195,19 @@ mpi_set_opaque( MPI a, void *p, int len ) a->d = p; a->alloced = 0; a->nlimbs = 0; - a->nbits = len; + a->sign = nbits; a->flags = 4; return a; } void * -mpi_get_opaque( MPI a, int *len ) +mpi_get_opaque( MPI a, unsigned int *nbits ) { if( !(a->flags & 4) ) log_bug("mpi_get_opaque on normal mpi\n"); - if( len ) - *len = a->nbits; + if( nbits ) + *nbits = a->sign; return a->d; } @@ -227,10 +223,10 @@ mpi_copy( MPI a ) MPI b; if( a && (a->flags & 4) ) { - 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 ); + void *p = g10_is_secure(a->d)? g10_xmalloc_secure( (a->sign+7)/8 ) + : g10_xmalloc( (a->sign+7)/8 ); + memcpy( p, a->d, (a->sign+7)/8 ); + b = mpi_set_opaque( NULL, p, a->sign ); } else if( a ) { b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs ) @@ -238,7 +234,6 @@ mpi_copy( MPI a ) b->nlimbs = a->nlimbs; b->sign = a->sign; b->flags = a->flags; - b->nbits = a->nbits; for(i=0; i < b->nlimbs; i++ ) b->d[i] = a->d[i]; } @@ -259,10 +254,11 @@ mpi_alloc_like( MPI a ) MPI b; if( a && (a->flags & 4) ) { - 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 ); + int n = (a->sign+7)/8; + void *p = g10_is_secure(a->d)? g10_malloc_secure( n ) + : g10_malloc( n ); + memcpy( p, a->d, n ); + b = mpi_set_opaque( NULL, p, a->sign ); } else if( a ) { b = mpi_is_secure(a)? mpi_alloc_secure( a->nlimbs ) @@ -270,7 +266,6 @@ mpi_alloc_like( MPI a ) b->nlimbs = 0; b->sign = 0; b->flags = a->flags; - b->nbits = 0; } else b = NULL; @@ -290,7 +285,6 @@ mpi_set( MPI w, MPI u) up = u->d; MPN_COPY( wp, up, usize ); w->nlimbs = usize; - w->nbits = u->nbits; w->flags = u->flags; w->sign = usign; } @@ -303,7 +297,6 @@ mpi_set_ui( MPI w, unsigned long u) w->d[0] = u; w->nlimbs = u? 1:0; w->sign = 0; - w->nbits = 0; w->flags = 0; } @@ -327,3 +320,72 @@ mpi_swap( MPI a, MPI b) tmp = *a; *a = *b; *b = tmp; } + +GCRY_MPI +gcry_mpi_new( unsigned int nbits ) +{ + return 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 ); +} + +void +gcry_mpi_release( GCRY_MPI a ) +{ + mpi_free( a ); +} + +GCRY_MPI +gcry_mpi_copy( const GCRY_MPI a ) +{ + return 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 ); + return w; +} + +GCRY_MPI +gcry_mpi_set_ui( GCRY_MPI w, unsigned long u ) +{ + if( !w ) + w = mpi_alloc(1); + 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 ); + m_free(p); +} + + |