summaryrefslogtreecommitdiff
path: root/src/misc.c
AgeCommit message (Collapse)AuthorFilesLines
2016-02-19Add helper function _gcry_strtokenize.Werner Koch1-0/+73
* src/misc.c (_gcry_strtokenize): New. -- The code has been taken from GnuPG and re-licensed to LPGLv2+ by me as its original author. Minor changes for use in Libgcrypt. Signed-off-by: Werner Koch <wk@gnupg.org>
2013-12-12Remove macro hacks for internal vs. external functions. Part 2 and last.Werner Koch1-3/+3
* src/visibility.h: Remove remaining define/undef hacks for symbol visibility. Add macros to detect the use of the public functions. Change all affected functions by replacing them by the x-macros. * src/g10lib.h: Add internal prototypes. (xtrymalloc, xtrycalloc, xtrymalloc_secure, xtrycalloc_secure) (xtryrealloc, xtrystrdup, xmalloc, xcalloc, xmalloc_secure) (xcalloc_secure, xrealloc, xstrdup, xfree): New macros. -- The use of xmalloc/xtrymalloc/xfree is a more common pattern than the gcry_free etc. functions. Those functions behave like those defined by C and thus for better readability we use these macros and not the underscore prefixed functions. Signed-off-by: Werner Koch <wk@gnupg.org>
2013-12-05Remove macro hacks for internal vs. external functions. Part 1.Werner Koch1-7/+6
* src/visibility.h: Remove almost all define/undef hacks for symbol visibility. Add macros to detect the use of the public functions. Change all affected functions by prefixing them explicitly with an underscore and change all internal callers to call the underscore prefixed versions. Provide convenience macros from sexp and mpi functions. * src/visibility.c: Change all functions to use only gpg_err_code_t and translate to gpg_error_t only in visibility.c. -- The use of the macro magic made if hard to follow the function calls in the source. It was not easy to see if an internal or external function (as defined by visibility.c) was called. The change is quite large but hopefully makes Libgcrypt easier to maintain. Some function have not yet been fixed; this will be done soon. Because Libgcrypt does no make use of any other libgpg-error using libraries it is useless to always translate between gpg_error_t and gpg_err_code_t (i.e with and w/o error source identifier). This translation has no mostly be moved to the function wrappers in visibility.c. An additional advantage of using gpg_err_code_t is that comparison can be done without using gpg_err_code(). I am sorry for that large patch, but a series of patches would actually be more work to audit. Signed-off-by: Werner Koch <wk@gnupg.org>
2013-10-10Prevent tail call optimization with _gcry_burn_stackJussi Kivilinna1-1/+8
* configure.ac: New check, HAVE_GCC_ASM_VOLATILE_MEMORY. * src/g10lib.h (_gcry_burn_stack): Rename to __gcry_burn_stack. (__gcry_burn_stack_dummy): New. (_gcry_burn_stack): New macro. * src/misc.c (_gcry_burn_stack): Rename to __gcry_burn_stack. (__gcry_burn_stack_dummy): New. -- Tail call optimization can turn _gcry_burn_stack call in to tail jump. When this happens, stack pointer is restored to initial state of current function. This causes problem for _gcry_burn_stack because its callers do not count in current function stack depth. One solution is to prevent gcry_burn_stack being tail optimized by inserting dummy function call behind it. Another would be to add memory barrier 'asm volatile("":::"memory")' behind every _gcry_burn_stack call. This however requires GCC asm support from compiler. Patch adds detection for memory barrier support and when available uses memory barrier to prevent when tail call optimization. If not available dummy function call is used instead. Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
2013-09-30ecc: Prepare for future Ed25519 optimization.Werner Koch1-1/+2
* mpi/ec-ed25519.c: New but empty file. * mpi/ec-internal.h: New. * mpi/ec.c: Include ec-internal.h. (ec_mod): New. (ec_addm): Use ec_mod. (ec_mulm): Remove commented code. Use ec_mod. (ec_subm): Call simple sub. (ec_pow2): Use ec_mulm. (ec_mul2): New. (dup_point_weierstrass): Use ec_mul2. (dup_point_twistededwards): Add special case for a == -1. Use ec_mul2. (add_points_weierstrass): Use ec_mul2. (add_points_twistededwards): Add special case for a == -1. (_gcry_mpi_ec_curve_point): Ditto. (ec_p_init): Add hack to test Barrett functions. * src/ec-context.h (mpi_ec_ctx_s): Add P_BARRETT. * mpi/mpi-mod.c (_gcry_mpi_mod_barrett): Fix sign problem. Signed-off-by: Werner Koch <wk@gnupg.org>
2013-09-30log: Try to print s-expressions in a more compact format.Werner Koch1-4/+29
* src/misc.c (count_closing_parens): New. (_gcry_log_printsxp): Use new function. * mpi/ec.c (_gcry_mpi_point_log): Take care of a NULL point. Signed-off-by: Werner Koch <wk@gnupg.org>
2013-09-30Add logging functions to the API.Werner Koch1-1/+50
* src/gcrypt.h.in (_GCRY_GCC_ATTR_PRINTF): New. (gcry_log_debug, gcry_log_debughex, gcry_log_debugmpi): New. (gcry_log_debugpnt, gcry_log_debugsxp): New. * src/visibility.c (gcry_log_debug): New. (gcry_log_debughex, gcry_log_debugmpi, gcry_log_debugpnt): New. (gcry_log_debugsxp): New. * src/libgcrypt.def, src/libgcrypt.vers: Add new functions. * src/misc.c (_gcry_logv): Make public. (_gcry_log_printsxp): New. * src/g10lib.h (log_printsxp): New macro. -- For debugging applications it is often required to dump certain data structures. Libgcrypt uses several internal functions for this. To avoid re-implementing everything in the caller, we now provide access to some of those functions. Signed-off-by: Werner Koch <wk@gnupg.org>
2013-09-16ecc: Implement Curve Ed25519 signing and verification.Werner Koch1-1/+1
* cipher/ecc-curves.c (domain_parms): Add curve "Ed25519". * cipher/ecc.c (reverse_buffer): New. (eddsa_encodempi): New. (eddsa_encodepoint): New. (eddsa_decodepoint): New. (sign_eddsa): Implement. (verify_eddsa): Implement. (ecc_sign): Init unused Q. Pass public key to sign_eddsa. (ecc_verify): Init pk.Q if not used. Pass public key verbatim to verify_eddsa. * cipher/pubkey.c (sexp_elements_extract): Add arg OPAQUE. Change all callers to pass 0. (sexp_to_sig): Add arg OPAQUE and pass it to sexp_elements_extract. (sexp_data_to_mpi): Allow for a zero length "value". (gcry_pk_verify): Reorder parameter processing. Pass OPAQUE flag as required. * mpi/ec.c (ec_invm): Print a warning if the inverse does not exist. (_gcry_mpi_ec_get_affine): Implement for our Twisted Edwards curve model. (dup_point_twistededwards): Implement. (add_points_twistededwards): Implement. (_gcry_mpi_ec_mul_point): Support Twisted Edwards. * mpi/mpicoder.c (do_get_buffer): Add arg FILL_LE. (_gcry_mpi_get_buffer): Ditto. Change all callers. (_gcry_mpi_get_secure_buffer): Ditto. * src/sexp.c (_gcry_sexp_nth_opaque_mpi): New. * tests/t-ed25519.c: New. * tests/t-ed25519.inp: New. * tests/t-mpi-point.c (basic_ec_math_simplified): Print some output only in debug mode. (twistededwards_math): New test. (main): Call new test. -- This is a non optimized version which takes far too long. On my X220 Thinkpad the 1024 test cases take 14 seconds (12 with --sign-with-pk). There should be a lot of room for improvements. Signed-off-by: Werner Koch <wk@gnupg.org>
2013-09-12Fix _gcry_log_printmpi to print 00 instead of a sole sign.Werner Koch1-1/+4
* src/misc.c: Special case an mpi length of 0.
2013-09-11Streamline the use of the internal mpi and hex debug functions.Werner Koch1-9/+81
* 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>
2013-09-05Change _gcry_burn_stack take burn depth as unsigned integerJussi Kivilinna1-5/+5
* src/misc.c (_gcry_burn_stack): Change to handle 'unsigned int' bytes. -- Unsigned integer is better here for code generation because we can now avoid possible branching caused by (bytes <= 0) check. Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
2013-09-04Make _gcry_burn_stack use variable length arrayJussi Kivilinna1-1/+8
* configure.ac (HAVE_VLA): Add check. * src/misc.c (_gcry_burn_stack) [HAVE_VLA]: Add VLA code. -- Some gcc versions convert _gcry_burn_stack into loop that overwrites the same 64-byte stack buffer instead of burn stack deeper. It's argued at GCC bugzilla that _gcry_burn_stack is doing wrong thing here [1] and that this kind of optimization is allowed. So lets fix _gcry_burn_stack by using variable length array when VLAs are supported by compiler. This should ensure proper stack burning to the requested depth and avoid GCC loop optimizations. [1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52285 Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
2013-08-17Remove burn_stack optimizationJussi Kivilinna1-26/+1
* src/misc.c (_gcry_burn_stack): Remove SIZEOF_UNSIGNED_LONG == 4 or 8 optimization. -- At least GCC 4.6 on Debian Wheezy (armhf) generates wrong code for burn_stack, causing recursive structure to be transformed in to iterative without updating stack pointer between iterations. Therefore only first 64 bytes of stack get zeroed. This appears to be fixed in GCC 4.7, but lets play this safe and remove this optimization. Better approach would probably be to add architecture specific assembly routine(s) that replace this generic function. Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
2013-06-09Optimize _gcry_burn_stack for 32-bit and 64-bit architecturesJussi Kivilinna1-0/+26
* src/misc.c (_gcry_burn_stack): Add optimization for 32-bit and 64-bit architectures. -- Busy looping 'tests/benchmark --cipher-repetitions 10 cipher blowfish' on ARM Cortex-A8 shows that _gcry_burn_stack takes 21% of CPU time. With this patch, that number drops to 3.4%. On AMD64 (Intel i5-4570) CPU usage for _gcry_burn_stack in the same test drops from 3.5% to 1.1%. Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
2012-08-16Replace deliberate division by zero with _gcry_divide_by_zero.Xi Wang1-0/+8
* mpi/mpi-pow.c: Replace 1 / msize. * mpi/mpih-div.c: Replace 1 / dsize. * src/misc.c: Add _gcry_divide_by_zero. -- 1) Division by zero doesn't "provoke a signal" on architectures like PowerPC. 2) C compilers like clang will optimize away these divisions, even though the code tries "to make the compiler not remove" them. This patch redirects these cases to _gcry_divide_by_zero.
2011-02-04Nuked almost all trailing whitespace.Werner Koch1-7/+7
Check and install the standard git pre-commit hook.
2010-04-192010-04-19 Marcus Brinkmann <marcus@g10code.de>Marcus Brinkmann1-1/+3
* misc.c (write2stderr): Dummy variable to silence gcc warning.
2008-08-21Finished the X9.31 RNG implementations.Werner Koch1-0/+21
2008-08-20Replace assert calls by a new gcry_assert at most places.Werner Koch1-2/+18
2008-08-19A whole bunch of changes to eventually support Werner Koch1-1/+3
FIPS restricted mode. Also some documentation improvements and other minor enhancements. See the ChangeLogs. Stay tuned.
2007-12-03Preparing a releaseWerner Koch1-22/+27
2007-04-30Dropped GCRYCTL_DUMP_CONFIG in favor of GCRYCTL_PRINT_CONFIG.Werner Koch1-0/+12
2004-08-232004-08-23 Moritz Schulte <moritz@g10code.com>Moritz Schulte1-1/+0
* global.c: Do not include <assert.h>. * sexp.c: Likewise. * module.c: Likewise. * misc.c: Likewise.
2004-04-15About to release 1.2Werner Koch1-4/+4
2003-10-31* acinclude.m4 (AC_CHECK_PTH): Added.Werner Koch1-2/+2
* configure.ac: Use it here instead of the generic lib test. Bumbed LT vesion to C9/A2/R0. * dsa.c (verify): s/exp/ex/ due to shadowing of a builtin. * elgamal.c (verify): Ditto. * ac.c (gcry_ac_data_get_index): s/index/idx/ (gcry_ac_data_copy_internal): Remove the cast in _gcry_malloc. (gcry_ac_data_add): Must use gcry_realloc instead of realloc. * pubkey.c (sexp_elements_extract): s/index/idx/ as tribute to the forehackers. (gcry_pk_encrypt): Removed shadowed definition of I. Reordered arguments to malloc for clarity. (gcry_pk_sign, gcry_pk_genkey): Ditto. * primegen.c (prime_generate_internal): s/random/randomlevel/. * i386/mpih-rshift.S, i386/mpih-lshift.S: Use %dl and not %edx for testb; this avoids an assembler warning. * mpi-pow.c (gcry_mpi_powm): s/exp/expo/ to avoid shadowing warning. * autogen.sh: Allow to override the tool name. Do not run libtoolize. Update required version numbers. * libgcrypt.vers (_gcry_generate_elg_prime): Removed this symbol; gnutls does not need it anymore. * secmem.c (mb_get_new): s/pool/block/ due to global pool. * misc.c (gcry_set_log_handler): s/logf/f/ to avoid shadowing warning against a builtin. * ath-pth-compat.c: cast pth_connect to get rid of the const prototype. * basic.c (check_aes128_cbc_cts_cipher): Make it a prototype * ac.c (check_run): Comment unused variable.
2003-06-152003-06-15 Moritz Schulte <moritz@g10code.com>Moritz Schulte1-10/+9
* global.c (gcry_control): Fixed typo. * misc.c (_gcry_fatal_error): Use gpg_strerror instead of gcry_strerror. * types.h (STRLIST): Removed type since it is not used.
2003-03-062003-03-06 Moritz Schulte <mo@g10code.com>Moritz Schulte1-2/+2
* secmem.h (GCRY_SECMEM_FLAG_NO_WARNING, GCRY_SECMEM_FLAG_SUSPEND_WARNING): New symbols. * global.c (gcry_control): Use GCRY_SECMEM_FLAG_{NO,SUSPEND}_WARNING, instead of hard-coded values. * secmem.c (_gcry_secmem_set_flags): Likewise. * secmem.c (_gcry_secmem_get_flags): Likewise.
2003-03-042003-03-03 Moritz Schulte <moritz@g10code.com>Moritz Schulte1-1/+1
* misc.c: Removed old FIXME, since there is already a function to set the value of `verbosity_level'. * gcrypt.h: Removed enumeration list: gcry_ctl_cmds. New enumeration lists: gcry_global_control_cmds, gcry_control_cmds, gcry_info_cmds, gcry_algo_info_cmds. 2003-03-02 Moritz Schulte <moritz@g10code.com> * gcrypt.h (gcry_cipher_reset): New macro for resetting a handle.
2003-03-012003-02-28 Moritz Schulte <moritz@g10code.com>Moritz Schulte1-1/+12
* secmem.c (DEFAULT_PAGESIZE): New symbol. (init_pool): Use DEFAULT_PAGESIZE. 2003-02-23 Moritz Schulte <moritz@g10code.com> * secmem.h: Fix typo in declaration of _gcry_secmem_term. * sexp.c: Move macro definitions of `digitp', `octdigit', `alphap' and `hexdigit' ... * g10lib.h: ... here. * misc.c (_gcry_burn_stack): New function (former name: burn_stack). * g10lib.h (burn_stack): Declare _gcry_burn_stack().
2002-05-21* misc.c (_gcry_log_printf): Don't initialize a va_list. Noted byWerner Koch1-1/+1
Jeff Johnson.
2002-05-14Change the license to the LGPL.Werner Koch1-7/+7
2001-12-06* misc.c (_gcry_log_printf): New.Werner Koch1-0/+12
* sexp.c (dump_string,gcry_sexp_dump): Use logging functions instead of stderr.
2001-05-31The first libgcrypt only release.Werner Koch1-22/+22
2000-12-21Changed program name in all filesWerner Koch1-3/+3
1999-12-08See ChangeLog: Wed Dec 8 21:58:32 CET 1999 Werner KochWerner Koch1-4/+17
1999-11-19See ChangeLog: Fri Nov 19 17:15:20 CET 1999 Werner KochWerner Koch1-0/+123
1999-11-15See ChangeLog: Mon Nov 15 21:36:02 CET 1999 Werner KochWerner Koch1-7/+40
1999-02-19See ChangeLog: Fri Feb 19 15:49:15 CET 1999 Werner KochWerner Koch1-3/+3
1999-02-10See ChangeLog: Wed Feb 10 17:15:39 CET 1999 Werner KochWerner Koch1-0/+12
1998-12-29See ChangeLog: Tue Dec 29 14:41:47 CET 1998 Werner KochWerner Koch1-0/+38