home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / gcrypt.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-04-11  |  67.7 KB  |  1,797 lines

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