1 /* gcrypt.h - GNU digital encryption library interface
2 * Copyright (C) 1998 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG 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 General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
28 #ifndef HAVE_BYTE_TYPEDEF
29 #undef byte /* maybe there is a macro with this name */
30 typedef unsigned char byte;
31 #define HAVE_BYTE_TYPEDEF
34 /*******************************************
36 * error handling etc. *
38 *******************************************/
41 GCRYERR_SUCCESS = 0, /* "no error" */
42 GCRYERR_GENERAL = 1, /* catch all the other errors code */
43 GCRYERR_INV_OP = 2, /* invalid operation code or ctl command */
44 GCRYERR_NOMEM = 3, /* out of core */
45 GCRYERR_INV_ALGO = 4, /* invalid algorithm */
46 GCRYERR_INV_ARG = 5, /* invalid argument */
47 GCRYERR_INTERNAL = 6, /* internal error */
48 GCRYERR_EOF = 7, /* (-1) is remapped to this value */
49 GCRYERR_TOO_SHORT = 8, /* provided buffer too short */
50 GCRYERR_TOO_LARGE = 9, /* object is too large */
51 GCRYERR_INV_OBJ = 10, /* an object is not valid */
56 const char *gcry_strerror( int ec );
62 GCRYCTL_RESET = 4, /* e.g. for MDs */
66 int gcry_control( enum gcry_ctl_cmds, ... );
68 enum gcry_random_level {
70 GCRY_STRONG_RANDOM = 1,
71 GCRY_VERY_STRONG_RANDOM = 2
76 typedef struct gcry_sexp *GCRY_SEXP;
78 enum gcry_sexp_format {
79 GCRY_SEXP_FMT_DEFAULT = 0,
80 GCRY_SEXP_FMT_CANON = 1,
81 GCRY_SEXP_FMT_BASE64 = 2,
82 GCRY_SEXP_FMT_ADVANCED = 3,
85 /*******************************************
87 * multi precision integer functions *
89 *******************************************/
91 enum gcry_mpi_format {
92 GCRYMPI_FMT_STD = 0, /* As used by OpenPGP */
93 GCRYMPI_FMT_SSH = 1, /* As used by SSH */
94 GCRYMPI_FMT_HEX = 2, /* hex format */
98 typedef struct gcry_mpi *GCRY_MPI;
100 GCRY_MPI gcry_mpi_new( unsigned int nbits );
101 GCRY_MPI gcry_mpi_snew( unsigned int nbits );
102 void gcry_mpi_release( GCRY_MPI a );
103 GCRY_MPI gcry_mpi_copy( const GCRY_MPI a );
104 GCRY_MPI gcry_mpi_set( GCRY_MPI w, const GCRY_MPI u );
105 GCRY_MPI gcry_mpi_set_ui( GCRY_MPI w, unsigned long u );
106 int gcry_mpi_cmp( const GCRY_MPI u, const GCRY_MPI v );
107 int gcry_mpi_cmp_ui( const GCRY_MPI u, unsigned long v );
108 void gcry_mpi_randomize( GCRY_MPI w,
109 unsigned int nbits, enum gcry_random_level level);
110 int gcry_mpi_scan( GCRY_MPI *ret_mpi, enum gcry_mpi_format format,
111 const char *buffer, size_t *nbytes );
112 int gcry_mpi_print( enum gcry_mpi_format format,
113 char *buffer, size_t *nbytes, const GCRY_MPI a );
115 void gcry_mpi_powm( GCRY_MPI w,
116 const GCRY_MPI b, const GCRY_MPI e, const GCRY_MPI m );
119 #ifndef GCRYPT_NO_MPI_MACROS
120 #define mpi_new(n) gcry_mpi_new( (n) )
121 #define mpi_secure_new( n ) gcry_mpi_snew( (n) )
122 #define mpi_release( a ) do { gcry_mpi_release( (a) ); \
123 (a) = NULL; } while(0)
124 #define mpi_copy( a ) gcry_mpi_copy( (a) )
125 #define mpi_set( w, u) gcry_mpi_set( (w), (u) )
126 #define mpi_set_ui( w, u) gcry_mpi_set_ui( (w), (u) )
127 #define mpi_cmp( u, v ) gcry_mpi_cmp( (u), (v) )
128 #define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) )
130 #define mpi_powm(w,b,e,m) gcry_mpi_powm( (w), (b), (e), (m) )
131 #endif /* GCRYPT_NO_MPI_MACROS */
133 /********************************************
134 ******* symmetric cipher functions *******
135 ********************************************/
137 struct gcry_cipher_context;
138 typedef struct gcry_cipher_context *GCRY_CIPHER_HD;
140 enum gcry_cipher_algos {
141 GCRY_CIPHER_NONE = 0,
142 GCRY_CIPHER_IDEA = 1,
143 GCRY_CIPHER_3DES = 2,
144 GCRY_CIPHER_CAST5 = 3,
145 GCRY_CIPHER_BLOWFISH = 4,
146 GCRY_CIPHER_SAFER_SK128 = 5,
147 GCRY_CIPHER_DES_SK = 6
150 enum gcry_cipher_modes {
151 GCRY_CIPHER_MODE_NONE = 0,
152 GCRY_CIPHER_MODE_ECB = 1,
153 GCRY_CIPHER_MODE_CFB = 2,
154 GCRY_CIPHER_MODE_CBC = 3,
157 enum gcry_cipher_flags {
158 GCRY_CIPHER_SECURE = 1, /* allocate in secure memory */
159 GCRY_CIPHER_ENABLE_SYNC = 2, /* enable CFB sync mode */
163 #if 0 /* not yet done */
164 int gcry_string_to_cipher_algo( const char *string );
165 const char * gcry_cipher_algo_to_string( int algo );
166 int gcry_check_cipher_algo( int algo );
167 unsigned gcry_cipher_get_keylen( int algo );
168 unsigned gcry_cipher_get_blocksize( int algo );
171 int gcry_cipher_open( GCRY_CIPHER_HD *rhd, int algo, int mode, unsigned flags);
172 void gcry_cipher_close( GCRY_CIPHER_HD h );
173 int gcry_cipher_ctl( GCRY_CIPHER_HD h, int cmd, byte *buffer, size_t buflen);
175 int gcry_cipher_encrypt( GCRY_CIPHER_HD h, byte *out, size_t outsize,
176 byte *in, size_t inlen );
177 int gcry_cipher_decrypt( GCRY_CIPHER_HD h, byte *out, size_t outsize,
178 byte *in, size_t inlen );
181 /* some handy macros */
182 #define gcry_cipher_setkey(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_KEY, \
184 #define gcry_cipher_setiv(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_IV, \
186 #define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, \
190 /*********************************************
191 ******* asymmetric cipher functions *******
192 *********************************************/
197 /*********************************************
198 ******* cryptograhic hash functions *******
199 *********************************************/
201 struct gcry_md_context;
202 typedef struct gcry_md_context *GCRY_MD_HD; /* same as the old MD_HANDLE */
213 GCRY_MD_FLAG_SECURE = 1
217 int gcry_md_open( GCRY_MD_HD *ret_hd, int algo, unsigned flags );
218 void gcry_md_close( GCRY_MD_HD hd );
219 int gcry_md_enable( GCRY_MD_HD hd, int algo );
220 GCRY_MD_HD gcry_md_copy( GCRY_MD_HD hd );
221 int gcry_md_ctl( GCRY_MD_HD hd, int cmd, byte *buffer, size_t buflen);
222 void gcry_md_write( GCRY_MD_HD hd, const byte *buffer, size_t length);
223 byte *gcry_md_read( GCRY_MD_HD hd, int algo );
224 int gcry_md_algo( GCRY_MD_HD hd );
225 size_t gcry_md_dlen( int algo );
226 int gcry_md_get( GCRY_MD_HD hd, int algo, byte *buffer, int buflen );
229 /*****************************************
230 ******* miscellaneous functions *******
231 *****************************************/
234 const char *g10m_revision_string(int mode);
235 const char *g10c_revision_string(int mode);
236 const char *g10u_revision_string(int mode);
238 MPI g10c_generate_secret_prime( unsigned nbits );
239 char *g10c_get_random_bits( unsigned nbits, int level, int secure );
242 void *g10_malloc( size_t n );
243 void *g10_calloc( size_t n );
244 void *g10_malloc_secure( size_t n );
245 void *g10_calloc_secure( size_t n );
246 void *g10_realloc( void *a, size_t n );
247 void g10_free( void *p );
248 char *g10_strdup( const char * a);
250 void g10_log_bug( const char *fmt, ... );
251 void g10_log_bug0( const char *, int );
252 void g10_log_fatal( const char *fmt, ... );
253 void g10_log_error( const char *fmt, ... );
254 void g10_log_info( const char *fmt, ... );
255 void g10_log_debug( const char *fmt, ... );
256 void g10_log_hexdump( const char *text, char *buf, size_t len );
257 void g10_log_mpidump( const char *text, MPI a );
260 /***************************
261 ******* constants *******
262 **************************/
264 #define CIPHER_ALGO_NONE 0
265 #define CIPHER_ALGO_IDEA 1
266 #define CIPHER_ALGO_3DES 2
267 #define CIPHER_ALGO_CAST5 3
268 #define CIPHER_ALGO_BLOWFISH 4 /* blowfish 128 bit key */
269 #define CIPHER_ALGO_SAFER_SK128 5
270 #define CIPHER_ALGO_DES_SK 6
271 #define CIPHER_ALGO_DUMMY 110 /* no encryption at all */
273 #define PUBKEY_ALGO_RSA 1
274 #define PUBKEY_ALGO_RSA_E 2 /* RSA encrypt only */
275 #define PUBKEY_ALGO_RSA_S 3 /* RSA sign only */
276 #define PUBKEY_ALGO_ELGAMAL_E 16 /* encrypt only ElGamal (but not for v3)*/
277 #define PUBKEY_ALGO_DSA 17
278 #define PUBKEY_ALGO_ELGAMAL 20 /* sign and encrypt elgamal */
280 #define DIGEST_ALGO_MD5 1
281 #define DIGEST_ALGO_SHA1 2
282 #define DIGEST_ALGO_RMD160 3
283 #define DIGEST_ALGO_TIGER 6
285 #define is_RSA(a) ((a)==PUBKEY_ALGO_RSA || (a)==PUBKEY_ALGO_RSA_E \
286 || (a)==PUBKEY_ALGO_RSA_S )
287 #define is_ELGAMAL(a) ((a)==PUBKEY_ALGO_ELGAMAL || (a)==PUBKEY_ALGO_ELGAMAL_E)
289 #define G10ERR_GENERAL 1
290 #define G10ERR_PUBKEY_ALGO 4
291 #define G10ERR_DIGEST_ALGO 5
292 #define G10ERR_BAD_PUBKEY 6
293 #define G10ERR_BAD_SECKEY 7
294 #define G10ERR_BAD_SIGN 8
295 #define G10ERR_CIPHER_ALGO 12
296 #define G10ERR_WRONG_SECKEY 18
297 #define G10ERR_UNSUPPORTED 19
298 #define G10ERR_NI_PUBKEY 27
299 #define G10ERR_NI_CIPHER 28
300 #define G10ERR_BAD_MPI 30
301 #define G10ERR_WR_PUBKEY_ALGO 41
304 /***********************************************
306 * Some very handy macros *
308 ***********************************************/
309 #ifndef GCRYPT_NO_MPI_MACROS
311 typedef struct gcry_mpi *MPI;
314 #endif /* GCRYPT_NO_MPI_MACROS */
319 #endif /* _GCRYPT_H */