1 /* g10lib.h - Internal definitions for libgcrypt
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005
3 * 2007, 2011 Free Software Foundation, Inc.
5 * This file is part of Libgcrypt.
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.
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.
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/>.
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
29 #error gcrypt.h already included
32 #ifndef _GCRYPT_IN_LIBGCRYPT
33 #error something is wrong with config.h
39 #include "visibility.h"
45 /* Attribute handling macros. */
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__))
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
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)))
66 #define GCC_ATTR_FORMAT_ARG(a)
72 /* Some handy macros */
76 #define STR2(v) STR(v)
77 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
78 #define DIMof(type,member) DIM(((type *)0)->member)
83 int _gcry_global_is_operational (void);
84 gcry_error_t
_gcry_vcontrol (enum gcry_ctl_cmds cmd
, va_list arg_ptr
);
85 void _gcry_check_heap (const void *a
);
86 int _gcry_get_debug_flag (unsigned int mask
);
91 #if defined(JNLIB_GCC_M_FUNCTION) || __STDC_VERSION__ >= 199901L
92 void _gcry_bug (const char *file
, int line
,
93 const char *func
) GCC_ATTR_NORETURN
;
94 void _gcry_assert_failed (const char *expr
, const char *file
, int line
,
95 const char *func
) GCC_ATTR_NORETURN
;
97 void _gcry_bug (const char *file
, int line
);
98 void _gcry_assert_failed (const char *expr
, const char *file
, int line
);
101 const char *_gcry_gettext (const char *key
) GCC_ATTR_FORMAT_ARG(1);
102 void _gcry_fatal_error(int rc
, const char *text
) JNLIB_GCC_A_NR
;
103 void _gcry_log( int level
, const char *fmt
, ... ) JNLIB_GCC_A_PRINTF(2,3);
104 void _gcry_log_bug( const char *fmt
, ... ) JNLIB_GCC_A_NR_PRINTF(1,2);
105 void _gcry_log_fatal( const char *fmt
, ... ) JNLIB_GCC_A_NR_PRINTF(1,2);
106 void _gcry_log_error( const char *fmt
, ... ) JNLIB_GCC_A_PRINTF(1,2);
107 void _gcry_log_info( const char *fmt
, ... ) JNLIB_GCC_A_PRINTF(1,2);
108 int _gcry_log_info_with_dummy_fp (FILE *fp
, const char *fmt
, ... )
109 JNLIB_GCC_A_PRINTF(2,3);
110 void _gcry_log_debug( const char *fmt
, ... ) JNLIB_GCC_A_PRINTF(1,2);
111 void _gcry_log_printf ( const char *fmt
, ... ) JNLIB_GCC_A_PRINTF(1,2);
112 void _gcry_log_printhex (const char *text
, const void *buffer
, size_t length
);
114 void _gcry_set_log_verbosity( int level
);
115 int _gcry_log_verbosity( int level
);
117 #ifdef JNLIB_GCC_M_FUNCTION
118 #define BUG() _gcry_bug( __FILE__ , __LINE__, __FUNCTION__ )
119 #define gcry_assert(expr) ((expr)? (void)0 \
120 : _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __FUNCTION__))
121 #elif __STDC_VERSION__ >= 199901L
122 #define BUG() _gcry_bug( __FILE__ , __LINE__, __func__ )
123 #define gcry_assert(expr) ((expr)? (void)0 \
124 : _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __func__))
126 #define BUG() _gcry_bug( __FILE__ , __LINE__ )
127 #define gcry_assert(expr) ((expr)? (void)0 \
128 : _gcry_assert_failed (STR(expr), __FILE__, __LINE__))
132 #define log_bug _gcry_log_bug
133 #define log_fatal _gcry_log_fatal
134 #define log_error _gcry_log_error
135 #define log_info _gcry_log_info
136 #define log_debug _gcry_log_debug
137 #define log_printf _gcry_log_printf
138 #define log_printhex _gcry_log_printhex
141 /*-- src/hwfeatures.c --*/
142 /* (Do not change these values unless synced with the asm code.) */
143 #define HWF_PADLOCK_RNG 1
144 #define HWF_PADLOCK_AES 2
145 #define HWF_PADLOCK_SHA 4
146 #define HWF_PADLOCK_MMUL 8
148 #define HWF_INTEL_AESNI 256
151 unsigned int _gcry_get_hw_features (void);
152 void _gcry_detect_hw_features (unsigned int);
155 /*-- mpi/mpiutil.c --*/
156 const char *_gcry_mpi_get_hw_config (void);
159 /*-- cipher/pubkey.c --*/
161 /* FIXME: shouldn't this go into mpi.h? */
163 #define mpi_powm(w,b,e,m) gcry_mpi_powm( (w), (b), (e), (m) )
167 gcry_mpi_t
_gcry_generate_secret_prime (unsigned int nbits
,
168 gcry_random_level_t random_level
,
169 int (*extra_check
)(void*, gcry_mpi_t
),
170 void *extra_check_arg
);
171 gcry_mpi_t
_gcry_generate_public_prime (unsigned int nbits
,
172 gcry_random_level_t random_level
,
173 int (*extra_check
)(void*, gcry_mpi_t
),
174 void *extra_check_arg
);
175 gcry_mpi_t
_gcry_generate_elg_prime (int mode
,
176 unsigned int pbits
, unsigned int qbits
,
177 gcry_mpi_t g
, gcry_mpi_t
**factors
);
178 gcry_mpi_t
_gcry_derive_x931_prime (const gcry_mpi_t xp
,
179 const gcry_mpi_t xp1
, const gcry_mpi_t xp2
,
181 gcry_mpi_t
*r_p1
, gcry_mpi_t
*r_p2
);
182 gpg_err_code_t _gcry_generate_fips186_2_prime
183 (unsigned int pbits
, unsigned int qbits
,
184 const void *seed
, size_t seedlen
,
185 gcry_mpi_t
*r_q
, gcry_mpi_t
*r_p
,
187 void **r_seed
, size_t *r_seedlen
);
188 gpg_err_code_t _gcry_generate_fips186_3_prime
189 (unsigned int pbits
, unsigned int qbits
,
190 const void *seed
, size_t seedlen
,
191 gcry_mpi_t
*r_q
, gcry_mpi_t
*r_p
,
193 void **r_seed
, size_t *r_seedlen
, int *r_hashalgo
);
196 /* Replacements of missing functions (missing-string.c). */
198 char *stpcpy (char *a
, const char *b
);
200 #ifndef HAVE_STRCASECMP
201 int strcasecmp (const char *a
, const char *b
) _GCRY_GCC_ATTR_PURE
;
205 /* Macros used to rename missing functions. */
207 #define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c)))
210 #define memmove(d, s, n) bcopy((s), (d), (n))
213 #define stricmp(a,b) strcasecmp( (a), (b) )
216 #define atexit(a) (on_exit((a),0))
219 #define raise(a) kill(getpid(), (a))
225 void _gcry_burn_stack (int bytes
);
228 /* To avoid that a compiler optimizes certain memset calls away, these
229 macros may be used instead. */
230 #define wipememory2(_ptr,_set,_len) do { \
231 volatile char *_vptr=(volatile char *)(_ptr); \
232 size_t _vlen=(_len); \
233 while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \
235 #define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
239 /* Digit predicates. */
241 #define digitp(p) (*(p) >= '0' && *(p) <= '9')
242 #define octdigitp(p) (*(p) >= '0' && *(p) <= '7')
243 #define alphap(a) ( (*(a) >= 'A' && *(a) <= 'Z') \
244 || (*(a) >= 'a' && *(a) <= 'z'))
245 #define hexdigitp(a) (digitp (a) \
246 || (*(a) >= 'A' && *(a) <= 'F') \
247 || (*(a) >= 'a' && *(a) <= 'f'))
251 gcry_error_t
_gcry_sexp_vbuild (gcry_sexp_t
*retsexp
, size_t *erroff
,
252 const char *format
, va_list arg_ptr
);
253 char *_gcry_sexp_nth_string (const gcry_sexp_t list
, int number
);
258 void _gcry_initialize_fips_mode (int force
);
260 int _gcry_fips_mode (void);
261 #define fips_mode() _gcry_fips_mode ()
263 int _gcry_enforced_fips_mode (void);
265 void _gcry_set_enforced_fips_mode (void);
267 void _gcry_inactivate_fips_mode (const char *text
);
268 int _gcry_is_fips_mode_inactive (void);
271 void _gcry_fips_signal_error (const char *srcfile
,
275 const char *description
);
276 #ifdef JNLIB_GCC_M_FUNCTION
277 # define fips_signal_error(a) \
278 _gcry_fips_signal_error (__FILE__, __LINE__, __FUNCTION__, 0, (a))
279 # define fips_signal_fatal_error(a) \
280 _gcry_fips_signal_error (__FILE__, __LINE__, __FUNCTION__, 1, (a))
282 # define fips_signal_error(a) \
283 _gcry_fips_signal_error (__FILE__, __LINE__, NULL, 0, (a))
284 # define fips_signal_fatal_error(a) \
285 _gcry_fips_signal_error (__FILE__, __LINE__, NULL, 1, (a))
288 int _gcry_fips_is_operational (void);
289 #define fips_is_operational() (_gcry_global_is_operational ())
290 #define fips_not_operational() (GCRY_GPG_ERR_NOT_OPERATIONAL)
292 int _gcry_fips_test_operational (void);
293 int _gcry_fips_test_error_or_operational (void);
295 gpg_err_code_t
_gcry_fips_run_selftests (int extended
);
297 void _gcry_fips_noreturn (void);
298 #define fips_noreturn() (_gcry_fips_noreturn ())
302 #endif /* G10LIB_H */