aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/libgcrypt/libgcrypt-1.2.2/doc/gcrypt.texi
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/libgcrypt/libgcrypt-1.2.2/doc/gcrypt.texi')
-rw-r--r--[-rwxr-xr-x]linden/indra/libgcrypt/libgcrypt-1.2.2/doc/gcrypt.texi7512
1 files changed, 3756 insertions, 3756 deletions
diff --git a/linden/indra/libgcrypt/libgcrypt-1.2.2/doc/gcrypt.texi b/linden/indra/libgcrypt/libgcrypt-1.2.2/doc/gcrypt.texi
index 76b5977..aece0af 100755..100644
--- a/linden/indra/libgcrypt/libgcrypt-1.2.2/doc/gcrypt.texi
+++ b/linden/indra/libgcrypt/libgcrypt-1.2.2/doc/gcrypt.texi
@@ -1,3756 +1,3756 @@
1\input texinfo @c -*- Texinfo -*- 1\input texinfo @c -*- Texinfo -*-
2@c %**start of header 2@c %**start of header
3@setfilename gcrypt.info 3@setfilename gcrypt.info
4@include version.texi 4@include version.texi
5@settitle The Libgcrypt Reference Manual 5@settitle The Libgcrypt Reference Manual
6@c Unify some of the indices. 6@c Unify some of the indices.
7@syncodeindex tp fn 7@syncodeindex tp fn
8@syncodeindex pg fn 8@syncodeindex pg fn
9@c %**end of header 9@c %**end of header
10@copying 10@copying
11This manual is for Libgcrypt 11This manual is for Libgcrypt
12(version @value{VERSION}, @value{UPDATED}), 12(version @value{VERSION}, @value{UPDATED}),
13which is GNU's library of cryptographic building blocks. 13which is GNU's library of cryptographic building blocks.
14 14
15Copyright @copyright{} 2000, 2002, 2003, 2004 Free Software Foundation, Inc. 15Copyright @copyright{} 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
16 16
17@quotation 17@quotation
18Permission is granted to copy, distribute and/or modify this document 18Permission is granted to copy, distribute and/or modify this document
19under the terms of the GNU General Public License as published by the 19under the terms of the GNU General Public License as published by the
20Free Software Foundation; either version 2 of the License, or (at your 20Free Software Foundation; either version 2 of the License, or (at your
21option) any later version. The text of the license can be found in the 21option) any later version. The text of the license can be found in the
22section entitled ``Copying''. 22section entitled ``Copying''.
23@end quotation 23@end quotation
24@end copying 24@end copying
25 25
26@dircategory GNU Libraries 26@dircategory GNU Libraries
27@direntry 27@direntry
28* libgcrypt: (gcrypt). Cryptographic function library. 28* libgcrypt: (gcrypt). Cryptographic function library.
29@end direntry 29@end direntry
30 30
31 31
32 32
33@c 33@c
34@c Titlepage 34@c Titlepage
35@c 35@c
36@setchapternewpage odd 36@setchapternewpage odd
37@titlepage 37@titlepage
38@title The Libgcrypt Reference Manual 38@title The Libgcrypt Reference Manual
39@subtitle Version @value{VERSION} 39@subtitle Version @value{VERSION}
40@subtitle @value{UPDATED} 40@subtitle @value{UPDATED}
41@author Werner Koch (@email{wk@@gnupg.org}) 41@author Werner Koch (@email{wk@@gnupg.org})
42@author Moritz Schulte (@email{mo@@g10code.com}) 42@author Moritz Schulte (@email{mo@@g10code.com})
43 43
44@page 44@page
45@vskip 0pt plus 1filll 45@vskip 0pt plus 1filll
46@insertcopying 46@insertcopying
47@end titlepage 47@end titlepage
48 48
49 49
50@summarycontents 50@summarycontents
51@contents 51@contents
52@page 52@page
53 53
54 54
55@ifnottex 55@ifnottex
56@node Top 56@node Top
57@top The Libgcrypt Library 57@top The Libgcrypt Library
58@insertcopying 58@insertcopying
59@end ifnottex 59@end ifnottex
60 60
61 61
62@menu 62@menu
63* Introduction:: What is @acronym{Libgcrypt}. 63* Introduction:: What is @acronym{Libgcrypt}.
64* Preparation:: What you should do before using the library. 64* Preparation:: What you should do before using the library.
65* Generalities:: General library functions and data types. 65* Generalities:: General library functions and data types.
66* Handler Functions:: Working with handler functions. 66* Handler Functions:: Working with handler functions.
67* Symmetric cryptography:: How to use symmetric cryptography. 67* Symmetric cryptography:: How to use symmetric cryptography.
68* Hashing:: How to use hashing. 68* Hashing:: How to use hashing.
69* Public Key cryptography (I):: How to use public key cryptography. 69* Public Key cryptography (I):: How to use public key cryptography.
70* Public Key cryptography (II):: How to use public key cryptography, alternatively. 70* Public Key cryptography (II):: How to use public key cryptography, alternatively.
71* Random Numbers:: How to work with random numbers. 71* Random Numbers:: How to work with random numbers.
72* S-expressions:: How to manage S-expressions. 72* S-expressions:: How to manage S-expressions.
73* MPI library:: How to work with multi-precision-integers. 73* MPI library:: How to work with multi-precision-integers.
74* Utilities:: Utility functions. 74* Utilities:: Utility functions.
75 75
76Appendices 76Appendices
77 77
78* Library Copying:: The GNU Lesser General Public License 78* Library Copying:: The GNU Lesser General Public License
79 says how you can copy and share `Libgcrypt'. 79 says how you can copy and share `Libgcrypt'.
80* Copying:: The GNU General Public License says how you 80* Copying:: The GNU General Public License says how you
81 can copy and share some parts of `Libgcrypt'. 81 can copy and share some parts of `Libgcrypt'.
82 82
83Indices 83Indices
84 84
85* Concept Index:: Index of concepts and programs. 85* Concept Index:: Index of concepts and programs.
86* Function and Data Index:: Index of functions, variables and data types. 86* Function and Data Index:: Index of functions, variables and data types.
87 87
88@detailmenu 88@detailmenu
89 --- The Detailed Node Listing --- 89 --- The Detailed Node Listing ---
90 90
91Introduction 91Introduction
92* Getting Started:: How to use this manual. 92* Getting Started:: How to use this manual.
93* Features:: A glance at @acronym{Libgcrypt}'s features. 93* Features:: A glance at @acronym{Libgcrypt}'s features.
94* Overview:: Overview about the library. 94* Overview:: Overview about the library.
95 95
96Preparation 96Preparation
97* Header:: What header file you need to include. 97* Header:: What header file you need to include.
98* Building sources:: How to build sources using the library. 98* Building sources:: How to build sources using the library.
99* Building sources using Automake:: How to build sources with the help of Automake. 99* Building sources using Automake:: How to build sources with the help of Automake.
100* Initializing the library:: How to initialize the library. 100* Initializing the library:: How to initialize the library.
101* Multi Threading:: How @acronym{Libgcrypt} can be used in a MT environment. 101* Multi Threading:: How @acronym{Libgcrypt} can be used in a MT environment.
102 102
103Generalities 103Generalities
104* Controlling the library:: Controlling @acronym{Libgcrypt}'s behavior. 104* Controlling the library:: Controlling @acronym{Libgcrypt}'s behavior.
105* Modules:: Description of extension modules. 105* Modules:: Description of extension modules.
106* Error Handling:: Error codes and such. 106* Error Handling:: Error codes and such.
107 107
108Handler Functions 108Handler Functions
109* Progress handler:: Using a progress handler function. 109* Progress handler:: Using a progress handler function.
110* Allocation handler:: Using special memory allocation functions. 110* Allocation handler:: Using special memory allocation functions.
111* Error handler:: Using error handler functions. 111* Error handler:: Using error handler functions.
112* Logging handler:: Using a special logging function. 112* Logging handler:: Using a special logging function.
113 113
114Symmetric cryptography 114Symmetric cryptography
115* Available ciphers:: List of ciphers supported by the library. 115* Available ciphers:: List of ciphers supported by the library.
116* Cipher modules:: How to work with cipher modules. 116* Cipher modules:: How to work with cipher modules.
117* Available cipher modes:: List of cipher modes supported by the library. 117* Available cipher modes:: List of cipher modes supported by the library.
118* Working with cipher handles:: How to perform operations related to cipher handles. 118* Working with cipher handles:: How to perform operations related to cipher handles.
119* General cipher functions:: General cipher functions independent of cipher handles. 119* General cipher functions:: General cipher functions independent of cipher handles.
120 120
121Hashing 121Hashing
122* Available hash algorithms:: List of hash algorithms supported by the library. 122* Available hash algorithms:: List of hash algorithms supported by the library.
123* Hash algorithm modules:: How to work with hash algorithm modules. 123* Hash algorithm modules:: How to work with hash algorithm modules.
124* Working with hash algorithms:: List of functions related to hashing. 124* Working with hash algorithms:: List of functions related to hashing.
125 125
126Public Key cryptography (I) 126Public Key cryptography (I)
127* Used S-expressions:: Introduction into the used S-expression. 127* Used S-expressions:: Introduction into the used S-expression.
128* Available algorithms:: Algorithms supported by the library. 128* Available algorithms:: Algorithms supported by the library.
129* Public key modules:: How to work with public key modules. 129* Public key modules:: How to work with public key modules.
130* Cryptographic Functions:: Functions for performing the cryptographic actions. 130* Cryptographic Functions:: Functions for performing the cryptographic actions.
131* General public-key related Functions:: General functions, not implementing any cryptography. 131* General public-key related Functions:: General functions, not implementing any cryptography.
132 132
133Public Key cryptography (II) 133Public Key cryptography (II)
134* Available asymmetric algorithms:: List of algorithms supported by the library. 134* Available asymmetric algorithms:: List of algorithms supported by the library.
135* Working with sets of data:: How to work with sets of data. 135* Working with sets of data:: How to work with sets of data.
136* Working with handles:: How to use handles. 136* Working with handles:: How to use handles.
137* Working with keys:: How to work with keys. 137* Working with keys:: How to work with keys.
138* Using cryptographic functions:: How to perform cryptographic operations. 138* Using cryptographic functions:: How to perform cryptographic operations.
139* Handle-independent functions:: General functions independent of handles. 139* Handle-independent functions:: General functions independent of handles.
140 140
141Random Numbers 141Random Numbers
142* Quality of random numbers:: @acronym{Libgcrypt} uses different quality levels. 142* Quality of random numbers:: @acronym{Libgcrypt} uses different quality levels.
143* Retrieving random numbers:: How to retrieve random numbers. 143* Retrieving random numbers:: How to retrieve random numbers.
144 144
145S-expressions 145S-expressions
146* Data types for S-expressions:: Data types related with S-expressions. 146* Data types for S-expressions:: Data types related with S-expressions.
147* Working with S-expressions:: How to work with S-expressions. 147* Working with S-expressions:: How to work with S-expressions.
148 148
149MPI library 149MPI library
150* Data types:: MPI related data types. 150* Data types:: MPI related data types.
151* Basic functions:: First steps with MPI numbers. 151* Basic functions:: First steps with MPI numbers.
152* MPI formats:: External representation of MPIs. 152* MPI formats:: External representation of MPIs.
153* Calculations:: Performing MPI calculations. 153* Calculations:: Performing MPI calculations.
154* Comparisons:: How to compare MPI values. 154* Comparisons:: How to compare MPI values.
155* Bit manipulations:: How to access single bits of MPI values. 155* Bit manipulations:: How to access single bits of MPI values.
156* Miscellaneous:: Miscellaneous MPI functions. 156* Miscellaneous:: Miscellaneous MPI functions.
157 157
158Utilities 158Utilities
159* Memory allocation:: Functions related with memory allocation. 159* Memory allocation:: Functions related with memory allocation.
160 160
161@end detailmenu 161@end detailmenu
162 162
163@end menu 163@end menu
164 164
165 165
166 166
167@c ********************************************************** 167@c **********************************************************
168@c ******************* Introduction *********************** 168@c ******************* Introduction ***********************
169@c ********************************************************** 169@c **********************************************************
170@node Introduction 170@node Introduction
171@chapter Introduction 171@chapter Introduction
172`@acronym{Libgcrypt}' is a library providing cryptographic building blocks. 172`@acronym{Libgcrypt}' is a library providing cryptographic building blocks.
173 173
174@menu 174@menu
175* Getting Started:: How to use this manual. 175* Getting Started:: How to use this manual.
176* Features:: A glance at @acronym{Libgcrypt}'s features. 176* Features:: A glance at @acronym{Libgcrypt}'s features.
177* Overview:: Overview about the library. 177* Overview:: Overview about the library.
178@end menu 178@end menu
179 179
180@node Getting Started 180@node Getting Started
181@section Getting Started 181@section Getting Started
182 182
183This manual documents the `@acronym{Libgcrypt}' library application programming 183This manual documents the `@acronym{Libgcrypt}' library application programming
184interface (API). All functions and data types provided by the library 184interface (API). All functions and data types provided by the library
185are explained. 185are explained.
186 186
187@noindent 187@noindent
188The reader is assumed to possess basic knowledge about applied 188The reader is assumed to possess basic knowledge about applied
189cryptography. 189cryptography.
190 190
191This manual can be used in several ways. If read from the beginning 191This manual can be used in several ways. If read from the beginning
192to the end, it gives a good introduction into the library and how it 192to the end, it gives a good introduction into the library and how it
193can be used in an application. Forward references are included where 193can be used in an application. Forward references are included where
194necessary. Later on, the manual can be used as a reference manual to 194necessary. Later on, the manual can be used as a reference manual to
195get just the information needed about any particular interface of the 195get just the information needed about any particular interface of the
196library. Experienced programmers might want to start looking at the 196library. Experienced programmers might want to start looking at the
197examples at the end of the manual, and then only read up those parts 197examples at the end of the manual, and then only read up those parts
198of the interface which are unclear. 198of the interface which are unclear.
199 199
200 200
201@node Features 201@node Features
202@section Features 202@section Features
203 203
204`Libgcrypt' might have a couple of advantages over other libraries doing 204`Libgcrypt' might have a couple of advantages over other libraries doing
205a similar job. 205a similar job.
206 206
207@table @asis 207@table @asis
208@item It's Free Software 208@item It's Free Software
209Anybody can use, modify, and redistribute it under the terms of the GNU 209Anybody can use, modify, and redistribute it under the terms of the GNU
210Lesser General Public License (@pxref{Library Copying}). Note, that 210Lesser General Public License (@pxref{Library Copying}). Note, that
211some parts (which are not needed on a GNU or GNU/Linux system) are 211some parts (which are not needed on a GNU or GNU/Linux system) are
212subject to the terms of the GNU General Public License 212subject to the terms of the GNU General Public License
213(@pxref{Copying}); please see the README file of the distribution for of 213(@pxref{Copying}); please see the README file of the distribution for of
214list of these parts. 214list of these parts.
215 215
216@item It encapsulates the low level cryptography 216@item It encapsulates the low level cryptography
217`@acronym{Libgcrypt}' provides a high level interface to cryptographic building 217`@acronym{Libgcrypt}' provides a high level interface to cryptographic building
218blocks using an extendable and flexible API. 218blocks using an extendable and flexible API.
219 219
220@end table 220@end table
221 221
222@node Overview 222@node Overview
223@section Overview 223@section Overview
224 224
225@noindent 225@noindent
226The `@acronym{Libgcrypt}' library is fully thread-safe, where it makes 226The `@acronym{Libgcrypt}' library is fully thread-safe, where it makes
227sense to be thread-safe. An exception for thread-safety are some 227sense to be thread-safe. An exception for thread-safety are some
228cryptographic functions that modify a certain context stored in 228cryptographic functions that modify a certain context stored in
229handles. If the user really intents to use such functions from 229handles. If the user really intents to use such functions from
230different threads on the same handle, he has to take care of the 230different threads on the same handle, he has to take care of the
231serialization of such functions himself. If not described otherwise, 231serialization of such functions himself. If not described otherwise,
232every function is thread-safe. 232every function is thread-safe.
233 233
234@acronym{Libgcrypt} depends on the library `libgpg-error', which 234@acronym{Libgcrypt} depends on the library `libgpg-error', which
235contains common error handling related code for GnuPG components. 235contains common error handling related code for GnuPG components.
236 236
237@c ********************************************************** 237@c **********************************************************
238@c ******************* Preparation ************************ 238@c ******************* Preparation ************************
239@c ********************************************************** 239@c **********************************************************
240@node Preparation 240@node Preparation
241@chapter Preparation 241@chapter Preparation
242 242
243To use `@acronym{Libgcrypt}', you have to perform some changes to your 243To use `@acronym{Libgcrypt}', you have to perform some changes to your
244sources and the build system. The necessary changes are small and 244sources and the build system. The necessary changes are small and
245explained in the following sections. At the end of this chapter, it 245explained in the following sections. At the end of this chapter, it
246is described how the library is initialized, and how the requirements 246is described how the library is initialized, and how the requirements
247of the library are verified. 247of the library are verified.
248 248
249@menu 249@menu
250* Header:: What header file you need to include. 250* Header:: What header file you need to include.
251* Building sources:: How to build sources using the library. 251* Building sources:: How to build sources using the library.
252* Building sources using Automake:: How to build sources with the help of Automake. 252* Building sources using Automake:: How to build sources with the help of Automake.
253* Initializing the library:: How to initialize the library. 253* Initializing the library:: How to initialize the library.
254* Multi Threading:: How @acronym{Libgcrypt} can be used in a MT environment. 254* Multi Threading:: How @acronym{Libgcrypt} can be used in a MT environment.
255@end menu 255@end menu
256 256
257 257
258@node Header 258@node Header
259@section Header 259@section Header
260 260
261All interfaces (data types and functions) of the library are defined 261All interfaces (data types and functions) of the library are defined
262in the header file `gcrypt.h'. You must include this in all source 262in the header file `gcrypt.h'. You must include this in all source
263files using the library, either directly or through some other header 263files using the library, either directly or through some other header
264file, like this: 264file, like this:
265 265
266@example 266@example
267#include <gcrypt.h> 267#include <gcrypt.h>
268@end example 268@end example
269 269
270The name space of `@acronym{Libgcrypt}' is @code{gcry_*} for function 270The name space of `@acronym{Libgcrypt}' is @code{gcry_*} for function
271and type names and @code{GCRY*} for other symbols. In addition the 271and type names and @code{GCRY*} for other symbols. In addition the
272same name prefixes with one prepended underscore are reserved for 272same name prefixes with one prepended underscore are reserved for
273internal use and should never be used by an application. Furthermore 273internal use and should never be used by an application. Furthermore
274`libgpg-error' defines functions prefixed with `gpg_' and preprocessor 274`libgpg-error' defines functions prefixed with `gpg_' and preprocessor
275symbols prefixed with `GPG_'. Note that @acronym{Libgcrypt} uses 275symbols prefixed with `GPG_'. Note that @acronym{Libgcrypt} uses
276libgpg-error, which uses @code{gpg_err_*} as name space for function 276libgpg-error, which uses @code{gpg_err_*} as name space for function
277and type names and @code{GPG_ERR_*} for other symbols, including all 277and type names and @code{GPG_ERR_*} for other symbols, including all
278the error codes. 278the error codes.
279 279
280@node Building sources 280@node Building sources
281@section Building sources 281@section Building sources
282 282
283If you want to compile a source file including the `gcrypt.h' header 283If you want to compile a source file including the `gcrypt.h' header
284file, you must make sure that the compiler can find it in the 284file, you must make sure that the compiler can find it in the
285directory hierarchy. This is accomplished by adding the path to the 285directory hierarchy. This is accomplished by adding the path to the
286directory in which the header file is located to the compilers include 286directory in which the header file is located to the compilers include
287file search path (via the @option{-I} option). 287file search path (via the @option{-I} option).
288 288
289However, the path to the include file is determined at the time the 289However, the path to the include file is determined at the time the
290source is configured. To solve this problem, `@acronym{Libgcrypt}' ships with a small 290source is configured. To solve this problem, `@acronym{Libgcrypt}' ships with a small
291helper program @command{libgcrypt-config} that knows the path to the 291helper program @command{libgcrypt-config} that knows the path to the
292include file and other configuration options. The options that need 292include file and other configuration options. The options that need
293to be added to the compiler invocation at compile time are output by 293to be added to the compiler invocation at compile time are output by
294the @option{--cflags} option to @command{libgcrypt-config}. The following 294the @option{--cflags} option to @command{libgcrypt-config}. The following
295example shows how it can be used at the command line: 295example shows how it can be used at the command line:
296 296
297@example 297@example
298gcc -c foo.c `libgcrypt-config --cflags` 298gcc -c foo.c `libgcrypt-config --cflags`
299@end example 299@end example
300 300
301Adding the output of @samp{libgcrypt-config --cflags} to the compilers 301Adding the output of @samp{libgcrypt-config --cflags} to the compilers
302command line will ensure that the compiler can find the `@acronym{Libgcrypt}' header 302command line will ensure that the compiler can find the `@acronym{Libgcrypt}' header
303file. 303file.
304 304
305A similar problem occurs when linking the program with the library. 305A similar problem occurs when linking the program with the library.
306Again, the compiler has to find the library files. For this to work, 306Again, the compiler has to find the library files. For this to work,
307the path to the library files has to be added to the library search path 307the path to the library files has to be added to the library search path
308(via the @option{-L} option). For this, the option @option{--libs} to 308(via the @option{-L} option). For this, the option @option{--libs} to
309@command{libgcrypt-config} can be used. For convenience, this option 309@command{libgcrypt-config} can be used. For convenience, this option
310also outputs all other options that are required to link the program 310also outputs all other options that are required to link the program
311with the `@acronym{Libgcrypt}' libraries (in particular, the @samp{-lgcrypt} 311with the `@acronym{Libgcrypt}' libraries (in particular, the @samp{-lgcrypt}
312option). The example shows how to link @file{foo.o} with the `@acronym{Libgcrypt}' 312option). The example shows how to link @file{foo.o} with the `@acronym{Libgcrypt}'
313library to a program @command{foo}. 313library to a program @command{foo}.
314 314
315@example 315@example
316gcc -o foo foo.o `libgcrypt-config --libs` 316gcc -o foo foo.o `libgcrypt-config --libs`
317@end example 317@end example
318 318
319Of course you can also combine both examples to a single command by 319Of course you can also combine both examples to a single command by
320specifying both options to @command{libgcrypt-config}: 320specifying both options to @command{libgcrypt-config}:
321 321
322@example 322@example
323gcc -o foo foo.c `libgcrypt-config --cflags --libs` 323gcc -o foo foo.c `libgcrypt-config --cflags --libs`
324@end example 324@end example
325 325
326@node Building sources using Automake 326@node Building sources using Automake
327@section Building sources using Automake 327@section Building sources using Automake
328 328
329It is much easier if you use GNU Automake instead of writing your own 329It is much easier if you use GNU Automake instead of writing your own
330Makefiles. If you do that you do not have to worry about finding and 330Makefiles. If you do that you do not have to worry about finding and
331invoking the @command{libgcrypt-config} script at all. 331invoking the @command{libgcrypt-config} script at all.
332@acronym{Libgcrypt} provides an extension to Automake that does all 332@acronym{Libgcrypt} provides an extension to Automake that does all
333the work for you. 333the work for you.
334 334
335@c A simple macro for optional variables. 335@c A simple macro for optional variables.
336@macro ovar{varname} 336@macro ovar{varname}
337@r{[}@var{\varname\}@r{]} 337@r{[}@var{\varname\}@r{]}
338@end macro 338@end macro
339@defmac AM_PATH_LIBGCRYPT (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found}) 339@defmac AM_PATH_LIBGCRYPT (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found})
340Check whether @acronym{Libgcrypt} (at least version 340Check whether @acronym{Libgcrypt} (at least version
341@var{minimum-version}, if given) exists on the host system. If it is 341@var{minimum-version}, if given) exists on the host system. If it is
342found, execute @var{action-if-found}, otherwise do 342found, execute @var{action-if-found}, otherwise do
343@var{action-if-not-found}, if given. 343@var{action-if-not-found}, if given.
344 344
345Additionally, the function defines @code{LIBGCRYPT_CFLAGS} to the 345Additionally, the function defines @code{LIBGCRYPT_CFLAGS} to the
346flags needed for compilation of the program to find the 346flags needed for compilation of the program to find the
347@file{gcrypt.h} header file, and @code{LIBGCRYPT_LIBS} to the linker 347@file{gcrypt.h} header file, and @code{LIBGCRYPT_LIBS} to the linker
348flags needed to link the program to the @acronym{Libgcrypt} library. 348flags needed to link the program to the @acronym{Libgcrypt} library.
349@end defmac 349@end defmac
350 350
351You can use the defined Autoconf variables like this in your 351You can use the defined Autoconf variables like this in your
352@file{Makefile.am}: 352@file{Makefile.am}:
353 353
354@example 354@example
355AM_CPPFLAGS = $(LIBGCRYPT_CFLAGS) 355AM_CPPFLAGS = $(LIBGCRYPT_CFLAGS)
356LDADD = $(LIBGCRYPT_LIBS) 356LDADD = $(LIBGCRYPT_LIBS)
357@end example 357@end example
358 358
359@node Initializing the library 359@node Initializing the library
360@section Initializing the library 360@section Initializing the library
361 361
362It is often desirable to check that the version of `@acronym{Libgcrypt}' used is 362It is often desirable to check that the version of `@acronym{Libgcrypt}' used is
363indeed one which fits all requirements. Even with binary compatibility 363indeed one which fits all requirements. Even with binary compatibility
364new features may have been introduced but due to problem with the 364new features may have been introduced but due to problem with the
365dynamic linker an old version is actually used. So you may want to 365dynamic linker an old version is actually used. So you may want to
366check that the version is okay right after program startup. 366check that the version is okay right after program startup.
367 367
368@deftypefun const char *gcry_check_version (const char *@var{req_version}) 368@deftypefun const char *gcry_check_version (const char *@var{req_version})
369 369
370The function @code{gcry_check_version} has three purposes. It can be 370The function @code{gcry_check_version} has three purposes. It can be
371used to retrieve the version number of the library. In addition it 371used to retrieve the version number of the library. In addition it
372can verify that the version number is higher than a certain required 372can verify that the version number is higher than a certain required
373version number. 373version number.
374 374
375In either case, the function initializes some sub-systems, and for 375In either case, the function initializes some sub-systems, and for
376this reason alone it must be invoked early in your program, before you 376this reason alone it must be invoked early in your program, before you
377make use of the other functions of @acronym{Libgcrypt}. 377make use of the other functions of @acronym{Libgcrypt}.
378@end deftypefun 378@end deftypefun
379 379
380@node Multi Threading 380@node Multi Threading
381@section Multi Threading 381@section Multi Threading
382 382
383As mentioned earlier, the `@acronym{Libgcrypt}' library is 383As mentioned earlier, the `@acronym{Libgcrypt}' library is
384thread-safe if you adhere to the following requirements: 384thread-safe if you adhere to the following requirements:
385 385
386@itemize @bullet 386@itemize @bullet
387@item 387@item
388If your application is multi-threaded, you must set the thread support 388If your application is multi-threaded, you must set the thread support
389callbacks with the @code{GCRYCTL_SET_THREAD_CBS} command 389callbacks with the @code{GCRYCTL_SET_THREAD_CBS} command
390@strong{before} any other function in the library. 390@strong{before} any other function in the library.
391 391
392This is easy enough if you are indeed writing an application using 392This is easy enough if you are indeed writing an application using
393Libgcrypt. It is rather problematic if you are writing a library 393Libgcrypt. It is rather problematic if you are writing a library
394instead. Here are some tips what to do if you are writing a library: 394instead. Here are some tips what to do if you are writing a library:
395 395
396If your library requires a certain thread package, just initialize 396If your library requires a certain thread package, just initialize
397Libgcrypt to use this thread package. If your library supports multiple 397Libgcrypt to use this thread package. If your library supports multiple
398thread packages, but needs to be configured, you will have to 398thread packages, but needs to be configured, you will have to
399implement a way to determine which thread package the application 399implement a way to determine which thread package the application
400wants to use with your library anyway. Then configure Libgcrypt to use 400wants to use with your library anyway. Then configure Libgcrypt to use
401this thread package. 401this thread package.
402 402
403If your library is fully reentrant without any special support by a 403If your library is fully reentrant without any special support by a
404thread package, then you are lucky indeed. Unfortunately, this does 404thread package, then you are lucky indeed. Unfortunately, this does
405not relieve you from doing either of the two above, or use a third 405not relieve you from doing either of the two above, or use a third
406option. The third option is to let the application initialize Libgcrypt 406option. The third option is to let the application initialize Libgcrypt
407for you. Then you are not using Libgcrypt transparently, though. 407for you. Then you are not using Libgcrypt transparently, though.
408 408
409As if this was not difficult enough, a conflict may arise if two 409As if this was not difficult enough, a conflict may arise if two
410libraries try to initialize Libgcrypt independently of each others, and 410libraries try to initialize Libgcrypt independently of each others, and
411both such libraries are then linked into the same application. To 411both such libraries are then linked into the same application. To
412make it a bit simpler for you, this will probably work, but only if 412make it a bit simpler for you, this will probably work, but only if
413both libraries have the same requirement for the thread package. This 413both libraries have the same requirement for the thread package. This
414is currently only supported for the non-threaded case, GNU Pth and 414is currently only supported for the non-threaded case, GNU Pth and
415pthread. Support for more thread packages is easy to add, so contact 415pthread. Support for more thread packages is easy to add, so contact
416us if you require it. 416us if you require it.
417 417
418@item 418@item
419The function @code{gcry_check_version} must be called before any other 419The function @code{gcry_check_version} must be called before any other
420function in the library, except the @code{GCRYCTL_SET_THREAD_CBS} 420function in the library, except the @code{GCRYCTL_SET_THREAD_CBS}
421command (called via the @code{gcry_control} function), because it 421command (called via the @code{gcry_control} function), because it
422initializes the thread support subsystem in @acronym{Libgcrypt}. To 422initializes the thread support subsystem in @acronym{Libgcrypt}. To
423achieve this in multi-threaded programs, you must synchronize the 423achieve this in multi-threaded programs, you must synchronize the
424memory with respect to other threads that also want to use 424memory with respect to other threads that also want to use
425@acronym{Libgcrypt}. For this, it is sufficient to call 425@acronym{Libgcrypt}. For this, it is sufficient to call
426@code{gcry_check_version} before creating the other threads using 426@code{gcry_check_version} before creating the other threads using
427@acronym{Libgcrypt}@footnote{At least this is true for POSIX threads, 427@acronym{Libgcrypt}@footnote{At least this is true for POSIX threads,
428as @code{pthread_create} is a function that synchronizes memory with 428as @code{pthread_create} is a function that synchronizes memory with
429respects to other threads. There are many functions which have this 429respects to other threads. There are many functions which have this
430property, a complete list can be found in POSIX, IEEE Std 1003.1-2003, 430property, a complete list can be found in POSIX, IEEE Std 1003.1-2003,
431Base Definitions, Issue 6, in the definition of the term ``Memory 431Base Definitions, Issue 6, in the definition of the term ``Memory
432Synchronization''. For other thread packages, more relaxed or more 432Synchronization''. For other thread packages, more relaxed or more
433strict rules may apply.}. 433strict rules may apply.}.
434 434
435@item 435@item
436 436
437As with the function @code{gpg_strerror}, @code{gcry_strerror} is not 437As with the function @code{gpg_strerror}, @code{gcry_strerror} is not
438thread safe. You have to use @code{gpg_strerror_r} instead. 438thread safe. You have to use @code{gpg_strerror_r} instead.
439@end itemize 439@end itemize
440 440
441 441
442@acronym{Libgcrypt} contains convenient macros, which define the 442@acronym{Libgcrypt} contains convenient macros, which define the
443necessary thread callbacks for PThread and for GNU Pth: 443necessary thread callbacks for PThread and for GNU Pth:
444 444
445@table @code 445@table @code
446@item GCRY_THREAD_OPTION_PTH_IMPL 446@item GCRY_THREAD_OPTION_PTH_IMPL
447 447
448This macro defines the following (static) symbols: gcry_pth_init, 448This macro defines the following (static) symbols: gcry_pth_init,
449gcry_pth_mutex_init, gcry_pth_mutex_destroy, gcry_pth_mutex_lock, 449gcry_pth_mutex_init, gcry_pth_mutex_destroy, gcry_pth_mutex_lock,
450gcry_pth_mutex_unlock, gcry_pth_read, gcry_pth_write, gcry_pth_select, 450gcry_pth_mutex_unlock, gcry_pth_read, gcry_pth_write, gcry_pth_select,
451gcry_pth_waitpid, gcry_pth_accept, gcry_pth_connect, gcry_threads_pth. 451gcry_pth_waitpid, gcry_pth_accept, gcry_pth_connect, gcry_threads_pth.
452 452
453After including this macro, gcry_control() shall be used with a 453After including this macro, gcry_control() shall be used with a
454command of GCRYCTL_SET_THREAD_CBS in order to register the thread 454command of GCRYCTL_SET_THREAD_CBS in order to register the thread
455callback structure named ``gcry_threads_pth''. 455callback structure named ``gcry_threads_pth''.
456 456
457@item GCRY_THREAD_OPTION_PTHREAD_IMPL 457@item GCRY_THREAD_OPTION_PTHREAD_IMPL
458 458
459This macro defines the following (static) symbols: 459This macro defines the following (static) symbols:
460gcry_pthread_mutex_init, gcry_pthread_mutex_destroy, gcry_mutex_lock, 460gcry_pthread_mutex_init, gcry_pthread_mutex_destroy, gcry_mutex_lock,
461gcry_mutex_unlock, gcry_threads_pthread. 461gcry_mutex_unlock, gcry_threads_pthread.
462 462
463After including this macro, gcry_control() shall be used with a 463After including this macro, gcry_control() shall be used with a
464command of GCRYCTL_SET_THREAD_CBS in order to register the thread 464command of GCRYCTL_SET_THREAD_CBS in order to register the thread
465callback structure named ``gcry_threads_pthread''. 465callback structure named ``gcry_threads_pthread''.
466@end table 466@end table
467 467
468Note that these macros need to be terminated with a semicolon. Keep 468Note that these macros need to be terminated with a semicolon. Keep
469in mind that these are convenient macros for C programmers; C++ 469in mind that these are convenient macros for C programmers; C++
470programmers might have to wrap these macros in an ``extern C'' body. 470programmers might have to wrap these macros in an ``extern C'' body.
471 471
472@c ********************************************************** 472@c **********************************************************
473@c ******************* General **************************** 473@c ******************* General ****************************
474@c ********************************************************** 474@c **********************************************************
475@node Generalities 475@node Generalities
476@chapter Generalities 476@chapter Generalities
477 477
478@menu 478@menu
479* Controlling the library:: Controlling @acronym{Libgcrypt}'s behavior. 479* Controlling the library:: Controlling @acronym{Libgcrypt}'s behavior.
480* Modules:: Description of extension modules. 480* Modules:: Description of extension modules.
481* Error Handling:: Error codes and such. 481* Error Handling:: Error codes and such.
482@end menu 482@end menu
483 483
484@node Controlling the library 484@node Controlling the library
485@section Controlling the library 485@section Controlling the library
486 486
487@deftypefun gcry_error_t gcry_control (enum gcry_ctl_cmds @var{cmd}, ...) 487@deftypefun gcry_error_t gcry_control (enum gcry_ctl_cmds @var{cmd}, ...)
488 488
489This function can be used to influence the general behavior of 489This function can be used to influence the general behavior of
490@acronym{Libgcrypt} in several ways. Depending on @var{cmd}, more 490@acronym{Libgcrypt} in several ways. Depending on @var{cmd}, more
491arguments can or have to be provided. 491arguments can or have to be provided.
492 492
493@end deftypefun 493@end deftypefun
494 494
495@node Modules 495@node Modules
496@section Modules 496@section Modules
497 497
498@acronym{Libgcrypt} supports the use of `extension modules', which 498@acronym{Libgcrypt} supports the use of `extension modules', which
499implement algorithms in addition to those already built into the 499implement algorithms in addition to those already built into the
500library directly. 500library directly.
501 501
502@deftp {Data type} gcry_module_t 502@deftp {Data type} gcry_module_t
503This data type represents a `module'. 503This data type represents a `module'.
504@end deftp 504@end deftp
505 505
506Functions registering modules provided by the user take a `module 506Functions registering modules provided by the user take a `module
507specification structure' as input and return a value of 507specification structure' as input and return a value of
508@code{gcry_module_t} and an ID that is unique in the modules' 508@code{gcry_module_t} and an ID that is unique in the modules'
509category. This ID can be used to reference the newly registered 509category. This ID can be used to reference the newly registered
510module. After registering a module successfully, the new functionality 510module. After registering a module successfully, the new functionality
511should be able to be used through the normal functions provided by 511should be able to be used through the normal functions provided by
512@acronym{Libgcrypt} until it is unregistered again. 512@acronym{Libgcrypt} until it is unregistered again.
513 513
514@c ********************************************************** 514@c **********************************************************
515@c ******************* Errors **************************** 515@c ******************* Errors ****************************
516@c ********************************************************** 516@c **********************************************************
517@node Error Handling 517@node Error Handling
518@section Error Handling 518@section Error Handling
519 519
520Many functions in @acronym{Libgcrypt} can return an error if they 520Many functions in @acronym{Libgcrypt} can return an error if they
521fail. For this reason, the application should always catch the error 521fail. For this reason, the application should always catch the error
522condition and take appropriate measures, for example by releasing the 522condition and take appropriate measures, for example by releasing the
523resources and passing the error up to the caller, or by displaying a 523resources and passing the error up to the caller, or by displaying a
524descriptive message to the user and cancelling the operation. 524descriptive message to the user and cancelling the operation.
525 525
526Some error values do not indicate a system error or an error in the 526Some error values do not indicate a system error or an error in the
527operation, but the result of an operation that failed properly. For 527operation, but the result of an operation that failed properly. For
528example, if you try to decrypt a tempered message, the decryption will 528example, if you try to decrypt a tempered message, the decryption will
529fail. Another error value actually means that the end of a data 529fail. Another error value actually means that the end of a data
530buffer or list has been reached. The following descriptions explain 530buffer or list has been reached. The following descriptions explain
531for many error codes what they mean usually. Some error values have 531for many error codes what they mean usually. Some error values have
532specific meanings if returned by a certain functions. Such cases are 532specific meanings if returned by a certain functions. Such cases are
533described in the documentation of those functions. 533described in the documentation of those functions.
534 534
535@acronym{Libgcrypt} uses the @code{libgpg-error} library. This allows 535@acronym{Libgcrypt} uses the @code{libgpg-error} library. This allows
536to share the error codes with other components of the GnuPG system, 536to share the error codes with other components of the GnuPG system,
537and thus pass error values transparently from the crypto engine, or 537and thus pass error values transparently from the crypto engine, or
538some helper application of the crypto engine, to the user. This way 538some helper application of the crypto engine, to the user. This way
539no information is lost. As a consequence, @acronym{Libgcrypt} does 539no information is lost. As a consequence, @acronym{Libgcrypt} does
540not use its own identifiers for error codes, but uses those provided 540not use its own identifiers for error codes, but uses those provided
541by @code{libgpg-error}. They usually start with @code{GPG_ERR_}. 541by @code{libgpg-error}. They usually start with @code{GPG_ERR_}.
542 542
543However, @acronym{Libgcrypt} does provide aliases for the functions 543However, @acronym{Libgcrypt} does provide aliases for the functions
544defined in libgpg-error, which might be preferred for name space 544defined in libgpg-error, which might be preferred for name space
545consistency. 545consistency.
546 546
547 547
548Most functions in @acronym{Libgcrypt} return an error code in the case 548Most functions in @acronym{Libgcrypt} return an error code in the case
549of failure. For this reason, the application should always catch the 549of failure. For this reason, the application should always catch the
550error condition and take appropriate measures, for example by 550error condition and take appropriate measures, for example by
551releasing the resources and passing the error up to the caller, or by 551releasing the resources and passing the error up to the caller, or by
552displaying a descriptive message to the user and canceling the 552displaying a descriptive message to the user and canceling the
553operation. 553operation.
554 554
555Some error values do not indicate a system error or an error in the 555Some error values do not indicate a system error or an error in the
556operation, but the result of an operation that failed properly. 556operation, but the result of an operation that failed properly.
557 557
558GnuPG components, including Libgcrypt, use an extra library named 558GnuPG components, including Libgcrypt, use an extra library named
559libgpg-error to provide a common error handling scheme. For more 559libgpg-error to provide a common error handling scheme. For more
560information on libgpg-error, see the according manual. 560information on libgpg-error, see the according manual.
561 561
562@menu 562@menu
563* Error Values:: The error value and what it means. 563* Error Values:: The error value and what it means.
564* Error Sources:: A list of important error sources. 564* Error Sources:: A list of important error sources.
565* Error Codes:: A list of important error codes. 565* Error Codes:: A list of important error codes.
566* Error Strings:: How to get a descriptive string from a value. 566* Error Strings:: How to get a descriptive string from a value.
567@end menu 567@end menu
568 568
569 569
570@node Error Values 570@node Error Values
571@subsection Error Values 571@subsection Error Values
572@cindex error values 572@cindex error values
573@cindex error codes 573@cindex error codes
574@cindex error sources 574@cindex error sources
575 575
576@deftp {Data type} {gcry_err_code_t} 576@deftp {Data type} {gcry_err_code_t}
577The @code{gcry_err_code_t} type is an alias for the 577The @code{gcry_err_code_t} type is an alias for the
578@code{libgpg-error} type @code{gpg_err_code_t}. The error code 578@code{libgpg-error} type @code{gpg_err_code_t}. The error code
579indicates the type of an error, or the reason why an operation failed. 579indicates the type of an error, or the reason why an operation failed.
580 580
581A list of important error codes can be found in the next section. 581A list of important error codes can be found in the next section.
582@end deftp 582@end deftp
583 583
584@deftp {Data type} {gcry_err_source_t} 584@deftp {Data type} {gcry_err_source_t}
585The @code{gcry_err_source_t} type is an alias for the 585The @code{gcry_err_source_t} type is an alias for the
586@code{libgpg-error} type @code{gpg_err_source_t}. The error source 586@code{libgpg-error} type @code{gpg_err_source_t}. The error source
587has not a precisely defined meaning. Sometimes it is the place where 587has not a precisely defined meaning. Sometimes it is the place where
588the error happened, sometimes it is the place where an error was 588the error happened, sometimes it is the place where an error was
589encoded into an error value. Usually the error source will give an 589encoded into an error value. Usually the error source will give an
590indication to where to look for the problem. This is not always true, 590indication to where to look for the problem. This is not always true,
591but it is attempted to achieve this goal. 591but it is attempted to achieve this goal.
592 592
593A list of important error sources can be found in the next section. 593A list of important error sources can be found in the next section.
594@end deftp 594@end deftp
595 595
596@deftp {Data type} {gcry_error_t} 596@deftp {Data type} {gcry_error_t}
597The @code{gcry_error_t} type is an alias for the @code{libgpg-error} 597The @code{gcry_error_t} type is an alias for the @code{libgpg-error}
598type @code{gpg_error_t}. An error value like this has always two 598type @code{gpg_error_t}. An error value like this has always two
599components, an error code and an error source. Both together form the 599components, an error code and an error source. Both together form the
600error value. 600error value.
601 601
602Thus, the error value can not be directly compared against an error 602Thus, the error value can not be directly compared against an error
603code, but the accessor functions described below must be used. 603code, but the accessor functions described below must be used.
604However, it is guaranteed that only 0 is used to indicate success 604However, it is guaranteed that only 0 is used to indicate success
605(@code{GPG_ERR_NO_ERROR}), and that in this case all other parts of 605(@code{GPG_ERR_NO_ERROR}), and that in this case all other parts of
606the error value are set to 0, too. 606the error value are set to 0, too.
607 607
608Note that in @acronym{Libgcrypt}, the error source is used purely for 608Note that in @acronym{Libgcrypt}, the error source is used purely for
609diagnostic purposes. Only the error code should be checked to test 609diagnostic purposes. Only the error code should be checked to test
610for a certain outcome of a function. The manual only documents the 610for a certain outcome of a function. The manual only documents the
611error code part of an error value. The error source is left 611error code part of an error value. The error source is left
612unspecified and might be anything. 612unspecified and might be anything.
613@end deftp 613@end deftp
614 614
615@deftypefun {gcry_err_code_t} gcry_err_code (@w{gcry_error_t @var{err}}) 615@deftypefun {gcry_err_code_t} gcry_err_code (@w{gcry_error_t @var{err}})
616The static inline function @code{gcry_err_code} returns the 616The static inline function @code{gcry_err_code} returns the
617@code{gcry_err_code_t} component of the error value @var{err}. This 617@code{gcry_err_code_t} component of the error value @var{err}. This
618function must be used to extract the error code from an error value in 618function must be used to extract the error code from an error value in
619order to compare it with the @code{GPG_ERR_*} error code macros. 619order to compare it with the @code{GPG_ERR_*} error code macros.
620@end deftypefun 620@end deftypefun
621 621
622@deftypefun {gcry_err_source_t} gcry_err_source (@w{gcry_error_t @var{err}}) 622@deftypefun {gcry_err_source_t} gcry_err_source (@w{gcry_error_t @var{err}})
623The static inline function @code{gcry_err_source} returns the 623The static inline function @code{gcry_err_source} returns the
624@code{gcry_err_source_t} component of the error value @var{err}. This 624@code{gcry_err_source_t} component of the error value @var{err}. This
625function must be used to extract the error source from an error value in 625function must be used to extract the error source from an error value in
626order to compare it with the @code{GPG_ERR_SOURCE_*} error source macros. 626order to compare it with the @code{GPG_ERR_SOURCE_*} error source macros.
627@end deftypefun 627@end deftypefun
628 628
629@deftypefun {gcry_error_t} gcry_err_make (@w{gcry_err_source_t @var{source}}, @w{gcry_err_code_t @var{code}}) 629@deftypefun {gcry_error_t} gcry_err_make (@w{gcry_err_source_t @var{source}}, @w{gcry_err_code_t @var{code}})
630The static inline function @code{gcry_err_make} returns the error 630The static inline function @code{gcry_err_make} returns the error
631value consisting of the error source @var{source} and the error code 631value consisting of the error source @var{source} and the error code
632@var{code}. 632@var{code}.
633 633
634This function can be used in callback functions to construct an error 634This function can be used in callback functions to construct an error
635value to return it to the library. 635value to return it to the library.
636@end deftypefun 636@end deftypefun
637 637
638@deftypefun {gcry_error_t} gcry_error (@w{gcry_err_code_t @var{code}}) 638@deftypefun {gcry_error_t} gcry_error (@w{gcry_err_code_t @var{code}})
639The static inline function @code{gcry_error} returns the error value 639The static inline function @code{gcry_error} returns the error value
640consisting of the default error source and the error code @var{code}. 640consisting of the default error source and the error code @var{code}.
641 641
642For @acronym{GCRY} applications, the default error source is 642For @acronym{GCRY} applications, the default error source is
643@code{GPG_ERR_SOURCE_USER_1}. You can define 643@code{GPG_ERR_SOURCE_USER_1}. You can define
644@code{GCRY_ERR_SOURCE_DEFAULT} before including @file{gcrypt.h} to 644@code{GCRY_ERR_SOURCE_DEFAULT} before including @file{gcrypt.h} to
645change this default. 645change this default.
646 646
647This function can be used in callback functions to construct an error 647This function can be used in callback functions to construct an error
648value to return it to the library. 648value to return it to the library.
649@end deftypefun 649@end deftypefun
650 650
651The @code{libgpg-error} library provides error codes for all system 651The @code{libgpg-error} library provides error codes for all system
652error numbers it knows about. If @var{err} is an unknown error 652error numbers it knows about. If @var{err} is an unknown error
653number, the error code @code{GPG_ERR_UNKNOWN_ERRNO} is used. The 653number, the error code @code{GPG_ERR_UNKNOWN_ERRNO} is used. The
654following functions can be used to construct error values from system 654following functions can be used to construct error values from system
655errno numbers. 655errno numbers.
656 656
657@deftypefun {gcry_error_t} gcry_err_make_from_errno (@w{gcry_err_source_t @var{source}}, @w{int @var{err}}) 657@deftypefun {gcry_error_t} gcry_err_make_from_errno (@w{gcry_err_source_t @var{source}}, @w{int @var{err}})
658The function @code{gcry_err_make_from_errno} is like 658The function @code{gcry_err_make_from_errno} is like
659@code{gcry_err_make}, but it takes a system error like @code{errno} 659@code{gcry_err_make}, but it takes a system error like @code{errno}
660instead of a @code{gcry_err_code_t} error code. 660instead of a @code{gcry_err_code_t} error code.
661@end deftypefun 661@end deftypefun
662 662
663@deftypefun {gcry_error_t} gcry_error_from_errno (@w{int @var{err}}) 663@deftypefun {gcry_error_t} gcry_error_from_errno (@w{int @var{err}})
664The function @code{gcry_error_from_errno} is like @code{gcry_error}, 664The function @code{gcry_error_from_errno} is like @code{gcry_error},
665but it takes a system error like @code{errno} instead of a 665but it takes a system error like @code{errno} instead of a
666@code{gcry_err_code_t} error code. 666@code{gcry_err_code_t} error code.
667@end deftypefun 667@end deftypefun
668 668
669Sometimes you might want to map system error numbers to error codes 669Sometimes you might want to map system error numbers to error codes
670directly, or map an error code representing a system error back to the 670directly, or map an error code representing a system error back to the
671system error number. The following functions can be used to do that. 671system error number. The following functions can be used to do that.
672 672
673@deftypefun {gcry_err_code_t} gcry_err_code_from_errno (@w{int @var{err}}) 673@deftypefun {gcry_err_code_t} gcry_err_code_from_errno (@w{int @var{err}})
674The function @code{gcry_err_code_from_errno} returns the error code 674The function @code{gcry_err_code_from_errno} returns the error code
675for the system error @var{err}. If @var{err} is not a known system 675for the system error @var{err}. If @var{err} is not a known system
676error, the function returns @code{GPG_ERR_UNKNOWN_ERRNO}. 676error, the function returns @code{GPG_ERR_UNKNOWN_ERRNO}.
677@end deftypefun 677@end deftypefun
678 678
679@deftypefun {int} gcry_err_code_to_errno (@w{gcry_err_code_t @var{err}}) 679@deftypefun {int} gcry_err_code_to_errno (@w{gcry_err_code_t @var{err}})
680The function @code{gcry_err_code_to_errno} returns the system error 680The function @code{gcry_err_code_to_errno} returns the system error
681for the error code @var{err}. If @var{err} is not an error code 681for the error code @var{err}. If @var{err} is not an error code
682representing a system error, or if this system error is not defined on 682representing a system error, or if this system error is not defined on
683this system, the function returns @code{0}. 683this system, the function returns @code{0}.
684@end deftypefun 684@end deftypefun
685 685
686 686
687@node Error Sources 687@node Error Sources
688@subsection Error Sources 688@subsection Error Sources
689@cindex error codes, list of 689@cindex error codes, list of
690 690
691The library @code{libgpg-error} defines an error source for every 691The library @code{libgpg-error} defines an error source for every
692component of the GnuPG system. The error source part of an error 692component of the GnuPG system. The error source part of an error
693value is not well defined. As such it is mainly useful to improve the 693value is not well defined. As such it is mainly useful to improve the
694diagnostic error message for the user. 694diagnostic error message for the user.
695 695
696If the error code part of an error value is @code{0}, the whole error 696If the error code part of an error value is @code{0}, the whole error
697value will be @code{0}. In this case the error source part is of 697value will be @code{0}. In this case the error source part is of
698course @code{GPG_ERR_SOURCE_UNKNOWN}. 698course @code{GPG_ERR_SOURCE_UNKNOWN}.
699 699
700The list of error sources that might occur in applications using 700The list of error sources that might occur in applications using
701@acronym{Libgctypt} is: 701@acronym{Libgctypt} is:
702 702
703@table @code 703@table @code
704@item GPG_ERR_SOURCE_UNKNOWN 704@item GPG_ERR_SOURCE_UNKNOWN
705The error source is not known. The value of this error source is 705The error source is not known. The value of this error source is
706@code{0}. 706@code{0}.
707 707
708@item GPG_ERR_SOURCE_GPGME 708@item GPG_ERR_SOURCE_GPGME
709The error source is @acronym{GPGME} itself. 709The error source is @acronym{GPGME} itself.
710 710
711@item GPG_ERR_SOURCE_GPG 711@item GPG_ERR_SOURCE_GPG
712The error source is GnuPG, which is the crypto engine used for the 712The error source is GnuPG, which is the crypto engine used for the
713OpenPGP protocol. 713OpenPGP protocol.
714 714
715@item GPG_ERR_SOURCE_GPGSM 715@item GPG_ERR_SOURCE_GPGSM
716The error source is GPGSM, which is the crypto engine used for the 716The error source is GPGSM, which is the crypto engine used for the
717OpenPGP protocol. 717OpenPGP protocol.
718 718
719@item GPG_ERR_SOURCE_GCRYPT 719@item GPG_ERR_SOURCE_GCRYPT
720The error source is @code{libgcrypt}, which is used by crypto engines 720The error source is @code{libgcrypt}, which is used by crypto engines
721to perform cryptographic operations. 721to perform cryptographic operations.
722 722
723@item GPG_ERR_SOURCE_GPGAGENT 723@item GPG_ERR_SOURCE_GPGAGENT
724The error source is @command{gpg-agent}, which is used by crypto 724The error source is @command{gpg-agent}, which is used by crypto
725engines to perform operations with the secret key. 725engines to perform operations with the secret key.
726 726
727@item GPG_ERR_SOURCE_PINENTRY 727@item GPG_ERR_SOURCE_PINENTRY
728The error source is @command{pinentry}, which is used by 728The error source is @command{pinentry}, which is used by
729@command{gpg-agent} to query the passphrase to unlock a secret key. 729@command{gpg-agent} to query the passphrase to unlock a secret key.
730 730
731@item GPG_ERR_SOURCE_SCD 731@item GPG_ERR_SOURCE_SCD
732The error source is the SmartCard Daemon, which is used by 732The error source is the SmartCard Daemon, which is used by
733@command{gpg-agent} to delegate operations with the secret key to a 733@command{gpg-agent} to delegate operations with the secret key to a
734SmartCard. 734SmartCard.
735 735
736@item GPG_ERR_SOURCE_KEYBOX 736@item GPG_ERR_SOURCE_KEYBOX
737The error source is @code{libkbx}, a library used by the crypto 737The error source is @code{libkbx}, a library used by the crypto
738engines to manage local keyrings. 738engines to manage local keyrings.
739 739
740@item GPG_ERR_SOURCE_USER_1 740@item GPG_ERR_SOURCE_USER_1
741@item GPG_ERR_SOURCE_USER_2 741@item GPG_ERR_SOURCE_USER_2
742@item GPG_ERR_SOURCE_USER_3 742@item GPG_ERR_SOURCE_USER_3
743@item GPG_ERR_SOURCE_USER_4 743@item GPG_ERR_SOURCE_USER_4
744These error sources are not used by any GnuPG component and can be 744These error sources are not used by any GnuPG component and can be
745used by other software. For example, applications using 745used by other software. For example, applications using
746@acronym{Libgcrypt} can use them to mark error values coming from callback 746@acronym{Libgcrypt} can use them to mark error values coming from callback
747handlers. Thus @code{GPG_ERR_SOURCE_USER_1} is the default for errors 747handlers. Thus @code{GPG_ERR_SOURCE_USER_1} is the default for errors
748created with @code{gcry_error} and @code{gcry_error_from_errno}, 748created with @code{gcry_error} and @code{gcry_error_from_errno},
749unless you define @code{GCRY_ERR_SOURCE_DEFAULT} before including 749unless you define @code{GCRY_ERR_SOURCE_DEFAULT} before including
750@file{gcrypt.h}. 750@file{gcrypt.h}.
751@end table 751@end table
752 752
753 753
754@node Error Codes 754@node Error Codes
755@subsection Error Codes 755@subsection Error Codes
756@cindex error codes, list of 756@cindex error codes, list of
757 757
758The library @code{libgpg-error} defines many error values. The 758The library @code{libgpg-error} defines many error values. The
759following list includes the most important error codes. 759following list includes the most important error codes.
760 760
761@table @code 761@table @code
762@item GPG_ERR_EOF 762@item GPG_ERR_EOF
763This value indicates the end of a list, buffer or file. 763This value indicates the end of a list, buffer or file.
764 764
765@item GPG_ERR_NO_ERROR 765@item GPG_ERR_NO_ERROR
766This value indicates success. The value of this error code is 766This value indicates success. The value of this error code is
767@code{0}. Also, it is guaranteed that an error value made from the 767@code{0}. Also, it is guaranteed that an error value made from the
768error code @code{0} will be @code{0} itself (as a whole). This means 768error code @code{0} will be @code{0} itself (as a whole). This means
769that the error source information is lost for this error code, 769that the error source information is lost for this error code,
770however, as this error code indicates that no error occured, this is 770however, as this error code indicates that no error occured, this is
771generally not a problem. 771generally not a problem.
772 772
773@item GPG_ERR_GENERAL 773@item GPG_ERR_GENERAL
774This value means that something went wrong, but either there is not 774This value means that something went wrong, but either there is not
775enough information about the problem to return a more useful error 775enough information about the problem to return a more useful error
776value, or there is no separate error value for this type of problem. 776value, or there is no separate error value for this type of problem.
777 777
778@item GPG_ERR_ENOMEM 778@item GPG_ERR_ENOMEM
779This value means that an out-of-memory condition occurred. 779This value means that an out-of-memory condition occurred.
780 780
781@item GPG_ERR_E... 781@item GPG_ERR_E...
782System errors are mapped to GPG_ERR_EFOO where FOO is the symbol for 782System errors are mapped to GPG_ERR_EFOO where FOO is the symbol for
783the system error. 783the system error.
784 784
785@item GPG_ERR_INV_VALUE 785@item GPG_ERR_INV_VALUE
786This value means that some user provided data was out of range. 786This value means that some user provided data was out of range.
787 787
788@item GPG_ERR_UNUSABLE_PUBKEY 788@item GPG_ERR_UNUSABLE_PUBKEY
789This value means that some recipients for a message were invalid. 789This value means that some recipients for a message were invalid.
790 790
791@item GPG_ERR_UNUSABLE_SECKEY 791@item GPG_ERR_UNUSABLE_SECKEY
792This value means that some signers were invalid. 792This value means that some signers were invalid.
793 793
794@item GPG_ERR_NO_DATA 794@item GPG_ERR_NO_DATA
795This value means that data was expected where no data was found. 795This value means that data was expected where no data was found.
796 796
797@item GPG_ERR_CONFLICT 797@item GPG_ERR_CONFLICT
798This value means that a conflict of some sort occurred. 798This value means that a conflict of some sort occurred.
799 799
800@item GPG_ERR_NOT_IMPLEMENTED 800@item GPG_ERR_NOT_IMPLEMENTED
801This value indicates that the specific function (or operation) is not 801This value indicates that the specific function (or operation) is not
802implemented. This error should never happen. It can only occur if 802implemented. This error should never happen. It can only occur if
803you use certain values or configuration options which do not work, 803you use certain values or configuration options which do not work,
804but for which we think that they should work at some later time. 804but for which we think that they should work at some later time.
805 805
806@item GPG_ERR_DECRYPT_FAILED 806@item GPG_ERR_DECRYPT_FAILED
807This value indicates that a decryption operation was unsuccessful. 807This value indicates that a decryption operation was unsuccessful.
808 808
809@item GPG_ERR_WRONG_KEY_USAGE 809@item GPG_ERR_WRONG_KEY_USAGE
810This value indicates that a key is not used appropriately. 810This value indicates that a key is not used appropriately.
811 811
812@item GPG_ERR_NO_SECKEY 812@item GPG_ERR_NO_SECKEY
813This value indicates that no secret key for the user ID is available. 813This value indicates that no secret key for the user ID is available.
814 814
815@item GPG_ERR_UNSUPPORTED_ALGORITHM 815@item GPG_ERR_UNSUPPORTED_ALGORITHM
816This value means a verification failed because the cryptographic 816This value means a verification failed because the cryptographic
817algorithm is not supported by the crypto backend. 817algorithm is not supported by the crypto backend.
818 818
819@item GPG_ERR_BAD_SIGNATURE 819@item GPG_ERR_BAD_SIGNATURE
820This value means a verification failed because the signature is bad. 820This value means a verification failed because the signature is bad.
821 821
822@item GPG_ERR_NO_PUBKEY 822@item GPG_ERR_NO_PUBKEY
823This value means a verification failed because the public key is not 823This value means a verification failed because the public key is not
824available. 824available.
825 825
826@item GPG_ERR_USER_1 826@item GPG_ERR_USER_1
827@item GPG_ERR_USER_2 827@item GPG_ERR_USER_2
828@item ... 828@item ...
829@item GPG_ERR_USER_16 829@item GPG_ERR_USER_16
830These error codes are not used by any GnuPG component and can be 830These error codes are not used by any GnuPG component and can be
831freely used by other software. Applications using @acronym{Libgcrypt} 831freely used by other software. Applications using @acronym{Libgcrypt}
832might use them to mark specific errors returned by callback handlers 832might use them to mark specific errors returned by callback handlers
833if no suitable error codes (including the system errors) for these 833if no suitable error codes (including the system errors) for these
834errors exist already. 834errors exist already.
835@end table 835@end table
836 836
837 837
838@node Error Strings 838@node Error Strings
839@subsection Error Strings 839@subsection Error Strings
840@cindex error values, printing of 840@cindex error values, printing of
841@cindex error codes, printing of 841@cindex error codes, printing of
842@cindex error sources, printing of 842@cindex error sources, printing of
843@cindex error strings 843@cindex error strings
844 844
845@deftypefun {const char *} gcry_strerror (@w{gcry_error_t @var{err}}) 845@deftypefun {const char *} gcry_strerror (@w{gcry_error_t @var{err}})
846The function @code{gcry_strerror} returns a pointer to a statically 846The function @code{gcry_strerror} returns a pointer to a statically
847allocated string containing a description of the error code contained 847allocated string containing a description of the error code contained
848in the error value @var{err}. This string can be used to output a 848in the error value @var{err}. This string can be used to output a
849diagnostic message to the user. 849diagnostic message to the user.
850@end deftypefun 850@end deftypefun
851 851
852 852
853@deftypefun {const char *} gcry_strsource (@w{gcry_error_t @var{err}}) 853@deftypefun {const char *} gcry_strsource (@w{gcry_error_t @var{err}})
854The function @code{gcry_strerror} returns a pointer to a statically 854The function @code{gcry_strerror} returns a pointer to a statically
855allocated string containing a description of the error source 855allocated string containing a description of the error source
856contained in the error value @var{err}. This string can be used to 856contained in the error value @var{err}. This string can be used to
857output a diagnostic message to the user. 857output a diagnostic message to the user.
858@end deftypefun 858@end deftypefun
859 859
860The following example illustrates the use of the functions described 860The following example illustrates the use of the functions described
861above: 861above:
862 862
863@example 863@example
864@{ 864@{
865 gcry_cipher_hd_t handle; 865 gcry_cipher_hd_t handle;
866 gcry_error_t err = 0; 866 gcry_error_t err = 0;
867 867
868 err = gcry_cipher_open (&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 0); 868 err = gcry_cipher_open (&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 0);
869 if (err) 869 if (err)
870 @{ 870 @{
871 fprintf (stderr, "Failure: %s/%s\n", 871 fprintf (stderr, "Failure: %s/%s\n",
872 gcry_strsource (err), 872 gcry_strsource (err),
873 gcry_strerror (err)); 873 gcry_strerror (err));
874 @} 874 @}
875@} 875@}
876@end example 876@end example
877 877
878@c ********************************************************** 878@c **********************************************************
879@c ******************* General **************************** 879@c ******************* General ****************************
880@c ********************************************************** 880@c **********************************************************
881@node Handler Functions 881@node Handler Functions
882@chapter Handler Functions 882@chapter Handler Functions
883 883
884@acronym{Libgcrypt} makes it possible to install so called `handler functions', 884@acronym{Libgcrypt} makes it possible to install so called `handler functions',
885which get called by @acronym{Libgcrypt} in case of certain events. 885which get called by @acronym{Libgcrypt} in case of certain events.
886 886
887@menu 887@menu
888* Progress handler:: Using a progress handler function. 888* Progress handler:: Using a progress handler function.
889* Allocation handler:: Using special memory allocation functions. 889* Allocation handler:: Using special memory allocation functions.
890* Error handler:: Using error handler functions. 890* Error handler:: Using error handler functions.
891* Logging handler:: Using a special logging function. 891* Logging handler:: Using a special logging function.
892@end menu 892@end menu
893 893
894@node Progress handler 894@node Progress handler
895@section Progress handler 895@section Progress handler
896 896
897It is often useful to retrieve some feedback while long running 897It is often useful to retrieve some feedback while long running
898operations are performed. 898operations are performed.
899 899
900@deftp {Data type} gcry_handler_progress_t 900@deftp {Data type} gcry_handler_progress_t
901Progress handler functions have to be of the type 901Progress handler functions have to be of the type
902@code{gcry_handler_progress_t}, which is defined as: 902@code{gcry_handler_progress_t}, which is defined as:
903 903
904@code{void (*gcry_handler_progress_t) (void *, const char *, int, int, int)} 904@code{void (*gcry_handler_progress_t) (void *, const char *, int, int, int)}
905@end deftp 905@end deftp
906 906
907The following function may be used to register a handler function for 907The following function may be used to register a handler function for
908this purpose. 908this purpose.
909 909
910@deftypefun void gcry_set_progress_handler (gcry_handler_progress_t @var{cb}, void *@var{cb_data}) 910@deftypefun void gcry_set_progress_handler (gcry_handler_progress_t @var{cb}, void *@var{cb_data})
911 911
912This function installs @var{cb} as the `Progress handler' function. 912This function installs @var{cb} as the `Progress handler' function.
913@var{cb} must be defined as follows: 913@var{cb} must be defined as follows:
914 914
915@example 915@example
916void 916void
917my_progress_handler (void *@var{cb_data}, const char *@var{what}, 917my_progress_handler (void *@var{cb_data}, const char *@var{what},
918 int @var{printchar}, int @var{current}, int @var{total}) 918 int @var{printchar}, int @var{current}, int @var{total})
919@{ 919@{
920 /* Do something. */ 920 /* Do something. */
921@} 921@}
922@end example 922@end example
923 923
924A description of the arguments of the progress handler function follows. 924A description of the arguments of the progress handler function follows.
925 925
926@table @var 926@table @var
927@item cb_data 927@item cb_data
928The argument provided in the call to @code{gcry_set_progress_handler}. 928The argument provided in the call to @code{gcry_set_progress_handler}.
929@item what 929@item what
930A string identifying the type of the progress output. The following 930A string identifying the type of the progress output. The following
931values for @var{what} are defined: 931values for @var{what} are defined:
932 932
933@table @code 933@table @code
934@item need_entropy 934@item need_entropy
935Not enough entropy is available. @var{total} holds the number of 935Not enough entropy is available. @var{total} holds the number of
936required bytes. 936required bytes.
937 937
938@item primegen 938@item primegen
939Values for @var{printchar}: 939Values for @var{printchar}:
940@table @code 940@table @code
941@item \n 941@item \n
942Prime generated. 942Prime generated.
943@item ! 943@item !
944Need to refresh the pool of prime numbers. 944Need to refresh the pool of prime numbers.
945@item <, > 945@item <, >
946Number of bits adjusted. 946Number of bits adjusted.
947@item ^ 947@item ^
948Searching for a generator. 948Searching for a generator.
949@item . 949@item .
950Fermat test on 10 candidates failed. 950Fermat test on 10 candidates failed.
951@item : 951@item :
952Restart with a new random value. 952Restart with a new random value.
953@item + 953@item +
954Rabin Miller test passed. 954Rabin Miller test passed.
955@end table 955@end table
956 956
957@end table 957@end table
958 958
959@end table 959@end table
960@end deftypefun 960@end deftypefun
961 961
962@node Allocation handler 962@node Allocation handler
963@section Allocation handler 963@section Allocation handler
964 964
965It is possible to make @acronym{Libgcrypt} use special memory 965It is possible to make @acronym{Libgcrypt} use special memory
966allocation functions instead of the built-in ones. 966allocation functions instead of the built-in ones.
967 967
968Memory allocation functions are of the following types: 968Memory allocation functions are of the following types:
969@deftp {Data type} gcry_handler_alloc_t 969@deftp {Data type} gcry_handler_alloc_t
970This type is defined as: @code{void *(*gcry_handler_alloc_t) (size_t n)}. 970This type is defined as: @code{void *(*gcry_handler_alloc_t) (size_t n)}.
971@end deftp 971@end deftp
972@deftp {Data type} gcry_handler_secure_check_t 972@deftp {Data type} gcry_handler_secure_check_t
973This type is defined as: @code{int *(*gcry_handler_secure_check_t) (const void *)}. 973This type is defined as: @code{int *(*gcry_handler_secure_check_t) (const void *)}.
974@end deftp 974@end deftp
975@deftp {Data type} gcry_handler_realloc_t 975@deftp {Data type} gcry_handler_realloc_t
976This type is defined as: @code{void *(*gcry_handler_realloc_t) (void *p, size_t n)}. 976This type is defined as: @code{void *(*gcry_handler_realloc_t) (void *p, size_t n)}.
977@end deftp 977@end deftp
978@deftp {Data type} gcry_handler_free_t 978@deftp {Data type} gcry_handler_free_t
979This type is defined as: @code{void *(*gcry_handler_free_t) (void *)}. 979This type is defined as: @code{void *(*gcry_handler_free_t) (void *)}.
980@end deftp 980@end deftp
981 981
982Special memory allocation functions can be installed with the 982Special memory allocation functions can be installed with the
983following function: 983following function:
984 984
985@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}) 985@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})
986Install the provided functions and use them instead of the built-in 986Install the provided functions and use them instead of the built-in
987functions for doing memory allocation. 987functions for doing memory allocation.
988@end deftypefun 988@end deftypefun
989 989
990@node Error handler 990@node Error handler
991@section Error handler 991@section Error handler
992 992
993The following functions may be used to register handler functions that 993The following functions may be used to register handler functions that
994are called by @acronym{Libgcrypt} in case certain error conditions 994are called by @acronym{Libgcrypt} in case certain error conditions
995occur. 995occur.
996 996
997@deftp {Data type} gcry_handler_no_mem_t 997@deftp {Data type} gcry_handler_no_mem_t
998This type is defined as: @code{void (*gcry_handler_no_mem_t) (void *, size_t, unsigned int)} 998This type is defined as: @code{void (*gcry_handler_no_mem_t) (void *, size_t, unsigned int)}
999@end deftp 999@end deftp
1000@deftypefun void gcry_set_outofcore_handler (gcry_handler_no_mem_t @var{func_no_mem}, void *@var{cb_data}) 1000@deftypefun void gcry_set_outofcore_handler (gcry_handler_no_mem_t @var{func_no_mem}, void *@var{cb_data})
1001This function registers @var{func_no_mem} as `out-of-core handler', 1001This function registers @var{func_no_mem} as `out-of-core handler',
1002which means that it will be called in the case of not having enough 1002which means that it will be called in the case of not having enough
1003memory available. 1003memory available.
1004@end deftypefun 1004@end deftypefun
1005 1005
1006@deftp {Data type} gcry_handler_error_t 1006@deftp {Data type} gcry_handler_error_t
1007This type is defined as: @code{void (*gcry_handler_error_t) (void *, int, const char *)} 1007This type is defined as: @code{void (*gcry_handler_error_t) (void *, int, const char *)}
1008@end deftp 1008@end deftp
1009 1009
1010@deftypefun void gcry_set_fatalerror_handler (gcry_handler_error_t @var{func_error}, void *@var{cb_data}) 1010@deftypefun void gcry_set_fatalerror_handler (gcry_handler_error_t @var{func_error}, void *@var{cb_data})
1011This function registers @var{func_error} as `error handler', 1011This function registers @var{func_error} as `error handler',
1012which means that it will be called in error conditions. 1012which means that it will be called in error conditions.
1013@end deftypefun 1013@end deftypefun
1014 1014
1015@node Logging handler 1015@node Logging handler
1016@section Logging handler 1016@section Logging handler
1017 1017
1018@deftp {Data type} gcry_handler_log_t 1018@deftp {Data type} gcry_handler_log_t
1019This type is defined as: @code{void (*gcry_handler_log_t) (void *, int, const char *, va_list)} 1019This type is defined as: @code{void (*gcry_handler_log_t) (void *, int, const char *, va_list)}
1020@end deftp 1020@end deftp
1021 1021
1022@deftypefun void gcry_set_log_handler (gcry_handler_log_t @var{func_log}, void *@var{cb_data}) 1022@deftypefun void gcry_set_log_handler (gcry_handler_log_t @var{func_log}, void *@var{cb_data})
1023This function registers @var{func_log} as `logging handler', which 1023This function registers @var{func_log} as `logging handler', which
1024means that it will be called in case @acronym{Libgcrypt} wants to log 1024means that it will be called in case @acronym{Libgcrypt} wants to log
1025a message. 1025a message.
1026@end deftypefun 1026@end deftypefun
1027 1027
1028@c ********************************************************** 1028@c **********************************************************
1029@c ******************* Ciphers **************************** 1029@c ******************* Ciphers ****************************
1030@c ********************************************************** 1030@c **********************************************************
1031@c @include cipher-ref.texi 1031@c @include cipher-ref.texi
1032@node Symmetric cryptography 1032@node Symmetric cryptography
1033@chapter Symmetric cryptography 1033@chapter Symmetric cryptography
1034 1034
1035The cipher functions are used for symmetrical cryptography, 1035The cipher functions are used for symmetrical cryptography,
1036i.e. cryptography using a shared key. The programming model follows 1036i.e. cryptography using a shared key. The programming model follows
1037an open/process/close paradigm and is in that similar to other 1037an open/process/close paradigm and is in that similar to other
1038building blocks provided by @acronym{Libgcrypt}. 1038building blocks provided by @acronym{Libgcrypt}.
1039 1039
1040@menu 1040@menu
1041* Available ciphers:: List of ciphers supported by the library. 1041* Available ciphers:: List of ciphers supported by the library.
1042* Cipher modules:: How to work with cipher modules. 1042* Cipher modules:: How to work with cipher modules.
1043* Available cipher modes:: List of cipher modes supported by the library. 1043* Available cipher modes:: List of cipher modes supported by the library.
1044* Working with cipher handles:: How to perform operations related to cipher handles. 1044* Working with cipher handles:: How to perform operations related to cipher handles.
1045* General cipher functions:: General cipher functions independent of cipher handles. 1045* General cipher functions:: General cipher functions independent of cipher handles.
1046@end menu 1046@end menu
1047 1047
1048@node Available ciphers 1048@node Available ciphers
1049@section Available ciphers 1049@section Available ciphers
1050 1050
1051@table @code 1051@table @code
1052@item GCRY_CIPHER_NONE 1052@item GCRY_CIPHER_NONE
1053This is not a real algorithm but used by some functions as error return. 1053This is not a real algorithm but used by some functions as error return.
1054The value always evaluates to false. 1054The value always evaluates to false.
1055 1055
1056@item GCRY_CIPHER_IDEA 1056@item GCRY_CIPHER_IDEA
1057This is the IDEA algorithm. The constant is provided but there is 1057This is the IDEA algorithm. The constant is provided but there is
1058currently no implementation for it because the algorithm is patented. 1058currently no implementation for it because the algorithm is patented.
1059 1059
1060@item GCRY_CIPHER_3DES 1060@item GCRY_CIPHER_3DES
1061Triple-DES with 3 Keys as EDE. The key size of this algorithm is 168 but 1061Triple-DES with 3 Keys as EDE. The key size of this algorithm is 168 but
1062you have to pass 192 bits because the most significant bits of each byte 1062you have to pass 192 bits because the most significant bits of each byte
1063are ignored. 1063are ignored.
1064 1064
1065@item GCRY_CIPHER_CAST5 1065@item GCRY_CIPHER_CAST5
1066CAST128-5 block cipher algorithm. The key size is 128 bits. 1066CAST128-5 block cipher algorithm. The key size is 128 bits.
1067 1067
1068@item GCRY_CIPHER_BLOWFISH 1068@item GCRY_CIPHER_BLOWFISH
1069The blowfish algorithm. The current implementation allows only for a key 1069The blowfish algorithm. The current implementation allows only for a key
1070size of 128 bits. 1070size of 128 bits.
1071 1071
1072@item GCRY_CIPHER_SAFER_SK128 1072@item GCRY_CIPHER_SAFER_SK128
1073Reserved and not currently implemented. 1073Reserved and not currently implemented.
1074 1074
1075@item GCRY_CIPHER_DES_SK 1075@item GCRY_CIPHER_DES_SK
1076Reserved and not currently implemented. 1076Reserved and not currently implemented.
1077 1077
1078@item GCRY_CIPHER_AES 1078@item GCRY_CIPHER_AES
1079@itemx GCRY_CIPHER_AES128 1079@itemx GCRY_CIPHER_AES128
1080@itemx GCRY_CIPHER_RIJNDAEL 1080@itemx GCRY_CIPHER_RIJNDAEL
1081@itemx GCRY_CIPHER_RIJNDAEL128 1081@itemx GCRY_CIPHER_RIJNDAEL128
1082AES (Rijndael) with a 128 bit key. 1082AES (Rijndael) with a 128 bit key.
1083 1083
1084@item GCRY_CIPHER_AES192 1084@item GCRY_CIPHER_AES192
1085@itemx GCRY_CIPHER_RIJNDAEL128 1085@itemx GCRY_CIPHER_RIJNDAEL128
1086AES (Rijndael) with a 192 bit key. 1086AES (Rijndael) with a 192 bit key.
1087 1087
1088@item GCRY_CIPHER_AES256 1088@item GCRY_CIPHER_AES256
1089@itemx GCRY_CIPHER_RIJNDAEL256 1089@itemx GCRY_CIPHER_RIJNDAEL256
1090AES (Rijndael) with a 256 bit key. 1090AES (Rijndael) with a 256 bit key.
1091 1091
1092@item GCRY_CIPHER_TWOFISH 1092@item GCRY_CIPHER_TWOFISH
1093The Twofish algorithm with a 256 bit key. 1093The Twofish algorithm with a 256 bit key.
1094 1094
1095@item GCRY_CIPHER_TWOFISH128 1095@item GCRY_CIPHER_TWOFISH128
1096The Twofish algorithm with a 128 bit key. 1096The Twofish algorithm with a 128 bit key.
1097 1097
1098@item GCRY_CIPHER_ARCFOUR 1098@item GCRY_CIPHER_ARCFOUR
1099An algorithm which is 100% compatible with RSA Inc.'s RC4 algorithm. 1099An algorithm which is 100% compatible with RSA Inc.'s RC4 algorithm.
1100Note that this is a stream cipher and must be used very carefully to 1100Note that this is a stream cipher and must be used very carefully to
1101avoid a couple of weaknesses. 1101avoid a couple of weaknesses.
1102 1102
1103@item GCRY_CIPHER_DES 1103@item GCRY_CIPHER_DES
1104Standard DES with a 56 bit key. You need to pass 64 bit but the high 1104Standard DES with a 56 bit key. You need to pass 64 bit but the high
1105bits of each byte are ignored. Note, that this is a weak algorithm 1105bits of each byte are ignored. Note, that this is a weak algorithm
1106which can be broken in reasonable time using a brute force approach. 1106which can be broken in reasonable time using a brute force approach.
1107 1107
1108@end table 1108@end table
1109 1109
1110@node Cipher modules 1110@node Cipher modules
1111@section Cipher modules 1111@section Cipher modules
1112 1112
1113@acronym{Libgcrypt} makes it possible to load additional `cipher 1113@acronym{Libgcrypt} makes it possible to load additional `cipher
1114modules'; these cipher can be used just like the cipher algorithms 1114modules'; these cipher can be used just like the cipher algorithms
1115that are built into the library directly. For an introduction into 1115that are built into the library directly. For an introduction into
1116extension modules, see @xref{Modules}. 1116extension modules, see @xref{Modules}.
1117 1117
1118@deftp {Data type} gcry_cipher_spec_t 1118@deftp {Data type} gcry_cipher_spec_t
1119This is the `module specification structure' needed for registering 1119This is the `module specification structure' needed for registering
1120cipher modules, which has to be filled in by the user before it can be 1120cipher modules, which has to be filled in by the user before it can be
1121used to register a module. It contains the following members: 1121used to register a module. It contains the following members:
1122 1122
1123@table @code 1123@table @code
1124@item const char *name 1124@item const char *name
1125The primary name of the algorithm. 1125The primary name of the algorithm.
1126@item const char **aliases 1126@item const char **aliases
1127A list of strings that are `aliases' for the algorithm. The list must 1127A list of strings that are `aliases' for the algorithm. The list must
1128be terminated with a NULL element. 1128be terminated with a NULL element.
1129@item gcry_cipher_oid_spec_t *oids 1129@item gcry_cipher_oid_spec_t *oids
1130A list of OIDs that are to be associated with the algorithm. The 1130A list of OIDs that are to be associated with the algorithm. The
1131list's last element must have it's `oid' member set to NULL. See 1131list's last element must have it's `oid' member set to NULL. See
1132below for an explanation of this type. 1132below for an explanation of this type.
1133@item size_t blocksize 1133@item size_t blocksize
1134The block size of the algorithm, in bytes. 1134The block size of the algorithm, in bytes.
1135@item size_t keylen 1135@item size_t keylen
1136The length of the key, in bits. 1136The length of the key, in bits.
1137@item size_t contextsize 1137@item size_t contextsize
1138The size of the algorithm-specific `context', that should be allocated 1138The size of the algorithm-specific `context', that should be allocated
1139for each handle. 1139for each handle.
1140@item gcry_cipher_setkey_t setkey 1140@item gcry_cipher_setkey_t setkey
1141The function responsible for initializing a handle with a provided 1141The function responsible for initializing a handle with a provided
1142key. See below for a description of this type. 1142key. See below for a description of this type.
1143@item gcry_cipher_encrypt_t encrypt 1143@item gcry_cipher_encrypt_t encrypt
1144The function responsible for encrypting a single block. See below for 1144The function responsible for encrypting a single block. See below for
1145a description of this type. 1145a description of this type.
1146@item gcry_cipher_decrypt_t decrypt 1146@item gcry_cipher_decrypt_t decrypt
1147The function responsible for decrypting a single block. See below for 1147The function responsible for decrypting a single block. See below for
1148a description of this type. 1148a description of this type.
1149@item gcry_cipher_stencrypt_t stencrypt 1149@item gcry_cipher_stencrypt_t stencrypt
1150Like `encrypt', for stream ciphers. See below for a description of 1150Like `encrypt', for stream ciphers. See below for a description of
1151this type. 1151this type.
1152@item gcry_cipher_stdecrypt_t stdecrypt 1152@item gcry_cipher_stdecrypt_t stdecrypt
1153Like `decrypt', for stream ciphers. See below for a description of 1153Like `decrypt', for stream ciphers. See below for a description of
1154this type. 1154this type.
1155@end table 1155@end table
1156@end deftp 1156@end deftp
1157 1157
1158@deftp {Data type} gcry_cipher_oid_spec_t 1158@deftp {Data type} gcry_cipher_oid_spec_t
1159This type is used for associating a user-provided algorithm 1159This type is used for associating a user-provided algorithm
1160implementation with certain OIDs. It contains the following members: 1160implementation with certain OIDs. It contains the following members:
1161@table @code 1161@table @code
1162@item const char *oid 1162@item const char *oid
1163Textual representation of the OID. 1163Textual representation of the OID.
1164@item int mode 1164@item int mode
1165Cipher mode for which this OID is valid. 1165Cipher mode for which this OID is valid.
1166@end table 1166@end table
1167@end deftp 1167@end deftp
1168 1168
1169@deftp {Data type} gcry_cipher_setkey_t 1169@deftp {Data type} gcry_cipher_setkey_t
1170Type for the `setkey' function, defined as: gcry_err_code_t 1170Type for the `setkey' function, defined as: gcry_err_code_t
1171(*gcry_cipher_setkey_t) (void *c, const unsigned char *key, unsigned 1171(*gcry_cipher_setkey_t) (void *c, const unsigned char *key, unsigned
1172keylen) 1172keylen)
1173@end deftp 1173@end deftp
1174 1174
1175@deftp {Data type} gcry_cipher_encrypt_t 1175@deftp {Data type} gcry_cipher_encrypt_t
1176Type for the `encrypt' function, defined as: gcry_err_code_t 1176Type for the `encrypt' function, defined as: gcry_err_code_t
1177(*gcry_cipher_encrypt_t) (void *c, const unsigned char *outbuf, const 1177(*gcry_cipher_encrypt_t) (void *c, const unsigned char *outbuf, const
1178unsigned char *inbuf) 1178unsigned char *inbuf)
1179@end deftp 1179@end deftp
1180 1180
1181@deftp {Data type} gcry_cipher_decrypt_t 1181@deftp {Data type} gcry_cipher_decrypt_t
1182Type for the `decrypt' function, defined as: gcry_err_code_t 1182Type for the `decrypt' function, defined as: gcry_err_code_t
1183(*gcry_cipher_decrypt_t) (void *c, const unsigned char *outbuf, const 1183(*gcry_cipher_decrypt_t) (void *c, const unsigned char *outbuf, const
1184unsigned char *inbuf) 1184unsigned char *inbuf)
1185@end deftp 1185@end deftp
1186 1186
1187@deftp {Data type} gcry_cipher_stencrypt_t 1187@deftp {Data type} gcry_cipher_stencrypt_t
1188Type for the `stencrypt' function, defined as: gcry_err_code_t 1188Type for the `stencrypt' function, defined as: gcry_err_code_t
1189(*gcry_cipher_stencrypt_t) (void *c, const unsigned char *outbuf, const 1189(*gcry_cipher_stencrypt_t) (void *c, const unsigned char *outbuf, const
1190unsigned char *, unsigned int n) 1190unsigned char *, unsigned int n)
1191@end deftp 1191@end deftp
1192 1192
1193@deftp {Data type} gcry_cipher_stdecrypt_t 1193@deftp {Data type} gcry_cipher_stdecrypt_t
1194Type for the `stdecrypt' function, defined as: gcry_err_code_t 1194Type for the `stdecrypt' function, defined as: gcry_err_code_t
1195(*gcry_cipher_stdecrypt_t) (void *c, const unsigned char *outbuf, const 1195(*gcry_cipher_stdecrypt_t) (void *c, const unsigned char *outbuf, const
1196unsigned char *, unsigned int n) 1196unsigned char *, unsigned int n)
1197@end deftp 1197@end deftp
1198 1198
1199@deftypefun gcry_error_t gcry_cipher_register (gcry_cipher_spec_t *@var{cipher}, unsigned int *algorithm_id, gcry_module_t *@var{module}) 1199@deftypefun gcry_error_t gcry_cipher_register (gcry_cipher_spec_t *@var{cipher}, unsigned int *algorithm_id, gcry_module_t *@var{module})
1200 1200
1201Register a new cipher module whose specification can be found in 1201Register a new cipher module whose specification can be found in
1202@var{cipher}. On success, a new algorithm ID is stored in 1202@var{cipher}. On success, a new algorithm ID is stored in
1203@var{algorithm_id} and a pointer representing this module is stored 1203@var{algorithm_id} and a pointer representing this module is stored
1204in @var{module}. 1204in @var{module}.
1205@end deftypefun 1205@end deftypefun
1206 1206
1207@deftypefun void gcry_cipher_unregister (gcry_module_t @var{module}) 1207@deftypefun void gcry_cipher_unregister (gcry_module_t @var{module})
1208Unregister the cipher identified by @var{module}, which must have been 1208Unregister the cipher identified by @var{module}, which must have been
1209registered with gcry_cipher_register. 1209registered with gcry_cipher_register.
1210@end deftypefun 1210@end deftypefun
1211 1211
1212@deftypefun gcry_error_t gcry_cipher_list (int *@var{list}, int *@var{list_length}) 1212@deftypefun gcry_error_t gcry_cipher_list (int *@var{list}, int *@var{list_length})
1213Get a list consisting of the IDs of the loaded cipher modules. If 1213Get a list consisting of the IDs of the loaded cipher modules. If
1214@var{list} is zero, write the number of loaded cipher modules to 1214@var{list} is zero, write the number of loaded cipher modules to
1215@var{list_length} and return. If @var{list} is non-zero, the first 1215@var{list_length} and return. If @var{list} is non-zero, the first
1216*@var{list_length} algorithm IDs are stored in @var{list}, which must 1216*@var{list_length} algorithm IDs are stored in @var{list}, which must
1217be of according size. In case there are less cipher modules than 1217be of according size. In case there are less cipher modules than
1218*@var{list_length}, *@var{list_length} is updated to the correct 1218*@var{list_length}, *@var{list_length} is updated to the correct
1219number. 1219number.
1220@end deftypefun 1220@end deftypefun
1221 1221
1222@node Available cipher modes 1222@node Available cipher modes
1223@section Available cipher modes 1223@section Available cipher modes
1224 1224
1225@table @code 1225@table @code
1226@item GCRY_CIPHER_MODE_NONE 1226@item GCRY_CIPHER_MODE_NONE
1227No mode specified, may be set later using other functions. The value 1227No mode specified, may be set later using other functions. The value
1228of this constant is always 0. 1228of this constant is always 0.
1229 1229
1230@item GCRY_CIPHER_MODE_ECB 1230@item GCRY_CIPHER_MODE_ECB
1231Electronic Codebook mode. 1231Electronic Codebook mode.
1232 1232
1233@item GCRY_CIPHER_MODE_CFB 1233@item GCRY_CIPHER_MODE_CFB
1234Cipher Feedback mode. 1234Cipher Feedback mode.
1235 1235
1236@item GCRY_CIPHER_MODE_CBC 1236@item GCRY_CIPHER_MODE_CBC
1237Cipher Block Chaining mode. 1237Cipher Block Chaining mode.
1238 1238
1239@item GCRY_CIPHER_MODE_STREAM 1239@item GCRY_CIPHER_MODE_STREAM
1240Stream mode, only to be used with stream cipher algorithms. 1240Stream mode, only to be used with stream cipher algorithms.
1241 1241
1242@item GCRY_CIPHER_MODE_OFB 1242@item GCRY_CIPHER_MODE_OFB
1243Outer Feedback mode. 1243Outer Feedback mode.
1244 1244
1245@item GCRY_CIPHER_MODE_CTR 1245@item GCRY_CIPHER_MODE_CTR
1246Counter mode. 1246Counter mode.
1247 1247
1248@end table 1248@end table
1249 1249
1250@node Working with cipher handles 1250@node Working with cipher handles
1251@section Working with cipher handles 1251@section Working with cipher handles
1252 1252
1253To use a cipher algorithm, you must first allocate an according 1253To use a cipher algorithm, you must first allocate an according
1254handle. This is to be done using the open function: 1254handle. This is to be done using the open function:
1255 1255
1256@deftypefun gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *@var{hd}, int @var{algo}, int @var{mode}, unsigned int @var{flags}) 1256@deftypefun gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *@var{hd}, int @var{algo}, int @var{mode}, unsigned int @var{flags})
1257 1257
1258This function creates the context handle required for most of the 1258This function creates the context handle required for most of the
1259other cipher functions and returns a handle to it in `hd'. In case of 1259other cipher functions and returns a handle to it in `hd'. In case of
1260an error, an according error code is returned. 1260an error, an according error code is returned.
1261 1261
1262The ID of algorithm to use must be specified via @var{algo}. See 1262The ID of algorithm to use must be specified via @var{algo}. See
1263@xref{Available ciphers}, for a list of supported ciphers and the 1263@xref{Available ciphers}, for a list of supported ciphers and the
1264according constants. 1264according constants.
1265 1265
1266Besides using the constants directly, the function 1266Besides using the constants directly, the function
1267@code{gcry_cipher_map_name} may be used to convert the textual name of 1267@code{gcry_cipher_map_name} may be used to convert the textual name of
1268an algorithm into the according numeric ID. 1268an algorithm into the according numeric ID.
1269 1269
1270The cipher mode to use must be specified via @var{mode}. See 1270The cipher mode to use must be specified via @var{mode}. See
1271@xref{Available cipher modes}, for a list of supported cipher modes 1271@xref{Available cipher modes}, for a list of supported cipher modes
1272and the according constants. Note, that some modes do not work 1272and the according constants. Note, that some modes do not work
1273together with all algorithms. 1273together with all algorithms.
1274 1274
1275The third argument @var{flags} can either be passed as @code{0} or as 1275The third argument @var{flags} can either be passed as @code{0} or as
1276the bit-wise OR of the following constants. 1276the bit-wise OR of the following constants.
1277 1277
1278@table @code 1278@table @code
1279@item GCRY_CIPHER_SECURE 1279@item GCRY_CIPHER_SECURE
1280Make sure that all operations are allocated in secure memory. This is 1280Make sure that all operations are allocated in secure memory. This is
1281useful, when the key material is highly confidential. 1281useful, when the key material is highly confidential.
1282@item GCRY_CIPHER_ENABLE_SYNC 1282@item GCRY_CIPHER_ENABLE_SYNC
1283This flag enables the CFB sync mode, which is a special feature of 1283This flag enables the CFB sync mode, which is a special feature of
1284@acronym{Libgcrypt}'s CFB mode implementation to allow for OpenPGP's CFB variant. 1284@acronym{Libgcrypt}'s CFB mode implementation to allow for OpenPGP's CFB variant.
1285See @code{gcry_cipher_sync}. 1285See @code{gcry_cipher_sync}.
1286@item GCRY_CIPHER_CBC_CTS 1286@item GCRY_CIPHER_CBC_CTS
1287Enable cipher text stealing (CTS) for the CBC mode. Cannot be used 1287Enable cipher text stealing (CTS) for the CBC mode. Cannot be used
1288simultaneous as GCRY_CIPHER_CBC_MAC 1288simultaneous as GCRY_CIPHER_CBC_MAC
1289@item GCRY_CIPHER_CBC_MAC 1289@item GCRY_CIPHER_CBC_MAC
1290Compute CBC-MAC keyed checksums. This is the same as CBC mode, but 1290Compute CBC-MAC keyed checksums. This is the same as CBC mode, but
1291only output the last block. Cannot be used simultaneous as 1291only output the last block. Cannot be used simultaneous as
1292GCRY_CIPHER_CBC_CTS. 1292GCRY_CIPHER_CBC_CTS.
1293@end table 1293@end table
1294@end deftypefun 1294@end deftypefun
1295 1295
1296Use the following function to release an existing handle: 1296Use the following function to release an existing handle:
1297 1297
1298@deftypefun void gcry_cipher_close (gcry_cipher_hd_t @var{h}) 1298@deftypefun void gcry_cipher_close (gcry_cipher_hd_t @var{h})
1299 1299
1300This function releases the context created by @code{gcry_cipher_open}. 1300This function releases the context created by @code{gcry_cipher_open}.
1301@end deftypefun 1301@end deftypefun
1302 1302
1303In order to use a handle for performing cryptographic operations, a 1303In order to use a handle for performing cryptographic operations, a
1304`key' has to be set first: 1304`key' has to be set first:
1305 1305
1306@deftypefun gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t @var{h}, void *@var{k}, size_t @var{l}) 1306@deftypefun gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t @var{h}, void *@var{k}, size_t @var{l})
1307 1307
1308Set the key @var{k} used for encryption or decryption in the context 1308Set the key @var{k} used for encryption or decryption in the context
1309denoted by the handle @var{h}. The length @var{l} of the key @var{k} 1309denoted by the handle @var{h}. The length @var{l} of the key @var{k}
1310must match the required length of the algorithm set for this context or 1310must match the required length of the algorithm set for this context or
1311be in the allowed range for algorithms with variable key size. The 1311be in the allowed range for algorithms with variable key size. The
1312function checks this and returns an error if there is a problem. A 1312function checks this and returns an error if there is a problem. A
1313caller should always check for an error. 1313caller should always check for an error.
1314 1314
1315Note, this is currently implemented as a macro but may be changed to a 1315Note, this is currently implemented as a macro but may be changed to a
1316function in the future. 1316function in the future.
1317@end deftypefun 1317@end deftypefun
1318 1318
1319Most crypto modes requires an initialization vector (IV), which 1319Most crypto modes requires an initialization vector (IV), which
1320usually is a non-secret random string acting as a kind of salt value. 1320usually is a non-secret random string acting as a kind of salt value.
1321The CTR mode requires a counter, which is also similar to a salt 1321The CTR mode requires a counter, which is also similar to a salt
1322value. To set the IV or CTR, use these functions: 1322value. To set the IV or CTR, use these functions:
1323 1323
1324@deftypefun gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t @var{h}, void *@var{k}, size_t @var{l}) 1324@deftypefun gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t @var{h}, void *@var{k}, size_t @var{l})
1325 1325
1326Set the initialization vector used for encryption or decryption. The 1326Set the initialization vector used for encryption or decryption. The
1327vector is passed as the buffer @var{K} of length @var{l} and copied to 1327vector is passed as the buffer @var{K} of length @var{l} and copied to
1328internal data structures. The function checks that the IV matches the 1328internal data structures. The function checks that the IV matches the
1329requirement of the selected algorithm and mode. Note, that this is 1329requirement of the selected algorithm and mode. Note, that this is
1330implemented as a macro. 1330implemented as a macro.
1331@end deftypefun 1331@end deftypefun
1332 1332
1333@deftypefun gcry_error_t gcry_cipher_setctr (gcry_cipher_hd_t @var{h}, void *@var{c}, size_t @var{l}) 1333@deftypefun gcry_error_t gcry_cipher_setctr (gcry_cipher_hd_t @var{h}, void *@var{c}, size_t @var{l})
1334 1334
1335Set the counter vector used for encryption or decryption. The counter 1335Set the counter vector used for encryption or decryption. The counter
1336is passed as the buffer @var{c} of length @var{l} and copied to 1336is passed as the buffer @var{c} of length @var{l} and copied to
1337internal data structures. The function checks that the counter 1337internal data structures. The function checks that the counter
1338matches the requirement of the selected algorithm (i.e., it must be 1338matches the requirement of the selected algorithm (i.e., it must be
1339the same size as the block size). Note, that this is implemented as a 1339the same size as the block size). Note, that this is implemented as a
1340macro. 1340macro.
1341@end deftypefun 1341@end deftypefun
1342 1342
1343@deftypefun gcry_error_t gcry_cipher_reset (gcry_cipher_hd_t @var{h}) 1343@deftypefun gcry_error_t gcry_cipher_reset (gcry_cipher_hd_t @var{h})
1344 1344
1345Set the given handle's context back to the state it had after the last 1345Set the given handle's context back to the state it had after the last
1346call to gcry_cipher_setkey and clear the initialization vector. 1346call to gcry_cipher_setkey and clear the initialization vector.
1347 1347
1348Note, that gcry_cipher_reset is implemented as a macro. 1348Note, that gcry_cipher_reset is implemented as a macro.
1349@end deftypefun 1349@end deftypefun
1350 1350
1351The actual encryption and decryption is done by using one of the 1351The actual encryption and decryption is done by using one of the
1352following functions. They may be used as often as required to process 1352following functions. They may be used as often as required to process
1353all the data. 1353all the data.
1354 1354
1355@deftypefun gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t @var{h}, void *{out}, size_t @var{outsize}, const void *@var{in}, size_t @var{inlen}) 1355@deftypefun gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t @var{h}, void *{out}, size_t @var{outsize}, const void *@var{in}, size_t @var{inlen})
1356 1356
1357@code{gcry_cipher_encrypt} is used to encrypt the data. This function 1357@code{gcry_cipher_encrypt} is used to encrypt the data. This function
1358can either work in place or with two buffers. It uses the cipher 1358can either work in place or with two buffers. It uses the cipher
1359context already setup and described by the handle @var{h}. There are 2 1359context already setup and described by the handle @var{h}. There are 2
1360ways to use the function: If @var{in} is passed as @code{NULL} and 1360ways to use the function: If @var{in} is passed as @code{NULL} and
1361@var{inlen} is @code{0}, in-place encryption of the data in @var{out} or 1361@var{inlen} is @code{0}, in-place encryption of the data in @var{out} or
1362length @var{outsize} takes place. With @var{in} being not @code{NULL}, 1362length @var{outsize} takes place. With @var{in} being not @code{NULL},
1363@var{inlen} bytes are encrypted to the buffer @var{out} which must have 1363@var{inlen} bytes are encrypted to the buffer @var{out} which must have
1364at least a size of @var{inlen}. @var{outlen} must be set to the 1364at least a size of @var{inlen}. @var{outlen} must be set to the
1365allocated size of @var{out}, so that the function can check that there 1365allocated size of @var{out}, so that the function can check that there
1366is sufficient space. Note, that overlapping buffers are not allowed. 1366is sufficient space. Note, that overlapping buffers are not allowed.
1367 1367
1368Depending on the selected algorithms and encryption mode, the length of 1368Depending on the selected algorithms and encryption mode, the length of
1369the buffers must be a multiple of the block size. 1369the buffers must be a multiple of the block size.
1370 1370
1371The function returns @code{0} on success or an error code. 1371The function returns @code{0} on success or an error code.
1372@end deftypefun 1372@end deftypefun
1373 1373
1374 1374
1375@deftypefun gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t @var{h}, void *{out}, size_t @var{outsize}, const void *@var{in}, size_t @var{inlen}) 1375@deftypefun gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t @var{h}, void *{out}, size_t @var{outsize}, const void *@var{in}, size_t @var{inlen})
1376 1376
1377@code{gcry_cipher_decrypt} is used to decrypt the data. This function 1377@code{gcry_cipher_decrypt} is used to decrypt the data. This function
1378can either work in place or with two buffers. It uses the cipher 1378can either work in place or with two buffers. It uses the cipher
1379context already setup and described by the handle @var{h}. There are 2 1379context already setup and described by the handle @var{h}. There are 2
1380ways to use the function: If @var{in} is passed as @code{NULL} and 1380ways to use the function: If @var{in} is passed as @code{NULL} and
1381@var{inlen} is @code{0}, in-place decryption of the data in @var{out} or 1381@var{inlen} is @code{0}, in-place decryption of the data in @var{out} or
1382length @var{outsize} takes place. With @var{in} being not @code{NULL}, 1382length @var{outsize} takes place. With @var{in} being not @code{NULL},
1383@var{inlen} bytes are decrypted to the buffer @var{out} which must have 1383@var{inlen} bytes are decrypted to the buffer @var{out} which must have
1384at least a size of @var{inlen}. @var{outlen} must be set to the 1384at least a size of @var{inlen}. @var{outlen} must be set to the
1385allocated size of @var{out}, so that the function can check that there 1385allocated size of @var{out}, so that the function can check that there
1386is sufficient space. Note, that overlapping buffers are not allowed. 1386is sufficient space. Note, that overlapping buffers are not allowed.
1387 1387
1388Depending on the selected algorithms and encryption mode, the length of 1388Depending on the selected algorithms and encryption mode, the length of
1389the buffers must be a multiple of the block size. 1389the buffers must be a multiple of the block size.
1390 1390
1391The function returns @code{0} on success or an error code. 1391The function returns @code{0} on success or an error code.
1392@end deftypefun 1392@end deftypefun
1393 1393
1394 1394
1395OpenPGP (as defined in RFC-2440) requires a special sync operation in 1395OpenPGP (as defined in RFC-2440) requires a special sync operation in
1396some places, the following function is used for this: 1396some places, the following function is used for this:
1397 1397
1398@deftypefun gcry_error_t gcry_cipher_sync (gcry_cipher_hd_t @var{h}) 1398@deftypefun gcry_error_t gcry_cipher_sync (gcry_cipher_hd_t @var{h})
1399 1399
1400Perform the OpenPGP sync operation on context @var{h}. Note, that this 1400Perform the OpenPGP sync operation on context @var{h}. Note, that this
1401is a no-op unless the context was created with the flag 1401is a no-op unless the context was created with the flag
1402@code{GCRY_CIPHER_ENABLE_SYNC} 1402@code{GCRY_CIPHER_ENABLE_SYNC}
1403@end deftypefun 1403@end deftypefun
1404 1404
1405Some of the described functions are implemented as macros utilizing a 1405Some of the described functions are implemented as macros utilizing a
1406catch-all control function. This control function is rarely used 1406catch-all control function. This control function is rarely used
1407directly but there is nothing which would inhibit it: 1407directly but there is nothing which would inhibit it:
1408 1408
1409@deftypefun gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t @var{h}, int @var{cmd}, void *@var{buffer}, size_t @var{buflen}) 1409@deftypefun gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t @var{h}, int @var{cmd}, void *@var{buffer}, size_t @var{buflen})
1410 1410
1411@code{gcry_cipher_ctl} controls various aspects of the cipher module and 1411@code{gcry_cipher_ctl} controls various aspects of the cipher module and
1412specific cipher contexts. Usually some more specialized functions or 1412specific cipher contexts. Usually some more specialized functions or
1413macros are used for this purpose. The semantics of the function and its 1413macros are used for this purpose. The semantics of the function and its
1414parameters depends on the the command @var{cmd} and the passed context 1414parameters depends on the the command @var{cmd} and the passed context
1415handle @var{h}. Please see the comments in the source code 1415handle @var{h}. Please see the comments in the source code
1416(@code{src/global.c}) for details. 1416(@code{src/global.c}) for details.
1417@end deftypefun 1417@end deftypefun
1418 1418
1419@deftypefun gcry_error_t gcry_cipher_info (gcry_cipher_hd_t @var{h}, int @var{what}, void *@var{buffer}, size_t *@var{nbytes}) 1419@deftypefun gcry_error_t gcry_cipher_info (gcry_cipher_hd_t @var{h}, int @var{what}, void *@var{buffer}, size_t *@var{nbytes})
1420 1420
1421@code{gcry_cipher_info} is used to retrieve various 1421@code{gcry_cipher_info} is used to retrieve various
1422information about a cipher context or the cipher module in general. 1422information about a cipher context or the cipher module in general.
1423 1423
1424Currently no information is available. 1424Currently no information is available.
1425@end deftypefun 1425@end deftypefun
1426 1426
1427@node General cipher functions 1427@node General cipher functions
1428@section General cipher functions 1428@section General cipher functions
1429 1429
1430To work with the algorithms, several functions are available to map 1430To work with the algorithms, several functions are available to map
1431algorithm names to the internal identifiers, as well as ways to 1431algorithm names to the internal identifiers, as well as ways to
1432retrieve information about an algorithm or the current cipher context. 1432retrieve information about an algorithm or the current cipher context.
1433 1433
1434@deftypefun gcry_error_t gcry_cipher_algo_info (int @var{algo}, int @var{what}, void *@var{buffer}, size_t *@var{nbytes}) 1434@deftypefun gcry_error_t gcry_cipher_algo_info (int @var{algo}, int @var{what}, void *@var{buffer}, size_t *@var{nbytes})
1435 1435
1436This function is used to retrieve information on a specific algorithm. 1436This function is used to retrieve information on a specific algorithm.
1437You pass the cipher algorithm ID as @var{algo} and the type of 1437You pass the cipher algorithm ID as @var{algo} and the type of
1438information requested as @var{what}. The result is either returned as 1438information requested as @var{what}. The result is either returned as
1439the return code of the function or copied to the provided @var{buffer} 1439the return code of the function or copied to the provided @var{buffer}
1440whose allocated length must be available in an integer variable with the 1440whose allocated length must be available in an integer variable with the
1441address passed in @var{nbytes}. This variable will also receive the 1441address passed in @var{nbytes}. This variable will also receive the
1442actual used length of the buffer. 1442actual used length of the buffer.
1443 1443
1444Here is a list of supported codes for @var{what}: 1444Here is a list of supported codes for @var{what}:
1445 1445
1446@c begin constants for gcry_cipher_algo_info 1446@c begin constants for gcry_cipher_algo_info
1447@table @code 1447@table @code
1448@item GCRYCTL_GET_KEYLEN: 1448@item GCRYCTL_GET_KEYLEN:
1449Return the length of the key. If the algorithm supports multiple key 1449Return the length of the key. If the algorithm supports multiple key
1450lengths, the maximum supported value is returned. The length is 1450lengths, the maximum supported value is returned. The length is
1451returned as number of octets (bytes) and not as number of bits in 1451returned as number of octets (bytes) and not as number of bits in
1452@var{nbytes}; @var{buffer} must be zero. 1452@var{nbytes}; @var{buffer} must be zero.
1453 1453
1454@item GCRYCTL_GET_BLKLEN: 1454@item GCRYCTL_GET_BLKLEN:
1455Return the block length of the algorithm. The length is returned as a 1455Return the block length of the algorithm. The length is returned as a
1456number of octets in @var{nbytes}; @var{buffer} must be zero. 1456number of octets in @var{nbytes}; @var{buffer} must be zero.
1457 1457
1458@item GCRYCTL_TEST_ALGO: 1458@item GCRYCTL_TEST_ALGO:
1459Returns @code{0} when the specified algorithm is available for use. 1459Returns @code{0} when the specified algorithm is available for use.
1460@var{buffer} and @var{nbytes} must be zero. 1460@var{buffer} and @var{nbytes} must be zero.
1461 1461
1462@end table 1462@end table
1463@c end constants for gcry_cipher_algo_info 1463@c end constants for gcry_cipher_algo_info
1464 1464
1465@end deftypefun 1465@end deftypefun
1466@c end gcry_cipher_algo_info 1466@c end gcry_cipher_algo_info
1467 1467
1468@deftypefun const char *gcry_cipher_algo_name (int @var{algo}) 1468@deftypefun const char *gcry_cipher_algo_name (int @var{algo})
1469 1469
1470@code{gcry_cipher_algo_name} returns a string with the name of the 1470@code{gcry_cipher_algo_name} returns a string with the name of the
1471cipher algorithm @var{algo}. If the algorithm is not known or another 1471cipher algorithm @var{algo}. If the algorithm is not known or another
1472error occurred, an empty string is returned. This function will never 1472error occurred, an empty string is returned. This function will never
1473return @code{NULL}. 1473return @code{NULL}.
1474@end deftypefun 1474@end deftypefun
1475 1475
1476@deftypefun int gcry_cipher_map_name (const char *@var{name}) 1476@deftypefun int gcry_cipher_map_name (const char *@var{name})
1477 1477
1478@code{gcry_cipher_map_name} returns the algorithm identifier for the 1478@code{gcry_cipher_map_name} returns the algorithm identifier for the
1479cipher algorithm described by the string @var{name}. If this algorithm 1479cipher algorithm described by the string @var{name}. If this algorithm
1480is not available @code{0} is returned. 1480is not available @code{0} is returned.
1481@end deftypefun 1481@end deftypefun
1482 1482
1483@deftypefun int gcry_cipher_mode_from_oid (const char *@var{string}) 1483@deftypefun int gcry_cipher_mode_from_oid (const char *@var{string})
1484 1484
1485Return the cipher mode associated with an @acronym{ASN.1} object 1485Return the cipher mode associated with an @acronym{ASN.1} object
1486identifier. The object identifier is expected to be in the 1486identifier. The object identifier is expected to be in the
1487@acronym{IETF}-style dotted decimal notation. The function returns 1487@acronym{IETF}-style dotted decimal notation. The function returns
1488@code{0} for an unknown object identifier or when no mode is associated 1488@code{0} for an unknown object identifier or when no mode is associated
1489with it. 1489with it.
1490@end deftypefun 1490@end deftypefun
1491 1491
1492 1492
1493@c ********************************************************** 1493@c **********************************************************
1494@c ******************* Hash Functions ********************* 1494@c ******************* Hash Functions *********************
1495@c ********************************************************** 1495@c **********************************************************
1496@node Hashing 1496@node Hashing
1497@chapter Hashing 1497@chapter Hashing
1498 1498
1499@acronym{Libgcrypt} provides an easy and consistent to use interface 1499@acronym{Libgcrypt} provides an easy and consistent to use interface
1500for hashing. Hashing is buffered and several hash algorithms can be 1500for hashing. Hashing is buffered and several hash algorithms can be
1501updated at once. It is possible to calculate a MAC using the same 1501updated at once. It is possible to calculate a MAC using the same
1502routines. The programming model follows an open/process/close 1502routines. The programming model follows an open/process/close
1503paradigm and is in that similar to other building blocks provided by 1503paradigm and is in that similar to other building blocks provided by
1504@acronym{Libgcrypt}. 1504@acronym{Libgcrypt}.
1505 1505
1506For convenience reasons, a few cyclic redundancy check value operations 1506For convenience reasons, a few cyclic redundancy check value operations
1507are also supported. 1507are also supported.
1508 1508
1509@menu 1509@menu
1510* Available hash algorithms:: List of hash algorithms supported by the library. 1510* Available hash algorithms:: List of hash algorithms supported by the library.
1511* Hash algorithm modules:: How to work with hash algorithm modules. 1511* Hash algorithm modules:: How to work with hash algorithm modules.
1512* Working with hash algorithms:: List of functions related to hashing. 1512* Working with hash algorithms:: List of functions related to hashing.
1513@end menu 1513@end menu
1514 1514
1515@node Available hash algorithms 1515@node Available hash algorithms
1516@section Available hash algorithms 1516@section Available hash algorithms
1517 1517
1518@c begin table of hash algorithms 1518@c begin table of hash algorithms
1519@table @code 1519@table @code
1520@item GCRY_MD_NONE 1520@item GCRY_MD_NONE
1521This is not a real algorithm but used by some functions as an error 1521This is not a real algorithm but used by some functions as an error
1522return value. This constant is guaranteed to have the value @code{0}. 1522return value. This constant is guaranteed to have the value @code{0}.
1523 1523
1524@item GCRY_MD_SHA1 1524@item GCRY_MD_SHA1
1525This is the SHA-1 algorithm which yields a message digest of 20 bytes. 1525This is the SHA-1 algorithm which yields a message digest of 20 bytes.
1526 1526
1527@item GCRY_MD_RMD160 1527@item GCRY_MD_RMD160
1528This is the 160 bit version of the RIPE message digest (RIPE-MD-160). 1528This is the 160 bit version of the RIPE message digest (RIPE-MD-160).
1529Like SHA-1 it also yields a digest of 20 bytes. 1529Like SHA-1 it also yields a digest of 20 bytes.
1530 1530
1531@item GCRY_MD_MD5 1531@item GCRY_MD_MD5
1532This is the well known MD5 algorithm, which yields a message digest of 1532This is the well known MD5 algorithm, which yields a message digest of
153316 bytes. 153316 bytes.
1534 1534
1535@item GCRY_MD_MD4 1535@item GCRY_MD_MD4
1536This is the MD4 algorithm, which yields a message digest of 16 bytes. 1536This is the MD4 algorithm, which yields a message digest of 16 bytes.
1537 1537
1538@item GCRY_MD_MD2 1538@item GCRY_MD_MD2
1539This is an reserved identifier for MD-2; there is no implementation yet. 1539This is an reserved identifier for MD-2; there is no implementation yet.
1540 1540
1541@item GCRY_MD_TIGER 1541@item GCRY_MD_TIGER
1542This is the TIGER/192 algorithm which yields a message digest of 24 bytes. 1542This is the TIGER/192 algorithm which yields a message digest of 24 bytes.
1543 1543
1544@item GCRY_MD_HAVAL 1544@item GCRY_MD_HAVAL
1545This is an reserved for the HAVAL algorithm with 5 passes and 160 1545This is an reserved for the HAVAL algorithm with 5 passes and 160
1546bit. It yields a message digest of 20 bytes. Note that there is no 1546bit. It yields a message digest of 20 bytes. Note that there is no
1547implementation yet available. 1547implementation yet available.
1548 1548
1549@item GCRY_MD_SHA256 1549@item GCRY_MD_SHA256
1550This is the SHA-256 algorithm which yields a message digest of 32 bytes. 1550This is the SHA-256 algorithm which yields a message digest of 32 bytes.
1551See FIPS 180-2 for the specification. 1551See FIPS 180-2 for the specification.
1552 1552
1553@item GCRY_MD_SHA384 1553@item GCRY_MD_SHA384
1554This is reserved for SHA-2 with 384 bits. It yields a message digest of 1554This is reserved for SHA-2 with 384 bits. It yields a message digest of
155548 bytes. Note that there is no implementation yet available. 155548 bytes. Note that there is no implementation yet available.
1556 1556
1557@item GCRY_MD_SHA512 1557@item GCRY_MD_SHA512
1558This is reserved for SHA-2 with 512 bits. It yields a message digest of 1558This is reserved for SHA-2 with 512 bits. It yields a message digest of
155964 bytes. Note that there is no implementation yet available. 155964 bytes. Note that there is no implementation yet available.
1560 1560
1561@item GCRY_MD_CRC32 1561@item GCRY_MD_CRC32
1562This is the ISO 3309 and ITU-T V.42 cyclic redundancy check. It 1562This is the ISO 3309 and ITU-T V.42 cyclic redundancy check. It
1563yields an output of 4 bytes. 1563yields an output of 4 bytes.
1564 1564
1565@item GCRY_MD_CRC32_RFC1510 1565@item GCRY_MD_CRC32_RFC1510
1566This is the above cyclic redundancy check function, as modified by RFC 1566This is the above cyclic redundancy check function, as modified by RFC
15671510. It yields an output of 4 bytes. 15671510. It yields an output of 4 bytes.
1568 1568
1569@item GCRY_MD_CRC24_RFC2440 1569@item GCRY_MD_CRC24_RFC2440
1570This is the OpenPGP cyclic redundancy check function. It yields an 1570This is the OpenPGP cyclic redundancy check function. It yields an
1571output of 3 bytes. 1571output of 3 bytes.
1572 1572
1573@end table 1573@end table
1574@c end table of hash algorithms 1574@c end table of hash algorithms
1575 1575
1576@node Hash algorithm modules 1576@node Hash algorithm modules
1577@section Hash algorithm modules 1577@section Hash algorithm modules
1578 1578
1579@acronym{Libgcrypt} makes it possible to load additional `message 1579@acronym{Libgcrypt} makes it possible to load additional `message
1580digest modules'; these cipher can be used just like the message digest 1580digest modules'; these cipher can be used just like the message digest
1581algorithms that are built into the library directly. For an 1581algorithms that are built into the library directly. For an
1582introduction into extension modules, see @xref{Modules}. 1582introduction into extension modules, see @xref{Modules}.
1583 1583
1584@deftp {Data type} gcry_md_spec_t 1584@deftp {Data type} gcry_md_spec_t
1585This is the `module specification structure' needed for registering 1585This is the `module specification structure' needed for registering
1586message digest modules, which has to be filled in by the user before 1586message digest modules, which has to be filled in by the user before
1587it can be used to register a module. It contains the following 1587it can be used to register a module. It contains the following
1588members: 1588members:
1589 1589
1590@table @code 1590@table @code
1591@item const char *name 1591@item const char *name
1592The primary name of this algorithm. 1592The primary name of this algorithm.
1593@item unsigned char *asnoid 1593@item unsigned char *asnoid
1594Array of bytes that form the ASN OID. 1594Array of bytes that form the ASN OID.
1595@item int asnlen 1595@item int asnlen
1596Length of bytes in `asnoid'. 1596Length of bytes in `asnoid'.
1597@item gcry_md_oid_spec_t *oids 1597@item gcry_md_oid_spec_t *oids
1598A list of OIDs that are to be associated with the algorithm. The 1598A list of OIDs that are to be associated with the algorithm. The
1599list's last element must have it's `oid' member set to NULL. See 1599list's last element must have it's `oid' member set to NULL. See
1600below for an explanation of this type. See below for an explanation 1600below for an explanation of this type. See below for an explanation
1601of this type. 1601of this type.
1602@item int mdlen 1602@item int mdlen
1603Length of the message digest algorithm. See below for an explanation 1603Length of the message digest algorithm. See below for an explanation
1604of this type. 1604of this type.
1605@item gcry_md_init_t init 1605@item gcry_md_init_t init
1606The function responsible for initializing a handle. See below for an 1606The function responsible for initializing a handle. See below for an
1607explanation of this type. 1607explanation of this type.
1608@item gcry_md_write_t write 1608@item gcry_md_write_t write
1609The function responsible for writing data into a message digest 1609The function responsible for writing data into a message digest
1610context. See below for an explanation of this type. 1610context. See below for an explanation of this type.
1611@item gcry_md_final_t final 1611@item gcry_md_final_t final
1612The function responsible for `finalizing' a message digest context. 1612The function responsible for `finalizing' a message digest context.
1613See below for an explanation of this type. 1613See below for an explanation of this type.
1614@item gcry_md_read_t read 1614@item gcry_md_read_t read
1615The function responsible for reading out a message digest result. See 1615The function responsible for reading out a message digest result. See
1616below for an explanation of this type. 1616below for an explanation of this type.
1617@item size_t contextsize 1617@item size_t contextsize
1618The size of the algorithm-specific `context', that should be 1618The size of the algorithm-specific `context', that should be
1619allocated for each handle. 1619allocated for each handle.
1620@end table 1620@end table
1621@end deftp 1621@end deftp
1622 1622
1623@deftp {Data type} gcry_md_oid_spec_t 1623@deftp {Data type} gcry_md_oid_spec_t
1624This type is used for associating a user-provided algorithm 1624This type is used for associating a user-provided algorithm
1625implementation with certain OIDs. It contains the following members: 1625implementation with certain OIDs. It contains the following members:
1626 1626
1627@table @code 1627@table @code
1628@item const char *oidstring 1628@item const char *oidstring
1629Textual representation of the OID. 1629Textual representation of the OID.
1630@end table 1630@end table
1631@end deftp 1631@end deftp
1632 1632
1633@deftp {Data type} gcry_md_init_t 1633@deftp {Data type} gcry_md_init_t
1634Type for the `init' function, defined as: void (*gcry_md_init_t) (void 1634Type for the `init' function, defined as: void (*gcry_md_init_t) (void
1635*c) 1635*c)
1636@end deftp 1636@end deftp
1637 1637
1638@deftp {Data type} gcry_md_write_t 1638@deftp {Data type} gcry_md_write_t
1639Type for the `write' function, defined as: void (*gcry_md_write_t) 1639Type for the `write' function, defined as: void (*gcry_md_write_t)
1640(void *c, unsigned char *buf, size_t nbytes) 1640(void *c, unsigned char *buf, size_t nbytes)
1641@end deftp 1641@end deftp
1642 1642
1643@deftp {Data type} gcry_md_final_t 1643@deftp {Data type} gcry_md_final_t
1644Type for the `final' function, defined as: void (*gcry_md_final_t) 1644Type for the `final' function, defined as: void (*gcry_md_final_t)
1645(void *c) 1645(void *c)
1646@end deftp 1646@end deftp
1647 1647
1648@deftp {Data type} gcry_md_read_t 1648@deftp {Data type} gcry_md_read_t
1649Type for the `read' function, defined as: unsigned char 1649Type for the `read' function, defined as: unsigned char
1650*(*gcry_md_read_t) (void *c) 1650*(*gcry_md_read_t) (void *c)
1651@end deftp 1651@end deftp
1652 1652
1653@deftypefun gcry_error_t gcry_md_register (gcry_md_spec_t *@var{digest}, unsigned int *algorithm_id, gcry_module_t *@var{module}) 1653@deftypefun gcry_error_t gcry_md_register (gcry_md_spec_t *@var{digest}, unsigned int *algorithm_id, gcry_module_t *@var{module})
1654 1654
1655Register a new digest module whose specification can be found in 1655Register a new digest module whose specification can be found in
1656@var{digest}. On success, a new algorithm ID is stored in 1656@var{digest}. On success, a new algorithm ID is stored in
1657@var{algorithm_id} and a pointer representing this module is stored 1657@var{algorithm_id} and a pointer representing this module is stored
1658in @var{module}. 1658in @var{module}.
1659@end deftypefun 1659@end deftypefun
1660 1660
1661@deftypefun void gcry_md_unregister (gcry_module_t @var{module}) 1661@deftypefun void gcry_md_unregister (gcry_module_t @var{module})
1662Unregister the digest identified by @var{module}, which must have been 1662Unregister the digest identified by @var{module}, which must have been
1663registered with gcry_md_register. 1663registered with gcry_md_register.
1664@end deftypefun 1664@end deftypefun
1665 1665
1666@deftypefun gcry_error_t gcry_md_list (int *@var{list}, int *@var{list_length}) 1666@deftypefun gcry_error_t gcry_md_list (int *@var{list}, int *@var{list_length})
1667Get a list consisting of the IDs of the loaded message digest modules. 1667Get a list consisting of the IDs of the loaded message digest modules.
1668If @var{list} is zero, write the number of loaded message digest 1668If @var{list} is zero, write the number of loaded message digest
1669modules to @var{list_length} and return. If @var{list} is non-zero, 1669modules to @var{list_length} and return. If @var{list} is non-zero,
1670the first *@var{list_length} algorithm IDs are stored in @var{list}, 1670the first *@var{list_length} algorithm IDs are stored in @var{list},
1671which must be of according size. In case there are less message 1671which must be of according size. In case there are less message
1672digests modules than *@var{list_length}, *@var{list_length} is updated 1672digests modules than *@var{list_length}, *@var{list_length} is updated
1673to the correct number. 1673to the correct number.
1674@end deftypefun 1674@end deftypefun
1675 1675
1676@node Working with hash algorithms 1676@node Working with hash algorithms
1677@section Working with hash algorithms 1677@section Working with hash algorithms
1678 1678
1679To use most of these function it is necessary to create a context; 1679To use most of these function it is necessary to create a context;
1680this is done using: 1680this is done using:
1681 1681
1682@deftypefun gcry_error_t gcry_md_open (gcry_md_hd_t *@var{hd}, int @var{algo}, unsigned int @var{flags}) 1682@deftypefun gcry_error_t gcry_md_open (gcry_md_hd_t *@var{hd}, int @var{algo}, unsigned int @var{flags})
1683 1683
1684Create a message digest object for algorithm @var{algo}. @var{flags} 1684Create a message digest object for algorithm @var{algo}. @var{flags}
1685may be given as an bitwise OR of constants described below. @var{algo} 1685may be given as an bitwise OR of constants described below. @var{algo}
1686may be given as @code{0} if the algorithms to use are later set using 1686may be given as @code{0} if the algorithms to use are later set using
1687@code{gcry_md_enable}. @var{hd} is guaranteed to either receive a valid 1687@code{gcry_md_enable}. @var{hd} is guaranteed to either receive a valid
1688handle or NULL. 1688handle or NULL.
1689 1689
1690For a list of supported algorithms, see @xref{Available hash 1690For a list of supported algorithms, see @xref{Available hash
1691algorithms}. 1691algorithms}.
1692 1692
1693The flags allowed for @var{mode} are: 1693The flags allowed for @var{mode} are:
1694 1694
1695@c begin table of hash flags 1695@c begin table of hash flags
1696@table @code 1696@table @code
1697@item GCRY_MD_FLAG_SECURE 1697@item GCRY_MD_FLAG_SECURE
1698Allocate all buffers and the resulting digest in "secure memory". Use 1698Allocate all buffers and the resulting digest in "secure memory". Use
1699this is the hashed data is highly confidential. 1699this is the hashed data is highly confidential.
1700 1700
1701@item GCRY_MD_FLAG_HMAC 1701@item GCRY_MD_FLAG_HMAC
1702Turn the algorithm into a HMAC message authentication algorithm. This 1702Turn the algorithm into a HMAC message authentication algorithm. This
1703does only work if just one algorithm is enabled for the handle and 1703does only work if just one algorithm is enabled for the handle and
1704SHA-384 and SHA512 is not used. Note that the function 1704SHA-384 and SHA512 is not used. Note that the function
1705@code{gcry_md_setkey} must be used set the MAC key. If you want CBC 1705@code{gcry_md_setkey} must be used set the MAC key. If you want CBC
1706message authentication codes based on a cipher, see @xref{Working with 1706message authentication codes based on a cipher, see @xref{Working with
1707cipher handles}. 1707cipher handles}.
1708 1708
1709@end table 1709@end table
1710@c begin table of hash flags 1710@c begin table of hash flags
1711 1711
1712You may use the function @code{gcry_md_is_enabled} to later check 1712You may use the function @code{gcry_md_is_enabled} to later check
1713whether an algorithm has been enabled. 1713whether an algorithm has been enabled.
1714 1714
1715@end deftypefun 1715@end deftypefun
1716@c end function gcry_md_open 1716@c end function gcry_md_open
1717 1717
1718If you want to calculate several hash algorithms at the same time, you 1718If you want to calculate several hash algorithms at the same time, you
1719have to use the following function right after the @code{gcry_md_open}: 1719have to use the following function right after the @code{gcry_md_open}:
1720 1720
1721@deftypefun gcry_error_t gcry_md_enable (gcry_md_hd_t @var{h}, int @var{algo}) 1721@deftypefun gcry_error_t gcry_md_enable (gcry_md_hd_t @var{h}, int @var{algo})
1722 1722
1723Add the message digest algorithm @var{algo} to the digest object 1723Add the message digest algorithm @var{algo} to the digest object
1724described by handle @var{h}. Duplicated enabling of algorithms is 1724described by handle @var{h}. Duplicated enabling of algorithms is
1725detected and ignored. 1725detected and ignored.
1726@end deftypefun 1726@end deftypefun
1727 1727
1728If the flag @code{GCRY_MD_FLAG_HMAC} was used, the key for the MAC must 1728If the flag @code{GCRY_MD_FLAG_HMAC} was used, the key for the MAC must
1729be set using the function: 1729be set using the function:
1730 1730
1731@deftypefun gcry_error_t gcry_md_setkey (gcry_md_hd_t @var{h}, const void *@var{key}, size_t @var{keylen}) 1731@deftypefun gcry_error_t gcry_md_setkey (gcry_md_hd_t @var{h}, const void *@var{key}, size_t @var{keylen})
1732 1732
1733For use with the HMAC feature, set the MAC key to the value of @var{key} 1733For use with the HMAC feature, set the MAC key to the value of @var{key}
1734of length @var{keylen}. 1734of length @var{keylen}.
1735@end deftypefun 1735@end deftypefun
1736 1736
1737 1737
1738After you are done with the hash calculation, you should release the 1738After you are done with the hash calculation, you should release the
1739resources by using: 1739resources by using:
1740 1740
1741@deftypefun void gcry_md_close (gcry_md_hd_t @var{h}) 1741@deftypefun void gcry_md_close (gcry_md_hd_t @var{h})
1742 1742
1743Release all resources of hash context @var{h}. @var{h} should not be 1743Release all resources of hash context @var{h}. @var{h} should not be
1744used after a call to this function. A @code{NULL} passed as @var{h} is 1744used after a call to this function. A @code{NULL} passed as @var{h} is
1745ignored. 1745ignored.
1746 1746
1747@end deftypefun 1747@end deftypefun
1748 1748
1749Often you have to do several hash operations using the same algorithm. 1749Often you have to do several hash operations using the same algorithm.
1750To avoid the overhead of creating and releasing context, a reset function 1750To avoid the overhead of creating and releasing context, a reset function
1751is provided: 1751is provided:
1752 1752
1753@deftypefun void gcry_md_reset (gcry_md_hd_t @var{h}) 1753@deftypefun void gcry_md_reset (gcry_md_hd_t @var{h})
1754 1754
1755Reset the current context to its initial state. This is effectively 1755Reset the current context to its initial state. This is effectively
1756identical to a close followed by an open and enabling all currently 1756identical to a close followed by an open and enabling all currently
1757active algorithms. 1757active algorithms.
1758@end deftypefun 1758@end deftypefun
1759 1759
1760 1760
1761Often it is necessary to start hashing some data and than continue to 1761Often it is necessary to start hashing some data and than continue to
1762hash different data. To avoid hashing the same data several times (which 1762hash different data. To avoid hashing the same data several times (which
1763might not even be possible if the data is received from a pipe), a 1763might not even be possible if the data is received from a pipe), a
1764snapshot of the current hash context can be taken and turned into a new 1764snapshot of the current hash context can be taken and turned into a new
1765context: 1765context:
1766 1766
1767@deftypefun gcry_error_t gcry_md_copy (gcry_md_hd_t *@var{handle_dst}, gcry_md_hd_t @var{handle_src}) 1767@deftypefun gcry_error_t gcry_md_copy (gcry_md_hd_t *@var{handle_dst}, gcry_md_hd_t @var{handle_src})
1768 1768
1769Create a new digest object as an exact copy of the object described by 1769Create a new digest object as an exact copy of the object described by
1770handle @var{handle_src} and store it in @var{handle_dst}. The context 1770handle @var{handle_src} and store it in @var{handle_dst}. The context
1771is not reset and you can continue to hash data using this context and 1771is not reset and you can continue to hash data using this context and
1772independently using the original context. 1772independently using the original context.
1773@end deftypefun 1773@end deftypefun
1774 1774
1775 1775
1776Now that we have prepared everything to calculate hashes, its time to 1776Now that we have prepared everything to calculate hashes, its time to
1777see how it is actually done. There are 2 ways for this, one to 1777see how it is actually done. There are 2 ways for this, one to
1778update the hash with a block of memory and one macro to update the hash 1778update the hash with a block of memory and one macro to update the hash
1779by just one character. Both may be used intermixed. 1779by just one character. Both may be used intermixed.
1780 1780
1781@deftypefun void gcry_md_write (gcry_md_hd_t @var{h}, const void *@var{buffer}, size_t @var{length}) 1781@deftypefun void gcry_md_write (gcry_md_hd_t @var{h}, const void *@var{buffer}, size_t @var{length})
1782 1782
1783Pass @var{length} bytes of the data in @var{buffer} to the digest object 1783Pass @var{length} bytes of the data in @var{buffer} to the digest object
1784with handle @var{h} to update the digest values. This 1784with handle @var{h} to update the digest values. This
1785function should be used for large blocks of data. 1785function should be used for large blocks of data.
1786@end deftypefun 1786@end deftypefun
1787 1787
1788@deftypefun void gcry_md_putc (gcry_md_hd_t @var{h}, int @var{c}) 1788@deftypefun void gcry_md_putc (gcry_md_hd_t @var{h}, int @var{c})
1789 1789
1790Pass the byte in @var{c} to the digest object with handle @var{h} to 1790Pass the byte in @var{c} to the digest object with handle @var{h} to
1791update the digest value. This is an efficient function, implemented as 1791update the digest value. This is an efficient function, implemented as
1792a macro to buffer the data before an actual update. 1792a macro to buffer the data before an actual update.
1793@end deftypefun 1793@end deftypefun
1794 1794
1795The semantics of the hash functions don't allow to read out intermediate 1795The semantics of the hash functions don't allow to read out intermediate
1796message digests because the calculation must be finalized fist. This 1796message digests because the calculation must be finalized fist. This
1797finalization may for example include the number of bytes hashed in the 1797finalization may for example include the number of bytes hashed in the
1798message digest. 1798message digest.
1799 1799
1800@deftypefun void gcry_md_final (gcry_md_hd_t @var{h}) 1800@deftypefun void gcry_md_final (gcry_md_hd_t @var{h})
1801 1801
1802Finalize the message digest calculation. This is not really needed 1802Finalize the message digest calculation. This is not really needed
1803because @code{gcry_md_read} does this implicitly. After this has been 1803because @code{gcry_md_read} does this implicitly. After this has been
1804done no further updates (by means of @code{gcry_md_write} or 1804done no further updates (by means of @code{gcry_md_write} or
1805@code{gcry_md_putc} are allowed. Only the first call to this function 1805@code{gcry_md_putc} are allowed. Only the first call to this function
1806has an effect. It is implemented as a macro. 1806has an effect. It is implemented as a macro.
1807@end deftypefun 1807@end deftypefun
1808 1808
1809The way to read out the calculated message digest is by using the 1809The way to read out the calculated message digest is by using the
1810function: 1810function:
1811 1811
1812@deftypefun unsigned char *gcry_md_read (gcry_md_hd_t @var{h}, int @var{algo}) 1812@deftypefun unsigned char *gcry_md_read (gcry_md_hd_t @var{h}, int @var{algo})
1813 1813
1814@code{gcry_md_read} returns the message digest after finalizing the 1814@code{gcry_md_read} returns the message digest after finalizing the
1815calculation. This function may be used as often as required but it will 1815calculation. This function may be used as often as required but it will
1816always return the same value for one handle. The returned message digest 1816always return the same value for one handle. The returned message digest
1817is allocated within the message context and therefore valid until the 1817is allocated within the message context and therefore valid until the
1818handle is released or reseted (using @code{gcry_md_close} or 1818handle is released or reseted (using @code{gcry_md_close} or
1819@code{gcry_md_reset}. @var{algo} may be given as 0 to return the only 1819@code{gcry_md_reset}. @var{algo} may be given as 0 to return the only
1820enabled message digest or it may specify one of the enabled algorithms. 1820enabled message digest or it may specify one of the enabled algorithms.
1821The function does return @code{NULL} if the requested algorithm has not 1821The function does return @code{NULL} if the requested algorithm has not
1822been enabled. 1822been enabled.
1823@end deftypefun 1823@end deftypefun
1824 1824
1825Because it is often necessary to get the message digest of one block of 1825Because it is often necessary to get the message digest of one block of
1826memory, a fast convenience function is available for this task: 1826memory, a fast convenience function is available for this task:
1827 1827
1828@deftypefun void gcry_md_hash_buffer (int @var{algo}, void *@var{digest}, const cvoid *@var{buffer}, size_t @var{length}); 1828@deftypefun void gcry_md_hash_buffer (int @var{algo}, void *@var{digest}, const cvoid *@var{buffer}, size_t @var{length});
1829 1829
1830@code{gcry_md_hash_buffer} is a shortcut function to calculate a message 1830@code{gcry_md_hash_buffer} is a shortcut function to calculate a message
1831digest of a buffer. This function does not require a context and 1831digest of a buffer. This function does not require a context and
1832immediately returns the message digest of the @var{length} bytes at 1832immediately returns the message digest of the @var{length} bytes at
1833@var{buffer}. @var{digest} must be allocated by the caller, large 1833@var{buffer}. @var{digest} must be allocated by the caller, large
1834enough to hold the message digest yielded by the the specified algorithm 1834enough to hold the message digest yielded by the the specified algorithm
1835@var{algo}. This required size may be obtained by using the function 1835@var{algo}. This required size may be obtained by using the function
1836@code{gcry_md_get_algo_dlen}. 1836@code{gcry_md_get_algo_dlen}.
1837 1837
1838Note, that this function will abort the process if an unavailable 1838Note, that this function will abort the process if an unavailable
1839algorithm is used. 1839algorithm is used.
1840@end deftypefun 1840@end deftypefun
1841 1841
1842@c *********************************** 1842@c ***********************************
1843@c ***** MD info functions *********** 1843@c ***** MD info functions ***********
1844@c *********************************** 1844@c ***********************************
1845 1845
1846Hash algorithms are identified by internal algorithm numbers (see 1846Hash algorithms are identified by internal algorithm numbers (see
1847@code{gcry_md_open} for a list. However, in most applications they are 1847@code{gcry_md_open} for a list. However, in most applications they are
1848used by names, so 2 functions are available to map between string 1848used by names, so 2 functions are available to map between string
1849representations and hash algorithm identifiers. 1849representations and hash algorithm identifiers.
1850 1850
1851@deftypefun const char *gcry_md_algo_name (int @var{algo}) 1851@deftypefun const char *gcry_md_algo_name (int @var{algo})
1852 1852
1853Map the digest algorithm id @var{algo} to a string representation of the 1853Map the digest algorithm id @var{algo} to a string representation of the
1854algorithm name. For unknown algorithms this functions returns an 1854algorithm name. For unknown algorithms this functions returns an
1855empty string. This function should not be used to test for the 1855empty string. This function should not be used to test for the
1856availability of an algorithm. 1856availability of an algorithm.
1857@end deftypefun 1857@end deftypefun
1858 1858
1859@deftypefun int gcry_md_map_name (const char *@var{name}) 1859@deftypefun int gcry_md_map_name (const char *@var{name})
1860 1860
1861Map the algorithm with @var{name} to a digest algorithm identifier. 1861Map the algorithm with @var{name} to a digest algorithm identifier.
1862Returns 0 if the algorithm name is not known. Names representing 1862Returns 0 if the algorithm name is not known. Names representing
1863@acronym{ASN.1} object identifiers are recognized if the @acronym{IETF} 1863@acronym{ASN.1} object identifiers are recognized if the @acronym{IETF}
1864dotted format is used and the OID is prefixed with either "@code{oid.}" 1864dotted format is used and the OID is prefixed with either "@code{oid.}"
1865or "@code{OID.}". For a list of supported OIDs, see the source code at 1865or "@code{OID.}". For a list of supported OIDs, see the source code at
1866@file{cipher/md.c}. This function should not be used to test for the 1866@file{cipher/md.c}. This function should not be used to test for the
1867availability of an algorithm. 1867availability of an algorithm.
1868@end deftypefun 1868@end deftypefun
1869 1869
1870@deftypefun gcry_error_t gcry_md_get_asnoid (int @var{algo}, void *@var{buffer}, size_t *@var{length}) 1870@deftypefun gcry_error_t gcry_md_get_asnoid (int @var{algo}, void *@var{buffer}, size_t *@var{length})
1871 1871
1872Return an DER encoded ASN.1 OID for the algorithm @var{algo} in the 1872Return an DER encoded ASN.1 OID for the algorithm @var{algo} in the
1873user allocated @var{buffer}. @var{length} must point to variable with 1873user allocated @var{buffer}. @var{length} must point to variable with
1874the available size of @var{buffer} and receives after return the 1874the available size of @var{buffer} and receives after return the
1875actual size of the returned OID. The returned error code may be 1875actual size of the returned OID. The returned error code may be
1876@code{GPG_ERR_TOO_SHORT} if the provided buffer is to short to receive 1876@code{GPG_ERR_TOO_SHORT} if the provided buffer is to short to receive
1877the OID; it is possible to call the function with @code{NULL} for 1877the OID; it is possible to call the function with @code{NULL} for
1878@var{buffer} to have it only return the required size. The function 1878@var{buffer} to have it only return the required size. The function
1879returns 0 on success. 1879returns 0 on success.
1880 1880
1881@end deftypefun 1881@end deftypefun
1882 1882
1883 1883
1884To test whether an algorithm is actually available for use, the 1884To test whether an algorithm is actually available for use, the
1885following macro should be used: 1885following macro should be used:
1886 1886
1887@deftypefun gcry_error_t gcry_md_test_algo (int @var{algo}) 1887@deftypefun gcry_error_t gcry_md_test_algo (int @var{algo})
1888 1888
1889The macro returns 0 if the algorithm @var{algo} is available for use. 1889The macro returns 0 if the algorithm @var{algo} is available for use.
1890@end deftypefun 1890@end deftypefun
1891 1891
1892If the length of a message digest is not known, it can be retrieved 1892If the length of a message digest is not known, it can be retrieved
1893using the following function: 1893using the following function:
1894 1894
1895@deftypefun unsigned int gcry_md_get_algo_dlen (int @var{algo}) 1895@deftypefun unsigned int gcry_md_get_algo_dlen (int @var{algo})
1896 1896
1897Retrieve the length in bytes of the digest yielded by algorithm 1897Retrieve the length in bytes of the digest yielded by algorithm
1898@var{algo}. This is often used prior to @code{gcry_md_read} to allocate 1898@var{algo}. This is often used prior to @code{gcry_md_read} to allocate
1899sufficient memory for the digest. 1899sufficient memory for the digest.
1900@end deftypefun 1900@end deftypefun
1901 1901
1902 1902
1903In some situations it might be hard to remember the algorithm used for 1903In some situations it might be hard to remember the algorithm used for
1904the ongoing hashing. The following function might be used to get that 1904the ongoing hashing. The following function might be used to get that
1905information: 1905information:
1906 1906
1907@deftypefun int gcry_md_get_algo (gcry_md_hd_t @var{h}) 1907@deftypefun int gcry_md_get_algo (gcry_md_hd_t @var{h})
1908 1908
1909Retrieve the algorithm used with the handle @var{h}. Note, that this 1909Retrieve the algorithm used with the handle @var{h}. Note, that this
1910does not work reliable if more than one algorithm is enabled in @var{h}. 1910does not work reliable if more than one algorithm is enabled in @var{h}.
1911@end deftypefun 1911@end deftypefun
1912 1912
1913The following macro might also be useful: 1913The following macro might also be useful:
1914 1914
1915@deftypefun int gcry_md_is_secure (gcry_md_hd_t @var{h}) 1915@deftypefun int gcry_md_is_secure (gcry_md_hd_t @var{h})
1916 1916
1917This function returns true when the digest object @var{h} is allocated 1917This function returns true when the digest object @var{h} is allocated
1918in "secure memory"; i.e. @var{h} was created with the 1918in "secure memory"; i.e. @var{h} was created with the
1919@code{GCRY_MD_FLAG_SECURE}. 1919@code{GCRY_MD_FLAG_SECURE}.
1920@end deftypefun 1920@end deftypefun
1921 1921
1922@deftypefun int gcry_md_is_enabled (gcry_md_hd_t @var{h}, int @var{algo}) 1922@deftypefun int gcry_md_is_enabled (gcry_md_hd_t @var{h}, int @var{algo})
1923 1923
1924This function returns true when the algorithm @var{algo} has been 1924This function returns true when the algorithm @var{algo} has been
1925enabled for the digest object @var{h}. 1925enabled for the digest object @var{h}.
1926@end deftypefun 1926@end deftypefun
1927 1927
1928 1928
1929 1929
1930Tracking bugs related to hashing is often a cumbersome task which 1930Tracking bugs related to hashing is often a cumbersome task which
1931requires to add a lot of printf statements into the code. @acronym{Libgcrypt} 1931requires to add a lot of printf statements into the code. @acronym{Libgcrypt}
1932provides an easy way to avoid this. The actual data hashed can be 1932provides an easy way to avoid this. The actual data hashed can be
1933written to files on request. The following 2 macros should be used to 1933written to files on request. The following 2 macros should be used to
1934implement such a debugging facility: 1934implement such a debugging facility:
1935 1935
1936@deftypefun void gcry_md_start_debug (gcry_md_hd_t @var{h}, const char *@var{suffix}) 1936@deftypefun void gcry_md_start_debug (gcry_md_hd_t @var{h}, const char *@var{suffix})
1937 1937
1938Enable debugging for the digest object with handle @var{h}. This 1938Enable debugging for the digest object with handle @var{h}. This
1939creates create files named @file{dbgmd-<n>.<string>} while doing the 1939creates create files named @file{dbgmd-<n>.<string>} while doing the
1940actual hashing. @var{suffix} is the string part in the filename. The 1940actual hashing. @var{suffix} is the string part in the filename. The
1941number is a counter incremented for each new hashing. The data in the 1941number is a counter incremented for each new hashing. The data in the
1942file is the raw data as passed to @code{gcry_md_write} or 1942file is the raw data as passed to @code{gcry_md_write} or
1943@code{gcry_md_putc}. 1943@code{gcry_md_putc}.
1944@end deftypefun 1944@end deftypefun
1945 1945
1946 1946
1947@deftypefun void gcry_md_stop_debug (gcry_md_hd_t @var{h}, int @var{reserved}) 1947@deftypefun void gcry_md_stop_debug (gcry_md_hd_t @var{h}, int @var{reserved})
1948 1948
1949Stop debugging on handle @var{h}. @var{reserved} should be specified as 1949Stop debugging on handle @var{h}. @var{reserved} should be specified as
19500. This function is usually not required because @code{gcry_md_close} 19500. This function is usually not required because @code{gcry_md_close}
1951does implicitly stop debugging. 1951does implicitly stop debugging.
1952@end deftypefun 1952@end deftypefun
1953 1953
1954 1954
1955@c ********************************************************** 1955@c **********************************************************
1956@c ******************* Public Key ************************* 1956@c ******************* Public Key *************************
1957@c ********************************************************** 1957@c **********************************************************
1958@node Public Key cryptography (I) 1958@node Public Key cryptography (I)
1959@chapter Public Key cryptography (I) 1959@chapter Public Key cryptography (I)
1960 1960
1961Public key cryptography, also known as asymmetric cryptography, is an 1961Public key cryptography, also known as asymmetric cryptography, is an
1962easy way for key management and to provide digital signatures. 1962easy way for key management and to provide digital signatures.
1963@acronym{Libgcrypt} provides two completely different interfaces to 1963@acronym{Libgcrypt} provides two completely different interfaces to
1964public key cryptography, this chapter explains the one based on 1964public key cryptography, this chapter explains the one based on
1965S-expressions. 1965S-expressions.
1966 1966
1967@menu 1967@menu
1968* Available algorithms:: Algorithms supported by the library. 1968* Available algorithms:: Algorithms supported by the library.
1969* Used S-expressions:: Introduction into the used S-expression. 1969* Used S-expressions:: Introduction into the used S-expression.
1970* Public key modules:: How to work with public key modules. 1970* Public key modules:: How to work with public key modules.
1971* Cryptographic Functions:: Functions for performing the cryptographic actions. 1971* Cryptographic Functions:: Functions for performing the cryptographic actions.
1972* General public-key related Functions:: General functions, not implementing any cryptography. 1972* General public-key related Functions:: General functions, not implementing any cryptography.
1973@end menu 1973@end menu
1974 1974
1975@node Available algorithms 1975@node Available algorithms
1976@section Available algorithms 1976@section Available algorithms
1977 1977
1978@acronym{Libgcrypt} supports the RSA (Rivest-Shamir-Adleman) algorithms as well 1978@acronym{Libgcrypt} supports the RSA (Rivest-Shamir-Adleman) algorithms as well
1979as DSA (Digital Signature Algorithm) and ElGamal. The versatile 1979as DSA (Digital Signature Algorithm) and ElGamal. The versatile
1980interface allows to add more algorithms in the future. 1980interface allows to add more algorithms in the future.
1981 1981
1982@node Used S-expressions 1982@node Used S-expressions
1983@section Used S-expressions 1983@section Used S-expressions
1984 1984
1985@acronym{Libgcrypt}'s API for asymmetric cryptography is based on data 1985@acronym{Libgcrypt}'s API for asymmetric cryptography is based on data
1986structures called S-expressions (see XXXX) and does not work with 1986structures called S-expressions (see XXXX) and does not work with
1987contexts as most of the other building blocks of @acronym{Libgcrypt} 1987contexts as most of the other building blocks of @acronym{Libgcrypt}
1988do. 1988do.
1989 1989
1990The following information are stored in S-expressions: 1990The following information are stored in S-expressions:
1991 1991
1992@table @asis 1992@table @asis
1993@item keys 1993@item keys
1994 1994
1995@item plain text data 1995@item plain text data
1996 1996
1997@item encrypted data 1997@item encrypted data
1998 1998
1999@item signatures 1999@item signatures
2000 2000
2001@end table 2001@end table
2002 2002
2003@noindent 2003@noindent
2004To describe how @acronym{Libgcrypt} expect keys, we use some examples. Note that 2004To describe how @acronym{Libgcrypt} expect keys, we use some examples. Note that
2005words in 2005words in
2006@ifnottex 2006@ifnottex
2007uppercase 2007uppercase
2008@end ifnottex 2008@end ifnottex
2009@iftex 2009@iftex
2010italics 2010italics
2011@end iftex 2011@end iftex
2012indicate parameters whereas lowercase words are literals. 2012indicate parameters whereas lowercase words are literals.
2013 2013
2014@example 2014@example
2015(private-key 2015(private-key
2016 (dsa 2016 (dsa
2017 (p @var{p-mpi}) 2017 (p @var{p-mpi})
2018 (q @var{q-mpi}) 2018 (q @var{q-mpi})
2019 (g @var{g-mpi}) 2019 (g @var{g-mpi})
2020 (y @var{y-mpi}) 2020 (y @var{y-mpi})
2021 (x @var{x-mpi}))) 2021 (x @var{x-mpi})))
2022@end example 2022@end example
2023 2023
2024@noindent 2024@noindent
2025This specifies a DSA private key with the following parameters: 2025This specifies a DSA private key with the following parameters:
2026 2026
2027@table @var 2027@table @var
2028@item p-mpi 2028@item p-mpi
2029DSA prime @math{p}. 2029DSA prime @math{p}.
2030@item q-mpi 2030@item q-mpi
2031DSA group order @math{q} (which is a prime divisor of @math{p-1}). 2031DSA group order @math{q} (which is a prime divisor of @math{p-1}).
2032@item g-mpi 2032@item g-mpi
2033DSA group generator @math{g}. 2033DSA group generator @math{g}.
2034@item y-mpi 2034@item y-mpi
2035DSA public key value @math{y = g^x \bmod p}. 2035DSA public key value @math{y = g^x \bmod p}.
2036@item x-mpi 2036@item x-mpi
2037DSA secret exponent x. 2037DSA secret exponent x.
2038@end table 2038@end table
2039 2039
2040All the MPI values are expected to be in @code{GCRYMPI_FMT_USG} format. 2040All the MPI values are expected to be in @code{GCRYMPI_FMT_USG} format.
2041The public key is similar with "private-key" replaced by "public-key" 2041The public key is similar with "private-key" replaced by "public-key"
2042and no @var{x-mpi}. 2042and no @var{x-mpi}.
2043 2043
2044An easy way to create such an S-expressions is by using 2044An easy way to create such an S-expressions is by using
2045@code{gcry_sexp_build} which allows to pass a string with printf-like 2045@code{gcry_sexp_build} which allows to pass a string with printf-like
2046escapes to insert MPI values. 2046escapes to insert MPI values.
2047 2047
2048@noindent 2048@noindent
2049Here is an example for an RSA key: 2049Here is an example for an RSA key:
2050 2050
2051@example 2051@example
2052(private-key 2052(private-key
2053 (rsa 2053 (rsa
2054 (n @var{n-mpi}) 2054 (n @var{n-mpi})
2055 (e @var{e-mpi}) 2055 (e @var{e-mpi})
2056 (d @var{d-mpi}) 2056 (d @var{d-mpi})
2057 (p @var{p-mpi}) 2057 (p @var{p-mpi})
2058 (q @var{q-mpi}) 2058 (q @var{q-mpi})
2059 (u @var{u-mpi}) 2059 (u @var{u-mpi})
2060@end example 2060@end example
2061 2061
2062@noindent 2062@noindent
2063with 2063with
2064 2064
2065@table @var 2065@table @var
2066@item n-mpi 2066@item n-mpi
2067RSA public modulus @math{n}. 2067RSA public modulus @math{n}.
2068@item e-mpi 2068@item e-mpi
2069RSA public exponent @math{e}. 2069RSA public exponent @math{e}.
2070@item d-mpi 2070@item d-mpi
2071RSA secret exponent @math{d = e^{-1} \bmod (p-1)(q-1)}. 2071RSA secret exponent @math{d = e^{-1} \bmod (p-1)(q-1)}.
2072@item p-mpi 2072@item p-mpi
2073RSA secret prime @math{p}. 2073RSA secret prime @math{p}.
2074@item q-mpi 2074@item q-mpi
2075RSA secret prime @math{q} with @math{q > p}. 2075RSA secret prime @math{q} with @math{q > p}.
2076@item u-mpi 2076@item u-mpi
2077multiplicative inverse @math{u = p^{-1} \bmod q}. 2077multiplicative inverse @math{u = p^{-1} \bmod q}.
2078@end table 2078@end table
2079 2079
2080@node Public key modules 2080@node Public key modules
2081@section Public key modules 2081@section Public key modules
2082 2082
2083@acronym{Libgcrypt} makes it possible to load additional `public key 2083@acronym{Libgcrypt} makes it possible to load additional `public key
2084modules'; these public key algorithms can be used just like the 2084modules'; these public key algorithms can be used just like the
2085algorithms that are built into the library directly. For an 2085algorithms that are built into the library directly. For an
2086introduction into extension modules, see @xref{Modules}. 2086introduction into extension modules, see @xref{Modules}.
2087 2087
2088@deftp {Data type} gcry_pk_spec_t 2088@deftp {Data type} gcry_pk_spec_t
2089This is the `module specification structure' needed for registering 2089This is the `module specification structure' needed for registering
2090public key modules, which has to be filled in by the user before it 2090public key modules, which has to be filled in by the user before it
2091can be used to register a module. It contains the following members: 2091can be used to register a module. It contains the following members:
2092 2092
2093@table @code 2093@table @code
2094@item const char *name 2094@item const char *name
2095The primary name of this algorithm. 2095The primary name of this algorithm.
2096@item char **aliases 2096@item char **aliases
2097A list of strings that are `aliases' for the algorithm. The list 2097A list of strings that are `aliases' for the algorithm. The list
2098must be terminated with a NULL element. 2098must be terminated with a NULL element.
2099@item const char *elements_pkey 2099@item const char *elements_pkey
2100String containing the one-letter names of the MPI values contained in 2100String containing the one-letter names of the MPI values contained in
2101a public key. 2101a public key.
2102@item const char *element_skey 2102@item const char *element_skey
2103String containing the one-letter names of the MPI values contained in 2103String containing the one-letter names of the MPI values contained in
2104a secret key. 2104a secret key.
2105@item const char *elements_enc 2105@item const char *elements_enc
2106String containing the one-letter names of the MPI values that are the 2106String containing the one-letter names of the MPI values that are the
2107result of an encryption operation using this algorithm. 2107result of an encryption operation using this algorithm.
2108@item const char *elements_sig 2108@item const char *elements_sig
2109String containing the one-letter names of the MPI values that are the 2109String containing the one-letter names of the MPI values that are the
2110result of a sign operation using this algorithm. 2110result of a sign operation using this algorithm.
2111@item const char *elements_grip 2111@item const char *elements_grip
2112String containing the one-letter names of the MPI values that are to 2112String containing the one-letter names of the MPI values that are to
2113be included in the `key grip'. 2113be included in the `key grip'.
2114@item int use 2114@item int use
2115The bitwise-OR of the following flags, depending on the abilities of 2115The bitwise-OR of the following flags, depending on the abilities of
2116the algorithm: 2116the algorithm:
2117@table @code 2117@table @code
2118@item GCRY_PK_USAGE_SIGN 2118@item GCRY_PK_USAGE_SIGN
2119The algorithm supports signing and verifying of data. 2119The algorithm supports signing and verifying of data.
2120@item GCRY_PK_USAGE_ENCR 2120@item GCRY_PK_USAGE_ENCR
2121The algorithm supports the encryption and decryption of data. 2121The algorithm supports the encryption and decryption of data.
2122@end table 2122@end table
2123@item gcry_pk_generate_t generate 2123@item gcry_pk_generate_t generate
2124The function responsible for generating a new key pair. See below for 2124The function responsible for generating a new key pair. See below for
2125a description of this type. 2125a description of this type.
2126@item gcry_pk_check_secret_key_t check_secret_key 2126@item gcry_pk_check_secret_key_t check_secret_key
2127The function responsible for checking the sanity of a provided secret 2127The function responsible for checking the sanity of a provided secret
2128key. See below for a description of this type. 2128key. See below for a description of this type.
2129@item gcry_pk_encrypt_t encrypt 2129@item gcry_pk_encrypt_t encrypt
2130The function responsible for encrypting data. See below for a 2130The function responsible for encrypting data. See below for a
2131description of this type. 2131description of this type.
2132@item gcry_pk_decrypt_t decrypt 2132@item gcry_pk_decrypt_t decrypt
2133The function responsible for decrypting data. See below for a 2133The function responsible for decrypting data. See below for a
2134description of this type. 2134description of this type.
2135@item gcry_pk_sign_t sign 2135@item gcry_pk_sign_t sign
2136The function responsible for signing data. See below for a description 2136The function responsible for signing data. See below for a description
2137of this type. 2137of this type.
2138@item gcry_pk_verify_t verify 2138@item gcry_pk_verify_t verify
2139The function responsible for verifying that the provided signature 2139The function responsible for verifying that the provided signature
2140matches the provided data. See below for a description of this type. 2140matches the provided data. See below for a description of this type.
2141@item gcry_pk_get_nbits_t get_nbits 2141@item gcry_pk_get_nbits_t get_nbits
2142The function responsible for returning the number of bits of a provided 2142The function responsible for returning the number of bits of a provided
2143key. See below for a description of this type. 2143key. See below for a description of this type.
2144@end table 2144@end table
2145@end deftp 2145@end deftp
2146 2146
2147@deftp {Data type} gcry_pk_generate_t 2147@deftp {Data type} gcry_pk_generate_t
2148Type for the `generate' function, defined as: gcry_err_code_t 2148Type for the `generate' function, defined as: gcry_err_code_t
2149(*gcry_pk_generate_t) (int algo, unsigned int nbits, unsigned long 2149(*gcry_pk_generate_t) (int algo, unsigned int nbits, unsigned long
2150use_e, gcry_mpi_t *skey, gcry_mpi_t **retfactors) 2150use_e, gcry_mpi_t *skey, gcry_mpi_t **retfactors)
2151@end deftp 2151@end deftp
2152 2152
2153@deftp {Data type} gcry_pk_check_secret_key_t 2153@deftp {Data type} gcry_pk_check_secret_key_t
2154Type for the `check_secret_key' function, defined as: gcry_err_code_t 2154Type for the `check_secret_key' function, defined as: gcry_err_code_t
2155(*gcry_pk_check_secret_key_t) (int algo, gcry_mpi_t *skey) 2155(*gcry_pk_check_secret_key_t) (int algo, gcry_mpi_t *skey)
2156@end deftp 2156@end deftp
2157 2157
2158@deftp {Data type} gcry_pk_encrypt_t 2158@deftp {Data type} gcry_pk_encrypt_t
2159Type for the `encrypt' function, defined as: gcry_err_code_t 2159Type for the `encrypt' function, defined as: gcry_err_code_t
2160(*gcry_pk_encrypt_t) (int algo, gcry_mpi_t *resarr, gcry_mpi_t data, 2160(*gcry_pk_encrypt_t) (int algo, gcry_mpi_t *resarr, gcry_mpi_t data,
2161gcry_mpi_t *pkey, int flags) 2161gcry_mpi_t *pkey, int flags)
2162@end deftp 2162@end deftp
2163 2163
2164@deftp {Data type} gcry_pk_decrypt_t 2164@deftp {Data type} gcry_pk_decrypt_t
2165Type for the `decrypt' function, defined as: gcry_err_code_t 2165Type for the `decrypt' function, defined as: gcry_err_code_t
2166(*gcry_pk_decrypt_t) (int algo, gcry_mpi_t *result, gcry_mpi_t *data, 2166(*gcry_pk_decrypt_t) (int algo, gcry_mpi_t *result, gcry_mpi_t *data,
2167gcry_mpi_t *skey, int flags) 2167gcry_mpi_t *skey, int flags)
2168@end deftp 2168@end deftp
2169 2169
2170@deftp {Data type} gcry_pk_sign_t 2170@deftp {Data type} gcry_pk_sign_t
2171Type for the `sign' function, defined as: gcry_err_code_t 2171Type for the `sign' function, defined as: gcry_err_code_t
2172(*gcry_pk_sign_t) (int algo, gcry_mpi_t *resarr, gcry_mpi_t data, 2172(*gcry_pk_sign_t) (int algo, gcry_mpi_t *resarr, gcry_mpi_t data,
2173gcry_mpi_t *skey) 2173gcry_mpi_t *skey)
2174@end deftp 2174@end deftp
2175 2175
2176@deftp {Data type} gcry_pk_verify_t 2176@deftp {Data type} gcry_pk_verify_t
2177Type for the `verify' function, defined as: gcry_err_code_t 2177Type for the `verify' function, defined as: gcry_err_code_t
2178(*gcry_pk_verify_t) (int algo, gcry_mpi_t hash, gcry_mpi_t *data, 2178(*gcry_pk_verify_t) (int algo, gcry_mpi_t hash, gcry_mpi_t *data,
2179gcry_mpi_t *pkey, int (*cmp) (void *, gcry_mpi_t), void *opaquev) 2179gcry_mpi_t *pkey, int (*cmp) (void *, gcry_mpi_t), void *opaquev)
2180@end deftp 2180@end deftp
2181 2181
2182@deftp {Data type} gcry_pk_get_nbits_t 2182@deftp {Data type} gcry_pk_get_nbits_t
2183Type for the `get_nbits' function, defined as: unsigned 2183Type for the `get_nbits' function, defined as: unsigned
2184(*gcry_pk_get_nbits_t) (int algo, gcry_mpi_t *pkey) 2184(*gcry_pk_get_nbits_t) (int algo, gcry_mpi_t *pkey)
2185@end deftp 2185@end deftp
2186 2186
2187@deftypefun gcry_error_t gcry_pk_register (gcry_pk_spec_t *@var{pubkey}, unsigned int *algorithm_id, gcry_module_t *@var{module}) 2187@deftypefun gcry_error_t gcry_pk_register (gcry_pk_spec_t *@var{pubkey}, unsigned int *algorithm_id, gcry_module_t *@var{module})
2188 2188
2189Register a new public key module whose specification can be found in 2189Register a new public key module whose specification can be found in
2190@var{pubkey}. On success, a new algorithm ID is stored in 2190@var{pubkey}. On success, a new algorithm ID is stored in
2191@var{algorithm_id} and a pointer representing this module is stored 2191@var{algorithm_id} and a pointer representing this module is stored
2192in @var{module}. 2192in @var{module}.
2193@end deftypefun 2193@end deftypefun
2194 2194
2195@deftypefun void gcry_pk_unregister (gcry_module_t @var{module}) 2195@deftypefun void gcry_pk_unregister (gcry_module_t @var{module})
2196Unregister the public key module identified by @var{module}, which 2196Unregister the public key module identified by @var{module}, which
2197must have been registered with gcry_pk_register. 2197must have been registered with gcry_pk_register.
2198@end deftypefun 2198@end deftypefun
2199 2199
2200@deftypefun gcry_error_t gcry_pk_list (int *@var{list}, int *@var{list_length}) 2200@deftypefun gcry_error_t gcry_pk_list (int *@var{list}, int *@var{list_length})
2201Get a list consisting of the IDs of the loaded pubkey modules. If 2201Get a list consisting of the IDs of the loaded pubkey modules. If
2202@var{list} is zero, write the number of loaded pubkey modules to 2202@var{list} is zero, write the number of loaded pubkey modules to
2203@var{list_length} and return. If @var{list} is non-zero, the first 2203@var{list_length} and return. If @var{list} is non-zero, the first
2204*@var{list_length} algorithm IDs are stored in @var{list}, which must 2204*@var{list_length} algorithm IDs are stored in @var{list}, which must
2205be of according size. In case there are less pubkey modules than 2205be of according size. In case there are less pubkey modules than
2206*@var{list_length}, *@var{list_length} is updated to the correct 2206*@var{list_length}, *@var{list_length} is updated to the correct
2207number. 2207number.
2208@end deftypefun 2208@end deftypefun
2209 2209
2210@node Cryptographic Functions 2210@node Cryptographic Functions
2211@section Cryptographic Functions 2211@section Cryptographic Functions
2212 2212
2213@noindent 2213@noindent
2214Note, that we will in future allow to use keys without p,q and u 2214Note, that we will in future allow to use keys without p,q and u
2215specified and may also support other parameters for performance 2215specified and may also support other parameters for performance
2216reasons. 2216reasons.
2217 2217
2218@noindent 2218@noindent
2219 2219
2220Some functions operating on S-expressions support `flags', that 2220Some functions operating on S-expressions support `flags', that
2221influence the operation. These flags have to be listed in a 2221influence the operation. These flags have to be listed in a
2222sub-S-expression named `flags'; the following flags are known: 2222sub-S-expression named `flags'; the following flags are known:
2223 2223
2224@table @var 2224@table @var
2225@item pkcs1 2225@item pkcs1
2226Use PKCS#1 block type 2 padding. 2226Use PKCS#1 block type 2 padding.
2227@item no-blinding 2227@item no-blinding
2228Do not use a technique called `blinding', which is used by default in 2228Do not use a technique called `blinding', which is used by default in
2229order to prevent leaking of secret information. Blinding is only 2229order to prevent leaking of secret information. Blinding is only
2230implemented by RSA, but it might be implemented by other algorithms in 2230implemented by RSA, but it might be implemented by other algorithms in
2231the future as well, when necessary. 2231the future as well, when necessary.
2232@end table 2232@end table
2233 2233
2234@noindent 2234@noindent
2235Now that we know the key basics, we can carry on and explain how to 2235Now that we know the key basics, we can carry on and explain how to
2236encrypt and decrypt data. In almost all cases the data is a random 2236encrypt and decrypt data. In almost all cases the data is a random
2237session key which is in turn used for the actual encryption of the real 2237session key which is in turn used for the actual encryption of the real
2238data. There are 2 functions to do this: 2238data. There are 2 functions to do this:
2239 2239
2240@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}}) 2240@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}})
2241 2241
2242Obviously a public key must be provided for encryption. It is 2242Obviously a public key must be provided for encryption. It is
2243expected as an appropriate S-expression (see above) in @var{pkey}. 2243expected as an appropriate S-expression (see above) in @var{pkey}.
2244The data to be encrypted can either be in the simple old format, which 2244The data to be encrypted can either be in the simple old format, which
2245is a very simple S-expression consisting only of one MPI, or it may be 2245is a very simple S-expression consisting only of one MPI, or it may be
2246a more complex S-expression which also allows to specify flags for 2246a more complex S-expression which also allows to specify flags for
2247operation, like e.g. padding rules. 2247operation, like e.g. padding rules.
2248 2248
2249@noindent 2249@noindent
2250If you don't want to let @acronym{Libgcrypt} handle the padding, you must pass an 2250If you don't want to let @acronym{Libgcrypt} handle the padding, you must pass an
2251appropriate MPI using this expression for @var{data}: 2251appropriate MPI using this expression for @var{data}:
2252 2252
2253@example 2253@example
2254(data 2254(data
2255 (flags raw) 2255 (flags raw)
2256 (value @var{mpi})) 2256 (value @var{mpi}))
2257@end example 2257@end example
2258 2258
2259@noindent 2259@noindent
2260This has the same semantics as the old style MPI only way. @var{MPI} is 2260This has the same semantics as the old style MPI only way. @var{MPI} is
2261the actual data, already padded appropriate for your protocol. Most 2261the actual data, already padded appropriate for your protocol. Most
2262systems however use PKCS#1 padding and so you can use this S-expression 2262systems however use PKCS#1 padding and so you can use this S-expression
2263for @var{data}: 2263for @var{data}:
2264 2264
2265@example 2265@example
2266(data 2266(data
2267 (flags pkcs1) 2267 (flags pkcs1)
2268 (value @var{block})) 2268 (value @var{block}))
2269@end example 2269@end example
2270 2270
2271@noindent 2271@noindent
2272Here, the "flags" list has the "pkcs1" flag which let the function know 2272Here, the "flags" list has the "pkcs1" flag which let the function know
2273that it should provide PKCS#1 block type 2 padding. The actual data to 2273that it should provide PKCS#1 block type 2 padding. The actual data to
2274be encrypted is passed as a string of octets in @var{block}. The 2274be encrypted is passed as a string of octets in @var{block}. The
2275function checks that this data actually can be used with the given key, 2275function checks that this data actually can be used with the given key,
2276does the padding and encrypts it. 2276does the padding and encrypts it.
2277 2277
2278If the function could successfully perform the encryption, the return 2278If the function could successfully perform the encryption, the return
2279value will be 0 and a a new S-expression with the encrypted result is 2279value will be 0 and a a new S-expression with the encrypted result is
2280allocated and assign to the variable at the address of @var{r_ciph}. 2280allocated and assign to the variable at the address of @var{r_ciph}.
2281The caller is responsible to release this value using 2281The caller is responsible to release this value using
2282@code{gcry_sexp_release}. In case of an error, an error code is 2282@code{gcry_sexp_release}. In case of an error, an error code is
2283returned and @var{r_ciph} will be set to @code{NULL}. 2283returned and @var{r_ciph} will be set to @code{NULL}.
2284 2284
2285@noindent 2285@noindent
2286The returned S-expression has this format when used with RSA: 2286The returned S-expression has this format when used with RSA:
2287 2287
2288@example 2288@example
2289(enc-val 2289(enc-val
2290 (rsa 2290 (rsa
2291 (a @var{a-mpi}))) 2291 (a @var{a-mpi})))
2292@end example 2292@end example
2293 2293
2294@noindent 2294@noindent
2295Where @var{a-mpi} is an MPI with the result of the RSA operation. When 2295Where @var{a-mpi} is an MPI with the result of the RSA operation. When
2296using the ElGamal algorithm, the return value will have this format: 2296using the ElGamal algorithm, the return value will have this format:
2297 2297
2298@example 2298@example
2299(enc-val 2299(enc-val
2300 (elg 2300 (elg
2301 (a @var{a-mpi}) 2301 (a @var{a-mpi})
2302 (b @var{b-mpi}))) 2302 (b @var{b-mpi})))
2303@end example 2303@end example
2304 2304
2305@noindent 2305@noindent
2306Where @var{a-mpi} and @var{b-mpi} are MPIs with the result of the 2306Where @var{a-mpi} and @var{b-mpi} are MPIs with the result of the
2307ElGamal encryption operation. 2307ElGamal encryption operation.
2308@end deftypefun 2308@end deftypefun
2309@c end gcry_pk_encrypt 2309@c end gcry_pk_encrypt
2310 2310
2311@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}}) 2311@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}})
2312 2312
2313Obviously a private key must be provided for decryption. It is expected 2313Obviously a private key must be provided for decryption. It is expected
2314as an appropriate S-expression (see above) in @var{skey}. The data to 2314as an appropriate S-expression (see above) in @var{skey}. The data to
2315be decrypted must match the format of the result as returned by 2315be decrypted must match the format of the result as returned by
2316@code{gcry_pk_encrypt}, but should be enlarged with a @code{flags} 2316@code{gcry_pk_encrypt}, but should be enlarged with a @code{flags}
2317element: 2317element:
2318 2318
2319@example 2319@example
2320(enc-val 2320(enc-val
2321 (flags) 2321 (flags)
2322 (elg 2322 (elg
2323 (a @var{a-mpi}) 2323 (a @var{a-mpi})
2324 (b @var{b-mpi}))) 2324 (b @var{b-mpi})))
2325@end example 2325@end example
2326 2326
2327@noindent 2327@noindent
2328Note, that this function currently does not know of any padding 2328Note, that this function currently does not know of any padding
2329methods and the caller must do any un-padding on his own. 2329methods and the caller must do any un-padding on his own.
2330 2330
2331@noindent 2331@noindent
2332The function returns 0 on success or an error code. The variable at the 2332The function returns 0 on success or an error code. The variable at the
2333address of @var{r_plain} will be set to NULL on error or receive the 2333address of @var{r_plain} will be set to NULL on error or receive the
2334decrypted value on success. The format of @var{r_plain} is a 2334decrypted value on success. The format of @var{r_plain} is a
2335simple S-expression part (i.e. not a valid one) with just one MPI if 2335simple S-expression part (i.e. not a valid one) with just one MPI if
2336there was no @code{flags} element in @var{data}; if at least an empty 2336there was no @code{flags} element in @var{data}; if at least an empty
2337@code{flags} is passed in @var{data}, the format is: 2337@code{flags} is passed in @var{data}, the format is:
2338 2338
2339@example 2339@example
2340(value @var{plaintext}) 2340(value @var{plaintext})
2341@end example 2341@end example
2342@end deftypefun 2342@end deftypefun
2343@c end gcry_pk_decrypt 2343@c end gcry_pk_decrypt
2344 2344
2345 2345
2346Another operation commonly performed using public key cryptography is 2346Another operation commonly performed using public key cryptography is
2347signing data. In some sense this is even more important than 2347signing data. In some sense this is even more important than
2348encryption because digital signatures are an important instrument for 2348encryption because digital signatures are an important instrument for
2349key management. @acronym{Libgcrypt} supports digital signatures using 2349key management. @acronym{Libgcrypt} supports digital signatures using
23502 functions, similar to the encryption functions: 23502 functions, similar to the encryption functions:
2351 2351
2352@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}}) 2352@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}})
2353 2353
2354This function creates a digital signature for @var{data} using the 2354This function creates a digital signature for @var{data} using the
2355private key @var{skey} and place it into the variable at the address of 2355private key @var{skey} and place it into the variable at the address of
2356@var{r_sig}. @var{data} may either be the simple old style S-expression 2356@var{r_sig}. @var{data} may either be the simple old style S-expression
2357with just one MPI or a modern and more versatile S-expression which 2357with just one MPI or a modern and more versatile S-expression which
2358allows to let @acronym{Libgcrypt} handle padding: 2358allows to let @acronym{Libgcrypt} handle padding:
2359 2359
2360@example 2360@example
2361 (data 2361 (data
2362 (flags pkcs1) 2362 (flags pkcs1)
2363 (hash @var{hash-algo} @var{block})) 2363 (hash @var{hash-algo} @var{block}))
2364@end example 2364@end example
2365 2365
2366@noindent 2366@noindent
2367This example requests to sign the data in @var{block} after applying 2367This example requests to sign the data in @var{block} after applying
2368PKCS#1 block type 1 style padding. @var{hash-algo} is a string with the 2368PKCS#1 block type 1 style padding. @var{hash-algo} is a string with the
2369hash algorithm to be encoded into the signature, this may be any hash 2369hash algorithm to be encoded into the signature, this may be any hash
2370algorithm name as supported by @acronym{Libgcrypt}. Most likely, this will be 2370algorithm name as supported by @acronym{Libgcrypt}. Most likely, this will be
2371"sha1", "rmd160" or "md5". It is obvious that the length of @var{block} 2371"sha1", "rmd160" or "md5". It is obvious that the length of @var{block}
2372must match the size of that message digests; the function checks that 2372must match the size of that message digests; the function checks that
2373this and other constraints are valid. 2373this and other constraints are valid.
2374 2374
2375@noindent 2375@noindent
2376If PKCS#1 padding is not required (because the caller does already 2376If PKCS#1 padding is not required (because the caller does already
2377provide a padded value), either the old format or better the following 2377provide a padded value), either the old format or better the following
2378format should be used: 2378format should be used:
2379 2379
2380@example 2380@example
2381(data 2381(data
2382 (flags raw) 2382 (flags raw)
2383 (value @var{mpi})) 2383 (value @var{mpi}))
2384@end example 2384@end example
2385 2385
2386@noindent 2386@noindent
2387Here, the data to be signed is directly given as an @var{MPI}. 2387Here, the data to be signed is directly given as an @var{MPI}.
2388 2388
2389@noindent 2389@noindent
2390The signature is returned as a newly allocated S-expression in 2390The signature is returned as a newly allocated S-expression in
2391@var{r_sig} using this format for RSA: 2391@var{r_sig} using this format for RSA:
2392 2392
2393@example 2393@example
2394(sig-val 2394(sig-val
2395 (rsa 2395 (rsa
2396 (s @var{s-mpi}))) 2396 (s @var{s-mpi})))
2397@end example 2397@end example
2398 2398
2399Where @var{s-mpi} is the result of the RSA sign operation. For DSA the 2399Where @var{s-mpi} is the result of the RSA sign operation. For DSA the
2400S-expression returned is: 2400S-expression returned is:
2401 2401
2402@example 2402@example
2403(sig-val 2403(sig-val
2404 (dsa 2404 (dsa
2405 (r @var{r-mpi}) 2405 (r @var{r-mpi})
2406 (s @var{s-mpi}))) 2406 (s @var{s-mpi})))
2407@end example 2407@end example
2408 2408
2409Where @var{r-mpi} and @var{s-mpi} are the result of the DSA sign 2409Where @var{r-mpi} and @var{s-mpi} are the result of the DSA sign
2410operation. For ElGamal signing (which is slow, yields large numbers 2410operation. For ElGamal signing (which is slow, yields large numbers
2411and probably is not as secure as the other algorithms), the same format is 2411and probably is not as secure as the other algorithms), the same format is
2412used with "elg" replacing "dsa". 2412used with "elg" replacing "dsa".
2413@end deftypefun 2413@end deftypefun
2414@c end gcry_pk_sign 2414@c end gcry_pk_sign
2415 2415
2416@noindent 2416@noindent
2417The operation most commonly used is definitely the verification of a 2417The operation most commonly used is definitely the verification of a
2418signature. @acronym{Libgcrypt} provides this function: 2418signature. @acronym{Libgcrypt} provides this function:
2419 2419
2420@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}}) 2420@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}})
2421 2421
2422This is used to check whether the signature @var{sig} matches the 2422This is used to check whether the signature @var{sig} matches the
2423@var{data}. The public key @var{pkey} must be provided to perform this 2423@var{data}. The public key @var{pkey} must be provided to perform this
2424verification. This function is similar in its parameters to 2424verification. This function is similar in its parameters to
2425@code{gcry_pk_sign} with the exceptions that the public key is used 2425@code{gcry_pk_sign} with the exceptions that the public key is used
2426instead of the private key and that no signature is created but a 2426instead of the private key and that no signature is created but a
2427signature, in a format as created by @code{gcry_pk_sign}, is passed to 2427signature, in a format as created by @code{gcry_pk_sign}, is passed to
2428the function in @var{sig}. 2428the function in @var{sig}.
2429 2429
2430@noindent 2430@noindent
2431The result is 0 for success (i.e. the data matches the signature), or an 2431The result is 0 for success (i.e. the data matches the signature), or an
2432error code where the most relevant code is @code{GCRYERR_BAD_SIGNATURE} 2432error code where the most relevant code is @code{GCRYERR_BAD_SIGNATURE}
2433to indicate that the signature does not match the provided data. 2433to indicate that the signature does not match the provided data.
2434 2434
2435@end deftypefun 2435@end deftypefun
2436@c end gcry_pk_verify 2436@c end gcry_pk_verify
2437 2437
2438@node General public-key related Functions 2438@node General public-key related Functions
2439@section General public-key related Functions 2439@section General public-key related Functions
2440 2440
2441@noindent 2441@noindent
2442A couple of utility functions are available to retrieve the length of 2442A couple of utility functions are available to retrieve the length of
2443the key, map algorithm identifiers and perform sanity checks: 2443the key, map algorithm identifiers and perform sanity checks:
2444 2444
2445@deftypefun {const char *} gcry_pk_algo_name (int @var{algo}) 2445@deftypefun {const char *} gcry_pk_algo_name (int @var{algo})
2446 2446
2447Map the public key algorithm id @var{algo} to a string representation of 2447Map the public key algorithm id @var{algo} to a string representation of
2448the algorithm name. For unknown algorithms this functions returns an 2448the algorithm name. For unknown algorithms this functions returns an
2449empty string. 2449empty string.
2450@end deftypefun 2450@end deftypefun
2451 2451
2452@deftypefun int gcry_pk_map_name (const char *@var{name}) 2452@deftypefun int gcry_pk_map_name (const char *@var{name})
2453 2453
2454Map the algorithm @var{name} to a public key algorithm Id. Returns 0 if 2454Map the algorithm @var{name} to a public key algorithm Id. Returns 0 if
2455the algorithm name is not known. 2455the algorithm name is not known.
2456@end deftypefun 2456@end deftypefun
2457 2457
2458@deftypefun int gcry_pk_test_algo (int @var{algo}) 2458@deftypefun int gcry_pk_test_algo (int @var{algo})
2459 2459
2460Return 0 if the public key algorithm @var{algo} is available for use. 2460Return 0 if the public key algorithm @var{algo} is available for use.
2461Note, that this is implemented as a macro. 2461Note, that this is implemented as a macro.
2462@end deftypefun 2462@end deftypefun
2463 2463
2464 2464
2465@deftypefun {unsigned int} gcry_pk_get_nbits (gcry_sexp_t @var{key}) 2465@deftypefun {unsigned int} gcry_pk_get_nbits (gcry_sexp_t @var{key})
2466 2466
2467Return what is commonly referred as the key length for the given 2467Return what is commonly referred as the key length for the given
2468public or private in @var{key}. 2468public or private in @var{key}.
2469@end deftypefun 2469@end deftypefun
2470 2470
2471@deftypefun {unsigned char *} gcry_pk_get_keygrip (@w{gcry_sexp_t @var{key}}, @w{unsigned char *@var{array}}) 2471@deftypefun {unsigned char *} gcry_pk_get_keygrip (@w{gcry_sexp_t @var{key}}, @w{unsigned char *@var{array}})
2472 2472
2473Return the so called "keygrip" which is the SHA-1 hash of the public key 2473Return the so called "keygrip" which is the SHA-1 hash of the public key
2474parameters expressed in a way depended on the algorithm. @var{array} 2474parameters expressed in a way depended on the algorithm. @var{array}
2475must either provide space for 20 bytes or @code{NULL;}. In the latter 2475must either provide space for 20 bytes or @code{NULL;}. In the latter
2476case a newly allocated array of that size is returned. On success a 2476case a newly allocated array of that size is returned. On success a
2477pointer to the newly allocated space or to @var{array} is returned. 2477pointer to the newly allocated space or to @var{array} is returned.
2478@code{NULL} is returned to indicate an error which is most likely an unknown 2478@code{NULL} is returned to indicate an error which is most likely an unknown
2479algorithm or one where a "keygrip" has not yet been defined. 2479algorithm or one where a "keygrip" has not yet been defined.
2480The function accepts public or secret keys in @var{key}. 2480The function accepts public or secret keys in @var{key}.
2481@end deftypefun 2481@end deftypefun
2482 2482
2483@deftypefun gcry_error_t gcry_pk_testkey (gcry_sexp_t @var{key}) 2483@deftypefun gcry_error_t gcry_pk_testkey (gcry_sexp_t @var{key})
2484 2484
2485Return zero if the private key @var{key} is `sane', an error code otherwise. 2485Return zero if the private key @var{key} is `sane', an error code otherwise.
2486Note, that it is not possible to chek the `saneness' of a public key. 2486Note, that it is not possible to chek the `saneness' of a public key.
2487 2487
2488@end deftypefun 2488@end deftypefun
2489 2489
2490 2490
2491@deftypefun int gcry_pk_algo_info (@w{int @var{algo}}, @w{int @var{what}}, @w{void *@var{buffer}}, @w{size_t *@var{nbytes}}) 2491@deftypefun int gcry_pk_algo_info (@w{int @var{algo}}, @w{int @var{what}}, @w{void *@var{buffer}}, @w{size_t *@var{nbytes}})
2492 2492
2493Depending on the value of @var{what} return various information about 2493Depending on the value of @var{what} return various information about
2494the public key algorithm with the id @var{algo}. Note, that the 2494the public key algorithm with the id @var{algo}. Note, that the
2495function returns @code{-1} on error and the actual error code must be 2495function returns @code{-1} on error and the actual error code must be
2496retrieved using the function @code{gcry_errno}. The currently defined 2496retrieved using the function @code{gcry_errno}. The currently defined
2497values for @var{what} are: 2497values for @var{what} are:
2498 2498
2499@table @code 2499@table @code
2500@item GCRYCTL_TEST_ALGO: 2500@item GCRYCTL_TEST_ALGO:
2501Return 0 when the specified algorithm is available for use. 2501Return 0 when the specified algorithm is available for use.
2502@var{buffer} must be @code{NULL}, @var{nbytes} may be passed as 2502@var{buffer} must be @code{NULL}, @var{nbytes} may be passed as
2503@code{NULL} or point to a variable with the required usage of the 2503@code{NULL} or point to a variable with the required usage of the
2504algorithm. This may be 0 for "don't care" or the bit-wise OR of these 2504algorithm. This may be 0 for "don't care" or the bit-wise OR of these
2505flags: 2505flags:
2506 2506
2507@table @code 2507@table @code
2508@item GCRY_PK_USAGE_SIGN 2508@item GCRY_PK_USAGE_SIGN
2509Algorithm is usable for signing. 2509Algorithm is usable for signing.
2510@item GCRY_PK_USAGE_ENCR 2510@item GCRY_PK_USAGE_ENCR
2511Algorithm is usable for encryption. 2511Algorithm is usable for encryption.
2512@end table 2512@end table
2513 2513
2514@item GCRYCTL_GET_ALGO_USAGE: 2514@item GCRYCTL_GET_ALGO_USAGE:
2515Return the usage flags for the given algorithm. An invalid algorithm 2515Return the usage flags for the given algorithm. An invalid algorithm
2516return 0. Disabled algorithms are ignored here because we 2516return 0. Disabled algorithms are ignored here because we
2517want to know whether the algorithm is at all capable of a certain usage. 2517want to know whether the algorithm is at all capable of a certain usage.
2518 2518
2519@item GCRYCTL_GET_ALGO_NPKEY 2519@item GCRYCTL_GET_ALGO_NPKEY
2520Return the number of elements the public key for algorithm @var{algo} 2520Return the number of elements the public key for algorithm @var{algo}
2521consist of. Return 0 for an unknown algorithm. 2521consist of. Return 0 for an unknown algorithm.
2522 2522
2523@item GCRYCTL_GET_ALGO_NSKEY 2523@item GCRYCTL_GET_ALGO_NSKEY
2524Return the number of elements the private key for algorithm @var{algo} 2524Return the number of elements the private key for algorithm @var{algo}
2525consist of. Note that this value is always larger than that of the 2525consist of. Note that this value is always larger than that of the
2526public key. Return 0 for an unknown algorithm. 2526public key. Return 0 for an unknown algorithm.
2527 2527
2528@item GCRYCTL_GET_ALGO_NSIGN 2528@item GCRYCTL_GET_ALGO_NSIGN
2529Return the number of elements a signature created with the algorithm 2529Return the number of elements a signature created with the algorithm
2530@var{algo} consists of. Return 0 for an unknown algorithm or for an 2530@var{algo} consists of. Return 0 for an unknown algorithm or for an
2531algorithm not capable of creating signatures. 2531algorithm not capable of creating signatures.
2532 2532
2533@item GCRYCTL_GET_ALGO_NENC 2533@item GCRYCTL_GET_ALGO_NENC
2534Return the number of elements a encrypted message created with the algorithm 2534Return the number of elements a encrypted message created with the algorithm
2535@var{algo} consists of. Return 0 for an unknown algorithm or for an 2535@var{algo} consists of. Return 0 for an unknown algorithm or for an
2536algorithm not capable of encryption. 2536algorithm not capable of encryption.
2537@end table 2537@end table
2538 2538
2539@noindent 2539@noindent
2540Please note that parameters not required should be passed as @code{NULL}. 2540Please note that parameters not required should be passed as @code{NULL}.
2541@end deftypefun 2541@end deftypefun
2542@c end gcry_pk_algo_info 2542@c end gcry_pk_algo_info
2543 2543
2544 2544
2545@deftypefun gcry_error_t gcry_pk_ctl (@w{int @var{cmd}}, @w{void *@var{buffer}}, @w{size_t @var{buflen}}) 2545@deftypefun gcry_error_t gcry_pk_ctl (@w{int @var{cmd}}, @w{void *@var{buffer}}, @w{size_t @var{buflen}})
2546 2546
2547This is a general purpose function to perform certain control 2547This is a general purpose function to perform certain control
2548operations. @var{cmd} controls what is to be done. The return value is 2548operations. @var{cmd} controls what is to be done. The return value is
25490 for success or an error code. Currently supported values for 25490 for success or an error code. Currently supported values for
2550@var{cmd} are: 2550@var{cmd} are:
2551 2551
2552@table @code 2552@table @code
2553@item GCRYCTL_DISABLE_ALGO 2553@item GCRYCTL_DISABLE_ALGO
2554Disable the algorithm given as an algorithm id in @var{buffer}. 2554Disable the algorithm given as an algorithm id in @var{buffer}.
2555@var{buffer} must point to an @code{int} variable with the algorithm id 2555@var{buffer} must point to an @code{int} variable with the algorithm id
2556and @var{buflen} must have the value @code{sizeof (int)}. 2556and @var{buflen} must have the value @code{sizeof (int)}.
2557 2557
2558@end table 2558@end table
2559@end deftypefun 2559@end deftypefun
2560@c end gcry_pk_ctl 2560@c end gcry_pk_ctl
2561 2561
2562@noindent 2562@noindent
2563@acronym{Libgcrypt} also provides a function for generating public key 2563@acronym{Libgcrypt} also provides a function for generating public key
2564pairs: 2564pairs:
2565 2565
2566@deftypefun gcry_error_t gcry_pk_genkey (@w{gcry_sexp_t *@var{r_key}}, @w{gcry_sexp_t @var{parms}}) 2566@deftypefun gcry_error_t gcry_pk_genkey (@w{gcry_sexp_t *@var{r_key}}, @w{gcry_sexp_t @var{parms}})
2567 2567
2568This function create a new public key pair using information given in 2568This function create a new public key pair using information given in
2569the S-expression @var{parms} and stores the private and the public key 2569the S-expression @var{parms} and stores the private and the public key
2570in one new S-expression at the address given by @var{r_key}. In case of 2570in one new S-expression at the address given by @var{r_key}. In case of
2571an error, @var{r_key} is set to @code{NULL}. The return code is 0 for 2571an error, @var{r_key} is set to @code{NULL}. The return code is 0 for
2572success or an error code otherwise. 2572success or an error code otherwise.
2573 2573
2574@noindent 2574@noindent
2575Here is an example for @var{parms} for creating a 1024 bit RSA key: 2575Here is an example for @var{parms} for creating a 1024 bit RSA key:
2576 2576
2577@example 2577@example
2578(genkey 2578(genkey
2579 (rsa 2579 (rsa
2580 (nbits 4:1024))) 2580 (nbits 4:1024)))
2581@end example 2581@end example
2582 2582
2583@noindent 2583@noindent
2584To create an ElGamal key, substitute "elg" for "rsa" and to create a DSA 2584To create an ElGamal key, substitute "elg" for "rsa" and to create a DSA
2585key use "dsa". Valid ranges for the key length depend on the 2585key use "dsa". Valid ranges for the key length depend on the
2586algorithms; all commonly used key lengths are supported. Currently 2586algorithms; all commonly used key lengths are supported. Currently
2587supported parameters are: 2587supported parameters are:
2588 2588
2589@table @code 2589@table @code
2590@item nbits 2590@item nbits
2591This is always required to specify the length of the key. The argument 2591This is always required to specify the length of the key. The argument
2592is a string with a number in C-notation. 2592is a string with a number in C-notation.
2593 2593
2594@item rsa-use-e 2594@item rsa-use-e
2595This is only used with RSA to give a hint for the public exponent. The 2595This is only used with RSA to give a hint for the public exponent. The
2596value will be used as a base to test for a usable exponent. Some values 2596value will be used as a base to test for a usable exponent. Some values
2597are special: 2597are special:
2598 2598
2599@table @samp 2599@table @samp
2600@item 0 2600@item 0
2601Use a secure and fast value. This is currently the number 41. 2601Use a secure and fast value. This is currently the number 41.
2602@item 1 2602@item 1
2603Use a secure value as required by some specification. This is currently 2603Use a secure value as required by some specification. This is currently
2604the number 65537. 2604the number 65537.
2605@item 2 2605@item 2
2606Reserved 2606Reserved
2607@end table 2607@end table
2608 2608
2609@noindent 2609@noindent
2610If this parameter is not used, @acronym{Libgcrypt} uses for historic reasons 2610If this parameter is not used, @acronym{Libgcrypt} uses for historic reasons
261165537. 261165537.
2612 2612
2613@end table 2613@end table
2614@c end table of parameters 2614@c end table of parameters
2615 2615
2616@noindent 2616@noindent
2617The key pair is returned in a format depending on the algorithm. Both 2617The key pair is returned in a format depending on the algorithm. Both
2618private and public keys are returned in one container and may be 2618private and public keys are returned in one container and may be
2619accompanied by some miscellaneous information. 2619accompanied by some miscellaneous information.
2620 2620
2621@noindent 2621@noindent
2622As an example, here is what the ElGamal key generation returns: 2622As an example, here is what the ElGamal key generation returns:
2623 2623
2624@example 2624@example
2625(key-data 2625(key-data
2626 (public-key 2626 (public-key
2627 (elg 2627 (elg
2628 (p @var{p-mpi}) 2628 (p @var{p-mpi})
2629 (g @var{g-mpi}) 2629 (g @var{g-mpi})
2630 (y @var{y-mpi}))) 2630 (y @var{y-mpi})))
2631 (private-key 2631 (private-key
2632 (elg 2632 (elg
2633 (p @var{p-mpi}) 2633 (p @var{p-mpi})
2634 (g @var{g-mpi}) 2634 (g @var{g-mpi})
2635 (y @var{y-mpi}) 2635 (y @var{y-mpi})
2636 (x @var{x-mpi}))) 2636 (x @var{x-mpi})))
2637 (misc-key-info 2637 (misc-key-info
2638 (pm1-factors @var{n1 n2 ... nn}))) 2638 (pm1-factors @var{n1 n2 ... nn})))
2639@end example 2639@end example
2640 2640
2641@noindent 2641@noindent
2642As you can see, some of the information is duplicated, but this provides 2642As you can see, some of the information is duplicated, but this provides
2643an easy way to extract either the public or the private key. Note that 2643an easy way to extract either the public or the private key. Note that
2644the order of the elements is not defined, e.g. the private key may be 2644the order of the elements is not defined, e.g. the private key may be
2645stored before the public key. @var{n1 n2 ... nn} is a list of prime 2645stored before the public key. @var{n1 n2 ... nn} is a list of prime
2646numbers used to composite @var{p-mpi}; this is in general not a very 2646numbers used to composite @var{p-mpi}; this is in general not a very
2647useful information. 2647useful information.
2648@end deftypefun 2648@end deftypefun
2649@c end gcry_pk_genkey 2649@c end gcry_pk_genkey
2650 2650
2651@node Public Key cryptography (II) 2651@node Public Key cryptography (II)
2652@chapter Public Key cryptography (II) 2652@chapter Public Key cryptography (II)
2653 2653
2654This chapter documents the alternative interface to asymmetric 2654This chapter documents the alternative interface to asymmetric
2655cryptography (ac) that is not based on S-expressions, but on native C 2655cryptography (ac) that is not based on S-expressions, but on native C
2656data structures. As opposed to the pk interface described in the 2656data structures. As opposed to the pk interface described in the
2657former chapter, this one follows an open/use/close paradigm like other 2657former chapter, this one follows an open/use/close paradigm like other
2658building blocks of the library. 2658building blocks of the library.
2659 2659
2660@menu 2660@menu
2661* Available asymmetric algorithms:: List of algorithms supported by the library. 2661* Available asymmetric algorithms:: List of algorithms supported by the library.
2662* Working with sets of data:: How to work with sets of data. 2662* Working with sets of data:: How to work with sets of data.
2663* Working with handles:: How to use handles. 2663* Working with handles:: How to use handles.
2664* Working with keys:: How to work with keys. 2664* Working with keys:: How to work with keys.
2665* Using cryptographic functions:: How to perform cryptographic operations. 2665* Using cryptographic functions:: How to perform cryptographic operations.
2666* Handle-independent functions:: General functions independent of handles. 2666* Handle-independent functions:: General functions independent of handles.
2667@end menu 2667@end menu
2668 2668
2669@node Available asymmetric algorithms 2669@node Available asymmetric algorithms
2670@section Available asymmetric algorithms 2670@section Available asymmetric algorithms
2671 2671
2672@acronym{Libgcrypt} supports the RSA (Rivest-Shamir-Adleman) 2672@acronym{Libgcrypt} supports the RSA (Rivest-Shamir-Adleman)
2673algorithms as well as DSA (Digital Signature Algorithm) and ElGamal. 2673algorithms as well as DSA (Digital Signature Algorithm) and ElGamal.
2674The versatile interface allows to add more algorithms in the future. 2674The versatile interface allows to add more algorithms in the future.
2675 2675
2676@deftp {Data type} gcry_ac_id_t 2676@deftp {Data type} gcry_ac_id_t
2677 2677
2678The following constants are defined for this type: 2678The following constants are defined for this type:
2679 2679
2680@table @code 2680@table @code
2681@item GCRY_AC_RSA 2681@item GCRY_AC_RSA
2682Riven-Shamir-Adleman 2682Riven-Shamir-Adleman
2683@item GCRY_AC_DSA 2683@item GCRY_AC_DSA
2684Digital Signature Algorithm 2684Digital Signature Algorithm
2685@item GCRY_AC_ELG 2685@item GCRY_AC_ELG
2686ElGamal 2686ElGamal
2687@item GCRY_AC_ELG_E 2687@item GCRY_AC_ELG_E
2688ElGamal, encryption only. 2688ElGamal, encryption only.
2689@end table 2689@end table
2690@end deftp 2690@end deftp
2691 2691
2692@node Working with sets of data 2692@node Working with sets of data
2693@section Working with sets of data 2693@section Working with sets of data
2694 2694
2695In the context of this interface the term `data set' refers to a list 2695In the context of this interface the term `data set' refers to a list
2696of `named MPI values' that is used by functions performing 2696of `named MPI values' that is used by functions performing
2697cryptographic operations. 2697cryptographic operations.
2698 2698
2699Such data sets are used for representing keys, since keys simply 2699Such data sets are used for representing keys, since keys simply
2700consist of a variable amount of numbers. Furthermore some functions 2700consist of a variable amount of numbers. Furthermore some functions
2701return data sets to the caller that are to be provided to other 2701return data sets to the caller that are to be provided to other
2702functions. 2702functions.
2703 2703
2704This section documents the data types, symbols and functions that are 2704This section documents the data types, symbols and functions that are
2705relevant for working with such data sets. 2705relevant for working with such data sets.
2706 2706
2707@deftp {Data type} gcry_ac_data_t 2707@deftp {Data type} gcry_ac_data_t
2708A data set, that is simply a list of named MPI values. 2708A data set, that is simply a list of named MPI values.
2709@end deftp 2709@end deftp
2710 2710
2711The following flags are supported: 2711The following flags are supported:
2712 2712
2713@table @code 2713@table @code
2714@item GCRY_AC_FLAG_DEALLOC 2714@item GCRY_AC_FLAG_DEALLOC
2715Used for storing data in a data set. If given, the data will be 2715Used for storing data in a data set. If given, the data will be
2716released by the library. 2716released by the library.
2717 2717
2718@item GCRY_AC_FLAG_COPY 2718@item GCRY_AC_FLAG_COPY
2719Used for storing/retrieving data in/from a data set. If given, the 2719Used for storing/retrieving data in/from a data set. If given, the
2720library will create copies of the provided/contained data, which will 2720library will create copies of the provided/contained data, which will
2721then be given to the user/associated with the data set. 2721then be given to the user/associated with the data set.
2722@end table 2722@end table
2723 2723
2724@deftypefun gcry_error_t gcry_ac_data_new (gcry_ac_data_t *@var{data}) 2724@deftypefun gcry_error_t gcry_ac_data_new (gcry_ac_data_t *@var{data})
2725Creates a new, empty data set and stores it in @var{data}. 2725Creates a new, empty data set and stores it in @var{data}.
2726@end deftypefun 2726@end deftypefun
2727 2727
2728@deftypefun void gcry_ac_data_destroy (gcry_ac_data_t @var{data}) 2728@deftypefun void gcry_ac_data_destroy (gcry_ac_data_t @var{data})
2729Destroys the data set @var{data}. 2729Destroys the data set @var{data}.
2730@end deftypefun 2730@end deftypefun
2731 2731
2732@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}) 2732@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})
2733Add the value @var{mpi} to @var{data} with the label @var{name}. If 2733Add the value @var{mpi} to @var{data} with the label @var{name}. If
2734@var{flags} contains GCRY_AC_FLAG_DATA_COPY, the data set will contain 2734@var{flags} contains GCRY_AC_FLAG_DATA_COPY, the data set will contain
2735copies of @var{name} and @var{mpi}. If @var{flags} contains 2735copies of @var{name} and @var{mpi}. If @var{flags} contains
2736GCRY_AC_FLAG_DATA_DEALLOC or GCRY_AC_FLAG_DATA_COPY, the values 2736GCRY_AC_FLAG_DATA_DEALLOC or GCRY_AC_FLAG_DATA_COPY, the values
2737contained in the data set will be deallocated when they are to be 2737contained in the data set will be deallocated when they are to be
2738removed from the data set. 2738removed from the data set.
2739@end deftypefun 2739@end deftypefun
2740 2740
2741@deftypefun gcry_error_t gcry_ac_data_copy (gcry_ac_data_t *@var{data_cp}, gcry_ac_data_t @var{data}) 2741@deftypefun gcry_error_t gcry_ac_data_copy (gcry_ac_data_t *@var{data_cp}, gcry_ac_data_t @var{data})
2742Create a copy of the data set @var{data} and store it in @var{data_cp}. 2742Create a copy of the data set @var{data} and store it in @var{data_cp}.
2743@end deftypefun 2743@end deftypefun
2744 2744
2745@deftypefun unsigned int gcry_ac_data_length (gcry_ac_data_t @var{data}) 2745@deftypefun unsigned int gcry_ac_data_length (gcry_ac_data_t @var{data})
2746Returns the number of named MPI values inside of the data set 2746Returns the number of named MPI values inside of the data set
2747@var{data}. 2747@var{data}.
2748@end deftypefun 2748@end deftypefun
2749 2749
2750@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}) 2750@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})
2751Store the value labelled with @var{name} found in @var{data} in 2751Store the value labelled with @var{name} found in @var{data} in
2752@var{mpi}. If @var{flags} contains GCRY_AC_FLAG_COPY, store a copy of 2752@var{mpi}. If @var{flags} contains GCRY_AC_FLAG_COPY, store a copy of
2753the @var{mpi} value contained in the data set. @var{mpi} may be NULL. 2753the @var{mpi} value contained in the data set. @var{mpi} may be NULL.
2754@end deftypefun 2754@end deftypefun
2755 2755
2756@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}) 2756@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})
2757Stores in @var{name} and @var{mpi} the named @var{mpi} value contained 2757Stores in @var{name} and @var{mpi} the named @var{mpi} value contained
2758in the data set @var{data} with the index @var{idx}. If @var{flags} 2758in the data set @var{data} with the index @var{idx}. If @var{flags}
2759contains GCRY_AC_FLAG_COPY, store copies of the values contained in 2759contains GCRY_AC_FLAG_COPY, store copies of the values contained in
2760the data set. @var{name} or @var{mpi} may be NULL. 2760the data set. @var{name} or @var{mpi} may be NULL.
2761@end deftypefun 2761@end deftypefun
2762 2762
2763@deftypefun void gcry_ac_data_clear (gcry_ac_data_t @var{data}) 2763@deftypefun void gcry_ac_data_clear (gcry_ac_data_t @var{data})
2764Destroys any values contained in the data set @var{data}. 2764Destroys any values contained in the data set @var{data}.
2765@end deftypefun 2765@end deftypefun
2766 2766
2767@node Working with handles 2767@node Working with handles
2768@section Working with handles 2768@section Working with handles
2769 2769
2770In order to use an algorithm, an according handle must be created. 2770In order to use an algorithm, an according handle must be created.
2771This is done using the following function: 2771This is done using the following function:
2772 2772
2773@deftypefun gcry_error_t gcry_ac_open (gcry_ac_handle_t *@var{handle}, int @var{algorithm}, int @var{flags}) 2773@deftypefun gcry_error_t gcry_ac_open (gcry_ac_handle_t *@var{handle}, int @var{algorithm}, int @var{flags})
2774 2774
2775Creates a new handle for the algorithm @var{algorithm} and stores it 2775Creates a new handle for the algorithm @var{algorithm} and stores it
2776in @var{handle}. @var{flags} is not used yet. 2776in @var{handle}. @var{flags} is not used yet.
2777 2777
2778@var{algorithm} must be a valid algorithm ID, see @xref{Available 2778@var{algorithm} must be a valid algorithm ID, see @xref{Available
2779algorithms}, for a list of supported algorithms and the according 2779algorithms}, for a list of supported algorithms and the according
2780constants. Besides using the listed constants directly, the functions 2780constants. Besides using the listed constants directly, the functions
2781@code{gcry_ac_name_to_id} may be used to convert the textual name of 2781@code{gcry_ac_name_to_id} may be used to convert the textual name of
2782an algorithm into the according numeric ID. 2782an algorithm into the according numeric ID.
2783@end deftypefun 2783@end deftypefun
2784 2784
2785@deftypefun void gcry_ac_close (gcry_ac_handle_t @var{handle}) 2785@deftypefun void gcry_ac_close (gcry_ac_handle_t @var{handle})
2786Destroys the handle @var{handle}. 2786Destroys the handle @var{handle}.
2787@end deftypefun 2787@end deftypefun
2788 2788
2789@node Working with keys 2789@node Working with keys
2790@section Working with keys 2790@section Working with keys
2791 2791
2792@deftp {Data type} gcry_ac_key_type_t 2792@deftp {Data type} gcry_ac_key_type_t
2793Defined constants: 2793Defined constants:
2794 2794
2795@table @code 2795@table @code
2796@item GCRY_AC_KEY_TYPE_SECRET 2796@item GCRY_AC_KEY_TYPE_SECRET
2797Specifies a secret key. 2797Specifies a secret key.
2798@item GCRY_AC_KEY_TYPE_PUBLIC 2798@item GCRY_AC_KEY_TYPE_PUBLIC
2799Specifies a public key. 2799Specifies a public key.
2800@end table 2800@end table
2801@end deftp 2801@end deftp
2802 2802
2803@deftp {Data type} gcry_ac_key_t 2803@deftp {Data type} gcry_ac_key_t
2804This type represents a single `key', either a secret one or a public 2804This type represents a single `key', either a secret one or a public
2805one. 2805one.
2806@end deftp 2806@end deftp
2807 2807
2808@deftp {Data type} gcry_ac_key_pair_t 2808@deftp {Data type} gcry_ac_key_pair_t
2809This type represents a `key pair' containing a secret and a public key. 2809This type represents a `key pair' containing a secret and a public key.
2810@end deftp 2810@end deftp
2811 2811
2812Key data structures can be created in two different ways; a new key 2812Key data structures can be created in two different ways; a new key
2813pair can be generated, resulting in ready-to-use key. Alternatively a 2813pair can be generated, resulting in ready-to-use key. Alternatively a
2814key can be initialized from a given data set. 2814key can be initialized from a given data set.
2815 2815
2816@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}) 2816@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})
2817Creates a new key of type @var{type}, consisting of the MPI values 2817Creates a new key of type @var{type}, consisting of the MPI values
2818contained in the data set @var{data} and stores it in @var{key}. 2818contained in the data set @var{data} and stores it in @var{key}.
2819@end deftypefun 2819@end deftypefun
2820 2820
2821@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}) 2821@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})
2822 2822
2823Generates a new key pair via the handle @var{handle} of @var{NBITS} 2823Generates a new key pair via the handle @var{handle} of @var{NBITS}
2824bits and stores it in @var{key_pair}. 2824bits and stores it in @var{key_pair}.
2825 2825
2826In case non-standard settings are wanted, a pointer to a structure of 2826In case non-standard settings are wanted, a pointer to a structure of
2827type @code{gcry_ac_key_spec_<algorithm>_t}, matching the selected 2827type @code{gcry_ac_key_spec_<algorithm>_t}, matching the selected
2828algorithm, can be given as @var{key_spec}. @var{misc_data} is not 2828algorithm, can be given as @var{key_spec}. @var{misc_data} is not
2829used yet. Such a structure does only exist for RSA. A descriptions 2829used yet. Such a structure does only exist for RSA. A descriptions
2830of the members of the supported structures follows. 2830of the members of the supported structures follows.
2831 2831
2832@table @code 2832@table @code
2833@item gcry_ac_key_spec_rsa_t 2833@item gcry_ac_key_spec_rsa_t
2834@table @code 2834@table @code
2835@item gcry_mpi_t e 2835@item gcry_mpi_t e
2836Generate the key pair using a special @code{e}. The value of @code{e} 2836Generate the key pair using a special @code{e}. The value of @code{e}
2837has the following meanings: 2837has the following meanings:
2838@table @code 2838@table @code
2839@item = 0 2839@item = 0
2840Let @acronym{Libgcrypt} device what exponent should be used. 2840Let @acronym{Libgcrypt} device what exponent should be used.
2841@item = 1 2841@item = 1
2842Request the use of a ``secure'' exponent; this is required by some 2842Request the use of a ``secure'' exponent; this is required by some
2843specification to be 65537. 2843specification to be 65537.
2844@item > 2 2844@item > 2
2845Try starting at this value until a working exponent is found. Note, 2845Try starting at this value until a working exponent is found. Note,
2846that the current implementation leaks some information about the 2846that the current implementation leaks some information about the
2847private key because the incrementation used is not randomized. Thus, 2847private key because the incrementation used is not randomized. Thus,
2848this function will be changed in the future to return a random 2848this function will be changed in the future to return a random
2849exponent of the given size. 2849exponent of the given size.
2850@end table 2850@end table
2851@end table 2851@end table
2852@end table 2852@end table
2853 2853
2854Example code: 2854Example code:
2855@example 2855@example
2856@{ 2856@{
2857 gcry_ac_key_pair_t key_pair; 2857 gcry_ac_key_pair_t key_pair;
2858 gcry_ac_key_spec_rsa rsa_spec; 2858 gcry_ac_key_spec_rsa rsa_spec;
2859 2859
2860 rsa_spec.e = gcry_mpi_new (0); 2860 rsa_spec.e = gcry_mpi_new (0);
2861 gcry_mpi_set_ui (rsa_spec.e, 1) 2861 gcry_mpi_set_ui (rsa_spec.e, 1)
2862 2862
2863 err = gcry_ac_open (&handle, GCRY_AC_RSA, 0); 2863 err = gcry_ac_open (&handle, GCRY_AC_RSA, 0);
2864 assert (! err); 2864 assert (! err);
2865 2865
2866 err = gcry_ac_key_pair_generate (handle, &key_pair, 1024, (void *) &rsa_spec); 2866 err = gcry_ac_key_pair_generate (handle, &key_pair, 1024, (void *) &rsa_spec);
2867 assert (! err); 2867 assert (! err);
2868@} 2868@}
2869@end example 2869@end example
2870@end deftypefun 2870@end deftypefun
2871 2871
2872 2872
2873@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}) 2873@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})
2874Returns the key of type @var{which} out of the key pair 2874Returns the key of type @var{which} out of the key pair
2875@var{key_pair}. 2875@var{key_pair}.
2876@end deftypefun 2876@end deftypefun
2877 2877
2878@deftypefun void gcry_ac_key_destroy (gcry_ac_key_t @var{key}) 2878@deftypefun void gcry_ac_key_destroy (gcry_ac_key_t @var{key})
2879Destroys the key @var{key}. 2879Destroys the key @var{key}.
2880@end deftypefun 2880@end deftypefun
2881 2881
2882@deftypefun void gcry_ac_key_pair_destroy (gcry_ac_key_pair_t @var{key_pair}) 2882@deftypefun void gcry_ac_key_pair_destroy (gcry_ac_key_pair_t @var{key_pair})
2883Destroys the key pair @var{key_pair}. 2883Destroys the key pair @var{key_pair}.
2884@end deftypefun 2884@end deftypefun
2885 2885
2886@deftypefun gcry_ac_data_t gcry_ac_key_data_get (gcry_ac_key_t @var{key}) 2886@deftypefun gcry_ac_data_t gcry_ac_key_data_get (gcry_ac_key_t @var{key})
2887Returns the data set contained in the key @var{key}. 2887Returns the data set contained in the key @var{key}.
2888@end deftypefun 2888@end deftypefun
2889 2889
2890@deftypefun gcry_error_t gcry_ac_key_test (gcry_ac_handle_t @var{handle}, gcry_ac_key_t @var{key}) 2890@deftypefun gcry_error_t gcry_ac_key_test (gcry_ac_handle_t @var{handle}, gcry_ac_key_t @var{key})
2891Verifies that the private key @var{key} is sane via @var{handle}. 2891Verifies that the private key @var{key} is sane via @var{handle}.
2892@end deftypefun 2892@end deftypefun
2893 2893
2894@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}) 2894@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})
2895Stores the number of bits of the key @var{key} in @var{nbits} via @var{handle}. 2895Stores the number of bits of the key @var{key} in @var{nbits} via @var{handle}.
2896@end deftypefun 2896@end deftypefun
2897 2897
2898@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}) 2898@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})
2899Writes the 20 byte long key grip of the key @var{key} to 2899Writes the 20 byte long key grip of the key @var{key} to
2900@var{key_grip} via @var{handle}. 2900@var{key_grip} via @var{handle}.
2901@end deftypefun 2901@end deftypefun
2902 2902
2903@node Using cryptographic functions 2903@node Using cryptographic functions
2904@section Using cryptographic functions 2904@section Using cryptographic functions
2905 2905
2906The following flags might be relevant: 2906The following flags might be relevant:
2907 2907
2908@table @code 2908@table @code
2909@item GCRY_AC_FLAG_NO_BLINDING 2909@item GCRY_AC_FLAG_NO_BLINDING
2910Disable any blinding, which might be supported by the chosen 2910Disable any blinding, which might be supported by the chosen
2911algorithm; blinding is the default. 2911algorithm; blinding is the default.
2912@end table 2912@end table
2913 2913
2914@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}) 2914@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})
2915Encrypts the plain text MPI value @var{data_plain} with the key public 2915Encrypts the plain text MPI value @var{data_plain} with the key public
2916@var{key} under the control of the flags @var{flags} and stores the 2916@var{key} under the control of the flags @var{flags} and stores the
2917resulting data set into @var{data_encrypted}. 2917resulting data set into @var{data_encrypted}.
2918@end deftypefun 2918@end deftypefun
2919 2919
2920@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}) 2920@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})
2921Decrypts the encrypted data contained in the data set 2921Decrypts the encrypted data contained in the data set
2922@var{data_encrypted} with the secret key KEY under the control of the 2922@var{data_encrypted} with the secret key KEY under the control of the
2923flags @var{flags} and stores the resulting plain text MPI value in 2923flags @var{flags} and stores the resulting plain text MPI value in
2924@var{DATA_PLAIN}. 2924@var{DATA_PLAIN}.
2925@end deftypefun 2925@end deftypefun
2926 2926
2927@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}) 2927@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})
2928Signs the data contained in @var{data} with the secret key @var{key} 2928Signs the data contained in @var{data} with the secret key @var{key}
2929and stores the resulting signature in the data set 2929and stores the resulting signature in the data set
2930@var{data_signature}. 2930@var{data_signature}.
2931@end deftypefun 2931@end deftypefun
2932 2932
2933@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}) 2933@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})
2934Verifies that the signature contained in the data set 2934Verifies that the signature contained in the data set
2935@var{data_signature} is indeed the result of signing the data 2935@var{data_signature} is indeed the result of signing the data
2936contained in @var{data} with the secret key belonging to the public 2936contained in @var{data} with the secret key belonging to the public
2937key @var{key}. 2937key @var{key}.
2938@end deftypefun 2938@end deftypefun
2939 2939
2940@node Handle-independent functions 2940@node Handle-independent functions
2941@section Handle-independent functions 2941@section Handle-independent functions
2942 2942
2943@deftypefun gcry_error_t gcry_ac_id_to_name (gcry_ac_id_t @var{algorithm}, const char **@var{name}) 2943@deftypefun gcry_error_t gcry_ac_id_to_name (gcry_ac_id_t @var{algorithm}, const char **@var{name})
2944Stores the textual representation of the algorithm whose id is given 2944Stores the textual representation of the algorithm whose id is given
2945in @var{algorithm} in @var{name}. 2945in @var{algorithm} in @var{name}.
2946@end deftypefun 2946@end deftypefun
2947 2947
2948@deftypefun gcry_error_t gcry_ac_name_to_id (const char *@var{name}, gcry_ac_id_t *@var{algorithm}) 2948@deftypefun gcry_error_t gcry_ac_name_to_id (const char *@var{name}, gcry_ac_id_t *@var{algorithm})
2949Stores the numeric ID of the algorithm whose textual representation is 2949Stores the numeric ID of the algorithm whose textual representation is
2950contained in @var{name} in @var{algorithm}. 2950contained in @var{name} in @var{algorithm}.
2951@end deftypefun 2951@end deftypefun
2952 2952
2953@c ********************************************************** 2953@c **********************************************************
2954@c ******************* Random ***************************** 2954@c ******************* Random *****************************
2955@c ********************************************************** 2955@c **********************************************************
2956@node Random Numbers 2956@node Random Numbers
2957@chapter Random Numbers 2957@chapter Random Numbers
2958 2958
2959@menu 2959@menu
2960* Quality of random numbers:: @acronym{Libgcrypt} uses different quality levels. 2960* Quality of random numbers:: @acronym{Libgcrypt} uses different quality levels.
2961* Retrieving random numbers:: How to retrieve random numbers. 2961* Retrieving random numbers:: How to retrieve random numbers.
2962@end menu 2962@end menu
2963 2963
2964@node Quality of random numbers 2964@node Quality of random numbers
2965@section Quality of random numbers 2965@section Quality of random numbers
2966 2966
2967@acronym{Libgcypt} offers random numbers of different quality levels: 2967@acronym{Libgcypt} offers random numbers of different quality levels:
2968 2968
2969@deftp {Data type} enum gcry_random_level 2969@deftp {Data type} enum gcry_random_level
2970The constants for the random quality levels are of this type. 2970The constants for the random quality levels are of this type.
2971@end deftp 2971@end deftp
2972 2972
2973@table @code 2973@table @code
2974@item GCRY_WEAK_RANDOM 2974@item GCRY_WEAK_RANDOM
2975This should not anymore be used. It has recently been changed to an 2975This should not anymore be used. It has recently been changed to an
2976alias of GCRY_STRONG_RANDOM. Use @code{gcry_create_nonce} instead. 2976alias of GCRY_STRONG_RANDOM. Use @code{gcry_create_nonce} instead.
2977@item GCRY_STRONG_RANDOM 2977@item GCRY_STRONG_RANDOM
2978Use this level for e.g. session keys and similar purposes. 2978Use this level for e.g. session keys and similar purposes.
2979@item GCRY_VERY_STRONG_RANDOM 2979@item GCRY_VERY_STRONG_RANDOM
2980Use this level for e.g. key material. 2980Use this level for e.g. key material.
2981@end table 2981@end table
2982 2982
2983@node Retrieving random numbers 2983@node Retrieving random numbers
2984@section Retrieving random numbers 2984@section Retrieving random numbers
2985 2985
2986@deftypefun void gcry_randomize (unsigned char *@var{buffer}, size_t @var{length}, enum gcry_random_level @var{level}) 2986@deftypefun void gcry_randomize (unsigned char *@var{buffer}, size_t @var{length}, enum gcry_random_level @var{level})
2987 2987
2988Fill @var{buffer} with @var{length} random bytes using a random quality 2988Fill @var{buffer} with @var{length} random bytes using a random quality
2989as defined by @var{level}. 2989as defined by @var{level}.
2990@end deftypefun 2990@end deftypefun
2991 2991
2992@deftypefun void * gcry_random_bytes (size_t @var{nbytes}, enum gcry_random_level @var{level}) 2992@deftypefun void * gcry_random_bytes (size_t @var{nbytes}, enum gcry_random_level @var{level})
2993 2993
2994Allocate a memory block consisting of @var{nbytes} fresh random bytes 2994Allocate a memory block consisting of @var{nbytes} fresh random bytes
2995using a random quality as defined by @var{level}. 2995using a random quality as defined by @var{level}.
2996@end deftypefun 2996@end deftypefun
2997 2997
2998@deftypefun void * gcry_random_bytes_secure (size_t @var{nbytes}, enum gcry_random_level @var{level}) 2998@deftypefun void * gcry_random_bytes_secure (size_t @var{nbytes}, enum gcry_random_level @var{level})
2999 2999
3000Allocate a memory block consisting of @var{nbytes} fresh random bytes 3000Allocate a memory block consisting of @var{nbytes} fresh random bytes
3001using a random quality as defined by @var{level}. This function 3001using a random quality as defined by @var{level}. This function
3002differs from @code{gcry_random_bytes} in that the returned buffer is 3002differs from @code{gcry_random_bytes} in that the returned buffer is
3003allocated in a ``secure'' area of the memory. 3003allocated in a ``secure'' area of the memory.
3004@end deftypefun 3004@end deftypefun
3005 3005
3006@deftypefun void gcry_create_nonce (void *@var{buffer}, size_t @var{length}) 3006@deftypefun void gcry_create_nonce (void *@var{buffer}, size_t @var{length})
3007 3007
3008Fill @var{buffer} with @var{length} unpredictable bytes. This is 3008Fill @var{buffer} with @var{length} unpredictable bytes. This is
3009commonly called a nonce and may also be used for initialization 3009commonly called a nonce and may also be used for initialization
3010vectors and padding. This is an extra function nearly independent of 3010vectors and padding. This is an extra function nearly independent of
3011the other random function for 3 reasons: It better protects the 3011the other random function for 3 reasons: It better protects the
3012regular random generator's internal state, provides better performance 3012regular random generator's internal state, provides better performance
3013and does not drain the precious entropy pool. 3013and does not drain the precious entropy pool.
3014 3014
3015@end deftypefun 3015@end deftypefun
3016 3016
3017 3017
3018 3018
3019@c ********************************************************** 3019@c **********************************************************
3020@c ******************* S-Expressions *********************** 3020@c ******************* S-Expressions ***********************
3021@c ********************************************************** 3021@c **********************************************************
3022@node S-expressions 3022@node S-expressions
3023@chapter S-expressions 3023@chapter S-expressions
3024 3024
3025S-expressions are used by the public key functions to pass complex data 3025S-expressions are used by the public key functions to pass complex data
3026structures around. These LISP like objects are used by some 3026structures around. These LISP like objects are used by some
3027cryptographic protocols (cf. RFC-2692) and @acronym{Libgcrypt} provides functions 3027cryptographic protocols (cf. RFC-2692) and @acronym{Libgcrypt} provides functions
3028to parse and construct them. For detailed information, see 3028to parse and construct them. For detailed information, see
3029@cite{Ron Rivest, code and description of S-expressions, 3029@cite{Ron Rivest, code and description of S-expressions,
3030@uref{http://theory.lcs.mit.edu/~rivest/sexp.html}}. 3030@uref{http://theory.lcs.mit.edu/~rivest/sexp.html}}.
3031 3031
3032@menu 3032@menu
3033* Data types for S-expressions:: Data types related with S-expressions. 3033* Data types for S-expressions:: Data types related with S-expressions.
3034* Working with S-expressions:: How to work with S-expressions. 3034* Working with S-expressions:: How to work with S-expressions.
3035@end menu 3035@end menu
3036 3036
3037@node Data types for S-expressions 3037@node Data types for S-expressions
3038@section Data types for S-expressions 3038@section Data types for S-expressions
3039 3039
3040@deftp {Data type} gcry_sexp_t 3040@deftp {Data type} gcry_sexp_t
3041The @code{gcry_sexp_t} type describes an object with the @acronym{Libgcrypt} internal 3041The @code{gcry_sexp_t} type describes an object with the @acronym{Libgcrypt} internal
3042representation of an S-expression. 3042representation of an S-expression.
3043@end deftp 3043@end deftp
3044 3044
3045@node Working with S-expressions 3045@node Working with S-expressions
3046@section Working with S-expressions 3046@section Working with S-expressions
3047 3047
3048@noindent 3048@noindent
3049There are several functions to create an @acronym{Libgcrypt} S-expression object 3049There are several functions to create an @acronym{Libgcrypt} S-expression object
3050from its external representation or from a string template. There is 3050from its external representation or from a string template. There is
3051also a function to convert the internal representation back into one of 3051also a function to convert the internal representation back into one of
3052the external formats: 3052the external formats:
3053 3053
3054 3054
3055@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}}) 3055@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}})
3056 3056
3057This is the generic function to create an new S-expression object from 3057This is the generic function to create an new S-expression object from
3058its external representation in @var{buffer} of @var{length} bytes. On 3058its external representation in @var{buffer} of @var{length} bytes. On
3059success the result is stored at the address given by @var{r_sexp}. 3059success the result is stored at the address given by @var{r_sexp}.
3060With @var{autodetect} set to 0, the data in @var{buffer} is expected to 3060With @var{autodetect} set to 0, the data in @var{buffer} is expected to
3061be in canonized format, with @var{autodetect} set to 1 the parses any of 3061be in canonized format, with @var{autodetect} set to 1 the parses any of
3062the defined external formats. If @var{buffer} does not hold a valid 3062the defined external formats. If @var{buffer} does not hold a valid
3063S-expression an error code is returned and @var{r_sexp} set to 3063S-expression an error code is returned and @var{r_sexp} set to
3064@code{NULL}. 3064@code{NULL}.
3065Note, that the caller is responsible for releasing the newly allocated 3065Note, that the caller is responsible for releasing the newly allocated
3066S-expression using @code{gcry_sexp_release}. 3066S-expression using @code{gcry_sexp_release}.
3067@end deftypefun 3067@end deftypefun
3068 3068
3069@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*)}) 3069@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*)})
3070 3070
3071This function is identical to @code{gcry_sexp_new} but has an extra 3071This function is identical to @code{gcry_sexp_new} but has an extra
3072argument @var{freefnc}, which, when not set to @code{NULL}, is expected 3072argument @var{freefnc}, which, when not set to @code{NULL}, is expected
3073to be a function to release the @var{buffer}; most likely the standard 3073to be a function to release the @var{buffer}; most likely the standard
3074@code{free} function is used for this argument. This has the effect of 3074@code{free} function is used for this argument. This has the effect of
3075transferring the ownership of @var{buffer} to the created object in 3075transferring the ownership of @var{buffer} to the created object in
3076@var{r_sexp}. The advantage of using this function is that @acronym{Libgcrypt} 3076@var{r_sexp}. The advantage of using this function is that @acronym{Libgcrypt}
3077might decide to directly use the provided buffer and thus avoid extra 3077might decide to directly use the provided buffer and thus avoid extra
3078copying. 3078copying.
3079@end deftypefun 3079@end deftypefun
3080 3080
3081@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}}) 3081@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}})
3082 3082
3083This is another variant of the above functions. It behaves nearly 3083This is another variant of the above functions. It behaves nearly
3084identical but provides an @var{erroff} argument which will receive the 3084identical but provides an @var{erroff} argument which will receive the
3085offset into the buffer where the parsing stopped on error. 3085offset into the buffer where the parsing stopped on error.
3086@end deftypefun 3086@end deftypefun
3087 3087
3088@deftypefun gcry_error_t gcry_sexp_build (@w{gcry_sexp_t *@var{r_sexp}}, @w{size_t *@var{erroff}}, @w{const char *@var{format}, ...}) 3088@deftypefun gcry_error_t gcry_sexp_build (@w{gcry_sexp_t *@var{r_sexp}}, @w{size_t *@var{erroff}}, @w{const char *@var{format}, ...})
3089 3089
3090This function creates an internal S-expression from the string template 3090This function creates an internal S-expression from the string template
3091@var{format} and stores it at the address of @var{r_sexp}. If there is a 3091@var{format} and stores it at the address of @var{r_sexp}. If there is a
3092parsing error, the function returns an appropriate error code and stores 3092parsing error, the function returns an appropriate error code and stores
3093the offset into @var{format} where the parsing stopped in @var{erroff}. 3093the offset into @var{format} where the parsing stopped in @var{erroff}.
3094The function supports a couple of printf-like formatting characters and 3094The function supports a couple of printf-like formatting characters and
3095expects arguments for some of these escape sequences right after 3095expects arguments for some of these escape sequences right after
3096@var{format}. The following format characters are defined: 3096@var{format}. The following format characters are defined:
3097 3097
3098@table @samp 3098@table @samp
3099@item %m 3099@item %m
3100The next argument is expected to be of type @code{gcry_mpi_t} and a copy of 3100The next argument is expected to be of type @code{gcry_mpi_t} and a copy of
3101its value is inserted into the resulting S-expression. 3101its value is inserted into the resulting S-expression.
3102@item %s 3102@item %s
3103The next argument is expected to be of type @code{char *} and that 3103The next argument is expected to be of type @code{char *} and that
3104string is inserted into the resulting S-expression. 3104string is inserted into the resulting S-expression.
3105@item %d 3105@item %d
3106The next argument is expected to be of type @code{int} and its 3106The next argument is expected to be of type @code{int} and its
3107value ist inserted into the resulting S-expression. 3107value ist inserted into the resulting S-expression.
3108@item %b 3108@item %b
3109The next argument is expected to be of type @code{int} directly 3109The next argument is expected to be of type @code{int} directly
3110followed by an argument of type @code{char *}. This represents a 3110followed by an argument of type @code{char *}. This represents a
3111buffer of given length to be inserted into the resulting regular 3111buffer of given length to be inserted into the resulting regular
3112expression. 3112expression.
3113@end table 3113@end table
3114 3114
3115@noindent 3115@noindent
3116No other format characters are defined and would return an error. Note, 3116No other format characters are defined and would return an error. Note,
3117that the format character @samp{%%} does not exists, because a percent 3117that the format character @samp{%%} does not exists, because a percent
3118sign is not a valid character in an S-expression. 3118sign is not a valid character in an S-expression.
3119@end deftypefun 3119@end deftypefun
3120 3120
3121@deftypefun void gcry_sexp_release (@w{gcry_sexp_t @var{sexp}}) 3121@deftypefun void gcry_sexp_release (@w{gcry_sexp_t @var{sexp}})
3122 3122
3123Release the S-expression object @var{sexp}. 3123Release the S-expression object @var{sexp}.
3124@end deftypefun 3124@end deftypefun
3125 3125
3126 3126
3127@noindent 3127@noindent
3128The next 2 functions are used to convert the internal representation 3128The next 2 functions are used to convert the internal representation
3129back into a regular external S-expression format and to show the 3129back into a regular external S-expression format and to show the
3130structure for debugging. 3130structure for debugging.
3131 3131
3132@deftypefun size_t gcry_sexp_sprint (@w{gcry_sexp_t @var{sexp}}, @w{int @var{mode}}, @w{void *@var{buffer}}, @w{size_t @var{maxlength}}) 3132@deftypefun size_t gcry_sexp_sprint (@w{gcry_sexp_t @var{sexp}}, @w{int @var{mode}}, @w{void *@var{buffer}}, @w{size_t @var{maxlength}})
3133 3133
3134Copies the S-expression object @var{sexp} into @var{buffer} using the 3134Copies the S-expression object @var{sexp} into @var{buffer} using the
3135format specified in @var{mode}. @var{maxlength} must be set to the 3135format specified in @var{mode}. @var{maxlength} must be set to the
3136allocated length of @var{buffer}. The function returns the actual 3136allocated length of @var{buffer}. The function returns the actual
3137length of valid bytes put into @var{buffer} or 0 if the provided buffer 3137length of valid bytes put into @var{buffer} or 0 if the provided buffer
3138is too short. Passing @code{NULL} for @var{buffer} returns the required 3138is too short. Passing @code{NULL} for @var{buffer} returns the required
3139length for @var{buffer}. For convenience reasons an extra byte with 3139length for @var{buffer}. For convenience reasons an extra byte with
3140value 0 is appended to the buffer. 3140value 0 is appended to the buffer.
3141 3141
3142@noindent 3142@noindent
3143The following formats are supported: 3143The following formats are supported:
3144 3144
3145@table @code 3145@table @code
3146@item GCRYSEXP_FMT_DEFAULT 3146@item GCRYSEXP_FMT_DEFAULT
3147Returns a convenient external S-expression representation. 3147Returns a convenient external S-expression representation.
3148 3148
3149@item GCRYSEXP_FMT_CANON 3149@item GCRYSEXP_FMT_CANON
3150Return the S-expression in canonical format. 3150Return the S-expression in canonical format.
3151 3151
3152@item GCRYSEXP_FMT_BASE64 3152@item GCRYSEXP_FMT_BASE64
3153Not currently supported. 3153Not currently supported.
3154 3154
3155@item GCRYSEXP_FMT_ADVANCED 3155@item GCRYSEXP_FMT_ADVANCED
3156Returns the S-expression in advanced format. 3156Returns the S-expression in advanced format.
3157@end table 3157@end table
3158@end deftypefun 3158@end deftypefun
3159 3159
3160@deftypefun void gcry_sexp_dump (@w{gcry_sexp_t @var{sexp}}) 3160@deftypefun void gcry_sexp_dump (@w{gcry_sexp_t @var{sexp}})
3161 3161
3162Dumps @var{sexp} in a format suitable for debugging to @acronym{Libgcrypt}'s 3162Dumps @var{sexp} in a format suitable for debugging to @acronym{Libgcrypt}'s
3163logging stream. 3163logging stream.
3164@end deftypefun 3164@end deftypefun
3165 3165
3166@noindent 3166@noindent
3167Often canonical encoding is used in the external representation. The 3167Often canonical encoding is used in the external representation. The
3168following function can be used to check for valid encoding and to learn 3168following function can be used to check for valid encoding and to learn
3169the length of the S-expression" 3169the length of the S-expression"
3170 3170
3171@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}}) 3171@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}})
3172 3172
3173Scan the canonical encoded @var{buffer} with implicit length values and 3173Scan the canonical encoded @var{buffer} with implicit length values and
3174return the actual length this S-expression uses. For a valid S-expression 3174return the actual length this S-expression uses. For a valid S-expression
3175it should never return 0. If @var{length} is not 0, the maximum 3175it should never return 0. If @var{length} is not 0, the maximum
3176length to scan is given; this can be used for syntax checks of 3176length to scan is given; this can be used for syntax checks of
3177data passed from outside. @var{errcode} and @var{erroff} may both be 3177data passed from outside. @var{errcode} and @var{erroff} may both be
3178passed as @code{NULL}. 3178passed as @code{NULL}.
3179 3179
3180@end deftypefun 3180@end deftypefun
3181 3181
3182 3182
3183@noindent 3183@noindent
3184There are a couple of functions to parse S-expressions and retrieve 3184There are a couple of functions to parse S-expressions and retrieve
3185elements: 3185elements:
3186 3186
3187@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}}) 3187@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}})
3188 3188
3189Scan the S-expression for a sublist with a type (the car of the list) 3189Scan the S-expression for a sublist with a type (the car of the list)
3190matching the string @var{token}. If @var{toklen} is not 0, the token is 3190matching the string @var{token}. If @var{toklen} is not 0, the token is
3191assumed to be raw memory of this length. The function returns a newly 3191assumed to be raw memory of this length. The function returns a newly
3192allocated S-expression consisting of the found sublist or @code{NULL} 3192allocated S-expression consisting of the found sublist or @code{NULL}
3193when not found. 3193when not found.
3194@end deftypefun 3194@end deftypefun
3195 3195
3196 3196
3197@deftypefun int gcry_sexp_length (@w{const gcry_sexp_t @var{list}}) 3197@deftypefun int gcry_sexp_length (@w{const gcry_sexp_t @var{list}})
3198 3198
3199Return the length of the @var{list}. For a valid S-expression this 3199Return the length of the @var{list}. For a valid S-expression this
3200should be at least 1. 3200should be at least 1.
3201@end deftypefun 3201@end deftypefun
3202 3202
3203 3203
3204@deftypefun gcry_sexp_t gcry_sexp_nth (@w{const gcry_sexp_t @var{list}}, @w{int @var{number}}) 3204@deftypefun gcry_sexp_t gcry_sexp_nth (@w{const gcry_sexp_t @var{list}}, @w{int @var{number}})
3205 3205
3206Create and return a new S-expression from the element with index @var{number} in 3206Create and return a new S-expression from the element with index @var{number} in
3207@var{list}. Note that the first element has the index 0. If there is 3207@var{list}. Note that the first element has the index 0. If there is
3208no such element, @code{NULL} is returned. 3208no such element, @code{NULL} is returned.
3209@end deftypefun 3209@end deftypefun
3210 3210
3211@deftypefun gcry_sexp_t gcry_sexp_car (@w{const gcry_sexp_t @var{list}}) 3211@deftypefun gcry_sexp_t gcry_sexp_car (@w{const gcry_sexp_t @var{list}})
3212 3212
3213Create and return a new S-expression from the first element in 3213Create and return a new S-expression from the first element in
3214@var{list}; this called the "type" and should always exist and be a 3214@var{list}; this called the "type" and should always exist and be a
3215string. @code{NULL} is returned in case of a problem. 3215string. @code{NULL} is returned in case of a problem.
3216@end deftypefun 3216@end deftypefun
3217 3217
3218@deftypefun gcry_sexp_t gcry_sexp_cdr (@w{const gcry_sexp_t @var{list}}) 3218@deftypefun gcry_sexp_t gcry_sexp_cdr (@w{const gcry_sexp_t @var{list}})
3219 3219
3220Create and return a new list form all elements except for the first one. 3220Create and return a new list form all elements except for the first one.
3221Note, that this function may return an invalid S-expression because it 3221Note, that this function may return an invalid S-expression because it
3222is not guaranteed, that the type exists and is a string. However, for 3222is not guaranteed, that the type exists and is a string. However, for
3223parsing a complex S-expression it might be useful for intermediate 3223parsing a complex S-expression it might be useful for intermediate
3224lists. Returns @code{NULL} on error. 3224lists. Returns @code{NULL} on error.
3225@end deftypefun 3225@end deftypefun
3226 3226
3227 3227
3228@deftypefun {const char *} gcry_sexp_nth_data (@w{const gcry_sexp_t @var{list}}, @w{int @var{number}}, @w{size_t *@var{datalen}}) 3228@deftypefun {const char *} gcry_sexp_nth_data (@w{const gcry_sexp_t @var{list}}, @w{int @var{number}}, @w{size_t *@var{datalen}})
3229 3229
3230This function is used to get data from a @var{list}. A pointer to the 3230This function is used to get data from a @var{list}. A pointer to the
3231actual data with index @var{number} is returned and the length of this 3231actual data with index @var{number} is returned and the length of this
3232data will be stored to @var{datalen}. If there is no data at the given 3232data will be stored to @var{datalen}. If there is no data at the given
3233index or the index represents another list, @code{NULL} is returned. 3233index or the index represents another list, @code{NULL} is returned.
3234@strong{Take care:} The returned pointer is valid as long as @var{list} is 3234@strong{Take care:} The returned pointer is valid as long as @var{list} is
3235not modified or released. 3235not modified or released.
3236 3236
3237@noindent 3237@noindent
3238Here is an example on how to extract and print the surname (Meier) from 3238Here is an example on how to extract and print the surname (Meier) from
3239the S-expression @samp{(Name Otto Meier (address Burgplatz 3))}: 3239the S-expression @samp{(Name Otto Meier (address Burgplatz 3))}:
3240 3240
3241@example 3241@example
3242size_t len; 3242size_t len;
3243const char *name; 3243const char *name;
3244 3244
3245name = gcry_sexp_nth_data (list, 2, &len); 3245name = gcry_sexp_nth_data (list, 2, &len);
3246printf ("my name is %.*s\n", (int)len, name); 3246printf ("my name is %.*s\n", (int)len, name);
3247@end example 3247@end example
3248@end deftypefun 3248@end deftypefun
3249 3249
3250@deftypefun gcry_mpi_t gcry_sexp_nth_mpi (@w{gcry_sexp_t @var{list}}, @w{int @var{number}}, @w{int @var{mpifmt}}) 3250@deftypefun gcry_mpi_t gcry_sexp_nth_mpi (@w{gcry_sexp_t @var{list}}, @w{int @var{number}}, @w{int @var{mpifmt}})
3251 3251
3252This function is used to get and convert data from a @var{list}. This 3252This function is used to get and convert data from a @var{list}. This
3253data is assumed to be an MPI stored in the format described by 3253data is assumed to be an MPI stored in the format described by
3254@var{mpifmt} and returned as a standard @acronym{Libgcrypt} MPI. The caller must 3254@var{mpifmt} and returned as a standard @acronym{Libgcrypt} MPI. The caller must
3255release this returned value using @code{gcry_mpi_release}. If there is 3255release this returned value using @code{gcry_mpi_release}. If there is
3256no data at the given index, the index represents a list or the value 3256no data at the given index, the index represents a list or the value
3257can't be converted to an MPI, @code{NULL} is returned. 3257can't be converted to an MPI, @code{NULL} is returned.
3258@end deftypefun 3258@end deftypefun
3259 3259
3260 3260
3261@c ********************************************************** 3261@c **********************************************************
3262@c ******************* MPIs ******** *********************** 3262@c ******************* MPIs ******** ***********************
3263@c ********************************************************** 3263@c **********************************************************
3264@node MPI library 3264@node MPI library
3265@chapter MPI library 3265@chapter MPI library
3266 3266
3267@menu 3267@menu
3268* Data types:: MPI related data types. 3268* Data types:: MPI related data types.
3269* Basic functions:: First steps with MPI numbers. 3269* Basic functions:: First steps with MPI numbers.
3270* MPI formats:: External representation of MPIs. 3270* MPI formats:: External representation of MPIs.
3271* Calculations:: Performing MPI calculations. 3271* Calculations:: Performing MPI calculations.
3272* Comparisons:: How to compare MPI values. 3272* Comparisons:: How to compare MPI values.
3273* Bit manipulations:: How to access single bits of MPI values. 3273* Bit manipulations:: How to access single bits of MPI values.
3274* Miscellaneous:: Miscellaneous MPI functions. 3274* Miscellaneous:: Miscellaneous MPI functions.
3275@end menu 3275@end menu
3276 3276
3277Public key cryptography is based on mathematics with large numbers. To 3277Public key cryptography is based on mathematics with large numbers. To
3278implement the public key functions, a library for handling these large 3278implement the public key functions, a library for handling these large
3279numbers is required. Because of the general usefulness of such a 3279numbers is required. Because of the general usefulness of such a
3280library, its interface is exposed by @acronym{Libgcrypt}. The implementation is 3280library, its interface is exposed by @acronym{Libgcrypt}. The implementation is
3281based on an old release of GNU Multi-Precision Library (GMP) but in the 3281based on an old release of GNU Multi-Precision Library (GMP) but in the
3282meantime heavily modified and stripped down to what is required for 3282meantime heavily modified and stripped down to what is required for
3283cryptography. For a lot of CPUs, high performance assembler 3283cryptography. For a lot of CPUs, high performance assembler
3284implementations of some very low level functions are used to gain much 3284implementations of some very low level functions are used to gain much
3285better performance than with the standard C implementation. 3285better performance than with the standard C implementation.
3286 3286
3287@noindent 3287@noindent
3288In the context of @acronym{Libgcrypt} and in most other applications, these large 3288In the context of @acronym{Libgcrypt} and in most other applications, these large
3289numbers are called MPIs (multi-precision-integers). 3289numbers are called MPIs (multi-precision-integers).
3290 3290
3291@node Data types 3291@node Data types
3292@section Data types 3292@section Data types
3293 3293
3294@deftp {Data type} gcry_mpi_t 3294@deftp {Data type} gcry_mpi_t
3295The @code{gcry_mpi_t} type represents an object to hold an MPI. 3295The @code{gcry_mpi_t} type represents an object to hold an MPI.
3296@end deftp 3296@end deftp
3297 3297
3298@node Basic functions 3298@node Basic functions
3299@section Basic functions 3299@section Basic functions
3300 3300
3301@noindent 3301@noindent
3302To work with MPIs, storage must be allocated and released for the 3302To work with MPIs, storage must be allocated and released for the
3303numbers. This can be done with one of these functions: 3303numbers. This can be done with one of these functions:
3304 3304
3305@deftypefun gcry_mpi_t gcry_mpi_new (@w{unsigned int @var{nbits}}) 3305@deftypefun gcry_mpi_t gcry_mpi_new (@w{unsigned int @var{nbits}})
3306 3306
3307Allocate a new MPI object, initialize it to 0 and initially allocate 3307Allocate a new MPI object, initialize it to 0 and initially allocate
3308enough memory for a number of at least @var{nbits}. This pre-allocation is 3308enough memory for a number of at least @var{nbits}. This pre-allocation is
3309only a small performance issue and not actually necessary because 3309only a small performance issue and not actually necessary because
3310@acronym{Libgcrypt} automatically re-allocates the required memory. 3310@acronym{Libgcrypt} automatically re-allocates the required memory.
3311@end deftypefun 3311@end deftypefun
3312 3312
3313@deftypefun gcry_mpi_t gcry_mpi_snew (@w{unsigned int @var{nbits}}) 3313@deftypefun gcry_mpi_t gcry_mpi_snew (@w{unsigned int @var{nbits}})
3314 3314
3315This is identical to @code{gcry_mpi_new} but allocates the MPI in the so 3315This is identical to @code{gcry_mpi_new} but allocates the MPI in the so
3316called "secure memory" which in turn will take care that all derived 3316called "secure memory" which in turn will take care that all derived
3317values will also be stored in this "secure memory". Use this for highly 3317values will also be stored in this "secure memory". Use this for highly
3318confidential data like private key parameters. 3318confidential data like private key parameters.
3319@end deftypefun 3319@end deftypefun
3320 3320
3321@deftypefun gcry_mpi_t gcry_mpi_copy (@w{const gcry_mpi_t @var{a}}) 3321@deftypefun gcry_mpi_t gcry_mpi_copy (@w{const gcry_mpi_t @var{a}})
3322 3322
3323Create a new MPI as the exact copy of @var{a}. 3323Create a new MPI as the exact copy of @var{a}.
3324@end deftypefun 3324@end deftypefun
3325 3325
3326 3326
3327@deftypefun void gcry_mpi_release (@w{gcry_mpi_t @var{a}}) 3327@deftypefun void gcry_mpi_release (@w{gcry_mpi_t @var{a}})
3328 3328
3329Release the MPI @var{a} and free all associated resources. Passing 3329Release the MPI @var{a} and free all associated resources. Passing
3330@code{NULL} is allowed and ignored. When a MPI stored in the "secure 3330@code{NULL} is allowed and ignored. When a MPI stored in the "secure
3331memory" is released, that memory gets wiped out immediately. 3331memory" is released, that memory gets wiped out immediately.
3332@end deftypefun 3332@end deftypefun
3333 3333
3334@noindent 3334@noindent
3335The simplest operations are used to assign a new value to an MPI: 3335The simplest operations are used to assign a new value to an MPI:
3336 3336
3337@deftypefun gcry_mpi_t gcry_mpi_set (@w{gcry_mpi_t @var{w}}, @w{const gcry_mpi_t @var{u}}) 3337@deftypefun gcry_mpi_t gcry_mpi_set (@w{gcry_mpi_t @var{w}}, @w{const gcry_mpi_t @var{u}})
3338 3338
3339Assign the value of @var{u} to @var{w} and return @var{w}. If 3339Assign the value of @var{u} to @var{w} and return @var{w}. If
3340@code{NULL} is passed for @var{w}, a new MPI is allocated, set to the 3340@code{NULL} is passed for @var{w}, a new MPI is allocated, set to the
3341value of @var{u} and returned. 3341value of @var{u} and returned.
3342@end deftypefun 3342@end deftypefun
3343 3343
3344@deftypefun gcry_mpi_t gcry_mpi_set_ui (@w{gcry_mpi_t @var{w}}, @w{unsigned long @var{u}}) 3344@deftypefun gcry_mpi_t gcry_mpi_set_ui (@w{gcry_mpi_t @var{w}}, @w{unsigned long @var{u}})
3345 3345
3346Assign the value of @var{u} to @var{w} and return @var{w}. If 3346Assign the value of @var{u} to @var{w} and return @var{w}. If
3347@code{NULL} is passed for @var{w}, a new MPI is allocated, set to the 3347@code{NULL} is passed for @var{w}, a new MPI is allocated, set to the
3348value of @var{u} and returned. This function takes an @code{unsigned 3348value of @var{u} and returned. This function takes an @code{unsigned
3349int} as type for @var{u} and thus it is only possible to set @var{w} to 3349int} as type for @var{u} and thus it is only possible to set @var{w} to
3350small values (usually up to the word size of the CPU). 3350small values (usually up to the word size of the CPU).
3351@end deftypefun 3351@end deftypefun
3352 3352
3353@deftypefun void gcry_mpi_swap (@w{gcry_mpi_t @var{a}}, @w{gcry_mpi_t @var{b}}) 3353@deftypefun void gcry_mpi_swap (@w{gcry_mpi_t @var{a}}, @w{gcry_mpi_t @var{b}})
3354 3354
3355Swap the values of @var{a} and @var{b}. 3355Swap the values of @var{a} and @var{b}.
3356@end deftypefun 3356@end deftypefun
3357 3357
3358@node MPI formats 3358@node MPI formats
3359@section MPI formats 3359@section MPI formats
3360 3360
3361@noindent 3361@noindent
3362The following functions are used to convert between an external 3362The following functions are used to convert between an external
3363representation of an MPI and the internal one of @acronym{Libgcrypt}. 3363representation of an MPI and the internal one of @acronym{Libgcrypt}.
3364 3364
3365@deftypefun int gcry_mpi_scan (@w{gcry_mpi_t *@var{r_mpi}}, @w{enum gcry_mpi_format @var{format}}, @w{const void *@var{buffer}}, @w{size_t @var{buflen}}, @w{size_t *@var{nscanned}}) 3365@deftypefun int gcry_mpi_scan (@w{gcry_mpi_t *@var{r_mpi}}, @w{enum gcry_mpi_format @var{format}}, @w{const void *@var{buffer}}, @w{size_t @var{buflen}}, @w{size_t *@var{nscanned}})
3366 3366
3367Convert the external representation of an integer stored in @var{buffer} 3367Convert the external representation of an integer stored in @var{buffer}
3368with a length of @var{buflen} into a newly created MPI returned which 3368with a length of @var{buflen} into a newly created MPI returned which
3369will be stored at the address of @var{r_mpi}. For certain formats the 3369will be stored at the address of @var{r_mpi}. For certain formats the
3370length argument is not required and may be passed as @code{0}. After a 3370length argument is not required and may be passed as @code{0}. After a
3371successful operation the variable @var{nscanned} receives the number of 3371successful operation the variable @var{nscanned} receives the number of
3372bytes actually scanned unless @var{nscanned} was given as 3372bytes actually scanned unless @var{nscanned} was given as
3373@code{NULL}. @var{format} describes the format of the MPI as stored in 3373@code{NULL}. @var{format} describes the format of the MPI as stored in
3374@var{buffer}: 3374@var{buffer}:
3375 3375
3376@table @code 3376@table @code
3377@item GCRYMPI_FMT_STD 3377@item GCRYMPI_FMT_STD
33782-complement stored without a length header. 33782-complement stored without a length header.
3379 3379
3380@item GCRYMPI_FMT_PGP 3380@item GCRYMPI_FMT_PGP
3381As used by OpenPGP (only defined as unsigned). This is basically 3381As used by OpenPGP (only defined as unsigned). This is basically
3382@code{GCRYMPI_FMT_STD} with a 2 byte big endian length header. 3382@code{GCRYMPI_FMT_STD} with a 2 byte big endian length header.
3383 3383
3384@item GCRYMPI_FMT_SSH 3384@item GCRYMPI_FMT_SSH
3385As used in the Secure Shell protocol. This is @code{GCRYMPI_FMT_STD} 3385As used in the Secure Shell protocol. This is @code{GCRYMPI_FMT_STD}
3386with a 4 byte big endian header. 3386with a 4 byte big endian header.
3387 3387
3388@item GCRYMPI_FMT_HEX 3388@item GCRYMPI_FMT_HEX
3389Stored as a C style string with each byte of the MPI encoded as 2 hex 3389Stored as a C style string with each byte of the MPI encoded as 2 hex
3390digits. 3390digits.
3391 3391
3392@item GCRYMPI_FMT_USG 3392@item GCRYMPI_FMT_USG
3393Simple unsigned integer. 3393Simple unsigned integer.
3394@end table 3394@end table
3395 3395
3396@noindent 3396@noindent
3397Note, that all of the above formats store the integer in big-endian 3397Note, that all of the above formats store the integer in big-endian
3398format (MSB first). 3398format (MSB first).
3399@end deftypefun 3399@end deftypefun
3400 3400
3401 3401
3402@deftypefun int 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}}) 3402@deftypefun int 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}})
3403 3403
3404Convert the MPI @var{a} into an external representation described by 3404Convert the MPI @var{a} into an external representation described by
3405@var{format} (see above) and store it in the provided @var{buffer} 3405@var{format} (see above) and store it in the provided @var{buffer}
3406which has a usable length of at least the @var{buflen} bytes. If 3406which has a usable length of at least the @var{buflen} bytes. If
3407@var{nwritten} is not NULL, it will receive the number of bytes 3407@var{nwritten} is not NULL, it will receive the number of bytes
3408actually stored in @var{buffer} after a successful operation. 3408actually stored in @var{buffer} after a successful operation.
3409@end deftypefun 3409@end deftypefun
3410 3410
3411@deftypefun int 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}}) 3411@deftypefun int 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}})
3412 3412
3413Convert the MPI @var{a} into an external representation described by 3413Convert the MPI @var{a} into an external representation described by
3414@var{format} (see above) and store it in a newly allocated buffer which 3414@var{format} (see above) and store it in a newly allocated buffer which
3415address will be stored in the variable @var{buffer} points to. The 3415address will be stored in the variable @var{buffer} points to. The
3416number of bytes stored in this buffer will be stored in the variable 3416number of bytes stored in this buffer will be stored in the variable
3417@var{nbytes} points to, unless @var{nbytes} is @code{NULL}. 3417@var{nbytes} points to, unless @var{nbytes} is @code{NULL}.
3418@end deftypefun 3418@end deftypefun
3419 3419
3420@deftypefun void gcry_mpi_dump (@w{const gcry_mpi_t @var{a}}) 3420@deftypefun void gcry_mpi_dump (@w{const gcry_mpi_t @var{a}})
3421 3421
3422Dump the value of @var{a} in a format suitable for debugging to 3422Dump the value of @var{a} in a format suitable for debugging to
3423Libgcrypt's logging stream. Note that one leading space but no trailing 3423Libgcrypt's logging stream. Note that one leading space but no trailing
3424space or linefeed will be printed. It is okay to pass @code{NULL} for 3424space or linefeed will be printed. It is okay to pass @code{NULL} for
3425@var{a}. 3425@var{a}.
3426@end deftypefun 3426@end deftypefun
3427 3427
3428 3428
3429@node Calculations 3429@node Calculations
3430@section Calculations 3430@section Calculations
3431 3431
3432@noindent 3432@noindent
3433Basic arithmetic operations: 3433Basic arithmetic operations:
3434 3434
3435@deftypefun void gcry_mpi_add (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}}) 3435@deftypefun void gcry_mpi_add (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}})
3436 3436
3437@math{@var{w} = @var{u} + @var{v}}. 3437@math{@var{w} = @var{u} + @var{v}}.
3438@end deftypefun 3438@end deftypefun
3439 3439
3440 3440
3441@deftypefun void gcry_mpi_add_ui (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{v}}) 3441@deftypefun void gcry_mpi_add_ui (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{v}})
3442 3442
3443@math{@var{w} = @var{u} + @var{v}}. Note, that @var{v} is an unsigned integer. 3443@math{@var{w} = @var{u} + @var{v}}. Note, that @var{v} is an unsigned integer.
3444@end deftypefun 3444@end deftypefun
3445 3445
3446 3446
3447@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}}) 3447@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}})
3448 3448
3449@math{@var{w} = @var{u} + @var{v} \bmod @var{m}}. 3449@math{@var{w} = @var{u} + @var{v} \bmod @var{m}}.
3450@end deftypefun 3450@end deftypefun
3451 3451
3452@deftypefun void gcry_mpi_sub (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}}) 3452@deftypefun void gcry_mpi_sub (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}})
3453 3453
3454@math{@var{w} = @var{u} - @var{v}}. 3454@math{@var{w} = @var{u} - @var{v}}.
3455@end deftypefun 3455@end deftypefun
3456 3456
3457@deftypefun void gcry_mpi_sub_ui (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{v}}) 3457@deftypefun void gcry_mpi_sub_ui (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{v}})
3458 3458
3459@math{@var{w} = @var{u} - @var{v}}. @var{v} is an unsigned integer. 3459@math{@var{w} = @var{u} - @var{v}}. @var{v} is an unsigned integer.
3460@end deftypefun 3460@end deftypefun
3461 3461
3462@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}}) 3462@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}})
3463 3463
3464@math{@var{w} = @var{u} - @var{v} \bmod @var{m}}. 3464@math{@var{w} = @var{u} - @var{v} \bmod @var{m}}.
3465@end deftypefun 3465@end deftypefun
3466 3466
3467@deftypefun void gcry_mpi_mul (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}}) 3467@deftypefun void gcry_mpi_mul (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{gcry_mpi_t @var{v}})
3468 3468
3469@math{@var{w} = @var{u} * @var{v}}. 3469@math{@var{w} = @var{u} * @var{v}}.
3470@end deftypefun 3470@end deftypefun
3471 3471
3472@deftypefun void gcry_mpi_mul_ui (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{v}}) 3472@deftypefun void gcry_mpi_mul_ui (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{v}})
3473 3473
3474@math{@var{w} = @var{u} * @var{v}}. @var{v} is an unsigned integer. 3474@math{@var{w} = @var{u} * @var{v}}. @var{v} is an unsigned integer.
3475@end deftypefun 3475@end deftypefun
3476 3476
3477@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}}) 3477@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}})
3478 3478
3479@math{@var{w} = @var{u} * @var{v} \bmod @var{m}}. 3479@math{@var{w} = @var{u} * @var{v} \bmod @var{m}}.
3480@end deftypefun 3480@end deftypefun
3481 3481
3482@deftypefun void gcry_mpi_mul_2exp (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{e}}) 3482@deftypefun void gcry_mpi_mul_2exp (@w{gcry_mpi_t @var{w}}, @w{gcry_mpi_t @var{u}}, @w{unsigned long @var{e}})
3483 3483
3484@c FIXME: I am in need for a real TeX{info} guru: 3484@c FIXME: I am in need for a real TeX{info} guru:
3485@c I don't know why TeX can grok @var{e} here. 3485@c I don't know why TeX can grok @var{e} here.
3486@math{@var{w} = @var{u} * 2^e}. 3486@math{@var{w} = @var{u} * 2^e}.
3487@end deftypefun 3487@end deftypefun
3488 3488
3489@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}}) 3489@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}})
3490 3490
3491@math{@var{q} = @var{dividend} / @var{divisor}}, @math{@var{r} = 3491@math{@var{q} = @var{dividend} / @var{divisor}}, @math{@var{r} =
3492@var{dividend} \bmod @var{divisor}}. @var{q} and @var{r} may be passed 3492@var{dividend} \bmod @var{divisor}}. @var{q} and @var{r} may be passed
3493as @code{NULL}. @var{round} should be negative or 0. 3493as @code{NULL}. @var{round} should be negative or 0.
3494@end deftypefun 3494@end deftypefun
3495 3495
3496@deftypefun void gcry_mpi_mod (@w{gcry_mpi_t @var{r}}, @w{gcry_mpi_t @var{dividend}}, @w{gcry_mpi_t @var{divisor}}) 3496@deftypefun void gcry_mpi_mod (@w{gcry_mpi_t @var{r}}, @w{gcry_mpi_t @var{dividend}}, @w{gcry_mpi_t @var{divisor}})
3497 3497
3498@math{@var{r} = @var{dividend} \bmod @var{divisor}}. 3498@math{@var{r} = @var{dividend} \bmod @var{divisor}}.
3499@end deftypefun 3499@end deftypefun
3500 3500
3501@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}}) 3501@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}})
3502 3502
3503@c I don't know why TeX can grok @var{e} here. 3503@c I don't know why TeX can grok @var{e} here.
3504@math{@var{w} = @var{b}^e \bmod @var{m}}. 3504@math{@var{w} = @var{b}^e \bmod @var{m}}.
3505@end deftypefun 3505@end deftypefun
3506 3506
3507@deftypefun int gcry_mpi_gcd (@w{gcry_mpi_t @var{g}}, @w{gcry_mpi_t @var{a}}, @w{gcry_mpi_t @var{b}}) 3507@deftypefun int gcry_mpi_gcd (@w{gcry_mpi_t @var{g}}, @w{gcry_mpi_t @var{a}}, @w{gcry_mpi_t @var{b}})
3508 3508
3509Set @var{g} to the greatest common divisor of @var{a} and @var{b}. 3509Set @var{g} to the greatest common divisor of @var{a} and @var{b}.
3510Return true if the @var{g} is 1. 3510Return true if the @var{g} is 1.
3511@end deftypefun 3511@end deftypefun
3512 3512
3513@deftypefun int gcry_mpi_invm (@w{gcry_mpi_t @var{x}}, @w{gcry_mpi_t @var{a}}, @w{gcry_mpi_t @var{m}}) 3513@deftypefun int gcry_mpi_invm (@w{gcry_mpi_t @var{x}}, @w{gcry_mpi_t @var{a}}, @w{gcry_mpi_t @var{m}})
3514 3514
3515Set @var{x} to the multiplicative inverse of @math{@var{a} \bmod @var{m}}. 3515Set @var{x} to the multiplicative inverse of @math{@var{a} \bmod @var{m}}.
3516Return true if the inverse exists. 3516Return true if the inverse exists.
3517@end deftypefun 3517@end deftypefun
3518 3518
3519 3519
3520@node Comparisons 3520@node Comparisons
3521@section Comparisons 3521@section Comparisons
3522 3522
3523@noindent 3523@noindent
3524The next 2 functions are used to compare MPIs: 3524The next 2 functions are used to compare MPIs:
3525 3525
3526 3526
3527@deftypefun int gcry_mpi_cmp (@w{const gcry_mpi_t @var{u}}, @w{const gcry_mpi_t @var{v}}) 3527@deftypefun int gcry_mpi_cmp (@w{const gcry_mpi_t @var{u}}, @w{const gcry_mpi_t @var{v}})
3528 3528
3529Compare the big integer number @var{u} and @var{v} returning 0 for 3529Compare the big integer number @var{u} and @var{v} returning 0 for
3530equality, a positive value for @var{u} > @var{v} and a negative for 3530equality, a positive value for @var{u} > @var{v} and a negative for
3531@var{u} < @var{v}. 3531@var{u} < @var{v}.
3532@end deftypefun 3532@end deftypefun
3533 3533
3534@deftypefun int gcry_mpi_cmp_ui (@w{const gcry_mpi_t @var{u}}, @w{unsigned long @var{v}}) 3534@deftypefun int gcry_mpi_cmp_ui (@w{const gcry_mpi_t @var{u}}, @w{unsigned long @var{v}})
3535 3535
3536Compare the big integer number @var{u} with the unsigned integer @var{v} 3536Compare the big integer number @var{u} with the unsigned integer @var{v}
3537returning 0 for equality, a positive value for @var{u} > @var{v} and a 3537returning 0 for equality, a positive value for @var{u} > @var{v} and a
3538negative for @var{u} < @var{v}. 3538negative for @var{u} < @var{v}.
3539@end deftypefun 3539@end deftypefun
3540 3540
3541 3541
3542@node Bit manipulations 3542@node Bit manipulations
3543@section Bit manipulations 3543@section Bit manipulations
3544 3544
3545@noindent 3545@noindent
3546There are a couple of functions to get information on arbitrary bits 3546There are a couple of functions to get information on arbitrary bits
3547in an MPI and to set or clear them: 3547in an MPI and to set or clear them:
3548 3548
3549@deftypefun {unsigned int} gcry_mpi_get_nbits (@w{gcry_mpi_t @var{a}}) 3549@deftypefun {unsigned int} gcry_mpi_get_nbits (@w{gcry_mpi_t @var{a}})
3550 3550
3551Return the number of bits required to represent @var{a}. 3551Return the number of bits required to represent @var{a}.
3552@end deftypefun 3552@end deftypefun
3553 3553
3554@deftypefun int gcry_mpi_test_bit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}}) 3554@deftypefun int gcry_mpi_test_bit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3555 3555
3556Return true if bit number @var{n} (counting from 0) is set in @var{a}. 3556Return true if bit number @var{n} (counting from 0) is set in @var{a}.
3557@end deftypefun 3557@end deftypefun
3558 3558
3559@deftypefun void gcry_mpi_set_bit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}}) 3559@deftypefun void gcry_mpi_set_bit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3560 3560
3561Set bit number @var{n} in @var{a}. 3561Set bit number @var{n} in @var{a}.
3562@end deftypefun 3562@end deftypefun
3563 3563
3564@deftypefun void gcry_mpi_clear_bit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}}) 3564@deftypefun void gcry_mpi_clear_bit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3565 3565
3566Clear bit number @var{n} in @var{a}. 3566Clear bit number @var{n} in @var{a}.
3567@end deftypefun 3567@end deftypefun
3568 3568
3569@deftypefun void gcry_mpi_set_highbit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}}) 3569@deftypefun void gcry_mpi_set_highbit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3570 3570
3571Set bit number @var{n} in @var{a} and clear all bits greater than @var{n}. 3571Set bit number @var{n} in @var{a} and clear all bits greater than @var{n}.
3572@end deftypefun 3572@end deftypefun
3573 3573
3574@deftypefun void gcry_mpi_clear_highbit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}}) 3574@deftypefun void gcry_mpi_clear_highbit (@w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3575 3575
3576Clear bit number @var{n} in @var{a} and all bits greater than @var{n}. 3576Clear bit number @var{n} in @var{a} and all bits greater than @var{n}.
3577@end deftypefun 3577@end deftypefun
3578 3578
3579@deftypefun void gcry_mpi_rshift (@w{gcry_mpi_t @var{x}}, @w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}}) 3579@deftypefun void gcry_mpi_rshift (@w{gcry_mpi_t @var{x}}, @w{gcry_mpi_t @var{a}}, @w{unsigned int @var{n}})
3580 3580
3581Shift the value of @var{a} by @var{n} bits to the right and store the 3581Shift the value of @var{a} by @var{n} bits to the right and store the
3582result in @var{x}. 3582result in @var{x}.
3583@end deftypefun 3583@end deftypefun
3584 3584
3585@node Miscellaneous 3585@node Miscellaneous
3586@section Miscellanous 3586@section Miscellanous
3587 3587
3588@deftypefun gcry_mpi_t gcry_mpi_set_opaque (@w{gcry_mpi_t @var{a}}, @w{void *@var{p}}, @w{unsigned int @var{nbits}}) 3588@deftypefun gcry_mpi_t gcry_mpi_set_opaque (@w{gcry_mpi_t @var{a}}, @w{void *@var{p}}, @w{unsigned int @var{nbits}})
3589 3589
3590Store @var{nbits} of the value @var{p} points to in @var{a} and mark 3590Store @var{nbits} of the value @var{p} points to in @var{a} and mark
3591@var{a} as an opaque value (i.e. an value that can't be used for any 3591@var{a} as an opaque value (i.e. an value that can't be used for any
3592math calculation and is only used to store an arbitrary bit pattern in 3592math calculation and is only used to store an arbitrary bit pattern in
3593@var{a}). 3593@var{a}).
3594 3594
3595WARNING: Never use an opaque MPI for actual math operations. The only 3595WARNING: Never use an opaque MPI for actual math operations. The only
3596valid functions are gcry_mpi_get_opaque and gcry_mpi_release. Use 3596valid functions are gcry_mpi_get_opaque and gcry_mpi_release. Use
3597gcry_mpi_scan to convert a string of arbitrary bytes into an MPI. 3597gcry_mpi_scan to convert a string of arbitrary bytes into an MPI.
3598 3598
3599@end deftypefun 3599@end deftypefun
3600 3600
3601@deftypefun {void *} gcry_mpi_get_opaque (@w{gcry_mpi_t @var{a}}, @w{unsigned int *@var{nbits}}) 3601@deftypefun {void *} gcry_mpi_get_opaque (@w{gcry_mpi_t @var{a}}, @w{unsigned int *@var{nbits}})
3602 3602
3603Return a pointer to an opaque value stored in @var{a} and return its 3603Return a pointer to an opaque value stored in @var{a} and return its
3604size in @var{nbits}. Note, that the returned pointer is still owned by 3604size in @var{nbits}. Note, that the returned pointer is still owned by
3605@var{a} and that the function should never be used for an non-opaque 3605@var{a} and that the function should never be used for an non-opaque
3606MPI. 3606MPI.
3607@end deftypefun 3607@end deftypefun
3608 3608
3609@deftypefun void gcry_mpi_set_flag (@w{gcry_mpi_t @var{a}}, @w{enum gcry_mpi_flag @var{flag}}) 3609@deftypefun void gcry_mpi_set_flag (@w{gcry_mpi_t @var{a}}, @w{enum gcry_mpi_flag @var{flag}})
3610 3610
3611Set the @var{flag} for the MPI @var{a}. Currently only the flag 3611Set the @var{flag} for the MPI @var{a}. Currently only the flag
3612@code{GCRYMPI_FLAG_SECURE} is allowed to convert @var{a} into an MPI 3612@code{GCRYMPI_FLAG_SECURE} is allowed to convert @var{a} into an MPI
3613stored in "secure memory". 3613stored in "secure memory".
3614@end deftypefun 3614@end deftypefun
3615 3615
3616@deftypefun void gcry_mpi_clear_flag (@w{gcry_mpi_t @var{a}}, @w{enum gcry_mpi_flag @var{flag}}) 3616@deftypefun void gcry_mpi_clear_flag (@w{gcry_mpi_t @var{a}}, @w{enum gcry_mpi_flag @var{flag}})
3617 3617
3618Clear @var{flag} for the big integer @var{a}. Note, that this function is 3618Clear @var{flag} for the big integer @var{a}. Note, that this function is
3619currently useless as no flags are allowed. 3619currently useless as no flags are allowed.
3620@end deftypefun 3620@end deftypefun
3621 3621
3622@deftypefun int gcry_mpi_get_flag (@w{gcry_mpi_t @var{a}}, @w{enum gcry_mpi_flag @var{flag}}) 3622@deftypefun int gcry_mpi_get_flag (@w{gcry_mpi_t @var{a}}, @w{enum gcry_mpi_flag @var{flag}})
3623 3623
3624Return true when the @var{flag} is set for @var{a}. 3624Return true when the @var{flag} is set for @var{a}.
3625@end deftypefun 3625@end deftypefun
3626 3626
3627@deftypefun void gcry_mpi_randomize (@w{gcry_mpi_t @var{w}}, @w{unsigned int @var{nbits}}, @w{enum gcry_random_level @var{level}}) 3627@deftypefun void gcry_mpi_randomize (@w{gcry_mpi_t @var{w}}, @w{unsigned int @var{nbits}}, @w{enum gcry_random_level @var{level}})
3628 3628
3629Set the big integer @var{w} to a random value of @var{nbits}, using 3629Set the big integer @var{w} to a random value of @var{nbits}, using
3630random data quality of level @var{level}. In case @var{nbits} is not 3630random data quality of level @var{level}. In case @var{nbits} is not
3631a multiple of a byte, @var{nbits} is rounded up to the next byte 3631a multiple of a byte, @var{nbits} is rounded up to the next byte
3632boundary. 3632boundary.
3633@end deftypefun 3633@end deftypefun
3634 3634
3635@node Utilities 3635@node Utilities
3636@chapter Utilities 3636@chapter Utilities
3637 3637
3638@menu 3638@menu
3639* Memory allocation:: Functions related with memory allocation. 3639* Memory allocation:: Functions related with memory allocation.
3640@end menu 3640@end menu
3641 3641
3642@node Memory allocation 3642@node Memory allocation
3643@section Memory allocation 3643@section Memory allocation
3644 3644
3645@deftypefun void *gcry_malloc (size_t @var{n}) 3645@deftypefun void *gcry_malloc (size_t @var{n})
3646 3646
3647This function tries to allocate @var{n} bytes of memory. On success 3647This function tries to allocate @var{n} bytes of memory. On success
3648it returns a pointer to the memory area, in an out-of-core condition, 3648it returns a pointer to the memory area, in an out-of-core condition,
3649it returns NULL. 3649it returns NULL.
3650@end deftypefun 3650@end deftypefun
3651 3651
3652@deftypefun void *gcry_malloc_secure (size_t @var{n}) 3652@deftypefun void *gcry_malloc_secure (size_t @var{n})
3653Like @code{gcry_malloc}, but uses secure memory. 3653Like @code{gcry_malloc}, but uses secure memory.
3654@end deftypefun 3654@end deftypefun
3655 3655
3656@deftypefun void *gcry_calloc (size_t @var{n}) 3656@deftypefun void *gcry_calloc (size_t @var{n})
3657 3657
3658This function tries to allocate @var{n} bytes of cleared memory 3658This function tries to allocate @var{n} bytes of cleared memory
3659(i.e. memory that is initialized with zero bytes). On success it 3659(i.e. memory that is initialized with zero bytes). On success it
3660returns a pointer to the memory area, in an out-of-core condition, it 3660returns a pointer to the memory area, in an out-of-core condition, it
3661returns NULL. 3661returns NULL.
3662@end deftypefun 3662@end deftypefun
3663 3663
3664@deftypefun void *gcry_calloc_secure (size_t @var{n}) 3664@deftypefun void *gcry_calloc_secure (size_t @var{n})
3665Like @code{gcry_calloc}, but uses secure memory. 3665Like @code{gcry_calloc}, but uses secure memory.
3666@end deftypefun 3666@end deftypefun
3667 3667
3668@deftypefun void *gcry_realloc (void *@var{p}, size_t @var{n}) 3668@deftypefun void *gcry_realloc (void *@var{p}, size_t @var{n})
3669 3669
3670This function tries to resize the memory area pointed to by @var{p} to 3670This function tries to resize the memory area pointed to by @var{p} to
3671@var{n} bytes. On success it returns a pointer to the new memory 3671@var{n} bytes. On success it returns a pointer to the new memory
3672area, in an out-of-core condition, it returns NULL. Depending on 3672area, in an out-of-core condition, it returns NULL. Depending on
3673whether the memory pointed to by @var{p} is secure memory or not, 3673whether the memory pointed to by @var{p} is secure memory or not,
3674gcry_realloc tries to use secure memory as well. 3674gcry_realloc tries to use secure memory as well.
3675@end deftypefun 3675@end deftypefun
3676 3676
3677@deftypefun void gcry_free (void *@var{p}) 3677@deftypefun void gcry_free (void *@var{p})
3678Release the memory area pointed to by @var{p}. 3678Release the memory area pointed to by @var{p}.
3679@end deftypefun 3679@end deftypefun
3680 3680
3681@c ********************************************************** 3681@c **********************************************************
3682@c ******************* Appendices ************************* 3682@c ******************* Appendices *************************
3683@c ********************************************************** 3683@c **********************************************************
3684 3684
3685@include lgpl.texi 3685@include lgpl.texi
3686 3686
3687@include gpl.texi 3687@include gpl.texi
3688 3688
3689@node Concept Index 3689@node Concept Index
3690@unnumbered Concept Index 3690@unnumbered Concept Index
3691 3691
3692@printindex cp 3692@printindex cp
3693 3693
3694@node Function and Data Index 3694@node Function and Data Index
3695@unnumbered Function and Data Index 3695@unnumbered Function and Data Index
3696 3696
3697@printindex fn 3697@printindex fn
3698 3698
3699@bye 3699@bye
3700 3700
3701 /* Version check should be the very first gcry call because it 3701 /* Version check should be the very first gcry call because it
3702 makes sure that constructor functions are run. */ 3702 makes sure that constructor functions are run. */
3703 if (!gcry_check_version (GCRYPT_VERSION)) 3703 if (!gcry_check_version (GCRYPT_VERSION))
3704 die ("version mismatch\n"); 3704 die ("version mismatch\n");
3705 /* Many applications don't require secure memory, so they should 3705 /* Many applications don't require secure memory, so they should
3706 disable it right away. There won't be a problem unless one makes 3706 disable it right away. There won't be a problem unless one makes
3707 use of a feature which requires secure memory - in that case the 3707 use of a feature which requires secure memory - in that case the
3708 process would abort because the secmem is not initialized. */ 3708 process would abort because the secmem is not initialized. */
3709 gcry_control (GCRYCTL_DISABLE_SECMEM, 0); 3709 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
3710 3710
3711 /* .. add whatever initialization you want, but better don't make calls 3711 /* .. add whatever initialization you want, but better don't make calls
3712 to libgcrypt from more than one thread ... */ 3712 to libgcrypt from more than one thread ... */
3713 3713
3714 /* Tell Libgcrypt that initialization has completed. */ 3714 /* Tell Libgcrypt that initialization has completed. */
3715 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); 3715 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
3716 3716
3717 3717
3718If you require secure memory, this code should be used: 3718If you require secure memory, this code should be used:
3719 3719
3720 if (!gcry_check_version (GCRYPT_VERSION)) 3720 if (!gcry_check_version (GCRYPT_VERSION))
3721 die ("version mismatch\n"); 3721 die ("version mismatch\n");
3722 /* We don't want to see any warnings, e.g. because we have not yet 3722 /* We don't want to see any warnings, e.g. because we have not yet
3723 parsed options which might be used to suppress such warnings */ 3723 parsed options which might be used to suppress such warnings */
3724 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN); 3724 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
3725 3725
3726 /* ... */ 3726 /* ... */
3727 3727
3728 /* Allocate a pool of 16k secure memory. This also drops priviliges 3728 /* Allocate a pool of 16k secure memory. This also drops priviliges
3729 on some systems. */ 3729 on some systems. */
3730 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0); 3730 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
3731 3731
3732 /* It is now okay to let Libgcrypt complain when there was/is a problem 3732 /* It is now okay to let Libgcrypt complain when there was/is a problem
3733 with the secure memory. */ 3733 with the secure memory. */
3734 gcry_control (GCRYCTL_RESUME_SECMEM_WARN); 3734 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
3735 3735
3736 /* Tell Libgcrypt that initialization has completed. */ 3736 /* Tell Libgcrypt that initialization has completed. */
3737 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); 3737 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
3738 3738
3739 3739
3740This sounds a bit complicated but has the advantage that the caller 3740This sounds a bit complicated but has the advantage that the caller
3741must decide whether he wants secure memory or not - there is no 3741must decide whether he wants secure memory or not - there is no
3742default. 3742default.
3743 3743
3744It is important that this initialization is not done by a library but 3744It is important that this initialization is not done by a library but
3745in the application. The library might want to check for finished 3745in the application. The library might want to check for finished
3746initialization using: 3746initialization using:
3747 3747
3748 if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) 3748 if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P))
3749 return MYLIB_ERROR_LIBGCRYPT_NOT_INITIALIZED; 3749 return MYLIB_ERROR_LIBGCRYPT_NOT_INITIALIZED;
3750 3750
3751 3751
3752@c LocalWords: int HD 3752@c LocalWords: int HD
3753 3753
3754 3754
3755 3755
3756 3756