1 /* gcrypt.h - GNU cryptographic library interface
2 * Copyright (C) 1998,1999,2000,2001,2002 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
29 #if 0 /* keep Emacsens's auto-indent happy */
34 /* The version of this header should match the one of the library It
35 should not be used by a program because gcry_check_version() should
36 reurn the same version. The purpose of this macro is to let
37 autoconf (using the AM_PATH_GCRYPT macro) check that this header
38 matches the installed library. Note: Do not edit the next line as
39 configure may fix the string here. */
40 #define GCRYPT_VERSION "1.1.12"
42 /* Internal: We can't use the convenience macros for the multi
43 precision integer functions when building this library. */
44 #ifdef _GCRYPT_IN_LIBGCRYPT
45 # ifndef GCRYPT_NO_MPI_MACROS
46 # define GCRYPT_NO_MPI_MACROS 1
50 /* We want to use gcc attributes when possible. Warning: Don't use
51 these macros in your progranms: As indicated by the leading
52 underscore they are subject to change without notice. */
53 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96 )
54 # define _GCRY_GCC_ATTR_PURE __attribute__ ((__pure__))
56 # define _GCRY_GCC_ATTR_PURE
58 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2 )
59 # define _GCRY_GCC_ATTR_MALLOC __attribute__ ((__malloc__))
61 # define _GCRY_GCC_ATTR_MALLOC
65 /* The data object used to hold a multi precision integer. GcryMPI is
68 typedef struct gcry_mpi *GCRY_MPI;
69 typedef struct gcry_mpi *GcryMPI;
72 /* Error handling etc. */
74 /* The error numbers used by Libgcrypt. */
75 /* FIXME: We should use the same values as they were used in GnuPG
76 1.0. gpg --status-fd may print some of these values. */
79 GCRYERR_SUCCESS = 0, /* "no error" (this is guaranteed to be 0) */
80 GCRYERR_GENERAL = 1, /* catch all the other errors code */
82 GCRYERR_INV_PK_ALGO = 4, /* invalid public key algorithm */
83 GCRYERR_INV_MD_ALGO = 5, /* invalid message digest algorithm */
84 GCRYERR_BAD_PUBLIC_KEY = 6, /* Bad public key */
85 GCRYERR_BAD_SECRET_KEY = 7, /* Bad secret key */
86 GCRYERR_BAD_SIGNATURE = 8, /* Bad signature */
88 GCRYERR_INV_CIPHER_ALGO = 12, /* invalid cipher algorithm */
89 GCRYERR_BAD_MPI = 30, /* problem with an MPI's value*/
90 GCRYERR_WRONG_PK_ALGO = 41, /* wrong public key algorithm */
91 GCRYERR_WEAK_KEY = 43, /* weak encryption key */
92 GCRYERR_INV_KEYLEN = 44, /* invalid length of a key*/
93 GCRYERR_INV_ARG = 45, /* invalid argument */
94 GCRYERR_SELFTEST = 50, /* selftest failed */
96 /* error codes not used in GnuPG 1.0 */
97 GCRYERR_INV_OP = 61, /* invalid operation code or ctl command */
98 GCRYERR_NO_MEM = 62, /* out of core */
99 GCRYERR_INTERNAL = 63, /* internal error */
100 GCRYERR_EOF = 64, /* (-1) is remapped to this value */
101 GCRYERR_INV_OBJ = 65, /* an object is not valid */
102 GCRYERR_TOO_SHORT = 66, /* provided buffer/object too short */
103 GCRYERR_TOO_LARGE = 67, /* object is too large */
104 GCRYERR_NO_OBJ = 68, /* Missing item in an object */
105 GCRYERR_NOT_IMPL = 69, /* Not implemented */
106 GCRYERR_CONFLICT = 70, /* conflicting use of functions/values */
107 GCRYERR_INV_CIPHER_MODE = 71, /* invalid/unsupported cipher mode */
108 GCRYERR_INV_FLAG = 72, /* invalid flag */
110 /* error codes pertaining to S-expressions */
111 GCRYERR_SEXP_INV_LEN_SPEC = 201,
112 GCRYERR_SEXP_STRING_TOO_LONG = 202,
113 GCRYERR_SEXP_UNMATCHED_PAREN = 203,
114 GCRYERR_SEXP_NOT_CANONICAL = 204,
115 GCRYERR_SEXP_BAD_CHARACTER = 205,
116 GCRYERR_SEXP_BAD_QUOTATION = 206,/* or invalid hex or octal value */
117 GCRYERR_SEXP_ZERO_PREFIX = 207,/* first character of a length is 0 */
118 GCRYERR_SEXP_NESTED_DH = 208,/* nested display hints */
119 GCRYERR_SEXP_UNMATCHED_DH = 209,/* unmatched display hint */
120 GCRYERR_SEXP_UNEXPECTED_PUNC = 210,/* unexpected reserved punctuation */
121 GCRYERR_SEXP_BAD_HEX_CHAR = 211,
122 GCRYERR_SEXP_ODD_HEX_NUMBERS = 212,
123 GCRYERR_SEXP_BAD_OCT_CHAR = 213
126 /* Check that the library fulfills the version requirement. */
127 const char *gcry_check_version (const char *req_version);
129 /* Return the error number for the last failed function call. */
130 int gcry_errno(void) _GCRY_GCC_ATTR_PURE;
132 /* Map an error number to a string. */
133 const char *gcry_strerror (int ec);
135 /* Codes used with the gcry_control function. */
140 GCRYCTL_CFB_SYNC = 3,
141 GCRYCTL_RESET = 4, /* e.g. for MDs */
142 GCRYCTL_FINALIZE = 5,
143 GCRYCTL_GET_KEYLEN = 6,
144 GCRYCTL_GET_BLKLEN = 7,
145 GCRYCTL_TEST_ALGO = 8,
146 GCRYCTL_IS_SECURE = 9,
147 GCRYCTL_GET_ASNOID = 10,
148 GCRYCTL_ENABLE_ALGO = 11,
149 GCRYCTL_DISABLE_ALGO = 12,
150 GCRYCTL_DUMP_RANDOM_STATS = 13,
151 GCRYCTL_DUMP_SECMEM_STATS = 14,
152 GCRYCTL_GET_ALGO_NPKEY = 15,
153 GCRYCTL_GET_ALGO_NSKEY = 16,
154 GCRYCTL_GET_ALGO_NSIGN = 17,
155 GCRYCTL_GET_ALGO_NENCR = 18,
156 GCRYCTL_SET_VERBOSITY = 19,
157 GCRYCTL_SET_DEBUG_FLAGS = 20,
158 GCRYCTL_CLEAR_DEBUG_FLAGS = 21,
159 GCRYCTL_USE_SECURE_RNDPOOL= 22,
160 GCRYCTL_DUMP_MEMORY_STATS = 23,
161 GCRYCTL_INIT_SECMEM = 24,
162 GCRYCTL_TERM_SECMEM = 25,
163 GCRYCTL_DISABLE_SECMEM_WARN = 27,
164 GCRYCTL_SUSPEND_SECMEM_WARN = 28,
165 GCRYCTL_RESUME_SECMEM_WARN = 29,
166 GCRYCTL_DROP_PRIVS = 30,
167 GCRYCTL_ENABLE_M_GUARD = 31,
168 GCRYCTL_START_DUMP = 32,
169 GCRYCTL_STOP_DUMP = 33,
170 GCRYCTL_GET_ALGO_USAGE = 34,
171 GCRYCTL_IS_ALGO_ENABLED = 35,
172 GCRYCTL_DISABLE_INTERNAL_LOCKING = 36,
173 GCRYCTL_DISABLE_SECMEM = 37,
174 GCRYCTL_INITIALIZATION_FINISHED = 38,
175 GCRYCTL_INITIALIZATION_FINISHED_P = 39,
176 GCRYCTL_ANY_INITIALIZATION_P = 40,
177 GCRYCTL_SET_CBC_CTS = 41
180 /* Perform various operations defined by CMD. */
181 int gcry_control (enum gcry_ctl_cmds CMD, ...);
185 /* S-expression management. */
187 /* The object to represent an S-expression as used with the
188 public key functions. GcrySexp is the preferrred form. */
190 typedef struct gcry_sexp *GCRY_SEXP;
191 typedef struct gcry_sexp *GcrySexp;
193 /* The possible values for the S-expression format. */
194 enum gcry_sexp_format {
195 GCRYSEXP_FMT_DEFAULT = 0,
196 GCRYSEXP_FMT_CANON = 1,
197 GCRYSEXP_FMT_BASE64 = 2,
198 GCRYSEXP_FMT_ADVANCED = 3
201 /* Create an new S-expression object from BUFFER of size LENGTH and
202 return it in RETSEXP. With AUTODETECT set to 0 the data in BUFFER
203 is expected to be in canonized format */
204 int gcry_sexp_new (GcrySexp *retsexp, const void *buffer, size_t length,
207 /* Same as gcry_sexp_new but allows to pass a FREEFNC which has the
208 effect to transfer ownership of BUFFER to the created object. */
209 int gcry_sexp_create (GcrySexp *retsexp, void *buffer, size_t length,
210 int autodetect, void (*freefnc)(void*) );
212 /* Scan BUFFER and return a new S-expression object in RETSEXP. This
213 function expects a printf like string in BUFFER. */
214 int gcry_sexp_sscan (GcrySexp *retsexp, size_t *erroff,
215 const char *buffer, size_t length );
217 /* Same as gcry_sexp_sscan but expects a string in FORMAT and can thus
218 only be used for certain encodings. */
219 int gcry_sexp_build (GcrySexp *retsexp, size_t *erroff,
220 const char *format, ... );
222 /* Release the S-expression object SEXP */
223 void gcry_sexp_release (GcrySexp sexp);
225 /* Calculate the length of an canonized S-expresion in BUFFER and
226 check for a valid encoding. */
227 size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length,
228 size_t *erroff, int *errcode);
230 /* Copies the S-expression object SEXP into BUFFER using the format
231 specified in MODE. */
232 size_t gcry_sexp_sprint (GCRY_SEXP sexp, int mode, char *buffer,
235 void gcry_sexp_dump( const GCRY_SEXP a );
236 GCRY_SEXP gcry_sexp_cons( const GCRY_SEXP a, const GCRY_SEXP b );
237 GCRY_SEXP gcry_sexp_alist( const GCRY_SEXP *array );
238 GCRY_SEXP gcry_sexp_vlist( const GCRY_SEXP a, ... );
239 GCRY_SEXP gcry_sexp_append( const GCRY_SEXP a, const GCRY_SEXP n );
240 GCRY_SEXP gcry_sexp_prepend( const GCRY_SEXP a, const GCRY_SEXP n );
241 GCRY_SEXP gcry_sexp_find_token( GCRY_SEXP list,
242 const char *tok, size_t toklen );
243 int gcry_sexp_length( const GCRY_SEXP list );
244 GCRY_SEXP gcry_sexp_nth( const GCRY_SEXP list, int number );
245 GCRY_SEXP gcry_sexp_car( const GCRY_SEXP list );
246 GCRY_SEXP gcry_sexp_cdr( const GCRY_SEXP list );
247 GCRY_SEXP gcry_sexp_cadr( const GCRY_SEXP list );
248 const char *gcry_sexp_nth_data( const GCRY_SEXP list, int number,
250 GCRY_MPI gcry_sexp_nth_mpi( GCRY_SEXP list, int number, int mpifmt );
254 /*******************************************
256 * multi precision integer functions *
258 *******************************************/
260 /* Different formats of external big integer representation. */
264 GCRYMPI_FMT_STD = 1, /* twos complement stored without length */
265 GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP (only defined as unsigned)*/
266 GCRYMPI_FMT_SSH = 3, /* As used by SSH (same as 1 but with length)*/
267 GCRYMPI_FMT_HEX = 4, /* hex format */
268 GCRYMPI_FMT_USG = 5 /* like STD but this is an unsigned one */
271 /* Flags used for creating big integers. */
274 GCRYMPI_FLAG_SECURE = 1, /* Allocate the number in "secure" memory. */
275 GCRYMPI_FLAG_OPAQUE = 2 /* The number is not a real one but just a
276 way to store some bytes. This is
277 useful for encrypted big integers. */
281 /* Allocate a new big integer object, initialize it with 0 and
282 initially allocate memory for a number of at least NBITS. */
283 GcryMPI gcry_mpi_new (unsigned int nbits);
285 /* Same as gcry_mpi_new() but allocate in "secure" memory. */
286 GcryMPI gcry_mpi_snew (unsigned int nbits);
288 /* Release the number A and free all associated resources. */
289 void gcry_mpi_release (GcryMPI a);
291 /* Create a new number with the same value as A. */
292 GcryMPI gcry_mpi_copy (const GcryMPI a);
294 /* Store the big integer value U in W. */
295 GcryMPI gcry_mpi_set (GcryMPI w, const GcryMPI u);
297 /* Store the unsigned integer value U in W. */
298 GcryMPI gcry_mpi_set_ui (GcryMPI w, unsigned long u);
300 /* Swap the values of A and B. */
301 void gcry_mpi_swap (GcryMPI a, GcryMPI b);
303 /* Compare the big integer number U and V returning 0 for equality, a
304 positive value for U > V and a negative for U < V. */
305 int gcry_mpi_cmp (const GcryMPI u, const GcryMPI v);
307 /* Compare the big integer number U with the unsigned integer V
308 returning 0 for equality, a positive value for U > V and a negative
310 int gcry_mpi_cmp_ui (const GcryMPI u, unsigned long v);
312 /* Convert the external representation of an integer stored in BUFFER
313 with a size of (*NBYTES) in a newly create MPI returned in RET_MPI.
314 For certain formats a length is not required and may be passed as
315 NULL. After a successful operation NBYTES received the number of
316 bytes actually scanned. */
317 int gcry_mpi_scan (GcryMPI *ret_mpi, enum gcry_mpi_format format,
318 const char *buffer, size_t *nbytes);
320 /* Convert the big integer A into the external representation
321 described by FORMAT and store it in the provided BUFFER which has
322 the size (*NBYTES). NBYTES receives the actual length of the
323 external representation. */
324 int gcry_mpi_print (enum gcry_mpi_format format,
325 char *buffer, size_t *nbytes, const GcryMPI a);
327 /* Convert the big integer A int the external representation desribed
328 by FORMAT and store it in a newly allocated buffer which address
329 will be put into BUFFER. NBYTES receives the actual lengths of the
330 external representation. */
331 int gcry_mpi_aprint (enum gcry_mpi_format format,
332 void **buffer, size_t *nbytes, const GcryMPI a);
335 void gcry_mpi_add (GcryMPI w, GcryMPI u, GcryMPI v);
337 /* W = U + V. V is an unsigned integer. */
338 void gcry_mpi_add_ui (GcryMPI w, GcryMPI u, unsigned long v);
340 /* W = U + V mod M. */
341 void gcry_mpi_addm (GcryMPI w, GcryMPI u, GcryMPI v, GcryMPI m);
344 void gcry_mpi_sub (GcryMPI w, GcryMPI u, GcryMPI v);
346 /* W = U - V. V is an unsigned integer. */
347 void gcry_mpi_sub_ui (GcryMPI w, GcryMPI u, unsigned long v );
349 /* W = U - V mod M */
350 void gcry_mpi_subm (GcryMPI w, GcryMPI u, GcryMPI v, GcryMPI m);
353 void gcry_mpi_mul (GcryMPI w, GcryMPI u, GcryMPI v);
355 /* W = U * V. V is an unsigned integer. */
356 void gcry_mpi_mul_ui (GcryMPI w, GcryMPI u, unsigned long v );
358 /* W = U * V mod M. */
359 void gcry_mpi_mulm (GcryMPI w, GcryMPI u, GcryMPI v, GcryMPI m);
361 /* W = U * (2 ^ CNT). */
362 void gcry_mpi_mul_2exp (GcryMPI w, GcryMPI u, unsigned long cnt);
364 /* Q = DIVIDEND / DIVISOR, R = DIVIDEND % DIVISOR,
365 Q or R may be passed as NULL. ROUND should be negative or 0. */
366 void gcry_mpi_div (GcryMPI q, GcryMPI r,
367 GcryMPI dividend, GcryMPI divisor, int round);
369 /* R = DIVIDEND % DIVISOR */
370 void gcry_mpi_mod (GcryMPI r, GcryMPI dividend, GcryMPI divisor);
372 /* W = B ^ E mod M. */
373 void gcry_mpi_powm (GcryMPI w,
374 const GcryMPI b, const GcryMPI e, const GcryMPI m);
376 /* Set G to the greatest common divisor of A and B.
377 Return true if the G is 1. */
378 int gcry_mpi_gcd (GcryMPI g, GcryMPI a, GcryMPI b);
380 /* Set X to the multiplicative inverse of A mod M.
381 Return true if the value exists. */
382 int gcry_mpi_invm (GcryMPI x, GcryMPI a, GcryMPI m);
385 /* Return the number of bits required to represent A. */
386 unsigned int gcry_mpi_get_nbits (GcryMPI a);
388 /* Return true when bit number N (counting from 0) is set in A. */
389 int gcry_mpi_test_bit (GcryMPI a, unsigned int n);
391 /* Set bit number N in A. */
392 void gcry_mpi_set_bit (GcryMPI a, unsigned int n);
394 /* Clear bit number N in A. */
395 void gcry_mpi_clear_bit (GcryMPI a, unsigned int n);
397 /* Set bit number N in A and clear all bits greater than N. */
398 void gcry_mpi_set_highbit (GcryMPI a, unsigned int n);
400 /* Clear bit number N in A and all bits greater than N. */
401 void gcry_mpi_clear_highbit (GcryMPI a, unsigned int n);
403 /* Shift the value of A by N bits to the right and store the result in X. */
404 void gcry_mpi_rshift (GcryMPI x, GcryMPI a, unsigned int n);
406 /* Store NBITS of the value P points to in A and mark A as an opaque
408 GcryMPI gcry_mpi_set_opaque (GcryMPI a, void *p, unsigned int nbits);
410 /* creturn a pointer to an opaque value stored in A and return its
411 size in NBITS. Note that the returned pointer is still owned by A
412 and that the function should never be used for an non-opaque
414 void *gcry_mpi_get_opaque (GcryMPI a, unsigned int *nbits);
416 /* Set the FLAG for the big integer A. Currently only the flag
417 GCRYMPI_FLAG_SECURE is allowed to convert A into an big intger
418 stored in "secure" memory. */
419 void gcry_mpi_set_flag (GcryMPI a, enum gcry_mpi_flag flag);
421 /* Clear FLAG for the big integer A. Note that this function is
422 currently useless as no flags are allowed. */
423 void gcry_mpi_clear_flag (GcryMPI a, enum gcry_mpi_flag flag);
425 /* Return true when the FLAG is set for A. */
426 int gcry_mpi_get_flag (GcryMPI a, enum gcry_mpi_flag flag);
428 /* Unless the GCRYPT_NO_MPI_MACROS is used, provide a couple of
429 convenience macors for the big integer functions. */
430 #ifndef GCRYPT_NO_MPI_MACROS
431 #define mpi_new(n) gcry_mpi_new( (n) )
432 #define mpi_secure_new( n ) gcry_mpi_snew( (n) )
433 #define mpi_release( a ) do { gcry_mpi_release( (a) ); \
434 (a) = NULL; } while(0)
435 #define mpi_copy( a ) gcry_mpi_copy( (a) )
436 #define mpi_set( w, u) gcry_mpi_set( (w), (u) )
437 #define mpi_set_ui( w, u) gcry_mpi_set_ui( (w), (u) )
438 #define mpi_cmp( u, v ) gcry_mpi_cmp( (u), (v) )
439 #define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) )
441 #define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v))
442 #define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v))
443 #define mpi_addm(w,u,v,m) gcry_mpi_addm ((w),(u),(v),(m))
444 #define mpi_sub_ui(w,u,v) gcry_mpi_sub_ui ((w),(u),(v))
445 #define mpi_sub(w,u,v) gcry_mpi_sub ((w),(u),(v))
446 #define mpi_subm(w,u,v,m) gcry_mpi_subm ((w),(u),(v),(m))
447 #define mpi_mul_ui(w,u,v) gcry_mpi_mul_ui ((w),(u),(v))
448 #define mpi_mul_2exp(w,u,v) gcry_mpi_mul_2exp ((w),(u),(v))
449 #define mpi_mul(w,u,v) gcry_mpi_mul ((w),(u),(v))
450 #define mpi_mulm(w,u,v,m) gcry_mpi_mulm ((w),(u),(v),(m))
451 #define mpi_powm(w,b,e,m) gcry_mpi_powm ( (w), (b), (e), (m) )
452 #define mpi_tdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), 0)
453 #define mpi_fdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), -1)
454 #define mpi_mod(r,a,m) gcry_mpi_mod ((r), (a), (m))
455 #define mpi_gcd(g,a,b) gcry_mpi_gcd ( (g), (a), (b) )
456 #define mpi_invm(g,a,b) gcry_mpi_invm ( (g), (a), (b) )
458 #define mpi_get_nbits(a) gcry_mpi_get_nbits ((a))
459 #define mpi_test_bit(a,b) gcry_mpi_test_bit ((a),(b))
460 #define mpi_set_bit(a,b) gcry_mpi_set_bit ((a),(b))
461 #define mpi_set_highbit(a,b) gcry_mpi_set_highbit ((a),(b))
462 #define mpi_clear_bit(a,b) gcry_mpi_clear_bit ((a),(b))
463 #define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b))
464 #define mpi_rshift(a,b,c) gcry_mpi_rshift ((a),(b),(c))
466 #define mpi_set_opaque(a,b,c) gcry_mpi_set_opaque( (a), (b), (c) )
467 #define mpi_get_opaque(a,b) gcry_mpi_get_opaque( (a), (b) )
468 #endif /* GCRYPT_NO_MPI_MACROS */
472 /************************************
474 * symmetric cipher functions *
476 ************************************/
478 /* The data object used to hold a handle to an encryption opject.
479 GcryCipherHd is the preferred one. */
480 struct gcry_cipher_handle;
481 typedef struct gcry_cipher_handle *GCRY_CIPHER_HD;
482 typedef struct gcry_cipher_handle *GcryCipherHd;
484 /* All symmetric encryption algorithms are identified by their IDs.
485 More IDs may be registered at runtime. */
486 enum gcry_cipher_algos
488 GCRY_CIPHER_NONE = 0,
489 GCRY_CIPHER_IDEA = 1,
490 GCRY_CIPHER_3DES = 2,
491 GCRY_CIPHER_CAST5 = 3,
492 GCRY_CIPHER_BLOWFISH = 4,
493 GCRY_CIPHER_SAFER_SK128 = 5,
494 GCRY_CIPHER_DES_SK = 6,
496 GCRY_CIPHER_AES192 = 8,
497 GCRY_CIPHER_AES256 = 9,
498 GCRY_CIPHER_TWOFISH = 10,
499 /* other cipher numbers are above 300 for OpenPGP reasons. */
500 GCRY_CIPHER_ARCFOUR = 301, /* fully compatible with RSA's RC4 (tm). */
501 GCRY_CIPHER_DES = 302 /* Yes, this is single key 56 bit DES. */
504 /* The Rijndael algorithm is basically AES, so provide some macros. */
505 #define GCRY_CIPHER_AES128 GCRY_CIPHER_AES
506 #define GCRY_CIPHER_RIJNDAEL GCRY_CIPHER_AES
507 #define GCRY_CIPHER_RIJNDAEL128 GCRY_CIPHER_AES128
508 #define GCRY_CIPHER_RIJNDAEL192 GCRY_CIPHER_AES192
509 #define GCRY_CIPHER_RIJNDAEL256 GCRY_CIPHER_AES256
511 /* The supported encryption modes. NOte that not all of them are
512 supported for each algorithm. */
513 enum gcry_cipher_modes
515 GCRY_CIPHER_MODE_NONE = 0, /* Not yet specified. */
516 GCRY_CIPHER_MODE_ECB = 1, /* Electronic codebook. */
517 GCRY_CIPHER_MODE_CFB = 2, /* Cipher feedback. */
518 GCRY_CIPHER_MODE_CBC = 3, /* Cipher block chaining. */
519 GCRY_CIPHER_MODE_STREAM = 4, /* Used with stream ciphers. */
520 GCRY_CIPHER_MODE_OFB = 5 /* Outer feedback. */
523 /* Flags used with the open function. */
524 enum gcry_cipher_flags
526 GCRY_CIPHER_SECURE = 1, /* Allocate in secure memory. */
527 GCRY_CIPHER_ENABLE_SYNC = 2, /* Enable CFB sync mode. */
528 GCRY_CIPHER_CBC_CTS = 4 /* Enable CBC cipher text stealing (CTS). */
532 /* Create a handle for algorithm ALGO to be used in MODE. FLAGS may
533 be given as an bitwise OR of the gcry_cipher_flags values. */
534 GcryCipherHd gcry_cipher_open (int algo, int mode, unsigned int flags);
536 /* Close the cioher handle H and release all resource. */
537 void gcry_cipher_close (GcryCipherHd h);
539 /* Perform various operations on the cipher object H. */
540 int gcry_cipher_ctl( GcryCipherHd h, int cmd, void *buffer, size_t buflen);
542 /* Retrieve various information about the cipher object H. */
543 int gcry_cipher_info( GcryCipherHd h, int what, void *buffer, size_t *nbytes);
545 /* Retrieve various information about the cipher algorithm ALGO. */
546 int gcry_cipher_algo_info (int algo, int what, void *buffer, size_t *nbytes);
548 /* Map the cipher algorithm id ALGO to a string representation of that
549 algorithm name. For unknown algorithms this functions returns an
551 const char *gcry_cipher_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
553 /* Map the algorithm name NAME to an cipher algorithm ID. Return 0 if
554 the algorithm name is not known. */
555 int gcry_cipher_map_name (const char *name) _GCRY_GCC_ATTR_PURE;
557 /* Given an ASN.1 object identifier in standard IETF dotted decimal
558 format in STING, return the encryption mode associated with that
559 OID or 0 if not known or applicable. */
560 int gcry_cipher_mode_from_oid (const char *string) _GCRY_GCC_ATTR_PURE;
562 /* Encrypt the plaintext of size INLEN in IN using the cipher handle H
563 into the buffer OUT which has an allocated length of OUTSIZE. For
564 most algorithms it is possible to pass NULL for in and 0 for INLEN
565 and do a in-place decryption of the data provided in OUT. */
566 int gcry_cipher_encrypt (GcryCipherHd h,
567 unsigned char *out, size_t outsize,
568 const unsigned char *in, size_t inlen);
570 /* The counterpart to gcry_cipher_encrypt. */
571 int gcry_cipher_decrypt (GcryCipherHd h,
572 unsigned char *out, size_t outsize,
573 const unsigned char *in, size_t inlen);
575 /* Set key K of length L for the cipher handle H.
576 (We have to cast away a const char* here - this catch-all ctl
577 function was probably not the best choice) */
578 #define gcry_cipher_setkey(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_KEY, \
581 /* Set initialization vector K of length L for the cipher handle H. */
582 #define gcry_cipher_setiv(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_IV, \
585 /* Perform the the OppenPGP sync operation if this is enabled for the
587 #define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, \
590 /* Enable or disable CTS in future calls to gcry_encrypt(). CBC mode only. */
591 #define gcry_cipher_cts(h,on) gcry_cipher_ctl( (h), GCRYCTL_SET_CBC_CTS, \
594 /* Retrieved the key length used with algorithm A. */
595 #define gcry_cipher_get_algo_keylen(a) \
596 gcry_cipher_algo_info( (a), GCRYCTL_GET_KEYLEN, NULL, NULL )
598 /* Retrieve the block length used with algorithm A. */
599 #define gcry_cipher_get_algo_blklen(a) \
600 gcry_cipher_algo_info( (a), GCRYCTL_GET_BLKLEN, NULL, NULL )
602 /* Return 0 if the algorithm A is available for use. */
603 #define gcry_cipher_test_algo(a) \
604 gcry_cipher_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
608 /************************************
610 * asymmetric cipher functions *
612 ************************************/
614 /* The algorithms and their IDs we support. */
618 GCRY_PK_RSA_E = 2, /* deprecated */
619 GCRY_PK_RSA_S = 3, /* deprecated */
620 GCRY_PK_ELG_E = 16, /* use only for OpenPGP */
625 /* Flags describing usage capabilities of a PK algorithm. */
626 #define GCRY_PK_USAGE_SIGN 1
627 #define GCRY_PK_USAGE_ENCR 2
629 /* Encrypt the DATA using the public key PKEY and store the result as
630 a newly created S-expression at RESULT. */
631 int gcry_pk_encrypt (GcrySexp *result, GcrySexp data, GcrySexp pkey);
633 /* Decrypt the DATA using the private key SKEY and store the result as
634 a newly created S-expression at RESULT. */
635 int gcry_pk_decrypt (GcrySexp *result, GcrySexp data, GcrySexp skey);
637 /* Sign the DATA using the private key SKEY and store the result as
638 a newly created S-expression at RESULT. */
639 int gcry_pk_sign (GcrySexp *result, GcrySexp data, GcrySexp skey);
641 /* Check the signature SIGVAL on DATA using the public key PKEY. */
642 int gcry_pk_verify (GcrySexp sigval, GcrySexp data, GcrySexp pkey);
644 /* Check that KEY (either private or public) is sane. */
645 int gcry_pk_testkey (GcrySexp key);
647 /* Generate a new key pair according to the parameters given in
648 S_PARMS. The new key pair is returned in as an S-expression in
650 int gcry_pk_genkey (GcrySexp *r_key, GcrySexp s_parms);
652 /* Catch all function for miscellaneous operations. */
653 int gcry_pk_ctl (int cmd, void *buffer, size_t buflen);
655 /* Retrieve information about the public key algorithm ALGO. */
656 int gcry_pk_algo_info (int algo, int what, void *buffer, size_t *nbytes);
658 /* Map the public key algorithm id ALGO to a string representation of the
659 algorithm name. For unknown algorithms this functions returns an
661 const char *gcry_pk_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
663 /* Map the algorithm NAME to a public key algorithm Id. Return 0 if
664 the algorithm name is not known. */
665 int gcry_pk_map_name (const char* name) _GCRY_GCC_ATTR_PURE;
667 /* Return what is commonly referred as the key length for the given
668 public or private KEY. */
669 unsigned int gcry_pk_get_nbits (GcrySexp key) _GCRY_GCC_ATTR_PURE;
671 /* Please note that keygrip is still experimental and should not be
672 used without contacting the author. */
673 unsigned char *gcry_pk_get_keygrip (GcrySexp key, unsigned char *array);
675 /* Return 0 if the public key algorithm A is available for use. */
676 #define gcry_pk_test_algo(a) \
677 gcry_pk_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
681 /************************************
683 * cryptograhic hash functions *
685 ************************************/
687 /* Algorithm IDs for the hash functions we know about. Not all of them
696 GCRY_MD_TIGER = 6, /* TIGER/192. */
697 GCRY_MD_HAVAL = 7, /* HAVAL, 5 pass, 160 bit. */
704 /* Flags used with the open function. */
707 GCRY_MD_FLAG_SECURE = 1, /* Allocate all buffers in "secure" memory */
708 GCRY_MD_FLAG_HMAC = 2 /* Make an HMAC out of this algorithm. */
712 /* This object is used to hold a handle to an message digest object.
713 GcryCipherHd is the preferred type. */
714 struct gcry_md_context;
715 struct gcry_md_handle
716 { /* This structure is private - only to be used by the gcry_md_ macros. */
717 struct gcry_md_context *ctx;
720 unsigned char buf[1];
722 typedef struct gcry_md_handle *GCRY_MD_HD;
723 typedef struct gcry_md_handle *GcryMDHd;
726 /* Create a message digest object for algorithm ALGO. FLAGS may be
727 given as an bitwise OR of the gcry_md_flags values. ALGO may be
728 given as 0 if the algorithms to be used are later set using
730 GcryMDHd gcry_md_open (int algo, unsigned int flags);
732 /* Release the message digest object HD. */
733 void gcry_md_close (GcryMDHd hd);
735 /* Add the message digest algorithm ALGO to the digest object HD. */
736 int gcry_md_enable( GcryMDHd hd, int algo );
738 /* Create a new digest object as an exact copy of the object HD. */
739 GcryMDHd gcry_md_copy (GcryMDHd hd);
741 /* Reset the digest object HD to its initail state. */
742 void gcry_md_reset (GcryMDHd hd);
744 /* Perform various operations on the digets object HD. */
745 int gcry_md_ctl (GcryMDHd hd, int cmd, unsigned char *buffer, size_t buflen);
747 /* Pass LENGTH bytes of data in BUFFER to the digest object HD so that
748 it can update the digest values. This is the actual hash
750 void gcry_md_write (GcryMDHd hd, const void *buffer, size_t length);
752 /* Read out the final digest from HD return the digest value for
754 unsigned char *gcry_md_read (GcryMDHd hd, int algo);
756 /* Convenience function to calculate the hash from the data in BUFFER
757 of size LENGTH using the algorithm ALGO avoiding the creating of a
758 hash object. The hash is returned in the caller provided buffer
759 DIGEST which must be large enough to hold the digest of the given
761 void gcry_md_hash_buffer (int algo, void *digest,
762 const void *buffer, size_t length);
764 /* Retrieve the algorithm used with HD. This does not work reliable
765 if more than one algorithm is enabled in HD. */
766 int gcry_md_get_algo (GcryMDHd hd);
768 /* Retrieved the length in bytes of the digest yielded by algorithm
770 unsigned int gcry_md_get_algo_dlen (int algo);
772 /* Retrieve various information about the object H. */
773 int gcry_md_info (GcryMDHd h, int what, void *buffer, size_t *nbytes);
775 /* Retrieve various information about the algorithm ALGO. */
776 int gcry_md_algo_info( int algo, int what, void *buffer, size_t *nbytes);
778 /* Map the digest algorithm id ALGO to a string representation of the
779 algorithm name. For unknown algorithms this functions returns an
781 const char *gcry_md_algo_name (int algo) _GCRY_GCC_ATTR_PURE;
783 /* Map the algorithm NAME to a digest algorithm Id. Return 0 if
784 the algorithm name is not known. */
785 int gcry_md_map_name (const char* name) _GCRY_GCC_ATTR_PURE;
787 /* For use with the HMAC feature, the set MAC key to the KEY of
789 int gcry_md_setkey (GcryMDHd hd, const void *key, size_t keylen);
791 /* Update the hash(s) of H with the character C. This is a buffered
792 version of the gcry_md_write function. */
793 #define gcry_md_putc(h,c) \
795 GcryMDHd h__ = (h); \
796 if( (h__)->bufpos == (h__)->bufsize ) \
797 gcry_md_write( (h__), NULL, 0 ); \
798 (h__)->buf[(h__)->bufpos++] = (c) & 0xff; \
801 /* Finalize the digest calculation. This is not really needed because
802 gcry_md_read() does this implicitly. */
803 #define gcry_md_final(a) \
804 gcry_md_ctl ((a), GCRYCTL_FINALIZE, NULL, 0)
806 /* Return true when the digest object is allocated in "secure" memory. */
807 #define gcry_md_is_secure(a) \
808 gcry_md_info( (a), GCRYCTL_IS_SECURE, NULL, NULL )
810 /* Return 0 if the algorithm A is available for use. */
811 #define gcry_md_test_algo(a) \
812 gcry_md_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL )
814 /* Return an DER encoded ASN.1 OID for the algorithm A in buffer B. N
815 must point to size_t variable with the available size of buffer B.
816 After return it will receive the actual size of the returned
818 #define gcry_md_get_asnoid(a,b,n) \
819 gcry_md_algo_info((a), GCRYCTL_GET_ASNOID, (b), (n))
821 /* Enable debugging for digets object A; i.e. create files named
822 dbgmd-<n>.<string> while hashing. B is a string used as the suffix
824 #define gcry_md_start_debug(a,b) \
825 gcry_md_ctl( (a), GCRYCTL_START_DUMP, (b), 0 )
827 /* Disable the debugging of A. */
828 #define gcry_md_stop_debug(a,b) \
829 gcry_md_ctl( (a), GCRYCTL_STOP_DUMP, (b), 0 )
833 /************************************
835 * random generating functions *
837 ************************************/
839 /* The possible values for the random quality. The rule of thumb is
840 to use WEAK for random number which don't need to be
841 cryptographically strong, STRONG for session keys and VERY_STRONG
843 enum gcry_random_level
845 GCRY_WEAK_RANDOM = 0,
846 GCRY_STRONG_RANDOM = 1,
847 GCRY_VERY_STRONG_RANDOM = 2
850 /* Fill BUFFER with LENGTH bytes of random, using random numbers of
852 void gcry_randomize (unsigned char *buffer, size_t length,
853 enum gcry_random_level level);
855 /* Return NBYTES of allocated random using a random numbers of quality
857 void *gcry_random_bytes (size_t nbytes, enum gcry_random_level level)
858 _GCRY_GCC_ATTR_MALLOC;
860 /* Return NBYTES of allocated random using a random numbers of quality
861 LEVEL. The random numbers are created returned in "secure"
863 void *gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level)
864 _GCRY_GCC_ATTR_MALLOC;
867 /* Set the big inetger W to a random value of NBITS using a random
868 generator with quality LEVEL. */
869 void gcry_mpi_randomize (GcryMPI w,
870 unsigned int nbits, enum gcry_random_level level);
874 /************************************
876 * miscellaneous stuff *
878 ************************************/
880 /* Log leveles used by the internal logging facility. */
883 GCRY_LOG_CONT = 0, /* continue the last log line */
893 /* Certain operations can provide progress information. This function
894 is used to register a handler for retrieving these information. */
895 void gcry_set_progress_handler (void (*cb)(void *,const char*,int, int, int),
900 /* Register a custom memory allocation functions. */
901 void gcry_set_allocation_handler (void *(*new_alloc_func)(size_t n),
902 void *(*new_alloc_secure_func)(size_t n),
903 int (*new_is_secure_func)(const void*),
904 void *(*new_realloc_func)(void *p, size_t n),
905 void (*new_free_func)(void*));
907 /* Register a function used instead of the internal out of memory
909 void gcry_set_outofcore_handler (int (*h)(void*, size_t, unsigned int),
912 /* Register a function used instead of the internal fatal error
914 void gcry_set_fatalerror_handler (void (*fnc)(void*,int, const char*),
917 /* Reserved for future use. */
918 void gcry_set_gettext_handler (const char *(*f)(const char*));
920 /* Regstier a function used instead of the internal logging
922 void gcry_set_log_handler (void (*f)(void*,int, const char*, va_list),
926 /* Libgcrypt uses its own memory allocation. It is important to use
927 gcry_free () to release memory allocated by libgcrypt. */
928 void *gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
929 void *gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
930 void *gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
931 void *gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
932 void *gcry_realloc (void *a, size_t n);
933 char *gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC;
934 void *gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
935 void *gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
936 void *gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
937 void *gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
938 void *gcry_xrealloc (void *a, size_t n);
939 char *gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC;
940 void gcry_free (void *a);
942 /* Return true if A is allocated in "secure" memory. */
943 int gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE;
946 #ifndef GCRYPT_NO_MPI_MACROS
947 # ifndef DID_MPI_TYPEDEF
948 typedef struct gcry_mpi *MPI;
949 # define DID_MPI_TYPEDEF
951 #endif /* GCRYPT_NO_MPI_MACROS */
956 #endif /* _GCRYPT_H */