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