gcrypt.h (75964B)
1 /* gcrypt.h - GNU Cryptographic Library Interface -*- c -*- 2 * Copyright (C) 2012-2023 g10 Code GmbH 3 * Copyright (C) 2013-2023 Jussi Kivilinna 4 * Copyright (C) 1998-2018 Free Software Foundation, Inc. 5 * 6 * This file is part of Libgcrypt. 7 * 8 * Libgcrypt is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU Lesser General Public License as 10 * published by the Free Software Foundation; either version 2.1 of 11 * the License, or (at your option) any later version. 12 * 13 * Libgcrypt is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this program; if not, see <http://www.gnu.org/licenses/>. 20 * 21 * File: src/gcrypt.h. Generated from gcrypt.h.in by configure. 22 */ 23 24 #ifndef _GCRYPT_H 25 #define _GCRYPT_H 26 27 #include <stdlib.h> 28 #include <stdarg.h> 29 #include <string.h> 30 31 #include <gpg-error.h> 32 33 #include <sys/types.h> 34 35 #if defined _WIN32 || defined __WIN32__ 36 # ifndef __GNUC__ 37 typedef long ssize_t; 38 typedef int pid_t; 39 # endif /*!__GNUC__*/ 40 #endif /*_WIN32*/ 41 42 /* This is required for error code compatibility. */ 43 #define _GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_GCRYPT 44 45 #ifdef __cplusplus 46 extern "C" { 47 #if 0 /* (Keep Emacsens' auto-indent happy.) */ 48 } 49 #endif 50 #endif 51 52 /* The version of this header should match the one of the library. It 53 should not be used by a program because gcry_check_version() should 54 return the same version. The purpose of this macro is to let 55 autoconf (using the AM_PATH_GCRYPT macro) check that this header 56 matches the installed library. */ 57 #define GCRYPT_VERSION "1.10.3" 58 59 /* The version number of this header. It may be used to handle minor 60 API incompatibilities. */ 61 #define GCRYPT_VERSION_NUMBER 0x010a03 62 63 64 /* Internal: We can't use the convenience macros for the multi 65 precision integer functions when building this library. */ 66 #ifdef _GCRYPT_IN_LIBGCRYPT 67 #ifndef GCRYPT_NO_MPI_MACROS 68 #define GCRYPT_NO_MPI_MACROS 1 69 #endif 70 #endif 71 72 /* We want to use gcc attributes when possible. Warning: Don't use 73 these macros in your programs: As indicated by the leading 74 underscore they are subject to change without notice. */ 75 #ifdef __GNUC__ 76 77 #define _GCRY_GCC_VERSION (__GNUC__ * 10000 \ 78 + __GNUC_MINOR__ * 100 \ 79 + __GNUC_PATCHLEVEL__) 80 81 #if _GCRY_GCC_VERSION >= 30100 82 #define _GCRY_GCC_ATTR_DEPRECATED __attribute__ ((__deprecated__)) 83 #endif 84 85 #if _GCRY_GCC_VERSION >= 29600 86 #define _GCRY_GCC_ATTR_PURE __attribute__ ((__pure__)) 87 #endif 88 89 #if _GCRY_GCC_VERSION >= 30200 90 #define _GCRY_GCC_ATTR_MALLOC __attribute__ ((__malloc__)) 91 #endif 92 93 #define _GCRY_GCC_ATTR_PRINTF(f,a) __attribute__ ((format (printf,f,a))) 94 95 #if _GCRY_GCC_VERSION >= 40000 96 #define _GCRY_GCC_ATTR_SENTINEL(a) __attribute__ ((sentinel(a))) 97 #endif 98 99 #endif /*__GNUC__*/ 100 101 #ifndef _GCRY_GCC_ATTR_DEPRECATED 102 #define _GCRY_GCC_ATTR_DEPRECATED 103 #endif 104 #ifndef _GCRY_GCC_ATTR_PURE 105 #define _GCRY_GCC_ATTR_PURE 106 #endif 107 #ifndef _GCRY_GCC_ATTR_MALLOC 108 #define _GCRY_GCC_ATTR_MALLOC 109 #endif 110 #ifndef _GCRY_GCC_ATTR_PRINTF 111 #define _GCRY_GCC_ATTR_PRINTF(f,a) 112 #endif 113 #ifndef _GCRY_GCC_ATTR_SENTINEL 114 #define _GCRY_GCC_ATTR_SENTINEL(a) 115 #endif 116 117 /* Make up an attribute to mark functions and types as deprecated but 118 allow internal use by Libgcrypt. */ 119 #ifdef _GCRYPT_IN_LIBGCRYPT 120 #define _GCRY_ATTR_INTERNAL 121 #else 122 #define _GCRY_ATTR_INTERNAL _GCRY_GCC_ATTR_DEPRECATED 123 #endif 124 125 /* Wrappers for the libgpg-error library. */ 126 127 typedef gpg_error_t gcry_error_t; 128 typedef gpg_err_code_t gcry_err_code_t; 129 typedef gpg_err_source_t gcry_err_source_t; 130 131 static GPG_ERR_INLINE gcry_error_t 132 gcry_err_make (gcry_err_source_t source, gcry_err_code_t code) 133 { 134 return gpg_err_make (source, code); 135 } 136 137 /* The user can define GPG_ERR_SOURCE_DEFAULT before including this 138 file to specify a default source for gpg_error. */ 139 #ifndef GCRY_ERR_SOURCE_DEFAULT 140 #define GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_1 141 #endif 142 143 static GPG_ERR_INLINE gcry_error_t 144 gcry_error (gcry_err_code_t code) 145 { 146 return gcry_err_make (GCRY_ERR_SOURCE_DEFAULT, code); 147 } 148 149 static GPG_ERR_INLINE gcry_err_code_t 150 gcry_err_code (gcry_error_t err) 151 { 152 return gpg_err_code (err); 153 } 154 155 156 static GPG_ERR_INLINE gcry_err_source_t 157 gcry_err_source (gcry_error_t err) 158 { 159 return gpg_err_source (err); 160 } 161 162 /* Return a pointer to a string containing a description of the error 163 code in the error value ERR. */ 164 const char *gcry_strerror (gcry_error_t err); 165 166 /* Return a pointer to a string containing a description of the error 167 source in the error value ERR. */ 168 const char *gcry_strsource (gcry_error_t err); 169 170 /* Retrieve the error code for the system error ERR. This returns 171 GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report 172 this). */ 173 gcry_err_code_t gcry_err_code_from_errno (int err); 174 175 /* Retrieve the system error for the error code CODE. This returns 0 176 if CODE is not a system error code. */ 177 int gcry_err_code_to_errno (gcry_err_code_t code); 178 179 /* Return an error value with the error source SOURCE and the system 180 error ERR. */ 181 gcry_error_t gcry_err_make_from_errno (gcry_err_source_t source, int err); 182 183 /* Return an error value with the system error ERR. */ 184 gcry_error_t gcry_error_from_errno (int err); 185 186 187 /* NOTE: Since Libgcrypt 1.6 the thread callbacks are not anymore 188 used. However we keep it to allow for some source code 189 compatibility if used in the standard way. */ 190 191 /* Constants defining the thread model to use. Used with the OPTION 192 field of the struct gcry_thread_cbs. */ 193 #define GCRY_THREAD_OPTION_DEFAULT 0 194 #define GCRY_THREAD_OPTION_USER 1 195 #define GCRY_THREAD_OPTION_PTH 2 196 #define GCRY_THREAD_OPTION_PTHREAD 3 197 198 /* The version number encoded in the OPTION field of the struct 199 gcry_thread_cbs. */ 200 #define GCRY_THREAD_OPTION_VERSION 1 201 202 /* Wrapper for struct ath_ops. */ 203 struct gcry_thread_cbs 204 { 205 /* The OPTION field encodes the thread model and the version number 206 of this structure. 207 Bits 7 - 0 are used for the thread model 208 Bits 15 - 8 are used for the version number. */ 209 unsigned int option; 210 } _GCRY_ATTR_INTERNAL; 211 212 #define GCRY_THREAD_OPTION_PTH_IMPL \ 213 static struct gcry_thread_cbs gcry_threads_pth = { \ 214 (GCRY_THREAD_OPTION_PTH | (GCRY_THREAD_OPTION_VERSION << 8))} 215 216 #define GCRY_THREAD_OPTION_PTHREAD_IMPL \ 217 static struct gcry_thread_cbs gcry_threads_pthread = { \ 218 (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8))} 219 220 221 222 /* A generic context object as used by some functions. */ 223 struct gcry_context; 224 typedef struct gcry_context *gcry_ctx_t; 225 226 /* The data objects used to hold multi precision integers. */ 227 struct gcry_mpi; 228 typedef struct gcry_mpi *gcry_mpi_t; 229 struct gcry_mpi_point; 230 typedef struct gcry_mpi_point *gcry_mpi_point_t; 231 232 #ifndef GCRYPT_NO_DEPRECATED 233 typedef struct gcry_mpi *GCRY_MPI _GCRY_GCC_ATTR_DEPRECATED; 234 typedef struct gcry_mpi *GcryMPI _GCRY_GCC_ATTR_DEPRECATED; 235 #endif 236 237 /* A structure used for scatter gather hashing. */ 238 typedef struct 239 { 240 size_t size; /* The allocated size of the buffer or 0. */ 241 size_t off; /* Offset into the buffer. */ 242 size_t len; /* The used length of the buffer. */ 243 void *data; /* The buffer. */ 244 } gcry_buffer_t; 245 246 247 248 249 /* Check that the library fulfills the version requirement. */ 250 const char *gcry_check_version (const char *req_version); 251 252 /* Codes for function dispatchers. */ 253 254 /* Codes used with the gcry_control function. */ 255 enum gcry_ctl_cmds 256 { 257 /* Note: 1 .. 2 are not anymore used. */ 258 GCRYCTL_CFB_SYNC = 3, 259 GCRYCTL_RESET = 4, /* e.g. for MDs */ 260 GCRYCTL_FINALIZE = 5, 261 GCRYCTL_GET_KEYLEN = 6, 262 GCRYCTL_GET_BLKLEN = 7, 263 GCRYCTL_TEST_ALGO = 8, 264 GCRYCTL_IS_SECURE = 9, 265 GCRYCTL_GET_ASNOID = 10, 266 GCRYCTL_ENABLE_ALGO = 11, 267 GCRYCTL_DISABLE_ALGO = 12, 268 GCRYCTL_DUMP_RANDOM_STATS = 13, 269 GCRYCTL_DUMP_SECMEM_STATS = 14, 270 GCRYCTL_GET_ALGO_NPKEY = 15, 271 GCRYCTL_GET_ALGO_NSKEY = 16, 272 GCRYCTL_GET_ALGO_NSIGN = 17, 273 GCRYCTL_GET_ALGO_NENCR = 18, 274 GCRYCTL_SET_VERBOSITY = 19, 275 GCRYCTL_SET_DEBUG_FLAGS = 20, 276 GCRYCTL_CLEAR_DEBUG_FLAGS = 21, 277 GCRYCTL_USE_SECURE_RNDPOOL= 22, 278 GCRYCTL_DUMP_MEMORY_STATS = 23, 279 GCRYCTL_INIT_SECMEM = 24, 280 GCRYCTL_TERM_SECMEM = 25, 281 GCRYCTL_DISABLE_SECMEM_WARN = 27, 282 GCRYCTL_SUSPEND_SECMEM_WARN = 28, 283 GCRYCTL_RESUME_SECMEM_WARN = 29, 284 GCRYCTL_DROP_PRIVS = 30, 285 GCRYCTL_ENABLE_M_GUARD = 31, 286 GCRYCTL_START_DUMP = 32, 287 GCRYCTL_STOP_DUMP = 33, 288 GCRYCTL_GET_ALGO_USAGE = 34, 289 GCRYCTL_IS_ALGO_ENABLED = 35, 290 GCRYCTL_DISABLE_INTERNAL_LOCKING = 36, 291 GCRYCTL_DISABLE_SECMEM = 37, 292 GCRYCTL_INITIALIZATION_FINISHED = 38, 293 GCRYCTL_INITIALIZATION_FINISHED_P = 39, 294 GCRYCTL_ANY_INITIALIZATION_P = 40, 295 GCRYCTL_SET_CBC_CTS = 41, 296 GCRYCTL_SET_CBC_MAC = 42, 297 /* Note: 43 is not anymore used. */ 298 GCRYCTL_ENABLE_QUICK_RANDOM = 44, 299 GCRYCTL_SET_RANDOM_SEED_FILE = 45, 300 GCRYCTL_UPDATE_RANDOM_SEED_FILE = 46, 301 GCRYCTL_SET_THREAD_CBS = 47, 302 GCRYCTL_FAST_POLL = 48, 303 GCRYCTL_SET_RANDOM_DAEMON_SOCKET = 49, 304 GCRYCTL_USE_RANDOM_DAEMON = 50, 305 GCRYCTL_FAKED_RANDOM_P = 51, 306 GCRYCTL_SET_RNDEGD_SOCKET = 52, 307 GCRYCTL_PRINT_CONFIG = 53, 308 GCRYCTL_OPERATIONAL_P = 54, 309 GCRYCTL_FIPS_MODE_P = 55, 310 GCRYCTL_FORCE_FIPS_MODE = 56, 311 GCRYCTL_SELFTEST = 57, 312 /* Note: 58 .. 62 are used internally. */ 313 GCRYCTL_DISABLE_HWF = 63, 314 GCRYCTL_SET_ENFORCED_FIPS_FLAG = 64, 315 GCRYCTL_SET_PREFERRED_RNG_TYPE = 65, 316 GCRYCTL_GET_CURRENT_RNG_TYPE = 66, 317 GCRYCTL_DISABLE_LOCKED_SECMEM = 67, 318 GCRYCTL_DISABLE_PRIV_DROP = 68, 319 GCRYCTL_SET_CCM_LENGTHS = 69, 320 GCRYCTL_CLOSE_RANDOM_DEVICE = 70, 321 GCRYCTL_INACTIVATE_FIPS_FLAG = 71, 322 GCRYCTL_REACTIVATE_FIPS_FLAG = 72, 323 GCRYCTL_SET_SBOX = 73, 324 GCRYCTL_DRBG_REINIT = 74, 325 GCRYCTL_SET_TAGLEN = 75, 326 GCRYCTL_GET_TAGLEN = 76, 327 GCRYCTL_REINIT_SYSCALL_CLAMP = 77, 328 GCRYCTL_AUTO_EXPAND_SECMEM = 78, 329 GCRYCTL_SET_ALLOW_WEAK_KEY = 79, 330 GCRYCTL_SET_DECRYPTION_TAG = 80, 331 GCRYCTL_FIPS_SERVICE_INDICATOR_CIPHER = 81, 332 GCRYCTL_FIPS_SERVICE_INDICATOR_KDF = 82, 333 GCRYCTL_NO_FIPS_MODE = 83, 334 GCRYCTL_FIPS_SERVICE_INDICATOR_FUNCTION = 84, 335 GCRYCTL_FIPS_SERVICE_INDICATOR_MAC = 85, 336 GCRYCTL_FIPS_SERVICE_INDICATOR_MD = 86, 337 GCRYCTL_FIPS_SERVICE_INDICATOR_PK_FLAGS = 87 338 }; 339 340 /* Perform various operations defined by CMD. */ 341 gcry_error_t gcry_control (enum gcry_ctl_cmds CMD, ...); 342 343 344 /* S-expression management. */ 345 346 /* The object to represent an S-expression as used with the public key 347 functions. */ 348 struct gcry_sexp; 349 typedef struct gcry_sexp *gcry_sexp_t; 350 351 #ifndef GCRYPT_NO_DEPRECATED 352 typedef struct gcry_sexp *GCRY_SEXP _GCRY_GCC_ATTR_DEPRECATED; 353 typedef struct gcry_sexp *GcrySexp _GCRY_GCC_ATTR_DEPRECATED; 354 #endif 355 356 /* The possible values for the S-expression format. */ 357 enum gcry_sexp_format 358 { 359 GCRYSEXP_FMT_DEFAULT = 0, 360 GCRYSEXP_FMT_CANON = 1, 361 GCRYSEXP_FMT_BASE64 = 2, 362 GCRYSEXP_FMT_ADVANCED = 3 363 }; 364 365 /* Create an new S-expression object from BUFFER of size LENGTH and 366 return it in RETSEXP. With AUTODETECT set to 0 the data in BUFFER 367 is expected to be in canonized format. */ 368 gcry_error_t gcry_sexp_new (gcry_sexp_t *retsexp, 369 const void *buffer, size_t length, 370 int autodetect); 371 372 /* Same as gcry_sexp_new but allows to pass a FREEFNC which has the 373 effect to transfer ownership of BUFFER to the created object. */ 374 gcry_error_t gcry_sexp_create (gcry_sexp_t *retsexp, 375 void *buffer, size_t length, 376 int autodetect, void (*freefnc) (void *)); 377 378 /* Scan BUFFER and return a new S-expression object in RETSEXP. This 379 function expects a printf like string in BUFFER. */ 380 gcry_error_t gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff, 381 const char *buffer, size_t length); 382 383 /* Same as gcry_sexp_sscan but expects a string in FORMAT and can thus 384 only be used for certain encodings. */ 385 gcry_error_t gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff, 386 const char *format, ...); 387 388 /* Like gcry_sexp_build, but uses an array instead of variable 389 function arguments. */ 390 gcry_error_t gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff, 391 const char *format, void **arg_list); 392 393 /* Release the S-expression object SEXP */ 394 void gcry_sexp_release (gcry_sexp_t sexp); 395 396 /* Calculate the length of an canonized S-expression in BUFFER and 397 check for a valid encoding. */ 398 size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length, 399 size_t *erroff, gcry_error_t *errcode); 400 401 /* Copies the S-expression object SEXP into BUFFER using the format 402 specified in MODE. */ 403 size_t gcry_sexp_sprint (gcry_sexp_t sexp, int mode, void *buffer, 404 size_t maxlength); 405 406 /* Dumps the S-expression object A in a format suitable for debugging 407 to Libgcrypt's logging stream. */ 408 void gcry_sexp_dump (const gcry_sexp_t a); 409 410 gcry_sexp_t gcry_sexp_cons (const gcry_sexp_t a, const gcry_sexp_t b); 411 gcry_sexp_t gcry_sexp_alist (const gcry_sexp_t *array); 412 gcry_sexp_t gcry_sexp_vlist (const gcry_sexp_t a, ...); 413 gcry_sexp_t gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n); 414 gcry_sexp_t gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n); 415 416 /* Scan the S-expression for a sublist with a type (the car of the 417 list) matching the string TOKEN. If TOKLEN is not 0, the token is 418 assumed to be raw memory of this length. The function returns a 419 newly allocated S-expression consisting of the found sublist or 420 `NULL' when not found. */ 421 gcry_sexp_t gcry_sexp_find_token (gcry_sexp_t list, 422 const char *tok, size_t toklen); 423 /* Return the length of the LIST. For a valid S-expression this 424 should be at least 1. */ 425 int gcry_sexp_length (const gcry_sexp_t list); 426 427 /* Create and return a new S-expression from the element with index 428 NUMBER in LIST. Note that the first element has the index 0. If 429 there is no such element, `NULL' is returned. */ 430 gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t list, int number); 431 432 /* Create and return a new S-expression from the first element in 433 LIST; this called the "type" and should always exist and be a 434 string. `NULL' is returned in case of a problem. */ 435 gcry_sexp_t gcry_sexp_car (const gcry_sexp_t list); 436 437 /* Create and return a new list form all elements except for the first 438 one. Note, that this function may return an invalid S-expression 439 because it is not guaranteed, that the type exists and is a string. 440 However, for parsing a complex S-expression it might be useful for 441 intermediate lists. Returns `NULL' on error. */ 442 gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t list); 443 444 gcry_sexp_t gcry_sexp_cadr (const gcry_sexp_t list); 445 446 447 /* This function is used to get data from a LIST. A pointer to the 448 actual data with index NUMBER is returned and the length of this 449 data will be stored to DATALEN. If there is no data at the given 450 index or the index represents another list, `NULL' is returned. 451 *Note:* The returned pointer is valid as long as LIST is not 452 modified or released. */ 453 const char *gcry_sexp_nth_data (const gcry_sexp_t list, int number, 454 size_t *datalen); 455 456 /* This function is used to get data from a LIST. A malloced buffer to the 457 data with index NUMBER is returned and the length of this 458 data will be stored to RLENGTH. If there is no data at the given 459 index or the index represents another list, `NULL' is returned. */ 460 void *gcry_sexp_nth_buffer (const gcry_sexp_t list, int number, 461 size_t *rlength); 462 463 /* This function is used to get and convert data from a LIST. The 464 data is assumed to be a Nul terminated string. The caller must 465 release the returned value using `gcry_free'. If there is no data 466 at the given index, the index represents a list or the value can't 467 be converted to a string, `NULL' is returned. */ 468 char *gcry_sexp_nth_string (gcry_sexp_t list, int number); 469 470 /* This function is used to get and convert data from a LIST. This 471 data is assumed to be an MPI stored in the format described by 472 MPIFMT and returned as a standard Libgcrypt MPI. The caller must 473 release this returned value using `gcry_mpi_release'. If there is 474 no data at the given index, the index represents a list or the 475 value can't be converted to an MPI, `NULL' is returned. */ 476 gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt); 477 478 /* Extract MPIs from an s-expression using a list of parameters. The 479 * names of these parameters are given by the string LIST. Some 480 * special characters may be given to control the conversion: 481 * 482 * + :: Switch to unsigned integer format (default). 483 * - :: Switch to standard signed format. 484 * / :: Switch to opaque format. 485 * & :: Switch to buffer descriptor mode - see below. 486 * ? :: The previous parameter is optional. 487 * 488 * In general parameter names are single letters. To use a string for 489 * a parameter name, enclose the name in single quotes. 490 * 491 * Unless in gcry_buffer_t mode for each parameter name a pointer to 492 * an MPI variable is expected that must be set to NULL prior to 493 * invoking this function, and finally a NULL is expected. Example: 494 * 495 * _gcry_sexp_extract_param (key, NULL, "n/x+ed", 496 * &mpi_n, &mpi_x, &mpi_e, NULL) 497 * 498 * This stores the parameter "N" from KEY as an unsigned MPI into 499 * MPI_N, the parameter "X" as an opaque MPI into MPI_X, and the 500 * parameter "E" again as an unsigned MPI into MPI_E. 501 * 502 * If in buffer descriptor mode a pointer to gcry_buffer_t descriptor 503 * is expected instead of a pointer to an MPI. The caller may use two 504 * different operation modes: If the DATA field of the provided buffer 505 * descriptor is NULL, the function allocates a new buffer and stores 506 * it at DATA; the other fields are set accordingly with OFF being 0. 507 * If DATA is not NULL, the function assumes that DATA, SIZE, and OFF 508 * describe a buffer where to but the data; on return the LEN field 509 * receives the number of bytes copied to that buffer; if the buffer 510 * is too small, the function immediately returns with an error code 511 * (and LEN set to 0). 512 * 513 * PATH is an optional string used to locate a token. The exclamation 514 * mark separated tokens are used to via gcry_sexp_find_token to find 515 * a start point inside SEXP. 516 * 517 * The function returns 0 on success. On error an error code is 518 * returned, all passed MPIs that might have been allocated up to this 519 * point are deallocated and set to NULL, and all passed buffers are 520 * either truncated if the caller supplied the buffer, or deallocated 521 * if the function allocated the buffer. 522 */ 523 gpg_error_t gcry_sexp_extract_param (gcry_sexp_t sexp, 524 const char *path, 525 const char *list, 526 ...) _GCRY_GCC_ATTR_SENTINEL(0); 527 528 529 /******************************************* 530 * * 531 * Multi Precision Integer Functions * 532 * * 533 *******************************************/ 534 535 /* Different formats of external big integer representation. */ 536 enum gcry_mpi_format 537 { 538 GCRYMPI_FMT_NONE= 0, 539 GCRYMPI_FMT_STD = 1, /* Twos complement stored without length. */ 540 GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP (unsigned only). */ 541 GCRYMPI_FMT_SSH = 3, /* As used by SSH (like STD but with length). */ 542 GCRYMPI_FMT_HEX = 4, /* Hex format. */ 543 GCRYMPI_FMT_USG = 5, /* Like STD but unsigned. */ 544 GCRYMPI_FMT_OPAQUE = 8 /* Opaque format (some functions only). */ 545 }; 546 547 /* Flags used for creating big integers. */ 548 enum gcry_mpi_flag 549 { 550 GCRYMPI_FLAG_SECURE = 1, /* Allocate the number in "secure" memory. */ 551 GCRYMPI_FLAG_OPAQUE = 2, /* The number is not a real one but just 552 a way to store some bytes. This is 553 useful for encrypted big integers. */ 554 GCRYMPI_FLAG_IMMUTABLE = 4, /* Mark the MPI as immutable. */ 555 GCRYMPI_FLAG_CONST = 8, /* Mark the MPI as a constant. */ 556 GCRYMPI_FLAG_USER1 = 0x0100,/* User flag 1. */ 557 GCRYMPI_FLAG_USER2 = 0x0200,/* User flag 2. */ 558 GCRYMPI_FLAG_USER3 = 0x0400,/* User flag 3. */ 559 GCRYMPI_FLAG_USER4 = 0x0800 /* User flag 4. */ 560 }; 561 562 563 /* Macros to return pre-defined MPI constants. */ 564 #define GCRYMPI_CONST_ONE (_gcry_mpi_get_const (1)) 565 #define GCRYMPI_CONST_TWO (_gcry_mpi_get_const (2)) 566 #define GCRYMPI_CONST_THREE (_gcry_mpi_get_const (3)) 567 #define GCRYMPI_CONST_FOUR (_gcry_mpi_get_const (4)) 568 #define GCRYMPI_CONST_EIGHT (_gcry_mpi_get_const (8)) 569 570 /* Allocate a new big integer object, initialize it with 0 and 571 initially allocate memory for a number of at least NBITS. */ 572 gcry_mpi_t gcry_mpi_new (unsigned int nbits); 573 574 /* Same as gcry_mpi_new() but allocate in "secure" memory. */ 575 gcry_mpi_t gcry_mpi_snew (unsigned int nbits); 576 577 /* Release the number A and free all associated resources. */ 578 void gcry_mpi_release (gcry_mpi_t a); 579 580 /* Create a new number with the same value as A. */ 581 gcry_mpi_t gcry_mpi_copy (const gcry_mpi_t a); 582 583 /* Store the big integer value U in W and release U. */ 584 void gcry_mpi_snatch (gcry_mpi_t w, gcry_mpi_t u); 585 586 /* Store the big integer value U in W. */ 587 gcry_mpi_t gcry_mpi_set (gcry_mpi_t w, const gcry_mpi_t u); 588 589 /* Store the unsigned integer value U in W. */ 590 gcry_mpi_t gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u); 591 592 /* Store U as an unsigned int at W or return GPG_ERR_ERANGE. */ 593 gpg_error_t gcry_mpi_get_ui (unsigned int *w, gcry_mpi_t u); 594 595 /* Swap the values of A and B. */ 596 void gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b); 597 598 /* Return 1 if A is negative; 0 if zero or positive. */ 599 int gcry_mpi_is_neg (gcry_mpi_t a); 600 601 /* W = - U */ 602 void gcry_mpi_neg (gcry_mpi_t w, gcry_mpi_t u); 603 604 /* W = [W] */ 605 void gcry_mpi_abs (gcry_mpi_t w); 606 607 /* Compare the big integer number U and V returning 0 for equality, a 608 positive value for U > V and a negative for U < V. */ 609 int gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v); 610 611 /* Compare the big integer number U with the unsigned integer V 612 returning 0 for equality, a positive value for U > V and a negative 613 for U < V. */ 614 int gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v); 615 616 /* Convert the external representation of an integer stored in BUFFER 617 with a length of BUFLEN into a newly create MPI returned in 618 RET_MPI. If NSCANNED is not NULL, it will receive the number of 619 bytes actually scanned after a successful operation. */ 620 gcry_error_t gcry_mpi_scan (gcry_mpi_t *ret_mpi, enum gcry_mpi_format format, 621 const void *buffer, size_t buflen, 622 size_t *nscanned); 623 624 /* Convert the big integer A into the external representation 625 described by FORMAT and store it in the provided BUFFER which has 626 been allocated by the user with a size of BUFLEN bytes. NWRITTEN 627 receives the actual length of the external representation unless it 628 has been passed as NULL. */ 629 gcry_error_t gcry_mpi_print (enum gcry_mpi_format format, 630 unsigned char *buffer, size_t buflen, 631 size_t *nwritten, 632 const gcry_mpi_t a); 633 634 /* Convert the big integer A into the external representation described 635 by FORMAT and store it in a newly allocated buffer which address 636 will be put into BUFFER. NWRITTEN receives the actual lengths of the 637 external representation. */ 638 gcry_error_t gcry_mpi_aprint (enum gcry_mpi_format format, 639 unsigned char **buffer, size_t *nwritten, 640 const gcry_mpi_t a); 641 642 /* Dump the value of A in a format suitable for debugging to 643 Libgcrypt's logging stream. Note that one leading space but no 644 trailing space or linefeed will be printed. It is okay to pass 645 NULL for A. */ 646 void gcry_mpi_dump (const gcry_mpi_t a); 647 648 649 /* W = U + V. */ 650 void gcry_mpi_add (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); 651 652 /* W = U + V. V is an unsigned integer. */ 653 void gcry_mpi_add_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v); 654 655 /* W = U + V mod M. */ 656 void gcry_mpi_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); 657 658 /* W = U - V. */ 659 void gcry_mpi_sub (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); 660 661 /* W = U - V. V is an unsigned integer. */ 662 void gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ); 663 664 /* W = U - V mod M */ 665 void gcry_mpi_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); 666 667 /* W = U * V. */ 668 void gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); 669 670 /* W = U * V. V is an unsigned integer. */ 671 void gcry_mpi_mul_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ); 672 673 /* W = U * V mod M. */ 674 void gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); 675 676 /* W = U * (2 ^ CNT). */ 677 void gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt); 678 679 /* Q = DIVIDEND / DIVISOR, R = DIVIDEND % DIVISOR, 680 Q or R may be passed as NULL. ROUND should be negative or 0. */ 681 void gcry_mpi_div (gcry_mpi_t q, gcry_mpi_t r, 682 gcry_mpi_t dividend, gcry_mpi_t divisor, int round); 683 684 /* R = DIVIDEND % DIVISOR */ 685 void gcry_mpi_mod (gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor); 686 687 /* W = B ^ E mod M. */ 688 void gcry_mpi_powm (gcry_mpi_t w, 689 const gcry_mpi_t b, const gcry_mpi_t e, 690 const gcry_mpi_t m); 691 692 /* Set G to the greatest common divisor of A and B. 693 Return true if the G is 1. */ 694 int gcry_mpi_gcd (gcry_mpi_t g, gcry_mpi_t a, gcry_mpi_t b); 695 696 /* Set X to the multiplicative inverse of A mod M. 697 Return true if the value exists. */ 698 int gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t m); 699 700 /* Create a new point object. NBITS is usually 0. */ 701 gcry_mpi_point_t gcry_mpi_point_new (unsigned int nbits); 702 703 /* Release the object POINT. POINT may be NULL. */ 704 void gcry_mpi_point_release (gcry_mpi_point_t point); 705 706 /* Return a copy of POINT. */ 707 gcry_mpi_point_t gcry_mpi_point_copy (gcry_mpi_point_t point); 708 709 /* Store the projective coordinates from POINT into X, Y, and Z. */ 710 void gcry_mpi_point_get (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z, 711 gcry_mpi_point_t point); 712 713 /* Store the projective coordinates from POINT into X, Y, and Z and 714 release POINT. */ 715 void gcry_mpi_point_snatch_get (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z, 716 gcry_mpi_point_t point); 717 718 /* Store the projective coordinates X, Y, and Z into POINT. */ 719 gcry_mpi_point_t gcry_mpi_point_set (gcry_mpi_point_t point, 720 gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z); 721 722 /* Store the projective coordinates X, Y, and Z into POINT and release 723 X, Y, and Z. */ 724 gcry_mpi_point_t gcry_mpi_point_snatch_set (gcry_mpi_point_t point, 725 gcry_mpi_t x, gcry_mpi_t y, 726 gcry_mpi_t z); 727 728 /* Allocate a new context for elliptic curve operations based on the 729 parameters given by KEYPARAM or using CURVENAME. */ 730 gpg_error_t gcry_mpi_ec_new (gcry_ctx_t *r_ctx, 731 gcry_sexp_t keyparam, const char *curvename); 732 733 /* Get a named MPI from an elliptic curve context. */ 734 gcry_mpi_t gcry_mpi_ec_get_mpi (const char *name, gcry_ctx_t ctx, int copy); 735 736 /* Get a named point from an elliptic curve context. */ 737 gcry_mpi_point_t gcry_mpi_ec_get_point (const char *name, 738 gcry_ctx_t ctx, int copy); 739 740 /* Store a named MPI into an elliptic curve context. */ 741 gpg_error_t gcry_mpi_ec_set_mpi (const char *name, gcry_mpi_t newvalue, 742 gcry_ctx_t ctx); 743 744 /* Store a named point into an elliptic curve context. */ 745 gpg_error_t gcry_mpi_ec_set_point (const char *name, gcry_mpi_point_t newvalue, 746 gcry_ctx_t ctx); 747 748 /* Decode and store VALUE into RESULT. */ 749 gpg_error_t gcry_mpi_ec_decode_point (gcry_mpi_point_t result, 750 gcry_mpi_t value, gcry_ctx_t ctx); 751 752 /* Store the affine coordinates of POINT into X and Y. */ 753 int gcry_mpi_ec_get_affine (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_point_t point, 754 gcry_ctx_t ctx); 755 756 /* W = 2 * U. */ 757 void gcry_mpi_ec_dup (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_ctx_t ctx); 758 759 /* W = U + V. */ 760 void gcry_mpi_ec_add (gcry_mpi_point_t w, 761 gcry_mpi_point_t u, gcry_mpi_point_t v, gcry_ctx_t ctx); 762 763 /* W = U - V. */ 764 void gcry_mpi_ec_sub (gcry_mpi_point_t w, 765 gcry_mpi_point_t u, gcry_mpi_point_t v, gcry_ctx_t ctx); 766 767 /* W = N * U. */ 768 void gcry_mpi_ec_mul (gcry_mpi_point_t w, gcry_mpi_t n, gcry_mpi_point_t u, 769 gcry_ctx_t ctx); 770 771 /* Return true if POINT is on the curve described by CTX. */ 772 int gcry_mpi_ec_curve_point (gcry_mpi_point_t w, gcry_ctx_t ctx); 773 774 /* Return the number of bits required to represent A. */ 775 unsigned int gcry_mpi_get_nbits (gcry_mpi_t a); 776 777 /* Return true when bit number N (counting from 0) is set in A. */ 778 int gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n); 779 780 /* Set bit number N in A. */ 781 void gcry_mpi_set_bit (gcry_mpi_t a, unsigned int n); 782 783 /* Clear bit number N in A. */ 784 void gcry_mpi_clear_bit (gcry_mpi_t a, unsigned int n); 785 786 /* Set bit number N in A and clear all bits greater than N. */ 787 void gcry_mpi_set_highbit (gcry_mpi_t a, unsigned int n); 788 789 /* Clear bit number N in A and all bits greater than N. */ 790 void gcry_mpi_clear_highbit (gcry_mpi_t a, unsigned int n); 791 792 /* Shift the value of A by N bits to the right and store the result in X. */ 793 void gcry_mpi_rshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n); 794 795 /* Shift the value of A by N bits to the left and store the result in X. */ 796 void gcry_mpi_lshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n); 797 798 /* Store NBITS of the value P points to in A and mark A as an opaque 799 value. On success A received the the ownership of the value P. 800 WARNING: Never use an opaque MPI for anything thing else than 801 gcry_mpi_release, gcry_mpi_get_opaque. */ 802 gcry_mpi_t gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits); 803 804 /* Store NBITS of the value P points to in A and mark A as an opaque 805 value. The function takes a copy of the provided value P. 806 WARNING: Never use an opaque MPI for anything thing else than 807 gcry_mpi_release, gcry_mpi_get_opaque. */ 808 gcry_mpi_t gcry_mpi_set_opaque_copy (gcry_mpi_t a, 809 const void *p, unsigned int nbits); 810 811 /* Return a pointer to an opaque value stored in A and return its size 812 in NBITS. Note that the returned pointer is still owned by A and 813 that the function should never be used for an non-opaque MPI. */ 814 void *gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits); 815 816 /* Set the FLAG for the big integer A. Currently only the flag 817 GCRYMPI_FLAG_SECURE is allowed to convert A into an big intger 818 stored in "secure" memory. */ 819 void gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); 820 821 /* Clear FLAG for the big integer A. Note that this function is 822 currently useless as no flags are allowed. */ 823 void gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); 824 825 /* Return true if the FLAG is set for A. */ 826 int gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); 827 828 /* Private function - do not use. */ 829 gcry_mpi_t _gcry_mpi_get_const (int no); 830 831 /* Unless the GCRYPT_NO_MPI_MACROS is used, provide a couple of 832 convenience macros for the big integer functions. */ 833 #ifndef GCRYPT_NO_MPI_MACROS 834 #define mpi_new(n) gcry_mpi_new( (n) ) 835 #define mpi_secure_new( n ) gcry_mpi_snew( (n) ) 836 #define mpi_release(a) \ 837 do \ 838 { \ 839 gcry_mpi_release ((a)); \ 840 (a) = NULL; \ 841 } \ 842 while (0) 843 844 #define mpi_copy( a ) gcry_mpi_copy( (a) ) 845 #define mpi_snatch( w, u) gcry_mpi_snatch( (w), (u) ) 846 #define mpi_set( w, u) gcry_mpi_set( (w), (u) ) 847 #define mpi_set_ui( w, u) gcry_mpi_set_ui( (w), (u) ) 848 #define mpi_get_ui( w, u) gcry_mpi_get_ui( (w), (u) ) 849 #define mpi_abs( w ) gcry_mpi_abs( (w) ) 850 #define mpi_neg( w, u) gcry_mpi_neg( (w), (u) ) 851 #define mpi_cmp( u, v ) gcry_mpi_cmp( (u), (v) ) 852 #define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) ) 853 #define mpi_is_neg( a ) gcry_mpi_is_neg ((a)) 854 855 #define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v)) 856 #define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v)) 857 #define mpi_addm(w,u,v,m) gcry_mpi_addm ((w),(u),(v),(m)) 858 #define mpi_sub_ui(w,u,v) gcry_mpi_sub_ui ((w),(u),(v)) 859 #define mpi_sub(w,u,v) gcry_mpi_sub ((w),(u),(v)) 860 #define mpi_subm(w,u,v,m) gcry_mpi_subm ((w),(u),(v),(m)) 861 #define mpi_mul_ui(w,u,v) gcry_mpi_mul_ui ((w),(u),(v)) 862 #define mpi_mul_2exp(w,u,v) gcry_mpi_mul_2exp ((w),(u),(v)) 863 #define mpi_mul(w,u,v) gcry_mpi_mul ((w),(u),(v)) 864 #define mpi_mulm(w,u,v,m) gcry_mpi_mulm ((w),(u),(v),(m)) 865 #define mpi_powm(w,b,e,m) gcry_mpi_powm ( (w), (b), (e), (m) ) 866 #define mpi_tdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), 0) 867 #define mpi_fdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), -1) 868 #define mpi_mod(r,a,m) gcry_mpi_mod ((r), (a), (m)) 869 #define mpi_gcd(g,a,b) gcry_mpi_gcd ( (g), (a), (b) ) 870 #define mpi_invm(g,a,b) gcry_mpi_invm ( (g), (a), (b) ) 871 872 #define mpi_point_new(n) gcry_mpi_point_new((n)) 873 #define mpi_point_release(p) \ 874 do \ 875 { \ 876 gcry_mpi_point_release ((p)); \ 877 (p) = NULL; \ 878 } \ 879 while (0) 880 #define mpi_point_copy(p) gcry_mpi_point_copy((p)) 881 #define mpi_point_get(x,y,z,p) gcry_mpi_point_get((x),(y),(z),(p)) 882 #define mpi_point_snatch_get(x,y,z,p) gcry_mpi_point_snatch_get((x),(y),(z),(p)) 883 #define mpi_point_set(p,x,y,z) gcry_mpi_point_set((p),(x),(y),(z)) 884 #define mpi_point_snatch_set(p,x,y,z) gcry_mpi_point_snatch_set((p),(x),(y),(z)) 885 886 #define mpi_get_nbits(a) gcry_mpi_get_nbits ((a)) 887 #define mpi_test_bit(a,b) gcry_mpi_test_bit ((a),(b)) 888 #define mpi_set_bit(a,b) gcry_mpi_set_bit ((a),(b)) 889 #define mpi_set_highbit(a,b) gcry_mpi_set_highbit ((a),(b)) 890 #define mpi_clear_bit(a,b) gcry_mpi_clear_bit ((a),(b)) 891 #define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b)) 892 #define mpi_rshift(a,b,c) gcry_mpi_rshift ((a),(b),(c)) 893 #define mpi_lshift(a,b,c) gcry_mpi_lshift ((a),(b),(c)) 894 895 #define mpi_set_opaque(a,b,c) gcry_mpi_set_opaque( (a), (b), (c) ) 896 #define mpi_get_opaque(a,b) gcry_mpi_get_opaque( (a), (b) ) 897 #endif /* GCRYPT_NO_MPI_MACROS */ 898 899 900 901 /************************************ 902 * * 903 * Symmetric Cipher Functions * 904 * * 905 ************************************/ 906 907 /* The data object used to hold a handle to an encryption object. */ 908 struct gcry_cipher_handle; 909 typedef struct gcry_cipher_handle *gcry_cipher_hd_t; 910 911 #ifndef GCRYPT_NO_DEPRECATED 912 typedef struct gcry_cipher_handle *GCRY_CIPHER_HD _GCRY_GCC_ATTR_DEPRECATED; 913 typedef struct gcry_cipher_handle *GcryCipherHd _GCRY_GCC_ATTR_DEPRECATED; 914 #endif 915 916 /* All symmetric encryption algorithms are identified by their IDs. 917 More IDs may be registered at runtime. */ 918 enum gcry_cipher_algos 919 { 920 GCRY_CIPHER_NONE = 0, 921 GCRY_CIPHER_IDEA = 1, 922 GCRY_CIPHER_3DES = 2, 923 GCRY_CIPHER_CAST5 = 3, 924 GCRY_CIPHER_BLOWFISH = 4, 925 GCRY_CIPHER_SAFER_SK128 = 5, 926 GCRY_CIPHER_DES_SK = 6, 927 GCRY_CIPHER_AES = 7, 928 GCRY_CIPHER_AES192 = 8, 929 GCRY_CIPHER_AES256 = 9, 930 GCRY_CIPHER_TWOFISH = 10, 931 932 /* Other cipher numbers are above 300 for OpenPGP reasons. */ 933 GCRY_CIPHER_ARCFOUR = 301, /* Fully compatible with RSA's RC4 (tm). */ 934 GCRY_CIPHER_DES = 302, /* Yes, this is single key 56 bit DES. */ 935 GCRY_CIPHER_TWOFISH128 = 303, 936 GCRY_CIPHER_SERPENT128 = 304, 937 GCRY_CIPHER_SERPENT192 = 305, 938 GCRY_CIPHER_SERPENT256 = 306, 939 GCRY_CIPHER_RFC2268_40 = 307, /* Ron's Cipher 2 (40 bit). */ 940 GCRY_CIPHER_RFC2268_128 = 308, /* Ron's Cipher 2 (128 bit). */ 941 GCRY_CIPHER_SEED = 309, /* 128 bit cipher described in RFC4269. */ 942 GCRY_CIPHER_CAMELLIA128 = 310, 943 GCRY_CIPHER_CAMELLIA192 = 311, 944 GCRY_CIPHER_CAMELLIA256 = 312, 945 GCRY_CIPHER_SALSA20 = 313, 946 GCRY_CIPHER_SALSA20R12 = 314, 947 GCRY_CIPHER_GOST28147 = 315, 948 GCRY_CIPHER_CHACHA20 = 316, 949 GCRY_CIPHER_GOST28147_MESH = 317, /* With CryptoPro key meshing. */ 950 GCRY_CIPHER_SM4 = 318 951 }; 952 953 /* The Rijndael algorithm is basically AES, so provide some macros. */ 954 #define GCRY_CIPHER_AES128 GCRY_CIPHER_AES 955 #define GCRY_CIPHER_RIJNDAEL GCRY_CIPHER_AES 956 #define GCRY_CIPHER_RIJNDAEL128 GCRY_CIPHER_AES128 957 #define GCRY_CIPHER_RIJNDAEL192 GCRY_CIPHER_AES192 958 #define GCRY_CIPHER_RIJNDAEL256 GCRY_CIPHER_AES256 959 960 /* The supported encryption modes. Note that not all of them are 961 supported for each algorithm. */ 962 enum gcry_cipher_modes 963 { 964 GCRY_CIPHER_MODE_NONE = 0, /* Not yet specified. */ 965 GCRY_CIPHER_MODE_ECB = 1, /* Electronic codebook. */ 966 GCRY_CIPHER_MODE_CFB = 2, /* Cipher feedback. */ 967 GCRY_CIPHER_MODE_CBC = 3, /* Cipher block chaining. */ 968 GCRY_CIPHER_MODE_STREAM = 4, /* Used with stream ciphers. */ 969 GCRY_CIPHER_MODE_OFB = 5, /* Outer feedback. */ 970 GCRY_CIPHER_MODE_CTR = 6, /* Counter. */ 971 GCRY_CIPHER_MODE_AESWRAP = 7, /* AES-WRAP algorithm. */ 972 GCRY_CIPHER_MODE_CCM = 8, /* Counter with CBC-MAC. */ 973 GCRY_CIPHER_MODE_GCM = 9, /* Galois Counter Mode. */ 974 GCRY_CIPHER_MODE_POLY1305 = 10, /* Poly1305 based AEAD mode. */ 975 GCRY_CIPHER_MODE_OCB = 11, /* OCB3 mode. */ 976 GCRY_CIPHER_MODE_CFB8 = 12, /* Cipher feedback (8 bit mode). */ 977 GCRY_CIPHER_MODE_XTS = 13, /* XTS mode. */ 978 GCRY_CIPHER_MODE_EAX = 14, /* EAX mode. */ 979 GCRY_CIPHER_MODE_SIV = 15, /* SIV mode. */ 980 GCRY_CIPHER_MODE_GCM_SIV = 16 /* GCM-SIV mode. */ 981 }; 982 983 /* Flags used with the open function. */ 984 enum gcry_cipher_flags 985 { 986 GCRY_CIPHER_SECURE = 1, /* Allocate in secure memory. */ 987 GCRY_CIPHER_ENABLE_SYNC = 2, /* Enable CFB sync mode. */ 988 GCRY_CIPHER_CBC_CTS = 4, /* Enable CBC cipher text stealing (CTS). */ 989 GCRY_CIPHER_CBC_MAC = 8, /* Enable CBC message auth. code (MAC). */ 990 GCRY_CIPHER_EXTENDED = 16 /* Enable extended AES-WRAP. */ 991 }; 992 993 /* GCM works only with blocks of 128 bits */ 994 #define GCRY_GCM_BLOCK_LEN (128 / 8) 995 996 /* CCM works only with blocks of 128 bits. */ 997 #define GCRY_CCM_BLOCK_LEN (128 / 8) 998 999 /* OCB works only with blocks of 128 bits. */ 1000 #define GCRY_OCB_BLOCK_LEN (128 / 8) 1001 1002 /* XTS works only with blocks of 128 bits. */ 1003 #define GCRY_XTS_BLOCK_LEN (128 / 8) 1004 1005 /* SIV and GCM-SIV works only with blocks of 128 bits */ 1006 #define GCRY_SIV_BLOCK_LEN (128 / 8) 1007 1008 /* Create a handle for algorithm ALGO to be used in MODE. FLAGS may 1009 be given as an bitwise OR of the gcry_cipher_flags values. */ 1010 gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *handle, 1011 int algo, int mode, unsigned int flags); 1012 1013 /* Close the cipher handle H and release all resource. */ 1014 void gcry_cipher_close (gcry_cipher_hd_t h); 1015 1016 /* Perform various operations on the cipher object H. */ 1017 gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, 1018 size_t buflen); 1019 1020 /* Retrieve various information about the cipher object H. */ 1021 gcry_error_t gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer, 1022 size_t *nbytes); 1023 1024 /* Retrieve various information about the cipher algorithm ALGO. */ 1025 gcry_error_t gcry_cipher_algo_info (int algo, int what, void *buffer, 1026 size_t *nbytes); 1027 1028 /* Map the cipher algorithm whose ID is contained in ALGORITHM to a 1029 string representation of the algorithm name. For unknown algorithm 1030 IDs this function returns "?". */ 1031 const char *gcry_cipher_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; 1032 1033 /* Map the algorithm name NAME to an cipher algorithm ID. Return 0 if 1034 the algorithm name is not known. */ 1035 int gcry_cipher_map_name (const char *name) _GCRY_GCC_ATTR_PURE; 1036 1037 /* Given an ASN.1 object identifier in standard IETF dotted decimal 1038 format in STRING, return the encryption mode associated with that 1039 OID or 0 if not known or applicable. */ 1040 int gcry_cipher_mode_from_oid (const char *string) _GCRY_GCC_ATTR_PURE; 1041 1042 /* Encrypt the plaintext of size INLEN in IN using the cipher handle H 1043 into the buffer OUT which has an allocated length of OUTSIZE. For 1044 most algorithms it is possible to pass NULL for in and 0 for INLEN 1045 and do a in-place decryption of the data provided in OUT. */ 1046 gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h, 1047 void *out, size_t outsize, 1048 const void *in, size_t inlen); 1049 1050 /* The counterpart to gcry_cipher_encrypt. */ 1051 gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t h, 1052 void *out, size_t outsize, 1053 const void *in, size_t inlen); 1054 1055 /* Set KEY of length KEYLEN bytes for the cipher handle HD. */ 1056 gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t hd, 1057 const void *key, size_t keylen); 1058 1059 1060 /* Set initialization vector IV of length IVLEN for the cipher handle HD. */ 1061 gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t hd, 1062 const void *iv, size_t ivlen); 1063 1064 /* Provide additional authentication data for AEAD modes/ciphers. */ 1065 gcry_error_t gcry_cipher_authenticate (gcry_cipher_hd_t hd, const void *abuf, 1066 size_t abuflen); 1067 1068 /* Get authentication tag for AEAD modes/ciphers. */ 1069 gcry_error_t gcry_cipher_gettag (gcry_cipher_hd_t hd, void *outtag, 1070 size_t taglen); 1071 1072 /* Check authentication tag for AEAD modes/ciphers. */ 1073 gcry_error_t gcry_cipher_checktag (gcry_cipher_hd_t hd, const void *intag, 1074 size_t taglen); 1075 1076 /* Reset the handle to the state after open. */ 1077 #define gcry_cipher_reset(h) gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0) 1078 1079 /* Perform the OpenPGP sync operation if this is enabled for the 1080 cipher handle H. */ 1081 #define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, NULL, 0) 1082 1083 /* Enable or disable CTS in future calls to gcry_cipher_encrypt(). 1084 * CBC mode only. */ 1085 #define gcry_cipher_cts(h,on) gcry_cipher_ctl( (h), GCRYCTL_SET_CBC_CTS, \ 1086 NULL, on ) 1087 1088 #define gcry_cipher_set_sbox(h,oid) gcry_cipher_ctl( (h), GCRYCTL_SET_SBOX, \ 1089 (void *) oid, 0); 1090 1091 /* Indicate to the encrypt and decrypt functions that the next call 1092 provides the final data. Only used with some modes. */ 1093 #define gcry_cipher_final(a) \ 1094 gcry_cipher_ctl ((a), GCRYCTL_FINALIZE, NULL, 0) 1095 1096 /* Set counter for CTR mode. (CTR,CTRLEN) must denote a buffer of 1097 block size length, or (NULL,0) to set the CTR to the all-zero block. */ 1098 gpg_error_t gcry_cipher_setctr (gcry_cipher_hd_t hd, 1099 const void *ctr, size_t ctrlen); 1100 1101 /* Retrieve the key length in bytes used with algorithm A. */ 1102 size_t gcry_cipher_get_algo_keylen (int algo); 1103 1104 /* Retrieve the block length in bytes used with algorithm A. */ 1105 size_t gcry_cipher_get_algo_blklen (int algo); 1106 1107 /* Return 0 if the algorithm A is available for use. */ 1108 #define gcry_cipher_test_algo(a) \ 1109 gcry_cipher_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) 1110 1111 /* Setup tag for decryption (for SIV and GCM-SIV mode). */ 1112 #define gcry_cipher_set_decryption_tag(a, tag, taglen) \ 1113 gcry_cipher_ctl ((a), GCRYCTL_SET_DECRYPTION_TAG, \ 1114 (void *)(tag), (taglen)) 1115 1116 1117 /************************************ 1118 * * 1119 * Asymmetric Cipher Functions * 1120 * * 1121 ************************************/ 1122 1123 /* The algorithms and their IDs we support. */ 1124 enum gcry_pk_algos 1125 { 1126 GCRY_PK_RSA = 1, /* RSA */ 1127 GCRY_PK_RSA_E = 2, /* (deprecated: use 1). */ 1128 GCRY_PK_RSA_S = 3, /* (deprecated: use 1). */ 1129 GCRY_PK_ELG_E = 16, /* (deprecated: use 20). */ 1130 GCRY_PK_DSA = 17, /* Digital Signature Algorithm. */ 1131 GCRY_PK_ECC = 18, /* Generic ECC. */ 1132 GCRY_PK_ELG = 20, /* Elgamal */ 1133 GCRY_PK_ECDSA = 301, /* (only for external use). */ 1134 GCRY_PK_ECDH = 302, /* (only for external use). */ 1135 GCRY_PK_EDDSA = 303 /* (only for external use). */ 1136 }; 1137 1138 /* Flags describing usage capabilities of a PK algorithm. */ 1139 #define GCRY_PK_USAGE_SIGN 1 /* Good for signatures. */ 1140 #define GCRY_PK_USAGE_ENCR 2 /* Good for encryption. */ 1141 #define GCRY_PK_USAGE_CERT 4 /* Good to certify other keys. */ 1142 #define GCRY_PK_USAGE_AUTH 8 /* Good for authentication. */ 1143 #define GCRY_PK_USAGE_UNKN 128 /* Unknown usage flag. */ 1144 1145 /* Modes used with gcry_pubkey_get_sexp. */ 1146 #define GCRY_PK_GET_PUBKEY 1 1147 #define GCRY_PK_GET_SECKEY 2 1148 1149 /* Encrypt the DATA using the public key PKEY and store the result as 1150 a newly created S-expression at RESULT. */ 1151 gcry_error_t gcry_pk_encrypt (gcry_sexp_t *result, 1152 gcry_sexp_t data, gcry_sexp_t pkey); 1153 1154 /* Decrypt the DATA using the private key SKEY and store the result as 1155 a newly created S-expression at RESULT. */ 1156 gcry_error_t gcry_pk_decrypt (gcry_sexp_t *result, 1157 gcry_sexp_t data, gcry_sexp_t skey); 1158 1159 /* Sign the DATA using the private key SKEY and store the result as 1160 a newly created S-expression at RESULT. */ 1161 gcry_error_t gcry_pk_sign (gcry_sexp_t *result, 1162 gcry_sexp_t data, gcry_sexp_t skey); 1163 1164 /* Check the signature SIGVAL on DATA using the public key PKEY. */ 1165 gcry_error_t gcry_pk_verify (gcry_sexp_t sigval, 1166 gcry_sexp_t data, gcry_sexp_t pkey); 1167 1168 /* Check that private KEY is sane. */ 1169 gcry_error_t gcry_pk_testkey (gcry_sexp_t key); 1170 1171 /* Generate a new key pair according to the parameters given in 1172 S_PARMS. The new key pair is returned in as an S-expression in 1173 R_KEY. */ 1174 gcry_error_t gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms); 1175 1176 /* Catch all function for miscellaneous operations. */ 1177 gcry_error_t gcry_pk_ctl (int cmd, void *buffer, size_t buflen); 1178 1179 /* Retrieve information about the public key algorithm ALGO. */ 1180 gcry_error_t gcry_pk_algo_info (int algo, int what, 1181 void *buffer, size_t *nbytes); 1182 1183 /* Map the public key algorithm whose ID is contained in ALGORITHM to 1184 a string representation of the algorithm name. For unknown 1185 algorithm IDs this functions returns "?". */ 1186 const char *gcry_pk_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; 1187 1188 /* Map the algorithm NAME to a public key algorithm Id. Return 0 if 1189 the algorithm name is not known. */ 1190 int gcry_pk_map_name (const char* name) _GCRY_GCC_ATTR_PURE; 1191 1192 /* Return what is commonly referred as the key length for the given 1193 public or private KEY. */ 1194 unsigned int gcry_pk_get_nbits (gcry_sexp_t key) _GCRY_GCC_ATTR_PURE; 1195 1196 /* Return the so called KEYGRIP which is the SHA-1 hash of the public 1197 key parameters expressed in a way depending on the algorithm. */ 1198 unsigned char *gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array); 1199 1200 /* Return the name of the curve matching KEY. */ 1201 const char *gcry_pk_get_curve (gcry_sexp_t key, int iterator, 1202 unsigned int *r_nbits); 1203 1204 /* Return an S-expression with the parameters of the named ECC curve 1205 NAME. ALGO must be set to an ECC algorithm. */ 1206 gcry_sexp_t gcry_pk_get_param (int algo, const char *name); 1207 1208 /* Return 0 if the public key algorithm A is available for use. */ 1209 #define gcry_pk_test_algo(a) \ 1210 gcry_pk_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) 1211 1212 /* Return an S-expression representing the context CTX. */ 1213 gcry_error_t gcry_pubkey_get_sexp (gcry_sexp_t *r_sexp, 1214 int mode, gcry_ctx_t ctx); 1215 1216 /************************************ 1217 * * 1218 * Modern ECC Functions * 1219 * * 1220 ************************************/ 1221 1222 /* The curves we support. */ 1223 enum gcry_ecc_curves 1224 { 1225 GCRY_ECC_CURVE25519 = 1, 1226 GCRY_ECC_CURVE448 = 2 1227 }; 1228 1229 /* Get the length of point to prepare buffer for the result. */ 1230 unsigned int gcry_ecc_get_algo_keylen (int curveid); 1231 1232 /* Convenience function to compute scalar multiplication of the 1233 * Montgomery form of curve. */ 1234 gpg_error_t gcry_ecc_mul_point (int curveid, unsigned char *result, 1235 const unsigned char *scalar, 1236 const unsigned char *point); 1237 1238 1239 1240 /************************************ 1241 * * 1242 * Cryptograhic Hash Functions * 1243 * * 1244 ************************************/ 1245 1246 /* Algorithm IDs for the hash functions we know about. Not all of them 1247 are implemented. */ 1248 enum gcry_md_algos 1249 { 1250 GCRY_MD_NONE = 0, 1251 GCRY_MD_MD5 = 1, 1252 GCRY_MD_SHA1 = 2, 1253 GCRY_MD_RMD160 = 3, 1254 GCRY_MD_MD2 = 5, 1255 GCRY_MD_TIGER = 6, /* TIGER/192 as used by gpg <= 1.3.2. */ 1256 GCRY_MD_HAVAL = 7, /* HAVAL, 5 pass, 160 bit. */ 1257 GCRY_MD_SHA256 = 8, 1258 GCRY_MD_SHA384 = 9, 1259 GCRY_MD_SHA512 = 10, 1260 GCRY_MD_SHA224 = 11, 1261 1262 GCRY_MD_MD4 = 301, 1263 GCRY_MD_CRC32 = 302, 1264 GCRY_MD_CRC32_RFC1510 = 303, 1265 GCRY_MD_CRC24_RFC2440 = 304, 1266 GCRY_MD_WHIRLPOOL = 305, 1267 GCRY_MD_TIGER1 = 306, /* TIGER fixed. */ 1268 GCRY_MD_TIGER2 = 307, /* TIGER2 variant. */ 1269 GCRY_MD_GOSTR3411_94 = 308, /* GOST R 34.11-94. */ 1270 GCRY_MD_STRIBOG256 = 309, /* GOST R 34.11-2012, 256 bit. */ 1271 GCRY_MD_STRIBOG512 = 310, /* GOST R 34.11-2012, 512 bit. */ 1272 GCRY_MD_GOSTR3411_CP = 311, /* GOST R 34.11-94 with CryptoPro-A S-Box. */ 1273 GCRY_MD_SHA3_224 = 312, 1274 GCRY_MD_SHA3_256 = 313, 1275 GCRY_MD_SHA3_384 = 314, 1276 GCRY_MD_SHA3_512 = 315, 1277 GCRY_MD_SHAKE128 = 316, 1278 GCRY_MD_SHAKE256 = 317, 1279 GCRY_MD_BLAKE2B_512 = 318, 1280 GCRY_MD_BLAKE2B_384 = 319, 1281 GCRY_MD_BLAKE2B_256 = 320, 1282 GCRY_MD_BLAKE2B_160 = 321, 1283 GCRY_MD_BLAKE2S_256 = 322, 1284 GCRY_MD_BLAKE2S_224 = 323, 1285 GCRY_MD_BLAKE2S_160 = 324, 1286 GCRY_MD_BLAKE2S_128 = 325, 1287 GCRY_MD_SM3 = 326, 1288 GCRY_MD_SHA512_256 = 327, 1289 GCRY_MD_SHA512_224 = 328 1290 }; 1291 1292 /* Flags used with the open function. */ 1293 enum gcry_md_flags 1294 { 1295 GCRY_MD_FLAG_SECURE = 1, /* Allocate all buffers in "secure" memory. */ 1296 GCRY_MD_FLAG_HMAC = 2, /* Make an HMAC out of this algorithm. */ 1297 GCRY_MD_FLAG_BUGEMU1 = 0x0100 1298 }; 1299 1300 /* (Forward declaration.) */ 1301 struct gcry_md_context; 1302 1303 /* This object is used to hold a handle to a message digest object. 1304 This structure is private - only to be used by the public gcry_md_* 1305 macros. */ 1306 typedef struct gcry_md_handle 1307 { 1308 /* Actual context. */ 1309 struct gcry_md_context *ctx; 1310 1311 /* Buffer management. */ 1312 int bufpos; 1313 int bufsize; 1314 unsigned char buf[1]; 1315 } *gcry_md_hd_t; 1316 1317 /* Compatibility types, do not use them. */ 1318 #ifndef GCRYPT_NO_DEPRECATED 1319 typedef struct gcry_md_handle *GCRY_MD_HD _GCRY_GCC_ATTR_DEPRECATED; 1320 typedef struct gcry_md_handle *GcryMDHd _GCRY_GCC_ATTR_DEPRECATED; 1321 #endif 1322 1323 /* Create a message digest object for algorithm ALGO. FLAGS may be 1324 given as an bitwise OR of the gcry_md_flags values. ALGO may be 1325 given as 0 if the algorithms to be used are later set using 1326 gcry_md_enable. */ 1327 gcry_error_t gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags); 1328 1329 /* Release the message digest object HD. */ 1330 void gcry_md_close (gcry_md_hd_t hd); 1331 1332 /* Add the message digest algorithm ALGO to the digest object HD. */ 1333 gcry_error_t gcry_md_enable (gcry_md_hd_t hd, int algo); 1334 1335 /* Create a new digest object as an exact copy of the object HD. */ 1336 gcry_error_t gcry_md_copy (gcry_md_hd_t *bhd, gcry_md_hd_t ahd); 1337 1338 /* Reset the digest object HD to its initial state. */ 1339 void gcry_md_reset (gcry_md_hd_t hd); 1340 1341 /* Perform various operations on the digest object HD. */ 1342 gcry_error_t gcry_md_ctl (gcry_md_hd_t hd, int cmd, 1343 void *buffer, size_t buflen); 1344 1345 /* Pass LENGTH bytes of data in BUFFER to the digest object HD so that 1346 it can update the digest values. This is the actual hash 1347 function. */ 1348 void gcry_md_write (gcry_md_hd_t hd, const void *buffer, size_t length); 1349 1350 /* Read out the final digest from HD return the digest value for 1351 algorithm ALGO. */ 1352 unsigned char *gcry_md_read (gcry_md_hd_t hd, int algo); 1353 1354 /* Read more output from algorithm ALGO to BUFFER of size LENGTH from 1355 * digest object HD. Algorithm needs to be 'expendable-output function'. */ 1356 gpg_error_t gcry_md_extract (gcry_md_hd_t hd, int algo, void *buffer, 1357 size_t length); 1358 1359 /* Convenience function to calculate the hash from the data in BUFFER 1360 of size LENGTH using the algorithm ALGO avoiding the creation of a 1361 hash object. The hash is returned in the caller provided buffer 1362 DIGEST which must be large enough to hold the digest of the given 1363 algorithm. */ 1364 void gcry_md_hash_buffer (int algo, void *digest, 1365 const void *buffer, size_t length); 1366 1367 /* Convenience function to hash multiple buffers. */ 1368 gpg_error_t gcry_md_hash_buffers (int algo, unsigned int flags, void *digest, 1369 const gcry_buffer_t *iov, int iovcnt); 1370 1371 /* Retrieve the algorithm used with HD. This does not work reliable 1372 if more than one algorithm is enabled in HD. */ 1373 int gcry_md_get_algo (gcry_md_hd_t hd); 1374 1375 /* Retrieve the length in bytes of the digest yielded by algorithm 1376 ALGO. */ 1377 unsigned int gcry_md_get_algo_dlen (int algo); 1378 1379 /* Return true if the the algorithm ALGO is enabled in the digest 1380 object A. */ 1381 int gcry_md_is_enabled (gcry_md_hd_t a, int algo); 1382 1383 /* Return true if the digest object A is allocated in "secure" memory. */ 1384 int gcry_md_is_secure (gcry_md_hd_t a); 1385 1386 /* Deprecated: Use gcry_md_is_enabled or gcry_md_is_secure. */ 1387 gcry_error_t gcry_md_info (gcry_md_hd_t h, int what, void *buffer, 1388 size_t *nbytes) _GCRY_ATTR_INTERNAL; 1389 1390 /* Retrieve various information about the algorithm ALGO. */ 1391 gcry_error_t gcry_md_algo_info (int algo, int what, void *buffer, 1392 size_t *nbytes); 1393 1394 /* Map the digest algorithm id ALGO to a string representation of the 1395 algorithm name. For unknown algorithms this function returns 1396 "?". */ 1397 const char *gcry_md_algo_name (int algo) _GCRY_GCC_ATTR_PURE; 1398 1399 /* Map the algorithm NAME to a digest algorithm Id. Return 0 if 1400 the algorithm name is not known. */ 1401 int gcry_md_map_name (const char* name) _GCRY_GCC_ATTR_PURE; 1402 1403 /* For use with the HMAC feature, the set MAC key to the KEY of 1404 KEYLEN bytes. */ 1405 gcry_error_t gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen); 1406 1407 /* Start or stop debugging for digest handle HD; i.e. create a file 1408 named dbgmd-<n>.<suffix> while hashing. If SUFFIX is NULL, 1409 debugging stops and the file will be closed. */ 1410 void gcry_md_debug (gcry_md_hd_t hd, const char *suffix); 1411 1412 1413 /* Update the hash(s) of H with the character C. This is a buffered 1414 version of the gcry_md_write function. */ 1415 #define gcry_md_putc(h,c) \ 1416 do { \ 1417 gcry_md_hd_t h__ = (h); \ 1418 if( (h__)->bufpos == (h__)->bufsize ) \ 1419 gcry_md_write( (h__), NULL, 0 ); \ 1420 (h__)->buf[(h__)->bufpos++] = (c) & 0xff; \ 1421 } while(0) 1422 1423 /* Finalize the digest calculation. This is not really needed because 1424 gcry_md_read() does this implicitly. */ 1425 #define gcry_md_final(a) \ 1426 gcry_md_ctl ((a), GCRYCTL_FINALIZE, NULL, 0) 1427 1428 /* Return 0 if the algorithm A is available for use. */ 1429 #define gcry_md_test_algo(a) \ 1430 gcry_md_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) 1431 1432 /* Return an DER encoded ASN.1 OID for the algorithm A in buffer B. N 1433 must point to size_t variable with the available size of buffer B. 1434 After return it will receive the actual size of the returned 1435 OID. */ 1436 #define gcry_md_get_asnoid(a,b,n) \ 1437 gcry_md_algo_info((a), GCRYCTL_GET_ASNOID, (b), (n)) 1438 1439 1440 1441 /********************************************** 1442 * * 1443 * Message Authentication Code Functions * 1444 * * 1445 **********************************************/ 1446 1447 /* The data object used to hold a handle to an encryption object. */ 1448 struct gcry_mac_handle; 1449 typedef struct gcry_mac_handle *gcry_mac_hd_t; 1450 1451 /* Algorithm IDs for the hash functions we know about. Not all of them 1452 are implemented. */ 1453 enum gcry_mac_algos 1454 { 1455 GCRY_MAC_NONE = 0, 1456 GCRY_MAC_GOST28147_IMIT = 1, 1457 1458 GCRY_MAC_HMAC_SHA256 = 101, 1459 GCRY_MAC_HMAC_SHA224 = 102, 1460 GCRY_MAC_HMAC_SHA512 = 103, 1461 GCRY_MAC_HMAC_SHA384 = 104, 1462 GCRY_MAC_HMAC_SHA1 = 105, 1463 GCRY_MAC_HMAC_MD5 = 106, 1464 GCRY_MAC_HMAC_MD4 = 107, 1465 GCRY_MAC_HMAC_RMD160 = 108, 1466 GCRY_MAC_HMAC_TIGER1 = 109, /* The fixed TIGER variant */ 1467 GCRY_MAC_HMAC_WHIRLPOOL = 110, 1468 GCRY_MAC_HMAC_GOSTR3411_94 = 111, 1469 GCRY_MAC_HMAC_STRIBOG256 = 112, 1470 GCRY_MAC_HMAC_STRIBOG512 = 113, 1471 GCRY_MAC_HMAC_MD2 = 114, 1472 GCRY_MAC_HMAC_SHA3_224 = 115, 1473 GCRY_MAC_HMAC_SHA3_256 = 116, 1474 GCRY_MAC_HMAC_SHA3_384 = 117, 1475 GCRY_MAC_HMAC_SHA3_512 = 118, 1476 GCRY_MAC_HMAC_GOSTR3411_CP = 119, 1477 GCRY_MAC_HMAC_BLAKE2B_512 = 120, 1478 GCRY_MAC_HMAC_BLAKE2B_384 = 121, 1479 GCRY_MAC_HMAC_BLAKE2B_256 = 122, 1480 GCRY_MAC_HMAC_BLAKE2B_160 = 123, 1481 GCRY_MAC_HMAC_BLAKE2S_256 = 124, 1482 GCRY_MAC_HMAC_BLAKE2S_224 = 125, 1483 GCRY_MAC_HMAC_BLAKE2S_160 = 126, 1484 GCRY_MAC_HMAC_BLAKE2S_128 = 127, 1485 GCRY_MAC_HMAC_SM3 = 128, 1486 GCRY_MAC_HMAC_SHA512_256 = 129, 1487 GCRY_MAC_HMAC_SHA512_224 = 130, 1488 1489 GCRY_MAC_CMAC_AES = 201, 1490 GCRY_MAC_CMAC_3DES = 202, 1491 GCRY_MAC_CMAC_CAMELLIA = 203, 1492 GCRY_MAC_CMAC_CAST5 = 204, 1493 GCRY_MAC_CMAC_BLOWFISH = 205, 1494 GCRY_MAC_CMAC_TWOFISH = 206, 1495 GCRY_MAC_CMAC_SERPENT = 207, 1496 GCRY_MAC_CMAC_SEED = 208, 1497 GCRY_MAC_CMAC_RFC2268 = 209, 1498 GCRY_MAC_CMAC_IDEA = 210, 1499 GCRY_MAC_CMAC_GOST28147 = 211, 1500 GCRY_MAC_CMAC_SM4 = 212, 1501 1502 GCRY_MAC_GMAC_AES = 401, 1503 GCRY_MAC_GMAC_CAMELLIA = 402, 1504 GCRY_MAC_GMAC_TWOFISH = 403, 1505 GCRY_MAC_GMAC_SERPENT = 404, 1506 GCRY_MAC_GMAC_SEED = 405, 1507 1508 GCRY_MAC_POLY1305 = 501, 1509 GCRY_MAC_POLY1305_AES = 502, 1510 GCRY_MAC_POLY1305_CAMELLIA = 503, 1511 GCRY_MAC_POLY1305_TWOFISH = 504, 1512 GCRY_MAC_POLY1305_SERPENT = 505, 1513 GCRY_MAC_POLY1305_SEED = 506 1514 }; 1515 1516 /* Flags used with the open function. */ 1517 enum gcry_mac_flags 1518 { 1519 GCRY_MAC_FLAG_SECURE = 1 /* Allocate all buffers in "secure" memory. */ 1520 }; 1521 1522 /* Create a MAC handle for algorithm ALGO. FLAGS may be given as an bitwise OR 1523 of the gcry_mac_flags values. CTX maybe NULL or gcry_ctx_t object to be 1524 associated with HANDLE. */ 1525 gcry_error_t gcry_mac_open (gcry_mac_hd_t *handle, int algo, 1526 unsigned int flags, gcry_ctx_t ctx); 1527 1528 /* Close the MAC handle H and release all resource. */ 1529 void gcry_mac_close (gcry_mac_hd_t h); 1530 1531 /* Perform various operations on the MAC object H. */ 1532 gcry_error_t gcry_mac_ctl (gcry_mac_hd_t h, int cmd, void *buffer, 1533 size_t buflen); 1534 1535 /* Retrieve various information about the MAC algorithm ALGO. */ 1536 gcry_error_t gcry_mac_algo_info (int algo, int what, void *buffer, 1537 size_t *nbytes); 1538 1539 /* Set KEY of length KEYLEN bytes for the MAC handle HD. */ 1540 gcry_error_t gcry_mac_setkey (gcry_mac_hd_t hd, const void *key, 1541 size_t keylen); 1542 1543 /* Set initialization vector IV of length IVLEN for the MAC handle HD. */ 1544 gcry_error_t gcry_mac_setiv (gcry_mac_hd_t hd, const void *iv, 1545 size_t ivlen); 1546 1547 /* Pass LENGTH bytes of data in BUFFER to the MAC object HD so that 1548 it can update the MAC values. */ 1549 gcry_error_t gcry_mac_write (gcry_mac_hd_t hd, const void *buffer, 1550 size_t length); 1551 1552 /* Read out the final authentication code from the MAC object HD to BUFFER. */ 1553 gcry_error_t gcry_mac_read (gcry_mac_hd_t hd, void *buffer, size_t *buflen); 1554 1555 /* Verify the final authentication code from the MAC object HD with BUFFER. */ 1556 gcry_error_t gcry_mac_verify (gcry_mac_hd_t hd, const void *buffer, 1557 size_t buflen); 1558 1559 /* Retrieve the algorithm used with MAC. */ 1560 int gcry_mac_get_algo (gcry_mac_hd_t hd); 1561 1562 /* Retrieve the length in bytes of the MAC yielded by algorithm ALGO. */ 1563 unsigned int gcry_mac_get_algo_maclen (int algo); 1564 1565 /* Retrieve the default key length in bytes used with algorithm A. */ 1566 unsigned int gcry_mac_get_algo_keylen (int algo); 1567 1568 /* Map the MAC algorithm whose ID is contained in ALGORITHM to a 1569 string representation of the algorithm name. For unknown algorithm 1570 IDs this function returns "?". */ 1571 const char *gcry_mac_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; 1572 1573 /* Map the algorithm name NAME to an MAC algorithm ID. Return 0 if 1574 the algorithm name is not known. */ 1575 int gcry_mac_map_name (const char *name) _GCRY_GCC_ATTR_PURE; 1576 1577 /* Reset the handle to the state after open/setkey. */ 1578 #define gcry_mac_reset(h) gcry_mac_ctl ((h), GCRYCTL_RESET, NULL, 0) 1579 1580 /* Return 0 if the algorithm A is available for use. */ 1581 #define gcry_mac_test_algo(a) \ 1582 gcry_mac_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) 1583 1584 1585 /****************************** 1586 * * 1587 * Key Derivation Functions * 1588 * * 1589 ******************************/ 1590 1591 /* Algorithm IDs for the KDFs. */ 1592 enum gcry_kdf_algos 1593 { 1594 GCRY_KDF_NONE = 0, 1595 GCRY_KDF_SIMPLE_S2K = 16, 1596 GCRY_KDF_SALTED_S2K = 17, 1597 GCRY_KDF_ITERSALTED_S2K = 19, 1598 GCRY_KDF_PBKDF1 = 33, 1599 GCRY_KDF_PBKDF2 = 34, 1600 GCRY_KDF_SCRYPT = 48, 1601 GCRY_KDF_ARGON2 = 64, 1602 GCRY_KDF_BALLOON = 65 1603 }; 1604 1605 enum gcry_kdf_subalgo_argon2 1606 { 1607 GCRY_KDF_ARGON2D = 0, 1608 GCRY_KDF_ARGON2I = 1, 1609 GCRY_KDF_ARGON2ID = 2 1610 }; 1611 1612 /* Derive a key from a passphrase. */ 1613 gpg_error_t gcry_kdf_derive (const void *passphrase, size_t passphraselen, 1614 int algo, int subalgo, 1615 const void *salt, size_t saltlen, 1616 unsigned long iterations, 1617 size_t keysize, void *keybuffer); 1618 1619 /* Another API to derive a key from a passphrase. */ 1620 typedef struct gcry_kdf_handle *gcry_kdf_hd_t; 1621 1622 typedef void (*gcry_kdf_job_fn_t) (void *priv); 1623 typedef int (*gcry_kdf_dispatch_job_fn_t) (void *jobs_context, 1624 gcry_kdf_job_fn_t job_fn, 1625 void *job_priv); 1626 typedef int (*gcry_kdf_wait_all_jobs_fn_t) (void *jobs_context); 1627 1628 /* Exposed structure for KDF computation to decouple thread functionality. */ 1629 typedef struct gcry_kdf_thread_ops 1630 { 1631 void *jobs_context; 1632 gcry_kdf_dispatch_job_fn_t dispatch_job; 1633 gcry_kdf_wait_all_jobs_fn_t wait_all_jobs; 1634 } gcry_kdf_thread_ops_t; 1635 1636 gcry_error_t gcry_kdf_open (gcry_kdf_hd_t *hd, int algo, int subalgo, 1637 const unsigned long *param, unsigned int paramlen, 1638 const void *passphrase, size_t passphraselen, 1639 const void *salt, size_t saltlen, 1640 const void *key, size_t keylen, 1641 const void *ad, size_t adlen); 1642 gcry_error_t gcry_kdf_compute (gcry_kdf_hd_t h, 1643 const gcry_kdf_thread_ops_t *ops); 1644 gcry_error_t gcry_kdf_final (gcry_kdf_hd_t h, size_t resultlen, void *result); 1645 void gcry_kdf_close (gcry_kdf_hd_t h); 1646 1647 /************************************ 1648 * * 1649 * Random Generating Functions * 1650 * * 1651 ************************************/ 1652 1653 /* The type of the random number generator. */ 1654 enum gcry_rng_types 1655 { 1656 GCRY_RNG_TYPE_STANDARD = 1, /* The default CSPRNG generator. */ 1657 GCRY_RNG_TYPE_FIPS = 2, /* The FIPS X9.31 AES generator. */ 1658 GCRY_RNG_TYPE_SYSTEM = 3 /* The system's native generator. */ 1659 }; 1660 1661 /* The possible values for the random quality. The rule of thumb is 1662 to use STRONG for session keys and VERY_STRONG for key material. 1663 WEAK is usually an alias for STRONG and should not be used anymore 1664 (except with gcry_mpi_randomize); use gcry_create_nonce instead. */ 1665 typedef enum gcry_random_level 1666 { 1667 GCRY_WEAK_RANDOM = 0, 1668 GCRY_STRONG_RANDOM = 1, 1669 GCRY_VERY_STRONG_RANDOM = 2 1670 } 1671 gcry_random_level_t; 1672 1673 /* Fill BUFFER with LENGTH bytes of random, using random numbers of 1674 quality LEVEL. */ 1675 void gcry_randomize (void *buffer, size_t length, 1676 enum gcry_random_level level); 1677 1678 /* Add the external random from BUFFER with LENGTH bytes into the 1679 pool. QUALITY should either be -1 for unknown or in the range of 0 1680 to 100 */ 1681 gcry_error_t gcry_random_add_bytes (const void *buffer, size_t length, 1682 int quality); 1683 1684 /* If random numbers are used in an application, this macro should be 1685 called from time to time so that new stuff gets added to the 1686 internal pool of the RNG. */ 1687 #define gcry_fast_random_poll() gcry_control (GCRYCTL_FAST_POLL, NULL) 1688 1689 1690 /* Return NBYTES of allocated random using a random numbers of quality 1691 LEVEL. */ 1692 void *gcry_random_bytes (size_t nbytes, enum gcry_random_level level) 1693 _GCRY_GCC_ATTR_MALLOC; 1694 1695 /* Return NBYTES of allocated random using a random numbers of quality 1696 LEVEL. The random is returned in "secure" memory. */ 1697 void *gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level) 1698 _GCRY_GCC_ATTR_MALLOC; 1699 1700 1701 /* Set the big integer W to a random value of NBITS using a random 1702 generator with quality LEVEL. Note that by using a level of 1703 GCRY_WEAK_RANDOM gcry_create_nonce is used internally. */ 1704 void gcry_mpi_randomize (gcry_mpi_t w, 1705 unsigned int nbits, enum gcry_random_level level); 1706 1707 1708 /* Create an unpredicable nonce of LENGTH bytes in BUFFER. */ 1709 void gcry_create_nonce (void *buffer, size_t length); 1710 1711 1712 1713 1714 1715 /*******************************/ 1716 /* */ 1717 /* Prime Number Functions */ 1718 /* */ 1719 /*******************************/ 1720 1721 /* Mode values passed to a gcry_prime_check_func_t. */ 1722 #define GCRY_PRIME_CHECK_AT_FINISH 0 1723 #define GCRY_PRIME_CHECK_AT_GOT_PRIME 1 1724 #define GCRY_PRIME_CHECK_AT_MAYBE_PRIME 2 1725 1726 /* The function should return 1 if the operation shall continue, 0 to 1727 reject the prime candidate. */ 1728 typedef int (*gcry_prime_check_func_t) (void *arg, int mode, 1729 gcry_mpi_t candidate); 1730 1731 /* Flags for gcry_prime_generate(): */ 1732 1733 /* Allocate prime numbers and factors in secure memory. */ 1734 #define GCRY_PRIME_FLAG_SECRET (1 << 0) 1735 1736 /* Make sure that at least one prime factor is of size 1737 `FACTOR_BITS'. */ 1738 #define GCRY_PRIME_FLAG_SPECIAL_FACTOR (1 << 1) 1739 1740 /* Generate a new prime number of PRIME_BITS bits and store it in 1741 PRIME. If FACTOR_BITS is non-zero, one of the prime factors of 1742 (prime - 1) / 2 must be FACTOR_BITS bits long. If FACTORS is 1743 non-zero, allocate a new, NULL-terminated array holding the prime 1744 factors and store it in FACTORS. FLAGS might be used to influence 1745 the prime number generation process. */ 1746 gcry_error_t gcry_prime_generate (gcry_mpi_t *prime, 1747 unsigned int prime_bits, 1748 unsigned int factor_bits, 1749 gcry_mpi_t **factors, 1750 gcry_prime_check_func_t cb_func, 1751 void *cb_arg, 1752 gcry_random_level_t random_level, 1753 unsigned int flags); 1754 1755 /* Find a generator for PRIME where the factorization of (prime-1) is 1756 in the NULL terminated array FACTORS. Return the generator as a 1757 newly allocated MPI in R_G. If START_G is not NULL, use this as 1758 the start for the search. */ 1759 gcry_error_t gcry_prime_group_generator (gcry_mpi_t *r_g, 1760 gcry_mpi_t prime, 1761 gcry_mpi_t *factors, 1762 gcry_mpi_t start_g); 1763 1764 1765 /* Convenience function to release the FACTORS array. */ 1766 void gcry_prime_release_factors (gcry_mpi_t *factors); 1767 1768 1769 /* Check whether the number X is prime. */ 1770 gcry_error_t gcry_prime_check (gcry_mpi_t x, unsigned int flags); 1771 1772 1773 1774 /************************************ 1775 * * 1776 * Miscellaneous Stuff * 1777 * * 1778 ************************************/ 1779 1780 /* Release the context object CTX. */ 1781 void gcry_ctx_release (gcry_ctx_t ctx); 1782 1783 /* Log data using Libgcrypt's own log interface. */ 1784 void gcry_log_debug (const char *fmt, ...) _GCRY_GCC_ATTR_PRINTF(1,2); 1785 void gcry_log_debughex (const char *text, const void *buffer, size_t length); 1786 void gcry_log_debugmpi (const char *text, gcry_mpi_t mpi); 1787 void gcry_log_debugpnt (const char *text, 1788 gcry_mpi_point_t point, gcry_ctx_t ctx); 1789 void gcry_log_debugsxp (const char *text, gcry_sexp_t sexp); 1790 1791 char *gcry_get_config (int mode, const char *what); 1792 1793 /* Log levels used by the internal logging facility. */ 1794 enum gcry_log_levels 1795 { 1796 GCRY_LOG_CONT = 0, /* (Continue the last log line.) */ 1797 GCRY_LOG_INFO = 10, 1798 GCRY_LOG_WARN = 20, 1799 GCRY_LOG_ERROR = 30, 1800 GCRY_LOG_FATAL = 40, 1801 GCRY_LOG_BUG = 50, 1802 GCRY_LOG_DEBUG = 100 1803 }; 1804 1805 /* Type for progress handlers. */ 1806 typedef void (*gcry_handler_progress_t) (void *, const char *, int, int, int); 1807 1808 /* Type for memory allocation handlers. */ 1809 typedef void *(*gcry_handler_alloc_t) (size_t n); 1810 1811 /* Type for secure memory check handlers. */ 1812 typedef int (*gcry_handler_secure_check_t) (const void *); 1813 1814 /* Type for memory reallocation handlers. */ 1815 typedef void *(*gcry_handler_realloc_t) (void *p, size_t n); 1816 1817 /* Type for memory free handlers. */ 1818 typedef void (*gcry_handler_free_t) (void *); 1819 1820 /* Type for out-of-memory handlers. */ 1821 typedef int (*gcry_handler_no_mem_t) (void *, size_t, unsigned int); 1822 1823 /* Type for fatal error handlers. */ 1824 typedef void (*gcry_handler_error_t) (void *, int, const char *); 1825 1826 /* Type for logging handlers. */ 1827 typedef void (*gcry_handler_log_t) (void *, int, const char *, va_list); 1828 1829 /* Certain operations can provide progress information. This function 1830 is used to register a handler for retrieving these information. */ 1831 void gcry_set_progress_handler (gcry_handler_progress_t cb, void *cb_data); 1832 1833 1834 /* Register a custom memory allocation functions. */ 1835 void gcry_set_allocation_handler ( 1836 gcry_handler_alloc_t func_alloc, 1837 gcry_handler_alloc_t func_alloc_secure, 1838 gcry_handler_secure_check_t func_secure_check, 1839 gcry_handler_realloc_t func_realloc, 1840 gcry_handler_free_t func_free); 1841 1842 /* Register a function used instead of the internal out of memory 1843 handler. */ 1844 void gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque); 1845 1846 /* Register a function used instead of the internal fatal error 1847 handler. */ 1848 void gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque); 1849 1850 /* Register a function used instead of the internal logging 1851 facility. */ 1852 void gcry_set_log_handler (gcry_handler_log_t f, void *opaque); 1853 1854 /* Reserved for future use. */ 1855 void gcry_set_gettext_handler (const char *(*f)(const char*)); 1856 1857 /* Libgcrypt uses its own memory allocation. It is important to use 1858 gcry_free () to release memory allocated by libgcrypt. */ 1859 void *gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC; 1860 void *gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; 1861 void *gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; 1862 void *gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; 1863 void *gcry_realloc (void *a, size_t n); 1864 char *gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC; 1865 void *gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC; 1866 void *gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; 1867 void *gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; 1868 void *gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; 1869 void *gcry_xrealloc (void *a, size_t n); 1870 char *gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC; 1871 void gcry_free (void *a); 1872 1873 /* Return true if A is allocated in "secure" memory. */ 1874 int gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE; 1875 1876 /* Return true if Libgcrypt is in FIPS mode. */ 1877 #define gcry_fips_mode_active() !!gcry_control (GCRYCTL_FIPS_MODE_P, 0) 1878 1879 /* Variant of gcry_pk_sign which takes as additional parameter a HD 1880 * handle for hash and an optional context. The hash algorithm used by the 1881 * handle needs to be enabled and input needs to be supplied beforehand. 1882 * DATA-TMPL specifies a template to compose an S-expression to be signed. 1883 * A template should include '(hash %s %b)' or '(hash ALGONAME %b)'. 1884 * For the former case, '%s' is substituted by the string of algorithm 1885 * of gcry_md_get_algo (HD) and when gcry_md_read is called, ALGO=0 is 1886 * used internally. For the latter case, hash algorithm by ALGONAME 1887 * is used when gcry_md_read is called internally. 1888 * The hash handle must not yet been finalized; the function 1889 * takes a copy of the state and does a finalize on the copy. This 1890 * function shall be used if a policy requires that hashing and signing 1891 * is done by the same function. CTX is currently not used and should 1892 * be passed as NULL. */ 1893 gcry_error_t gcry_pk_hash_sign (gcry_sexp_t *result, 1894 const char *data_tmpl, gcry_sexp_t skey, 1895 gcry_md_hd_t hd, gcry_ctx_t ctx); 1896 1897 /* Variant of gcry_pk_verify which takes as additional parameter a HD 1898 * handle for hash and an optional context. Similar to gcry_pk_hash_sign. */ 1899 gcry_error_t gcry_pk_hash_verify (gcry_sexp_t sigval, 1900 const char *data_tmpl, gcry_sexp_t pkey, 1901 gcry_md_hd_t hd, gcry_ctx_t ctx); 1902 1903 gcry_error_t gcry_pk_random_override_new (gcry_ctx_t *r_ctx, 1904 const unsigned char *p, size_t len); 1905 1906 #if 0 /* (Keep Emacsens' auto-indent happy.) */ 1907 { 1908 #endif 1909 #ifdef __cplusplus 1910 } 1911 #endif 1912 #endif /* _GCRYPT_H */ 1913 /* 1914 Local Variables: 1915 buffer-read-only: t 1916 End: 1917 */