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