xref: /onnv-gate/usr/src/common/openssl/crypto/mem.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/mem.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include <stdlib.h>
61*0Sstevel@tonic-gate #include <openssl/crypto.h>
62*0Sstevel@tonic-gate #include "cryptlib.h"
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static int allow_customize = 1;      /* we provide flexible functions for */
66*0Sstevel@tonic-gate static int allow_customize_debug = 1;/* exchanging memory-related functions at
67*0Sstevel@tonic-gate                                       * run-time, but this must be done
68*0Sstevel@tonic-gate                                       * before any blocks are actually
69*0Sstevel@tonic-gate                                       * allocated; or we'll run into huge
70*0Sstevel@tonic-gate                                       * problems when malloc/free pairs
71*0Sstevel@tonic-gate                                       * don't match etc. */
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /* the following pointers may be changed as long as 'allow_customize' is set */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static void *(*malloc_func)(size_t)         = malloc;
78*0Sstevel@tonic-gate static void *default_malloc_ex(size_t num, const char *file, int line)
79*0Sstevel@tonic-gate 	{ return malloc_func(num); }
80*0Sstevel@tonic-gate static void *(*malloc_ex_func)(size_t, const char *file, int line)
81*0Sstevel@tonic-gate         = default_malloc_ex;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate static void *(*realloc_func)(void *, size_t)= realloc;
84*0Sstevel@tonic-gate static void *default_realloc_ex(void *str, size_t num,
85*0Sstevel@tonic-gate         const char *file, int line)
86*0Sstevel@tonic-gate 	{ return realloc_func(str,num); }
87*0Sstevel@tonic-gate static void *(*realloc_ex_func)(void *, size_t, const char *file, int line)
88*0Sstevel@tonic-gate         = default_realloc_ex;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate static void (*free_func)(void *)            = free;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate static void *(*malloc_locked_func)(size_t)  = malloc;
93*0Sstevel@tonic-gate static void *default_malloc_locked_ex(size_t num, const char *file, int line)
94*0Sstevel@tonic-gate 	{ return malloc_locked_func(num); }
95*0Sstevel@tonic-gate static void *(*malloc_locked_ex_func)(size_t, const char *file, int line)
96*0Sstevel@tonic-gate         = default_malloc_locked_ex;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate static void (*free_locked_func)(void *)     = free;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate /* may be changed as long as 'allow_customize_debug' is set */
103*0Sstevel@tonic-gate /* XXX use correct function pointer types */
104*0Sstevel@tonic-gate #ifdef CRYPTO_MDEBUG
105*0Sstevel@tonic-gate /* use default functions from mem_dbg.c */
106*0Sstevel@tonic-gate static void (*malloc_debug_func)(void *,int,const char *,int,int)
107*0Sstevel@tonic-gate 	= CRYPTO_dbg_malloc;
108*0Sstevel@tonic-gate static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
109*0Sstevel@tonic-gate 	= CRYPTO_dbg_realloc;
110*0Sstevel@tonic-gate static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free;
111*0Sstevel@tonic-gate static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options;
112*0Sstevel@tonic-gate static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options;
113*0Sstevel@tonic-gate #else
114*0Sstevel@tonic-gate /* applications can use CRYPTO_malloc_debug_init() to select above case
115*0Sstevel@tonic-gate  * at run-time */
116*0Sstevel@tonic-gate static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL;
117*0Sstevel@tonic-gate static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
118*0Sstevel@tonic-gate 	= NULL;
119*0Sstevel@tonic-gate static void (*free_debug_func)(void *,int) = NULL;
120*0Sstevel@tonic-gate static void (*set_debug_options_func)(long) = NULL;
121*0Sstevel@tonic-gate static long (*get_debug_options_func)(void) = NULL;
122*0Sstevel@tonic-gate #endif
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
126*0Sstevel@tonic-gate 	void (*f)(void *))
127*0Sstevel@tonic-gate 	{
128*0Sstevel@tonic-gate 	if (!allow_customize)
129*0Sstevel@tonic-gate 		return 0;
130*0Sstevel@tonic-gate 	if ((m == 0) || (r == 0) || (f == 0))
131*0Sstevel@tonic-gate 		return 0;
132*0Sstevel@tonic-gate 	malloc_func=m; malloc_ex_func=default_malloc_ex;
133*0Sstevel@tonic-gate 	realloc_func=r; realloc_ex_func=default_realloc_ex;
134*0Sstevel@tonic-gate 	free_func=f;
135*0Sstevel@tonic-gate 	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
136*0Sstevel@tonic-gate 	free_locked_func=f;
137*0Sstevel@tonic-gate 	return 1;
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate int CRYPTO_set_mem_ex_functions(
141*0Sstevel@tonic-gate         void *(*m)(size_t,const char *,int),
142*0Sstevel@tonic-gate         void *(*r)(void *, size_t,const char *,int),
143*0Sstevel@tonic-gate 	void (*f)(void *))
144*0Sstevel@tonic-gate 	{
145*0Sstevel@tonic-gate 	if (!allow_customize)
146*0Sstevel@tonic-gate 		return 0;
147*0Sstevel@tonic-gate 	if ((m == 0) || (r == 0) || (f == 0))
148*0Sstevel@tonic-gate 		return 0;
149*0Sstevel@tonic-gate 	malloc_func=0; malloc_ex_func=m;
150*0Sstevel@tonic-gate 	realloc_func=0; realloc_ex_func=r;
151*0Sstevel@tonic-gate 	free_func=f;
152*0Sstevel@tonic-gate 	malloc_locked_func=0; malloc_locked_ex_func=m;
153*0Sstevel@tonic-gate 	free_locked_func=f;
154*0Sstevel@tonic-gate 	return 1;
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
158*0Sstevel@tonic-gate 	{
159*0Sstevel@tonic-gate 	if (!allow_customize)
160*0Sstevel@tonic-gate 		return 0;
161*0Sstevel@tonic-gate 	if ((m == NULL) || (f == NULL))
162*0Sstevel@tonic-gate 		return 0;
163*0Sstevel@tonic-gate 	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
164*0Sstevel@tonic-gate 	free_locked_func=f;
165*0Sstevel@tonic-gate 	return 1;
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate int CRYPTO_set_locked_mem_ex_functions(
169*0Sstevel@tonic-gate         void *(*m)(size_t,const char *,int),
170*0Sstevel@tonic-gate         void (*f)(void *))
171*0Sstevel@tonic-gate 	{
172*0Sstevel@tonic-gate 	if (!allow_customize)
173*0Sstevel@tonic-gate 		return 0;
174*0Sstevel@tonic-gate 	if ((m == NULL) || (f == NULL))
175*0Sstevel@tonic-gate 		return 0;
176*0Sstevel@tonic-gate 	malloc_locked_func=0; malloc_locked_ex_func=m;
177*0Sstevel@tonic-gate 	free_func=f;
178*0Sstevel@tonic-gate 	return 1;
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
182*0Sstevel@tonic-gate 				   void (*r)(void *,void *,int,const char *,int,int),
183*0Sstevel@tonic-gate 				   void (*f)(void *,int),
184*0Sstevel@tonic-gate 				   void (*so)(long),
185*0Sstevel@tonic-gate 				   long (*go)(void))
186*0Sstevel@tonic-gate 	{
187*0Sstevel@tonic-gate 	if (!allow_customize_debug)
188*0Sstevel@tonic-gate 		return 0;
189*0Sstevel@tonic-gate 	malloc_debug_func=m;
190*0Sstevel@tonic-gate 	realloc_debug_func=r;
191*0Sstevel@tonic-gate 	free_debug_func=f;
192*0Sstevel@tonic-gate 	set_debug_options_func=so;
193*0Sstevel@tonic-gate 	get_debug_options_func=go;
194*0Sstevel@tonic-gate 	return 1;
195*0Sstevel@tonic-gate 	}
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
199*0Sstevel@tonic-gate 	void (**f)(void *))
200*0Sstevel@tonic-gate 	{
201*0Sstevel@tonic-gate 	if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ?
202*0Sstevel@tonic-gate 	                     malloc_func : 0;
203*0Sstevel@tonic-gate 	if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ?
204*0Sstevel@tonic-gate 	                     realloc_func : 0;
205*0Sstevel@tonic-gate 	if (f != NULL) *f=free_func;
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate void CRYPTO_get_mem_ex_functions(
209*0Sstevel@tonic-gate         void *(**m)(size_t,const char *,int),
210*0Sstevel@tonic-gate         void *(**r)(void *, size_t,const char *,int),
211*0Sstevel@tonic-gate 	void (**f)(void *))
212*0Sstevel@tonic-gate 	{
213*0Sstevel@tonic-gate 	if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ?
214*0Sstevel@tonic-gate 	                    malloc_ex_func : 0;
215*0Sstevel@tonic-gate 	if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ?
216*0Sstevel@tonic-gate 	                    realloc_ex_func : 0;
217*0Sstevel@tonic-gate 	if (f != NULL) *f=free_func;
218*0Sstevel@tonic-gate 	}
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
221*0Sstevel@tonic-gate 	{
222*0Sstevel@tonic-gate 	if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ?
223*0Sstevel@tonic-gate 	                     malloc_locked_func : 0;
224*0Sstevel@tonic-gate 	if (f != NULL) *f=free_locked_func;
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate void CRYPTO_get_locked_mem_ex_functions(
228*0Sstevel@tonic-gate         void *(**m)(size_t,const char *,int),
229*0Sstevel@tonic-gate         void (**f)(void *))
230*0Sstevel@tonic-gate 	{
231*0Sstevel@tonic-gate 	if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ?
232*0Sstevel@tonic-gate 	                    malloc_locked_ex_func : 0;
233*0Sstevel@tonic-gate 	if (f != NULL) *f=free_locked_func;
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
237*0Sstevel@tonic-gate 				    void (**r)(void *,void *,int,const char *,int,int),
238*0Sstevel@tonic-gate 				    void (**f)(void *,int),
239*0Sstevel@tonic-gate 				    void (**so)(long),
240*0Sstevel@tonic-gate 				    long (**go)(void))
241*0Sstevel@tonic-gate 	{
242*0Sstevel@tonic-gate 	if (m != NULL) *m=malloc_debug_func;
243*0Sstevel@tonic-gate 	if (r != NULL) *r=realloc_debug_func;
244*0Sstevel@tonic-gate 	if (f != NULL) *f=free_debug_func;
245*0Sstevel@tonic-gate 	if (so != NULL) *so=set_debug_options_func;
246*0Sstevel@tonic-gate 	if (go != NULL) *go=get_debug_options_func;
247*0Sstevel@tonic-gate 	}
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate void *CRYPTO_malloc_locked(int num, const char *file, int line)
251*0Sstevel@tonic-gate 	{
252*0Sstevel@tonic-gate 	void *ret = NULL;
253*0Sstevel@tonic-gate 	extern unsigned char cleanse_ctr;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	if (num <= 0) return NULL;
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	allow_customize = 0;
258*0Sstevel@tonic-gate 	if (malloc_debug_func != NULL)
259*0Sstevel@tonic-gate 		{
260*0Sstevel@tonic-gate 		allow_customize_debug = 0;
261*0Sstevel@tonic-gate 		malloc_debug_func(NULL, num, file, line, 0);
262*0Sstevel@tonic-gate 		}
263*0Sstevel@tonic-gate 	ret = malloc_locked_ex_func(num,file,line);
264*0Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
265*0Sstevel@tonic-gate 	fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
266*0Sstevel@tonic-gate #endif
267*0Sstevel@tonic-gate 	if (malloc_debug_func != NULL)
268*0Sstevel@tonic-gate 		malloc_debug_func(ret, num, file, line, 1);
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate         /* Create a dependency on the value of 'cleanse_ctr' so our memory
271*0Sstevel@tonic-gate          * sanitisation function can't be optimised out. NB: We only do
272*0Sstevel@tonic-gate          * this for >2Kb so the overhead doesn't bother us. */
273*0Sstevel@tonic-gate         if(ret && (num > 2048))
274*0Sstevel@tonic-gate 		((unsigned char *)ret)[0] = cleanse_ctr;
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	return ret;
277*0Sstevel@tonic-gate 	}
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate void CRYPTO_free_locked(void *str)
280*0Sstevel@tonic-gate 	{
281*0Sstevel@tonic-gate 	if (free_debug_func != NULL)
282*0Sstevel@tonic-gate 		free_debug_func(str, 0);
283*0Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
284*0Sstevel@tonic-gate 	fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
285*0Sstevel@tonic-gate #endif
286*0Sstevel@tonic-gate 	free_locked_func(str);
287*0Sstevel@tonic-gate 	if (free_debug_func != NULL)
288*0Sstevel@tonic-gate 		free_debug_func(NULL, 1);
289*0Sstevel@tonic-gate 	}
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate void *CRYPTO_malloc(int num, const char *file, int line)
292*0Sstevel@tonic-gate 	{
293*0Sstevel@tonic-gate 	void *ret = NULL;
294*0Sstevel@tonic-gate 	extern unsigned char cleanse_ctr;
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	if (num <= 0) return NULL;
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	allow_customize = 0;
299*0Sstevel@tonic-gate 	if (malloc_debug_func != NULL)
300*0Sstevel@tonic-gate 		{
301*0Sstevel@tonic-gate 		allow_customize_debug = 0;
302*0Sstevel@tonic-gate 		malloc_debug_func(NULL, num, file, line, 0);
303*0Sstevel@tonic-gate 		}
304*0Sstevel@tonic-gate 	ret = malloc_ex_func(num,file,line);
305*0Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
306*0Sstevel@tonic-gate 	fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
307*0Sstevel@tonic-gate #endif
308*0Sstevel@tonic-gate 	if (malloc_debug_func != NULL)
309*0Sstevel@tonic-gate 		malloc_debug_func(ret, num, file, line, 1);
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate         /* Create a dependency on the value of 'cleanse_ctr' so our memory
312*0Sstevel@tonic-gate          * sanitisation function can't be optimised out. NB: We only do
313*0Sstevel@tonic-gate          * this for >2Kb so the overhead doesn't bother us. */
314*0Sstevel@tonic-gate         if(ret && (num > 2048))
315*0Sstevel@tonic-gate                 ((unsigned char *)ret)[0] = cleanse_ctr;
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	return ret;
318*0Sstevel@tonic-gate 	}
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate void *CRYPTO_realloc(void *str, int num, const char *file, int line)
321*0Sstevel@tonic-gate 	{
322*0Sstevel@tonic-gate 	void *ret = NULL;
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	if (str == NULL)
325*0Sstevel@tonic-gate 		return CRYPTO_malloc(num, file, line);
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate  	if (num <= 0) return NULL;
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	if (realloc_debug_func != NULL)
330*0Sstevel@tonic-gate 		realloc_debug_func(str, NULL, num, file, line, 0);
331*0Sstevel@tonic-gate 	ret = realloc_ex_func(str,num,file,line);
332*0Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
333*0Sstevel@tonic-gate 	fprintf(stderr, "LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n", str, ret, num);
334*0Sstevel@tonic-gate #endif
335*0Sstevel@tonic-gate 	if (realloc_debug_func != NULL)
336*0Sstevel@tonic-gate 		realloc_debug_func(str, ret, num, file, line, 1);
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	return ret;
339*0Sstevel@tonic-gate 	}
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file,
342*0Sstevel@tonic-gate 			   int line)
343*0Sstevel@tonic-gate 	{
344*0Sstevel@tonic-gate 	void *ret = NULL;
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	if (str == NULL)
347*0Sstevel@tonic-gate 		return CRYPTO_malloc(num, file, line);
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate  	if (num <= 0) return NULL;
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	if (realloc_debug_func != NULL)
352*0Sstevel@tonic-gate 		realloc_debug_func(str, NULL, num, file, line, 0);
353*0Sstevel@tonic-gate 	ret=malloc_ex_func(num,file,line);
354*0Sstevel@tonic-gate 	if(ret)
355*0Sstevel@tonic-gate 		{
356*0Sstevel@tonic-gate 		memcpy(ret,str,old_len);
357*0Sstevel@tonic-gate 		OPENSSL_cleanse(str,old_len);
358*0Sstevel@tonic-gate 		free_func(str);
359*0Sstevel@tonic-gate 		}
360*0Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
361*0Sstevel@tonic-gate 	fprintf(stderr,
362*0Sstevel@tonic-gate 		"LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n",
363*0Sstevel@tonic-gate 		str, ret, num);
364*0Sstevel@tonic-gate #endif
365*0Sstevel@tonic-gate 	if (realloc_debug_func != NULL)
366*0Sstevel@tonic-gate 		realloc_debug_func(str, ret, num, file, line, 1);
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	return ret;
369*0Sstevel@tonic-gate 	}
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate void CRYPTO_free(void *str)
372*0Sstevel@tonic-gate 	{
373*0Sstevel@tonic-gate 	if (free_debug_func != NULL)
374*0Sstevel@tonic-gate 		free_debug_func(str, 0);
375*0Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
376*0Sstevel@tonic-gate 	fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
377*0Sstevel@tonic-gate #endif
378*0Sstevel@tonic-gate 	free_func(str);
379*0Sstevel@tonic-gate 	if (free_debug_func != NULL)
380*0Sstevel@tonic-gate 		free_debug_func(NULL, 1);
381*0Sstevel@tonic-gate 	}
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate void *CRYPTO_remalloc(void *a, int num, const char *file, int line)
384*0Sstevel@tonic-gate 	{
385*0Sstevel@tonic-gate 	if (a != NULL) OPENSSL_free(a);
386*0Sstevel@tonic-gate 	a=(char *)OPENSSL_malloc(num);
387*0Sstevel@tonic-gate 	return(a);
388*0Sstevel@tonic-gate 	}
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate void CRYPTO_set_mem_debug_options(long bits)
391*0Sstevel@tonic-gate 	{
392*0Sstevel@tonic-gate 	if (set_debug_options_func != NULL)
393*0Sstevel@tonic-gate 		set_debug_options_func(bits);
394*0Sstevel@tonic-gate 	}
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate long CRYPTO_get_mem_debug_options(void)
397*0Sstevel@tonic-gate 	{
398*0Sstevel@tonic-gate 	if (get_debug_options_func != NULL)
399*0Sstevel@tonic-gate 		return get_debug_options_func();
400*0Sstevel@tonic-gate 	return 0;
401*0Sstevel@tonic-gate 	}
402