Debian Codegraph @

doc/gcrypt.info-1

raw
1 This is gcrypt.info, produced by makeinfo version 6.8 from gcrypt.texi.
2
3 This manual is for Libgcrypt version 1.12.2 and was last updated 24
4 September 2025. Libgcrypt is GNU's library of cryptographic building
5 blocks.
6
7 Copyright (C) 2000, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2011, 2012
8 Free Software Foundation, Inc.
9 Copyright (C) 2012, 2013, 2016, 2017 g10 Code GmbH
10
11 Permission is granted to copy, distribute and/or modify this
12 document under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version. The text of the
15 license can be found in the section entitled "GNU General Public
16 License".
17 INFO-DIR-SECTION GNU Libraries
18 START-INFO-DIR-ENTRY
19 * libgcrypt: (gcrypt). Cryptographic function library.
20 END-INFO-DIR-ENTRY
21
22 
23 File: gcrypt.info, Node: Top, Next: Introduction, Up: (dir)
24
25 The Libgcrypt Library
26 *********************
27
28 This manual is for Libgcrypt version 1.12.2 and was last updated 24
29 September 2025. Libgcrypt is GNU's library of cryptographic building
30 blocks.
31
32 Copyright (C) 2000, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2011, 2012
33 Free Software Foundation, Inc.
34 Copyright (C) 2012, 2013, 2016, 2017 g10 Code GmbH
35
36 Permission is granted to copy, distribute and/or modify this
37 document under the terms of the GNU General Public License as
38 published by the Free Software Foundation; either version 2 of the
39 License, or (at your option) any later version. The text of the
40 license can be found in the section entitled "GNU General Public
41 License".
42
43 * Menu:
44
45 * Introduction:: What is Libgcrypt.
46 * Preparation:: What you should do before using the library.
47 * Generalities:: General library functions and data types.
48 * Handler Functions:: Working with handler functions.
49 * Symmetric cryptography:: How to use symmetric cryptography.
50 * Public Key cryptography:: How to use public key cryptography.
51 * Hashing:: How to use hash algorithms.
52 * Message Authentication Codes:: How to use MAC algorithms.
53 * Key Derivation:: How to derive keys from strings
54 * Random Numbers:: How to work with random numbers.
55 * S-expressions:: How to manage S-expressions.
56 * MPI library:: How to work with multi-precision-integers.
57 * Prime numbers:: How to use the Prime number related functions.
58 * Utilities:: Utility functions.
59 * Tools:: Utility tools.
60 * Configuration:: Configuration files and environment variables.
61 * Architecture:: How Libgcrypt works internally.
62
63 Appendices
64
65 * Self-Tests:: Description of the self-tests.
66 * FIPS Mode:: Description of the FIPS mode.
67 * Library Copying:: The GNU Lesser General Public License
68 says how you can copy and share Libgcrypt.
69 * Copying:: The GNU General Public License says how you
70 can copy and share some parts of Libgcrypt.
71
72 Indices
73
74 * Figures and Tables:: Index of figures and tables.
75 * Concept Index:: Index of concepts and programs.
76 * Function and Data Index:: Index of functions, variables and data types.
77
78 
79 File: gcrypt.info, Node: Introduction, Next: Preparation, Prev: Top, Up: Top
80
81 1 Introduction
82 **************
83
84 Libgcrypt is a library providing cryptographic building blocks.
85
86 * Menu:
87
88 * Getting Started:: How to use this manual.
89 * Features:: A glance at Libgcrypt's features.
90 * Overview:: Overview about the library.
91
92 
93 File: gcrypt.info, Node: Getting Started, Next: Features, Up: Introduction
94
95 1.1 Getting Started
96 ===================
97
98 This manual documents the Libgcrypt library application programming
99 interface (API). All functions and data types provided by the library
100 are explained.
101
102 The reader is assumed to possess basic knowledge about applied
103 cryptography.
104
105 This manual can be used in several ways. If read from the beginning
106 to the end, it gives a good introduction into the library and how it can
107 be used in an application. Forward references are included where
108 necessary. Later on, the manual can be used as a reference manual to
109 get just the information needed about any particular interface of the
110 library. Experienced programmers might want to start looking at the
111 examples at the end of the manual, and then only read up those parts of
112 the interface which are unclear.
113
114 
115 File: gcrypt.info, Node: Features, Next: Overview, Prev: Getting Started, Up: Introduction
116
117 1.2 Features
118 ============
119
120 Libgcrypt might have a couple of advantages over other libraries doing a
121 similar job.
122
123 It's Free Software
124 Anybody can use, modify, and redistribute it under the terms of the
125 GNU Lesser General Public License (*note Library Copying::). Note,
126 that some parts (which are in general not needed by applications)
127 are subject to the terms of the GNU General Public License (*note
128 Copying::); please see the README file of the distribution for the
129 list of these parts.
130
131 It encapsulates the low level cryptography
132 Libgcrypt provides a high level interface to cryptographic building
133 blocks using an extensible and flexible API.
134
135 
136 File: gcrypt.info, Node: Overview, Prev: Features, Up: Introduction
137
138 1.3 Overview
139 ============
140
141 The Libgcrypt library is fully thread-safe, where it makes sense to be
142 thread-safe. Not thread-safe are some cryptographic functions that
143 modify a certain context stored in handles. If the user really intents
144 to use such functions from different threads on the same handle, he has
145 to take care of the serialization of such functions himself. If not
146 described otherwise, every function is thread-safe.
147
148 Libgcrypt depends on the library 'libgpg-error', which contains some
149 common code used by other GnuPG components.
150
151 
152 File: gcrypt.info, Node: Preparation, Next: Generalities, Prev: Introduction, Up: Top
153
154 2 Preparation
155 *************
156
157 To use Libgcrypt, you have to perform some changes to your sources and
158 the build system. The necessary changes are small and explained in the
159 following sections. At the end of this chapter, it is described how the
160 library is initialized, and how the requirements of the library are
161 verified.
162
163 * Menu:
164
165 * Header:: What header file you need to include.
166 * Building sources:: How to build sources using the library.
167 * Building sources using Automake:: How to build sources with the help of Automake.
168 * Initializing the library:: How to initialize the library.
169 * Multi-Threading:: How Libgcrypt can be used in a MT environment.
170 * Enabling FIPS mode:: How to enable the FIPS mode.
171 * Disabling FIPS mode:: How to disable the FIPS mode.
172 * Hardware features:: How to disable hardware features.
173
174 
175 File: gcrypt.info, Node: Header, Next: Building sources, Up: Preparation
176
177 2.1 Header
178 ==========
179
180 All interfaces (data types and functions) of the library are defined in
181 the header file 'gcrypt.h'. You must include this in all source files
182 using the library, either directly or through some other header file,
183 like this:
184
185 #include <gcrypt.h>
186
187 The name space of Libgcrypt is 'gcry_*' for function and type names
188 and 'GCRY*' for other symbols. In addition the same name prefixes with
189 one prepended underscore are reserved for internal use and should never
190 be used by an application. Note that Libgcrypt uses libgpg-error, which
191 uses 'gpg_*' as name space for function and type names and 'GPG_*' for
192 other symbols, including all the error codes.
193
194 Certain parts of gcrypt.h may be excluded by defining these macros:
195
196 'GCRYPT_NO_MPI_MACROS'
197 Do not define the shorthand macros 'mpi_*' for 'gcry_mpi_*'.
198
199 'GCRYPT_NO_DEPRECATED'
200 Do not include definitions for deprecated features. This is useful
201 to make sure that no deprecated features are used.
202
203 
204 File: gcrypt.info, Node: Building sources, Next: Building sources using Automake, Prev: Header, Up: Preparation
205
206 2.2 Building sources
207 ====================
208
209 If you want to compile a source file including the 'gcrypt.h' header
210 file, you must make sure that the compiler can find it in the directory
211 hierarchy. This is accomplished by adding the path to the directory in
212 which the header file is located to the compilers include file search
213 path (via the '-I' option).
214
215 However, the path to the include file is determined at the time the
216 source is configured. To solve this problem, Libgcrypt ships with
217 'libgcrypt.pc' file, that knows about the path to the include file and
218 other configuration options. The options that need to be added to the
219 compiler invocation at compile time are output by the '--cflags' option
220 to 'pkg-config libgcrypt'. The following example shows how it can be
221 used at the command line:
222
223 gcc -c foo.c `pkg-config --cflags libgcrypt`
224
225 Adding the output of 'pkg-config --cflags libgcrypt' to the
226 compiler's command line will ensure that the compiler can find the
227 Libgcrypt header file.
228
229 A similar problem occurs when linking the program with the library.
230 Again, the compiler has to find the library files. For this to work,
231 the path to the library files has to be added to the library search path
232 (via the '-L' option). For this, the option '--libs' to 'pkg-config
233 libgcrypt' can be used. For convenience, this option also outputs all
234 other options that are required to link the program with the Libgcrypt
235 libraries (in particular, the '-lgcrypt' option). The example shows how
236 to link 'foo.o' with the Libgcrypt library to a program 'foo'.
237
238 gcc -o foo foo.o `pkg-config --libs libgcrypt`
239
240 Of course you can also combine both examples to a single command by
241 specifying both options to 'pkg-config libgcrypt':
242
243 gcc -o foo foo.c `pkg-config --cflags --libs libgcrypt`
244
245 
246 File: gcrypt.info, Node: Building sources using Automake, Next: Initializing the library, Prev: Building sources, Up: Preparation
247
248 2.3 Building sources using Automake
249 ===================================
250
251 You can simply use 'PKG_CHECK_MODULES' macro with 'pkg-config':
252
253 PKG_CHECK_MODULES([LIBGCRYPT], [libgcrypt >= 1.11])
254
255 Alternatively, instead of using 'pkg-config', for building on an
256 environment with no pkg-config, libgcrypt also provides an extension to
257 Automake that does all the work for you. Please note that it is
258 required to have gpgrt-config from libgpg-error installed in this case.
259
260 -- Macro: AM_PATH_LIBGCRYPT ([MINIMUM-VERSION], [ACTION-IF-FOUND],
261 [ACTION-IF-NOT-FOUND])
262 Check whether Libgcrypt (at least version MINIMUM-VERSION, if
263 given) exists on the host system. If it is found, execute
264 ACTION-IF-FOUND, otherwise do ACTION-IF-NOT-FOUND, if given.
265
266 Additionally, the function defines 'LIBGCRYPT_CFLAGS' to the flags
267 needed for compilation of the program to find the 'gcrypt.h' header
268 file, and 'LIBGCRYPT_LIBS' to the linker flags needed to link the
269 program to the Libgcrypt library.
270
271 This macro locates for 'libgcrypt.pc', with cross-compile support.
272
273 You can use the defined Autoconf variables like this in your
274 'Makefile.am':
275
276 AM_CPPFLAGS = $(LIBGCRYPT_CFLAGS)
277 LDADD = $(LIBGCRYPT_LIBS)
278
279 
280 File: gcrypt.info, Node: Initializing the library, Next: Multi-Threading, Prev: Building sources using Automake, Up: Preparation
281
282 2.4 Initializing the library
283 ============================
284
285 Before the library can be used, it must initialize itself. This is
286 achieved by invoking the function 'gcry_check_version' described below.
287
288 Also, it is often desirable to check that the version of Libgcrypt
289 used is indeed one which fits all requirements. Even with binary
290 compatibility, new features may have been introduced, but due to problem
291 with the dynamic linker an old version may actually be used. So you may
292 want to check that the version is okay right after program startup.
293
294 -- Function: const char * gcry_check_version (const char *REQ_VERSION)
295
296 The function 'gcry_check_version' initializes some subsystems used
297 by Libgcrypt and must be invoked before any other function in the
298 library. *Note Multi-Threading::.
299
300 Furthermore, this function returns the version number of the
301 library. It can also verify that the version number is higher than
302 a certain required version number REQ_VERSION, if this value is not
303 a null pointer.
304
305 Libgcrypt uses a concept known as secure memory, which is a region of
306 memory set aside for storing sensitive data. Because such memory is a
307 scarce resource, it needs to be setup in advanced to a fixed size.
308 Further, most operating systems have special requirements on how that
309 secure memory can be used. For example, it might be required to install
310 an application as "setuid(root)" to allow allocating such memory.
311 Libgcrypt requires a sequence of initialization steps to make sure that
312 this works correctly. The following examples show the necessary steps.
313
314 If you don't have a need for secure memory, for example if your
315 application does not use secret keys or other confidential data or it
316 runs in a controlled environment where key material floating around in
317 memory is not a problem, you should initialize Libgcrypt this way:
318
319 /* Version check should be the very first call because it
320 makes sure that important subsystems are initialized.
321 #define NEED_LIBGCRYPT_VERSION to the minimum required version. */
322 if (!gcry_check_version (NEED_LIBGCRYPT_VERSION))
323 {
324 fprintf (stderr, "libgcrypt is too old (need %s, have %s)\n",
325 NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL));
326 exit (2);
327 }
328
329 /* Disable secure memory. */
330 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
331
332 /* ... If required, other initialization goes here. */
333
334 /* Tell Libgcrypt that initialization has completed. */
335 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
336
337 If you have to protect your keys or other information in memory
338 against being swapped out to disk and to enable an automatic overwrite
339 of used and freed memory, you need to initialize Libgcrypt this way:
340
341 /* Version check should be the very first call because it
342 makes sure that important subsystems are initialized.
343 #define NEED_LIBGCRYPT_VERSION to the minimum required version. */
344 if (!gcry_check_version (NEED_LIBGCRYPT_VERSION))
345 {
346 fprintf (stderr, "libgcrypt is too old (need %s, have %s)\n",
347 NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL));
348 exit (2);
349 }
350
351 /* We don't want to see any warnings, e.g., because we have not yet
352 parsed program options which might be used to suppress such
353 warnings. */
354 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
355
356 /* ... If required, other initialization goes here. Note that the
357 process might still be running with increased privileges and that
358 the secure memory has not been initialized. */
359
360 /* Allocate a pool of 16k secure memory. This makes the secure memory
361 available and also drops privileges where needed. Note that by
362 using functions like gcry_xmalloc_secure and gcry_mpi_snew Libgcrypt
363 may expand the secure memory pool with memory which lacks the
364 property of not being swapped out to disk. */
365 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
366
367 /* It is now okay to let Libgcrypt complain when there was/is
368 a problem with the secure memory. */
369 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
370
371 /* ... If required, other initialization goes here. */
372
373 /* Tell Libgcrypt that initialization has completed. */
374 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
375
376 It is important that these initialization steps are not done by a
377 library but by the actual application. A library using Libgcrypt might
378 want to check for finished initialization using:
379
380 if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P))
381 {
382 fputs ("libgcrypt has not been initialized\n", stderr);
383 abort ();
384 }
385
386 Instead of terminating the process, the library may instead print a
387 warning and try to initialize Libgcrypt itself. See also the section on
388 multi-threading below for more pitfalls.
389
390 
391 File: gcrypt.info, Node: Multi-Threading, Next: Enabling FIPS mode, Prev: Initializing the library, Up: Preparation
392
393 2.5 Multi-Threading
394 ===================
395
396 As mentioned earlier, the Libgcrypt library is thread-safe if you adhere
397 to the following requirements:
398
399 * If you use pthread and your applications forks and does not
400 directly call exec (even calling stdio functions), all kind of
401 problems may occur. Future versions of Libgcrypt will try to
402 cleanup using pthread_atfork but even that may lead to problems.
403 This is a common problem with almost all applications using pthread
404 and fork.
405
406 * The function 'gcry_check_version' must be called before any other
407 function in the library. To achieve this in multi-threaded
408 programs, you must synchronize the memory with respect to other
409 threads that also want to use Libgcrypt. For this, it is
410 sufficient to call 'gcry_check_version' before creating the other
411 threads using Libgcrypt(1).
412
413 * Just like the function 'gpg_strerror', the function 'gcry_strerror'
414 is not thread safe. You have to use 'gpg_strerror_r' instead.
415
416 ---------- Footnotes ----------
417
418 (1) At least this is true for POSIX threads, as 'pthread_create' is a
419 function that synchronizes memory with respects to other threads. There
420 are many functions which have this property, a complete list can be
421 found in POSIX, IEEE Std 1003.1-2003, Base Definitions, Issue 6, in the
422 definition of the term "Memory Synchronization". For other thread
423 packages, more relaxed or more strict rules may apply.
424
425 
426 File: gcrypt.info, Node: Enabling FIPS mode, Next: Disabling FIPS mode, Prev: Multi-Threading, Up: Preparation
427
428 2.6 How to enable the FIPS mode
429 ===============================
430
431 Libgcrypt may be used in a FIPS 140-3 mode. Note, that this does not
432 necessary mean that Libcgrypt is an appoved FIPS 140-3 module. Check
433 the NIST database at <http://csrc.nist.gov/groups/STM/cmvp/> to see what
434 versions of Libgcrypt are approved.
435
436 Because FIPS 140 has certain restrictions on the use of cryptography
437 which are not always wanted, Libgcrypt needs to be put into FIPS mode
438 explicitly. Four alternative mechanisms are provided to switch
439 Libgcrypt into this mode:
440
441 * If the file '/proc/sys/crypto/fips_enabled' exists and contains a
442 numeric value other than '0', Libgcrypt is put into FIPS mode at
443 initialization time. Obviously this works only on systems with a
444 'proc' file system (i.e., GNU/Linux).
445
446 * If the file '/etc/gcrypt/fips_enabled' exists, Libgcrypt is put
447 into FIPS mode at initialization time. Note that this filename is
448 hardwired and does not depend on any configuration options.
449
450 * By setting the environment variable 'LIBGCRYPT_FORCE_FIPS_MODE',
451 Libgcrypt is put into FIPS mode at initialization time.
452
453 * If the application requests FIPS mode using the control command
454 'GCRYCTL_FORCE_FIPS_MODE'. This must be done prior to any
455 initialization (i.e., before 'gcry_check_version').
456
457 
458 File: gcrypt.info, Node: Disabling FIPS mode, Next: Hardware features, Prev: Enabling FIPS mode, Up: Preparation
459
460 2.7 How to disable the FIPS mode
461 ================================
462
463 When the system is configured using libgcrypt in FIPS mode (by file or
464 environement variable), but an application wants to use non-FIPS
465 features, Libgcrypt needs to be gotten out of FIPS mode. A mechanism is
466 provided to switch Libgcrypt into non-FIPS mode:
467
468 * If the application requests non-FIPS mode using the control command
469 'GCRYCTL_NO_FIPS_MODE'. This must be done prior to any
470 initialization (i.e., before 'gcry_check_version').
471
472 
473 File: gcrypt.info, Node: Hardware features, Prev: Disabling FIPS mode, Up: Preparation
474
475 2.8 How to disable hardware features
476 ====================================
477
478 Libgcrypt makes use of certain hardware features. If the use of a
479 feature is not desired, it may be disabled either by a program or
480 globally using a configuration file. The currently supported features
481 are
482
483 'padlock-rng'
484 'padlock-aes'
485 'padlock-sha'
486 'padlock-mmul'
487 'intel-cpu'
488 'intel-fast-shld'
489 'intel-bmi2'
490 'intel-ssse3'
491 'intel-sse4.1'
492 'intel-pclmul'
493 'intel-aesni'
494 'intel-rdrand'
495 'intel-avx'
496 'intel-avx2'
497 'intel-rdtsc'
498 'intel-shaext'
499 'intel-vaes-vpclmul'
500 'intel-avx512'
501 'intel-gfni'
502 'arm-neon'
503 'arm-aes'
504 'arm-sha1'
505 'arm-sha2'
506 'arm-pmull'
507 'arm-sha3'
508 'arm-sm3'
509 'arm-sm4'
510 'arm-sha512'
511 'arm-sve'
512 'arm-sve2'
513 'arm-sveaes'
514 'arm-svepmull'
515 'arm-svesha3'
516 'arm-svesm4'
517 'ppc-vcrypto'
518 'ppc-arch_3_00'
519 'ppc-arch_2_07'
520 'ppc-arch_3_10'
521 's390x-msa'
522 's390x-msa-4'
523 's390x-msa-8'
524 's390x-msa-9'
525 's390x-vx'
526 'riscv-imafdc'
527 'riscv-b'
528 'riscv-v'
529 'riscv-zbb'
530 'riscv-zbc'
531 'riscv-zvkb'
532 'riscv-zvkg'
533 'riscv-zvkned'
534 'riscv-zvknha'
535 'riscv-zvknhb'
536
537 To disable a feature for all processes using Libgcrypt 1.6 or newer,
538 create the file '/etc/gcrypt/hwf.deny' and put each feature not to be
539 used on a single line. Empty lines, white space, and lines prefixed
540 with a hash mark are ignored. The file should be world readable.
541
542 To disable a feature specifically for a program, that program must
543 tell it Libgcrypt before before calling 'gcry_check_version'.
544 Example:(1)
545
546 gcry_control (GCRYCTL_DISABLE_HWF, "intel-rdrand", NULL);
547
548 To print the list of active features you may use this command:
549
550 mpicalc --print-config | grep ^hwflist: | tr : '\n' | tail -n +2
551
552 ---------- Footnotes ----------
553
554 (1) NB. Libgcrypt uses the RDRAND feature only as one source of
555 entropy. A CPU with a broken RDRAND will thus not compromise the random
556 number generator
557
558 
559 File: gcrypt.info, Node: Generalities, Next: Handler Functions, Prev: Preparation, Up: Top
560
561 3 Generalities
562 **************
563
564 * Menu:
565
566 * Controlling the library:: Controlling Libgcrypt's behavior.
567 * Error Handling:: Error codes and such.
568
569 
570 File: gcrypt.info, Node: Controlling the library, Next: Error Handling, Up: Generalities
571
572 3.1 Controlling the library
573 ===========================
574
575 -- Function: gcry_error_t gcry_control (enum gcry_ctl_cmds CMD, ...)
576
577 This function can be used to influence the general behavior of
578 Libgcrypt in several ways. Depending on CMD, more arguments can or
579 have to be provided.
580
581 'GCRYCTL_ENABLE_M_GUARD; Arguments: none'
582 This command was to enable the built-in memory guard, but not
583 supported any more.
584
585 'GCRYCTL_ENABLE_QUICK_RANDOM; Arguments: none'
586 This command inhibits the use the very secure random quality
587 level ('GCRY_VERY_STRONG_RANDOM') and degrades all request
588 down to 'GCRY_STRONG_RANDOM'. In general this is not
589 recommended. However, for some applications the extra quality
590 random Libgcrypt tries to create is not justified and this
591 option may help to get better performance. Please check with
592 a crypto expert whether this option can be used for your
593 application.
594
595 This option can only be used at initialization time.
596
597 'GCRYCTL_DUMP_RANDOM_STATS; Arguments: none'
598 This command dumps random number generator related statistics
599 to the library's logging stream.
600
601 'GCRYCTL_DUMP_MEMORY_STATS; Arguments: none'
602 This command dumps memory management related statistics to the
603 library's logging stream.
604
605 'GCRYCTL_DUMP_SECMEM_STATS; Arguments: none'
606 This command dumps secure memory management related statistics
607 to the library's logging stream.
608
609 'GCRYCTL_DROP_PRIVS; Arguments: none'
610 This command disables the use of secure memory and drops the
611 privileges of the current process. This command has not much
612 use; the suggested way to disable secure memory is to use
613 'GCRYCTL_DISABLE_SECMEM' right after initialization.
614
615 'GCRYCTL_DISABLE_SECMEM; Arguments: none'
616 This command disables the use of secure memory. In FIPS mode
617 this command has no effect at all.
618
619 Many applications do not require secure memory, so they should
620 disable it right away. This command should be executed right
621 after 'gcry_check_version'.
622
623 'GCRYCTL_DISABLE_LOCKED_SECMEM; Arguments: none'
624 This command disables the use of the mlock call for secure
625 memory. Disabling the use of mlock may for example be done if
626 an encrypted swap space is in use. This command should be
627 executed right after 'gcry_check_version'. Note that by using
628 functions like 'gcry_xmalloc_secure' and 'gcry_mpi_snew'
629 Libgcrypt may expand the secure memory pool with memory which
630 lacks the property of not being swapped out to disk (but will
631 still be zeroed out on free).
632
633 'GCRYCTL_DISABLE_PRIV_DROP; Arguments: none'
634 This command sets a global flag to tell the secure memory
635 subsystem that it shall not drop privileges after secure
636 memory has been allocated. This command is commonly used
637 right after 'gcry_check_version' but may also be used right
638 away at program startup. It won't have an effect after the
639 secure memory pool has been initialized. WARNING: A process
640 running setuid(root) is a severe security risk. Processes
641 making use of Libgcrypt or other complex code should drop
642 these extra privileges as soon as possible. If this command
643 has been used the caller is responsible for dropping the
644 privileges.
645
646 'GCRYCTL_INIT_SECMEM; Arguments: unsigned int nbytes'
647 This command is used to allocate a pool of secure memory and
648 thus enabling the use of secure memory. It also drops all
649 extra privileges the process has (i.e., if it is run as setuid
650 (root)). If the argument NBYTES is 0, secure memory will be
651 disabled. The minimum amount of secure memory allocated is
652 currently 16384 bytes; you may thus use a value of 1 to
653 request that default size.
654
655 'GCRYCTL_AUTO_EXPAND_SECMEM; Arguments: unsigned int chunksize'
656 This command enables on-the-fly expanding of the secure memory
657 area. Note that by using functions like 'gcry_xmalloc_secure'
658 and 'gcry_mpi_snew' will do this auto expanding anyway. The
659 argument to this option is the suggested size for new secure
660 memory areas. A larger size improves performance of all
661 memory allocation and releasing functions. The given
662 chunksize is rounded up to the next 32KiB. The drawback of
663 auto expanding is that memory might be swapped out to disk;
664 this can be fixed by configuring the system to use an
665 encrypted swap space.
666
667 'GCRYCTL_TERM_SECMEM; Arguments: none'
668 This command zeroises the secure memory and destroys the
669 handler. The secure memory pool may not be used anymore after
670 running this command. If the secure memory pool has already
671 been destroyed, this command has no effect. Applications
672 might want to run this command from their exit handler to make
673 sure that the secure memory gets properly destroyed. This
674 command is not necessarily thread-safe but that should not be
675 needed in cleanup code. It may be called from a signal
676 handler.
677
678 'GCRYCTL_DISABLE_SECMEM_WARN; Arguments: none'
679 Disable warning messages about problems with the secure memory
680 subsystem. This command should be run right after
681 'gcry_check_version'.
682
683 'GCRYCTL_SUSPEND_SECMEM_WARN; Arguments: none'
684 Postpone warning messages from the secure memory subsystem.
685 *Note the initialization example: sample-use-suspend-secmem,
686 on how to use it.
687
688 'GCRYCTL_RESUME_SECMEM_WARN; Arguments: none'
689 Resume warning messages from the secure memory subsystem.
690 *Note the initialization example: sample-use-resume-secmem, on
691 how to use it.
692
693 'GCRYCTL_USE_SECURE_RNDPOOL; Arguments: none'
694 This command tells the PRNG to store random numbers in secure
695 memory. This command should be run right after
696 'gcry_check_version' and not later than the command
697 GCRYCTL_INIT_SECMEM. Note that in FIPS mode the secure memory
698 is always used.
699
700 'GCRYCTL_SET_RANDOM_SEED_FILE; Arguments: const char *filename'
701 This command specifies the file, which is to be used as seed
702 file for the PRNG. If the seed file is registered prior to
703 initialization of the PRNG, the seed file's content (if it
704 exists and seems to be valid) is fed into the PRNG pool.
705 After the seed file has been registered, the PRNG can be
706 signalled to write out the PRNG pool's content into the seed
707 file with the following command.
708
709 'GCRYCTL_UPDATE_RANDOM_SEED_FILE; Arguments: none'
710 Write out the PRNG pool's content into the registered seed
711 file.
712
713 Multiple instances of the applications sharing the same random
714 seed file can be started in parallel, in which case they will
715 read out the same pool and then race for updating it (the last
716 update overwrites earlier updates). They will differentiate
717 only by the weak entropy that is added in read_seed_file based
718 on the PID and clock, and up to 16 bytes of weak random
719 non-blockingly. The consequence is that the output of these
720 different instances is correlated to some extent. In a
721 perfect attack scenario, the attacker can control (or at least
722 guess) the PID and clock of the application, and drain the
723 system's entropy pool to reduce the "up to 16 bytes" above to
724 0. Then the dependencies of the initial states of the pools
725 are completely known. Note that this is not an issue if
726 random of 'GCRY_VERY_STRONG_RANDOM' quality is requested, as
727 in this case enough extra entropy gets mixed. It is also not
728 an issue when using rndgetentropy or rndoldlinux module,
729 because the module guarantees to read full 16 bytes and thus
730 there is no way for an attacker without kernel access to
731 control these 16 bytes.
732
733 'GCRYCTL_CLOSE_RANDOM_DEVICE; Arguments: none'
734 Try to close the random device. If on Unix system you call
735 fork(), the child process does no call exec(), and you do not
736 intend to use Libgcrypt in the child, it might be useful to
737 use this control code to close the inherited file descriptors
738 of the random device. If Libgcrypt is later used again by the
739 child, the device will be re-opened. On non-Unix systems this
740 control code is ignored.
741
742 'GCRYCTL_SET_VERBOSITY; Arguments: int level'
743 This command sets the verbosity of the logging. A level of 0
744 disables all extra logging, whereas positive numbers enable
745 more verbose logging. The level may be changed at any time
746 but be aware that no memory synchronization is done so the
747 effect of this command might not immediately show up in other
748 threads. This command may even be used prior to
749 'gcry_check_version'.
750
751 'GCRYCTL_SET_DEBUG_FLAGS; Arguments: unsigned int flags'
752 Set the debug flag bits as given by the argument. Be aware
753 that no memory synchronization is done so the effect of this
754 command might not immediately show up in other threads. The
755 debug flags are not considered part of the API and thus may
756 change without notice. As of now bit 0 enables debugging of
757 cipher functions and bit 1 debugging of
758 multi-precision-integers. This command may even be used prior
759 to 'gcry_check_version'.
760
761 'GCRYCTL_CLEAR_DEBUG_FLAGS; Arguments: unsigned int flags'
762 Set the debug flag bits as given by the argument. Be aware
763 that that no memory synchronization is done so the effect of
764 this command might not immediately show up in other threads.
765 This command may even be used prior to 'gcry_check_version'.
766
767 'GCRYCTL_DISABLE_INTERNAL_LOCKING; Arguments: none'
768 This command does nothing. It exists only for backward
769 compatibility.
770
771 'GCRYCTL_ANY_INITIALIZATION_P; Arguments: none'
772 This command returns true if the library has been basically
773 initialized. Such a basic initialization happens implicitly
774 with many commands to get certain internal subsystems running.
775 The common and suggested way to do this basic initialization
776 is by calling 'gcry_check_version'.
777
778 'GCRYCTL_INITIALIZATION_FINISHED; Arguments: none'
779 This command tells the library that the application has
780 finished the initialization.
781
782 'GCRYCTL_INITIALIZATION_FINISHED_P; Arguments: none'
783 This command returns true if the command
784 'GCRYCTL_INITIALIZATION_FINISHED' has already been run.
785
786 'GCRYCTL_SET_THREAD_CBS; Arguments: struct ath_ops *ath_ops'
787 This command is obsolete since version 1.6.
788
789 'GCRYCTL_FAST_POLL; Arguments: none'
790 Run a fast random poll.
791
792 'GCRYCTL_SET_RNDEGD_SOCKET; Arguments: const char *filename'
793 This command may be used to override the default name of the
794 EGD socket to connect to. It may be used only during
795 initialization as it is not thread safe. Changing the socket
796 name again is not supported. The function may return an error
797 if the given filename is too long for a local socket name.
798
799 EGD is an alternative random gatherer, used only on systems
800 lacking a proper random device.
801
802 'GCRYCTL_PRINT_CONFIG; Arguments: FILE *stream'
803 This command dumps information pertaining to the configuration
804 of the library to the given stream. If 'NULL' is given for
805 STREAM, the log system is used. This command may be used
806 before the initialization has been finished but not before a
807 'gcry_check_version'. Note that the macro 'estream_t' can be
808 used instead of 'gpgrt_stream_t'.
809
810 'GCRYCTL_OPERATIONAL_P; Arguments: none'
811 This command returns true if the library is in an operational
812 state. This information makes sense only in FIPS mode. In
813 contrast to other functions, this is a pure test function and
814 won't put the library into FIPS mode or change the internal
815 state. This command may be used before the initialization has
816 been finished but not before a 'gcry_check_version'.
817
818 'GCRYCTL_FIPS_MODE_P; Arguments: none'
819 This command returns true if the library is in FIPS mode.
820 Note, that this is no indication about the current state of
821 the library. This command may be used before the
822 initialization has been finished but not before a
823 'gcry_check_version'. An application may use this command or
824 the convenience macro below to check whether FIPS mode is
825 actually active.
826
827 -- Function: int gcry_fips_mode_active (void)
828
829 Returns true if the FIPS mode is active. Note that this
830 is implemented as a macro.
831
832 'GCRYCTL_FORCE_FIPS_MODE; Arguments: none'
833 Running this command puts the library into FIPS mode. If the
834 library is already in FIPS mode, a self-test is triggered and
835 thus the library will be put into operational state. This
836 command may be used before a call to 'gcry_check_version' and
837 that is actually the recommended way to let an application
838 switch the library into FIPS mode. Note that Libgcrypt will
839 reject an attempt to switch to FIPS mode during or after the
840 initialization.
841
842 'GCRYCTL_NO_FIPS_MODE; Arguments: none'
843 Running this command puts the library into non-FIPS mode.
844 This command may be used before a call to 'gcry_check_version'
845 and that is actually the recommended way to let an application
846 switch the library into non-FIPS mode. Note that Libgcrypt
847 will reject an attempt to switch to non-FIPS mode during or
848 after the initialization.
849
850 'GCRYCTL_SET_ENFORCED_FIPS_FLAG; Arguments: none'
851 This command is obsolete and has no effect; do not use it.
852
853 'GCRYCTL_SET_PREFERRED_RNG_TYPE; Arguments: int'
854 These are advisory commands to select a certain random number
855 generator. They are only advisory because libraries may not
856 know what an application actually wants or vice versa. Thus
857 Libgcrypt employs a priority check to select the actually used
858 RNG. If an applications selects a lower priority RNG but a
859 library requests a higher priority RNG, Libgcrypt will switch
860 to the higher priority RNG. Applications and libraries should
861 use these control codes before 'gcry_check_version'. The
862 available generators are:
863 'GCRY_RNG_TYPE_STANDARD'
864 A conservative standard generator based on the
865 "Continuously Seeded Pseudo Random Number Generator"
866 designed by Peter Gutmann.
867 'GCRY_RNG_TYPE_FIPS'
868 A deterministic random number generator conforming to the
869 document "NIST-Recommended Random Number Generator Based
870 on ANSI X9.31 Appendix A.2.4 Using the 3-Key Triple DES
871 and AES Algorithms" (2005-01-31). This implementation
872 uses the AES variant.
873 'GCRY_RNG_TYPE_SYSTEM'
874 A wrapper around the system's native RNG. On Unix system
875 these are usually the /dev/random and /dev/urandom
876 devices.
877 The default is 'GCRY_RNG_TYPE_STANDARD' unless FIPS mode as
878 been enabled; in which case 'GCRY_RNG_TYPE_FIPS' is used and
879 locked against further changes.
880
881 'GCRYCTL_GET_CURRENT_RNG_TYPE; Arguments: int *'
882 This command stores the type of the currently used RNG as an
883 integer value at the provided address.
884
885 'GCRYCTL_SELFTEST; Arguments: none'
886 This may be used at anytime to have the library run all
887 implemented self-tests. It works in standard and in FIPS
888 mode. Returns 0 on success or an error code on failure.
889
890 'GCRYCTL_DISABLE_HWF; Arguments: const char *name'
891
892 Libgcrypt detects certain features of the CPU at startup time.
893 For performance tests it is sometimes required not to use such
894 a feature. This option may be used to disable a certain
895 feature; i.e., Libgcrypt behaves as if this feature has not
896 been detected. This call can be used several times to disable
897 a set of features, or features may be given as a colon or
898 comma delimited string. The special feature "all" can be used
899 to disable all available features.
900
901 Note that the detection code might be run if the feature has
902 been disabled. This command must be used at initialization
903 time; i.e., before calling 'gcry_check_version'.
904
905 'GCRYCTL_REINIT_SYSCALL_CLAMP; Arguments: none'
906
907 Libgcrypt wraps blocking system calls with two functions calls
908 ("system call clamp") to give user land threading libraries a
909 hook for re-scheduling. This works by reading the system call
910 clamp from Libgpg-error at initialization time. However
911 sometimes Libgcrypt needs to be initialized before the user
912 land threading systems and at that point the system call clamp
913 has not been registered with Libgpg-error and in turn
914 Libgcrypt would not use them. The control code can be used to
915 tell Libgcrypt that a system call clamp has now been
916 registered with Libgpg-error and advise Libgcrypt to read the
917 clamp again. Obviously this control code may only be used
918 before a second thread is started in a process.
919
920 'GCRYCTL_FIPS_SERVICE_INDICATOR_CIPHER; Arguments: enum gcry_cipher_algos [, enum gcry_cipher_modes]'
921
922 Check if the given symmetric cipher and optional cipher mode
923 combination is approved under the current FIPS 140-3
924 certification. If the combination is approved, this function
925 returns 'GPG_ERR_NO_ERROR'. Otherwise 'GPG_ERR_NOT_SUPPORTED'
926 is returned.
927
928 'GCRYCTL_FIPS_SERVICE_INDICATOR_KDF; Arguments: enum gcry_kdf_algos'
929
930 Check if the given KDF is approved under the current FIPS
931 140-3 certification. If the KDF is approved, this function
932 returns 'GPG_ERR_NO_ERROR'. Otherwise 'GPG_ERR_NOT_SUPPORTED'
933 is returned.
934
935 'GCRYCTL_FIPS_SERVICE_INDICATOR_FUNCTION; Arguments: const char *'
936
937 Check if the given function is approved under the current FIPS
938 140-3 certification. If the function is approved, this
939 function returns 'GPG_ERR_NO_ERROR' (other restrictions might
940 still apply). Otherwise 'GPG_ERR_NOT_SUPPORTED' is returned.
941
942 'GCRYCTL_FIPS_SERVICE_INDICATOR_MAC; Arguments: enum gcry_mac_algos'
943
944 Check if the given MAC is approved under the current FIPS
945 140-3 certification. If the MAC is approved, this function
946 returns 'GPG_ERR_NO_ERROR'. Otherwise 'GPG_ERR_NOT_SUPPORTED'
947 is returned.
948
949 'GCRYCTL_FIPS_SERVICE_INDICATOR_MD; Arguments: enum gcry_md_algos'
950
951 Check if the given message digest algorithm is approved under
952 the current FIPS 140-3 certification. If the algorithm is
953 approved, this function returns 'GPG_ERR_NO_ERROR'. Otherwise
954 'GPG_ERR_NOT_SUPPORTED' is returned.
955
956 'GCRYCTL_FIPS_SERVICE_INDICATOR_PK_FLAGS; Arguments: const char *'
957
958 Check if the given public key operation flag or s-expression
959 object name is approved under the current FIPS 140-3
960 certification. If the flag is approved, this function returns
961 'GPG_ERR_NO_ERROR'.
962
963 Otherwise 'GPG_ERR_NOT_SUPPORTED' is returned.
964
965 For compound s-expression objects, if the object name is
966 allowed, the user is responsible to check also the internal
967 members. For example:
968
969 gcry_sexp_t s_sig = NULL;
970 gcry_md_hd_t hd = NULL;
971 gcry_sexp_t s_sk = NULL;
972 const char *data_tmpl = "(data(flags pss)(hash %s %b)(salt-length 1:0))";
973
974 if (err = gcry_control(GCRYCTL_FIPS_SERVICE_INDICATOR_FUNCTION, "gcry_md_open") &&
975 err = gcry_control(GCRYCTL_FIPS_SERVICE_INDICATOR_MD, GCRY_MD_SHA512) &&
976 err = gcry_md_open (&hd, GCRY_MD_SHA512, 0))
977 {
978 printf ("gcry_md_open failed: %s", gpg_strerror (err));
979 return;
980 }
981 gcry_md_write (hd, buffer, buflen);
982
983 /* initialize the key in s_sk */
984
985 if (err = gcry_control(GCRYCTL_FIPS_SERVICE_INDICATOR_FUNCTION, "gcry_pk_hash_sign") &&
986 err = gcry_control(GCRYCTL_FIPS_SERVICE_INDICATOR_PK_FLAGS, "data") &&
987 err = gcry_control(GCRYCTL_FIPS_SERVICE_INDICATOR_PK_FLAGS, "flags") &&
988 err = gcry_control(GCRYCTL_FIPS_SERVICE_INDICATOR_PK_FLAGS, "pss") &&
989 err = gcry_control(GCRYCTL_FIPS_SERVICE_INDICATOR_PK_FLAGS, "hash") &&
990 err = gcry_control(GCRYCTL_FIPS_SERVICE_INDICATOR_PK_FLAGS, "salt-length")
991 err = gcry_pk_hash_sign (&s_sig, data_tmpl, s_sk, hd, NULL))
992 {
993 printf ("gcry_pk_hash_sign failed: %s", gpg_strerror (err));
994 return;
995 }
996 /* ok */
997
998 'GCRYCTL_FIPS_SERVICE_INDICATOR; Arguments: none'
999 This commands provides "dynamic" service indicator.
1000
1001 After a function call (of the use of security services), this
1002 command can be used to check if the call is valid or not. If
1003 the computation is done in an approved way, it returns
1004 'GPG_ERR_NO_ERROR'. Otherwise 'GPG_ERR_NOT_SUPPORTED' is
1005 returned.
1006
1007 An application may use this command directly or use the
1008 convenience macro below.
1009
1010 -- Function: gcry_error_t gcry_get_fips_service_indicator
1011 (void)
1012
1013 Returns 'GPG_ERR_NO_ERROR' if a preceeding function call
1014 is valid. Otherwise 'GPG_ERR_NOT_SUPPORTED' is returned.
1015
1016 'GCRYCTL_FIPS_REJECT_NON_FIPS; Arguments: unsigned int flags'
1017 In Libgcrypt 1.10, static implicit indicator is used; For an
1018 approved function (which can be checked by
1019 GCRYCTL_FIPS_SERVICE_INDICATOR_FUNCTION command) with an
1020 approved algo/operation (which can be checked
1021 GCRYCTL_FIPS_SERVICE_INDICATOR_* command), success of the
1022 function call means that it's valid and error return
1023 (rejection) means it's invalid. This command controls thread
1024 specific behavior of the rejection.
1025
1026 When using "dynamic" service indicator, this command with
1027 FLAGS=0 disables all rejections.
1028 gcry_control (GCRYCTL_FIPS_REJECT_NON_FIPS, 0);
1029
1030 
1031 File: gcrypt.info, Node: Error Handling, Prev: Controlling the library, Up: Generalities
1032
1033 3.2 Error Handling
1034 ==================
1035
1036 Many functions in Libgcrypt can return an error if they fail. For this
1037 reason, the application should always catch the error condition and take
1038 appropriate measures, for example by releasing the resources and passing
1039 the error up to the caller, or by displaying a descriptive message to
1040 the user and cancelling the operation.
1041
1042 Some error values do not indicate a system error or an error in the
1043 operation, but the result of an operation that failed properly. For
1044 example, if you try to decrypt a tampered message, the decryption will
1045 fail. Another error value actually means that the end of a data buffer
1046 or list has been reached. The following descriptions explain for many
1047 error codes what they mean usually. Some error values have specific
1048 meanings if returned by a certain functions. Such cases are described
1049 in the documentation of those functions.
1050
1051 Libgcrypt uses the 'libgpg-error' library. This allows sharing the
1052 error codes with other components of the GnuPG system, and to pass error
1053 values transparently from the crypto engine, or some helper application
1054 of the crypto engine, to the user. This way no information is lost. As
1055 a consequence, Libgcrypt does not use its own identifiers for error
1056 codes, but uses those provided by 'libgpg-error'. They usually start
1057 with 'GPG_ERR_'.
1058
1059 However, Libgcrypt does provide aliases for the functions defined in
1060 libgpg-error, which might be preferred for name space consistency.
1061
1062 Most functions in Libgcrypt return an error code in the case of
1063 failure. For this reason, the application should always catch the error
1064 condition and take appropriate measures, for example by releasing the
1065 resources and passing the error up to the caller, or by displaying a
1066 descriptive message to the user and canceling the operation.
1067
1068 Some error values do not indicate a system error or an error in the
1069 operation, but the result of an operation that failed properly.
1070
1071 GnuPG components, including Libgcrypt, use an extra library named
1072 libgpg-error to provide a common error handling scheme. For more
1073 information on libgpg-error, see the according manual.
1074
1075 * Menu:
1076
1077 * Error Values:: The error value and what it means.
1078 * Error Sources:: A list of important error sources.
1079 * Error Codes:: A list of important error codes.
1080 * Error Strings:: How to get a descriptive string from a value.
1081
1082 
1083 File: gcrypt.info, Node: Error Values, Next: Error Sources, Up: Error Handling
1084
1085 3.2.1 Error Values
1086 ------------------
1087
1088 -- Data type: gcry_err_code_t
1089 The 'gcry_err_code_t' type is an alias for the 'libgpg-error' type
1090 'gpg_err_code_t'. The error code indicates the type of an error,
1091 or the reason why an operation failed.
1092
1093 A list of important error codes can be found in the next section.
1094
1095 -- Data type: gcry_err_source_t
1096 The 'gcry_err_source_t' type is an alias for the 'libgpg-error'
1097 type 'gpg_err_source_t'. The error source has not a precisely
1098 defined meaning. Sometimes it is the place where the error
1099 happened, sometimes it is the place where an error was encoded into
1100 an error value. Usually the error source will give an indication
1101 to where to look for the problem. This is not always true, but it
1102 is attempted to achieve this goal.
1103
1104 A list of important error sources can be found in the next section.
1105
1106 -- Data type: gcry_error_t
1107 The 'gcry_error_t' type is an alias for the 'libgpg-error' type
1108 'gpg_error_t'. An error value like this has always two components:
1109 an error code and an error source. Both together form the error
1110 value.
1111
1112 Thus, the error value can not be directly compared against an error
1113 code, but the accessor functions described below must be used.
1114 However, it is guaranteed that only 0 is used to indicate success
1115 ('GPG_ERR_NO_ERROR'), and that in this case all other parts of the
1116 error value are set to 0, too.
1117
1118 Note that in Libgcrypt, the error source is used purely for
1119 diagnostic purposes. Only the error code should be checked to test
1120 for a certain outcome of a function. The manual only documents the
1121 error code part of an error value. The error source is left
1122 unspecified and might be anything.
1123
1124 -- Function: gcry_err_code_t gcry_err_code (gcry_error_t ERR)
1125 The static inline function 'gcry_err_code' returns the
1126 'gcry_err_code_t' component of the error value ERR. This function
1127 must be used to extract the error code from an error value in order
1128 to compare it with the 'GPG_ERR_*' error code macros.
1129
1130 -- Function: gcry_err_source_t gcry_err_source (gcry_error_t ERR)
1131 The static inline function 'gcry_err_source' returns the
1132 'gcry_err_source_t' component of the error value ERR. This
1133 function must be used to extract the error source from an error
1134 value in order to compare it with the 'GPG_ERR_SOURCE_*' error
1135 source macros.
1136
1137 -- Function: gcry_error_t gcry_err_make (gcry_err_source_t SOURCE,
1138 gcry_err_code_t CODE)
1139 The static inline function 'gcry_err_make' returns the error value
1140 consisting of the error source SOURCE and the error code CODE.
1141
1142 This function can be used in callback functions to construct an
1143 error value to return it to the library.
1144
1145 -- Function: gcry_error_t gcry_error (gcry_err_code_t CODE)
1146 The static inline function 'gcry_error' returns the error value
1147 consisting of the default error source and the error code CODE.
1148
1149 For GCRY applications, the default error source is
1150 'GPG_ERR_SOURCE_USER_1'. You can define 'GCRY_ERR_SOURCE_DEFAULT'
1151 before including 'gcrypt.h' to change this default.
1152
1153 This function can be used in callback functions to construct an
1154 error value to return it to the library.
1155
1156 The 'libgpg-error' library provides error codes for all system error
1157 numbers it knows about. If ERR is an unknown error number, the error
1158 code 'GPG_ERR_UNKNOWN_ERRNO' is used. The following functions can be
1159 used to construct error values from system errno numbers.
1160
1161 -- Function: gcry_error_t gcry_err_make_from_errno
1162 (gcry_err_source_t SOURCE, int ERR)
1163 The function 'gcry_err_make_from_errno' is like 'gcry_err_make',
1164 but it takes a system error like 'errno' instead of a
1165 'gcry_err_code_t' error code.
1166
1167 -- Function: gcry_error_t gcry_error_from_errno (int ERR)
1168 The function 'gcry_error_from_errno' is like 'gcry_error', but it
1169 takes a system error like 'errno' instead of a 'gcry_err_code_t'
1170 error code.
1171
1172 Sometimes you might want to map system error numbers to error codes
1173 directly, or map an error code representing a system error back to the
1174 system error number. The following functions can be used to do that.
1175
1176 -- Function: gcry_err_code_t gcry_err_code_from_errno (int ERR)
1177 The function 'gcry_err_code_from_errno' returns the error code for
1178 the system error ERR. If ERR is not a known system error, the
1179 function returns 'GPG_ERR_UNKNOWN_ERRNO'.
1180
1181 -- Function: int gcry_err_code_to_errno (gcry_err_code_t ERR)
1182 The function 'gcry_err_code_to_errno' returns the system error for
1183 the error code ERR. If ERR is not an error code representing a
1184 system error, or if this system error is not defined on this
1185 system, the function returns '0'.
1186
1187 
1188 File: gcrypt.info, Node: Error Sources, Next: Error Codes, Prev: Error Values, Up: Error Handling
1189
1190 3.2.2 Error Sources
1191 -------------------
1192
1193 The library 'libgpg-error' defines an error source for every component
1194 of the GnuPG system. The error source part of an error value is not
1195 well defined. As such it is mainly useful to improve the diagnostic
1196 error message for the user.
1197
1198 If the error code part of an error value is '0', the whole error
1199 value will be '0'. In this case the error source part is of course
1200 'GPG_ERR_SOURCE_UNKNOWN'.
1201
1202 The list of error sources that might occur in applications using
1203 Libgcrypt is:
1204
1205 'GPG_ERR_SOURCE_UNKNOWN'
1206 The error source is not known. The value of this error source is
1207 '0'.
1208
1209 'GPG_ERR_SOURCE_GPGME'
1210 The error source is GPGME itself.
1211
1212 'GPG_ERR_SOURCE_GPG'
1213 The error source is GnuPG, which is the crypto engine used for the
1214 OpenPGP protocol.
1215
1216 'GPG_ERR_SOURCE_GPGSM'
1217 The error source is GPGSM, which is the crypto engine used for the
1218 OpenPGP protocol.
1219
1220 'GPG_ERR_SOURCE_GCRYPT'
1221 The error source is 'libgcrypt', which is used by crypto engines to
1222 perform cryptographic operations.
1223
1224 'GPG_ERR_SOURCE_GPGAGENT'
1225 The error source is 'gpg-agent', which is used by crypto engines to
1226 perform operations with the secret key.
1227
1228 'GPG_ERR_SOURCE_PINENTRY'
1229 The error source is 'pinentry', which is used by 'gpg-agent' to
1230 query the passphrase to unlock a secret key.
1231
1232 'GPG_ERR_SOURCE_SCD'
1233 The error source is the SmartCard Daemon, which is used by
1234 'gpg-agent' to delegate operations with the secret key to a
1235 SmartCard.
1236
1237 'GPG_ERR_SOURCE_KEYBOX'
1238 The error source is 'libkbx', a library used by the crypto engines
1239 to manage local keyrings.
1240
1241 'GPG_ERR_SOURCE_USER_1'
1242 'GPG_ERR_SOURCE_USER_2'
1243 'GPG_ERR_SOURCE_USER_3'
1244 'GPG_ERR_SOURCE_USER_4'
1245 These error sources are not used by any GnuPG component and can be
1246 used by other software. For example, applications using Libgcrypt
1247 can use them to mark error values coming from callback handlers.
1248 Thus 'GPG_ERR_SOURCE_USER_1' is the default for errors created with
1249 'gcry_error' and 'gcry_error_from_errno', unless you define
1250 'GCRY_ERR_SOURCE_DEFAULT' before including 'gcrypt.h'.
1251
1252 
1253 File: gcrypt.info, Node: Error Codes, Next: Error Strings, Prev: Error Sources, Up: Error Handling
1254
1255 3.2.3 Error Codes
1256 -----------------
1257
1258 The library 'libgpg-error' defines many error values. The following
1259 list includes the most important error codes.
1260
1261 'GPG_ERR_EOF'
1262 This value indicates the end of a list, buffer or file.
1263
1264 'GPG_ERR_NO_ERROR'
1265 This value indicates success. The value of this error code is '0'.
1266 Also, it is guaranteed that an error value made from the error code
1267 '0' will be '0' itself (as a whole). This means that the error
1268 source information is lost for this error code, however, as this
1269 error code indicates that no error occurred, this is generally not
1270 a problem.
1271
1272 'GPG_ERR_GENERAL'
1273 This value means that something went wrong, but either there is not
1274 enough information about the problem to return a more useful error
1275 value, or there is no separate error value for this type of
1276 problem.
1277
1278 'GPG_ERR_ENOMEM'
1279 This value means that an out-of-memory condition occurred.
1280
1281 'GPG_ERR_E...'
1282 System errors are mapped to GPG_ERR_EFOO where FOO is the symbol
1283 for the system error.
1284
1285 'GPG_ERR_INV_VALUE'
1286 This value means that some user provided data was out of range.
1287
1288 'GPG_ERR_UNUSABLE_PUBKEY'
1289 This value means that some recipients for a message were invalid.
1290
1291 'GPG_ERR_UNUSABLE_SECKEY'
1292 This value means that some signers were invalid.
1293
1294 'GPG_ERR_NO_DATA'
1295 This value means that data was expected where no data was found.
1296
1297 'GPG_ERR_CONFLICT'
1298 This value means that a conflict of some sort occurred.
1299
1300 'GPG_ERR_NOT_IMPLEMENTED'
1301 This value indicates that the specific function (or operation) is
1302 not implemented. This error should never happen. It can only
1303 occur if you use certain values or configuration options which do
1304 not work, but for which we think that they should work at some
1305 later time.
1306
1307 'GPG_ERR_DECRYPT_FAILED'
1308 This value indicates that a decryption operation was unsuccessful.
1309
1310 'GPG_ERR_WRONG_KEY_USAGE'
1311 This value indicates that a key is not used appropriately.
1312
1313 'GPG_ERR_NO_SECKEY'
1314 This value indicates that no secret key for the user ID is
1315 available.
1316
1317 'GPG_ERR_UNSUPPORTED_ALGORITHM'
1318 This value means a verification failed because the cryptographic
1319 algorithm is not supported by the crypto backend.
1320
1321 'GPG_ERR_BAD_SIGNATURE'
1322 This value means a verification failed because the signature is
1323 bad.
1324
1325 'GPG_ERR_NO_PUBKEY'
1326 This value means a verification failed because the public key is
1327 not available.
1328
1329 'GPG_ERR_NOT_OPERATIONAL'
1330 This value means that the library is not yet in state which allows
1331 using this function. This error code is in particular returned if
1332 Libgcrypt is operated in FIPS mode and the internal state of the
1333 library does not yet or not anymore allow the use of a service.
1334
1335 This error code is only available with newer libgpg-error versions,
1336 thus you might see "invalid error code" when passing this to
1337 'gpg_strerror'. The numeric value of this error code is 176.
1338
1339 'GPG_ERR_USER_1'
1340 'GPG_ERR_USER_2'
1341 '...'
1342 'GPG_ERR_USER_16'
1343 These error codes are not used by any GnuPG component and can be
1344 freely used by other software. Applications using Libgcrypt might
1345 use them to mark specific errors returned by callback handlers if
1346 no suitable error codes (including the system errors) for these
1347 errors exist already.
1348
1349 
1350 File: gcrypt.info, Node: Error Strings, Prev: Error Codes, Up: Error Handling
1351
1352 3.2.4 Error Strings
1353 -------------------
1354
1355 -- Function: const char * gcry_strerror (gcry_error_t ERR)
1356 The function 'gcry_strerror' returns a pointer to a statically
1357 allocated string containing a description of the error code
1358 contained in the error value ERR. This string can be used to
1359 output a diagnostic message to the user.
1360
1361 -- Function: const char * gcry_strsource (gcry_error_t ERR)
1362 The function 'gcry_strsource' returns a pointer to a statically
1363 allocated string containing a description of the error source
1364 contained in the error value ERR. This string can be used to
1365 output a diagnostic message to the user.
1366
1367 The following example illustrates the use of the functions described
1368 above:
1369
1370 {
1371 gcry_cipher_hd_t handle;
1372 gcry_error_t err = 0;
1373
1374 err = gcry_cipher_open (&handle, GCRY_CIPHER_AES,
1375 GCRY_CIPHER_MODE_CBC, 0);
1376 if (err)
1377 {
1378 fprintf (stderr, "Failure: %s/%s\n",
1379 gcry_strsource (err),
1380 gcry_strerror (err));
1381 }
1382 }
1383
1384 
1385 File: gcrypt.info, Node: Handler Functions, Next: Symmetric cryptography, Prev: Generalities, Up: Top
1386
1387 4 Handler Functions
1388 *******************
1389
1390 Libgcrypt makes it possible to install so called 'handler functions',
1391 which get called by Libgcrypt in case of certain events.
1392
1393 * Menu:
1394
1395 * Progress handler:: Using a progress handler function.
1396 * Allocation handler:: Using special memory allocation functions.
1397 * Error handler:: Using error handler functions.
1398 * Logging handler:: Using a special logging function.
1399
1400 
1401 File: gcrypt.info, Node: Progress handler, Next: Allocation handler, Up: Handler Functions
1402
1403 4.1 Progress handler
1404 ====================
1405
1406 It is often useful to retrieve some feedback while long running
1407 operations are performed.
1408
1409 -- Data type: gcry_handler_progress_t
1410 Progress handler functions have to be of the type
1411 'gcry_handler_progress_t', which is defined as:
1412
1413 'void (*gcry_handler_progress_t) (void *, const char *, int, int,
1414 int)'
1415
1416 The following function may be used to register a handler function for
1417 this purpose.
1418
1419 -- Function: void gcry_set_progress_handler (gcry_handler_progress_t
1420 CB, void *CB_DATA)
1421
1422 This function installs CB as the 'Progress handler' function. It
1423 may be used only during initialization. CB must be defined as
1424 follows:
1425
1426 void
1427 my_progress_handler (void *CB_DATA, const char *WHAT,
1428 int PRINTCHAR, int CURRENT, int TOTAL)
1429 {
1430 /* Do something. */
1431 }
1432
1433 A description of the arguments of the progress handler function
1434 follows.
1435
1436 CB_DATA
1437 The argument provided in the call to
1438 'gcry_set_progress_handler'.
1439 WHAT
1440 A string identifying the type of the progress output. The
1441 following values for WHAT are defined:
1442
1443 'need_entropy'
1444 Not enough entropy is available. TOTAL holds the number
1445 of required bytes.
1446
1447 'wait_dev_random'
1448 Waiting to re-open a random device. TOTAL gives the
1449 number of seconds until the next try.
1450
1451 'primegen'
1452 Values for PRINTCHAR:
1453 '\n'
1454 Prime generated.
1455 '!'
1456 Need to refresh the pool of prime numbers.
1457 '<, >'
1458 Number of bits adjusted.
1459 '^'
1460 Searching for a generator.
1461 '.'
1462 Fermat test on 10 candidates failed.
1463 ':'
1464 Restart with a new random value.
1465 '+'
1466 Rabin-Miller test passed.
1467
1468 
1469 File: gcrypt.info, Node: Allocation handler, Next: Error handler, Prev: Progress handler, Up: Handler Functions
1470
1471 4.2 Allocation handler
1472 ======================
1473
1474 It is possible to make Libgcrypt use special memory allocation functions
1475 instead of the built-in ones.
1476
1477 Memory allocation functions are of the following types:
1478 -- Data type: gcry_handler_alloc_t
1479 This type is defined as: 'void *(*gcry_handler_alloc_t) (size_t
1480 n)'.
1481 -- Data type: gcry_handler_secure_check_t
1482 This type is defined as: 'int *(*gcry_handler_secure_check_t)
1483 (const void *)'.
1484 -- Data type: gcry_handler_realloc_t
1485 This type is defined as: 'void *(*gcry_handler_realloc_t) (void *p,
1486 size_t n)'.
1487 -- Data type: gcry_handler_free_t
1488 This type is defined as: 'void *(*gcry_handler_free_t) (void *)'.
1489
1490 Special memory allocation functions can be installed with the
1491 following function:
1492
1493 -- Function: void gcry_set_allocation_handler (gcry_handler_alloc_t
1494 FUNC_ALLOC, gcry_handler_alloc_t FUNC_ALLOC_SECURE,
1495 gcry_handler_secure_check_t FUNC_SECURE_CHECK,
1496 gcry_handler_realloc_t FUNC_REALLOC, gcry_handler_free_t
1497 FUNC_FREE)
1498 Install the provided functions and use them instead of the built-in
1499 functions for doing memory allocation. Using this function is in
1500 general not recommended because the standard Libgcrypt allocation
1501 functions are guaranteed to zeroize memory if needed.
1502
1503 This function may be used only during initialization and may not be
1504 used in FIPS mode.
1505
1506 
1507 File: gcrypt.info, Node: Error handler, Next: Logging handler, Prev: Allocation handler, Up: Handler Functions
1508
1509 4.3 Error handler
1510 =================
1511
1512 The following functions may be used to register handler functions that
1513 are called by Libgcrypt in case certain error conditions occur. They
1514 may and should be registered prior to calling 'gcry_check_version'.
1515
1516 -- Data type: gcry_handler_no_mem_t
1517 This type is defined as: 'int (*gcry_handler_no_mem_t) (void *,
1518 size_t, unsigned int)'
1519 -- Function: void gcry_set_outofcore_handler (gcry_handler_no_mem_t
1520 FUNC_NO_MEM, void *CB_DATA)
1521 This function registers FUNC_NO_MEM as 'out-of-core handler', which
1522 means that it will be called in the case of not having enough
1523 memory available. The handler is called with 3 arguments: The
1524 first one is the pointer CB_DATA as set with this function, the
1525 second is the requested memory size and the last being a flag. If
1526 bit 0 of the flag is set, secure memory has been requested. The
1527 handler should either return true to indicate that Libgcrypt should
1528 try again allocating memory or return false to let Libgcrypt use
1529 its default fatal error handler.
1530
1531 -- Data type: gcry_handler_error_t
1532 This type is defined as: 'void (*gcry_handler_error_t) (void *,
1533 int, const char *)'
1534
1535 -- Function: void gcry_set_fatalerror_handler (gcry_handler_error_t
1536 FUNC_ERROR, void *CB_DATA)
1537 This function registers FUNC_ERROR as 'error handler', which means
1538 that it will be called in error conditions.
1539
1540 
1541 File: gcrypt.info, Node: Logging handler, Prev: Error handler, Up: Handler Functions
1542
1543 4.4 Logging handler
1544 ===================
1545
1546 Libgcrypt provides a way to install a different log handler to be used
1547 instead of the internal. Only few programs make use of it and thus it
1548 has been deprecated. If a log handler is not installed and since
1549 version 1.11 Libgcrypt uses the logging facility of GpgRT (aka
1550 Libgpg-error). They are more flexible that Libgcrypt's old logging
1551 functions and given that GpgRT is anyway a dependency of Libgcrypt it is
1552 better to always use the GpgRT logging facilities.
1553
1554 -- Data type: gcry_handler_log_t
1555 This type is defined as: 'void (*gcry_handler_log_t) (void *, int,
1556 const char *, va_list)'
1557
1558 -- Function: void gcry_set_log_handler (gcry_handler_log_t FUNC_LOG,
1559 void *CB_DATA)
1560 This deprecated function registers FUNC_LOG as 'logging handler',
1561 which means that it will be called in case Libgcrypt wants to log a
1562 message. This function may and should be used prior to calling
1563 'gcry_check_version'.
1564
1565 
1566 File: gcrypt.info, Node: Symmetric cryptography, Next: Public Key cryptography, Prev: Handler Functions, Up: Top
1567
1568 5 Symmetric cryptography
1569 ************************
1570
1571 The cipher functions are used for symmetrical cryptography, i.e.,
1572 cryptography using a shared key. The programming model follows an
1573 open/process/close paradigm and is in that similar to other building
1574 blocks provided by Libgcrypt.
1575
1576 * Menu:
1577
1578 * Available ciphers:: List of ciphers supported by the library.
1579 * Available cipher modes:: List of cipher modes supported by the library.
1580 * Working with cipher handles:: How to perform operations related to cipher handles.
1581 * General cipher functions:: General cipher functions independent of cipher handles.
1582
1583 
1584 File: gcrypt.info, Node: Available ciphers, Next: Available cipher modes, Up: Symmetric cryptography
1585
1586 5.1 Available ciphers
1587 =====================
1588
1589 'GCRY_CIPHER_NONE'
1590 This is not a real algorithm but used by some functions as error
1591 return. The value always evaluates to false.
1592
1593 'GCRY_CIPHER_IDEA'
1594 This is the IDEA algorithm.
1595
1596 'GCRY_CIPHER_3DES'
1597 Triple-DES with 3 keys as EDE. The key size of this algorithm is
1598 168 bits but you have to pass 192 bits because the most significant
1599 bits of each byte are ignored.
1600
1601 'GCRY_CIPHER_CAST5'
1602 CAST128-5 block cipher algorithm. The key size is 128 bits.
1603
1604 'GCRY_CIPHER_BLOWFISH'
1605 The blowfish algorithm. The supported key sizes are 8 to 576 bits
1606 in 8 bit increments.
1607
1608 'GCRY_CIPHER_SAFER_SK128'
1609 Reserved and not currently implemented.
1610
1611 'GCRY_CIPHER_DES_SK'
1612 Reserved and not currently implemented.
1613
1614 'GCRY_CIPHER_AES'
1615 'GCRY_CIPHER_AES128'
1616 'GCRY_CIPHER_RIJNDAEL'
1617 'GCRY_CIPHER_RIJNDAEL128'
1618 AES (Rijndael) with a 128 bit key.
1619
1620 'GCRY_CIPHER_AES192'
1621 'GCRY_CIPHER_RIJNDAEL192'
1622 AES (Rijndael) with a 192 bit key.
1623
1624 'GCRY_CIPHER_AES256'
1625 'GCRY_CIPHER_RIJNDAEL256'
1626 AES (Rijndael) with a 256 bit key.
1627
1628 'GCRY_CIPHER_TWOFISH'
1629 The Twofish algorithm with a 256 bit key.
1630
1631 'GCRY_CIPHER_TWOFISH128'
1632 The Twofish algorithm with a 128 bit key.
1633
1634 'GCRY_CIPHER_ARCFOUR'
1635 An algorithm which is 100% compatible with RSA Inc.'s RC4
1636 algorithm. Note that this is a stream cipher and must be used very
1637 carefully to avoid a couple of weaknesses.
1638
1639 'GCRY_CIPHER_DES'
1640 Standard DES with a 56 bit key. You need to pass 64 bits but the
1641 high bits of each byte are ignored. Note, that this is a weak
1642 algorithm which can be broken in reasonable time using a brute
1643 force approach.
1644
1645 'GCRY_CIPHER_SERPENT128'
1646 'GCRY_CIPHER_SERPENT192'
1647 'GCRY_CIPHER_SERPENT256'
1648 The Serpent cipher from the AES contest.
1649
1650 'GCRY_CIPHER_RFC2268_40'
1651 'GCRY_CIPHER_RFC2268_128'
1652 Ron's Cipher 2 in the 40 and 128 bit variants.
1653
1654 'GCRY_CIPHER_SEED'
1655 A 128 bit cipher as described by RFC4269.
1656
1657 'GCRY_CIPHER_CAMELLIA128'
1658 'GCRY_CIPHER_CAMELLIA192'
1659 'GCRY_CIPHER_CAMELLIA256'
1660 The Camellia cipher by NTT. See
1661 <http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications.html>.
1662
1663 'GCRY_CIPHER_SALSA20'
1664 This is the Salsa20 stream cipher.
1665
1666 'GCRY_CIPHER_SALSA20R12'
1667 This is the Salsa20/12 - reduced round version of Salsa20 stream
1668 cipher.
1669
1670 'GCRY_CIPHER_GOST28147'
1671 The GOST 28147-89 cipher, defined in the respective GOST standard.
1672 Translation of this GOST into English is provided in the RFC-5830.
1673
1674 'GCRY_CIPHER_GOST28147_MESH'
1675 The GOST 28147-89 cipher, defined in the respective GOST standard.
1676 Translation of this GOST into English is provided in the RFC-5830.
1677 This cipher will use CryptoPro keymeshing as defined in RFC 4357 if
1678 it has to be used for the selected parameter set.
1679
1680 'GCRY_CIPHER_CHACHA20'
1681 This is the ChaCha20 stream cipher.
1682
1683 'GCRY_CIPHER_SM4'
1684 A 128 bit cipher by the State Cryptography Administration of China
1685 (SCA). See <https://tools.ietf.org/html/draft-ribose-cfrg-sm4-10>.
1686
1687 'GCRY_CIPHER_ARIA128'
1688 'GCRY_CIPHER_ARIA192'
1689 'GCRY_CIPHER_ARIA256'
1690 ARIA is a general-purpose block cipher algorithm developed by
1691 Korean cryptographers in 2003. It was established as a Korean
1692 standard block cipher algorithm in 2004. See
1693 <https://www.rfc-editor.org/rfc/rfc5794.html>.
1694
1695 
1696 File: gcrypt.info, Node: Available cipher modes, Next: Working with cipher handles, Prev: Available ciphers, Up: Symmetric cryptography
1697
1698 5.2 Available cipher modes
1699 ==========================
1700
1701 'GCRY_CIPHER_MODE_NONE'
1702 No mode specified. This should not be used. The only exception is
1703 that if Libgcrypt is not used in FIPS mode and if any debug flag
1704 has been set, this mode may be used to bypass the actual
1705 encryption.
1706
1707 'GCRY_CIPHER_MODE_ECB'
1708 Electronic Codebook mode.
1709
1710 'GCRY_CIPHER_MODE_CFB'
1711 'GCRY_CIPHER_MODE_CFB8'
1712 Cipher Feedback mode. For 'GCRY_CIPHER_MODE_CFB' the shift size
1713 equals the block size of the cipher (e.g., for AES it is CFB-128).
1714 For 'GCRY_CIPHER_MODE_CFB8' the shift size is 8 bits but that
1715 variant is not yet available.
1716
1717 'GCRY_CIPHER_MODE_CBC'
1718 Cipher Block Chaining mode.
1719
1720 'GCRY_CIPHER_MODE_STREAM'
1721 Stream mode, only to be used with stream cipher algorithms.
1722
1723 'GCRY_CIPHER_MODE_OFB'
1724 Output Feedback mode.
1725
1726 'GCRY_CIPHER_MODE_CTR'
1727 Counter mode.
1728
1729 'GCRY_CIPHER_MODE_AESWRAP'
1730 This mode is used to implement the AES-Wrap algorithm according to
1731 RFC-3394. It may be used with any 128 bit block length algorithm,
1732 however the specs require one of the 3 AES algorithms. These
1733 special conditions apply: If 'gcry_cipher_setiv' has not been used,
1734 the standard IV is used; if it has been used, the lower 64 bits of
1735 the IV are used as the Alternative Initial Value. On encryption
1736 the provided output buffer must be 64 bits (8 bytes) larger than
1737 the input buffer; in-place encryption is still allowed. On
1738 decryption the output buffer may be specified 64 bits (8 bytes)
1739 shorter than then input buffer. As per specs the input length must
1740 be at least 128 bits and the length must be a multiple of 64 bits.
1741
1742 'GCRY_CIPHER_MODE_CCM'
1743 Counter with CBC-MAC mode is an Authenticated Encryption with
1744 Associated Data (AEAD) block cipher mode, which is specified in
1745 'NIST Special Publication 800-38C' and RFC 3610.
1746
1747 'GCRY_CIPHER_MODE_GCM'
1748 Galois/Counter Mode (GCM) is an Authenticated Encryption with
1749 Associated Data (AEAD) block cipher mode, which is specified in
1750 'NIST Special Publication 800-38D'.
1751
1752 'GCRY_CIPHER_MODE_POLY1305'
1753 This mode implements the Poly1305 Authenticated Encryption with
1754 Associated Data (AEAD) mode according to RFC-8439. This mode can
1755 be used with ChaCha20 stream cipher.
1756
1757 'GCRY_CIPHER_MODE_OCB'
1758 OCB is an Authenticated Encryption with Associated Data (AEAD)
1759 block cipher mode, which is specified in RFC-7253. Supported tag
1760 lengths are 128, 96, and 64 bits with the default being 128 bits.
1761 To switch to a different tag length, 'gcry_cipher_ctl' using the
1762 command 'GCRYCTL_SET_TAGLEN' and the address of an 'int' variable
1763 set to 12 (for 96 bits) or 8 (for 64 bits) provided for the
1764 'buffer' argument and 'sizeof(int)' for 'buflen'.
1765
1766 Note that the use of 'gcry_cipher_final' is required.
1767
1768 'GCRY_CIPHER_MODE_XTS'
1769 XEX-based tweaked-codebook mode with ciphertext stealing (XTS) mode
1770 is used to implement the AES-XTS as specified in IEEE 1619 Standard
1771 Architecture for Encrypted Shared Storage Media and NIST SP800-38E.
1772
1773 The XTS mode requires doubling key-length, for example, using
1774 512-bit key with AES-256 ('GCRY_CIPHER_AES256'). The 128-bit tweak
1775 value is feed to XTS mode as little-endian byte array using
1776 'gcry_cipher_setiv' function. When encrypting or decrypting,
1777 full-sized data unit buffers needs to be passed to
1778 'gcry_cipher_encrypt' or 'gcry_cipher_decrypt'. The tweak value is
1779 automatically incremented after each call of 'gcry_cipher_encrypt'
1780 and 'gcry_cipher_decrypt'. Auto-increment allows avoiding need of
1781 setting IV between processing of sequential data units.
1782
1783 'GCRY_CIPHER_MODE_EAX'
1784 EAX is an Authenticated Encryption with Associated Data (AEAD)
1785 block cipher mode by Bellare, Rogaway, and Wagner (see
1786 <http://web.cs.ucdavis.edu/~rogaway/papers/eax.html>).
1787
1788 'GCRY_CIPHER_MODE_SIV'
1789 Synthetic Initialization Vector (SIV) is an Authenticated
1790 Encryption with Associated Data (AEAD) block cipher mode, which is
1791 specified in RFC-5297. This mode works with block ciphers with
1792 block size of 128 bits and uses tag length of 128 bits. Depending
1793 on how it is used, SIV achieves either the goal of deterministic
1794 authenticated encryption or the goal of nonce-based,
1795 misuse-resistant authenticated encryption.
1796
1797 The SIV mode requires doubling key-length, for example, using
1798 512-bit key with AES-256 ('GCRY_CIPHER_AES256'). Multiple AD
1799 instances can be passed to SIV mode with separate calls to
1800 'gcry_cipher_authenticate'. Nonce may be passed either through
1801 'gcry_cipher_setiv' or in the last call to
1802 'gcry_cipher_authenticate'. Note that use of 'gcry_cipher_setiv'
1803 blocks any further calls to 'gcry_cipher_authenticate' as nonce
1804 needs to be the last AD element with the SIV mode. When encrypting
1805 or decrypting, full-sized plaintext or ciphertext needs to be
1806 passed to 'gcry_cipher_encrypt' or 'gcry_cipher_decrypt'.
1807 Decryption tag needs to be given to SIV mode before decryption
1808 using 'gcry_cipher_set_decryption_tag'.
1809
1810 'GCRY_CIPHER_MODE_GCM_SIV'
1811 This mode implements is GCM-SIV Authenticated Encryption with
1812 Associated Data (AEAD) block cipher mode specified in RFC-8452
1813 (AES-GCM-SIV: Nonce Misuse-Resistant Authenticated Encryption).
1814 This implementations works with block ciphers with block size of
1815 128 bits and uses tag length of 128 bits. Supported key lengths by
1816 the mode are 128 bits and 256 bits. GCM-SIV is specified as nonce
1817 misuse resistant, so that it does not fail catastrophically if a
1818 nonce is repeated.
1819
1820 When encrypting or decrypting, full-sized plaintext or ciphertext
1821 needs to be passed to 'gcry_cipher_encrypt' or
1822 'gcry_cipher_decrypt'. Decryption tag needs to be given to GCM-SIV
1823 mode before decryption using 'gcry_cipher_set_decryption_tag'.
1824
1825 
1826 File: gcrypt.info, Node: Working with cipher handles, Next: General cipher functions, Prev: Available cipher modes, Up: Symmetric cryptography
1827
1828 5.3 Working with cipher handles
1829 ===============================
1830
1831 To use a cipher algorithm, you must first allocate an according handle.
1832 This is to be done using the open function:
1833
1834 -- Function: gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *HD, int
1835 ALGO, int MODE, unsigned int FLAGS)
1836
1837 This function creates the context handle required for most of the
1838 other cipher functions and returns a handle to it in 'hd'. In case
1839 of an error, an according error code is returned.
1840
1841 The ID of algorithm to use must be specified via ALGO. See *note
1842 Available ciphers:: for a list of supported ciphers and the
1843 according constants.
1844
1845 Besides using the constants directly, the function
1846 'gcry_cipher_map_name' may be used to convert the textual name of
1847 an algorithm into the according numeric ID.
1848
1849 The cipher mode to use must be specified via MODE. See *note
1850 Available cipher modes:: for a list of supported cipher modes and
1851 the according constants. Note that some modes are incompatible
1852 with some algorithms - in particular, stream mode
1853 ('GCRY_CIPHER_MODE_STREAM') only works with stream ciphers.
1854 Poly1305 AEAD mode ('GCRY_CIPHER_MODE_POLY1305') only works with
1855 ChaCha20 stream cipher. The block cipher modes
1856 ('GCRY_CIPHER_MODE_ECB', 'GCRY_CIPHER_MODE_CBC',
1857 'GCRY_CIPHER_MODE_CFB', 'GCRY_CIPHER_MODE_OFB',
1858 'GCRY_CIPHER_MODE_CTR' and 'GCRY_CIPHER_MODE_EAX') will work with
1859 any block cipher algorithm. GCM mode ('GCRY_CIPHER_MODE_GCM'), CCM
1860 mode ('GCRY_CIPHER_MODE_CCM'), OCB mode ('GCRY_CIPHER_MODE_OCB'),
1861 XTS mode ('GCRY_CIPHER_MODE_XTS'), SIV mode
1862 ('GCRY_CIPHER_MODE_SIV') and GCM-SIV mode
1863 ('GCRY_CIPHER_MODE_GCM_SIV') will only work with block cipher
1864 algorithms which have the block size of 16 bytes.
1865
1866 The third argument FLAGS can either be passed as '0' or as the
1867 bit-wise OR of the following constants.
1868
1869 'GCRY_CIPHER_SECURE'
1870 Make sure that all operations are allocated in secure memory.
1871 This is useful when the key material is highly confidential.
1872 'GCRY_CIPHER_ENABLE_SYNC'
1873 This flag enables the CFB sync mode, which is a special
1874 feature of Libgcrypt's CFB mode implementation to allow for
1875 OpenPGP's CFB variant. See 'gcry_cipher_sync'.
1876 'GCRY_CIPHER_CBC_CTS'
1877 Enable cipher text stealing (CTS) for the CBC mode. Cannot be
1878 used simultaneously with GCRY_CIPHER_CBC_MAC. CTS mode makes
1879 it possible to transform data of almost arbitrary size (only
1880 limitation is that it must be greater than the algorithm's
1881 block size).
1882 'GCRY_CIPHER_CBC_MAC'
1883 Compute CBC-MAC keyed checksums. This is the same as CBC
1884 mode, but only output the last block. Cannot be used
1885 simultaneously with GCRY_CIPHER_CBC_CTS.
1886
1887 Use the following function to release an existing handle:
1888
1889 -- Function: void gcry_cipher_close (gcry_cipher_hd_t H)
1890
1891 This function releases the context created by 'gcry_cipher_open'.
1892 It also zeroises all sensitive information associated with this
1893 cipher handle.
1894
1895 In order to use a handle for performing cryptographic operations, a
1896 'key' has to be set first:
1897
1898 -- Function: gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t H, const
1899 void *K, size_t L)
1900
1901 Set the key K used for encryption or decryption in the context
1902 denoted by the handle H. The length L (in bytes) of the key K must
1903 match the required length of the algorithm set for this context or
1904 be in the allowed range for algorithms with variable key size. The
1905 function checks this and returns an error if there is a problem. A
1906 caller should always check for an error.
1907
1908 Most crypto modes requires an initialization vector (IV), which
1909 usually is a non-secret random string acting as a kind of salt value.
1910 The CTR mode requires a counter, which is also similar to a salt value.
1911 To set the IV or CTR, use these functions:
1912
1913 -- Function: gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t H, const
1914 void *K, size_t L)
1915
1916 Set the initialization vector used for encryption or decryption.
1917 The vector is passed as the buffer K of length L bytes and copied
1918 to internal data structures. The function checks that the IV
1919 matches the requirement of the selected algorithm and mode.
1920
1921 This function is also used by AEAD modes and with Salsa20 and
1922 ChaCha20 stream ciphers to set or update the required nonce. In
1923 these cases it needs to be called after setting the key.
1924
1925 -- Function: gcry_error_t gcry_cipher_setctr (gcry_cipher_hd_t H, const
1926 void *C, size_t L)
1927
1928 Set the counter vector used for encryption or decryption. The
1929 counter is passed as the buffer C of length L bytes and copied to
1930 internal data structures. The function checks that the counter
1931 matches the requirement of the selected algorithm (i.e.,, it must
1932 have the same size as the block size).
1933
1934 -- Function: gcry_error_t gcry_cipher_reset (gcry_cipher_hd_t H)
1935
1936 Set the given handle's context back to the state it had after the
1937 last call to 'gcry_cipher_setkey' and clear the initialization
1938 vector.
1939
1940 Note that 'gcry_cipher_reset' is implemented as a macro.
1941
1942 Authenticated Encryption with Associated Data (AEAD) block cipher
1943 modes require the handling of the authentication tag and the additional
1944 authenticated data, which can be done by using the following functions:
1945
1946 -- Function: gcry_error_t gcry_cipher_authenticate (gcry_cipher_hd_t H,
1947 const void *ABUF, size_t ABUFLEN)
1948
1949 Process the buffer ABUF of length ABUFLEN as the additional
1950 authenticated data (AAD) for AEAD cipher modes.
1951
1952 -- Function: gcry_error_t gcry_cipher_gettag (gcry_cipher_hd_t H,
1953 void *TAG, size_t TAGLEN)
1954
1955 This function is used to read the authentication tag after
1956 encryption. The function finalizes and outputs the authentication
1957 tag to the buffer TAG of length TAGLEN bytes.
1958
1959 Depending on the used mode certain restrictions for TAGLEN are
1960 enforced: For GCM TAGLEN must be at least 16 or one of the allowed
1961 truncated lengths (4, 8, 12, 13, 14, or 15).
1962
1963 -- Function: gcry_error_t gcry_cipher_checktag (gcry_cipher_hd_t H,
1964 const void *TAG, size_t TAGLEN)
1965
1966 Check the authentication tag after decryption. The authentication
1967 tag is passed as the buffer TAG of length TAGLEN bytes and compared
1968 to internal authentication tag computed during decryption. Error
1969 code 'GPG_ERR_CHECKSUM' is returned if the authentication tag in
1970 the buffer TAG does not match the authentication tag calculated
1971 during decryption.
1972
1973 Depending on the used mode certain restrictions for TAGLEN are
1974 enforced: For GCM TAGLEN must either be 16 or one of the allowed
1975 truncated lengths (4, 8, 12, 13, 14, or 15).
1976
1977 For encryption of AEAD cipher modes, it should be possible to
1978 generate an initialization vector internally within libgcrypt
1979 implementation, in coordinated way, instead of calling
1980 'gcry_cipher_setiv' with arbitrary value, so that it can ensure the
1981 security properties of AEAD block cipher. For this purpose, the
1982 following two functions are provided:
1983
1984 -- Function: gcry_error_t gcry_cipher_setup_geniv (gcry_cipher_hd_t H,
1985 int METHOD, const void *FIXED_IV, size_t FIXED_IVLEN, const
1986 void *DYN_IV, size_t DYN_IVLEN)
1987
1988 Set up an initialization vector generation for AEAD cipher modes.
1989 Generation is specified by METHOD, fixed part of initialization
1990 vector by FIXED_IV and FIXED_IVLEN, and dynamic part of
1991 initialization vector by DYN_IV and DYN_IVLEN. For METHOD, valid
1992 values are 'GCRY_CIPHER_GENIV_METHOD_CONCAT' and
1993 'GCRY_CIPHER_GENIV_METHOD_XOR'.
1994
1995 -- Function: gcry_error_t gcry_cipher_geniv (gcry_cipher_hd_t H, void
1996 *IV, size_t IVLEN)
1997
1998 Generate the initialization vector into the output buffer IV with
1999 length IVLEN. The initialization vector will be used by following
2000 'gcry_cipher_encrypt' call.
2001
2002 The actual encryption and decryption is done by using one of the
2003 following functions. They may be used as often as required to process
2004 all the data.
2005
2006 -- Function: gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t H,
2007 unsigned char *out, size_t OUTSIZE, const unsigned char *IN,
2008 size_t INLEN)
2009
2010 'gcry_cipher_encrypt' is used to encrypt the data. This function
2011 can either work in place or with two buffers. It uses the cipher
2012 context already setup and described by the handle H. There are 2
2013 ways to use the function: If IN is passed as 'NULL' and INLEN is
2014 '0', in-place encryption of the data in OUT of length OUTSIZE takes
2015 place. With IN being not 'NULL', INLEN bytes are encrypted to the
2016 buffer OUT which must have at least a size of INLEN. OUTSIZE must
2017 be set to the allocated size of OUT, so that the function can check
2018 that there is sufficient space. Note that overlapping buffers are
2019 not allowed.
2020
2021 Depending on the selected algorithms and encryption mode, the
2022 length of the buffers must be a multiple of the block size.
2023
2024 Some encryption modes require that 'gcry_cipher_final' is used
2025 before the final data chunk is passed to this function.
2026
2027 The function returns '0' on success or an error code.
2028
2029 -- Function: gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t H,
2030 unsigned char *out, size_t OUTSIZE, const unsigned char *IN,
2031 size_t INLEN)
2032
2033 'gcry_cipher_decrypt' is used to decrypt the data. This function
2034 can either work in place or with two buffers. It uses the cipher
2035 context already setup and described by the handle H. There are 2
2036 ways to use the function: If IN is passed as 'NULL' and INLEN is
2037 '0', in-place decryption of the data in OUT or length OUTSIZE takes
2038 place. With IN being not 'NULL', INLEN bytes are decrypted to the
2039 buffer OUT which must have at least a size of INLEN. OUTSIZE must
2040 be set to the allocated size of OUT, so that the function can check
2041 that there is sufficient space. Note that overlapping buffers are
2042 not allowed.
2043
2044 Depending on the selected algorithms and encryption mode, the
2045 length of the buffers must be a multiple of the block size.
2046
2047 Some encryption modes require that 'gcry_cipher_final' is used
2048 before the final data chunk is passed to this function.
2049
2050 The function returns '0' on success or an error code.
2051
2052 The OCB mode features integrated padding and must thus be told about
2053 the end of the input data. This is done with:
2054
2055 -- Function: gcry_error_t gcry_cipher_final (gcry_cipher_hd_t H)
2056
2057 Set a flag in the context to tell the encrypt and decrypt functions
2058 that their next call will provide the last chunk of data. Only the
2059 first call to this function has an effect and only for modes which
2060 support it. Checking the error is in general not necessary. This
2061 is implemented as a macro.
2062
2063 The SIV mode and the GCM-SIV mode requires decryption tag to be input
2064 before decryption. This is done with:
2065
2066 -- Function: gcry_error_t gcry_cipher_set_decryption_tag
2067 (gcry_cipher_hd_t H, const void *TAG, size_t TAGLEN)
2068
2069 Set decryption tag for SIV or GCM-SIV mode decryption. This is
2070 implemented as a macro.
2071
2072 OpenPGP (as defined in RFC-4880) requires a special sync operation in
2073 some places. The following function is used for this:
2074
2075 -- Function: gcry_error_t gcry_cipher_sync (gcry_cipher_hd_t H)
2076
2077 Perform the OpenPGP sync operation on context H. Note that this is
2078 a no-op unless the context was created with the flag
2079 'GCRY_CIPHER_ENABLE_SYNC'.
2080
2081 Some of the described functions are implemented as macros utilizing a
2082 catch-all control function. This control function is rarely used
2083 directly but there is nothing which would inhibit it:
2084
2085 -- Function: gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t H, int CMD,
2086 void *BUFFER, size_t BUFLEN)
2087
2088 'gcry_cipher_ctl' controls various aspects of the cipher module and
2089 specific cipher contexts. Usually some more specialized functions
2090 or macros are used for this purpose. The semantics of the function
2091 and its parameters depends on the command CMD and the passed
2092 context handle H. Please see the comments in the source code
2093 ('src/global.c') for details.
2094
2095 'GCRYCTL_SET_ALLOW_WEAK_KEY:'
2096
2097 This may be used to allow use of weak keys for certain block
2098 ciphers. BUFFER must be given as 'NULL'. To allow weak keys for a
2099 cipher context H, set BUFLEN to '1'. To disallow weak keys, set
2100 BUFLEN to '0'.
2101
2102 Default setting for a cipher context is to disallow weak keys.
2103
2104 Note that even if weak keys are allowed, 'gcry_cipher_setkey' will
2105 return error code 'GPG_ERR_WEAK_KEY' if a weak key is detected.
2106 However, the cipher context is configured with the weak key and can
2107 proceed with encryption/decryption. When weak keys are disallowed,
2108 error code 'GPG_ERR_WEAK_KEY' is returned and the cipher context is
2109 cannot be used for encryption/decryption.
2110
2111 -- Function: gcry_error_t gcry_cipher_info (gcry_cipher_hd_t H, int
2112 WHAT, void *BUFFER, size_t *NBYTES)
2113
2114 'gcry_cipher_info' is used to retrieve various information about a
2115 cipher context or the cipher module in general.
2116
2117 'GCRYCTL_GET_TAGLEN:'
2118 Return the length of the tag for an AE algorithm mode. An
2119 error is returned for modes which do not support a tag.
2120 BUFFER must be given as 'NULL'. On success the result is
2121 stored NBYTES. The taglen is returned in bytes.
2122
2123 
2124 File: gcrypt.info, Node: General cipher functions, Prev: Working with cipher handles, Up: Symmetric cryptography
2125
2126 5.4 General cipher functions
2127 ============================
2128
2129 To work with the algorithms, several functions are available to map
2130 algorithm names to the internal identifiers, as well as ways to retrieve
2131 information about an algorithm or the current cipher context.
2132
2133 -- Function: gcry_error_t gcry_cipher_algo_info (int ALGO, int WHAT,
2134 void *BUFFER, size_t *NBYTES)
2135
2136 This function is used to retrieve information on a specific
2137 algorithm. You pass the cipher algorithm ID as ALGO and the type
2138 of information requested as WHAT. The result is either returned as
2139 the return code of the function or copied to the provided BUFFER
2140 whose allocated length must be available in an integer variable
2141 with the address passed in NBYTES. This variable will also receive
2142 the actual used length of the buffer.
2143
2144 Here is a list of supported codes for WHAT:
2145
2146 'GCRYCTL_GET_KEYLEN:'
2147 Return the length of the key. If the algorithm supports
2148 multiple key lengths, the maximum supported value is returned.
2149 The length is returned as number of octets (bytes) and not as
2150 number of bits in NBYTES; BUFFER must be zero. Note that it
2151 is usually better to use the convenience function
2152 'gcry_cipher_get_algo_keylen'.
2153
2154 'GCRYCTL_GET_BLKLEN:'
2155 Return the block length of the algorithm. The length is
2156 returned as a number of octets in NBYTES; BUFFER must be zero.
2157 Note that it is usually better to use the convenience function
2158 'gcry_cipher_get_algo_blklen'.
2159
2160 'GCRYCTL_TEST_ALGO:'
2161 Returns '0' when the specified algorithm is available for use.
2162 BUFFER and NBYTES must be zero.
2163
2164 -- Function: size_t gcry_cipher_get_algo_keylen (ALGO)
2165
2166 This function returns length of the key for algorithm ALGO. If the
2167 algorithm supports multiple key lengths, the maximum supported key
2168 length is returned. On error '0' is returned. The key length is
2169 returned as number of octets.
2170
2171 This is a convenience functions which should be preferred over
2172 'gcry_cipher_algo_info' because it allows proper type checking.
2173
2174 -- Function: size_t gcry_cipher_get_algo_blklen (int ALGO)
2175
2176 This functions returns the block-length of the algorithm ALGO
2177 counted in octets. On error '0' is returned.
2178
2179 This is a convenience functions which should be preferred over
2180 'gcry_cipher_algo_info' because it allows proper type checking.
2181
2182 -- Function: const char * gcry_cipher_algo_name (int ALGO)
2183
2184 'gcry_cipher_algo_name' returns a string with the name of the
2185 cipher algorithm ALGO. If the algorithm is not known or another
2186 error occurred, the string '"?"' is returned. This function should
2187 not be used to test for the availability of an algorithm.
2188
2189 -- Function: int gcry_cipher_map_name (const char *NAME)
2190
2191 'gcry_cipher_map_name' returns the algorithm identifier for the
2192 cipher algorithm described by the string NAME. If this algorithm
2193 is not available, '0' is returned.
2194
2195 -- Function: int gcry_cipher_mode_from_oid (const char *STRING)
2196
2197 Return the cipher mode associated with an ASN.1 object identifier.
2198 The object identifier is expected to be in the IETF-style dotted
2199 decimal notation. The function returns '0' for an unknown object
2200 identifier or when no mode is associated with it.
2201
2202 
2203 File: gcrypt.info, Node: Public Key cryptography, Next: Hashing, Prev: Symmetric cryptography, Up: Top
2204
2205 6 Public Key cryptography
2206 *************************
2207
2208 Public key cryptography, also known as asymmetric cryptography, is an
2209 easy way for key management and to provide digital signatures.
2210 Libgcrypt provides two completely different interfaces to public key
2211 cryptography, this chapter explains the one based on S-expressions.
2212
2213 * Menu:
2214
2215 * Available algorithms:: Algorithms supported by the library.
2216 * Used S-expressions:: Introduction into the used S-expression.
2217 * Cryptographic Functions:: Functions for performing the cryptographic actions.
2218 * Dedicated ECC Functions:: Dedicated functions for elliptic curves.
2219 * General public-key related Functions:: General functions, not implementing any cryptography.
2220
2221 
2222 File: gcrypt.info, Node: Available algorithms, Next: Used S-expressions, Up: Public Key cryptography
2223
2224 6.1 Available algorithms
2225 ========================
2226
2227 Libgcrypt supports the RSA (Rivest-Shamir-Adleman) algorithms as well as
2228 DSA (Digital Signature Algorithm), Elgamal, ECDSA, ECDH, and EdDSA.
2229
2230 
2231 File: gcrypt.info, Node: Used S-expressions, Next: Cryptographic Functions, Prev: Available algorithms, Up: Public Key cryptography
2232
2233 6.2 Used S-expressions
2234 ======================
2235
2236 Libgcrypt's API for asymmetric cryptography is based on data structures
2237 called S-expressions (see
2238 <https://web.archive.orgweb/20230305073119http://people.csail.mit.edurivest/sexp.html>)
2239 and does not work with contexts/handles as most of the other building
2240 blocks of Libgcrypt do.
2241
2242 The following information are stored in S-expressions:
2243
2244 * keys
2245
2246 * plain text data
2247
2248 * encrypted data
2249
2250 * signatures
2251
2252 To describe how Libgcrypt expect keys, we use examples. Note that words
2253 in uppercase indicate parameters, whereas lowercase words are literals.
2254
2255 Note that all MPI (multi-precision-integers) values are expected to
2256 be in 'GCRYMPI_FMT_USG' format. An easy way to create S-expressions is
2257 by using 'gcry_sexp_build' which allows passing a string with
2258 printf-like escapes to insert MPI values.
2259
2260 * Menu:
2261
2262 * RSA key parameters:: Parameters used with an RSA key.
2263 * DSA key parameters:: Parameters used with a DSA key.
2264 * ECC key parameters:: Parameters used with ECC keys.
2265
2266 
2267 File: gcrypt.info, Node: RSA key parameters, Next: DSA key parameters, Up: Used S-expressions
2268
2269 6.2.1 RSA key parameters
2270 ------------------------
2271
2272 An RSA private key is described by this S-expression:
2273
2274 (private-key
2275 (rsa
2276 (n N-MPI)
2277 (e E-MPI)
2278 (d D-MPI)
2279 (p P-MPI)
2280 (q Q-MPI)
2281 (u U-MPI)))
2282
2283 An RSA public key is described by this S-expression:
2284
2285 (public-key
2286 (rsa
2287 (n N-MPI)
2288 (e E-MPI)))
2289
2290 N-MPI
2291 RSA public modulus n.
2292 E-MPI
2293 RSA public exponent e.
2294 D-MPI
2295 RSA secret exponent d = e^{-1} \bmod (p-1)(q-1).
2296 P-MPI
2297 RSA secret prime p.
2298 Q-MPI
2299 RSA secret prime q with p < q.
2300 U-MPI
2301 Multiplicative inverse u = p^{-1} \bmod q.
2302
2303 For signing and decryption, the parameters (p, q, u) are optional but
2304 greatly improve the performance. Either all of these optional
2305 parameters must be given or none of them. They are mandatory for
2306 'gcry_pk_testkey'.
2307
2308 Note that OpenSSL uses slighly different parameters: q < p and u =
2309 q^{-1} \bmod p. To use these parameters you will need to swap the
2310 values and recompute u. Here is example code to do this:
2311
2312 if (gcry_mpi_cmp (p, q) > 0)
2313 {
2314 gcry_mpi_swap (p, q);
2315 gcry_mpi_invm (u, p, q);
2316 }
2317
2318 
2319 File: gcrypt.info, Node: DSA key parameters, Next: ECC key parameters, Prev: RSA key parameters, Up: Used S-expressions
2320
2321 6.2.2 DSA key parameters
2322 ------------------------
2323
2324 A DSA private key is described by this S-expression:
2325
2326 (private-key
2327 (dsa
2328 (p P-MPI)
2329 (q Q-MPI)
2330 (g G-MPI)
2331 (y Y-MPI)
2332 (x X-MPI)))
2333
2334 P-MPI
2335 DSA prime p.
2336 Q-MPI
2337 DSA group order q (which is a prime divisor of p-1).
2338 G-MPI
2339 DSA group generator g.
2340 Y-MPI
2341 DSA public key value y = g^x \bmod p.
2342 X-MPI
2343 DSA secret exponent x.
2344
2345 The public key is similar, with "private-key" replaced by
2346 "public-key" and no X-MPI.
2347
2348 
2349 File: gcrypt.info, Node: ECC key parameters, Prev: DSA key parameters, Up: Used S-expressions
2350
2351 6.2.3 ECC key parameters
2352 ------------------------
2353
2354 An ECC private key is described by this S-expression:
2355
2356 (private-key
2357 (ecc
2358 (p P-MPI)
2359 (a A-MPI)
2360 (b B-MPI)
2361 (g G-POINT)
2362 (n N-MPI)
2363 (q Q-POINT)
2364 (d D-MPI)))
2365
2366 P-MPI
2367 Prime specifying the field GF(p).
2368 A-MPI
2369 B-MPI
2370 The two coefficients of the Weierstrass equation y^2 = x^3 + ax + b
2371 G-POINT
2372 Base point g.
2373 N-MPI
2374 Order of g
2375 Q-POINT
2376 The point representing the public key Q = dG.
2377 D-MPI
2378 The private key d
2379
2380 All point values are encoded in standard format; Libgcrypt does in
2381 general only support uncompressed points, thus the first byte needs to
2382 be '0x04'. However "EdDSA" describes its own compression scheme which
2383 is used by default; the non-standard first byte '0x40' may optionally be
2384 used to explicit flag the use of the algorithm's native compression
2385 method.
2386
2387 The public key is similar, with "private-key" replaced by
2388 "public-key" and no D-MPI.
2389
2390 If the domain parameters are well-known, the name of this curve may
2391 be used. For example
2392
2393 (private-key
2394 (ecc
2395 (curve "NIST P-192")
2396 (q Q-POINT)
2397 (d D-MPI)))
2398
2399 Note that Q-POINT is optional for a private key. The 'curve'
2400 parameter may be given in any case and is used to replace missing
2401 parameters.
2402
2403 Currently implemented curves are:
2404
2405 'Curve25519'
2406 'X25519'
2407 '1.3.6.1.4.1.3029.1.5.1'
2408 '1.3.101.110'
2409 The RFC-8410 255 bit curve, its RFC name, OpenPGP and RFC OIDs.
2410
2411 'X448'
2412 '1.3.101.111'
2413 The RFC-8410 448 bit curve and its RFC OID.
2414
2415 'Ed25519'
2416 '1.3.6.1.4.1.11591.15.1'
2417 '1.3.101.112'
2418 The signing variant of the RFC-8410 255 bit curve, its OpenPGP and
2419 RFC OIDs.
2420
2421 'Ed448'
2422 '1.3.101.113'
2423 The signing variant of the RFC-8410 448 bit curve and its RFC OID.
2424
2425 'NIST P-192'
2426 '1.2.840.10045.3.1.1'
2427 'nistp192'
2428 'prime192v1'
2429 'secp192r1'
2430 The NIST 192 bit curve, its OID and aliases.
2431
2432 'NIST P-224'
2433 '1.3.132.0.33'
2434 'nistp224'
2435 'secp224r1'
2436 The NIST 224 bit curve, its OID and aliases.
2437
2438 'NIST P-256'
2439 '1.2.840.10045.3.1.7'
2440 'nistp256'
2441 'prime256v1'
2442 'secp256r1'
2443 The NIST 256 bit curve, its OID and aliases.
2444
2445 'NIST P-384'
2446 '1.3.132.0.34'
2447 'nistp384'
2448 'secp384r1'
2449 The NIST 384 bit curve, its OID and aliases.
2450
2451 'NIST P-521'
2452 '1.3.132.0.35'
2453 'nistp521'
2454 'secp521r1'
2455 The NIST 521 bit curve, its OID and aliases.
2456
2457 'brainpoolP160r1'
2458 '1.3.36.3.3.2.8.1.1.1'
2459 The Brainpool 160 bit curve and its OID.
2460
2461 'brainpoolP192r1'
2462 '1.3.36.3.3.2.8.1.1.3'
2463 The Brainpool 192 bit curve and its OID.
2464
2465 'brainpoolP224r1'
2466 '1.3.36.3.3.2.8.1.1.5'
2467 The Brainpool 224 bit curve and its OID.
2468
2469 'brainpoolP256r1'
2470 '1.3.36.3.3.2.8.1.1.7'
2471 The Brainpool 256 bit curve and its OID.
2472
2473 'brainpoolP320r1'
2474 '1.3.36.3.3.2.8.1.1.9'
2475 The Brainpool 320 bit curve and its OID.
2476
2477 'brainpoolP384r1'
2478 '1.3.36.3.3.2.8.1.1.11'
2479 The Brainpool 384 bit curve and its OID.
2480
2481 'brainpoolP512r1'
2482 '1.3.36.3.3.2.8.1.1.13'
2483 The Brainpool 512 bit curve and its OID.
2484
2485 'GOST2001-test'
2486 '1.2.643.2.2.35.0'
2487
2488 'GOST2001-CryptoPro-A'
2489 '1.2.643.2.2.35.1'
2490
2491 'GOST2001-CryptoPro-B'
2492 '1.2.643.2.2.35.2'
2493
2494 'GOST2001-CryptoPro-C'
2495 '1.2.643.2.2.35.3'
2496
2497 'GOST2001-CryptoPro-A'
2498 'GOST2001-CryptoPro-XchA'
2499
2500 'GOST2001-CryptoPro-C'
2501 'GOST2001-CryptoPro-XchB'
2502
2503 'GOST2001-CryptoPro-A'
2504 '1.2.643.2.2.36.0'
2505
2506 'GOST2001-CryptoPro-C'
2507 '1.2.643.2.2.36.1'
2508
2509 'GOST2012-256-tc26-A'
2510 '1.2.643.7.1.2.1.1.1'
2511
2512 'GOST2001-CryptoPro-A'
2513 '1.2.643.7.1.2.1.1.2'
2514
2515 'GOST2001-CryptoPro-A'
2516 'GOST2012-256-tc26-B'
2517
2518 'GOST2001-CryptoPro-B'
2519 '1.2.643.7.1.2.1.1.3'
2520
2521 'GOST2001-CryptoPro-B'
2522 'GOST2012-256-tc26-C'
2523
2524 'GOST2001-CryptoPro-C'
2525 '1.2.643.7.1.2.1.1.4'
2526
2527 'GOST2001-CryptoPro-C'
2528 'GOST2012-256-tc26-D'
2529
2530 'GOST2012-512-test'
2531 'GOST2012-test'
2532
2533 'GOST2012-512-test'
2534 '1.2.643.7.1.2.1.2.0'
2535
2536 'GOST2012-512-tc26-A'
2537 'GOST2012-tc26-A'
2538
2539 'GOST2012-512-tc26-B'
2540 'GOST2012-tc26-B'
2541
2542 'GOST2012-512-tc26-A'
2543 '1.2.643.7.1.2.1.2.1'
2544
2545 'GOST2012-512-tc26-B'
2546 '1.2.643.7.1.2.1.2.2'
2547
2548 'GOST2012-512-tc26-C'
2549 '1.2.643.7.1.2.1.2.3'
2550
2551 'secp256k1'
2552 '1.3.132.0.10'
2553
2554 'sm2p256v1'
2555 '1.2.156.10197.1.301'
2556
2557 As usual the OIDs may optionally be prefixed with the string 'OID.'
2558 or 'oid.'.
2559
2560 
2561 File: gcrypt.info, Node: Cryptographic Functions, Next: Dedicated ECC Functions, Prev: Used S-expressions, Up: Public Key cryptography
2562
2563 6.3 Cryptographic Functions
2564 ===========================
2565
2566 Some functions operating on S-expressions support 'flags' to influence
2567 the operation. These flags have to be listed in a sub-S-expression
2568 named 'flags'. Flag names are case-sensitive. The following flags are
2569 known:
2570
2571 'comp'
2572 'nocomp'
2573 If supported by the algorithm and curve, the 'comp' flag requests
2574 that points are returned in compact (compressed) representation.
2575 The 'nocomp' flag requests that points are returned with full
2576 coordinates. The default depends on the the algorithm and curve.
2577 The compact representation requires a small overhead before a point
2578 can be used but halves the size of a public key to be conveyed. If
2579 'comp' is used with the "EdDSA" algorithm, the key generation
2580 prefixes the public key with a '0x40' byte.
2581
2582 'pkcs1'
2583 Use PKCS#1 block type 2 padding for encryption, block type 1
2584 padding for signing.
2585
2586 'oaep'
2587 Use RSA-OAEP padding for encryption.
2588
2589 'pss'
2590 Use RSA-PSS padding for signing.
2591
2592 'eddsa'
2593 Use the EdDSA scheme signing instead of the default ECDSA
2594 algorithm. Note that the EdDSA uses a special form of the public
2595 key.
2596
2597 'rfc6979'
2598 For DSA and ECDSA use a deterministic scheme for the k parameter.
2599
2600 'no-blinding'
2601 Do not use a technique called 'blinding', which is used by default
2602 in order to prevent leaking of secret information. Blinding is
2603 only implemented by RSA, but it might be implemented by other
2604 algorithms in the future as well, when necessary.
2605
2606 'param'
2607 For ECC key generation also return the domain parameters. For ECC
2608 signing and verification override default parameters by provided
2609 domain parameters of the public or private key.
2610
2611 'transient-key'
2612 This flag is only meaningful for RSA, DSA, and ECC key generation.
2613 If given, the key is created using a faster and a somewhat less
2614 secure random number generator. This flag may be used for keys
2615 which are only used for a short time or per-message and do not
2616 require full cryptographic strength.
2617
2618 'no-keytest'
2619 This flag skips internal failsafe tests to assert that a generated
2620 key is properly working. It currently has an effect only for
2621 standard ECC key generation. It is mostly useful along with
2622 transient-key to achieve fastest ECC key generation.
2623
2624 'use-x931'
2625 Force the use of the ANSI X9.31 key generation algorithm instead of
2626 the default algorithm. This flag is only meaningful for RSA key
2627 generation and usually not required. Note that this algorithm is
2628 implicitly used if either 'derive-parms' is given.
2629
2630 'use-fips186'
2631 Force the use of the FIPS 186 key generation algorithm instead of
2632 the default algorithm. This flag is only meaningful for DSA and
2633 usually not required. Note that this algorithm is implicitly used
2634 if either 'derive-parms' is given or Libgcrypt is in FIPS mode. As
2635 of now FIPS 186-2 is implemented; after the approval of FIPS 186-3
2636 the code will be changed to implement 186-3.
2637
2638 'use-fips186-2'
2639 Force the use of the FIPS 186-2 key generation algorithm instead of
2640 the default algorithm. This algorithm is slightly different from
2641 FIPS 186-3 and allows only 1024 bit keys. This flag is only
2642 meaningful for DSA and only required for FIPS testing backward
2643 compatibility.
2644
2645 Now that we know the key basics, we can carry on and explain how to
2646 encrypt and decrypt data. In almost all cases the data is a random
2647 session key which is in turn used for the actual encryption of the real
2648 data. There are 2 functions to do this:
2649
2650 -- Function: gcry_error_t gcry_pk_encrypt (gcry_sexp_t *R_CIPH,
2651 gcry_sexp_t DATA, gcry_sexp_t PKEY)
2652
2653 Obviously a public key must be provided for encryption. It is
2654 expected as an appropriate S-expression (see above) in PKEY. The
2655 data to be encrypted can either be in the simple old format, which
2656 is a very simple S-expression consisting only of one MPI, or it may
2657 be a more complex S-expression which also allows specifying flags
2658 for operation, like e.g., padding rules.
2659
2660 If you don't want to let Libgcrypt handle the padding, you must
2661 pass an appropriate MPI using this expression for DATA:
2662
2663 (data
2664 (flags raw)
2665 (value MPI))
2666
2667 This has the same semantics as the old style MPI only way. MPI is
2668 the actual data, already padded appropriate for your protocol.
2669 Most RSA based systems however use PKCS#1 padding and so you can
2670 use this S-expression for DATA:
2671
2672 (data
2673 (flags pkcs1)
2674 (value BLOCK))
2675
2676 Here, the "flags" list has the "pkcs1" flag which let the function
2677 know that it should provide PKCS#1 block type 2 padding. The
2678 actual data to be encrypted is passed as a string of octets in
2679 BLOCK. The function checks that this data actually can be used
2680 with the given key, does the padding and encrypts it.
2681
2682 If the function could successfully perform the encryption, the
2683 return value will be 0 and a new S-expression with the encrypted
2684 result is allocated and assigned to the variable at the address of
2685 R_CIPH. The caller is responsible to release this value using
2686 'gcry_sexp_release'. In case of an error, an error code is
2687 returned and R_CIPH will be set to 'NULL'.
2688
2689 The returned S-expression has this format when used with RSA:
2690
2691 (enc-val
2692 (rsa
2693 (a A-MPI)))
2694
2695 Where A-MPI is an MPI with the result of the RSA operation. When
2696 using the Elgamal algorithm, the return value will have this
2697 format:
2698
2699 (enc-val
2700 (elg
2701 (a A-MPI)
2702 (b B-MPI)))
2703
2704 Where A-MPI and B-MPI are MPIs with the result of the Elgamal
2705 encryption operation.
2706
2707 -- Function: gcry_error_t gcry_pk_decrypt (gcry_sexp_t *R_PLAIN,
2708 gcry_sexp_t DATA, gcry_sexp_t SKEY)
2709
2710 Obviously a private key must be provided for decryption. It is
2711 expected as an appropriate S-expression (see above) in SKEY. The
2712 data to be decrypted must match the format of the result as
2713 returned by 'gcry_pk_encrypt', but should be enlarged with a
2714 'flags' element:
2715
2716 (enc-val
2717 (flags)
2718 (elg
2719 (a A-MPI)
2720 (b B-MPI)))
2721
2722 This function does not remove padding from the data by default. To
2723 let Libgcrypt remove padding, give a hint in 'flags' telling which
2724 padding method was used when encrypting:
2725
2726 (flags PADDING-METHOD)
2727
2728 Currently PADDING-METHOD is either 'pkcs1' for PKCS#1 block type 2
2729 padding, or 'oaep' for RSA-OAEP padding.
2730
2731 The function returns 0 on success or an error code. The variable
2732 at the address of R_PLAIN will be set to 'NULL' on error or receive
2733 the decrypted value on success. The format of R_PLAIN is a simple
2734 S-expression part (i.e., not a valid one) with just one MPI if
2735 there was no 'flags' element in DATA; if at least an empty 'flags'
2736 is passed in DATA, the format is:
2737
2738 (value PLAINTEXT)
2739
2740 Another operation commonly performed using public key cryptography is
2741 signing data. In some sense this is even more important than encryption
2742 because digital signatures are an important instrument for key
2743 management. Libgcrypt supports digital signatures using 2 functions,
2744 similar to the encryption functions:
2745
2746 -- Function: gcry_error_t gcry_pk_sign (gcry_sexp_t *R_SIG,
2747 gcry_sexp_t DATA, gcry_sexp_t SKEY)
2748
2749 This function creates a digital signature for DATA using the
2750 private key SKEY and place it into the variable at the address of
2751 R_SIG. DATA may either be the simple old style S-expression with
2752 just one MPI or a modern and more versatile S-expression which
2753 allows letting Libgcrypt handle padding:
2754
2755 (data
2756 (flags pkcs1)
2757 (hash HASH-ALGO BLOCK))
2758
2759 This example requests to sign the data in BLOCK after applying
2760 PKCS#1 block type 1 style padding. HASH-ALGO is a string with the
2761 hash algorithm to be encoded into the signature, this may be any
2762 hash algorithm name as supported by Libgcrypt. Most likely, this
2763 will be "sha256" or "sha1". It is obvious that the length of BLOCK
2764 must match the size of that message digests; the function checks
2765 that this and other constraints are valid.
2766
2767 If PKCS#1 padding is not required (because the caller does already
2768 provide a padded value), either the old format or better the
2769 following format should be used:
2770
2771 (data
2772 (flags raw)
2773 (value MPI))
2774
2775 Here, the data to be signed is directly given as an MPI.
2776
2777 For DSA the input data is expected in this format:
2778
2779 (data
2780 (flags raw)
2781 (value MPI))
2782
2783 Here, the data to be signed is directly given as an MPI. It is
2784 expect that this MPI is the hash value. For the standard DSA,
2785 using a MPI is not a problem in regard to leading zeroes because
2786 the hash value is directly used as an MPI. For better standard
2787 conformance it would be better to explicitly use a memory string
2788 (like with pkcs1) but that is currently not supported. However,
2789 for deterministic DSA as specified in RFC6979 this can't be used.
2790 Instead the following input is expected.
2791
2792 (data
2793 (flags rfc6979)
2794 (hash HASH-ALGO BLOCK))
2795
2796 Note that the provided hash-algo is used for the internal HMAC; it
2797 should match the hash-algo used to create BLOCK.
2798
2799 The signature is returned as a newly allocated S-expression in
2800 R_SIG using this format for RSA:
2801
2802 (sig-val
2803 (rsa
2804 (s S-MPI)))
2805
2806 Where S-MPI is the result of the RSA sign operation. For DSA the
2807 S-expression returned is:
2808
2809 (sig-val
2810 (dsa
2811 (r R-MPI)
2812 (s S-MPI)))
2813
2814 Where R-MPI and S-MPI are the result of the DSA sign operation.
2815
2816 For Elgamal signing (which is slow, yields large numbers and
2817 probably is not as secure as the other algorithms), the same format
2818 is used with "elg" replacing "dsa"; for ECDSA signing, the same
2819 format is used with "ecdsa" replacing "dsa".
2820
2821 For the EdDSA algorithm (cf. Ed25519) the required input parameters
2822 are:
2823
2824 (data
2825 (flags eddsa)
2826 (hash-algo sha512)
2827 (value MESSAGE))
2828
2829 Note that the MESSAGE may be of any length; hashing is part of the
2830 algorithm. Using a large data block for MESSAGE is in general not
2831 suggested; in that case the used protocol should better require
2832 that a hash of the message is used as input to the EdDSA algorithm.
2833 Note that for X.509 certificates MESSAGE is the 'tbsCertificate'
2834 part and in CMS MESSAGE is the 'signedAttrs' part; see RFC-8410 and
2835 RFC-8419.
2836
2837 The operation most commonly used is definitely the verification of a
2838 signature. Libgcrypt provides this function:
2839
2840 -- Function: gcry_error_t gcry_pk_verify (gcry_sexp_t SIG,
2841 gcry_sexp_t DATA, gcry_sexp_t PKEY)
2842
2843 This is used to check whether the signature SIG matches the DATA.
2844 The public key PKEY must be provided to perform this verification.
2845 This function is similar in its parameters to 'gcry_pk_sign' with
2846 the exceptions that the public key is used instead of the private
2847 key and that no signature is created but a signature, in a format
2848 as created by 'gcry_pk_sign', is passed to the function in SIG.
2849
2850 The result is 0 for success (i.e., the data matches the signature),
2851 or an error code where the most relevant code is
2852 'GCRY_ERR_BAD_SIGNATURE' to indicate that the signature does not
2853 match the provided data.
2854
2855 Additionally, libgcrypt provides three functions for digital
2856 signatures. Those functions are useful when hashing computation should
2857 be closely combined with signature computation.
2858
2859 -- Function: gcry_error_t gcry_pk_hash_sign (gcry_sexp_t *RESULT,
2860 const char *DATA_TMPL, gcry_sexp_t SKEY, gcry_md_hd_t HD,
2861 gcry_ctx_t CTX)
2862
2863 This function is a variant of 'gcry_pk_sign' which takes as
2864 additional parameter HD, handle for hash, and an optional context
2865 CTX. SKEY is a private key in S-expression. The hash algorithm
2866 used by the handle needs to be enabled and input needs to be
2867 supplied beforehand. DATA-TMPL specifies a template to compose an
2868 S-expression to be signed. A template should include '"(hash %s
2869 %b)"' or '"(hash ALGONAME %b)"'. For the former case, "%s" is
2870 substituted by the string of algorithm of 'gcry_md_get_algo ('HD')'
2871 and when 'gcry_md_read' is called, 'ALGO=0' is used internally.
2872 For the latter case, hash algorithm by 'ALGONAME' is used when
2873 'gcry_md_read' is called internally. The hash handle must not yet
2874 been finalized; the function takes a copy of the state and does a
2875 finalize on the copy. The last argument, CTX, may be used for
2876 supplying nonce externally. If no need, CTX should be passed as
2877 'NULL'.
2878
2879 -- Function: gcry_error_t gcry_pk_hash_verify (gcry_sexp_t SIGVAL,
2880 const char *DATA_TMPL, gcry_sexp_t PKEY, gcry_md_hd_t HD,
2881 gcry_ctx_t CTX)
2882
2883 This function is a variant of 'gcry_pk_verify' which takes as
2884 additional parameter HD, handle for hash, and an optional context
2885 CTX. PKEY is a public key in S-expression. See
2886 'gcry_pk_hash_sign', for the explanation of handle for hash,
2887 DATA-TMPL and CTX.
2888
2889 -- Function: gcry_error_t gcry_pk_random_override_new
2890 (gcry_ctx_t *R_CTX, const unsigned char *P, size_t LEN)
2891
2892 This function is used to allocate a new context for nonce, by
2893 memory area pointed to by P to LEN bytes. This context can be used
2894 when calling 'gcry_pk_hash_sign' or 'gcry_pk_hash_verify' to supply
2895 nonce externally, instead of generating internally.
2896
2897 On success the function returns 0 and stores the new context object
2898 at R_CTX; this object eventually needs to be released (*note
2899 gcry_ctx_release::). On error the function stores 'NULL' at R_CTX
2900 and returns an error code.
2901
2902 
2903 File: gcrypt.info, Node: Dedicated ECC Functions, Next: General public-key related Functions, Prev: Cryptographic Functions, Up: Public Key cryptography
2904
2905 6.4 Dedicated functions for elliptic curves.
2906 ============================================
2907
2908 The S-expression based interface is not optimal for certain operations
2909 on elliptic curves. Thus a few special functions are implemented to
2910 support common operations on curves with one of these assigned curve
2911 ids:
2912
2913 'GCRY_ECC_CURVE25519'
2914 'GCRY_ECC_CURVE448'
2915
2916 -- Function: unsigned int gcry_ecc_get_algo_keylen (int CURVEID);
2917
2918 Returns the length in bytes of a point on the curve with the id
2919 CURVEID. 0 is returned for curves which have no assigned id.
2920
2921 -- Function: gpg_error_t gcry_ecc_mul_point (int CURVEID,
2922 unsigned char *RESULT, const unsigned char *SCALAR,
2923 const unsigned char *POINT)
2924
2925 This function computes the scalar multiplication on the Montgomery
2926 form of the curve with id CURVEID. If POINT is 'NULL', the base
2927 point of the curve is used. The caller needs to provide a large
2928 enough buffer for RESULT and a valid SCALAR and POINT.
2929
2930 
2931 File: gcrypt.info, Node: General public-key related Functions, Prev: Dedicated ECC Functions, Up: Public Key cryptography
2932
2933 6.5 General public-key related Functions
2934 ========================================
2935
2936 A couple of utility functions are available to retrieve the length of
2937 the key, map algorithm identifiers and perform sanity checks:
2938
2939 -- Function: const char * gcry_pk_algo_name (int ALGO)
2940
2941 Map the public key algorithm id ALGO to a string representation of
2942 the algorithm name. For unknown algorithms this functions returns
2943 the string '"?"'. This function should not be used to test for the
2944 availability of an algorithm.
2945
2946 -- Function: int gcry_pk_map_name (const char *NAME)
2947
2948 Map the algorithm NAME to a public key algorithm Id. Returns 0 if
2949 the algorithm name is not known.
2950
2951 -- Function: int gcry_pk_test_algo (int ALGO)
2952
2953 Return 0 if the public key algorithm ALGO is available for use.
2954 Note that this is implemented as a macro.
2955
2956 -- Function: unsigned int gcry_pk_get_nbits (gcry_sexp_t KEY)
2957
2958 Return what is commonly referred as the key length for the given
2959 public or private key in KEY.
2960
2961 -- Function: unsigned char * gcry_pk_get_keygrip (gcry_sexp_t KEY,
2962 unsigned char *ARRAY)
2963
2964 Return the so called "keygrip" which is the SHA-1 hash of the
2965 public key parameters expressed in a way depended on the algorithm.
2966 ARRAY must either provide space for 20 bytes or be 'NULL'. In the
2967 latter case a newly allocated array of that size is returned. On
2968 success a pointer to the newly allocated space or to ARRAY is
2969 returned. 'NULL' is returned to indicate an error which is most
2970 likely an unknown algorithm or one where a "keygrip" has not yet
2971 been defined. The function accepts public or secret keys in KEY.
2972
2973 -- Function: gcry_error_t gcry_pk_testkey (gcry_sexp_t KEY)
2974
2975 Return zero if the private key KEY is 'sane', an error code
2976 otherwise. Note that it is not possible to check the 'saneness' of
2977 a public key.
2978
2979 -- Function: gcry_error_t gcry_pk_algo_info (int ALGO, int WHAT,
2980 void *BUFFER, size_t *NBYTES)
2981
2982 Depending on the value of WHAT return various information about the
2983 public key algorithm with the id ALGO. Note that the function
2984 returns '-1' on error and the actual error code must be retrieved
2985 using the function 'gcry_errno'. The currently defined values for
2986 WHAT are:
2987
2988 'GCRYCTL_TEST_ALGO:'
2989 Return 0 if the specified algorithm is available for use.
2990 BUFFER must be 'NULL', NBYTES may be passed as 'NULL' or point
2991 to a variable with the required usage of the algorithm. This
2992 may be 0 for "don't care" or the bit-wise OR of these flags:
2993
2994 'GCRY_PK_USAGE_SIGN'
2995 Algorithm is usable for signing.
2996 'GCRY_PK_USAGE_ENCR'
2997 Algorithm is usable for encryption.
2998
2999 Unless you need to test for the allowed usage, it is in
3000 general better to use the macro gcry_pk_test_algo instead.
3001
3002 'GCRYCTL_GET_ALGO_USAGE:'
3003 Return the usage flags for the given algorithm. For an
3004 invalid algorithm return 0. Disabled algorithms are ignored
3005 here because we want to know whether the algorithm is at all
3006 capable of a certain usage.
3007
3008 'GCRYCTL_GET_ALGO_NPKEY'
3009 Return the number of elements the public key for algorithm
3010 ALGO consist of. Return 0 for an unknown algorithm.
3011
3012 'GCRYCTL_GET_ALGO_NSKEY'
3013 Return the number of elements the private key for algorithm
3014 ALGO consist of. Note that this value is always larger than
3015 that of the public key. Return 0 for an unknown algorithm.
3016
3017 'GCRYCTL_GET_ALGO_NSIGN'
3018 Return the number of elements a signature created with the
3019 algorithm ALGO consists of. Return 0 for an unknown algorithm
3020 or for an algorithm not capable of creating signatures.
3021
3022 'GCRYCTL_GET_ALGO_NENCR'
3023 Return the number of elements a encrypted message created with
3024 the algorithm ALGO consists of. Return 0 for an unknown
3025 algorithm or for an algorithm not capable of encryption.
3026
3027 Please note that parameters not required should be passed as
3028 'NULL'.
3029
3030 -- Function: gcry_error_t gcry_pk_ctl (int CMD, void *BUFFER,
3031 size_t BUFLEN)
3032
3033 This is a general purpose function to perform certain control
3034 operations. CMD controls what is to be done. The return value is
3035 0 for success or an error code. Currently supported values for CMD
3036 are:
3037
3038 'GCRYCTL_DISABLE_ALGO'
3039 Disable the algorithm given as an algorithm id in BUFFER.
3040 BUFFER must point to an 'int' variable with the algorithm id
3041 and BUFLEN must have the value 'sizeof (int)'. This function
3042 is not thread safe and should thus be used before any other
3043 threads are started.
3044
3045 Libgcrypt also provides a function to generate public key pairs:
3046
3047 -- Function: gcry_error_t gcry_pk_genkey (gcry_sexp_t *R_KEY,
3048 gcry_sexp_t PARMS)
3049
3050 This function create a new public key pair using information given
3051 in the S-expression PARMS and stores the private and the public key
3052 in one new S-expression at the address given by R_KEY. In case of
3053 an error, R_KEY is set to 'NULL'. The return code is 0 for success
3054 or an error code otherwise.
3055
3056 Here is an example for PARMS to create an 2048 bit RSA key:
3057
3058 (genkey
3059 (rsa
3060 (nbits 4:2048)))
3061
3062 To create an Elgamal key, substitute "elg" for "rsa" and to create
3063 a DSA key use "dsa". Valid ranges for the key length depend on the
3064 algorithms; all commonly used key lengths are supported. Currently
3065 supported parameters are:
3066
3067 'nbits'
3068 This is always required to specify the length of the key. The
3069 argument is a string with a number in C-notation. The value
3070 should be a multiple of 8. Note that the S-expression syntax
3071 requires that a number is prefixed with its string length;
3072 thus the '4:' in the above example.
3073
3074 'curve NAME'
3075 For ECC a named curve may be used instead of giving the number
3076 of requested bits. This allows requesting a specific curve to
3077 override a default selection Libgcrypt would have taken if
3078 'nbits' has been given. The available names are listed with
3079 the description of the ECC public key parameters.
3080
3081 'rsa-use-e VALUE'
3082 This is only used with RSA to give a hint for the public
3083 exponent. The VALUE will be used as a base to test for a
3084 usable exponent. Some values are special:
3085
3086 '0'
3087 Use a secure and fast value. This is currently the
3088 number 41.
3089 '1'
3090 Use a value as required by some crypto policies. This is
3091 currently the number 65537.
3092 '2'
3093 Reserved
3094 '> 2'
3095 Use the given value.
3096
3097 If this parameter is not used, Libgcrypt uses for historic
3098 reasons 65537. Note that the value must fit into a 32 bit
3099 unsigned variable and that the usual C prefixes are considered
3100 (e.g., 017 gives 15).
3101
3102 'qbits N'
3103 This is only meanigful for DSA keys. If it is given, the DSA
3104 key is generated with a Q parameter of size N bits. If it is
3105 not given or zero, Q is deduced from NBITS in this way:
3106 '512 <= N <= 1024'
3107 Q = 160
3108 'N = 2048'
3109 Q = 224
3110 'N = 3072'
3111 Q = 256
3112 'N = 7680'
3113 Q = 384
3114 'N = 15360'
3115 Q = 512
3116 Note that in this case only the values for N, as given in the
3117 table, are allowed. When specifying Q, all values of N in the
3118 range 512 to 15680 are valid as long as they are multiples of
3119 8.
3120
3121 'domain LIST'
3122 This is only meaningful for DLP algorithms. If specified,
3123 keys are generated with domain parameters taken from this
3124 list. The exact format of this parameter depends on the
3125 actual algorithm. It is currently only implemented for DSA
3126 using this format:
3127
3128 (genkey
3129 (dsa
3130 (domain
3131 (p P-MPI)
3132 (q Q-MPI)
3133 (g Q-MPI))))
3134
3135 'nbits' and 'qbits' may not be specified because they are
3136 derived from the domain parameters.
3137
3138 'derive-parms LIST'
3139 This is currently only implemented for RSA and DSA keys. It
3140 is not allowed to use this together with a 'domain'
3141 specification. If given, it is used to derive the keys using
3142 the given parameters.
3143
3144 If given for an RSA key, the X9.31 key generation algorithm is
3145 used. If given for a DSA key, the FIPS 186 algorithm is used
3146 even if libgcrypt is not in FIPS mode.
3147
3148 (genkey
3149 (rsa
3150 (nbits 4:1024)
3151 (rsa-use-e 1:3)
3152 (derive-parms
3153 (Xp1 #1A1916DDB29B4EB7EB6732E128#)
3154 (Xp2 #192E8AAC41C576C822D93EA433#)
3155 (Xp #D8CD81F035EC57EFE822955149D3BFF70C53520D
3156 769D6D76646C7A792E16EBD89FE6FC5B605A6493
3157 39DFC925A86A4C6D150B71B9EEA02D68885F5009
3158 B98BD984#)
3159 (Xq1 #1A5CF72EE770DE50CB09ACCEA9#)
3160 (Xq2 #134E4CAA16D2350A21D775C404#)
3161 (Xq #CC1092495D867E64065DEE3E7955F2EBC7D47A2D
3162 7C9953388F97DDDC3E1CA19C35CA659EDC2FC325
3163 6D29C2627479C086A699A49C4C9CEE7EF7BD1B34
3164 321DE34A#))))
3165
3166 (genkey
3167 (dsa
3168 (nbits 4:1024)
3169 (derive-parms
3170 (seed SEED-MPI))))
3171
3172 'test-parms LIST'
3173 This is currently only implemented for RSA keys. If given,
3174 the libgcrypt will not generate parameter, but tests whether
3175 the p,q is probably prime. Returns key with zeroes.
3176
3177 The FIPS key generation algorithm is used even if libgcrypt is
3178 not in FIPS mode.
3179
3180 (genkey
3181 (rsa
3182 (nbits 4:1024)
3183 (rsa-use-e 1:3)
3184 (test-parms
3185 (e 5:65537)
3186 (p #00bbccabcee15d343944a47e492d4b1f4de79633e2
3187 0cbb46f7d2d6813392a807ad048cf77528edd19f77
3188 e7453f25173b9dcb70423afa2037aae147b81a33d5
3189 41fc58f875eff1e852ab55e2e09a3debfbc151b3b0
3190 d17fef6f74d81fca14fbae531418e211ef818592af
3191 70de5cec3b92795cc3578572bf456099cd8727150e
3192 523261#)
3193 (q #00ca87ecf2883f4ed00a9ec65abdeba81d28edbfcc
3194 34ecc563d587f166b52d42bfbe22bbc095b0b8426a
3195 2f8bbc55baaa8859b42cbc376ed3067db3ef7b135b
3196 63481322911ebbd7014db83aa051e0ca2dbf302b75
3197 cd37f2ae8df90e134226e92f6353a284b28bb30af0
3198 bbf925b345b955328379866ebac11d55bc80fe84f1
3199 05d415#)
3200
3201
3202 'flags FLAGLIST'
3203 This is preferred way to define flags. FLAGLIST may contain
3204 any number of flags. See above for a specification of these
3205 flags.
3206
3207 Here is an example on how to create a key using curve Ed25519
3208 with the ECDSA signature algorithm. Note that the use of
3209 ECDSA with that curve is in general not recommended.
3210 (genkey
3211 (ecc
3212 (flags transient-key)))
3213
3214 'transient-key'
3215 'use-x931'
3216 'use-fips186'
3217 'use-fips186-2'
3218 These are deprecated ways to set a flag with that name; see
3219 above for a description of each flag.
3220
3221 The key pair is returned in a format depending on the algorithm.
3222 Both private and public keys are returned in one container and may
3223 be accompanied by some miscellaneous information.
3224
3225 Here are two examples: the first for Elgamal and the second for
3226 elliptic curve key generation:
3227
3228 (key-data
3229 (public-key
3230 (elg
3231 (p P-MPI)
3232 (g G-MPI)
3233 (y Y-MPI)))
3234 (private-key
3235 (elg
3236 (p P-MPI)
3237 (g G-MPI)
3238 (y Y-MPI)
3239 (x X-MPI)))
3240 (misc-key-info
3241 (pm1-factors N1 N2 ... NN))
3242
3243 (key-data
3244 (public-key
3245 (ecc
3246 (curve Ed25519)
3247 (flags eddsa)
3248 (q Q-VALUE)))
3249 (private-key
3250 (ecc
3251 (curve Ed25519)
3252 (flags eddsa)
3253 (q Q-VALUE)
3254 (d D-VALUE))))
3255
3256 As you can see, some of the information is duplicated, but this
3257 provides an easy way to extract either the public or the private
3258 key. Note that the order of the elements is not defined, e.g., the
3259 private key may be stored before the public key. N1 N2 ... NN is a
3260 list of prime numbers used to composite P-MPI; this is in general
3261 not a very useful information and only available if the key
3262 generation algorithm provides them.
3263
3264 Future versions of Libgcrypt will have extended versions of the public
3265 key interface which will take an additional context to allow for
3266 pre-computations, special operations, and other optimization. As a
3267 first step a new function is introduced to help using the ECC algorithms
3268 in new ways:
3269
3270 -- Function: gcry_error_t gcry_pubkey_get_sexp (gcry_sexp_t *R_SEXP,
3271 int MODE, gcry_ctx_t CTX)
3272
3273 Return an S-expression representing the context CTX. Depending on
3274 the state of that context, the S-expression may either be a public
3275 key, a private key or any other object used with public key
3276 operations. On success 0 is returned and a new S-expression is
3277 stored at R_SEXP; on error an error code is returned and 'NULL' is
3278 stored at R_SEXP. MODE must be one of:
3279
3280 '0'
3281 Decide what to return depending on the context. For example
3282 if the private key parameter is available, a private key is
3283 returned; if not, a public key is returned.
3284
3285 'GCRY_PK_GET_PUBKEY'
3286 Return the public key even if the context has the private key
3287 parameter.
3288
3289 'GCRY_PK_GET_SECKEY'
3290 Return the private key or the error 'GPG_ERR_NO_SECKEY' if it
3291 is not possible.
3292
3293 As of now this function supports only certain ECC operations
3294 because a context object is right now only defined for ECC. Over
3295 time this function will be extended to cover more algorithms.
3296
3297 
3298 File: gcrypt.info, Node: Hashing, Next: Message Authentication Codes, Prev: Public Key cryptography, Up: Top
3299
3300 7 Hashing
3301 *********
3302
3303 Libgcrypt provides an easy to use and consistent interface for hashing.
3304 Hashing is buffered and several hash algorithms can be updated at once.
3305 It is possible to compute a HMAC using the same routines. The
3306 programming model follows an open/process/close paradigm and is in that
3307 similar to other building blocks provided by Libgcrypt.
3308
3309 For convenience reasons, a few cyclic redundancy check value
3310 operations are also supported.
3311
3312 * Menu:
3313
3314 * Available hash algorithms:: List of hash algorithms supported by the library.
3315 * Working with hash algorithms:: List of functions related to hashing.
3316
3317 
3318 File: gcrypt.info, Node: Available hash algorithms, Next: Working with hash algorithms, Up: Hashing
3319
3320 7.1 Available hash algorithms
3321 =============================
3322
3323 'GCRY_MD_NONE'
3324 This is not a real algorithm but used by some functions as an error
3325 return value. This constant is guaranteed to have the value '0'.
3326
3327 'GCRY_MD_SHA1'
3328 This is the SHA-1 algorithm which yields a message digest of 20
3329 bytes. Note that SHA-1 begins to show some weaknesses and it is
3330 suggested to fade out its use if strong cryptographic properties
3331 are required.
3332
3333 'GCRY_MD_RMD160'
3334 This is the 160 bit version of the RIPE message digest
3335 (RIPE-MD-160). Like SHA-1 it also yields a digest of 20 bytes.
3336 This algorithm shares a lot of design properties with SHA-1 and
3337 thus it is advisable not to use it for new protocols.
3338
3339 'GCRY_MD_MD5'
3340 This is the well known MD5 algorithm, which yields a message digest
3341 of 16 bytes. Note that the MD5 algorithm has severe weaknesses,
3342 for example it is easy to compute two messages yielding the same
3343 hash (collision attack). The use of this algorithm is only
3344 justified for non-cryptographic application.
3345
3346 'GCRY_MD_MD4'
3347 This is the MD4 algorithm, which yields a message digest of 16
3348 bytes. This algorithm has severe weaknesses and should not be
3349 used.
3350
3351 'GCRY_MD_MD2'
3352 This is a reserved identifier for MD-2; there is no implementation
3353 yet. This algorithm has severe weaknesses and should not be used.
3354
3355 'GCRY_MD_TIGER'
3356 This is the TIGER/192 algorithm which yields a message digest of 24
3357 bytes. Actually this is a variant of TIGER with a different output
3358 print order as used by GnuPG up to version 1.3.2.
3359
3360 'GCRY_MD_TIGER1'
3361 This is the TIGER variant as used by the NESSIE project. It uses
3362 the most commonly used output print order.
3363
3364 'GCRY_MD_TIGER2'
3365 This is another variant of TIGER with a different padding scheme.
3366
3367 'GCRY_MD_HAVAL'
3368 This is an reserved value for the HAVAL algorithm with 5 passes and
3369 160 bits. It yields a message digest of 20 bytes. Note that there
3370 is no implementation yet available.
3371
3372 'GCRY_MD_SHA224'
3373 This is the SHA-224 algorithm which yields a message digest of 28
3374 bytes. See Change Notice 1 for FIPS 180-2 for the specification.
3375
3376 'GCRY_MD_SHA256'
3377 This is the SHA-256 algorithm which yields a message digest of 32
3378 bytes. See FIPS 180-2 for the specification.
3379
3380 'GCRY_MD_SHA384'
3381 This is the SHA-384 algorithm which yields a message digest of 48
3382 bytes. See FIPS 180-2 for the specification.
3383
3384 'GCRY_MD_SHA512'
3385 This is the SHA-512 algorithm which yields a message digest of 64
3386 bytes. See FIPS 180-2 for the specification.
3387
3388 'GCRY_MD_SHA512_224'
3389 This is the SHA-512/224 algorithm which yields a message digest of
3390 28 bytes. See FIPS 180-4 for the specification.
3391
3392 'GCRY_MD_SHA512_256'
3393 This is the SHA-512/256 algorithm which yields a message digest of
3394 32 bytes. See FIPS 180-4 for the specification.
3395
3396 'GCRY_MD_SHA3_224'
3397 This is the SHA3-224 algorithm which yields a message digest of 28
3398 bytes. See FIPS 202 for the specification.
3399
3400 'GCRY_MD_SHA3_256'
3401 This is the SHA3-256 algorithm which yields a message digest of 32
3402 bytes. See FIPS 202 for the specification.
3403
3404 'GCRY_MD_SHA3_384'
3405 This is the SHA3-384 algorithm which yields a message digest of 48
3406 bytes. See FIPS 202 for the specification.
3407
3408 'GCRY_MD_SHA3_512'
3409 This is the SHA3-512 algorithm which yields a message digest of 64
3410 bytes. See FIPS 202 for the specification.
3411
3412 'GCRY_MD_SHAKE128'
3413 This is the SHAKE128 extendable-output function (XOF) algorithm
3414 with 128 bit security strength. See FIPS 202 for the
3415 specification.
3416
3417 'GCRY_MD_SHAKE256'
3418 This is the SHAKE256 extendable-output function (XOF) algorithm
3419 with 256 bit security strength. See FIPS 202 for the
3420 specification.
3421
3422 'GCRY_MD_CSHAKE128'
3423 This is the cSHAKE128 extendable-output function (XOF) algorithm
3424 with 128 bit security strength defined in NIST SP 800-185. cSHAKE
3425 takes two optional additional inputs N and S, which can be set by
3426 using 'gcry_md_ctl' with the control commands
3427 'GCRYCTL_MD_CUSTOMISE', and the argument 'struct
3428 gcry_cshake_customization'. The lengths of N or S is limited to
3429 255 bytes.
3430
3431 'GCRY_MD_CSHAKE256'
3432 This is the cSHAKE256 extendable-output function (XOF) algorithm
3433 with 256 bit security strength defined in NIST SP 800-185.
3434 Regarding the usage of the optional additional inputs N and S, see
3435 the above description of cSHAKE128.
3436
3437 'GCRY_MD_CRC32'
3438 This is the ISO 3309 and ITU-T V.42 cyclic redundancy check. It
3439 yields an output of 4 bytes. Note that this is not a hash
3440 algorithm in the cryptographic sense.
3441
3442 'GCRY_MD_CRC32_RFC1510'
3443 This is the above cyclic redundancy check function, as modified by
3444 RFC 1510. It yields an output of 4 bytes. Note that this is not a
3445 hash algorithm in the cryptographic sense.
3446
3447 'GCRY_MD_CRC24_RFC2440'
3448 This is the OpenPGP cyclic redundancy check function. It yields an
3449 output of 3 bytes. Note that this is not a hash algorithm in the
3450 cryptographic sense.
3451
3452 'GCRY_MD_WHIRLPOOL'
3453 This is the Whirlpool algorithm which yields a message digest of 64
3454 bytes.
3455
3456 'GCRY_MD_GOSTR3411_94'
3457 This is the hash algorithm described in GOST R 34.11-94 which
3458 yields a message digest of 32 bytes.
3459
3460 'GCRY_MD_STRIBOG256'
3461 This is the 256-bit version of hash algorithm described in GOST R
3462 34.11-2012 which yields a message digest of 32 bytes.
3463
3464 'GCRY_MD_STRIBOG512'
3465 This is the 512-bit version of hash algorithm described in GOST R
3466 34.11-2012 which yields a message digest of 64 bytes.
3467
3468 'GCRY_MD_BLAKE2B_512'
3469 This is the BLAKE2b-512 algorithm which yields a message digest of
3470 64 bytes. See RFC 7693 for the specification.
3471
3472 'GCRY_MD_BLAKE2B_384'
3473 This is the BLAKE2b-384 algorithm which yields a message digest of
3474 48 bytes. See RFC 7693 for the specification.
3475
3476 'GCRY_MD_BLAKE2B_256'
3477 This is the BLAKE2b-256 algorithm which yields a message digest of
3478 32 bytes. See RFC 7693 for the specification.
3479
3480 'GCRY_MD_BLAKE2B_160'
3481 This is the BLAKE2b-160 algorithm which yields a message digest of
3482 20 bytes. See RFC 7693 for the specification.
3483
3484 'GCRY_MD_BLAKE2S_256'
3485 This is the BLAKE2s-256 algorithm which yields a message digest of
3486 32 bytes. See RFC 7693 for the specification.
3487
3488 'GCRY_MD_BLAKE2S_224'
3489 This is the BLAKE2s-224 algorithm which yields a message digest of
3490 28 bytes. See RFC 7693 for the specification.
3491
3492 'GCRY_MD_BLAKE2S_160'
3493 This is the BLAKE2s-160 algorithm which yields a message digest of
3494 20 bytes. See RFC 7693 for the specification.
3495
3496 'GCRY_MD_BLAKE2S_128'
3497 This is the BLAKE2s-128 algorithm which yields a message digest of
3498 16 bytes. See RFC 7693 for the specification.
3499
3500 'GCRY_MD_SM3'
3501 This is the SM3 algorithm which yields a message digest of 32
3502 bytes.
3503
3504 
3505 File: gcrypt.info, Node: Working with hash algorithms, Prev: Available hash algorithms, Up: Hashing
3506
3507 7.2 Working with hash algorithms
3508 ================================
3509
3510 To use most of these function it is necessary to create a context; this
3511 is done using:
3512
3513 -- Function: gcry_error_t gcry_md_open (gcry_md_hd_t *HD, int ALGO,
3514 unsigned int FLAGS)
3515
3516 Create a message digest object for algorithm ALGO. FLAGS may be
3517 given as an bitwise OR of constants described below. ALGO may be
3518 given as '0' if the algorithms to use are later set using
3519 'gcry_md_enable'. HD is guaranteed to either receive a valid
3520 handle or 'NULL'.
3521
3522 For a list of supported algorithms, see *note Available hash
3523 algorithms::.
3524
3525 The flags allowed for MODE are:
3526
3527 'GCRY_MD_FLAG_SECURE'
3528 Allocate all buffers and the resulting digest in "secure
3529 memory". Use this if the hashed data is highly confidential.
3530
3531 'GCRY_MD_FLAG_HMAC'
3532 Turn the algorithm into a HMAC message authentication
3533 algorithm. This only works if just one algorithm is enabled
3534 for the handle and that algorithm is not an extendable-output
3535 function. Note that the function 'gcry_md_setkey' must be
3536 used to set the MAC key. The size of the MAC is equal to the
3537 message digest of the underlying hash algorithm. If you want
3538 CBC message authentication codes based on a cipher, see *note
3539 Working with cipher handles::.
3540
3541 'GCRY_MD_FLAG_BUGEMU1'
3542 Versions of Libgcrypt before 1.6.0 had a bug in the Whirlpool
3543 code which led to a wrong result for certain input sizes and
3544 write patterns. Using this flag emulates that bug. This may
3545 for example be useful for applications which use Whirlpool as
3546 part of their key generation. It is strongly suggested to use
3547 this flag only if really needed; and if possible, the data
3548 should be re-processed using the regular Whirlpool algorithm.
3549
3550 Note that this flag works for the entire hash context. If
3551 need arises, it may be used to enable bug emulation for other
3552 hash algorithms. Thus you should not use this flag for a
3553 multi-algorithm hash context.
3554
3555 You may use the function 'gcry_md_is_enabled' to later check
3556 whether an algorithm has been enabled.
3557
3558 If you want to calculate several hash algorithms at the same time,
3559 you have to use the following function right after the 'gcry_md_open':
3560
3561 -- Function: gcry_error_t gcry_md_enable (gcry_md_hd_t H, int ALGO)
3562
3563 Add the message digest algorithm ALGO to the digest object
3564 described by handle H. Duplicated enabling of algorithms is
3565 detected and ignored.
3566
3567 If the flag 'GCRY_MD_FLAG_HMAC' was used, the key for the MAC must be
3568 set using the function:
3569
3570 -- Function: gcry_error_t gcry_md_setkey (gcry_md_hd_t H, const void
3571 *KEY, size_t KEYLEN)
3572
3573 For use with the HMAC feature or BLAKE2 keyed hash, set the MAC key
3574 to the value of KEY of length KEYLEN bytes. For HMAC, there is no
3575 restriction on the length of the key. For keyed BLAKE2b hash,
3576 length of the key must be in the range 1 to 64 bytes. For keyed
3577 BLAKE2s hash, length of the key must be in the range 1 to 32 bytes.
3578
3579 After you are done with the hash calculation, you should release the
3580 resources by using:
3581
3582 -- Function: void gcry_md_close (gcry_md_hd_t H)
3583
3584 Release all resources of hash context H. H should not be used
3585 after a call to this function. A 'NULL' passed as H is ignored.
3586 The function also zeroises all sensitive information associated
3587 with this handle.
3588
3589 Often you have to do several hash operations using the same
3590 algorithm. To avoid the overhead of creating and releasing context, a
3591 reset function is provided:
3592
3593 -- Function: void gcry_md_reset (gcry_md_hd_t H)
3594
3595 Reset the current context to its initial state. This is
3596 effectively identical to a close followed by an open and enabling
3597 all currently active algorithms.
3598
3599 Often it is necessary to start hashing some data and then continue to
3600 hash different data. To avoid hashing the same data several times
3601 (which might not even be possible if the data is received from a pipe),
3602 a snapshot of the current hash context can be taken and turned into a
3603 new context:
3604
3605 -- Function: gcry_error_t gcry_md_copy (gcry_md_hd_t *HANDLE_DST,
3606 gcry_md_hd_t HANDLE_SRC)
3607
3608 Create a new digest object as an exact copy of the object described
3609 by handle HANDLE_SRC and store it in HANDLE_DST. The context is
3610 not reset and you can continue to hash data using this context and
3611 independently using the original context.
3612
3613 Now that we have prepared everything to calculate hashes, it is time
3614 to see how it is actually done. There are two ways for this: one to
3615 update the hash with a block of memory and one macro to update the hash
3616 by just one character. Both methods can be used on the same hash
3617 context.
3618
3619 -- Function: void gcry_md_write (gcry_md_hd_t H, const void *BUFFER,
3620 size_t LENGTH)
3621
3622 Pass LENGTH bytes of the data in BUFFER to the digest object with
3623 handle H to update the digest values. This function should be used
3624 for large blocks of data. If this function is used after the
3625 context has been finalized, it will keep on pushing the data
3626 through the algorithm specific transform function and change the
3627 context; however the results are not meaningful and this feature is
3628 only available to mitigate timing attacks.
3629
3630 -- Function: void gcry_md_putc (gcry_md_hd_t H, int C)
3631
3632 Pass the byte in C to the digest object with handle H to update the
3633 digest value. This is an efficient function, implemented as a
3634 macro to buffer the data before an actual update.
3635
3636 The semantics of the hash functions do not provide for reading out
3637 intermediate message digests because the calculation must be finalized
3638 first. This finalization may for example include the number of bytes
3639 hashed in the message digest or some padding.
3640
3641 -- Function: void gcry_md_final (gcry_md_hd_t H)
3642
3643 Finalize the message digest calculation. This is not really needed
3644 because 'gcry_md_read' and 'gcry_md_extract' do this implicitly.
3645 After this has been done no further updates (by means of
3646 'gcry_md_write' or 'gcry_md_putc') should be done; However, to
3647 mitigate timing attacks it is sometimes useful to keep on updating
3648 the context after having stored away the actual digest. Only the
3649 first call to this function has an effect. It is implemented as a
3650 macro.
3651
3652 The way to read out the calculated message digest is by using the
3653 function:
3654
3655 -- Function: unsigned char * gcry_md_read (gcry_md_hd_t H, int ALGO)
3656
3657 'gcry_md_read' returns the message digest after finalizing the
3658 calculation. This function may be used as often as required but it
3659 will always return the same value for one handle. The returned
3660 message digest is allocated within the message context and
3661 therefore valid until the handle is released or reset-ed (using
3662 'gcry_md_close' or 'gcry_md_reset') or it has been updated as a
3663 mitigation measure against timing attacks. ALGO may be given as 0
3664 to return the only enabled message digest or it may specify one of
3665 the enabled algorithms. The function does return 'NULL' if the
3666 requested algorithm has not been enabled.
3667
3668 The way to read output of extendable-output function is by using the
3669 function:
3670
3671 -- Function: gpg_err_code_t gcry_md_extract (gcry_md_hd_t H, int ALGO,
3672 void *BUFFER, size_t LENGTH)
3673
3674 'gcry_md_extract' returns output from extendable-output function.
3675 This function may be used as often as required to generate more
3676 output byte stream from the algorithm. Function extracts the new
3677 output bytes to BUFFER of the length LENGTH. Buffer will be fully
3678 populated with new output. ALGO may be given as 0 to return the
3679 only enabled message digest or it may specify one of the enabled
3680 algorithms. The function does return non-zero value if the
3681 requested algorithm has not been enabled.
3682
3683 Because it is often necessary to get the message digest of blocks of
3684 memory, three fast convenience functions are available for this task:
3685
3686 -- Function: gpg_err_code_t gcry_md_hash_buffers ( int ALGO,
3687 unsigned int FLAGS, void *DIGEST, const gcry_buffer_t *IOV,
3688 int IOVCNT )
3689
3690 'gcry_md_hash_buffers' is a shortcut function to calculate a
3691 message digest from several buffers. This function does not
3692 require a context and immediately returns the message digest of the
3693 data described by IOV and IOVCNT. DIGEST must be allocated by the
3694 caller, large enough to hold the message digest yielded by the the
3695 specified algorithm ALGO. This required size may be obtained by
3696 using the function 'gcry_md_get_algo_dlen'.
3697
3698 IOV is an array of buffer descriptions with IOVCNT items. The
3699 caller should zero out the structures in this array and for each
3700 array item set the fields '.data' to the address of the data to be
3701 hashed, '.len' to number of bytes to be hashed. If .OFF is also
3702 set, the data is taken starting at .OFF bytes from the begin of the
3703 buffer. The field '.size' is not used.
3704
3705 The only supported flag value for FLAGS is GCRY_MD_FLAG_HMAC which
3706 turns this function into a HMAC function; the first item in IOV is
3707 then used as the key.
3708
3709 On success the function returns 0 and stores the resulting hash or
3710 MAC at DIGEST.
3711
3712 -- Function: gpg_err_code_t gcry_md_hash_buffers_ext ( int ALGO,
3713 unsigned int FLAGS, void *DIGEST, void *DIGESTLEN,
3714 const gcry_buffer_t *IOV, int IOVCNT )
3715
3716 'gcry_md_hash_buffers_ext' is a variant of gcry_md_hash_buffers, so
3717 that it can be used with extendable-output function. It has an
3718 additiona argument for DIGESTLEN.
3719
3720 -- Function: void gcry_md_hash_buffer (int ALGO, void *DIGEST, const
3721 void *BUFFER, size_t LENGTH);
3722
3723 'gcry_md_hash_buffer' is a shortcut function to calculate a message
3724 digest of a buffer. This function does not require a context and
3725 immediately returns the message digest of the LENGTH bytes at
3726 BUFFER. DIGEST must be allocated by the caller, large enough to
3727 hold the message digest yielded by the specified algorithm ALGO.
3728 This required size may be obtained by using the function
3729 'gcry_md_get_algo_dlen'.
3730
3731 Note that in contrast to 'gcry_md_hash_buffers' this function will
3732 abort the process if an unavailable algorithm is used.
3733
3734 Hash algorithms are identified by internal algorithm numbers (see
3735 'gcry_md_open' for a list). However, in most applications they are used
3736 by names, so two functions are available to map between string
3737 representations and hash algorithm identifiers.
3738
3739 -- Function: const char * gcry_md_algo_name (int ALGO)
3740
3741 Map the digest algorithm id ALGO to a string representation of the
3742 algorithm name. For unknown algorithms this function returns the
3743 string '"?"'. This function should not be used to test for the
3744 availability of an algorithm.
3745
3746 -- Function: int gcry_md_map_name (const char *NAME)
3747
3748 Map the algorithm with NAME to a digest algorithm identifier.
3749 Returns 0 if the algorithm name is not known. Names representing
3750 ASN.1 object identifiers are recognized if the IETF dotted format
3751 is used and the OID is prefixed with either "'oid.'" or "'OID.'".
3752 For a list of supported OIDs, see the source code at 'cipher/md.c'.
3753 This function should not be used to test for the availability of an
3754 algorithm.
3755
3756 -- Function: gcry_error_t gcry_md_get_asnoid (int ALGO, void *BUFFER,
3757 size_t *LENGTH)
3758
3759 Return an DER encoded ASN.1 OID for the algorithm ALGO in the user
3760 allocated BUFFER. LENGTH must point to variable with the available
3761 size of BUFFER and receives after return the actual size of the
3762 returned OID. The returned error code may be 'GPG_ERR_TOO_SHORT' if
3763 the provided buffer is too short to receive the OID; it is possible
3764 to call the function with 'NULL' for BUFFER to have it only return
3765 the required size. The function returns 0 on success.
3766
3767 To test whether an algorithm is actually available for use, the
3768 following macro should be used:
3769
3770 -- Function: gcry_error_t gcry_md_test_algo (int ALGO)
3771
3772 The macro returns 0 if the algorithm ALGO is available for use.
3773
3774 If the length of a message digest is not known, it can be retrieved
3775 using the following function:
3776
3777 -- Function: unsigned int gcry_md_get_algo_dlen (int ALGO)
3778
3779 Retrieve the length in bytes of the digest yielded by algorithm
3780 ALGO. This is often used prior to 'gcry_md_read' to allocate
3781 sufficient memory for the digest.
3782
3783 In some situations it might be hard to remember the algorithm used
3784 for the ongoing hashing. The following function might be used to get
3785 that information:
3786
3787 -- Function: int gcry_md_get_algo (gcry_md_hd_t H)
3788
3789 Retrieve the algorithm used with the handle H. Note that this does
3790 not work reliable if more than one algorithm is enabled in H.
3791
3792 The following macro might also be useful:
3793
3794 -- Function: int gcry_md_is_secure (gcry_md_hd_t H)
3795
3796 This function returns true when the digest object H is allocated in
3797 "secure memory"; i.e., H was created with the
3798 'GCRY_MD_FLAG_SECURE'.
3799
3800 -- Function: int gcry_md_is_enabled (gcry_md_hd_t H, int ALGO)
3801
3802 This function returns true when the algorithm ALGO has been enabled
3803 for the digest object H.
3804
3805 Tracking bugs related to hashing is often a cumbersome task which
3806 requires to add a lot of printf statements into the code. Libgcrypt
3807 provides an easy way to avoid this. The actual data hashed can be
3808 written to files on request.
3809
3810 -- Function: void gcry_md_debug (gcry_md_hd_t H, const char *SUFFIX)
3811
3812 Enable debugging for the digest object with handle H. This creates
3813 files named 'dbgmd-<n>.<string>' while doing the actual hashing.
3814 SUFFIX is the string part in the filename. The number is a counter
3815 incremented for each new hashing. The data in the file is the raw
3816 data as passed to 'gcry_md_write' or 'gcry_md_putc'. If 'NULL' is
3817 used for SUFFIX, the debugging is stopped and the file closed.
3818 This is only rarely required because 'gcry_md_close' implicitly
3819 stops debugging.
3820
3821 
3822 File: gcrypt.info, Node: Message Authentication Codes, Next: Key Derivation, Prev: Hashing, Up: Top
3823
3824 8 Message Authentication Codes
3825 ******************************
3826
3827 Libgcrypt provides an easy to use and consistent interface for
3828 generating Message Authentication Codes (MAC). MAC generation is
3829 buffered and interface similar to the one used with hash algorithms.
3830 The programming model follows an open/process/close paradigm and is in
3831 that similar to other building blocks provided by Libgcrypt.
3832
3833 * Menu:
3834
3835 * Available MAC algorithms:: List of MAC algorithms supported by the library.
3836 * Working with MAC algorithms:: List of functions related to MAC algorithms.
3837
3838 
3839 File: gcrypt.info, Node: Available MAC algorithms, Next: Working with MAC algorithms, Up: Message Authentication Codes
3840
3841 8.1 Available MAC algorithms
3842 ============================
3843
3844 'GCRY_MAC_NONE'
3845 This is not a real algorithm but used by some functions as an error
3846 return value. This constant is guaranteed to have the value '0'.
3847
3848 'GCRY_MAC_HMAC_SHA256'
3849 This is keyed-hash message authentication code (HMAC) message
3850 authentication algorithm based on the SHA-256 hash algorithm.
3851
3852 'GCRY_MAC_HMAC_SHA224'
3853 This is HMAC message authentication algorithm based on the SHA-224
3854 hash algorithm.
3855
3856 'GCRY_MAC_HMAC_SHA512'
3857 This is HMAC message authentication algorithm based on the SHA-512
3858 hash algorithm.
3859
3860 'GCRY_MAC_HMAC_SHA384'
3861 This is HMAC message authentication algorithm based on the SHA-384
3862 hash algorithm.
3863
3864 'GCRY_MAC_HMAC_SHA3_256'
3865 This is HMAC message authentication algorithm based on the SHA3-256
3866 hash algorithm.
3867
3868 'GCRY_MAC_HMAC_SHA3_224'
3869 This is HMAC message authentication algorithm based on the SHA3-224
3870 hash algorithm.
3871
3872 'GCRY_MAC_HMAC_SHA3_512'
3873 This is HMAC message authentication algorithm based on the SHA3-512
3874 hash algorithm.
3875
3876 'GCRY_MAC_HMAC_SHA3_384'
3877 This is HMAC message authentication algorithm based on the SHA3-384
3878 hash algorithm.
3879
3880 'GCRY_MAC_HMAC_SHA512_224'
3881 This is HMAC message authentication algorithm based on the
3882 SHA-512/224 hash algorithm.
3883
3884 'GCRY_MAC_HMAC_SHA512_256'
3885 This is HMAC message authentication algorithm based on the
3886 SHA-512/256 hash algorithm.
3887
3888 'GCRY_MAC_HMAC_SHA1'
3889 This is HMAC message authentication algorithm based on the SHA-1
3890 hash algorithm.
3891
3892 'GCRY_MAC_HMAC_MD5'
3893 This is HMAC message authentication algorithm based on the MD5 hash
3894 algorithm.
3895
3896 'GCRY_MAC_HMAC_MD4'
3897 This is HMAC message authentication algorithm based on the MD4 hash
3898 algorithm.
3899
3900 'GCRY_MAC_HMAC_RMD160'
3901 This is HMAC message authentication algorithm based on the
3902 RIPE-MD-160 hash algorithm.
3903
3904 'GCRY_MAC_HMAC_WHIRLPOOL'
3905 This is HMAC message authentication algorithm based on the
3906 WHIRLPOOL hash algorithm.
3907
3908 'GCRY_MAC_HMAC_GOSTR3411_94'
3909 This is HMAC message authentication algorithm based on the GOST R
3910 34.11-94 hash algorithm.
3911
3912 'GCRY_MAC_HMAC_STRIBOG256'
3913 This is HMAC message authentication algorithm based on the 256-bit
3914 hash algorithm described in GOST R 34.11-2012.
3915
3916 'GCRY_MAC_HMAC_STRIBOG512'
3917 This is HMAC message authentication algorithm based on the 512-bit
3918 hash algorithm described in GOST R 34.11-2012.
3919
3920 'GCRY_MAC_HMAC_BLAKE2B_512'
3921 This is HMAC message authentication algorithm based on the
3922 BLAKE2b-512 hash algorithm.
3923
3924 'GCRY_MAC_HMAC_BLAKE2B_384'
3925 This is HMAC message authentication algorithm based on the
3926 BLAKE2b-384 hash algorithm.
3927
3928 'GCRY_MAC_HMAC_BLAKE2B_256'
3929 This is HMAC message authentication algorithm based on the
3930 BLAKE2b-256 hash algorithm.
3931
3932 'GCRY_MAC_HMAC_BLAKE2B_160'
3933 This is HMAC message authentication algorithm based on the
3934 BLAKE2b-160 hash algorithm.
3935
3936 'GCRY_MAC_HMAC_BLAKE2S_256'
3937 This is HMAC message authentication algorithm based on the
3938 BLAKE2s-256 hash algorithm.
3939
3940 'GCRY_MAC_HMAC_BLAKE2S_224'
3941 This is HMAC message authentication algorithm based on the
3942 BLAKE2s-224 hash algorithm.
3943
3944 'GCRY_MAC_HMAC_BLAKE2S_160'
3945 This is HMAC message authentication algorithm based on the
3946 BLAKE2s-160 hash algorithm.
3947
3948 'GCRY_MAC_HMAC_BLAKE2S_128'
3949 This is HMAC message authentication algorithm based on the
3950 BLAKE2s-128 hash algorithm.
3951
3952 'GCRY_MAC_HMAC_SM3'
3953 This is HMAC message authentication algorithm based on the SM3 hash
3954 algorithm.
3955
3956 'GCRY_MAC_CMAC_AES'
3957 This is CMAC (Cipher-based MAC) message authentication algorithm
3958 based on the AES block cipher algorithm.
3959
3960 'GCRY_MAC_CMAC_3DES'
3961 This is CMAC message authentication algorithm based on the
3962 three-key EDE Triple-DES block cipher algorithm.
3963
3964 'GCRY_MAC_CMAC_CAMELLIA'
3965 This is CMAC message authentication algorithm based on the Camellia
3966 block cipher algorithm.
3967
3968 'GCRY_MAC_CMAC_CAST5'
3969 This is CMAC message authentication algorithm based on the
3970 CAST128-5 block cipher algorithm.
3971
3972 'GCRY_MAC_CMAC_BLOWFISH'
3973 This is CMAC message authentication algorithm based on the Blowfish
3974 block cipher algorithm.
3975
3976 'GCRY_MAC_CMAC_TWOFISH'
3977 This is CMAC message authentication algorithm based on the Twofish
3978 block cipher algorithm.
3979
3980 'GCRY_MAC_CMAC_SERPENT'
3981 This is CMAC message authentication algorithm based on the Serpent
3982 block cipher algorithm.
3983
3984 'GCRY_MAC_CMAC_SEED'
3985 This is CMAC message authentication algorithm based on the SEED
3986 block cipher algorithm.
3987
3988 'GCRY_MAC_CMAC_RFC2268'
3989 This is CMAC message authentication algorithm based on the Ron's
3990 Cipher 2 block cipher algorithm.
3991
3992 'GCRY_MAC_CMAC_IDEA'
3993 This is CMAC message authentication algorithm based on the IDEA
3994 block cipher algorithm.
3995
3996 'GCRY_MAC_CMAC_GOST28147'
3997 This is CMAC message authentication algorithm based on the GOST
3998 28147-89 block cipher algorithm.
3999
4000 'GCRY_MAC_CMAC_SM4'
4001 This is CMAC message authentication algorithm based on the SM4
4002 block cipher algorithm.
4003
4004 'GCRY_MAC_CMAC_ARIA'
4005 This is CMAC message authentication algorithm based on the ARIA
4006 block cipher algorithm.
4007
4008 'GCRY_MAC_GMAC_AES'
4009 This is GMAC (GCM mode based MAC) message authentication algorithm
4010 based on the AES block cipher algorithm.
4011
4012 'GCRY_MAC_GMAC_CAMELLIA'
4013 This is GMAC message authentication algorithm based on the Camellia
4014 block cipher algorithm.
4015
4016 'GCRY_MAC_GMAC_TWOFISH'
4017 This is GMAC message authentication algorithm based on the Twofish
4018 block cipher algorithm.
4019
4020 'GCRY_MAC_GMAC_SERPENT'
4021 This is GMAC message authentication algorithm based on the Serpent
4022 block cipher algorithm.
4023
4024 'GCRY_MAC_GMAC_SEED'
4025 This is GMAC message authentication algorithm based on the SEED
4026 block cipher algorithm.
4027
4028 'GCRY_MAC_GMAC_SM4'
4029 This is GMAC message authentication algorithm based on the SM4
4030 block cipher algorithm.
4031
4032 'GCRY_MAC_GMAC_ARIA'
4033 This is GMAC message authentication algorithm based on the ARIA
4034 block cipher algorithm.
4035
4036 'GCRY_MAC_POLY1305'
4037 This is plain Poly1305 message authentication algorithm, used with
4038 one-time key.
4039
4040 'GCRY_MAC_POLY1305_AES'
4041 This is Poly1305-AES message authentication algorithm, used with
4042 key and one-time nonce.
4043
4044 'GCRY_MAC_POLY1305_CAMELLIA'
4045 This is Poly1305-Camellia message authentication algorithm, used
4046 with key and one-time nonce.
4047
4048 'GCRY_MAC_POLY1305_TWOFISH'
4049 This is Poly1305-Twofish message authentication algorithm, used
4050 with key and one-time nonce.
4051
4052 'GCRY_MAC_POLY1305_SERPENT'
4053 This is Poly1305-Serpent message authentication algorithm, used
4054 with key and one-time nonce.
4055
4056 'GCRY_MAC_POLY1305_SEED'
4057 This is Poly1305-SEED message authentication algorithm, used with
4058 key and one-time nonce.
4059
4060 'GCRY_MAC_POLY1305_SM4'
4061 This is Poly1305-SM4 message authentication algorithm, used with
4062 key and one-time nonce.
4063
4064 'GCRY_MAC_POLY1305_ARIA'
4065 This is Poly1305-ARIA message authentication algorithm, used with
4066 key and one-time nonce.
4067
4068 'GCRY_MAC_GOST28147_IMIT'
4069 This is MAC construction defined in GOST 28147-89 (see RFC 5830
4070 Section 8).
4071
4072 
4073 File: gcrypt.info, Node: Working with MAC algorithms, Prev: Available MAC algorithms, Up: Message Authentication Codes
4074
4075 8.2 Working with MAC algorithms
4076 ===============================
4077
4078 To use most of these function it is necessary to create a context; this
4079 is done using:
4080
4081 -- Function: gcry_error_t gcry_mac_open (gcry_mac_hd_t *HD, int ALGO,
4082 unsigned int FLAGS, gcry_ctx_t CTX)
4083
4084 Create a MAC object for algorithm ALGO. FLAGS may be given as a
4085 bitwise OR of constants described below. HD is guaranteed to
4086 either receive a valid handle or 'NULL'. CTX is context object to
4087 associate MAC object with. CTX maybe set to 'NULL'.
4088
4089 For a list of supported algorithms, see *note Available MAC
4090 algorithms::.
4091
4092 The flags allowed for MODE are:
4093
4094 'GCRY_MAC_FLAG_SECURE'
4095 Allocate all buffers and the resulting MAC in "secure memory".
4096 Use this if the MAC data is highly confidential.
4097
4098 In order to use a handle for performing MAC algorithm operations, a
4099 'key' has to be set first:
4100
4101 -- Function: gcry_error_t gcry_mac_setkey (gcry_mac_hd_t H, const void
4102 *KEY, size_t KEYLEN)
4103
4104 Set the MAC key to the value of KEY of length KEYLEN bytes. With
4105 HMAC algorithms, there is no restriction on the length of the key.
4106 With CMAC algorithms, the length of the key is restricted to those
4107 supported by the underlying block cipher.
4108
4109 GMAC algorithms and Poly1305-with-cipher algorithms need
4110 initialization vector to be set, which can be performed with function:
4111
4112 -- Function: gcry_error_t gcry_mac_setiv (gcry_mac_hd_t H, const void
4113 *IV, size_t IVLEN)
4114
4115 Set the IV to the value of IV of length IVLEN bytes.
4116
4117 After you are done with the MAC calculation, you should release the
4118 resources by using:
4119
4120 -- Function: void gcry_mac_close (gcry_mac_hd_t H)
4121
4122 Release all resources of MAC context H. H should not be used after
4123 a call to this function. A 'NULL' passed as H is ignored. The
4124 function also clears all sensitive information associated with this
4125 handle.
4126
4127 Often you have to do several MAC operations using the same algorithm.
4128 To avoid the overhead of creating and releasing context, a reset
4129 function is provided:
4130
4131 -- Function: gcry_error_t gcry_mac_reset (gcry_mac_hd_t H)
4132
4133 Reset the current context to its initial state. This is
4134 effectively identical to a close followed by an open and setting
4135 same key.
4136
4137 Note that gcry_mac_reset is implemented as a macro.
4138
4139 Now that we have prepared everything to calculate MAC, it is time to
4140 see how it is actually done.
4141
4142 -- Function: gcry_error_t gcry_mac_write (gcry_mac_hd_t H, const void
4143 *BUFFER, size_t LENGTH)
4144
4145 Pass LENGTH bytes of the data in BUFFER to the MAC object with
4146 handle H to update the MAC values. If this function is used after
4147 the context has been finalized, it will keep on pushing the data
4148 through the algorithm specific transform function and thereby
4149 change the context; however the results are not meaningful and this
4150 feature is only available to mitigate timing attacks.
4151
4152 The way to read out the calculated MAC is by using the function:
4153
4154 -- Function: gcry_error_t gcry_mac_read (gcry_mac_hd_t H, void *BUFFER,
4155 size_t *LENGTH)
4156
4157 'gcry_mac_read' returns the MAC after finalizing the calculation.
4158 Function copies the resulting MAC value to BUFFER of the length
4159 LENGTH. If LENGTH is larger than length of resulting MAC value,
4160 then length of MAC is returned through LENGTH.
4161
4162 To compare existing MAC value with recalculated MAC, one is to use
4163 the function:
4164
4165 -- Function: gcry_error_t gcry_mac_verify (gcry_mac_hd_t H, void
4166 *BUFFER, size_t LENGTH)
4167
4168 'gcry_mac_verify' finalizes MAC calculation and compares result
4169 with LENGTH bytes of data in BUFFER. Error code 'GPG_ERR_CHECKSUM'
4170 is returned if the MAC value in the buffer BUFFER does not match
4171 the MAC calculated in object H.
4172
4173 In some situations it might be hard to remember the algorithm used
4174 for the MAC calculation. The following function might be used to get
4175 that information:
4176
4177 -- Function: int gcry_mac_get_algo (gcry_mac_hd_t H)
4178
4179 Retrieve the algorithm used with the handle H.
4180
4181 MAC algorithms are identified by internal algorithm numbers (see
4182 'gcry_mac_open' for a list). However, in most applications they are
4183 used by names, so two functions are available to map between string
4184 representations and MAC algorithm identifiers.
4185
4186 -- Function: const char * gcry_mac_algo_name (int ALGO)
4187
4188 Map the MAC algorithm id ALGO to a string representation of the
4189 algorithm name. For unknown algorithms this function returns the
4190 string '"?"'. This function should not be used to test for the
4191 availability of an algorithm.
4192
4193 -- Function: int gcry_mac_map_name (const char *NAME)
4194
4195 Map the algorithm with NAME to a MAC algorithm identifier. Returns
4196 0 if the algorithm name is not known. This function should not be
4197 used to test for the availability of an algorithm.
4198
4199 To test whether an algorithm is actually available for use, the
4200 following macro should be used:
4201
4202 -- Function: gcry_error_t gcry_mac_test_algo (int ALGO)
4203
4204 The macro returns 0 if the MAC algorithm ALGO is available for use.
4205
4206 If the length of a message digest is not known, it can be retrieved
4207 using the following function:
4208
4209 -- Function: unsigned int gcry_mac_get_algo_maclen (int ALGO)
4210
4211 Retrieve the length in bytes of the MAC yielded by algorithm ALGO.
4212 This is often used prior to 'gcry_mac_read' to allocate sufficient
4213 memory for the MAC value. On error '0' is returned.
4214
4215 -- Function: unsigned int gcry_mac_get_algo_keylen (ALGO)
4216
4217 This function returns length of the key for MAC algorithm ALGO. If
4218 the algorithm supports multiple key lengths, the default supported
4219 key length is returned. On error '0' is returned. The key length
4220 is returned as number of octets.
4221
4222 
4223 File: gcrypt.info, Node: Key Derivation, Next: Random Numbers, Prev: Message Authentication Codes, Up: Top
4224
4225 9 Key Derivation
4226 ****************
4227
4228 Libgcypt provides a general purpose function to derive keys from
4229 strings.
4230
4231 -- Function: gpg_error_t gcry_kdf_derive ( const void *PASSPHRASE,
4232 size_t PASSPHRASELEN, int ALGO, int SUBALGO, const void *SALT,
4233 size_t SALTLEN, unsigned long ITERATIONS, size_t KEYSIZE,
4234 void *KEYBUFFER )
4235
4236 Derive a key from a passphrase. KEYSIZE gives the requested size
4237 of the key in octets. KEYBUFFER is a caller provided buffer filled
4238 on success with the derived key. The input passphrase is taken
4239 from PASSPHRASE which is an arbitrary memory buffer of
4240 PASSPHRASELEN octets. ALGO specifies the KDF algorithm to use; see
4241 below. SUBALGO specifies an algorithm used internally by the KDF
4242 algorithms; this is usually a hash algorithm but certain KDF
4243 algorithms may use it differently. SALT is a salt of length
4244 SALTLEN octets, as needed by most KDF algorithms. ITERATIONS is a
4245 positive integer parameter to most KDFs.
4246
4247 On success 0 is returned; on failure an error code.
4248
4249 Currently supported KDFs (parameter ALGO):
4250
4251 'GCRY_KDF_SIMPLE_S2K'
4252 The OpenPGP simple S2K algorithm (cf. RFC4880). Its use is
4253 strongly deprecated. SALT and ITERATIONS are not needed and
4254 may be passed as 'NULL'/'0'.
4255
4256 'GCRY_KDF_SALTED_S2K'
4257 The OpenPGP salted S2K algorithm (cf. RFC4880). Usually not
4258 used. ITERATIONS is not needed and may be passed as '0'.
4259 SALTLEN must be given as 8.
4260
4261 'GCRY_KDF_ITERSALTED_S2K'
4262 The OpenPGP iterated+salted S2K algorithm (cf. RFC4880). This
4263 is the default for most OpenPGP applications. SALTLEN must be
4264 given as 8. Note that OpenPGP defines a special encoding of
4265 the ITERATIONS; however this function takes the plain decoded
4266 iteration count.
4267
4268 'GCRY_KDF_PBKDF2'
4269 The PKCS#5 Passphrase Based Key Derivation Function number 2.
4270
4271 'GCRY_KDF_SCRYPT'
4272 The SCRYPT Key Derivation Function. The subalgorithm is used
4273 to specify the CPU/memory cost parameter N, and the number of
4274 iterations is used for the parallelization parameter p. The
4275 block size is fixed at 8 in the current implementation.
4276
4277 
4278 File: gcrypt.info, Node: Random Numbers, Next: S-expressions, Prev: Key Derivation, Up: Top
4279
4280 10 Random Numbers
4281 *****************
4282
4283 * Menu:
4284
4285 * Quality of random numbers:: Libgcrypt uses different quality levels.
4286 * Retrieving random numbers:: How to retrieve random numbers.
4287
4288 
4289 File: gcrypt.info, Node: Quality of random numbers, Next: Retrieving random numbers, Up: Random Numbers
4290
4291 10.1 Quality of random numbers
4292 ==============================
4293
4294 Libgcypt offers random numbers of different quality levels:
4295
4296 -- Data type: gcry_random_level_t
4297 The constants for the random quality levels are of this enum type.
4298
4299 'GCRY_WEAK_RANDOM'
4300 For all functions, except for 'gcry_mpi_randomize', this level maps
4301 to 'GCRY_STRONG_RANDOM'. If you do not want this, consider using
4302 'gcry_create_nonce'.
4303 'GCRY_STRONG_RANDOM'
4304 Use this level for session keys and similar purposes.
4305 'GCRY_VERY_STRONG_RANDOM'
4306 Use this level for long term key material.
4307
4308 
4309 File: gcrypt.info, Node: Retrieving random numbers, Prev: Quality of random numbers, Up: Random Numbers
4310
4311 10.2 Retrieving random numbers
4312 ==============================
4313
4314 -- Function: void gcry_randomize (unsigned char *BUFFER, size_t LENGTH,
4315 enum gcry_random_level LEVEL)
4316
4317 Fill BUFFER with LENGTH random bytes using a random quality as
4318 defined by LEVEL.
4319
4320 -- Function: void * gcry_random_bytes (size_t NBYTES, enum
4321 gcry_random_level LEVEL)
4322
4323 Convenience function to allocate a memory block consisting of
4324 NBYTES fresh random bytes using a random quality as defined by
4325 LEVEL.
4326
4327 -- Function: void * gcry_random_bytes_secure (size_t NBYTES, enum
4328 gcry_random_level LEVEL)
4329
4330 Convenience function to allocate a memory block consisting of
4331 NBYTES fresh random bytes using a random quality as defined by
4332 LEVEL. This function differs from 'gcry_random_bytes' in that the
4333 returned buffer is allocated in a "secure" area of the memory.
4334
4335 -- Function: void gcry_create_nonce (unsigned char *BUFFER, size_t
4336 LENGTH)
4337
4338 Fill BUFFER with LENGTH unpredictable bytes. This is commonly
4339 called a nonce and may also be used for initialization vectors and
4340 padding. This is an extra function nearly independent of the other
4341 random function for 3 reasons: It better protects the regular
4342 random generator's internal state, provides better performance and
4343 does not drain the precious entropy pool.
4344
4345 
4346 File: gcrypt.info, Node: S-expressions, Next: MPI library, Prev: Random Numbers, Up: Top
4347
4348 11 S-expressions
4349 ****************
4350
4351 S-expressions are used by the public key functions to pass complex data
4352 structures around. These LISP like objects are used by some
4353 cryptographic protocols (cf. RFC-2692) and Libgcrypt provides functions
4354 to parse and construct them. For detailed information, see 'Ron Rivest,
4355 code and description of S-expressions,
4356 <http://theory.lcs.mit.edu/~rivest/sexp.html>'.
4357
4358 * Menu:
4359
4360 * Data types for S-expressions:: Data types related to S-expressions.
4361 * Working with S-expressions:: How to work with S-expressions.
4362
4363 
4364 File: gcrypt.info, Node: Data types for S-expressions, Next: Working with S-expressions, Up: S-expressions
4365
4366 11.1 Data types for S-expressions
4367 =================================
4368
4369 -- Data type: gcry_sexp_t
4370 The 'gcry_sexp_t' type describes an object with the Libgcrypt
4371 internal representation of an S-expression.
4372
4373 
4374 File: gcrypt.info, Node: Working with S-expressions, Prev: Data types for S-expressions, Up: S-expressions
4375
4376 11.2 Working with S-expressions
4377 ===============================
4378
4379 There are several functions to create an Libgcrypt S-expression object
4380 from its external representation or from a string template. There is
4381 also a function to convert the internal representation back into one of
4382 the external formats:
4383
4384 -- Function: gcry_error_t gcry_sexp_new (gcry_sexp_t *R_SEXP,
4385 const void *BUFFER, size_t LENGTH, int AUTODETECT)
4386
4387 This is the generic function to create an new S-expression object
4388 from its external representation in BUFFER of LENGTH bytes. On
4389 success the result is stored at the address given by R_SEXP. With
4390 AUTODETECT set to 0, the data in BUFFER is expected to be in
4391 canonized format, with AUTODETECT set to 1 the function parses any
4392 of the defined external formats. If BUFFER does not hold a valid
4393 S-expression, an error code is returned and R_SEXP set to 'NULL'.
4394 Note that the caller is responsible for releasing the newly
4395 allocated S-expression using 'gcry_sexp_release'.
4396
4397 -- Function: gcry_error_t gcry_sexp_create (gcry_sexp_t *R_SEXP,
4398 void *BUFFER, size_t LENGTH, int AUTODETECT,
4399 void (*FREEFNC)(void*))
4400
4401 This function is identical to 'gcry_sexp_new' but has an extra
4402 argument FREEFNC, which, when not set to 'NULL', is expected to be
4403 a function to release the BUFFER; most likely the standard 'free'
4404 function is used for this argument. This has the effect of
4405 transferring the ownership of BUFFER to the created object in
4406 R_SEXP. The advantage of using this function is that Libgcrypt
4407 might decide to directly use the provided buffer and thus avoid
4408 extra copying.
4409
4410 -- Function: gcry_error_t gcry_sexp_sscan (gcry_sexp_t *R_SEXP,
4411 size_t *ERROFF, const char *BUFFER, size_t LENGTH)
4412
4413 This is another variant of the above functions. It behaves nearly
4414 identical but provides an ERROFF argument which will receive the
4415 offset into the buffer where the parsing stopped on error.
4416
4417 -- Function: gcry_error_t gcry_sexp_build (gcry_sexp_t *R_SEXP,
4418 size_t *ERROFF, const char *FORMAT, ...)
4419
4420 This function creates an internal S-expression from the string
4421 template FORMAT and stores it at the address of R_SEXP. If there
4422 is a parsing error, the function returns an appropriate error code
4423 and stores the offset into FORMAT where the parsing stopped in
4424 ERROFF. The function supports a couple of printf-like formatting
4425 characters and expects arguments for some of these escape sequences
4426 right after FORMAT. The following format characters are defined:
4427
4428 '%m'
4429 The next argument is expected to be of type 'gcry_mpi_t' and a
4430 copy of its value is inserted into the resulting S-expression.
4431 The MPI is stored as a signed integer.
4432 '%M'
4433 The next argument is expected to be of type 'gcry_mpi_t' and a
4434 copy of its value is inserted into the resulting S-expression.
4435 The MPI is stored as an unsigned integer.
4436 '%s'
4437 The next argument is expected to be of type 'char *' and that
4438 string is inserted into the resulting S-expression.
4439 '%d'
4440 The next argument is expected to be of type 'int' and its
4441 value is inserted into the resulting S-expression.
4442 '%u'
4443 The next argument is expected to be of type 'unsigned int' and
4444 its value is inserted into the resulting S-expression.
4445 '%b'
4446 The next argument is expected to be of type 'int' directly
4447 followed by an argument of type 'char *'. This represents a
4448 buffer of given length to be inserted into the resulting
4449 S-expression.
4450 '%S'
4451 The next argument is expected to be of type 'gcry_sexp_t' and
4452 a copy of that S-expression is embedded in the resulting
4453 S-expression. The argument needs to be a regular
4454 S-expression, starting with a parenthesis.
4455
4456 No other format characters are defined and would return an error.
4457 Note that the format character '%%' does not exists, because a
4458 percent sign is not a valid character in an S-expression.
4459
4460 -- Function: void gcry_sexp_release (gcry_sexp_t SEXP)
4461
4462 Release the S-expression object SEXP. If the S-expression is
4463 stored in secure memory, it explicitly zeroises that memory; note
4464 that this is done in addition to the zeroisation always done when
4465 freeing secure memory.
4466
4467 The next 2 functions are used to convert the internal representation
4468 back into a regular external S-expression format and to show the
4469 structure for debugging.
4470
4471 -- Function: size_t gcry_sexp_sprint (gcry_sexp_t SEXP, int MODE,
4472 char *BUFFER, size_t MAXLENGTH)
4473
4474 Copies the S-expression object SEXP into BUFFER using the format
4475 specified in MODE. MAXLENGTH must be set to the allocated length
4476 of BUFFER. The function returns the actual length of valid bytes
4477 put into BUFFER or 0 if the provided buffer is too short. Passing
4478 'NULL' for BUFFER returns the required length for BUFFER. For
4479 convenience reasons an extra byte with value 0 is appended to the
4480 buffer.
4481
4482 The following formats are supported:
4483
4484 'GCRYSEXP_FMT_DEFAULT'
4485 Returns a convenient external S-expression representation.
4486
4487 'GCRYSEXP_FMT_CANON'
4488 Return the S-expression in canonical format.
4489
4490 'GCRYSEXP_FMT_BASE64'
4491 Not currently supported.
4492
4493 'GCRYSEXP_FMT_ADVANCED'
4494 Returns the S-expression in advanced format.
4495
4496 -- Function: void gcry_sexp_dump (gcry_sexp_t SEXP)
4497
4498 Dumps SEXP in a format suitable for debugging to Libgcrypt's
4499 logging stream.
4500
4501 Often canonical encoding is used in the external representation. The
4502 following function can be used to check for valid encoding and to learn
4503 the length of the S-expression.
4504
4505 -- Function: size_t gcry_sexp_canon_len (const unsigned char *BUFFER,
4506 size_t LENGTH, size_t *ERROFF, int *ERRCODE)
4507
4508 Scan the canonical encoded BUFFER with implicit length values and
4509 return the actual length this S-expression uses. For a valid
4510 S-expression it should never return 0. If LENGTH is not 0, the
4511 maximum length to scan is given; this can be used for syntax checks
4512 of data passed from outside. ERRCODE and ERROFF may both be passed
4513 as 'NULL'.
4514
4515 There are functions to parse S-expressions and retrieve elements:
4516
4517 -- Function: gcry_sexp_t gcry_sexp_find_token (const gcry_sexp_t LIST,
4518 const char *TOKEN, size_t TOKLEN)
4519
4520 Scan the S-expression for a sublist with a type (the car of the
4521 list) matching the string TOKEN. If TOKLEN is not 0, the token is
4522 assumed to be raw memory of this length. The function returns a
4523 newly allocated S-expression consisting of the found sublist or
4524 'NULL' when not found.
4525
4526 -- Function: int gcry_sexp_length (const gcry_sexp_t LIST)
4527
4528 Return the length of the LIST. For a valid S-expression this
4529 should be at least 1.
4530
4531 -- Function: gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t LIST,
4532 int NUMBER)
4533
4534 Create and return a new S-expression from the element with index
4535 NUMBER in LIST. Note that the first element has the index 0. If
4536 there is no such element, 'NULL' is returned.
4537
4538 -- Function: gcry_sexp_t gcry_sexp_car (const gcry_sexp_t LIST)
4539
4540 Create and return a new S-expression from the first element in
4541 LIST; this is called the "type" and should always exist per
4542 S-expression specification and in general be a string. 'NULL' is
4543 returned in case of a problem.
4544
4545 -- Function: gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t LIST)
4546
4547 Create and return a new list form all elements except for the first
4548 one. Note that this function may return an invalid S-expression
4549 because it is not guaranteed that the type exists and is a string.
4550 However, for parsing a complex S-expression it might be useful for
4551 intermediate lists. Returns 'NULL' on error.
4552
4553 -- Function: const char * gcry_sexp_nth_data (const gcry_sexp_t LIST,
4554 int NUMBER, size_t *DATALEN)
4555
4556 This function is used to get data from a LIST. A pointer to the
4557 actual data with index NUMBER is returned and the length of this
4558 data will be stored to DATALEN. If there is no data at the given
4559 index or the index represents another list, 'NULL' is returned.
4560 *Caution:* The returned pointer is valid as long as LIST is not
4561 modified or released.
4562
4563 Here is an example on how to extract and print the surname (Meier)
4564 from the S-expression '(Name Otto Meier (address Burgplatz 3))':
4565
4566 size_t len;
4567 const char *name;
4568
4569 name = gcry_sexp_nth_data (list, 2, &len);
4570 printf ("my name is %.*s\n", (int)len, name);
4571
4572 -- Function: void * gcry_sexp_nth_buffer (const gcry_sexp_t LIST,
4573 int NUMBER, size_t *RLENGTH)
4574
4575 This function is used to get data from a LIST. A malloced buffer
4576 with the actual data at list index NUMBER is returned and the
4577 length of this buffer will be stored to RLENGTH. If there is no
4578 data at the given index or the index represents another list,
4579 'NULL' is returned. The caller must release the result using
4580 'gcry_free'.
4581
4582 Here is an example on how to extract and print the CRC value from
4583 the S-expression '(hash crc32 #23ed00d7)':
4584
4585 size_t len;
4586 char *value;
4587
4588 value = gcry_sexp_nth_buffer (list, 2, &len);
4589 if (value)
4590 fwrite (value, len, 1, stdout);
4591 gcry_free (value);
4592
4593 -- Function: char * gcry_sexp_nth_string (gcry_sexp_t LIST, int NUMBER)
4594
4595 This function is used to get and convert data from a LIST. The
4596 data is assumed to be a Nul terminated string. The caller must
4597 release this returned value using 'gcry_free'. If there is no data
4598 at the given index, the index represents a list or the value can't
4599 be converted to a string, 'NULL' is returned.
4600
4601 -- Function: gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t LIST,
4602 int NUMBER, int MPIFMT)
4603
4604 This function is used to get and convert data from a LIST. This
4605 data is assumed to be an MPI stored in the format described by
4606 MPIFMT and returned as a standard Libgcrypt MPI. The caller must
4607 release this returned value using 'gcry_mpi_release'. If there is
4608 no data at the given index, the index represents a list or the
4609 value can't be converted to an MPI, 'NULL' is returned. If you use
4610 this function to parse results of a public key function, you most
4611 likely want to use 'GCRYMPI_FMT_USG'.
4612
4613 -- Function: gpg_error_t gcry_sexp_extract_param ( gcry_sexp_t SEXP,
4614 const char *PATH, const char *LIST, ...)
4615
4616 Extract parameters from an S-expression using a list of parameter
4617 names. The names of these parameters are specified in LIST. White
4618 space between the parameter names are ignored. Some special
4619 characters and character sequences may be given to control the
4620 conversion:
4621
4622 '+'
4623 Switch to unsigned integer format ('GCRYMPI_FMT_USG'). This
4624 is the default mode.
4625 '-'
4626 Switch to standard signed format ('GCRYMPI_FMT_STD').
4627 '/'
4628 Switch to opaque MPI format. The resulting MPIs may not be
4629 used for computations; see 'gcry_mpi_get_opaque' for details.
4630 '&'
4631 Switch to buffer descriptor mode. See below for details.
4632 '%s'
4633 Switch to string mode. The expected argument is the address
4634 of a 'char *' variable; the caller must release that value.
4635 If the parameter was marked optional and is not found, 'NULL'
4636 is stored.
4637 '%#s'
4638 Switch to multi string mode. The expected argument is the
4639 address of a 'char *' variable; the caller must release that
4640 value. If the parameter was marked optional and is not found,
4641 'NULL' is stored. A multi string takes all values, assumes
4642 they are strings and concatenates them using a space as
4643 delimiter. In case a value is actually another list, this is
4644 not further parsed but a '()' is inserted in place of that
4645 sublist.
4646 '%u'
4647 Switch to unsigned integer mode. The expected argument is
4648 address of a 'unsigned int' variable.
4649 '%lu'
4650 Switch to unsigned long integer mode. The expected argument
4651 is address of a 'unsigned long' variable.
4652 '%d'
4653 Switch to signed integer mode. The expected argument is
4654 address of a 'int' variable.
4655 '%ld'
4656 Switch to signed long integer mode. The expected argument is
4657 address of a 'long' variable.
4658 '%zu'
4659 Switch to size_t mode. The expected argument is address of a
4660 'size_t' variable.
4661 '?'
4662 If immediately following a parameter letter (no white space
4663 allowed), that parameter is considered optional.
4664
4665 In general, parameter names are single letters. To use a string
4666 for a parameter name, enclose the name in single quotes.
4667
4668 Unless in buffer descriptor mode, for each parameter name a pointer
4669 to an 'gcry_mpi_t' variable is expected that must be set to 'NULL'
4670 prior to invoking this function, and finally a 'NULL' is expected.
4671 For example
4672
4673 gcry_sexp_extract_param (key, NULL, "n/x+e d-'foo'",
4674 &mpi_n, &mpi_x, &mpi_e, &mpi_d, &mpi_foo, NULL)
4675
4676 stores the parameter 'n' from KEY as an unsigned MPI into MPI_N,
4677 the parameter 'x' as an opaque MPI into MPI_X, the parameters 'e'
4678 and 'd' again as an unsigned MPI into MPI_E and MPI_D and finally
4679 the parameter 'foo' as a signed MPI into MPI_FOO.
4680
4681 PATH is an optional string used to locate a token. The exclamation
4682 mark separated tokens are used via 'gcry_sexp_find_token' to find a
4683 start point inside the S-expression.
4684
4685 In buffer descriptor mode a pointer to a 'gcry_buffer_t' descriptor
4686 is expected instead of a pointer to an MPI. The caller may use two
4687 different operation modes here: If the DATA field of the provided
4688 descriptor is 'NULL', the function allocates a new buffer and
4689 stores it at DATA; the other fields are set accordingly with OFF
4690 set to 0. If DATA is not 'NULL', the function assumes that the
4691 DATA, SIZE, and OFF fields specify a buffer where to put the value
4692 of the respective parameter; on return the LEN field receives the
4693 number of bytes copied to that buffer; in case the buffer is too
4694 small, the function immediately returns with an error code (and LEN
4695 is set to 0).
4696
4697 The function returns 0 on success. On error an error code is
4698 returned, all passed MPIs that might have been allocated up to this
4699 point are deallocated and set to 'NULL', and all passed buffers are
4700 either truncated if the caller supplied the buffer, or deallocated
4701 if the function allocated the buffer.
4702
4703 
4704 File: gcrypt.info, Node: MPI library, Next: Prime numbers, Prev: S-expressions, Up: Top
4705
4706 12 MPI library
4707 **************
4708
4709 * Menu:
4710
4711 * Data types:: MPI related data types.
4712 * Basic functions:: First steps with MPI numbers.
4713 * MPI formats:: External representation of MPIs.
4714 * Calculations:: Performing MPI calculations.
4715 * Comparisons:: How to compare MPI values.
4716 * Bit manipulations:: How to access single bits of MPI values.
4717 * EC functions:: Elliptic curve related functions.
4718 * Miscellaneous:: Miscellaneous MPI functions.
4719
4720 Public key cryptography is based on mathematics with large numbers. To
4721 implement the public key functions, a library for handling these large
4722 numbers is required. Because of the general usefulness of such a
4723 library, its interface is exposed by Libgcrypt. In the context of
4724 Libgcrypt and in most other applications, these large numbers are called
4725 MPIs (multi-precision-integers).
4726
4727 
4728 File: gcrypt.info, Node: Data types, Next: Basic functions, Up: MPI library
4729
4730 12.1 Data types
4731 ===============
4732
4733 -- Data type: gcry_mpi_t
4734 This type represents an object to hold an MPI.
4735
4736 -- Data type: gcry_mpi_point_t
4737 This type represents an object to hold a point for elliptic curve
4738 math.
4739
4740 
4741 File: gcrypt.info, Node: Basic functions, Next: MPI formats, Prev: Data types, Up: MPI library
4742
4743 12.2 Basic functions
4744 ====================
4745
4746 To work with MPIs, storage must be allocated and released for the
4747 numbers. This can be done with one of these functions:
4748
4749 -- Function: gcry_mpi_t gcry_mpi_new (unsigned int NBITS)
4750
4751 Allocate a new MPI object, initialize it to 0 and initially
4752 allocate enough memory for a number of at least NBITS. This
4753 pre-allocation is only a small performance issue and not actually
4754 necessary because Libgcrypt automatically re-allocates the required
4755 memory.
4756
4757 -- Function: gcry_mpi_t gcry_mpi_snew (unsigned int NBITS)
4758
4759 This is identical to 'gcry_mpi_new' but allocates the MPI in the so
4760 called "secure memory" which in turn will take care that all
4761 derived values will also be stored in this "secure memory". Use
4762 this for highly confidential data like private key parameters.
4763
4764 -- Function: gcry_mpi_t gcry_mpi_copy (const gcry_mpi_t A)
4765
4766 Create a new MPI as the exact copy of A but with the constant and
4767 immutable flags cleared.
4768
4769 -- Function: void gcry_mpi_release (gcry_mpi_t A)
4770
4771 Release the MPI A and free all associated resources. Passing
4772 'NULL' is allowed and ignored. When a MPI stored in the "secure
4773 memory" is released, that memory gets wiped out immediately.
4774
4775 The simplest operations are used to assign a new value to an MPI:
4776
4777 -- Function: gcry_mpi_t gcry_mpi_set (gcry_mpi_t W, const gcry_mpi_t U)
4778
4779 Assign the value of U to W and return W. If 'NULL' is passed for
4780 W, a new MPI is allocated, set to the value of U and returned.
4781
4782 -- Function: gcry_mpi_t gcry_mpi_set_ui (gcry_mpi_t W, unsigned long U)
4783
4784 Assign the value of U to W and return W. If 'NULL' is passed for
4785 W, a new MPI is allocated, set to the value of U and returned.
4786 This function takes an 'unsigned int' as type for U and thus it is
4787 only possible to set W to small values (usually up to the word size
4788 of the CPU).
4789
4790 -- Function: gcry_error_t gcry_mpi_get_ui (unsigned int *W,
4791 gcry_mpi_t U)
4792
4793 If U is not negative and small enough to be stored in an 'unsigned
4794 int' variable, store its value at W. If the value does not fit or
4795 is negative, return 'GPG_ERR_ERANGE' and do not change the value
4796 stored at W. Note that this function returns an 'unsigned int' so
4797 that this value can immediately be used with the bit test
4798 functions. This is in contrast to the other "_ui" functions which
4799 allow for values up to an 'unsigned long'.
4800
4801 -- Function: void gcry_mpi_swap (gcry_mpi_t A, gcry_mpi_t B)
4802
4803 Swap the values of A and B.
4804
4805 -- Function: void gcry_mpi_snatch (gcry_mpi_t W, const gcry_mpi_t U)
4806
4807 Set U into W and release U. If W is 'NULL', only U will be
4808 released.
4809
4810 -- Function: void gcry_mpi_neg (gcry_mpi_t W, gcry_mpi_t U)
4811
4812 Set the sign of W to the negative of U.
4813
4814 -- Function: void gcry_mpi_abs (gcry_mpi_t W)
4815
4816 Clear the sign of W.
4817
4818 
4819 File: gcrypt.info, Node: MPI formats, Next: Calculations, Prev: Basic functions, Up: MPI library
4820
4821 12.3 MPI formats
4822 ================
4823
4824 The following functions are used to convert between an external
4825 representation of an MPI and the internal one of Libgcrypt.
4826
4827 -- Function: gcry_error_t gcry_mpi_scan (gcry_mpi_t *R_MPI,
4828 enum gcry_mpi_format FORMAT, const unsigned char *BUFFER,
4829 size_t BUFLEN, size_t *NSCANNED)
4830
4831 Convert the external representation of an integer stored in BUFFER
4832 with a length of BUFLEN into a newly created MPI returned which
4833 will be stored at the address of R_MPI. For certain formats the
4834 length argument is not required and should be passed as '0'. A
4835 BUFLEN larger than 16 MiB will be rejected. After a successful
4836 operation the variable NSCANNED receives the number of bytes
4837 actually scanned unless NSCANNED was given as 'NULL'. FORMAT
4838 describes the format of the MPI as stored in BUFFER:
4839
4840 'GCRYMPI_FMT_STD'
4841 2-complement stored without a length header. Note that
4842 'gcry_mpi_print' stores a '0' as a string of zero length.
4843
4844 'GCRYMPI_FMT_PGP'
4845 As used by OpenPGP (only defined as unsigned). This is
4846 basically 'GCRYMPI_FMT_STD' with a 2 byte big endian length
4847 header. A length header indicating a length of more than
4848 16384 is not allowed.
4849
4850 'GCRYMPI_FMT_SSH'
4851 As used in the Secure Shell protocol. This is
4852 'GCRYMPI_FMT_STD' with a 4 byte big endian header.
4853
4854 'GCRYMPI_FMT_HEX'
4855 Stored as a string with each byte of the MPI encoded as 2 hex
4856 digits. Negative numbers are prefixed with a minus sign and
4857 in addition the high bit is always zero to make clear that an
4858 explicit sign ist used. When using this format, BUFLEN must
4859 be zero.
4860
4861 'GCRYMPI_FMT_USG'
4862 Simple unsigned integer.
4863
4864 Note that all of the above formats store the integer in big-endian
4865 format (MSB first). Leading zeroes are stripped unless they are
4866 required to keep a value positive.
4867
4868 -- Function: gcry_error_t gcry_mpi_print (enum gcry_mpi_format FORMAT,
4869 unsigned char *BUFFER, size_t BUFLEN, size_t *NWRITTEN,
4870 const gcry_mpi_t A)
4871
4872 Convert the MPI A into an external representation described by
4873 FORMAT (see above) and store it in the provided BUFFER which has a
4874 usable length of at least BUFLEN bytes. If NWRITTEN is not 'NULL',
4875 it will receive the number of bytes actually stored in BUFFER after
4876 a successful operation.
4877
4878 -- Function: gcry_error_t gcry_mpi_aprint (enum gcry_mpi_format FORMAT,
4879 unsigned char **BUFFER, size_t *NBYTES, const gcry_mpi_t A)
4880
4881 Convert the MPI A into an external representation described by
4882 FORMAT (see above) and store it in a newly allocated buffer which
4883 address will be stored in the variable BUFFER points to. The
4884 number of bytes stored in this buffer will be stored in the
4885 variable NBYTES points to, unless NBYTES is 'NULL'.
4886
4887 Even if NBYTES is zero, the function allocates at least one byte
4888 and store a zero there. Thus with formats 'GCRYMPI_FMT_STD' and
4889 'GCRYMPI_FMT_USG' the caller may safely set a returned length of 0
4890 to 1 to represent a zero as a 1 byte string.
4891
4892 -- Function: void gcry_mpi_dump (const gcry_mpi_t A)
4893
4894 Dump the value of A in a format suitable for debugging to
4895 Libgcrypt's logging stream. Note that one leading space but no
4896 trailing space or linefeed will be printed. It is okay to pass
4897 'NULL' for A.
4898
4899 
4900 File: gcrypt.info, Node: Calculations, Next: Comparisons, Prev: MPI formats, Up: MPI library
4901
4902 12.4 Calculations
4903 =================
4904
4905 Basic arithmetic operations:
4906
4907 -- Function: void gcry_mpi_add (gcry_mpi_t W, gcry_mpi_t U,
4908 gcry_mpi_t V)
4909
4910 W = U + V.
4911
4912 -- Function: void gcry_mpi_add_ui (gcry_mpi_t W, gcry_mpi_t U,
4913 unsigned long V)
4914
4915 W = U + V. Note that V is an unsigned integer.
4916
4917 -- Function: void gcry_mpi_addm (gcry_mpi_t W, gcry_mpi_t U,
4918 gcry_mpi_t V, gcry_mpi_t M)
4919
4920 W = U + V \bmod M.
4921
4922 -- Function: void gcry_mpi_sub (gcry_mpi_t W, gcry_mpi_t U,
4923 gcry_mpi_t V)
4924
4925 W = U - V.
4926
4927 -- Function: void gcry_mpi_sub_ui (gcry_mpi_t W, gcry_mpi_t U,
4928 unsigned long V)
4929
4930 W = U - V. V is an unsigned integer.
4931
4932 -- Function: void gcry_mpi_subm (gcry_mpi_t W, gcry_mpi_t U,
4933 gcry_mpi_t V, gcry_mpi_t M)
4934
4935 W = U - V \bmod M.
4936
4937 -- Function: void gcry_mpi_mul (gcry_mpi_t W, gcry_mpi_t U,
4938 gcry_mpi_t V)
4939
4940 W = U * V.
4941
4942 -- Function: void gcry_mpi_mul_ui (gcry_mpi_t W, gcry_mpi_t U,
4943 unsigned long V)
4944
4945 W = U * V. V is an unsigned integer.
4946
4947 -- Function: void gcry_mpi_mulm (gcry_mpi_t W, gcry_mpi_t U,
4948 gcry_mpi_t V, gcry_mpi_t M)
4949
4950 W = U * V \bmod M.
4951
4952 -- Function: void gcry_mpi_mul_2exp (gcry_mpi_t W, gcry_mpi_t U,
4953 unsigned long E)
4954
4955 W = U * 2^e.
4956
4957 -- Function: void gcry_mpi_div (gcry_mpi_t Q, gcry_mpi_t R,
4958 gcry_mpi_t DIVIDEND, gcry_mpi_t DIVISOR, int ROUND)
4959
4960 Q = DIVIDEND / DIVISOR, R = DIVIDEND \bmod DIVISOR. Q and R may be
4961 passed as 'NULL'. ROUND is either negative for floored division
4962 (rounds towards the next lower integer) or zero for truncated
4963 division (rounds towards zero).
4964
4965 -- Function: void gcry_mpi_mod (gcry_mpi_t R, gcry_mpi_t DIVIDEND,
4966 gcry_mpi_t DIVISOR)
4967
4968 R = DIVIDEND \bmod DIVISOR.
4969
4970 -- Function: void gcry_mpi_powm (gcry_mpi_t W, const gcry_mpi_t B,
4971 const gcry_mpi_t E, const gcry_mpi_t M)
4972
4973 W = B^e \bmod M.
4974
4975 -- Function: int gcry_mpi_gcd (gcry_mpi_t G, gcry_mpi_t A,
4976 gcry_mpi_t B)
4977
4978 Set G to the greatest common divisor of A and B. Return true if G
4979 is 1.
4980
4981 -- Function: int gcry_mpi_invm (gcry_mpi_t X, gcry_mpi_t A,
4982 gcry_mpi_t M)
4983
4984 Set X to the multiplicative inverse of A \bmod M. Return true if
4985 the inverse exists.
4986
4987 
4988 File: gcrypt.info, Node: Comparisons, Next: Bit manipulations, Prev: Calculations, Up: MPI library
4989
4990 12.5 Comparisons
4991 ================
4992
4993 The next 2 functions are used to compare MPIs:
4994
4995 -- Function: int gcry_mpi_cmp (const gcry_mpi_t U, const gcry_mpi_t V)
4996
4997 Compare the multi-precision-integers number U and V, returning 0
4998 for equality, a positive value for U > V and a negative for U < V.
4999 If both numbers are opaque values (cf. 'gcry_mpi_set_opaque'), the
5000 comparison is done by checking the bit sizes using memcmp. If only
5001 one number is an opaque value, the opaque value is less than the
5002 other number.
5003
5004 -- Function: int gcry_mpi_cmp_ui (const gcry_mpi_t U, unsigned long V)
5005
5006 Compare the multi-precision-integers number U with the unsigned
5007 integer V, returning 0 for equality, a positive value for U > V and
5008 a negative for U < V.
5009
5010 -- Function: int gcry_mpi_is_neg (const gcry_mpi_t A)
5011
5012 Return 1 if A is less than zero; return 0 if zero or positive.
5013
5014 
5015 File: gcrypt.info, Node: Bit manipulations, Next: EC functions, Prev: Comparisons, Up: MPI library
5016
5017 12.6 Bit manipulations
5018 ======================
5019
5020 There are a couple of functions to get information on arbitrary bits in
5021 an MPI and to set or clear them:
5022
5023 -- Function: unsigned int gcry_mpi_get_nbits (gcry_mpi_t A)
5024
5025 Return the number of bits required to represent A.
5026
5027 -- Function: int gcry_mpi_test_bit (gcry_mpi_t A, unsigned int N)
5028
5029 Return true if bit number N (counting from 0) is set in A.
5030
5031 -- Function: void gcry_mpi_set_bit (gcry_mpi_t A, unsigned int N)
5032
5033 Set bit number N in A.
5034
5035 -- Function: void gcry_mpi_clear_bit (gcry_mpi_t A, unsigned int N)
5036
5037 Clear bit number N in A.
5038
5039 -- Function: void gcry_mpi_set_highbit (gcry_mpi_t A, unsigned int N)
5040
5041 Set bit number N in A and clear all bits greater than N.
5042
5043 -- Function: void gcry_mpi_clear_highbit (gcry_mpi_t A, unsigned int N)
5044
5045 Clear bit number N in A and all bits greater than N.
5046
5047 -- Function: void gcry_mpi_rshift (gcry_mpi_t X, gcry_mpi_t A,
5048 unsigned int N)
5049
5050 Shift the value of A by N bits to the right and store the result in
5051 X.
5052
5053 -- Function: void gcry_mpi_lshift (gcry_mpi_t X, gcry_mpi_t A,
5054 unsigned int N)
5055
5056 Shift the value of A by N bits to the left and store the result in
5057 X.
5058
5059 
5060 File: gcrypt.info, Node: EC functions, Next: Miscellaneous, Prev: Bit manipulations, Up: MPI library
5061
5062 12.7 EC functions
5063 =================
5064
5065 Libgcrypt provides an API to access low level functions used by its
5066 elliptic curve implementation. These functions allow implementing
5067 elliptic curve methods for which no explicit support is available.
5068
5069 -- Function: gcry_mpi_point_t gcry_mpi_point_new (unsigned int NBITS)
5070
5071 Allocate a new point object, initialize it to 0, and allocate
5072 enough memory for a points of at least NBITS. This pre-allocation
5073 yields only a small performance win and is not really necessary
5074 because Libgcrypt automatically re-allocates the required memory.
5075 Using 0 for NBITS is usually the right thing to do.
5076
5077 -- Function: void gcry_mpi_point_release (gcry_mpi_point_t POINT)
5078
5079 Release POINT and free all associated resources. Passing 'NULL' is
5080 allowed and ignored.
5081
5082 -- Function: gcry_mpi_point_t gcry_mpi_point_copy
5083 (gcry_mpi_point_t POINT)
5084
5085 Allocate and return a new point object and initialize it with
5086 POINT. If POINT is 'NULL', the function is identical to
5087 'gcry_mpi_point_new(0)'.
5088
5089 -- Function: void gcry_mpi_point_get (gcry_mpi_t X, gcry_mpi_t Y,
5090 gcry_mpi_t Z, gcry_mpi_point_t POINT)
5091
5092 Store the projective coordinates from POINT into the MPIs X, Y, and
5093 Z. If a coordinate is not required, 'NULL' may be used for X, Y,
5094 or Z.
5095
5096 -- Function: void gcry_mpi_point_snatch_get (gcry_mpi_t X,
5097 gcry_mpi_t Y, gcry_mpi_t Z, gcry_mpi_point_t POINT)
5098
5099 Store the projective coordinates from POINT into the MPIs X, Y, and
5100 Z. If a coordinate is not required, 'NULL' may be used for X, Y,
5101 or Z. The object POINT is then released. Using this function
5102 instead of 'gcry_mpi_point_get' and 'gcry_mpi_point_release' has
5103 the advantage of avoiding some extra memory allocations and copies.
5104
5105 -- Function: gcry_mpi_point_t gcry_mpi_point_set (
5106 gcry_mpi_point_t POINT, gcry_mpi_t X, gcry_mpi_t Y,
5107 gcry_mpi_t Z)
5108
5109 Store the projective coordinates from X, Y, and Z into POINT. If a
5110 coordinate is given as 'NULL', the value 0 is used. If 'NULL' is
5111 used for POINT, a new point object is allocated and returned.
5112 Returns POINT or the newly allocated point object.
5113
5114 -- Function: gcry_mpi_point_t gcry_mpi_point_snatch_set (
5115 gcry_mpi_point_t POINT, gcry_mpi_t X, gcry_mpi_t Y,
5116 gcry_mpi_t Z)
5117
5118 Store the projective coordinates from X, Y, and Z into POINT. If a
5119 coordinate is given as 'NULL', the value 0 is used. If 'NULL' is
5120 used for POINT, a new point object is allocated and returned. The
5121 MPIs X, Y, and Z are released. Using this function instead of
5122 'gcry_mpi_point_set' and 3 calls to 'gcry_mpi_release' has the
5123 advantage of avoiding some extra memory allocations and copies.
5124 Returns POINT or the newly allocated point object.
5125
5126 -- Function: gpg_error_t gcry_mpi_ec_new (gcry_ctx_t *R_CTX,
5127 gcry_sexp_t KEYPARAM, const char *CURVENAME)
5128
5129 Allocate a new context for elliptic curve operations. If KEYPARAM
5130 is given, it specifies the parameters of the curve (*note
5131 ecc_keyparam::). If CURVENAME is given in addition to KEYPARAM and
5132 the key parameters do not include a named curve reference, the
5133 string CURVENAME is used to fill in missing parameters. If only
5134 CURVENAME is given, the context is initialized for this named
5135 curve.
5136
5137 If a parameter specifying a point (e.g., 'g' or 'q') is not found,
5138 the parser looks for a non-encoded point by appending '.x', '.y',
5139 and '.z' to the parameter name and looking them all up to create a
5140 point. A parameter with the suffix '.z' is optional and defaults
5141 to 1.
5142
5143 On success the function returns 0 and stores the new context object
5144 at R_CTX; this object eventually needs to be released (*note
5145 gcry_ctx_release::). On error the function stores 'NULL' at R_CTX
5146 and returns an error code.
5147
5148 -- Function: gcry_mpi_t gcry_mpi_ec_get_mpi ( const char *NAME,
5149 gcry_ctx_t CTX, int COPY)
5150
5151 Return the MPI with NAME from the context CTX. If not found,
5152 'NULL' is returned. If the returned MPI may later be modified, it
5153 is suggested to pass '1' to COPY, so that the function guarantees
5154 that a modifiable copy of the MPI is returned. If '0' is used for
5155 COPY, this function may return a constant flagged MPI. In any case
5156 'gcry_mpi_release' needs to be called to release the result. For
5157 valid names, see *note ecc_keyparam::. If the public key 'q' is
5158 requested but only the private key 'd' is available, 'q' will be
5159 recomputed on the fly. If a point parameter is requested, it is
5160 returned as an uncompressed encoded point unless these special
5161 names are used:
5162 Q@EDDSA
5163 Return an EdDSA style compressed point. This is only
5164 supported for Twisted Edwards curves.
5165
5166 -- Function: gcry_mpi_point_t gcry_mpi_ec_get_point ( const char *NAME,
5167 gcry_ctx_t CTX, int COPY)
5168
5169 Return the point with NAME from the context CTX. If not found,
5170 'NULL' is returned. If the returned MPI may later be modified, it
5171 is suggested to pass '1' to COPY, so that the function guarantees
5172 that a modifiable copy of the MPI is returned. If '0' is used for
5173 COPY, this function may return a constant flagged point. In any
5174 case 'gcry_mpi_point_release' needs to be called to release the
5175 result. If the public key 'q' is requested but only the private
5176 key 'd' is available, 'q' will be recomputed on the fly.
5177
5178 -- Function: gpg_error_t gcry_mpi_ec_set_mpi ( const char *NAME,
5179 gcry_mpi_t NEWVALUE, gcry_ctx_t CTX)
5180
5181 Store the MPI NEWVALUE at NAME into the context CTX. On success
5182 '0' is returned; on error an error code. Valid names are the MPI
5183 parameters of an elliptic curve (*note ecc_keyparam::).
5184
5185 -- Function: gpg_error_t gcry_mpi_ec_set_point ( const char *NAME,
5186 gcry_mpi_point_t NEWVALUE, gcry_ctx_t CTX)
5187
5188 Store the point NEWVALUE at NAME into the context CTX. On success
5189 '0' is returned; on error an error code. Valid names are the point
5190 parameters of an elliptic curve (*note ecc_keyparam::).
5191
5192 -- Function: gpg_err_code_t gcry_mpi_ec_decode_point (
5193 mpi_point_t RESULT, gcry_mpi_t VALUE, gcry_ctx_t CTX)
5194
5195 Decode the point given as an MPI in VALUE and store at RESULT. To
5196 decide which encoding is used the function takes a context CTX
5197 which can be created with 'gcry_mpi_ec_new'. If 'NULL' is given
5198 for the context, the function assumes a 0x04 prefixed uncompressed
5199 encoding. On error an error code is returned and RESULT might be
5200 changed.
5201
5202 -- Function: int gcry_mpi_ec_get_affine ( gcry_mpi_t X, gcry_mpi_t Y,
5203 gcry_mpi_point_t POINT, gcry_ctx_t CTX)
5204
5205 Compute the affine coordinates from the projective coordinates in
5206 POINT and store them into X and Y. If one coordinate is not
5207 required, 'NULL' may be passed to X or Y. CTX is the context
5208 object which has been created using 'gcry_mpi_ec_new'. Returns 0
5209 on success or not 0 if POINT is at infinity.
5210
5211 Note that you can use 'gcry_mpi_ec_set_point' with the value
5212 'GCRYMPI_CONST_ONE' for Z to convert affine coordinates back into
5213 projective coordinates.
5214
5215 -- Function: void gcry_mpi_ec_dup ( gcry_mpi_point_t W,
5216 gcry_mpi_point_t U, gcry_ctx_t CTX)
5217
5218 Double the point U of the elliptic curve described by CTX and store
5219 the result into W.
5220
5221 -- Function: void gcry_mpi_ec_add ( gcry_mpi_point_t W,
5222 gcry_mpi_point_t U, gcry_mpi_point_t V, gcry_ctx_t CTX)
5223
5224 Add the points U and V of the elliptic curve described by CTX and
5225 store the result into W.
5226
5227 -- Function: void gcry_mpi_ec_sub ( gcry_mpi_point_t W,
5228 gcry_mpi_point_t U, gcry_mpi_point_t V, gcry_ctx_t CTX)
5229
5230 Subtracts the point V from the point U of the elliptic curve
5231 described by CTX and store the result into W. Only Twisted Edwards
5232 curves are supported for now.
5233
5234 -- Function: void gcry_mpi_ec_mul ( gcry_mpi_point_t W, gcry_mpi_t N,
5235 gcry_mpi_point_t U, gcry_ctx_t CTX)
5236
5237 Multiply the point U of the elliptic curve described by CTX by N
5238 and store the result into W.
5239
5240 -- Function: int gcry_mpi_ec_curve_point ( gcry_mpi_point_t POINT,
5241 gcry_ctx_t CTX)
5242
5243 Return true if POINT is on the elliptic curve described by CTX.
5244
5245 
5246 File: gcrypt.info, Node: Miscellaneous, Prev: EC functions, Up: MPI library
5247
5248 12.8 Miscellaneous
5249 ==================
5250
5251 An MPI data type is allowed to be "misused" to store an arbitrary value.
5252 Two functions implement this kludge:
5253
5254 -- Function: gcry_mpi_t gcry_mpi_set_opaque (gcry_mpi_t A, void *P,
5255 unsigned int NBITS)
5256
5257 Store NBITS of the value P points to in A and mark A as an opaque
5258 value (i.e., an value that can't be used for any math calculation
5259 and is only used to store an arbitrary bit pattern in A).
5260 Ownership of P is taken by this function and thus the user may not
5261 dereference the passed value anymore. It is required that the
5262 memory referenced by P has been allocated in a way that 'gcry_free'
5263 is able to release it.
5264
5265 WARNING: Never use an opaque MPI for actual math operations. The
5266 only valid functions are 'gcry_mpi_get_opaque' and
5267 'gcry_mpi_release'. Use 'gcry_mpi_scan' to convert a string of
5268 arbitrary bytes into an MPI.
5269
5270 -- Function: gcry_mpi_t gcry_mpi_set_opaque_copy (gcry_mpi_t A,
5271 const void *P, unsigned int NBITS)
5272
5273 Same as 'gcry_mpi_set_opaque' but ownership of P is not taken;
5274 instead a copy of P is used.
5275
5276 -- Function: void * gcry_mpi_get_opaque (gcry_mpi_t A,
5277 unsigned int *NBITS)
5278
5279 Return a pointer to an opaque value stored in A and return its size
5280 in NBITS. Note that the returned pointer is still owned by A and
5281 that the function should never be used for an non-opaque MPI.
5282
5283 Each MPI has an associated set of flags for special purposes. The
5284 currently defined flags are:
5285
5286 'GCRYMPI_FLAG_SECURE'
5287 Setting this flag converts A into an MPI stored in "secure memory".
5288 Clearing this flag is not allowed.
5289 'GCRYMPI_FLAG_OPAQUE'
5290 This is an internal flag, indicating that an opaque value and not
5291 an integer is stored. This is an read-only flag; it may not be set
5292 or cleared.
5293 'GCRYMPI_FLAG_IMMUTABLE'
5294 If this flag is set, the MPI is marked as immutable. Setting or
5295 changing the value of that MPI is ignored and an error message is
5296 logged. The flag is sometimes useful for debugging.
5297 'GCRYMPI_FLAG_CONST'
5298 If this flag is set, the MPI is marked as a constant and as
5299 immutable Setting or changing the value of that MPI is ignored and
5300 an error message is logged. Such an MPI will never be deallocated
5301 and may thus be used without copying. Note that using
5302 'gcry_mpi_copy' will return a copy of that constant with this and
5303 the immutable flag cleared. A few commonly used constants are
5304 pre-defined and accessible using the macros 'GCRYMPI_CONST_ONE',
5305 'GCRYMPI_CONST_TWO', 'GCRYMPI_CONST_THREE', 'GCRYMPI_CONST_FOUR',
5306 and 'GCRYMPI_CONST_EIGHT'.
5307 'GCRYMPI_FLAG_USER1'
5308 'GCRYMPI_FLAG_USER2'
5309 'GCRYMPI_FLAG_USER3'
5310 'GCRYMPI_FLAG_USER4'
5311 These flags are reserved for use by the application.
5312
5313 -- Function: void gcry_mpi_set_flag (gcry_mpi_t A,
5314 enum gcry_mpi_flag FLAG)
5315
5316 Set the FLAG for the MPI A. The only allowed flags are
5317 'GCRYMPI_FLAG_SECURE', 'GCRYMPI_FLAG_IMMUTABLE', and
5318 'GCRYMPI_FLAG_CONST'.
5319
5320 -- Function: void gcry_mpi_clear_flag (gcry_mpi_t A,
5321 enum gcry_mpi_flag FLAG)
5322
5323 Clear FLAG for the multi-precision-integers A. The only allowed
5324 flag is 'GCRYMPI_FLAG_IMMUTABLE' but only if 'GCRYMPI_FLAG_CONST'
5325 is not set. If 'GCRYMPI_FLAG_CONST' is set, clearing
5326 'GCRYMPI_FLAG_IMMUTABLE' will simply be ignored.
5327
5328 -- Function: int gcry_mpi_get_flag (gcry_mpi_t A,
5329 enum gcry_mpi_flag FLAG)
5330
5331 Return true if FLAG is set for A.
5332
5333 To put a random value into an MPI, the following convenience function
5334 may be used:
5335
5336 -- Function: void gcry_mpi_randomize (gcry_mpi_t W, unsigned int NBITS,
5337 enum gcry_random_level LEVEL)
5338
5339 Set the multi-precision-integers W to a random non-negative number
5340 of NBITS, using random data quality of level LEVEL. In case NBITS
5341 is not a multiple of a byte, NBITS is rounded up to the next byte
5342 boundary. When using a LEVEL of 'GCRY_WEAK_RANDOM', this function
5343 makes use of 'gcry_create_nonce'.
5344
5345 
5346 File: gcrypt.info, Node: Prime numbers, Next: Utilities, Prev: MPI library, Up: Top
5347
5348 13 Prime numbers
5349 ****************
5350
5351 * Menu:
5352
5353 * Generation:: Generation of new prime numbers.
5354 * Checking:: Checking if a given number is prime.
5355
5356 
5357 File: gcrypt.info, Node: Generation, Next: Checking, Up: Prime numbers
5358
5359 13.1 Generation
5360 ===============
5361
5362 -- Function: gcry_error_t gcry_prime_generate (gcry_mpi_t
5363 *PRIME,unsigned int PRIME_BITS, unsigned int FACTOR_BITS,
5364 gcry_mpi_t **FACTORS, gcry_prime_check_func_t CB_FUNC, void
5365 *CB_ARG, gcry_random_level_t RANDOM_LEVEL, unsigned int FLAGS)
5366
5367 Generate a new prime number of PRIME_BITS bits and store it in
5368 PRIME. If FACTOR_BITS is non-zero, one of the prime factors of
5369 (PRIME - 1) / 2 must be FACTOR_BITS bits long. If FACTORS is
5370 non-zero, allocate a new, 'NULL'-terminated array holding the prime
5371 factors and store it in FACTORS. FLAGS might be used to influence
5372 the prime number generation process.
5373
5374 -- Function: gcry_error_t gcry_prime_group_generator (gcry_mpi_t *R_G,
5375 gcry_mpi_t PRIME, gcry_mpi_t *FACTORS, gcry_mpi_t START_G)
5376
5377 Find a generator for PRIME where the factorization of (PRIME - 1)
5378 is in the 'NULL' terminated array FACTORS. Return the generator as
5379 a newly allocated MPI in R_G. If START_G is not 'NULL', use this
5380 as the start for the search.
5381
5382 -- Function: void gcry_prime_release_factors (gcry_mpi_t *FACTORS)
5383
5384 Convenience function to release the FACTORS array.
5385
5386 
5387 File: gcrypt.info, Node: Checking, Prev: Generation, Up: Prime numbers
5388
5389 13.2 Checking
5390 =============
5391
5392 -- Function: gcry_error_t gcry_prime_check (gcry_mpi_t P, unsigned int
5393 FLAGS)
5394
5395 Check whether the number P is prime. Returns zero in case P is
5396 indeed a prime, returns 'GPG_ERR_NO_PRIME' in case P is not a prime
5397 and a different error code in case something went horribly wrong.
5398
5399 
5400 File: gcrypt.info, Node: Utilities, Next: Tools, Prev: Prime numbers, Up: Top
5401
5402 14 Utilities
5403 ************
5404
5405 * Menu:
5406
5407 * Memory allocation:: Functions related to memory allocation.
5408 * Context management:: Functions related to context management.
5409 * Buffer description:: A data type to describe buffers.
5410 * Config reporting:: How to check Libgcrypt's configuration.
5411
5412 
5413 File: gcrypt.info, Node: Memory allocation, Next: Context management, Up: Utilities
5414
5415 14.1 Memory allocation
5416 ======================
5417
5418 -- Function: void * gcry_malloc (size_t N)
5419
5420 This function tries to allocate N bytes of memory. On success it
5421 returns a pointer to the memory area, in an out-of-core condition,
5422 it returns 'NULL'.
5423
5424 -- Function: void * gcry_malloc_secure (size_t N)
5425 Like 'gcry_malloc', but uses secure memory.
5426
5427 -- Function: void * gcry_calloc (size_t N, size_t M)
5428
5429 This function allocates a cleared block of memory (i.e.,
5430 initialized with zero bytes) long enough to contain a vector of N
5431 elements, each of size M bytes. On success it returns a pointer to
5432 the memory block; in an out-of-core condition, it returns 'NULL'.
5433
5434 -- Function: void * gcry_calloc_secure (size_t N, size_t M)
5435 Like 'gcry_calloc', but uses secure memory.
5436
5437 -- Function: void * gcry_realloc (void *P, size_t N)
5438
5439 This function tries to resize the memory area pointed to by P to N
5440 bytes. On success it returns a pointer to the new memory area, in
5441 an out-of-core condition, it returns 'NULL'. Depending on whether
5442 the memory pointed to by P is secure memory or not, 'gcry_realloc'
5443 tries to use secure memory as well.
5444
5445 -- Function: void gcry_free (void *P)
5446 Release the memory area pointed to by P.
5447
5448 
5449 File: gcrypt.info, Node: Context management, Next: Buffer description, Prev: Memory allocation, Up: Utilities
5450
5451 14.2 Context management
5452 =======================
5453
5454 Some function make use of a context object. As of now, there are only a
5455 few math functions. However, future versions of Libgcrypt may make more
5456 use of this context object.
5457
5458 -- Data type: gcry_ctx_t
5459 This type is used to refer to the general purpose context object.
5460
5461 -- Function: void gcry_ctx_release (gcry_ctx_t CTX)
5462 Release the context object CTX and all associated resources. A
5463 'NULL' passed as CTX is ignored.
5464
5465 
5466 File: gcrypt.info, Node: Buffer description, Next: Config reporting, Prev: Context management, Up: Utilities
5467
5468 14.3 Buffer description
5469 =======================
5470
5471 To help hashing non-contiguous areas of memory, a general purpose data
5472 type is defined:
5473
5474 -- Data type: gcry_buffer_t
5475 This type is a structure to describe a buffer. The user should
5476 make sure that this structure is initialized to zero. The
5477 available fields of this structure are:
5478
5479 '.size'
5480 This is either 0 for no information available or indicates the
5481 allocated length of the buffer.
5482 '.off'
5483 This is the offset into the buffer.
5484 '.len'
5485 This is the valid length of the buffer starting at '.off'.
5486 '.data'
5487 This is the address of the buffer.
5488
5489 
5490 File: gcrypt.info, Node: Config reporting, Prev: Buffer description, Up: Utilities
5491
5492 14.4 How to check Libgcrypt's configuration.
5493 ============================================
5494
5495 Although 'GCRYCTL_PRINT_CONFIG' can be used to print configuration
5496 options, it is sometimes necessary to check them in a program. This can
5497 be accomplished by using this function:
5498
5499 -- Function: char * gcry_get_config (int MODE, const char *WHAT)
5500
5501 This function returns a malloced string with colon delimited
5502 configure options. With a value of 0 for MODE this string
5503 resembles the output of 'GCRYCTL_PRINT_CONFIG'. However, if WHAT
5504 is not 'NULL', only the line where the first field (e.g.,
5505 "cpu-arch") matches WHAT is returned.
5506
5507 Other values than 0 for MODE are not defined. The caller shall
5508 free the string using 'gcry_free'. On error 'NULL' is returned and
5509 'ERRNO' is set; if a value for WHAT is unknown, 'ERRNO' will be set
5510 to 0.
5511
5512 
5513 File: gcrypt.info, Node: Tools, Next: Configuration, Prev: Utilities, Up: Top
5514
5515 15 Tools
5516 ********
5517
5518 * Menu:
5519
5520 * hmac256:: A standalone HMAC-SHA-256 implementation
5521
5522 
5523 File: gcrypt.info, Node: hmac256, Up: Tools
5524
5525 15.1 A HMAC-SHA-256 tool
5526 ========================
5527
5528 This is a standalone HMAC-SHA-256 implementation used to compute an
5529 HMAC-SHA-256 message authentication code. The tool has originally been
5530 developed as a second implementation for Libgcrypt to allow comparing
5531 against the primary implementation and to be used for internal
5532 consistency checks. It should not be used for sensitive data because no
5533 mechanisms to clear the stack etc are used.
5534
5535 The code has been written in a highly portable manner and requires
5536 only a few standard definitions to be provided in a config.h file.
5537
5538 'hmac256' is commonly invoked as
5539
5540 hmac256 "This is my key" foo.txt
5541
5542 This compute the MAC on the file 'foo.txt' using the key given on the
5543 command line.
5544
5545 'hmac256' understands these options:
5546
5547 '--binary'
5548 Print the MAC as a binary string. The default is to print the MAC
5549 encoded as lower case hex digits.
5550
5551 '--version'
5552 Print version of the program and exit.
5553
5554 
5555 File: gcrypt.info, Node: Configuration, Next: Architecture, Prev: Tools, Up: Top
5556
5557 16 Configuration files and environment variables
5558 ************************************************
5559
5560 This chapter describes which files and environment variables can be used
5561 to change the behaviour of Libgcrypt.
5562
5563 The environment variables considered by Libgcrypt are:
5564
5565 'LIBGCRYPT_FORCE_FIPS_MODE'
5566 By setting this variable to any value, Libgcrypt is put into FIPS
5567 mode at initialization time (*note enabling fips mode::).
5568
5569 'GCRYPT_BARRETT'
5570 By setting this variable to any value a different algorithm for
5571 modular reduction is used for ECC.
5572
5573 'GCRYPT_RNDUNIX_DBG'
5574 'GCRYPT_RNDUNIX_DBGALL'
5575 These two environment variables are used to enable debug output for
5576 the rndunix entropy gatherer, which is used on systems lacking a
5577 /dev/random device. The value of 'GCRYPT_RNDUNIX_DBG' is a file
5578 name or '-' for stdout. Debug output is the written to this file.
5579 Setting 'GCRYPT_RNDUNIX_DBGALL' to any value will make the debug
5580 output more verbose.
5581
5582 'GCRYPT_RNDW32_NOPERF'
5583 Setting this environment variable on Windows to any value disables
5584 the use of performance data ('HKEY_PERFORMANCE_DATA') as source for
5585 entropy. On some older Windows systems this could help to speed up
5586 the creation of random numbers but also decreases the amount of
5587 data used to init the random number generator.
5588
5589 'GCRYPT_RNDW32_DBG'
5590 Setting the value of this variable to a positive integer logs
5591 information about the Windows entropy gatherer using the standard
5592 log interface.
5593
5594 'HOME'
5595 This is used to locate the socket to connect to the EGD random
5596 daemon. The EGD can be used on system without a /dev/random to
5597 speed up the random number generator. It is not needed on the
5598 majority of today's operating systems, and support for EGD requires
5599 the use of a configure option at build time.
5600
5601 The files which Libgcrypt uses to retrieve system information and the
5602 files which can be created by the user to modify Libgcrypt's behavior
5603 are:
5604
5605 '/etc/gcrypt/hwf.deny'
5606 This file can be used to disable the use of hardware based
5607 optimizations, *note hardware features::.
5608
5609 '/etc/gcrypt/random.conf'
5610 This file can be used to globally change parameters of the random
5611 generator. The file is a simple text file where empty lines and
5612 lines with the first non white-space character being '#' are
5613 ignored. Supported options are
5614
5615 'disable-jent'
5616 Disable the use of the jitter based entropy generator.
5617
5618 'only-urandom'
5619 Always use the non-blocking /dev/urandom or the respective
5620 system call instead of the blocking /dev/random. If Libgcrypt
5621 is used early in the boot process of the system, this option
5622 should only be used if the system also supports the getrandom
5623 system call.
5624
5625 '/etc/gcrypt/fips_enabled'
5626 '/proc/sys/crypto/fips_enabled'
5627 On Linux these files are used to enable FIPS mode, *note enabling
5628 fips mode::.
5629
5630 '/proc/cpuinfo'
5631 '/proc/self/auxv'
5632 On Linux running on the ARM architecture, these files are used to
5633 read hardware capabilities of the CPU.
5634
5635 
5636 File: gcrypt.info, Node: Architecture, Next: Self-Tests, Prev: Configuration, Up: Top
5637
5638 17 Architecture
5639 ***************
5640
5641 This chapter describes the internal architecture of Libgcrypt.
5642
5643 Libgcrypt is a function library written in ISO C-90. Any compliant
5644 compiler should be able to build Libgcrypt as long as the target is
5645 either a POSIX platform or compatible to the API used by Windows NT.
5646 Provisions have been taken so that the library can be directly used from
5647 C++ applications; however building with a C++ compiler is not supported.
5648
5649 Building Libgcrypt is done by using the common './configure && make'
5650 approach. The configure command is included in the source distribution,
5651 and as a portable shell script it works on any Unix-alike system. The
5652 result of running the configure script are a C header file ('config.h'),
5653 customized Makefiles, the setup of symbolic links and a few other
5654 things. After that the make tool builds and optionally installs the
5655 library and the documentation. See the files 'INSTALL' and 'README' in
5656 the source distribution on how to do this.
5657
5658 Libgcrypt is developed using a Subversion(1) repository. Although
5659 all released versions are tagged in this repository, they should not be
5660 used to build production versions of Libgcrypt. Instead released
5661 tarballs should be used. These tarballs are available from several
5662 places with the master copy at 'ftp://ftp.gnupg.org/gcrypt/libgcrypt/'.
5663 Announcements of new releases are posted to the
5664 '[email protected]' mailing list(2).
5665
5666
5667 [image src="libgcrypt-modules.png" alt="Libgcrypt subsystems"]
5668
5669 Figure 17.1: Libgcrypt subsystems
5670
5671 Libgcrypt consists of several subsystems (*note Figure 17.1:
5672 fig:subsystems.) and all these subsystems provide a public API; this
5673 includes the helper subsystems like the one for S-expressions. The API
5674 style depends on the subsystem; in general an open-use-close approach is
5675 implemented. The open returns a handle to a context used for all
5676 further operations on this handle, several functions may then be used on
5677 this handle, and a final close function releases all resources
5678 associated with the handle.
5679
5680 * Menu:
5681
5682 * Public-Key Subsystem Architecture:: About public keys.
5683 * Symmetric Encryption Subsystem Architecture:: About standard ciphers.
5684 * Hashing and MACing Subsystem Architecture:: About hashing.
5685 * Multi-Precision-Integer Subsystem Architecture:: About big integers.
5686 * Prime-Number-Generator Subsystem Architecture:: About prime numbers.
5687 * Random-Number Subsystem Architecture:: About random stuff.
5688
5689 ---------- Footnotes ----------
5690
5691 (1) A version control system available for many platforms
5692
5693 (2) See <http://www.gnupg.org/documentation/mailing-lists.en.html>
5694 for details.
5695
5696 
5697 File: gcrypt.info, Node: Public-Key Subsystem Architecture, Next: Symmetric Encryption Subsystem Architecture, Up: Architecture
5698
5699 17.1 Public-Key Architecture
5700 ============================
5701
5702 Because public key cryptography is almost always used to process small
5703 amounts of data (hash values or session keys), the interface is not
5704 implemented using the open-use-close paradigm, but with single
5705 self-contained functions. Due to the wide variety of parameters
5706 required by different algorithms, S-expressions - as flexible way to
5707 convey these parameters - are used. There is a set of helper functions
5708 to work with these S-expressions.
5709
5710 Aside from functions to register new algorithms, map algorithms names
5711 to algorithms identifiers and to lookup properties of a key, the
5712 following main functions are available:
5713
5714 'gcry_pk_encrypt'
5715 Encrypt data using a public key.
5716
5717 'gcry_pk_decrypt'
5718 Decrypt data using a private key.
5719
5720 'gcry_pk_sign'
5721 Sign data using a private key.
5722
5723 'gcry_pk_verify'
5724 Verify that a signature matches the data.
5725
5726 'gcry_pk_testkey'
5727 Perform a consistency over a public or private key.
5728
5729 'gcry_pk_genkey'
5730 Create a new public/private key pair.
5731
5732 All these functions lookup the module implementing the algorithm and
5733 pass the actual work to that module. The parsing of the S-expression
5734 input and the construction of S-expression for the return values is done
5735 by the high level code ('cipher/pubkey.c'). Thus the internal interface
5736 between the algorithm modules and the high level functions passes data
5737 in a custom format.
5738
5739 By default Libgcrypt uses a blinding technique for RSA decryption to
5740 mitigate real world timing attacks over a network: Instead of using the
5741 RSA decryption directly, a blinded value y = x r^{e} \bmod n is
5742 decrypted and the unblinded value x' = y' r^{-1} \bmod n returned. The
5743 blinding value r is a random value with the size of the modulus n and
5744 generated with 'GCRY_WEAK_RANDOM' random level.
5745
5746 The algorithm used for RSA and DSA key generation depends on whether
5747 Libgcrypt is operating in standard or in FIPS mode. In standard mode an
5748 algorithm based on the Lim-Lee prime number generator is used. In FIPS
5749 mode RSA keys are generated as specified in ANSI X9.31 (1998) and DSA
5750 keys as specified in FIPS 186-2.
5751
5752 
5753 File: gcrypt.info, Node: Symmetric Encryption Subsystem Architecture, Next: Hashing and MACing Subsystem Architecture, Prev: Public-Key Subsystem Architecture, Up: Architecture
5754
5755 17.2 Symmetric Encryption Subsystem Architecture
5756 ================================================
5757
5758 The interface to work with symmetric encryption algorithms is made up of
5759 functions from the 'gcry_cipher_' name space. The implementation
5760 follows the open-use-close paradigm and uses registered algorithm
5761 modules for the actual work. Unless a module implements optimized
5762 cipher mode implementations, the high level code ('cipher/cipher.c')
5763 implements the modes and calls the core algorithm functions to process
5764 each block.
5765
5766 The most important functions are:
5767
5768 'gcry_cipher_open'
5769 Create a new instance to encrypt or decrypt using a specified
5770 algorithm and mode.
5771
5772 'gcry_cipher_close'
5773 Release an instance.
5774
5775 'gcry_cipher_setkey'
5776 Set a key to be used for encryption or decryption.
5777
5778 'gcry_cipher_setiv'
5779 Set an initialization vector to be used for encryption or
5780 decryption.
5781
5782 'gcry_cipher_encrypt'
5783 'gcry_cipher_decrypt'
5784 Encrypt or decrypt data. These functions may be called with
5785 arbitrary amounts of data and as often as needed to encrypt or
5786 decrypt all data.
5787
5788 There is no strict alignment requirements for data, but the best
5789 performance can be archived if data is aligned to cacheline
5790 boundary.
5791
5792 There are also functions to query properties of algorithms or
5793 context, like block length, key length, map names or to enable features
5794 like padding methods.
5795
5796 
5797 File: gcrypt.info, Node: Hashing and MACing Subsystem Architecture, Next: Multi-Precision-Integer Subsystem Architecture, Prev: Symmetric Encryption Subsystem Architecture, Up: Architecture
5798
5799 17.3 Hashing and MACing Subsystem Architecture
5800 ==============================================
5801
5802 The interface to work with message digests and CRC algorithms is made up
5803 of functions from the 'gcry_md_' name space. The implementation follows
5804 the open-use-close paradigm and uses registered algorithm modules for
5805 the actual work. Although CRC algorithms are not considered
5806 cryptographic hash algorithms, they share enough properties so that it
5807 makes sense to handle them in the same way. It is possible to use
5808 several algorithms at once with one context and thus compute them all on
5809 the same data.
5810
5811 The most important functions are:
5812
5813 'gcry_md_open'
5814 Create a new message digest instance and optionally enable one
5815 algorithm. A flag may be used to turn the message digest algorithm
5816 into a HMAC algorithm.
5817
5818 'gcry_md_enable'
5819 Enable an additional algorithm for the instance.
5820
5821 'gcry_md_setkey'
5822 Set the key for the MAC.
5823
5824 'gcry_md_write'
5825 Pass more data for computing the message digest to an instance.
5826
5827 There is no strict alignment requirements for data, but the best
5828 performance can be archived if data is aligned to cacheline
5829 boundary.
5830
5831 'gcry_md_putc'
5832 Buffered version of 'gcry_md_write' implemented as a macro.
5833
5834 'gcry_md_read'
5835 Finalize the computation of the message digest or HMAC and return
5836 the result.
5837
5838 'gcry_md_close'
5839 Release an instance
5840
5841 'gcry_md_hash_buffer'
5842 Convenience function to directly compute a message digest over a
5843 memory buffer without the need to create an instance first.
5844
5845 There are also functions to query properties of algorithms or the
5846 instance, like enabled algorithms, digest length, map algorithm names.
5847 It is also possible to reset an instance or to copy the current state of
5848 an instance at any time. Debug functions to write the hashed data to
5849 files are available as well.
5850
5851 
5852 File: gcrypt.info, Node: Multi-Precision-Integer Subsystem Architecture, Next: Prime-Number-Generator Subsystem Architecture, Prev: Hashing and MACing Subsystem Architecture, Up: Architecture
5853
5854 17.4 Multi-Precision-Integer Subsystem Architecture
5855 ===================================================
5856
5857 The implementation of Libgcrypt's big integer computation code is based
5858 on an old release of GNU Multi-Precision Library (GMP). The decision not
5859 to use the GMP library directly was due to stalled development at that
5860 time and due to security requirements which could not be provided by the
5861 code in GMP. As GMP does, Libgcrypt provides high performance assembler
5862 implementations of low level code for several CPUS to gain much better
5863 performance than with a generic C implementation.
5864
5865 Major features of Libgcrypt's multi-precision-integer code compared to
5866 GMP are:
5867
5868 * Avoidance of stack based allocations to allow protection against
5869 swapping out of sensitive data and for easy zeroing of sensitive
5870 intermediate results.
5871
5872 * Optional use of secure memory and tracking of its use so that
5873 results are also put into secure memory.
5874
5875 * MPIs are identified by a handle (implemented as a pointer) to give
5876 better control over allocations and to augment them with extra
5877 properties like opaque data.
5878
5879 * Removal of unnecessary code to reduce complexity.
5880
5881 * Functions specialized for public key cryptography.
5882
5883 
5884 File: gcrypt.info, Node: Prime-Number-Generator Subsystem Architecture, Next: Random-Number Subsystem Architecture, Prev: Multi-Precision-Integer Subsystem Architecture, Up: Architecture
5885
5886 17.5 Prime-Number-Generator Subsystem Architecture
5887 ==================================================
5888
5889 Libgcrypt provides an interface to its prime number generator. These
5890 functions make use of the internal prime number generator which is
5891 required for the generation for public key pairs. The plain prime
5892 checking function is exported as well.
5893
5894 The generation of random prime numbers is based on the Lim and Lee
5895 algorithm to create practically safe primes.(1) This algorithm creates
5896 a pool of smaller primes, select a few of them to create candidate
5897 primes of the form 2 * p_0 * p_1 * ... * p_n + 1, tests the candidate
5898 for primality and permutates the pool until a prime has been found. It
5899 is possible to clamp one of the small primes to a certain size to help
5900 DSA style algorithms. Because most of the small primes in the pool are
5901 not used for the resulting prime number, they are saved for later use
5902 (see 'save_pool_prime' and 'get_pool_prime' in 'cipher/primegen.c').
5903 The prime generator optionally supports the finding of an appropriate
5904 generator.
5905
5906 The primality test works in three steps:
5907
5908 1. The standard sieve algorithm using the primes up to 4999 is used as
5909 a quick first check.
5910
5911 2. A Fermat test filters out almost all non-primes.
5912
5913 3. A 5 round Rabin-Miller test is finally used. The first round uses
5914 a witness of 2, whereas the next rounds use a random witness.
5915
5916 To support the generation of RSA and DSA keys in FIPS mode according
5917 to X9.31 and FIPS 186-2, Libgcrypt implements two additional prime
5918 generation functions: '_gcry_derive_x931_prime' and
5919 '_gcry_generate_fips186_2_prime'. These functions are internal and not
5920 available through the public API.
5921
5922 ---------- Footnotes ----------
5923
5924 (1) Chae Hoon Lim and Pil Joong Lee. A key recovery attack on
5925 discrete log-based schemes using a prime order subgroup. In Burton S.
5926 Kaliski Jr., editor, Advances in Cryptology: Crypto '97, pages 249-263,
5927 Berlin / Heidelberg / New York, 1997. Springer-Verlag. Described on
5928 page 260.
5929
5930 
5931 File: gcrypt.info, Node: Random-Number Subsystem Architecture, Prev: Prime-Number-Generator Subsystem Architecture, Up: Architecture
5932
5933 17.6 Random-Number Subsystem Architecture
5934 =========================================
5935
5936 Libgcrypt provides 3 levels or random quality: The level
5937 'GCRY_VERY_STRONG_RANDOM' usually used for key generation, the level
5938 'GCRY_STRONG_RANDOM' for all other strong random requirements and the
5939 function 'gcry_create_nonce' which is used for weaker usages like
5940 nonces. There is also a level 'GCRY_WEAK_RANDOM' which in general maps
5941 to 'GCRY_STRONG_RANDOM' except when used with the function
5942 'gcry_mpi_randomize', where it randomizes a multi-precision integer
5943 using the 'gcry_create_nonce' function.
5944
5945 There are three distinct random generators available:
5946
5947 * The Continuously Seeded Pseudo Random Number Generator (CSPRNG),
5948 which is based on the classic GnuPG derived big pool
5949 implementation. Implemented in 'random/random-csprng.c' and used
5950 by default.
5951 * The Deterministic Random Bits Generator (DRBG), based on the
5952 specification by NIST SP800-90A. Implemented in
5953 'random/random-drbg.c' and used if Libgcrypt is in FIPS mode, or
5954 Libgcrypt is configured by GCRYCTL_SET_PREFERRED_RNG_TYPE with
5955 GCRY_RNG_TYPE_FIPS.
5956 * Direct access to native RNG on the system. Implemented in
5957 'random/random-system.c' and used if Libgcrypt is configured by
5958 GCRYCTL_SET_PREFERRED_RNG_TYPE with GCRY_RNG_TYPE_SYSTEM.
5959
5960 All generators make use of so-called entropy gathering modules:
5961
5962 rndgetentropy
5963 Uses the operating system provided 'getentropy' function.
5964
5965 rndoldlinux
5966 Uses the operating system provided '/dev/random' and '/dev/urandom'
5967 devices. The '/dev/gcrypt/random.conf' config option
5968 'only-urandom' can be used to inhibit the use of the blocking
5969 '/dev/random' device.
5970
5971 rndunix
5972 Runs several operating system commands to collect entropy from
5973 sources like virtual machine and process statistics. It is a kind
5974 of poor-man's '/dev/random' implementation. It is not available in
5975 FIPS mode.
5976
5977 rndegd
5978 Uses the operating system provided Entropy Gathering Daemon (EGD).
5979 The EGD basically uses the same algorithms as rndunix does.
5980 However as a system daemon it keeps on running and thus can serve
5981 several processes requiring entropy input and does not waste
5982 collected entropy if the application does not need all the
5983 collected entropy.
5984
5985 rndw32
5986 Targeted for the Microsoft Windows OS. It uses certain properties
5987 of that system and is the only gathering module available for that
5988 OS.
5989
5990 rndhw
5991 Extra module to collect additional entropy by utilizing a hardware
5992 random number generator. As of now the supported hardware RNG is
5993 the Padlock engine of VIA (Centaur) CPUs and x86 CPUs with the
5994 RDRAND instruction.
5995
5996 rndjent
5997 Extra module to collect additional entropy using a CPU jitter based
5998 approach. The '/dev/gcrypt/random.conf' config option
5999 'disable-jent' can be used to inhibit the use of this module.
6000
6001 * Menu:
6002
6003 * CSPRNG Description:: Description of the CSPRNG.
6004 * DRBG Description:: Description of the DRBG.
6005
6006 
6007 File: gcrypt.info, Node: CSPRNG Description, Next: DRBG Description, Up: Random-Number Subsystem Architecture
6008
6009 17.6.1 Description of the CSPRNG
6010 --------------------------------
6011
6012 This random number generator is loosely modelled after the one described
6013 in Peter Gutmann's paper "Software Generation of Practically Strong
6014 Random Numbers".(1)
6015
6016 A pool of 600 bytes is used and mixed using the core SHA-1 hash
6017 transform function. Several extra features are used to make it robust
6018 against a wide variety of attacks and to protect against failures of
6019 subsystems. The state of the generator may be saved to a file and
6020 initially seeded form a file.
6021
6022 Depending on how Libgcrypt was build, the generator is able to select
6023 the best working entropy gathering module. It makes use of the slow and
6024 fast collection methods and requires the pool to be initially seeded
6025 form the slow gatherer or a seed file. An entropy estimation is used to
6026 mix in enough data from the gather modules before returning the actual
6027 random output. Process fork detection and protection is implemented.
6028
6029 The implementation of the nonce generator (for 'gcry_create_nonce')
6030 is a straightforward repeated hash design: A 28 byte buffer is initially
6031 seeded with the PID and the time in seconds in the first 20 bytes and
6032 with 8 bytes of random taken from the 'GCRY_STRONG_RANDOM' generator.
6033 Random numbers are then created by hashing all the 28 bytes with SHA-1
6034 and saving that again in the first 20 bytes. The hash is also returned
6035 as result.
6036
6037 ---------- Footnotes ----------
6038
6039 (1) Also described in chapter 6 of his book "Cryptographic Security
6040 Architecture", New York, 2004, ISBN 0-387-95387-6.
6041
6042 
6043 File: gcrypt.info, Node: DRBG Description, Prev: CSPRNG Description, Up: Random-Number Subsystem Architecture
6044
6045 17.6.2 Description of the DRBG
6046 ------------------------------
6047
6048 The core of this deterministic random number generator is implemented
6049 according to the document "NIST Recommended DRBG Based on ANSI NIST
6050 SP800-90A". By default, this implementation uses the
6051 DRBG_NOPR_HMACSHA256 variant (HMAC DRBG with DF with SHA256, without
6052 prediction resistance.
6053
6054 The generator is based on contexts to utilize the same core functions
6055 for all random levels as required by the high-level interface. All
6056 random generators return their data in 128 bit blocks. If the caller
6057 requests fewer bits, the extra bits are not used. The key for each
6058 generator is only set once at the first time a generator context is
6059 used. The seed value is set along with the key and again after 1000
6060 output blocks.
6061
6062 On Unix like systems the 'GCRY_VERY_STRONG_RANDOM' and
6063 'GCRY_STRONG_RANDOM' generators are keyed and seeded using the
6064 rndgetentropy or rndoldlinux module. With rndoldlinux module, these
6065 generators may block until the OS kernel has collected enough entropy.
6066 When used with Microsoft Windows, the rndw32 module is used instead.
6067
6068 The generator used for 'gcry_create_nonce' is keyed and seeded from
6069 the 'GCRY_STRONG_RANDOM' generator. Thus, with rndoldlinux module, it
6070 may also block if the 'GCRY_STRONG_RANDOM' generator has not yet been
6071 used before and thus gets initialized on the first use by
6072 'gcry_create_nonce'. This special treatment is justified by the weaker
6073 requirements for a nonce generator and to save precious kernel entropy
6074 for use by the "real" random generators.
6075
6076 
6077 File: gcrypt.info, Node: Self-Tests, Next: FIPS Mode, Prev: Architecture, Up: Top
6078
6079 Appendix A Description of the Self-Tests
6080 ****************************************
6081
6082 In addition to the build time regression test suite, Libgcrypt
6083 implements self-tests to be performed at runtime. Which self-tests are
6084 actually used depends on the mode Libgcrypt is used in. In standard
6085 mode a limited set of self-tests is run at the time an algorithm is
6086 first used. Note that not all algorithms feature a self-test in
6087 standard mode. The 'GCRYCTL_SELFTEST' control command may be used to
6088 run all implemented self-tests at any time; this will even run more
6089 tests than those run in FIPS mode.
6090
6091 If any of the self-tests fails, the library immediately returns an
6092 error code to the caller. If Libgcrypt is in FIPS mode, the self-tests
6093 will be performed within the "Self-Test" state and any failure puts the
6094 library into the "Error" state.
6095
6096 A.1 Power-Up Tests
6097 ==================
6098
6099 Power-up tests are only performed if Libgcrypt is in FIPS mode.
6100
6101 A.1.1 Symmetric Cipher Algorithm Power-Up Tests
6102 -----------------------------------------------
6103
6104 The following symmetric encryption algorithm tests are run during
6105 power-up:
6106
6107 AES-128
6108 A known answer tests is run using one test vector and one test key
6109 with AES in ECB mode. ('cipher/rijndael.c:selftest_basic_128')
6110
6111 AES-192
6112 A known answer tests is run using one test vector and one test key
6113 with AES in ECB mode. ('cipher/rijndael.c:selftest_basic_192')
6114
6115 AES-256
6116 A known answer tests is run using one test vector and one test key
6117 with AES in ECB mode. ('cipher/rijndael.c:selftest_basic_256')
6118
6119 A.1.2 Hash Algorithm Power-Up Tests
6120 -----------------------------------
6121
6122 The following hash algorithm tests are run during power-up:
6123
6124 SHA-1
6125 A known answer test using the string '"abc"' is run.
6126 ('cipher/sha1.c:selftests_sha1')
6127 SHA-224
6128 A known answer test using the string '"abc"' is run.
6129 ('cipher/sha256.c:selftests_sha224')
6130 SHA-256
6131 A known answer test using the string '"abc"' is run.
6132 ('cipher/sha256.c:selftests_sha256')
6133 SHA-384
6134 A known answer test using the string '"abc"' is run.
6135 ('cipher/sha512.c:selftests_sha384')
6136 SHA-512
6137 A known answer test using the string '"abc"' is run.
6138 ('cipher/sha512.c:selftests_sha512')
6139
6140 A.1.3 MAC Algorithm Power-Up Tests
6141 ----------------------------------
6142
6143 The following MAC algorithm tests are run during power-up:
6144
6145 HMAC SHA-1
6146 A known answer test using 9 bytes of data and a 64 byte key is run.
6147 ('cipher/mac-hmac.c:selftests_sha1')
6148 HMAC SHA-224
6149 A known answer test using 28 bytes of data and a 4 byte key is run.
6150 ('cipher/mac-hmac.c:selftests_sha224')
6151 HMAC SHA-256
6152 A known answer test using 28 bytes of data and a 4 byte key is run.
6153 ('cipher/mac-hmac.c:selftests_sha256')
6154 HMAC SHA-384
6155 A known answer test using 28 bytes of data and a 4 byte key is run.
6156 ('cipher/mac-hmac.c:selftests_sha384')
6157 HMAC SHA-512
6158 A known answer test using 28 bytes of data and a 4 byte key is run.
6159 ('cipher/mac-hmac.c:selftests_sha512')
6160 HMAC SHA3-224
6161 HMAC SHA3-256
6162 HMAC SHA3-384
6163 HMAC SHA3-512
6164 A known answer test using 9 bytes of data and a 20 byte key is run.
6165 ('cipher/mac-hmac.c:selftests_sha3')
6166 CMAC AES
6167 A known answer test using 40 bytes of data and a 16 byte key is
6168 run. ('cipher/mac-cmac.c:selftests_cmac_aes')
6169
6170 A.1.4 Random Number Power-Up Test
6171 ---------------------------------
6172
6173 The DRNG is tested during power-up this way:
6174
6175 1. Requesting one block of random using the public interface to check
6176 general working and the duplicated block detection.
6177 2. 3 know answer tests using pre-defined keys, seed and initial DT
6178 values. For each test 3 blocks of 16 bytes are requested and
6179 compared to the expected result. The DT value is incremented for
6180 each block.
6181
6182 A.1.5 Public Key Algorithm Power-Up Tests
6183 -----------------------------------------
6184
6185 The public key algorithms are tested during power-up:
6186
6187 RSA
6188 A pre-defined 2048 bit RSA key is used and these tests are run in
6189 turn:
6190 1. Conversion of S-expression to internal format.
6191 ('cipher/rsa.c:selftests_rsa')
6192 2. Private key consistency check. ('cipher/rsa.c:selftests_rsa')
6193 3. A pre-defined 20 byte value is signed with PKCS#1 padding for
6194 SHA-256. The result is verified using the public key against
6195 the original data and against modified data.
6196 ('cipher/rsa.c:selftest_sign_2048')
6197 4. A predefined 66 byte value is encrypted and checked that it
6198 matches reference encyrpted message. The encrypted result is
6199 then decrypted and checked that it matches the original random
6200 value. ('cipher/rsa.c:selftest_encr_2048')
6201
6202 ECC
6203 A pre-defined SEC P-256 ECDSA key is used and these tests are run
6204 in turn:
6205 1. Conversion of S-expression to internal format.
6206 ('cipher/ecc.c:selftests_ecdsa')
6207 2. Private key consistency check.
6208 ('cipher/ecc.c:selftests_ecdsa')
6209 3. A pre-defined 32 byte value (SHA-256 digest) is signed. The
6210 result is verified using the public key against the original
6211 data and against modified data.
6212 ('cipher/ecc.c:selftest_sign')
6213
6214 A.1.6 Key derivation function Power-Up Tests
6215 --------------------------------------------
6216
6217 The key derivation functions are tested during power-up:
6218
6219 PBKDF2
6220 A known answer tests with 8 byte password and 4 byte salt and SHA-1
6221 is used. ('cipher/kdf.c:selftest_pbkdf2')
6222
6223 A.1.7 Integrity Power-Up Tests
6224 ------------------------------
6225
6226 The integrity of the Libgcrypt is tested during power-up but only if
6227 checking has been enabled at build time. The check works by computing a
6228 HMAC SHA-256 checksum over the file used to load Libgcrypt into memory.
6229 That checksum is compared against a checksum stored inside of the same
6230 file as in the text in the .rodata1 section of the ELF file.
6231
6232 A.2 Conditional Tests
6233 =====================
6234
6235 The conditional tests are performed if a certain condition is met. This
6236 may occur at any time; the library does not necessary enter the
6237 "Self-Test" state to run these tests but will transit to the "Error"
6238 state if a test failed.
6239
6240 A.2.1 Key-Pair Generation Tests
6241 -------------------------------
6242
6243 After an asymmetric key-pair has been generated, Libgcrypt runs a
6244 pair-wise consistency tests on the generated key. On failure the
6245 generated key is not used, an error code is returned and, if in FIPS
6246 mode, the library is put into the "Error" state.
6247
6248 RSA
6249 The test uses a random number 64 bits less the size of the modulus
6250 as plaintext and runs an encryption and decryption operation in
6251 turn. The encrypted value is checked to not match the plaintext,
6252 and the result of the decryption is checked to match the plaintext.
6253
6254 A new random number of the same size is generated, signed and
6255 verified to test the correctness of the signing operation. As a
6256 second signing test, the signature is modified by incrementing its
6257 value and then verified with the expected result that the
6258 verification fails. ('cipher/rsa.c:test_keys')
6259
6260 A.2.2 Software Load Tests
6261 -------------------------
6262
6263 No code is loaded at runtime.
6264
6265 A.2.3 Manual Key Entry Tests
6266 ----------------------------
6267
6268 A manual key entry feature is not implemented in Libgcrypt.
6269
6270 A.3 Application Requested Tests
6271 ===============================
6272
6273 The application may requests tests at any time by means of the
6274 'GCRYCTL_SELFTEST' control command. Note that using these tests is not
6275 FIPS conformant: Although Libgcrypt rejects all application requests for
6276 services while running self-tests, it does not ensure that no other
6277 operations of Libgcrypt are still being executed. Thus, in FIPS mode an
6278 application requesting self-tests needs to power-cycle Libgcrypt
6279 instead.
6280
6281 When self-tests are requested, Libgcrypt runs all the tests it does
6282 during power-up as well as a few extra checks as described below.
6283
6284 A.3.1 Symmetric Cipher Algorithm Tests
6285 --------------------------------------
6286
6287 The following symmetric encryption algorithm tests are run in addition
6288 to the power-up tests:
6289
6290 AES-128
6291 A known answer tests with test vectors taken from NIST SP800-38a
6292 and using the high level functions is run for block modes CFB and
6293 OFB.
6294
6295 A.3.2 Hash Algorithm Tests
6296 --------------------------
6297
6298 The following hash algorithm tests are run in addition to the power-up
6299 tests:
6300
6301 SHA-1
6302 SHA-224
6303 SHA-256
6304 1. A known answer test using a 56 byte string is run.
6305 2. A known answer test using a string of one million letters "a"
6306 is run.
6307 ('cipher/sha1.c:selftests_sha1',
6308 'cipher/sha256.c:selftests_sha224',
6309 'cipher/sha256.c:selftests_sha256')
6310 SHA-384
6311 SHA-512
6312 1. A known answer test using a 112 byte string is run.
6313 2. A known answer test using a string of one million letters "a"
6314 is run.
6315 ('cipher/sha512.c:selftests_sha384',
6316 'cipher/sha512.c:selftests_sha512')
6317
6318 A.3.3 MAC Algorithm Tests
6319 -------------------------
6320
6321 The following MAC algorithm tests are run in addition to the power-up
6322 tests:
6323
6324 HMAC SHA-1
6325 1. A known answer test using 9 bytes of data and a 20 byte key is
6326 run.
6327 2. A known answer test using 9 bytes of data and a 100 byte key
6328 is run.
6329 3. A known answer test using 9 bytes of data and a 49 byte key is
6330 run.
6331 ('cipher/mac-hmac.c:selftests_sha1')
6332 HMAC SHA-224
6333 HMAC SHA-256
6334 HMAC SHA-384
6335 HMAC SHA-512
6336 1. A known answer test using 9 bytes of data and a 20 byte key is
6337 run.
6338 2. A known answer test using 50 bytes of data and a 20 byte key
6339 is run.
6340 3. A known answer test using 50 bytes of data and a 26 byte key
6341 is run.
6342 4. A known answer test using 54 bytes of data and a 131 byte key
6343 is run.
6344 5. A known answer test using 152 bytes of data and a 131 byte key
6345 is run.
6346 ('cipher/mac-hmac.c:selftests_sha224',
6347 'cipher/mac-hmac.c:selftests_sha256',
6348 'cipher/mac-hmac.c:selftests_sha384',
6349 'cipher/mac-hmac.c:selftests_sha512')
6350
6351 HMAC SHA3-224
6352 HMAC SHA3-256
6353 HMAC SHA3-384
6354 HMAC SHA3-512
6355 1. A known answer test using 28 byte of data and a 4 byte key is
6356 run.
6357 2. A known answer test using 50 byte of data and a 20 byte key is
6358 run.
6359 3. A known answer test using 50 byte of data and a 25 byte key is
6360 run.
6361 4. A known answer test using 20 byte of data and a 20 byte key
6362 with truncation is run.
6363 5. A known answer test using 54 byte of data and a 131 byte key
6364 is run.
6365 6. A known answer test using 54 byte of data and a 147 byte key
6366 is run.
6367 7. A known answer test using 152 byte of data and a 131 byte key
6368 is run.
6369 8. A known answer test using 152 byte of data and a 147 byte key
6370 is run.
6371 ('cipher/mac-hmac.c:selftests_sha3',
6372
6373 CMAC AES
6374 1. A known answer test using 0 byte of data and a 16 byte key is
6375 run.
6376 2. A known answer test using 24 byte of data and a 16 byte key is
6377 run.
6378 3. A known answer test using 64 byte of data and a 32 byte key is
6379 run.
6380 4. A known answer test using 16 byte of data and a 16 byte key is
6381 run.
6382 5. A known answer test using 64 byte of data and a 16 byte key is
6383 run.
6384 6. A known answer test using 0 byte of data and a 24 byte key is
6385 run.
6386 7. A known answer test using 64 byte of data and a 24 byte key is
6387 run.
6388 8. A known answer test using 0 byte of data and a 32 byte key is
6389 run.
6390 9. A known answer test using 16 byte of data and a 32 byte key is
6391 run.
6392 ('cipher/mac-cmac.c:selftests_cmac_aes',
6393
6394 
6395 File: gcrypt.info, Node: FIPS Mode, Next: Library Copying, Prev: Self-Tests, Up: Top
6396
6397 Appendix B Description of the FIPS Mode
6398 ***************************************
6399
6400 This appendix gives detailed information pertaining to the FIPS mode.
6401 In particular, the changes to the standard mode and the finite state
6402 machine are described. The self-tests required in this mode are
6403 described in the appendix on self-tests.
6404
6405 B.1 Restrictions in FIPS Mode
6406 =============================
6407
6408 If Libgcrypt is used in FIPS mode, these restrictions are effective:
6409
6410 * The cryptographic algorithms are restricted to this list:
6411
6412 GCRY_CIPHER_AES128
6413 AES 128 bit symmetric encryption.
6414 GCRY_CIPHER_AES192
6415 AES 192 bit symmetric encryption.
6416 GCRY_CIPHER_AES256
6417 AES 256 bit symmetric encryption.
6418 GCRY_MD_SHA1
6419 SHA-1 message digest.
6420 GCRY_MD_SHA224
6421 SHA-224 message digest.
6422 GCRY_MD_SHA256
6423 SHA-256 message digest.
6424 GCRY_MD_SHA384
6425 SHA-384 message digest.
6426 GCRY_MD_SHA512
6427 SHA-512 message digest.
6428 GCRY_MD_SHA3_224
6429 SHA3-224 message digest.
6430 GCRY_MD_SHA3_256
6431 SHA3-256 message digest.
6432 GCRY_MD_SHA3_384
6433 SHA3-384 message digest.
6434 GCRY_MD_SHA3_512
6435 SHA3-512 message digest.
6436 GCRY_MD_SHA1,GCRY_MD_FLAG_HMAC
6437 HMAC using a SHA-1 message digest.
6438 GCRY_MD_SHA224,GCRY_MD_FLAG_HMAC
6439 HMAC using a SHA-224 message digest.
6440 GCRY_MD_SHA256,GCRY_MD_FLAG_HMAC
6441 HMAC using a SHA-256 message digest.
6442 GCRY_MD_SHA384,GCRY_MD_FLAG_HMAC
6443 HMAC using a SHA-384 message digest.
6444 GCRY_MD_SHA512,GCRY_MD_FLAG_HMAC
6445 HMAC using a SHA-512 message digest.
6446 GCRY_MD_SHA3_224,GCRY_MD_FLAG_HMAC
6447 HMAC using a SHA3-224 message digest.
6448 GCRY_MD_SHA3_256,GCRY_MD_FLAG_HMAC
6449 HMAC using a SHA3-256 message digest.
6450 GCRY_MD_SHA3_384,GCRY_MD_FLAG_HMAC
6451 HMAC using a SHA3-384 message digest.
6452 GCRY_MD_SHA3_512,GCRY_MD_FLAG_HMAC
6453 HMAC using a SHA3-512 message digest.
6454 GCRY_MAC_CMAC_AES
6455 CMAC using a AES key.
6456 GCRY_PK_RSA
6457 RSA encryption and signing.
6458 GCRY_PK_ECC
6459 ECC encryption and signing.
6460
6461 Note that the CRC algorithms are not considered cryptographic
6462 algorithms and thus are in addition available.
6463
6464 * RSA key generation refuses to create and use a key with a keysize
6465 of less than 2048 bits.
6466
6467 * The 'transient-key' flag for RSA key generation is ignored.
6468
6469 * Support for the VIA Padlock engine is disabled.
6470
6471 * FIPS mode may only be used on systems with a /dev/random device or
6472 with a getentropy syscall. Switching into FIPS mode on other
6473 systems will fail at runtime.
6474
6475 * Saving and loading a random seed file is ignored.
6476
6477 * The DRBG style random number generator is used in place of the
6478 large-pool-CSPRNG generator.
6479
6480 * The command 'GCRYCTL_ENABLE_QUICK_RANDOM' is ignored.
6481
6482 * Message digest debugging is disabled.
6483
6484 * All debug output related to cryptographic data is suppressed.
6485
6486 * On-the-fly self-tests are not performed, instead self-tests are run
6487 before entering operational state.
6488
6489 * The function 'gcry_set_allocation_handler' may not be used. In
6490 FIPS mode this function does not have any effect, because FIPS has
6491 requirements for memory zeroization.
6492
6493 * The digest algorithm MD5 may not be used.
6494
6495 * The signatures using SHA-1 digest algorithm may not be used.
6496
6497 * In FIPS mode the command 'GCRYCTL_DISABLE_SECMEM' is ignored.
6498
6499 * A handler set by 'gcry_set_outofcore_handler' is ignored.
6500
6501 * A handler set by 'gcry_set_fatalerror_handler' is ignored.
6502
6503 Note that when we speak about disabling FIPS mode, it merely means
6504 that the function 'gcry_fips_mode_active' returns false; it does not
6505 mean that any non FIPS algorithms are allowed.
6506
6507 B.2 FIPS Finite State Machine
6508 =============================
6509
6510 The FIPS mode of Libgcrypt implements a finite state machine (FSM) using
6511 8 states (*note Table B.1: tbl:fips-states.) and checks at runtime that
6512 only valid transitions (*note Table B.2: tbl:fips-state-transitions.)
6513 may happen.
6514
6515
6516 [image src="fips-fsm.png" alt="FIPS FSM Diagram"]
6517
6518 Figure B.1: FIPS mode state diagram
6519
6520
6521 States used by the FIPS FSM:
6522
6523 Power-Off
6524 Libgcrypt is not runtime linked to another application. This
6525 usually means that the library is not loaded into main memory.
6526 This state is documentation only.
6527
6528 Power-On
6529 Libgcrypt is loaded into memory and API calls may be made.
6530 Compiler introduced constructor functions may be run. Note that
6531 Libgcrypt does not implement any arbitrary constructor functions to
6532 be called by the operating system
6533
6534 Init
6535 The Libgcrypt initialization functions are performed and the
6536 library has not yet run any self-test.
6537
6538 Self-Test
6539 Libgcrypt is performing self-tests.
6540
6541 Operational
6542 Libgcrypt is in the operational state and all interfaces may be
6543 used.
6544
6545 Error
6546 Libgrypt is in the error state. When calling any FIPS relevant
6547 interfaces they either return an error ('GPG_ERR_NOT_OPERATIONAL')
6548 or put Libgcrypt into the Fatal-Error state and won't return.
6549
6550 Fatal-Error
6551 Libgcrypt is in a non-recoverable error state and will
6552 automatically transit into the Shutdown state.
6553
6554 Shutdown
6555 Libgcrypt is about to be terminated and removed from the memory.
6556 The application may at this point still run cleanup handlers.
6557
6558 Table B.1: FIPS mode states
6559
6560
6561 The valid state transitions (*note Figure B.1: fig:fips-fsm.) are:
6562 '1'
6563 Power-Off to Power-On is implicitly done by the OS loading
6564 Libgcrypt as a shared library and having it linked to an
6565 application.
6566
6567 '2'
6568 Power-On to Init is triggered by the application calling the
6569 Libgcrypt initialization function 'gcry_check_version'.
6570
6571 '3'
6572 Init to Self-Test is either triggered by a dedicated API call or
6573 implicit by invoking a Libgrypt service controlled by the FSM.
6574
6575 '4'
6576 Self-Test to Operational is triggered after all self-tests passed
6577 successfully.
6578
6579 '5'
6580 Operational to Shutdown is an artificial state without any direct
6581 action in Libgcrypt. When reaching the Shutdown state the library
6582 is deinitialized and can't return to any other state again.
6583
6584 '6'
6585 Shutdown to Power-Off is the process of removing Libgcrypt from the
6586 computer's memory. For obvious reasons the Power-Off state can't
6587 be represented within Libgcrypt and thus this transition is for
6588 documentation only.
6589
6590 '7'
6591 Operational to Error is triggered if Libgcrypt detected an
6592 application error which can't be returned to the caller but still
6593 allows Libgcrypt to properly run. In the Error state all FIPS
6594 relevant interfaces return an error code.
6595
6596 '8'
6597 Error to Shutdown is similar to the Operational to Shutdown
6598 transition (5).
6599
6600 '9'
6601 Error to Fatal-Error is triggered if Libgrypt detects an fatal
6602 error while already being in Error state.
6603
6604 '10'
6605 Fatal-Error to Shutdown is automatically entered by Libgcrypt after
6606 having reported the error.
6607
6608 '11'
6609 Power-On to Shutdown is an artificial state to document that
6610 Libgcrypt has not yet been initialized but the process is about to
6611 terminate.
6612
6613 '12'
6614 Power-On to Fatal-Error will be triggered if certain Libgcrypt
6615 functions are used without having reached the Init state.
6616
6617 '13'
6618 Self-Test to Fatal-Error is triggered by severe errors in Libgcrypt
6619 while running self-tests.
6620
6621 '14'
6622 Self-Test to Error is triggered by a failed self-test.
6623
6624 '15'
6625 Operational to Fatal-Error is triggered if Libcrypt encountered a
6626 non-recoverable error.
6627
6628 '16'
6629 Operational to Self-Test is triggered if the application requested
6630 to run the self-tests again.
6631
6632 '17'
6633 Error to Self-Test is triggered if the application has requested to
6634 run self-tests to get back into operational state after an error.
6635
6636 '18'
6637 Init to Error is triggered by errors in the initialization code.
6638
6639 '19'
6640 Init to Fatal-Error is triggered by non-recoverable errors in the
6641 initialization code.
6642
6643 '20'
6644 Error to Error is triggered by errors while already in the Error
6645 state.
6646
6647 Table B.2: FIPS mode state transitions
6648
6649 B.3 FIPS Miscellaneous Information
6650 ==================================
6651
6652 Libgcrypt does not do any key management on itself; the application
6653 needs to care about it. Keys which are passed to Libgcrypt should be
6654 allocated in secure memory as available with the functions
6655 'gcry_malloc_secure' and 'gcry_calloc_secure'. By calling 'gcry_free'
6656 on this memory, the memory and thus the keys are overwritten with zero
6657 bytes before releasing the memory.
6658
6659 For use with the random number generator, Libgcrypt generates 3
6660 internal keys which are stored in the encryption contexts used by the
6661 RNG. These keys are stored in secure memory for the lifetime of the
6662 process. Application are required to use 'GCRYCTL_TERM_SECMEM' before
6663 process termination. This will zero out the entire secure memory and
6664 thus also the encryption contexts with these keys.
6665
6666 
6667 File: gcrypt.info, Node: Library Copying, Next: Copying, Prev: FIPS Mode, Up: Top
6668
6669 GNU Lesser General Public License
6670 *********************************
6671
6672 Version 2.1, February 1999
6673
6674 Copyright (C) 1991, 1999 Free Software Foundation, Inc.
6675 <https://fsf.org/>
6676
6677 Everyone is permitted to copy and distribute verbatim copies
6678 of this license document, but changing it is not allowed.
6679
6680 [This is the first released version of the Lesser GPL. It also counts
6681 as the successor of the GNU Library Public License, version 2, hence the
6682 version number 2.1.]
6683
6684 Preamble
6685 --------
6686
6687 The licenses for most software are designed to take away your freedom to
6688 share and change it. By contrast, the GNU General Public Licenses are
6689 intended to guarantee your freedom to share and change free software--to
6690 make sure the software is free for all its users.
6691
6692 This license, the Lesser General Public License, applies to some
6693 specially designated software--typically libraries--of the Free Software
6694 Foundation and other authors who decide to use it. You can use it too,
6695 but we suggest you first think carefully about whether this license or
6696 the ordinary General Public License is the better strategy to use in any
6697 particular case, based on the explanations below.
6698
6699 When we speak of free software, we are referring to freedom of use,
6700 not price. Our General Public Licenses are designed to make sure that
6701 you have the freedom to distribute copies of free software (and charge
6702 for this service if you wish); that you receive source code or can get
6703 it if you want it; that you can change the software and use pieces of it
6704 in new free programs; and that you are informed that you can do these
6705 things.
6706
6707 To protect your rights, we need to make restrictions that forbid
6708 distributors to deny you these rights or to ask you to surrender these
6709 rights. These restrictions translate to certain responsibilities for
6710 you if you distribute copies of the library or if you modify it.
6711
6712 For example, if you distribute copies of the library, whether gratis
6713 or for a fee, you must give the recipients all the rights that we gave
6714 you. You must make sure that they, too, receive or can get the source
6715 code. If you link other code with the library, you must provide
6716 complete object files to the recipients, so that they can relink them
6717 with the library after making changes to the library and recompiling it.
6718 And you must show them these terms so they know their rights.
6719
6720 We protect your rights with a two-step method: (1) we copyright the
6721 library, and (2) we offer you this license, which gives you legal
6722 permission to copy, distribute and/or modify the library.
6723
6724 To protect each distributor, we want to make it very clear that there
6725 is no warranty for the free library. Also, if the library is modified
6726 by someone else and passed on, the recipients should know that what they
6727 have is not the original version, so that the original author's
6728 reputation will not be affected by problems that might be introduced by
6729 others.
6730
6731 Finally, software patents pose a constant threat to the existence of
6732 any free program. We wish to make sure that a company cannot
6733 effectively restrict the users of a free program by obtaining a
6734 restrictive license from a patent holder. Therefore, we insist that any
6735 patent license obtained for a version of the library must be consistent
6736 with the full freedom of use specified in this license.
6737
6738 Most GNU software, including some libraries, is covered by the
6739 ordinary GNU General Public License. This license, the GNU Lesser
6740 General Public License, applies to certain designated libraries, and is
6741 quite different from the ordinary General Public License. We use this
6742 license for certain libraries in order to permit linking those libraries
6743 into non-free programs.
6744
6745 When a program is linked with a library, whether statically or using
6746 a shared library, the combination of the two is legally speaking a
6747 combined work, a derivative of the original library. The ordinary
6748 General Public License therefore permits such linking only if the entire
6749 combination fits its criteria of freedom. The Lesser General Public
6750 License permits more lax criteria for linking other code with the
6751 library.
6752
6753 We call this license the "Lesser" General Public License because it
6754 does _Less_ to protect the user's freedom than the ordinary General
6755 Public License. It also provides other free software developers Less of
6756 an advantage over competing non-free programs. These disadvantages are
6757 the reason we use the ordinary General Public License for many
6758 libraries. However, the Lesser license provides advantages in certain
6759 special circumstances.
6760
6761 For example, on rare occasions, there may be a special need to
6762 encourage the widest possible use of a certain library, so that it
6763 becomes a de-facto standard. To achieve this, non-free programs must be
6764 allowed to use the library. A more frequent case is that a free library
6765 does the same job as widely used non-free libraries. In this case,
6766 there is little to gain by limiting the free library to free software
6767 only, so we use the Lesser General Public License.
6768
6769 In other cases, permission to use a particular library in non-free
6770 programs enables a greater number of people to use a large body of free
6771 software. For example, permission to use the GNU C Library in non-free
6772 programs enables many more people to use the whole GNU operating system,
6773 as well as its variant, the GNU/Linux operating system.
6774
6775 Although the Lesser General Public License is Less protective of the
6776 users' freedom, it does ensure that the user of a program that is linked
6777 with the Library has the freedom and the wherewithal to run that program
6778 using a modified version of the Library.
6779
6780 The precise terms and conditions for copying, distribution and
6781 modification follow. Pay close attention to the difference between a
6782 "work based on the library" and a "work that uses the library". The
6783 former contains code derived from the library, whereas the latter must
6784 be combined with the library in order to run.
6785
6786 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
6787 ---------------------------------------------------------------
6788
6789 0. This License Agreement applies to any software library or other
6790 program which contains a notice placed by the copyright holder or
6791 other authorized party saying it may be distributed under the terms
6792 of this Lesser General Public License (also called "this License").
6793 Each licensee is addressed as "you".
6794
6795 A "library" means a collection of software functions and/or data
6796 prepared so as to be conveniently linked with application programs
6797 (which use some of those functions and data) to form executables.
6798
6799 The "Library", below, refers to any such software library or work
6800 which has been distributed under these terms. A "work based on the
6801 Library" means either the Library or any derivative work under
6802 copyright law: that is to say, a work containing the Library or a
6803 portion of it, either verbatim or with modifications and/or
6804 translated straightforwardly into another language. (Hereinafter,
6805 translation is included without limitation in the term
6806 "modification".)
6807
6808 "Source code" for a work means the preferred form of the work for
6809 making modifications to it. For a library, complete source code
6810 means all the source code for all modules it contains, plus any
6811 associated interface definition files, plus the scripts used to
6812 control compilation and installation of the library.
6813
6814 Activities other than copying, distribution and modification are
6815 not covered by this License; they are outside its scope. The act
6816 of running a program using the Library is not restricted, and
6817 output from such a program is covered only if its contents
6818 constitute a work based on the Library (independent of the use of
6819 the Library in a tool for writing it). Whether that is true
6820 depends on what the Library does and what the program that uses the
6821 Library does.
6822
6823 1. You may copy and distribute verbatim copies of the Library's
6824 complete source code as you receive it, in any medium, provided
6825 that you conspicuously and appropriately publish on each copy an
6826 appropriate copyright notice and disclaimer of warranty; keep
6827 intact all the notices that refer to this License and to the
6828 absence of any warranty; and distribute a copy of this License
6829 along with the Library.
6830
6831 You may charge a fee for the physical act of transferring a copy,
6832 and you may at your option offer warranty protection in exchange
6833 for a fee.
6834
6835 2. You may modify your copy or copies of the Library or any portion of
6836 it, thus forming a work based on the Library, and copy and
6837 distribute such modifications or work under the terms of Section 1
6838 above, provided that you also meet all of these conditions:
6839
6840 a. The modified work must itself be a software library.
6841
6842 b. You must cause the files modified to carry prominent notices
6843 stating that you changed the files and the date of any change.
6844
6845 c. You must cause the whole of the work to be licensed at no
6846 charge to all third parties under the terms of this License.
6847
6848 d. If a facility in the modified Library refers to a function or
6849 a table of data to be supplied by an application program that
6850 uses the facility, other than as an argument passed when the
6851 facility is invoked, then you must make a good faith effort to
6852 ensure that, in the event an application does not supply such
6853 function or table, the facility still operates, and performs
6854 whatever part of its purpose remains meaningful.
6855
6856 (For example, a function in a library to compute square roots
6857 has a purpose that is entirely well-defined independent of the
6858 application. Therefore, Subsection 2d requires that any
6859 application-supplied function or table used by this function
6860 must be optional: if the application does not supply it, the
6861 square root function must still compute square roots.)
6862
6863 These requirements apply to the modified work as a whole. If
6864 identifiable sections of that work are not derived from the
6865 Library, and can be reasonably considered independent and separate
6866 works in themselves, then this License, and its terms, do not apply
6867 to those sections when you distribute them as separate works. But
6868 when you distribute the same sections as part of a whole which is a
6869 work based on the Library, the distribution of the whole must be on
6870 the terms of this License, whose permissions for other licensees
6871 extend to the entire whole, and thus to each and every part
6872 regardless of who wrote it.
6873
6874 Thus, it is not the intent of this section to claim rights or
6875 contest your rights to work written entirely by you; rather, the
6876 intent is to exercise the right to control the distribution of
6877 derivative or collective works based on the Library.
6878
6879 In addition, mere aggregation of another work not based on the
6880 Library with the Library (or with a work based on the Library) on a
6881 volume of a storage or distribution medium does not bring the other
6882 work under the scope of this License.
6883
6884 3. You may opt to apply the terms of the ordinary GNU General Public
6885 License instead of this License to a given copy of the Library. To
6886 do this, you must alter all the notices that refer to this License,
6887 so that they refer to the ordinary GNU General Public License,
6888 version 2, instead of to this License. (If a newer version than
6889 version 2 of the ordinary GNU General Public License has appeared,
6890 then you can specify that version instead if you wish.) Do not
6891 make any other change in these notices.
6892
6893 Once this change is made in a given copy, it is irreversible for
6894 that copy, so the ordinary GNU General Public License applies to
6895 all subsequent copies and derivative works made from that copy.
6896
6897 This option is useful when you wish to copy part of the code of the
6898 Library into a program that is not a library.
6899
6900 4. You may copy and distribute the Library (or a portion or derivative
6901 of it, under Section 2) in object code or executable form under the
6902 terms of Sections 1 and 2 above provided that you accompany it with
6903 the complete corresponding machine-readable source code, which must
6904 be distributed under the terms of Sections 1 and 2 above on a
6905 medium customarily used for software interchange.
6906
6907 If distribution of object code is made by offering access to copy
6908 from a designated place, then offering equivalent access to copy
6909 the source code from the same place satisfies the requirement to
6910 distribute the source code, even though third parties are not
6911 compelled to copy the source along with the object code.
6912
6913 5. A program that contains no derivative of any portion of the
6914 Library, but is designed to work with the Library by being compiled
6915 or linked with it, is called a "work that uses the Library". Such
6916 a work, in isolation, is not a derivative work of the Library, and
6917 therefore falls outside the scope of this License.
6918
6919 However, linking a "work that uses the Library" with the Library
6920 creates an executable that is a derivative of the Library (because
6921 it contains portions of the Library), rather than a "work that uses
6922 the library". The executable is therefore covered by this License.
6923 Section 6 states terms for distribution of such executables.
6924
6925 When a "work that uses the Library" uses material from a header
6926 file that is part of the Library, the object code for the work may
6927 be a derivative work of the Library even though the source code is
6928 not. Whether this is true is especially significant if the work
6929 can be linked without the Library, or if the work is itself a
6930 library. The threshold for this to be true is not precisely
6931 defined by law.
6932
6933 If such an object file uses only numerical parameters, data
6934 structure layouts and accessors, and small macros and small inline
6935 functions (ten lines or less in length), then the use of the object
6936 file is unrestricted, regardless of whether it is legally a
6937 derivative work. (Executables containing this object code plus
6938 portions of the Library will still fall under Section 6.)
6939
6940 Otherwise, if the work is a derivative of the Library, you may
6941 distribute the object code for the work under the terms of Section
6942 6. Any executables containing that work also fall under Section 6,
6943 whether or not they are linked directly with the Library itself.
6944
6945 6. As an exception to the Sections above, you may also combine or link
6946 a "work that uses the Library" with the Library to produce a work
6947 containing portions of the Library, and distribute that work under
6948 terms of your choice, provided that the terms permit modification
6949 of the work for the customer's own use and reverse engineering for
6950 debugging such modifications.
6951
6952 You must give prominent notice with each copy of the work that the
6953 Library is used in it and that the Library and its use are covered
6954 by this License. You must supply a copy of this License. If the
6955 work during execution displays copyright notices, you must include
6956 the copyright notice for the Library among them, as well as a
6957 reference directing the user to the copy of this License. Also,
6958 you must do one of these things:
6959
6960 a. Accompany the work with the complete corresponding
6961 machine-readable source code for the Library including
6962 whatever changes were used in the work (which must be
6963 distributed under Sections 1 and 2 above); and, if the work is
6964 an executable linked with the Library, with the complete
6965 machine-readable "work that uses the Library", as object code
6966 and/or source code, so that the user can modify the Library
6967 and then relink to produce a modified executable containing
6968 the modified Library. (It is understood that the user who
6969 changes the contents of definitions files in the Library will
6970 not necessarily be able to recompile the application to use
6971 the modified definitions.)
6972
6973 b. Use a suitable shared library mechanism for linking with the
6974 Library. A suitable mechanism is one that (1) uses at run
6975 time a copy of the library already present on the user's
6976 computer system, rather than copying library functions into
6977 the executable, and (2) will operate properly with a modified
6978 version of the library, if the user installs one, as long as
6979 the modified version is interface-compatible with the version
6980 that the work was made with.
6981
6982 c. Accompany the work with a written offer, valid for at least
6983 three years, to give the same user the materials specified in
6984 Subsection 6a, above, for a charge no more than the cost of
6985 performing this distribution.
6986
6987 d. If distribution of the work is made by offering access to copy
6988 from a designated place, offer equivalent access to copy the
6989 above specified materials from the same place.
6990
6991 e. Verify that the user has already received a copy of these
6992 materials or that you have already sent this user a copy.
6993
6994 For an executable, the required form of the "work that uses the
6995 Library" must include any data and utility programs needed for
6996 reproducing the executable from it. However, as a special
6997 exception, the materials to be distributed need not include
6998 anything that is normally distributed (in either source or binary
6999 form) with the major components (compiler, kernel, and so on) of
7000 the operating system on which the executable runs, unless that
7001 component itself accompanies the executable.
7002
7003 It may happen that this requirement contradicts the license
7004 restrictions of other proprietary libraries that do not normally
7005 accompany the operating system. Such a contradiction means you
7006 cannot use both them and the Library together in an executable that
7007 you distribute.
7008
7009 7. You may place library facilities that are a work based on the
7010 Library side-by-side in a single library together with other
7011 library facilities not covered by this License, and distribute such
7012 a combined library, provided that the separate distribution of the
7013 work based on the Library and of the other library facilities is
7014 otherwise permitted, and provided that you do these two things:
7015
7016 a. Accompany the combined library with a copy of the same work
7017 based on the Library, uncombined with any other library
7018 facilities. This must be distributed under the terms of the
7019 Sections above.
7020
7021 b. Give prominent notice with the combined library of the fact
7022 that part of it is a work based on the Library, and explaining
7023 where to find the accompanying uncombined form of the same
7024 work.
7025
7026 8. You may not copy, modify, sublicense, link with, or distribute the
7027 Library except as expressly provided under this License. Any
7028 attempt otherwise to copy, modify, sublicense, link with, or
7029 distribute the Library is void, and will automatically terminate
7030 your rights under this License. However, parties who have received
7031 copies, or rights, from you under this License will not have their
7032 licenses terminated so long as such parties remain in full
7033 compliance.
7034
7035 9. You are not required to accept this License, since you have not
7036 signed it. However, nothing else grants you permission to modify
7037 or distribute the Library or its derivative works. These actions
7038 are prohibited by law if you do not accept this License.
7039 Therefore, by modifying or distributing the Library (or any work
7040 based on the Library), you indicate your acceptance of this License
7041 to do so, and all its terms and conditions for copying,
7042 distributing or modifying the Library or works based on it.
7043
7044 10. Each time you redistribute the Library (or any work based on the
7045 Library), the recipient automatically receives a license from the
7046 original licensor to copy, distribute, link with or modify the
7047 Library subject to these terms and conditions. You may not impose
7048 any further restrictions on the recipients' exercise of the rights
7049 granted herein. You are not responsible for enforcing compliance
7050 by third parties with this License.
7051
7052 11. If, as a consequence of a court judgment or allegation of patent
7053 infringement or for any other reason (not limited to patent
7054 issues), conditions are imposed on you (whether by court order,
7055 agreement or otherwise) that contradict the conditions of this
7056 License, they do not excuse you from the conditions of this
7057 License. If you cannot distribute so as to satisfy simultaneously
7058 your obligations under this License and any other pertinent
7059 obligations, then as a consequence you may not distribute the
7060 Library at all. For example, if a patent license would not permit
7061 royalty-free redistribution of the Library by all those who receive
7062 copies directly or indirectly through you, then the only way you
7063 could satisfy both it and this License would be to refrain entirely
7064 from distribution of the Library.
7065
7066 If any portion of this section is held invalid or unenforceable
7067 under any particular circumstance, the balance of the section is
7068 intended to apply, and the section as a whole is intended to apply
7069 in other circumstances.
7070
7071 It is not the purpose of this section to induce you to infringe any
7072 patents or other property right claims or to contest validity of
7073 any such claims; this section has the sole purpose of protecting
7074 the integrity of the free software distribution system which is
7075 implemented by public license practices. Many people have made
7076 generous contributions to the wide range of software distributed
7077 through that system in reliance on consistent application of that
7078 system; it is up to the author/donor to decide if he or she is
7079 willing to distribute software through any other system and a
7080 licensee cannot impose that choice.
7081
7082 This section is intended to make thoroughly clear what is believed
7083 to be a consequence of the rest of this License.
7084
7085 12. If the distribution and/or use of the Library is restricted in
7086 certain countries either by patents or by copyrighted interfaces,
7087 the original copyright holder who places the Library under this
7088 License may add an explicit geographical distribution limitation
7089 excluding those countries, so that distribution is permitted only
7090 in or among countries not thus excluded. In such case, this
7091 License incorporates the limitation as if written in the body of
7092 this License.
7093
7094 13. The Free Software Foundation may publish revised and/or new
7095 versions of the Lesser General Public License from time to time.
7096 Such new versions will be similar in spirit to the present version,
7097 but may differ in detail to address new problems or concerns.
7098
7099 Each version is given a distinguishing version number. If the
7100 Library specifies a version number of this License which applies to
7101 it and "any later version", you have the option of following the
7102 terms and conditions either of that version or of any later version
7103 published by the Free Software Foundation. If the Library does not
7104 specify a license version number, you may choose any version ever
7105 published by the Free Software Foundation.
7106
7107 14. If you wish to incorporate parts of the Library into other free
7108 programs whose distribution conditions are incompatible with these,
7109 write to the author to ask for permission. For software which is
7110 copyrighted by the Free Software Foundation, write to the Free
7111 Software Foundation; we sometimes make exceptions for this. Our
7112 decision will be guided by the two goals of preserving the free
7113 status of all derivatives of our free software and of promoting the
7114 sharing and reuse of software generally.
7115
7116 NO WARRANTY
7117
7118 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
7119 WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE
7120 LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS
7121 AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY
7122 OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
7123 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
7124 FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
7125 PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE
7126 DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR
7127 OR CORRECTION.
7128
7129 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
7130 WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY
7131 MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE
7132 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
7133 INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
7134 INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
7135 DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU
7136 OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY
7137 OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
7138 ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
7139
7140 END OF TERMS AND CONDITIONS
7141 ---------------------------
7142
7143 How to Apply These Terms to Your New Libraries
7144 ----------------------------------------------
7145
7146 If you develop a new library, and you want it to be of the greatest
7147 possible use to the public, we recommend making it free software that
7148 everyone can redistribute and change. You can do so by permitting
7149 redistribution under these terms (or, alternatively, under the terms of
7150 the ordinary General Public License).
7151
7152 To apply these terms, attach the following notices to the library.
7153 It is safest to attach them to the start of each source file to most
7154 effectively convey the exclusion of warranty; and each file should have
7155 at least the "copyright" line and a pointer to where the full notice is
7156 found.
7157
7158 ONE LINE TO GIVE THE LIBRARY'S NAME AND AN IDEA OF WHAT IT DOES.
7159 Copyright (C) YEAR NAME OF AUTHOR
7160
7161 This library is free software; you can redistribute it and/or modify it
7162 under the terms of the GNU Lesser General Public License as published by
7163 the Free Software Foundation; either version 2.1 of the License, or (at
7164 your option) any later version.
7165
7166 This library is distributed in the hope that it will be useful, but
7167 WITHOUT ANY WARRANTY; without even the implied warranty of
7168 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7169 Lesser General Public License for more details.
7170
7171 You should have received a copy of the GNU Lesser General Public
7172 License along with this library; if not, see <https://www.gnu.org/licenses/>.
7173
7174 Also add information on how to contact you by electronic and paper
7175 mail.
7176
7177 You should also get your employer (if you work as a programmer) or
7178 your school, if any, to sign a "copyright disclaimer" for the library,
7179 if necessary. Here is a sample; alter the names:
7180
7181 Yoyodyne, Inc., hereby disclaims all copyright interest in the library
7182 `Frob' (a library for tweaking knobs) written by James Random Hacker.
7183
7184 SIGNATURE OF MOE GHOUL, 1 April 1990
7185 Moe Ghoul, President of Vice
7186
7187 That's all there is to it!
7188
7189 
7190 File: gcrypt.info, Node: Copying, Next: Figures and Tables, Prev: Library Copying, Up: Top
7191
7192 GNU General Public License
7193 **************************
7194
7195 Version 2, June 1991
7196
7197 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
7198 <https://fsf.org/>
7199
7200 Everyone is permitted to copy and distribute verbatim copies
7201 of this license document, but changing it is not allowed.
7202
7203 Preamble
7204 ========
7205
7206 The licenses for most software are designed to take away your freedom to
7207 share and change it. By contrast, the GNU General Public License is
7208 intended to guarantee your freedom to share and change free software--to
7209 make sure the software is free for all its users. This General Public
7210 License applies to most of the Free Software Foundation's software and
7211 to any other program whose authors commit to using it. (Some other Free
7212 Software Foundation software is covered by the GNU Lesser General Public
7213 License instead.) You can apply it to your programs, too.
7214
7215 When we speak of free software, we are referring to freedom, not
7216 price. Our General Public Licenses are designed to make sure that you
7217 have the freedom to distribute copies of free software (and charge for
7218 this service if you wish), that you receive source code or can get it if
7219 you want it, that you can change the software or use pieces of it in new
7220 free programs; and that you know you can do these things.
7221
7222 To protect your rights, we need to make restrictions that forbid
7223 anyone to deny you these rights or to ask you to surrender the rights.
7224 These restrictions translate to certain responsibilities for you if you
7225 distribute copies of the software, or if you modify it.
7226
7227 For example, if you distribute copies of such a program, whether
7228 gratis or for a fee, you must give the recipients all the rights that
7229 you have. You must make sure that they, too, receive or can get the
7230 source code. And you must show them these terms so they know their
7231 rights.
7232
7233 We protect your rights with two steps: (1) copyright the software,
7234 and (2) offer you this license which gives you legal permission to copy,
7235 distribute and/or modify the software.
7236
7237 Also, for each author's protection and ours, we want to make certain
7238 that everyone understands that there is no warranty for this free
7239 software. If the software is modified by someone else and passed on, we
7240 want its recipients to know that what they have is not the original, so
7241 that any problems introduced by others will not reflect on the original
7242 authors' reputations.
7243
7244 Finally, any free program is threatened constantly by software
7245 patents. We wish to avoid the danger that redistributors of a free
7246 program will individually obtain patent licenses, in effect making the
7247 program proprietary. To prevent this, we have made it clear that any
7248 patent must be licensed for everyone's free use or not licensed at all.
7249
7250 The precise terms and conditions for copying, distribution and
7251 modification follow.
7252
7253 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7254 ===============================================================
7255
7256 0. This License applies to any program or other work which contains a
7257 notice placed by the copyright holder saying it may be distributed
7258 under the terms of this General Public License. The "Program",
7259 below, refers to any such program or work, and a "work based on the
7260 Program" means either the Program or any derivative work under
7261 copyright law: that is to say, a work containing the Program or a
7262 portion of it, either verbatim or with modifications and/or
7263 translated into another language. (Hereinafter, translation is
7264 included without limitation in the term "modification".) Each
7265 licensee is addressed as "you".
7266
7267 Activities other than copying, distribution and modification are
7268 not covered by this License; they are outside its scope. The act
7269 of running the Program is not restricted, and the output from the
7270 Program is covered only if its contents constitute a work based on
7271 the Program (independent of having been made by running the
7272 Program). Whether that is true depends on what the Program does.
7273
7274 1. You may copy and distribute verbatim copies of the Program's source
7275 code as you receive it, in any medium, provided that you
7276 conspicuously and appropriately publish on each copy an appropriate
7277 copyright notice and disclaimer of warranty; keep intact all the
7278 notices that refer to this License and to the absence of any
7279 warranty; and give any other recipients of the Program a copy of
7280 this License along with the Program.
7281
7282 You may charge a fee for the physical act of transferring a copy,
7283 and you may at your option offer warranty protection in exchange
7284 for a fee.
7285
7286 2. You may modify your copy or copies of the Program or any portion of
7287 it, thus forming a work based on the Program, and copy and
7288 distribute such modifications or work under the terms of Section 1
7289 above, provided that you also meet all of these conditions:
7290
7291 a. You must cause the modified files to carry prominent notices
7292 stating that you changed the files and the date of any change.
7293
7294 b. You must cause any work that you distribute or publish, that
7295 in whole or in part contains or is derived from the Program or
7296 any part thereof, to be licensed as a whole at no charge to
7297 all third parties under the terms of this License.
7298
7299 c. If the modified program normally reads commands interactively
7300 when run, you must cause it, when started running for such
7301 interactive use in the most ordinary way, to print or display
7302 an announcement including an appropriate copyright notice and
7303 a notice that there is no warranty (or else, saying that you
7304 provide a warranty) and that users may redistribute the
7305 program under these conditions, and telling the user how to
7306 view a copy of this License. (Exception: if the Program
7307 itself is interactive but does not normally print such an
7308 announcement, your work based on the Program is not required
7309 to print an announcement.)
7310
7311 These requirements apply to the modified work as a whole. If
7312 identifiable sections of that work are not derived from the
7313 Program, and can be reasonably considered independent and separate
7314 works in themselves, then this License, and its terms, do not apply
7315 to those sections when you distribute them as separate works. But
7316 when you distribute the same sections as part of a whole which is a
7317 work based on the Program, the distribution of the whole must be on
7318 the terms of this License, whose permissions for other licensees
7319 extend to the entire whole, and thus to each and every part
7320 regardless of who wrote it.
7321
7322 Thus, it is not the intent of this section to claim rights or
7323 contest your rights to work written entirely by you; rather, the
7324 intent is to exercise the right to control the distribution of
7325 derivative or collective works based on the Program.
7326
7327 In addition, mere aggregation of another work not based on the
7328 Program with the Program (or with a work based on the Program) on a
7329 volume of a storage or distribution medium does not bring the other
7330 work under the scope of this License.
7331
7332 3. You may copy and distribute the Program (or a work based on it,
7333 under Section 2) in object code or executable form under the terms
7334 of Sections 1 and 2 above provided that you also do one of the
7335 following:
7336
7337 a. Accompany it with the complete corresponding machine-readable
7338 source code, which must be distributed under the terms of
7339 Sections 1 and 2 above on a medium customarily used for
7340 software interchange; or,
7341
7342 b. Accompany it with a written offer, valid for at least three
7343 years, to give any third party, for a charge no more than your
7344 cost of physically performing source distribution, a complete
7345 machine-readable copy of the corresponding source code, to be
7346 distributed under the terms of Sections 1 and 2 above on a
7347 medium customarily used for software interchange; or,
7348
7349 c. Accompany it with the information you received as to the offer
7350 to distribute corresponding source code. (This alternative is
7351 allowed only for noncommercial distribution and only if you
7352 received the program in object code or executable form with
7353 such an offer, in accord with Subsection b above.)
7354
7355 The source code for a work means the preferred form of the work for
7356 making modifications to it. For an executable work, complete
7357 source code means all the source code for all modules it contains,
7358 plus any associated interface definition files, plus the scripts
7359 used to control compilation and installation of the executable.
7360 However, as a special exception, the source code distributed need
7361 not include anything that is normally distributed (in either source
7362 or binary form) with the major components (compiler, kernel, and so
7363 on) of the operating system on which the executable runs, unless
7364 that component itself accompanies the executable.
7365
7366 If distribution of executable or object code is made by offering
7367 access to copy from a designated place, then offering equivalent
7368 access to copy the source code from the same place counts as
7369 distribution of the source code, even though third parties are not
7370 compelled to copy the source along with the object code.
7371
7372 4. You may not copy, modify, sublicense, or distribute the Program
7373 except as expressly provided under this License. Any attempt
7374 otherwise to copy, modify, sublicense or distribute the Program is
7375 void, and will automatically terminate your rights under this
7376 License. However, parties who have received copies, or rights,
7377 from you under this License will not have their licenses terminated
7378 so long as such parties remain in full compliance.
7379
7380 5. You are not required to accept this License, since you have not
7381 signed it. However, nothing else grants you permission to modify
7382 or distribute the Program or its derivative works. These actions
7383 are prohibited by law if you do not accept this License.
7384 Therefore, by modifying or distributing the Program (or any work
7385 based on the Program), you indicate your acceptance of this License
7386 to do so, and all its terms and conditions for copying,
7387 distributing or modifying the Program or works based on it.
7388
7389 6. Each time you redistribute the Program (or any work based on the
7390 Program), the recipient automatically receives a license from the
7391 original licensor to copy, distribute or modify the Program subject
7392 to these terms and conditions. You may not impose any further
7393 restrictions on the recipients' exercise of the rights granted
7394 herein. You are not responsible for enforcing compliance by third
7395 parties to this License.
7396
7397 7. If, as a consequence of a court judgment or allegation of patent
7398 infringement or for any other reason (not limited to patent
7399 issues), conditions are imposed on you (whether by court order,
7400 agreement or otherwise) that contradict the conditions of this
7401 License, they do not excuse you from the conditions of this
7402 License. If you cannot distribute so as to satisfy simultaneously
7403 your obligations under this License and any other pertinent
7404 obligations, then as a consequence you may not distribute the
7405 Program at all. For example, if a patent license would not permit
7406 royalty-free redistribution of the Program by all those who receive
7407 copies directly or indirectly through you, then the only way you
7408 could satisfy both it and this License would be to refrain entirely
7409 from distribution of the Program.
7410
7411 If any portion of this section is held invalid or unenforceable
7412 under any particular circumstance, the balance of the section is
7413 intended to apply and the section as a whole is intended to apply
7414 in other circumstances.
7415
7416 It is not the purpose of this section to induce you to infringe any
7417 patents or other property right claims or to contest validity of
7418 any such claims; this section has the sole purpose of protecting
7419 the integrity of the free software distribution system, which is
7420 implemented by public license practices. Many people have made
7421 generous contributions to the wide range of software distributed
7422 through that system in reliance on consistent application of that
7423 system; it is up to the author/donor to decide if he or she is
7424 willing to distribute software through any other system and a
7425 licensee cannot impose that choice.
7426
7427 This section is intended to make thoroughly clear what is believed
7428 to be a consequence of the rest of this License.
7429
7430 8. If the distribution and/or use of the Program is restricted in
7431 certain countries either by patents or by copyrighted interfaces,
7432 the original copyright holder who places the Program under this
7433 License may add an explicit geographical distribution limitation
7434 excluding those countries, so that distribution is permitted only
7435 in or among countries not thus excluded. In such case, this
7436 License incorporates the limitation as if written in the body of
7437 this License.
7438
7439 9. The Free Software Foundation may publish revised and/or new
7440 versions of the General Public License from time to time. Such new
7441 versions will be similar in spirit to the present version, but may
7442 differ in detail to address new problems or concerns.
7443
7444 Each version is given a distinguishing version number. If the
7445 Program specifies a version number of this License which applies to
7446 it and "any later version", you have the option of following the
7447 terms and conditions either of that version or of any later version
7448 published by the Free Software Foundation. If the Program does not
7449 specify a version number of this License, you may choose any
7450 version ever published by the Free Software Foundation.
7451
7452 10. If you wish to incorporate parts of the Program into other free
7453 programs whose distribution conditions are different, write to the
7454 author to ask for permission. For software which is copyrighted by
7455 the Free Software Foundation, write to the Free Software
7456 Foundation; we sometimes make exceptions for this. Our decision
7457 will be guided by the two goals of preserving the free status of
7458 all derivatives of our free software and of promoting the sharing
7459 and reuse of software generally.
7460
7461 NO WARRANTY
7462
7463 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
7464 WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE
7465 LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS
7466 AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
7467 OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
7468 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
7469 FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
7470 PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
7471 DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR
7472 OR CORRECTION.
7473
7474 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
7475 WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY
7476 MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
7477 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
7478 INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
7479 INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
7480 DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU
7481 OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY
7482 OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
7483 ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
7484
7485 END OF TERMS AND CONDITIONS
7486
7487 Appendix: How to Apply These Terms to Your New Programs
7488 =======================================================
7489
7490 If you develop a new program, and you want it to be of the greatest
7491 possible use to the public, the best way to achieve this is to make it
7492 free software which everyone can redistribute and change under these
7493 terms.
7494
7495 To do so, attach the following notices to the program. It is safest
7496 to attach them to the start of each source file to most effectively
7497 convey the exclusion of warranty; and each file should have at least the
7498 "copyright" line and a pointer to where the full notice is found.
7499
7500 ONE LINE TO GIVE THE PROGRAM'S NAME AND A BRIEF IDEA OF WHAT IT DOES.
7501 Copyright (C) YYYY NAME OF AUTHOR
7502
7503 This program is free software; you can redistribute it and/or modify
7504 it under the terms of the GNU General Public License as published by
7505 the Free Software Foundation; either version 2 of the License, or
7506 (at your option) any later version.
7507
7508 This program is distributed in the hope that it will be useful,
7509 but WITHOUT ANY WARRANTY; without even the implied warranty of
7510 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7511 GNU General Public License for more details.
7512
7513 You should have received a copy of the GNU General Public License
7514 along with this program; if not, see <https://www.gnu.org/licenses/>.
7515
7516 Also add information on how to contact you by electronic and paper
7517 mail.
7518
7519 If the program is interactive, make it output a short notice like
7520 this when it starts in an interactive mode:
7521
7522 Gnomovision version 69, Copyright (C) YEAR NAME OF AUTHOR
7523 Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
7524 This is free software, and you are welcome to redistribute it
7525 under certain conditions; type `show c' for details.
7526
7527 The hypothetical commands 'show w' and 'show c' should show the
7528 appropriate parts of the General Public License. Of course, the
7529 commands you use may be called something other than 'show w' and 'show
7530 c'; they could even be mouse-clicks or menu items--whatever suits your
7531 program.
7532
7533 You should also get your employer (if you work as a programmer) or
7534 your school, if any, to sign a "copyright disclaimer" for the program,
7535 if necessary. Here is a sample; alter the names:
7536
7537 Yoyodyne, Inc., hereby disclaims all copyright interest in the program
7538 `Gnomovision' (which makes passes at compilers) written by James Hacker.
7539
7540 SIGNATURE OF MOE GHOUL, 1 April 1989
7541 Moe Ghoul, President of Vice
7542
7543 This General Public License does not permit incorporating your
7544 program into proprietary programs. If your program is a subroutine
7545 library, you may consider it more useful to permit linking proprietary
7546 applications with the library. If this is what you want to do, use the
7547 GNU Lesser General Public License instead of this License.
7548