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 /* The data object used to hold a multi precision integer. */
135 typedef struct gcry_mpi *gcry_mpi_t;
137 typedef struct gcry_mpi *GCRY_MPI _GCRY_GCC_ATTR_DEPRECATED;
138 typedef struct gcry_mpi *GcryMPI _GCRY_GCC_ATTR_DEPRECATED;
142 /* Check that the library fulfills the version requirement. */
143 const char *gcry_check_version (const char *req_version);
145 /* Codes for function dispatchers. */
147 /* Codes used with the gcry_control function. */
152 GCRYCTL_CFB_SYNC = 3,
153 GCRYCTL_RESET = 4, /* e.g. for MDs */
154 GCRYCTL_FINALIZE = 5,
155 GCRYCTL_GET_KEYLEN = 6,
156 GCRYCTL_GET_BLKLEN = 7,
157 GCRYCTL_TEST_ALGO = 8,
158 GCRYCTL_IS_SECURE = 9,
159 GCRYCTL_GET_ASNOID = 10,
160 GCRYCTL_ENABLE_ALGO = 11,
161 GCRYCTL_DISABLE_ALGO = 12,
162 GCRYCTL_DUMP_RANDOM_STATS = 13,
163 GCRYCTL_DUMP_SECMEM_STATS = 14,
164 GCRYCTL_GET_ALGO_NPKEY = 15,
165 GCRYCTL_GET_ALGO_NSKEY = 16,
166 GCRYCTL_GET_ALGO_NSIGN = 17,
167 GCRYCTL_GET_ALGO_NENCR = 18,
168 GCRYCTL_SET_VERBOSITY = 19,
169 GCRYCTL_SET_DEBUG_FLAGS = 20,
170 GCRYCTL_CLEAR_DEBUG_FLAGS = 21,
171 GCRYCTL_USE_SECURE_RNDPOOL= 22,
172 GCRYCTL_DUMP_MEMORY_STATS = 23,
173 GCRYCTL_INIT_SECMEM = 24,
174 GCRYCTL_TERM_SECMEM = 25,
175 GCRYCTL_DISABLE_SECMEM_WARN = 27,
176 GCRYCTL_SUSPEND_SECMEM_WARN = 28,
177 GCRYCTL_RESUME_SECMEM_WARN = 29,
178 GCRYCTL_DROP_PRIVS = 30,
179 GCRYCTL_ENABLE_M_GUARD = 31,
180 GCRYCTL_START_DUMP = 32,
181 GCRYCTL_STOP_DUMP = 33,
182 GCRYCTL_GET_ALGO_USAGE = 34,
183 GCRYCTL_IS_ALGO_ENABLED = 35,
184 GCRYCTL_DISABLE_INTERNAL_LOCKING = 36,
185 GCRYCTL_DISABLE_SECMEM = 37,
186 GCRYCTL_INITIALIZATION_FINISHED = 38,
187 GCRYCTL_INITIALIZATION_FINISHED_P = 39,
188 GCRYCTL_ANY_INITIALIZATION_P = 40,
189 GCRYCTL_SET_CBC_CTS = 41,
190 GCRYCTL_SET_CBC_MAC = 42,
191 GCRYCTL_SET_CTR = 43,
192 GCRYCTL_ENABLE_QUICK_RANDOM = 44,
195 /* Perform various operations defined by CMD. */
196 gcry_error_t gcry_control (enum gcry_ctl_cmds CMD, ...);
198 const char *gcry_strerror (gcry_error_t ec);
201 /* S-expression management. */
203 /* The object to represent an S-expression as used with the public key
206 typedef struct gcry_sexp *gcry_sexp_t;
208 typedef struct gcry_sexp *GCRY_SEXP _GCRY_GCC_ATTR_DEPRECATED;
209 typedef struct gcry_sexp *GcrySexp _GCRY_GCC_ATTR_DEPRECATED;
211 /* The possible values for the S-expression format. */
212 enum gcry_sexp_format
214 GCRYSEXP_FMT_DEFAULT = 0,
215 GCRYSEXP_FMT_CANON = 1,
216 GCRYSEXP_FMT_BASE64 = 2,
217 GCRYSEXP_FMT_ADVANCED = 3
220 /* Create an new S-expression object from BUFFER of size LENGTH and
221 return it in RETSEXP. With AUTODETECT set to 0 the data in BUFFER
222 is expected to be in canonized format. */
223 gcry_error_t gcry_sexp_new (gcry_sexp_t *retsexp, const void *buffer, size_t length,
226 /* Same as gcry_sexp_new but allows to pass a FREEFNC which has the
227 effect to transfer ownership of BUFFER to the created object. */
228 gcry_error_t gcry_sexp_create (gcry_sexp_t *retsexp, void *buffer, size_t length,
229 int autodetect, void (*freefnc) (void *));
231 /* Scan BUFFER and return a new S-expression object in RETSEXP. This
232 function expects a printf like string in BUFFER. */
233 gcry_error_t gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff,
234 const char *buffer, size_t length);
236 /* Same as gcry_sexp_sscan but expects a string in FORMAT and can thus
237 only be used for certain encodings. */
238 gcry_error_t gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff,
239 const char *format, ...);
241 /* Like gcry_sexp_build, but uses an array instead of variable
242 function arguments. */
243 gcry_error_t gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff,
244 const char *format, void **arg_list);
246 /* Release the S-expression object SEXP */
247 void gcry_sexp_release (gcry_sexp_t sexp);
249 /* Calculate the length of an canonized S-expresion in BUFFER and
250 check for a valid encoding. */
251 size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length,
252 size_t *erroff, gcry_error_t *errcode);
254 /* Copies the S-expression object SEXP into BUFFER using the format
255 specified in MODE. */
256 size_t gcry_sexp_sprint (gcry_sexp_t sexp, int mode, char *buffer,
259 /* Dumps the S-expression object A in a aformat suitable for debugging
260 to Libgcrypt's logging stream. */
261 void gcry_sexp_dump (const gcry_sexp_t a);
263 gcry_sexp_t gcry_sexp_cons (const gcry_sexp_t a, const gcry_sexp_t b);
264 gcry_sexp_t gcry_sexp_alist (const gcry_sexp_t *array);
265 gcry_sexp_t gcry_sexp_vlist (const gcry_sexp_t a, ...);
266 gcry_sexp_t gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n);
267 gcry_sexp_t gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n);
269 /* Scan the S-expression for a sublist with a type (the car of the
270 list) matching the string TOKEN. If TOKLEN is not 0, the token is
271 assumed to be raw memory of this length. The function returns a
272 newly allocated S-expression consisting of the found sublist or
273 `NULL' when not found. */
274 gcry_sexp_t gcry_sexp_find_token (gcry_sexp_t list,
275 const char *tok, size_t toklen);
276 /* Return the length of the LIST. For a valid S-expression this
277 should be at least 1. */
278 int gcry_sexp_length (const gcry_sexp_t list);
280 /* Create and return a new S-expression from the element with index
281 NUMBER in LIST. Note that the first element has the index 0. If
282 there is no such element, `NULL' is returned. */
283 gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t list, int number);
285 /* Create and return a new S-expression from the first element in
286 LIST; this called the "type" and should always exist and be a
287 string. `NULL' is returned in case of a problem. */
288 gcry_sexp_t gcry_sexp_car (const gcry_sexp_t list);
290 /* Create and return a new list form all elements except for the first
291 one. Note, that this function may return an invalid S-expression
292 because it is not guaranteed, that the type exists and is a string.
293 However, for parsing a complex S-expression it might be useful for
294 intermediate lists. Returns `NULL' on error. */
295 gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t list);
297 gcry_sexp_t gcry_sexp_cadr (const gcry_sexp_t list);
300 /* This function is used to get data from a LIST. A pointer to the
301 actual data with index NUMBER is returned and the length of this
302 data will be stored to DATALEN. If there is no data at the given
303 index or the index represents another list, `NULL' is returned.
304 *Note:* The returned pointer is valid as long as LIST is not
305 modified or released. */
306 const char *gcry_sexp_nth_data (const gcry_sexp_t list, int number,
309 /* This function is used to get and convert data from a LIST. This
310 data is assumed to be an MPI stored in the format described by
311 MPIFMT and returned as a standard Libgcrypt MPI. The caller must
312 release this returned value using `gcry_mpi_release'. If there is
313 no data at the given index, the index represents a list or the
314 value can't be converted to an MPI, `NULL' is returned. */
315 gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt);
319 /*******************************************
321 * multi precision integer functions *
323 *******************************************/
325 /* Different formats of external big integer representation. */
329 GCRYMPI_FMT_STD = 1, /* twos complement stored without length */
330 GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP (only defined as unsigned)*/
331 GCRYMPI_FMT_SSH = 3, /* As used by SSH (same as 1 but with length)*/
332 GCRYMPI_FMT_HEX = 4, /* hex format */
333 GCRYMPI_FMT_USG = 5 /* like STD but this is an unsigned one */
336 /* Flags used for creating big integers. */
339 GCRYMPI_FLAG_SECURE = 1, /* Allocate the number in "secure" memory. */
340 GCRYMPI_FLAG_OPAQUE = 2 /* The number is not a real one but just a
341 way to store some bytes. This is
342 useful for encrypted big integers. */
346 /* Allocate a new big integer object, initialize it with 0 and
347 initially allocate memory for a number of at least NBITS. */
348 gcry_mpi_t gcry_mpi_new (unsigned int nbits);
350 /* Same as gcry_mpi_new() but allocate in "secure" memory. */
351 gcry_mpi_t gcry_mpi_snew (unsigned int nbits);
353 /* Release the number A and free all associated resources. */
354 void gcry_mpi_release (gcry_mpi_t a);
356 /* Create a new number with the same value as A. */
357 gcry_mpi_t gcry_mpi_copy (const gcry_mpi_t a);
359 /* Store the big integer value U in W. */
360 gcry_mpi_t gcry_mpi_set (gcry_mpi_t w, const gcry_mpi_t u);
362 /* Store the unsigned integer value U in W. */
363 gcry_mpi_t gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u);
365 /* Swap the values of A and B. */
366 void gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b);
368 /* Compare the big integer number U and V returning 0 for equality, a
369 positive value for U > V and a negative for U < V. */
370 int gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v);
372 /* Compare the big integer number U with the unsigned integer V
373 returning 0 for equality, a positive value for U > V and a negative
375 int gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v);
377 /* Convert the external representation of an integer stored in BUFFER
378 with a length of BUFLEN into a newly create MPI returned in
379 RET_MPI. If NSCANNED is not NULL, it will receive the number of
380 bytes actually scanned after a successful operation. */
381 gcry_error_t gcry_mpi_scan (gcry_mpi_t *ret_mpi, enum gcry_mpi_format format,
382 const unsigned char *buffer, size_t buflen,
385 /* Convert the big integer A into the external representation
386 described by FORMAT and store it in the provided BUFFER which has
387 been allocated by the user with a size of BUFLEN bytes. NWRITTEN
388 receives the actual length of the external representation unless it
389 has been passed as NULL. */
390 gcry_error_t gcry_mpi_print (enum gcry_mpi_format format,
391 unsigned char *buffer, size_t buflen,
395 /* Convert the big integer A int the external representation described
396 by FORMAT and store it in a newly allocated buffer which address
397 will be put into BUFFER. NWRITTEN receives the actual lengths of the
398 external representation. */
399 gcry_error_t gcry_mpi_aprint (enum gcry_mpi_format format,
400 unsigned char **buffer, size_t *nwritten,
403 /* Dump the value of A in a format suitable for debugging to
404 Libgcrypt's logging stream. Note that one leading space but no
405 trailing space or linefeed will be printed. It is okay to pass
407 void gcry_mpi_dump (const gcry_mpi_t a);
411 void gcry_mpi_add (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
413 /* W = U + V. V is an unsigned integer. */
414 void gcry_mpi_add_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v);
416 /* W = U + V mod M. */
417 void gcry_mpi_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
420 void gcry_mpi_sub (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
422 /* W = U - V. V is an unsigned integer. */
423 void gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v );
425 /* W = U - V mod M */
426 void gcry_mpi_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
429 void gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
431 /* W = U * V. V is an unsigned integer. */
432 void gcry_mpi_mul_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v );
434 /* W = U * V mod M. */
435 void gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
437 /* W = U * (2 ^ CNT). */
438 void gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt);
440 /* Q = DIVIDEND / DIVISOR, R = DIVIDEND % DIVISOR,
441 Q or R may be passed as NULL. ROUND should be negative or 0. */
442 void gcry_mpi_div (gcry_mpi_t q, gcry_mpi_t r,
443 gcry_mpi_t dividend, gcry_mpi_t divisor, int round);
445 /* R = DIVIDEND % DIVISOR */
446 void gcry_mpi_mod (gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor);
448 /* W = B ^ E mod M. */
449 void gcry_mpi_powm (gcry_mpi_t w,
450 const gcry_mpi_t b, const gcry_mpi_t e, const gcry_mpi_t m);
452 /* Set G to the greatest common divisor of A and B.
453 Return true if the G is 1. */
454 int gcry_mpi_gcd (gcry_mpi_t g, gcry_mpi_t a, gcry_mpi_t b);
456 /* Set X to the multiplicative inverse of A mod M.
457 Return true if the value exists. */
458 int gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t m);
461 /* Return the number of bits required to represent A. */
462 unsigned int gcry_mpi_get_nbits (gcry_mpi_t a);
464 /* Return true when bit number N (counting from 0) is set in A. */
465 int gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n);
467 /* Set bit number N in A. */
468 void gcry_mpi_set_bit (gcry_mpi_t a, unsigned int n);
470 /* Clear bit number N in A. */
471 void gcry_mpi_clear_bit (gcry_mpi_t a, unsigned int n);
473 /* Set bit number N in A and clear all bits greater than N. */
474 void gcry_mpi_set_highbit (gcry_mpi_t a, unsigned int n);
476 /* Clear bit number N in A and all bits greater than N. */
477 void gcry_mpi_clear_highbit (gcry_mpi_t a, unsigned int n);
479 /* Shift the value of A by N bits to the right and store the result in X. */
480 void gcry_mpi_rshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n);
482 /* Store NBITS of the value P points to in A and mark A as an opaque
483 value. WARNING: Never use an opaque MPI for anything thing else then
484 gcry_mpi_release, gcry_mpi_get_opaque. */
485 gcry_mpi_t gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits);
487 /* Return a pointer to an opaque value stored in A and return its size
488 in NBITS. Note that the returned pointer is still owned by A and
489 that the function should never be used for an non-opaque MPI. */
490 void *gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits);
492 /* Set the FLAG for the big integer A. Currently only the flag
493 GCRYMPI_FLAG_SECURE is allowed to convert A into an big intger
494 stored in "secure" memory. */
495 void gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
497 /* Clear FLAG for the big integer A. Note that this function is
498 currently useless as no flags are allowed. */
499 void gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
501 /* Return true when the FLAG is set for A. */
502 int gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
504 /* Unless the GCRYPT_NO_MPI_MACROS is used, provide a couple of
505 convenience macors for the big integer functions. */
506 #ifndef GCRYPT_NO_MPI_MACROS
507 #define mpi_new(n) gcry_mpi_new( (n) )
508 #define mpi_secure_new( n ) gcry_mpi_snew( (n) )
509 #define mpi_release(a) \
512 gcry_mpi_release ((a)); \
517 #define mpi_copy( a ) gcry_mpi_copy( (a) )
518 #define mpi_set( w, u) gcry_mpi_set( (w), (u) )
519 #define mpi_set_ui( w, u) gcry_mpi_set_ui( (w), (u) )
520 #define mpi_cmp( u, v ) gcry_mpi_cmp( (u), (v) )
521 #define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) )
523 #define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v))
524 #define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v))
525 #define mpi_addm(w,u,v,m) gcry_mpi_addm ((w),(u),(v),(m))
526 #define mpi_sub_ui(w,u,v) gcry_mpi_sub_ui ((w),(u),(v))
527 #define mpi_sub(w,u,v) gcry_mpi_sub ((w),(u),(v))
528 #define mpi_subm(w,u,v,m) gcry_mpi_subm ((w),(u),(v),(m))
529 #define mpi_mul_ui(w,u,v) gcry_mpi_mul_ui ((w),(u),(v))
530 #define mpi_mul_2exp(w,u,v) gcry_mpi_mul_2exp ((w),(u),(v))
531 #define mpi_mul(w,u,v) gcry_mpi_mul ((w),(u),(v))
532 #define mpi_mulm(w,u,v,m) gcry_mpi_mulm ((w),(u),(v),(m))
533 #define mpi_powm(w,b,e,m) gcry_mpi_powm ( (w), (b), (e), (m) )
534 #define mpi_tdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), 0)
535 #define mpi_fdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), -1)
536 #define mpi_mod(r,a,m) gcry_mpi_mod ((r), (a), (m))
537 #define mpi_gcd(g,a,b) gcry_mpi_gcd ( (g), (a), (b) )
538 #define mpi_invm(g,a,b) gcry_mpi_invm ( (g), (a), (b) )
540 #define mpi_get_nbits(a) gcry_mpi_get_nbits ((a))
541 #define mpi_test_bit(a,b) gcry_mpi_test_bit ((a),(b))
542 #define mpi_set_bit(a,b) gcry_mpi_set_bit ((a),(b))
543 #define mpi_set_highbit(a,b) gcry_mpi_set_highbit ((a),(b))
544 #define mpi_clear_bit(a,b) gcry_mpi_clear_bit ((a),(b))
545 #define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b))
546 #define mpi_rshift(a,b,c) gcry_mpi_rshift ((a),(b),(c))
548 #define mpi_set_opaque(a,b,c) gcry_mpi_set_opaque( (a), (b), (c) )
549 #define mpi_get_opaque(a,b) gcry_mpi_get_opaque( (a), (b) )
550 #endif /* GCRYPT_NO_MPI_MACROS */
554 /************************************
556 * symmetric cipher functions *
558 ************************************/
560 /* The data object used to hold a handle to an encryption object. */
561 struct gcry_cipher_handle;
562 typedef struct gcry_cipher_handle *gcry_cipher_hd_t;
564 typedef struct gcry_cipher_handle *GCRY_CIPHER_HD _GCRY_GCC_ATTR_DEPRECATED;
565 typedef struct gcry_cipher_handle *GcryCipherHd _GCRY_GCC_ATTR_DEPRECATED;
567 /* All symmetric encryption algorithms are identified by their IDs.
568 More IDs may be registered at runtime. */
569 enum gcry_cipher_algos
571 GCRY_CIPHER_NONE = 0,
572 GCRY_CIPHER_IDEA = 1,
573 GCRY_CIPHER_3DES = 2,
574 GCRY_CIPHER_CAST5 = 3,
575 GCRY_CIPHER_BLOWFISH = 4,
576 GCRY_CIPHER_SAFER_SK128 = 5,
577 GCRY_CIPHER_DES_SK = 6,
579 GCRY_CIPHER_AES192 = 8,
580 GCRY_CIPHER_AES256 = 9,
581 GCRY_CIPHER_TWOFISH = 10,
583 /* other cipher numbers are above 300 for OpenPGP reasons. */
584 GCRY_CIPHER_ARCFOUR = 301, /* fully compatible with RSA's RC4 (tm). */
585 GCRY_CIPHER_DES = 302, /* Yes, this is single key 56 bit DES. */
586 GCRY_CIPHER_TWOFISH128 = 303,
587 GCRY_CIPHER_SERPENT128 = 304,
588 GCRY_CIPHER_SERPENT192 = 305,
589 GCRY_CIPHER_SERPENT256 = 306,
593 /* The Rijndael algorithm is basically AES, so provide some macros. */
594 #define GCRY_CIPHER_AES128 GCRY_CIPHER_AES
595 #define GCRY_CIPHER_RIJNDAEL GCRY_CIPHER_AES
596 #define GCRY_CIPHER_RIJNDAEL128 GCRY_CIPHER_AES128
597 #define GCRY_CIPHER_RIJNDAEL192 GCRY_CIPHER_AES192
598 #define GCRY_CIPHER_RIJNDAEL256 GCRY_CIPHER_AES256
600 /* The supported encryption modes. Note that not all of them are
601 supported for each algorithm. */
602 enum gcry_cipher_modes
604 GCRY_CIPHER_MODE_NONE = 0, /* Not yet specified. */
605 GCRY_CIPHER_MODE_ECB = 1, /* Electronic codebook. */
606 GCRY_CIPHER_MODE_CFB = 2, /* Cipher feedback. */
607 GCRY_CIPHER_MODE_CBC = 3, /* Cipher block chaining. */
608 GCRY_CIPHER_MODE_STREAM = 4, /* Used with stream ciphers. */
609 GCRY_CIPHER_MODE_OFB = 5, /* Outer feedback. */
610 GCRY_CIPHER_MODE_CTR = 6 /* Counter. */
613 /* Flags used with the open function. */
614 enum gcry_cipher_flags
616 GCRY_CIPHER_SECURE = 1, /* Allocate in secure memory. */
617 GCRY_CIPHER_ENABLE_SYNC = 2, /* Enable CFB sync mode. */
618 GCRY_CIPHER_CBC_CTS = 4, /* Enable CBC cipher text stealing (CTS). */
619 GCRY_CIPHER_CBC_MAC = 8 /* Enable CBC message auth. code (MAC). */
623 /* Create a handle for algorithm ALGO to be used in MODE. FLAGS may
624 be given as an bitwise OR of the gcry_cipher_flags values. */
625 gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *handle,
626 int algo, int mode, unsigned int flags);
628 /* Close the cioher handle H and release all resource. */
629 void gcry_cipher_close (gcry_cipher_hd_t h);
631 /* Perform various operations on the cipher object H. */
632 gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer,
635 /* Retrieve various information about the cipher object H. */
636 gcry_error_t gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer,
639 /* Retrieve various information about the cipher algorithm ALGO. */
640 gcry_error_t gcry_cipher_algo_info (int algo, int what, void *buffer,
643 /* Map the cipher algorithm id ALGO to a string representation of that
644 algorithm name. For unknown algorithms this functions returns an
646 const char *gcry_cipher_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
648 /* Map the algorithm name NAME to an cipher algorithm ID. Return 0 if
649 the algorithm name is not known. */
650 int gcry_cipher_map_name (const char *name) _GCRY_GCC_ATTR_PURE;
652 /* Given an ASN.1 object identifier in standard IETF dotted decimal
653 format in STRING, return the encryption mode associated with that
654 OID or 0 if not known or applicable. */
655 int gcry_cipher_mode_from_oid (const char *string) _GCRY_GCC_ATTR_PURE;
657 /* Encrypt the plaintext of size INLEN in IN using the cipher handle H
658 into the buffer OUT which has an allocated length of OUTSIZE. For
659 most algorithms it is possible to pass NULL for in and 0 for INLEN
660 and do a in-place decryption of the data provided in OUT. */
661 gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h,
662 unsigned char *out, size_t outsize,
663 const unsigned char *in, size_t inlen);
665 /* The counterpart to gcry_cipher_encrypt. */
666 gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t h,
667 unsigned char *out, size_t outsize,
668 const unsigned char *in, size_t inlen);
670 /* Set key K of length L for the cipher handle H. (We have to cast
671 away a const char* here - this catch-all ctl function was probably
672 not the best choice) */
673 #define gcry_cipher_setkey(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_KEY, \
676 /* Set initialization vector K of length L for the cipher handle H. */
677 #define gcry_cipher_setiv(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_IV, \
680 /* Reset the handle to the state after open. */
681 #define gcry_cipher_reset(h) gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0)
683 /* Perform the the OpenPGP sync operation if this is enabled for the
685 #define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, \
688 /* Enable or disable CTS in future calls to gcry_encrypt(). CBC mode only. */
689 #define gcry_cipher_cts(h,on) gcry_cipher_ctl( (h), GCRYCTL_SET_CBC_CTS, \
692 /* Set counter for CTR mode. (K,L) must denote a buffer of block size
693 length, or (NULL,0) to set the CTR to the all-zero block. */
694 #define gcry_cipher_setctr(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_CTR, \
697 /* Retrieved the key length used with algorithm A. */
698 size_t gcry_cipher_get_algo_keylen (int algo);
700 /* Retrieve the block length used with algorithm A. */
701 size_t gcry_cipher_get_algo_blklen (int algo);
703 /* Return 0 if the algorithm A is available for use. */
704 #define gcry_cipher_test_algo(a) \
705 gcry_cipher_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
707 /* Get a list consisting of the IDs of the loaded cipher modules. If
708 LIST is zero, write the number of loaded cipher modules to
709 LIST_LENGTH and return. If LIST is non-zero, the first
710 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
711 according size. In case there are less cipher modules than
712 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */
713 gcry_error_t gcry_cipher_list (int *list, int *list_length);
716 /************************************
718 * asymmetric cipher functions *
720 ************************************/
722 /* The algorithms and their IDs we support. */
726 GCRY_PK_RSA_E = 2, /* deprecated */
727 GCRY_PK_RSA_S = 3, /* deprecated */
728 GCRY_PK_ELG_E = 16, /* use only for OpenPGP */
733 /* Flags describing usage capabilities of a PK algorithm. */
734 #define GCRY_PK_USAGE_SIGN 1
735 #define GCRY_PK_USAGE_ENCR 2
737 /* Encrypt the DATA using the public key PKEY and store the result as
738 a newly created S-expression at RESULT. */
739 gcry_error_t gcry_pk_encrypt (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t pkey);
741 /* Decrypt the DATA using the private key SKEY and store the result as
742 a newly created S-expression at RESULT. */
743 gcry_error_t gcry_pk_decrypt (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t skey);
745 /* Sign the DATA using the private key SKEY and store the result as
746 a newly created S-expression at RESULT. */
747 gcry_error_t gcry_pk_sign (gcry_sexp_t *result, gcry_sexp_t data, gcry_sexp_t skey);
749 /* Check the signature SIGVAL on DATA using the public key PKEY. */
750 gcry_error_t gcry_pk_verify (gcry_sexp_t sigval, gcry_sexp_t data, gcry_sexp_t pkey);
752 /* Check that KEY (either private or public) is sane. */
753 gcry_error_t gcry_pk_testkey (gcry_sexp_t key);
755 /* Generate a new key pair according to the parameters given in
756 S_PARMS. The new key pair is returned in as an S-expression in
758 gcry_error_t gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms);
760 /* Catch all function for miscellaneous operations. */
761 gcry_error_t gcry_pk_ctl (int cmd, void *buffer, size_t buflen);
763 /* Retrieve information about the public key algorithm ALGO. */
764 gcry_error_t gcry_pk_algo_info (int algo, int what, void *buffer, size_t *nbytes);
766 /* Map the public key algorithm id ALGO to a string representation of the
767 algorithm name. For unknown algorithms this functions returns an
769 const char *gcry_pk_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
771 /* Map the algorithm NAME to a public key algorithm Id. Return 0 if
772 the algorithm name is not known. */
773 int gcry_pk_map_name (const char* name) _GCRY_GCC_ATTR_PURE;
775 /* Return what is commonly referred as the key length for the given
776 public or private KEY. */
777 unsigned int gcry_pk_get_nbits (gcry_sexp_t key) _GCRY_GCC_ATTR_PURE;
779 /* Please note that keygrip is still experimental and should not be
780 used without contacting the author. */
781 unsigned char *gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array);
783 /* Return 0 if the public key algorithm A is available for use. */
784 #define gcry_pk_test_algo(a) \
785 gcry_pk_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
787 /* Get a list consisting of the IDs of the loaded pubkey modules. If
788 LIST is zero, write the number of loaded pubkey modules to
789 LIST_LENGTH and return. If LIST is non-zero, the first
790 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
791 according size. In case there are less pubkey modules than
792 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */
793 gcry_error_t gcry_pk_list (int *list, int *list_length);
795 /* Alternative interface for asymetric cryptography. */
797 /* The algorithm IDs. */
798 typedef enum gcry_ac_id
807 typedef enum gcry_ac_key_type
814 /* Flags for data. */
815 #define GCRY_AC_FLAG_DATA_NO_BLINDING 1 << 0
817 /* This type represents a `data set'. */
818 typedef struct gcry_ac_data *gcry_ac_data_t;
820 /* This type represents a single `key', either a secret one or a
822 typedef struct gcry_ac_key *gcry_ac_key_t;
824 /* This type represents a `key pair' containing a secret and a public
826 typedef struct gcry_ac_key_pair *gcry_ac_key_pair_t;
828 /* This type represents a `handle' that is needed by functions
829 performing cryptographic operations. */
830 typedef struct gcry_ac_handle *gcry_ac_handle_t;
832 /* The caller of gcry_ac_key_pair_generate can provide one of these
833 structures in order to influence the key generation process in an
834 algorithm-specific way. */
835 typedef struct gcry_ac_key_spec_rsa
837 gcry_mpi_t e; /* E to use. */
838 } gcry_ac_key_spec_rsa_t;
840 /* Returns a new, empty data set in DATA. */
841 gcry_error_t gcry_ac_data_new (gcry_ac_data_t *data);
843 /* Destroy the data set DATA. */
844 void gcry_ac_data_destroy (gcry_ac_data_t data);
846 /* Add the value MPI to DATA with the label NAME. If there is already
847 a value with that label, replace it, otherwise add it. */
848 gcry_error_t gcry_ac_data_set (gcry_ac_data_t data,
852 /* Create a copy of the data set DATA and store it in DATA_CP. */
853 gcry_error_t gcry_ac_data_copy (gcry_ac_data_t *data_cp,
854 gcry_ac_data_t data);
856 /* Return the number of named MPI values inside of the data set
858 unsigned int gcry_ac_data_length (gcry_ac_data_t data);
860 /* Store the value labelled with NAME found in DATA in MPI or NULL if
861 a value with that label was not found. */
862 gcry_error_t gcry_ac_data_get_name (gcry_ac_data_t data, const char *name,
865 /* Return the MPI value with index INDEX contained in the data set
867 gcry_error_t gcry_ac_data_get_index (gcry_ac_data_t data, unsigned int index,
868 const char **name, gcry_mpi_t *mpi);
870 /* Destroy any values contained in the data set DATA. */
871 void gcry_ac_data_clear (gcry_ac_data_t data);
873 /* Create a new ac handle. */
874 gcry_error_t gcry_ac_open (gcry_ac_handle_t *handle,
875 gcry_ac_id_t algorithm,
878 /* Destroy an ac handle. */
879 void gcry_ac_close (gcry_ac_handle_t handle);
881 /* Initialize a key from a given data set. */
882 gcry_error_t gcry_ac_key_init (gcry_ac_key_t *key,
883 gcry_ac_handle_t handle,
884 gcry_ac_key_type_t type,
885 gcry_ac_data_t data);
887 /* Generate a new key pair. */
888 gcry_error_t gcry_ac_key_pair_generate (gcry_ac_handle_t handle,
889 gcry_ac_key_pair_t *key_pair,
893 /* Returns a specified key from a key pair. */
894 gcry_ac_key_t gcry_ac_key_pair_extract (gcry_ac_key_pair_t key_pair,
895 gcry_ac_key_type_t which);
897 /* Returns the data set contained in the key KEY. */
898 gcry_ac_data_t gcry_ac_key_data_get (gcry_ac_key_t key);
900 /* Verify that the key KEY is sane. */
901 gcry_error_t gcry_ac_key_test (gcry_ac_key_t key);
903 /* Return the number of bits of the key KEY in NBITS. */
904 gcry_error_t gcry_ac_key_get_nbits (gcry_ac_key_t key,
905 unsigned int *nbits);
907 /* Write the 20 byte long key grip of the key KEY to KEY_GRIP. */
908 gcry_error_t gcry_ac_key_get_grip (gcry_ac_key_t key,
909 unsigned char *key_grip);
912 void gcry_ac_key_destroy (gcry_ac_key_t key);
914 /* Destroy a key pair. */
915 void gcry_ac_key_pair_destroy (gcry_ac_key_pair_t key_pair);
917 /* Encrypt the plain text MPI value DATA_PLAIN with the key KEY under
918 the control of the flags FLAGS and store the resulting data set
919 into DATA_ENCRYPTED. */
920 gcry_error_t gcry_ac_data_encrypt (gcry_ac_handle_t handle,
923 gcry_mpi_t data_plain,
924 gcry_ac_data_t *data_encrypted);
926 /* Decrypt the decrypted data contained in the data set DATA_ENCRYPTED
927 with the key KEY under the control of the flags FLAGS and store the
928 resulting plain text MPI value in DATA_PLAIN. */
929 gcry_error_t gcry_ac_data_decrypt (gcry_ac_handle_t handle,
932 gcry_mpi_t *data_plain,
933 gcry_ac_data_t data_encrypted);
935 /* Sign the data contained in DATA with the key KEY and store the
936 resulting signature in the data set DATA_SIGNATURE. */
937 gcry_error_t gcry_ac_data_sign (gcry_ac_handle_t handle,
940 gcry_ac_data_t *data_signature);
942 /* Verify that the signature contained in the data set DATA_SIGNATURE
943 is indeed the result of signing the data contained in DATA with the
944 secret key belonging to the public key KEY. */
945 gcry_error_t gcry_ac_data_verify (gcry_ac_handle_t handle,
948 gcry_ac_data_t data_signature);
950 /* Store the textual representation of the algorithm whose id is given
951 in ALGORITHM in NAME. */
952 gcry_error_t gcry_ac_id_to_name (gcry_ac_id_t algorithm,
955 /* Store the numeric ID of the algorithm whose textual representation
956 is contained in NAME in ALGORITHM. */
957 gcry_error_t gcry_ac_name_to_id (const char *name,
958 gcry_ac_id_t *algorithm);
962 /************************************
964 * cryptograhic hash functions *
966 ************************************/
968 /* Algorithm IDs for the hash functions we know about. Not all of them
977 GCRY_MD_TIGER = 6, /* TIGER/192. */
978 GCRY_MD_HAVAL = 7, /* HAVAL, 5 pass, 160 bit. */
984 GCRY_MD_CRC32_RFC1510 = 303,
985 GCRY_MD_CRC24_RFC2440 = 304
988 /* Flags used with the open function. */
990 /* Allocate all buffers in "secure" memory. */
991 #define GCRY_MD_FLAG_SECURE 1 << 0
992 /* Make an HMAC out of this algorithm. */
993 #define GCRY_MD_FLAG_HMAC 1 << 1
995 /* Forward declaration. */
996 struct gcry_md_context;
998 /* This object is used to hold a handle to a message digest object.
999 This structure is private - only to be used by the public gcry_md_*
1001 typedef struct gcry_md_handle
1003 /* Actual context. */
1004 struct gcry_md_context *ctx;
1006 /* Buffer management. */
1009 unsigned char buf[1];
1012 /* Compatibility types, do not use them. */
1013 typedef struct gcry_md_handle *GCRY_MD_HD _GCRY_GCC_ATTR_DEPRECATED;
1014 typedef struct gcry_md_handle *GcryMDHd _GCRY_GCC_ATTR_DEPRECATED;
1016 /* Create a message digest object for algorithm ALGO. FLAGS may be
1017 given as an bitwise OR of the gcry_md_flags values. ALGO may be
1018 given as 0 if the algorithms to be used are later set using
1020 gcry_error_t gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags);
1022 /* Release the message digest object HD. */
1023 void gcry_md_close (gcry_md_hd_t hd);
1025 /* Add the message digest algorithm ALGO to the digest object HD. */
1026 gcry_error_t gcry_md_enable (gcry_md_hd_t hd, int algo);
1028 /* Create a new digest object as an exact copy of the object HD. */
1029 gcry_error_t gcry_md_copy (gcry_md_hd_t *bhd, gcry_md_hd_t ahd);
1031 /* Reset the digest object HD to its initial state. */
1032 void gcry_md_reset (gcry_md_hd_t hd);
1034 /* Perform various operations on the digets object HD. */
1035 gcry_error_t gcry_md_ctl (gcry_md_hd_t hd, int cmd, unsigned char *buffer,
1038 /* Pass LENGTH bytes of data in BUFFER to the digest object HD so that
1039 it can update the digest values. This is the actual hash
1041 void gcry_md_write (gcry_md_hd_t hd, const void *buffer, size_t length);
1043 /* Read out the final digest from HD return the digest value for
1045 unsigned char *gcry_md_read (gcry_md_hd_t hd, int algo);
1047 /* Convenience function to calculate the hash from the data in BUFFER
1048 of size LENGTH using the algorithm ALGO avoiding the creating of a
1049 hash object. The hash is returned in the caller provided buffer
1050 DIGEST which must be large enough to hold the digest of the given
1052 void gcry_md_hash_buffer (int algo, void *digest,
1053 const void *buffer, size_t length);
1055 /* Retrieve the algorithm used with HD. This does not work reliable
1056 if more than one algorithm is enabled in HD. */
1057 int gcry_md_get_algo (gcry_md_hd_t hd);
1059 /* Retrieve the length in bytes of the digest yielded by algorithm
1061 unsigned int gcry_md_get_algo_dlen (int algo);
1063 /* Return true if the the algorithm ALGO is enabled in the digest
1065 int gcry_md_is_enabled (gcry_md_hd_t a, int algo);
1067 /* Return true if the digest object A is allocated in "secure" memory. */
1068 int gcry_md_is_secure (gcry_md_hd_t a);
1070 /* Retrieve various information about the object H. */
1071 gcry_error_t gcry_md_info (gcry_md_hd_t h, int what, void *buffer,
1074 /* Retrieve various information about the algorithm ALGO. */
1075 gcry_error_t gcry_md_algo_info (int algo, int what, void *buffer,
1078 /* Map the digest algorithm id ALGO to a string representation of the
1079 algorithm name. For unknown algorithms this functions returns an
1081 const char *gcry_md_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
1083 /* Map the algorithm NAME to a digest algorithm Id. Return 0 if
1084 the algorithm name is not known. */
1085 int gcry_md_map_name (const char* name) _GCRY_GCC_ATTR_PURE;
1087 /* For use with the HMAC feature, the set MAC key to the KEY of
1089 gcry_error_t gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen);
1091 /* Update the hash(s) of H with the character C. This is a buffered
1092 version of the gcry_md_write function. */
1093 #define gcry_md_putc(h,c) \
1095 gcry_md_hd_t h__ = (h); \
1096 if( (h__)->bufpos == (h__)->bufsize ) \
1097 gcry_md_write( (h__), NULL, 0 ); \
1098 (h__)->buf[(h__)->bufpos++] = (c) & 0xff; \
1101 /* Finalize the digest calculation. This is not really needed because
1102 gcry_md_read() does this implicitly. */
1103 #define gcry_md_final(a) \
1104 gcry_md_ctl ((a), GCRYCTL_FINALIZE, NULL, 0)
1106 /* Return 0 if the algorithm A is available for use. */
1107 #define gcry_md_test_algo(a) \
1108 gcry_md_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
1110 /* Return an DER encoded ASN.1 OID for the algorithm A in buffer B. N
1111 must point to size_t variable with the available size of buffer B.
1112 After return it will receive the actual size of the returned
1114 #define gcry_md_get_asnoid(a,b,n) \
1115 gcry_md_algo_info((a), GCRYCTL_GET_ASNOID, (b), (n))
1117 /* Enable debugging for digets object A; i.e. create files named
1118 dbgmd-<n>.<string> while hashing. B is a string used as the suffix
1119 for the filename. */
1120 #define gcry_md_start_debug(a,b) \
1121 gcry_md_ctl( (a), GCRYCTL_START_DUMP, (b), 0 )
1123 /* Disable the debugging of A. */
1124 #define gcry_md_stop_debug(a,b) \
1125 gcry_md_ctl( (a), GCRYCTL_STOP_DUMP, (b), 0 )
1127 /* Get a list consisting of the IDs of the loaded message digest
1128 modules. If LIST is zero, write the number of loaded message
1129 digest modules to LIST_LENGTH and return. If LIST is non-zero, the
1130 first *LIST_LENGTH algorithm IDs are stored in LIST, which must be
1131 of according size. In case there are less message digest modules
1132 than *LIST_LENGTH, *LIST_LENGTH is updated to the correct
1134 gcry_error_t gcry_md_list (int *list, int *list_length);
1137 /************************************
1139 * random generating functions *
1141 ************************************/
1143 /* The possible values for the random quality. The rule of thumb is
1144 to use WEAK for random number which don't need to be
1145 cryptographically strong, STRONG for session keys and VERY_STRONG
1146 for key material. */
1147 enum gcry_random_level
1149 GCRY_WEAK_RANDOM = 0,
1150 GCRY_STRONG_RANDOM = 1,
1151 GCRY_VERY_STRONG_RANDOM = 2
1155 /* Fill BUFFER with LENGTH bytes of random, using random numbers of
1157 void gcry_randomize (unsigned char *buffer, size_t length,
1158 enum gcry_random_level level);
1160 /* Add the external random from BUFFER with LENGTH bytes into the
1161 pool. QUALITY should either be -1 for unknown or in the range of 0
1163 gcry_error_t gcry_random_add_bytes (const void *buffer, size_t length,
1166 /* Return NBYTES of allocated random using a random numbers of quality
1168 void *gcry_random_bytes (size_t nbytes, enum gcry_random_level level)
1169 _GCRY_GCC_ATTR_MALLOC;
1171 /* Return NBYTES of allocated random using a random numbers of quality
1172 LEVEL. The random numbers are created returned in "secure"
1174 void *gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level)
1175 _GCRY_GCC_ATTR_MALLOC;
1178 /* Set the big inetger W to a random value of NBITS using a random
1179 generator with quality LEVEL. */
1180 void gcry_mpi_randomize (gcry_mpi_t w,
1181 unsigned int nbits, enum gcry_random_level level);
1185 /************************************
1187 * miscellaneous stuff *
1189 ************************************/
1191 /* Log levels used by the internal logging facility. */
1192 enum gcry_log_levels
1194 GCRY_LOG_CONT = 0, /* continue the last log line */
1197 GCRY_LOG_ERROR = 30,
1198 GCRY_LOG_FATAL = 40,
1200 GCRY_LOG_DEBUG = 100
1203 /* Type for progress handlers. */
1204 typedef void (*gcry_handler_progress_t) (void *, const char *, int, int, int);
1206 /* Type for memory allocation handlers. */
1207 typedef void *(*gcry_handler_alloc_t) (size_t n);
1209 /* Type for secure memory check handlers. */
1210 typedef int (*gcry_handler_secure_check_t) (const void *);
1212 /* Type for memory reallocation handlers. */
1213 typedef void *(*gcry_handler_realloc_t) (void *p, size_t n);
1215 /* Type for memory free handlers. */
1216 typedef void (*gcry_handler_free_t) (void *);
1218 /* Type for out-of-memory handlers. */
1219 typedef int (*gcry_handler_no_mem_t) (void *, size_t, unsigned int);
1221 /* Type for fatal error handlers. */
1222 typedef void (*gcry_handler_error_t) (void *, int, const char *);
1224 /* Type for logging handlers. */
1225 typedef void (*gcry_handler_log_t) (void *, int, const char *, va_list);
1227 /* Certain operations can provide progress information. This function
1228 is used to register a handler for retrieving these information. */
1229 void gcry_set_progress_handler (gcry_handler_progress_t cb, void *cb_data);
1232 /* Register a custom memory allocation functions. */
1233 void gcry_set_allocation_handler (gcry_handler_alloc_t func_alloc,
1234 gcry_handler_alloc_t func_alloc_secure,
1235 gcry_handler_secure_check_t func_secure_check,
1236 gcry_handler_realloc_t func_realloc,
1237 gcry_handler_free_t func_free);
1239 /* Register a function used instead of the internal out of memory
1241 void gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque);
1243 /* Register a function used instead of the internal fatal error
1245 void gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque);
1247 /* Register a function used instead of the internal logging
1249 void gcry_set_log_handler (gcry_handler_log_t f, void *opaque);
1251 /* Reserved for future use. */
1252 void gcry_set_gettext_handler (const char *(*f)(const char*));
1254 /* Libgcrypt uses its own memory allocation. It is important to use
1255 gcry_free () to release memory allocated by libgcrypt. */
1256 void *gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
1257 void *gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1258 void *gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
1259 void *gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1260 void *gcry_realloc (void *a, size_t n);
1261 char *gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC;
1262 void *gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
1263 void *gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1264 void *gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
1265 void *gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1266 void *gcry_xrealloc (void *a, size_t n);
1267 char *gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC;
1268 void gcry_free (void *a);
1270 /* Return true if A is allocated in "secure" memory. */
1271 int gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE;
1273 /* Include support for Libgcrypt modules. */
1274 #include <gcrypt-module.h>
1276 #if 0 /* keep Emacsens's auto-indent happy */
1282 #endif /* _GCRYPT_H */