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