source: grub-pc/trunk/fuentes/include/grub/gcrypt/gcrypt.h @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 64.8 KB
Line 
1/* gcrypt.h -  GNU Cryptographic Library Interface              -*- c -*-
2   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
3                 2007, 2008, 2009, 2010, 2011,
4                 2012  Free Software Foundation, Inc.
5   Copyright (C) 2012, 2013  g10 Code GmbH
6
7   This file is part of Libgcrypt.
8
9   Libgcrypt is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as
11   published by the Free Software Foundation; either version 2.1 of
12   the License, or (at your option) any later version.
13
14   Libgcrypt is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public
20   License along with this program; if not, see <http://www.gnu.org/licenses/>.
21
22   File: @configure_input@ */
23
24#ifndef _GCRYPT_H
25#define _GCRYPT_H
26
27#include <stdarg.h>
28
29#include <grub/gcrypt/gpg-error.h>
30
31#include <sys/types.h>
32
33#if defined _WIN32 || defined __WIN32__
34# ifndef __GNUC__
35# endif /*!__GNUC__*/
36#else
37#endif /*!_WIN32*/
38
39
40/* This is required for error code compatibility. */
41#define _GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_GCRYPT
42
43#ifdef __cplusplus
44extern "C" {
45#if 0 /* (Keep Emacsens' auto-indent happy.) */
46}
47#endif
48#endif
49
50/* The version of this header should match the one of the library. It
51   should not be used by a program because gcry_check_version() should
52   return the same version.  The purpose of this macro is to let
53   autoconf (using the AM_PATH_GCRYPT macro) check that this header
54   matches the installed library.  */
55#define GCRYPT_VERSION "@VERSION@"
56
57/* The version number of this header.  It may be used to handle minor
58   API incompatibilities.  */
59#define GCRYPT_VERSION_NUMBER @VERSION_NUMBER@
60
61
62/* Internal: We can't use the convenience macros for the multi
63   precision integer functions when building this library. */
64#ifdef _GCRYPT_IN_LIBGCRYPT
65#ifndef GCRYPT_NO_MPI_MACROS
66#define GCRYPT_NO_MPI_MACROS 1
67#endif
68#endif
69
70/* We want to use gcc attributes when possible.  Warning: Don't use
71   these macros in your programs: As indicated by the leading
72   underscore they are subject to change without notice. */
73#ifdef __GNUC__
74
75#define _GCRY_GCC_VERSION (__GNUC__ * 10000 \
76                             + __GNUC_MINOR__ * 100 \
77                             + __GNUC_PATCHLEVEL__)
78
79#if _GCRY_GCC_VERSION >= 30100
80#define _GCRY_GCC_ATTR_DEPRECATED __attribute__ ((__deprecated__))
81#endif
82
83#if _GCRY_GCC_VERSION >= 29600
84#define _GCRY_GCC_ATTR_PURE  __attribute__ ((__pure__))
85#endif
86
87#if _GCRY_GCC_VERSION >= 30200
88#define _GCRY_GCC_ATTR_MALLOC  __attribute__ ((__malloc__))
89#endif
90
91#endif /*__GNUC__*/
92
93#ifndef _GCRY_GCC_ATTR_DEPRECATED
94#define _GCRY_GCC_ATTR_DEPRECATED
95#endif
96#ifndef _GCRY_GCC_ATTR_PURE
97#define _GCRY_GCC_ATTR_PURE
98#endif
99#ifndef _GCRY_GCC_ATTR_MALLOC
100#define _GCRY_GCC_ATTR_MALLOC
101#endif
102
103/* Make up an attribute to mark functions and types as deprecated but
104   allow internal use by Libgcrypt.  */
105#ifdef _GCRYPT_IN_LIBGCRYPT
106#define _GCRY_ATTR_INTERNAL
107#else
108#define _GCRY_ATTR_INTERNAL     _GCRY_GCC_ATTR_DEPRECATED
109#endif
110
111/* Wrappers for the libgpg-error library.  */
112
113typedef gpg_err_source_t gcry_err_source_t;
114
115static GPG_ERR_INLINE gcry_error_t
116gcry_err_make (gcry_err_source_t source, gcry_err_code_t code)
117{
118  return gpg_err_make (source, code);
119}
120
121/* The user can define GPG_ERR_SOURCE_DEFAULT before including this
122   file to specify a default source for gpg_error.  */
123#ifndef GCRY_ERR_SOURCE_DEFAULT
124#define GCRY_ERR_SOURCE_DEFAULT  GPG_ERR_SOURCE_USER_1
125#endif
126
127static GPG_ERR_INLINE gcry_error_t
128gcry_error (gcry_err_code_t code)
129{
130  return gcry_err_make (GCRY_ERR_SOURCE_DEFAULT, code);
131}
132
133static GPG_ERR_INLINE gcry_err_code_t
134gcry_err_code (gcry_error_t err)
135{
136  return gpg_err_code (err);
137}
138
139
140static GPG_ERR_INLINE gcry_err_source_t
141gcry_err_source (gcry_error_t err)
142{
143  return gpg_err_source (err);
144}
145
146/* Return a pointer to a string containing a description of the error
147   code in the error value ERR.  */
148const char *gcry_strerror (gcry_error_t err);
149
150/* Return a pointer to a string containing a description of the error
151   source in the error value ERR.  */
152const char *gcry_strsource (gcry_error_t err);
153
154/* Retrieve the error code for the system error ERR.  This returns
155   GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report
156   this).  */
157gcry_err_code_t gcry_err_code_from_errno (int err);
158
159/* Retrieve the system error for the error code CODE.  This returns 0
160   if CODE is not a system error code.  */
161int gcry_err_code_to_errno (gcry_err_code_t code);
162
163/* Return an error value with the error source SOURCE and the system
164   error ERR.  */
165gcry_error_t gcry_err_make_from_errno (gcry_err_source_t source, int err);
166
167/* Return an error value with the system error ERR.  */
168gcry_err_code_t gcry_error_from_errno (int err);
169
170
171/* The data object used to hold a multi precision integer.  */
172struct gcry_mpi;
173
174#ifndef GCRYPT_NO_DEPRECATED
175typedef struct gcry_mpi *GCRY_MPI _GCRY_GCC_ATTR_DEPRECATED;
176typedef struct gcry_mpi *GcryMPI _GCRY_GCC_ATTR_DEPRECATED;
177#endif
178
179
180
181/* Check that the library fulfills the version requirement.  */
182const char *gcry_check_version (const char *req_version);
183
184/* Codes for function dispatchers.  */
185
186/* Codes used with the gcry_control function. */
187enum gcry_ctl_cmds
188  {
189    GCRYCTL_SET_KEY  = 1,
190    GCRYCTL_SET_IV   = 2,
191    GCRYCTL_CFB_SYNC = 3,
192    GCRYCTL_RESET    = 4,   /* e.g. for MDs */
193    GCRYCTL_FINALIZE = 5,
194    GCRYCTL_GET_KEYLEN = 6,
195    GCRYCTL_GET_BLKLEN = 7,
196    GCRYCTL_TEST_ALGO = 8,
197    GCRYCTL_IS_SECURE = 9,
198    GCRYCTL_GET_ASNOID = 10,
199    GCRYCTL_ENABLE_ALGO = 11,
200    GCRYCTL_DISABLE_ALGO = 12,
201    GCRYCTL_DUMP_RANDOM_STATS = 13,
202    GCRYCTL_DUMP_SECMEM_STATS = 14,
203    GCRYCTL_GET_ALGO_NPKEY    = 15,
204    GCRYCTL_GET_ALGO_NSKEY    = 16,
205    GCRYCTL_GET_ALGO_NSIGN    = 17,
206    GCRYCTL_GET_ALGO_NENCR    = 18,
207    GCRYCTL_SET_VERBOSITY     = 19,
208    GCRYCTL_SET_DEBUG_FLAGS   = 20,
209    GCRYCTL_CLEAR_DEBUG_FLAGS = 21,
210    GCRYCTL_USE_SECURE_RNDPOOL= 22,
211    GCRYCTL_DUMP_MEMORY_STATS = 23,
212    GCRYCTL_INIT_SECMEM       = 24,
213    GCRYCTL_TERM_SECMEM       = 25,
214    GCRYCTL_DISABLE_SECMEM_WARN = 27,
215    GCRYCTL_SUSPEND_SECMEM_WARN = 28,
216    GCRYCTL_RESUME_SECMEM_WARN  = 29,
217    GCRYCTL_DROP_PRIVS          = 30,
218    GCRYCTL_ENABLE_M_GUARD      = 31,
219    GCRYCTL_START_DUMP          = 32,
220    GCRYCTL_STOP_DUMP           = 33,
221    GCRYCTL_GET_ALGO_USAGE      = 34,
222    GCRYCTL_IS_ALGO_ENABLED     = 35,
223    GCRYCTL_DISABLE_INTERNAL_LOCKING = 36,
224    GCRYCTL_DISABLE_SECMEM      = 37,
225    GCRYCTL_INITIALIZATION_FINISHED = 38,
226    GCRYCTL_INITIALIZATION_FINISHED_P = 39,
227    GCRYCTL_ANY_INITIALIZATION_P = 40,
228    GCRYCTL_SET_CBC_CTS = 41,
229    GCRYCTL_SET_CBC_MAC = 42,
230    GCRYCTL_SET_CTR = 43,
231    GCRYCTL_ENABLE_QUICK_RANDOM = 44,
232    GCRYCTL_SET_RANDOM_SEED_FILE = 45,
233    GCRYCTL_UPDATE_RANDOM_SEED_FILE = 46,
234    GCRYCTL_SET_THREAD_CBS = 47,
235    GCRYCTL_FAST_POLL = 48,
236    GCRYCTL_SET_RANDOM_DAEMON_SOCKET = 49,
237    GCRYCTL_USE_RANDOM_DAEMON = 50,
238    GCRYCTL_FAKED_RANDOM_P = 51,
239    GCRYCTL_SET_RNDEGD_SOCKET = 52,
240    GCRYCTL_PRINT_CONFIG = 53,
241    GCRYCTL_OPERATIONAL_P = 54,
242    GCRYCTL_FIPS_MODE_P = 55,
243    GCRYCTL_FORCE_FIPS_MODE = 56,
244    GCRYCTL_SELFTEST = 57,
245    /* Note: 58 .. 62 are used internally.  */
246    GCRYCTL_DISABLE_HWF = 63,
247    GCRYCTL_SET_ENFORCED_FIPS_FLAG = 64
248  };
249
250/* Perform various operations defined by CMD. */
251gcry_error_t gcry_control (enum gcry_ctl_cmds CMD, ...);
252
253
254/* S-expression management. */
255
256/* The object to represent an S-expression as used with the public key
257   functions.  */
258struct gcry_sexp;
259typedef struct gcry_sexp *gcry_sexp_t;
260
261#ifndef GCRYPT_NO_DEPRECATED
262typedef struct gcry_sexp *GCRY_SEXP _GCRY_GCC_ATTR_DEPRECATED;
263typedef struct gcry_sexp *GcrySexp _GCRY_GCC_ATTR_DEPRECATED;
264#endif
265
266/* The possible values for the S-expression format. */
267enum gcry_sexp_format
268  {
269    GCRYSEXP_FMT_DEFAULT   = 0,
270    GCRYSEXP_FMT_CANON     = 1,
271    GCRYSEXP_FMT_BASE64    = 2,
272    GCRYSEXP_FMT_ADVANCED  = 3
273  };
274
275/* Create an new S-expression object from BUFFER of size LENGTH and
276   return it in RETSEXP.  With AUTODETECT set to 0 the data in BUFFER
277   is expected to be in canonized format.  */
278gcry_error_t gcry_sexp_new (gcry_sexp_t *retsexp,
279                            const void *buffer, size_t length,
280                            int autodetect);
281
282 /* Same as gcry_sexp_new but allows to pass a FREEFNC which has the
283    effect to transfer ownership of BUFFER to the created object.  */
284gcry_error_t gcry_sexp_create (gcry_sexp_t *retsexp,
285                               void *buffer, size_t length,
286                               int autodetect, void (*freefnc) (void *));
287
288/* Scan BUFFER and return a new S-expression object in RETSEXP.  This
289   function expects a printf like string in BUFFER.  */
290gcry_error_t gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff,
291                              const char *buffer, size_t length);
292
293/* Same as gcry_sexp_sscan but expects a string in FORMAT and can thus
294   only be used for certain encodings.  */
295gcry_error_t gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff,
296                              const char *format, ...);
297
298/* Like gcry_sexp_build, but uses an array instead of variable
299   function arguments.  */
300gcry_error_t gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff,
301                                    const char *format, void **arg_list);
302
303/* Release the S-expression object SEXP */
304void gcry_sexp_release (gcry_sexp_t sexp);
305
306/* Calculate the length of an canonized S-expresion in BUFFER and
307   check for a valid encoding. */
308size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length,
309                            size_t *erroff, gcry_error_t *errcode);
310
311/* Copies the S-expression object SEXP into BUFFER using the format
312   specified in MODE.  */
313size_t gcry_sexp_sprint (gcry_sexp_t sexp, int mode, void *buffer,
314                         size_t maxlength);
315
316/* Dumps the S-expression object A in a format suitable for debugging
317   to Libgcrypt's logging stream.  */
318void gcry_sexp_dump (const gcry_sexp_t a);
319
320gcry_sexp_t gcry_sexp_cons (const gcry_sexp_t a, const gcry_sexp_t b);
321gcry_sexp_t gcry_sexp_alist (const gcry_sexp_t *array);
322gcry_sexp_t gcry_sexp_vlist (const gcry_sexp_t a, ...);
323gcry_sexp_t gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n);
324gcry_sexp_t gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n);
325
326/* Scan the S-expression for a sublist with a type (the car of the
327   list) matching the string TOKEN.  If TOKLEN is not 0, the token is
328   assumed to be raw memory of this length.  The function returns a
329   newly allocated S-expression consisting of the found sublist or
330   `NULL' when not found.  */
331gcry_sexp_t gcry_sexp_find_token (gcry_sexp_t list,
332                                const char *tok, size_t toklen);
333/* Return the length of the LIST.  For a valid S-expression this
334   should be at least 1.  */
335int gcry_sexp_length (const gcry_sexp_t list);
336
337/* Create and return a new S-expression from the element with index
338   NUMBER in LIST.  Note that the first element has the index 0.  If
339   there is no such element, `NULL' is returned.  */
340gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t list, int number);
341
342/* Create and return a new S-expression from the first element in
343   LIST; this called the "type" and should always exist and be a
344   string. `NULL' is returned in case of a problem.  */
345gcry_sexp_t gcry_sexp_car (const gcry_sexp_t list);
346
347/* Create and return a new list form all elements except for the first
348   one.  Note, that this function may return an invalid S-expression
349   because it is not guaranteed, that the type exists and is a string.
350   However, for parsing a complex S-expression it might be useful for
351   intermediate lists.  Returns `NULL' on error.  */
352gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t list);
353
354gcry_sexp_t gcry_sexp_cadr (const gcry_sexp_t list);
355
356
357/* This function is used to get data from a LIST.  A pointer to the
358   actual data with index NUMBER is returned and the length of this
359   data will be stored to DATALEN.  If there is no data at the given
360   index or the index represents another list, `NULL' is returned.
361   *Note:* The returned pointer is valid as long as LIST is not
362   modified or released.  */
363const char *gcry_sexp_nth_data (const gcry_sexp_t list, int number,
364                                size_t *datalen);
365
366/* This function is used to get and convert data from a LIST.  The
367   data is assumed to be a Nul terminated string.  The caller must
368   release the returned value using `gcry_free'.  If there is no data
369   at the given index, the index represents a list or the value can't
370   be converted to a string, `NULL' is returned.  */
371char *gcry_sexp_nth_string (gcry_sexp_t list, int number);
372
373/* This function is used to get and convert data from a LIST. This
374   data is assumed to be an MPI stored in the format described by
375   MPIFMT and returned as a standard Libgcrypt MPI.  The caller must
376   release this returned value using `gcry_mpi_release'.  If there is
377   no data at the given index, the index represents a list or the
378   value can't be converted to an MPI, `NULL' is returned.  */
379gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt);
380
381
382
383/*******************************************
384 *                                         *
385 *  Multi Precision Integer Functions      *
386 *                                         *
387 *******************************************/
388
389/* Different formats of external big integer representation. */
390enum gcry_mpi_format
391  {
392    GCRYMPI_FMT_NONE= 0,
393    GCRYMPI_FMT_STD = 1,    /* Twos complement stored without length.  */
394    GCRYMPI_FMT_PGP = 2,    /* As used by OpenPGP (unsigned only).  */
395    GCRYMPI_FMT_SSH = 3,    /* As used by SSH (like STD but with length).  */
396    GCRYMPI_FMT_HEX = 4,    /* Hex format. */
397    GCRYMPI_FMT_USG = 5     /* Like STD but unsigned. */
398  };
399
400/* Flags used for creating big integers.  */
401enum gcry_mpi_flag
402  {
403    GCRYMPI_FLAG_SECURE = 1,  /* Allocate the number in "secure" memory.  */
404    GCRYMPI_FLAG_OPAQUE = 2   /* The number is not a real one but just
405                                 a way to store some bytes.  This is
406                                 useful for encrypted big integers.  */
407  };
408
409
410/* Allocate a new big integer object, initialize it with 0 and
411   initially allocate memory for a number of at least NBITS. */
412gcry_mpi_t gcry_mpi_new (unsigned int nbits);
413
414/* Same as gcry_mpi_new() but allocate in "secure" memory. */
415gcry_mpi_t gcry_mpi_snew (unsigned int nbits);
416
417/* Release the number A and free all associated resources. */
418void gcry_mpi_release (gcry_mpi_t a);
419
420/* Create a new number with the same value as A. */
421gcry_mpi_t gcry_mpi_copy (const gcry_mpi_t a);
422
423/* Store the big integer value U in W. */
424gcry_mpi_t gcry_mpi_set (gcry_mpi_t w, const gcry_mpi_t u);
425
426/* Store the unsigned integer value U in W. */
427gcry_mpi_t gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u);
428
429/* Swap the values of A and B. */
430void gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b);
431
432/* Compare the big integer number U and V returning 0 for equality, a
433   positive value for U > V and a negative for U < V. */
434int gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v);
435
436/* Compare the big integer number U with the unsigned integer V
437   returning 0 for equality, a positive value for U > V and a negative
438   for U < V. */
439int gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v);
440
441/* Convert the external representation of an integer stored in BUFFER
442   with a length of BUFLEN into a newly create MPI returned in
443   RET_MPI.  If NSCANNED is not NULL, it will receive the number of
444   bytes actually scanned after a successful operation. */
445gcry_error_t gcry_mpi_scan (gcry_mpi_t *ret_mpi, enum gcry_mpi_format format,
446                            const void *buffer, size_t buflen,
447                            size_t *nscanned);
448
449/* Convert the big integer A into the external representation
450   described by FORMAT and store it in the provided BUFFER which has
451   been allocated by the user with a size of BUFLEN bytes.  NWRITTEN
452   receives the actual length of the external representation unless it
453   has been passed as NULL. */
454gcry_error_t gcry_mpi_print (enum gcry_mpi_format format,
455                             unsigned char *buffer, size_t buflen,
456                             size_t *nwritten,
457                             const gcry_mpi_t a);
458
459/* Convert the big integer A int the external representation described
460   by FORMAT and store it in a newly allocated buffer which address
461   will be put into BUFFER.  NWRITTEN receives the actual lengths of the
462   external representation. */
463gcry_error_t gcry_mpi_aprint (enum gcry_mpi_format format,
464                              unsigned char **buffer, size_t *nwritten,
465                              const gcry_mpi_t a);
466
467/* Dump the value of A in a format suitable for debugging to
468   Libgcrypt's logging stream.  Note that one leading space but no
469   trailing space or linefeed will be printed.  It is okay to pass
470   NULL for A. */
471void gcry_mpi_dump (const gcry_mpi_t a);
472
473
474/* W = U + V.  */
475void gcry_mpi_add (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
476
477/* W = U + V.  V is an unsigned integer. */
478void gcry_mpi_add_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v);
479
480/* W = U + V mod M. */
481void gcry_mpi_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
482
483/* W = U - V. */
484void gcry_mpi_sub (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
485
486/* W = U - V.  V is an unsigned integer. */
487void gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v );
488
489/* W = U - V mod M */
490void gcry_mpi_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
491
492/* W = U * V. */
493void gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v);
494
495/* W = U * V.  V is an unsigned integer. */
496void gcry_mpi_mul_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v );
497
498/* W = U * V mod M. */
499void gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m);
500
501/* W = U * (2 ^ CNT). */
502void gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt);
503
504/* Q = DIVIDEND / DIVISOR, R = DIVIDEND % DIVISOR,
505   Q or R may be passed as NULL.  ROUND should be negative or 0. */
506void gcry_mpi_div (gcry_mpi_t q, gcry_mpi_t r,
507                   gcry_mpi_t dividend, gcry_mpi_t divisor, int round);
508
509/* R = DIVIDEND % DIVISOR */
510void gcry_mpi_mod (gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor);
511
512/* W = B ^ E mod M. */
513void gcry_mpi_powm (gcry_mpi_t w,
514                    const gcry_mpi_t b, const gcry_mpi_t e,
515                    const gcry_mpi_t m);
516
517/* Set G to the greatest common divisor of A and B.
518   Return true if the G is 1. */
519int gcry_mpi_gcd (gcry_mpi_t g, gcry_mpi_t a, gcry_mpi_t b);
520
521/* Set X to the multiplicative inverse of A mod M.
522   Return true if the value exists. */
523int gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t m);
524
525
526/* Return the number of bits required to represent A. */
527unsigned int gcry_mpi_get_nbits (gcry_mpi_t a);
528
529/* Return true when bit number N (counting from 0) is set in A. */
530int      gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n);
531
532/* Set bit number N in A. */
533void     gcry_mpi_set_bit (gcry_mpi_t a, unsigned int n);
534
535/* Clear bit number N in A. */
536void     gcry_mpi_clear_bit (gcry_mpi_t a, unsigned int n);
537
538/* Set bit number N in A and clear all bits greater than N. */
539void     gcry_mpi_set_highbit (gcry_mpi_t a, unsigned int n);
540
541/* Clear bit number N in A and all bits greater than N. */
542void     gcry_mpi_clear_highbit (gcry_mpi_t a, unsigned int n);
543
544/* Shift the value of A by N bits to the right and store the result in X. */
545void     gcry_mpi_rshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n);
546
547/* Shift the value of A by N bits to the left and store the result in X. */
548void     gcry_mpi_lshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n);
549
550/* Store NBITS of the value P points to in A and mark A as an opaque
551   value.  WARNING: Never use an opaque MPI for anything thing else then
552   gcry_mpi_release, gcry_mpi_get_opaque. */
553gcry_mpi_t gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits);
554
555/* Return a pointer to an opaque value stored in A and return its size
556   in NBITS.  Note that the returned pointer is still owned by A and
557   that the function should never be used for an non-opaque MPI. */
558void *gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits);
559
560/* Set the FLAG for the big integer A.  Currently only the flag
561   GCRYMPI_FLAG_SECURE is allowed to convert A into an big intger
562   stored in "secure" memory. */
563void gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
564
565/* Clear FLAG for the big integer A.  Note that this function is
566   currently useless as no flags are allowed. */
567void gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
568
569/* Return true when the FLAG is set for A. */
570int gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag);
571
572/* Unless the GCRYPT_NO_MPI_MACROS is used, provide a couple of
573   convenience macros for the big integer functions. */
574#ifndef GCRYPT_NO_MPI_MACROS
575#define mpi_new(n)          gcry_mpi_new( (n) )
576#define mpi_secure_new( n ) gcry_mpi_snew( (n) )
577#define mpi_release(a)      \
578  do \
579    { \
580      gcry_mpi_release ((a)); \
581      (a) = NULL; \
582    } \
583  while (0)
584
585#define mpi_copy( a )          gcry_mpi_copy( (a) )
586#define mpi_set( w, u)         gcry_mpi_set( (w), (u) )
587#define mpi_set_ui( w, u)      gcry_mpi_set_ui( (w), (u) )
588#define mpi_cmp( u, v )        gcry_mpi_cmp( (u), (v) )
589#define mpi_cmp_ui( u, v )     gcry_mpi_cmp_ui( (u), (v) )
590
591#define mpi_add_ui(w,u,v)      gcry_mpi_add_ui((w),(u),(v))
592#define mpi_add(w,u,v)         gcry_mpi_add ((w),(u),(v))
593#define mpi_addm(w,u,v,m)      gcry_mpi_addm ((w),(u),(v),(m))
594#define mpi_sub_ui(w,u,v)      gcry_mpi_sub_ui ((w),(u),(v))
595#define mpi_sub(w,u,v)         gcry_mpi_sub ((w),(u),(v))
596#define mpi_subm(w,u,v,m)      gcry_mpi_subm ((w),(u),(v),(m))
597#define mpi_mul_ui(w,u,v)      gcry_mpi_mul_ui ((w),(u),(v))
598#define mpi_mul_2exp(w,u,v)    gcry_mpi_mul_2exp ((w),(u),(v))
599#define mpi_mul(w,u,v)         gcry_mpi_mul ((w),(u),(v))
600#define mpi_mulm(w,u,v,m)      gcry_mpi_mulm ((w),(u),(v),(m))
601#define mpi_powm(w,b,e,m)      gcry_mpi_powm ( (w), (b), (e), (m) )
602#define mpi_tdiv(q,r,a,m)      gcry_mpi_div ( (q), (r), (a), (m), 0)
603#define mpi_fdiv(q,r,a,m)      gcry_mpi_div ( (q), (r), (a), (m), -1)
604#define mpi_mod(r,a,m)         gcry_mpi_mod ((r), (a), (m))
605#define mpi_gcd(g,a,b)         gcry_mpi_gcd ( (g), (a), (b) )
606#define mpi_invm(g,a,b)        gcry_mpi_invm ( (g), (a), (b) )
607
608#define mpi_get_nbits(a)       gcry_mpi_get_nbits ((a))
609#define mpi_test_bit(a,b)      gcry_mpi_test_bit ((a),(b))
610#define mpi_set_bit(a,b)       gcry_mpi_set_bit ((a),(b))
611#define mpi_set_highbit(a,b)   gcry_mpi_set_highbit ((a),(b))
612#define mpi_clear_bit(a,b)     gcry_mpi_clear_bit ((a),(b))
613#define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b))
614#define mpi_rshift(a,b,c)      gcry_mpi_rshift ((a),(b),(c))
615#define mpi_lshift(a,b,c)      gcry_mpi_lshift ((a),(b),(c))
616
617#define mpi_set_opaque(a,b,c)  gcry_mpi_set_opaque( (a), (b), (c) )
618#define mpi_get_opaque(a,b)    gcry_mpi_get_opaque( (a), (b) )
619#endif /* GCRYPT_NO_MPI_MACROS */
620
621
622
623/************************************
624 *                                  *
625 *   Symmetric Cipher Functions     *
626 *                                  *
627 ************************************/
628
629/* The data object used to hold a handle to an encryption object.  */
630struct gcry_cipher_handle;
631typedef struct gcry_cipher_handle *gcry_cipher_hd_t;
632
633#ifndef GCRYPT_NO_DEPRECATED
634typedef struct gcry_cipher_handle *GCRY_CIPHER_HD _GCRY_GCC_ATTR_DEPRECATED;
635typedef struct gcry_cipher_handle *GcryCipherHd _GCRY_GCC_ATTR_DEPRECATED;
636#endif
637
638/* All symmetric encryption algorithms are identified by their IDs.
639   More IDs may be registered at runtime. */
640enum gcry_cipher_algos
641  {
642    GCRY_CIPHER_NONE        = 0,
643    GCRY_CIPHER_IDEA        = 1,
644    GCRY_CIPHER_3DES        = 2,
645    GCRY_CIPHER_CAST5       = 3,
646    GCRY_CIPHER_BLOWFISH    = 4,
647    GCRY_CIPHER_SAFER_SK128 = 5,
648    GCRY_CIPHER_DES_SK      = 6,
649    GCRY_CIPHER_AES         = 7,
650    GCRY_CIPHER_AES192      = 8,
651    GCRY_CIPHER_AES256      = 9,
652    GCRY_CIPHER_TWOFISH     = 10,
653
654    /* Other cipher numbers are above 300 for OpenPGP reasons. */
655    GCRY_CIPHER_ARCFOUR     = 301,  /* Fully compatible with RSA's RC4 (tm). */
656    GCRY_CIPHER_DES         = 302,  /* Yes, this is single key 56 bit DES. */
657    GCRY_CIPHER_TWOFISH128  = 303,
658    GCRY_CIPHER_SERPENT128  = 304,
659    GCRY_CIPHER_SERPENT192  = 305,
660    GCRY_CIPHER_SERPENT256  = 306,
661    GCRY_CIPHER_RFC2268_40  = 307,  /* Ron's Cipher 2 (40 bit). */
662    GCRY_CIPHER_RFC2268_128 = 308,  /* Ron's Cipher 2 (128 bit). */
663    GCRY_CIPHER_SEED        = 309,  /* 128 bit cipher described in RFC4269. */
664    GCRY_CIPHER_CAMELLIA128 = 310,
665    GCRY_CIPHER_CAMELLIA192 = 311,
666    GCRY_CIPHER_CAMELLIA256 = 312
667  };
668
669/* The Rijndael algorithm is basically AES, so provide some macros. */
670#define GCRY_CIPHER_AES128      GCRY_CIPHER_AES
671#define GCRY_CIPHER_RIJNDAEL    GCRY_CIPHER_AES
672#define GCRY_CIPHER_RIJNDAEL128 GCRY_CIPHER_AES128
673#define GCRY_CIPHER_RIJNDAEL192 GCRY_CIPHER_AES192
674#define GCRY_CIPHER_RIJNDAEL256 GCRY_CIPHER_AES256
675
676/* The supported encryption modes.  Note that not all of them are
677   supported for each algorithm. */
678enum gcry_cipher_modes
679  {
680    GCRY_CIPHER_MODE_NONE   = 0,  /* Not yet specified. */
681    GCRY_CIPHER_MODE_ECB    = 1,  /* Electronic codebook. */
682    GCRY_CIPHER_MODE_CFB    = 2,  /* Cipher feedback. */
683    GCRY_CIPHER_MODE_CBC    = 3,  /* Cipher block chaining. */
684    GCRY_CIPHER_MODE_STREAM = 4,  /* Used with stream ciphers. */
685    GCRY_CIPHER_MODE_OFB    = 5,  /* Outer feedback. */
686    GCRY_CIPHER_MODE_CTR    = 6,  /* Counter. */
687    GCRY_CIPHER_MODE_AESWRAP= 7   /* AES-WRAP algorithm.  */
688  };
689
690/* Flags used with the open function. */
691enum gcry_cipher_flags
692  {
693    GCRY_CIPHER_SECURE      = 1,  /* Allocate in secure memory. */
694    GCRY_CIPHER_ENABLE_SYNC = 2,  /* Enable CFB sync mode. */
695    GCRY_CIPHER_CBC_CTS     = 4,  /* Enable CBC cipher text stealing (CTS). */
696    GCRY_CIPHER_CBC_MAC     = 8   /* Enable CBC message auth. code (MAC). */
697  };
698
699
700/* Create a handle for algorithm ALGO to be used in MODE.  FLAGS may
701   be given as an bitwise OR of the gcry_cipher_flags values. */
702gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *handle,
703                              int algo, int mode, unsigned int flags);
704
705/* Close the cioher handle H and release all resource. */
706void gcry_cipher_close (gcry_cipher_hd_t h);
707
708/* Perform various operations on the cipher object H. */
709gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer,
710                             size_t buflen);
711
712/* Retrieve various information about the cipher object H. */
713gcry_error_t gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer,
714                              size_t *nbytes);
715
716/* Retrieve various information about the cipher algorithm ALGO. */
717gcry_error_t gcry_cipher_algo_info (int algo, int what, void *buffer,
718                                   size_t *nbytes);
719
720/* Map the cipher algorithm whose ID is contained in ALGORITHM to a
721   string representation of the algorithm name.  For unknown algorithm
722   IDs this function returns "?".  */
723const char *gcry_cipher_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE;
724
725/* Map the algorithm name NAME to an cipher algorithm ID.  Return 0 if
726   the algorithm name is not known. */
727int gcry_cipher_map_name (const char *name) _GCRY_GCC_ATTR_PURE;
728
729/* Given an ASN.1 object identifier in standard IETF dotted decimal
730   format in STRING, return the encryption mode associated with that
731   OID or 0 if not known or applicable. */
732int gcry_cipher_mode_from_oid (const char *string) _GCRY_GCC_ATTR_PURE;
733
734/* Encrypt the plaintext of size INLEN in IN using the cipher handle H
735   into the buffer OUT which has an allocated length of OUTSIZE.  For
736   most algorithms it is possible to pass NULL for in and 0 for INLEN
737   and do a in-place decryption of the data provided in OUT.  */
738gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h,
739                                  void *out, size_t outsize,
740                                  const void *in, size_t inlen);
741
742/* The counterpart to gcry_cipher_encrypt.  */
743gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t h,
744                                  void *out, size_t outsize,
745                                  const void *in, size_t inlen);
746
747/* Set KEY of length KEYLEN bytes for the cipher handle HD.  */
748gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t hd,
749                                 const void *key, size_t keylen);
750
751
752/* Set initialization vector IV of length IVLEN for the cipher handle HD. */
753gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t hd,
754                                const void *iv, size_t ivlen);
755
756
757/* Reset the handle to the state after open.  */
758#define gcry_cipher_reset(h)  gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0)
759
760/* Perform the OpenPGP sync operation if this is enabled for the
761   cipher handle H. */
762#define gcry_cipher_sync(h)  gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, NULL, 0)
763
764/* Enable or disable CTS in future calls to gcry_encrypt(). CBC mode only. */
765#define gcry_cipher_cts(h,on)  gcry_cipher_ctl( (h), GCRYCTL_SET_CBC_CTS, \
766                                                                   NULL, on )
767
768/* Set counter for CTR mode.  (CTR,CTRLEN) must denote a buffer of
769   block size length, or (NULL,0) to set the CTR to the all-zero block. */
770gpg_error_t gcry_cipher_setctr (gcry_cipher_hd_t hd,
771                                const void *ctr, size_t ctrlen);
772
773/* Retrieve the key length in bytes used with algorithm A. */
774size_t gcry_cipher_get_algo_keylen (int algo);
775
776/* Retrieve the block length in bytes used with algorithm A. */
777size_t gcry_cipher_get_algo_blklen (int algo);
778
779/* Return 0 if the algorithm A is available for use. */
780#define gcry_cipher_test_algo(a) \
781            gcry_cipher_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
782
783/* Get a list consisting of the IDs of the loaded cipher modules.  If
784   LIST is zero, write the number of loaded cipher modules to
785   LIST_LENGTH and return.  If LIST is non-zero, the first
786   *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
787   according size.  In case there are less cipher modules than
788   *LIST_LENGTH, *LIST_LENGTH is updated to the correct number.  */
789gcry_error_t gcry_cipher_list (int *list, int *list_length);
790
791
792/************************************
793 *                                  *
794 *    Asymmetric Cipher Functions   *
795 *                                  *
796 ************************************/
797
798/* The algorithms and their IDs we support. */
799enum gcry_pk_algos
800  {
801    GCRY_PK_RSA   = 1,
802    GCRY_PK_RSA_E = 2,      /* (deprecated) */
803    GCRY_PK_RSA_S = 3,      /* (deprecated) */
804    GCRY_PK_ELG_E = 16,
805    GCRY_PK_DSA   = 17,
806    GCRY_PK_ELG   = 20,
807    GCRY_PK_ECDSA = 301,
808    GCRY_PK_ECDH  = 302
809  };
810
811/* Flags describing usage capabilities of a PK algorithm. */
812#define GCRY_PK_USAGE_SIGN 1   /* Good for signatures. */
813#define GCRY_PK_USAGE_ENCR 2   /* Good for encryption. */
814#define GCRY_PK_USAGE_CERT 4   /* Good to certify other keys. */
815#define GCRY_PK_USAGE_AUTH 8   /* Good for authentication. */
816#define GCRY_PK_USAGE_UNKN 128 /* Unknown usage flag. */
817
818/* Encrypt the DATA using the public key PKEY and store the result as
819   a newly created S-expression at RESULT. */
820gcry_error_t gcry_pk_encrypt (gcry_sexp_t *result,
821                              gcry_sexp_t data, gcry_sexp_t pkey);
822
823/* Decrypt the DATA using the private key SKEY and store the result as
824   a newly created S-expression at RESULT. */
825gcry_error_t gcry_pk_decrypt (gcry_sexp_t *result,
826                              gcry_sexp_t data, gcry_sexp_t skey);
827
828/* Sign the DATA using the private key SKEY and store the result as
829   a newly created S-expression at RESULT. */
830gcry_error_t gcry_pk_sign (gcry_sexp_t *result,
831                           gcry_sexp_t data, gcry_sexp_t skey);
832
833/* Check the signature SIGVAL on DATA using the public key PKEY. */
834gcry_error_t gcry_pk_verify (gcry_sexp_t sigval,
835                             gcry_sexp_t data, gcry_sexp_t pkey);
836
837/* Check that private KEY is sane. */
838gcry_error_t gcry_pk_testkey (gcry_sexp_t key);
839
840/* Generate a new key pair according to the parameters given in
841   S_PARMS.  The new key pair is returned in as an S-expression in
842   R_KEY. */
843gcry_error_t gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms);
844
845/* Catch all function for miscellaneous operations. */
846gcry_error_t gcry_pk_ctl (int cmd, void *buffer, size_t buflen);
847
848/* Retrieve information about the public key algorithm ALGO. */
849gcry_error_t gcry_pk_algo_info (int algo, int what,
850                                void *buffer, size_t *nbytes);
851
852/* Map the public key algorithm whose ID is contained in ALGORITHM to
853   a string representation of the algorithm name.  For unknown
854   algorithm IDs this functions returns "?". */
855const char *gcry_pk_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE;
856
857/* Map the algorithm NAME to a public key algorithm Id.  Return 0 if
858   the algorithm name is not known. */
859int gcry_pk_map_name (const char* name) _GCRY_GCC_ATTR_PURE;
860
861/* Return what is commonly referred as the key length for the given
862   public or private KEY.  */
863unsigned int gcry_pk_get_nbits (gcry_sexp_t key) _GCRY_GCC_ATTR_PURE;
864
865/* Please note that keygrip is still experimental and should not be
866   used without contacting the author. */
867unsigned char *gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array);
868
869/* Return the name of the curve matching KEY.  */
870const char *gcry_pk_get_curve (gcry_sexp_t key, int iterator,
871                               unsigned int *r_nbits);
872
873/* Return an S-expression with the parameters of the named ECC curve
874   NAME.  ALGO must be set to an ECC algorithm.  */
875gcry_sexp_t gcry_pk_get_param (int algo, const char *name);
876
877/* Return 0 if the public key algorithm A is available for use. */
878#define gcry_pk_test_algo(a) \
879            gcry_pk_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
880
881/* Get a list consisting of the IDs of the loaded pubkey modules.  If
882   LIST is zero, write the number of loaded pubkey modules to
883   LIST_LENGTH and return.  If LIST is non-zero, the first
884   *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
885   according size.  In case there are less pubkey modules than
886   *LIST_LENGTH, *LIST_LENGTH is updated to the correct number.  */
887gcry_error_t gcry_pk_list (int *list, int *list_length);
888
889
890
891/************************************
892 *                                  *
893 *   Cryptograhic Hash Functions    *
894 *                                  *
895 ************************************/
896
897/* Algorithm IDs for the hash functions we know about. Not all of them
898   are implemnted. */
899enum gcry_md_algos
900  {
901    GCRY_MD_NONE    = 0,
902    GCRY_MD_MD5     = 1,
903    GCRY_MD_SHA1    = 2,
904    GCRY_MD_RMD160  = 3,
905    GCRY_MD_MD2     = 5,
906    GCRY_MD_TIGER   = 6,   /* TIGER/192 as used by gpg <= 1.3.2. */
907    GCRY_MD_HAVAL   = 7,   /* HAVAL, 5 pass, 160 bit. */
908    GCRY_MD_SHA256  = 8,
909    GCRY_MD_SHA384  = 9,
910    GCRY_MD_SHA512  = 10,
911    GCRY_MD_SHA224  = 11,
912    GCRY_MD_MD4     = 301,
913    GCRY_MD_CRC32         = 302,
914    GCRY_MD_CRC32_RFC1510 = 303,
915    GCRY_MD_CRC24_RFC2440 = 304,
916    GCRY_MD_WHIRLPOOL = 305,
917    GCRY_MD_TIGER1  = 306, /* TIGER fixed.  */
918    GCRY_MD_TIGER2  = 307  /* TIGER2 variant.   */
919  };
920
921/* Flags used with the open function.  */
922enum gcry_md_flags
923  {
924    GCRY_MD_FLAG_SECURE = 1,  /* Allocate all buffers in "secure" memory.  */
925    GCRY_MD_FLAG_HMAC   = 2   /* Make an HMAC out of this algorithm.  */
926  };
927
928/* (Forward declaration.)  */
929struct gcry_md_context;
930
931/* This object is used to hold a handle to a message digest object.
932   This structure is private - only to be used by the public gcry_md_*
933   macros.  */
934typedef struct gcry_md_handle
935{
936  /* Actual context.  */
937  struct gcry_md_context *ctx;
938
939  /* Buffer management.  */
940  int  bufpos;
941  int  bufsize;
942  unsigned char buf[1];
943} *gcry_md_hd_t;
944
945/* Compatibility types, do not use them.  */
946#ifndef GCRYPT_NO_DEPRECATED
947typedef struct gcry_md_handle *GCRY_MD_HD _GCRY_GCC_ATTR_DEPRECATED;
948typedef struct gcry_md_handle *GcryMDHd _GCRY_GCC_ATTR_DEPRECATED;
949#endif
950
951/* Create a message digest object for algorithm ALGO.  FLAGS may be
952   given as an bitwise OR of the gcry_md_flags values.  ALGO may be
953   given as 0 if the algorithms to be used are later set using
954   gcry_md_enable.  */
955gcry_error_t gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags);
956
957/* Release the message digest object HD.  */
958void gcry_md_close (gcry_md_hd_t hd);
959
960/* Add the message digest algorithm ALGO to the digest object HD.  */
961gcry_error_t gcry_md_enable (gcry_md_hd_t hd, int algo);
962
963/* Create a new digest object as an exact copy of the object HD.  */
964gcry_error_t gcry_md_copy (gcry_md_hd_t *bhd, gcry_md_hd_t ahd);
965
966/* Reset the digest object HD to its initial state.  */
967void gcry_md_reset (gcry_md_hd_t hd);
968
969/* Perform various operations on the digest object HD. */
970gcry_error_t gcry_md_ctl (gcry_md_hd_t hd, int cmd,
971                          void *buffer, size_t buflen);
972
973/* Pass LENGTH bytes of data in BUFFER to the digest object HD so that
974   it can update the digest values.  This is the actual hash
975   function. */
976void gcry_md_write (gcry_md_hd_t hd, const void *buffer, size_t length);
977
978/* Read out the final digest from HD return the digest value for
979   algorithm ALGO. */
980unsigned char *gcry_md_read (gcry_md_hd_t hd, int algo);
981
982/* Convenience function to calculate the hash from the data in BUFFER
983   of size LENGTH using the algorithm ALGO avoiding the creating of a
984   hash object.  The hash is returned in the caller provided buffer
985   DIGEST which must be large enough to hold the digest of the given
986   algorithm. */
987void gcry_md_hash_buffer (int algo, void *digest,
988                          const void *buffer, size_t length);
989
990/* Retrieve the algorithm used with HD.  This does not work reliable
991   if more than one algorithm is enabled in HD. */
992int gcry_md_get_algo (gcry_md_hd_t hd);
993
994/* Retrieve the length in bytes of the digest yielded by algorithm
995   ALGO. */
996unsigned int gcry_md_get_algo_dlen (int algo);
997
998/* Return true if the the algorithm ALGO is enabled in the digest
999   object A. */
1000int gcry_md_is_enabled (gcry_md_hd_t a, int algo);
1001
1002/* Return true if the digest object A is allocated in "secure" memory. */
1003int gcry_md_is_secure (gcry_md_hd_t a);
1004
1005/* Retrieve various information about the object H.  */
1006gcry_error_t gcry_md_info (gcry_md_hd_t h, int what, void *buffer,
1007                          size_t *nbytes);
1008
1009/* Retrieve various information about the algorithm ALGO.  */
1010gcry_error_t gcry_md_algo_info (int algo, int what, void *buffer,
1011                               size_t *nbytes);
1012
1013/* Map the digest algorithm id ALGO to a string representation of the
1014   algorithm name.  For unknown algorithms this function returns
1015   "?". */
1016const char *gcry_md_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
1017
1018/* Map the algorithm NAME to a digest algorithm Id.  Return 0 if
1019   the algorithm name is not known. */
1020int gcry_md_map_name (const char* name) _GCRY_GCC_ATTR_PURE;
1021
1022/* For use with the HMAC feature, the set MAC key to the KEY of
1023   KEYLEN bytes. */
1024gcry_error_t gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen);
1025
1026/* Start or stop debugging for digest handle HD; i.e. create a file
1027   named dbgmd-<n>.<suffix> while hashing.  If SUFFIX is NULL,
1028   debugging stops and the file will be closed. */
1029void gcry_md_debug (gcry_md_hd_t hd, const char *suffix);
1030
1031
1032/* Update the hash(s) of H with the character C.  This is a buffered
1033   version of the gcry_md_write function. */
1034#define gcry_md_putc(h,c)  \
1035            do {                                          \
1036                gcry_md_hd_t h__ = (h);                   \
1037                if( (h__)->bufpos == (h__)->bufsize )     \
1038                    gcry_md_write( (h__), NULL, 0 );      \
1039                (h__)->buf[(h__)->bufpos++] = (c) & 0xff; \
1040            } while(0)
1041
1042/* Finalize the digest calculation.  This is not really needed because
1043   gcry_md_read() does this implicitly. */
1044#define gcry_md_final(a) \
1045            gcry_md_ctl ((a), GCRYCTL_FINALIZE, NULL, 0)
1046
1047/* Return 0 if the algorithm A is available for use. */
1048#define gcry_md_test_algo(a) \
1049            gcry_md_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
1050
1051/* Return an DER encoded ASN.1 OID for the algorithm A in buffer B. N
1052   must point to size_t variable with the available size of buffer B.
1053   After return it will receive the actual size of the returned
1054   OID. */
1055#define gcry_md_get_asnoid(a,b,n) \
1056            gcry_md_algo_info((a), GCRYCTL_GET_ASNOID, (b), (n))
1057
1058/* Enable debugging for digest object A; i.e. create files named
1059   dbgmd-<n>.<string> while hashing.  B is a string used as the suffix
1060   for the filename.  This macro is deprecated, use gcry_md_debug. */
1061#ifndef GCRYPT_NO_DEPRECATED
1062#define gcry_md_start_debug(a,b) \
1063            gcry_md_ctl( (a), GCRYCTL_START_DUMP, (b), 0 )
1064
1065/* Disable the debugging of A.  This macro is deprecated, use
1066   gcry_md_debug.  */
1067#define gcry_md_stop_debug(a,b) \
1068            gcry_md_ctl( (a), GCRYCTL_STOP_DUMP, (b), 0 )
1069#endif
1070
1071/* Get a list consisting of the IDs of the loaded message digest
1072   modules.  If LIST is zero, write the number of loaded message
1073   digest modules to LIST_LENGTH and return.  If LIST is non-zero, the
1074   first *LIST_LENGTH algorithm IDs are stored in LIST, which must be
1075   of according size.  In case there are less message digest modules
1076   than *LIST_LENGTH, *LIST_LENGTH is updated to the correct
1077   number.  */
1078gcry_error_t gcry_md_list (int *list, int *list_length);
1079
1080
1081#if !defined(GCRYPT_NO_DEPRECATED) || defined(_GCRYPT_IN_LIBGCRYPT)
1082/* Alternative interface for asymmetric cryptography.  This interface
1083   is deprecated.  */
1084
1085/* The algorithm IDs. */
1086typedef enum gcry_ac_id
1087  {
1088    GCRY_AC_RSA = 1,
1089    GCRY_AC_DSA = 17,
1090    GCRY_AC_ELG = 20,
1091    GCRY_AC_ELG_E = 16
1092  }
1093gcry_ac_id_t _GCRY_ATTR_INTERNAL;
1094
1095/* Key types.  */
1096typedef enum gcry_ac_key_type
1097  {
1098    GCRY_AC_KEY_SECRET,
1099    GCRY_AC_KEY_PUBLIC
1100  }
1101gcry_ac_key_type_t _GCRY_ATTR_INTERNAL;
1102
1103/* Encoding methods.  */
1104typedef enum gcry_ac_em
1105  {
1106    GCRY_AC_EME_PKCS_V1_5,
1107    GCRY_AC_EMSA_PKCS_V1_5
1108  }
1109gcry_ac_em_t _GCRY_ATTR_INTERNAL;
1110
1111/* Encryption and Signature schemes.  */
1112typedef enum gcry_ac_scheme
1113  {
1114    GCRY_AC_ES_PKCS_V1_5,
1115    GCRY_AC_SSA_PKCS_V1_5
1116  }
1117gcry_ac_scheme_t _GCRY_ATTR_INTERNAL;
1118
1119/* AC data.  */
1120#define GCRY_AC_FLAG_DEALLOC     (1 << 0)
1121#define GCRY_AC_FLAG_COPY        (1 << 1)
1122#define GCRY_AC_FLAG_NO_BLINDING (1 << 2)
1123
1124/* This type represents a `data set'.  */
1125typedef struct gcry_ac_data *gcry_ac_data_t _GCRY_ATTR_INTERNAL;
1126
1127/* This type represents a single `key', either a secret one or a
1128   public one.  */
1129typedef struct gcry_ac_key *gcry_ac_key_t _GCRY_ATTR_INTERNAL;
1130
1131/* This type represents a `key pair' containing a secret and a public
1132   key.  */
1133typedef struct gcry_ac_key_pair *gcry_ac_key_pair_t _GCRY_ATTR_INTERNAL;
1134
1135/* This type represents a `handle' that is needed by functions
1136   performing cryptographic operations.  */
1137typedef struct gcry_ac_handle *gcry_ac_handle_t _GCRY_ATTR_INTERNAL;
1138
1139typedef gpg_error_t (*gcry_ac_data_read_cb_t) (void *opaque,
1140                                               unsigned char *buffer,
1141                                               size_t *buffer_n)
1142  /* */  _GCRY_ATTR_INTERNAL;
1143
1144typedef gpg_error_t (*gcry_ac_data_write_cb_t) (void *opaque,
1145                                                unsigned char *buffer,
1146                                                size_t buffer_n)
1147  /* */  _GCRY_ATTR_INTERNAL;
1148
1149typedef enum
1150  {
1151    GCRY_AC_IO_READABLE,
1152    GCRY_AC_IO_WRITABLE
1153  }
1154gcry_ac_io_mode_t _GCRY_ATTR_INTERNAL;
1155
1156typedef enum
1157  {
1158    GCRY_AC_IO_STRING,
1159    GCRY_AC_IO_CALLBACK
1160  }
1161gcry_ac_io_type_t _GCRY_ATTR_INTERNAL;
1162
1163typedef struct gcry_ac_io
1164{
1165  /* This is an INTERNAL structure, do NOT use manually.  */
1166  gcry_ac_io_mode_t mode _GCRY_ATTR_INTERNAL;
1167  gcry_ac_io_type_t type _GCRY_ATTR_INTERNAL;
1168  union
1169  {
1170    union
1171    {
1172      struct
1173      {
1174        gcry_ac_data_read_cb_t cb;
1175        void *opaque;
1176      } callback;
1177      struct
1178      {
1179        unsigned char *data;
1180        size_t data_n;
1181      } string;
1182      void *opaque;
1183    } readable;
1184    union
1185    {
1186      struct
1187      {
1188        gcry_ac_data_write_cb_t cb;
1189        void *opaque;
1190      } callback;
1191      struct
1192      {
1193        unsigned char **data;
1194        size_t *data_n;
1195      } string;
1196      void *opaque;
1197    } writable;
1198  } io _GCRY_ATTR_INTERNAL;
1199}
1200gcry_ac_io_t _GCRY_ATTR_INTERNAL;
1201
1202/* The caller of gcry_ac_key_pair_generate can provide one of these
1203   structures in order to influence the key generation process in an
1204   algorithm-specific way.  */
1205typedef struct gcry_ac_key_spec_rsa
1206{
1207  gcry_mpi_t e;                 /* E to use.  */
1208} gcry_ac_key_spec_rsa_t _GCRY_ATTR_INTERNAL;
1209
1210/* Structure used for passing data to the implementation of the
1211   `EME-PKCS-V1_5' encoding method.  */
1212typedef struct gcry_ac_eme_pkcs_v1_5
1213{
1214  size_t key_size;
1215} gcry_ac_eme_pkcs_v1_5_t _GCRY_ATTR_INTERNAL;
1216
1217typedef enum gcry_md_algos gcry_md_algo_t _GCRY_ATTR_INTERNAL;
1218
1219/* Structure used for passing data to the implementation of the
1220   `EMSA-PKCS-V1_5' encoding method.  */
1221typedef struct gcry_ac_emsa_pkcs_v1_5
1222{
1223  gcry_md_algo_t md;
1224  size_t em_n;
1225} gcry_ac_emsa_pkcs_v1_5_t _GCRY_ATTR_INTERNAL;
1226
1227/* Structure used for passing data to the implementation of the
1228   `SSA-PKCS-V1_5' signature scheme.  */
1229typedef struct gcry_ac_ssa_pkcs_v1_5
1230{
1231  gcry_md_algo_t md;
1232} gcry_ac_ssa_pkcs_v1_5_t _GCRY_ATTR_INTERNAL;
1233#endif /* !GCRYPT_NO_DEPRECATED || !_GCRYPT_IN_LIBGCRYPT */
1234
1235
1236#ifndef GCRYPT_NO_DEPRECATED
1237/* Returns a new, empty data set in DATA.  */
1238gcry_error_t gcry_ac_data_new (gcry_ac_data_t *data)
1239  /* */                       _GCRY_ATTR_INTERNAL;
1240
1241/* Destroy the data set DATA.  */
1242void gcry_ac_data_destroy (gcry_ac_data_t data)
1243  /* */                       _GCRY_ATTR_INTERNAL;
1244
1245/* Create a copy of the data set DATA and store it in DATA_CP.  */
1246gcry_error_t gcry_ac_data_copy (gcry_ac_data_t *data_cp,
1247                                gcry_ac_data_t data)
1248  /* */                       _GCRY_ATTR_INTERNAL;
1249
1250/* Return the number of named MPI values inside of the data set
1251   DATA.  */
1252unsigned int gcry_ac_data_length (gcry_ac_data_t data)
1253  /* */                       _GCRY_ATTR_INTERNAL;
1254
1255/* Destroy any values contained in the data set DATA.  */
1256void gcry_ac_data_clear (gcry_ac_data_t data)
1257  /* */ _GCRY_ATTR_INTERNAL;
1258
1259/* Add the value MPI to DATA with the label NAME.  If FLAGS contains
1260   GCRY_AC_FLAG_DATA_COPY, the data set will contain copies of NAME
1261   and MPI.  If FLAGS contains GCRY_AC_FLAG_DATA_DEALLOC or
1262   GCRY_AC_FLAG_DATA_COPY, the values contained in the data set will
1263   be deallocated when they are to be removed from the data set.  */
1264gcry_error_t gcry_ac_data_set (gcry_ac_data_t data, unsigned int flags,
1265                               const char *name, gcry_mpi_t mpi)
1266  /* */ _GCRY_ATTR_INTERNAL;
1267
1268/* Store the value labelled with NAME found in DATA in MPI.  If FLAGS
1269   contains GCRY_AC_FLAG_COPY, store a copy of the MPI value contained
1270   in the data set.  MPI may be NULL.  */
1271gcry_error_t gcry_ac_data_get_name (gcry_ac_data_t data, unsigned int flags,
1272                                    const char *name, gcry_mpi_t *mpi)
1273  /* */ _GCRY_ATTR_INTERNAL;
1274
1275/* Stores in NAME and MPI the named MPI value contained in the data
1276   set DATA with the index IDX.  If FLAGS contains GCRY_AC_FLAG_COPY,
1277   store copies of the values contained in the data set. NAME or MPI
1278   may be NULL.  */
1279gcry_error_t gcry_ac_data_get_index (gcry_ac_data_t data, unsigned int flags,
1280                                     unsigned int idx,
1281                                     const char **name, gcry_mpi_t *mpi)
1282  /* */ _GCRY_ATTR_INTERNAL;
1283
1284/* Convert the data set DATA into a new S-Expression, which is to be
1285   stored in SEXP, according to the identifiers contained in
1286   IDENTIFIERS.  */
1287gcry_error_t gcry_ac_data_to_sexp (gcry_ac_data_t data, gcry_sexp_t *sexp,
1288                                   const char **identifiers)
1289  /* */ _GCRY_ATTR_INTERNAL;
1290
1291/* Create a new data set, which is to be stored in DATA_SET, from the
1292   S-Expression SEXP, according to the identifiers contained in
1293   IDENTIFIERS.  */
1294gcry_error_t gcry_ac_data_from_sexp (gcry_ac_data_t *data, gcry_sexp_t sexp,
1295                                     const char **identifiers)
1296  /* */ _GCRY_ATTR_INTERNAL;
1297
1298/* Initialize AC_IO according to MODE, TYPE and the variable list of
1299   arguments.  The list of variable arguments to specify depends on
1300   the given TYPE.  */
1301void gcry_ac_io_init (gcry_ac_io_t *ac_io, gcry_ac_io_mode_t mode,
1302                      gcry_ac_io_type_t type, ...)
1303  /* */ _GCRY_ATTR_INTERNAL;
1304
1305/* Initialize AC_IO according to MODE, TYPE and the variable list of
1306   arguments AP.  The list of variable arguments to specify depends on
1307   the given TYPE.  */
1308void gcry_ac_io_init_va (gcry_ac_io_t *ac_io, gcry_ac_io_mode_t mode,
1309                         gcry_ac_io_type_t type, va_list ap)
1310  /* */ _GCRY_ATTR_INTERNAL;
1311
1312/* Create a new ac handle.  */
1313gcry_error_t gcry_ac_open (gcry_ac_handle_t *handle,
1314                           gcry_ac_id_t algorithm, unsigned int flags)
1315  /* */ _GCRY_ATTR_INTERNAL;
1316
1317/* Destroy an ac handle.  */
1318void gcry_ac_close (gcry_ac_handle_t handle)
1319  /* */ _GCRY_ATTR_INTERNAL;
1320
1321/* Initialize a key from a given data set.  */
1322gcry_error_t gcry_ac_key_init (gcry_ac_key_t *key, gcry_ac_handle_t handle,
1323                               gcry_ac_key_type_t type, gcry_ac_data_t data)
1324  /* */ _GCRY_ATTR_INTERNAL;
1325
1326/* Generates a new key pair via the handle HANDLE of NBITS bits and
1327   stores it in KEY_PAIR.  In case non-standard settings are wanted, a
1328   pointer to a structure of type gcry_ac_key_spec_<algorithm>_t,
1329   matching the selected algorithm, can be given as KEY_SPEC.
1330   MISC_DATA is not used yet.  */
1331gcry_error_t gcry_ac_key_pair_generate (gcry_ac_handle_t handle,
1332                                        unsigned int nbits, void *spec,
1333                                        gcry_ac_key_pair_t *key_pair,
1334                                        gcry_mpi_t **misc_data)
1335  /* */ _GCRY_ATTR_INTERNAL;
1336
1337/* Returns the key of type WHICH out of the key pair KEY_PAIR.  */
1338gcry_ac_key_t gcry_ac_key_pair_extract (gcry_ac_key_pair_t key_pair,
1339                                        gcry_ac_key_type_t which)
1340  /* */ _GCRY_ATTR_INTERNAL;
1341
1342/* Returns the data set contained in the key KEY.  */
1343gcry_ac_data_t gcry_ac_key_data_get (gcry_ac_key_t key)
1344  /* */ _GCRY_ATTR_INTERNAL;
1345
1346/* Verifies that the key KEY is sane via HANDLE.  */
1347gcry_error_t gcry_ac_key_test (gcry_ac_handle_t handle, gcry_ac_key_t key)
1348  /* */ _GCRY_ATTR_INTERNAL;
1349
1350/* Stores the number of bits of the key KEY in NBITS via HANDLE.  */
1351gcry_error_t gcry_ac_key_get_nbits (gcry_ac_handle_t handle,
1352                                    gcry_ac_key_t key, unsigned int *nbits)
1353  /* */ _GCRY_ATTR_INTERNAL;
1354
1355/* Writes the 20 byte long key grip of the key KEY to KEY_GRIP via
1356   HANDLE.  */
1357gcry_error_t gcry_ac_key_get_grip (gcry_ac_handle_t handle, gcry_ac_key_t key,
1358                                   unsigned char *key_grip)
1359  /* */ _GCRY_ATTR_INTERNAL;
1360
1361/* Destroy a key.  */
1362void gcry_ac_key_destroy (gcry_ac_key_t key)
1363  /* */ _GCRY_ATTR_INTERNAL;
1364
1365/* Destroy a key pair.  */
1366void gcry_ac_key_pair_destroy (gcry_ac_key_pair_t key_pair)
1367  /* */ _GCRY_ATTR_INTERNAL;
1368
1369/* Encodes a message according to the encoding method METHOD.  OPTIONS
1370   must be a pointer to a method-specific structure
1371   (gcry_ac_em*_t).  */
1372gcry_error_t gcry_ac_data_encode (gcry_ac_em_t method,
1373                                  unsigned int flags, void *options,
1374                                  gcry_ac_io_t *io_read,
1375                                  gcry_ac_io_t *io_write)
1376  /* */ _GCRY_ATTR_INTERNAL;
1377
1378/* Decodes a message according to the encoding method METHOD.  OPTIONS
1379   must be a pointer to a method-specific structure
1380   (gcry_ac_em*_t).  */
1381gcry_error_t gcry_ac_data_decode (gcry_ac_em_t method,
1382                                  unsigned int flags, void *options,
1383                                  gcry_ac_io_t *io_read,
1384                                  gcry_ac_io_t *io_write)
1385  /* */ _GCRY_ATTR_INTERNAL;
1386
1387/* Encrypt the plain text MPI value DATA_PLAIN with the key KEY under
1388   the control of the flags FLAGS and store the resulting data set
1389   into DATA_ENCRYPTED.  */
1390gcry_error_t gcry_ac_data_encrypt (gcry_ac_handle_t handle,
1391                                   unsigned int flags,
1392                                   gcry_ac_key_t key,
1393                                   gcry_mpi_t data_plain,
1394                                   gcry_ac_data_t *data_encrypted)
1395  /* */ _GCRY_ATTR_INTERNAL;
1396
1397/* Decrypt the decrypted data contained in the data set DATA_ENCRYPTED
1398   with the key KEY under the control of the flags FLAGS and store the
1399   resulting plain text MPI value in DATA_PLAIN.  */
1400gcry_error_t gcry_ac_data_decrypt (gcry_ac_handle_t handle,
1401                                   unsigned int flags,
1402                                   gcry_ac_key_t key,
1403                                   gcry_mpi_t *data_plain,
1404                                   gcry_ac_data_t data_encrypted)
1405  /* */ _GCRY_ATTR_INTERNAL;
1406
1407/* Sign the data contained in DATA with the key KEY and store the
1408   resulting signature in the data set DATA_SIGNATURE.  */
1409gcry_error_t gcry_ac_data_sign (gcry_ac_handle_t handle,
1410                                gcry_ac_key_t key,
1411                                gcry_mpi_t data,
1412                                gcry_ac_data_t *data_signature)
1413  /* */ _GCRY_ATTR_INTERNAL;
1414
1415/* Verify that the signature contained in the data set DATA_SIGNATURE
1416   is indeed the result of signing the data contained in DATA with the
1417   secret key belonging to the public key KEY.  */
1418gcry_error_t gcry_ac_data_verify (gcry_ac_handle_t handle,
1419                                  gcry_ac_key_t key,
1420                                  gcry_mpi_t data,
1421                                  gcry_ac_data_t data_signature)
1422  /* */ _GCRY_ATTR_INTERNAL;
1423
1424/* Encrypts the plain text readable from IO_MESSAGE through HANDLE
1425   with the public key KEY according to SCHEME, FLAGS and OPTS.  If
1426   OPTS is not NULL, it has to be a pointer to a structure specific to
1427   the chosen scheme (gcry_ac_es_*_t).  The encrypted message is
1428   written to IO_CIPHER. */
1429gcry_error_t gcry_ac_data_encrypt_scheme (gcry_ac_handle_t handle,
1430                                          gcry_ac_scheme_t scheme,
1431                                          unsigned int flags, void *opts,
1432                                          gcry_ac_key_t key,
1433                                          gcry_ac_io_t *io_message,
1434                                          gcry_ac_io_t *io_cipher)
1435  /* */ _GCRY_ATTR_INTERNAL;
1436
1437/* Decrypts the cipher text readable from IO_CIPHER through HANDLE
1438   with the secret key KEY according to SCHEME, @var{flags} and OPTS.
1439   If OPTS is not NULL, it has to be a pointer to a structure specific
1440   to the chosen scheme (gcry_ac_es_*_t).  The decrypted message is
1441   written to IO_MESSAGE.  */
1442gcry_error_t gcry_ac_data_decrypt_scheme (gcry_ac_handle_t handle,
1443                                          gcry_ac_scheme_t scheme,
1444                                          unsigned int flags, void *opts,
1445                                          gcry_ac_key_t key,
1446                                          gcry_ac_io_t *io_cipher,
1447                                          gcry_ac_io_t *io_message)
1448  /* */ _GCRY_ATTR_INTERNAL;
1449
1450/* Signs the message readable from IO_MESSAGE through HANDLE with the
1451   secret key KEY according to SCHEME, FLAGS and OPTS.  If OPTS is not
1452   NULL, it has to be a pointer to a structure specific to the chosen
1453   scheme (gcry_ac_ssa_*_t).  The signature is written to
1454   IO_SIGNATURE.  */
1455gcry_error_t gcry_ac_data_sign_scheme (gcry_ac_handle_t handle,
1456                                       gcry_ac_scheme_t scheme,
1457                                       unsigned int flags, void *opts,
1458                                       gcry_ac_key_t key,
1459                                       gcry_ac_io_t *io_message,
1460                                       gcry_ac_io_t *io_signature)
1461  /* */ _GCRY_ATTR_INTERNAL;
1462
1463/* Verifies through HANDLE that the signature readable from
1464   IO_SIGNATURE is indeed the result of signing the message readable
1465   from IO_MESSAGE with the secret key belonging to the public key KEY
1466   according to SCHEME and OPTS.  If OPTS is not NULL, it has to be an
1467   anonymous structure (gcry_ac_ssa_*_t) specific to the chosen
1468   scheme.  */
1469gcry_error_t gcry_ac_data_verify_scheme (gcry_ac_handle_t handle,
1470                                         gcry_ac_scheme_t scheme,
1471                                         unsigned int flags, void *opts,
1472                                         gcry_ac_key_t key,
1473                                         gcry_ac_io_t *io_message,
1474                                         gcry_ac_io_t *io_signature)
1475  /* */ _GCRY_ATTR_INTERNAL;
1476
1477/* Store the textual representation of the algorithm whose id is given
1478   in ALGORITHM in NAME.  This function is deprecated; use
1479   gcry_pk_algo_name. */
1480gcry_error_t gcry_ac_id_to_name (gcry_ac_id_t algorithm,
1481                                 const char **name)
1482     /* */                      _GCRY_GCC_ATTR_DEPRECATED;
1483/* Store the numeric ID of the algorithm whose textual representation
1484   is contained in NAME in ALGORITHM.  This function is deprecated;
1485   use gcry_pk_map_name. */
1486gcry_error_t gcry_ac_name_to_id (const char *name,
1487                                 gcry_ac_id_t *algorithm)
1488     /* */                      _GCRY_GCC_ATTR_DEPRECATED;
1489#endif /*GCRYPT_NO_DEPRECATED*/
1490
1491
1492/******************************
1493 *                            *
1494 *  Key Derivation Functions  *
1495 *                            *
1496 ******************************/
1497
1498/* Algorithm IDs for the KDFs.  */
1499enum gcry_kdf_algos
1500  {
1501    GCRY_KDF_NONE = 0,
1502    GCRY_KDF_SIMPLE_S2K = 16,
1503    GCRY_KDF_SALTED_S2K = 17,
1504    GCRY_KDF_ITERSALTED_S2K = 19,
1505    GCRY_KDF_PBKDF1 = 33,
1506    GCRY_KDF_PBKDF2 = 34
1507  };
1508
1509/* Derive a key from a passphrase.  */
1510gpg_error_t gcry_kdf_derive (const void *passphrase, size_t passphraselen,
1511                             int algo, int subalgo,
1512                             const void *salt, size_t saltlen,
1513                             unsigned long iterations,
1514                             size_t keysize, void *keybuffer);
1515
1516
1517
1518
1519/************************************
1520 *                                  *
1521 *   Random Generating Functions    *
1522 *                                  *
1523 ************************************/
1524
1525/* The possible values for the random quality.  The rule of thumb is
1526   to use STRONG for session keys and VERY_STRONG for key material.
1527   WEAK is usually an alias for STRONG and should not be used anymore
1528   (except with gcry_mpi_randomize); use gcry_create_nonce instead. */
1529typedef enum gcry_random_level
1530  {
1531    GCRY_WEAK_RANDOM = 0,
1532    GCRY_STRONG_RANDOM = 1,
1533    GCRY_VERY_STRONG_RANDOM = 2
1534  }
1535gcry_random_level_t;
1536
1537/* Fill BUFFER with LENGTH bytes of random, using random numbers of
1538   quality LEVEL. */
1539void gcry_randomize (void *buffer, size_t length,
1540                     enum gcry_random_level level);
1541
1542/* Add the external random from BUFFER with LENGTH bytes into the
1543   pool. QUALITY should either be -1 for unknown or in the range of 0
1544   to 100 */
1545gcry_error_t gcry_random_add_bytes (const void *buffer, size_t length,
1546                                    int quality);
1547
1548/* If random numbers are used in an application, this macro should be
1549   called from time to time so that new stuff gets added to the
1550   internal pool of the RNG.  */
1551#define gcry_fast_random_poll()  gcry_control (GCRYCTL_FAST_POLL, NULL)
1552
1553
1554/* Return NBYTES of allocated random using a random numbers of quality
1555   LEVEL. */
1556void *gcry_random_bytes (size_t nbytes, enum gcry_random_level level)
1557                         _GCRY_GCC_ATTR_MALLOC;
1558
1559/* Return NBYTES of allocated random using a random numbers of quality
1560   LEVEL.  The random numbers are created returned in "secure"
1561   memory. */
1562void *gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level)
1563                                _GCRY_GCC_ATTR_MALLOC;
1564
1565
1566/* Set the big integer W to a random value of NBITS using a random
1567   generator with quality LEVEL.  Note that by using a level of
1568   GCRY_WEAK_RANDOM gcry_create_nonce is used internally. */
1569void gcry_mpi_randomize (gcry_mpi_t w,
1570                         unsigned int nbits, enum gcry_random_level level);
1571
1572
1573/* Create an unpredicable nonce of LENGTH bytes in BUFFER. */
1574void gcry_create_nonce (void *buffer, size_t length);
1575
1576
1577
1578
1579
1580/*******************************/
1581/*                             */
1582/*    Prime Number Functions   */
1583/*                             */
1584/*******************************/
1585
1586/* Mode values passed to a gcry_prime_check_func_t. */
1587#define GCRY_PRIME_CHECK_AT_FINISH      0
1588#define GCRY_PRIME_CHECK_AT_GOT_PRIME   1
1589#define GCRY_PRIME_CHECK_AT_MAYBE_PRIME 2
1590
1591/* The function should return 1 if the operation shall continue, 0 to
1592   reject the prime candidate. */
1593typedef int (*gcry_prime_check_func_t) (void *arg, int mode,
1594                                        gcry_mpi_t candidate);
1595
1596/* Flags for gcry_prime_generate():  */
1597
1598/* Allocate prime numbers and factors in secure memory.  */
1599#define GCRY_PRIME_FLAG_SECRET         (1 << 0)
1600
1601/* Make sure that at least one prime factor is of size
1602   `FACTOR_BITS'.  */
1603#define GCRY_PRIME_FLAG_SPECIAL_FACTOR (1 << 1)
1604
1605/* Generate a new prime number of PRIME_BITS bits and store it in
1606   PRIME.  If FACTOR_BITS is non-zero, one of the prime factors of
1607   (prime - 1) / 2 must be FACTOR_BITS bits long.  If FACTORS is
1608   non-zero, allocate a new, NULL-terminated array holding the prime
1609   factors and store it in FACTORS.  FLAGS might be used to influence
1610   the prime number generation process.  */
1611gcry_error_t gcry_prime_generate (gcry_mpi_t *prime,
1612                                  unsigned int prime_bits,
1613                                  unsigned int factor_bits,
1614                                  gcry_mpi_t **factors,
1615                                  gcry_prime_check_func_t cb_func,
1616                                  void *cb_arg,
1617                                  gcry_random_level_t random_level,
1618                                  unsigned int flags);
1619
1620/* Find a generator for PRIME where the factorization of (prime-1) is
1621   in the NULL terminated array FACTORS. Return the generator as a
1622   newly allocated MPI in R_G.  If START_G is not NULL, use this as
1623   teh start for the search. */
1624gcry_error_t gcry_prime_group_generator (gcry_mpi_t *r_g,
1625                                         gcry_mpi_t prime,
1626                                         gcry_mpi_t *factors,
1627                                         gcry_mpi_t start_g);
1628
1629
1630/* Convenience function to release the FACTORS array. */
1631void gcry_prime_release_factors (gcry_mpi_t *factors);
1632
1633
1634/* Check wether the number X is prime.  */
1635gcry_error_t gcry_prime_check (gcry_mpi_t x, unsigned int flags);
1636
1637
1638
1639/************************************
1640 *                                  *
1641 *     Miscellaneous Stuff          *
1642 *                                  *
1643 ************************************/
1644
1645/* Log levels used by the internal logging facility. */
1646enum gcry_log_levels
1647  {
1648    GCRY_LOG_CONT   = 0,    /* (Continue the last log line.) */
1649    GCRY_LOG_INFO   = 10,
1650    GCRY_LOG_WARN   = 20,
1651    GCRY_LOG_ERROR  = 30,
1652    GCRY_LOG_FATAL  = 40,
1653    GCRY_LOG_BUG    = 50,
1654    GCRY_LOG_DEBUG  = 100
1655  };
1656
1657/* Type for progress handlers.  */
1658typedef void (*gcry_handler_progress_t) (void *, const char *, int, int, int);
1659
1660/* Type for memory allocation handlers.  */
1661typedef void *(*gcry_handler_alloc_t) (size_t n);
1662
1663/* Type for secure memory check handlers.  */
1664typedef int (*gcry_handler_secure_check_t) (const void *);
1665
1666/* Type for memory reallocation handlers.  */
1667typedef void *(*gcry_handler_realloc_t) (void *p, size_t n);
1668
1669/* Type for memory free handlers.  */
1670typedef void (*gcry_handler_free_t) (void *);
1671
1672/* Type for out-of-memory handlers.  */
1673typedef int (*gcry_handler_no_mem_t) (void *, size_t, unsigned int);
1674
1675/* Type for fatal error handlers.  */
1676typedef void (*gcry_handler_error_t) (void *, int, const char *);
1677
1678/* Type for logging handlers.  */
1679typedef void (*gcry_handler_log_t) (void *, int, const char *, va_list);
1680
1681/* Certain operations can provide progress information.  This function
1682   is used to register a handler for retrieving these information. */
1683void gcry_set_progress_handler (gcry_handler_progress_t cb, void *cb_data);
1684
1685
1686/* Register a custom memory allocation functions. */
1687void gcry_set_allocation_handler (
1688                             gcry_handler_alloc_t func_alloc,
1689                             gcry_handler_alloc_t func_alloc_secure,
1690                             gcry_handler_secure_check_t func_secure_check,
1691                             gcry_handler_realloc_t func_realloc,
1692                             gcry_handler_free_t func_free);
1693
1694/* Register a function used instead of the internal out of memory
1695   handler. */
1696void gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque);
1697
1698/* Register a function used instead of the internal fatal error
1699   handler. */
1700void gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque);
1701
1702/* Register a function used instead of the internal logging
1703   facility. */
1704void gcry_set_log_handler (gcry_handler_log_t f, void *opaque);
1705
1706/* Reserved for future use. */
1707void gcry_set_gettext_handler (const char *(*f)(const char*));
1708
1709/* Libgcrypt uses its own memory allocation.  It is important to use
1710   gcry_free () to release memory allocated by libgcrypt. */
1711void *gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
1712void *gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1713void *gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
1714void *gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1715void *gcry_realloc (void *a, size_t n);
1716char *gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC;
1717void *gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
1718void *gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1719void *gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
1720void *gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
1721void *gcry_xrealloc (void *a, size_t n);
1722char *gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC;
1723void  gcry_free (void *a);
1724
1725/* Return true if A is allocated in "secure" memory. */
1726int gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE;
1727
1728/* Return true if Libgcrypt is in FIPS mode.  */
1729#define gcry_fips_mode_active()  !!gcry_control (GCRYCTL_FIPS_MODE_P, 0)
1730
1731
1732/* Include support for Libgcrypt modules.  */
1733#include <gcrypt-module.h>
1734
1735#if 0 /* (Keep Emacsens' auto-indent happy.) */
1736{
1737#endif
1738#ifdef __cplusplus
1739}
1740#endif
1741#endif /* _GCRYPT_H */
1742/*
1743@emacs_local_vars_begin@
1744@emacs_local_vars_read_only@
1745@emacs_local_vars_end@
1746*/
Note: See TracBrowser for help on using the repository browser.