fix DH_generate_parameters for general 'generator'
[openssl.git] / crypto / crypto.h
1 /* crypto/crypto.h */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #ifndef HEADER_CRYPTO_H
60 #define HEADER_CRYPTO_H
61
62 #include <stdlib.h>
63
64 #ifndef OPENSSL_NO_FP_API
65 #include <stdio.h>
66 #endif
67
68 #include <openssl/stack.h>
69 #include <openssl/safestack.h>
70 #include <openssl/opensslv.h>
71
72 #ifdef CHARSET_EBCDIC
73 #include <openssl/ebcdic.h>
74 #endif
75
76 /* Resolve problems on some operating systems with symbol names that clash
77    one way or another */
78 #include <openssl/symhacks.h>
79
80 #ifdef  __cplusplus
81 extern "C" {
82 #endif
83
84 /* Backward compatibility to SSLeay */
85 /* This is more to be used to check the correct DLL is being used
86  * in the MS world. */
87 #define SSLEAY_VERSION_NUMBER   OPENSSL_VERSION_NUMBER
88 #define SSLEAY_VERSION          0
89 /* #define SSLEAY_OPTIONS       1 no longer supported */
90 #define SSLEAY_CFLAGS           2
91 #define SSLEAY_BUILT_ON         3
92 #define SSLEAY_PLATFORM         4
93 #define SSLEAY_DIR              5
94
95 /* When changing the CRYPTO_LOCK_* list, be sure to maintin the text lock
96  * names in cryptlib.c
97  */
98
99 #define CRYPTO_LOCK_ERR                 1
100 #define CRYPTO_LOCK_EX_DATA             2
101 #define CRYPTO_LOCK_X509                3
102 #define CRYPTO_LOCK_X509_INFO           4
103 #define CRYPTO_LOCK_X509_PKEY           5
104 #define CRYPTO_LOCK_X509_CRL            6
105 #define CRYPTO_LOCK_X509_REQ            7
106 #define CRYPTO_LOCK_DSA                 8
107 #define CRYPTO_LOCK_RSA                 9
108 #define CRYPTO_LOCK_EVP_PKEY            10
109 #define CRYPTO_LOCK_X509_STORE          11
110 #define CRYPTO_LOCK_SSL_CTX             12
111 #define CRYPTO_LOCK_SSL_CERT            13
112 #define CRYPTO_LOCK_SSL_SESSION         14
113 #define CRYPTO_LOCK_SSL_SESS_CERT       15
114 #define CRYPTO_LOCK_SSL                 16
115 #define CRYPTO_LOCK_RAND                17
116 #define CRYPTO_LOCK_RAND2               18
117 #define CRYPTO_LOCK_MALLOC              19
118 #define CRYPTO_LOCK_BIO                 20
119 #define CRYPTO_LOCK_GETHOSTBYNAME       21
120 #define CRYPTO_LOCK_GETSERVBYNAME       22
121 #define CRYPTO_LOCK_READDIR             23
122 #define CRYPTO_LOCK_RSA_BLINDING        24
123 #define CRYPTO_LOCK_DH                  25
124 #define CRYPTO_LOCK_MALLOC2             26
125 #define CRYPTO_LOCK_DSO                 27
126 #define CRYPTO_LOCK_DYNLOCK             28
127 #define CRYPTO_LOCK_ENGINE              29
128 #define CRYPTO_LOCK_UI                  30
129 #define CRYPTO_LOCK_ECDSA               31
130 #define CRYPTO_LOCK_EC                  32
131 #define CRYPTO_NUM_LOCKS                33
132
133 #define CRYPTO_LOCK             1
134 #define CRYPTO_UNLOCK           2
135 #define CRYPTO_READ             4
136 #define CRYPTO_WRITE            8
137
138 #ifndef OPENSSL_NO_LOCKING
139 #ifndef CRYPTO_w_lock
140 #define CRYPTO_w_lock(type)     \
141         CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
142 #define CRYPTO_w_unlock(type)   \
143         CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
144 #define CRYPTO_r_lock(type)     \
145         CRYPTO_lock(CRYPTO_LOCK|CRYPTO_READ,type,__FILE__,__LINE__)
146 #define CRYPTO_r_unlock(type)   \
147         CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_READ,type,__FILE__,__LINE__)
148 #define CRYPTO_add(addr,amount,type)    \
149         CRYPTO_add_lock(addr,amount,type,__FILE__,__LINE__)
150 #endif
151 #else
152 #define CRYPTO_w_lock(a)
153 #define CRYPTO_w_unlock(a)
154 #define CRYPTO_r_lock(a)
155 #define CRYPTO_r_unlock(a)
156 #define CRYPTO_add(a,b,c)       ((*(a))+=(b))
157 #endif
158
159 /* Some applications as well as some parts of OpenSSL need to allocate
160    and deallocate locks in a dynamic fashion.  The following typedef
161    makes this possible in a type-safe manner.  */
162 /* struct CRYPTO_dynlock_value has to be defined by the application. */
163 typedef struct
164         {
165         int references;
166         struct CRYPTO_dynlock_value *data;
167         } CRYPTO_dynlock;
168
169
170 /* The following can be used to detect memory leaks in the SSLeay library.
171  * It used, it turns on malloc checking */
172
173 #define CRYPTO_MEM_CHECK_OFF    0x0     /* an enume */
174 #define CRYPTO_MEM_CHECK_ON     0x1     /* a bit */
175 #define CRYPTO_MEM_CHECK_ENABLE 0x2     /* a bit */
176 #define CRYPTO_MEM_CHECK_DISABLE 0x3    /* an enume */
177
178 /* The following are bit values to turn on or off options connected to the
179  * malloc checking functionality */
180
181 /* Adds time to the memory checking information */
182 #define V_CRYPTO_MDEBUG_TIME    0x1 /* a bit */
183 /* Adds thread number to the memory checking information */
184 #define V_CRYPTO_MDEBUG_THREAD  0x2 /* a bit */
185
186 #define V_CRYPTO_MDEBUG_ALL (V_CRYPTO_MDEBUG_TIME | V_CRYPTO_MDEBUG_THREAD)
187
188
189 /* predec of the BIO type */
190 typedef struct bio_st BIO_dummy;
191
192 typedef struct crypto_ex_data_st
193         {
194         STACK *sk;
195         int dummy; /* gcc is screwing up this data structure :-( */
196         } CRYPTO_EX_DATA;
197
198 /* Called when a new object is created */
199 typedef int CRYPTO_EX_new(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
200                                         int idx, long argl, void *argp);
201 /* Called when an object is free()ed */
202 typedef void CRYPTO_EX_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
203                                         int idx, long argl, void *argp);
204 /* Called when we need to dup an object */
205 typedef int CRYPTO_EX_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from, void *from_d, 
206                                         int idx, long argl, void *argp);
207
208 /* This stuff is basically class callback functions
209  * The current classes are SSL_CTX, SSL, SSL_SESSION, and a few more */
210
211 typedef struct crypto_ex_data_func_st
212         {
213         long argl;      /* Arbitary long */
214         void *argp;     /* Arbitary void * */
215         CRYPTO_EX_new *new_func;
216         CRYPTO_EX_free *free_func;
217         CRYPTO_EX_dup *dup_func;
218         } CRYPTO_EX_DATA_FUNCS;
219
220 DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
221
222 /* Per class, we have a STACK of CRYPTO_EX_DATA_FUNCS for each CRYPTO_EX_DATA
223  * entry.
224  */
225
226 #define CRYPTO_EX_INDEX_BIO             0
227 #define CRYPTO_EX_INDEX_SSL             1
228 #define CRYPTO_EX_INDEX_SSL_CTX         2
229 #define CRYPTO_EX_INDEX_SSL_SESSION     3
230 #define CRYPTO_EX_INDEX_X509_STORE      4
231 #define CRYPTO_EX_INDEX_X509_STORE_CTX  5
232 #define CRYPTO_EX_INDEX_RSA             6
233 #define CRYPTO_EX_INDEX_DSA             7
234 #define CRYPTO_EX_INDEX_DH              8
235 #define CRYPTO_EX_INDEX_ENGINE          9
236 #define CRYPTO_EX_INDEX_X509            10
237 #define CRYPTO_EX_INDEX_UI              11
238 #define CRYPTO_EX_INDEX_ECDSA           12
239
240 /* Dynamically assigned indexes start from this value (don't use directly, use
241  * via CRYPTO_ex_data_new_class). */
242 #define CRYPTO_EX_INDEX_USER            100
243
244
245 /* This is the default callbacks, but we can have others as well:
246  * this is needed in Win32 where the application malloc and the
247  * library malloc may not be the same.
248  */
249 #define CRYPTO_malloc_init()    CRYPTO_set_mem_functions(\
250         malloc, realloc, free)
251
252 #if defined CRYPTO_MDEBUG_ALL || defined CRYPTO_MDEBUG_TIME || defined CRYPTO_MDEBUG_THREAD
253 # ifndef CRYPTO_MDEBUG /* avoid duplicate #define */
254 #  define CRYPTO_MDEBUG
255 # endif
256 #endif
257
258 /* Set standard debugging functions (not done by default
259  * unless CRYPTO_MDEBUG is defined) */
260 #define CRYPTO_malloc_debug_init()      do {\
261         CRYPTO_set_mem_debug_functions(\
262                 CRYPTO_dbg_malloc,\
263                 CRYPTO_dbg_realloc,\
264                 CRYPTO_dbg_free,\
265                 CRYPTO_dbg_set_options,\
266                 CRYPTO_dbg_get_options);\
267         } while(0)
268
269 int CRYPTO_mem_ctrl(int mode);
270 int CRYPTO_is_mem_check_on(void);
271
272 /* for applications */
273 #define MemCheck_start() CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON)
274 #define MemCheck_stop() CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_OFF)
275
276 /* for library-internal use */
277 #define MemCheck_on()   CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE)
278 #define MemCheck_off()  CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE)
279 #define is_MemCheck_on() CRYPTO_is_mem_check_on()
280
281 #define OPENSSL_malloc(num)     CRYPTO_malloc((int)num,__FILE__,__LINE__)
282 #define OPENSSL_realloc(addr,num) \
283         CRYPTO_realloc((char *)addr,(int)num,__FILE__,__LINE__)
284 #define OPENSSL_remalloc(addr,num) \
285         CRYPTO_remalloc((char **)addr,(int)num,__FILE__,__LINE__)
286 #define OPENSSL_freeFunc        CRYPTO_free
287 #define OPENSSL_free(addr)      CRYPTO_free(addr)
288
289 #define OPENSSL_malloc_locked(num) \
290         CRYPTO_malloc_locked((int)num,__FILE__,__LINE__)
291 #define OPENSSL_free_locked(addr) CRYPTO_free_locked(addr)
292
293
294 const char *SSLeay_version(int type);
295 unsigned long SSLeay(void);
296
297 int OPENSSL_issetugid(void);
298
299 /* An opaque type representing an implementation of "ex_data" support */
300 typedef struct st_CRYPTO_EX_DATA_IMPL   CRYPTO_EX_DATA_IMPL;
301 /* Return an opaque pointer to the current "ex_data" implementation */
302 const CRYPTO_EX_DATA_IMPL *CRYPTO_get_ex_data_implementation(void);
303 /* Sets the "ex_data" implementation to be used (if it's not too late) */
304 int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i);
305 /* Get a new "ex_data" class, and return the corresponding "class_index" */
306 int CRYPTO_ex_data_new_class(void);
307 /* Within a given class, get/register a new index */
308 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
309                 CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
310                 CRYPTO_EX_free *free_func);
311 /* Initialise/duplicate/free CRYPTO_EX_DATA variables corresponding to a given
312  * class (invokes whatever per-class callbacks are applicable) */
313 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
314 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
315                 CRYPTO_EX_DATA *from);
316 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
317 /* Get/set data in a CRYPTO_EX_DATA variable corresponding to a particular index
318  * (relative to the class type involved) */
319 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val);
320 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad,int idx);
321 /* This function cleans up all "ex_data" state. It mustn't be called under
322  * potential race-conditions. */
323 void CRYPTO_cleanup_all_ex_data(void);
324
325 int CRYPTO_get_new_lockid(char *name);
326
327 int CRYPTO_num_locks(void); /* return CRYPTO_NUM_LOCKS (shared libs!) */
328 void CRYPTO_lock(int mode, int type,const char *file,int line);
329 void CRYPTO_set_locking_callback(void (*func)(int mode,int type,
330                                               const char *file,int line));
331 void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file,
332                 int line);
333 void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,int type,
334                                               const char *file, int line));
335 int (*CRYPTO_get_add_lock_callback(void))(int *num,int mount,int type,
336                                           const char *file,int line);
337 void CRYPTO_set_id_callback(unsigned long (*func)(void));
338 unsigned long (*CRYPTO_get_id_callback(void))(void);
339 unsigned long CRYPTO_thread_id(void);
340 const char *CRYPTO_get_lock_name(int type);
341 int CRYPTO_add_lock(int *pointer,int amount,int type, const char *file,
342                     int line);
343
344 int CRYPTO_get_new_dynlockid(void);
345 void CRYPTO_destroy_dynlockid(int i);
346 struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i);
347 void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*dyn_create_function)(const char *file, int line));
348 void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line));
349 void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)(struct CRYPTO_dynlock_value *l, const char *file, int line));
350 struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))(const char *file,int line);
351 void (*CRYPTO_get_dynlock_lock_callback(void))(int mode, struct CRYPTO_dynlock_value *l, const char *file,int line);
352 void (*CRYPTO_get_dynlock_destroy_callback(void))(struct CRYPTO_dynlock_value *l, const char *file,int line);
353
354 /* CRYPTO_set_mem_functions includes CRYPTO_set_locked_mem_functions --
355  * call the latter last if you need different functions */
356 int CRYPTO_set_mem_functions(void *(*m)(size_t),void *(*r)(void *,size_t), void (*f)(void *));
357 int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*free_func)(void *));
358 int CRYPTO_set_mem_ex_functions(void *(*m)(size_t,const char *,int),
359                                 void *(*r)(void *,size_t,const char *,int),
360                                 void (*f)(void *));
361 int CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t,const char *,int),
362                                        void (*free_func)(void *));
363 int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
364                                    void (*r)(void *,void *,int,const char *,int,int),
365                                    void (*f)(void *,int),
366                                    void (*so)(long),
367                                    long (*go)(void));
368 void CRYPTO_get_mem_functions(void *(**m)(size_t),void *(**r)(void *, size_t), void (**f)(void *));
369 void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *));
370 void CRYPTO_get_mem_ex_functions(void *(**m)(size_t,const char *,int),
371                                  void *(**r)(void *, size_t,const char *,int),
372                                  void (**f)(void *));
373 void CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t,const char *,int),
374                                         void (**f)(void *));
375 void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
376                                     void (**r)(void *,void *,int,const char *,int,int),
377                                     void (**f)(void *,int),
378                                     void (**so)(long),
379                                     long (**go)(void));
380
381 void *CRYPTO_malloc_locked(int num, const char *file, int line);
382 void CRYPTO_free_locked(void *);
383 void *CRYPTO_malloc(int num, const char *file, int line);
384 void CRYPTO_free(void *);
385 void *CRYPTO_realloc(void *addr,int num, const char *file, int line);
386 void *CRYPTO_remalloc(void *addr,int num, const char *file, int line);
387
388 void CRYPTO_set_mem_debug_options(long bits);
389 long CRYPTO_get_mem_debug_options(void);
390
391 #define CRYPTO_push_info(info) \
392         CRYPTO_push_info_(info, __FILE__, __LINE__);
393 int CRYPTO_push_info_(const char *info, const char *file, int line);
394 int CRYPTO_pop_info(void);
395 int CRYPTO_remove_all_info(void);
396
397
398 /* Default debugging functions (enabled by CRYPTO_malloc_debug_init() macro;
399  * used as default in CRYPTO_MDEBUG compilations): */
400 /* The last argument has the following significance:
401  *
402  * 0:   called before the actual memory allocation has taken place
403  * 1:   called after the actual memory allocation has taken place
404  */
405 void CRYPTO_dbg_malloc(void *addr,int num,const char *file,int line,int before_p);
406 void CRYPTO_dbg_realloc(void *addr1,void *addr2,int num,const char *file,int line,int before_p);
407 void CRYPTO_dbg_free(void *addr,int before_p);
408 /* Tell the debugging code about options.  By default, the following values
409  * apply:
410  *
411  * 0:                           Clear all options.
412  * V_CRYPTO_MDEBUG_TIME (1):    Set the "Show Time" option.
413  * V_CRYPTO_MDEBUG_THREAD (2):  Set the "Show Thread Number" option.
414  * V_CRYPTO_MDEBUG_ALL (3):     1 + 2
415  */
416 void CRYPTO_dbg_set_options(long bits);
417 long CRYPTO_dbg_get_options(void);
418
419
420 #ifndef OPENSSL_NO_FP_API
421 void CRYPTO_mem_leaks_fp(FILE *);
422 #endif
423 void CRYPTO_mem_leaks(struct bio_st *bio);
424 /* unsigned long order, char *file, int line, int num_bytes, char *addr */
425 typedef void *CRYPTO_MEM_LEAK_CB(unsigned long, const char *, int, int, void *);
426 void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb);
427
428
429 /* BEGIN ERROR CODES */
430 /* The following lines are auto generated by the script mkerr.pl. Any changes
431  * made after this point may be overwritten when the script is next run.
432  */
433 void ERR_load_CRYPTO_strings(void);
434
435 /* Error codes for the CRYPTO functions. */
436
437 /* Function codes. */
438 #define CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX                 100
439 #define CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID                103
440 #define CRYPTO_F_CRYPTO_GET_NEW_LOCKID                   101
441 #define CRYPTO_F_CRYPTO_SET_EX_DATA                      102
442 #define CRYPTO_F_DEF_ADD_INDEX                           104
443 #define CRYPTO_F_DEF_GET_CLASS                           105
444 #define CRYPTO_F_INT_DUP_EX_DATA                         106
445 #define CRYPTO_F_INT_FREE_EX_DATA                        107
446 #define CRYPTO_F_INT_NEW_EX_DATA                         108
447
448 /* Reason codes. */
449 #define CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK              100
450
451 #ifdef  __cplusplus
452 }
453 #endif
454 #endif