diff options
author | Werner Koch <wk@gnupg.org> | 2013-09-07 10:06:46 +0200 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2013-09-11 17:36:34 +0200 |
commit | e35ed615acc624a8b6c07576ea0650aac2bdb0db (patch) | |
tree | 7c90aa5c5dbf091108ca749f5fc8220583bba8e0 | |
parent | f3bca0c77c4979504f95fdbc618f7458e61e3e45 (diff) | |
download | libgcrypt-e35ed615acc624a8b6c07576ea0650aac2bdb0db.tar.gz |
Streamline the use of the internal mpi and hex debug functions.
* mpi/mpicoder.c (gcry_mpi_dump): Remove.
(_gcry_log_mpidump): Remove.
* src/misc.c (_gcry_log_printhex): Factor all code out to ...
(do_printhex): new. Add line wrapping a and compact printing.
(_gcry_log_printmpi): New.
* src/mpi.h (log_mpidump): Remove macro.
* src/g10lib.h (log_mpidump): Add compatibility macro.
(log_printmpi): New macro
* src/visibility.c (gcry_mpi_dump): Call _gcry_log_printmpi.
* cipher/primegen.c (prime_generate_internal): Replace gcry_mpi_dump
by log_printmpi.
(gcry_prime_group_generator): Ditto.
* cipher/pubkey.c: Remove extra colons from log_mpidump call.
* cipher/rsa.c (stronger_key_check): Use log_printmpi.
--
The values to debug get longer and longer and the different debug
functions made it hard to check them out. Now MPIs and hex buffers are
printed very similar. Lines may now wrap with an backslash as
indicator. MPIs are distinguished from plain buffers in the output by
always using a sign.
Signed-off-by: Werner Koch <wk@gnupg.org>
-rw-r--r-- | cipher/primegen.c | 18 | ||||
-rw-r--r-- | cipher/pubkey.c | 22 | ||||
-rw-r--r-- | cipher/rsa.c | 8 | ||||
-rw-r--r-- | mpi/mpicoder.c | 59 | ||||
-rw-r--r-- | src/g10lib.h | 5 | ||||
-rw-r--r-- | src/misc.c | 90 | ||||
-rw-r--r-- | src/mpi.h | 2 | ||||
-rw-r--r-- | src/visibility.c | 2 |
8 files changed, 107 insertions, 99 deletions
diff --git a/cipher/primegen.c b/cipher/primegen.c index 3610ae7e..3c9f14be 100644 --- a/cipher/primegen.c +++ b/cipher/primegen.c @@ -655,11 +655,7 @@ prime_generate_internal (int need_q_factor, { mpi_add_ui (g, g, 1); if (DBG_CIPHER) - { - log_debug ("checking g:"); - gcry_mpi_dump (g); - log_printf ("\n"); - } + log_printmpi ("checking g", g); else progress('^'); for (i = 0; i < n + 2; i++) @@ -1242,11 +1238,7 @@ gcry_prime_group_generator (gcry_mpi_t *r_g, gcry_mpi_add_ui (g, g, 1); if (DBG_CIPHER) - { - log_debug ("checking g:"); - gcry_mpi_dump (g); - log_debug ("\n"); - } + log_printmpi ("checking g", g); else progress('^'); @@ -1835,9 +1827,9 @@ _gcry_generate_fips186_3_prime (unsigned int pbits, unsigned int qbits, /* Step 12: Save p, q, counter and seed. */ log_debug ("fips186-3 pbits p=%u q=%u counter=%d\n", mpi_get_nbits (prime_p), mpi_get_nbits (prime_q), counter); - log_printhex("fips186-3 seed:", seed, seedlen); - log_mpidump ("fips186-3 prime p", prime_p); - log_mpidump ("fips186-3 prime q", prime_q); + log_printhex ("fips186-3 seed", seed, seedlen); + log_printmpi ("fips186-3 p", prime_p); + log_printmpi ("fips186-3 q", prime_q); if (r_q) { *r_q = prime_q; diff --git a/cipher/pubkey.c b/cipher/pubkey.c index 141d6567..98e3074f 100644 --- a/cipher/pubkey.c +++ b/cipher/pubkey.c @@ -632,8 +632,8 @@ pubkey_encrypt (int algorithm, gcry_mpi_t *resarr, gcry_mpi_t data, { log_debug ("pubkey_encrypt: algo=%d\n", algorithm); for(i = 0; i < pubkey_get_npkey (algorithm); i++) - log_mpidump (" pkey:", pkey[i]); - log_mpidump (" data:", data); + log_mpidump (" pkey", pkey[i]); + log_mpidump (" data", data); } ath_mutex_lock (&pubkeys_registered_lock); @@ -653,7 +653,7 @@ pubkey_encrypt (int algorithm, gcry_mpi_t *resarr, gcry_mpi_t data, if (!rc && DBG_CIPHER && !fips_mode ()) { for(i = 0; i < pubkey_get_nenc (algorithm); i++) - log_mpidump(" encr:", resarr[i] ); + log_mpidump(" encr", resarr[i] ); } return rc; } @@ -680,9 +680,9 @@ pubkey_decrypt (int algorithm, gcry_mpi_t *result, gcry_mpi_t *data, { log_debug ("pubkey_decrypt: algo=%d\n", algorithm); for(i = 0; i < pubkey_get_nskey (algorithm); i++) - log_mpidump (" skey:", skey[i]); + log_mpidump (" skey", skey[i]); for(i = 0; i < pubkey_get_nenc (algorithm); i++) - log_mpidump (" data:", data[i]); + log_mpidump (" data", data[i]); } ath_mutex_lock (&pubkeys_registered_lock); @@ -701,7 +701,7 @@ pubkey_decrypt (int algorithm, gcry_mpi_t *result, gcry_mpi_t *data, ath_mutex_unlock (&pubkeys_registered_lock); if (!rc && DBG_CIPHER && !fips_mode ()) - log_mpidump (" plain:", *result); + log_mpidump (" plain", *result); return rc; } @@ -726,8 +726,8 @@ pubkey_sign (int algorithm, gcry_mpi_t *resarr, gcry_mpi_t data, { log_debug ("pubkey_sign: algo=%d\n", algorithm); for(i = 0; i < pubkey_get_nskey (algorithm); i++) - log_mpidump (" skey:", skey[i]); - log_mpidump(" data:", data ); + log_mpidump (" skey", skey[i]); + log_mpidump(" data", data ); } ath_mutex_lock (&pubkeys_registered_lock); @@ -748,7 +748,7 @@ pubkey_sign (int algorithm, gcry_mpi_t *resarr, gcry_mpi_t data, if (!rc && DBG_CIPHER && !fips_mode ()) for (i = 0; i < pubkey_get_nsig (algorithm); i++) - log_mpidump (" sig:", resarr[i]); + log_mpidump (" sig", resarr[i]); return rc; } @@ -1038,7 +1038,7 @@ pkcs1_decode_for_encryption (unsigned char **r_result, size_t *r_resultlen, *r_resultlen = nframe - n; if (DBG_CIPHER) - log_printhex ("value extracted from PKCS#1 block type 2 encoded data:", + log_printhex ("value extracted from PKCS#1 block type 2 encoded data", *r_result, *r_resultlen); return 0; @@ -1481,7 +1481,7 @@ oaep_decode (unsigned char **r_result, size_t *r_resultlen, seed = NULL; if (DBG_CIPHER) - log_printhex ("value extracted from OAEP encoded data:", + log_printhex ("value extracted from OAEP encoded data", *r_result, *r_resultlen); return 0; diff --git a/cipher/rsa.c b/cipher/rsa.c index 8d17129c..e7c880da 100644 --- a/cipher/rsa.c +++ b/cipher/rsa.c @@ -662,7 +662,7 @@ stronger_key_check ( RSA_secret_key *skey ) { log_info ( "RSA Oops: d is wrong - fixed\n"); mpi_set (skey->d, t); - _gcry_log_mpidump (" fixed d", skey->d); + log_printmpi (" fixed d", skey->d); } /* check for correctness of u */ @@ -671,7 +671,7 @@ stronger_key_check ( RSA_secret_key *skey ) { log_info ( "RSA Oops: u is wrong - fixed\n"); mpi_set (skey->u, t); - _gcry_log_mpidump (" fixed u", skey->u); + log_printmpi (" fixed u", skey->u); } log_info ( "RSA secret key check finished\n"); @@ -1043,8 +1043,8 @@ rsa_verify (int algo, gcry_mpi_t hash, gcry_mpi_t *data, gcry_mpi_t *pkey, #ifdef IS_DEVELOPMENT_VERSION if (DBG_CIPHER) { - log_mpidump ("rsa verify result:", result ); - log_mpidump (" hash:", hash ); + log_mpidump ("rsa verify result", result ); + log_mpidump (" hash", hash ); } #endif /*IS_DEVELOPMENT_VERSION*/ if (cmp) diff --git a/mpi/mpicoder.c b/mpi/mpicoder.c index a9259228..ffe6538a 100644 --- a/mpi/mpicoder.c +++ b/mpi/mpicoder.c @@ -176,65 +176,6 @@ mpi_fromstr (gcry_mpi_t val, const char *str) } -/* Dump the value of A in a format suitable for debugging to - Libgcrypt's logging stream. Note that one leading space but no - trailing space or linefeed will be printed. It is okay to pass - NULL for A. Note that this function prints the sign as it is used - internally and won't map -0 to 0. */ -void -gcry_mpi_dump (const gcry_mpi_t a) -{ - int i; - - log_printf (" "); - if (!a) - log_printf ("[MPI_NULL]"); - else if (mpi_is_opaque (a)) - { - unsigned int nbits; - const unsigned char *p; - - p = gcry_mpi_get_opaque (a, &nbits); - log_printf ("[%u bit: ", nbits); - for (i=0; i < (nbits + 7)/8; i++) - log_printf ("%02x", p[i]); - log_printf ("]"); - } - else - { - if (a->sign) - log_printf ( "-"); -#if BYTES_PER_MPI_LIMB == 2 -# define X "4" -#elif BYTES_PER_MPI_LIMB == 4 -# define X "8" -#elif BYTES_PER_MPI_LIMB == 8 -# define X "16" -#elif BYTES_PER_MPI_LIMB == 16 -# define X "32" -#else -# error please define the format here -#endif - for (i=a->nlimbs; i > 0 ; i-- ) - { - log_printf (i != a->nlimbs? "%0" X "lX":"%lX", (ulong)a->d[i-1]); - } -#undef X - if (!a->nlimbs) - log_printf ("0"); - } -} - -/* Convience function used internally. */ -void -_gcry_log_mpidump (const char *text, gcry_mpi_t a) -{ - log_printf ("%s:", text); - gcry_mpi_dump (a); - log_printf ("\n"); -} - - /* Return an allocated buffer with the MPI (msb first). NBYTES receives the length of this buffer. Caller must free the return string. This function returns an allocated buffer with NBYTES set diff --git a/src/g10lib.h b/src/g10lib.h index 6023c607..f8953e7b 100644 --- a/src/g10lib.h +++ b/src/g10lib.h @@ -125,6 +125,7 @@ int _gcry_log_info_with_dummy_fp (FILE *fp, const char *fmt, ... ) void _gcry_log_debug( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); void _gcry_log_printf ( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); void _gcry_log_printhex (const char *text, const void *buffer, size_t length); +void _gcry_log_printmpi (const char *text, gcry_mpi_t mpi); void _gcry_set_log_verbosity( int level ); int _gcry_log_verbosity( int level ); @@ -151,6 +152,10 @@ int _gcry_log_verbosity( int level ); #define log_debug _gcry_log_debug #define log_printf _gcry_log_printf #define log_printhex _gcry_log_printhex +#define log_printmpi _gcry_log_printmpi + +/* Compatibility macro. */ +#define log_mpidump _gcry_log_printmpi /*-- src/hwfeatures.c --*/ @@ -28,6 +28,7 @@ #include "g10lib.h" #include "secmem.h" +#include "mpi.h" static int verbosity_level = 0; @@ -267,26 +268,97 @@ _gcry_log_printf (const char *fmt, ...) } } -/* Print a hexdump of BUFFER. With TEXT of NULL print just the raw - dump, with TEXT an empty string, print a trailing linefeed, - otherwise print an entire debug line. */ -void -_gcry_log_printhex (const char *text, const void *buffer, size_t length) + +/* Helper for _gcry_log_printhex and _gcry_log_printmpi. */ +static void +do_printhex (const char *text, const char *text2, + const void *buffer, size_t length) { + int wrap = 0; + int cnt = 0; + if (text && *text) - log_debug ("%s ", text); + { + wrap = 1; + log_debug ("%s:%s", text, text2); + if (text2[1] == '[' && length && buffer) + { + /* Start with a new line so that we get nice output for + opaque MPIS: + "value: [31 bit]" + " 01020300" */ + log_printf ("\n"); + text2 = " "; + log_debug ("%*s ", (int)strlen(text), ""); + } + } if (length) { const unsigned char *p = buffer; - log_printf ("%02X", *p); - for (length--, p++; length--; p++) - log_printf (" %02X", *p); + for (; length--; p++) + { + log_printf ("%02x", *p); + if (wrap && ++cnt == 32 && length) + { + cnt = 0; + log_printf (" \\\n"); + log_debug ("%*s %*s", + (int)strlen(text), "", (int)strlen(text2), ""); + } + } } if (text) log_printf ("\n"); } +/* Print a hexdump of BUFFER. With TEXT of NULL print just the raw + dump without any wrappping, with TEXT an empty string, print a + trailing linefeed, otherwise print an entire debug line. */ +void +_gcry_log_printhex (const char *text, const void *buffer, size_t length) +{ + do_printhex (text, " ", buffer, length); +} + + +/* Print MPI in hex notation. To make clear that the output is an MPI + a sign is always printed. With TEXT of NULL print just the raw dump + without any wrapping, with TEXT an empty string, print a trailing + linefeed, otherwise print an entire debug line. */ +void +_gcry_log_printmpi (const char *text, gcry_mpi_t mpi) +{ + unsigned char *rawmpi; + unsigned int rawmpilen; + int sign; + + if (!mpi) + do_printhex (text? text:" ", " (null)", NULL, 0); + else if (mpi_is_opaque (mpi)) + { + unsigned int nbits; + const unsigned char *p; + char prefix[30]; + + p = gcry_mpi_get_opaque (mpi, &nbits); + snprintf (prefix, sizeof prefix, " [%u bit]", nbits); + do_printhex (text? text:" ", prefix, p, (nbits+7)/8); + } + else + { + rawmpi = _gcry_mpi_get_buffer (mpi, &rawmpilen, &sign); + if (!rawmpi) + do_printhex (text? text:" ", " [out of core]", NULL, 0); + else + { + do_printhex (text, sign? "-":"+", rawmpi, rawmpilen); + gcry_free (rawmpi); + } + } +} + + void _gcry_burn_stack (unsigned int bytes) { @@ -163,8 +163,6 @@ byte *_gcry_mpi_get_secure_buffer( gcry_mpi_t a, unsigned *nbytes, int *sign ); void _gcry_mpi_set_buffer ( gcry_mpi_t a, const void *buffer, unsigned int nbytes, int sign ); -#define log_mpidump _gcry_log_mpidump - /*-- mpi-add.c --*/ #define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v)) #define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v)) diff --git a/src/visibility.c b/src/visibility.c index 48725ffe..3c8041ab 100644 --- a/src/visibility.c +++ b/src/visibility.c @@ -350,7 +350,7 @@ gcry_mpi_aprint (enum gcry_mpi_format format, void gcry_mpi_dump (const gcry_mpi_t a) { - _gcry_mpi_dump (a); + _gcry_log_printmpi (NULL, a); } void |