PageRenderTime 59ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/libgcrypt/libgcrypt-1.2.2/doc/gcrypt.info

https://bitbucket.org/lkalif/emerald-snowglobe
Unknown | 4698 lines | 3737 code | 961 blank | 0 comment | 0 complexity | 20c855858e53f0cbed0617a6e61e1af9 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0, BSD-3-Clause, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. This is gcrypt.info, produced by makeinfo version 4.7 from gcrypt.texi.
  2. This manual is for Libgcrypt (version 1.2.2, 29 July 2005), which is
  3. GNU's library of cryptographic building blocks.
  4. Copyright (C) 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
  5. Permission is granted to copy, distribute and/or modify this
  6. document under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version. The text of the
  9. license can be found in the section entitled "Copying".
  10. INFO-DIR-SECTION GNU Libraries
  11. START-INFO-DIR-ENTRY
  12. * libgcrypt: (gcrypt). Cryptographic function library.
  13. END-INFO-DIR-ENTRY
  14. 
  15. File: gcrypt.info, Node: Top, Next: Introduction, Up: (dir)
  16. The Libgcrypt Library
  17. *********************
  18. This manual is for Libgcrypt (version 1.2.2, 29 July 2005), which is
  19. GNU's library of cryptographic building blocks.
  20. Copyright (C) 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
  21. Permission is granted to copy, distribute and/or modify this
  22. document under the terms of the GNU General Public License as
  23. published by the Free Software Foundation; either version 2 of the
  24. License, or (at your option) any later version. The text of the
  25. license can be found in the section entitled "Copying".
  26. * Menu:
  27. * Introduction:: What is Libgcrypt.
  28. * Preparation:: What you should do before using the library.
  29. * Generalities:: General library functions and data types.
  30. * Handler Functions:: Working with handler functions.
  31. * Symmetric cryptography:: How to use symmetric cryptography.
  32. * Hashing:: How to use hashing.
  33. * Public Key cryptography (I):: How to use public key cryptography.
  34. * Public Key cryptography (II):: How to use public key cryptography, alternatively.
  35. * Random Numbers:: How to work with random numbers.
  36. * S-expressions:: How to manage S-expressions.
  37. * MPI library:: How to work with multi-precision-integers.
  38. * Utilities:: Utility functions.
  39. Appendices
  40. * Library Copying:: The GNU Lesser General Public License
  41. says how you can copy and share `Libgcrypt'.
  42. * Copying:: The GNU General Public License says how you
  43. can copy and share some parts of `Libgcrypt'.
  44. Indices
  45. * Concept Index:: Index of concepts and programs.
  46. * Function and Data Index:: Index of functions, variables and data types.
  47. --- The Detailed Node Listing ---
  48. Introduction
  49. * Getting Started:: How to use this manual.
  50. * Features:: A glance at Libgcrypt's features.
  51. * Overview:: Overview about the library.
  52. Preparation
  53. * Header:: What header file you need to include.
  54. * Building sources:: How to build sources using the library.
  55. * Building sources using Automake:: How to build sources with the help of Automake.
  56. * Initializing the library:: How to initialize the library.
  57. * Multi Threading:: How Libgcrypt can be used in a MT environment.
  58. Generalities
  59. * Controlling the library:: Controlling Libgcrypt's behavior.
  60. * Modules:: Description of extension modules.
  61. * Error Handling:: Error codes and such.
  62. Handler Functions
  63. * Progress handler:: Using a progress handler function.
  64. * Allocation handler:: Using special memory allocation functions.
  65. * Error handler:: Using error handler functions.
  66. * Logging handler:: Using a special logging function.
  67. Symmetric cryptography
  68. * Available ciphers:: List of ciphers supported by the library.
  69. * Cipher modules:: How to work with cipher modules.
  70. * Available cipher modes:: List of cipher modes supported by the library.
  71. * Working with cipher handles:: How to perform operations related to cipher handles.
  72. * General cipher functions:: General cipher functions independent of cipher handles.
  73. Hashing
  74. * Available hash algorithms:: List of hash algorithms supported by the library.
  75. * Hash algorithm modules:: How to work with hash algorithm modules.
  76. * Working with hash algorithms:: List of functions related to hashing.
  77. Public Key cryptography (I)
  78. * Used S-expressions:: Introduction into the used S-expression.
  79. * Available algorithms:: Algorithms supported by the library.
  80. * Public key modules:: How to work with public key modules.
  81. * Cryptographic Functions:: Functions for performing the cryptographic actions.
  82. * General public-key related Functions:: General functions, not implementing any cryptography.
  83. Public Key cryptography (II)
  84. * Available asymmetric algorithms:: List of algorithms supported by the library.
  85. * Working with sets of data:: How to work with sets of data.
  86. * Working with handles:: How to use handles.
  87. * Working with keys:: How to work with keys.
  88. * Using cryptographic functions:: How to perform cryptographic operations.
  89. * Handle-independent functions:: General functions independent of handles.
  90. Random Numbers
  91. * Quality of random numbers:: Libgcrypt uses different quality levels.
  92. * Retrieving random numbers:: How to retrieve random numbers.
  93. S-expressions
  94. * Data types for S-expressions:: Data types related with S-expressions.
  95. * Working with S-expressions:: How to work with S-expressions.
  96. MPI library
  97. * Data types:: MPI related data types.
  98. * Basic functions:: First steps with MPI numbers.
  99. * MPI formats:: External representation of MPIs.
  100. * Calculations:: Performing MPI calculations.
  101. * Comparisons:: How to compare MPI values.
  102. * Bit manipulations:: How to access single bits of MPI values.
  103. * Miscellaneous:: Miscellaneous MPI functions.
  104. Utilities
  105. * Memory allocation:: Functions related with memory allocation.
  106. 
  107. File: gcrypt.info, Node: Introduction, Next: Preparation, Prev: Top, Up: Top
  108. 1 Introduction
  109. **************
  110. `Libgcrypt' is a library providing cryptographic building blocks.
  111. * Menu:
  112. * Getting Started:: How to use this manual.
  113. * Features:: A glance at Libgcrypt's features.
  114. * Overview:: Overview about the library.
  115. 
  116. File: gcrypt.info, Node: Getting Started, Next: Features, Up: Introduction
  117. 1.1 Getting Started
  118. ===================
  119. This manual documents the `Libgcrypt' library application programming
  120. interface (API). All functions and data types provided by the library
  121. are explained.
  122. The reader is assumed to possess basic knowledge about applied
  123. cryptography.
  124. This manual can be used in several ways. If read from the beginning
  125. to the end, it gives a good introduction into the library and how it
  126. can be used in an application. Forward references are included where
  127. necessary. Later on, the manual can be used as a reference manual to
  128. get just the information needed about any particular interface of the
  129. library. Experienced programmers might want to start looking at the
  130. examples at the end of the manual, and then only read up those parts of
  131. the interface which are unclear.
  132. 
  133. File: gcrypt.info, Node: Features, Next: Overview, Prev: Getting Started, Up: Introduction
  134. 1.2 Features
  135. ============
  136. `Libgcrypt' might have a couple of advantages over other libraries doing
  137. a similar job.
  138. It's Free Software
  139. Anybody can use, modify, and redistribute it under the terms of
  140. the GNU Lesser General Public License (*note Library Copying::).
  141. Note, that some parts (which are not needed on a GNU or GNU/Linux
  142. system) are subject to the terms of the GNU General Public License
  143. (*note Copying::); please see the README file of the distribution
  144. for of list of these parts.
  145. It encapsulates the low level cryptography
  146. `Libgcrypt' provides a high level interface to cryptographic
  147. building blocks using an extendable and flexible API.
  148. 
  149. File: gcrypt.info, Node: Overview, Prev: Features, Up: Introduction
  150. 1.3 Overview
  151. ============
  152. The `Libgcrypt' library is fully thread-safe, where it makes sense to
  153. be thread-safe. An exception for thread-safety are some cryptographic
  154. functions that modify a certain context stored in handles. If the user
  155. really intents to use such functions from different threads on the same
  156. handle, he has to take care of the serialization of such functions
  157. himself. If not described otherwise, every function is thread-safe.
  158. Libgcrypt depends on the library `libgpg-error', which contains
  159. common error handling related code for GnuPG components.
  160. 
  161. File: gcrypt.info, Node: Preparation, Next: Generalities, Prev: Introduction, Up: Top
  162. 2 Preparation
  163. *************
  164. To use `Libgcrypt', you have to perform some changes to your sources
  165. and the build system. The necessary changes are small and explained in
  166. the following sections. At the end of this chapter, it is described
  167. how the library is initialized, and how the requirements of the library
  168. are verified.
  169. * Menu:
  170. * Header:: What header file you need to include.
  171. * Building sources:: How to build sources using the library.
  172. * Building sources using Automake:: How to build sources with the help of Automake.
  173. * Initializing the library:: How to initialize the library.
  174. * Multi Threading:: How Libgcrypt can be used in a MT environment.
  175. 
  176. File: gcrypt.info, Node: Header, Next: Building sources, Up: Preparation
  177. 2.1 Header
  178. ==========
  179. All interfaces (data types and functions) of the library are defined in
  180. the header file `gcrypt.h'. You must include this in all source files
  181. using the library, either directly or through some other header file,
  182. like this:
  183. #include <gcrypt.h>
  184. The name space of `Libgcrypt' is `gcry_*' for function and type
  185. names and `GCRY*' for other symbols. In addition the same name
  186. prefixes with one prepended underscore are reserved for internal use
  187. and should never be used by an application. Furthermore `libgpg-error'
  188. defines functions prefixed with `gpg_' and preprocessor symbols
  189. prefixed with `GPG_'. Note that Libgcrypt uses libgpg-error, which
  190. uses `gpg_err_*' as name space for function and type names and
  191. `GPG_ERR_*' for other symbols, including all the error codes.
  192. 
  193. File: gcrypt.info, Node: Building sources, Next: Building sources using Automake, Prev: Header, Up: Preparation
  194. 2.2 Building sources
  195. ====================
  196. If you want to compile a source file including the `gcrypt.h' header
  197. file, you must make sure that the compiler can find it in the directory
  198. hierarchy. This is accomplished by adding the path to the directory in
  199. which the header file is located to the compilers include file search
  200. path (via the `-I' option).
  201. However, the path to the include file is determined at the time the
  202. source is configured. To solve this problem, `Libgcrypt' ships with a
  203. small helper program `libgcrypt-config' that knows the path to the
  204. include file and other configuration options. The options that need to
  205. be added to the compiler invocation at compile time are output by the
  206. `--cflags' option to `libgcrypt-config'. The following example shows
  207. how it can be used at the command line:
  208. gcc -c foo.c `libgcrypt-config --cflags`
  209. Adding the output of `libgcrypt-config --cflags' to the compilers
  210. command line will ensure that the compiler can find the `Libgcrypt'
  211. header file.
  212. A similar problem occurs when linking the program with the library.
  213. Again, the compiler has to find the library files. For this to work,
  214. the path to the library files has to be added to the library search path
  215. (via the `-L' option). For this, the option `--libs' to
  216. `libgcrypt-config' can be used. For convenience, this option also
  217. outputs all other options that are required to link the program with
  218. the `Libgcrypt' libraries (in particular, the `-lgcrypt' option). The
  219. example shows how to link `foo.o' with the `Libgcrypt' library to a
  220. program `foo'.
  221. gcc -o foo foo.o `libgcrypt-config --libs`
  222. Of course you can also combine both examples to a single command by
  223. specifying both options to `libgcrypt-config':
  224. gcc -o foo foo.c `libgcrypt-config --cflags --libs`
  225. 
  226. File: gcrypt.info, Node: Building sources using Automake, Next: Initializing the library, Prev: Building sources, Up: Preparation
  227. 2.3 Building sources using Automake
  228. ===================================
  229. It is much easier if you use GNU Automake instead of writing your own
  230. Makefiles. If you do that you do not have to worry about finding and
  231. invoking the `libgcrypt-config' script at all. Libgcrypt provides an
  232. extension to Automake that does all the work for you.
  233. -- Macro: AM_PATH_LIBGCRYPT ([MINIMUM-VERSION], [ACTION-IF-FOUND],
  234. [ACTION-IF-NOT-FOUND])
  235. Check whether Libgcrypt (at least version MINIMUM-VERSION, if
  236. given) exists on the host system. If it is found, execute
  237. ACTION-IF-FOUND, otherwise do ACTION-IF-NOT-FOUND, if given.
  238. Additionally, the function defines `LIBGCRYPT_CFLAGS' to the flags
  239. needed for compilation of the program to find the `gcrypt.h'
  240. header file, and `LIBGCRYPT_LIBS' to the linker flags needed to
  241. link the program to the Libgcrypt library.
  242. You can use the defined Autoconf variables like this in your
  243. `Makefile.am':
  244. AM_CPPFLAGS = $(LIBGCRYPT_CFLAGS)
  245. LDADD = $(LIBGCRYPT_LIBS)
  246. 
  247. File: gcrypt.info, Node: Initializing the library, Next: Multi Threading, Prev: Building sources using Automake, Up: Preparation
  248. 2.4 Initializing the library
  249. ============================
  250. It is often desirable to check that the version of `Libgcrypt' used is
  251. indeed one which fits all requirements. Even with binary compatibility
  252. new features may have been introduced but due to problem with the
  253. dynamic linker an old version is actually used. So you may want to
  254. check that the version is okay right after program startup.
  255. -- Function: const char *gcry_check_version (const char *REQ_VERSION)
  256. The function `gcry_check_version' has three purposes. It can be
  257. used to retrieve the version number of the library. In addition it
  258. can verify that the version number is higher than a certain
  259. required version number.
  260. In either case, the function initializes some sub-systems, and for
  261. this reason alone it must be invoked early in your program, before
  262. you make use of the other functions of Libgcrypt.
  263. 
  264. File: gcrypt.info, Node: Multi Threading, Prev: Initializing the library, Up: Preparation
  265. 2.5 Multi Threading
  266. ===================
  267. As mentioned earlier, the `Libgcrypt' library is thread-safe if you
  268. adhere to the following requirements:
  269. * If your application is multi-threaded, you must set the thread
  270. support callbacks with the `GCRYCTL_SET_THREAD_CBS' command
  271. *before* any other function in the library.
  272. This is easy enough if you are indeed writing an application using
  273. Libgcrypt. It is rather problematic if you are writing a library
  274. instead. Here are some tips what to do if you are writing a
  275. library:
  276. If your library requires a certain thread package, just initialize
  277. Libgcrypt to use this thread package. If your library supports
  278. multiple thread packages, but needs to be configured, you will
  279. have to implement a way to determine which thread package the
  280. application wants to use with your library anyway. Then configure
  281. Libgcrypt to use this thread package.
  282. If your library is fully reentrant without any special support by a
  283. thread package, then you are lucky indeed. Unfortunately, this
  284. does not relieve you from doing either of the two above, or use a
  285. third option. The third option is to let the application
  286. initialize Libgcrypt for you. Then you are not using Libgcrypt
  287. transparently, though.
  288. As if this was not difficult enough, a conflict may arise if two
  289. libraries try to initialize Libgcrypt independently of each
  290. others, and both such libraries are then linked into the same
  291. application. To make it a bit simpler for you, this will probably
  292. work, but only if both libraries have the same requirement for the
  293. thread package. This is currently only supported for the
  294. non-threaded case, GNU Pth and pthread. Support for more thread
  295. packages is easy to add, so contact us if you require it.
  296. * The function `gcry_check_version' must be called before any other
  297. function in the library, except the `GCRYCTL_SET_THREAD_CBS'
  298. command (called via the `gcry_control' function), because it
  299. initializes the thread support subsystem in Libgcrypt. To achieve
  300. this in multi-threaded programs, you must synchronize the memory
  301. with respect to other threads that also want to use Libgcrypt.
  302. For this, it is sufficient to call `gcry_check_version' before
  303. creating the other threads using Libgcrypt(1).
  304. * As with the function `gpg_strerror', `gcry_strerror' is not
  305. thread safe. You have to use `gpg_strerror_r' instead.
  306. Libgcrypt contains convenient macros, which define the necessary
  307. thread callbacks for PThread and for GNU Pth:
  308. `GCRY_THREAD_OPTION_PTH_IMPL'
  309. This macro defines the following (static) symbols: gcry_pth_init,
  310. gcry_pth_mutex_init, gcry_pth_mutex_destroy, gcry_pth_mutex_lock,
  311. gcry_pth_mutex_unlock, gcry_pth_read, gcry_pth_write,
  312. gcry_pth_select, gcry_pth_waitpid, gcry_pth_accept,
  313. gcry_pth_connect, gcry_threads_pth.
  314. After including this macro, gcry_control() shall be used with a
  315. command of GCRYCTL_SET_THREAD_CBS in order to register the thread
  316. callback structure named "gcry_threads_pth".
  317. `GCRY_THREAD_OPTION_PTHREAD_IMPL'
  318. This macro defines the following (static) symbols:
  319. gcry_pthread_mutex_init, gcry_pthread_mutex_destroy,
  320. gcry_mutex_lock, gcry_mutex_unlock, gcry_threads_pthread.
  321. After including this macro, gcry_control() shall be used with a
  322. command of GCRYCTL_SET_THREAD_CBS in order to register the thread
  323. callback structure named "gcry_threads_pthread".
  324. Note that these macros need to be terminated with a semicolon. Keep
  325. in mind that these are convenient macros for C programmers; C++
  326. programmers might have to wrap these macros in an "extern C" body.
  327. ---------- Footnotes ----------
  328. (1) At least this is true for POSIX threads, as `pthread_create' is
  329. a function that synchronizes memory with respects to other threads.
  330. There are many functions which have this property, a complete list can
  331. be found in POSIX, IEEE Std 1003.1-2003, Base Definitions, Issue 6, in
  332. the definition of the term "Memory Synchronization". For other thread
  333. packages, more relaxed or more strict rules may apply.
  334. 
  335. File: gcrypt.info, Node: Generalities, Next: Handler Functions, Prev: Preparation, Up: Top
  336. 3 Generalities
  337. **************
  338. * Menu:
  339. * Controlling the library:: Controlling Libgcrypt's behavior.
  340. * Modules:: Description of extension modules.
  341. * Error Handling:: Error codes and such.
  342. 
  343. File: gcrypt.info, Node: Controlling the library, Next: Modules, Up: Generalities
  344. 3.1 Controlling the library
  345. ===========================
  346. -- Function: gcry_error_t gcry_control (enum gcry_ctl_cmds CMD, ...)
  347. This function can be used to influence the general behavior of
  348. Libgcrypt in several ways. Depending on CMD, more arguments can
  349. or have to be provided.
  350. 
  351. File: gcrypt.info, Node: Modules, Next: Error Handling, Prev: Controlling the library, Up: Generalities
  352. 3.2 Modules
  353. ===========
  354. Libgcrypt supports the use of `extension modules', which implement
  355. algorithms in addition to those already built into the library directly.
  356. -- Data type: gcry_module_t
  357. This data type represents a `module'.
  358. Functions registering modules provided by the user take a `module
  359. specification structure' as input and return a value of `gcry_module_t'
  360. and an ID that is unique in the modules' category. This ID can be used
  361. to reference the newly registered module. After registering a module
  362. successfully, the new functionality should be able to be used through
  363. the normal functions provided by Libgcrypt until it is unregistered
  364. again.
  365. 
  366. File: gcrypt.info, Node: Error Handling, Prev: Modules, Up: Generalities
  367. 3.3 Error Handling
  368. ==================
  369. Many functions in Libgcrypt can return an error if they fail. For this
  370. reason, the application should always catch the error condition and
  371. take appropriate measures, for example by releasing the resources and
  372. passing the error up to the caller, or by displaying a descriptive
  373. message to the user and cancelling the operation.
  374. Some error values do not indicate a system error or an error in the
  375. operation, but the result of an operation that failed properly. For
  376. example, if you try to decrypt a tempered message, the decryption will
  377. fail. Another error value actually means that the end of a data buffer
  378. or list has been reached. The following descriptions explain for many
  379. error codes what they mean usually. Some error values have specific
  380. meanings if returned by a certain functions. Such cases are described
  381. in the documentation of those functions.
  382. Libgcrypt uses the `libgpg-error' library. This allows to share the
  383. error codes with other components of the GnuPG system, and thus pass
  384. error values transparently from the crypto engine, or some helper
  385. application of the crypto engine, to the user. This way no information
  386. is lost. As a consequence, Libgcrypt does not use its own identifiers
  387. for error codes, but uses those provided by `libgpg-error'. They
  388. usually start with `GPG_ERR_'.
  389. However, Libgcrypt does provide aliases for the functions defined in
  390. libgpg-error, which might be preferred for name space consistency.
  391. Most functions in Libgcrypt return an error code in the case of
  392. failure. For this reason, the application should always catch the
  393. error condition and take appropriate measures, for example by releasing
  394. the resources and passing the error up to the caller, or by displaying
  395. a descriptive message to the user and canceling the operation.
  396. Some error values do not indicate a system error or an error in the
  397. operation, but the result of an operation that failed properly.
  398. GnuPG components, including Libgcrypt, use an extra library named
  399. libgpg-error to provide a common error handling scheme. For more
  400. information on libgpg-error, see the according manual.
  401. * Menu:
  402. * Error Values:: The error value and what it means.
  403. * Error Sources:: A list of important error sources.
  404. * Error Codes:: A list of important error codes.
  405. * Error Strings:: How to get a descriptive string from a value.
  406. 
  407. File: gcrypt.info, Node: Error Values, Next: Error Sources, Up: Error Handling
  408. 3.3.1 Error Values
  409. ------------------
  410. -- Data type: gcry_err_code_t
  411. The `gcry_err_code_t' type is an alias for the `libgpg-error' type
  412. `gpg_err_code_t'. The error code indicates the type of an error,
  413. or the reason why an operation failed.
  414. A list of important error codes can be found in the next section.
  415. -- Data type: gcry_err_source_t
  416. The `gcry_err_source_t' type is an alias for the `libgpg-error'
  417. type `gpg_err_source_t'. The error source has not a precisely
  418. defined meaning. Sometimes it is the place where the error
  419. happened, sometimes it is the place where an error was encoded
  420. into an error value. Usually the error source will give an
  421. indication to where to look for the problem. This is not always
  422. true, but it is attempted to achieve this goal.
  423. A list of important error sources can be found in the next section.
  424. -- Data type: gcry_error_t
  425. The `gcry_error_t' type is an alias for the `libgpg-error' type
  426. `gpg_error_t'. An error value like this has always two
  427. components, an error code and an error source. Both together form
  428. the error value.
  429. Thus, the error value can not be directly compared against an error
  430. code, but the accessor functions described below must be used.
  431. However, it is guaranteed that only 0 is used to indicate success
  432. (`GPG_ERR_NO_ERROR'), and that in this case all other parts of the
  433. error value are set to 0, too.
  434. Note that in Libgcrypt, the error source is used purely for
  435. diagnostic purposes. Only the error code should be checked to test
  436. for a certain outcome of a function. The manual only documents the
  437. error code part of an error value. The error source is left
  438. unspecified and might be anything.
  439. -- Function: gcry_err_code_t gcry_err_code (gcry_error_t ERR)
  440. The static inline function `gcry_err_code' returns the
  441. `gcry_err_code_t' component of the error value ERR. This function
  442. must be used to extract the error code from an error value in
  443. order to compare it with the `GPG_ERR_*' error code macros.
  444. -- Function: gcry_err_source_t gcry_err_source (gcry_error_t ERR)
  445. The static inline function `gcry_err_source' returns the
  446. `gcry_err_source_t' component of the error value ERR. This
  447. function must be used to extract the error source from an error
  448. value in order to compare it with the `GPG_ERR_SOURCE_*' error
  449. source macros.
  450. -- Function: gcry_error_t gcry_err_make (gcry_err_source_t SOURCE,
  451. gcry_err_code_t CODE)
  452. The static inline function `gcry_err_make' returns the error value
  453. consisting of the error source SOURCE and the error code CODE.
  454. This function can be used in callback functions to construct an
  455. error value to return it to the library.
  456. -- Function: gcry_error_t gcry_error (gcry_err_code_t CODE)
  457. The static inline function `gcry_error' returns the error value
  458. consisting of the default error source and the error code CODE.
  459. For GCRY applications, the default error source is
  460. `GPG_ERR_SOURCE_USER_1'. You can define `GCRY_ERR_SOURCE_DEFAULT'
  461. before including `gcrypt.h' to change this default.
  462. This function can be used in callback functions to construct an
  463. error value to return it to the library.
  464. The `libgpg-error' library provides error codes for all system error
  465. numbers it knows about. If ERR is an unknown error number, the error
  466. code `GPG_ERR_UNKNOWN_ERRNO' is used. The following functions can be
  467. used to construct error values from system errno numbers.
  468. -- Function: gcry_error_t gcry_err_make_from_errno
  469. (gcry_err_source_t SOURCE, int ERR)
  470. The function `gcry_err_make_from_errno' is like `gcry_err_make',
  471. but it takes a system error like `errno' instead of a
  472. `gcry_err_code_t' error code.
  473. -- Function: gcry_error_t gcry_error_from_errno (int ERR)
  474. The function `gcry_error_from_errno' is like `gcry_error', but it
  475. takes a system error like `errno' instead of a `gcry_err_code_t'
  476. error code.
  477. Sometimes you might want to map system error numbers to error codes
  478. directly, or map an error code representing a system error back to the
  479. system error number. The following functions can be used to do that.
  480. -- Function: gcry_err_code_t gcry_err_code_from_errno (int ERR)
  481. The function `gcry_err_code_from_errno' returns the error code for
  482. the system error ERR. If ERR is not a known system error, the
  483. function returns `GPG_ERR_UNKNOWN_ERRNO'.
  484. -- Function: int gcry_err_code_to_errno (gcry_err_code_t ERR)
  485. The function `gcry_err_code_to_errno' returns the system error for
  486. the error code ERR. If ERR is not an error code representing a
  487. system error, or if this system error is not defined on this
  488. system, the function returns `0'.
  489. 
  490. File: gcrypt.info, Node: Error Sources, Next: Error Codes, Prev: Error Values, Up: Error Handling
  491. 3.3.2 Error Sources
  492. -------------------
  493. The library `libgpg-error' defines an error source for every component
  494. of the GnuPG system. The error source part of an error value is not
  495. well defined. As such it is mainly useful to improve the diagnostic
  496. error message for the user.
  497. If the error code part of an error value is `0', the whole error
  498. value will be `0'. In this case the error source part is of course
  499. `GPG_ERR_SOURCE_UNKNOWN'.
  500. The list of error sources that might occur in applications using
  501. Libgctypt is:
  502. `GPG_ERR_SOURCE_UNKNOWN'
  503. The error source is not known. The value of this error source is
  504. `0'.
  505. `GPG_ERR_SOURCE_GPGME'
  506. The error source is GPGME itself.
  507. `GPG_ERR_SOURCE_GPG'
  508. The error source is GnuPG, which is the crypto engine used for the
  509. OpenPGP protocol.
  510. `GPG_ERR_SOURCE_GPGSM'
  511. The error source is GPGSM, which is the crypto engine used for the
  512. OpenPGP protocol.
  513. `GPG_ERR_SOURCE_GCRYPT'
  514. The error source is `libgcrypt', which is used by crypto engines
  515. to perform cryptographic operations.
  516. `GPG_ERR_SOURCE_GPGAGENT'
  517. The error source is `gpg-agent', which is used by crypto engines
  518. to perform operations with the secret key.
  519. `GPG_ERR_SOURCE_PINENTRY'
  520. The error source is `pinentry', which is used by `gpg-agent' to
  521. query the passphrase to unlock a secret key.
  522. `GPG_ERR_SOURCE_SCD'
  523. The error source is the SmartCard Daemon, which is used by
  524. `gpg-agent' to delegate operations with the secret key to a
  525. SmartCard.
  526. `GPG_ERR_SOURCE_KEYBOX'
  527. The error source is `libkbx', a library used by the crypto engines
  528. to manage local keyrings.
  529. `GPG_ERR_SOURCE_USER_1'
  530. `GPG_ERR_SOURCE_USER_2'
  531. `GPG_ERR_SOURCE_USER_3'
  532. `GPG_ERR_SOURCE_USER_4'
  533. These error sources are not used by any GnuPG component and can be
  534. used by other software. For example, applications using Libgcrypt
  535. can use them to mark error values coming from callback handlers.
  536. Thus `GPG_ERR_SOURCE_USER_1' is the default for errors created
  537. with `gcry_error' and `gcry_error_from_errno', unless you define
  538. `GCRY_ERR_SOURCE_DEFAULT' before including `gcrypt.h'.
  539. 
  540. File: gcrypt.info, Node: Error Codes, Next: Error Strings, Prev: Error Sources, Up: Error Handling
  541. 3.3.3 Error Codes
  542. -----------------
  543. The library `libgpg-error' defines many error values. The following
  544. list includes the most important error codes.
  545. `GPG_ERR_EOF'
  546. This value indicates the end of a list, buffer or file.
  547. `GPG_ERR_NO_ERROR'
  548. This value indicates success. The value of this error code is
  549. `0'. Also, it is guaranteed that an error value made from the
  550. error code `0' will be `0' itself (as a whole). This means that
  551. the error source information is lost for this error code, however,
  552. as this error code indicates that no error occured, this is
  553. generally not a problem.
  554. `GPG_ERR_GENERAL'
  555. This value means that something went wrong, but either there is not
  556. enough information about the problem to return a more useful error
  557. value, or there is no separate error value for this type of
  558. problem.
  559. `GPG_ERR_ENOMEM'
  560. This value means that an out-of-memory condition occurred.
  561. `GPG_ERR_E...'
  562. System errors are mapped to GPG_ERR_EFOO where FOO is the symbol
  563. for the system error.
  564. `GPG_ERR_INV_VALUE'
  565. This value means that some user provided data was out of range.
  566. `GPG_ERR_UNUSABLE_PUBKEY'
  567. This value means that some recipients for a message were invalid.
  568. `GPG_ERR_UNUSABLE_SECKEY'
  569. This value means that some signers were invalid.
  570. `GPG_ERR_NO_DATA'
  571. This value means that data was expected where no data was found.
  572. `GPG_ERR_CONFLICT'
  573. This value means that a conflict of some sort occurred.
  574. `GPG_ERR_NOT_IMPLEMENTED'
  575. This value indicates that the specific function (or operation) is
  576. not implemented. This error should never happen. It can only
  577. occur if you use certain values or configuration options which do
  578. not work, but for which we think that they should work at some
  579. later time.
  580. `GPG_ERR_DECRYPT_FAILED'
  581. This value indicates that a decryption operation was unsuccessful.
  582. `GPG_ERR_WRONG_KEY_USAGE'
  583. This value indicates that a key is not used appropriately.
  584. `GPG_ERR_NO_SECKEY'
  585. This value indicates that no secret key for the user ID is
  586. available.
  587. `GPG_ERR_UNSUPPORTED_ALGORITHM'
  588. This value means a verification failed because the cryptographic
  589. algorithm is not supported by the crypto backend.
  590. `GPG_ERR_BAD_SIGNATURE'
  591. This value means a verification failed because the signature is
  592. bad.
  593. `GPG_ERR_NO_PUBKEY'
  594. This value means a verification failed because the public key is
  595. not available.
  596. `GPG_ERR_USER_1'
  597. `GPG_ERR_USER_2'
  598. `...'
  599. `GPG_ERR_USER_16'
  600. These error codes are not used by any GnuPG component and can be
  601. freely used by other software. Applications using Libgcrypt might
  602. use them to mark specific errors returned by callback handlers if
  603. no suitable error codes (including the system errors) for these
  604. errors exist already.
  605. 
  606. File: gcrypt.info, Node: Error Strings, Prev: Error Codes, Up: Error Handling
  607. 3.3.4 Error Strings
  608. -------------------
  609. -- Function: const char * gcry_strerror (gcry_error_t ERR)
  610. The function `gcry_strerror' returns a pointer to a statically
  611. allocated string containing a description of the error code
  612. contained in the error value ERR. This string can be used to
  613. output a diagnostic message to the user.
  614. -- Function: const char * gcry_strsource (gcry_error_t ERR)
  615. The function `gcry_strerror' returns a pointer to a statically
  616. allocated string containing a description of the error source
  617. contained in the error value ERR. This string can be used to
  618. output a diagnostic message to the user.
  619. The following example illustrates the use of the functions described
  620. above:
  621. {
  622. gcry_cipher_hd_t handle;
  623. gcry_error_t err = 0;
  624. err = gcry_cipher_open (&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 0);
  625. if (err)
  626. {
  627. fprintf (stderr, "Failure: %s/%s\n",
  628. gcry_strsource (err),
  629. gcry_strerror (err));
  630. }
  631. }
  632. 
  633. File: gcrypt.info, Node: Handler Functions, Next: Symmetric cryptography, Prev: Generalities, Up: Top
  634. 4 Handler Functions
  635. *******************
  636. Libgcrypt makes it possible to install so called `handler functions',
  637. which get called by Libgcrypt in case of certain events.
  638. * Menu:
  639. * Progress handler:: Using a progress handler function.
  640. * Allocation handler:: Using special memory allocation functions.
  641. * Error handler:: Using error handler functions.
  642. * Logging handler:: Using a special logging function.
  643. 
  644. File: gcrypt.info, Node: Progress handler, Next: Allocation handler, Up: Handler Functions
  645. 4.1 Progress handler
  646. ====================
  647. It is often useful to retrieve some feedback while long running
  648. operations are performed.
  649. -- Data type: gcry_handler_progress_t
  650. Progress handler functions have to be of the type
  651. `gcry_handler_progress_t', which is defined as:
  652. `void (*gcry_handler_progress_t) (void *, const char *, int, int,
  653. int)'
  654. The following function may be used to register a handler function for
  655. this purpose.
  656. -- Function: void gcry_set_progress_handler (gcry_handler_progress_t
  657. CB, void *CB_DATA)
  658. This function installs CB as the `Progress handler' function. CB
  659. must be defined as follows:
  660. void
  661. my_progress_handler (void *CB_DATA, const char *WHAT,
  662. int PRINTCHAR, int CURRENT, int TOTAL)
  663. {
  664. /* Do something. */
  665. }
  666. A description of the arguments of the progress handler function
  667. follows.
  668. CB_DATA
  669. The argument provided in the call to
  670. `gcry_set_progress_handler'.
  671. WHAT
  672. A string identifying the type of the progress output. The
  673. following values for WHAT are defined:
  674. `need_entropy'
  675. Not enough entropy is available. TOTAL holds the number
  676. of required bytes.
  677. `primegen'
  678. Values for PRINTCHAR:
  679. `\n'
  680. Prime generated.
  681. `!'
  682. Need to refresh the pool of prime numbers.
  683. `<, >'
  684. Number of bits adjusted.
  685. `^'
  686. Searching for a generator.
  687. `.'
  688. Fermat test on 10 candidates failed.
  689. `:'
  690. Restart with a new random value.
  691. `+'
  692. Rabin Miller test passed.
  693. 
  694. File: gcrypt.info, Node: Allocation handler, Next: Error handler, Prev: Progress handler, Up: Handler Functions
  695. 4.2 Allocation handler
  696. ======================
  697. It is possible to make Libgcrypt use special memory allocation
  698. functions instead of the built-in ones.
  699. Memory allocation functions are of the following types:
  700. -- Data type: gcry_handler_alloc_t
  701. This type is defined as: `void *(*gcry_handler_alloc_t) (size_t
  702. n)'.
  703. -- Data type: gcry_handler_secure_check_t
  704. This type is defined as: `int *(*gcry_handler_secure_check_t)
  705. (const void *)'.
  706. -- Data type: gcry_handler_realloc_t
  707. This type is defined as: `void *(*gcry_handler_realloc_t) (void
  708. *p, size_t n)'.
  709. -- Data type: gcry_handler_free_t
  710. This type is defined as: `void *(*gcry_handler_free_t) (void *)'.
  711. Special memory allocation functions can be installed with the
  712. following function:
  713. -- Function: void gcry_set_allocation_handler (gcry_handler_alloc_t
  714. FUNC_ALLOC, gcry_handler_alloc_t FUNC_ALLOC_SECURE,
  715. gcry_handler_secure_check_t FUNC_SECURE_CHECK,
  716. gcry_handler_realloc_t FUNC_REALLOC, gcry_handler_free_t
  717. FUNC_FREE)
  718. Install the provided functions and use them instead of the built-in
  719. functions for doing memory allocation.
  720. 
  721. File: gcrypt.info, Node: Error handler, Next: Logging handler, Prev: Allocation handler, Up: Handler Functions
  722. 4.3 Error handler
  723. =================
  724. The following functions may be used to register handler functions that
  725. are called by Libgcrypt in case certain error conditions occur.
  726. -- Data type: gcry_handler_no_mem_t
  727. This type is defined as: `void (*gcry_handler_no_mem_t) (void *,
  728. size_t, unsigned int)'
  729. -- Function: void gcry_set_outofcore_handler (gcry_handler_no_mem_t
  730. FUNC_NO_MEM, void *CB_DATA)
  731. This function registers FUNC_NO_MEM as `out-of-core handler',
  732. which means that it will be called in the case of not having enough
  733. memory available.
  734. -- Data type: gcry_handler_error_t
  735. This type is defined as: `void (*gcry_handler_error_t) (void *,
  736. int, const char *)'
  737. -- Function: void gcry_set_fatalerror_handler (gcry_handler_error_t
  738. FUNC_ERROR, void *CB_DATA)
  739. This function registers FUNC_ERROR as `error handler', which means
  740. that it will be called in error conditions.
  741. 
  742. File: gcrypt.info, Node: Logging handler, Prev: Error handler, Up: Handler Functions
  743. 4.4 Logging handler
  744. ===================
  745. -- Data type: gcry_handler_log_t
  746. This type is defined as: `void (*gcry_handler_log_t) (void *, int,
  747. const char *, va_list)'
  748. -- Function: void gcry_set_log_handler (gcry_handler_log_t FUNC_LOG,
  749. void *CB_DATA)
  750. This function registers FUNC_LOG as `logging handler', which means
  751. that it will be called in case Libgcrypt wants to log a message.
  752. 
  753. File: gcrypt.info, Node: Symmetric cryptography, Next: Hashing, Prev: Handler Functions, Up: Top
  754. 5 Symmetric cryptography
  755. ************************
  756. The cipher functions are used for symmetrical cryptography, i.e.
  757. cryptography using a shared key. The programming model follows an
  758. open/process/close paradigm and is in that similar to other building
  759. blocks provided by Libgcrypt.
  760. * Menu:
  761. * Available ciphers:: List of ciphers supported by the library.
  762. * Cipher modules:: How to work with cipher modules.
  763. * Available cipher modes:: List of cipher modes supported by the library.
  764. * Working with cipher handles:: How to perform operations related to cipher handles.
  765. * General cipher functions:: General cipher functions independent of cipher handles.
  766. 
  767. File: gcrypt.info, Node: Available ciphers, Next: Cipher modules, Up: Symmetric cryptography
  768. 5.1 Available ciphers
  769. =====================
  770. `GCRY_CIPHER_NONE'
  771. This is not a real algorithm but used by some functions as error
  772. return. The value always evaluates to false.
  773. `GCRY_CIPHER_IDEA'
  774. This is the IDEA algorithm. The constant is provided but there is
  775. currently no implementation for it because the algorithm is
  776. patented.
  777. `GCRY_CIPHER_3DES'
  778. Triple-DES with 3 Keys as EDE. The key size of this algorithm is
  779. 168 but you have to pass 192 bits because the most significant
  780. bits of each byte are ignored.
  781. `GCRY_CIPHER_CAST5'
  782. CAST128-5 block cipher algorithm. The key size is 128 bits.
  783. `GCRY_CIPHER_BLOWFISH'
  784. The blowfish algorithm. The current implementation allows only for
  785. a key size of 128 bits.
  786. `GCRY_CIPHER_SAFER_SK128'
  787. Reserved and not currently implemented.
  788. `GCRY_CIPHER_DES_SK'
  789. Reserved and not currently implemented.
  790. `GCRY_CIPHER_AES'
  791. `GCRY_CIPHER_AES128'
  792. `GCRY_CIPHER_RIJNDAEL'
  793. `GCRY_CIPHER_RIJNDAEL128'
  794. AES (Rijndael) with a 128 bit key.
  795. `GCRY_CIPHER_AES192'
  796. `GCRY_CIPHER_RIJNDAEL128'
  797. AES (Rijndael) with a 192 bit key.
  798. `GCRY_CIPHER_AES256'
  799. `GCRY_CIPHER_RIJNDAEL256'
  800. AES (Rijndael) with a 256 bit key.
  801. `GCRY_CIPHER_TWOFISH'
  802. The Twofish algorithm with a 256 bit key.
  803. `GCRY_CIPHER_TWOFISH128'
  804. The Twofish algorithm with a 128 bit key.
  805. `GCRY_CIPHER_ARCFOUR'
  806. An algorithm which is 100% compatible with RSA Inc.'s RC4
  807. algorithm. Note that this is a stream cipher and must be used
  808. very carefully to avoid a couple of weaknesses.
  809. `GCRY_CIPHER_DES'
  810. Standard DES with a 56 bit key. You need to pass 64 bit but the
  811. high bits of each byte are ignored. Note, that this is a weak
  812. algorithm which can be broken in reasonable time using a brute
  813. force approach.
  814. 
  815. File: gcrypt.info, Node: Cipher modules, Next: Available cipher modes, Prev: Available ciphers, Up: Symmetric cryptography
  816. 5.2 Cipher modules
  817. ==================
  818. Libgcrypt makes it possible to load additional `cipher modules'; these
  819. cipher can be used just like the cipher algorithms that are built into
  820. the library directly. For an introduction into extension modules, see
  821. *Note Modules::.
  822. -- Data type: gcry_cipher_spec_t
  823. This is the `module specification structure' needed for registering
  824. cipher modules, which has to be filled in by the user before it
  825. can be used to register a module. It contains the following
  826. members:
  827. `const char *name'
  828. The primary name of the algorithm.
  829. `const char **aliases'
  830. A list of strings that are `aliases' for the algorithm. The
  831. list must be terminated with a NULL element.
  832. `gcry_cipher_oid_spec_t *oids'
  833. A list of OIDs that are to be associated with the algorithm.
  834. The list's last element must have it's `oid' member set to
  835. NULL. See below for an explanation of this type.
  836. `size_t blocksize'
  837. The block size of the algorithm, in bytes.
  838. `size_t keylen'
  839. The length of the key, in bits.
  840. `size_t contextsize'
  841. The size of the algorithm-specific `context', that should be
  842. allocated for each handle.
  843. `gcry_cipher_setkey_t setkey'
  844. The function responsible for initializing a handle with a
  845. provided key. See below for a description of this type.
  846. `gcry_cipher_encrypt_t encrypt'
  847. The function responsible for encrypting a single block. See
  848. below for a description of this type.
  849. `gcry_cipher_decrypt_t decrypt'
  850. The function responsible for decrypting a single block. See
  851. below for a description of this type.
  852. `gcry_cipher_stencrypt_t stencrypt'
  853. Like `encrypt', for stream ciphers. See below for a
  854. description of this type.
  855. `gcry_cipher_stdecrypt_t stdecrypt'
  856. Like `decrypt', for stream ciphers. See below for a
  857. description of this type.
  858. -- Data type: gcry_cipher_oid_spec_t
  859. This type is used for associating a user-provided algorithm
  860. implementation with certain OIDs. It contains the following
  861. members:
  862. `const char *oid'
  863. Textual representation of the OID.
  864. `int mode'
  865. Cipher mode for which this OID is valid.
  866. -- Data type: gcry_cipher_setkey_t
  867. Type for the `setkey' function, defined as: gcry_err_code_t
  868. (*gcry_cipher_setkey_t) (void *c, const unsigned char *key,
  869. unsigned keylen)
  870. -- Data type: gcry_cipher_encrypt_t
  871. Type for the `encrypt' function, defined as: gcry_err_code_t
  872. (*gcry_cipher_encrypt_t) (void *c, const unsigned char *outbuf,
  873. const unsigned char *inbuf)
  874. -- Data type: gcry_cipher_decrypt_t
  875. Type for the `decrypt' function, defined as: gcry_err_code_t
  876. (*gcry_cipher_decrypt_t) (void *c, const unsigned char *outbuf,
  877. const unsigned char *inbuf)
  878. -- Data type: gcry_cipher_stencrypt_t
  879. Type for the `stencrypt' function, defined as: gcry_err_code_t
  880. (*gcry_cipher_stencrypt_t) (void *c, const unsigned char *outbuf,
  881. const unsigned char *, unsigned int n)
  882. -- Data type: gcry_cipher_stdecrypt_t
  883. Type for the `stdecrypt' function, defined as: gcry_err_code_t
  884. (*gcry_cipher_stdecrypt_t) (void *c, const unsigned char *outbuf,
  885. const unsigned char *, unsigned int n)
  886. -- Function: gcry_error_t gcry_cipher_register (gcry_cipher_spec_t
  887. *CIPHER, unsigned int *algorithm_id, gcry_module_t *MODULE)
  888. Register a new cipher module whose specification can be found in
  889. CIPHER. On success, a new algorithm ID is stored in ALGORITHM_ID
  890. and a pointer representing this module is stored in MODULE.
  891. -- Function: void gcry_cipher_unregister (gcry_module_t MODULE)
  892. Unregister the cipher identified by MODULE, which must have been
  893. registered with gcry_cipher_register.
  894. -- Function: gcry_error_t gcry_cipher_list (int *LIST, int
  895. *LIST_LENGTH)
  896. Get a list consisting of the IDs of the loaded cipher modules. If
  897. LIST is zero, write the number of loaded cipher modules to
  898. LIST_LENGTH and return. If LIST is non-zero, the first
  899. *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
  900. according size. In case there are less cipher modules than
  901. *LIST_LENGTH, *LIST_LENGTH is updated to the correct number.
  902. 
  903. File: gcrypt.info, Node: Available cipher modes, Next: Working with cipher handles, Prev: Cipher modules, Up: Symmetric cryptography
  904. 5.3 Available cipher modes
  905. ==========================
  906. `GCRY_CIPHER_MODE_NONE'
  907. No mode specified, may be set later using other functions. The
  908. value of this constant is always 0.
  909. `GCRY_CIPHER_MODE_ECB'
  910. Electronic Codebook mode.
  911. `GCRY_CIPHER_MODE_CFB'
  912. Cipher Feedback mode.
  913. `GCRY_CIPHER_MODE_CBC'
  914. Cipher Block Chaining mode.
  915. `GCRY_CIPHER_MODE_STREAM'
  916. Stream mode, only to be used with stream cipher algorithms.
  917. `GCRY_CIPHER_MODE_OFB'
  918. Outer Feedback mode.
  919. `GCRY_CIPHER_MODE_CTR'
  920. Counter mode.
  921. 
  922. File: gcrypt.info, Node: Working with cipher handles, Next: General cipher functions, Prev: Available cipher modes, Up: Symmetric cryptography
  923. 5.4 Working with cipher handles
  924. ===============================
  925. To use a cipher algorithm, you must first allocate an according handle.
  926. This is to be done using the open function:
  927. -- Function: gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *HD, int
  928. ALGO, int MODE, unsigned int FLAGS)
  929. This function creates the context handle required for most of the
  930. other cipher functions and returns a handle to it in `hd'. In
  931. case of an error, an according error code is returned.
  932. The ID of algorithm to use must be specified via ALGO. See *Note
  933. Available ciphers::, for a list of supported ciphers and the
  934. according constants.
  935. Besides using the constants directly, the function
  936. `gcry_cipher_map_name' may be used to convert the textual name of
  937. an algorithm into the according numeric ID.
  938. The cipher mode to use must be specified via MODE. See *Note
  939. Available cipher modes::, for a list of supported cipher m

Large files files are truncated, but you can click here to view the full file