1/* gcrypt.h - GNU Cryptographic Library Interface -*- c -*- 2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006 3 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4 5 This file is part of Libgcrypt. 6 7 Libgcrypt is free software; you can redistribute it and/or modify 8 it under the terms of the GNU Lesser General Public License as 9 published by the Free Software Foundation; either version 2.1 of 10 the License, or (at your option) any later version. 11 12 Libgcrypt is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU Lesser General Public License for more details. 16 17 You should have received a copy of the GNU Lesser General Public 18 License along with this program; if not, see <http://www.gnu.org/licenses/>. 19 20 File: @configure_input@ */ 21 22#ifndef _GCRYPT_H 23#define _GCRYPT_H 24 25#include <stdlib.h> 26#include <stdarg.h> 27#include <string.h> 28 29#include <gpg-error.h> 30 31#include <sys/types.h> 32 33#if defined _WIN32 || defined __WIN32__ 34# include <winsock2.h> 35# include <ws2tcpip.h> 36# include <time.h> 37# ifndef __GNUC__ 38 typedef long ssize_t; 39 typedef int pid_t; 40# endif /*!__GNUC__*/ 41#else 42# include <sys/socket.h> 43# include <sys/time.h> 44#@INSERT_SYS_SELECT_H@ 45#endif /*!_WIN32*/ 46 47@FALLBACK_SOCKLEN_T@ 48 49/* This is required for error code compatibility. */ 50#define _GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_GCRYPT 51 52#ifdef __cplusplus 53extern "C" { 54#if 0 /* (Keep Emacsens' auto-indent happy.) */ 55} 56#endif 57#endif 58 59/* The version of this header should match the one of the library. It 60 should not be used by a program because gcry_check_version() should 61 return the same version. The purpose of this macro is to let 62 autoconf (using the AM_PATH_GCRYPT macro) check that this header 63 matches the installed library. */ 64#define GCRYPT_VERSION "@VERSION@" 65 66/* Internal: We can't use the convenience macros for the multi 67 precision integer functions when building this library. */ 68#ifdef _GCRYPT_IN_LIBGCRYPT 69#ifndef GCRYPT_NO_MPI_MACROS 70#define GCRYPT_NO_MPI_MACROS 1 71#endif 72#endif 73 74/* We want to use gcc attributes when possible. Warning: Don't use 75 these macros in your programs: As indicated by the leading 76 underscore they are subject to change without notice. */ 77#ifdef __GNUC__ 78 79#define _GCRY_GCC_VERSION (__GNUC__ * 10000 \ 80 + __GNUC_MINOR__ * 100 \ 81 + __GNUC_PATCHLEVEL__) 82 83#if _GCRY_GCC_VERSION >= 30100 84#define _GCRY_GCC_ATTR_DEPRECATED __attribute__ ((__deprecated__)) 85#endif 86 87#if _GCRY_GCC_VERSION >= 29600 88#define _GCRY_GCC_ATTR_PURE __attribute__ ((__pure__)) 89#endif 90 91#if _GCRY_GCC_VERSION >= 30200 92#define _GCRY_GCC_ATTR_MALLOC __attribute__ ((__malloc__)) 93#endif 94 95#endif /*__GNUC__*/ 96 97#ifndef _GCRY_GCC_ATTR_DEPRECATED 98#define _GCRY_GCC_ATTR_DEPRECATED 99#endif 100#ifndef _GCRY_GCC_ATTR_PURE 101#define _GCRY_GCC_ATTR_PURE 102#endif 103#ifndef _GCRY_GCC_ATTR_MALLOC 104#define _GCRY_GCC_ATTR_MALLOC 105#endif 106 107/* Make up an attribute to mark functions and types as deprecated but 108 allow internal use by Libgcrypt. */ 109#ifdef _GCRYPT_IN_LIBGCRYPT 110#define _GCRY_ATTR_INTERNAL 111#else 112#define _GCRY_ATTR_INTERNAL _GCRY_GCC_ATTR_DEPRECATED 113#endif 114 115/* Wrappers for the libgpg-error library. */ 116 117typedef gpg_error_t gcry_error_t; 118typedef gpg_err_code_t gcry_err_code_t; 119typedef gpg_err_source_t gcry_err_source_t; 120 121static GPG_ERR_INLINE gcry_error_t 122gcry_err_make (gcry_err_source_t source, gcry_err_code_t code) 123{ 124 return gpg_err_make (source, code); 125} 126 127/* The user can define GPG_ERR_SOURCE_DEFAULT before including this 128 file to specify a default source for gpg_error. */ 129#ifndef GCRY_ERR_SOURCE_DEFAULT 130#define GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_1 131#endif 132 133static GPG_ERR_INLINE gcry_error_t 134gcry_error (gcry_err_code_t code) 135{ 136 return gcry_err_make (GCRY_ERR_SOURCE_DEFAULT, code); 137} 138 139static GPG_ERR_INLINE gcry_err_code_t 140gcry_err_code (gcry_error_t err) 141{ 142 return gpg_err_code (err); 143} 144 145 146static GPG_ERR_INLINE gcry_err_source_t 147gcry_err_source (gcry_error_t err) 148{ 149 return gpg_err_source (err); 150} 151 152/* Return a pointer to a string containing a description of the error 153 code in the error value ERR. */ 154const char *gcry_strerror (gcry_error_t err); 155 156/* Return a pointer to a string containing a description of the error 157 source in the error value ERR. */ 158const char *gcry_strsource (gcry_error_t err); 159 160/* Retrieve the error code for the system error ERR. This returns 161 GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report 162 this). */ 163gcry_err_code_t gcry_err_code_from_errno (int err); 164 165/* Retrieve the system error for the error code CODE. This returns 0 166 if CODE is not a system error code. */ 167int gcry_err_code_to_errno (gcry_err_code_t code); 168 169/* Return an error value with the error source SOURCE and the system 170 error ERR. */ 171gcry_error_t gcry_err_make_from_errno (gcry_err_source_t source, int err); 172 173/* Return an error value with the system error ERR. */ 174gcry_err_code_t gcry_error_from_errno (int err); 175 176 177/* This enum is deprecated; it is only declared for the sake of 178 complete API compatibility. */ 179enum gcry_thread_option 180 { 181 _GCRY_THREAD_OPTION_DUMMY 182 } _GCRY_GCC_ATTR_DEPRECATED; 183 184 185/* Constants defining the thread model to use. Used with the OPTION 186 field of the struct gcry_thread_cbs. */ 187#define GCRY_THREAD_OPTION_DEFAULT 0 188#define GCRY_THREAD_OPTION_USER 1 189#define GCRY_THREAD_OPTION_PTH 2 190#define GCRY_THREAD_OPTION_PTHREAD 3 191 192/* The version number encoded in the OPTION field of the struct 193 gcry_thread_cbs. */ 194#define GCRY_THREAD_OPTION_VERSION 0 195 196/* Wrapper for struct ath_ops. */ 197struct gcry_thread_cbs 198{ 199 /* The OPTION field encodes the thread model and the version number 200 of this structure. 201 Bits 7 - 0 are used for the thread model 202 Bits 15 - 8 are used for the version number. 203 */ 204 unsigned int option; 205 206 int (*init) (void); 207 int (*mutex_init) (void **priv); 208 int (*mutex_destroy) (void **priv); 209 int (*mutex_lock) (void **priv); 210 int (*mutex_unlock) (void **priv); 211 ssize_t (*read) (int fd, void *buf, size_t nbytes); 212 ssize_t (*write) (int fd, const void *buf, size_t nbytes); 213#ifdef _WIN32 214 ssize_t (*select) (int nfd, void *rset, void *wset, void *eset, 215 struct timeval *timeout); 216 ssize_t (*waitpid) (pid_t pid, int *status, int options); 217 int (*accept) (int s, void *addr, int *length_ptr); 218 int (*connect) (int s, void *addr, gcry_socklen_t length); 219 int (*sendmsg) (int s, const void *msg, int flags); 220 int (*recvmsg) (int s, void *msg, int flags); 221#else 222 ssize_t (*select) (int nfd, fd_set *rset, fd_set *wset, fd_set *eset, 223 struct timeval *timeout); 224 ssize_t (*waitpid) (pid_t pid, int *status, int options); 225 int (*accept) (int s, struct sockaddr *addr, gcry_socklen_t *length_ptr); 226 int (*connect) (int s, struct sockaddr *addr, gcry_socklen_t length); 227 int (*sendmsg) (int s, const struct msghdr *msg, int flags); 228 int (*recvmsg) (int s, struct msghdr *msg, int flags); 229#endif 230}; 231 232#ifdef _WIN32 233# define _GCRY_THREAD_OPTION_PTH_IMPL_NET \ 234static ssize_t gcry_pth_select (int nfd, void *rset, void *wset, \ 235 void *eset, struct timeval *timeout) \ 236 { return pth_select (nfd, rset, wset, eset, timeout); } \ 237static ssize_t gcry_pth_waitpid (pid_t pid, int *status, int options) \ 238 { return pth_waitpid (pid, status, options); } \ 239static int gcry_pth_accept (int s, void *addr, \ 240 gcry_socklen_t *length_ptr) \ 241 { return pth_accept (s, addr, length_ptr); } \ 242static int gcry_pth_connect (int s, void *addr, \ 243 gcry_socklen_t length) \ 244 { return pth_connect (s, addr, length); } 245#else /*!_WIN32*/ 246# define _GCRY_THREAD_OPTION_PTH_IMPL_NET \ 247static ssize_t gcry_pth_select (int nfd, fd_set *rset, fd_set *wset, \ 248 fd_set *eset, struct timeval *timeout) \ 249 { return pth_select (nfd, rset, wset, eset, timeout); } \ 250static ssize_t gcry_pth_waitpid (pid_t pid, int *status, int options) \ 251 { return pth_waitpid (pid, status, options); } \ 252static int gcry_pth_accept (int s, struct sockaddr *addr, \ 253 gcry_socklen_t *length_ptr) \ 254 { return pth_accept (s, addr, length_ptr); } \ 255static int gcry_pth_connect (int s, struct sockaddr *addr, \ 256 gcry_socklen_t length) \ 257 { return pth_connect (s, addr, length); } 258#endif /*!_WIN32*/ 259 260 261 262#define GCRY_THREAD_OPTION_PTH_IMPL \ 263static int gcry_pth_init (void) \ 264{ return (pth_init () == FALSE) ? errno : 0; } \ 265static int gcry_pth_mutex_init (void **priv) \ 266{ \ 267 int err = 0; \ 268 pth_mutex_t *lock = malloc (sizeof (pth_mutex_t)); \ 269 \ 270 if (!lock) \ 271 err = ENOMEM; \ 272 if (!err) \ 273 { \ 274 err = pth_mutex_init (lock); \ 275 if (err == FALSE) \ 276 err = errno; \ 277 else \ 278 err = 0; \ 279 if (err) \ 280 free (lock); \ 281 else \ 282 *priv = lock; \ 283 } \ 284 return err; \ 285} \ 286static int gcry_pth_mutex_destroy (void **lock) \ 287 { /* GNU Pth has no destructor function. */ free (*lock); return 0; } \ 288static int gcry_pth_mutex_lock (void **lock) \ 289 { return ((pth_mutex_acquire (*lock, 0, NULL)) == FALSE) \ 290 ? errno : 0; } \ 291static int gcry_pth_mutex_unlock (void **lock) \ 292 { return ((pth_mutex_release (*lock)) == FALSE) \ 293 ? errno : 0; } \ 294static ssize_t gcry_pth_read (int fd, void *buf, size_t nbytes) \ 295 { return pth_read (fd, buf, nbytes); } \ 296static ssize_t gcry_pth_write (int fd, const void *buf, size_t nbytes) \ 297 { return pth_write (fd, buf, nbytes); } \ 298_GCRY_THREAD_OPTION_PTH_IMPL_NET \ 299 \ 300/* Note: GNU Pth is missing pth_sendmsg and pth_recvmsg. */ \ 301static struct gcry_thread_cbs gcry_threads_pth = { \ 302 (GCRY_THREAD_OPTION_PTH | (GCRY_THREAD_OPTION_VERSION << 8)), \ 303 gcry_pth_init, gcry_pth_mutex_init, gcry_pth_mutex_destroy, \ 304 gcry_pth_mutex_lock, gcry_pth_mutex_unlock, gcry_pth_read, gcry_pth_write, \ 305 gcry_pth_select, gcry_pth_waitpid, gcry_pth_accept, gcry_pth_connect, \ 306 NULL, NULL } 307 308 309#define GCRY_THREAD_OPTION_PTHREAD_IMPL \ 310static int gcry_pthread_mutex_init (void **priv) \ 311{ \ 312 int err = 0; \ 313 pthread_mutex_t *lock = (pthread_mutex_t*)malloc (sizeof (pthread_mutex_t));\ 314 \ 315 if (!lock) \ 316 err = ENOMEM; \ 317 if (!err) \ 318 { \ 319 err = pthread_mutex_init (lock, NULL); \ 320 if (err) \ 321 free (lock); \ 322 else \ 323 *priv = lock; \ 324 } \ 325 return err; \ 326} \ 327static int gcry_pthread_mutex_destroy (void **lock) \ 328 { int err = pthread_mutex_destroy ((pthread_mutex_t*)*lock); \ 329 free (*lock); return err; } \ 330static int gcry_pthread_mutex_lock (void **lock) \ 331 { return pthread_mutex_lock ((pthread_mutex_t*)*lock); } \ 332static int gcry_pthread_mutex_unlock (void **lock) \ 333 { return pthread_mutex_unlock ((pthread_mutex_t*)*lock); } \ 334 \ 335static struct gcry_thread_cbs gcry_threads_pthread = { \ 336 (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)), \ 337 NULL, gcry_pthread_mutex_init, gcry_pthread_mutex_destroy, \ 338 gcry_pthread_mutex_lock, gcry_pthread_mutex_unlock, \ 339 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } 340 341 342/* The data object used to hold a multi precision integer. */ 343struct gcry_mpi; 344typedef struct gcry_mpi *gcry_mpi_t; 345 346#ifndef GCRYPT_NO_DEPRECATED 347typedef struct gcry_mpi *GCRY_MPI _GCRY_GCC_ATTR_DEPRECATED; 348typedef struct gcry_mpi *GcryMPI _GCRY_GCC_ATTR_DEPRECATED; 349#endif 350 351 352 353/* Check that the library fulfills the version requirement. */ 354const char *gcry_check_version (const char *req_version); 355 356/* Codes for function dispatchers. */ 357 358/* Codes used with the gcry_control function. */ 359enum gcry_ctl_cmds 360 { 361 GCRYCTL_SET_KEY = 1, 362 GCRYCTL_SET_IV = 2, 363 GCRYCTL_CFB_SYNC = 3, 364 GCRYCTL_RESET = 4, /* e.g. for MDs */ 365 GCRYCTL_FINALIZE = 5, 366 GCRYCTL_GET_KEYLEN = 6, 367 GCRYCTL_GET_BLKLEN = 7, 368 GCRYCTL_TEST_ALGO = 8, 369 GCRYCTL_IS_SECURE = 9, 370 GCRYCTL_GET_ASNOID = 10, 371 GCRYCTL_ENABLE_ALGO = 11, 372 GCRYCTL_DISABLE_ALGO = 12, 373 GCRYCTL_DUMP_RANDOM_STATS = 13, 374 GCRYCTL_DUMP_SECMEM_STATS = 14, 375 GCRYCTL_GET_ALGO_NPKEY = 15, 376 GCRYCTL_GET_ALGO_NSKEY = 16, 377 GCRYCTL_GET_ALGO_NSIGN = 17, 378 GCRYCTL_GET_ALGO_NENCR = 18, 379 GCRYCTL_SET_VERBOSITY = 19, 380 GCRYCTL_SET_DEBUG_FLAGS = 20, 381 GCRYCTL_CLEAR_DEBUG_FLAGS = 21, 382 GCRYCTL_USE_SECURE_RNDPOOL= 22, 383 GCRYCTL_DUMP_MEMORY_STATS = 23, 384 GCRYCTL_INIT_SECMEM = 24, 385 GCRYCTL_TERM_SECMEM = 25, 386 GCRYCTL_DISABLE_SECMEM_WARN = 27, 387 GCRYCTL_SUSPEND_SECMEM_WARN = 28, 388 GCRYCTL_RESUME_SECMEM_WARN = 29, 389 GCRYCTL_DROP_PRIVS = 30, 390 GCRYCTL_ENABLE_M_GUARD = 31, 391 GCRYCTL_START_DUMP = 32, 392 GCRYCTL_STOP_DUMP = 33, 393 GCRYCTL_GET_ALGO_USAGE = 34, 394 GCRYCTL_IS_ALGO_ENABLED = 35, 395 GCRYCTL_DISABLE_INTERNAL_LOCKING = 36, 396 GCRYCTL_DISABLE_SECMEM = 37, 397 GCRYCTL_INITIALIZATION_FINISHED = 38, 398 GCRYCTL_INITIALIZATION_FINISHED_P = 39, 399 GCRYCTL_ANY_INITIALIZATION_P = 40, 400 GCRYCTL_SET_CBC_CTS = 41, 401 GCRYCTL_SET_CBC_MAC = 42, 402 GCRYCTL_SET_CTR = 43, 403 GCRYCTL_ENABLE_QUICK_RANDOM = 44, 404 GCRYCTL_SET_RANDOM_SEED_FILE = 45, 405 GCRYCTL_UPDATE_RANDOM_SEED_FILE = 46, 406 GCRYCTL_SET_THREAD_CBS = 47, 407 GCRYCTL_FAST_POLL = 48, 408 GCRYCTL_SET_RANDOM_DAEMON_SOCKET = 49, 409 GCRYCTL_USE_RANDOM_DAEMON = 50, 410 GCRYCTL_FAKED_RANDOM_P = 51, 411 GCRYCTL_SET_RNDEGD_SOCKET = 52, 412 GCRYCTL_PRINT_CONFIG = 53, 413 GCRYCTL_OPERATIONAL_P = 54, 414 GCRYCTL_FIPS_MODE_P = 55, 415 GCRYCTL_FORCE_FIPS_MODE = 56, 416 GCRYCTL_SELFTEST = 57, 417 /* Note: 58 .. 62 are used internally. */ 418 GCRYCTL_DISABLE_HWF = 63 419 }; 420 421/* Perform various operations defined by CMD. */ 422gcry_error_t gcry_control (enum gcry_ctl_cmds CMD, ...); 423 424 425/* S-expression management. */ 426 427/* The object to represent an S-expression as used with the public key 428 functions. */ 429struct gcry_sexp; 430typedef struct gcry_sexp *gcry_sexp_t; 431 432#ifndef GCRYPT_NO_DEPRECATED 433typedef struct gcry_sexp *GCRY_SEXP _GCRY_GCC_ATTR_DEPRECATED; 434typedef struct gcry_sexp *GcrySexp _GCRY_GCC_ATTR_DEPRECATED; 435#endif 436 437/* The possible values for the S-expression format. */ 438enum gcry_sexp_format 439 { 440 GCRYSEXP_FMT_DEFAULT = 0, 441 GCRYSEXP_FMT_CANON = 1, 442 GCRYSEXP_FMT_BASE64 = 2, 443 GCRYSEXP_FMT_ADVANCED = 3 444 }; 445 446/* Create an new S-expression object from BUFFER of size LENGTH and 447 return it in RETSEXP. With AUTODETECT set to 0 the data in BUFFER 448 is expected to be in canonized format. */ 449gcry_error_t gcry_sexp_new (gcry_sexp_t *retsexp, 450 const void *buffer, size_t length, 451 int autodetect); 452 453 /* Same as gcry_sexp_new but allows to pass a FREEFNC which has the 454 effect to transfer ownership of BUFFER to the created object. */ 455gcry_error_t gcry_sexp_create (gcry_sexp_t *retsexp, 456 void *buffer, size_t length, 457 int autodetect, void (*freefnc) (void *)); 458 459/* Scan BUFFER and return a new S-expression object in RETSEXP. This 460 function expects a printf like string in BUFFER. */ 461gcry_error_t gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff, 462 const char *buffer, size_t length); 463 464/* Same as gcry_sexp_sscan but expects a string in FORMAT and can thus 465 only be used for certain encodings. */ 466gcry_error_t gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff, 467 const char *format, ...); 468 469/* Like gcry_sexp_build, but uses an array instead of variable 470 function arguments. */ 471gcry_error_t gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff, 472 const char *format, void **arg_list); 473 474/* Release the S-expression object SEXP */ 475void gcry_sexp_release (gcry_sexp_t sexp); 476 477/* Calculate the length of an canonized S-expresion in BUFFER and 478 check for a valid encoding. */ 479size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length, 480 size_t *erroff, gcry_error_t *errcode); 481 482/* Copies the S-expression object SEXP into BUFFER using the format 483 specified in MODE. */ 484size_t gcry_sexp_sprint (gcry_sexp_t sexp, int mode, void *buffer, 485 size_t maxlength); 486 487/* Dumps the S-expression object A in a format suitable for debugging 488 to Libgcrypt's logging stream. */ 489void gcry_sexp_dump (const gcry_sexp_t a); 490 491gcry_sexp_t gcry_sexp_cons (const gcry_sexp_t a, const gcry_sexp_t b); 492gcry_sexp_t gcry_sexp_alist (const gcry_sexp_t *array); 493gcry_sexp_t gcry_sexp_vlist (const gcry_sexp_t a, ...); 494gcry_sexp_t gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n); 495gcry_sexp_t gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n); 496 497/* Scan the S-expression for a sublist with a type (the car of the 498 list) matching the string TOKEN. If TOKLEN is not 0, the token is 499 assumed to be raw memory of this length. The function returns a 500 newly allocated S-expression consisting of the found sublist or 501 `NULL' when not found. */ 502gcry_sexp_t gcry_sexp_find_token (gcry_sexp_t list, 503 const char *tok, size_t toklen); 504/* Return the length of the LIST. For a valid S-expression this 505 should be at least 1. */ 506int gcry_sexp_length (const gcry_sexp_t list); 507 508/* Create and return a new S-expression from the element with index 509 NUMBER in LIST. Note that the first element has the index 0. If 510 there is no such element, `NULL' is returned. */ 511gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t list, int number); 512 513/* Create and return a new S-expression from the first element in 514 LIST; this called the "type" and should always exist and be a 515 string. `NULL' is returned in case of a problem. */ 516gcry_sexp_t gcry_sexp_car (const gcry_sexp_t list); 517 518/* Create and return a new list form all elements except for the first 519 one. Note, that this function may return an invalid S-expression 520 because it is not guaranteed, that the type exists and is a string. 521 However, for parsing a complex S-expression it might be useful for 522 intermediate lists. Returns `NULL' on error. */ 523gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t list); 524 525gcry_sexp_t gcry_sexp_cadr (const gcry_sexp_t list); 526 527 528/* This function is used to get data from a LIST. A pointer to the 529 actual data with index NUMBER is returned and the length of this 530 data will be stored to DATALEN. If there is no data at the given 531 index or the index represents another list, `NULL' is returned. 532 *Note:* The returned pointer is valid as long as LIST is not 533 modified or released. */ 534const char *gcry_sexp_nth_data (const gcry_sexp_t list, int number, 535 size_t *datalen); 536 537/* This function is used to get and convert data from a LIST. The 538 data is assumed to be a Nul terminated string. The caller must 539 release the returned value using `gcry_free'. If there is no data 540 at the given index, the index represents a list or the value can't 541 be converted to a string, `NULL' is returned. */ 542char *gcry_sexp_nth_string (gcry_sexp_t list, int number); 543 544/* This function is used to get and convert data from a LIST. This 545 data is assumed to be an MPI stored in the format described by 546 MPIFMT and returned as a standard Libgcrypt MPI. The caller must 547 release this returned value using `gcry_mpi_release'. If there is 548 no data at the given index, the index represents a list or the 549 value can't be converted to an MPI, `NULL' is returned. */ 550gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt); 551 552 553 554/******************************************* 555 * * 556 * Multi Precision Integer Functions * 557 * * 558 *******************************************/ 559 560/* Different formats of external big integer representation. */ 561enum gcry_mpi_format 562 { 563 GCRYMPI_FMT_NONE= 0, 564 GCRYMPI_FMT_STD = 1, /* Twos complement stored without length. */ 565 GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP (unsigned only). */ 566 GCRYMPI_FMT_SSH = 3, /* As used by SSH (like STD but with length). */ 567 GCRYMPI_FMT_HEX = 4, /* Hex format. */ 568 GCRYMPI_FMT_USG = 5 /* Like STD but unsigned. */ 569 }; 570 571/* Flags used for creating big integers. */ 572enum gcry_mpi_flag 573 { 574 GCRYMPI_FLAG_SECURE = 1, /* Allocate the number in "secure" memory. */ 575 GCRYMPI_FLAG_OPAQUE = 2 /* The number is not a real one but just 576 a way to store some bytes. This is 577 useful for encrypted big integers. */ 578 }; 579 580 581/* Allocate a new big integer object, initialize it with 0 and 582 initially allocate memory for a number of at least NBITS. */ 583gcry_mpi_t gcry_mpi_new (unsigned int nbits); 584 585/* Same as gcry_mpi_new() but allocate in "secure" memory. */ 586gcry_mpi_t gcry_mpi_snew (unsigned int nbits); 587 588/* Release the number A and free all associated resources. */ 589void gcry_mpi_release (gcry_mpi_t a); 590 591/* Create a new number with the same value as A. */ 592gcry_mpi_t gcry_mpi_copy (const gcry_mpi_t a); 593 594/* Store the big integer value U in W. */ 595gcry_mpi_t gcry_mpi_set (gcry_mpi_t w, const gcry_mpi_t u); 596 597/* Store the unsigned integer value U in W. */ 598gcry_mpi_t gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u); 599 600/* Swap the values of A and B. */ 601void gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b); 602 603/* Compare the big integer number U and V returning 0 for equality, a 604 positive value for U > V and a negative for U < V. */ 605int gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v); 606 607/* Compare the big integer number U with the unsigned integer V 608 returning 0 for equality, a positive value for U > V and a negative 609 for U < V. */ 610int gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v); 611 612/* Convert the external representation of an integer stored in BUFFER 613 with a length of BUFLEN into a newly create MPI returned in 614 RET_MPI. If NSCANNED is not NULL, it will receive the number of 615 bytes actually scanned after a successful operation. */ 616gcry_error_t gcry_mpi_scan (gcry_mpi_t *ret_mpi, enum gcry_mpi_format format, 617 const void *buffer, size_t buflen, 618 size_t *nscanned); 619 620/* Convert the big integer A into the external representation 621 described by FORMAT and store it in the provided BUFFER which has 622 been allocated by the user with a size of BUFLEN bytes. NWRITTEN 623 receives the actual length of the external representation unless it 624 has been passed as NULL. */ 625gcry_error_t gcry_mpi_print (enum gcry_mpi_format format, 626 unsigned char *buffer, size_t buflen, 627 size_t *nwritten, 628 const gcry_mpi_t a); 629 630/* Convert the big integer A int the external representation described 631 by FORMAT and store it in a newly allocated buffer which address 632 will be put into BUFFER. NWRITTEN receives the actual lengths of the 633 external representation. */ 634gcry_error_t gcry_mpi_aprint (enum gcry_mpi_format format, 635 unsigned char **buffer, size_t *nwritten, 636 const gcry_mpi_t a); 637 638/* Dump the value of A in a format suitable for debugging to 639 Libgcrypt's logging stream. Note that one leading space but no 640 trailing space or linefeed will be printed. It is okay to pass 641 NULL for A. */ 642void gcry_mpi_dump (const gcry_mpi_t a); 643 644 645/* W = U + V. */ 646void gcry_mpi_add (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); 647 648/* W = U + V. V is an unsigned integer. */ 649void gcry_mpi_add_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v); 650 651/* W = U + V mod M. */ 652void gcry_mpi_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); 653 654/* W = U - V. */ 655void gcry_mpi_sub (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); 656 657/* W = U - V. V is an unsigned integer. */ 658void gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ); 659 660/* W = U - V mod M */ 661void gcry_mpi_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); 662 663/* W = U * V. */ 664void gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); 665 666/* W = U * V. V is an unsigned integer. */ 667void gcry_mpi_mul_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ); 668 669/* W = U * V mod M. */ 670void gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); 671 672/* W = U * (2 ^ CNT). */ 673void gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt); 674 675/* Q = DIVIDEND / DIVISOR, R = DIVIDEND % DIVISOR, 676 Q or R may be passed as NULL. ROUND should be negative or 0. */ 677void gcry_mpi_div (gcry_mpi_t q, gcry_mpi_t r, 678 gcry_mpi_t dividend, gcry_mpi_t divisor, int round); 679 680/* R = DIVIDEND % DIVISOR */ 681void gcry_mpi_mod (gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor); 682 683/* W = B ^ E mod M. */ 684void gcry_mpi_powm (gcry_mpi_t w, 685 const gcry_mpi_t b, const gcry_mpi_t e, 686 const gcry_mpi_t m); 687 688/* Set G to the greatest common divisor of A and B. 689 Return true if the G is 1. */ 690int gcry_mpi_gcd (gcry_mpi_t g, gcry_mpi_t a, gcry_mpi_t b); 691 692/* Set X to the multiplicative inverse of A mod M. 693 Return true if the value exists. */ 694int gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t m); 695 696 697/* Return the number of bits required to represent A. */ 698unsigned int gcry_mpi_get_nbits (gcry_mpi_t a); 699 700/* Return true when bit number N (counting from 0) is set in A. */ 701int gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n); 702 703/* Set bit number N in A. */ 704void gcry_mpi_set_bit (gcry_mpi_t a, unsigned int n); 705 706/* Clear bit number N in A. */ 707void gcry_mpi_clear_bit (gcry_mpi_t a, unsigned int n); 708 709/* Set bit number N in A and clear all bits greater than N. */ 710void gcry_mpi_set_highbit (gcry_mpi_t a, unsigned int n); 711 712/* Clear bit number N in A and all bits greater than N. */ 713void gcry_mpi_clear_highbit (gcry_mpi_t a, unsigned int n); 714 715/* Shift the value of A by N bits to the right and store the result in X. */ 716void gcry_mpi_rshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n); 717 718/* Shift the value of A by N bits to the left and store the result in X. */ 719void gcry_mpi_lshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n); 720 721/* Store NBITS of the value P points to in A and mark A as an opaque 722 value. WARNING: Never use an opaque MPI for anything thing else then 723 gcry_mpi_release, gcry_mpi_get_opaque. */ 724gcry_mpi_t gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits); 725 726/* Return a pointer to an opaque value stored in A and return its size 727 in NBITS. Note that the returned pointer is still owned by A and 728 that the function should never be used for an non-opaque MPI. */ 729void *gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits); 730 731/* Set the FLAG for the big integer A. Currently only the flag 732 GCRYMPI_FLAG_SECURE is allowed to convert A into an big intger 733 stored in "secure" memory. */ 734void gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); 735 736/* Clear FLAG for the big integer A. Note that this function is 737 currently useless as no flags are allowed. */ 738void gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); 739 740/* Return true when the FLAG is set for A. */ 741int gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); 742 743/* Unless the GCRYPT_NO_MPI_MACROS is used, provide a couple of 744 convenience macros for the big integer functions. */ 745#ifndef GCRYPT_NO_MPI_MACROS 746#define mpi_new(n) gcry_mpi_new( (n) ) 747#define mpi_secure_new( n ) gcry_mpi_snew( (n) ) 748#define mpi_release(a) \ 749 do \ 750 { \ 751 gcry_mpi_release ((a)); \ 752 (a) = NULL; \ 753 } \ 754 while (0) 755 756#define mpi_copy( a ) gcry_mpi_copy( (a) ) 757#define mpi_set( w, u) gcry_mpi_set( (w), (u) ) 758#define mpi_set_ui( w, u) gcry_mpi_set_ui( (w), (u) ) 759#define mpi_cmp( u, v ) gcry_mpi_cmp( (u), (v) ) 760#define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) ) 761 762#define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v)) 763#define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v)) 764#define mpi_addm(w,u,v,m) gcry_mpi_addm ((w),(u),(v),(m)) 765#define mpi_sub_ui(w,u,v) gcry_mpi_sub_ui ((w),(u),(v)) 766#define mpi_sub(w,u,v) gcry_mpi_sub ((w),(u),(v)) 767#define mpi_subm(w,u,v,m) gcry_mpi_subm ((w),(u),(v),(m)) 768#define mpi_mul_ui(w,u,v) gcry_mpi_mul_ui ((w),(u),(v)) 769#define mpi_mul_2exp(w,u,v) gcry_mpi_mul_2exp ((w),(u),(v)) 770#define mpi_mul(w,u,v) gcry_mpi_mul ((w),(u),(v)) 771#define mpi_mulm(w,u,v,m) gcry_mpi_mulm ((w),(u),(v),(m)) 772#define mpi_powm(w,b,e,m) gcry_mpi_powm ( (w), (b), (e), (m) ) 773#define mpi_tdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), 0) 774#define mpi_fdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), -1) 775#define mpi_mod(r,a,m) gcry_mpi_mod ((r), (a), (m)) 776#define mpi_gcd(g,a,b) gcry_mpi_gcd ( (g), (a), (b) ) 777#define mpi_invm(g,a,b) gcry_mpi_invm ( (g), (a), (b) ) 778 779#define mpi_get_nbits(a) gcry_mpi_get_nbits ((a)) 780#define mpi_test_bit(a,b) gcry_mpi_test_bit ((a),(b)) 781#define mpi_set_bit(a,b) gcry_mpi_set_bit ((a),(b)) 782#define mpi_set_highbit(a,b) gcry_mpi_set_highbit ((a),(b)) 783#define mpi_clear_bit(a,b) gcry_mpi_clear_bit ((a),(b)) 784#define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b)) 785#define mpi_rshift(a,b,c) gcry_mpi_rshift ((a),(b),(c)) 786#define mpi_lshift(a,b,c) gcry_mpi_lshift ((a),(b),(c)) 787 788#define mpi_set_opaque(a,b,c) gcry_mpi_set_opaque( (a), (b), (c) ) 789#define mpi_get_opaque(a,b) gcry_mpi_get_opaque( (a), (b) ) 790#endif /* GCRYPT_NO_MPI_MACROS */ 791 792 793 794/************************************ 795 * * 796 * Symmetric Cipher Functions * 797 * * 798 ************************************/ 799 800/* The data object used to hold a handle to an encryption object. */ 801struct gcry_cipher_handle; 802typedef struct gcry_cipher_handle *gcry_cipher_hd_t; 803 804#ifndef GCRYPT_NO_DEPRECATED 805typedef struct gcry_cipher_handle *GCRY_CIPHER_HD _GCRY_GCC_ATTR_DEPRECATED; 806typedef struct gcry_cipher_handle *GcryCipherHd _GCRY_GCC_ATTR_DEPRECATED; 807#endif 808 809/* All symmetric encryption algorithms are identified by their IDs. 810 More IDs may be registered at runtime. */ 811enum gcry_cipher_algos 812 { 813 GCRY_CIPHER_NONE = 0, 814 GCRY_CIPHER_IDEA = 1, 815 GCRY_CIPHER_3DES = 2, 816 GCRY_CIPHER_CAST5 = 3, 817 GCRY_CIPHER_BLOWFISH = 4, 818 GCRY_CIPHER_SAFER_SK128 = 5, 819 GCRY_CIPHER_DES_SK = 6, 820 GCRY_CIPHER_AES = 7, 821 GCRY_CIPHER_AES192 = 8, 822 GCRY_CIPHER_AES256 = 9, 823 GCRY_CIPHER_TWOFISH = 10, 824 825 /* Other cipher numbers are above 300 for OpenPGP reasons. */ 826 GCRY_CIPHER_ARCFOUR = 301, /* Fully compatible with RSA's RC4 (tm). */ 827 GCRY_CIPHER_DES = 302, /* Yes, this is single key 56 bit DES. */ 828 GCRY_CIPHER_TWOFISH128 = 303, 829 GCRY_CIPHER_SERPENT128 = 304, 830 GCRY_CIPHER_SERPENT192 = 305, 831 GCRY_CIPHER_SERPENT256 = 306, 832 GCRY_CIPHER_RFC2268_40 = 307, /* Ron's Cipher 2 (40 bit). */ 833 GCRY_CIPHER_RFC2268_128 = 308, /* Ron's Cipher 2 (128 bit). */ 834 GCRY_CIPHER_SEED = 309, /* 128 bit cipher described in RFC4269. */ 835 GCRY_CIPHER_CAMELLIA128 = 310, 836 GCRY_CIPHER_CAMELLIA192 = 311, 837 GCRY_CIPHER_CAMELLIA256 = 312 838 }; 839 840/* The Rijndael algorithm is basically AES, so provide some macros. */ 841#define GCRY_CIPHER_AES128 GCRY_CIPHER_AES 842#define GCRY_CIPHER_RIJNDAEL GCRY_CIPHER_AES 843#define GCRY_CIPHER_RIJNDAEL128 GCRY_CIPHER_AES128 844#define GCRY_CIPHER_RIJNDAEL192 GCRY_CIPHER_AES192 845#define GCRY_CIPHER_RIJNDAEL256 GCRY_CIPHER_AES256 846 847/* The supported encryption modes. Note that not all of them are 848 supported for each algorithm. */ 849enum gcry_cipher_modes 850 { 851 GCRY_CIPHER_MODE_NONE = 0, /* Not yet specified. */ 852 GCRY_CIPHER_MODE_ECB = 1, /* Electronic codebook. */ 853 GCRY_CIPHER_MODE_CFB = 2, /* Cipher feedback. */ 854 GCRY_CIPHER_MODE_CBC = 3, /* Cipher block chaining. */ 855 GCRY_CIPHER_MODE_STREAM = 4, /* Used with stream ciphers. */ 856 GCRY_CIPHER_MODE_OFB = 5, /* Outer feedback. */ 857 GCRY_CIPHER_MODE_CTR = 6, /* Counter. */ 858 GCRY_CIPHER_MODE_AESWRAP= 7 /* AES-WRAP algorithm. */ 859 }; 860 861/* Flags used with the open function. */ 862enum gcry_cipher_flags 863 { 864 GCRY_CIPHER_SECURE = 1, /* Allocate in secure memory. */ 865 GCRY_CIPHER_ENABLE_SYNC = 2, /* Enable CFB sync mode. */ 866 GCRY_CIPHER_CBC_CTS = 4, /* Enable CBC cipher text stealing (CTS). */ 867 GCRY_CIPHER_CBC_MAC = 8 /* Enable CBC message auth. code (MAC). */ 868 }; 869 870 871/* Create a handle for algorithm ALGO to be used in MODE. FLAGS may 872 be given as an bitwise OR of the gcry_cipher_flags values. */ 873gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *handle, 874 int algo, int mode, unsigned int flags); 875 876/* Close the cioher handle H and release all resource. */ 877void gcry_cipher_close (gcry_cipher_hd_t h); 878 879/* Perform various operations on the cipher object H. */ 880gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, 881 size_t buflen); 882 883/* Retrieve various information about the cipher object H. */ 884gcry_error_t gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer, 885 size_t *nbytes); 886 887/* Retrieve various information about the cipher algorithm ALGO. */ 888gcry_error_t gcry_cipher_algo_info (int algo, int what, void *buffer, 889 size_t *nbytes); 890 891/* Map the cipher algorithm whose ID is contained in ALGORITHM to a 892 string representation of the algorithm name. For unknown algorithm 893 IDs this function returns "?". */ 894const char *gcry_cipher_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; 895 896/* Map the algorithm name NAME to an cipher algorithm ID. Return 0 if 897 the algorithm name is not known. */ 898int gcry_cipher_map_name (const char *name) _GCRY_GCC_ATTR_PURE; 899 900/* Given an ASN.1 object identifier in standard IETF dotted decimal 901 format in STRING, return the encryption mode associated with that 902 OID or 0 if not known or applicable. */ 903int gcry_cipher_mode_from_oid (const char *string) _GCRY_GCC_ATTR_PURE; 904 905/* Encrypt the plaintext of size INLEN in IN using the cipher handle H 906 into the buffer OUT which has an allocated length of OUTSIZE. For 907 most algorithms it is possible to pass NULL for in and 0 for INLEN 908 and do a in-place decryption of the data provided in OUT. */ 909gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h, 910 void *out, size_t outsize, 911 const void *in, size_t inlen); 912 913/* The counterpart to gcry_cipher_encrypt. */ 914gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t h, 915 void *out, size_t outsize, 916 const void *in, size_t inlen); 917 918/* Set KEY of length KEYLEN bytes for the cipher handle HD. */ 919gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t hd, 920 const void *key, size_t keylen); 921 922 923/* Set initialization vector IV of length IVLEN for the cipher handle HD. */ 924gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t hd, 925 const void *iv, size_t ivlen); 926 927 928/* Reset the handle to the state after open. */ 929#define gcry_cipher_reset(h) gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0) 930 931/* Perform the OpenPGP sync operation if this is enabled for the 932 cipher handle H. */ 933#define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, NULL, 0) 934 935/* Enable or disable CTS in future calls to gcry_encrypt(). CBC mode only. */ 936#define gcry_cipher_cts(h,on) gcry_cipher_ctl( (h), GCRYCTL_SET_CBC_CTS, \ 937 NULL, on ) 938 939/* Set counter for CTR mode. (CTR,CTRLEN) must denote a buffer of 940 block size length, or (NULL,0) to set the CTR to the all-zero block. */ 941gpg_error_t gcry_cipher_setctr (gcry_cipher_hd_t hd, 942 const void *ctr, size_t ctrlen); 943 944/* Retrieved the key length in bytes used with algorithm A. */ 945size_t gcry_cipher_get_algo_keylen (int algo); 946 947/* Retrieve the block length in bytes used with algorithm A. */ 948size_t gcry_cipher_get_algo_blklen (int algo); 949 950/* Return 0 if the algorithm A is available for use. */ 951#define gcry_cipher_test_algo(a) \ 952 gcry_cipher_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) 953 954/* Get a list consisting of the IDs of the loaded cipher modules. If 955 LIST is zero, write the number of loaded cipher modules to 956 LIST_LENGTH and return. If LIST is non-zero, the first 957 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of 958 according size. In case there are less cipher modules than 959 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */ 960gcry_error_t gcry_cipher_list (int *list, int *list_length); 961 962 963/************************************ 964 * * 965 * Asymmetric Cipher Functions * 966 * * 967 ************************************/ 968 969/* The algorithms and their IDs we support. */ 970enum gcry_pk_algos 971 { 972 GCRY_PK_RSA = 1, 973 GCRY_PK_RSA_E = 2, /* (deprecated) */ 974 GCRY_PK_RSA_S = 3, /* (deprecated) */ 975 GCRY_PK_ELG_E = 16, 976 GCRY_PK_DSA = 17, 977 GCRY_PK_ELG = 20, 978 GCRY_PK_ECDSA = 301, 979 GCRY_PK_ECDH = 302 980 }; 981 982/* Flags describing usage capabilities of a PK algorithm. */ 983#define GCRY_PK_USAGE_SIGN 1 /* Good for signatures. */ 984#define GCRY_PK_USAGE_ENCR 2 /* Good for encryption. */ 985#define GCRY_PK_USAGE_CERT 4 /* Good to certify other keys. */ 986#define GCRY_PK_USAGE_AUTH 8 /* Good for authentication. */ 987#define GCRY_PK_USAGE_UNKN 128 /* Unknown usage flag. */ 988 989/* Encrypt the DATA using the public key PKEY and store the result as 990 a newly created S-expression at RESULT. */ 991gcry_error_t gcry_pk_encrypt (gcry_sexp_t *result, 992 gcry_sexp_t data, gcry_sexp_t pkey); 993 994/* Decrypt the DATA using the private key SKEY and store the result as 995 a newly created S-expression at RESULT. */ 996gcry_error_t gcry_pk_decrypt (gcry_sexp_t *result, 997 gcry_sexp_t data, gcry_sexp_t skey); 998 999/* Sign the DATA using the private key SKEY and store the result as 1000 a newly created S-expression at RESULT. */ 1001gcry_error_t gcry_pk_sign (gcry_sexp_t *result, 1002 gcry_sexp_t data, gcry_sexp_t skey); 1003 1004/* Check the signature SIGVAL on DATA using the public key PKEY. */ 1005gcry_error_t gcry_pk_verify (gcry_sexp_t sigval, 1006 gcry_sexp_t data, gcry_sexp_t pkey); 1007 1008/* Check that private KEY is sane. */ 1009gcry_error_t gcry_pk_testkey (gcry_sexp_t key); 1010 1011/* Generate a new key pair according to the parameters given in 1012 S_PARMS. The new key pair is returned in as an S-expression in 1013 R_KEY. */ 1014gcry_error_t gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms); 1015 1016/* Catch all function for miscellaneous operations. */ 1017gcry_error_t gcry_pk_ctl (int cmd, void *buffer, size_t buflen); 1018 1019/* Retrieve information about the public key algorithm ALGO. */ 1020gcry_error_t gcry_pk_algo_info (int algo, int what, 1021 void *buffer, size_t *nbytes); 1022 1023/* Map the public key algorithm whose ID is contained in ALGORITHM to 1024 a string representation of the algorithm name. For unknown 1025 algorithm IDs this functions returns "?". */ 1026const char *gcry_pk_algo_name (int algorithm) _GCRY_GCC_ATTR_PURE; 1027 1028/* Map the algorithm NAME to a public key algorithm Id. Return 0 if 1029 the algorithm name is not known. */ 1030int gcry_pk_map_name (const char* name) _GCRY_GCC_ATTR_PURE; 1031 1032/* Return what is commonly referred as the key length for the given 1033 public or private KEY. */ 1034unsigned int gcry_pk_get_nbits (gcry_sexp_t key) _GCRY_GCC_ATTR_PURE; 1035 1036/* Please note that keygrip is still experimental and should not be 1037 used without contacting the author. */ 1038unsigned char *gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array); 1039 1040/* Return the name of the curve matching KEY. */ 1041const char *gcry_pk_get_curve (gcry_sexp_t key, int iterator, 1042 unsigned int *r_nbits); 1043 1044/* Return an S-expression with the parameters of the named ECC curve 1045 NAME. ALGO must be set to an ECC algorithm. */ 1046gcry_sexp_t gcry_pk_get_param (int algo, const char *name); 1047 1048/* Return 0 if the public key algorithm A is available for use. */ 1049#define gcry_pk_test_algo(a) \ 1050 gcry_pk_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) 1051 1052/* Get a list consisting of the IDs of the loaded pubkey modules. If 1053 LIST is zero, write the number of loaded pubkey modules to 1054 LIST_LENGTH and return. If LIST is non-zero, the first 1055 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of 1056 according size. In case there are less pubkey modules than 1057 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */ 1058gcry_error_t gcry_pk_list (int *list, int *list_length); 1059 1060 1061 1062/************************************ 1063 * * 1064 * Cryptograhic Hash Functions * 1065 * * 1066 ************************************/ 1067 1068/* Algorithm IDs for the hash functions we know about. Not all of them 1069 are implemnted. */ 1070enum gcry_md_algos 1071 { 1072 GCRY_MD_NONE = 0, 1073 GCRY_MD_MD5 = 1, 1074 GCRY_MD_SHA1 = 2, 1075 GCRY_MD_RMD160 = 3, 1076 GCRY_MD_MD2 = 5, 1077 GCRY_MD_TIGER = 6, /* TIGER/192 as used by gpg <= 1.3.2. */ 1078 GCRY_MD_HAVAL = 7, /* HAVAL, 5 pass, 160 bit. */ 1079 GCRY_MD_SHA256 = 8, 1080 GCRY_MD_SHA384 = 9, 1081 GCRY_MD_SHA512 = 10, 1082 GCRY_MD_SHA224 = 11, 1083 GCRY_MD_MD4 = 301, 1084 GCRY_MD_CRC32 = 302, 1085 GCRY_MD_CRC32_RFC1510 = 303, 1086 GCRY_MD_CRC24_RFC2440 = 304, 1087 GCRY_MD_WHIRLPOOL = 305, 1088 GCRY_MD_TIGER1 = 306, /* TIGER fixed. */ 1089 GCRY_MD_TIGER2 = 307 /* TIGER2 variant. */ 1090 }; 1091 1092/* Flags used with the open function. */ 1093enum gcry_md_flags 1094 { 1095 GCRY_MD_FLAG_SECURE = 1, /* Allocate all buffers in "secure" memory. */ 1096 GCRY_MD_FLAG_HMAC = 2 /* Make an HMAC out of this algorithm. */ 1097 }; 1098 1099/* (Forward declaration.) */ 1100struct gcry_md_context; 1101 1102/* This object is used to hold a handle to a message digest object. 1103 This structure is private - only to be used by the public gcry_md_* 1104 macros. */ 1105typedef struct gcry_md_handle 1106{ 1107 /* Actual context. */ 1108 struct gcry_md_context *ctx; 1109 1110 /* Buffer management. */ 1111 int bufpos; 1112 int bufsize; 1113 unsigned char buf[1]; 1114} *gcry_md_hd_t; 1115 1116/* Compatibility types, do not use them. */ 1117#ifndef GCRYPT_NO_DEPRECATED 1118typedef struct gcry_md_handle *GCRY_MD_HD _GCRY_GCC_ATTR_DEPRECATED; 1119typedef struct gcry_md_handle *GcryMDHd _GCRY_GCC_ATTR_DEPRECATED; 1120#endif 1121 1122/* Create a message digest object for algorithm ALGO. FLAGS may be 1123 given as an bitwise OR of the gcry_md_flags values. ALGO may be 1124 given as 0 if the algorithms to be used are later set using 1125 gcry_md_enable. */ 1126gcry_error_t gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags); 1127 1128/* Release the message digest object HD. */ 1129void gcry_md_close (gcry_md_hd_t hd); 1130 1131/* Add the message digest algorithm ALGO to the digest object HD. */ 1132gcry_error_t gcry_md_enable (gcry_md_hd_t hd, int algo); 1133 1134/* Create a new digest object as an exact copy of the object HD. */ 1135gcry_error_t gcry_md_copy (gcry_md_hd_t *bhd, gcry_md_hd_t ahd); 1136 1137/* Reset the digest object HD to its initial state. */ 1138void gcry_md_reset (gcry_md_hd_t hd); 1139 1140/* Perform various operations on the digest object HD. */ 1141gcry_error_t gcry_md_ctl (gcry_md_hd_t hd, int cmd, 1142 void *buffer, size_t buflen); 1143 1144/* Pass LENGTH bytes of data in BUFFER to the digest object HD so that 1145 it can update the digest values. This is the actual hash 1146 function. */ 1147void gcry_md_write (gcry_md_hd_t hd, const void *buffer, size_t length); 1148 1149/* Read out the final digest from HD return the digest value for 1150 algorithm ALGO. */ 1151unsigned char *gcry_md_read (gcry_md_hd_t hd, int algo); 1152 1153/* Convenience function to calculate the hash from the data in BUFFER 1154 of size LENGTH using the algorithm ALGO avoiding the creating of a 1155 hash object. The hash is returned in the caller provided buffer 1156 DIGEST which must be large enough to hold the digest of the given 1157 algorithm. */ 1158void gcry_md_hash_buffer (int algo, void *digest, 1159 const void *buffer, size_t length); 1160 1161/* Retrieve the algorithm used with HD. This does not work reliable 1162 if more than one algorithm is enabled in HD. */ 1163int gcry_md_get_algo (gcry_md_hd_t hd); 1164 1165/* Retrieve the length in bytes of the digest yielded by algorithm 1166 ALGO. */ 1167unsigned int gcry_md_get_algo_dlen (int algo); 1168 1169/* Return true if the the algorithm ALGO is enabled in the digest 1170 object A. */ 1171int gcry_md_is_enabled (gcry_md_hd_t a, int algo); 1172 1173/* Return true if the digest object A is allocated in "secure" memory. */ 1174int gcry_md_is_secure (gcry_md_hd_t a); 1175 1176/* Retrieve various information about the object H. */ 1177gcry_error_t gcry_md_info (gcry_md_hd_t h, int what, void *buffer, 1178 size_t *nbytes); 1179 1180/* Retrieve various information about the algorithm ALGO. */ 1181gcry_error_t gcry_md_algo_info (int algo, int what, void *buffer, 1182 size_t *nbytes); 1183 1184/* Map the digest algorithm id ALGO to a string representation of the 1185 algorithm name. For unknown algorithms this function returns 1186 "?". */ 1187const char *gcry_md_algo_name (int algo) _GCRY_GCC_ATTR_PURE; 1188 1189/* Map the algorithm NAME to a digest algorithm Id. Return 0 if 1190 the algorithm name is not known. */ 1191int gcry_md_map_name (const char* name) _GCRY_GCC_ATTR_PURE; 1192 1193/* For use with the HMAC feature, the set MAC key to the KEY of 1194 KEYLEN bytes. */ 1195gcry_error_t gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen); 1196 1197/* Start or stop debugging for digest handle HD; i.e. create a file 1198 named dbgmd-<n>.<suffix> while hashing. If SUFFIX is NULL, 1199 debugging stops and the file will be closed. */ 1200void gcry_md_debug (gcry_md_hd_t hd, const char *suffix); 1201 1202 1203/* Update the hash(s) of H with the character C. This is a buffered 1204 version of the gcry_md_write function. */ 1205#define gcry_md_putc(h,c) \ 1206 do { \ 1207 gcry_md_hd_t h__ = (h); \ 1208 if( (h__)->bufpos == (h__)->bufsize ) \ 1209 gcry_md_write( (h__), NULL, 0 ); \ 1210 (h__)->buf[(h__)->bufpos++] = (c) & 0xff; \ 1211 } while(0) 1212 1213/* Finalize the digest calculation. This is not really needed because 1214 gcry_md_read() does this implicitly. */ 1215#define gcry_md_final(a) \ 1216 gcry_md_ctl ((a), GCRYCTL_FINALIZE, NULL, 0) 1217 1218/* Return 0 if the algorithm A is available for use. */ 1219#define gcry_md_test_algo(a) \ 1220 gcry_md_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) 1221 1222/* Return an DER encoded ASN.1 OID for the algorithm A in buffer B. N 1223 must point to size_t variable with the available size of buffer B. 1224 After return it will receive the actual size of the returned 1225 OID. */ 1226#define gcry_md_get_asnoid(a,b,n) \ 1227 gcry_md_algo_info((a), GCRYCTL_GET_ASNOID, (b), (n)) 1228 1229/* Enable debugging for digest object A; i.e. create files named 1230 dbgmd-<n>.<string> while hashing. B is a string used as the suffix 1231 for the filename. This macro is deprecated, use gcry_md_debug. */ 1232#ifndef GCRYPT_NO_DEPRECATED 1233#define gcry_md_start_debug(a,b) \ 1234 gcry_md_ctl( (a), GCRYCTL_START_DUMP, (b), 0 ) 1235 1236/* Disable the debugging of A. This macro is deprecated, use 1237 gcry_md_debug. */ 1238#define gcry_md_stop_debug(a,b) \ 1239 gcry_md_ctl( (a), GCRYCTL_STOP_DUMP, (b), 0 ) 1240#endif 1241 1242/* Get a list consisting of the IDs of the loaded message digest 1243 modules. If LIST is zero, write the number of loaded message 1244 digest modules to LIST_LENGTH and return. If LIST is non-zero, the 1245 first *LIST_LENGTH algorithm IDs are stored in LIST, which must be 1246 of according size. In case there are less message digest modules 1247 than *LIST_LENGTH, *LIST_LENGTH is updated to the correct 1248 number. */ 1249gcry_error_t gcry_md_list (int *list, int *list_length); 1250 1251 1252/* Alternative interface for asymmetric cryptography. This interface 1253 is deprecated. */ 1254 1255/* The algorithm IDs. */ 1256typedef enum gcry_ac_id 1257 { 1258 GCRY_AC_RSA = 1, 1259 GCRY_AC_DSA = 17, 1260 GCRY_AC_ELG = 20, 1261 GCRY_AC_ELG_E = 16 1262 } 1263gcry_ac_id_t _GCRY_ATTR_INTERNAL; 1264 1265/* Key types. */ 1266typedef enum gcry_ac_key_type 1267 { 1268 GCRY_AC_KEY_SECRET, 1269 GCRY_AC_KEY_PUBLIC 1270 } 1271gcry_ac_key_type_t _GCRY_ATTR_INTERNAL; 1272 1273/* Encoding methods. */ 1274typedef enum gcry_ac_em 1275 { 1276 GCRY_AC_EME_PKCS_V1_5, 1277 GCRY_AC_EMSA_PKCS_V1_5 1278 } 1279gcry_ac_em_t _GCRY_ATTR_INTERNAL; 1280 1281/* Encryption and Signature schemes. */ 1282typedef enum gcry_ac_scheme 1283 { 1284 GCRY_AC_ES_PKCS_V1_5, 1285 GCRY_AC_SSA_PKCS_V1_5 1286 } 1287gcry_ac_scheme_t _GCRY_ATTR_INTERNAL; 1288 1289/* AC data. */ 1290#define GCRY_AC_FLAG_DEALLOC (1 << 0) 1291#define GCRY_AC_FLAG_COPY (1 << 1) 1292#define GCRY_AC_FLAG_NO_BLINDING (1 << 2) 1293 1294/* This type represents a `data set'. */ 1295typedef struct gcry_ac_data *gcry_ac_data_t _GCRY_ATTR_INTERNAL; 1296 1297/* This type represents a single `key', either a secret one or a 1298 public one. */ 1299typedef struct gcry_ac_key *gcry_ac_key_t _GCRY_ATTR_INTERNAL; 1300 1301/* This type represents a `key pair' containing a secret and a public 1302 key. */ 1303typedef struct gcry_ac_key_pair *gcry_ac_key_pair_t _GCRY_ATTR_INTERNAL; 1304 1305/* This type represents a `handle' that is needed by functions 1306 performing cryptographic operations. */ 1307typedef struct gcry_ac_handle *gcry_ac_handle_t _GCRY_ATTR_INTERNAL; 1308 1309typedef gpg_error_t (*gcry_ac_data_read_cb_t) (void *opaque, 1310 unsigned char *buffer, 1311 size_t *buffer_n) 1312 /* */ _GCRY_ATTR_INTERNAL; 1313 1314typedef gpg_error_t (*gcry_ac_data_write_cb_t) (void *opaque, 1315 unsigned char *buffer, 1316 size_t buffer_n) 1317 /* */ _GCRY_ATTR_INTERNAL; 1318 1319typedef enum 1320 { 1321 GCRY_AC_IO_READABLE, 1322 GCRY_AC_IO_WRITABLE 1323 } 1324gcry_ac_io_mode_t _GCRY_ATTR_INTERNAL; 1325 1326typedef enum 1327 { 1328 GCRY_AC_IO_STRING, 1329 GCRY_AC_IO_CALLBACK 1330 } 1331gcry_ac_io_type_t _GCRY_ATTR_INTERNAL; 1332 1333typedef struct gcry_ac_io 1334{ 1335 /* This is an INTERNAL structure, do NOT use manually. */ 1336 gcry_ac_io_mode_t mode _GCRY_ATTR_INTERNAL; 1337 gcry_ac_io_type_t type _GCRY_ATTR_INTERNAL; 1338 union 1339 { 1340 union 1341 { 1342 struct 1343 { 1344 gcry_ac_data_read_cb_t cb; 1345 void *opaque; 1346 } callback; 1347 struct 1348 { 1349 unsigned char *data; 1350 size_t data_n; 1351 } string; 1352 void *opaque; 1353 } readable; 1354 union 1355 { 1356 struct 1357 { 1358 gcry_ac_data_write_cb_t cb; 1359 void *opaque; 1360 } callback; 1361 struct 1362 { 1363 unsigned char **data; 1364 size_t *data_n; 1365 } string; 1366 void *opaque; 1367 } writable; 1368 } io _GCRY_ATTR_INTERNAL; 1369} 1370gcry_ac_io_t _GCRY_ATTR_INTERNAL; 1371 1372/* The caller of gcry_ac_key_pair_generate can provide one of these 1373 structures in order to influence the key generation process in an 1374 algorithm-specific way. */ 1375typedef struct gcry_ac_key_spec_rsa 1376{ 1377 gcry_mpi_t e; /* E to use. */ 1378} gcry_ac_key_spec_rsa_t _GCRY_ATTR_INTERNAL; 1379 1380/* Structure used for passing data to the implementation of the 1381 `EME-PKCS-V1_5' encoding method. */ 1382typedef struct gcry_ac_eme_pkcs_v1_5 1383{ 1384 size_t key_size; 1385} gcry_ac_eme_pkcs_v1_5_t _GCRY_ATTR_INTERNAL; 1386 1387typedef enum gcry_md_algos gcry_md_algo_t _GCRY_ATTR_INTERNAL; 1388 1389/* Structure used for passing data to the implementation of the 1390 `EMSA-PKCS-V1_5' encoding method. */ 1391typedef struct gcry_ac_emsa_pkcs_v1_5 1392{ 1393 gcry_md_algo_t md; 1394 size_t em_n; 1395} gcry_ac_emsa_pkcs_v1_5_t _GCRY_ATTR_INTERNAL; 1396 1397/* Structure used for passing data to the implementation of the 1398 `SSA-PKCS-V1_5' signature scheme. */ 1399typedef struct gcry_ac_ssa_pkcs_v1_5 1400{ 1401 gcry_md_algo_t md; 1402} gcry_ac_ssa_pkcs_v1_5_t _GCRY_ATTR_INTERNAL; 1403 1404 1405#ifndef GCRYPT_NO_DEPRECATED 1406/* Returns a new, empty data set in DATA. */ 1407gcry_error_t gcry_ac_data_new (gcry_ac_data_t *data) 1408 /* */ _GCRY_ATTR_INTERNAL; 1409 1410/* Destroy the data set DATA. */ 1411void gcry_ac_data_destroy (gcry_ac_data_t data) 1412 /* */ _GCRY_ATTR_INTERNAL; 1413 1414/* Create a copy of the data set DATA and store it in DATA_CP. */ 1415gcry_error_t gcry_ac_data_copy (gcry_ac_data_t *data_cp, 1416 gcry_ac_data_t data) 1417 /* */ _GCRY_ATTR_INTERNAL; 1418 1419/* Return the number of named MPI values inside of the data set 1420 DATA. */ 1421unsigned int gcry_ac_data_length (gcry_ac_data_t data) 1422 /* */ _GCRY_ATTR_INTERNAL; 1423 1424/* Destroy any values contained in the data set DATA. */ 1425void gcry_ac_data_clear (gcry_ac_data_t data) 1426 /* */ _GCRY_ATTR_INTERNAL; 1427 1428/* Add the value MPI to DATA with the label NAME. If FLAGS contains 1429 GCRY_AC_FLAG_DATA_COPY, the data set will contain copies of NAME 1430 and MPI. If FLAGS contains GCRY_AC_FLAG_DATA_DEALLOC or 1431 GCRY_AC_FLAG_DATA_COPY, the values contained in the data set will 1432 be deallocated when they are to be removed from the data set. */ 1433gcry_error_t gcry_ac_data_set (gcry_ac_data_t data, unsigned int flags, 1434 const char *name, gcry_mpi_t mpi) 1435 /* */ _GCRY_ATTR_INTERNAL; 1436 1437/* Store the value labelled with NAME found in DATA in MPI. If FLAGS 1438 contains GCRY_AC_FLAG_COPY, store a copy of the MPI value contained 1439 in the data set. MPI may be NULL. */ 1440gcry_error_t gcry_ac_data_get_name (gcry_ac_data_t data, unsigned int flags, 1441 const char *name, gcry_mpi_t *mpi) 1442 /* */ _GCRY_ATTR_INTERNAL; 1443 1444/* Stores in NAME and MPI the named MPI value contained in the data 1445 set DATA with the index IDX. If FLAGS contains GCRY_AC_FLAG_COPY, 1446 store copies of the values contained in the data set. NAME or MPI 1447 may be NULL. */ 1448gcry_error_t gcry_ac_data_get_index (gcry_ac_data_t data, unsigned int flags, 1449 unsigned int idx, 1450 const char **name, gcry_mpi_t *mpi) 1451 /* */ _GCRY_ATTR_INTERNAL; 1452 1453/* Convert the data set DATA into a new S-Expression, which is to be 1454 stored in SEXP, according to the identifiers contained in 1455 IDENTIFIERS. */ 1456gcry_error_t gcry_ac_data_to_sexp (gcry_ac_data_t data, gcry_sexp_t *sexp, 1457 const char **identifiers) 1458 /* */ _GCRY_ATTR_INTERNAL; 1459 1460/* Create a new data set, which is to be stored in DATA_SET, from the 1461 S-Expression SEXP, according to the identifiers contained in 1462 IDENTIFIERS. */ 1463gcry_error_t gcry_ac_data_from_sexp (gcry_ac_data_t *data, gcry_sexp_t sexp, 1464 const char **identifiers) 1465 /* */ _GCRY_ATTR_INTERNAL; 1466 1467/* Initialize AC_IO according to MODE, TYPE and the variable list of 1468 arguments. The list of variable arguments to specify depends on 1469 the given TYPE. */ 1470void gcry_ac_io_init (gcry_ac_io_t *ac_io, gcry_ac_io_mode_t mode, 1471 gcry_ac_io_type_t type, ...) 1472 /* */ _GCRY_ATTR_INTERNAL; 1473 1474/* Initialize AC_IO according to MODE, TYPE and the variable list of 1475 arguments AP. The list of variable arguments to specify depends on 1476 the given TYPE. */ 1477void gcry_ac_io_init_va (gcry_ac_io_t *ac_io, gcry_ac_io_mode_t mode, 1478 gcry_ac_io_type_t type, va_list ap) 1479 /* */ _GCRY_ATTR_INTERNAL; 1480 1481/* Create a new ac handle. */ 1482gcry_error_t gcry_ac_open (gcry_ac_handle_t *handle, 1483 gcry_ac_id_t algorithm, unsigned int flags) 1484 /* */ _GCRY_ATTR_INTERNAL; 1485 1486/* Destroy an ac handle. */ 1487void gcry_ac_close (gcry_ac_handle_t handle) 1488 /* */ _GCRY_ATTR_INTERNAL; 1489 1490/* Initialize a key from a given data set. */ 1491gcry_error_t gcry_ac_key_init (gcry_ac_key_t *key, gcry_ac_handle_t handle, 1492 gcry_ac_key_type_t type, gcry_ac_data_t data) 1493 /* */ _GCRY_ATTR_INTERNAL; 1494 1495/* Generates a new key pair via the handle HANDLE of NBITS bits and 1496 stores it in KEY_PAIR. In case non-standard settings are wanted, a 1497 pointer to a structure of type gcry_ac_key_spec_<algorithm>_t, 1498 matching the selected algorithm, can be given as KEY_SPEC. 1499 MISC_DATA is not used yet. */ 1500gcry_error_t gcry_ac_key_pair_generate (gcry_ac_handle_t handle, 1501 unsigned int nbits, void *spec, 1502 gcry_ac_key_pair_t *key_pair, 1503 gcry_mpi_t **misc_data) 1504 /* */ _GCRY_ATTR_INTERNAL; 1505 1506/* Returns the key of type WHICH out of the key pair KEY_PAIR. */ 1507gcry_ac_key_t gcry_ac_key_pair_extract (gcry_ac_key_pair_t key_pair, 1508 gcry_ac_key_type_t which) 1509 /* */ _GCRY_ATTR_INTERNAL; 1510 1511/* Returns the data set contained in the key KEY. */ 1512gcry_ac_data_t gcry_ac_key_data_get (gcry_ac_key_t key) 1513 /* */ _GCRY_ATTR_INTERNAL; 1514 1515/* Verifies that the key KEY is sane via HANDLE. */ 1516gcry_error_t gcry_ac_key_test (gcry_ac_handle_t handle, gcry_ac_key_t key) 1517 /* */ _GCRY_ATTR_INTERNAL; 1518 1519/* Stores the number of bits of the key KEY in NBITS via HANDLE. */ 1520gcry_error_t gcry_ac_key_get_nbits (gcry_ac_handle_t handle, 1521 gcry_ac_key_t key, unsigned int *nbits) 1522 /* */ _GCRY_ATTR_INTERNAL; 1523 1524/* Writes the 20 byte long key grip of the key KEY to KEY_GRIP via 1525 HANDLE. */ 1526gcry_error_t gcry_ac_key_get_grip (gcry_ac_handle_t handle, gcry_ac_key_t key, 1527 unsigned char *key_grip) 1528 /* */ _GCRY_ATTR_INTERNAL; 1529 1530/* Destroy a key. */ 1531void gcry_ac_key_destroy (gcry_ac_key_t key) 1532 /* */ _GCRY_ATTR_INTERNAL; 1533 1534/* Destroy a key pair. */ 1535void gcry_ac_key_pair_destroy (gcry_ac_key_pair_t key_pair) 1536 /* */ _GCRY_ATTR_INTERNAL; 1537 1538/* Encodes a message according to the encoding method METHOD. OPTIONS 1539 must be a pointer to a method-specific structure 1540 (gcry_ac_em*_t). */ 1541gcry_error_t gcry_ac_data_encode (gcry_ac_em_t method, 1542 unsigned int flags, void *options, 1543 gcry_ac_io_t *io_read, 1544 gcry_ac_io_t *io_write) 1545 /* */ _GCRY_ATTR_INTERNAL; 1546 1547/* Decodes a message according to the encoding method METHOD. OPTIONS 1548 must be a pointer to a method-specific structure 1549 (gcry_ac_em*_t). */ 1550gcry_error_t gcry_ac_data_decode (gcry_ac_em_t method, 1551 unsigned int flags, void *options, 1552 gcry_ac_io_t *io_read, 1553 gcry_ac_io_t *io_write) 1554 /* */ _GCRY_ATTR_INTERNAL; 1555 1556/* Encrypt the plain text MPI value DATA_PLAIN with the key KEY under 1557 the control of the flags FLAGS and store the resulting data set 1558 into DATA_ENCRYPTED. */ 1559gcry_error_t gcry_ac_data_encrypt (gcry_ac_handle_t handle, 1560 unsigned int flags, 1561 gcry_ac_key_t key, 1562 gcry_mpi_t data_plain, 1563 gcry_ac_data_t *data_encrypted) 1564 /* */ _GCRY_ATTR_INTERNAL; 1565 1566/* Decrypt the decrypted data contained in the data set DATA_ENCRYPTED 1567 with the key KEY under the control of the flags FLAGS and store the 1568 resulting plain text MPI value in DATA_PLAIN. */ 1569gcry_error_t gcry_ac_data_decrypt (gcry_ac_handle_t handle, 1570 unsigned int flags, 1571 gcry_ac_key_t key, 1572 gcry_mpi_t *data_plain, 1573 gcry_ac_data_t data_encrypted) 1574 /* */ _GCRY_ATTR_INTERNAL; 1575 1576/* Sign the data contained in DATA with the key KEY and store the 1577 resulting signature in the data set DATA_SIGNATURE. */ 1578gcry_error_t gcry_ac_data_sign (gcry_ac_handle_t handle, 1579 gcry_ac_key_t key, 1580 gcry_mpi_t data, 1581 gcry_ac_data_t *data_signature) 1582 /* */ _GCRY_ATTR_INTERNAL; 1583 1584/* Verify that the signature contained in the data set DATA_SIGNATURE 1585 is indeed the result of signing the data contained in DATA with the 1586 secret key belonging to the public key KEY. */ 1587gcry_error_t gcry_ac_data_verify (gcry_ac_handle_t handle, 1588 gcry_ac_key_t key, 1589 gcry_mpi_t data, 1590 gcry_ac_data_t data_signature) 1591 /* */ _GCRY_ATTR_INTERNAL; 1592 1593/* Encrypts the plain text readable from IO_MESSAGE through HANDLE 1594 with the public key KEY according to SCHEME, FLAGS and OPTS. If 1595 OPTS is not NULL, it has to be a pointer to a structure specific to 1596 the chosen scheme (gcry_ac_es_*_t). The encrypted message is 1597 written to IO_CIPHER. */ 1598gcry_error_t gcry_ac_data_encrypt_scheme (gcry_ac_handle_t handle, 1599 gcry_ac_scheme_t scheme, 1600 unsigned int flags, void *opts, 1601 gcry_ac_key_t key, 1602 gcry_ac_io_t *io_message, 1603 gcry_ac_io_t *io_cipher) 1604 /* */ _GCRY_ATTR_INTERNAL; 1605 1606/* Decrypts the cipher text readable from IO_CIPHER through HANDLE 1607 with the secret key KEY according to SCHEME, @var{flags} and OPTS. 1608 If OPTS is not NULL, it has to be a pointer to a structure specific 1609 to the chosen scheme (gcry_ac_es_*_t). The decrypted message is 1610 written to IO_MESSAGE. */ 1611gcry_error_t gcry_ac_data_decrypt_scheme (gcry_ac_handle_t handle, 1612 gcry_ac_scheme_t scheme, 1613 unsigned int flags, void *opts, 1614 gcry_ac_key_t key, 1615 gcry_ac_io_t *io_cipher, 1616 gcry_ac_io_t *io_message) 1617 /* */ _GCRY_ATTR_INTERNAL; 1618 1619/* Signs the message readable from IO_MESSAGE through HANDLE with the 1620 secret key KEY according to SCHEME, FLAGS and OPTS. If OPTS is not 1621 NULL, it has to be a pointer to a structure specific to the chosen 1622 scheme (gcry_ac_ssa_*_t). The signature is written to 1623 IO_SIGNATURE. */ 1624gcry_error_t gcry_ac_data_sign_scheme (gcry_ac_handle_t handle, 1625 gcry_ac_scheme_t scheme, 1626 unsigned int flags, void *opts, 1627 gcry_ac_key_t key, 1628 gcry_ac_io_t *io_message, 1629 gcry_ac_io_t *io_signature) 1630 /* */ _GCRY_ATTR_INTERNAL; 1631 1632/* Verifies through HANDLE that the signature readable from 1633 IO_SIGNATURE is indeed the result of signing the message readable 1634 from IO_MESSAGE with the secret key belonging to the public key KEY 1635 according to SCHEME and OPTS. If OPTS is not NULL, it has to be an 1636 anonymous structure (gcry_ac_ssa_*_t) specific to the chosen 1637 scheme. */ 1638gcry_error_t gcry_ac_data_verify_scheme (gcry_ac_handle_t handle, 1639 gcry_ac_scheme_t scheme, 1640 unsigned int flags, void *opts, 1641 gcry_ac_key_t key, 1642 gcry_ac_io_t *io_message, 1643 gcry_ac_io_t *io_signature) 1644 /* */ _GCRY_ATTR_INTERNAL; 1645 1646/* Store the textual representation of the algorithm whose id is given 1647 in ALGORITHM in NAME. This function is deprecated; use 1648 gcry_pk_algo_name. */ 1649gcry_error_t gcry_ac_id_to_name (gcry_ac_id_t algorithm, 1650 const char **name) 1651 /* */ _GCRY_GCC_ATTR_DEPRECATED; 1652/* Store the numeric ID of the algorithm whose textual representation 1653 is contained in NAME in ALGORITHM. This function is deprecated; 1654 use gcry_pk_map_name. */ 1655gcry_error_t gcry_ac_name_to_id (const char *name, 1656 gcry_ac_id_t *algorithm) 1657 /* */ _GCRY_GCC_ATTR_DEPRECATED; 1658#endif /*GCRYPT_NO_DEPRECATED*/ 1659 1660 1661/****************************** 1662 * * 1663 * Key Derivation Functions * 1664 * * 1665 ******************************/ 1666 1667/* Algorithm IDs for the KDFs. */ 1668enum gcry_kdf_algos 1669 { 1670 GCRY_KDF_NONE = 0, 1671 GCRY_KDF_SIMPLE_S2K = 16, 1672 GCRY_KDF_SALTED_S2K = 17, 1673 GCRY_KDF_ITERSALTED_S2K = 19, 1674 GCRY_KDF_PBKDF1 = 33, 1675 GCRY_KDF_PBKDF2 = 34 1676 }; 1677 1678/* Derive a key from a passphrase. */ 1679gpg_error_t gcry_kdf_derive (const void *passphrase, size_t passphraselen, 1680 int algo, int subalgo, 1681 const void *salt, size_t saltlen, 1682 unsigned long iterations, 1683 size_t keysize, void *keybuffer); 1684 1685 1686 1687 1688/************************************ 1689 * * 1690 * Random Generating Functions * 1691 * * 1692 ************************************/ 1693 1694/* The possible values for the random quality. The rule of thumb is 1695 to use STRONG for session keys and VERY_STRONG for key material. 1696 WEAK is usually an alias for STRONG and should not be used anymore 1697 (except with gcry_mpi_randomize); use gcry_create_nonce instead. */ 1698typedef enum gcry_random_level 1699 { 1700 GCRY_WEAK_RANDOM = 0, 1701 GCRY_STRONG_RANDOM = 1, 1702 GCRY_VERY_STRONG_RANDOM = 2 1703 } 1704gcry_random_level_t; 1705 1706/* Fill BUFFER with LENGTH bytes of random, using random numbers of 1707 quality LEVEL. */ 1708void gcry_randomize (void *buffer, size_t length, 1709 enum gcry_random_level level); 1710 1711/* Add the external random from BUFFER with LENGTH bytes into the 1712 pool. QUALITY should either be -1 for unknown or in the range of 0 1713 to 100 */ 1714gcry_error_t gcry_random_add_bytes (const void *buffer, size_t length, 1715 int quality); 1716 1717/* If random numbers are used in an application, this macro should be 1718 called from time to time so that new stuff gets added to the 1719 internal pool of the RNG. */ 1720#define gcry_fast_random_poll() gcry_control (GCRYCTL_FAST_POLL, NULL) 1721 1722 1723/* Return NBYTES of allocated random using a random numbers of quality 1724 LEVEL. */ 1725void *gcry_random_bytes (size_t nbytes, enum gcry_random_level level) 1726 _GCRY_GCC_ATTR_MALLOC; 1727 1728/* Return NBYTES of allocated random using a random numbers of quality 1729 LEVEL. The random numbers are created returned in "secure" 1730 memory. */ 1731void *gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level) 1732 _GCRY_GCC_ATTR_MALLOC; 1733 1734 1735/* Set the big integer W to a random value of NBITS using a random 1736 generator with quality LEVEL. Note that by using a level of 1737 GCRY_WEAK_RANDOM gcry_create_nonce is used internally. */ 1738void gcry_mpi_randomize (gcry_mpi_t w, 1739 unsigned int nbits, enum gcry_random_level level); 1740 1741 1742/* Create an unpredicable nonce of LENGTH bytes in BUFFER. */ 1743void gcry_create_nonce (void *buffer, size_t length); 1744 1745 1746 1747 1748 1749/*******************************/ 1750/* */ 1751/* Prime Number Functions */ 1752/* */ 1753/*******************************/ 1754 1755/* Mode values passed to a gcry_prime_check_func_t. */ 1756#define GCRY_PRIME_CHECK_AT_FINISH 0 1757#define GCRY_PRIME_CHECK_AT_GOT_PRIME 1 1758#define GCRY_PRIME_CHECK_AT_MAYBE_PRIME 2 1759 1760/* The function should return 1 if the operation shall continue, 0 to 1761 reject the prime candidate. */ 1762typedef int (*gcry_prime_check_func_t) (void *arg, int mode, 1763 gcry_mpi_t candidate); 1764 1765/* Flags for gcry_prime_generate(): */ 1766 1767/* Allocate prime numbers and factors in secure memory. */ 1768#define GCRY_PRIME_FLAG_SECRET (1 << 0) 1769 1770/* Make sure that at least one prime factor is of size 1771 `FACTOR_BITS'. */ 1772#define GCRY_PRIME_FLAG_SPECIAL_FACTOR (1 << 1) 1773 1774/* Generate a new prime number of PRIME_BITS bits and store it in 1775 PRIME. If FACTOR_BITS is non-zero, one of the prime factors of 1776 (prime - 1) / 2 must be FACTOR_BITS bits long. If FACTORS is 1777 non-zero, allocate a new, NULL-terminated array holding the prime 1778 factors and store it in FACTORS. FLAGS might be used to influence 1779 the prime number generation process. */ 1780gcry_error_t gcry_prime_generate (gcry_mpi_t *prime, 1781 unsigned int prime_bits, 1782 unsigned int factor_bits, 1783 gcry_mpi_t **factors, 1784 gcry_prime_check_func_t cb_func, 1785 void *cb_arg, 1786 gcry_random_level_t random_level, 1787 unsigned int flags); 1788 1789/* Find a generator for PRIME where the factorization of (prime-1) is 1790 in the NULL terminated array FACTORS. Return the generator as a 1791 newly allocated MPI in R_G. If START_G is not NULL, use this as 1792 teh start for the search. */ 1793gcry_error_t gcry_prime_group_generator (gcry_mpi_t *r_g, 1794 gcry_mpi_t prime, 1795 gcry_mpi_t *factors, 1796 gcry_mpi_t start_g); 1797 1798 1799/* Convenience function to release the FACTORS array. */ 1800void gcry_prime_release_factors (gcry_mpi_t *factors); 1801 1802 1803/* Check wether the number X is prime. */ 1804gcry_error_t gcry_prime_check (gcry_mpi_t x, unsigned int flags); 1805 1806 1807 1808/************************************ 1809 * * 1810 * Miscellaneous Stuff * 1811 * * 1812 ************************************/ 1813 1814/* Log levels used by the internal logging facility. */ 1815enum gcry_log_levels 1816 { 1817 GCRY_LOG_CONT = 0, /* (Continue the last log line.) */ 1818 GCRY_LOG_INFO = 10, 1819 GCRY_LOG_WARN = 20, 1820 GCRY_LOG_ERROR = 30, 1821 GCRY_LOG_FATAL = 40, 1822 GCRY_LOG_BUG = 50, 1823 GCRY_LOG_DEBUG = 100 1824 }; 1825 1826/* Type for progress handlers. */ 1827typedef void (*gcry_handler_progress_t) (void *, const char *, int, int, int); 1828 1829/* Type for memory allocation handlers. */ 1830typedef void *(*gcry_handler_alloc_t) (size_t n); 1831 1832/* Type for secure memory check handlers. */ 1833typedef int (*gcry_handler_secure_check_t) (const void *); 1834 1835/* Type for memory reallocation handlers. */ 1836typedef void *(*gcry_handler_realloc_t) (void *p, size_t n); 1837 1838/* Type for memory free handlers. */ 1839typedef void (*gcry_handler_free_t) (void *); 1840 1841/* Type for out-of-memory handlers. */ 1842typedef int (*gcry_handler_no_mem_t) (void *, size_t, unsigned int); 1843 1844/* Type for fatal error handlers. */ 1845typedef void (*gcry_handler_error_t) (void *, int, const char *); 1846 1847/* Type for logging handlers. */ 1848typedef void (*gcry_handler_log_t) (void *, int, const char *, va_list); 1849 1850/* Certain operations can provide progress information. This function 1851 is used to register a handler for retrieving these information. */ 1852void gcry_set_progress_handler (gcry_handler_progress_t cb, void *cb_data); 1853 1854 1855/* Register a custom memory allocation functions. */ 1856void gcry_set_allocation_handler ( 1857 gcry_handler_alloc_t func_alloc, 1858 gcry_handler_alloc_t func_alloc_secure, 1859 gcry_handler_secure_check_t func_secure_check, 1860 gcry_handler_realloc_t func_realloc, 1861 gcry_handler_free_t func_free); 1862 1863/* Register a function used instead of the internal out of memory 1864 handler. */ 1865void gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque); 1866 1867/* Register a function used instead of the internal fatal error 1868 handler. */ 1869void gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque); 1870 1871/* Register a function used instead of the internal logging 1872 facility. */ 1873void gcry_set_log_handler (gcry_handler_log_t f, void *opaque); 1874 1875/* Reserved for future use. */ 1876void gcry_set_gettext_handler (const char *(*f)(const char*)); 1877 1878/* Libgcrypt uses its own memory allocation. It is important to use 1879 gcry_free () to release memory allocated by libgcrypt. */ 1880void *gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC; 1881void *gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; 1882void *gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; 1883void *gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; 1884void *gcry_realloc (void *a, size_t n); 1885char *gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC; 1886void *gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC; 1887void *gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; 1888void *gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; 1889void *gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; 1890void *gcry_xrealloc (void *a, size_t n); 1891char *gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC; 1892void gcry_free (void *a); 1893 1894/* Return true if A is allocated in "secure" memory. */ 1895int gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE; 1896 1897/* Return true if Libgcrypt is in FIPS mode. */ 1898#define gcry_fips_mode_active() !!gcry_control (GCRYCTL_FIPS_MODE_P, 0) 1899 1900 1901/* Include support for Libgcrypt modules. */ 1902#include <gcrypt-module.h> 1903 1904#if 0 /* (Keep Emacsens' auto-indent happy.) */ 1905{ 1906#endif 1907#ifdef __cplusplus 1908} 1909#endif 1910#endif /* _GCRYPT_H */ 1911/* 1912@emacs_local_vars_begin@ 1913@emacs_local_vars_read_only@ 1914@emacs_local_vars_end@ 1915*/ 1916