diff options
author | Werner Koch <wk@gnupg.org> | 1999-12-08 21:03:01 +0000 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 1999-12-08 21:03:01 +0000 |
commit | 2af22dd84e396dc2baf12d37d4642ceba4dffc83 (patch) | |
tree | fc2c24de49bf8615b8a2c0958711cf8d1231fcce | |
parent | 5d68aaccefcb3e9c7ff515096a240712962b72c9 (diff) | |
download | libgcrypt-2af22dd84e396dc2baf12d37d4642ceba4dffc83.tar.gz |
See ChangeLog: Wed Dec 8 21:58:32 CET 1999 Werner Koch
-rw-r--r-- | AUTHORS | 2 | ||||
-rw-r--r-- | THANKS | 4 | ||||
-rw-r--r-- | cipher/ChangeLog | 10 | ||||
-rw-r--r-- | cipher/Makefile.am | 2 | ||||
-rw-r--r-- | cipher/cipher.c | 2 | ||||
-rw-r--r-- | cipher/dsa.c | 8 | ||||
-rw-r--r-- | cipher/dynload.c | 4 | ||||
-rw-r--r-- | cipher/elgamal.c | 20 | ||||
-rw-r--r-- | cipher/md.c | 2 | ||||
-rw-r--r-- | cipher/primegen.c | 10 | ||||
-rw-r--r-- | cipher/pubkey.c | 2 | ||||
-rw-r--r-- | cipher/random.c | 1 | ||||
-rw-r--r-- | cipher/rndegd.c | 5 | ||||
-rw-r--r-- | cipher/rndlinux.c | 1 | ||||
-rw-r--r-- | configure.in | 2 | ||||
-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 | ||||
-rw-r--r-- | src/ChangeLog | 20 | ||||
-rw-r--r-- | src/Makefile.am | 12 | ||||
-rw-r--r-- | src/cipher.h | 73 | ||||
-rw-r--r-- | src/gcrypt.h | 45 | ||||
-rw-r--r-- | src/gcrypt.m4 | 187 | ||||
-rw-r--r-- | src/global.c | 114 | ||||
-rw-r--r-- | src/misc.c | 21 | ||||
-rw-r--r-- | src/mpi.h | 162 | ||||
-rw-r--r-- | src/mpiapi.c | 347 | ||||
-rw-r--r-- | src/testapi.c | 7 | ||||
-rw-r--r-- | src/wrapper.c | 41 |
33 files changed, 1059 insertions, 637 deletions
@@ -67,7 +67,7 @@ jungmann@cwb.matrix.com.br TRANSLATIONS Pedro Morais -??????????. [pt_PT] +Disclaimer. [pt_PT] morais@poli.org @@ -108,8 +108,8 @@ Wim Vandeputte bunbun@reptile.rug.ac.be nbecker@hns.com Thanks to the German Unix User Group for providing FTP space, -Martin Hamilton for hosting the mailing list and HSP for -hosting gnupg.org. +Martin Hamilton for initially hosting the mailing list, HSP for +hosting gnupg.org and Linux Laptops Ltd. for a nice toy. Many thanks to my wife Gerlinde for having so much patience with me while hacking late in the evening. diff --git a/cipher/ChangeLog b/cipher/ChangeLog index bfe180d9..df0eeee1 100644 --- a/cipher/ChangeLog +++ b/cipher/ChangeLog @@ -1,3 +1,13 @@ +Wed Dec 8 21:58:32 CET 1999 Werner Koch <wk@gnupg.de> + + * dsa.c: s/mpi_powm/gcry_mpi_powm/g + * elgamal.c: Ditto. + * primegen.c: Ditto. + + * : Replaced g10_opt_verbose by g10_log_verbosity(). + + * Makefile.am (INCLUDES): removed intl, add ../gcrypt + Fri Nov 19 17:15:20 CET 1999 Werner Koch <wk@gnupg.de> * dynload.c (cmp_filenames): New to replaced compare_filename() in diff --git a/cipher/Makefile.am b/cipher/Makefile.am index 23142d9c..aa766bbc 100644 --- a/cipher/Makefile.am +++ b/cipher/Makefile.am @@ -1,7 +1,7 @@ ## Process this file with automake to produce Makefile.in -INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/intl +INCLUDES = -I$(top_srcdir)/gcrypt noinst_LTLIBRARIES = libcipher.la diff --git a/cipher/cipher.c b/cipher/cipher.c index 1a7a6584..7808d876 100644 --- a/cipher/cipher.c +++ b/cipher/cipher.c @@ -193,7 +193,7 @@ load_cipher_modules(void) continue; } /* put it into the table */ - if( g10_opt_verbose > 1 ) + if( g10_log_verbosity( 2 ) ) log_info("loaded cipher %d (%s)\n", ct->algo, name); ct->name = name; ct_idx++; diff --git a/cipher/dsa.c b/cipher/dsa.c index 903625c1..1f132ae0 100644 --- a/cipher/dsa.c +++ b/cipher/dsa.c @@ -178,7 +178,7 @@ generate( DSA_secret_key *sk, unsigned nbits, MPI **ret_factors ) do { mpi_add_ui( h, h, 1 ); /* g = h^e mod p */ - mpi_powm( g, h, e, p ); + gcry_mpi_powm( g, h, e, p ); } while( !mpi_cmp_ui( g, 1 ) ); /* continue until g != 1 */ /* select a random number which has these properties: @@ -212,7 +212,7 @@ generate( DSA_secret_key *sk, unsigned nbits, MPI **ret_factors ) /* y = g^x mod p */ y = mpi_alloc( mpi_get_nlimbs(p) ); - mpi_powm( y, g, x, p ); + gcry_mpi_powm( y, g, x, p ); if( DBG_CIPHER ) { progress('\n'); @@ -246,7 +246,7 @@ check_secret_key( DSA_secret_key *sk ) int rc; MPI y = mpi_alloc( mpi_get_nlimbs(sk->y) ); - mpi_powm( y, sk->g, sk->x, sk->p ); + gcry_mpi_powm( y, sk->g, sk->x, sk->p ); rc = !mpi_cmp( y, sk->y ); mpi_free( y ); return rc; @@ -269,7 +269,7 @@ sign(MPI r, MPI s, MPI hash, DSA_secret_key *skey ) k = gen_k( skey->q ); /* r = (a^k mod p) mod q */ - mpi_powm( r, skey->g, k, skey->p ); + gcry_mpi_powm( r, skey->g, k, skey->p ); mpi_fdiv_r( r, r, skey->q ); /* kinv = k^(-1) mod q */ diff --git a/cipher/dynload.c b/cipher/dynload.c index 262325c7..d2c40b3f 100644 --- a/cipher/dynload.c +++ b/cipher/dynload.c @@ -270,7 +270,7 @@ load_extension( EXTLIST el ) name = (char**)addr; #endif - if( g10_opt_verbose > 1 ) + if( g10_log_verbosity( 2 ) ) log_info("%s: %s%s%s%s\n", el->name, *name, el->hintstr? " (":"", el->hintstr? el->hintstr:"", @@ -301,7 +301,7 @@ load_extension( EXTLIST el ) #endif #ifdef HAVE_DL_DLOPEN - if( g10_opt_verbose > 2 ) { + if( g10_log_verbosity( 3 ) ) { /* list the contents of the module */ while( (sym = (*el->enumfunc)(0, &seq, &class, &vers)) ) { if( vers != 1 ) { diff --git a/cipher/elgamal.c b/cipher/elgamal.c index f88aa91d..02995e02 100644 --- a/cipher/elgamal.c +++ b/cipher/elgamal.c @@ -229,7 +229,7 @@ generate( ELG_secret_key *sk, unsigned nbits, MPI **ret_factors ) g10_free(rndbuf); y = mpi_alloc(nbits/BITS_PER_MPI_LIMB); - mpi_powm( y, g, x, p ); + gcry_mpi_powm( y, g, x, p ); if( DBG_CIPHER ) { progress('\n'); @@ -263,7 +263,7 @@ check_secret_key( ELG_secret_key *sk ) int rc; MPI y = mpi_alloc( mpi_get_nlimbs(sk->y) ); - mpi_powm( y, sk->g, sk->x, sk->p ); + gcry_mpi_powm( y, sk->g, sk->x, sk->p ); rc = !mpi_cmp( y, sk->y ); mpi_free( y ); return rc; @@ -281,13 +281,13 @@ encrypt(MPI a, MPI b, MPI input, ELG_public_key *pkey ) */ k = gen_k( pkey->p ); - mpi_powm( a, pkey->g, k, pkey->p ); + gcry_mpi_powm( a, pkey->g, k, pkey->p ); /* b = (y^k * input) mod p * = ((y^k mod p) * (input mod p)) mod p * and because input is < p * = ((y^k mod p) * input) mod p */ - mpi_powm( b, pkey->y, k, pkey->p ); + gcry_mpi_powm( b, pkey->y, k, pkey->p ); mpi_mulm( b, b, input, pkey->p ); #if 0 if( DBG_CIPHER ) { @@ -312,7 +312,7 @@ decrypt(MPI output, MPI a, MPI b, ELG_secret_key *skey ) /* output = b/(a^x) mod p */ - mpi_powm( t1, a, skey->x, skey->p ); + gcry_mpi_powm( t1, a, skey->x, skey->p ); mpi_invm( t1, t1, skey->p ); mpi_mulm( output, b, t1, skey->p ); #if 0 @@ -348,7 +348,7 @@ sign(MPI a, MPI b, MPI input, ELG_secret_key *skey ) */ mpi_sub_ui(p_1, p_1, 1); k = gen_k( skey->p ); - mpi_powm( a, skey->g, k, skey->p ); + gcry_mpi_powm( a, skey->g, k, skey->p ); mpi_mul(t, skey->x, a ); mpi_subm(t, input, t, p_1 ); while( mpi_is_neg(t) ) { @@ -397,12 +397,12 @@ verify(MPI a, MPI b, MPI input, ELG_public_key *pkey ) #if 0 /* t1 = (y^a mod p) * (a^b mod p) mod p */ - mpi_powm( t1, pkey->y, a, pkey->p ); - mpi_powm( t2, a, b, pkey->p ); + gcry_mpi_powm( t1, pkey->y, a, pkey->p ); + gcry_mpi_powm( t2, a, b, pkey->p ); mpi_mulm( t1, t1, t2, pkey->p ); /* t2 = g ^ input mod p */ - mpi_powm( t2, pkey->g, input, pkey->p ); + gcry_mpi_powm( t2, pkey->g, input, pkey->p ); rc = !mpi_cmp( t1, t2 ); #elif 0 @@ -413,7 +413,7 @@ verify(MPI a, MPI b, MPI input, ELG_public_key *pkey ) mpi_mulpowm( t1, base, exp, pkey->p ); /* t2 = g ^ input mod p */ - mpi_powm( t2, pkey->g, input, pkey->p ); + gcry_mpi_powm( t2, pkey->g, input, pkey->p ); rc = !mpi_cmp( t1, t2 ); #else diff --git a/cipher/md.c b/cipher/md.c index bc9c6e86..bc0a6c30 100644 --- a/cipher/md.c +++ b/cipher/md.c @@ -157,7 +157,7 @@ load_digest_module( int req_algo ) continue; } /* put it into the list */ - if( g10_opt_verbose > 1 ) + if( g10_log_verbosity( 2 ) ) log_info("loaded digest %d\n", algo); r->next = digest_list; digest_list = r; diff --git a/cipher/primegen.c b/cipher/primegen.c index cb7327a4..1ad16333 100644 --- a/cipher/primegen.c +++ b/cipher/primegen.c @@ -249,7 +249,7 @@ generate_elg_prime( int mode, unsigned pbits, unsigned qbits, /*fputc('~', stderr);*/ mpi_fdiv_q(tmp, pmin1, factors[i] ); /* (no mpi_pow(), but it is okay to use this with mod prime) */ - mpi_powm(b, g, tmp, prime ); + gcry_mpi_powm(b, g, tmp, prime ); if( !mpi_cmp_ui(b, 1) ) break; } @@ -334,7 +334,7 @@ gen_prime( unsigned nbits, int secret, int randomlevel ) /* do a faster Fermat test */ count2++; mpi_sub_ui( pminus1, ptest, 1); - mpi_powm( result, val_2, pminus1, ptest ); + gcry_mpi_powm( result, val_2, pminus1, ptest ); if( !mpi_cmp_ui( result, 1 ) ) { /* not composite */ /* perform stronger tests */ if( is_prime(ptest, 5, &count2 ) ) { @@ -383,7 +383,7 @@ check_prime( MPI prime, MPI val_2 ) MPI result = mpi_alloc_like( prime ); MPI pminus1 = mpi_alloc_like( prime ); mpi_sub_ui( pminus1, prime, 1); - mpi_powm( result, val_2, pminus1, prime ); + gcry_mpi_powm( result, val_2, pminus1, prime ); mpi_free( pminus1 ); if( mpi_cmp_ui( result, 1 ) ) { /* if composite */ mpi_free( result ); @@ -443,10 +443,10 @@ is_prime( MPI n, int steps, int *count ) } assert( mpi_cmp( x, nminus1 ) < 0 && mpi_cmp_ui( x, 1 ) > 0 ); } - mpi_powm( y, x, q, n); + gcry_mpi_powm( y, x, q, n); if( mpi_cmp_ui(y, 1) && mpi_cmp( y, nminus1 ) ) { for( j=1; j < k && mpi_cmp( y, nminus1 ); j++ ) { - mpi_powm(y, y, a2, n); + gcry_mpi_powm(y, y, a2, n); if( !mpi_cmp_ui( y, 1 ) ) goto leave; /* not a prime */ } diff --git a/cipher/pubkey.c b/cipher/pubkey.c index 49f4773e..4b2c55e4 100644 --- a/cipher/pubkey.c +++ b/cipher/pubkey.c @@ -256,7 +256,7 @@ load_pubkey_modules(void) if( !ct->verify ) ct->verify = dummy_verify; if( !ct->get_nbits ) ct->get_nbits= dummy_get_nbits; /* put it into the table */ - if( g10_opt_verbose > 1 ) + if( g10_log_verbosity( 2 ) ) log_info("loaded pubkey %d (%s)\n", ct->algo, name); ct->name = name; ct_idx++; diff --git a/cipher/random.c b/cipher/random.c index 78c9ecda..38991a18 100644 --- a/cipher/random.c +++ b/cipher/random.c @@ -47,7 +47,6 @@ #endif #include "g10lib.h" #include "rmd.h" -#include "ttyio.h" #include "random.h" #include "rand-internal.h" #include "dynload.h" diff --git a/cipher/rndegd.c b/cipher/rndegd.c index 36c8b4e3..4d5f0ef3 100644 --- a/cipher/rndegd.c +++ b/cipher/rndegd.c @@ -32,7 +32,9 @@ #include <sys/un.h> #include "types.h" #include "g10lib.h" +#ifndef IS_MODULE #include "ttyio.h" +#endif #include "dynload.h" #include "cipher.h" @@ -141,7 +143,8 @@ gather_random( void (*add)(const void*, size_t, int), int requester, } } if( fd == -1 ) { - char *name = my_make_filename( g10_opt_homedir, "entropy", NULL ); + #warning Fixme: make the filename configurable + char *name = my_make_filename( "~/.gnupg-test", "entropy", NULL ); struct sockaddr_un addr; int addr_len; diff --git a/cipher/rndlinux.c b/cipher/rndlinux.c index d25abcd3..bca596fd 100644 --- a/cipher/rndlinux.c +++ b/cipher/rndlinux.c @@ -42,7 +42,6 @@ #endif #include "types.h" #include "g10lib.h" -#include "ttyio.h" #include "dynload.h" static int open_device( const char *name, int minor ); diff --git a/configure.in b/configure.in index 0bd7ecfa..e553917a 100644 --- a/configure.in +++ b/configure.in @@ -700,6 +700,7 @@ else mv g10defs.tmp g10defs.h echo "g10defs.h created" fi +chmod +x gcrypt/gcrypt-config ],[ prefix=$prefix exec_prefix=$exec_prefix @@ -723,6 +724,7 @@ tools/Makefile zlib/Makefile checks/Makefile gcrypt/Makefile +gcrypt/gcrypt-config ]) dnl *-*wedit:notab*-* Please keep this as the last line. diff --git a/mpi/ChangeLog b/mpi/ChangeLog index 3a0fca1a..81fec2a0 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 ef9816aa..98ad5fcc 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 c43e067a..00000000 --- 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 f1eff863..45ca029e 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 e8d55f9b..a57eff87 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 25d37ec4..352468cb 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 317940b5..381db480 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); +} + + diff --git a/src/ChangeLog b/src/ChangeLog index 128e6af8..d59feab9 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,23 @@ +Wed Dec 8 21:58:32 CET 1999 Werner Koch <wk@gnupg.de> + + * gcrypt.m4: New. + * gcrypt-config: New. + + * mpi.h (mpi_get_nbit_info): Removed + (mpi_set_nbit_info): Removed. + (struct gcry_mpi): Removed the nbits field. + + * misc.c (g10_log_verbosity): New. + + * global.c (g10_xstrdup): New. + + * mpiapi.c: Removed. + + * mpi.h: Moved from ../include to here. Removed some obsolete + prototypes and the iobuf.h header. + * cipher.h: Moved from ../include to here. Removed the mpi.h header. + * g10lib.h: Moved from ../include to here. + Fri Nov 19 17:15:20 CET 1999 Werner Koch <wk@gnupg.de> * sexp.c (dump_string): New. Taken from gnupg/util/miscutil.c. diff --git a/src/Makefile.am b/src/Makefile.am index adcc0099..74862a24 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,18 +5,26 @@ INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/intl lib_LTLIBRARIES = libgcrypt.la +bin_SCRIPTS = gcrypt-config + +m4datadir = $(datadir)/aclocal +m4data_DATA = gcrypt.m4 + noinst_PROGRAMS = testapi #sexp_SOURCES = sexp.c mpiapi.c #sexp_LDADD = ../cipher/libcipher.la ../mpi/libmpi.la ../util/libutil.la ./libgcrypt.la @INTLLIBS@ testapi_SOURCES = testapi.c testapi_LDADD = libgcrypt.la +include_HEADERS = gcrypt.h + libgcrypt_la_LDFLAGS = -version-info 0:0:0 -export-symbols libgcrypt.sym -libgcrypt_la_SOURCES = gcrypt.h \ +libgcrypt_la_SOURCES = mpi.h \ + cipher.h \ misc.c \ global.c \ sexp.c \ - mpiapi.c \ + wrapper.c \ memory.c \ secmem.c diff --git a/src/cipher.h b/src/cipher.h new file mode 100644 index 00000000..b9bbd085 --- /dev/null +++ b/src/cipher.h @@ -0,0 +1,73 @@ +/* cipher.h + * 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 + */ +#ifndef G10_CIPHER_H +#define G10_CIPHER_H + +#define DBG_CIPHER g10_get_debug_flag( 1 ) + +#include "../cipher/random.h" + + +#define CIPHER_ALGO_NONE 0 +#define CIPHER_ALGO_IDEA 1 +#define CIPHER_ALGO_3DES 2 +#define CIPHER_ALGO_CAST5 3 +#define CIPHER_ALGO_BLOWFISH 4 /* blowfish 128 bit key */ +#define CIPHER_ALGO_SAFER_SK128 5 +#define CIPHER_ALGO_DES_SK 6 +#define CIPHER_ALGO_TWOFISH 10 /* twofish 256 bit */ +#define CIPHER_ALGO_SKIPJACK 101 /* experimental: skipjack */ +#define CIPHER_ALGO_TWOFISH_OLD 102 /* experimental: twofish 128 bit */ +#define CIPHER_ALGO_DUMMY 110 /* no encryption at all */ + +#define PUBKEY_ALGO_RSA 1 +#define PUBKEY_ALGO_RSA_E 2 /* RSA encrypt only */ +#define PUBKEY_ALGO_RSA_S 3 /* RSA sign only */ +#define PUBKEY_ALGO_ELGAMAL_E 16 /* encrypt only ElGamal (but not for v3)*/ +#define PUBKEY_ALGO_DSA 17 +#define PUBKEY_ALGO_ELGAMAL 20 /* sign and encrypt elgamal */ + +#if 0 +#define PUBKEY_USAGE_SIG 1 /* key is good for signatures */ +#define PUBKEY_USAGE_ENC 2 /* key is good for encryption */ +#endif + +#define DIGEST_ALGO_MD5 1 +#define DIGEST_ALGO_SHA1 2 +#define DIGEST_ALGO_RMD160 3 +#define DIGEST_ALGO_TIGER 6 + +#define is_RSA(a) ((a)==PUBKEY_ALGO_RSA || (a)==PUBKEY_ALGO_RSA_E \ + || (a)==PUBKEY_ALGO_RSA_S ) +#define is_ELGAMAL(a) ((a)==PUBKEY_ALGO_ELGAMAL || (a)==PUBKEY_ALGO_ELGAMAL_E) + + +/*-- dynload.c --*/ +void register_cipher_extension( const char *mainpgm, const char *fname ); + +/*-- rmd160.c --*/ +void rmd160_hash_buffer( char *outbuf, const char *buffer, size_t length ); + + +/*-- smallprime.c --*/ +extern ushort small_prime_numbers[]; + + +#endif /*G10_CIPHER_H*/ diff --git a/src/gcrypt.h b/src/gcrypt.h index 85de69cd..be58ce7e 100644 --- a/src/gcrypt.h +++ b/src/gcrypt.h @@ -27,14 +27,17 @@ extern "C" { #endif -#ifndef GCRYPT_NO_MPI_MACROS -#define GCRYPT_NO_MPI_MACROS -#endif #ifndef HAVE_BYTE_TYPEDEF - #undef byte /* maybe there is a macro with this name */ +# undef byte /* maybe there is a macro with this name */ typedef unsigned char byte; - #define HAVE_BYTE_TYPEDEF +# define HAVE_BYTE_TYPEDEF +#endif + +#ifdef _GCRYPT_IN_LIBGCRYPT +# ifndef GCRYPT_NO_MPI_MACROS +# define GCRYPT_NO_MPI_MACROS 1 +# endif #endif struct gcry_mpi; @@ -77,6 +80,7 @@ enum { GCRYERR_NO_OBJ = 68, /* Missign item in an object */ }; +const char *gcry_check_version( const char *req_version ); int gcry_errno(void); const char *gcry_strerror( int ec ); @@ -96,10 +100,14 @@ enum gcry_ctl_cmds { GCRYCTL_DISABLE_ALGO = 12, GCRYCTL_DUMP_RANDOM_STATS = 13, GCRYCTL_DUMP_SECMEM_STATS = 14, - GCRYCTL_GET_ALGO_NPKEY = 15, - GCRYCTL_GET_ALGO_NSKEY = 16, - GCRYCTL_GET_ALGO_NSIGN = 17, - GCRYCTL_GET_ALGO_NENCR = 18, + GCRYCTL_GET_ALGO_NPKEY = 15, + GCRYCTL_GET_ALGO_NSKEY = 16, + GCRYCTL_GET_ALGO_NSIGN = 17, + GCRYCTL_GET_ALGO_NENCR = 18, + GCRYCTL_SET_VERBOSITY = 19, + GCRYCTL_SET_DEBUG_FLAGS = 20, + GCRYCTL_CLEAR_DEBUG_FLAGS = 21, + GCRYCTL_USE_SECURE_RNDPOOL= 22, }; int gcry_control( enum gcry_ctl_cmds, ... ); @@ -171,6 +179,12 @@ enum gcry_mpi_format { }; +enum gcry_mpi_flag { + GCRYMPI_FLAG_SECURE = 1, + GCRYMPI_FLAG_OPAQUE = 2, +}; + + GCRY_MPI gcry_mpi_new( unsigned int nbits ); GCRY_MPI gcry_mpi_snew( unsigned int nbits ); @@ -189,6 +203,11 @@ int gcry_mpi_print( enum gcry_mpi_format format, void gcry_mpi_powm( GCRY_MPI w, const GCRY_MPI b, const GCRY_MPI e, const GCRY_MPI m ); +GCRY_MPI gcry_mpi_set_opaque( GCRY_MPI a, void *p, unsigned int nbits ); +void *gcry_mpi_get_opaque( GCRY_MPI a, unsigned int *nbits ); +void gcry_mpi_set_flag( GCRY_MPI a, enum gcry_mpi_flag flag ); +void gcry_mpi_clear_flag( GCRY_MPI a, enum gcry_mpi_flag flag ); +int gcry_mpi_get_flag( GCRY_MPI a, enum gcry_mpi_flag flag ); #ifndef GCRYPT_NO_MPI_MACROS @@ -203,6 +222,8 @@ void gcry_mpi_powm( GCRY_MPI w, #define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) ) #define mpi_powm(w,b,e,m) gcry_mpi_powm( (w), (b), (e), (m) ) +#define mpi_set_opaque(a,b,c) gcry_mpi_set_opaque( (a), (b), (c) ) +#define mpi_get_opaque(a,b) gcry_mpi_get_opaque( (a), (b) ) #endif /* GCRYPT_NO_MPI_MACROS */ /******************************************** @@ -415,9 +436,11 @@ char *gcry_xstrdup( const char * a); void gcry_free( void *p ); - #ifndef GCRYPT_NO_MPI_MACROS - typedef struct gcry_mpi *MPI; +# ifndef DID_MPI_TYPEDEF + typedef struct gcry_mpi *MPI; +# define DID_MPI_TYPEDEF +# endif #endif /* GCRYPT_NO_MPI_MACROS */ #ifdef __cplusplus diff --git a/src/gcrypt.m4 b/src/gcrypt.m4 new file mode 100644 index 00000000..fd1ee889 --- /dev/null +++ b/src/gcrypt.m4 @@ -0,0 +1,187 @@ +dnl Autoconf macros for libgcrypt + +# Configure paths for GCRYPT +# Shamelessly stolen from the one of XDELTA by Owen Taylor +# Werner Koch 99-12-08 + +dnl AM_PATH_GCRYPT([MINIMUM-VERSION, [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND ]]]) +dnl Test for gcrypt, and define GCRYPT_CFLAGS and GCRYPT_LIBS +dnl +AC_DEFUN(AM_PATH_GCRYPT, +[dnl +dnl Get the cflags and libraries from the gcrypt-config script +dnl +AC_ARG_WITH(gcrypt-prefix, + [ --with-gcrypt-prefix=PFX Prefix where gcrypt is installed (optional)], + gcrypt_config_prefix="$withval", gcrypt_config_prefix="") +AC_ARG_ENABLE(gcrypttest, + [ --disable-gcrypttest Do not try to compile and run a test gcrypt program], + , enable_gcrypttest=yes) + + if test x$gcrypt_config_prefix != x ; then + gcrypt_config_args="$gcrypt_config_args --prefix=$gcrypt_config_prefix" + if test x${GCRYPT_CONFIG+set} != xset ; then + GCRYPT_CONFIG=$gcrypt_config_prefix/bin/gcrypt-config + fi + fi + + AC_PATH_PROG(GCRYPT_CONFIG, gcrypt-config, no) + min_gcrypt_version=ifelse([$1], ,1.1.0,$1) + AC_MSG_CHECKING(for gcrypt - version >= $min_gcrypt_version) + no_gcrypt="" + if test "$GCRYPT_CONFIG" = "no" ; then + no_gcrypt=yes + else + GCRYPT_CFLAGS=`$GCRYPT_CONFIG $gcrypt_config_args --cflags` + GCRYPT_LIBS=`$GCRYPT_CONFIG $gcrypt_config_args --libs` + gcrypt_config_major_version=`$GCRYPT_CONFIG $gcrypt_config_args --version | \ + sed 's/.* \([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\1/'` + gcrypt_config_minor_version=`$GCRYPT_CONFIG $gcrypt_config_args --version | \ + sed 's/.* \([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\2/'` + gcrypt_config_micro_version=`$GCRYPT_CONFIG $gcrypt_config_args --version | \ + sed 's/.* \([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\3/'` + if test "x$enable_gcrypttest" = "xyes" ; then + ac_save_CFLAGS="$CFLAGS" + ac_save_LIBS="$LIBS" + CFLAGS="$CFLAGS $GCRYPT_CFLAGS" + LIBS="$LIBS $GCRYPT_LIBS" +dnl +dnl Now check if the installed gcrypt is sufficiently new. Also sanity +dnl checks the results of gcrypt-config to some extent +dnl + rm -f conf.gcrypttest + AC_TRY_RUN([ +#include <gcrypt.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int +main () +{ + int major, minor, micro; + unsigned int major_gcrypt, minor_gcrypt, micro_gcrypt, patlvl_gcrypt; + char *tmp_version; + char ver_string[20]; + + system ("touch conf.gcrypttest"); + + /* HP/UX 9 (%@#!) writes to sscanf strings */ + tmp_version = strdup("$min_gcrypt_version"); + if( !tmp_version ) + exit(1); + if (sscanf(tmp_version, "%d.%d.%d", &major, &minor, µ) != 3) { + printf("%s, bad version string\n", "$min_gcrypt_version"); + exit(1); + } + + sprintf( ver_string, "%lX", gcrypt_version() ); + if ( sscanf(ver_string, "%1x%2x%1x%2x", + &major_gcrypt, &minor_gcrypt, &patlvl_gcrypt, µ_gcrypt) != 4) { + printf("%s, gcrypt returned bad version string\n", ver_string ); + exit(1); + } + + if ((major_gcrypt != $gcrypt_config_major_version) || + (minor_gcrypt != $gcrypt_config_minor_version) || + (micro_gcrypt != $gcrypt_config_micro_version)) + { + printf("\n*** 'gcrypt-config --version' returned %d.%d.%d, but GCRYPT (%u.%u.%u)\n", + $gcrypt_config_major_version, $gcrypt_config_minor_version, $gcrypt_config_micro_version, + major_gcrypt, minor_gcrypt, micro_gcrypt); + printf("*** was found! If gcrypt-config was correct, then it is best\n"); + printf("*** to remove the old version of GCRYPT. You may also be able to fix the error\n"); + printf("*** by modifying your LD_LIBRARY_PATH enviroment variable, or by editing\n"); + printf("*** /etc/ld.so.conf. Make sure you have run ldconfig if that is\n"); + printf("*** required on your system.\n"); + printf("*** If gcrypt-config was wrong, set the environment variable GCRYPT_CONFIG\n"); + printf("*** to point to the correct copy of gcrypt-config, and remove the file config.cache\n"); + printf("*** before re-running configure\n"); + } + else if ( gcrypt_version() != GCRYPT_VERSION ) + { + printf("*** GCRYPT header file (version %lx) does not match\n", GCRYPT_VERSION); + printf("*** library (version %lx)\n", gcrypt_version() ); + } + else + { + if ((major_gcrypt > major) || + ((major_gcrypt == major) && (minor_gcrypt > minor)) || + ((major_gcrypt == major) && (minor_gcrypt == minor) && (micro_gcrypt >= micro))) + { + return 0; + } + else + { + printf("\n*** An old version of GCRYPT (%u.%u.%u) was found.\n", + major_gcrypt, minor_gcrypt, micro_gcrypt); + printf("*** You need a version of GCRYPT newer than %d.%d.%d. The latest version of\n", + major, minor, micro); + printf("*** GCRYPT is always available from ftp://ftp.gnupg.org/pub/gcrypt/gnupg.\n"); + printf("*** (It comes along with GnuPG).\n"); + printf("*** \n"); + printf("*** If you have already installed a sufficiently new version, this error\n"); + printf("*** probably means that the wrong copy of the gcrypt-config shell script is\n"); + printf("*** being found. The easiest way to fix this is to remove the old version\n"); + printf("*** of GCRYPT, but you can also set the GCRYPT_CONFIG environment to point to the\n"); + printf("*** correct copy of gcrypt-config. (In this case, you will have to\n"); + printf("*** modify your LD_LIBRARY_PATH enviroment variable, or edit /etc/ld.so.conf\n"); + printf("*** so that the correct libraries are found at run-time))\n"); + } + } + return 1; +} +],, no_gcrypt=yes,[echo $ac_n "cross compiling; assumed OK... $ac_c"]) + CFLAGS="$ac_save_CFLAGS" + LIBS="$ac_save_LIBS" + fi + fi + if test "x$no_gcrypt" = x ; then + AC_MSG_RESULT(yes) + ifelse([$2], , :, [$2]) + else + AC_MSG_RESULT(no) + if test "$GCRYPT_CONFIG" = "no" ; then + echo "*** The gcrypt-config script installed by GCRYPT could not be found" + echo "*** If GCRYPT was installed in PREFIX, make sure PREFIX/bin is in" + echo "*** your path, or set the GCRYPT_CONFIG environment variable to the" + echo "*** full path to gcrypt-config." + else + if test -f conf.gcrypttest ; then + : + else + echo "*** Could not run gcrypt test program, checking why..." + CFLAGS="$CFLAGS $GCRYPT_CFLAGS" + LIBS="$LIBS $GCRYPT_LIBS" + AC_TRY_LINK([ +#include <gcrypt.h> +#include <stdio.h> +], [ return !!gcrypt_version(); ], + [ echo "*** The test program compiled, but did not run. This usually means" + echo "*** that the run-time linker is not finding GCRYPT or finding the wrong" + echo "*** version of GCRYPT. If it is not finding GCRYPT, you'll need to set your" + echo "*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point" + echo "*** to the installed location Also, make sure you have run ldconfig if that" + echo "*** is required on your system" + echo "***" + echo "*** If you have an old version installed, it is best to remove it, although" + echo "*** you may also be able to get things to work by modifying LD_LIBRARY_PATH" + echo "***" ], + [ echo "*** The test program failed to compile or link. See the file config.log for the" + echo "*** exact error that occured. This usually means GCRYPT was incorrectly installed" + echo "*** or that you have moved GCRYPT since it was installed. In the latter case, you" + echo "*** may want to edit the gcrypt-config script: $GCRYPT_CONFIG" ]) + CFLAGS="$ac_save_CFLAGS" + LIBS="$ac_save_LIBS" + fi + fi + GCRYPT_CFLAGS="" + GCRYPT_LIBS="" + ifelse([$3], , :, [$3]) + fi + AC_SUBST(GCRYPT_CFLAGS) + AC_SUBST(GCRYPT_LIBS) + rm -f conf.gcrypttest +]) + +dnl *-*wedit:notab*-* Please keep this as the last line. diff --git a/src/global.c b/src/global.c index 06d62f7e..1de2bd62 100644 --- a/src/global.c +++ b/src/global.c @@ -23,11 +23,17 @@ #include <stdlib.h> #include <string.h> #include <stdarg.h> +#include <ctype.h> #include <assert.h> #include "g10lib.h" #include "memory.h" /* for the m_* functions */ +/**************** + * flag bits: 0 : general cipher debug + * 1 : general MPI debug + */ +static unsigned int debug_flags; static int last_ec; /* fixme: make thread safe */ static void *(*alloc_func)(size_t n) = NULL; @@ -38,9 +44,83 @@ static void (*free_func)(void*) = NULL; static int (*outofcore_handler)( void*, size_t, unsigned int ) = NULL; static void *outofcore_handler_value = NULL; +static const char* +parse_version_number( const char *s, int *number ) +{ + int val = 0; + + if( *s == '0' && isdigit(s[1]) ) + return NULL; /* leading zeros are not allowed */ + for ( ; isdigit(*s); s++ ) { + val *= 10; + val += *s - '0'; + } + *number = val; + return val < 0? NULL : s; +} + + +static const char * +parse_version_string( const char *s, int *major, int *minor, int *micro ) +{ + s = parse_version_number( s, major ); + if( !s || *s != '.' ) + return NULL; + s++; + s = parse_version_number( s, minor ); + if( !s || *s != '.' ) + return NULL; + s++; + s = parse_version_number( s, micro ); + if( !s ) + return NULL; + return s; /* patchlevel */ +} + +/**************** + * Check that the the version of the library is at minimum the requested one + * and return the version string; return NULL if the condition is not + * satisfied. If a NULL is passed to thsi function, no check is done, + * but the version string is simpley returned. + */ +const char * +gcry_check_version( const char *req_version ) +{ + const char *ver = VERSION; + int my_major, my_minor, my_micro; + int rq_major, rq_minor, rq_micro; + const char *my_plvl, *rq_plvl; + + if ( !req_version ) + return ver; + + my_plvl = parse_version_string( ver, &my_major, &my_minor, &my_micro ); + if ( !my_plvl ) + return NULL; /* very strange our own version is bogus */ + rq_plvl = parse_version_string( req_version, &rq_major, &rq_minor, + &rq_micro ); + if ( !rq_plvl ) + return NULL; /* req version string is invalid */ + + if ( my_major > rq_major + || (my_major == rq_major && my_minor > rq_minor) + || (my_major == rq_major && my_minor == rq_minor + && my_micro > rq_micro) + || (my_major == rq_major && my_minor == rq_minor + && my_micro == rq_micro + && strcmp( my_plvl, rq_plvl ) >= 0) ) { + return ver; + } + return NULL; +} + + int gcry_control( enum gcry_ctl_cmds cmd, ... ) { + va_list arg_ptr ; + + va_start( arg_ptr, cmd ) ; switch( cmd ) { #if 0 case GCRYCTL_NO_MEM_IS_FATAL: @@ -56,9 +136,27 @@ gcry_control( enum gcry_ctl_cmds cmd, ... ) secmem_dump_stats(); break; + case GCRYCTL_USE_SECURE_RNDPOOL: + secure_random_alloc(); /* put random number into secure memory */ + break; + + case GCRYCTL_SET_VERBOSITY: + g10_set_log_verbosity( va_arg( arg_ptr, int ) ); + break; + + case GCRYCTL_SET_DEBUG_FLAGS: + debug_flags |= va_arg( arg_ptr, unsigned int ); + break; + + case GCRYCTL_CLEAR_DEBUG_FLAGS: + debug_flags &= ~va_arg( arg_ptr, unsigned int ); + break; + default: + va_end(arg_ptr); return GCRYERR_INV_OP; } + va_end(arg_ptr); return 0; } @@ -291,3 +389,19 @@ g10_xcalloc_secure( size_t n, size_t m ) return p; } +char * +g10_xstrdup( const char *string ) +{ + void *p = g10_xmalloc( strlen(string)+1 ); + strcpy( p, string ); + return p; +} + + +int +g10_get_debug_flag( unsigned int mask ) +{ + return debug_flags & mask; +} + + @@ -1,5 +1,5 @@ -/* misc.c - symmetric cipher function interface - * Copyright (C) 1998 Free Software Foundation, Inc. +/* misc.c + * Copyright (C) 1999 Free Software Foundation, Inc. * * This file is part of GnuPG. * @@ -28,6 +28,8 @@ #include "g10lib.h" +static int verbosity_level = 0; /* fixme: add a function to set it */ + static void (*fatal_error_handler)(void*,int, const char*) = NULL; static void *fatal_error_handler_value = 0; static void (*log_handler)(void*,int, const char*, va_list) = NULL; @@ -92,6 +94,17 @@ gcry_set_log_handler( void (*logf)(void*,int, const char*, va_list ), log_handler_value = opaque; } +void +g10_set_log_verbosity( int level ) +{ + verbosity_level = level; +} + +int +g10_log_verbosity( int level ) +{ + return verbosity_level >= level; +} /**************** * This is our log function which prints all log messages to stderr or @@ -139,7 +152,7 @@ g10_bug( const char *file, int line, const char *func ) { g10_log( GCRY_LOG_BUG, ("... this is a bug (%s:%d:%s)\n"), file, line, func ); - abort(); /* never called, bugs it makes the compiler happy */ + abort(); /* never called, but it makes the compiler happy */ } #else void @@ -147,7 +160,7 @@ g10_bug( const char *file, int line ) { g10_log( GCRY_LOG_BUG, _("you found a bug ... (%s:%d)\n"), file, line); - abort(); /* never called, bugs it makes the compiler happy */ + abort(); /* never called, but it makes the compiler happy */ } #endif diff --git a/src/mpi.h b/src/mpi.h new file mode 100644 index 00000000..f92a1f8b --- /dev/null +++ b/src/mpi.h @@ -0,0 +1,162 @@ +/* mpi.h - Multi Precision Integers + * Copyright (C) 1994, 1996, 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 + * + * Note: This code is heavily based on the GNU MP Library. + * Actually it's the same code with only minor changes in the + * way the data is stored; this is to support the abstraction + * of an optional secure memory allocation which may be used + * to avoid revealing of sensitive data due to paging etc. + * The GNU MP Library itself is published under the LGPL; + * however I decided to publish this code under the plain GPL. + */ + +#ifndef G10_MPI_H +#define G10_MPI_H + +#include <stdio.h> +#include "types.h" +#include "memory.h" + + +#define DBG_MPI g10_get_debug_flag( 2 ); + +#define BITS_PER_MPI_LIMB (8*SIZEOF_UNSIGNED_LONG) +#define BYTES_PER_MPI_LIMB SIZEOF_UNSIGNED_LONG +typedef unsigned long int mpi_limb_t; +typedef signed long int mpi_limb_signed_t; + +struct gcry_mpi { + int alloced; /* array size (# of allocated limbs) */ + int nlimbs; /* number of valid limbs */ + int sign; /* indicates a negative number and is used for opaque + * MPIs to store the length */ + unsigned flags; /* bit 0: array must be allocated in secure memory space */ + /* bit 2: the limb is a pointer to some m_alloced data */ + mpi_limb_t *d; /* array with the limbs */ +}; + +#ifndef DID_MPI_TYPEDEF + typedef struct gcry_mpi *MPI; + #define DID_MPI_TYPEDEF +#endif + +#define MPI_NULL NULL + +#define mpi_get_nlimbs(a) ((a)->nlimbs) +#define mpi_is_neg(a) ((a)->sign) + +/*-- mpiutil.c --*/ + +#ifdef M_DEBUG + #define mpi_alloc(n) mpi_debug_alloc((n), M_DBGINFO( __LINE__ ) ) + #define mpi_alloc_secure(n) mpi_debug_alloc_secure((n), M_DBGINFO( __LINE__ ) ) + #define mpi_free(a) mpi_debug_free((a), M_DBGINFO(__LINE__) ) + #define mpi_resize(a,b) mpi_debug_resize((a),(b), M_DBGINFO(__LINE__) ) + #define mpi_copy(a) mpi_debug_copy((a), M_DBGINFO(__LINE__) ) + MPI mpi_debug_alloc( unsigned nlimbs, const char *info ); + MPI mpi_debug_alloc_secure( unsigned nlimbs, const char *info ); + void mpi_debug_free( MPI a, const char *info ); + void mpi_debug_resize( MPI a, unsigned nlimbs, const char *info ); + MPI mpi_debug_copy( MPI a, const char *info ); +#else + MPI mpi_alloc( unsigned nlimbs ); + MPI mpi_alloc_secure( unsigned nlimbs ); + void mpi_free( MPI a ); + void mpi_resize( MPI a, unsigned nlimbs ); + MPI mpi_copy( MPI a ); +#endif +#define mpi_is_opaque(a) ((a) && ((a)->flags&4)) +MPI mpi_set_opaque( MPI a, void *p, unsigned int len ); +void *mpi_get_opaque( MPI a, unsigned int *len ); +#define mpi_is_secure(a) ((a) && ((a)->flags&1)) +void mpi_set_secure( MPI a ); +void mpi_clear( MPI a ); +MPI mpi_alloc_like( MPI a ); +void mpi_set( MPI w, MPI u); +void mpi_set_ui( MPI w, ulong u); +MPI mpi_alloc_set_ui( unsigned long u); +void mpi_m_check( MPI a ); +void mpi_swap( MPI a, MPI b); + +/*-- mpicoder.c --*/ +MPI mpi_read_from_buffer(byte *buffer, unsigned *ret_nread, int secure); +int mpi_fromstr(MPI val, const char *str); +int mpi_print( FILE *fp, MPI a, int mode ); +void g10_log_mpidump( const char *text, MPI a ); +u32 mpi_get_keyid( MPI a, u32 *keyid ); +byte *mpi_get_buffer( MPI a, unsigned *nbytes, int *sign ); +byte *mpi_get_secure_buffer( MPI a, unsigned *nbytes, int *sign ); +void mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign ); + +#define log_mpidump g10_log_mpidump + +/*-- mpi-add.c --*/ +void mpi_add_ui(MPI w, MPI u, ulong v ); +void mpi_add(MPI w, MPI u, MPI v); +void mpi_addm(MPI w, MPI u, MPI v, MPI m); +void mpi_sub_ui(MPI w, MPI u, ulong v ); +void mpi_sub( MPI w, MPI u, MPI v); +void mpi_subm( MPI w, MPI u, MPI v, MPI m); + +/*-- mpi-mul.c --*/ +void mpi_mul_ui(MPI w, MPI u, ulong v ); +void mpi_mul_2exp( MPI w, MPI u, ulong cnt); +void mpi_mul( MPI w, MPI u, MPI v); +void mpi_mulm( MPI w, MPI u, MPI v, MPI m); + +/*-- mpi-div.c --*/ +ulong mpi_fdiv_r_ui( MPI rem, MPI dividend, ulong divisor ); +void mpi_fdiv_r( MPI rem, MPI dividend, MPI divisor ); +void mpi_fdiv_q( MPI quot, MPI dividend, MPI divisor ); +void mpi_fdiv_qr( MPI quot, MPI rem, MPI dividend, MPI divisor ); +void mpi_tdiv_r( MPI rem, MPI num, MPI den); +void mpi_tdiv_qr( MPI quot, MPI rem, MPI num, MPI den); +void mpi_tdiv_q_2exp( MPI w, MPI u, unsigned count ); +int mpi_divisible_ui(MPI dividend, ulong divisor ); + +/*-- mpi-gcd.c --*/ +int mpi_gcd( MPI g, MPI a, MPI b ); + +/*-- mpi-mpow.c --*/ +void mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI mod); + +/*-- mpi-cmp.c --*/ +int mpi_cmp_ui( MPI u, ulong v ); +int mpi_cmp( MPI u, MPI v ); + +/*-- mpi-scan.c --*/ +int mpi_getbyte( MPI a, unsigned idx ); +void mpi_putbyte( MPI a, unsigned idx, int value ); +unsigned mpi_trailing_zeros( MPI a ); + +/*-- mpi-bit.c --*/ +void mpi_normalize( MPI a ); +unsigned int mpi_get_nbits( MPI a ); +int mpi_test_bit( MPI a, unsigned n ); +void mpi_set_bit( MPI a, unsigned n ); +void mpi_set_highbit( MPI a, unsigned n ); +void mpi_clear_highbit( MPI a, unsigned n ); +void mpi_clear_bit( MPI a, unsigned n ); +void mpi_rshift( MPI x, MPI a, unsigned n ); + +/*-- mpi-inv.c --*/ +void mpi_invm( MPI x, MPI u, MPI v ); + + +#endif /*G10_MPI_H*/ diff --git a/src/mpiapi.c b/src/mpiapi.c deleted file mode 100644 index da21aba5..00000000 --- a/src/mpiapi.c +++ /dev/null @@ -1,347 +0,0 @@ -/* mpiapi.a - MPI function interface - * Copyright (C) 1998,1999 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 <string.h> -#include <stdarg.h> -#include <assert.h> - -#define GCRYPT_NO_MPI_MACROS 1 -#include "g10lib.h" -#include "mpi.h" -#include "../cipher/random.h" - - -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); -} - - - -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. - * Return the number of bytes actually written in nbytes. - * TODO: Move this stuff to mpicoder.c or replace mpicoder.c - */ -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'; - } - - #if BYTES_PER_MPI_LIMB == 2 - #define X "4" - #elif BYTES_PER_MPI_LIMB == 4 - #define X "8" - #elif BYTES_PER_MPI_LIMB == 8 - #define X "16" - #else - #error please define the format here - #endif - 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; - #undef X - return 0; - } - else - return GCRYERR_INV_ARG; -} - - -void -gcry_mpi_powm( MPI w, MPI b, MPI e, MPI m ) -{ - mpi_powm( w, b, e, m ); -} - - diff --git a/src/testapi.c b/src/testapi.c index 9e51b427..a42b037c 100644 --- a/src/testapi.c +++ b/src/testapi.c @@ -10,12 +10,7 @@ int main( int argc, char **argv ) { - GCRY_MD_HD md; - - - md = gcry_md_open( GCRY_MD_RMD160, 0 ); - - + printf("%s\n", gcry_check_version ( argc > 1 ? argv[1] : NULL ) ); return 0; diff --git a/src/wrapper.c b/src/wrapper.c new file mode 100644 index 00000000..3b486de6 --- /dev/null +++ b/src/wrapper.c @@ -0,0 +1,41 @@ +/* wrapper.c - wrapper around some inertal functions + * Copyright (C) 1999 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 <stdlib.h> + +#include "g10lib.h" + + +void *gcry_malloc( size_t n ) { return g10_malloc( n ); } +void *gcry_calloc( size_t n, size_t m ) { return g10_calloc( n, m ); } +void *gcry_malloc_secure( size_t n ) { return g10_malloc_secure( n );} +void *gcry_calloc_secure( size_t n, size_t m ) + { return g10_calloc_secure( n, m ); } +void *gcry_realloc( void *a, size_t n ) { return g10_realloc( a, n ); } +void *gcry_xmalloc( size_t n ) { return g10_xmalloc( n ); } +void *gcry_xcalloc( size_t n, size_t m ) { return g10_xcalloc( n, m ); } +void *gcry_xmalloc_secure( size_t n ) { return g10_xmalloc_secure( n ); } +void *gcry_xcalloc_secure( size_t n, size_t m ) + { return g10_xcalloc_secure( n, m ); } +void *gcry_xrealloc( void *a, size_t n ) { return g10_xrealloc( a, n ); } +char *gcry_xstrdup( const char * a) { return g10_xstrdup( a); } +void gcry_free( void *p ) { g10_free( p ); } + |