1/* g10lib.h - Internal definitions for libgcrypt 2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005 3 * 2007, 2011 Free Software Foundation, Inc. 4 * 5 * This file is part of Libgcrypt. 6 * 7 * Libgcrypt is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser general Public License as 9 * published by the Free Software Foundation; either version 2.1 of 10 * the License, or (at your option) any later version. 11 * 12 * Libgcrypt is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this program; if not, see <http://www.gnu.org/licenses/>. 19 */ 20 21/* This header is to be used inside of libgcrypt in place of gcrypt.h. 22 This way we can better distinguish between internal and external 23 usage of gcrypt.h. */ 24 25#ifndef G10LIB_H 26#define G10LIB_H 1 27 28#ifdef _GCRYPT_H 29#error gcrypt.h already included 30#endif 31 32#ifndef _GCRYPT_IN_LIBGCRYPT 33#error something is wrong with config.h 34#endif 35 36#include <stdio.h> 37#include <stdarg.h> 38 39#include "visibility.h" 40#include "types.h" 41 42 43 44 45/* Attribute handling macros. */ 46 47#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) 48#define JNLIB_GCC_M_FUNCTION 1 49#define JNLIB_GCC_A_NR __attribute__ ((noreturn)) 50#define JNLIB_GCC_A_PRINTF( f, a ) __attribute__ ((format (printf,f,a))) 51#define JNLIB_GCC_A_NR_PRINTF( f, a ) \ 52 __attribute__ ((noreturn, format (printf,f,a))) 53#define GCC_ATTR_NORETURN __attribute__ ((__noreturn__)) 54#else 55#define JNLIB_GCC_A_NR 56#define JNLIB_GCC_A_PRINTF( f, a ) 57#define JNLIB_GCC_A_NR_PRINTF( f, a ) 58#define GCC_ATTR_NORETURN 59#endif 60 61#if __GNUC__ >= 3 62/* According to glibc this attribute is available since 2.8 however we 63 better play safe and use it only with gcc 3 or newer. */ 64#define GCC_ATTR_FORMAT_ARG(a) __attribute__ ((format_arg (a))) 65#else 66#define GCC_ATTR_FORMAT_ARG(a) 67#endif 68 69 70/* Gettext macros. */ 71 72#define _(a) _gcry_gettext(a) 73#define N_(a) (a) 74 75/* Some handy macros */ 76#ifndef STR 77#define STR(v) #v 78#endif 79#define STR2(v) STR(v) 80#define DIM(v) (sizeof(v)/sizeof((v)[0])) 81#define DIMof(type,member) DIM(((type *)0)->member) 82 83 84 85/*-- src/global.c -*/ 86int _gcry_global_is_operational (void); 87gcry_error_t _gcry_vcontrol (enum gcry_ctl_cmds cmd, va_list arg_ptr); 88void _gcry_check_heap (const void *a); 89int _gcry_get_debug_flag (unsigned int mask); 90 91 92/*-- src/misc.c --*/ 93 94#if defined(JNLIB_GCC_M_FUNCTION) || __STDC_VERSION__ >= 199901L 95void _gcry_bug (const char *file, int line, 96 const char *func) GCC_ATTR_NORETURN; 97void _gcry_assert_failed (const char *expr, const char *file, int line, 98 const char *func) GCC_ATTR_NORETURN; 99#else 100void _gcry_bug (const char *file, int line); 101void _gcry_assert_failed (const char *expr, const char *file, int line); 102#endif 103 104const char *_gcry_gettext (const char *key) GCC_ATTR_FORMAT_ARG(1); 105void _gcry_fatal_error(int rc, const char *text ) JNLIB_GCC_A_NR; 106void _gcry_log( int level, const char *fmt, ... ) JNLIB_GCC_A_PRINTF(2,3); 107void _gcry_log_bug( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2); 108void _gcry_log_fatal( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2); 109void _gcry_log_error( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); 110void _gcry_log_info( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); 111int _gcry_log_info_with_dummy_fp (FILE *fp, const char *fmt, ... ) 112 JNLIB_GCC_A_PRINTF(2,3); 113void _gcry_log_debug( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); 114void _gcry_log_printf ( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); 115void _gcry_log_printhex (const char *text, const void *buffer, size_t length); 116 117void _gcry_set_log_verbosity( int level ); 118int _gcry_log_verbosity( int level ); 119 120#ifdef JNLIB_GCC_M_FUNCTION 121#define BUG() _gcry_bug( __FILE__ , __LINE__, __FUNCTION__ ) 122#define gcry_assert(expr) ((expr)? (void)0 \ 123 : _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __FUNCTION__)) 124#elif __STDC_VERSION__ >= 199901L 125#define BUG() _gcry_bug( __FILE__ , __LINE__, __func__ ) 126#define gcry_assert(expr) ((expr)? (void)0 \ 127 : _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __func__)) 128#else 129#define BUG() _gcry_bug( __FILE__ , __LINE__ ) 130#define gcry_assert(expr) ((expr)? (void)0 \ 131 : _gcry_assert_failed (STR(expr), __FILE__, __LINE__)) 132#endif 133 134 135#define log_bug _gcry_log_bug 136#define log_fatal _gcry_log_fatal 137#define log_error _gcry_log_error 138#define log_info _gcry_log_info 139#define log_debug _gcry_log_debug 140#define log_printf _gcry_log_printf 141#define log_printhex _gcry_log_printhex 142 143 144/*-- src/hwfeatures.c --*/ 145/* (Do not change these values unless synced with the asm code.) */ 146#define HWF_PADLOCK_RNG 1 147#define HWF_PADLOCK_AES 2 148#define HWF_PADLOCK_SHA 4 149#define HWF_PADLOCK_MMUL 8 150 151#define HWF_INTEL_AESNI 256 152 153 154unsigned int _gcry_get_hw_features (void); 155void _gcry_detect_hw_features (unsigned int); 156 157 158/*-- mpi/mpiutil.c --*/ 159const char *_gcry_mpi_get_hw_config (void); 160 161 162/*-- cipher/pubkey.c --*/ 163 164/* FIXME: shouldn't this go into mpi.h? */ 165#ifndef mpi_powm 166#define mpi_powm(w,b,e,m) gcry_mpi_powm( (w), (b), (e), (m) ) 167#endif 168 169/*-- primegen.c --*/ 170gcry_mpi_t _gcry_generate_secret_prime (unsigned int nbits, 171 gcry_random_level_t random_level, 172 int (*extra_check)(void*, gcry_mpi_t), 173 void *extra_check_arg); 174gcry_mpi_t _gcry_generate_public_prime (unsigned int nbits, 175 gcry_random_level_t random_level, 176 int (*extra_check)(void*, gcry_mpi_t), 177 void *extra_check_arg); 178gcry_mpi_t _gcry_generate_elg_prime (int mode, 179 unsigned int pbits, unsigned int qbits, 180 gcry_mpi_t g, gcry_mpi_t **factors); 181gcry_mpi_t _gcry_derive_x931_prime (const gcry_mpi_t xp, 182 const gcry_mpi_t xp1, const gcry_mpi_t xp2, 183 const gcry_mpi_t e, 184 gcry_mpi_t *r_p1, gcry_mpi_t *r_p2); 185gpg_err_code_t _gcry_generate_fips186_2_prime 186 (unsigned int pbits, unsigned int qbits, 187 const void *seed, size_t seedlen, 188 gcry_mpi_t *r_q, gcry_mpi_t *r_p, 189 int *r_counter, 190 void **r_seed, size_t *r_seedlen); 191gpg_err_code_t _gcry_generate_fips186_3_prime 192 (unsigned int pbits, unsigned int qbits, 193 const void *seed, size_t seedlen, 194 gcry_mpi_t *r_q, gcry_mpi_t *r_p, 195 int *r_counter, 196 void **r_seed, size_t *r_seedlen, int *r_hashalgo); 197 198 199/* Replacements of missing functions (missing-string.c). */ 200#ifndef HAVE_STPCPY 201char *stpcpy (char *a, const char *b); 202#endif 203#ifndef HAVE_STRCASECMP 204int strcasecmp (const char *a, const char *b) _GCRY_GCC_ATTR_PURE; 205#endif 206 207#include "../compat/libcompat.h" 208 209 210/* Macros used to rename missing functions. */ 211#ifndef HAVE_STRTOUL 212#define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c))) 213#endif 214#ifndef HAVE_MEMMOVE 215#define memmove(d, s, n) bcopy((s), (d), (n)) 216#endif 217#ifndef HAVE_STRICMP 218#define stricmp(a,b) strcasecmp( (a), (b) ) 219#endif 220#ifndef HAVE_ATEXIT 221#define atexit(a) (on_exit((a),0)) 222#endif 223#ifndef HAVE_RAISE 224#define raise(a) kill(getpid(), (a)) 225#endif 226 227 228/* Stack burning. */ 229 230void _gcry_burn_stack (int bytes); 231 232 233/* To avoid that a compiler optimizes certain memset calls away, these 234 macros may be used instead. */ 235#define wipememory2(_ptr,_set,_len) do { \ 236 volatile char *_vptr=(volatile char *)(_ptr); \ 237 size_t _vlen=(_len); \ 238 while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \ 239 } while(0) 240#define wipememory(_ptr,_len) wipememory2(_ptr,0,_len) 241 242 243 244/* Digit predicates. */ 245 246#define digitp(p) (*(p) >= '0' && *(p) <= '9') 247#define octdigitp(p) (*(p) >= '0' && *(p) <= '7') 248#define alphap(a) ( (*(a) >= 'A' && *(a) <= 'Z') \ 249 || (*(a) >= 'a' && *(a) <= 'z')) 250#define hexdigitp(a) (digitp (a) \ 251 || (*(a) >= 'A' && *(a) <= 'F') \ 252 || (*(a) >= 'a' && *(a) <= 'f')) 253 254/* Management for ciphers/digests/pubkey-ciphers. */ 255 256/* Structure for each registered `module'. */ 257struct gcry_module 258{ 259 struct gcry_module *next; /* List pointers. */ 260 struct gcry_module **prevp; 261 void *spec; /* Pointer to the subsystem-specific 262 specification structure. */ 263 void *extraspec; /* Pointer to the subsystem-specific 264 extra specification structure. */ 265 int flags; /* Associated flags. */ 266 int counter; /* Use counter. */ 267 unsigned int mod_id; /* ID of this module. */ 268}; 269 270/* Flags for the `flags' member of gcry_module_t. */ 271#define FLAG_MODULE_DISABLED (1 << 0) 272 273gcry_err_code_t _gcry_module_add (gcry_module_t *entries, 274 unsigned int id, 275 void *spec, 276 void *extraspec, 277 gcry_module_t *module); 278 279typedef int (*gcry_module_lookup_t) (void *spec, void *data); 280 281/* Lookup a module specification by it's ID. After a successful 282 lookup, the module has it's resource counter incremented. */ 283gcry_module_t _gcry_module_lookup_id (gcry_module_t entries, 284 unsigned int id); 285 286/* Internal function. Lookup a module specification. */ 287gcry_module_t _gcry_module_lookup (gcry_module_t entries, void *data, 288 gcry_module_lookup_t func); 289 290/* Release a module. In case the use-counter reaches zero, destroy 291 the module. */ 292void _gcry_module_release (gcry_module_t entry); 293 294/* Add a reference to a module. */ 295void _gcry_module_use (gcry_module_t module); 296 297/* Return a list of module IDs. */ 298gcry_err_code_t _gcry_module_list (gcry_module_t modules, 299 int *list, int *list_length); 300 301gcry_err_code_t _gcry_cipher_init (void); 302gcry_err_code_t _gcry_md_init (void); 303gcry_err_code_t _gcry_pk_init (void); 304gcry_err_code_t _gcry_ac_init (void); 305 306gcry_err_code_t _gcry_pk_module_lookup (int id, gcry_module_t *module); 307void _gcry_pk_module_release (gcry_module_t module); 308gcry_err_code_t _gcry_pk_get_elements (int algo, char **enc, char **sig); 309 310/* Memory management. */ 311#define GCRY_ALLOC_FLAG_SECURE (1 << 0) 312 313 314/*-- sexp.c --*/ 315gcry_error_t _gcry_sexp_vbuild (gcry_sexp_t *retsexp, size_t *erroff, 316 const char *format, va_list arg_ptr); 317char *_gcry_sexp_nth_string (const gcry_sexp_t list, int number); 318 319 320/*-- fips.c --*/ 321 322void _gcry_initialize_fips_mode (int force); 323 324int _gcry_fips_mode (void); 325#define fips_mode() _gcry_fips_mode () 326 327int _gcry_enforced_fips_mode (void); 328 329void _gcry_set_enforced_fips_mode (void); 330 331void _gcry_inactivate_fips_mode (const char *text); 332int _gcry_is_fips_mode_inactive (void); 333 334 335void _gcry_fips_signal_error (const char *srcfile, 336 int srcline, 337 const char *srcfunc, 338 int is_fatal, 339 const char *description); 340#ifdef JNLIB_GCC_M_FUNCTION 341# define fips_signal_error(a) \ 342 _gcry_fips_signal_error (__FILE__, __LINE__, __FUNCTION__, 0, (a)) 343# define fips_signal_fatal_error(a) \ 344 _gcry_fips_signal_error (__FILE__, __LINE__, __FUNCTION__, 1, (a)) 345#else 346# define fips_signal_error(a) \ 347 _gcry_fips_signal_error (__FILE__, __LINE__, NULL, 0, (a)) 348# define fips_signal_fatal_error(a) \ 349 _gcry_fips_signal_error (__FILE__, __LINE__, NULL, 1, (a)) 350#endif 351 352int _gcry_fips_is_operational (void); 353#define fips_is_operational() (_gcry_global_is_operational ()) 354#define fips_not_operational() (GCRY_GPG_ERR_NOT_OPERATIONAL) 355 356int _gcry_fips_test_operational (void); 357int _gcry_fips_test_error_or_operational (void); 358 359gpg_err_code_t _gcry_fips_run_selftests (int extended); 360 361void _gcry_fips_noreturn (void); 362#define fips_noreturn() (_gcry_fips_noreturn ()) 363 364 365 366#endif /* G10LIB_H */ 367