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