xref: /onnv-gate/usr/src/common/openssl/crypto/mem.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/mem.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <stdlib.h>
610Sstevel@tonic-gate #include <openssl/crypto.h>
620Sstevel@tonic-gate #include "cryptlib.h"
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static int allow_customize = 1;      /* we provide flexible functions for */
660Sstevel@tonic-gate static int allow_customize_debug = 1;/* exchanging memory-related functions at
670Sstevel@tonic-gate                                       * run-time, but this must be done
680Sstevel@tonic-gate                                       * before any blocks are actually
690Sstevel@tonic-gate                                       * allocated; or we'll run into huge
700Sstevel@tonic-gate                                       * problems when malloc/free pairs
710Sstevel@tonic-gate                                       * don't match etc. */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /* the following pointers may be changed as long as 'allow_customize' is set */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static void *(*malloc_func)(size_t)         = malloc;
default_malloc_ex(size_t num,const char * file,int line)780Sstevel@tonic-gate static void *default_malloc_ex(size_t num, const char *file, int line)
790Sstevel@tonic-gate 	{ return malloc_func(num); }
800Sstevel@tonic-gate static void *(*malloc_ex_func)(size_t, const char *file, int line)
810Sstevel@tonic-gate         = default_malloc_ex;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static void *(*realloc_func)(void *, size_t)= realloc;
default_realloc_ex(void * str,size_t num,const char * file,int line)840Sstevel@tonic-gate static void *default_realloc_ex(void *str, size_t num,
850Sstevel@tonic-gate         const char *file, int line)
860Sstevel@tonic-gate 	{ return realloc_func(str,num); }
870Sstevel@tonic-gate static void *(*realloc_ex_func)(void *, size_t, const char *file, int line)
880Sstevel@tonic-gate         = default_realloc_ex;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static void (*free_func)(void *)            = free;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static void *(*malloc_locked_func)(size_t)  = malloc;
default_malloc_locked_ex(size_t num,const char * file,int line)930Sstevel@tonic-gate static void *default_malloc_locked_ex(size_t num, const char *file, int line)
940Sstevel@tonic-gate 	{ return malloc_locked_func(num); }
950Sstevel@tonic-gate static void *(*malloc_locked_ex_func)(size_t, const char *file, int line)
960Sstevel@tonic-gate         = default_malloc_locked_ex;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate static void (*free_locked_func)(void *)     = free;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /* may be changed as long as 'allow_customize_debug' is set */
1030Sstevel@tonic-gate /* XXX use correct function pointer types */
1040Sstevel@tonic-gate #ifdef CRYPTO_MDEBUG
1050Sstevel@tonic-gate /* use default functions from mem_dbg.c */
1060Sstevel@tonic-gate static void (*malloc_debug_func)(void *,int,const char *,int,int)
1070Sstevel@tonic-gate 	= CRYPTO_dbg_malloc;
1080Sstevel@tonic-gate static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
1090Sstevel@tonic-gate 	= CRYPTO_dbg_realloc;
1100Sstevel@tonic-gate static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free;
1110Sstevel@tonic-gate static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options;
1120Sstevel@tonic-gate static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options;
1130Sstevel@tonic-gate #else
1140Sstevel@tonic-gate /* applications can use CRYPTO_malloc_debug_init() to select above case
1150Sstevel@tonic-gate  * at run-time */
1160Sstevel@tonic-gate static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL;
1170Sstevel@tonic-gate static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
1180Sstevel@tonic-gate 	= NULL;
1190Sstevel@tonic-gate static void (*free_debug_func)(void *,int) = NULL;
1200Sstevel@tonic-gate static void (*set_debug_options_func)(long) = NULL;
1210Sstevel@tonic-gate static long (*get_debug_options_func)(void) = NULL;
1220Sstevel@tonic-gate #endif
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 
CRYPTO_set_mem_functions(void * (* m)(size_t),void * (* r)(void *,size_t),void (* f)(void *))1250Sstevel@tonic-gate int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
1260Sstevel@tonic-gate 	void (*f)(void *))
1270Sstevel@tonic-gate 	{
1280Sstevel@tonic-gate 	if (!allow_customize)
1290Sstevel@tonic-gate 		return 0;
1300Sstevel@tonic-gate 	if ((m == 0) || (r == 0) || (f == 0))
1310Sstevel@tonic-gate 		return 0;
1320Sstevel@tonic-gate 	malloc_func=m; malloc_ex_func=default_malloc_ex;
1330Sstevel@tonic-gate 	realloc_func=r; realloc_ex_func=default_realloc_ex;
1340Sstevel@tonic-gate 	free_func=f;
1350Sstevel@tonic-gate 	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
1360Sstevel@tonic-gate 	free_locked_func=f;
1370Sstevel@tonic-gate 	return 1;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
CRYPTO_set_mem_ex_functions(void * (* m)(size_t,const char *,int),void * (* r)(void *,size_t,const char *,int),void (* f)(void *))1400Sstevel@tonic-gate int CRYPTO_set_mem_ex_functions(
1410Sstevel@tonic-gate         void *(*m)(size_t,const char *,int),
1420Sstevel@tonic-gate         void *(*r)(void *, size_t,const char *,int),
1430Sstevel@tonic-gate 	void (*f)(void *))
1440Sstevel@tonic-gate 	{
1450Sstevel@tonic-gate 	if (!allow_customize)
1460Sstevel@tonic-gate 		return 0;
1470Sstevel@tonic-gate 	if ((m == 0) || (r == 0) || (f == 0))
1480Sstevel@tonic-gate 		return 0;
1490Sstevel@tonic-gate 	malloc_func=0; malloc_ex_func=m;
1500Sstevel@tonic-gate 	realloc_func=0; realloc_ex_func=r;
1510Sstevel@tonic-gate 	free_func=f;
1520Sstevel@tonic-gate 	malloc_locked_func=0; malloc_locked_ex_func=m;
1530Sstevel@tonic-gate 	free_locked_func=f;
1540Sstevel@tonic-gate 	return 1;
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
CRYPTO_set_locked_mem_functions(void * (* m)(size_t),void (* f)(void *))1570Sstevel@tonic-gate int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
1580Sstevel@tonic-gate 	{
1590Sstevel@tonic-gate 	if (!allow_customize)
1600Sstevel@tonic-gate 		return 0;
1610Sstevel@tonic-gate 	if ((m == NULL) || (f == NULL))
1620Sstevel@tonic-gate 		return 0;
1630Sstevel@tonic-gate 	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
1640Sstevel@tonic-gate 	free_locked_func=f;
1650Sstevel@tonic-gate 	return 1;
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
CRYPTO_set_locked_mem_ex_functions(void * (* m)(size_t,const char *,int),void (* f)(void *))1680Sstevel@tonic-gate int CRYPTO_set_locked_mem_ex_functions(
1690Sstevel@tonic-gate         void *(*m)(size_t,const char *,int),
1700Sstevel@tonic-gate         void (*f)(void *))
1710Sstevel@tonic-gate 	{
1720Sstevel@tonic-gate 	if (!allow_customize)
1730Sstevel@tonic-gate 		return 0;
1740Sstevel@tonic-gate 	if ((m == NULL) || (f == NULL))
1750Sstevel@tonic-gate 		return 0;
1760Sstevel@tonic-gate 	malloc_locked_func=0; malloc_locked_ex_func=m;
1770Sstevel@tonic-gate 	free_func=f;
1780Sstevel@tonic-gate 	return 1;
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
CRYPTO_set_mem_debug_functions(void (* m)(void *,int,const char *,int,int),void (* r)(void *,void *,int,const char *,int,int),void (* f)(void *,int),void (* so)(long),long (* go)(void))1810Sstevel@tonic-gate int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
1820Sstevel@tonic-gate 				   void (*r)(void *,void *,int,const char *,int,int),
1830Sstevel@tonic-gate 				   void (*f)(void *,int),
1840Sstevel@tonic-gate 				   void (*so)(long),
1850Sstevel@tonic-gate 				   long (*go)(void))
1860Sstevel@tonic-gate 	{
1870Sstevel@tonic-gate 	if (!allow_customize_debug)
1880Sstevel@tonic-gate 		return 0;
1890Sstevel@tonic-gate 	malloc_debug_func=m;
1900Sstevel@tonic-gate 	realloc_debug_func=r;
1910Sstevel@tonic-gate 	free_debug_func=f;
1920Sstevel@tonic-gate 	set_debug_options_func=so;
1930Sstevel@tonic-gate 	get_debug_options_func=go;
1940Sstevel@tonic-gate 	return 1;
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 
CRYPTO_get_mem_functions(void * (** m)(size_t),void * (** r)(void *,size_t),void (** f)(void *))1980Sstevel@tonic-gate void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
1990Sstevel@tonic-gate 	void (**f)(void *))
2000Sstevel@tonic-gate 	{
2010Sstevel@tonic-gate 	if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ?
2020Sstevel@tonic-gate 	                     malloc_func : 0;
2030Sstevel@tonic-gate 	if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ?
2040Sstevel@tonic-gate 	                     realloc_func : 0;
2050Sstevel@tonic-gate 	if (f != NULL) *f=free_func;
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
CRYPTO_get_mem_ex_functions(void * (** m)(size_t,const char *,int),void * (** r)(void *,size_t,const char *,int),void (** f)(void *))2080Sstevel@tonic-gate void CRYPTO_get_mem_ex_functions(
2090Sstevel@tonic-gate         void *(**m)(size_t,const char *,int),
2100Sstevel@tonic-gate         void *(**r)(void *, size_t,const char *,int),
2110Sstevel@tonic-gate 	void (**f)(void *))
2120Sstevel@tonic-gate 	{
2130Sstevel@tonic-gate 	if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ?
2140Sstevel@tonic-gate 	                    malloc_ex_func : 0;
2150Sstevel@tonic-gate 	if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ?
2160Sstevel@tonic-gate 	                    realloc_ex_func : 0;
2170Sstevel@tonic-gate 	if (f != NULL) *f=free_func;
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
CRYPTO_get_locked_mem_functions(void * (** m)(size_t),void (** f)(void *))2200Sstevel@tonic-gate void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
2210Sstevel@tonic-gate 	{
2220Sstevel@tonic-gate 	if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ?
2230Sstevel@tonic-gate 	                     malloc_locked_func : 0;
2240Sstevel@tonic-gate 	if (f != NULL) *f=free_locked_func;
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
CRYPTO_get_locked_mem_ex_functions(void * (** m)(size_t,const char *,int),void (** f)(void *))2270Sstevel@tonic-gate void CRYPTO_get_locked_mem_ex_functions(
2280Sstevel@tonic-gate         void *(**m)(size_t,const char *,int),
2290Sstevel@tonic-gate         void (**f)(void *))
2300Sstevel@tonic-gate 	{
2310Sstevel@tonic-gate 	if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ?
2320Sstevel@tonic-gate 	                    malloc_locked_ex_func : 0;
2330Sstevel@tonic-gate 	if (f != NULL) *f=free_locked_func;
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
CRYPTO_get_mem_debug_functions(void (** m)(void *,int,const char *,int,int),void (** r)(void *,void *,int,const char *,int,int),void (** f)(void *,int),void (** so)(long),long (** go)(void))2360Sstevel@tonic-gate void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
2370Sstevel@tonic-gate 				    void (**r)(void *,void *,int,const char *,int,int),
2380Sstevel@tonic-gate 				    void (**f)(void *,int),
2390Sstevel@tonic-gate 				    void (**so)(long),
2400Sstevel@tonic-gate 				    long (**go)(void))
2410Sstevel@tonic-gate 	{
2420Sstevel@tonic-gate 	if (m != NULL) *m=malloc_debug_func;
2430Sstevel@tonic-gate 	if (r != NULL) *r=realloc_debug_func;
2440Sstevel@tonic-gate 	if (f != NULL) *f=free_debug_func;
2450Sstevel@tonic-gate 	if (so != NULL) *so=set_debug_options_func;
2460Sstevel@tonic-gate 	if (go != NULL) *go=get_debug_options_func;
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 
CRYPTO_malloc_locked(int num,const char * file,int line)2500Sstevel@tonic-gate void *CRYPTO_malloc_locked(int num, const char *file, int line)
2510Sstevel@tonic-gate 	{
2520Sstevel@tonic-gate 	void *ret = NULL;
2530Sstevel@tonic-gate 	extern unsigned char cleanse_ctr;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if (num <= 0) return NULL;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	allow_customize = 0;
2580Sstevel@tonic-gate 	if (malloc_debug_func != NULL)
2590Sstevel@tonic-gate 		{
2600Sstevel@tonic-gate 		allow_customize_debug = 0;
2610Sstevel@tonic-gate 		malloc_debug_func(NULL, num, file, line, 0);
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 	ret = malloc_locked_ex_func(num,file,line);
2640Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
2650Sstevel@tonic-gate 	fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
2660Sstevel@tonic-gate #endif
2670Sstevel@tonic-gate 	if (malloc_debug_func != NULL)
2680Sstevel@tonic-gate 		malloc_debug_func(ret, num, file, line, 1);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate         /* Create a dependency on the value of 'cleanse_ctr' so our memory
2710Sstevel@tonic-gate          * sanitisation function can't be optimised out. NB: We only do
2720Sstevel@tonic-gate          * this for >2Kb so the overhead doesn't bother us. */
2730Sstevel@tonic-gate         if(ret && (num > 2048))
2740Sstevel@tonic-gate 		((unsigned char *)ret)[0] = cleanse_ctr;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	return ret;
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
CRYPTO_free_locked(void * str)2790Sstevel@tonic-gate void CRYPTO_free_locked(void *str)
2800Sstevel@tonic-gate 	{
2810Sstevel@tonic-gate 	if (free_debug_func != NULL)
2820Sstevel@tonic-gate 		free_debug_func(str, 0);
2830Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
2840Sstevel@tonic-gate 	fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
2850Sstevel@tonic-gate #endif
2860Sstevel@tonic-gate 	free_locked_func(str);
2870Sstevel@tonic-gate 	if (free_debug_func != NULL)
2880Sstevel@tonic-gate 		free_debug_func(NULL, 1);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
CRYPTO_malloc(int num,const char * file,int line)2910Sstevel@tonic-gate void *CRYPTO_malloc(int num, const char *file, int line)
2920Sstevel@tonic-gate 	{
2930Sstevel@tonic-gate 	void *ret = NULL;
2940Sstevel@tonic-gate 	extern unsigned char cleanse_ctr;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if (num <= 0) return NULL;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	allow_customize = 0;
2990Sstevel@tonic-gate 	if (malloc_debug_func != NULL)
3000Sstevel@tonic-gate 		{
3010Sstevel@tonic-gate 		allow_customize_debug = 0;
3020Sstevel@tonic-gate 		malloc_debug_func(NULL, num, file, line, 0);
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 	ret = malloc_ex_func(num,file,line);
3050Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
3060Sstevel@tonic-gate 	fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
3070Sstevel@tonic-gate #endif
3080Sstevel@tonic-gate 	if (malloc_debug_func != NULL)
3090Sstevel@tonic-gate 		malloc_debug_func(ret, num, file, line, 1);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate         /* Create a dependency on the value of 'cleanse_ctr' so our memory
3120Sstevel@tonic-gate          * sanitisation function can't be optimised out. NB: We only do
3130Sstevel@tonic-gate          * this for >2Kb so the overhead doesn't bother us. */
3140Sstevel@tonic-gate         if(ret && (num > 2048))
3150Sstevel@tonic-gate                 ((unsigned char *)ret)[0] = cleanse_ctr;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	return ret;
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
CRYPTO_realloc(void * str,int num,const char * file,int line)3200Sstevel@tonic-gate void *CRYPTO_realloc(void *str, int num, const char *file, int line)
3210Sstevel@tonic-gate 	{
3220Sstevel@tonic-gate 	void *ret = NULL;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (str == NULL)
3250Sstevel@tonic-gate 		return CRYPTO_malloc(num, file, line);
3260Sstevel@tonic-gate 
327*2139Sjp161948 	if (num <= 0) return NULL;
328*2139Sjp161948 
3290Sstevel@tonic-gate 	if (realloc_debug_func != NULL)
3300Sstevel@tonic-gate 		realloc_debug_func(str, NULL, num, file, line, 0);
3310Sstevel@tonic-gate 	ret = realloc_ex_func(str,num,file,line);
3320Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
3330Sstevel@tonic-gate 	fprintf(stderr, "LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n", str, ret, num);
3340Sstevel@tonic-gate #endif
3350Sstevel@tonic-gate 	if (realloc_debug_func != NULL)
3360Sstevel@tonic-gate 		realloc_debug_func(str, ret, num, file, line, 1);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	return ret;
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
CRYPTO_realloc_clean(void * str,int old_len,int num,const char * file,int line)3410Sstevel@tonic-gate void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file,
3420Sstevel@tonic-gate 			   int line)
3430Sstevel@tonic-gate 	{
3440Sstevel@tonic-gate 	void *ret = NULL;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (str == NULL)
3470Sstevel@tonic-gate 		return CRYPTO_malloc(num, file, line);
348*2139Sjp161948 
349*2139Sjp161948 	if (num <= 0) return NULL;
350*2139Sjp161948 
3510Sstevel@tonic-gate 	if (realloc_debug_func != NULL)
3520Sstevel@tonic-gate 		realloc_debug_func(str, NULL, num, file, line, 0);
3530Sstevel@tonic-gate 	ret=malloc_ex_func(num,file,line);
3540Sstevel@tonic-gate 	if(ret)
3550Sstevel@tonic-gate 		{
3560Sstevel@tonic-gate 		memcpy(ret,str,old_len);
3570Sstevel@tonic-gate 		OPENSSL_cleanse(str,old_len);
3580Sstevel@tonic-gate 		free_func(str);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
3610Sstevel@tonic-gate 	fprintf(stderr,
3620Sstevel@tonic-gate 		"LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n",
3630Sstevel@tonic-gate 		str, ret, num);
3640Sstevel@tonic-gate #endif
3650Sstevel@tonic-gate 	if (realloc_debug_func != NULL)
3660Sstevel@tonic-gate 		realloc_debug_func(str, ret, num, file, line, 1);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	return ret;
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
CRYPTO_free(void * str)3710Sstevel@tonic-gate void CRYPTO_free(void *str)
3720Sstevel@tonic-gate 	{
3730Sstevel@tonic-gate 	if (free_debug_func != NULL)
3740Sstevel@tonic-gate 		free_debug_func(str, 0);
3750Sstevel@tonic-gate #ifdef LEVITTE_DEBUG_MEM
3760Sstevel@tonic-gate 	fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
3770Sstevel@tonic-gate #endif
3780Sstevel@tonic-gate 	free_func(str);
3790Sstevel@tonic-gate 	if (free_debug_func != NULL)
3800Sstevel@tonic-gate 		free_debug_func(NULL, 1);
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 
CRYPTO_remalloc(void * a,int num,const char * file,int line)3830Sstevel@tonic-gate void *CRYPTO_remalloc(void *a, int num, const char *file, int line)
3840Sstevel@tonic-gate 	{
3850Sstevel@tonic-gate 	if (a != NULL) OPENSSL_free(a);
3860Sstevel@tonic-gate 	a=(char *)OPENSSL_malloc(num);
3870Sstevel@tonic-gate 	return(a);
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
CRYPTO_set_mem_debug_options(long bits)3900Sstevel@tonic-gate void CRYPTO_set_mem_debug_options(long bits)
3910Sstevel@tonic-gate 	{
3920Sstevel@tonic-gate 	if (set_debug_options_func != NULL)
3930Sstevel@tonic-gate 		set_debug_options_func(bits);
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
CRYPTO_get_mem_debug_options(void)3960Sstevel@tonic-gate long CRYPTO_get_mem_debug_options(void)
3970Sstevel@tonic-gate 	{
3980Sstevel@tonic-gate 	if (get_debug_options_func != NULL)
3990Sstevel@tonic-gate 		return get_debug_options_func();
4000Sstevel@tonic-gate 	return 0;
4010Sstevel@tonic-gate 	}
402