diff --git a/src/g10lib.h b/src/g10lib.h index c64cbcf2..9b214781 100644 --- a/src/g10lib.h +++ b/src/g10lib.h @@ -1,484 +1,476 @@ /* g10lib.h - Internal definitions for libgcrypt * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005 * 2007, 2011 Free Software Foundation, Inc. * * This file is part of Libgcrypt. * * Libgcrypt is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser general Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Libgcrypt 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . */ /* This header is to be used inside of libgcrypt in place of gcrypt.h. This way we can better distinguish between internal and external usage of gcrypt.h. */ #ifndef G10LIB_H #define G10LIB_H 1 #ifdef _GCRYPT_H #error gcrypt.h already included #endif #ifndef _GCRYPT_IN_LIBGCRYPT #error something is wrong with config.h #endif #include #include #include "visibility.h" #include "types.h" /* Attribute handling macros. */ #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) #define JNLIB_GCC_M_FUNCTION 1 #define JNLIB_GCC_A_NR __attribute__ ((noreturn)) #define JNLIB_GCC_A_PRINTF( f, a ) __attribute__ ((format (printf,f,a))) #define JNLIB_GCC_A_NR_PRINTF( f, a ) \ __attribute__ ((noreturn, format (printf,f,a))) #define GCC_ATTR_NORETURN __attribute__ ((__noreturn__)) #else #define JNLIB_GCC_A_NR #define JNLIB_GCC_A_PRINTF( f, a ) #define JNLIB_GCC_A_NR_PRINTF( f, a ) #define GCC_ATTR_NORETURN #endif #if __GNUC__ >= 3 /* According to glibc this attribute is available since 2.8 however we better play safe and use it only with gcc 3 or newer. */ #define GCC_ATTR_FORMAT_ARG(a) __attribute__ ((format_arg (a))) #else #define GCC_ATTR_FORMAT_ARG(a) #endif /* I am not sure since when the unused attribute is really supported. In any case it it only needed for gcc versions which print a warning. Thus let us require gcc >= 3.5. */ #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 5 ) #define GCC_ATTR_UNUSED __attribute__ ((unused)) #else #define GCC_ATTR_UNUSED #endif #if __GNUC__ >= 3 -#define LIKELY( expr ) __builtin_expect( !!(expr), 1 ) -#define UNLIKELY( expr ) __builtin_expect( !!(expr), 0 ) +#define LIKELY(expr) __builtin_expect( !!(expr), 1 ) +#define UNLIKELY(expr) __builtin_expect( !!(expr), 0 ) +#define CONSTANT_P(expr) __builtin_constant_p( expr ) #else -#define LIKELY( expr ) (!!(expr)) -#define UNLIKELY( expr ) (!!(expr)) +#define LIKELY(expr) (!!(expr)) +#define UNLIKELY(expr) (!!(expr)) +#define CONSTANT_P(expr) (0) #endif /* Gettext macros. */ #define _(a) _gcry_gettext(a) #define N_(a) (a) /* Some handy 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) #define my_isascii(c) (!((c) & 0x80)) /*-- src/global.c -*/ extern int _gcry_global_any_init_done; int _gcry_global_is_operational (void); gcry_err_code_t _gcry_vcontrol (enum gcry_ctl_cmds cmd, va_list arg_ptr); void _gcry_check_heap (const void *a); void _gcry_pre_syscall (void); void _gcry_post_syscall (void); int _gcry_get_debug_flag (unsigned int mask); char *_gcry_get_config (int mode, const char *what); /* Malloc functions and common wrapper macros. */ void *_gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC; void *_gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *_gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; void *_gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *_gcry_realloc (void *a, size_t n); char *_gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC; void *_gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC; void *_gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *_gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; void *_gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *_gcry_xrealloc (void *a, size_t n); char *_gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC; void _gcry_free (void *a); int _gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE; #define xtrymalloc(a) _gcry_malloc ((a)) #define xtrycalloc(a,b) _gcry_calloc ((a),(b)) #define xtrymalloc_secure(a) _gcry_malloc_secure ((a)) #define xtrycalloc_secure(a,b) _gcry_calloc_secure ((a),(b)) #define xtryrealloc(a,b) _gcry_realloc ((a),(b)) #define xtrystrdup(a) _gcry_strdup ((a)) #define xmalloc(a) _gcry_xmalloc ((a)) #define xcalloc(a,b) _gcry_xcalloc ((a),(b)) #define xmalloc_secure(a) _gcry_xmalloc_secure ((a)) #define xcalloc_secure(a,b) _gcry_xcalloc_secure ((a),(b)) #define xrealloc(a,b) _gcry_xrealloc ((a),(b)) #define xstrdup(a) _gcry_xstrdup ((a)) #define xfree(a) _gcry_free ((a)) /*-- src/misc.c --*/ #if defined(JNLIB_GCC_M_FUNCTION) || __STDC_VERSION__ >= 199901L void _gcry_bug (const char *file, int line, const char *func) GCC_ATTR_NORETURN; void _gcry_assert_failed (const char *expr, const char *file, int line, const char *func) GCC_ATTR_NORETURN; #else void _gcry_bug (const char *file, int line); void _gcry_assert_failed (const char *expr, const char *file, int line); #endif void _gcry_divide_by_zero (void) JNLIB_GCC_A_NR; const char *_gcry_gettext (const char *key) GCC_ATTR_FORMAT_ARG(1); void _gcry_fatal_error(int rc, const char *text ) JNLIB_GCC_A_NR; void _gcry_logv (int level, const char *fmt, va_list arg_ptr) JNLIB_GCC_A_PRINTF(2,0); void _gcry_log( int level, const char *fmt, ... ) JNLIB_GCC_A_PRINTF(2,3); void _gcry_log_bug( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2); void _gcry_log_fatal( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2); void _gcry_log_error( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); void _gcry_log_info( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); 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_log_printsxp (const char *text, gcry_sexp_t sexp); void _gcry_set_log_verbosity( int level ); int _gcry_log_verbosity( int level ); #ifdef JNLIB_GCC_M_FUNCTION #define BUG() _gcry_bug( __FILE__ , __LINE__, __FUNCTION__ ) #define gcry_assert(expr) (LIKELY(expr)? (void)0 \ : _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __FUNCTION__)) #elif __STDC_VERSION__ >= 199901L #define BUG() _gcry_bug( __FILE__ , __LINE__, __func__ ) #define gcry_assert(expr) (LIKELY(expr)? (void)0 \ : _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __func__)) #else #define BUG() _gcry_bug( __FILE__ , __LINE__ ) #define gcry_assert(expr) (LIKELY(expr)? (void)0 \ : _gcry_assert_failed (STR(expr), __FILE__, __LINE__)) #endif #define log_bug _gcry_log_bug #define log_fatal _gcry_log_fatal #define log_error _gcry_log_error #define log_info _gcry_log_info #define log_debug _gcry_log_debug #define log_printf _gcry_log_printf #define log_printhex _gcry_log_printhex #define log_printmpi _gcry_log_printmpi #define log_printsxp _gcry_log_printsxp /* Compatibility macro. */ #define log_mpidump _gcry_log_printmpi /* Tokeninze STRING and return a malloced array. */ char **_gcry_strtokenize (const char *string, const char *delim); /*-- src/hwfeatures.c --*/ #define HWF_PADLOCK_RNG (1 << 0) #define HWF_PADLOCK_AES (1 << 1) #define HWF_PADLOCK_SHA (1 << 2) #define HWF_PADLOCK_MMUL (1 << 3) #define HWF_INTEL_CPU (1 << 4) #define HWF_INTEL_FAST_SHLD (1 << 5) #define HWF_INTEL_BMI2 (1 << 6) #define HWF_INTEL_SSSE3 (1 << 7) #define HWF_INTEL_SSE4_1 (1 << 8) #define HWF_INTEL_PCLMUL (1 << 9) #define HWF_INTEL_AESNI (1 << 10) #define HWF_INTEL_RDRAND (1 << 11) #define HWF_INTEL_AVX (1 << 12) #define HWF_INTEL_AVX2 (1 << 13) #define HWF_INTEL_FAST_VPGATHER (1 << 14) #define HWF_INTEL_RDTSC (1 << 15) #define HWF_INTEL_SHAEXT (1 << 16) #define HWF_ARM_NEON (1 << 17) #define HWF_ARM_AES (1 << 18) #define HWF_ARM_SHA1 (1 << 19) #define HWF_ARM_SHA2 (1 << 20) #define HWF_ARM_PMULL (1 << 21) gpg_err_code_t _gcry_disable_hw_feature (const char *name); void _gcry_detect_hw_features (void); unsigned int _gcry_get_hw_features (void); const char *_gcry_enum_hw_features (int idx, unsigned int *r_feature); /*-- mpi/mpiutil.c --*/ const char *_gcry_mpi_get_hw_config (void); /*-- cipher/pubkey.c --*/ /* FIXME: shouldn't this go into mpi.h? */ #ifndef mpi_powm #define mpi_powm(w,b,e,m) gcry_mpi_powm( (w), (b), (e), (m) ) #endif /*-- primegen.c --*/ gcry_err_code_t _gcry_primegen_init (void); gcry_mpi_t _gcry_generate_secret_prime (unsigned int nbits, gcry_random_level_t random_level, int (*extra_check)(void*, gcry_mpi_t), void *extra_check_arg); gcry_mpi_t _gcry_generate_public_prime (unsigned int nbits, gcry_random_level_t random_level, int (*extra_check)(void*, gcry_mpi_t), void *extra_check_arg); gcry_err_code_t _gcry_generate_elg_prime (int mode, unsigned int pbits, unsigned int qbits, gcry_mpi_t g, gcry_mpi_t *r_prime, gcry_mpi_t **factors); gcry_mpi_t _gcry_derive_x931_prime (const gcry_mpi_t xp, const gcry_mpi_t xp1, const gcry_mpi_t xp2, const gcry_mpi_t e, gcry_mpi_t *r_p1, gcry_mpi_t *r_p2); gpg_err_code_t _gcry_generate_fips186_2_prime (unsigned int pbits, unsigned int qbits, const void *seed, size_t seedlen, gcry_mpi_t *r_q, gcry_mpi_t *r_p, int *r_counter, void **r_seed, size_t *r_seedlen); gpg_err_code_t _gcry_generate_fips186_3_prime (unsigned int pbits, unsigned int qbits, const void *seed, size_t seedlen, gcry_mpi_t *r_q, gcry_mpi_t *r_p, int *r_counter, void **r_seed, size_t *r_seedlen, int *r_hashalgo); gpg_err_code_t _gcry_fips186_4_prime_check (const gcry_mpi_t x, unsigned int bits); /* Replacements of missing functions (missing-string.c). */ #ifndef HAVE_STPCPY char *stpcpy (char *a, const char *b); #endif #ifndef HAVE_STRCASECMP int strcasecmp (const char *a, const char *b) _GCRY_GCC_ATTR_PURE; #endif #include "../compat/libcompat.h" /* Macros used to rename missing functions. */ #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 HAVE_ATEXIT #define atexit(a) (on_exit((a),0)) #endif #ifndef HAVE_RAISE #define raise(a) kill(getpid(), (a)) #endif /* Stack burning. */ #ifdef HAVE_GCC_ASM_VOLATILE_MEMORY #define __gcry_burn_stack_dummy() asm volatile ("":::"memory") #else void __gcry_burn_stack_dummy (void); #endif void __gcry_burn_stack (unsigned int bytes); #define _gcry_burn_stack(bytes) \ do { __gcry_burn_stack (bytes); \ __gcry_burn_stack_dummy (); } while(0) /* To avoid that a compiler optimizes certain memset calls away, these - macros may be used instead. */ + macros may be used instead. For small constant length buffers, + memory wiping is inlined. For non-constant or large length buffers, + memory is wiped with memset through _gcry_wipememory. */ +void _gcry_wipememory2(void *ptr, int set, size_t len); #define wipememory2(_ptr,_set,_len) do { \ - volatile char *_vptr=(volatile char *)(_ptr); \ - size_t _vlen=(_len); \ - unsigned char _vset=(_set); \ - fast_wipememory2(_vptr,_vset,_vlen); \ - while(_vlen) { *_vptr=(_vset); _vptr++; _vlen--; } \ - } while(0) + if (!CONSTANT_P(_len) || _len > 64) { \ + _gcry_wipememory2((void *)_ptr, _set, _len); \ + } else {\ + volatile char *_vptr = (volatile char *)(_ptr); \ + size_t _vlen = (_len); \ + const unsigned char _vset = (_set); \ + fast_wipememory2(_vptr, _vset, _vlen); \ + while(_vlen) { *_vptr = (_vset); _vptr++; _vlen--; } \ + } \ + } while(0) #define wipememory(_ptr,_len) wipememory2(_ptr,0,_len) -#define FASTWIPE_T u64 -#define FASTWIPE_MULT (U64_C(0x0101010101010101)) - -/* Following architectures can handle unaligned accesses fast. */ #if defined(HAVE_GCC_ATTRIBUTE_PACKED) && \ defined(HAVE_GCC_ATTRIBUTE_ALIGNED) && \ - defined(HAVE_GCC_ATTRIBUTE_MAY_ALIAS) && \ - (defined(__i386__) || defined(__x86_64__) || \ - defined(__powerpc__) || defined(__powerpc64__) || \ - (defined(__arm__) && defined(__ARM_FEATURE_UNALIGNED)) || \ - defined(__aarch64__)) -#define fast_wipememory2_unaligned_head(_ptr,_set,_len) /*do nothing*/ + defined(HAVE_GCC_ATTRIBUTE_MAY_ALIAS) typedef struct fast_wipememory_s { - FASTWIPE_T a; + u64 a; } __attribute__((packed, aligned(1), may_alias)) fast_wipememory_t; +/* fast_wipememory may leave tail bytes unhandled, in which case tail bytes + are handled by wipememory. */ +# define fast_wipememory2(_vptr,_vset,_vlen) do { \ + fast_wipememory_t _vset_long; \ + if (_vlen < sizeof(fast_wipememory_t)) \ + break; \ + _vset_long.a = (_vset); \ + _vset_long.a *= U64_C(0x0101010101010101); \ + do { \ + volatile fast_wipememory_t *_vptr_long = \ + (volatile void *)_vptr; \ + _vptr_long->a = _vset_long.a; \ + _vlen -= sizeof(fast_wipememory_t); \ + _vptr += sizeof(fast_wipememory_t); \ + } while (_vlen >= sizeof(fast_wipememory_t)); \ + } while (0) #else -#define fast_wipememory2_unaligned_head(_vptr,_vset,_vlen) do { \ - while(UNLIKELY((size_t)(_vptr)&(sizeof(FASTWIPE_T)-1)) && _vlen) \ - { *_vptr=(_vset); _vptr++; _vlen--; } \ - } while(0) -typedef struct fast_wipememory_s -{ - FASTWIPE_T a; -} fast_wipememory_t; +# define fast_wipememory2(_vptr,_vset,_vlen) #endif -/* fast_wipememory2 may leave tail bytes unhandled, in which case tail bytes - are handled by wipememory2. */ -#define fast_wipememory2(_vptr,_vset,_vlen) do { \ - FASTWIPE_T _vset_long = _vset; \ - fast_wipememory2_unaligned_head(_vptr,_vset,_vlen); \ - if (_vlen < sizeof(FASTWIPE_T)) \ - break; \ - _vset_long *= FASTWIPE_MULT; \ - do { \ - volatile fast_wipememory_t *_vptr_long = \ - (volatile void *)_vptr; \ - _vptr_long->a = _vset_long; \ - _vlen -= sizeof(FASTWIPE_T); \ - _vptr += sizeof(FASTWIPE_T); \ - } while (_vlen >= sizeof(FASTWIPE_T)); \ - } while (0) - /* Digit predicates. */ #define digitp(p) (*(p) >= '0' && *(p) <= '9') #define octdigitp(p) (*(p) >= '0' && *(p) <= '7') #define alphap(a) ( (*(a) >= 'A' && *(a) <= 'Z') \ || (*(a) >= 'a' && *(a) <= 'z')) #define hexdigitp(a) (digitp (a) \ || (*(a) >= 'A' && *(a) <= 'F') \ || (*(a) >= 'a' && *(a) <= 'f')) /* Init functions. */ gcry_err_code_t _gcry_cipher_init (void); gcry_err_code_t _gcry_md_init (void); gcry_err_code_t _gcry_mac_init (void); gcry_err_code_t _gcry_pk_init (void); gcry_err_code_t _gcry_secmem_module_init (void); gcry_err_code_t _gcry_mpi_init (void); /* Memory management. */ #define GCRY_ALLOC_FLAG_SECURE (1 << 0) #define GCRY_ALLOC_FLAG_XHINT (1 << 1) /* Called from xmalloc. */ /*-- sexp.c --*/ gcry_err_code_t _gcry_sexp_vbuild (gcry_sexp_t *retsexp, size_t *erroff, const char *format, va_list arg_ptr); char *_gcry_sexp_nth_string (const gcry_sexp_t list, int number); gpg_err_code_t _gcry_sexp_vextract_param (gcry_sexp_t sexp, const char *path, const char *list, va_list arg_ptr); /*-- fips.c --*/ extern int _gcry_no_fips_mode_required; void _gcry_initialize_fips_mode (int force); /* This macro returns true if fips mode is enabled. This is independent of the fips required finite state machine and only used to enable fips specific code. No locking is required because we have the requirement that this variable is only initialized once with no other threads existing. */ #define fips_mode() (!_gcry_no_fips_mode_required) int _gcry_enforced_fips_mode (void); void _gcry_set_enforced_fips_mode (void); void _gcry_inactivate_fips_mode (const char *text); int _gcry_is_fips_mode_inactive (void); void _gcry_fips_signal_error (const char *srcfile, int srcline, const char *srcfunc, int is_fatal, const char *description); #ifdef JNLIB_GCC_M_FUNCTION # define fips_signal_error(a) \ _gcry_fips_signal_error (__FILE__, __LINE__, __FUNCTION__, 0, (a)) # define fips_signal_fatal_error(a) \ _gcry_fips_signal_error (__FILE__, __LINE__, __FUNCTION__, 1, (a)) #else # define fips_signal_error(a) \ _gcry_fips_signal_error (__FILE__, __LINE__, NULL, 0, (a)) # define fips_signal_fatal_error(a) \ _gcry_fips_signal_error (__FILE__, __LINE__, NULL, 1, (a)) #endif int _gcry_fips_is_operational (void); /* Return true if the library is in the operational state. */ #define fips_is_operational() \ (!_gcry_global_any_init_done ? \ _gcry_global_is_operational() : \ (!fips_mode () || _gcry_global_is_operational ())) #define fips_not_operational() (GPG_ERR_NOT_OPERATIONAL) int _gcry_fips_test_operational (void); int _gcry_fips_test_error_or_operational (void); gpg_err_code_t _gcry_fips_run_selftests (int extended); void _gcry_fips_noreturn (void); #define fips_noreturn() (_gcry_fips_noreturn ()) #endif /* G10LIB_H */ diff --git a/src/misc.c b/src/misc.c index 47d2dc71..420ce74d 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1,532 +1,540 @@ /* misc.c * Copyright (C) 1999, 2001, 2002, 2003, 2007, * 2008 Free Software Foundation, Inc. * * This file is part of Libgcrypt. * * Libgcrypt is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Libgcrypt 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . */ #include #include #include #include #include #include #include #include "g10lib.h" #include "secmem.h" #include "mpi.h" static int verbosity_level = 0; +static void *(*volatile memset_ptr)(void *, int, size_t) = (void *)memset; + static void (*fatal_error_handler)(void*,int, const char*) = NULL; static void *fatal_error_handler_value = 0; static void (*log_handler)(void*,int, const char*, va_list) = NULL; static void *log_handler_value = 0; static const char *(*user_gettext_handler)( const char * ) = NULL; void _gcry_set_gettext_handler (const char *(*f)(const char*)) { user_gettext_handler = f; } const char * _gcry_gettext( const char *key ) { if( user_gettext_handler ) return user_gettext_handler( key ); /* FIXME: switch the domain to gnupg and restore later */ return key; } void _gcry_set_fatalerror_handler( void (*fnc)(void*,int, const char*), void *value) { fatal_error_handler_value = value; fatal_error_handler = fnc; } static void write2stderr( const char *s ) { /* Dummy variable to silence gcc warning. */ int res = write( 2, s, strlen(s) ); (void) res; } /* * This function is called for fatal errors. A caller might want to * set his own handler because this function simply calls abort(). */ void _gcry_fatal_error (int rc, const char *text) { if ( !text ) /* get a default text */ text = gpg_strerror (rc); if (fatal_error_handler && !fips_mode () ) fatal_error_handler (fatal_error_handler_value, rc, text); fips_signal_fatal_error (text); write2stderr("\nFatal error: "); write2stderr(text); write2stderr("\n"); _gcry_secmem_term (); abort (); } void _gcry_set_log_handler (void (*f)(void*,int, const char*, va_list), void *opaque) { log_handler = f; log_handler_value = opaque; } void _gcry_set_log_verbosity( int level ) { verbosity_level = level; } int _gcry_log_verbosity( int level ) { return verbosity_level >= level; } /**************** * This is our log function which prints all log messages to stderr or * using the function defined with gcry_set_log_handler(). */ void _gcry_logv( int level, const char *fmt, va_list arg_ptr ) { if (log_handler) log_handler (log_handler_value, level, fmt, arg_ptr); else { switch (level) { case GCRY_LOG_CONT: break; case GCRY_LOG_INFO: break; case GCRY_LOG_WARN: break; case GCRY_LOG_ERROR: break; case GCRY_LOG_FATAL: fputs("Fatal: ",stderr ); break; case GCRY_LOG_BUG: fputs("Ohhhh jeeee: ", stderr); break; case GCRY_LOG_DEBUG: fputs("DBG: ", stderr ); break; default: fprintf(stderr,"[Unknown log level %d]: ", level ); break; } vfprintf(stderr,fmt,arg_ptr) ; } if ( level == GCRY_LOG_FATAL || level == GCRY_LOG_BUG ) { fips_signal_fatal_error ("internal error (fatal or bug)"); _gcry_secmem_term (); abort (); } } void _gcry_log( int level, const char *fmt, ... ) { va_list arg_ptr ; va_start( arg_ptr, fmt ) ; _gcry_logv( level, fmt, arg_ptr ); va_end(arg_ptr); } #if defined(JNLIB_GCC_M_FUNCTION) || __STDC_VERSION__ >= 199901L void _gcry_bug( const char *file, int line, const char *func ) { _gcry_log( GCRY_LOG_BUG, ("... this is a bug (%s:%d:%s)\n"), file, line, func ); abort(); /* never called, but it makes the compiler happy */ } void _gcry_assert_failed (const char *expr, const char *file, int line, const char *func) { _gcry_log (GCRY_LOG_BUG, ("Assertion `%s' failed (%s:%d:%s)\n"), expr, file, line, func ); abort(); /* Never called, but it makes the compiler happy. */ } #else void _gcry_bug( const char *file, int line ) { _gcry_log( GCRY_LOG_BUG, _("you found a bug ... (%s:%d)\n"), file, line); abort(); /* never called, but it makes the compiler happy */ } void _gcry_assert_failed (const char *expr, const char *file, int line) { _gcry_log (GCRY_LOG_BUG, ("Assertion `%s' failed (%s:%d)\n"), expr, file, line); abort(); /* Never called, but it makes the compiler happy. */ } #endif void _gcry_log_info( const char *fmt, ... ) { va_list arg_ptr ; va_start( arg_ptr, fmt ) ; _gcry_logv( GCRY_LOG_INFO, fmt, arg_ptr ); va_end(arg_ptr); } void _gcry_log_error( const char *fmt, ... ) { va_list arg_ptr ; va_start( arg_ptr, fmt ) ; _gcry_logv( GCRY_LOG_ERROR, fmt, arg_ptr ); va_end(arg_ptr); } void _gcry_log_fatal( const char *fmt, ... ) { va_list arg_ptr ; va_start( arg_ptr, fmt ) ; _gcry_logv( GCRY_LOG_FATAL, fmt, arg_ptr ); va_end(arg_ptr); abort(); /* never called, but it makes the compiler happy */ } void _gcry_log_bug( const char *fmt, ... ) { va_list arg_ptr ; va_start( arg_ptr, fmt ) ; _gcry_logv( GCRY_LOG_BUG, fmt, arg_ptr ); va_end(arg_ptr); abort(); /* never called, but it makes the compiler happy */ } void _gcry_log_debug( const char *fmt, ... ) { va_list arg_ptr ; va_start( arg_ptr, fmt ) ; _gcry_logv( GCRY_LOG_DEBUG, fmt, arg_ptr ); va_end(arg_ptr); } void _gcry_log_printf (const char *fmt, ...) { va_list arg_ptr; if (fmt) { va_start( arg_ptr, fmt ) ; _gcry_logv (GCRY_LOG_CONT, fmt, arg_ptr); va_end(arg_ptr); } } /* 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) { 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 && buffer) { const unsigned char *p = buffer; 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 = 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, 0, &rawmpilen, &sign); if (!rawmpi) do_printhex (text? text:" ", " [out of core]", NULL, 0); else { if (!rawmpilen) do_printhex (text, sign? "-":"+", "", 1); else do_printhex (text, sign? "-":"+", rawmpi, rawmpilen); xfree (rawmpi); } } } static int count_closing_parens (const char *p) { int count = 0; for (; *p; p++) if (*p == ')') count++; else if (!strchr ("\n \t", *p)) return 0; return count; } /* Print SEXP in human readabale format. With TEXT of NULL print just the raw dump without any wrappping, with TEXT an empty string, print a trailing linefeed, otherwise print the full debug output. */ void _gcry_log_printsxp (const char *text, gcry_sexp_t sexp) { int with_lf = 0; if (text && *text) { if ((with_lf = !!strchr (text, '\n'))) log_debug ("%s", text); else log_debug ("%s: ", text); } if (sexp) { int any = 0; int n_closing; char *buf, *pend; const char *p; size_t size; size = sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, NULL, 0); p = buf = xmalloc (size); sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, buf, size); do { if (any && !with_lf) log_debug ("%*s ", text?(int)strlen(text):0, ""); else any = 1; pend = strchr (p, '\n'); size = pend? (pend - p) : strlen (p); if (with_lf) log_debug ("%.*s", (int)size, p); else log_printf ("%.*s", (int)size, p); if (pend) p = pend + 1; else p += size; n_closing = count_closing_parens (p); if (n_closing) { while (n_closing--) log_printf (")"); p = ""; } log_printf ("\n"); } while (*p); xfree (buf); } else if (text) log_printf ("\n"); } /* * Tokenize STRING using the set of delimiters in DELIM. Leading * white spaces are removed from all tokens. The caller must xfree * the result. * * Returns: A malloced and NULL delimited array with the tokens. On * memory error NULL is returned and ERRNO is set. */ char ** _gcry_strtokenize (const char *string, const char *delim) { const char *s; size_t fields; size_t bytes, n; char *buffer; char *p, *px, *pend; char **result; char const ws[] = " \t\v\f\r\n"; if (!delim) delim = ws; /* Count the number of fields. */ for (fields = 1, s = strpbrk (string, delim); s; s = strpbrk (s + 1, delim)) fields++; fields++; /* Add one for the terminating NULL. */ /* Allocate an array for all fields, a terminating NULL, and space for a copy of the string. */ bytes = fields * sizeof *result; if (bytes / sizeof *result != fields) { gpg_err_set_errno (ENOMEM); return NULL; } n = strlen (string) + 1; bytes += n; if (bytes < n) { gpg_err_set_errno (ENOMEM); return NULL; } result = xtrymalloc (bytes); if (!result) return NULL; buffer = (char*)(result + fields); /* Copy and parse the string. */ strcpy (buffer, string); for (n = 0, p = buffer; (pend = strpbrk (p, delim)); p = pend + 1) { *pend = 0; while (strchr (ws, *(byte*)p)) p++; for (px = pend - 1; px >= p && strchr (ws, *(byte*)px); px--) *px = 0; result[n++] = p; } while (*p && strchr (ws, *(byte*)p)) p++; for (px = p + strlen (p) - 1; px >= p && strchr (ws, *(byte*)px); px--) *px = 0; /* Traling spaces may result in an empty field. We do not want to store that. */ result[n++] = *p? p : NULL; result[n] = NULL; gcry_assert ((char*)(result + n + 1) == buffer); return result; } +void +_gcry_wipememory2 (void *ptr, int set, size_t len) +{ + memset_ptr (ptr, set, len); +} + + void __gcry_burn_stack (unsigned int bytes) { #ifdef HAVE_VLA - static void *(*volatile memset_ptr)(void *, int, size_t) = (void *)memset; - /* (bytes == 0 ? 1 : bytes) == (!bytes + bytes) */ - unsigned int buflen = ((!bytes + bytes) + 63) & ~63; - char buf[buflen]; + /* (bytes == 0 ? 1 : bytes) == (!bytes + bytes) */ + unsigned int buflen = ((!bytes + bytes) + 63) & ~63; + char buf[buflen]; - memset_ptr (buf, 0, sizeof buf); + memset_ptr (buf, 0, buflen); #else - volatile char buf[64]; + volatile char buf[64]; - wipememory (buf, sizeof buf); + wipememory (buf, sizeof buf); - if (bytes > sizeof buf) - _gcry_burn_stack (bytes - sizeof buf); + if (bytes > sizeof buf) + _gcry_burn_stack (bytes - sizeof buf); #endif } #ifndef HAVE_GCC_ASM_VOLATILE_MEMORY void __gcry_burn_stack_dummy (void) { } #endif void _gcry_divide_by_zero (void) { gpg_err_set_errno (EDOM); _gcry_fatal_error (gpg_err_code_from_errno (errno), "divide by zero"); }