1 /* gcrypt.h - GNU digital encryption library interface
2 * Copyright (C) 1998 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
31 * The version of this header should match the one of the library
32 * It should not be used by a program because gcry_check_version()
33 * should reurn the same version. The purpose of this macro is to
34 * let autoconf (using the AM_PATH_GCRYPT macro) check that this
35 * header matches the installed library.
36 * Note: Do not edit the next line as configure may fix the string here.
38 #define GCRYPT_VERSION "1.1.2a"
41 #ifndef HAVE_BYTE_TYPEDEF
42 # undef byte /* maybe there is a macro with this name */
43 typedef unsigned char byte;
44 # define HAVE_BYTE_TYPEDEF
47 #ifdef _GCRYPT_IN_LIBGCRYPT
48 # ifndef GCRYPT_NO_MPI_MACROS
49 # define GCRYPT_NO_MPI_MACROS 1
54 typedef struct gcry_mpi *GCRY_MPI;
56 /*******************************************
58 * error handling etc. *
60 *******************************************/
62 /* FIXME: We should use the same values as they were used in GnuPG 1.0.
63 * gpg --status-fd may print some of these values */
65 GCRYERR_SUCCESS = 0, /* "no error" */
66 GCRYERR_GENERAL = 1, /* catch all the other errors code */
68 GCRYERR_INV_PK_ALGO = 4, /* invalid public key algorithm */
69 GCRYERR_INV_MD_ALGO = 5, /* invalid message digest algorithm */
70 GCRYERR_BAD_PUBLIC_KEY = 6, /* Bad public key */
71 GCRYERR_BAD_SECRET_KEY = 7, /* Bad secret key */
72 GCRYERR_BAD_SIGNATURE = 8, /* Bad signature */
74 GCRYERR_INV_CIPHER_ALGO = 12, /* invalid cipher algorithm */
76 GCRYERR_WRONG_PK_ALGO = 41, /* wrong public key algorithm */
77 GCRYERR_WEAK_KEY = 43, /* weak encryption key */
78 GCRYERR_INV_KEYLEN = 44, /* invalid length of a key*/
79 GCRYERR_INV_ARG = 45, /* invalid argument */
80 GCRYERR_SELFTEST = 50, /* selftest failed */
82 /* error codes not used in GnuPG 1.0 */
83 GCRYERR_INV_OP = 61, /* invalid operation code or ctl command */
84 GCRYERR_NO_MEM = 62, /* out of core */
85 GCRYERR_INTERNAL = 63, /* internal error */
86 GCRYERR_EOF = 64, /* (-1) is remapped to this value */
87 GCRYERR_INV_OBJ = 65, /* an object is not valid */
88 GCRYERR_TOO_SHORT = 66, /* provided buffer too short */
89 GCRYERR_TOO_LARGE = 67, /* object is too large */
90 GCRYERR_NO_OBJ = 68, /* Missing item in an object */
91 GCRYERR_NOT_IMPL = 69, /* Not implemented */
92 GCRYERR_CONFLICT = 70,
93 GCRYERR_INV_CIPHER_MODE = 71,
96 const char *gcry_check_version( const char *req_version );
99 const char *gcry_strerror( int ec );
104 GCRYCTL_CFB_SYNC = 3,
105 GCRYCTL_RESET = 4, /* e.g. for MDs */
106 GCRYCTL_FINALIZE = 5,
107 GCRYCTL_GET_KEYLEN = 6,
108 GCRYCTL_GET_BLKLEN = 7,
109 GCRYCTL_TEST_ALGO = 8,
110 GCRYCTL_IS_SECURE = 9,
111 GCRYCTL_GET_ASNOID = 10,
112 GCRYCTL_ENABLE_ALGO = 11,
113 GCRYCTL_DISABLE_ALGO = 12,
114 GCRYCTL_DUMP_RANDOM_STATS = 13,
115 GCRYCTL_DUMP_SECMEM_STATS = 14,
116 GCRYCTL_GET_ALGO_NPKEY = 15,
117 GCRYCTL_GET_ALGO_NSKEY = 16,
118 GCRYCTL_GET_ALGO_NSIGN = 17,
119 GCRYCTL_GET_ALGO_NENCR = 18,
120 GCRYCTL_SET_VERBOSITY = 19,
121 GCRYCTL_SET_DEBUG_FLAGS = 20,
122 GCRYCTL_CLEAR_DEBUG_FLAGS = 21,
123 GCRYCTL_USE_SECURE_RNDPOOL= 22,
124 GCRYCTL_DUMP_MEMORY_STATS = 23,
125 GCRYCTL_INIT_SECMEM = 24,
126 GCRYCTL_TERM_SECMEM = 25,
127 GCRYCTL_DISABLE_SECMEM_WARN = 27,
128 GCRYCTL_SUSPEND_SECMEM_WARN = 28,
129 GCRYCTL_RESUME_SECMEM_WARN = 29,
130 GCRYCTL_DROP_PRIVS = 30,
131 GCRYCTL_ENABLE_M_GUARD = 31,
132 GCRYCTL_START_DUMP = 32,
133 GCRYCTL_STOP_DUMP = 33,
134 GCRYCTL_GET_ALGO_USAGE = 34
137 int gcry_control( enum gcry_ctl_cmds, ... );
139 enum gcry_random_level {
140 GCRY_WEAK_RANDOM = 0,
141 GCRY_STRONG_RANDOM = 1,
142 GCRY_VERY_STRONG_RANDOM = 2
147 typedef struct gcry_sexp *GCRY_SEXP;
149 enum gcry_sexp_format {
150 GCRYSEXP_FMT_DEFAULT = 0,
151 GCRYSEXP_FMT_CANON = 1,
152 GCRYSEXP_FMT_BASE64 = 2,
153 GCRYSEXP_FMT_ADVANCED = 3,
157 void gcry_sexp_release( GCRY_SEXP sexp );
158 void gcry_sexp_dump( const GCRY_SEXP a );
159 GCRY_SEXP gcry_sexp_cons( const GCRY_SEXP a, const GCRY_SEXP b );
160 GCRY_SEXP gcry_sexp_alist( const GCRY_SEXP *array );
161 GCRY_SEXP gcry_sexp_vlist( const GCRY_SEXP a, ... );
162 GCRY_SEXP gcry_sexp_append( const GCRY_SEXP a, const GCRY_SEXP n );
163 GCRY_SEXP gcry_sexp_prepend( const GCRY_SEXP a, const GCRY_SEXP n );
164 int gcry_sexp_sscan( GCRY_SEXP *retsexp, size_t *erroff,
165 const char *buffer, size_t length );
166 int gcry_sexp_build( GCRY_SEXP *retsexp, size_t *erroff,
167 const char *format, ... );
168 size_t gcry_sexp_sprint( GCRY_SEXP sexp, int mode, char *buffer,
170 GCRY_SEXP gcry_sexp_find_token( GCRY_SEXP list,
171 const char *tok, size_t toklen );
172 int gcry_sexp_length( const GCRY_SEXP list );
173 GCRY_SEXP gcry_sexp_nth( const GCRY_SEXP list, int number );
174 GCRY_SEXP gcry_sexp_car( const GCRY_SEXP list );
175 GCRY_SEXP gcry_sexp_cdr( const GCRY_SEXP list );
176 GCRY_SEXP gcry_sexp_cadr( const GCRY_SEXP list );
177 const char *gcry_sexp_nth_data( const GCRY_SEXP list, int number,
179 GCRY_MPI gcry_sexp_nth_mpi( GCRY_SEXP list, int number, int mpifmt );
182 /*******************************************
184 * multi precision integer functions *
186 *******************************************/
188 enum gcry_mpi_format {
190 GCRYMPI_FMT_STD = 1, /* twos complement stored without length */
191 GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP */
192 GCRYMPI_FMT_SSH = 3, /* As used by SSH (same as 1 but with length)*/
193 GCRYMPI_FMT_HEX = 4, /* hex format */
194 GCRYMPI_FMT_USG = 5, /* like STD but this is an unsigned one */
199 GCRYMPI_FLAG_SECURE = 1,
200 GCRYMPI_FLAG_OPAQUE = 2,
205 GCRY_MPI gcry_mpi_new( unsigned int nbits );
206 GCRY_MPI gcry_mpi_snew( unsigned int nbits );
207 void gcry_mpi_release( GCRY_MPI a );
208 GCRY_MPI gcry_mpi_copy( const GCRY_MPI a );
209 GCRY_MPI gcry_mpi_set( GCRY_MPI w, const GCRY_MPI u );
210 GCRY_MPI gcry_mpi_set_ui( GCRY_MPI w, unsigned long u );
211 int gcry_mpi_cmp( const GCRY_MPI u, const GCRY_MPI v );
212 int gcry_mpi_cmp_ui( const GCRY_MPI u, unsigned long v );
213 void gcry_mpi_randomize( GCRY_MPI w,
214 unsigned int nbits, enum gcry_random_level level);
215 int gcry_mpi_scan( GCRY_MPI *ret_mpi, enum gcry_mpi_format format,
216 const char *buffer, size_t *nbytes );
217 int gcry_mpi_print( enum gcry_mpi_format format,
218 char *buffer, size_t *nbytes, const GCRY_MPI a );
219 int gcry_mpi_aprint( enum gcry_mpi_format format,
220 void **buffer, size_t *nbytes, const GCRY_MPI a );
222 void gcry_mpi_powm( GCRY_MPI w,
223 const GCRY_MPI b, const GCRY_MPI e, const GCRY_MPI m );
224 GCRY_MPI gcry_mpi_set_opaque( GCRY_MPI a, void *p, unsigned int nbits );
225 void *gcry_mpi_get_opaque( GCRY_MPI a, unsigned int *nbits );
226 void gcry_mpi_set_flag( GCRY_MPI a, enum gcry_mpi_flag flag );
227 void gcry_mpi_clear_flag( GCRY_MPI a, enum gcry_mpi_flag flag );
228 int gcry_mpi_get_flag( GCRY_MPI a, enum gcry_mpi_flag flag );
229 unsigned int gcry_mpi_get_nbits( GCRY_MPI a );
232 #ifndef GCRYPT_NO_MPI_MACROS
233 #define mpi_new(n) gcry_mpi_new( (n) )
234 #define mpi_secure_new( n ) gcry_mpi_snew( (n) )
235 #define mpi_release( a ) do { gcry_mpi_release( (a) ); \
236 (a) = NULL; } while(0)
237 #define mpi_copy( a ) gcry_mpi_copy( (a) )
238 #define mpi_set( w, u) gcry_mpi_set( (w), (u) )
239 #define mpi_set_ui( w, u) gcry_mpi_set_ui( (w), (u) )
240 #define mpi_cmp( u, v ) gcry_mpi_cmp( (u), (v) )
241 #define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) )
243 #define mpi_powm(w,b,e,m) gcry_mpi_powm( (w), (b), (e), (m) )
244 #define mpi_set_opaque(a,b,c) gcry_mpi_set_opaque( (a), (b), (c) )
245 #define mpi_get_opaque(a,b) gcry_mpi_get_opaque( (a), (b) )
246 #endif /* GCRYPT_NO_MPI_MACROS */
248 /********************************************
249 ******* symmetric cipher functions *******
250 ********************************************/
252 struct gcry_cipher_handle;
253 typedef struct gcry_cipher_handle *GCRY_CIPHER_HD;
255 enum gcry_cipher_algos {
256 GCRY_CIPHER_NONE = 0,
257 GCRY_CIPHER_IDEA = 1,
258 GCRY_CIPHER_3DES = 2,
259 GCRY_CIPHER_CAST5 = 3,
260 GCRY_CIPHER_BLOWFISH = 4,
261 GCRY_CIPHER_SAFER_SK128 = 5,
262 GCRY_CIPHER_DES_SK = 6,
263 GCRY_CIPHER_RIJNDAEL = 7,
264 GCRY_CIPHER_RIJNDAEL192 = 8,
265 GCRY_CIPHER_RIJNDAEL256 = 9,
266 GCRY_CIPHER_TWOFISH = 10,
267 /* other cipher numbers are above 300 for OpenPGP reasons. */
268 GCRY_CIPHER_ARCFOUR = 301
271 enum gcry_cipher_modes {
272 GCRY_CIPHER_MODE_NONE = 0,
273 GCRY_CIPHER_MODE_ECB = 1,
274 GCRY_CIPHER_MODE_CFB = 2,
275 GCRY_CIPHER_MODE_CBC = 3,
276 GCRY_CIPHER_MODE_STREAM = 4 /* native stream mode of some the algorithms */
279 enum gcry_cipher_flags {
280 GCRY_CIPHER_SECURE = 1, /* allocate in secure memory */
281 GCRY_CIPHER_ENABLE_SYNC = 2, /* enable CFB sync mode */
285 GCRY_CIPHER_HD gcry_cipher_open( int algo, int mode, unsigned flags);
286 void gcry_cipher_close( GCRY_CIPHER_HD h );
287 int gcry_cipher_ctl( GCRY_CIPHER_HD h, int cmd, void *buffer, size_t buflen);
288 int gcry_cipher_info( GCRY_CIPHER_HD h, int what, void *buffer, size_t *nbytes);
289 int gcry_cipher_algo_info( int algo, int what, void *buffer, size_t *nbytes);
290 const char *gcry_cipher_algo_name( int algo );
291 int gcry_cipher_map_name( const char* name );
293 int gcry_cipher_encrypt( GCRY_CIPHER_HD h, byte *out, size_t outsize,
294 const byte *in, size_t inlen );
295 int gcry_cipher_decrypt( GCRY_CIPHER_HD h, byte *out, size_t outsize,
296 const byte *in, size_t inlen );
299 /* some handy macros */
300 /* We have to cast a way a const char* here - this catch-all ctl function
301 * was probably not the best choice */
302 #define gcry_cipher_setkey(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_KEY, \
304 #define gcry_cipher_setiv(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_IV, \
306 #define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, \
309 #define gcry_cipher_get_algo_keylen(a) \
310 gcry_cipher_algo_info( (a), GCRYCTL_GET_KEYLEN, NULL, NULL )
311 #define gcry_cipher_get_algo_blklen(a) \
312 gcry_cipher_algo_info( (a), GCRYCTL_GET_BLKLEN, NULL, NULL )
313 #define gcry_cipher_test_algo(a) \
314 gcry_cipher_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
317 /*********************************************
318 ******* asymmetric cipher functions *******
319 *********************************************/
323 GCRY_PK_RSA_E = 2, /* use only for OpenPGP */
324 GCRY_PK_RSA_S = 3, /* use only for OpenPGP */
325 GCRY_PK_ELG_E = 16, /* use only for OpenPGP */
330 /* Flags describing usage capabilites/request of a PK algorithm */
331 #define GCRY_PK_USAGE_SIGN 1
332 #define GCRY_PK_USAGE_ENCR 2
334 int gcry_pk_encrypt( GCRY_SEXP *result, GCRY_SEXP data, GCRY_SEXP pkey );
335 int gcry_pk_decrypt( GCRY_SEXP *result, GCRY_SEXP data, GCRY_SEXP skey );
336 int gcry_pk_sign( GCRY_SEXP *result, GCRY_SEXP data, GCRY_SEXP skey );
337 int gcry_pk_verify( GCRY_SEXP sigval, GCRY_SEXP data, GCRY_SEXP pkey );
338 int gcry_pk_testkey( GCRY_SEXP key );
339 int gcry_pk_genkey( GCRY_SEXP *r_key, GCRY_SEXP s_parms );
341 int gcry_pk_ctl( int cmd, void *buffer, size_t buflen);
342 int gcry_pk_algo_info( int algo, int what, void *buffer, size_t *nbytes);
343 const char *gcry_pk_algo_name( int algo );
344 int gcry_pk_map_name( const char* name );
345 unsigned int gcry_pk_get_nbits( GCRY_SEXP key );
348 #define gcry_pk_test_algo(a) \
349 gcry_pk_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
351 /*********************************************
352 ******* cryptograhic hash functions *******
353 *********************************************/
364 GCRY_MD_FLAG_SECURE = 1,
365 GCRY_MD_FLAG_HMAC = 2
369 struct gcry_md_context;
370 struct gcry_md_handle {
371 struct gcry_md_context *ctx;
376 typedef struct gcry_md_handle *GCRY_MD_HD;
379 GCRY_MD_HD gcry_md_open( int algo, unsigned flags );
380 void gcry_md_close( GCRY_MD_HD hd );
381 int gcry_md_enable( GCRY_MD_HD hd, int algo );
382 GCRY_MD_HD gcry_md_copy( GCRY_MD_HD hd );
383 void gcry_md_reset( GCRY_MD_HD hd );
384 int gcry_md_ctl( GCRY_MD_HD hd, int cmd, byte *buffer, size_t buflen);
385 void gcry_md_write( GCRY_MD_HD hd, const byte *buffer, size_t length);
386 byte *gcry_md_read( GCRY_MD_HD hd, int algo );
387 void gcry_md_hash_buffer( int algo, char *digest,
388 const char *buffer, size_t length);
389 int gcry_md_get_algo( GCRY_MD_HD hd );
390 unsigned int gcry_md_get_algo_dlen( int algo );
391 /*??int gcry_md_get( GCRY_MD_HD hd, int algo, byte *buffer, int buflen );*/
392 int gcry_md_info( GCRY_MD_HD h, int what, void *buffer, size_t *nbytes);
393 int gcry_md_algo_info( int algo, int what, void *buffer, size_t *nbytes);
394 const char *gcry_md_algo_name( int algo );
395 int gcry_md_map_name( const char* name );
396 int gcry_md_setkey( GCRY_MD_HD hd, const char *key, size_t keylen );
398 #define gcry_md_putc(h,c) \
400 if( (h)->bufpos == (h)->bufsize ) \
401 gcry_md_write( (h), NULL, 0 ); \
402 (h)->buf[(h)->bufpos++] = (c) & 0xff; \
405 #define gcry_md_final(a) \
406 gcry_md_ctl( (a), GCRYCTL_FINALIZE, NULL, 0 )
408 #define gcry_md_is_secure(a) \
409 gcry_md_info( (a), GCRYCTL_IS_SECURE, NULL, NULL )
411 #define gcry_md_test_algo(a) \
412 gcry_md_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
414 #define gcry_md_start_debug(a,b) \
415 gcry_md_ctl( (a), GCRYCTL_START_DUMP, (b), 0 )
416 #define gcry_md_stop_debug(a,b) \
417 gcry_md_ctl( (a), GCRYCTL_STOP_DUMP, (b), 0 )
420 /*********************************************
421 ******* random generating functions *******
422 *********************************************/
423 void gcry_randomize( byte *buffer, size_t length,
424 enum gcry_random_level level );
425 void *gcry_random_bytes( size_t nbytes, enum gcry_random_level level );
426 void *gcry_random_bytes_secure( size_t nbytes, enum gcry_random_level level );
428 /*****************************************
429 ******* miscellaneous stuff **********
430 *****************************************/
432 enum gcry_log_levels {
433 GCRY_LOG_CONT = 0, /* continue the last log line */
439 GCRY_LOG_DEBUG = 100,
443 /* Provide custom functions for special tasks of libgcrypt.
445 void gcry_set_allocation_handler( void *(*new_alloc_func)(size_t n),
446 void *(*new_alloc_secure_func)(size_t n),
447 int (*new_is_secure_func)(const void*),
448 void *(*new_realloc_func)(void *p, size_t n),
449 void (*new_free_func)(void*) );
450 void gcry_set_outofcore_handler( int (*h)( void*, size_t, unsigned int ),
452 void gcry_set_fatalerror_handler( void (*fnc)(void*,int, const char*),
454 void gcry_set_gettext_handler( const char *(*f)(const char*) );
455 void gcry_set_log_handler( void (*f)(void*,int, const char*, va_list ),
459 /* Access to the memory function of libgcrypt.
460 * Especially the gcry_free() should be used for memory
461 * allocated by gcry_ functions.
463 void *gcry_malloc( size_t n );
464 void *gcry_calloc( size_t n, size_t m );
465 void *gcry_malloc_secure( size_t n );
466 void *gcry_calloc_secure( size_t n, size_t m );
467 void *gcry_realloc( void *a, size_t n );
468 void *gcry_xmalloc( size_t n );
469 void *gcry_xcalloc( size_t n, size_t m );
470 void *gcry_xmalloc_secure( size_t n );
471 void *gcry_xcalloc_secure( size_t n, size_t m );
472 void *gcry_xrealloc( void *a, size_t n );
473 char *gcry_xstrdup( const char * a);
474 void gcry_free( void *a );
475 int gcry_is_secure( const void *a );
478 #ifndef GCRYPT_NO_MPI_MACROS
479 # ifndef DID_MPI_TYPEDEF
480 typedef struct gcry_mpi *MPI;
481 # define DID_MPI_TYPEDEF
483 #endif /* GCRYPT_NO_MPI_MACROS */
488 #endif /* _GCRYPT_H */