1 /* gcrypt.h - GNU cryptographic library interface
2 * Copyright (C) 1998,1999,2000,2001,2002,2003 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt 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 Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
27 #include <gpg-error.h>
29 /* This is required for error code compatibility. */
30 #define _GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_GCRYPT
34 #if 0 /* keep Emacsens's auto-indent happy */
39 /* The version of this header should match the one of the library It
40 should not be used by a program because gcry_check_version() should
41 return the same version. The purpose of this macro is to let
42 autoconf (using the AM_PATH_GCRYPT macro) check that this header
43 matches the installed library. Note: Do not edit the next line as
44 configure may fix the string here. */
45 #define GCRYPT_VERSION "1.1.42-cvs"
47 /* Internal: We can't use the convenience macros for the multi
48 precision integer functions when building this library. */
49 #ifdef _GCRYPT_IN_LIBGCRYPT
50 #ifndef GCRYPT_NO_MPI_MACROS
51 #define GCRYPT_NO_MPI_MACROS 1
55 /* We want to use gcc attributes when possible. Warning: Don't use
56 these macros in your progranms: As indicated by the leading
57 underscore they are subject to change without notice. */
60 #define _GCRY_GCC_VERSION (__GNUC__ * 10000 \
61 + __GNUC_MINOR__ * 100 \
62 + __GNUC_PATCHLEVEL__)
64 #if _GCRY_GCC_VERSION >= 30100
65 #define _GCRY_GCC_ATTR_DEPRECATED __attribute__ ((__deprecated__))
68 #if _GCRY_GCC_VERSION >= 29600
69 #define _GCRY_GCC_ATTR_PURE __attribute__ ((__pure__))
72 #if _GCRY_GCC_VERSION >= 300200
73 #define _GCRY_GCC_ATTR_MALLOC __attribute__ ((__malloc__))
78 #ifndef _GCRY_GCC_ATTR_DEPRECATED
79 #define _GCRY_GCC_ATTR_DEPRECATED
81 #ifndef _GCRY_GCC_ATTR_PURE
82 #define _GCRY_GCC_ATTR_PURE
84 #ifndef _GCRY_GCC_ATTR_MALLOC
85 #define _GCRY_GCC_ATTR_MALLOC
88 /* Wrappers for the libgpg-error library. */
90 typedef gpg_error_t gcry_error_t;
91 typedef gpg_err_code_t gcry_err_code_t;
92 typedef gpg_err_source_t gcry_err_source_t;
94 static __inline__ gcry_error_t
95 gcry_err_make (gcry_err_source_t source, gcry_err_code_t code)
97 return gpg_err_make (source, code);
100 /* The user can define GPG_ERR_SOURCE_DEFAULT before including this
101 file to specify a default source for gpg_error. */
102 #ifndef GCRY_ERR_SOURCE_DEFAULT
103 #define GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_1
106 static __inline__ gcry_error_t
107 gcry_error (gcry_err_code_t code)
109 return gcry_err_make (GCRY_ERR_SOURCE_DEFAULT, code);
112 static __inline__ gcry_err_code_t
113 gcry_err_code (gcry_error_t err)
115 return gpg_err_code (err);
119 static __inline__ gcry_err_source_t
120 gcry_err_source (gcry_error_t err)
122 return gpg_err_source (err);
125 /* Return a pointer to a string containing a description of the error
126 code in the error value ERR. */
127 const char * gcry_strerror (gcry_error_t err);
129 /* Return a pointer to a string containing a description of the error
130 source in the error value ERR. */
131 const char *gcry_strsource (gcry_error_t err);
133 /* Retrieve the error code for the system error ERR. This returns
134 GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report
136 gcry_err_code_t gcry_err_code_from_errno (int err);
138 /* Retrieve the system error for the error code CODE. This returns 0
139 if CODE is not a system error code. */
140 int gcry_err_code_to_errno (gcry_err_code_t code);
142 /* Return an error value with the error source SOURCE and the system
144 gcry_error_t gcry_err_make_from_errno (gcry_err_source_t source, int err);
146 /* Return an error value with the system error ERR. */
147 gcry_err_code_t gcry_error_from_errno (int err);
149 /* The data object used to hold a multi precision integer. */
151 typedef struct gcry_mpi *gcry_mpi_t;
153 typedef struct gcry_mpi *GCRY_MPI _GCRY_GCC_ATTR_DEPRECATED;
154 typedef struct gcry_mpi *GcryMPI _GCRY_GCC_ATTR_DEPRECATED;
158 /* Check that the library fulfills the version requirement. */
159 const char *gcry_check_version (const char *req_version);
161 /* Codes for function dispatchers. */
163 /* Codes used with the gcry_control function. */
168 GCRYCTL_CFB_SYNC = 3,
169 GCRYCTL_RESET = 4, /* e.g. for MDs */
170 GCRYCTL_FINALIZE = 5,
171 GCRYCTL_GET_KEYLEN = 6,
172 GCRYCTL_GET_BLKLEN = 7,
173 GCRYCTL_TEST_ALGO = 8,
174 GCRYCTL_IS_SECURE = 9,
175 GCRYCTL_GET_ASNOID = 10,
176 GCRYCTL_ENABLE_ALGO = 11,
177 GCRYCTL_DISABLE_ALGO = 12,
178 GCRYCTL_DUMP_RANDOM_STATS = 13,
179 GCRYCTL_DUMP_SECMEM_STATS = 14,
180 GCRYCTL_GET_ALGO_NPKEY = 15,
181 GCRYCTL_GET_ALGO_NSKEY = 16,
182 GCRYCTL_GET_ALGO_NSIGN = 17,
183 GCRYCTL_GET_ALGO_NENCR = 18,
184 GCRYCTL_SET_VERBOSITY = 19,
185 GCRYCTL_SET_DEBUG_FLAGS = 20,
186 GCRYCTL_CLEAR_DEBUG_FLAGS = 21,
187 GCRYCTL_USE_SECURE_RNDPOOL= 22,
188 GCRYCTL_DUMP_MEMORY_STATS = 23,
189 GCRYCTL_INIT_SECMEM = 24,
190 GCRYCTL_TERM_SECMEM = 25,
191 GCRYCTL_DISABLE_SECMEM_WARN = 27,
192 GCRYCTL_SUSPEND_SECMEM_WARN = 28,
193 GCRYCTL_RESUME_SECMEM_WARN = 29,
194 GCRYCTL_DROP_PRIVS = 30,
195 GCRYCTL_ENABLE_M_GUARD = 31,
196 GCRYCTL_START_DUMP = 32,
197 GCRYCTL_STOP_DUMP = 33,
198 GCRYCTL_GET_ALGO_USAGE = 34,
199 GCRYCTL_IS_ALGO_ENABLED = 35,
200 GCRYCTL_DISABLE_INTERNAL_LOCKING = 36,
201 GCRYCTL_DISABLE_SECMEM = 37,
202 GCRYCTL_INITIALIZATION_FINISHED = 38,
203 GCRYCTL_INITIALIZATION_FINISHED_P = 39,
204 GCRYCTL_ANY_INITIALIZATION_P = 40,
205 GCRYCTL_SET_CBC_CTS = 41,
206 GCRYCTL_SET_CBC_MAC = 42,
207 GCRYCTL_SET_CTR = 43,
208 GCRYCTL_ENABLE_QUICK_RANDOM = 44,
209 GCRYCTL_SET_RANDOM_SEED_FILE = 45,
210 GCRYCTL_UPDATE_RANDOM_SEED_FILE = 46
213 /* Perform various operations defined by CMD. */
214 gcry_error_t gcry_control (enum gcry_ctl_cmds CMD, ...);
217 /* S-expression management. */
219 /* The object to represent an S-expression as used with the public key
222 typedef struct gcry_sexp *gcry_sexp_t;
224 typedef struct gcry_sexp *GCRY_SEXP _GCRY_GCC_ATTR_DEPRECATED;
225 typedef struct gcry_sexp *GcrySexp _GCRY_GCC_ATTR_DEPRECATED;
227 /* The possible values for the S-expression format. */
228 enum gcry_sexp_format
230 GCRYSEXP_FMT_DEFAULT = 0,
231 GCRYSEXP_FMT_CANON = 1,
232 GCRYSEXP_FMT_BASE64 = 2,
233 GCRYSEXP_FMT_ADVANCED = 3
236 /* Create an new S-expression object from BUFFER of size LENGTH and
237 return it in RETSEXP. With AUTODETECT set to 0 the data in BUFFER
238 is expected to be in canonized format. */
239 gcry_error_t gcry_sexp_new (gcry_sexp_t *retsexp, const void *buffer, size_t length,
242 /* Same as gcry_sexp_new but allows to pass a FREEFNC which has the
243 effect to transfer ownership of BUFFER to the created object. */
244 gcry_error_t gcry_sexp_create (gcry_sexp_t *retsexp, void *buffer, size_t length,
245 int autodetect, void (*freefnc) (void *));
247 /* Scan BUFFER and return a new S-expression object in RETSEXP. This
248 function expects a printf like string in BUFFER. */
249 gcry_error_t gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff,
250 const char *buffer, size_t length);
252 /* Same as gcry_sexp_sscan but expects a string in FORMAT and can thus
253 only be used for certain encodings. */
254 gcry_error_t gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff,
255 const char *format, ...);
257 /* Like gcry_sexp_build, but uses an array instead of variable
258 function arguments. */
259 gcry_error_t gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff,
260 const char *format, void **arg_list);
262 /* Release the S-expression object SEXP */
263 void gcry_sexp_release (gcry_sexp_t sexp);
265 /* Calculate the length of an canonized S-expresion in BUFFER and
266 check for a valid encoding. */
267 size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length,
268 size_t *erroff, gcry_error_t *errcode);
270 /* Copies the S-expression object SEXP into BUFFER using the format
271 specified in MODE. */
272 size_t gcry_sexp_sprint (gcry_sexp_t sexp, int mode, char *buffer,
275 /* Dumps the S-expression object A in a aformat suitable for debugging
276 to Libgcrypt's logging stream. */
277 void gcry_sexp_dump (const gcry_sexp_t a);
279 gcry_sexp_t gcry_sexp_cons (const gcry_sexp_t a, const gcry_sexp_t b);
280 gcry_sexp_t gcry_sexp_alist (const gcry_sexp_t *array);
281 gcry_sexp_t gcry_sexp_vlist (const gcry_sexp_t a, ...);
282 gcry_sexp_t gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n);
283 gcry_sexp_t gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n);
285 /* Scan the S-expression for a sublist with a type (the car of the
286 list) matching the string TOKEN. If TOKLEN is not 0, the token is
287 assumed to be raw memory of this length. The function returns a
288 newly allocated S-expression consisting of the found sublist or
289 `NULL' when not found. */
290 gcry_sexp_t gcry_sexp_find_token (gcry_sexp_t list,
291 const char *tok, size_t toklen);
292 /* Return the length of the LIST. For a valid S-expression this
293 should be at least 1. */
294 int gcry_sexp_length (const gcry_sexp_t list);
296 /* Create and return a new S-expression from the element with index
297 NUMBER in LIST. Note that the first element has the index 0. If
298 there is no such element, `NULL' is returned. */
299 gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t list, int number);
301 /* Create and return a new S-expression from the first element in
302 LIST; this called the "type" and should always exist and be a
303 string. `NULL' is returned in case of a problem. */
304 gcry_sexp_t gcry_sexp_car (const gcry_sexp_t list);
306 /* Create and return a new list form all elements except for the first
307 one. Note, that this function may return an invalid S-expression
308 because it is not guaranteed, that the type exists and is a string.
309 However, for parsing a complex S-expression it might be useful for
310 intermediate lists. Returns `NULL' on error. */
311 gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t list);
313 gcry_sexp_t gcry_sexp_cadr (const gcry_sexp_t list);
316 /* This function is used to get data from a LIST. A pointer to the
317 actual data with index NUMBER is returned and the length of this
318 data will be stored to DATALEN. If there is no data at the given
319 index or the index represents another list, `NULL' is returned.
320 *Note:* The returned pointer is valid as long as LIST is not
321 modified or released. */
322 const char *gcry_sexp_nth_data (const gcry_sexp_t list, int number,
325 /* This function is used to get and convert data from a LIST. This
326 data is assumed to be an MPI stored in the format described by
327 MPIFMT and returned as a standard Libgcrypt MPI. The caller must
328 release this returned value using `gcry_mpi_release'. If there is
329 no data at the given index, the index represents a list or the
330 value can't be converted to an MPI, `NULL' is returned. */
331 gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt);
335 /*******************************************
337 * multi precision integer functions *
339 *******************************************/
341 /* Different formats of external big integer representation. */
345 GCRYMPI_FMT_STD = 1, /* twos complement stored without length */
346 GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP (only defined as unsigned)*/
347 GCRYMPI_FMT_SSH = 3, /* As used by SSH (same as 1 but with length)*/
348 GCRYMPI_FMT_HEX = 4, /* hex format */
349 GCRYMPI_FMT_USG = 5 /* like STD but this is an unsigned one */
352 /* Flags used for creating big integers. */
355 GCRYMPI_FLAG_SECURE = 1, /* Allocate the number in "secure" memory. */
356 GCRYMPI_FLAG_OPAQUE = 2 /* The number is not a real one but just a
357 way to store some bytes. This is
358 useful for encrypted big integers. */
362 /* Allocate a new big integer object, initialize it with 0 and
363 initially allocate memory for a number of at least NBITS. */
364 gcry_mpi_t gcry_mpi_new (unsigned int nbits);
366 /* Same as gcry_mpi_new() but allocate in "secure" memory. */
367 gcry_mpi_t gcry_mpi_snew (unsigned int nbits);
369 /* Release the number A and free all associated resources. */
370 void gcry_mpi_release (gcry_mpi_t a);
372 /* Create a new number with the same value as A. */
373 gcry_mpi_t gcry_mpi_copy (const gcry_mpi_t a);
375 /* Store the big integer value U in W. */
376 gcry_mpi_t gcry_mpi_set (gcry_mpi_t w, const gcry_mpi_t u);
378 /* Store the unsigned integer value U in W. */
379 gcry_mpi_t gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u);
381 /* Swap the values of A and B. */
382 void gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b);
384 /* Compare the big integer number U and V returning 0 for equality, a
385 positive value for U > V and a negative for U < V. */
386 int gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v);
388 /* Compare the big integer number U with the unsigned integer V
389 returning 0 for equality, a positive value for U > V and a negative
391 int gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v);
393 /* Convert the external representation of an integer stored in BUFFER
394 with a length of BUFLEN into a newly create MPI returned in
395 RET_MPI. If NSCANNED is not NULL, it will receive the number of
396 bytes actually scanned after a successful operation. */
397 gcry_error_t gcry_mpi_scan (gcry_mpi_t *ret_mpi, enum gcry_mpi_format format,
398 const unsigned char *buffer, size_t buflen,
401 /* Convert the big integer A into the external representation
402 described by FORMAT and store it in the provided BUFFER which has
403 been allocated by the user with a size of BUFLEN bytes. NWRITTEN
404 receives the actual length of the external representation unless it
405 has been passed as NULL. */
406 gcry_error_t gcry_mpi_print (enum gcry_mpi_format format,
407 unsigned char *buffer, size_t buflen,
411 /* Convert the big integer A int the external representation described
412 by FORMAT and store it in a newly allocated buffer which address
413 will be put into BUFFER. NWRITTEN receives the actual lengths of the
414 external representation. */
415 gcry_error_t gcry_mpi_aprint (enum gcry_mpi_format format,
416 unsigned char **buffer, size_t *nwritten,
419 /* Dump the value of A in a format suitable for debugging to
420 Libgcrypt's logging stream. Note that one leading space but no
421 trailing space or linefeed will be printed. It is okay to pass
423 void gcry_mpi_dump (const gcry_mpi_t a);
427 void gcry_mpi_add (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
429 /* W = U + V. V is an unsigned integer. */
430 void gcry_mpi_add_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v);
432 /* W = U + V mod M. */
433 void gcry_mpi_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
436 void gcry_mpi_sub (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
438 /* W = U - V. V is an unsigned integer. */
439 void gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v );
441 /* W = U - V mod M */
442 void gcry_mpi_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
445 void gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
447 /* W = U * V. V is an unsigned integer. */
448 void gcry_mpi_mul_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v );
450 /* W = U * V mod M. */
451 void gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
453 /* W = U * (2 ^ CNT). */
454 void gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt);
456 /* Q = DIVIDEND / DIVISOR, R = DIVIDEND % DIVISOR,
457 Q or R may be passed as NULL. ROUND should be negative or 0. */
458 void gcry_mpi_div (gcry_mpi_t q, gcry_mpi_t r,
459 gcry_mpi_t dividend, gcry_mpi_t divisor, int round);
461 /* R = DIVIDEND % DIVISOR */
462 void gcry_mpi_mod (gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor);
464 /* W = B ^ E mod M. */
465 void gcry_mpi_powm (gcry_mpi_t w,
466 const gcry_mpi_t b, const gcry_mpi_t e, const gcry_mpi_t m);
468 /* Set G to the greatest common divisor of A and B.
469 Return true if the G is 1. */
470 int gcry_mpi_gcd (gcry_mpi_t g, gcry_mpi_t a, gcry_mpi_t b);
472 /* Set X to the multiplicative inverse of A mod M.
473 Return true if the value exists. */
474 int gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t m);
477 /* Return the number of bits required to represent A. */
478 unsigned int gcry_mpi_get_nbits (gcry_mpi_t a);
480 /* Return true when bit number N (counting from 0) is set in A. */
481 int gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n);
483 /* Set bit number N in A. */
484 void gcry_mpi_set_bit (gcry_mpi_t a, unsigned int n);
486 /* Clear bit number N in A. */
487 void gcry_mpi_clear_bit (gcry_mpi_t a, unsigned int n);
489 /* Set bit number N in A and clear all bits greater than N. */
490 void gcry_mpi_set_highbit (gcry_mpi_t a, unsigned int n);
492 /* Clear bit number N in A and all bits greater than N. */
493 void gcry_mpi_clear_highbit (gcry_mpi_t a, unsigned int n);
495 /* Shift the value of A by N bits to the right and store the result in X. */
496 void gcry_mpi_rshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n);
498 /* Store NBITS of the value P points to in A and mark A as an opaque
499 value. WARNING: Never use an opaque MPI for anything thing else then
500 gcry_mpi_release, gcry_mpi_get_opaque. */
501 gcry_mpi_t gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits);
503 /* Return a pointer to an opaque value stored in A and return its size
504 in NBITS. Note that the returned pointer is still owned by A and
505 that the function should never be used for an non-opaque MPI. */
506 void *gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits);
508 /* Set the FLAG for the big integer A. Currently only the flag
509 GCRYMPI_FLAG_SECURE is allowed to convert A into an big intger
510 stored in "secure" memory. */
511 void gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
513 /* Clear FLAG for the big integer A. Note that this function is
514 currently useless as no flags are allowed. */
515 void gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
517 /* Return true when the FLAG is set for A. */
518 int gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
520 /* Unless the GCRYPT_NO_MPI_MACROS is used, provide a couple of
521 convenience macors for the big integer functions. */
522 #ifndef GCRYPT_NO_MPI_MACROS
523 #define mpi_new(n) gcry_mpi_new( (n) )
524 #define mpi_secure_new( n ) gcry_mpi_snew( (n) )
525 #define mpi_release(a) \
528 gcry_mpi_release ((a)); \
533 #define mpi_copy( a ) gcry_mpi_copy( (a) )
534 #define mpi_set( w, u) gcry_mpi_set( (w), (u) )
535 #define mpi_set_ui( w, u) gcry_mpi_set_ui( (w), (u) )
536 #define mpi_cmp( u, v ) gcry_mpi_cmp( (u), (v) )
537 #define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) )
539 #define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v))
540 #define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v))
541 #define mpi_addm(w,u,v,m) gcry_mpi_addm ((w),(u),(v),(m))
542 #define mpi_sub_ui(w,u,v) gcry_mpi_sub_ui ((w),(u),(v))
543 #define mpi_sub(w,u,v) gcry_mpi_sub ((w),(u),(v))
544 #define mpi_subm(w,u,v,m) gcry_mpi_subm ((w),(u),(v),(m))
545 #define mpi_mul_ui(w,u,v) gcry_mpi_mul_ui ((w),(u),(v))
546 #define mpi_mul_2exp(w,u,v) gcry_mpi_mul_2exp ((w),(u),(v))
547 #define mpi_mul(w,u,v) gcry_mpi_mul ((w),(u),(v))
548 #define mpi_mulm(w,u,v,m) gcry_mpi_mulm ((w),(u),(v),(m))
549 #define mpi_powm(w,b,e,m) gcry_mpi_powm ( (w), (b), (e), (m) )
550 #define mpi_tdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), 0)
551 #define mpi_fdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), -1)
552 #define mpi_mod(r,a,m) gcry_mpi_mod ((r), (a), (m))
553 #define mpi_gcd(g,a,b) gcry_mpi_gcd ( (g), (a), (b) )
554 #define mpi_invm(g,a,b) gcry_mpi_invm ( (g), (a), (b) )
556 #define mpi_get_nbits(a) gcry_mpi_get_nbits ((a))
557 #define mpi_test_bit(a,b) gcry_mpi_test_bit ((a),(b))
558 #define mpi_set_bit(a,b) gcry_mpi_set_bit ((a),(b))
559 #define mpi_set_highbit(a,b) gcry_mpi_set_highbit ((a),(b))
560 #define mpi_clear_bit(a,b) gcry_mpi_clear_bit ((a),(b))
561 #define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b))
562 #define mpi_rshift(a,b,c) gcry_mpi_rshift ((a),(b),(c))
564 #define mpi_set_opaque(a,b,c) gcry_mpi_set_opaque( (a), (b), (c) )
565 #define mpi_get_opaque(a,b) gcry_mpi_get_opaque( (a), (b) )
566 #endif /* GCRYPT_NO_MPI_MACROS */
570 /************************************
572 * symmetric cipher functions *
574 ************************************/
576 /* The data object used to hold a handle to an encryption object. */
577 struct gcry_cipher_handle;
578 typedef struct gcry_cipher_handle *gcry_cipher_hd_t;
580 typedef struct gcry_cipher_handle *GCRY_CIPHER_HD _GCRY_GCC_ATTR_DEPRECATED;
581 typedef struct gcry_cipher_handle *GcryCipherHd _GCRY_GCC_ATTR_DEPRECATED;
583 /* All symmetric encryption algorithms are identified by their IDs.
584 More IDs may be registered at runtime. */
585 enum gcry_cipher_algos
587 GCRY_CIPHER_NONE = 0,
588 GCRY_CIPHER_IDEA = 1,
589 GCRY_CIPHER_3DES = 2,
590 GCRY_CIPHER_CAST5 = 3,
591 GCRY_CIPHER_BLOWFISH = 4,
592 GCRY_CIPHER_SAFER_SK128 = 5,
593 GCRY_CIPHER_DES_SK = 6,
595 GCRY_CIPHER_AES192 = 8,
596 GCRY_CIPHER_AES256 = 9,
597 GCRY_CIPHER_TWOFISH = 10,
599 /* other cipher numbers are above 300 for OpenPGP reasons. */
600 GCRY_CIPHER_ARCFOUR = 301, /* fully compatible with RSA's RC4 (tm). */
601 GCRY_CIPHER_DES = 302, /* Yes, this is single key 56 bit DES. */
602 GCRY_CIPHER_TWOFISH128 = 303,
603 GCRY_CIPHER_SERPENT128 = 304,
604 GCRY_CIPHER_SERPENT192 = 305,
605 GCRY_CIPHER_SERPENT256 = 306,
609 /* The Rijndael algorithm is basically AES, so provide some macros. */
610 #define GCRY_CIPHER_AES128 GCRY_CIPHER_AES
611 #define GCRY_CIPHER_RIJNDAEL GCRY_CIPHER_AES
612 #define GCRY_CIPHER_RIJNDAEL128 GCRY_CIPHER_AES128
613 #define GCRY_CIPHER_RIJNDAEL192 GCRY_CIPHER_AES192
614 #define GCRY_CIPHER_RIJNDAEL256 GCRY_CIPHER_AES256
616 /* The supported encryption modes. Note that not all of them are
617 supported for each algorithm. */
618 enum gcry_cipher_modes
620 GCRY_CIPHER_MODE_NONE = 0, /* Not yet specified. */
621 GCRY_CIPHER_MODE_ECB = 1, /* Electronic codebook. */
622 GCRY_CIPHER_MODE_CFB = 2, /* Cipher feedback. */
623 GCRY_CIPHER_MODE_CBC = 3, /* Cipher block chaining. */
624 GCRY_CIPHER_MODE_STREAM = 4, /* Used with stream ciphers. */
625 GCRY_CIPHER_MODE_OFB = 5, /* Outer feedback. */
626 GCRY_CIPHER_MODE_CTR = 6 /* Counter. */
629 /* Flags used with the open function. */
630 enum gcry_cipher_flags
632 GCRY_CIPHER_SECURE = 1, /* Allocate in secure memory. */
633 GCRY_CIPHER_ENABLE_SYNC = 2, /* Enable CFB sync mode. */
634 GCRY_CIPHER_CBC_CTS = 4, /* Enable CBC cipher text stealing (CTS). */
635 GCRY_CIPHER_CBC_MAC = 8 /* Enable CBC message auth. code (MAC). */
639 /* Create a handle for algorithm ALGO to be used in MODE. FLAGS may
640 be given as an bitwise OR of the gcry_cipher_flags values. */
641 gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *handle,
642 int algo, int mode, unsigned int flags);
644 /* Close the cioher handle H and release all resource. */
645 void gcry_cipher_close (gcry_cipher_hd_t h);
647 /* Perform various operations on the cipher object H. */
648 gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer,
651 /* Retrieve various information about the cipher object H. */
652 gcry_error_t gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer,
655 /* Retrieve various information about the cipher algorithm ALGO. */
656 gcry_error_t gcry_cipher_algo_info (int algo, int what, void *buffer,
659 /* Map the cipher algorithm id ALGO to a string representation of that
660 algorithm name. For unknown algorithms this functions returns an
662 const char *gcry_cipher_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
664 /* Map the algorithm name NAME to an cipher algorithm ID. Return 0 if
665 the algorithm name is not known. */
666 int gcry_cipher_map_name (const char *name) _GCRY_GCC_ATTR_PURE;
668 /* Given an ASN.1 object identifier in standard IETF dotted decimal
669 format in STRING, return the encryption mode associated with that
670 OID or 0 if not known or applicable. */
671 int gcry_cipher_mode_from_oid (const char *string) _GCRY_GCC_ATTR_PURE;
673 /* Encrypt the plaintext of size INLEN in IN using the cipher handle H
674 into the buffer OUT which has an allocated length of OUTSIZE. For
675 most algorithms it is possible to pass NULL for in and 0 for INLEN
676 and do a in-place decryption of the data provided in OUT. */
677 gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h,
678 unsigned char *out, size_t outsize,
679 const unsigned char *in, size_t inlen);
681 /* The counterpart to gcry_cipher_encrypt. */
682 gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t h,
683 unsigned char *out, size_t outsize,
684 const unsigned char *in, size_t inlen);
686 /* Set key K of length L for the cipher handle H. (We have to cast
687 away a const char* here - this catch-all ctl function was probably
688 not the best choice) */
689 #define gcry_cipher_setkey(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_KEY, \
692 /* Set initialization vector K of length L for the cipher handle H. */
693 #define gcry_cipher_setiv(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_IV, \
696 /* Reset the handle to the state after open. */
697 #define gcry_cipher_reset(h) gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0)
699 /* Perform the the OpenPGP sync operation if this is enabled for the
701 #define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, \
704 /* Enable or disable CTS in future calls to gcry_encrypt(). CBC mode only. */
705 #define gcry_cipher_cts(h,on) gcry_cipher_ctl( (h), GCRYCTL_SET_CBC_CTS, \
708 /* Set counter for CTR mode. (K,L) must denote a buffer of block size
709 length, or (NULL,0) to set the CTR to the all-zero block. */
710 #define gcry_cipher_setctr(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_CTR, \
713 /* Retrieved the key length used with algorithm A. */
714 size_t gcry_cipher_get_algo_keylen (int algo);
716 /* Retrieve the block length used with algorithm A. */
717 size_t gcry_cipher_get_algo_blklen (int algo);
719 /* Return 0 if the algorithm A is available for use. */
720 #define gcry_cipher_test_algo(a) \
721 gcry_cipher_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
723 /* Get a list consisting of the IDs of the loaded cipher modules. If
724 LIST is zero, write the number of loaded cipher modules to
725 LIST_LENGTH and return. If LIST is non-zero, the first
726 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
727 according size. In case there are less cipher modules than
728 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */
729 gcry_error_t gcry_cipher_list (int *list, int *list_length);
732 /************************************
734 * asymmetric cipher functions *
736 ************************************/
738 /* The algorithms and their IDs we support. */
742 GCRY_PK_RSA_E = 2, /* deprecated */
743 GCRY_PK_RSA_S = 3, /* deprecated */
744 GCRY_PK_ELG_E = 16, /* use only for OpenPGP */
749 /* Flags describing usage capabilities of a PK algorithm. */
750 #define GCRY_PK_USAGE_SIGN 1
751 #define GCRY_PK_USAGE_ENCR 2
753 /* Encrypt the DATA using the public key PKEY and store the result as
754 a newly created S-expression at RESULT. */
755 gcry_error_t gcry_pk_encrypt (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t pkey);
757 /* Decrypt the DATA using the private key SKEY and store the result as
758 a newly created S-expression at RESULT. */
759 gcry_error_t gcry_pk_decrypt (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t skey);
761 /* Sign the DATA using the private key SKEY and store the result as
762 a newly created S-expression at RESULT. */
763 gcry_error_t gcry_pk_sign (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t skey);
765 /* Check the signature SIGVAL on DATA using the public key PKEY. */
766 gcry_error_t gcry_pk_verify (gcry_sexp_t sigval, gcry_sexp_t data, gcry_sexp_t pkey);
768 /* Check that KEY (either private or public) is sane. */
769 gcry_error_t gcry_pk_testkey (gcry_sexp_t key);
771 /* Generate a new key pair according to the parameters given in
772 S_PARMS. The new key pair is returned in as an S-expression in
774 gcry_error_t gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms);
776 /* Catch all function for miscellaneous operations. */
777 gcry_error_t gcry_pk_ctl (int cmd, void *buffer, size_t buflen);
779 /* Retrieve information about the public key algorithm ALGO. */
780 gcry_error_t gcry_pk_algo_info (int algo, int what, void *buffer, size_t *nbytes);
782 /* Map the public key algorithm id ALGO to a string representation of the
783 algorithm name. For unknown algorithms this functions returns an
785 const char *gcry_pk_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
787 /* Map the algorithm NAME to a public key algorithm Id. Return 0 if
788 the algorithm name is not known. */
789 int gcry_pk_map_name (const char* name) _GCRY_GCC_ATTR_PURE;
791 /* Return what is commonly referred as the key length for the given
792 public or private KEY. */
793 unsigned int gcry_pk_get_nbits (gcry_sexp_t key) _GCRY_GCC_ATTR_PURE;
795 /* Please note that keygrip is still experimental and should not be
796 used without contacting the author. */
797 unsigned char *gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array);
799 /* Return 0 if the public key algorithm A is available for use. */
800 #define gcry_pk_test_algo(a) \
801 gcry_pk_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
803 /* Get a list consisting of the IDs of the loaded pubkey modules. If
804 LIST is zero, write the number of loaded pubkey modules to
805 LIST_LENGTH and return. If LIST is non-zero, the first
806 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
807 according size. In case there are less pubkey modules than
808 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */
809 gcry_error_t gcry_pk_list (int *list, int *list_length);
811 /* Alternative interface for asymetric cryptography. */
813 /* The algorithm IDs. */
814 typedef enum gcry_ac_id
823 typedef enum gcry_ac_key_type
830 /* Flags for data. */
831 #define GCRY_AC_FLAG_DATA_NO_BLINDING 1 << 0
833 /* This type represents a `data set'. */
834 typedef struct gcry_ac_data *gcry_ac_data_t;
836 /* This type represents a single `key', either a secret one or a
838 typedef struct gcry_ac_key *gcry_ac_key_t;
840 /* This type represents a `key pair' containing a secret and a public
842 typedef struct gcry_ac_key_pair *gcry_ac_key_pair_t;
844 /* This type represents a `handle' that is needed by functions
845 performing cryptographic operations. */
846 typedef struct gcry_ac_handle *gcry_ac_handle_t;
848 /* The caller of gcry_ac_key_pair_generate can provide one of these
849 structures in order to influence the key generation process in an
850 algorithm-specific way. */
851 typedef struct gcry_ac_key_spec_rsa
853 gcry_mpi_t e; /* E to use. */
854 } gcry_ac_key_spec_rsa_t;
856 /* Returns a new, empty data set in DATA. */
857 gcry_error_t gcry_ac_data_new (gcry_ac_data_t *data);
859 /* Destroy the data set DATA. */
860 void gcry_ac_data_destroy (gcry_ac_data_t data);
862 /* Add the value MPI to DATA with the label NAME. If there is already
863 a value with that label, replace it, otherwise add it. */
864 gcry_error_t gcry_ac_data_set (gcry_ac_data_t data,
868 /* Create a copy of the data set DATA and store it in DATA_CP. */
869 gcry_error_t gcry_ac_data_copy (gcry_ac_data_t *data_cp,
870 gcry_ac_data_t data);
872 /* Return the number of named MPI values inside of the data set
874 unsigned int gcry_ac_data_length (gcry_ac_data_t data);
876 /* Store the value labelled with NAME found in DATA in MPI or NULL if
877 a value with that label was not found. */
878 gcry_error_t gcry_ac_data_get_name (gcry_ac_data_t data, const char *name,
881 /* Return the MPI value with index INDEX contained in the data set
883 gcry_error_t gcry_ac_data_get_index (gcry_ac_data_t data, unsigned int index,
884 const char **name, gcry_mpi_t *mpi);
886 /* Destroy any values contained in the data set DATA. */
887 void gcry_ac_data_clear (gcry_ac_data_t data);
889 /* Create a new ac handle. */
890 gcry_error_t gcry_ac_open (gcry_ac_handle_t *handle,
891 gcry_ac_id_t algorithm,
894 /* Destroy an ac handle. */
895 void gcry_ac_close (gcry_ac_handle_t handle);
897 /* Initialize a key from a given data set. */
898 gcry_error_t gcry_ac_key_init (gcry_ac_key_t *key,
899 gcry_ac_handle_t handle,
900 gcry_ac_key_type_t type,
901 gcry_ac_data_t data);
903 /* Generate a new key pair. */
904 gcry_error_t gcry_ac_key_pair_generate (gcry_ac_handle_t handle,
905 gcry_ac_key_pair_t *key_pair,
909 /* Returns a specified key from a key pair. */
910 gcry_ac_key_t gcry_ac_key_pair_extract (gcry_ac_key_pair_t key_pair,
911 gcry_ac_key_type_t which);
913 /* Returns the data set contained in the key KEY. */
914 gcry_ac_data_t gcry_ac_key_data_get (gcry_ac_key_t key);
916 /* Verify that the key KEY is sane. */
917 gcry_error_t gcry_ac_key_test (gcry_ac_key_t key);
919 /* Return the number of bits of the key KEY in NBITS. */
920 gcry_error_t gcry_ac_key_get_nbits (gcry_ac_key_t key,
921 unsigned int *nbits);
923 /* Write the 20 byte long key grip of the key KEY to KEY_GRIP. */
924 gcry_error_t gcry_ac_key_get_grip (gcry_ac_key_t key,
925 unsigned char *key_grip);
928 void gcry_ac_key_destroy (gcry_ac_key_t key);
930 /* Destroy a key pair. */
931 void gcry_ac_key_pair_destroy (gcry_ac_key_pair_t key_pair);
933 /* Encrypt the plain text MPI value DATA_PLAIN with the key KEY under
934 the control of the flags FLAGS and store the resulting data set
935 into DATA_ENCRYPTED. */
936 gcry_error_t gcry_ac_data_encrypt (gcry_ac_handle_t handle,
939 gcry_mpi_t data_plain,
940 gcry_ac_data_t *data_encrypted);
942 /* Decrypt the decrypted data contained in the data set DATA_ENCRYPTED
943 with the key KEY under the control of the flags FLAGS and store the
944 resulting plain text MPI value in DATA_PLAIN. */
945 gcry_error_t gcry_ac_data_decrypt (gcry_ac_handle_t handle,
948 gcry_mpi_t *data_plain,
949 gcry_ac_data_t data_encrypted);
951 /* Sign the data contained in DATA with the key KEY and store the
952 resulting signature in the data set DATA_SIGNATURE. */
953 gcry_error_t gcry_ac_data_sign (gcry_ac_handle_t handle,
956 gcry_ac_data_t *data_signature);
958 /* Verify that the signature contained in the data set DATA_SIGNATURE
959 is indeed the result of signing the data contained in DATA with the
960 secret key belonging to the public key KEY. */
961 gcry_error_t gcry_ac_data_verify (gcry_ac_handle_t handle,
964 gcry_ac_data_t data_signature);
966 /* Store the textual representation of the algorithm whose id is given
967 in ALGORITHM in NAME. */
968 gcry_error_t gcry_ac_id_to_name (gcry_ac_id_t algorithm,
971 /* Store the numeric ID of the algorithm whose textual representation
972 is contained in NAME in ALGORITHM. */
973 gcry_error_t gcry_ac_name_to_id (const char *name,
974 gcry_ac_id_t *algorithm);
978 /************************************
980 * cryptograhic hash functions *
982 ************************************/
984 /* Algorithm IDs for the hash functions we know about. Not all of them
993 GCRY_MD_TIGER = 6, /* TIGER/192. */
994 GCRY_MD_HAVAL = 7, /* HAVAL, 5 pass, 160 bit. */
1000 GCRY_MD_CRC32_RFC1510 = 303,
1001 GCRY_MD_CRC24_RFC2440 = 304
1004 /* Flags used with the open function. */
1006 /* Allocate all buffers in "secure" memory. */
1007 #define GCRY_MD_FLAG_SECURE 1 << 0
1008 /* Make an HMAC out of this algorithm. */
1009 #define GCRY_MD_FLAG_HMAC 1 << 1
1011 /* Forward declaration. */
1012 struct gcry_md_context;
1014 /* This object is used to hold a handle to a message digest object.
1015 This structure is private - only to be used by the public gcry_md_*
1017 typedef struct gcry_md_handle
1019 /* Actual context. */
1020 struct gcry_md_context *ctx;
1022 /* Buffer management. */
1025 unsigned char buf[1];
1028 /* Compatibility types, do not use them. */
1029 typedef struct gcry_md_handle *GCRY_MD_HD _GCRY_GCC_ATTR_DEPRECATED;
1030 typedef struct gcry_md_handle *GcryMDHd _GCRY_GCC_ATTR_DEPRECATED;
1032 /* Create a message digest object for algorithm ALGO. FLAGS may be
1033 given as an bitwise OR of the gcry_md_flags values. ALGO may be
1034 given as 0 if the algorithms to be used are later set using
1036 gcry_error_t gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags);
1038 /* Release the message digest object HD. */
1039 void gcry_md_close (gcry_md_hd_t hd);
1041 /* Add the message digest algorithm ALGO to the digest object HD. */
1042 gcry_error_t gcry_md_enable (gcry_md_hd_t hd, int algo);
1044 /* Create a new digest object as an exact copy of the object HD. */
1045 gcry_error_t gcry_md_copy (gcry_md_hd_t *bhd, gcry_md_hd_t ahd);
1047 /* Reset the digest object HD to its initial state. */
1048 void gcry_md_reset (gcry_md_hd_t hd);
1050 /* Perform various operations on the digets object HD. */
1051 gcry_error_t gcry_md_ctl (gcry_md_hd_t hd, int cmd, unsigned char *buffer,
1054 /* Pass LENGTH bytes of data in BUFFER to the digest object HD so that
1055 it can update the digest values. This is the actual hash
1057 void gcry_md_write (gcry_md_hd_t hd, const void *buffer, size_t length);
1059 /* Read out the final digest from HD return the digest value for
1061 unsigned char *gcry_md_read (gcry_md_hd_t hd, int algo);
1063 /* Convenience function to calculate the hash from the data in BUFFER
1064 of size LENGTH using the algorithm ALGO avoiding the creating of a
1065 hash object. The hash is returned in the caller provided buffer
1066 DIGEST which must be large enough to hold the digest of the given
1068 void gcry_md_hash_buffer (int algo, void *digest,
1069 const void *buffer, size_t length);
1071 /* Retrieve the algorithm used with HD. This does not work reliable
1072 if more than one algorithm is enabled in HD. */
1073 int gcry_md_get_algo (gcry_md_hd_t hd);
1075 /* Retrieve the length in bytes of the digest yielded by algorithm
1077 unsigned int gcry_md_get_algo_dlen (int algo);
1079 /* Return true if the the algorithm ALGO is enabled in the digest
1081 int gcry_md_is_enabled (gcry_md_hd_t a, int algo);
1083 /* Return true if the digest object A is allocated in "secure" memory. */
1084 int gcry_md_is_secure (gcry_md_hd_t a);
1086 /* Retrieve various information about the object H. */
1087 gcry_error_t gcry_md_info (gcry_md_hd_t h, int what, void *buffer,
1090 /* Retrieve various information about the algorithm ALGO. */
1091 gcry_error_t gcry_md_algo_info (int algo, int what, void *buffer,
1094 /* Map the digest algorithm id ALGO to a string representation of the
1095 algorithm name. For unknown algorithms this functions returns an
1097 const char *gcry_md_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
1099 /* Map the algorithm NAME to a digest algorithm Id. Return 0 if
1100 the algorithm name is not known. */
1101 int gcry_md_map_name (const char* name) _GCRY_GCC_ATTR_PURE;
1103 /* For use with the HMAC feature, the set MAC key to the KEY of
1105 gcry_error_t gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen);
1107 /* Update the hash(s) of H with the character C. This is a buffered
1108 version of the gcry_md_write function. */
1109 #define gcry_md_putc(h,c) \
1111 gcry_md_hd_t h__ = (h); \
1112 if( (h__)->bufpos == (h__)->bufsize ) \
1113 gcry_md_write( (h__), NULL, 0 ); \
1114 (h__)->buf[(h__)->bufpos++] = (c) & 0xff; \
1117 /* Finalize the digest calculation. This is not really needed because
1118 gcry_md_read() does this implicitly. */
1119 #define gcry_md_final(a) \
1120 gcry_md_ctl ((a), GCRYCTL_FINALIZE, NULL, 0)
1122 /* Return 0 if the algorithm A is available for use. */
1123 #define gcry_md_test_algo(a) \
1124 gcry_md_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
1126 /* Return an DER encoded ASN.1 OID for the algorithm A in buffer B. N
1127 must point to size_t variable with the available size of buffer B.
1128 After return it will receive the actual size of the returned
1130 #define gcry_md_get_asnoid(a,b,n) \
1131 gcry_md_algo_info((a), GCRYCTL_GET_ASNOID, (b), (n))
1133 /* Enable debugging for digets object A; i.e. create files named
1134 dbgmd-<n>.<string> while hashing. B is a string used as the suffix
1135 for the filename. */
1136 #define gcry_md_start_debug(a,b) \
1137 gcry_md_ctl( (a), GCRYCTL_START_DUMP, (b), 0 )
1139 /* Disable the debugging of A. */
1140 #define gcry_md_stop_debug(a,b) \
1141 gcry_md_ctl( (a), GCRYCTL_STOP_DUMP, (b), 0 )
1143 /* Get a list consisting of the IDs of the loaded message digest
1144 modules. If LIST is zero, write the number of loaded message
1145 digest modules to LIST_LENGTH and return. If LIST is non-zero, the
1146 first *LIST_LENGTH algorithm IDs are stored in LIST, which must be
1147 of according size. In case there are less message digest modules
1148 than *LIST_LENGTH, *LIST_LENGTH is updated to the correct
1150 gcry_error_t gcry_md_list (int *list, int *list_length);
1153 /************************************
1155 * random generating functions *
1157 ************************************/
1159 /* The possible values for the random quality. The rule of thumb is
1160 to use WEAK for random number which don't need to be
1161 cryptographically strong, STRONG for session keys and VERY_STRONG
1162 for key material. */
1163 enum gcry_random_level
1165 GCRY_WEAK_RANDOM = 0,
1166 GCRY_STRONG_RANDOM = 1,
1167 GCRY_VERY_STRONG_RANDOM = 2
1171 /* Fill BUFFER with LENGTH bytes of random, using random numbers of
1173 void gcry_randomize (unsigned char *buffer, size_t length,
1174 enum gcry_random_level level);
1176 /* Add the external random from BUFFER with LENGTH bytes into the
1177 pool. QUALITY should either be -1 for unknown or in the range of 0
1179 gcry_error_t gcry_random_add_bytes (const void *buffer, size_t length,
1182 /* Return NBYTES of allocated random using a random numbers of quality
1184 void *gcry_random_bytes (size_t nbytes, enum gcry_random_level level)
1185 _GCRY_GCC_ATTR_MALLOC;
1187 /* Return NBYTES of allocated random using a random numbers of quality
1188 LEVEL. The random numbers are created returned in "secure"
1190 void *gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level)
1191 _GCRY_GCC_ATTR_MALLOC;
1194 /* Set the big inetger W to a random value of NBITS using a random
1195 generator with quality LEVEL. */
1196 void gcry_mpi_randomize (gcry_mpi_t w,
1197 unsigned int nbits, enum gcry_random_level level);
1201 /************************************
1203 * miscellaneous stuff *
1205 ************************************/
1207 /* Log levels used by the internal logging facility. */
1208 enum gcry_log_levels
1210 GCRY_LOG_CONT = 0, /* continue the last log line */
1213 GCRY_LOG_ERROR = 30,
1214 GCRY_LOG_FATAL = 40,
1216 GCRY_LOG_DEBUG = 100
1219 /* Type for progress handlers. */
1220 typedef void (*gcry_handler_progress_t) (void *, const char *, int, int, int);
1222 /* Type for memory allocation handlers. */
1223 typedef void *(*gcry_handler_alloc_t) (size_t n);
1225 /* Type for secure memory check handlers. */
1226 typedef int (*gcry_handler_secure_check_t) (const void *);
1228 /* Type for memory reallocation handlers. */
1229 typedef void *(*gcry_handler_realloc_t) (void *p, size_t n);
1231 /* Type for memory free handlers. */
1232 typedef void (*gcry_handler_free_t) (void *);
1234 /* Type for out-of-memory handlers. */
1235 typedef int (*gcry_handler_no_mem_t) (void *, size_t, unsigned int);
1237 /* Type for fatal error handlers. */
1238 typedef void (*gcry_handler_error_t) (void *, int, const char *);
1240 /* Type for logging handlers. */
1241 typedef void (*gcry_handler_log_t) (void *, int, const char *, va_list);
1243 /* Certain operations can provide progress information. This function
1244 is used to register a handler for retrieving these information. */
1245 void gcry_set_progress_handler (gcry_handler_progress_t cb, void *cb_data);
1248 /* Register a custom memory allocation functions. */
1249 void gcry_set_allocation_handler (gcry_handler_alloc_t func_alloc,
1250 gcry_handler_alloc_t func_alloc_secure,
1251 gcry_handler_secure_check_t func_secure_check,
1252 gcry_handler_realloc_t func_realloc,
1253 gcry_handler_free_t func_free);
1255 /* Register a function used instead of the internal out of memory
1257 void gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque);
1259 /* Register a function used instead of the internal fatal error
1261 void gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque);
1263 /* Register a function used instead of the internal logging
1265 void gcry_set_log_handler (gcry_handler_log_t f, void *opaque);
1267 /* Reserved for future use. */
1268 void gcry_set_gettext_handler (const char *(*f)(const char*));
1270 /* Libgcrypt uses its own memory allocation. It is important to use
1271 gcry_free () to release memory allocated by libgcrypt. */
1272 void *gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
1273 void *gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1274 void *gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
1275 void *gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1276 void *gcry_realloc (void *a, size_t n);
1277 char *gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC;
1278 void *gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
1279 void *gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1280 void *gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
1281 void *gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1282 void *gcry_xrealloc (void *a, size_t n);
1283 char *gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC;
1284 void gcry_free (void *a);
1286 /* Return true if A is allocated in "secure" memory. */
1287 int gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE;
1289 /* Include support for Libgcrypt modules. */
1290 #include <gcrypt-module.h>
1292 #if 0 /* keep Emacsens's auto-indent happy */
1298 #endif /* _GCRYPT_H */