1 \input texinfo @c -*- Texinfo -*-
3 @setfilename gcrypt.info
5 @settitle The Libgcrypt Reference Manual
6 @c Unify some of the indices.
11 This manual is for Libgcrypt
12 (version @value{VERSION}, @value{UPDATED}),
13 which is GNU's library of cryptographic building blocks.
15 Copyright @copyright{} 2000, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
18 Permission is granted to copy, distribute and/or modify this document
19 under the terms of the GNU General Public License as published by the
20 Free Software Foundation; either version 2 of the License, or (at your
21 option) any later version. The text of the license can be found in the
22 section entitled ``Copying''.
26 @dircategory GNU Libraries
28 * libgcrypt: (gcrypt). Cryptographic function library.
36 @setchapternewpage odd
38 @title The Libgcrypt Reference Manual
39 @subtitle Version @value{VERSION}
40 @subtitle @value{UPDATED}
41 @author Werner Koch (@email{wk@@gnupg.org})
42 @author Moritz Schulte (@email{mo@@g10code.com})
45 @vskip 0pt plus 1filll
57 @top The Libgcrypt Library
63 * Introduction:: What is @acronym{Libgcrypt}.
64 * Preparation:: What you should do before using the library.
65 * Generalities:: General library functions and data types.
66 * Handler Functions:: Working with handler functions.
67 * Symmetric cryptography:: How to use symmetric cryptography.
68 * Hashing:: How to use hashing.
69 * Public Key cryptography (I):: How to use public key cryptography.
70 * Public Key cryptography (II):: How to use public key cryptography, alternatively.
71 * Random Numbers:: How to work with random numbers.
72 * S-expressions:: How to manage S-expressions.
73 * MPI library:: How to work with multi-precision-integers.
74 * Prime numbers:: How to use the Prime number related functions.
75 * Utilities:: Utility functions.
79 * Library Copying:: The GNU Lesser General Public License
80 says how you can copy and share `Libgcrypt'.
81 * Copying:: The GNU General Public License says how you
82 can copy and share some parts of `Libgcrypt'.
86 * Concept Index:: Index of concepts and programs.
87 * Function and Data Index:: Index of functions, variables and data types.
90 --- The Detailed Node Listing ---
93 * Getting Started:: How to use this manual.
94 * Features:: A glance at @acronym{Libgcrypt}'s features.
95 * Overview:: Overview about the library.
98 * Header:: What header file you need to include.
99 * Building sources:: How to build sources using the library.
100 * Building sources using Automake:: How to build sources with the help of Automake.
101 * Initializing the library:: How to initialize the library.
102 * Multi Threading:: How @acronym{Libgcrypt} can be used in a MT environment.
105 * Controlling the library:: Controlling @acronym{Libgcrypt}'s behavior.
106 * Modules:: Description of extension modules.
107 * Error Handling:: Error codes and such.
110 * Progress handler:: Using a progress handler function.
111 * Allocation handler:: Using special memory allocation functions.
112 * Error handler:: Using error handler functions.
113 * Logging handler:: Using a special logging function.
115 Symmetric cryptography
116 * Available ciphers:: List of ciphers supported by the library.
117 * Cipher modules:: How to work with cipher modules.
118 * Available cipher modes:: List of cipher modes supported by the library.
119 * Working with cipher handles:: How to perform operations related to cipher handles.
120 * General cipher functions:: General cipher functions independent of cipher handles.
123 * Available hash algorithms:: List of hash algorithms supported by the library.
124 * Hash algorithm modules:: How to work with hash algorithm modules.
125 * Working with hash algorithms:: List of functions related to hashing.
127 Public Key cryptography (I)
128 * Used S-expressions:: Introduction into the used S-expression.
129 * Available algorithms:: Algorithms supported by the library.
130 * Public key modules:: How to work with public key modules.
131 * Cryptographic Functions:: Functions for performing the cryptographic actions.
132 * General public-key related Functions:: General functions, not implementing any cryptography.
134 Public Key cryptography (II)
135 * Available asymmetric algorithms:: List of algorithms supported by the library.
136 * Working with sets of data:: How to work with sets of data.
137 * Working with handles:: How to use handles.
138 * Working with keys:: How to work with keys.
139 * Using cryptographic functions:: How to perform cryptographic operations.
140 * Handle-independent functions:: General functions independent of handles.
143 * Quality of random numbers:: @acronym{Libgcrypt} uses different quality levels.
144 * Retrieving random numbers:: How to retrieve random numbers.
147 * Data types for S-expressions:: Data types related with S-expressions.
148 * Working with S-expressions:: How to work with S-expressions.
151 * Data types:: MPI related data types.
152 * Basic functions:: First steps with MPI numbers.
153 * MPI formats:: External representation of MPIs.
154 * Calculations:: Performing MPI calculations.
155 * Comparisons:: How to compare MPI values.
156 * Bit manipulations:: How to access single bits of MPI values.
157 * Miscellaneous:: Miscellaneous MPI functions.
160 * Generation:: Generation of new prime numbers.
161 * Checking:: Checking if a given number is prime.
164 * Memory allocation:: Functions related with memory allocation.
172 @c **********************************************************
173 @c ******************* Introduction ***********************
174 @c **********************************************************
176 @chapter Introduction
177 `@acronym{Libgcrypt}' is a library providing cryptographic building blocks.
180 * Getting Started:: How to use this manual.
181 * Features:: A glance at @acronym{Libgcrypt}'s features.
182 * Overview:: Overview about the library.
185 @node Getting Started
186 @section Getting Started
188 This manual documents the `@acronym{Libgcrypt}' library application programming
189 interface (API). All functions and data types provided by the library
193 The reader is assumed to possess basic knowledge about applied
196 This manual can be used in several ways. If read from the beginning
197 to the end, it gives a good introduction into the library and how it
198 can be used in an application. Forward references are included where
199 necessary. Later on, the manual can be used as a reference manual to
200 get just the information needed about any particular interface of the
201 library. Experienced programmers might want to start looking at the
202 examples at the end of the manual, and then only read up those parts
203 of the interface which are unclear.
209 `Libgcrypt' might have a couple of advantages over other libraries doing
213 @item It's Free Software
214 Anybody can use, modify, and redistribute it under the terms of the GNU
215 Lesser General Public License (@pxref{Library Copying}). Note, that
216 some parts (which are not needed on a GNU or GNU/Linux system) are
217 subject to the terms of the GNU General Public License
218 (@pxref{Copying}); please see the README file of the distribution for of
221 @item It encapsulates the low level cryptography
222 `@acronym{Libgcrypt}' provides a high level interface to cryptographic building
223 blocks using an extendable and flexible API.
231 The `@acronym{Libgcrypt}' library is fully thread-safe, where it makes
232 sense to be thread-safe. An exception for thread-safety are some
233 cryptographic functions that modify a certain context stored in
234 handles. If the user really intents to use such functions from
235 different threads on the same handle, he has to take care of the
236 serialization of such functions himself. If not described otherwise,
237 every function is thread-safe.
239 @acronym{Libgcrypt} depends on the library `libgpg-error', which
240 contains common error handling related code for GnuPG components.
242 @c **********************************************************
243 @c ******************* Preparation ************************
244 @c **********************************************************
248 To use `@acronym{Libgcrypt}', you have to perform some changes to your
249 sources and the build system. The necessary changes are small and
250 explained in the following sections. At the end of this chapter, it
251 is described how the library is initialized, and how the requirements
252 of the library are verified.
255 * Header:: What header file you need to include.
256 * Building sources:: How to build sources using the library.
257 * Building sources using Automake:: How to build sources with the help of Automake.
258 * Initializing the library:: How to initialize the library.
259 * Multi Threading:: How @acronym{Libgcrypt} can be used in a MT environment.
266 All interfaces (data types and functions) of the library are defined
267 in the header file `gcrypt.h'. You must include this in all source
268 files using the library, either directly or through some other header
275 The name space of `@acronym{Libgcrypt}' is @code{gcry_*} for function
276 and type names and @code{GCRY*} for other symbols. In addition the
277 same name prefixes with one prepended underscore are reserved for
278 internal use and should never be used by an application. Furthermore
279 `libgpg-error' defines functions prefixed with `gpg_' and preprocessor
280 symbols prefixed with `GPG_'. Note that @acronym{Libgcrypt} uses
281 libgpg-error, which uses @code{gpg_err_*} as name space for function
282 and type names and @code{GPG_ERR_*} for other symbols, including all
285 @node Building sources
286 @section Building sources
288 If you want to compile a source file including the `gcrypt.h' header
289 file, you must make sure that the compiler can find it in the
290 directory hierarchy. This is accomplished by adding the path to the
291 directory in which the header file is located to the compilers include
292 file search path (via the @option{-I} option).
294 However, the path to the include file is determined at the time the
295 source is configured. To solve this problem, `@acronym{Libgcrypt}' ships with a small
296 helper program @command{libgcrypt-config} that knows the path to the
297 include file and other configuration options. The options that need
298 to be added to the compiler invocation at compile time are output by
299 the @option{--cflags} option to @command{libgcrypt-config}. The following
300 example shows how it can be used at the command line:
303 gcc -c foo.c `libgcrypt-config --cflags`
306 Adding the output of @samp{libgcrypt-config --cflags} to the compilers
307 command line will ensure that the compiler can find the `@acronym{Libgcrypt}' header
310 A similar problem occurs when linking the program with the library.
311 Again, the compiler has to find the library files. For this to work,
312 the path to the library files has to be added to the library search path
313 (via the @option{-L} option). For this, the option @option{--libs} to
314 @command{libgcrypt-config} can be used. For convenience, this option
315 also outputs all other options that are required to link the program
316 with the `@acronym{Libgcrypt}' libraries (in particular, the @samp{-lgcrypt}
317 option). The example shows how to link @file{foo.o} with the `@acronym{Libgcrypt}'
318 library to a program @command{foo}.
321 gcc -o foo foo.o `libgcrypt-config --libs`
324 Of course you can also combine both examples to a single command by
325 specifying both options to @command{libgcrypt-config}:
328 gcc -o foo foo.c `libgcrypt-config --cflags --libs`
331 @node Building sources using Automake
332 @section Building sources using Automake
334 It is much easier if you use GNU Automake instead of writing your own
335 Makefiles. If you do that you do not have to worry about finding and
336 invoking the @command{libgcrypt-config} script at all.
337 @acronym{Libgcrypt} provides an extension to Automake that does all
340 @c A simple macro for optional variables.
342 @r{[}@var{\varname\}@r{]}
344 @defmac AM_PATH_LIBGCRYPT (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found})
345 Check whether @acronym{Libgcrypt} (at least version
346 @var{minimum-version}, if given) exists on the host system. If it is
347 found, execute @var{action-if-found}, otherwise do
348 @var{action-if-not-found}, if given.
350 Additionally, the function defines @code{LIBGCRYPT_CFLAGS} to the
351 flags needed for compilation of the program to find the
352 @file{gcrypt.h} header file, and @code{LIBGCRYPT_LIBS} to the linker
353 flags needed to link the program to the @acronym{Libgcrypt} library.
356 You can use the defined Autoconf variables like this in your
360 AM_CPPFLAGS = $(LIBGCRYPT_CFLAGS)
361 LDADD = $(LIBGCRYPT_LIBS)
364 @node Initializing the library
365 @section Initializing the library
367 It is often desirable to check that the version of `@acronym{Libgcrypt}' used is
368 indeed one which fits all requirements. Even with binary compatibility
369 new features may have been introduced but due to problem with the
370 dynamic linker an old version is actually used. So you may want to
371 check that the version is okay right after program startup.
373 @deftypefun const char *gcry_check_version (const char *@var{req_version})
375 The function @code{gcry_check_version} has three purposes. It can be
376 used to retrieve the version number of the library. In addition it
377 can verify that the version number is higher than a certain required
380 In either case, the function initializes some sub-systems, and for
381 this reason alone it must be invoked early in your program, before you
382 make use of the other functions of @acronym{Libgcrypt}.
385 @node Multi Threading
386 @section Multi Threading
388 As mentioned earlier, the `@acronym{Libgcrypt}' library is
389 thread-safe if you adhere to the following requirements:
393 If your application is multi-threaded, you must set the thread support
394 callbacks with the @code{GCRYCTL_SET_THREAD_CBS} command
395 @strong{before} any other function in the library.
397 This is easy enough if you are indeed writing an application using
398 Libgcrypt. It is rather problematic if you are writing a library
399 instead. Here are some tips what to do if you are writing a library:
401 If your library requires a certain thread package, just initialize
402 Libgcrypt to use this thread package. If your library supports multiple
403 thread packages, but needs to be configured, you will have to
404 implement a way to determine which thread package the application
405 wants to use with your library anyway. Then configure Libgcrypt to use
408 If your library is fully reentrant without any special support by a
409 thread package, then you are lucky indeed. Unfortunately, this does
410 not relieve you from doing either of the two above, or use a third
411 option. The third option is to let the application initialize Libgcrypt
412 for you. Then you are not using Libgcrypt transparently, though.
414 As if this was not difficult enough, a conflict may arise if two
415 libraries try to initialize Libgcrypt independently of each others, and
416 both such libraries are then linked into the same application. To
417 make it a bit simpler for you, this will probably work, but only if
418 both libraries have the same requirement for the thread package. This
419 is currently only supported for the non-threaded case, GNU Pth and
420 pthread. Support for more thread packages is easy to add, so contact
421 us if you require it.
424 The function @code{gcry_check_version} must be called before any other
425 function in the library, except the @code{GCRYCTL_SET_THREAD_CBS}
426 command (called via the @code{gcry_control} function), because it
427 initializes the thread support subsystem in @acronym{Libgcrypt}. To
428 achieve this in multi-threaded programs, you must synchronize the
429 memory with respect to other threads that also want to use
430 @acronym{Libgcrypt}. For this, it is sufficient to call
431 @code{gcry_check_version} before creating the other threads using
432 @acronym{Libgcrypt}@footnote{At least this is true for POSIX threads,
433 as @code{pthread_create} is a function that synchronizes memory with
434 respects to other threads. There are many functions which have this
435 property, a complete list can be found in POSIX, IEEE Std 1003.1-2003,
436 Base Definitions, Issue 6, in the definition of the term ``Memory
437 Synchronization''. For other thread packages, more relaxed or more
438 strict rules may apply.}.
442 As with the function @code{gpg_strerror}, @code{gcry_strerror} is not
443 thread safe. You have to use @code{gpg_strerror_r} instead.
447 @acronym{Libgcrypt} contains convenient macros, which define the
448 necessary thread callbacks for PThread and for GNU Pth:
451 @item GCRY_THREAD_OPTION_PTH_IMPL
453 This macro defines the following (static) symbols: gcry_pth_init,
454 gcry_pth_mutex_init, gcry_pth_mutex_destroy, gcry_pth_mutex_lock,
455 gcry_pth_mutex_unlock, gcry_pth_read, gcry_pth_write, gcry_pth_select,
456 gcry_pth_waitpid, gcry_pth_accept, gcry_pth_connect, gcry_threads_pth.
458 After including this macro, gcry_control() shall be used with a
459 command of GCRYCTL_SET_THREAD_CBS in order to register the thread
460 callback structure named ``gcry_threads_pth''.
462 @item GCRY_THREAD_OPTION_PTHREAD_IMPL
464 This macro defines the following (static) symbols:
465 gcry_pthread_mutex_init, gcry_pthread_mutex_destroy, gcry_mutex_lock,
466 gcry_mutex_unlock, gcry_threads_pthread.
468 After including this macro, gcry_control() shall be used with a
469 command of GCRYCTL_SET_THREAD_CBS in order to register the thread
470 callback structure named ``gcry_threads_pthread''.
473 Note that these macros need to be terminated with a semicolon. Keep
474 in mind that these are convenient macros for C programmers; C++
475 programmers might have to wrap these macros in an ``extern C'' body.
477 @c **********************************************************
478 @c ******************* General ****************************
479 @c **********************************************************
481 @chapter Generalities
484 * Controlling the library:: Controlling @acronym{Libgcrypt}'s behavior.
485 * Modules:: Description of extension modules.
486 * Error Handling:: Error codes and such.
489 @node Controlling the library
490 @section Controlling the library
492 @deftypefun gcry_error_t gcry_control (enum gcry_ctl_cmds @var{cmd}, ...)
494 This function can be used to influence the general behavior of
495 @acronym{Libgcrypt} in several ways. Depending on @var{cmd}, more
496 arguments can or have to be provided.
499 @item GCRYCTL_ENABLE_M_GUARD; Arguments: none
500 This command enables the built-in memory guard. It must not be used to
501 activate the memory guard after the memory management has already been
502 used; therefore it can ONLY be used at initialization time. Note that
503 the memory guard is NOT used when the user of the library has set his
504 own memory management callbacks.
506 @item GCRYCTL_ENABLE_QUICK_RANDOM; Arguments: none
507 This command activates the use of a highly-insecure, but fast PRNG. It
508 can only be used at initialization time - FIXME: is this correct?
510 @item GCRYCTL_DUMP_RANDOM_STATS
511 This command dumps PRNG related statistics to the librarys logging
514 @item GCRYCTL_DUMP_MEMORY_STATS
515 This command dumps memory manamgent related statistics to the librarys
518 @item GCRYCTL_DUMP_SECMEM_STATS
519 This command dumps secure memory manamgent related statistics to the
520 librarys logging stream.
522 @item GCRYCTL_DROP_PRIVS
523 This command disables the use of secure memory and drops the priviliges
524 of the current process. FIXME.
526 @item GCRYCTL_DISABLE_SECMEM
527 This command disables the use of secure memory. FIXME.
529 @item GCRYCTL_INIT_SECMEM
530 @item GCRYCTL_TERM_SECMEM
531 @item GCRYCTL_DISABLE_SECMEM_WARN
532 @item GCRYCTL_SUSPEND_SECMEM_WARN
533 @item GCRYCTL_RESUME_SECMEM_WARN
535 @item GCRYCTL_USE_SECURE_RNDPOOL; Arguments: none
537 This command tells the PRNG to store random numbers in secure memory.
538 FIXME: what about initialization time?
540 @item GCRYCTL_SET_RANDOM_SEED_FILE; Arguments: const char *filename
542 This command specifies the file, which is to be used as seed file for
543 the PRNG. If the seed file is registered prior to initialization of the
544 PRNG, the seed file's content (if it exists and seems to be valid) is
545 feed into the PRNG pool. After the seed file has been registered, the
546 PRNG can be signalled to write out the PRNG pool's content into the seed
547 file with the following command.
549 @item GCRYCTL_UPDATE_RANDOM_SEED_FILE; Arguments: none
551 Write out the PRNG pool's content into the registered seed file.
553 @item GCRYCTL_SET_VERBOSITY
556 @item GCRYCTL_SET_DEBUG_FLAGS
557 @item GCRYCTL_CLEAR_DEBUG_FLAGS
558 @item GCRYCTL_DISABLE_INTERNAL_LOCKING
559 @item GCRYCTL_ANY_INITIALIZATION_P
560 @item GCRYCTL_INITIALIZATION_FINISHED_P
561 @item GCRYCTL_INITIALIZATION_FINISHED
563 @item GCRYCTL_SET_THREAD_CBS; Arguments: struct ath_ops *ath_ops
565 This command registers a thread-callback structure. See section ``multi
566 threading'' for more information on this command.
568 @item GCRYCTL_FAST_POOL
576 @acronym{Libgcrypt} supports the use of `extension modules', which
577 implement algorithms in addition to those already built into the library
580 @deftp {Data type} gcry_module_t
581 This data type represents a `module'.
584 Functions registering modules provided by the user take a `module
585 specification structure' as input and return a value of
586 @code{gcry_module_t} and an ID that is unique in the modules'
587 category. This ID can be used to reference the newly registered
588 module. After registering a module successfully, the new functionality
589 should be able to be used through the normal functions provided by
590 @acronym{Libgcrypt} until it is unregistered again.
592 @c **********************************************************
593 @c ******************* Errors ****************************
594 @c **********************************************************
596 @section Error Handling
598 Many functions in @acronym{Libgcrypt} can return an error if they
599 fail. For this reason, the application should always catch the error
600 condition and take appropriate measures, for example by releasing the
601 resources and passing the error up to the caller, or by displaying a
602 descriptive message to the user and cancelling the operation.
604 Some error values do not indicate a system error or an error in the
605 operation, but the result of an operation that failed properly. For
606 example, if you try to decrypt a tempered message, the decryption will
607 fail. Another error value actually means that the end of a data
608 buffer or list has been reached. The following descriptions explain
609 for many error codes what they mean usually. Some error values have
610 specific meanings if returned by a certain functions. Such cases are
611 described in the documentation of those functions.
613 @acronym{Libgcrypt} uses the @code{libgpg-error} library. This allows
614 to share the error codes with other components of the GnuPG system,
615 and thus pass error values transparently from the crypto engine, or
616 some helper application of the crypto engine, to the user. This way
617 no information is lost. As a consequence, @acronym{Libgcrypt} does
618 not use its own identifiers for error codes, but uses those provided
619 by @code{libgpg-error}. They usually start with @code{GPG_ERR_}.
621 However, @acronym{Libgcrypt} does provide aliases for the functions
622 defined in libgpg-error, which might be preferred for name space
626 Most functions in @acronym{Libgcrypt} return an error code in the case
627 of failure. For this reason, the application should always catch the
628 error condition and take appropriate measures, for example by
629 releasing the resources and passing the error up to the caller, or by
630 displaying a descriptive message to the user and canceling the
633 Some error values do not indicate a system error or an error in the
634 operation, but the result of an operation that failed properly.
636 GnuPG components, including Libgcrypt, use an extra library named
637 libgpg-error to provide a common error handling scheme. For more
638 information on libgpg-error, see the according manual.
641 * Error Values:: The error value and what it means.
642 * Error Sources:: A list of important error sources.
643 * Error Codes:: A list of important error codes.
644 * Error Strings:: How to get a descriptive string from a value.
649 @subsection Error Values
652 @cindex error sources
654 @deftp {Data type} {gcry_err_code_t}
655 The @code{gcry_err_code_t} type is an alias for the
656 @code{libgpg-error} type @code{gpg_err_code_t}. The error code
657 indicates the type of an error, or the reason why an operation failed.
659 A list of important error codes can be found in the next section.
662 @deftp {Data type} {gcry_err_source_t}
663 The @code{gcry_err_source_t} type is an alias for the
664 @code{libgpg-error} type @code{gpg_err_source_t}. The error source
665 has not a precisely defined meaning. Sometimes it is the place where
666 the error happened, sometimes it is the place where an error was
667 encoded into an error value. Usually the error source will give an
668 indication to where to look for the problem. This is not always true,
669 but it is attempted to achieve this goal.
671 A list of important error sources can be found in the next section.
674 @deftp {Data type} {gcry_error_t}
675 The @code{gcry_error_t} type is an alias for the @code{libgpg-error}
676 type @code{gpg_error_t}. An error value like this has always two
677 components, an error code and an error source. Both together form the
680 Thus, the error value can not be directly compared against an error
681 code, but the accessor functions described below must be used.
682 However, it is guaranteed that only 0 is used to indicate success
683 (@code{GPG_ERR_NO_ERROR}), and that in this case all other parts of
684 the error value are set to 0, too.
686 Note that in @acronym{Libgcrypt}, the error source is used purely for
687 diagnostic purposes. Only the error code should be checked to test
688 for a certain outcome of a function. The manual only documents the
689 error code part of an error value. The error source is left
690 unspecified and might be anything.
693 @deftypefun {gcry_err_code_t} gcry_err_code (@w{gcry_error_t @var{err}})
694 The static inline function @code{gcry_err_code} returns the
695 @code{gcry_err_code_t} component of the error value @var{err}. This
696 function must be used to extract the error code from an error value in
697 order to compare it with the @code{GPG_ERR_*} error code macros.
700 @deftypefun {gcry_err_source_t} gcry_err_source (@w{gcry_error_t @var{err}})
701 The static inline function @code{gcry_err_source} returns the
702 @code{gcry_err_source_t} component of the error value @var{err}. This
703 function must be used to extract the error source from an error value in
704 order to compare it with the @code{GPG_ERR_SOURCE_*} error source macros.
707 @deftypefun {gcry_error_t} gcry_err_make (@w{gcry_err_source_t @var{source}}, @w{gcry_err_code_t @var{code}})
708 The static inline function @code{gcry_err_make} returns the error
709 value consisting of the error source @var{source} and the error code
712 This function can be used in callback functions to construct an error
713 value to return it to the library.
716 @deftypefun {gcry_error_t} gcry_error (@w{gcry_err_code_t @var{code}})
717 The static inline function @code{gcry_error} returns the error value
718 consisting of the default error source and the error code @var{code}.
720 For @acronym{GCRY} applications, the default error source is
721 @code{GPG_ERR_SOURCE_USER_1}. You can define
722 @code{GCRY_ERR_SOURCE_DEFAULT} before including @file{gcrypt.h} to
725 This function can be used in callback functions to construct an error
726 value to return it to the library.
729 The @code{libgpg-error} library provides error codes for all system
730 error numbers it knows about. If @var{err} is an unknown error
731 number, the error code @code{GPG_ERR_UNKNOWN_ERRNO} is used. The
732 following functions can be used to construct error values from system
735 @deftypefun {gcry_error_t} gcry_err_make_from_errno (@w{gcry_err_source_t @var{source}}, @w{int @var{err}})
736 The function @code{gcry_err_make_from_errno} is like
737 @code{gcry_err_make}, but it takes a system error like @code{errno}
738 instead of a @code{gcry_err_code_t} error code.
741 @deftypefun {gcry_error_t} gcry_error_from_errno (@w{int @var{err}})
742 The function @code{gcry_error_from_errno} is like @code{gcry_error},
743 but it takes a system error like @code{errno} instead of a
744 @code{gcry_err_code_t} error code.
747 Sometimes you might want to map system error numbers to error codes
748 directly, or map an error code representing a system error back to the
749 system error number. The following functions can be used to do that.
751 @deftypefun {gcry_err_code_t} gcry_err_code_from_errno (@w{int @var{err}})
752 The function @code{gcry_err_code_from_errno} returns the error code
753 for the system error @var{err}. If @var{err} is not a known system
754 error, the function returns @code{GPG_ERR_UNKNOWN_ERRNO}.
757 @deftypefun {int} gcry_err_code_to_errno (@w{gcry_err_code_t @var{err}})
758 The function @code{gcry_err_code_to_errno} returns the system error
759 for the error code @var{err}. If @var{err} is not an error code
760 representing a system error, or if this system error is not defined on
761 this system, the function returns @code{0}.
766 @subsection Error Sources
767 @cindex error codes, list of
769 The library @code{libgpg-error} defines an error source for every
770 component of the GnuPG system. The error source part of an error
771 value is not well defined. As such it is mainly useful to improve the
772 diagnostic error message for the user.
774 If the error code part of an error value is @code{0}, the whole error
775 value will be @code{0}. In this case the error source part is of
776 course @code{GPG_ERR_SOURCE_UNKNOWN}.
778 The list of error sources that might occur in applications using
779 @acronym{Libgctypt} is:
782 @item GPG_ERR_SOURCE_UNKNOWN
783 The error source is not known. The value of this error source is
786 @item GPG_ERR_SOURCE_GPGME
787 The error source is @acronym{GPGME} itself.
789 @item GPG_ERR_SOURCE_GPG
790 The error source is GnuPG, which is the crypto engine used for the
793 @item GPG_ERR_SOURCE_GPGSM
794 The error source is GPGSM, which is the crypto engine used for the
797 @item GPG_ERR_SOURCE_GCRYPT
798 The error source is @code{libgcrypt}, which is used by crypto engines
799 to perform cryptographic operations.
801 @item GPG_ERR_SOURCE_GPGAGENT
802 The error source is @command{gpg-agent}, which is used by crypto
803 engines to perform operations with the secret key.
805 @item GPG_ERR_SOURCE_PINENTRY
806 The error source is @command{pinentry}, which is used by
807 @command{gpg-agent} to query the passphrase to unlock a secret key.
809 @item GPG_ERR_SOURCE_SCD
810 The error source is the SmartCard Daemon, which is used by
811 @command{gpg-agent} to delegate operations with the secret key to a
814 @item GPG_ERR_SOURCE_KEYBOX
815 The error source is @code{libkbx}, a library used by the crypto
816 engines to manage local keyrings.
818 @item GPG_ERR_SOURCE_USER_1
819 @item GPG_ERR_SOURCE_USER_2
820 @item GPG_ERR_SOURCE_USER_3
821 @item GPG_ERR_SOURCE_USER_4
822 These error sources are not used by any GnuPG component and can be
823 used by other software. For example, applications using
824 @acronym{Libgcrypt} can use them to mark error values coming from callback
825 handlers. Thus @code{GPG_ERR_SOURCE_USER_1} is the default for errors
826 created with @code{gcry_error} and @code{gcry_error_from_errno},
827 unless you define @code{GCRY_ERR_SOURCE_DEFAULT} before including
833 @subsection Error Codes
834 @cindex error codes, list of
836 The library @code{libgpg-error} defines many error values. The
837 following list includes the most important error codes.
841 This value indicates the end of a list, buffer or file.
843 @item GPG_ERR_NO_ERROR
844 This value indicates success. The value of this error code is
845 @code{0}. Also, it is guaranteed that an error value made from the
846 error code @code{0} will be @code{0} itself (as a whole). This means
847 that the error source information is lost for this error code,
848 however, as this error code indicates that no error occurred, this is
849 generally not a problem.
851 @item GPG_ERR_GENERAL
852 This value means that something went wrong, but either there is not
853 enough information about the problem to return a more useful error
854 value, or there is no separate error value for this type of problem.
857 This value means that an out-of-memory condition occurred.
860 System errors are mapped to GPG_ERR_EFOO where FOO is the symbol for
863 @item GPG_ERR_INV_VALUE
864 This value means that some user provided data was out of range.
866 @item GPG_ERR_UNUSABLE_PUBKEY
867 This value means that some recipients for a message were invalid.
869 @item GPG_ERR_UNUSABLE_SECKEY
870 This value means that some signers were invalid.
872 @item GPG_ERR_NO_DATA
873 This value means that data was expected where no data was found.
875 @item GPG_ERR_CONFLICT
876 This value means that a conflict of some sort occurred.
878 @item GPG_ERR_NOT_IMPLEMENTED
879 This value indicates that the specific function (or operation) is not
880 implemented. This error should never happen. It can only occur if
881 you use certain values or configuration options which do not work,
882 but for which we think that they should work at some later time.
884 @item GPG_ERR_DECRYPT_FAILED
885 This value indicates that a decryption operation was unsuccessful.
887 @item GPG_ERR_WRONG_KEY_USAGE
888 This value indicates that a key is not used appropriately.
890 @item GPG_ERR_NO_SECKEY
891 This value indicates that no secret key for the user ID is available.
893 @item GPG_ERR_UNSUPPORTED_ALGORITHM
894 This value means a verification failed because the cryptographic
895 algorithm is not supported by the crypto backend.
897 @item GPG_ERR_BAD_SIGNATURE
898 This value means a verification failed because the signature is bad.
900 @item GPG_ERR_NO_PUBKEY
901 This value means a verification failed because the public key is not
907 @item GPG_ERR_USER_16
908 These error codes are not used by any GnuPG component and can be
909 freely used by other software. Applications using @acronym{Libgcrypt}
910 might use them to mark specific errors returned by callback handlers
911 if no suitable error codes (including the system errors) for these
912 errors exist already.
917 @subsection Error Strings
918 @cindex error values, printing of
919 @cindex error codes, printing of
920 @cindex error sources, printing of
921 @cindex error strings
923 @deftypefun {const char *} gcry_strerror (@w{gcry_error_t @var{err}})
924 The function @code{gcry_strerror} returns a pointer to a statically
925 allocated string containing a description of the error code contained
926 in the error value @var{err}. This string can be used to output a
927 diagnostic message to the user.
931 @deftypefun {const char *} gcry_strsource (@w{gcry_error_t @var{err}})
932 The function @code{gcry_strerror} returns a pointer to a statically
933 allocated string containing a description of the error source
934 contained in the error value @var{err}. This string can be used to
935 output a diagnostic message to the user.
938 The following example illustrates the use of the functions described
943 gcry_cipher_hd_t handle;
944 gcry_error_t err = 0;
946 err = gcry_cipher_open (&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 0);
949 fprintf (stderr, "Failure: %s/%s\n",
950 gcry_strsource (err),
951 gcry_strerror (err));
956 @c **********************************************************
957 @c ******************* General ****************************
958 @c **********************************************************
959 @node Handler Functions
960 @chapter Handler Functions
962 @acronym{Libgcrypt} makes it possible to install so called `handler functions',
963 which get called by @acronym{Libgcrypt} in case of certain events.
966 * Progress handler:: Using a progress handler function.
967 * Allocation handler:: Using special memory allocation functions.
968 * Error handler:: Using error handler functions.
969 * Logging handler:: Using a special logging function.
972 @node Progress handler
973 @section Progress handler
975 It is often useful to retrieve some feedback while long running
976 operations are performed.
978 @deftp {Data type} gcry_handler_progress_t
979 Progress handler functions have to be of the type
980 @code{gcry_handler_progress_t}, which is defined as:
982 @code{void (*gcry_handler_progress_t) (void *, const char *, int, int, int)}
985 The following function may be used to register a handler function for
988 @deftypefun void gcry_set_progress_handler (gcry_handler_progress_t @var{cb}, void *@var{cb_data})
990 This function installs @var{cb} as the `Progress handler' function.
991 @var{cb} must be defined as follows:
995 my_progress_handler (void *@var{cb_data}, const char *@var{what},
996 int @var{printchar}, int @var{current}, int @var{total})
1002 A description of the arguments of the progress handler function follows.
1006 The argument provided in the call to @code{gcry_set_progress_handler}.
1008 A string identifying the type of the progress output. The following
1009 values for @var{what} are defined:
1013 Not enough entropy is available. @var{total} holds the number of
1017 Values for @var{printchar}:
1022 Need to refresh the pool of prime numbers.
1024 Number of bits adjusted.
1026 Searching for a generator.
1028 Fermat test on 10 candidates failed.
1030 Restart with a new random value.
1032 Rabin Miller test passed.
1040 @node Allocation handler
1041 @section Allocation handler
1043 It is possible to make @acronym{Libgcrypt} use special memory
1044 allocation functions instead of the built-in ones.
1046 Memory allocation functions are of the following types:
1047 @deftp {Data type} gcry_handler_alloc_t
1048 This type is defined as: @code{void *(*gcry_handler_alloc_t) (size_t n)}.
1050 @deftp {Data type} gcry_handler_secure_check_t
1051 This type is defined as: @code{int *(*gcry_handler_secure_check_t) (const void *)}.
1053 @deftp {Data type} gcry_handler_realloc_t
1054 This type is defined as: @code{void *(*gcry_handler_realloc_t) (void *p, size_t n)}.
1056 @deftp {Data type} gcry_handler_free_t
1057 This type is defined as: @code{void *(*gcry_handler_free_t) (void *)}.
1060 Special memory allocation functions can be installed with the
1063 @deftypefun void gcry_set_allocation_handler (gcry_handler_alloc_t @var{func_alloc}, gcry_handler_alloc_t @var{func_alloc_secure}, gcry_handler_secure_check_t @var{func_secure_check}, gcry_handler_realloc_t @var{func_realloc}, gcry_handler_free_t @var{func_free})
1064 Install the provided functions and use them instead of the built-in
1065 functions for doing memory allocation.
1069 @section Error handler
1071 The following functions may be used to register handler functions that
1072 are called by @acronym{Libgcrypt} in case certain error conditions
1075 @deftp {Data type} gcry_handler_no_mem_t
1076 This type is defined as: @code{void (*gcry_handler_no_mem_t) (void *, size_t, unsigned int)}
1078 @deftypefun void gcry_set_outofcore_handler (gcry_handler_no_mem_t @var{func_no_mem}, void *@var{cb_data})
1079 This function registers @var{func_no_mem} as `out-of-core handler',
1080 which means that it will be called in the case of not having enough
1084 @deftp {Data type} gcry_handler_error_t
1085 This type is defined as: @code{void (*gcry_handler_error_t) (void *, int, const char *)}
1088 @deftypefun void gcry_set_fatalerror_handler (gcry_handler_error_t @var{func_error}, void *@var{cb_data})
1089 This function registers @var{func_error} as `error handler',
1090 which means that it will be called in error conditions.
1093 @node Logging handler
1094 @section Logging handler
1096 @deftp {Data type} gcry_handler_log_t
1097 This type is defined as: @code{void (*gcry_handler_log_t) (void *, int, const char *, va_list)}
1100 @deftypefun void gcry_set_log_handler (gcry_handler_log_t @var{func_log}, void *@var{cb_data})
1101 This function registers @var{func_log} as `logging handler', which
1102 means that it will be called in case @acronym{Libgcrypt} wants to log
1106 @c **********************************************************
1107 @c ******************* Ciphers ****************************
1108 @c **********************************************************
1109 @c @include cipher-ref.texi
1110 @node Symmetric cryptography
1111 @chapter Symmetric cryptography
1113 The cipher functions are used for symmetrical cryptography,
1114 i.e. cryptography using a shared key. The programming model follows
1115 an open/process/close paradigm and is in that similar to other
1116 building blocks provided by @acronym{Libgcrypt}.
1119 * Available ciphers:: List of ciphers supported by the library.
1120 * Cipher modules:: How to work with cipher modules.
1121 * Available cipher modes:: List of cipher modes supported by the library.
1122 * Working with cipher handles:: How to perform operations related to cipher handles.
1123 * General cipher functions:: General cipher functions independent of cipher handles.
1126 @node Available ciphers
1127 @section Available ciphers
1130 @item GCRY_CIPHER_NONE
1131 This is not a real algorithm but used by some functions as error return.
1132 The value always evaluates to false.
1134 @item GCRY_CIPHER_IDEA
1135 This is the IDEA algorithm. The constant is provided but there is
1136 currently no implementation for it because the algorithm is patented.
1138 @item GCRY_CIPHER_3DES
1139 Triple-DES with 3 Keys as EDE. The key size of this algorithm is 168 but
1140 you have to pass 192 bits because the most significant bits of each byte
1143 @item GCRY_CIPHER_CAST5
1144 CAST128-5 block cipher algorithm. The key size is 128 bits.
1146 @item GCRY_CIPHER_BLOWFISH
1147 The blowfish algorithm. The current implementation allows only for a key
1150 @item GCRY_CIPHER_SAFER_SK128
1151 Reserved and not currently implemented.
1153 @item GCRY_CIPHER_DES_SK
1154 Reserved and not currently implemented.
1156 @item GCRY_CIPHER_AES
1157 @itemx GCRY_CIPHER_AES128
1158 @itemx GCRY_CIPHER_RIJNDAEL
1159 @itemx GCRY_CIPHER_RIJNDAEL128
1160 AES (Rijndael) with a 128 bit key.
1162 @item GCRY_CIPHER_AES192
1163 @itemx GCRY_CIPHER_RIJNDAEL192
1164 AES (Rijndael) with a 192 bit key.
1166 @item GCRY_CIPHER_AES256
1167 @itemx GCRY_CIPHER_RIJNDAEL256
1168 AES (Rijndael) with a 256 bit key.
1170 @item GCRY_CIPHER_TWOFISH
1171 The Twofish algorithm with a 256 bit key.
1173 @item GCRY_CIPHER_TWOFISH128
1174 The Twofish algorithm with a 128 bit key.
1176 @item GCRY_CIPHER_ARCFOUR
1177 An algorithm which is 100% compatible with RSA Inc.'s RC4 algorithm.
1178 Note that this is a stream cipher and must be used very carefully to
1179 avoid a couple of weaknesses.
1181 @item GCRY_CIPHER_DES
1182 Standard DES with a 56 bit key. You need to pass 64 bit but the high
1183 bits of each byte are ignored. Note, that this is a weak algorithm
1184 which can be broken in reasonable time using a brute force approach.
1188 @node Cipher modules
1189 @section Cipher modules
1191 @acronym{Libgcrypt} makes it possible to load additional `cipher
1192 modules'; these cipher can be used just like the cipher algorithms
1193 that are built into the library directly. For an introduction into
1194 extension modules, see @xref{Modules}.
1196 @deftp {Data type} gcry_cipher_spec_t
1197 This is the `module specification structure' needed for registering
1198 cipher modules, which has to be filled in by the user before it can be
1199 used to register a module. It contains the following members:
1202 @item const char *name
1203 The primary name of the algorithm.
1204 @item const char **aliases
1205 A list of strings that are `aliases' for the algorithm. The list must
1206 be terminated with a NULL element.
1207 @item gcry_cipher_oid_spec_t *oids
1208 A list of OIDs that are to be associated with the algorithm. The
1209 list's last element must have it's `oid' member set to NULL. See
1210 below for an explanation of this type.
1211 @item size_t blocksize
1212 The block size of the algorithm, in bytes.
1214 The length of the key, in bits.
1215 @item size_t contextsize
1216 The size of the algorithm-specific `context', that should be allocated
1218 @item gcry_cipher_setkey_t setkey
1219 The function responsible for initializing a handle with a provided
1220 key. See below for a description of this type.
1221 @item gcry_cipher_encrypt_t encrypt
1222 The function responsible for encrypting a single block. See below for
1223 a description of this type.
1224 @item gcry_cipher_decrypt_t decrypt
1225 The function responsible for decrypting a single block. See below for
1226 a description of this type.
1227 @item gcry_cipher_stencrypt_t stencrypt
1228 Like `encrypt', for stream ciphers. See below for a description of
1230 @item gcry_cipher_stdecrypt_t stdecrypt
1231 Like `decrypt', for stream ciphers. See below for a description of
1236 @deftp {Data type} gcry_cipher_oid_spec_t
1237 This type is used for associating a user-provided algorithm
1238 implementation with certain OIDs. It contains the following members:
1240 @item const char *oid
1241 Textual representation of the OID.
1243 Cipher mode for which this OID is valid.
1247 @deftp {Data type} gcry_cipher_setkey_t
1248 Type for the `setkey' function, defined as: gcry_err_code_t
1249 (*gcry_cipher_setkey_t) (void *c, const unsigned char *key, unsigned
1253 @deftp {Data type} gcry_cipher_encrypt_t
1254 Type for the `encrypt' function, defined as: gcry_err_code_t
1255 (*gcry_cipher_encrypt_t) (void *c, const unsigned char *outbuf, const
1256 unsigned char *inbuf)
1259 @deftp {Data type} gcry_cipher_decrypt_t
1260 Type for the `decrypt' function, defined as: gcry_err_code_t
1261 (*gcry_cipher_decrypt_t) (void *c, const unsigned char *outbuf, const
1262 unsigned char *inbuf)
1265 @deftp {Data type} gcry_cipher_stencrypt_t
1266 Type for the `stencrypt' function, defined as: gcry_err_code_t
1267 (*gcry_cipher_stencrypt_t) (void *c, const unsigned char *outbuf, const
1268 unsigned char *, unsigned int n)
1271 @deftp {Data type} gcry_cipher_stdecrypt_t
1272 Type for the `stdecrypt' function, defined as: gcry_err_code_t
1273 (*gcry_cipher_stdecrypt_t) (void *c, const unsigned char *outbuf, const
1274 unsigned char *, unsigned int n)
1277 @deftypefun gcry_error_t gcry_cipher_register (gcry_cipher_spec_t *@var{cipher}, unsigned int *algorithm_id, gcry_module_t *@var{module})
1279 Register a new cipher module whose specification can be found in
1280 @var{cipher}. On success, a new algorithm ID is stored in
1281 @var{algorithm_id} and a pointer representing this module is stored
1285 @deftypefun void gcry_cipher_unregister (gcry_module_t @var{module})
1286 Unregister the cipher identified by @var{module}, which must have been
1287 registered with gcry_cipher_register.
1290 @deftypefun gcry_error_t gcry_cipher_list (int *@var{list}, int *@var{list_length})
1291 Get a list consisting of the IDs of the loaded cipher modules. If
1292 @var{list} is zero, write the number of loaded cipher modules to
1293 @var{list_length} and return. If @var{list} is non-zero, the first
1294 *@var{list_length} algorithm IDs are stored in @var{list}, which must
1295 be of according size. In case there are less cipher modules than
1296 *@var{list_length}, *@var{list_length} is updated to the correct
1300 @node Available cipher modes
1301 @section Available cipher modes
1304 @item GCRY_CIPHER_MODE_NONE
1305 No mode specified, may be set later using other functions. The value
1306 of this constant is always 0.
1308 @item GCRY_CIPHER_MODE_ECB
1309 Electronic Codebook mode.
1311 @item GCRY_CIPHER_MODE_CFB
1312 Cipher Feedback mode.
1314 @item GCRY_CIPHER_MODE_CBC
1315 Cipher Block Chaining mode.
1317 @item GCRY_CIPHER_MODE_STREAM
1318 Stream mode, only to be used with stream cipher algorithms.
1320 @item GCRY_CIPHER_MODE_OFB
1321 Output Feedback mode.
1323 @item GCRY_CIPHER_MODE_CTR
1328 @node Working with cipher handles
1329 @section Working with cipher handles
1331 To use a cipher algorithm, you must first allocate an according
1332 handle. This is to be done using the open function:
1334 @deftypefun gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *@var{hd}, int @var{algo}, int @var{mode}, unsigned int @var{flags})
1336 This function creates the context handle required for most of the
1337 other cipher functions and returns a handle to it in `hd'. In case of
1338 an error, an according error code is returned.
1340 The ID of algorithm to use must be specified via @var{algo}. See
1341 @xref{Available ciphers}, for a list of supported ciphers and the
1342 according constants.
1344 Besides using the constants directly, the function
1345 @code{gcry_cipher_map_name} may be used to convert the textual name of
1346 an algorithm into the according numeric ID.
1348 The cipher mode to use must be specified via @var{mode}. See
1349 @xref{Available cipher modes}, for a list of supported cipher modes
1350 and the according constants. Note that some modes are incompatible
1351 with some algorithms - in particular, stream mode
1352 (GCRY_CIPHER_MODE_STREAM) only works with stream ciphers. Any block
1353 cipher mode (GCRY_CIPHER_MODE_ECB, GCRY_CIPHER_MODE_CBC,
1354 GCRY_CIPHER_MODE_CFB, GCRY_CIPHER_MODE_OFB or GCRY_CIPHER_MODE_CTR)
1355 will work with any block cipher algorithm.
1357 The third argument @var{flags} can either be passed as @code{0} or as
1358 the bit-wise OR of the following constants.
1361 @item GCRY_CIPHER_SECURE
1362 Make sure that all operations are allocated in secure memory. This is
1363 useful, when the key material is highly confidential.
1364 @item GCRY_CIPHER_ENABLE_SYNC
1365 This flag enables the CFB sync mode, which is a special feature of
1366 @acronym{Libgcrypt}'s CFB mode implementation to allow for OpenPGP's CFB variant.
1367 See @code{gcry_cipher_sync}.
1368 @item GCRY_CIPHER_CBC_CTS
1369 Enable cipher text stealing (CTS) for the CBC mode. Cannot be used
1370 simultaneous as GCRY_CIPHER_CBC_MAC. CTS mode makes it possible to
1371 transform data of almost arbitrary size (only limitation is that it
1372 must be greater than the algorithm's block size).
1373 @item GCRY_CIPHER_CBC_MAC
1374 Compute CBC-MAC keyed checksums. This is the same as CBC mode, but
1375 only output the last block. Cannot be used simultaneous as
1376 GCRY_CIPHER_CBC_CTS.
1380 Use the following function to release an existing handle:
1382 @deftypefun void gcry_cipher_close (gcry_cipher_hd_t @var{h})
1384 This function releases the context created by @code{gcry_cipher_open}.
1387 In order to use a handle for performing cryptographic operations, a
1388 `key' has to be set first:
1390 @deftypefun gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t @var{h}, void *@var{k}, size_t @var{l})
1392 Set the key @var{k} used for encryption or decryption in the context
1393 denoted by the handle @var{h}. The length @var{l} of the key @var{k}
1394 must match the required length of the algorithm set for this context or
1395 be in the allowed range for algorithms with variable key size. The
1396 function checks this and returns an error if there is a problem. A
1397 caller should always check for an error.
1399 Note, this is currently implemented as a macro but may be changed to a
1400 function in the future.
1403 Most crypto modes requires an initialization vector (IV), which
1404 usually is a non-secret random string acting as a kind of salt value.
1405 The CTR mode requires a counter, which is also similar to a salt
1406 value. To set the IV or CTR, use these functions:
1408 @deftypefun gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t @var{h}, void *@var{k}, size_t @var{l})
1410 Set the initialization vector used for encryption or decryption. The
1411 vector is passed as the buffer @var{K} of length @var{l} and copied to
1412 internal data structures. The function checks that the IV matches the
1413 requirement of the selected algorithm and mode. Note, that this is
1414 implemented as a macro.
1417 @deftypefun gcry_error_t gcry_cipher_setctr (gcry_cipher_hd_t @var{h}, void *@var{c}, size_t @var{l})
1419 Set the counter vector used for encryption or decryption. The counter
1420 is passed as the buffer @var{c} of length @var{l} and copied to
1421 internal data structures. The function checks that the counter
1422 matches the requirement of the selected algorithm (i.e., it must be
1423 the same size as the block size). Note, that this is implemented as a
1427 @deftypefun gcry_error_t gcry_cipher_reset (gcry_cipher_hd_t @var{h})
1429 Set the given handle's context back to the state it had after the last
1430 call to gcry_cipher_setkey and clear the initialization vector.
1432 Note, that gcry_cipher_reset is implemented as a macro.
1435 The actual encryption and decryption is done by using one of the
1436 following functions. They may be used as often as required to process
1439 @deftypefun gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t @var{h}, unsigned char *{out}, size_t @var{outsize}, const unsigned char *@var{in}, size_t @var{inlen})
1441 @code{gcry_cipher_encrypt} is used to encrypt the data. This function
1442 can either work in place or with two buffers. It uses the cipher
1443 context already setup and described by the handle @var{h}. There are 2
1444 ways to use the function: If @var{in} is passed as @code{NULL} and
1445 @var{inlen} is @code{0}, in-place encryption of the data in @var{out} or
1446 length @var{outsize} takes place. With @var{in} being not @code{NULL},
1447 @var{inlen} bytes are encrypted to the buffer @var{out} which must have
1448 at least a size of @var{inlen}. @var{outsize} must be set to the
1449 allocated size of @var{out}, so that the function can check that there
1450 is sufficient space. Note, that overlapping buffers are not allowed.
1452 Depending on the selected algorithms and encryption mode, the length of
1453 the buffers must be a multiple of the block size.
1455 The function returns @code{0} on success or an error code.
1459 @deftypefun gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t @var{h}, unsigned char *{out}, size_t @var{outsize}, const unsigned char *@var{in}, size_t @var{inlen})
1461 @code{gcry_cipher_decrypt} is used to decrypt the data. This function
1462 can either work in place or with two buffers. It uses the cipher
1463 context already setup and described by the handle @var{h}. There are 2
1464 ways to use the function: If @var{in} is passed as @code{NULL} and
1465 @var{inlen} is @code{0}, in-place decryption of the data in @var{out} or
1466 length @var{outsize} takes place. With @var{in} being not @code{NULL},
1467 @var{inlen} bytes are decrypted to the buffer @var{out} which must have
1468 at least a size of @var{inlen}. @var{outsize} must be set to the
1469 allocated size of @var{out}, so that the function can check that there
1470 is sufficient space. Note, that overlapping buffers are not allowed.
1472 Depending on the selected algorithms and encryption mode, the length of
1473 the buffers must be a multiple of the block size.
1475 The function returns @code{0} on success or an error code.
1479 OpenPGP (as defined in RFC-2440) requires a special sync operation in
1480 some places, the following function is used for this:
1482 @deftypefun gcry_error_t gcry_cipher_sync (gcry_cipher_hd_t @var{h})
1484 Perform the OpenPGP sync operation on context @var{h}. Note, that this
1485 is a no-op unless the context was created with the flag
1486 @code{GCRY_CIPHER_ENABLE_SYNC}
1489 Some of the described functions are implemented as macros utilizing a
1490 catch-all control function. This control function is rarely used
1491 directly but there is nothing which would inhibit it:
1493 @deftypefun gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t @var{h}, int @var{cmd}, void *@var{buffer}, size_t @var{buflen})
1495 @code{gcry_cipher_ctl} controls various aspects of the cipher module and
1496 specific cipher contexts. Usually some more specialized functions or
1497 macros are used for this purpose. The semantics of the function and its
1498 parameters depends on the the command @var{cmd} and the passed context
1499 handle @var{h}. Please see the comments in the source code
1500 (@code{src/global.c}) for details.
1503 @deftypefun gcry_error_t gcry_cipher_info (gcry_cipher_hd_t @var{h}, int @var{what}, void *@var{buffer}, size_t *@var{nbytes})
1505 @code{gcry_cipher_info} is used to retrieve various
1506 information about a cipher context or the cipher module in general.
1508 Currently no information is available.
1511 @node General cipher functions
1512 @section General cipher functions
1514 To work with the algorithms, several functions are available to map
1515 algorithm names to the internal identifiers, as well as ways to
1516 retrieve information about an algorithm or the current cipher context.
1518 @deftypefun gcry_error_t gcry_cipher_algo_info (int @var{algo}, int @var{what}, void *@var{buffer}, size_t *@var{nbytes})
1520 This function is used to retrieve information on a specific algorithm.
1521 You pass the cipher algorithm ID as @var{algo} and the type of
1522 information requested as @var{what}. The result is either returned as
1523 the return code of the function or copied to the provided @var{buffer}
1524 whose allocated length must be available in an integer variable with the
1525 address passed in @var{nbytes}. This variable will also receive the
1526 actual used length of the buffer.
1528 Here is a list of supported codes for @var{what}:
1530 @c begin constants for gcry_cipher_algo_info
1532 @item GCRYCTL_GET_KEYLEN:
1533 Return the length of the key. If the algorithm supports multiple key
1534 lengths, the maximum supported value is returned. The length is
1535 returned as number of octets (bytes) and not as number of bits in
1536 @var{nbytes}; @var{buffer} must be zero.
1538 @item GCRYCTL_GET_BLKLEN:
1539 Return the block length of the algorithm. The length is returned as a
1540 number of octets in @var{nbytes}; @var{buffer} must be zero.
1542 @item GCRYCTL_TEST_ALGO:
1543 Returns @code{0} when the specified algorithm is available for use.
1544 @var{buffer} and @var{nbytes} must be zero.
1547 @c end constants for gcry_cipher_algo_info
1550 @c end gcry_cipher_algo_info
1552 @deftypefun const char *gcry_cipher_algo_name (int @var{algo})
1554 @code{gcry_cipher_algo_name} returns a string with the name of the
1555 cipher algorithm @var{algo}. If the algorithm is not known or another
1556 error occurred, an empty string is returned. This function will never
1560 @deftypefun int gcry_cipher_map_name (const char *@var{name})
1562 @code{gcry_cipher_map_name} returns the algorithm identifier for the
1563 cipher algorithm described by the string @var{name}. If this algorithm
1564 is not available @code{0} is returned.
1567 @deftypefun int gcry_cipher_mode_from_oid (const char *@var{string})
1569 Return the cipher mode associated with an @acronym{ASN.1} object
1570 identifier. The object identifier is expected to be in the
1571 @acronym{IETF}-style dotted decimal notation. The function returns
1572 @code{0} for an unknown object identifier or when no mode is associated
1577 @c **********************************************************
1578 @c ******************* Hash Functions *********************
1579 @c **********************************************************
1583 @acronym{Libgcrypt} provides an easy and consistent to use interface
1584 for hashing. Hashing is buffered and several hash algorithms can be
1585 updated at once. It is possible to calculate a MAC using the same
1586 routines. The programming model follows an open/process/close
1587 paradigm and is in that similar to other building blocks provided by
1588 @acronym{Libgcrypt}.
1590 For convenience reasons, a few cyclic redundancy check value operations
1594 * Available hash algorithms:: List of hash algorithms supported by the library.
1595 * Hash algorithm modules:: How to work with hash algorithm modules.
1596 * Working with hash algorithms:: List of functions related to hashing.
1599 @node Available hash algorithms
1600 @section Available hash algorithms
1602 @c begin table of hash algorithms
1605 This is not a real algorithm but used by some functions as an error
1606 return value. This constant is guaranteed to have the value @code{0}.
1609 This is the SHA-1 algorithm which yields a message digest of 20 bytes.
1611 @item GCRY_MD_RMD160
1612 This is the 160 bit version of the RIPE message digest (RIPE-MD-160).
1613 Like SHA-1 it also yields a digest of 20 bytes.
1616 This is the well known MD5 algorithm, which yields a message digest of
1620 This is the MD4 algorithm, which yields a message digest of 16 bytes.
1623 This is an reserved identifier for MD-2; there is no implementation yet.
1626 This is the TIGER/192 algorithm which yields a message digest of 24 bytes.
1629 This is an reserved for the HAVAL algorithm with 5 passes and 160
1630 bit. It yields a message digest of 20 bytes. Note that there is no
1631 implementation yet available.
1633 @item GCRY_MD_SHA224
1634 This is the SHA-224 algorithm which yields a message digest of 28 bytes.
1635 See Change Notice 1 for FIPS 180-2 for the specification.
1637 @item GCRY_MD_SHA256
1638 This is the SHA-256 algorithm which yields a message digest of 32 bytes.
1639 See FIPS 180-2 for the specification.
1641 @item GCRY_MD_SHA384
1642 This is the SHA-384 algorithm which yields a message digest of 48 bytes.
1643 See FIPS 180-2 for the specification.
1645 @item GCRY_MD_SHA512
1646 This is the SHA-384 algorithm which yields a message digest of 64 bytes.
1647 See FIPS 180-2 for the specification.
1650 This is the ISO 3309 and ITU-T V.42 cyclic redundancy check. It
1651 yields an output of 4 bytes.
1653 @item GCRY_MD_CRC32_RFC1510
1654 This is the above cyclic redundancy check function, as modified by RFC
1655 1510. It yields an output of 4 bytes.
1657 @item GCRY_MD_CRC24_RFC2440
1658 This is the OpenPGP cyclic redundancy check function. It yields an
1661 @item GCRY_MD_WHIRLPOOL
1662 This is the Whirlpool algorithm which yields a message digest of 64
1666 @c end table of hash algorithms
1668 @node Hash algorithm modules
1669 @section Hash algorithm modules
1671 @acronym{Libgcrypt} makes it possible to load additional `message
1672 digest modules'; these digests can be used just like the message digest
1673 algorithms that are built into the library directly. For an
1674 introduction into extension modules, see @xref{Modules}.
1676 @deftp {Data type} gcry_md_spec_t
1677 This is the `module specification structure' needed for registering
1678 message digest modules, which has to be filled in by the user before
1679 it can be used to register a module. It contains the following
1683 @item const char *name
1684 The primary name of this algorithm.
1685 @item unsigned char *asnoid
1686 Array of bytes that form the ASN OID.
1688 Length of bytes in `asnoid'.
1689 @item gcry_md_oid_spec_t *oids
1690 A list of OIDs that are to be associated with the algorithm. The
1691 list's last element must have it's `oid' member set to NULL. See
1692 below for an explanation of this type. See below for an explanation
1695 Length of the message digest algorithm. See below for an explanation
1697 @item gcry_md_init_t init
1698 The function responsible for initializing a handle. See below for an
1699 explanation of this type.
1700 @item gcry_md_write_t write
1701 The function responsible for writing data into a message digest
1702 context. See below for an explanation of this type.
1703 @item gcry_md_final_t final
1704 The function responsible for `finalizing' a message digest context.
1705 See below for an explanation of this type.
1706 @item gcry_md_read_t read
1707 The function responsible for reading out a message digest result. See
1708 below for an explanation of this type.
1709 @item size_t contextsize
1710 The size of the algorithm-specific `context', that should be
1711 allocated for each handle.
1715 @deftp {Data type} gcry_md_oid_spec_t
1716 This type is used for associating a user-provided algorithm
1717 implementation with certain OIDs. It contains the following members:
1720 @item const char *oidstring
1721 Textual representation of the OID.
1725 @deftp {Data type} gcry_md_init_t
1726 Type for the `init' function, defined as: void (*gcry_md_init_t) (void
1730 @deftp {Data type} gcry_md_write_t
1731 Type for the `write' function, defined as: void (*gcry_md_write_t)
1732 (void *c, unsigned char *buf, size_t nbytes)
1735 @deftp {Data type} gcry_md_final_t
1736 Type for the `final' function, defined as: void (*gcry_md_final_t)
1740 @deftp {Data type} gcry_md_read_t
1741 Type for the `read' function, defined as: unsigned char
1742 *(*gcry_md_read_t) (void *c)
1745 @deftypefun gcry_error_t gcry_md_register (gcry_md_spec_t *@var{digest}, unsigned int *algorithm_id, gcry_module_t *@var{module})
1747 Register a new digest module whose specification can be found in
1748 @var{digest}. On success, a new algorithm ID is stored in
1749 @var{algorithm_id} and a pointer representing this module is stored
1753 @deftypefun void gcry_md_unregister (gcry_module_t @var{module})
1754 Unregister the digest identified by @var{module}, which must have been
1755 registered with gcry_md_register.
1758 @deftypefun gcry_error_t gcry_md_list (int *@var{list}, int *@var{list_length})
1759 Get a list consisting of the IDs of the loaded message digest modules.
1760 If @var{list} is zero, write the number of loaded message digest
1761 modules to @var{list_length} and return. If @var{list} is non-zero,
1762 the first *@var{list_length} algorithm IDs are stored in @var{list},
1763 which must be of according size. In case there are less message
1764 digests modules than *@var{list_length}, *@var{list_length} is updated
1765 to the correct number.
1768 @node Working with hash algorithms
1769 @section Working with hash algorithms
1771 To use most of these function it is necessary to create a context;
1774 @deftypefun gcry_error_t gcry_md_open (gcry_md_hd_t *@var{hd}, int @var{algo}, unsigned int @var{flags})
1776 Create a message digest object for algorithm @var{algo}. @var{flags}
1777 may be given as an bitwise OR of constants described below. @var{algo}
1778 may be given as @code{0} if the algorithms to use are later set using
1779 @code{gcry_md_enable}. @var{hd} is guaranteed to either receive a valid
1782 For a list of supported algorithms, see @xref{Available hash
1785 The flags allowed for @var{mode} are:
1787 @c begin table of hash flags
1789 @item GCRY_MD_FLAG_SECURE
1790 Allocate all buffers and the resulting digest in "secure memory". Use
1791 this is the hashed data is highly confidential.
1793 @item GCRY_MD_FLAG_HMAC
1794 Turn the algorithm into a HMAC message authentication algorithm. This
1795 only works if just one algorithm is enabled for the handle. Note that the function
1796 @code{gcry_md_setkey} must be used to set the MAC key. If you want CBC
1797 message authentication codes based on a cipher, see @xref{Working with
1801 @c begin table of hash flags
1803 You may use the function @code{gcry_md_is_enabled} to later check
1804 whether an algorithm has been enabled.
1807 @c end function gcry_md_open
1809 If you want to calculate several hash algorithms at the same time, you
1810 have to use the following function right after the @code{gcry_md_open}:
1812 @deftypefun gcry_error_t gcry_md_enable (gcry_md_hd_t @var{h}, int @var{algo})
1814 Add the message digest algorithm @var{algo} to the digest object
1815 described by handle @var{h}. Duplicated enabling of algorithms is
1816 detected and ignored.
1819 If the flag @code{GCRY_MD_FLAG_HMAC} was used, the key for the MAC must
1820 be set using the function:
1822 @deftypefun gcry_error_t gcry_md_setkey (gcry_md_hd_t @var{h}, const void *@var{key}, size_t @var{keylen})
1824 For use with the HMAC feature, set the MAC key to the value of @var{key}
1825 of length @var{keylen}.
1829 After you are done with the hash calculation, you should release the
1832 @deftypefun void gcry_md_close (gcry_md_hd_t @var{h})
1834 Release all resources of hash context @var{h}. @var{h} should not be
1835 used after a call to this function. A @code{NULL} passed as @var{h} is
1840 Often you have to do several hash operations using the same algorithm.
1841 To avoid the overhead of creating and releasing context, a reset function
1844 @deftypefun void gcry_md_reset (gcry_md_hd_t @var{h})
1846 Reset the current context to its initial state. This is effectively
1847 identical to a close followed by an open and enabling all currently
1852 Often it is necessary to start hashing some data and then continue to
1853 hash different data. To avoid hashing the same data several times (which
1854 might not even be possible if the data is received from a pipe), a
1855 snapshot of the current hash context can be taken and turned into a new
1858 @deftypefun gcry_error_t gcry_md_copy (gcry_md_hd_t *@var{handle_dst}, gcry_md_hd_t @var{handle_src})
1860 Create a new digest object as an exact copy of the object described by
1861 handle @var{handle_src} and store it in @var{handle_dst}. The context
1862 is not reset and you can continue to hash data using this context and
1863 independently using the original context.
1867 Now that we have prepared everything to calculate hashes, it is time to
1868 see how it is actually done. There are two ways for this, one to
1869 update the hash with a block of memory and one macro to update the hash
1870 by just one character. Both methods can be used on the same hash context.
1872 @deftypefun void gcry_md_write (gcry_md_hd_t @var{h}, const void *@var{buffer}, size_t @var{length})
1874 Pass @var{length} bytes of the data in @var{buffer} to the digest object
1875 with handle @var{h} to update the digest values. This
1876 function should be used for large blocks of data.
1879 @deftypefun void gcry_md_putc (gcry_md_hd_t @var{h}, int @var{c})
1881 Pass the byte in @var{c} to the digest object with handle @var{h} to
1882 update the digest value. This is an efficient function, implemented as
1883 a macro to buffer the data before an actual update.
1886 The semantics of the hash functions do not provide for reading out intermediate
1887 message digests because the calculation must be finalized first. This
1888 finalization may for example include the number of bytes hashed in the
1889 message digest or some padding.
1891 @deftypefun void gcry_md_final (gcry_md_hd_t @var{h})
1893 Finalize the message digest calculation. This is not really needed
1894 because @code{gcry_md_read} does this implicitly. After this has been
1895 done no further updates (by means of @code{gcry_md_write} or
1896 @code{gcry_md_putc} are allowed. Only the first call to this function
1897 has an effect. It is implemented as a macro.
1900 The way to read out the calculated message digest is by using the
1903 @deftypefun unsigned char *gcry_md_read (gcry_md_hd_t @var{h}, int @var{algo})
1905 @code{gcry_md_read} returns the message digest after finalizing the
1906 calculation. This function may be used as often as required but it will
1907 always return the same value for one handle. The returned message digest
1908 is allocated within the message context and therefore valid until the
1909 handle is released or reseted (using @code{gcry_md_close} or
1910 @code{gcry_md_reset}. @var{algo} may be given as 0 to return the only
1911 enabled message digest or it may specify one of the enabled algorithms.
1912 The function does return @code{NULL} if the requested algorithm has not
1916 Because it is often necessary to get the message digest of one block of
1917 memory, a fast convenience function is available for this task:
1919 @deftypefun void gcry_md_hash_buffer (int @var{algo}, void *@var{digest}, const void *@var{buffer}, size_t @var{length});
1921 @code{gcry_md_hash_buffer} is a shortcut function to calculate a message
1922 digest of a buffer. This function does not require a context and
1923 immediately returns the message digest of the @var{length} bytes at
1924 @var{buffer}. @var{digest} must be allocated by the caller, large
1925 enough to hold the message digest yielded by the the specified algorithm
1926 @var{algo}. This required size may be obtained by using the function
1927 @code{gcry_md_get_algo_dlen}.
1929 Note, that this function will abort the process if an unavailable
1933 @c ***********************************
1934 @c ***** MD info functions ***********
1935 @c ***********************************
1937 Hash algorithms are identified by internal algorithm numbers (see
1938 @code{gcry_md_open} for a list). However, in most applications they are
1939 used by names, so two functions are available to map between string
1940 representations and hash algorithm identifiers.
1942 @deftypefun const char *gcry_md_algo_name (int @var{algo})
1944 Map the digest algorithm id @var{algo} to a string representation of the
1945 algorithm name. For unknown algorithms this function returns an
1946 empty string. This function should not be used to test for the
1947 availability of an algorithm.
1950 @deftypefun int gcry_md_map_name (const char *@var{name})
1952 Map the algorithm with @var{name} to a digest algorithm identifier.
1953 Returns 0 if the algorithm name is not known. Names representing
1954 @acronym{ASN.1} object identifiers are recognized if the @acronym{IETF}
1955 dotted format is used and the OID is prefixed with either "@code{oid.}"
1956 or "@code{OID.}". For a list of supported OIDs, see the source code at
1957 @file{cipher/md.c}. This function should not be used to test for the
1958 availability of an algorithm.
1961 @deftypefun gcry_error_t gcry_md_get_asnoid (int @var{algo}, void *@var{buffer}, size_t *@var{length})
1963 Return an DER encoded ASN.1 OID for the algorithm @var{algo} in the
1964 user allocated @var{buffer}. @var{length} must point to variable with
1965 the available size of @var{buffer} and receives after return the
1966 actual size of the returned OID. The returned error code may be
1967 @code{GPG_ERR_TOO_SHORT} if the provided buffer is to short to receive
1968 the OID; it is possible to call the function with @code{NULL} for
1969 @var{buffer} to have it only return the required size. The function
1970 returns 0 on success.
1975 To test whether an algorithm is actually available for use, the
1976 following macro should be used:
1978 @deftypefun gcry_error_t gcry_md_test_algo (int @var{algo})
1980 The macro returns 0 if the algorithm @var{algo} is available for use.
1983 If the length of a message digest is not known, it can be retrieved
1984 using the following function:
1986 @deftypefun unsigned int gcry_md_get_algo_dlen (int @var{algo})
1988 Retrieve the length in bytes of the digest yielded by algorithm
1989 @var{algo}. This is often used prior to @code{gcry_md_read} to allocate
1990 sufficient memory for the digest.
1994 In some situations it might be hard to remember the algorithm used for
1995 the ongoing hashing. The following function might be used to get that
1998 @deftypefun int gcry_md_get_algo (gcry_md_hd_t @var{h})
2000 Retrieve the algorithm used with the handle @var{h}. Note, that this
2001 does not work reliable if more than one algorithm is enabled in @var{h}.
2004 The following macro might also be useful:
2006 @deftypefun int gcry_md_is_secure (gcry_md_hd_t @var{h})
2008 This function returns true when the digest object @var{h} is allocated
2009 in "secure memory"; i.e. @var{h} was created with the
2010 @code{GCRY_MD_FLAG_SECURE}.
2013 @deftypefun int gcry_md_is_enabled (gcry_md_hd_t @var{h}, int @var{algo})
2015 This function returns true when the algorithm @var{algo} has been
2016 enabled for the digest object @var{h}.
2021 Tracking bugs related to hashing is often a cumbersome task which
2022 requires to add a lot of printf statements into the code. @acronym{Libgcrypt}
2023 provides an easy way to avoid this. The actual data hashed can be
2024 written to files on request. The following 2 macros should be used to
2025 implement such a debugging facility:
2027 @deftypefun void gcry_md_start_debug (gcry_md_hd_t @var{h}, const char *@var{suffix})
2029 Enable debugging for the digest object with handle @var{h}. This
2030 creates create files named @file{dbgmd-<n>.<string>} while doing the
2031 actual hashing. @var{suffix} is the string part in the filename. The
2032 number is a counter incremented for each new hashing. The data in the
2033 file is the raw data as passed to @code{gcry_md_write} or
2034 @code{gcry_md_putc}.
2038 @deftypefun void gcry_md_stop_debug (gcry_md_hd_t @var{h}, int @var{reserved})
2040 Stop debugging on handle @var{h}. @var{reserved} should be specified as
2041 0. This function is usually not required because @code{gcry_md_close}
2042 does implicitly stop debugging.
2046 @c **********************************************************
2047 @c ******************* Public Key *************************
2048 @c **********************************************************
2049 @node Public Key cryptography (I)
2050 @chapter Public Key cryptography (I)
2052 Public key cryptography, also known as asymmetric cryptography, is an
2053 easy way for key management and to provide digital signatures.
2054 @acronym{Libgcrypt} provides two completely different interfaces to
2055 public key cryptography, this chapter explains the one based on
2059 * Available algorithms:: Algorithms supported by the library.
2060 * Used S-expressions:: Introduction into the used S-expression.
2061 * Public key modules:: How to work with public key modules.
2062 * Cryptographic Functions:: Functions for performing the cryptographic actions.
2063 * General public-key related Functions:: General functions, not implementing any cryptography.
2066 @node Available algorithms
2067 @section Available algorithms
2069 @acronym{Libgcrypt} supports the RSA (Rivest-Shamir-Adleman) algorithms as well
2070 as DSA (Digital Signature Algorithm) and ElGamal. The versatile
2071 interface allows to add more algorithms in the future.
2073 @node Used S-expressions
2074 @section Used S-expressions
2076 @acronym{Libgcrypt}'s API for asymmetric cryptography is based on data
2077 structures called S-expressions (see XXXX) and does not work with
2078 contexts as most of the other building blocks of @acronym{Libgcrypt}
2081 The following information are stored in S-expressions:
2086 @item plain text data
2088 @item encrypted data
2095 To describe how @acronym{Libgcrypt} expect keys, we use some examples. Note that
2103 indicate parameters whereas lowercase words are literals.
2116 This specifies a DSA private key with the following parameters:
2122 DSA group order @math{q} (which is a prime divisor of @math{p-1}).
2124 DSA group generator @math{g}.
2126 DSA public key value @math{y = g^x \bmod p}.
2128 DSA secret exponent x.
2131 All the MPI values are expected to be in @code{GCRYMPI_FMT_USG} format.
2132 The public key is similar with "private-key" replaced by "public-key"
2135 An easy way to create such an S-expressions is by using
2136 @code{gcry_sexp_build} which allows to pass a string with printf-like
2137 escapes to insert MPI values.
2140 Here is an example for an RSA key:
2158 RSA public modulus @math{n}.
2160 RSA public exponent @math{e}.
2162 RSA secret exponent @math{d = e^{-1} \bmod (p-1)(q-1)}.
2164 RSA secret prime @math{p}.
2166 RSA secret prime @math{q} with @math{q > p}.
2168 multiplicative inverse @math{u = p^{-1} \bmod q}.
2171 @node Public key modules
2172 @section Public key modules
2174 @acronym{Libgcrypt} makes it possible to load additional `public key
2175 modules'; these public key algorithms can be used just like the
2176 algorithms that are built into the library directly. For an
2177 introduction into extension modules, see @xref{Modules}.
2179 @deftp {Data type} gcry_pk_spec_t
2180 This is the `module specification structure' needed for registering
2181 public key modules, which has to be filled in by the user before it
2182 can be used to register a module. It contains the following members:
2185 @item const char *name
2186 The primary name of this algorithm.
2187 @item char **aliases
2188 A list of strings that are `aliases' for the algorithm. The list
2189 must be terminated with a NULL element.
2190 @item const char *elements_pkey
2191 String containing the one-letter names of the MPI values contained in
2193 @item const char *element_skey
2194 String containing the one-letter names of the MPI values contained in
2196 @item const char *elements_enc
2197 String containing the one-letter names of the MPI values that are the
2198 result of an encryption operation using this algorithm.
2199 @item const char *elements_sig
2200 String containing the one-letter names of the MPI values that are the
2201 result of a sign operation using this algorithm.
2202 @item const char *elements_grip
2203 String containing the one-letter names of the MPI values that are to
2204 be included in the `key grip'.
2206 The bitwise-OR of the following flags, depending on the abilities of
2209 @item GCRY_PK_USAGE_SIGN
2210 The algorithm supports signing and verifying of data.
2211 @item GCRY_PK_USAGE_ENCR
2212 The algorithm supports the encryption and decryption of data.
2214 @item gcry_pk_generate_t generate
2215 The function responsible for generating a new key pair. See below for
2216 a description of this type.
2217 @item gcry_pk_check_secret_key_t check_secret_key
2218 The function responsible for checking the sanity of a provided secret
2219 key. See below for a description of this type.
2220 @item gcry_pk_encrypt_t encrypt
2221 The function responsible for encrypting data. See below for a
2222 description of this type.
2223 @item gcry_pk_decrypt_t decrypt
2224 The function responsible for decrypting data. See below for a
2225 description of this type.
2226 @item gcry_pk_sign_t sign
2227 The function responsible for signing data. See below for a description
2229 @item gcry_pk_verify_t verify
2230 The function responsible for verifying that the provided signature
2231 matches the provided data. See below for a description of this type.
2232 @item gcry_pk_get_nbits_t get_nbits
2233 The function responsible for returning the number of bits of a provided
2234 key. See below for a description of this type.
2238 @deftp {Data type} gcry_pk_generate_t
2239 Type for the `generate' function, defined as: gcry_err_code_t
2240 (*gcry_pk_generate_t) (int algo, unsigned int nbits, unsigned long
2241 use_e, gcry_mpi_t *skey, gcry_mpi_t **retfactors)
2244 @deftp {Data type} gcry_pk_check_secret_key_t
2245 Type for the `check_secret_key' function, defined as: gcry_err_code_t
2246 (*gcry_pk_check_secret_key_t) (int algo, gcry_mpi_t *skey)
2249 @deftp {Data type} gcry_pk_encrypt_t
2250 Type for the `encrypt' function, defined as: gcry_err_code_t
2251 (*gcry_pk_encrypt_t) (int algo, gcry_mpi_t *resarr, gcry_mpi_t data,
2252 gcry_mpi_t *pkey, int flags)
2255 @deftp {Data type} gcry_pk_decrypt_t
2256 Type for the `decrypt' function, defined as: gcry_err_code_t
2257 (*gcry_pk_decrypt_t) (int algo, gcry_mpi_t *result, gcry_mpi_t *data,
2258 gcry_mpi_t *skey, int flags)
2261 @deftp {Data type} gcry_pk_sign_t
2262 Type for the `sign' function, defined as: gcry_err_code_t
2263 (*gcry_pk_sign_t) (int algo, gcry_mpi_t *resarr, gcry_mpi_t data,
2267 @deftp {Data type} gcry_pk_verify_t
2268 Type for the `verify' function, defined as: gcry_err_code_t
2269 (*gcry_pk_verify_t) (int algo, gcry_mpi_t hash, gcry_mpi_t *data,
2270 gcry_mpi_t *pkey, int (*cmp) (void *, gcry_mpi_t), void *opaquev)
2273 @deftp {Data type} gcry_pk_get_nbits_t
2274 Type for the `get_nbits' function, defined as: unsigned
2275 (*gcry_pk_get_nbits_t) (int algo, gcry_mpi_t *pkey)
2278 @deftypefun gcry_error_t gcry_pk_register (gcry_pk_spec_t *@var{pubkey}, unsigned int *algorithm_id, gcry_module_t *@var{module})
2280 Register a new public key module whose specification can be found in
2281 @var{pubkey}. On success, a new algorithm ID is stored in
2282 @var{algorithm_id} and a pointer representing this module is stored
2286 @deftypefun void gcry_pk_unregister (gcry_module_t @var{module})
2287 Unregister the public key module identified by @var{module}, which
2288 must have been registered with gcry_pk_register.
2291 @deftypefun gcry_error_t gcry_pk_list (int *@var{list}, int *@var{list_length})
2292 Get a list consisting of the IDs of the loaded pubkey modules. If
2293 @var{list} is zero, write the number of loaded pubkey modules to
2294 @var{list_length} and return. If @var{list} is non-zero, the first
2295 *@var{list_length} algorithm IDs are stored in @var{list}, which must
2296 be of according size. In case there are less pubkey modules than
2297 *@var{list_length}, *@var{list_length} is updated to the correct
2301 @node Cryptographic Functions
2302 @section Cryptographic Functions
2305 Note, that we will in future allow to use keys without p,q and u
2306 specified and may also support other parameters for performance
2311 Some functions operating on S-expressions support `flags', that
2312 influence the operation. These flags have to be listed in a
2313 sub-S-expression named `flags'; the following flags are known:
2317 Use PKCS#1 block type 2 padding.
2319 Do not use a technique called `blinding', which is used by default in
2320 order to prevent leaking of secret information. Blinding is only
2321 implemented by RSA, but it might be implemented by other algorithms in
2322 the future as well, when necessary.
2326 Now that we know the key basics, we can carry on and explain how to
2327 encrypt and decrypt data. In almost all cases the data is a random
2328 session key which is in turn used for the actual encryption of the real
2329 data. There are 2 functions to do this:
2331 @deftypefun gcry_error_t gcry_pk_encrypt (@w{gcry_sexp_t *@var{r_ciph},} @w{gcry_sexp_t @var{data},} @w{gcry_sexp_t @var{pkey}})
2333 Obviously a public key must be provided for encryption. It is
2334 expected as an appropriate S-expression (see above) in @var{pkey}.
2335 The data to be encrypted can either be in the simple old format, which
2336 is a very simple S-expression consisting only of one MPI, or it may be
2337 a more complex S-expression which also allows to specify flags for
2338 operation, like e.g. padding rules.
2341 If you don't want to let @acronym{Libgcrypt} handle the padding, you must pass an
2342 appropriate MPI using this expression for @var{data}:
2351 This has the same semantics as the old style MPI only way. @var{MPI} is
2352 the actual data, already padded appropriate for your protocol. Most
2353 systems however use PKCS#1 padding and so you can use this S-expression
2359 (value @var{block}))
2363 Here, the "flags" list has the "pkcs1" flag which let the function know
2364 that it should provide PKCS#1 block type 2 padding. The actual data to
2365 be encrypted is passed as a string of octets in @var{block}. The
2366 function checks that this data actually can be used with the given key,
2367 does the padding and encrypts it.
2369 If the function could successfully perform the encryption, the return
2370 value will be 0 and a a new S-expression with the encrypted result is
2371 allocated and assign to the variable at the address of @var{r_ciph}.
2372 The caller is responsible to release this value using
2373 @code{gcry_sexp_release}. In case of an error, an error code is
2374 returned and @var{r_ciph} will be set to @code{NULL}.
2377 The returned S-expression has this format when used with RSA:
2386 Where @var{a-mpi} is an MPI with the result of the RSA operation. When
2387 using the ElGamal algorithm, the return value will have this format:
2397 Where @var{a-mpi} and @var{b-mpi} are MPIs with the result of the
2398 ElGamal encryption operation.
2400 @c end gcry_pk_encrypt
2402 @deftypefun gcry_error_t gcry_pk_decrypt (@w{gcry_sexp_t *@var{r_plain},} @w{gcry_sexp_t @var{data},} @w{gcry_sexp_t @var{skey}})
2404 Obviously a private key must be provided for decryption. It is expected
2405 as an appropriate S-expression (see above) in @var{skey}. The data to
2406 be decrypted must match the format of the result as returned by
2407 @code{gcry_pk_encrypt}, but should be enlarged with a @code{flags}
2419 Note, that this function currently does not know of any padding
2420 methods and the caller must do any un-padding on his own.
2423 The function returns 0 on success or an error code. The variable at the
2424 address of @var{r_plain} will be set to NULL on error or receive the
2425 decrypted value on success. The format of @var{r_plain} is a
2426 simple S-expression part (i.e. not a valid one) with just one MPI if
2427 there was no @code{flags} element in @var{data}; if at least an empty
2428 @code{flags} is passed in @var{data}, the format is:
2431 (value @var{plaintext})
2434 @c end gcry_pk_decrypt
2437 Another operation commonly performed using public key cryptography is
2438 signing data. In some sense this is even more important than
2439 encryption because digital signatures are an important instrument for
2440 key management. @acronym{Libgcrypt} supports digital signatures using
2441 2 functions, similar to the encryption functions:
2443 @deftypefun gcry_error_t gcry_pk_sign (@w{gcry_sexp_t *@var{r_sig},} @w{gcry_sexp_t @var{data},} @w{gcry_sexp_t @var{skey}})
2445 This function creates a digital signature for @var{data} using the
2446 private key @var{skey} and place it into the variable at the address of
2447 @var{r_sig}. @var{data} may either be the simple old style S-expression
2448 with just one MPI or a modern and more versatile S-expression which
2449 allows to let @acronym{Libgcrypt} handle padding:
2454 (hash @var{hash-algo} @var{block}))
2458 This example requests to sign the data in @var{block} after applying
2459 PKCS#1 block type 1 style padding. @var{hash-algo} is a string with the
2460 hash algorithm to be encoded into the signature, this may be any hash
2461 algorithm name as supported by @acronym{Libgcrypt}. Most likely, this will be
2462 "sha1", "rmd160" or "md5". It is obvious that the length of @var{block}
2463 must match the size of that message digests; the function checks that
2464 this and other constraints are valid.
2467 If PKCS#1 padding is not required (because the caller does already
2468 provide a padded value), either the old format or better the following
2469 format should be used:
2478 Here, the data to be signed is directly given as an @var{MPI}.
2481 The signature is returned as a newly allocated S-expression in
2482 @var{r_sig} using this format for RSA:
2490 Where @var{s-mpi} is the result of the RSA sign operation. For DSA the
2491 S-expression returned is:
2500 Where @var{r-mpi} and @var{s-mpi} are the result of the DSA sign
2501 operation. For ElGamal signing (which is slow, yields large numbers
2502 and probably is not as secure as the other algorithms), the same format is
2503 used with "elg" replacing "dsa".
2508 The operation most commonly used is definitely the verification of a
2509 signature. @acronym{Libgcrypt} provides this function:
2511 @deftypefun gcry_error_t gcry_pk_verify (@w{gcry_sexp_t @var{sig}}, @w{gcry_sexp_t @var{data}}, @w{gcry_sexp_t @var{pkey}})
2513 This is used to check whether the signature @var{sig} matches the
2514 @var{data}. The public key @var{pkey} must be provided to perform this
2515 verification. This function is similar in its parameters to
2516 @code{gcry_pk_sign} with the exceptions that the public key is used
2517 instead of the private key and that no signature is created but a
2518 signature, in a format as created by @code{gcry_pk_sign}, is passed to
2519 the function in @var{sig}.
2522 The result is 0 for success (i.e. the data matches the signature), or an
2523 error code where the most relevant code is @code{GCRYERR_BAD_SIGNATURE}
2524 to indicate that the signature does not match the provided data.
2527 @c end gcry_pk_verify
2529 @node General public-key related Functions
2530 @section General public-key related Functions
2533 A couple of utility functions are available to retrieve the length of
2534 the key, map algorithm identifiers and perform sanity checks:
2536 @deftypefun {const char *} gcry_pk_algo_name (int @var{algo})
2538 Map the public key algorithm id @var{algo} to a string representation of
2539 the algorithm name. For unknown algorithms this functions returns an
2543 @deftypefun int gcry_pk_map_name (const char *@var{name})
2545 Map the algorithm @var{name} to a public key algorithm Id. Returns 0 if
2546 the algorithm name is not known.
2549 @deftypefun int gcry_pk_test_algo (int @var{algo})
2551 Return 0 if the public key algorithm @var{algo} is available for use.
2552 Note, that this is implemented as a macro.
2556 @deftypefun {unsigned int} gcry_pk_get_nbits (gcry_sexp_t @var{key})
2558 Return what is commonly referred as the key length for the given
2559 public or private in @var{key}.
2562 @deftypefun {unsigned char *} gcry_pk_get_keygrip (@w{gcry_sexp_t @var{key}}, @w{unsigned char *@var{array}})
2564 Return the so called "keygrip" which is the SHA-1 hash of the public key
2565 parameters expressed in a way depended on the algorithm. @var{array}
2566 must either provide space for 20 bytes or @code{NULL;}. In the latter
2567 case a newly allocated array of that size is returned. On success a
2568 pointer to the newly allocated space or to @var{array} is returned.
2569 @code{NULL} is returned to indicate an error which is most likely an unknown
2570 algorithm or one where a "keygrip" has not yet been defined.
2571 The function accepts public or secret keys in @var{key}.
2574 @deftypefun gcry_error_t gcry_pk_testkey (gcry_sexp_t @var{key})
2576 Return zero if the private key @var{key} is `sane', an error code otherwise.
2577 Note, that it is not possible to check the `saneness' of a public key.
2582 @deftypefun gcry_error_t gcry_pk_algo_info (@w{int @var{algo}}, @w{int @var{what}}, @w{void *@var{buffer}}, @w{size_t *@var{nbytes}})
2584 Depending on the value of @var{what} return various information about
2585 the public key algorithm with the id @var{algo}. Note, that the
2586 function returns @code{-1} on error and the actual error code must be
2587 retrieved using the function @code{gcry_errno}. The currently defined
2588 values for @var{what} are:
2591 @item GCRYCTL_TEST_ALGO:
2592 Return 0 when the specified algorithm is available for use.
2593 @var{buffer} must be @code{NULL}, @var{nbytes} may be passed as
2594 @code{NULL} or point to a variable with the required usage of the
2595 algorithm. This may be 0 for "don't care" or the bit-wise OR of these
2599 @item GCRY_PK_USAGE_SIGN
2600 Algorithm is usable for signing.
2601 @item GCRY_PK_USAGE_ENCR
2602 Algorithm is usable for encryption.
2605 @item GCRYCTL_GET_ALGO_USAGE:
2606 Return the usage flags for the given algorithm. An invalid algorithm
2607 return 0. Disabled algorithms are ignored here because we
2608 want to know whether the algorithm is at all capable of a certain usage.
2610 @item GCRYCTL_GET_ALGO_NPKEY
2611 Return the number of elements the public key for algorithm @var{algo}
2612 consist of. Return 0 for an unknown algorithm.
2614 @item GCRYCTL_GET_ALGO_NSKEY
2615 Return the number of elements the private key for algorithm @var{algo}
2616 consist of. Note that this value is always larger than that of the
2617 public key. Return 0 for an unknown algorithm.
2619 @item GCRYCTL_GET_ALGO_NSIGN
2620 Return the number of elements a signature created with the algorithm
2621 @var{algo} consists of. Return 0 for an unknown algorithm or for an
2622 algorithm not capable of creating signatures.
2624 @item GCRYCTL_GET_ALGO_NENC
2625 Return the number of elements a encrypted message created with the algorithm
2626 @var{algo} consists of. Return 0 for an unknown algorithm or for an
2627 algorithm not capable of encryption.
2631 Please note that parameters not required should be passed as @code{NULL}.
2633 @c end gcry_pk_algo_info
2636 @deftypefun gcry_error_t gcry_pk_ctl (@w{int @var{cmd}}, @w{void *@var{buffer}}, @w{size_t @var{buflen}})
2638 This is a general purpose function to perform certain control
2639 operations. @var{cmd} controls what is to be done. The return value is
2640 0 for success or an error code. Currently supported values for
2644 @item GCRYCTL_DISABLE_ALGO
2645 Disable the algorithm given as an algorithm id in @var{buffer}.
2646 @var{buffer} must point to an @code{int} variable with the algorithm id
2647 and @var{buflen} must have the value @code{sizeof (int)}.
2654 @acronym{Libgcrypt} also provides a function for generating public key
2657 @deftypefun gcry_error_t gcry_pk_genkey (@w{gcry_sexp_t *@var{r_key}}, @w{gcry_sexp_t @var{parms}})
2659 This function create a new public key pair using information given in
2660 the S-expression @var{parms} and stores the private and the public key
2661 in one new S-expression at the address given by @var{r_key}. In case of
2662 an error, @var{r_key} is set to @code{NULL}. The return code is 0 for
2663 success or an error code otherwise.
2666 Here is an example for @var{parms} for creating a 1024 bit RSA key:
2675 To create an ElGamal key, substitute "elg" for "rsa" and to create a DSA
2676 key use "dsa". Valid ranges for the key length depend on the
2677 algorithms; all commonly used key lengths are supported. Currently
2678 supported parameters are:
2682 This is always required to specify the length of the key. The argument
2683 is a string with a number in C-notation.
2686 This is only used with RSA to give a hint for the public exponent. The
2687 value will be used as a base to test for a usable exponent. Some values
2692 Use a secure and fast value. This is currently the number 41.
2694 Use a secure value as required by some specification. This is currently
2701 If this parameter is not used, @acronym{Libgcrypt} uses for historic reasons
2705 @c end table of parameters
2708 The key pair is returned in a format depending on the algorithm. Both
2709 private and public keys are returned in one container and may be
2710 accompanied by some miscellaneous information.
2713 As an example, here is what the ElGamal key generation returns:
2729 (pm1-factors @var{n1 n2 ... nn})))
2733 As you can see, some of the information is duplicated, but this provides
2734 an easy way to extract either the public or the private key. Note that
2735 the order of the elements is not defined, e.g. the private key may be
2736 stored before the public key. @var{n1 n2 ... nn} is a list of prime
2737 numbers used to composite @var{p-mpi}; this is in general not a very
2740 @c end gcry_pk_genkey
2742 @node Public Key cryptography (II)
2743 @chapter Public Key cryptography (II)
2745 This chapter documents the alternative interface to asymmetric
2746 cryptography (ac) that is not based on S-expressions, but on native C
2747 data structures. As opposed to the pk interface described in the
2748 former chapter, this one follows an open/use/close paradigm like other
2749 building blocks of the library.
2752 * Available asymmetric algorithms:: List of algorithms supported by the library.
2753 * Working with sets of data:: How to work with sets of data.
2754 * Working with IO objects:: How to work with IO objects.
2755 * Working with handles:: How to use handles.
2756 * Working with keys:: How to work with keys.
2757 * Using cryptographic functions:: How to perform cryptographic operations.
2758 * Handle-independent functions:: General functions independent of handles.
2761 @node Available asymmetric algorithms
2762 @section Available asymmetric algorithms
2764 @acronym{Libgcrypt} supports the RSA (Rivest-Shamir-Adleman)
2765 algorithms as well as DSA (Digital Signature Algorithm) and ElGamal.
2766 The versatile interface allows to add more algorithms in the future.
2768 @deftp {Data type} gcry_ac_id_t
2770 The following constants are defined for this type:
2774 Riven-Shamir-Adleman
2776 Digital Signature Algorithm
2780 ElGamal, encryption only.
2784 @node Working with sets of data
2785 @section Working with sets of data
2787 In the context of this interface the term `data set' refers to a list
2788 of `named MPI values' that is used by functions performing
2789 cryptographic operations; a named MPI value is a an MPI value,
2790 associated with a label.
2792 Such data sets are used for representing keys, since keys simply
2793 consist of a variable amount of numbers. Furthermore some functions
2794 return data sets to the caller that are to be provided to other
2797 This section documents the data types, symbols and functions that are
2798 relevant for working with data sets.
2800 @deftp {Data type} gcry_ac_data_t
2804 The following flags are supported:
2807 @item GCRY_AC_FLAG_DEALLOC
2808 Used for storing data in a data set. If given, the data will be
2809 released by the library. Note that whenever one of the ac functions
2810 is about to release objects because of this flag, the objects are
2811 expected to be stored in memory allocated through the Libgcrypt memory
2812 management. In other words: gcry_free() is used instead of free().
2814 @item GCRY_AC_FLAG_COPY
2815 Used for storing/retrieving data in/from a data set. If given, the
2816 library will create copies of the provided/contained data, which will
2817 then be given to the user/associated with the data set.
2820 @deftypefun gcry_error_t gcry_ac_data_new (gcry_ac_data_t *@var{data})
2821 Creates a new, empty data set and stores it in @var{data}.
2824 @deftypefun void gcry_ac_data_destroy (gcry_ac_data_t @var{data})
2825 Destroys the data set @var{data}.
2828 @deftypefun gcry_error_t gcry_ac_data_set (gcry_ac_data_t @var{data}, unsigned int @var{flags}, char *@var{name}, gcry_mpi_t @var{mpi})
2829 Add the value @var{mpi} to @var{data} with the label @var{name}. If
2830 @var{flags} contains GCRY_AC_FLAG_DATA_COPY, the data set will contain
2831 copies of @var{name} and @var{mpi}. If @var{flags} contains
2832 GCRY_AC_FLAG_DATA_DEALLOC or GCRY_AC_FLAG_DATA_COPY, the values
2833 contained in the data set will be deallocated when they are to be
2834 removed from the data set.
2837 @deftypefun gcry_error_t gcry_ac_data_copy (gcry_ac_data_t *@var{data_cp}, gcry_ac_data_t @var{data})
2838 Create a copy of the data set @var{data} and store it in
2839 @var{data_cp}. FIXME: exact semantics undefined.
2842 @deftypefun unsigned int gcry_ac_data_length (gcry_ac_data_t @var{data})
2843 Returns the number of named MPI values inside of the data set
2847 @deftypefun gcry_error_t gcry_ac_data_get_name (gcry_ac_data_t @var{data}, unsigned int @var{flags}, char *@var{name}, gcry_mpi_t *@var{mpi})
2848 Store the value labelled with @var{name} found in @var{data} in
2849 @var{mpi}. If @var{flags} contains GCRY_AC_FLAG_COPY, store a copy of
2850 the @var{mpi} value contained in the data set. @var{mpi} may be NULL
2851 (this might be useful for checking the existence of an MPI with
2855 @deftypefun gcry_error_t gcry_ac_data_get_index (gcry_ac_data_t @var{data}, unsigned int flags, unsigned int @var{index}, const char **@var{name}, gcry_mpi_t *@var{mpi})
2856 Stores in @var{name} and @var{mpi} the named @var{mpi} value contained
2857 in the data set @var{data} with the index @var{idx}. If @var{flags}
2858 contains GCRY_AC_FLAG_COPY, store copies of the values contained in
2859 the data set. @var{name} or @var{mpi} may be NULL.
2862 @deftypefun void gcry_ac_data_clear (gcry_ac_data_t @var{data})
2863 Destroys any values contained in the data set @var{data}.
2866 @deftypefun gcry_error_t gcry_ac_data_to_sexp (gcry_ac_data_t @var{data}, gcry_sexp_t *@var{sexp}, const char **@var{identifiers})
2867 This function converts the data set @var{data} into a newly created
2868 S-Expression, which is to be stored in @var{sexp}; @var{identifiers}
2869 is a NULL terminated list of C strings, which specifies the structure
2870 of the S-Expression.
2874 If @var{identifiers} is a list of pointers to the strings ``foo'' and
2875 ``bar'' and if @var{data} is a data set containing the values ``val1 =
2876 0x01'' and ``val2 = 0x02'', then the resulting S-Expression will look
2877 like this: (foo (bar ((val1 0x01) (val2 0x02))).
2880 @deftypefun gcry_error gcry_ac_data_from_sexp (gcry_ac_data_t *@var{data}, gcry_sexp_t @var{sexp}, const char **@var{identifiers})
2881 This function converts the S-Expression @var{sexp} into a newly
2882 created data set, which is to be stored in @var{data};
2883 @var{identifiers} is a NULL terminated list of C strings, which
2884 specifies the structure of the S-Expression. If the list of
2885 identifiers does not match the structure of the S-Expression, the
2889 @node Working with IO objects
2890 @section Working with IO objects
2892 Note: IO objects are currently only used in the context of message
2893 encoding/decoding and encryption/signature schemes.
2895 @deftp {Data type} {gcry_ac_io_t}
2896 @code{gcry_ac_io_t} is the type to be used for IO objects.
2899 IO objects provide an uniform IO layer on top of different underlying
2900 IO mechanisms; either they can be used for providing data to the
2901 library (mode is GCRY_AC_IO_READABLE) or they can be used for
2902 retrieving data from the library (mode is GCRY_AC_IO_WRITABLE).
2904 IO object need to be initialized by calling on of the following
2907 @deftypefun void gcry_ac_io_init (gcry_ac_io_t *@var{ac_io}, gcry_ac_io_mode_t @var{mode}, gcry_ac_io_type_t @var{type}, ...);
2908 Initialize @var{ac_io} according to @var{mode}, @var{type} and the
2909 variable list of arguments. The list of variable arguments to specify
2910 depends on the given @var{type}.
2913 @deftypefun void gcry_ac_io_init_va (gcry_ac_io_t *@var{ac_io}, gcry_ac_io_mode_t @var{mode}, gcry_ac_io_type_t @var{type}, va_list @var{ap});
2914 Initialize @var{ac_io} according to @var{mode}, @var{type} and the
2915 variable list of arguments @var{ap}. The list of variable arguments
2916 to specify depends on the given @var{type}.
2919 The following types of IO objects exist:
2922 @item GCRY_AC_IO_STRING
2923 In case of GCRY_AC_IO_READABLE the IO object will provide data from a
2924 memory string. Arguments to specify at initialization time:
2926 @item unsigned char *
2927 Pointer to the beginning of the memory string
2929 Size of the memory string
2931 In case of GCRY_AC_IO_WRITABLE the object will store retrieved data in
2932 a newly allocated memory string. Arguments to specify at
2933 initialization time:
2935 @item unsigned char **
2936 Pointer to address, at which the pointer to the newly created memory
2937 string is to be stored
2939 Pointer to address, at which the size of the newly created memory
2940 string is to be stored
2943 @item GCRY_AC_IO_CALLBACK
2944 In case of GCRY_AC_IO_READABLE the object will forward read requests
2945 to a provided callback function. Arguments to specify at
2946 initialization time:
2948 @item gcry_ac_data_read_cb_t
2949 Callback function to use
2951 Opaque argument to provide to the callback function
2953 In case of GCRY_AC_IO_WRITABLE the object will forward write requests
2954 to a provided callback function. Arguments to specify at
2955 initialization time:
2957 @item gcry_ac_data_write_cb_t
2958 Callback function to use
2960 Opaque argument to provide to the callback function
2964 @node Working with handles
2965 @section Working with handles
2967 In order to use an algorithm, an according handle must be created.
2968 This is done using the following function:
2970 @deftypefun gcry_error_t gcry_ac_open (gcry_ac_handle_t *@var{handle}, int @var{algorithm}, int @var{flags})
2972 Creates a new handle for the algorithm @var{algorithm} and stores it
2973 in @var{handle}. @var{flags} is not used currently.
2975 @var{algorithm} must be a valid algorithm ID, see @xref{Available
2976 algorithms}, for a list of supported algorithms and the according
2977 constants. Besides using the listed constants directly, the functions
2978 @code{gcry_ac_name_to_id} may be used to convert the textual name of
2979 an algorithm into the according numeric ID.
2982 @deftypefun void gcry_ac_close (gcry_ac_handle_t @var{handle})
2983 Destroys the handle @var{handle}.
2986 @node Working with keys
2987 @section Working with keys
2989 @deftp {Data type} gcry_ac_key_type_t
2993 @item GCRY_AC_KEY_TYPE_SECRET
2994 Specifies a secret key.
2995 @item GCRY_AC_KEY_TYPE_PUBLIC
2996 Specifies a public key.
3000 @deftp {Data type} gcry_ac_key_t
3001 This type represents a single `key', either a secret one or a public
3005 @deftp {Data type} gcry_ac_key_pair_t
3006 This type represents a `key pair' containing a secret and a public key.
3009 Key data structures can be created in two different ways; a new key
3010 pair can be generated, resulting in ready-to-use key. Alternatively a
3011 key can be initialized from a given data set.
3013 @deftypefun gcry_error_t gcry_ac_key_init (gcry_ac_key_t *@var{key}, gcry_ac_handle_t @var{handle}, gcry_ac_key_type_t @var{type}, gcry_ac_data_t @var{data})
3014 Creates a new key of type @var{type}, consisting of the MPI values
3015 contained in the data set @var{data} and stores it in @var{key}.
3018 @deftypefun gcry_error_t gcry_ac_key_pair_generate (gcry_ac_handle_t @var{handle}, unsigned int @var{nbits}, void *@var{key_spec}, gcry_ac_key_pair_t *@var{key_pair}, gcry_mpi_t **@var{misc_data})
3020 Generates a new key pair via the handle @var{handle} of @var{NBITS}
3021 bits and stores it in @var{key_pair}.
3023 In case non-standard settings are wanted, a pointer to a structure of
3024 type @code{gcry_ac_key_spec_<algorithm>_t}, matching the selected
3025 algorithm, can be given as @var{key_spec}. @var{misc_data} is not
3026 used yet. Such a structure does only exist for RSA. A descriptions
3027 of the members of the supported structures follows.
3030 @item gcry_ac_key_spec_rsa_t
3033 Generate the key pair using a special @code{e}. The value of @code{e}
3034 has the following meanings:
3037 Let @acronym{Libgcrypt} decide what exponent should be used.
3039 Request the use of a ``secure'' exponent; this is required by some
3040 specification to be 65537.
3042 Try starting at this value until a working exponent is found. Note,
3043 that the current implementation leaks some information about the
3044 private key because the incrementation used is not randomized. Thus,
3045 this function will be changed in the future to return a random
3046 exponent of the given size.
3054 gcry_ac_key_pair_t key_pair;
3055 gcry_ac_key_spec_rsa_t rsa_spec;
3057 rsa_spec.e = gcry_mpi_new (0);
3058 gcry_mpi_set_ui (rsa_spec.e, 1)
3060 err = gcry_ac_open (&handle, GCRY_AC_RSA, 0);
3063 err = gcry_ac_key_pair_generate (handle, &key_pair, 1024, (void *) &rsa_spec);
3070 @deftypefun gcry_ac_key_t gcry_ac_key_pair_extract (gcry_ac_key_pair_t @var{key_pair}, gcry_ac_key_type_t @var{which})
3071 Returns the key of type @var{which} out of the key pair
3075 @deftypefun void gcry_ac_key_destroy (gcry_ac_key_t @var{key})
3076 Destroys the key @var{key}.
3079 @deftypefun void gcry_ac_key_pair_destroy (gcry_ac_key_pair_t @var{key_pair})
3080 Destroys the key pair @var{key_pair}.
3083 @deftypefun gcry_ac_data_t gcry_ac_key_data_get (gcry_ac_key_t @var{key})
3084 Returns the data set contained in the key @var{key}.
3087 @deftypefun gcry_error_t gcry_ac_key_test (gcry_ac_handle_t @var{handle}, gcry_ac_key_t @var{key})
3088 Verifies that the private key @var{key} is sane via @var{handle}.
3091 @deftypefun gcry_error_t gcry_ac_key_get_nbits (gcry_ac_handle_t @var{handle}, gcry_ac_key_t @var{key}, unsigned int *@var{nbits})
3092 Stores the number of bits of the key @var{key} in @var{nbits} via @var{handle}.
3095 @deftypefun gcry_error_t gcry_ac_key_get_grip (gcry_ac_handle_t @var{handle}, gcry_ac_key_t @var{key}, unsigned char *@var{key_grip})
3096 Writes the 20 byte long key grip of the key @var{key} to
3097 @var{key_grip} via @var{handle}.
3100 @node Using cryptographic functions
3101 @section Using cryptographic functions
3103 The following flags might be relevant:
3106 @item GCRY_AC_FLAG_NO_BLINDING
3107 Disable any blinding, which might be supported by the chosen
3108 algorithm; blinding is the default.
3111 There exist two kinds of cryptographic functions available through the
3112 ac interface: primitives, and high-level functions.
3114 Primitives deal with MPIs (data sets) directly; what they provide is
3115 direct access to the cryptographic operations provided by an algorithm
3118 High-level functions deal with octet strings, according to a specified
3119 ``scheme''. Schemes make use of ``encoding methods'', which are
3120 responsible for converting the provided octet strings into MPIs, which
3121 are then forwared to the cryptographic primitives. Since schemes are
3122 to be used for a special purpose in order to achieve a particular
3123 security goal, there exist ``encryption schemes'' and ``signature
3124 schemes''. Encoding methods can be used seperately or implicitly
3127 What follows is a description of the cryptographic primitives.
3129 @deftypefun gcry_error_t gcry_ac_data_encrypt (gcry_ac_handle_t @var{handle}, unsigned int @var{flags}, gcry_ac_key_t @var{key}, gcry_mpi_t @var{data_plain}, gcry_ac_data_t **@var{data_encrypted})
3130 Encrypts the plain text MPI value @var{data_plain} with the key public
3131 @var{key} under the control of the flags @var{flags} and stores the
3132 resulting data set into @var{data_encrypted}.
3135 @deftypefun gcry_error_t gcry_ac_data_decrypt (gcry_ac_handle_t @var{handle}, unsigned int @var{flags}, gcry_ac_key_t @var{key}, gcry_mpi_t *@var{data_plain}, gcry_ac_data_t @var{data_encrypted})
3136 Decrypts the encrypted data contained in the data set
3137 @var{data_encrypted} with the secret key KEY under the control of the
3138 flags @var{flags} and stores the resulting plain text MPI value in
3142 @deftypefun gcry_error_t gcry_ac_data_sign (gcry_ac_handle_t @var{handle}, gcry_ac_key_t @var{key}, gcry_mpi_t @var{data}, gcry_ac_data_t *@var{data_signature})
3143 Signs the data contained in @var{data} with the secret key @var{key}
3144 and stores the resulting signature in the data set
3145 @var{data_signature}.
3148 @deftypefun gcry_error_t gcry_ac_data_verify (gcry_ac_handle_t @var{handle}, gcry_ac_key_t @var{key}, gcry_mpi_t @var{data}, gcry_ac_data_t @var{data_signature})
3149 Verifies that the signature contained in the data set
3150 @var{data_signature} is indeed the result of signing the data
3151 contained in @var{data} with the secret key belonging to the public
3155 What follows is a description of the high-level functions.
3157 The type ``gcry_ac_em_t'' is used for specifying encoding methods; the
3158 following methods are supported:
3161 @item GCRY_AC_EME_PKCS_V1_5
3162 PKCS-V1_5 Encoding Method for Encryption. Options must be provided
3163 through a pointer to a correctly initialized object of type
3164 gcry_ac_eme_pkcs_v1_5_t.
3166 @item GCRY_AC_EMSA_PKCS_V1_5
3167 PKCS-V1_5 Encoding Method for Signatures with Appendix. Options must
3168 be provided through a pointer to a correctly initialized object of
3169 type gcry_ac_emsa_pkcs_v1_5_t.
3172 Option structure types:
3175 @item gcry_ac_eme_pkcs_v1_5_t
3177 @item gcry_ac_key_t key
3178 @item gcry_ac_handle_t handle
3180 @item gcry_ac_emsa_pkcs_v1_5_t
3182 @item gcry_md_algo_t md
3187 Encoding methods can be used directly through the following functions:
3189 @deftypefun gcry_error_t gcry_ac_data_encode (gcry_ac_em_t @var{method}, unsigned int @var{flags}, void *@var{options}, unsigned char *@var{m}, size_t @var{m_n}, unsigned char **@var{em}, size_t *@var{em_n})
3190 Encodes the message contained in @var{m} of size @var{m_n} according
3191 to @var{method}, @var{flags} and @var{options}. The newly created
3192 encoded message is stored in @var{em} and @var{em_n}.
3195 @deftypefun gcry_error_t gcry_ac_data_decode (gcry_ac_em_t @var{method}, unsigned int @var{flags}, void *@var{options}, unsigned char *@var{em}, size_t @var{em_n}, unsigned char **@var{m}, size_t *@var{m_n})
3196 Decodes the message contained in @var{em} of size @var{em_n} according
3197 to @var{method}, @var{flags} and @var{options}. The newly created
3198 decoded message is stored in @var{m} and @var{m_n}.
3201 The type ``gcry_ac_scheme_t'' is used for specifying schemes; the
3202 following schemes are supported:
3205 @item GCRY_AC_ES_PKCS_V1_5
3206 PKCS-V1_5 Encryption Scheme. No options can be provided.
3207 @item GCRY_AC_SSA_PKCS_V1_5
3208 PKCS-V1_5 Signature Scheme (with Appendix). Options can be provided
3209 through a pointer to a correctly initialized object of type
3210 gcry_ac_ssa_pkcs_v1_5_t.
3213 Option structure types:
3216 @item gcry_ac_ssa_pkcs_v1_5_t
3218 @item gcry_md_algo_t md
3222 The functions implementing schemes:
3224 @deftypefun gcry_error_t gcry_ac_data_encrypt_scheme (gcry_ac_handle_t @var{handle}, gcry_ac_scheme_t @var{scheme}, unsigned int @var{flags}, void *@var{opts}, gcry_ac_key_t @var{key}, gcry_ac_io_t *@var{io_message}, gcry_ac_io_t *@var{io_cipher})
3225 Encrypts the plain text readable from @var{io_message} through
3226 @var{handle} with the public key @var{key} according to @var{scheme},
3227 @var{flags} and @var{opts}. If @var{opts} is not NULL, it has to be a
3228 pointer to a structure specific to the chosen scheme (gcry_ac_es_*_t).
3229 The encrypted message is written to @var{io_cipher}.
3232 @deftypefun gcry_error_t gcry_ac_data_decrypt_scheme (gcry_ac_handle_t @var{handle}, gcry_ac_scheme_t @var{scheme}, unsigned int @var{flags}, void *@var{opts}, gcry_ac_key_t @var{key}, gcry_ac_io_t *@var{io_cipher}, gcry_ac_io_t *@var{io_message})
3233 Decrypts the cipher text readable from @var{io_cipher} through
3234 @var{handle} with the secret key @var{key} according to @var{scheme},
3235 @var{flags} and @var{opts}. If @var{opts} is not NULL, it has to be a
3236 pointer to a structure specific to the chosen scheme (gcry_ac_es_*_t).
3237 The decrypted message is written to @var{io_message}.
3240 @deftypefun gcry_error_t gcry_ac_data_sign_scheme (gcry_ac_handle_t @var{handle}, gcry_ac_scheme_t @var{scheme}, unsigned int @var{flags}, void *@var{opts}, gcry_ac_key_t @var{key}, gcry_ac_io_t *@var{io_message}, gcry_ac_io_t *@var{io_signature})
3241 Signs the message readable from @var{io_message} through @var{handle}
3242 with the secret key @var{key} according to @var{scheme}, @var{flags}
3243 and @var{opts}. If @var{opts} is not NULL, it has to be a pointer to
3244 a structure specific to the chosen scheme (gcry_ac_ssa_*_t). The
3245 signature is written to @var{io_signature}.
3248 @deftypefun gcry_error_t gcry_ac_data_verify_scheme (gcry_ac_handle_t @var{handle}, gcry_ac_scheme_t @var{scheme}, unsigned int @var{flags}, void *@var{opts}, gcry_ac_key_t @var{key}, gcry_ac_io_t *@var{io_message}, gcry_ac_io_t *@var{io_signature})
3249 Verifies through @var{handle} that the signature readable from
3250 @var{io_signature} is indeed the result of signing the message
3251 readable from @var{io_message} with the secret key belonging to the
3252 public key @var{key} according to @var{scheme} and @var{opts}. If
3253 @var{opts} is not NULL, it has to be an anonymous structure
3254 (gcry_ac_ssa_*_t) specific to the chosen scheme.
3257 @node Handle-independent functions
3258 @section Handle-independent functions
3260 @deftypefun gcry_error_t gcry_ac_id_to_name (gcry_ac_id_t @var{algorithm}, const char **@var{name})
3261 Stores the textual representation of the algorithm whose id is given
3262 in @var{algorithm} in @var{name}.
3265 @deftypefun gcry_error_t gcry_ac_name_to_id (const char *@var{name}, gcry_ac_id_t *@var{algorithm})
3266 Stores the numeric ID of the algorithm whose textual representation is
3267 contained in @var{name} in @var{algorithm}.
3270 @c **********************************************************
3271 @c ******************* Random *****************************
3272 @c **********************************************************
3273 @node Random Numbers
3274 @chapter Random Numbers
3277 * Quality of random numbers:: @acronym{Libgcrypt} uses different quality levels.
3278 * Retrieving random numbers:: How to retrieve random numbers.
3281 @node Quality of random numbers
3282 @section Quality of random numbers
3284 @acronym{Libgcypt} offers random numbers of different quality levels:
3286 @deftp {Data type} enum gcry_random_level
3287 The constants for the random quality levels are of this type.
3291 @item GCRY_WEAK_RANDOM
3292 This should not anymore be used. It has recently been changed to an
3293 alias of GCRY_STRONG_RANDOM. Use @code{gcry_create_nonce} instead.
3294 @item GCRY_STRONG_RANDOM
3295 Use this level for e.g. session keys and similar purposes.
3296 @item GCRY_VERY_STRONG_RANDOM
3297 Use this level for e.g. key material.
3300 @node Retrieving random numbers
3301 @section Retrieving random numbers
3303 @deftypefun void gcry_randomize (unsigned char *@var{buffer}, size_t @var{length}, enum gcry_random_level @var{level})
3305 Fill @var{buffer} with @var{length} random bytes using a random quality
3306 as defined by @var{level}.
3309 @deftypefun void * gcry_random_bytes (size_t @var{nbytes}, enum gcry_random_level @var{level})
3311 Allocate a memory block consisting of @var{nbytes} fresh random bytes
3312 using a random quality as defined by @var{level}.
3315 @deftypefun void * gcry_random_bytes_secure (size_t @var{nbytes}, enum gcry_random_level @var{level})
3317 Allocate a memory block consisting of @var{nbytes} fresh random bytes
3318 using a random quality as defined by @var{level}. This function
3319 differs from @code{gcry_random_bytes} in that the returned buffer is
3320 allocated in a ``secure'' area of the memory.
3323 @deftypefun void gcry_create_nonce (unsigned char *@var{buffer}, size_t @var{length})
3325 Fill @var{buffer} with @var{length} unpredictable bytes. This is
3326 commonly called a nonce and may also be used for initialization
3327 vectors and padding. This is an extra function nearly independent of
3328 the other random function for 3 reasons: It better protects the
3329 regular random generator's internal state, provides better performance
3330 and does not drain the precious entropy pool.
3336 @c **********************************************************
3337 @c ******************* S-Expressions ***********************
3338 @c **********************************************************
3340 @chapter S-expressions
3342 S-expressions are used by the public key functions to pass complex data
3343 structures around. These LISP like objects are used by some
3344 cryptographic protocols (cf. RFC-2692) and @acronym{Libgcrypt} provides functions
3345 to parse and construct them. For detailed information, see
3346 @cite{Ron Rivest, code and description of S-expressions,
3347 @uref{http://theory.lcs.mit.edu/~rivest/sexp.html}}.
3350 * Data types for S-expressions:: Data types related with S-expressions.
3351 * Working with S-expressions:: How to work with S-expressions.
3354 @node Data types for S-expressions
3355 @section Data types for S-expressions
3357 @deftp {Data type} gcry_sexp_t
3358 The @code{gcry_sexp_t} type describes an object with the @acronym{Libgcrypt} internal
3359 representation of an S-expression.
3362 @node Working with S-expressions
3363 @section Working with S-expressions
3366 There are several functions to create an @acronym{Libgcrypt} S-expression object
3367 from its external representation or from a string template. There is
3368 also a function to convert the internal representation back into one of
3369 the external formats:
3372 @deftypefun gcry_error_t gcry_sexp_new (@w{gcry_sexp_t *@var{r_sexp}}, @w{const void *@var{buffer}}, @w{size_t @var{length}}, @w{int @var{autodetect}})
3374 This is the generic function to create an new S-expression object from
3375 its external representation in @var{buffer} of @var{length} bytes. On
3376 success the result is stored at the address given by @var{r_sexp}.
3377 With @var{autodetect} set to 0, the data in @var{buffer} is expected to
3378 be in canonized format, with @var{autodetect} set to 1 the parses any of
3379 the defined external formats. If @var{buffer} does not hold a valid
3380 S-expression an error code is returned and @var{r_sexp} set to
3382 Note, that the caller is responsible for releasing the newly allocated
3383 S-expression using @code{gcry_sexp_release}.
3386 @deftypefun gcry_error_t gcry_sexp_create (@w{gcry_sexp_t *@var{r_sexp}}, @w{void *@var{buffer}}, @w{size_t @var{length}}, @w{int @var{autodetect}}, @w{void (*@var{freefnc})(void*)})
3388 This function is identical to @code{gcry_sexp_new} but has an extra
3389 argument @var{freefnc}, which, when not set to @code{NULL}, is expected
3390 to be a function to release the @var{buffer}; most likely the standard
3391 @code{free} function is used for this argument. This has the effect of
3392 transferring the ownership of @var{buffer} to the created object in
3393 @var{r_sexp}. The advantage of using this function is that @acronym{Libgcrypt}
3394 might decide to directly use the provided buffer and thus avoid extra
3398 @deftypefun gcry_error_t gcry_sexp_sscan (@w{gcry_sexp_t *@var{r_sexp}}, @w{size_t *@var{erroff}}, @w{const char *@var{buffer}}, @w{size_t @var{length}})
3400 This is another variant of the above functions. It behaves nearly
3401 identical but provides an @var{erroff} argument which will receive the
3402 offset into the buffer where the parsing stopped on error.
3405 @deftypefun gcry_error_t gcry_sexp_build (@w{gcry_sexp_t *@var{r_sexp}}, @w{size_t *@var{erroff}}, @w{const char *@var{format}, ...})
3407 This function creates an internal S-expression from the string template
3408 @var{format} and stores it at the address of @var{r_sexp}. If there is a
3409 parsing error, the function returns an appropriate error code and stores
3410 the offset into @var{format} where the parsing stopped in @var{erroff}.
3411 The function supports a couple of printf-like formatting characters and
3412 expects arguments for some of these escape sequences right after
3413 @var{format}. The following format characters are defined:
3417 The next argument is expected to be of type @code{gcry_mpi_t} and a copy of
3418 its value is inserted into the resulting S-expression.
3420 The next argument is expected to be of type @code{char *} and that
3421 string is inserted into the resulting S-expression.
3423 The next argument is expected to be of type @code{int} and its value is
3424 inserted into the resulting S-expression.
3426 The next argument is expected to be of type @code{int} directly
3427 followed by an argument of type @code{char *}. This represents a
3428 buffer of given length to be inserted into the resulting regular
3433 No other format characters are defined and would return an error. Note,
3434 that the format character @samp{%%} does not exists, because a percent
3435 sign is not a valid character in an S-expression.
3438 @deftypefun void gcry_sexp_release (@w{gcry_sexp_t @var{sexp}})
3440 Release the S-expression object @var{sexp}.
3445 The next 2 functions are used to convert the internal representation
3446 back into a regular external S-expression format and to show the
3447 structure for debugging.
3449 @deftypefun size_t gcry_sexp_sprint (@w{gcry_sexp_t @var{sexp}}, @w{int @var{mode}}, @w{char *@var{buffer}}, @w{size_t @var{maxlength}})
3451 Copies the S-expression object @var{sexp} into @var{buffer} using the
3452 format specified in @var{mode}. @var{maxlength} must be set to the
3453 allocated length of @var{buffer}. The function returns the actual
3454 length of valid bytes put into @var{buffer} or 0 if the provided buffer
3455 is too short. Passing @code{NULL} for @var{buffer} returns the required
3456 length for @var{buffer}. For convenience reasons an extra byte with
3457 value 0 is appended to the buffer.
3460 The following formats are supported:
3463 @item GCRYSEXP_FMT_DEFAULT
3464 Returns a convenient external S-expression representation.
3466 @item GCRYSEXP_FMT_CANON
3467 Return the S-expression in canonical format.
3469 @item GCRYSEXP_FMT_BASE64
3470 Not currently supported.
3472 @item GCRYSEXP_FMT_ADVANCED
3473 Returns the S-expression in advanced format.
3477 @deftypefun void gcry_sexp_dump (@w{gcry_sexp_t @var{sexp}})
3479 Dumps @var{sexp} in a format suitable for debugging to @acronym{Libgcrypt}'s
3484 Often canonical encoding is used in the external representation. The
3485 following function can be used to check for valid encoding and to learn
3486 the length of the S-expression"
3488 @deftypefun size_t gcry_sexp_canon_len (@w{const unsigned char *@var{buffer}}, @w{size_t @var{length}}, @w{size_t *@var{erroff}}, @w{int *@var{errcode}})
3490 Scan the canonical encoded @var{buffer} with implicit length values and
3491 return the actual length this S-expression uses. For a valid S-expression
3492 it should never return 0. If @var{length} is not 0, the maximum
3493 length to scan is given; this can be used for syntax checks of
3494 data passed from outside. @var{errcode} and @var{erroff} may both be
3495 passed as @code{NULL}.
3501 There are a couple of functions to parse S-expressions and retrieve
3504 @deftypefun gcry_sexp_t gcry_sexp_find_token (@w{const gcry_sexp_t @var{list}}, @w{const char *@var{token}}, @w{size_t @var{toklen}})
3506 Scan the S-expression for a sublist with a type (the car of the list)
3507 matching the string @var{token}. If @var{toklen} is not 0, the token is
3508 assumed to be raw memory of this length. The function returns a newly
3509 allocated S-expression consisting of the found sublist or @code{NULL}
3514 @deftypefun int gcry_sexp_length (@w{const gcry_sexp_t @var{list}})
3516 Return the length of the @var{list}. For a valid S-expression this
3517 should be at least 1.
3521 @deftypefun gcry_sexp_t gcry_sexp_nth (@w{const gcry_sexp_t @var{list}}, @w{int @var{number}})
3523 Create and return a new S-expression from the element with index @var{number} in
3524 @var{list}. Note that the first element has the index 0. If there is
3525 no such element, @code{NULL} is returned.
3528 @deftypefun gcry_sexp_t gcry_sexp_car (@w{const gcry_sexp_t @var{list}})
3530 Create and return a new S-expression from the first element in
3531 @var{list}; this called the "type" and should always exist and be a
3532 string. @code{NULL} is returned in case of a problem.
3535 @deftypefun gcry_sexp_t gcry_sexp_cdr (@w{const gcry_sexp_t @var{list}})
3537 Create and return a new list form all elements except for the first one.
3538 Note, that this function may return an invalid S-expression because it
3539 is not guaranteed, that the type exists and is a string. However, for
3540 parsing a complex S-expression it might be useful for intermediate
3541 lists. Returns @code{NULL} on error.
3545 @deftypefun {const char *} gcry_sexp_nth_data (@w{const gcry_sexp_t @var{list}}, @w{int @var{number}}, @w{size_t *@var{datalen}})
3547 This function is used to get data from a @var{list}. A pointer to the
3548 actual data with index @var{number} is returned and the length of this
3549 data will be stored to @var{datalen}. If there is no data at the given
3550 index or the index represents another list, @code{NULL} is returned.
3551 @strong{Caution:} The returned pointer is valid as long as @var{list} is
3552 not modified or released.
3555 Here is an example on how to extract and print the surname (Meier) from
3556 the S-expression @samp{(Name Otto Meier (address Burgplatz 3))}:
3562 name = gcry_sexp_nth_data (list, 2, &len);
3563 printf ("my name is %.*s\n", (int)len, name);
3567 @deftypefun gcry_mpi_t gcry_sexp_nth_mpi (@w{gcry_sexp_t @var{list}}, @w{int @var{number}}, @w{int @var{mpifmt}})
3569 This function is used to get and convert data from a @var{list}. This
3570 data is assumed to be an MPI stored in the format described by
3571 @var{mpifmt} and returned as a standard @acronym{Libgcrypt} MPI. The caller must
3572 release this returned value using @code{gcry_mpi_release}. If there is
3573 no data at the given index, the index represents a list or the value
3574 can't be converted to an MPI, @code{NULL} is returned.
3578 @c **********************************************************
3579 @c ******************* MPIs ******** ***********************
3580 @c **********************************************************
3582 @chapter MPI library
3585 * Data types:: MPI related data types.
3586 * Basic functions:: First steps with MPI numbers.
3587 * MPI formats:: External representation of MPIs.
3588 * Calculations:: Performing MPI calculations.
3589 * Comparisons:: How to compare MPI values.
3590 * Bit manipulations:: How to access single bits of MPI values.
3591 * Miscellaneous:: Miscellaneous MPI functions.
3594 Public key cryptography is based on mathematics with large numbers. To
3595 implement the public key functions, a library for handling these large
3596 numbers is required. Because of the general usefulness of such a
3597 library, its interface is exposed by @acronym{Libgcrypt}. The implementation is
3598 based on an old release of GNU Multi-Precision Library (GMP) but in the
3599 meantime heavily modified and stripped down to what is required for
3600 cryptography. For a lot of CPUs, high performance assembler
3601 implementations of some very low level functions are used to gain much
3602 better performance than with the standard C implementation.
3605 In the context of @acronym{Libgcrypt} and in most other applications, these large
3606 numbers are called MPIs (multi-precision-integers).
3611 @deftp {Data type} gcry_mpi_t
3612 The @code{gcry_mpi_t} type represents an object to hold an MPI.
3615 @node Basic functions
3616 @section Basic functions
3619 To work with MPIs, storage must be allocated and released for the
3620 numbers. This can be done with one of these functions:
3622 @deftypefun gcry_mpi_t gcry_mpi_new (@w{unsigned int @var{nbits}})
3624 Allocate a new MPI object, initialize it to 0 and initially allocate
3625 enough memory for a number of at least @var{nbits}. This pre-allocation is
3626 only a small performance issue and not actually necessary because
3627 @acronym{Libgcrypt} automatically re-allocates the required memory.
3630 @deftypefun gcry_mpi_t gcry_mpi_snew (@w{unsigned int @var{nbits}})
3632 This is identical to @code{gcry_mpi_new} but allocates the MPI in the so
3633 called "secure memory" which in turn will take care that all derived
3634 values will also be stored in this "secure memory". Use this for highly
3635 confidential data like private key parameters.
3638 @deftypefun gcry_mpi_t gcry_mpi_copy (@w{const gcry_mpi_t @var{a}})
3640 Create a new MPI as the exact copy of @var{a}.
3644 @deftypefun void gcry_mpi_release (@w{gcry_mpi_t @var{a}})
3646 Release the MPI @var{a} and free all associated resources. Passing
3647 @code{NULL} is allowed and ignored. When a MPI stored in the "secure
3648 memory" is released, that memory gets wiped out immediately.
3652 The simplest operations are used to assign a new value to an MPI:
3654 @deftypefun gcry_mpi_t gcry_mpi_set (@w{gcry_mpi_t @var{w}}, @w{const gcry_mpi_t @var{u}})
3656 Assign the value of @var{u} to @var{w} and return @var{w}. If
3657 @code{NULL} is passed for @var{w}, a new MPI is allocated, set to the
3658 value of @var{u} and returned.
3661 @deftypefun gcry_mpi_t gcry_mpi_set_ui (@w{gcry_mpi_t @var{w}}, @w{unsigned long @var{u}})
3663 Assign the value of @var{u} to @var{w} and return @var{w}. If
3664 @code{NULL} is passed for @var{w}, a new MPI is allocated, set to the
3665 value of @var{u} and returned. This function takes an @code{unsigned
3666 int} as type for @var{u} and thus it is only possible to set @var{w} to
3667 small values (usually up to the word size of the CPU).
3670 @deftypefun void gcry_mpi_swap (@w{gcry_mpi_t @var{a}}, @w{gcry_mpi_t @var{b}})
3672 Swap the values of @var{a} and @var{b}.
3676 @section MPI formats
3679 The following functions are used to convert between an external
3680 representation of an MPI and the internal one of @acronym{Libgcrypt}.
3682 @deftypefun gcry_error_t gcry_mpi_scan (@w{gcry_mpi_t *@var{r_mpi}}, @w{enum gcry_mpi_format @var{format}}, @w{const unsigned char *@var{buffer}}, @w{size_t @var{buflen}}, @w{size_t *@var{nscanned}})
3684 Convert the external representation of an integer stored in @var{buffer}
3685 with a length of @var{buflen} into a newly created MPI returned which
3686 will be stored at the address of @var{r_mpi}. For certain formats the
3687 length argument is not required and may be passed as @code{0}. After a
3688 successful operation the variable @var{nscanned} receives the number of
3689 bytes actually scanned unless @var{nscanned} was given as
3690 @code{NULL}. @var{format} describes the format of the MPI as stored in
3694 @item GCRYMPI_FMT_STD
3695 2-complement stored without a length header.
3697 @item GCRYMPI_FMT_PGP
3698 As used by OpenPGP (only defined as unsigned). This is basically
3699 @code{GCRYMPI_FMT_STD} with a 2 byte big endian length header.
3701 @item GCRYMPI_FMT_SSH
3702 As used in the Secure Shell protocol. This is @code{GCRYMPI_FMT_STD}
3703 with a 4 byte big endian header.
3705 @item GCRYMPI_FMT_HEX
3706 Stored as a C style string with each byte of the MPI encoded as 2 hex
3707 digits. When using this format, @var{buflen} must be zero.
3709 @item GCRYMPI_FMT_USG
3710 Simple unsigned integer.
3714 Note, that all of the above formats store the integer in big-endian
3719 @deftypefun gcry_error_t gcry_mpi_print (@w{enum gcry_mpi_format @var{format}}, @w{unsigned char *@var{buffer}}, @w{size_t @var{buflen}}, @w{size_t *@var{nwritten}}, @w{const gcry_mpi_t @var{a}})
3721 Convert the MPI @var{a} into an external representation described by
3722 @var{format} (see above) and store it in the provided @var{buffer}
3723 which has a usable length of at least the @var{buflen} bytes. If
3724 @var{nwritten} is not NULL, it will receive the number of bytes
3725 actually stored in @var{buffer} after a successful operation.
3728 @deftypefun gcry_error_t gcry_mpi_aprint (@w{enum gcry_mpi_format @var{format}}, @w{unsigned char **@var{buffer}}, @w{size_t *@var{nbytes}}, @w{const gcry_mpi_t @var{a}})
3730 Convert the MPI @var{a} into an external representation described by
3731 @var{format} (see above) and store it in a newly allocated buffer which
3732 address will be stored in the variable @var{buffer} points to. The
3733 number of bytes stored in this buffer will be stored in the variable
3734 @var{nbytes} points to, unless @var{nbytes} is @code{NULL}.
3737 @deftypefun void gcry_mpi_dump (@w{const gcry_mpi_t @var{a}})
3739 Dump the value of @var{a} in a format suitable for debugging to
3740 Libgcrypt's logging stream. Note that one leading space but no trailing
3741 space or linefeed will be printed. It is okay to pass @code{NULL} for
3747 @section Calculations
3750 Basic arithmetic operations:
3752 @deftypefun void gcry_mpi_add (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}})
3754 @math{@var{w} = @var{u} + @var{v}}.
3758 @deftypefun void gcry_mpi_add_ui (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{v}})
3760 @math{@var{w} = @var{u} + @var{v}}. Note, that @var{v} is an unsigned integer.
3764 @deftypefun void gcry_mpi_addm (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}}, @w{gcry_mpi_t @var{m}})
3766 @math{@var{w} = @var{u} + @var{v} \bmod @var{m}}.
3769 @deftypefun void gcry_mpi_sub (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}})
3771 @math{@var{w} = @var{u} - @var{v}}.
3774 @deftypefun void gcry_mpi_sub_ui (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{v}})
3776 @math{@var{w} = @var{u} - @var{v}}. @var{v} is an unsigned integer.
3779 @deftypefun void gcry_mpi_subm (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}}, @w{gcry_mpi_t @var{m}})
3781 @math{@var{w} = @var{u} - @var{v} \bmod @var{m}}.
3784 @deftypefun void gcry_mpi_mul (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}})
3786 @math{@var{w} = @var{u} * @var{v}}.
3789 @deftypefun void gcry_mpi_mul_ui (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{v}})
3791 @math{@var{w} = @var{u} * @var{v}}. @var{v} is an unsigned integer.
3794 @deftypefun void gcry_mpi_mulm (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}}, @w{gcry_mpi_t @var{m}})
3796 @math{@var{w} = @var{u} * @var{v} \bmod @var{m}}.
3799 @deftypefun void gcry_mpi_mul_2exp (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{e}})
3801 @c FIXME: I am in need for a real TeX{info} guru:
3802 @c I don't know why TeX can grok @var{e} here.
3803 @math{@var{w} = @var{u} * 2^e}.
3806 @deftypefun void gcry_mpi_div (@w{gcry_mpi_t @var{q}}, @w{gcry_mpi_t @var{r}}, @w{gcry_mpi_t @var{dividend}}, @w{gcry_mpi_t @var{divisor}}, @w{int @var{round}})
3808 @math{@var{q} = @var{dividend} / @var{divisor}}, @math{@var{r} =
3809 @var{dividend} \bmod @var{divisor}}. @var{q} and @var{r} may be passed
3810 as @code{NULL}. @var{round} should be negative or 0.
3813 @deftypefun void gcry_mpi_mod (@w{gcry_mpi_t @var{r}}, @w{gcry_mpi_t @var{dividend}}, @w{gcry_mpi_t @var{divisor}})
3815 @math{@var{r} = @var{dividend} \bmod @var{divisor}}.
3818 @deftypefun void gcry_mpi_powm (@w{gcry_mpi_t @var{w}}, @w{const gcry_mpi_t @var{b}}, @w{const gcry_mpi_t @var{e}}, @w{const gcry_mpi_t @var{m}})
3820 @c I don't know why TeX can grok @var{e} here.
3821 @math{@var{w} = @var{b}^e \bmod @var{m}}.
3824 @deftypefun int gcry_mpi_gcd (@w{gcry_mpi_t @var{g}}, @w{gcry_mpi_t @var{a}}, @w{gcry_mpi_t @var{b}})
3826 Set @var{g} to the greatest common divisor of @var{a} and @var{b}.
3827 Return true if the @var{g} is 1.
3830 @deftypefun int gcry_mpi_invm (@w{gcry_mpi_t @var{x}}, @w{gcry_mpi_t @var{a}}, @w{gcry_mpi_t @var{m}})
3832 Set @var{x} to the multiplicative inverse of @math{@var{a} \bmod @var{m}}.
3833 Return true if the inverse exists.
3838 @section Comparisons
3841 The next 2 functions are used to compare MPIs:
3844 @deftypefun int gcry_mpi_cmp (@w{const gcry_mpi_t @var{u}}, @w{const gcry_mpi_t @var{v}})
3846 Compare the big integer number @var{u} and @var{v} returning 0 for
3847 equality, a positive value for @var{u} > @var{v} and a negative for
3851 @deftypefun int gcry_mpi_cmp_ui (@w{const gcry_mpi_t @var{u}}, @w{unsigned long @var{v}})
3853 Compare the big integer number @var{u} with the unsigned integer @var{v}
3854 returning 0 for equality, a positive value for @var{u} > @var{v} and a
3855 negative for @var{u} < @var{v}.
3859 @node Bit manipulations
3860 @section Bit manipulations
3863 There are a couple of functions to get information on arbitrary bits
3864 in an MPI and to set or clear them:
3866 @deftypefun {unsigned int} gcry_mpi_get_nbits (@w{gcry_mpi_t @var{a}})
3868 Return the number of bits required to represent @var{a}.
3871 @deftypefun int gcry_mpi_test_bit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3873 Return true if bit number @var{n} (counting from 0) is set in @var{a}.
3876 @deftypefun void gcry_mpi_set_bit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3878 Set bit number @var{n} in @var{a}.
3881 @deftypefun void gcry_mpi_clear_bit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3883 Clear bit number @var{n} in @var{a}.
3886 @deftypefun void gcry_mpi_set_highbit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3888 Set bit number @var{n} in @var{a} and clear all bits greater than @var{n}.
3891 @deftypefun void gcry_mpi_clear_highbit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3893 Clear bit number @var{n} in @var{a} and all bits greater than @var{n}.
3896 @deftypefun void gcry_mpi_rshift (@w{gcry_mpi_t @var{x}}, @w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3898 Shift the value of @var{a} by @var{n} bits to the right and store the
3903 @section Miscellaneous
3905 @deftypefun gcry_mpi_t gcry_mpi_set_opaque (@w{gcry_mpi_t @var{a}}, @w{void *@var{p}}, @w{unsigned int @var{nbits}})
3907 Store @var{nbits} of the value @var{p} points to in @var{a} and mark
3908 @var{a} as an opaque value (i.e. an value that can't be used for any
3909 math calculation and is only used to store an arbitrary bit pattern in
3912 WARNING: Never use an opaque MPI for actual math operations. The only
3913 valid functions are gcry_mpi_get_opaque and gcry_mpi_release. Use
3914 gcry_mpi_scan to convert a string of arbitrary bytes into an MPI.
3918 @deftypefun {void *} gcry_mpi_get_opaque (@w{gcry_mpi_t @var{a}}, @w{unsigned int *@var{nbits}})
3920 Return a pointer to an opaque value stored in @var{a} and return its
3921 size in @var{nbits}. Note, that the returned pointer is still owned by
3922 @var{a} and that the function should never be used for an non-opaque
3926 @deftypefun void gcry_mpi_set_flag (@w{gcry_mpi_t @var{a}}, @w{enum gcry_mpi_flag @var{flag}})
3928 Set the @var{flag} for the MPI @var{a}. Currently only the flag
3929 @code{GCRYMPI_FLAG_SECURE} is allowed to convert @var{a} into an MPI
3930 stored in "secure memory".
3933 @deftypefun void gcry_mpi_clear_flag (@w{gcry_mpi_t @var{a}}, @w{enum gcry_mpi_flag @var{flag}})
3935 Clear @var{flag} for the big integer @var{a}. Note, that this function is
3936 currently useless as no flags are allowed.
3939 @deftypefun int gcry_mpi_get_flag (@w{gcry_mpi_t @var{a}}, @w{enum gcry_mpi_flag @var{flag}})
3941 Return true when the @var{flag} is set for @var{a}.
3944 @deftypefun void gcry_mpi_randomize (@w{gcry_mpi_t @var{w}}, @w{unsigned int @var{nbits}}, @w{enum gcry_random_level @var{level}})
3946 Set the big integer @var{w} to a random value of @var{nbits}, using
3947 random data quality of level @var{level}. In case @var{nbits} is not
3948 a multiple of a byte, @var{nbits} is rounded up to the next byte
3952 @c **********************************************************
3953 @c ******************** Prime numbers ***********************
3954 @c **********************************************************
3956 @chapter Prime numbers
3959 * Generation:: Generation of new prime numbers.
3960 * Checking:: Checking if a given number is prime.
3966 @deftypefun gcry_error_t gcry_prime_generate (gcry_mpi_t *@var{prime},
3967 unsigned int @var{prime_bits}, unsigned int @var{factor_bits},
3968 gcry_mpi_t **@var{factors}, gcry_prime_check_func_t @var{cb_func},
3969 void *@var{cb_arg}, gcry_random_level_t @var{random_level},
3970 unsigned int @var{flags})
3972 Generate a new prime number of @var{prime_bits} bits and store it in
3973 @var{prime}. If @var{factor_bits} is non-zero, one of the prime factors
3974 of (@var{prime} - 1) / 2 must be @var{factor_bits} bits long. If
3975 @var{factors} is non-zero, allocate a new, @code{NULL}-terminated array
3976 holding the prime factors and store it in @var{factors}. @var{flags}
3977 might be used to influence the prime number generation process.
3980 @deftypefun gcry_prime_group_generator (gcry_mpi_t *@var{r_g},
3981 gcry_mpi_t @var{prime}, gcry_mpi_t *@var{factors}, gcry_mpi_t @var{start_g})
3983 Find a generator for @var{prime} where the factorization of (@var{prime}
3984 - 1) is in the @code{NULL} terminated array @var{factors}. Return the
3985 generator as a newly allocated MPI in @var{r_g}. If @var{start_g} is
3986 not NULL, use this as the start for the search.
3989 @deftypefun void gcry_prime_release_factors (gcry_mpi_t *@var{factors})
3991 Convenience function to release the @var{factors} array.
3997 @deftypefun gcry_error_t gcry_prime_check (gcry_mpi_t @var{p},
3998 unsigned int @var{flags})
4000 Check wether the number @var{p} is prime. Returns zero in case @var{p}
4001 is indeed a prime, returns @code{GPG_ERR_NO_PRIME} in case @var{p} is
4002 not a prime and a different error code in case something went horribly
4010 * Memory allocation:: Functions related with memory allocation.
4013 @node Memory allocation
4014 @section Memory allocation
4016 @deftypefun void *gcry_malloc (size_t @var{n})
4018 This function tries to allocate @var{n} bytes of memory. On success
4019 it returns a pointer to the memory area, in an out-of-core condition,
4023 @deftypefun void *gcry_malloc_secure (size_t @var{n})
4024 Like @code{gcry_malloc}, but uses secure memory.
4027 @deftypefun void *gcry_calloc (size_t @var{n})
4029 This function tries to allocate @var{n} bytes of cleared memory
4030 (i.e. memory that is initialized with zero bytes). On success it
4031 returns a pointer to the memory area, in an out-of-core condition, it
4035 @deftypefun void *gcry_calloc_secure (size_t @var{n})
4036 Like @code{gcry_calloc}, but uses secure memory.
4039 @deftypefun void *gcry_realloc (void *@var{p}, size_t @var{n})
4041 This function tries to resize the memory area pointed to by @var{p} to
4042 @var{n} bytes. On success it returns a pointer to the new memory
4043 area, in an out-of-core condition, it returns NULL. Depending on
4044 whether the memory pointed to by @var{p} is secure memory or not,
4045 gcry_realloc tries to use secure memory as well.
4048 @deftypefun void gcry_free (void *@var{p})
4049 Release the memory area pointed to by @var{p}.
4052 @c **********************************************************
4053 @c ******************* Appendices *************************
4054 @c **********************************************************
4061 @unnumbered Concept Index
4065 @node Function and Data Index
4066 @unnumbered Function and Data Index
4072 /* Version check should be the very first gcry call because it
4073 makes sure that constructor functions are run. */
4074 if (!gcry_check_version (GCRYPT_VERSION))
4075 die ("version mismatch\n");
4076 /* Many applications don't require secure memory, so they should
4077 disable it right away. There won't be a problem unless one makes
4078 use of a feature which requires secure memory - in that case the
4079 process would abort because the secmem is not initialized. */
4080 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
4082 /* .. add whatever initialization you want, but better don't make calls
4083 to libgcrypt from more than one thread ... */
4085 /* Tell Libgcrypt that initialization has completed. */
4086 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
4089 If you require secure memory, this code should be used:
4091 if (!gcry_check_version (GCRYPT_VERSION))
4092 die ("version mismatch\n");
4093 /* We don't want to see any warnings, e.g. because we have not yet
4094 parsed options which might be used to suppress such warnings */
4095 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
4099 /* Allocate a pool of 16k secure memory. This also drops privileges
4101 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
4103 /* It is now okay to let Libgcrypt complain when there was/is a problem
4104 with the secure memory. */
4105 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
4107 /* Tell Libgcrypt that initialization has completed. */
4108 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
4111 This sounds a bit complicated but has the advantage that the caller
4112 must decide whether he wants secure memory or not - there is no
4115 It is important that this initialization is not done by a library but
4116 in the application. The library might want to check for finished
4117 initialization using:
4119 if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P))
4120 return MYLIB_ERROR_LIBGCRYPT_NOT_INITIALIZED;
4123 @c LocalWords: int HD