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