diff options
74 files changed, 1574 insertions, 955 deletions
@@ -67,7 +67,7 @@ jungmann@cwb.matrix.com.br TRANSLATIONS Pedro Morais -??????????. [pt_PT] +Disclaimer. [pt_PT] morais@poli.org @@ -35,6 +35,7 @@ and after about half a day in the rsync snapshots. on sparc-solaris2.6. --> Solaris fixed. --> IRIX bug still there but someone should test this again! + (<mack@sgi.com> reports that it is still alive in 1.0.0) [ *] #18 1999-05-27 <Steffen.Zahn@icn.siemens.de> 0.9.7 rndunix hangs on hp/ux. The problem is related to my_plcose which is @@ -42,7 +43,8 @@ and after about half a day in the rsync snapshots. [ *] #22 1999-07-22 Solaris make has problems with the generated POTFILES - seems to be a - gettext bug. Use GNU gmake as a workaround. + gettext bug. Use GNU make as a workaround. + FIX: 1999-12-03 (meanwhile fixed in gettext) [ *] #23 1999-09-03 <todd.brooks@yale.edu> 0.9.11 Only the first signature of a cleartext sig seems to be verified. @@ -58,7 +60,8 @@ and after about half a day in the rsync snapshots. Validity check based on user name is only done when listing a key; the key ID based checking (selecting the user ID with the most validity) is used in all other cases. The Edit menu does not have - a way to disbly user ID based validity. + a way to disable user ID based validity. + FIX: 1999-12-03 (1.0.x only) [ *] #26 1999-11-11 gpg still does not take UTF8 strings to select a UID. @@ -66,7 +69,23 @@ and after about half a day in the rsync snapshots. [ **] #27 1999-11-12 <oren@hishome.net> Unknown packets (type 17 - photo ID?) mess up the checking - of self-signature becuase they are simply ignored. + of self-signature because they are simply ignored. + FIX: 1999-11-12 (1.0.x only) +[ *] #28 1999-11-29 + --list-key will only emit an error about unknown UIDs when all UIDs + are unknown. + [Postponed for 1.2] -Next #28 + +[ **] #29 1999-12-01 <karney@pppl.gov> + Using a --gen-key on a new user account (w/o ~./gnupg) doesn't pay + attention to the standard options files, which get's installed + afterwards. + FIX: 1999-12-02 + + + + + +Next #30 @@ -1,3 +1,8 @@ + + * Removed option --emulate-checksum-bug + + + Noteworthy changes in version 1.1.0 (1999-10-26) ----------------------------------- @@ -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. @@ -1,4 +1,22 @@ + * options.skel wird mit umaks von root installiert. + + * See how we can handle the symlinks in ./gcrypt + + * Add SIGSEGV handler to overcome zlib problems with truncated data. + + * Use --output for keylistings too. + + * Add to the (EGD) docs that ~/.gnupg/entropy should be a symlink to the + real socket. + + * Add a way to generate keys in batch mode with arbitrary parameters. + + * Never allocate packet memory with a m-alloc, but use a specific function. + + * Should we change names like mpi_write in g10/ so that we don't + use the prefix mpi here? + * Implement the AXP syscall to enable bus traps for GLIB 2 * parse a paramter file to do automatic key generation and to set diff --git a/checks/Makefile.am b/checks/Makefile.am index 3568fe35e..41ded3cd6 100644 --- a/checks/Makefile.am +++ b/checks/Makefile.am @@ -1,6 +1,6 @@ ## Process this file with automake to create Makefile.in -GPG_DEARMOR = --no-options --quiet --yes --dearmor +GPG_DEARMOR = ../g10/gpg --no-options --quiet --yes --dearmor TESTS = version.test mds.test \ decrypt.test decrypt-dsa.test \ diff --git a/cipher/ChangeLog b/cipher/ChangeLog index bfe180d9f..df0eeee12 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 23142d9cb..aa766bbc4 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 1a7a65845..7808d8769 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 903625c11..1f132ae0c 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 262325c71..d2c40b3f5 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 f88aa91d3..02995e02e 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 bc9c6e867..bc0a6c30d 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 cb7327a4a..1ad163332 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 49f4773e2..4b2c55e41 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 78c9ecdaa..38991a182 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 36c8b4e3d..4d5f0ef3a 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 d25abcd31..bca596fd1 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 0bd7ecfac..e553917a7 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/g10/ChangeLog b/g10/ChangeLog index 3ba8449b8..de4fc890c 100644 --- a/g10/ChangeLog +++ b/g10/ChangeLog @@ -1,3 +1,36 @@ +Wed Dec 8 21:58:32 CET 1999 Werner Koch <wk@gnupg.de> + + * seckey-cert.c: Removed obsolete mpi_*_protect_flag. + * parse-packet.c: Ditto. + + * misc.c (mpi_read): Removed the secure argumet becuase it is + never used. Changed all Callers. + (mpi_read_opaque): New. + (mpi_write_opaque): New. + * parse-packet.c (parse_key): Use the opaque method also for + v3 keys. + * build-packet.c (do_secret_key): Likewise. + + * g10.c (main): Check libgcrypt version. + + * packet.h: replaced inclusion of mpi.h by a plain typeedef of the + gcry_mpi structure and removed all inclusions of "mpi.h" in all + sources. + + * g10.c: Add --delete-secret-key to the help page. + + * g10.c (main): Changed the default homedir to "~/.gnupg-test" so + that we don't mess up with the stable version. + + * misc.c (mpi_write): New. + (mpi_write): New. + + * misc.c (checksum_u16_nobug): Removed. + (checksum_mpi_counted_nbits): Renamed to ... + (checksum_mpi): ... this to superseed the old one. Changed all + callers. This is because we do not emulate the old gpg bug anymore. + * g10.c (oEmuChecksumBug): Removed. + Fri Nov 19 17:15:20 CET 1999 Werner Koch <wk@gnupg.de> * g10.c (register_extension): New... diff --git a/g10/armor.c b/g10/armor.c index 053cae7ff..d595141ba 100644 --- a/g10/armor.c +++ b/g10/armor.c @@ -830,19 +830,19 @@ armor_filter( void *opaque, int control, buf[n++] = 0x01; /* sigclass 0x01 (canonical text mode)*/ if( hashes & 1 ) { hashes &= ~1; - buf[n++] = DIGEST_ALGO_RMD160; + buf[n++] = GCRY_MD_RMD160; } else if( hashes & 2 ) { hashes &= ~2; - buf[n++] = DIGEST_ALGO_SHA1; + buf[n++] = GCRY_MD_SHA1; } else if( hashes & 4 ) { hashes &= ~4; - buf[n++] = DIGEST_ALGO_MD5; + buf[n++] = GCRY_MD_MD5; } else if( hashes & 8 ) { hashes &= ~8; - buf[n++] = DIGEST_ALGO_TIGER; + buf[n++] = GCRY_MD_TIGER; } else buf[n++] = 0; /* (don't know) */ diff --git a/g10/build-packet.c b/g10/build-packet.c index eb2608a44..84ca3a8db 100644 --- a/g10/build-packet.c +++ b/g10/build-packet.c @@ -27,9 +27,8 @@ #include "packet.h" #include "errors.h" #include "iobuf.h" -#include "mpi.h" #include "util.h" -#include "cipher.h" +#include "dummy-cipher.h" #include "memory.h" #include "options.h" #include "main.h" @@ -174,11 +173,11 @@ static void write_fake_data( IOBUF out, MPI a ) { if( a ) { - int i; + size_t i; void *p; - p = mpi_get_opaque( a, &i ); - iobuf_write( out, p, i ); + p = gcry_mpi_get_opaque( a, &i ); + iobuf_write( out, p, (i+7)/8 ); } } @@ -372,13 +371,14 @@ do_secret_key( IOBUF out, int ctb, PKT_secret_key *sk ) iobuf_put(a, 0 ); if( sk->is_protected && sk->version >= 4 ) { byte *p; - assert( mpi_is_opaque( sk->skey[npkey] ) ); - p = mpi_get_opaque( sk->skey[npkey], &i ); - iobuf_write(a, p, i ); + size_t n; + assert( gcry_mpi_get_flag( sk->skey[i], GCRYMPI_FLAG_OPAQUE ) ); + p = gcry_mpi_get_opaque( sk->skey[i], &n ); + iobuf_write(a, p, (n+7)/8 ); } else { for( ; i < nskey; i++ ) - mpi_write(a, sk->skey[i] ); + mpi_write_opaque(a, sk->skey[i] ); write_16(a, sk->csum ); } diff --git a/g10/cipher.c b/g10/cipher.c index 162bbc35f..ef4c86502 100644 --- a/g10/cipher.c +++ b/g10/cipher.c @@ -54,8 +54,8 @@ write_header( cipher_filter_context_t *cfx, IOBUF a ) ed.len = cfx->datalen; ed.new_ctb = !ed.len && !opt.rfc1991; if( use_mdc ) { - ed.mdc_method = DIGEST_ALGO_SHA1; - cfx->mdc_hash = gcry_md_open( DIGEST_ALGO_SHA1, 0 ); + ed.mdc_method = GCRY_MD_SHA1; + cfx->mdc_hash = gcry_md_open( GCRY_MD_SHA1, 0 ); /*should we check the function works, or is it better to provide a flag which makes the function die itself ?? FIXME */ /*md_start_debug( cfx->mdc_hash, "mdccreat" );*/ diff --git a/g10/encode.c b/g10/encode.c index e87616d70..15761d09d 100644 --- a/g10/encode.c +++ b/g10/encode.c @@ -474,7 +474,7 @@ write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out ) frame = encode_session_key( dek, pubkey_nbits( pk->pubkey_algo, pk->pkey ) ); rc = pubkey_encrypt( pk->pubkey_algo, enc->data, frame, pk->pkey ); - mpi_free( frame ); + mpi_release( frame ); if( rc ) log_error("pubkey_encrypt failed: %s\n", g10_errstr(rc) ); else { diff --git a/g10/encr-data.c b/g10/encr-data.c index 7f5b16392..c4c9ff2f0 100644 --- a/g10/encr-data.c +++ b/g10/encr-data.c @@ -27,8 +27,7 @@ #include "util.h" #include "memory.h" #include "packet.h" -#include "mpi.h" -#include "cipher.h" +#include "dummy-cipher.h" #include "options.h" #include "i18n.h" diff --git a/g10/filter.h b/g10/filter.h index 321a4ca9e..c9ea2f394 100644 --- a/g10/filter.h +++ b/g10/filter.h @@ -23,6 +23,7 @@ #include <gcrypt.h> #include "basicdefs.h" +#include "iobuf.h" diff --git a/g10/free-packet.c b/g10/free-packet.c index 4c533d859..93c93de19 100644 --- a/g10/free-packet.c +++ b/g10/free-packet.c @@ -26,9 +26,8 @@ #include "packet.h" #include "iobuf.h" -#include "mpi.h" #include "util.h" -#include "cipher.h" +#include "dummy-cipher.h" #include "memory.h" #include "options.h" #include "main.h" @@ -45,9 +44,9 @@ free_pubkey_enc( PKT_pubkey_enc *enc ) int n, i; n = pubkey_get_nenc( enc->pubkey_algo ); if( !n ) - mpi_free(enc->data[0]); + mpi_release(enc->data[0]); for(i=0; i < n; i++ ) - mpi_free( enc->data[i] ); + mpi_release( enc->data[i] ); m_free(enc); } @@ -57,9 +56,9 @@ free_seckey_enc( PKT_signature *sig ) int n, i; n = pubkey_get_nsig( sig->pubkey_algo ); if( !n ) - mpi_free(sig->data[0]); + mpi_release(sig->data[0]); for(i=0; i < n; i++ ) - mpi_free( sig->data[i] ); + mpi_release( sig->data[i] ); m_free(sig->hashed_data); m_free(sig->unhashed_data); m_free(sig); @@ -73,9 +72,9 @@ release_public_key_parts( PKT_public_key *pk ) int n, i; n = pubkey_get_npkey( pk->pubkey_algo ); if( !n ) - mpi_free(pk->pkey[0]); + mpi_release(pk->pkey[0]); for(i=0; i < n; i++ ) { - mpi_free( pk->pkey[i] ); + mpi_release( pk->pkey[i] ); pk->pkey[i] = NULL; } if( pk->namehash ) { @@ -180,9 +179,9 @@ release_secret_key_parts( PKT_secret_key *sk ) n = pubkey_get_nskey( sk->pubkey_algo ); if( !n ) - mpi_free(sk->skey[0]); + mpi_release(sk->skey[0]); for(i=0; i < n; i++ ) { - mpi_free( sk->skey[i] ); + mpi_release( sk->skey[i] ); sk->skey[i] = NULL; } } @@ -36,7 +36,6 @@ #include "options.h" #include "keydb.h" #include "trustdb.h" -#include "mpi.h" #include "filter.h" #include "ttyio.h" #include "i18n.h" @@ -200,6 +199,8 @@ static ARGPARSE_OPTS opts[] = { { aListSecretKeys, "list-secret-keys", 256, N_("list secret keys")}, { aKeygen, "gen-key", 256, N_("generate a new key pair")}, { aDeleteKey, "delete-key",256, N_("remove key from the public keyring")}, + { aDeleteSecretKey, "delete-secret-key",256, + N_("remove key from the secret keyring")}, { aSignKey, "sign-key" ,256, N_("sign a key")}, { aLSignKey, "lsign-key" ,256, N_("sign a key locally")}, { aEditKey, "edit-key" ,256, N_("sign or edit a key")}, @@ -299,7 +300,6 @@ static ARGPARSE_OPTS opts[] = { { aListTrustPath, "list-trust-path",0, "@"}, { oKOption, NULL, 0, "@"}, { oPasswdFD, "passphrase-fd",1, "@" }, - { aDeleteSecretKey, "delete-secret-key",0, "@" }, { oNoVerbose, "no-verbose", 0, "@"}, { oTrustDBName, "trustdb-name", 2, "@" }, { oNoSecmemWarn, "no-secmem-warning", 0, "@" }, /* used only by regression tests */ @@ -319,7 +319,6 @@ static ARGPARSE_OPTS opts[] = { { oCompressKeys, "compress-keys",0, "@"}, { oCompressSigs, "compress-sigs",0, "@"}, { oAlwaysTrust, "always-trust", 0, "@"}, - { oEmuChecksumBug, "emulate-checksum-bug", 0, "@"}, { oRunAsShmCP, "run-as-shm-coprocess", 4, "@" }, { oSetFilename, "set-filename", 2, "@" }, { oSetPolicyURL, "set-policy-url", 2, "@" }, @@ -489,6 +488,8 @@ make_username( const char *string ) static void register_extension( const char *mainpgm, const char *fname ) { + #warning fixme add resgitser cipher extension + #if 0 if( *fname != '/' ) { /* do tilde expansion etc */ char *tmp; @@ -501,6 +502,7 @@ register_extension( const char *mainpgm, const char *fname ) } else register_cipher_extension( mainpgm, fname ); + #endif } @@ -512,10 +514,12 @@ set_debug(void) memory_debug_mode = 1; if( opt.debug & DBG_MEMSTAT_VALUE ) memory_stat_debug_mode = 1; + if( opt.debug & DBG_MPI_VALUE ) - mpi_debug_mode = 1; + gcry_control( GCRYCTL_SET_DEBUG_FLAGS, 2 ); if( opt.debug & DBG_CIPHER_VALUE ) - g10c_debug_mode = 1; + gcry_control( GCRYCTL_SET_DEBUG_FLAGS, 1 ); + if( opt.debug & DBG_IOBUF_VALUE ) iobuf_debug_mode = 1; @@ -590,7 +594,14 @@ main( int argc, char **argv ) * secmem_init() somewhere after the option parsing */ log_set_name("gpg"); - secure_random_alloc(); /* put random number into secure memory */ + /* check that the libraries are suitable. Do it here because + * the option parse may need services of the library */ + if ( !gcry_check_version ( "1.1.1" ) ) { + log_fatal(_("libgcrypt is too old (need %s, have %s)\n"), + VERSION, gcry_check_version(NULL) ); + } + + gcry_control( GCRYCTL_USE_SECURE_RNDPOOL ); disable_core_dumps(); init_signals(); create_dotlock(NULL); /* register locking cleanup */ @@ -609,9 +620,9 @@ main( int argc, char **argv ) opt.homedir = getenv("GNUPGHOME"); if( !opt.homedir || !*opt.homedir ) { #ifdef HAVE_DRIVE_LETTERS - opt.homedir = "c:/gnupg"; + opt.homedir = "c:/gnupg-test"; #else - opt.homedir = "~/.gnupg"; + opt.homedir = "~/.gnupg-test"; #endif } @@ -663,6 +674,7 @@ main( int argc, char **argv ) if( default_config ) configname = make_filename(opt.homedir, "options", NULL ); + argc = orig_argc; argv = orig_argv; pargs.argc = &argc; @@ -741,8 +753,10 @@ main( int argc, char **argv ) case oNoTTY: opt.quiet = 1; tty_no_terminal(1); break; case oDryRun: opt.dry_run = 1; break; case oInteractive: opt.interactive = 1; break; - case oVerbose: g10_opt_verbose++; - opt.verbose++; opt.list_sigs=1; break; + case oVerbose: + opt.verbose++; opt.list_sigs=1; + gcry_control( GCRYCTL_SET_VERBOSITY, (int)opt.verbose ); + break; case oKOption: set_cmd( &cmd, aKMode ); break; case oBatch: opt.batch = 1; greeting = 0; break; @@ -768,8 +782,10 @@ main( int argc, char **argv ) case oNoArmor: opt.no_armor=1; opt.armor=0; break; case oNoDefKeyring: default_keyring = 0; break; case oNoGreeting: nogreeting = 1; break; - case oNoVerbose: g10_opt_verbose = 0; - opt.verbose = 0; opt.list_sigs=0; break; + case oNoVerbose: + opt.verbose = 0; opt.list_sigs=0; + gcry_control( GCRYCTL_SET_VERBOSITY, (int)opt.verbose ); + break; case oNoComment: opt.no_comment=1; break; case oNoVersion: opt.no_version=1; break; case oEmitVersion: opt.no_version=0; break; @@ -823,7 +839,6 @@ main( int argc, char **argv ) opt.s2k_digest_algo = GCRY_MD_RMD160; opt.s2k_cipher_algo = GCRY_CIPHER_BLOWFISH; break; - case oEmuChecksumBug: opt.emulate_bugs |= EMUBUG_GPGCHKSUM; break; case oCompressSigs: opt.compress_sigs = 1; break; case oRunAsShmCP: #ifndef USE_SHM_COPROCESSING @@ -950,10 +965,11 @@ main( int argc, char **argv ) secmem_set_flags( secmem_get_flags() & ~2 ); /* resume warnings */ set_debug(); - g10_opt_homedir = opt.homedir; + /* FIXME: should set filenames of libgcrypt explicitly + * g10_opt_homedir = opt.homedir; */ /* must do this after dropping setuid, because string_to... - * may try to load an module */ + * may try to load a module */ if( def_cipher_string ) { opt.def_cipher_algo = gcry_cipher_map_name(def_cipher_string); m_free(def_cipher_string); def_cipher_string = NULL; @@ -1018,7 +1034,7 @@ main( int argc, char **argv ) opt.list_sigs++; opt.verbose = opt.verbose > 1; - g10_opt_verbose = opt.verbose; + gcry_control( GCRYCTL_SET_VERBOSITY, (int)opt.verbose ); } @@ -1202,6 +1218,7 @@ main( int argc, char **argv ) case aListSigs: opt.list_sigs = 1; case aListKeys: + /* fixme: we chnaged this in 1.0 */ public_key_list( argc, argv ); break; case aListSecretKeys: @@ -1318,13 +1335,13 @@ main( int argc, char **argv ) mpi_print( stdout, factors[0], 1 ); /* print q */ } else if( mode == 4 && argc == 3 ) { - MPI g = mpi_alloc(1); + MPI g = mpi_new(8); mpi_print( stdout, generate_elg_prime( 0, atoi(argv[1]), atoi(argv[2]), g, NULL ), 1); putchar('\n'); mpi_print( stdout, g, 1 ); - mpi_free(g); + mpi_release(g); } else #endif diff --git a/g10/gpgd.c b/g10/gpgd.c index d68e5a843..6ee87ec8a 100644 --- a/g10/gpgd.c +++ b/g10/gpgd.c @@ -38,7 +38,7 @@ #include <sys/stat.h> #include "util.h" -#include "cipher.h" +#include "dummy-cipher.h" #include "options.h" #include "main.h" @@ -30,6 +30,7 @@ #include "util.h" #include "ttyio.h" #include "i18n.h" +#include "memory.h" #include "options.h" #include "filter.h" #include "http.h" diff --git a/g10/keygen.c b/g10/keygen.c index b4da909f8..2d3c88f43 100644 --- a/g10/keygen.c +++ b/g10/keygen.c @@ -28,7 +28,7 @@ #include "util.h" #include "main.h" #include "packet.h" -#include "cipher.h" +#include "dummy-cipher.h" #include "ttyio.h" #include "options.h" #include "keydb.h" @@ -227,7 +227,7 @@ gen_elg(int algo, unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek, sk->is_protected = 0; sk->protect.algo = 0; - sk->csum = checksum_mpi_counted_nbits( sk->skey[3] ); + sk->csum = checksum_mpi( sk->skey[3] ); if( ret_sk ) /* not a subkey: return an unprotected version of the sk */ *ret_sk = copy_secret_key( NULL, sk ); @@ -306,7 +306,7 @@ gen_dsa(unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek, sk->is_protected = 0; sk->protect.algo = 0; - sk->csum = checksum_mpi_counted_nbits( sk->skey[4] ); + sk->csum = checksum_mpi( sk->skey[4] ); if( ret_sk ) /* not a subkey: return an unprotected version of the sk */ *ret_sk = copy_secret_key( NULL, sk ); diff --git a/g10/keyid.c b/g10/keyid.c index 0c82aa193..ac3e71258 100644 --- a/g10/keyid.c +++ b/g10/keyid.c @@ -28,9 +28,9 @@ #include <gcrypt.h> #include "util.h" #include "main.h" +#include "memory.h" #include "packet.h" #include "options.h" -#include "mpi.h" #include "keydb.h" @@ -353,7 +353,7 @@ fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len ) /* RSA in version 3 packets is special */ GCRY_MD_HD md; - md = gcry_md_open( DIGEST_ALGO_MD5, 0); + md = gcry_md_open( GCRY_MD_MD5, 0); if( !md ) BUG(); if( pubkey_get_npkey( pk->pubkey_algo ) > 1 ) { @@ -368,7 +368,7 @@ fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len ) if( !array ) array = m_alloc( 16 ); len = 16; - memcpy(array, gcry_md_read(md, DIGEST_ALGO_MD5), 16 ); + memcpy(array, gcry_md_read(md, GCRY_MD_MD5), 16 ); gcry_md_close(md); } else { @@ -399,7 +399,7 @@ fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len ) /* RSA in version 3 packets is special */ GCRY_MD_HD md; - md = gcry_md_open( DIGEST_ALGO_MD5, 0); + md = gcry_md_open( GCRY_MD_MD5, 0); if( !md ) BUG(); if( pubkey_get_npkey( sk->pubkey_algo ) > 1 ) { diff --git a/g10/main.h b/g10/main.h index dc5eb32c7..1649a69ef 100644 --- a/g10/main.h +++ b/g10/main.h @@ -23,13 +23,16 @@ #include <gcrypt.h> #include "basicdefs.h" #include "iobuf.h" -#include "mpi.h" #include "keydb.h" #define DEFAULT_CIPHER_ALGO GCRY_CIPHER_BLOWFISH #define DEFAULT_PUBKEY_ALGO GCRY_PUBKEY_ELGAMAL #define DEFAULT_DIGEST_ALGO GCRY_MD_RMD160 +#define is_RSA(a) ((a)==GCRY_PK_RSA || (a)==GCRY_PK_RSA_E \ + || (a)==GCRY_PK_RSA_S ) +#define is_ELGAMAL(a) ((a)==GCRY_PK_ELG || (a)==GCRY_PK_ELG_E) + /*-- g10.c --*/ extern int g10_errors_seen; @@ -52,9 +55,12 @@ void disable_core_dumps(void); u16 checksum_u16( unsigned n ); u16 checksum( byte *p, unsigned n ); u16 checksum_mpi( MPI a ); -u16 checksum_mpi_counted_nbits( MPI a ); u32 buffer_to_u32( const byte *buffer ); +int mpi_write( IOBUF out, GCRY_MPI a ); +GCRY_MPI mpi_read(IOBUF inp, unsigned *ret_nread ); +GCRY_MPI mpi_read_opaque(IOBUF inp, unsigned *ret_nread ); + int openpgp_cipher_test_algo( int algo ); int openpgp_pk_test_algo( int algo, unsigned int usage_flags ); int openpgp_md_test_algo( int algo ); diff --git a/g10/mainproc.c b/g10/mainproc.c index db43e50b3..af26118c7 100644 --- a/g10/mainproc.c +++ b/g10/mainproc.c @@ -376,7 +376,7 @@ proc_plaintext( CTX c, PACKET *pkt ) gcry_md_enable( c->mfx.md, n->pkt->pkt.onepass_sig->digest_algo ); if( !any && n->pkt->pkt.onepass_sig->digest_algo - == DIGEST_ALGO_MD5 ) + == GCRY_MD_MD5 ) only_md5 = 1; else only_md5 = 0; @@ -400,9 +400,9 @@ proc_plaintext( CTX c, PACKET *pkt ) } } if( !any ) { /* no onepass sig packet: enable all standard algos */ - gcry_md_enable( c->mfx.md, DIGEST_ALGO_RMD160 ); - gcry_md_enable( c->mfx.md, DIGEST_ALGO_SHA1 ); - gcry_md_enable( c->mfx.md, DIGEST_ALGO_MD5 ); + gcry_md_enable( c->mfx.md, GCRY_MD_RMD160 ); + gcry_md_enable( c->mfx.md, GCRY_MD_SHA1 ); + gcry_md_enable( c->mfx.md, GCRY_MD_MD5 ); } if( only_md5 ) { /* This is a kludge to work around a bug in pgp2. It does only @@ -410,7 +410,7 @@ proc_plaintext( CTX c, PACKET *pkt ) * pgp mails we could see whether there is the signature packet * in front of the plaintext. If someone needs this, send me a patch. */ - if( !(c->mfx.md2 = gcry_md_open( DIGEST_ALGO_MD5, 0)) ) + if( !(c->mfx.md2 = gcry_md_open( GCRY_MD_MD5, 0)) ) BUG(); } #if 0 @@ -1188,13 +1188,13 @@ proc_tree( CTX c, KBNODE node ) free_md_filter_context( &c->mfx ); if( !(c->mfx.md = gcry_md_open(sig->digest_algo, 0)) ) BUG(); - if( sig->digest_algo == DIGEST_ALGO_MD5 + if( sig->digest_algo == GCRY_MD_MD5 && is_RSA( sig->pubkey_algo ) ) { /* enable a workaround for a pgp2 bug */ - if( !(c->mfx.md2 = gcry_md_open( DIGEST_ALGO_MD5, 0 )) ) + if( !(c->mfx.md2 = gcry_md_open( GCRY_MD_MD5, 0 )) ) BUG(); } - else if( sig->digest_algo == DIGEST_ALGO_SHA1 + else if( sig->digest_algo == GCRY_MD_SHA1 && sig->pubkey_algo == GCRY_PK_DSA && sig->sig_class == 0x01 ) { /* enable the workaround also for pgp5 when the detached diff --git a/g10/misc.c b/g10/misc.c index 7e8e85a9e..4d7644f4c 100644 --- a/g10/misc.c +++ b/g10/misc.c @@ -34,10 +34,13 @@ #include <gcrypt.h> #include "util.h" #include "main.h" +#include "memory.h" #include "options.h" #include "i18n.h" +#define MAX_EXTERN_MPI_BITS 16384 + #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2 #warning using trap_unaligned @@ -86,23 +89,140 @@ disable_core_dumps() -u16 -checksum_u16( unsigned n ) +/**************** + * write an mpi to out. + */ +int +mpi_write( IOBUF out, MPI a ) { - u16 a; + char buffer[(MAX_EXTERN_MPI_BITS+7)/8]; + size_t nbytes; + int rc; - a = (n >> 8) & 0xff; - if( opt.emulate_bugs & EMUBUG_GPGCHKSUM ) { - a |= n & 0xff; - log_debug("csum_u16 emulated for n=%u\n", n); + nbytes = (MAX_EXTERN_MPI_BITS+7)/8; + rc = gcry_mpi_print( GCRYMPI_FMT_PGP, buffer, &nbytes, a ); + if( !rc ) + rc = iobuf_write( out, buffer, nbytes ); + + return rc; +} + +/**************** + * Writye a MPI to out, but in this case it is an opaque one, + * s used vor v3 protected keys. + */ +int +mpi_write_opaque( IOBUF out, MPI a ) +{ + size_t nbytes, nbits; + int rc; + + assert( gcry_mpi_get_flag( a, GCRYMPI_FLAG_OPAQUE ) ); + p = gcry_mpi_get_opaque( a, &nbits ); + nbytes = (nbits+7) / 8; + iobuf_put( out, nbits >> 8 ); + iobuf_put( out, nbits ) + rc = iobuf_write( out, p, nbytes ); + return rc; +} + + +/**************** + * 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 +mpi_read(IOBUF inp, unsigned *ret_nread, int secure) +{ + int c, c1, c2, i; + unsigned nbits, nbytes, nread=0; + MPI a = NULL; + byte *buf = NULL; + byte *p; + + if( (c = c1 = iobuf_get(inp)) == -1 ) + goto leave; + nbits = c << 8; + if( (c = c2 = 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; + buf = secure? m_alloc_secure( nbytes+2 ) : m_alloc( nbytes+2 ); + p = buf; + p[0] = c1; + p[1] = c2; + for( i=0 ; i < nbytes; i++ ) { + p[i+2] = iobuf_get(inp) & 0xff; + nread++; + } + nread += nbytes; + /* FIXME: replace with the gcry_scan function */ + a = mpi_read_from_buffer( buf, &nread, secure ); + + leave: + m_free(buf); + if( nread > *ret_nread ) + log_bug("mpi larger than packet"); + else + *ret_nread = nread; + return a; +} + +/**************** + * Same as mpi_read but the value is stored as an opaque MPI. + * This function is used to read encrpted MPI of v3 packets. + */ +GCRY_MPI +mpi_read_opaque(IOBUF inp, unsigned *ret_nread ) +{ + int c, c1, c2, i; + unsigned nbits, nbytes, nread=0; + GCRY_MPI a = NULL; + byte *buf = NULL; + byte *p; + + if( (c = c1 = iobuf_get(inp)) == -1 ) + goto leave; + nbits = c << 8; + if( (c = c2 = 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; + buf = m_alloc( nbytes ); + p = buf; + for( i=0 ; i < nbytes; i++ ) { + p[i] = iobuf_get(inp) & 0xff; + nread++; + } + nread += nbytes; + a = gcry_mpi_set_opaque(NULL, buf, nbits ); + buf = NULL; + + leave: + m_free(buf); + if( nread > *ret_nread ) + log_bug("mpi larger than packet"); else - a += n & 0xff; + *ret_nread = nread; return a; } -static u16 -checksum_u16_nobug( unsigned n ) + + +u16 +checksum_u16( unsigned n ) { u16 a; @@ -130,43 +250,13 @@ checksum_mpi( MPI a ) unsigned nbits; buffer = mpi_get_buffer( a, &nbytes, NULL ); - /* some versions of gpg encode wrong values for the length of an mpi - * so that mpi_get_nbits() which counts the mpi yields another (shorter) - * value than the one store with the mpi. mpi_get_nbit_info() returns - * this stored value if it is still available. - */ - - if( opt.emulate_bugs & EMUBUG_GPGCHKSUM ) - nbits = 0; - else - nbits = mpi_get_nbit_info(a); - if( !nbits ) - nbits = mpi_get_nbits(a); + nbits = mpi_get_nbits(a); csum = checksum_u16( nbits ); csum += checksum( buffer, nbytes ); m_free( buffer ); return csum; } -/**************** - * This is the correct function - */ -u16 -checksum_mpi_counted_nbits( MPI a ) -{ - u16 csum; - byte *buffer; - unsigned nbytes; - unsigned nbits; - - buffer = mpi_get_buffer( a, &nbytes, NULL ); - nbits = mpi_get_nbits(a); - mpi_set_nbit_info(a,nbits); - csum = checksum_u16_nobug( nbits ); - csum += checksum( buffer, nbytes ); - m_free( buffer ); - return csum; -} u32 @@ -213,10 +303,10 @@ print_cipher_algo_note( int algo ) { if( algo >= 100 && algo <= 110 ) no_exp_algo(); - else if( algo == CIPHER_ALGO_3DES - || algo == CIPHER_ALGO_CAST5 - || algo == CIPHER_ALGO_BLOWFISH - || algo == CIPHER_ALGO_TWOFISH + else if( algo == GCRY_CIPHER_3DES + || algo == GCRY_CIPHER_CAST5 + || algo == GCRY_CIPHER_BLOWFISH + || algo == GCRY_CIPHER_TWOFISH ) ; else { diff --git a/g10/options.h b/g10/options.h index 02613e3cc..4a2251052 100644 --- a/g10/options.h +++ b/g10/options.h @@ -86,8 +86,6 @@ struct { } opt; -#define EMUBUG_GPGCHKSUM 1 - #define DBG_PACKET_VALUE 1 /* debug packet reading/writing */ #define DBG_MPI_VALUE 2 /* debug mpi details */ #define DBG_CIPHER_VALUE 4 /* debug cipher handling */ diff --git a/g10/packet.h b/g10/packet.h index 17893875f..8e0c8c7e1 100644 --- a/g10/packet.h +++ b/g10/packet.h @@ -23,10 +23,13 @@ #include "types.h" #include "iobuf.h" -#include "mpi.h" -#include "cipher.h" +#include "dummy-cipher.h" #include "filter.h" +#ifndef DID_MPI_TYPEDEF + typedef struct gcry_mpi *MPI; + #define DID_MPI_TYPEDEF +#endif #define GNUPG_MAX_NPKEY 4 #define GNUPG_MAX_NSKEY 6 diff --git a/g10/parse-packet.c b/g10/parse-packet.c index e261b5366..e4e9fdd51 100644 --- a/g10/parse-packet.c +++ b/g10/parse-packet.c @@ -26,9 +26,8 @@ #include "packet.h" #include "iobuf.h" -#include "mpi.h" #include "util.h" -#include "cipher.h" +#include "dummy-cipher.h" #include "memory.h" #include "filter.h" #include "options.h" @@ -98,7 +97,7 @@ set_packet_list_mode( int mode ) { int old = list_mode; list_mode = mode; - mpi_print_mode = DBG_MPI; + mpi_print_mode = (opt.debug & DBG_MPI_VALUE); return old; } @@ -1133,7 +1132,8 @@ parse_signature( IOBUF inp, int pkttype, unsigned long pktlen, unknown_pubkey_warning( sig->pubkey_algo ); /* we store the plain material in data[0], so that we are able * to write it back with build_packet() */ - sig->data[0] = mpi_set_opaque(NULL, read_rest(inp, pktlen), pktlen ); + sig->data[0] = gcry_mpi_set_opaque(NULL, + read_rest(inp, pktlen), pktlen*8 ); pktlen = 0; } else { @@ -1292,8 +1292,8 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen, byte temp[16]; if( !npkey ) { - sk->skey[0] = mpi_set_opaque( NULL, - read_rest(inp, pktlen), pktlen ); + sk->skey[0] = gcry_mpi_set_opaque( NULL, + read_rest(inp, pktlen), pktlen*8 ); pktlen = 0; goto leave; } @@ -1368,7 +1368,7 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen, } else { /* old version; no S2K, so we set mode to 0, hash MD5 */ sk->protect.s2k.mode = 0; - sk->protect.s2k.hash_algo = DIGEST_ALGO_MD5; + sk->protect.s2k.hash_algo = GCRY_MD_MD5; if( list_mode ) printf( "\tprotect algo: %d (hash algo: %d)\n", sk->protect.algo, sk->protect.s2k.hash_algo ); @@ -1412,9 +1412,9 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen, if( is_v4 && sk->is_protected ) { /* ugly; the length is encrypted too, so we read all * stuff up to the end of the packet into the first - * skey element */ - sk->skey[npkey] = mpi_set_opaque(NULL, - read_rest(inp, pktlen), pktlen ); + * skey element (which is the one indexed by npkey) */ + sk->skey[npkey] = gcry_mpi_set_opaque(NULL, + read_rest(inp, pktlen), pktlen*8 ); pktlen = 0; if( list_mode ) { printf("\tencrypted stuff follows\n"); @@ -1422,9 +1422,9 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen, } else { /* v3 method: the mpi length is not encrypted */ for(i=npkey; i < nskey; i++ ) { - n = pktlen; sk->skey[i] = mpi_read(inp, &n, 0 ); pktlen -=n; - if( sk->is_protected ) - mpi_set_protect_flag(sk->skey[i]); + n = pktlen; + sk->skey[i] = mpi_read_opaque(inp, &n, 0 ); + pktlen -=n; if( list_mode ) { printf( "\tskey[%d]: ", i); if( sk->is_protected ) @@ -1446,8 +1446,8 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen, PKT_public_key *pk = pkt->pkt.public_key; if( !npkey ) { - pk->pkey[0] = mpi_set_opaque( NULL, - read_rest(inp, pktlen), pktlen ); + pk->pkey[0] = gcry_mpi_set_opaque( NULL, + read_rest(inp, pktlen), pktlen*8 ); pktlen = 0; goto leave; } @@ -1635,7 +1635,7 @@ parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen, goto leave; } method = iobuf_get_noeof(inp); pktlen--; - if( method != DIGEST_ALGO_SHA1 ) { + if( method != GCRY_MD_SHA1 ) { log_error("encrypted_mdc does not use SHA1 method\n" ); goto leave; } diff --git a/g10/passphrase.c b/g10/passphrase.c index 49b7a60c2..ae937e26d 100644 --- a/g10/passphrase.c +++ b/g10/passphrase.c @@ -28,7 +28,7 @@ #include "memory.h" #include "options.h" #include "ttyio.h" -#include "cipher.h" +#include "dummy-cipher.h" #include "keydb.h" #include "main.h" #include "i18n.h" diff --git a/g10/pkclist.c b/g10/pkclist.c index 6eae1f024..4f47e5563 100644 --- a/g10/pkclist.c +++ b/g10/pkclist.c @@ -908,7 +908,7 @@ static int algo_available( int preftype, int algo ) { if( preftype == PREFTYPE_SYM ) { - if( algo == CIPHER_ALGO_TWOFISH ) + if( algo == GCRY_CIPHER_TWOFISH ) return 0; /* we don't want to generate Twofish messages for now*/ return algo && !openpgp_cipher_test_algo( algo ); } diff --git a/g10/pubkey-enc.c b/g10/pubkey-enc.c index f11e6f12a..94fe2777b 100644 --- a/g10/pubkey-enc.c +++ b/g10/pubkey-enc.c @@ -27,10 +27,9 @@ #include "memory.h" #include "packet.h" #include "main.h" -#include "mpi.h" #include "keydb.h" #include "trustdb.h" -#include "cipher.h" +#include "dummy-cipher.h" #include "status.h" #include "options.h" #include "i18n.h" @@ -110,7 +109,7 @@ get_it( PKT_pubkey_enc *k, DEK *dek, PKT_secret_key *sk, u32 *keyid ) if( rc ) goto leave; frame = mpi_get_buffer( plain_dek, &nframe, NULL ); - mpi_free( plain_dek ); plain_dek = NULL; + mpi_release( plain_dek ); plain_dek = NULL; /* Now get the DEK (data encryption key) from the frame * @@ -129,7 +128,7 @@ get_it( PKT_pubkey_enc *k, DEK *dek, PKT_secret_key *sk, u32 *keyid ) * DEK is the encryption key (session key) with length k * CSUM */ - if( DBG_CIPHER ) + if( (opt.debug & DBG_CIPHER_VALUE) ) log_hexdump("DEK frame:", frame, nframe ); n=0; if( n + 7 > nframe ) @@ -171,7 +170,7 @@ get_it( PKT_pubkey_enc *k, DEK *dek, PKT_secret_key *sk, u32 *keyid ) rc = G10ERR_WRONG_SECKEY; goto leave; } - if( DBG_CIPHER ) + if( (opt.debug & DBG_CIPHER_VALUE) ) log_hexdump("DEK is:", dek->key, dek->keylen ); /* check that the algo is in the preferences */ { @@ -197,7 +196,7 @@ get_it( PKT_pubkey_enc *k, DEK *dek, PKT_secret_key *sk, u32 *keyid ) } leave: - mpi_free(plain_dek); + mpi_release(plain_dek); m_free(frame); return rc; } diff --git a/g10/ringedit.c b/g10/ringedit.c index 7db1b0b6b..b5eedd6ef 100644 --- a/g10/ringedit.c +++ b/g10/ringedit.c @@ -54,7 +54,6 @@ #include "util.h" #include "packet.h" #include "memory.h" -#include "mpi.h" #include "iobuf.h" #include "keydb.h" #include "host2net.h" diff --git a/g10/seckey-cert.c b/g10/seckey-cert.c index a0e41eea9..b228bc9c8 100644 --- a/g10/seckey-cert.c +++ b/g10/seckey-cert.c @@ -28,7 +28,6 @@ #include "util.h" #include "memory.h" #include "packet.h" -#include "mpi.h" #include "keydb.h" #include "main.h" #include "options.h" @@ -84,15 +83,17 @@ do_check( PKT_secret_key *sk ) log_fatal("set IV failed: %s\n", gcry_strerror(-1) ); csum = 0; if( sk->version >= 4 ) { - int ndata; + size_t ndata; + unsigned int ndatabits; byte *p, *data; i = pubkey_get_npkey(sk->pubkey_algo); - assert( mpi_is_opaque( sk->skey[i] ) ); - p = mpi_get_opaque( sk->skey[i], &ndata ); + assert( gcry_mpi_get_flag( sk->skey[i], GCRYMPI_FLAG_OPAQUE ) ); + p = gcry_mpi_get_opaque( sk->skey[i], &ndatabits ); + ndata = (ndatabits+7)/8; data = m_alloc_secure( ndata ); gcry_cipher_decrypt( cipher_hd, data, ndata, p, ndata ); - mpi_free( sk->skey[i] ); sk->skey[i] = NULL ; + mpi_release( sk->skey[i] ); sk->skey[i] = NULL ; p = data; if( ndata < 2 ) { log_error("not enough bytes for checksum\n"); @@ -104,11 +105,12 @@ do_check( PKT_secret_key *sk ) sk->csum = data[ndata-2] << 8 | data[ndata-1]; } /* must check it here otherwise the mpi_read_xx would fail - * because the length das an abritary value */ + * because the length may have an arbitrary value */ if( sk->csum == csum ) { for( ; i < pubkey_get_nskey(sk->pubkey_algo); i++ ) { nbytes = ndata; sk->skey[i] = mpi_read_from_buffer(p, &nbytes, 1 ); + /* fixme: replace by mpi_scan */ ndata -= nbytes; p += nbytes; } @@ -118,18 +120,24 @@ do_check( PKT_secret_key *sk ) else { for(i=pubkey_get_npkey(sk->pubkey_algo); i < pubkey_get_nskey(sk->pubkey_algo); i++ ) { - buffer = mpi_get_secure_buffer( sk->skey[i], &nbytes, NULL ); + size_t ndata; + unsigned int ndatabits; + byte *p, *data; + + assert( gcry_mpi_get_flag( sk->skey[i], GCRYMPI_FLAG_OPAQUE ) ); + p = gcry_mpi_get_opaque( sk->skey[i], &ndatabits ); + ndata = (ndatabits+7)/8; + data = m_alloc_secure( ndata ); gcry_cipher_sync( cipher_hd ); - assert( mpi_is_protected(sk->skey[i]) ); - gcry_cipher_decrypt( cipher_hd, buffer, nbytes, NULL, 0 ); - mpi_set_buffer( sk->skey[i], buffer, nbytes, 0 ); - mpi_clear_protect_flag( sk->skey[i] ); + gcry_cipher_decrypt( cipher_hd, data, ndata, p, ndata ); + mpi_release( sk->skey[i] ); sk->skey[i] = NULL ; + + res = gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_USG, + data, &ndata ); + csum += checksum_mpi( sk->skey[i] ); m_free( buffer ); } - if( opt.emulate_bugs & EMUBUG_GPGCHKSUM ) { - csum = sk->csum; - } } gcry_cipher_close( cipher_hd ); /* now let's see whether we have used the right passphrase */ @@ -261,6 +269,7 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek ) gcry_randomize(sk->protect.iv, sk->protect.ivlen, GCRY_STRONG_RANDOM); gcry_cipher_setiv( cipher_hd, sk->protect.iv, sk->protect.ivlen ); + #warning FIXME: replace set/get buffer if( sk->version >= 4 ) { #define NMPIS (GNUPG_MAX_NSKEY - GNUPG_MAX_NPKEY) byte *bufarr[NMPIS]; @@ -271,7 +280,7 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek ) for(j=0, i = pubkey_get_npkey(sk->pubkey_algo); i < pubkey_get_nskey(sk->pubkey_algo); i++, j++ ) { - assert( !mpi_is_opaque( sk->skey[i] ) ); + assert( !gcry_mpi_get_flag( sk->skey[i], GCRYMPI_FLAG_OPAQUE ) ); bufarr[j] = mpi_get_buffer( sk->skey[i], &narr[j], NULL ); nbits[j] = mpi_get_nbits( sk->skey[i] ); ndata += narr[j] + 2; @@ -299,25 +308,25 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek ) gcry_cipher_encrypt( cipher_hd, data, ndata, NULL, 0 ); for(i = pubkey_get_npkey(sk->pubkey_algo); i < pubkey_get_nskey(sk->pubkey_algo); i++ ) { - mpi_free( sk->skey[i] ); + mpi_release( sk->skey[i] ); sk->skey[i] = NULL; } i = pubkey_get_npkey(sk->pubkey_algo); - sk->skey[i] = mpi_set_opaque(NULL, data, ndata ); + sk->skey[i] = gcry_mpi_set_opaque(NULL, data, ndata*8 ); } else { /* NOTE: we always recalculate the checksum because there * are some test releases which calculated it wrong */ + #warning FIXME: Replace this code csum = 0; for(i=pubkey_get_npkey(sk->pubkey_algo); i < pubkey_get_nskey(sk->pubkey_algo); i++ ) { - csum += checksum_mpi_counted_nbits( sk->skey[i] ); + csum += checksum_mpi( sk->skey[i] ); buffer = mpi_get_buffer( sk->skey[i], &nbytes, NULL ); gcry_cipher_sync( cipher_hd ); - assert( !mpi_is_protected(sk->skey[i]) ); + assert( !mpi_is_opaque(sk->skey[i]) ); gcry_cipher_encrypt( cipher_hd, buffer, nbytes, NULL, 0 ); mpi_set_buffer( sk->skey[i], buffer, nbytes, 0 ); - mpi_set_protect_flag( sk->skey[i] ); m_free( buffer ); } sk->csum = csum; diff --git a/g10/seskey.c b/g10/seskey.c index 19f40636f..eac15c41f 100644 --- a/g10/seskey.c +++ b/g10/seskey.c @@ -26,10 +26,10 @@ #include <gcrypt.h> #include "util.h" -#include "cipher.h" -#include "mpi.h" +#include "dummy-cipher.h" #include "main.h" #include "i18n.h" +#include "memory.h" /**************** @@ -144,7 +144,7 @@ encode_session_key( DEK *dek, unsigned nbits ) frame[n++] = csum >>8; frame[n++] = csum; assert( n == nframe ); - a = mpi_alloc_secure( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ); + a = mpi_secure_new( nframe ); mpi_set_buffer( a, frame, nframe, 0 ); m_free(frame); return a; @@ -182,9 +182,7 @@ do_encode_md( GCRY_MD_HD md, int algo, size_t len, unsigned nbits, memcpy( frame+n, asn, asnlen ); n += asnlen; memcpy( frame+n, gcry_md_read(md, algo), len ); n += len; assert( n == nframe ); - a = gcry_md_is_secure(md)? - mpi_alloc_secure( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ) - : mpi_alloc( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ); + a = gcry_md_is_secure(md)? mpi_secure_new( nframe ) : mpi_new( nframe ); mpi_set_buffer( a, frame, nframe, 0 ); m_free(frame); return a; @@ -198,11 +196,9 @@ encode_md_value( int pubkey_algo, GCRY_MD_HD md, int hash_algo, unsigned nbits ) MPI frame; if( pubkey_algo == GCRY_PK_DSA ) { - frame = gcry_md_is_secure(md)? mpi_alloc_secure( - (gcry_md_get_algo_dlen(hash_algo) - +BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ) - : mpi_alloc((gcry_md_get_algo_dlen(hash_algo) - +BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ); + frame = gcry_md_is_secure(md)? + mpi_secure_new( gcry_md_get_algo_dlen(hash_algo) ) + : mpi_new( gcry_md_get_algo_dlen(hash_algo) ); mpi_set_buffer( frame, gcry_md_read(md, hash_algo), gcry_md_get_algo_dlen(hash_algo), 0 ); } diff --git a/g10/sig-check.c b/g10/sig-check.c index 03477b537..4fbf0808c 100644 --- a/g10/sig-check.c +++ b/g10/sig-check.c @@ -28,7 +28,6 @@ #include "util.h" #include "packet.h" #include "memory.h" -#include "mpi.h" #include "keydb.h" #include "main.h" #include "status.h" @@ -452,7 +451,7 @@ do_check( PKT_public_key *pk, PKT_signature *sig, GCRY_MD_HD digest ) ctx.md = digest; rc = pk_verify( pk->pubkey_algo, result, sig->data, pk->pkey, cmp_help, &ctx ); - mpi_free( result ); + mpi_release( result ); if( !rc && sig->flags.unknown_critical ) { log_info(_("assuming bad signature due to an unknown critical bit\n")); rc = G10ERR_BAD_SIGN; diff --git a/g10/sign.c b/g10/sign.c index d278945a3..d65e0e2d1 100644 --- a/g10/sign.c +++ b/g10/sign.c @@ -181,7 +181,7 @@ do_sign( PKT_secret_key *sk, PKT_signature *sig, frame = encode_md_value( sk->pubkey_algo, md, digest_algo, mpi_get_nbits(sk->skey[0])); rc = pk_sign( sk->pubkey_algo, sig->data, frame, sk->skey ); - mpi_free(frame); + mpi_release(frame); if( rc ) log_error(_("signing failed: %s\n"), g10_errstr(rc) ); else { @@ -217,9 +217,9 @@ hash_for(int pubkey_algo ) if( opt.def_digest_algo ) return opt.def_digest_algo; if( pubkey_algo == GCRY_PK_DSA ) - return DIGEST_ALGO_SHA1; + return GCRY_MD_SHA1; if( pubkey_algo == GCRY_PK_RSA ) - return DIGEST_ALGO_MD5; + return GCRY_MD_MD5; return DEFAULT_DIGEST_ALGO; } @@ -645,7 +645,7 @@ clearsign_file( const char *fname, STRLIST locusr, const char *outfile ) for( sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next ) { PKT_secret_key *sk = sk_rover->sk; - if( hash_for(sk->pubkey_algo) == DIGEST_ALGO_MD5 ) + if( hash_for(sk->pubkey_algo) == GCRY_MD_MD5 ) only_md5 = 1; else { only_md5 = 0; @@ -813,10 +813,10 @@ make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk, || sigclass == 0x30 || sigclass == 0x28 ); if( !digest_algo ) { switch( sk->pubkey_algo ) { - case GCRY_PK_DSA: digest_algo = DIGEST_ALGO_SHA1; break; + case GCRY_PK_DSA: digest_algo = GCRY_MD_SHA1; break; case GCRY_PK_RSA_S: - case GCRY_PK_RSA: digest_algo = DIGEST_ALGO_MD5; break; - default: digest_algo = DIGEST_ALGO_RMD160; break; + case GCRY_PK_RSA: digest_algo = GCRY_MD_MD5; break; + default: digest_algo = GCRY_MD_RMD160; break; } } if( !(md = gcry_md_open( digest_algo, 0 ))) diff --git a/g10/skclist.c b/g10/skclist.c index 381601e90..be9769185 100644 --- a/g10/skclist.c +++ b/g10/skclist.c @@ -33,7 +33,7 @@ #include "memory.h" #include "util.h" #include "i18n.h" -#include "cipher.h" +#include "dummy-cipher.h" #include "main.h" diff --git a/g10/status.c b/g10/status.c index 9f1c5a669..079c0dbd9 100644 --- a/g10/status.c +++ b/g10/status.c @@ -43,6 +43,7 @@ #include "ttyio.h" #include "options.h" #include "main.h" +#include "memory.h" #include "i18n.h" static int fd = -1; diff --git a/include/ChangeLog b/include/ChangeLog index f118fb32d..ed7e47992 100644 --- a/include/ChangeLog +++ b/include/ChangeLog @@ -1,3 +1,12 @@ +Wed Dec 8 21:58:32 CET 1999 Werner Koch <wk@gnupg.de> + + * util.h: Moved argparse stuff to the argparse header. Move some + of the string stuff to the stringhelp header. + + * mpi.h: Moved to ../gcrypt + * cipher.h: Moved to ../gcrypt + * g10lib.h: Moved to ../gcrypt + Tue Oct 26 14:10:21 CEST 1999 Werner Koch <wk@gnupg.de> * g10lib.h: Moved from ../gcrypt to here. diff --git a/include/cipher.h b/include/cipher.h deleted file mode 100644 index aaab08d55..000000000 --- a/include/cipher.h +++ /dev/null @@ -1,77 +0,0 @@ -/* 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 g10c_debug_mode - -#include "mpi.h" -#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) - -int g10c_debug_mode; -int g10_opt_verbose; -const char *g10_opt_homedir; - -/*-- 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/include/mpi.h b/include/mpi.h deleted file mode 100644 index dca6426da..000000000 --- a/include/mpi.h +++ /dev/null @@ -1,181 +0,0 @@ -/* 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 "iobuf.h" -#include "types.h" -#include "memory.h" - - -#define DBG_MPI mpi_debug_mode -int mpi_debug_mode; - -#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 nbits; /* the real number of valid bits (info only) */ - int sign; /* indicates a negative number */ - unsigned flags; /* bit 0: array must be allocated in secure memory space */ - /* bit 1: the mpi is encrypted */ - /* 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_get_nbit_info(a) ((a)->nbits) -#define mpi_set_nbit_info(a,b) ((a)->nbits = (b)) -#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, int len ); -void *mpi_get_opaque( MPI a, int *len ); -#define mpi_is_protected(a) ((a) && ((a)->flags&2)) -#define mpi_set_protect_flag(a) ((a)->flags |= 2) -#define mpi_clear_protect_flag(a) ((a)->flags &= ~2) -#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 --*/ -int mpi_write( IOBUF out, MPI a ); -#ifdef M_DEBUG - #define mpi_read(a,b,c) mpi_debug_read((a),(b),(c), M_DBGINFO( __LINE__ ) ) - MPI mpi_debug_read(IOBUF inp, unsigned *nread, int secure, const char *info); -#else - MPI mpi_read(IOBUF inp, unsigned *nread, int secure); -#endif -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-pow.c --*/ -void mpi_pow( MPI w, MPI u, MPI v); -void mpi_powm( MPI res, MPI base, MPI exp, MPI mod); - -/*-- 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 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/include/util.h b/include/util.h index cf656f625..190bff6ff 100644 --- a/include/util.h +++ b/include/util.h @@ -24,42 +24,13 @@ #error This header should not be used internally by libgcrypt #endif +#include <stdio.h> #include "types.h" #include "errors.h" -#include "types.h" -#include "mpi.h" - - -typedef struct { - int *argc; /* pointer to argc (value subject to change) */ - char ***argv; /* pointer to argv (value subject to change) */ - unsigned flags; /* Global flags (DO NOT CHANGE) */ - int err; /* print error about last option */ - /* 1 = warning, 2 = abort */ - int r_opt; /* return option */ - int r_type; /* type of return value (0 = no argument found)*/ - union { - int ret_int; - long ret_long; - ulong ret_ulong; - char *ret_str; - } r; /* Return values */ - struct { - int idx; - int inarg; - int stopped; - const char *last; - void *aliases; - const void *cur_alias; - } internal; /* DO NOT CHANGE */ -} ARGPARSE_ARGS; - -typedef struct { - int short_opt; - const char *long_opt; - unsigned flags; - const char *description; /* optional option description */ -} ARGPARSE_OPTS; +#include "../util/mischelp.h" +#include "../util/stringhelp.h" +#include "../util/argparse.h" + /*-- logger.c --*/ void log_set_logfile( const char *name, int fd ); @@ -119,14 +90,6 @@ void g10_log_hexdump( const char *text, const char *buf, size_t len ); /*-- errors.c --*/ const char * g10_errstr( int no ); -/*-- argparse.c --*/ -int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); -int optfile_parse( FILE *fp, const char *filename, unsigned *lineno, - ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); -void usage( int level ); -const char *strusage( int level ); -void set_strusage( const char *(*f)( int ) ); - /*-- dotlock.c --*/ struct dotlock_handle; @@ -168,11 +131,6 @@ STRLIST append_to_strlist( STRLIST *list, const char *string ); STRLIST append_to_strlist2( STRLIST *list, const char *string, int is_utf8 ); STRLIST strlist_prev( STRLIST head, STRLIST node ); STRLIST strlist_last( STRLIST node ); -const char *memistr( const char *buf, size_t buflen, const char *sub ); -char *mem2str( char *, const void *, size_t); -char *trim_spaces( char *string ); -unsigned trim_trailing_chars( byte *line, unsigned len, const char *trimchars); -unsigned trim_trailing_ws( byte *line, unsigned len ); int string_count_chr( const char *string, int c ); int set_native_charset( const char *newset ); const char* get_native_charset(void); @@ -180,24 +138,6 @@ char *native_to_utf8( const char *string ); char *utf8_to_native( const char *string, size_t length ); int check_utf8_string( const char *string ); -#ifndef HAVE_MEMICMP -int memicmp( const char *a, const char *b, size_t n ); -#endif -#ifndef HAVE_STPCPY -char *stpcpy(char *a,const char *b); -#endif -#ifndef HAVE_STRLWR -char *strlwr(char *a); -#endif -#ifndef HAVE_STRTOUL - #define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c))) -#endif -#ifndef HAVE_MEMMOVE - #define memmove(d, s, n) bcopy((s), (d), (n)) -#endif -#ifndef HAVE_STRICMP - #define stricmp(a,b) strcasecmp( (a), (b) ) -#endif /**** other missing stuff ****/ #ifndef HAVE_ATEXIT /* For SunOS */ @@ -209,10 +149,6 @@ char *strlwr(char *a); #endif /******** some macros ************/ -#ifndef STR - #define STR(v) #v -#endif -#define STR2(v) STR(v) #define DIM(v) (sizeof(v)/sizeof((v)[0])) #define DIMof(type,member) DIM(((type *)0)->member) 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); +} + + diff --git a/scripts/mkdiff b/scripts/mkdiff index 6fad6c188..92efcda6a 100755 --- a/scripts/mkdiff +++ b/scripts/mkdiff @@ -1,21 +1,25 @@ #!/bin/sh -if [ $# != 1 ] ; then - echo "usage: mkdiff package-name" >&2 +if [ $# = 1 ]; then + pack="$1" + vprf="" +elif [ $# = 2 ] ; then + pack="$1" + vprf="$2" +else + echo "usage: mkdiff package-name [version-prefix]" >&2 exit 1 fi -pack="$1" - set -e -curr_ver=$(ls $pack-*.tar.gz 2>/dev/null | sed "s/^$pack-\(.*\)\.tar\.gz/\1/"\ +curr_ver=$(ls $pack-${vprf}*.tar.gz 2>/dev/null | sed "s/^$pack-\(.*\)\.tar\.gz/\1/"\ | sort -r -t '.' -n +0 -1 +1 -2 +2 | head -1 ) if [ ! -f $pack-$curr_ver.tar.gz ]; then echo "mkdiff: no current version of package $pack found" >&2 exit 1 fi -prev_ver=$(ls $pack-*.tar.gz 2>/dev/null | sed "s/^$pack-\(.*\)\.tar\.gz/\1/"\ +prev_ver=$(ls $pack-${vprf}*.tar.gz 2>/dev/null | sed "s/^$pack-\(.*\)\.tar\.gz/\1/"\ | sort -r -t '.' -n +0 -1 +1 -2 +2 | head -2 | tail -1 ) if [ "$prev_ver" = "$curr_ver" ]; then echo "mkdiff: no previous version of package $pack found" >&2 diff --git a/tools/Makefile.am b/tools/Makefile.am index 68d1044df..07fd1145a 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -2,8 +2,7 @@ EXTRA_DIST = lspgpot INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/intl -needed_libs = ../cipher/libcipher.la \ - ../mpi/libmpi.la ../util/libutil.la ../gcrypt/libgcrypt.la @INTLLIBS@ +needed_libs = ../util/libutil.la ../gcrypt/libgcrypt.la @INTLLIBS@ noinst_PROGRAMS = mpicalc bftest clean-sat mk-tdata shmtest diff --git a/util/ChangeLog b/util/ChangeLog index ea9c7f3fe..aadb8b4d0 100644 --- a/util/ChangeLog +++ b/util/ChangeLog @@ -1,3 +1,21 @@ +Wed Dec 8 21:58:32 CET 1999 Werner Koch <wk@gnupg.de> + + * strgutil.c (strcasecmp): New. + + * argparse.h: New. + * libutil.h: New. + * argparse.c: Use these new files. + (optfile_parse): s/m_alloc/libutil_xalloc/ + + * strgutil.c: Moved a lot of function to ... + * stringhelp.c: ... this new file + * stringhelp.h: ... and the definitions to here + + * mischelp.h: New. + + * logging.h: New, but not yet used in GnuPG. + * logging.c: Ditto. + Fri Nov 19 17:15:20 CET 1999 Werner Koch <wk@gnupg.de> * argparse.c (default_strusage): Renamed to strusage. Fall back diff --git a/util/Makefile.am b/util/Makefile.am index c695db586..acb8f05cb 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -1,5 +1,8 @@ ## Process this file with automake to produce Makefile.in +# Those 2 files are in the CVS but currently not used. +EXTRA_DIST = xmalloc.c xmalloc.h logging.c logging.c + INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/intl noinst_LTLIBRARIES = libutil.la @@ -7,8 +10,12 @@ noinst_LTLIBRARIES = libutil.la libutil_la_LDFLAGS = libutil_la_SOURCES = logger.c fileutil.c miscutil.c strgutil.c \ - ttyio.c argparse.c memory.c secmem.c errors.c iobuf.c \ - dotlock.c http.c simple-gettext.c + ttyio.c memory.c secmem.c errors.c iobuf.c \ + dotlock.c http.c simple-gettext.c \ + libutil-config.h \ + mischelp.h \ + stringhelp.h stringhelp.c \ + argparse.h argparse.c http-test: http.c @@ -17,3 +24,5 @@ http-test: http.c + + diff --git a/util/argparse.c b/util/argparse.c index 12b0ebd7f..ba51980a9 100644 --- a/util/argparse.c +++ b/util/argparse.c @@ -15,9 +15,6 @@ * 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 is an independent version of the one in WkLib */ #include <config.h> @@ -26,8 +23,13 @@ #include <ctype.h> #include <string.h> -#include "util.h" -#include "i18n.h" +#include "libutil-config.h" +#include "mischelp.h" +#include "stringhelp.h" +#ifndef LIBUTIL_CONFIG_OF_GNUPG + #include "logging.h" /* currently not used in GnUPG */ +#endif +#include "argparse.h" /********************************* @@ -155,7 +157,7 @@ initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno ) arg->err = 0; arg->flags |= 1<<15; /* mark initialized */ if( *arg->argc < 0 ) - log_bug("Invalid argument for ArgParse\n"); + libutil_log_bug("Invalid argument for ArgParse\n"); } @@ -177,7 +179,7 @@ initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno ) s = "%s:%u: invalid alias definition\n"; else s = "%s:%u: invalid option\n"; - log_error(s, filename, *lineno ); + libutil_log_error(s, filename, *lineno ); } else { if( arg->r_opt == -3 ) @@ -192,7 +194,7 @@ initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno ) s = "Command \"%.50s\" is ambiguous\n"; else s = "Invalid option \"%.50s\"\n"; - log_error(s, arg->internal.last? arg->internal.last:"[??]" ); + libutil_log_error(s, arg->internal.last? arg->internal.last:"[??]" ); } if( arg->err != 1 ) exit(2); @@ -306,7 +308,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, trim_spaces( p ); } if( !p || !*p ) { - m_free( buffer ); + libutil_free( buffer ); arg->r_opt = -10; } else { @@ -320,7 +322,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, char *p; if( !buffer ) { keyword[i] = 0; - buffer = m_strdup(keyword); + buffer = libutil_strdup(keyword); } else buffer[i] = 0; @@ -333,7 +335,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, p[strlen(p)-1] = 0; } if( !set_opt_arg(arg, opts[idx].flags, p) ) - m_free(buffer); + libutil_free(buffer); } break; } @@ -388,7 +390,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, buffer[i++] = c; else { buflen += 50; - buffer = m_realloc(buffer, buflen); + buffer = libutil_realloc(buffer, buflen); buffer[i++] = c; } } @@ -396,7 +398,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, keyword[i++] = c; else { buflen = DIM(keyword)+50; - buffer = m_alloc(buflen); + buffer = libutil_xmalloc(buflen); memcpy(buffer, keyword, i); buffer[i++] = c; } diff --git a/util/argparse.h b/util/argparse.h new file mode 100644 index 000000000..12132edd3 --- /dev/null +++ b/util/argparse.h @@ -0,0 +1,64 @@ +/* argparse.h + * 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 + */ + +#ifndef LIBUTIL_ARGPARSE_H +#define LIBUTIL_ARGPARSE_H + +typedef struct { + int *argc; /* pointer to argc (value subject to change) */ + char ***argv; /* pointer to argv (value subject to change) */ + unsigned flags; /* Global flags (DO NOT CHANGE) */ + int err; /* print error about last option */ + /* 1 = warning, 2 = abort */ + int r_opt; /* return option */ + int r_type; /* type of return value (0 = no argument found)*/ + union { + int ret_int; + long ret_long; + ulong ret_ulong; + char *ret_str; + } r; /* Return values */ + struct { + int idx; + int inarg; + int stopped; + const char *last; + void *aliases; + const void *cur_alias; + } internal; /* DO NOT CHANGE */ +} ARGPARSE_ARGS; + +typedef struct { + int short_opt; + const char *long_opt; + unsigned flags; + const char *description; /* optional option description */ +} ARGPARSE_OPTS; + + + +int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); +int optfile_parse( FILE *fp, const char *filename, unsigned *lineno, + ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); +void usage( int level ); +const char *strusage( int level ); +void set_strusage( const char *(*f)( int ) ); + +#endif /*LIBUTIL_ARGPARSE_H*/ diff --git a/util/libutil-config.h b/util/libutil-config.h new file mode 100644 index 000000000..a4c80115b --- /dev/null +++ b/util/libutil-config.h @@ -0,0 +1,56 @@ +/* libutil-config.h - configuration of the libutil 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 + */ + +/**************** + * This header is to be included only by the files in this directory + * it should not be used by other modules. + */ + +#ifndef LIBUTIL_CONFIG_H +#define LIBUTIL_CONFIG_H + +#define LIBUTIL_CONFIG_OF_GNUPG 1 /* currently we need this kludge */ + +#include <sys/types.h> + +#ifndef HAVE_BYTE_TYPEDEF + #undef byte /* (this matches the test used by configure) */ + typedef unsigned char byte; + #define HAVE_BYTE_TYPEDEF +#endif + +#include "types.h" +#include "memory.h" +#include "util.h" +#include "i18n.h" + +#define libutil_xmalloc(a) m_alloc( (a) ) +#define libutil_realloc(a,n) m_realloc( (a), (n) ) +#define libutil_strdup(a) m_strdup( (a) ) +#define libutil_free(a) m_free( (a) ) + +#define libutil_log_debug log_debug +#define libutil_log_info log_info +#define libutil_log_error log_error +#define libutil_log_fatal log_fatal +#define libutil_log_bug log_bug + + +#endif /*LIBUTIL_CONFIGH*/ diff --git a/util/logging.c b/util/logging.c new file mode 100644 index 000000000..76931f9be --- /dev/null +++ b/util/logging.c @@ -0,0 +1,170 @@ +/* logging.c - useful logging functions + * 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 + */ + + +/* This file should replace logger.c in the future - for now it is not + * used GnuPG. + * It is a quite simple implemenation but sufficient for most purposes. + */ + +#include <config.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> + +#include "libutil-config.h" +#include "logging.h" + +enum my_log_levels { + MY_LOG_CONT, + MY_LOG_INFO, + MY_LOG_WARN, + MY_LOG_ERROR, + MY_LOG_FATAL, + MY_LOG_BUG, + MY_LOG_DEBUG +}; + + +#if 0 +static void +write2stderr( const char *s ) +{ + write( 2, s, strlen(s) ); +} + + +static void +do_die(int rc, const char *text ) +{ + write2stderr("\nFatal error: "); + write2stderr(text); + write2stderr("\n"); + abort(); +} +#endif + + +static void +do_logv( int level, const char *fmt, va_list arg_ptr ) +{ + switch ( level ) { + case MY_LOG_CONT: break; + case MY_LOG_INFO: break; + case MY_LOG_WARN: break; + case MY_LOG_ERROR: break; + case MY_LOG_FATAL: fputs("Fatal: ",stderr ); break; + case MY_LOG_BUG: fputs("Ohhhh jeeee: ", stderr); break; + case MY_LOG_DEBUG: fputs("DBG: ", stderr ); break; + default: fprintf(stderr,"[Unknown log level %d]: ", level ); break; + } + vfprintf(stderr,fmt,arg_ptr) ; + + if( level == MY_LOG_FATAL ) + exit(2); + if( level == MY_LOG_BUG ) + abort(); +} + +static void +do_log( int level, const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( level, fmt, arg_ptr ); + va_end(arg_ptr); +} + + + +void +log_info( const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( MY_LOG_INFO, fmt, arg_ptr ); + va_end(arg_ptr); +} + +void +log_error( const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( MY_LOG_ERROR, fmt, arg_ptr ); + va_end(arg_ptr); +} + + +void +log_fatal( const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( MY_LOG_FATAL, fmt, arg_ptr ); + va_end(arg_ptr); + abort(); /* never called, bugs it makes the compiler happy */ +} + +void +log_bug( const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( MY_LOG_BUG, fmt, arg_ptr ); + va_end(arg_ptr); + abort(); /* never called, bugs it makes the compiler happy */ +} + +void +log_debug( const char *fmt, ... ) +{ + va_list arg_ptr ; + + va_start( arg_ptr, fmt ) ; + do_logv( MY_LOG_DEBUG, fmt, arg_ptr ); + va_end(arg_ptr); +} + + +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) +void +bug_at( const char *file, int line, const char *func ) +{ + do_log( MY_LOG_BUG, + ("... this is a bug (%s:%d:%s)\n"), file, line, func ); + abort(); /* never called, but it makes the compiler happy */ +} +#else +void +bug_at( const char *file, int line ) +{ + do_log( MY_LOG_BUG, + _("you found a bug ... (%s:%d)\n"), file, line); + abort(); /* never called, but it makes the compiler happy */ +} +#endif + diff --git a/util/logging.h b/util/logging.h new file mode 100644 index 000000000..961751b52 --- /dev/null +++ b/util/logging.h @@ -0,0 +1,41 @@ +/* logging.h + * 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 + */ + +#ifndef LIBUTIL_LOGGING_H +#define LIBUTIL_LOGGING_H + +#include "mischelp.h" + +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) + void bug_at( const char *file, int line, const char *func ) LIBUTIL_GCC_A_NR; +# define BUG() bug_at( __FILE__ , __LINE__, __FUNCTION__ ) +#else + void bug_at( const char *file, int line ); +# define BUG() bug_at( __FILE__ , __LINE__ ) +#endif + +void log_bug( const char *fmt, ... ) LIBUTIL_GCC_A_NR_PRINTF(1,2); +void log_fatal( const char *fmt, ... ) LIBUTIL_GCC_A_NR_PRINTF(1,2); +void log_error( const char *fmt, ... ) LIBUTIL_GCC_A_PRINTF(1,2); +void log_info( const char *fmt, ... ) LIBUTIL_GCC_A_PRINTF(1,2); +void log_debug( const char *fmt, ... ) LIBUTIL_GCC_A_PRINTF(1,2); + + +#endif /*LIBUTIL_LOGGING_H*/ diff --git a/util/mischelp.h b/util/mischelp.h new file mode 100644 index 000000000..1c83da1be --- /dev/null +++ b/util/mischelp.h @@ -0,0 +1,42 @@ +/* mischelp.h + * 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 + */ + +#ifndef LIBUTIL_MISCHELP_H +#define LIBUTIL_MISCHHELP_H + + +#define DIM(v) (sizeof(v)/sizeof((v)[0])) +#define DIMof(type,member) DIM(((type *)0)->member) + + +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) +# define LIBUTIL_GCC_A_NR __attribute__ ((noreturn)) +# define LIBUTIL_GCC_A_PRINTF( f, a ) __attribute__ ((format (printf,f,a))) +# define LIBUTIL_GCC_A_NR_PRINTF( f, a ) \ + __attribute__ ((noreturn, format (printf,f,a))) +#else +# define LIBUTIL_GCC_A_NR +# define LIBUTIL_GCC_A_PRINTF( f, a ) +# define LIBUTIL_GCC_A_NR_PRINTF( f, a ) +#endif + + + +#endif /*LIBUTIL_MISCHELP_H*/ diff --git a/util/strgutil.c b/util/strgutil.c index ea9527bd8..a20ba2c75 100644 --- a/util/strgutil.c +++ b/util/strgutil.c @@ -189,117 +189,6 @@ strlist_last( STRLIST node ) -/**************** - * look for the substring SUB in buffer and return a pointer to that - * substring in BUF or NULL if not found. - * Comparison is case-insensitive. - */ -const char * -memistr( const char *buf, size_t buflen, const char *sub ) -{ - const byte *t, *s ; - size_t n; - - for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) - if( toupper(*t) == toupper(*s) ) { - for( buf=t++, buflen = n--, s++; - n && toupper(*t) == toupper(*s); t++, s++, n-- ) - ; - if( !*s ) - return buf; - t = buf; n = buflen; s = sub ; - } - - return NULL ; -} - -/**************** - * Wie strncpy(), aber es werden maximal n-1 zeichen kopiert und ein - * '\0' angehängt. Ist n = 0, so geschieht nichts, ist Destination - * gleich NULL, so wird via m_alloc Speicher besorgt, ist dann nicht - * genügend Speicher vorhanden, so bricht die funktion ab. - */ -char * -mem2str( char *dest , const void *src , size_t n ) -{ - char *d; - const char *s; - - if( n ) { - if( !dest ) - dest = m_alloc( n ) ; - d = dest; - s = src ; - for(n--; n && *s; n-- ) - *d++ = *s++; - *d = '\0' ; - } - - return dest ; -} - - -/**************** - * remove leading and trailing white spaces - */ -char * -trim_spaces( char *str ) -{ - char *string, *p, *mark; - - string = str; - /* find first non space character */ - for( p=string; *p && isspace( *(byte*)p ) ; p++ ) - ; - /* move characters */ - for( (mark = NULL); (*string = *p); string++, p++ ) - if( isspace( *(byte*)p ) ) { - if( !mark ) - mark = string ; - } - else - mark = NULL ; - if( mark ) - *mark = '\0' ; /* remove trailing spaces */ - - return str ; -} - - - -unsigned -trim_trailing_chars( byte *line, unsigned len, const char *trimchars ) -{ - byte *p, *mark; - unsigned n; - - for(mark=NULL, p=line, n=0; n < len; n++, p++ ) { - if( strchr(trimchars, *p ) ) { - if( !mark ) - mark = p; - } - else - mark = NULL; - } - - if( mark ) { - *mark = 0; - return mark - line; - } - return len; -} - -/**************** - * remove trailing white spaces and return the length of the buffer - */ -unsigned -trim_trailing_ws( byte *line, unsigned len ) -{ - return trim_trailing_chars( line, len, " \t\r\n" ); -} - - - int string_count_chr( const char *string, int c ) { @@ -606,46 +495,3 @@ utf8_to_native( const char *string, size_t length ) } - -/********************************************* - ********** missing string functions ********* - *********************************************/ - -#ifndef HAVE_STPCPY -char * -stpcpy(char *a,const char *b) -{ - while( *b ) - *a++ = *b++; - *a = 0; - - return (char*)a; -} -#endif - -#ifndef HAVE_STRLWR -char * -strlwr(char *s) -{ - char *p; - for(p=s; *p; p++ ) - *p = tolower(*p); - return s; -} -#endif - -/**************** - * mingw32/cpd has a memicmp() - */ -#ifndef HAVE_MEMICMP -int -memicmp( const char *a, const char *b, size_t n ) -{ - for( ; n; n--, a++, b++ ) - if( *a != *b && toupper(*(const byte*)a) != toupper(*(const byte*)b) ) - return *(const byte *)a - *(const byte*)b; - return 0; -} -#endif - - diff --git a/util/stringhelp.c b/util/stringhelp.c new file mode 100644 index 000000000..16908f9a3 --- /dev/null +++ b/util/stringhelp.c @@ -0,0 +1,197 @@ +/* stringhelp.c - standard string helper functions + * 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 <stdlib.h> +#include <string.h> +#include <ctype.h> + +#include "libutil-config.h" +#include "stringhelp.h" + + +/**************** + * look for the substring SUB in buffer and return a pointer to that + * substring in BUF or NULL if not found. + * Comparison is case-insensitive. + */ +const char * +memistr( const char *buf, size_t buflen, const char *sub ) +{ + const byte *t, *s ; + size_t n; + + for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) + if( toupper(*t) == toupper(*s) ) { + for( buf=t++, buflen = n--, s++; + n && toupper(*t) == toupper(*s); t++, s++, n-- ) + ; + if( !*s ) + return buf; + t = buf; n = buflen; s = sub ; + } + + return NULL ; +} + +/**************** + * Wie strncpy(), aber es werden maximal n-1 zeichen kopiert und ein + * '\0' angehängt. Ist n = 0, so geschieht nichts, ist Destination + * gleich NULL, so wird via libutil_malloc Speicher besorgt, ist dann nicht + * genügend Speicher vorhanden, so bricht die funktion ab. + */ +char * +mem2str( char *dest , const void *src , size_t n ) +{ + char *d; + const char *s; + + if( n ) { + if( !dest ) + dest = libutil_xmalloc( n ) ; + d = dest; + s = src ; + for(n--; n && *s; n-- ) + *d++ = *s++; + *d = '\0' ; + } + + return dest ; +} + + +/**************** + * remove leading and trailing white spaces + */ +char * +trim_spaces( char *str ) +{ + char *string, *p, *mark; + + string = str; + /* find first non space character */ + for( p=string; *p && isspace( *(byte*)p ) ; p++ ) + ; + /* move characters */ + for( (mark = NULL); (*string = *p); string++, p++ ) + if( isspace( *(byte*)p ) ) { + if( !mark ) + mark = string ; + } + else + mark = NULL ; + if( mark ) + *mark = '\0' ; /* remove trailing spaces */ + + return str ; +} + + + +unsigned +trim_trailing_chars( byte *line, unsigned len, const char *trimchars ) +{ + byte *p, *mark; + unsigned n; + + for(mark=NULL, p=line, n=0; n < len; n++, p++ ) { + if( strchr(trimchars, *p ) ) { + if( !mark ) + mark = p; + } + else + mark = NULL; + } + + if( mark ) { + *mark = 0; + return mark - line; + } + return len; +} + +/**************** + * remove trailing white spaces and return the length of the buffer + */ +unsigned +trim_trailing_ws( byte *line, unsigned len ) +{ + return trim_trailing_chars( line, len, " \t\r\n" ); +} + + + +/********************************************* + ********** missing string functions ********* + *********************************************/ + +#ifndef HAVE_STPCPY +char * +stpcpy(char *a,const char *b) +{ + while( *b ) + *a++ = *b++; + *a = 0; + + return (char*)a; +} +#endif + +#ifndef HAVE_STRLWR +char * +strlwr(char *s) +{ + char *p; + for(p=s; *p; p++ ) + *p = tolower(*p); + return s; +} +#endif + + +#ifndef HAVE_STRCASECMP +int +strcasecmp( const char *a, const char *b ) +{ + for( ; *a && *b; a++, b++ ) { + if( *a != *b && toupper(*a) != toupper(*b) ) + break; + } + return *(const byte*)a - *(const byte*)b; +} +#endif + + +/**************** + * mingw32/cpd has a memicmp() + */ +#ifndef HAVE_MEMICMP +int +memicmp( const char *a, const char *b, size_t n ) +{ + for( ; n; n--, a++, b++ ) + if( *a != *b && toupper(*(const byte*)a) != toupper(*(const byte*)b) ) + return *(const byte *)a - *(const byte*)b; + return 0; +} +#endif + + + diff --git a/util/stringhelp.h b/util/stringhelp.h new file mode 100644 index 000000000..98b3ad551 --- /dev/null +++ b/util/stringhelp.h @@ -0,0 +1,59 @@ +/* stringhelp.h + * 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 + */ + +#ifndef LIBUTIL_STRINGHELP_H +#define LIBUTIL_STRINGHELP_H + + +const char *memistr( const char *buf, size_t buflen, const char *sub ); +char *mem2str( char *, const void *, size_t); +char *trim_spaces( char *string ); +unsigned int trim_trailing_chars( byte *line, unsigned len, + const char *trimchars); +unsigned int trim_trailing_ws( byte *line, unsigned len ); + + +#ifndef HAVE_MEMICMP +int memicmp( const char *a, const char *b, size_t n ); +#endif +#ifndef HAVE_STPCPY +char *stpcpy(char *a,const char *b); +#endif +#ifndef HAVE_STRLWR +char *strlwr(char *a); +#endif +#ifndef HAVE_STRTOUL + #define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c))) +#endif +#ifndef HAVE_MEMMOVE + #define memmove(d, s, n) bcopy((s), (d), (n)) +#endif +#ifndef HAVE_STRICMP + #define stricmp(a,b) strcasecmp( (a), (b) ) +#endif + + +#ifndef STR + #define STR(v) #v +#endif +#define STR2(v) STR(v) + + +#endif /*LIBUTIL_STRINGHELP_H*/ diff --git a/util/xmalloc.c b/util/xmalloc.c new file mode 100644 index 000000000..21e23d0dc --- /dev/null +++ b/util/xmalloc.c @@ -0,0 +1,70 @@ +/* xmalloc.c - standard malloc wrappers + * 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 <stdio.h> + +#include "libutil-config.h" +#include "xmalloc.h" + +static void +out_of_core(void) +{ + fputs("\nfatal: out of memory\n", stderr ); + exit(2); +} + + +void * +xmalloc( size_t n ) +{ + void *p = malloc( n ); + if( !p ) + out_of_core(); + return p; +} + +void * +xrealloc( void *a, size_t n ) +{ + void *p = realloc( a, n ); + if( !p ) + out_of_core(); + return p; +} + +void * +xcalloc( size_t n, size_t m ) +{ + void *p = calloc( n, m ); + if( !p ) + out_of_core(); + return p; +} + +char * +xstrdup( const char *string ) +{ + void *p = xmalloc( strlen(string)+1 ); + strcpy( p, string ); + return p; +} + diff --git a/util/xmalloc.h b/util/xmalloc.h new file mode 100644 index 000000000..4a33d36a5 --- /dev/null +++ b/util/xmalloc.h @@ -0,0 +1,30 @@ +/* xmalloc.h + * 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 + */ + +#ifndef LIBUTIL_XMALLOC_H +#define LIBUTIL_XMALLOC_H + +void *xmalloc( size_t n ); +void *xrealloc( void *a, size_t n ); +void *xcalloc( size_t n, size_t m ); +char *xstrdup( const char *string ); + + +#endif /*LIBUTIL_XMALLOC_H*/ |