Move Makefiles to Makefile.in
[openssl.git] / crypto / mem.c
1 /* crypto/mem.c */
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 #include <stdio.h>
60 #include <stdlib.h>
61 #include <limits.h>
62 #include <openssl/crypto.h>
63 #include "internal/cryptlib.h"
64
65 /*
66  * the following pointers may be changed as long as 'allow_customize' is set
67  */
68 static int allow_customize = 1;
69
70 static void *(*malloc_wrapper)(size_t, const char *, int)
71     = CRYPTO_malloc;
72 static void *(*realloc_wrapper)(void *, size_t, const char *, int)
73     = CRYPTO_realloc;
74 static void (*free_wrapper)(void *)
75     = CRYPTO_free;
76
77 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
78 static int call_malloc_debug = 1;
79 #else
80 static int call_malloc_debug = 0;
81 #endif
82
83 int CRYPTO_set_mem_functions(
84         void *(*m)(size_t, const char *, int),
85         void *(*r)(void *, size_t, const char *, int),
86         void (*f)(void *))
87 {
88     if (!allow_customize)
89         return 0;
90     if (m)
91         malloc_wrapper = m;
92     if (r)
93         realloc_wrapper = r;
94     if (f)
95         free_wrapper = f;
96     return 1;
97 }
98
99 int CRYPTO_set_mem_debug(int flag)
100 {
101     if (!allow_customize)
102         return 0;
103     call_malloc_debug = flag;
104     return 1;
105 }
106
107 void CRYPTO_get_mem_functions(
108         void *(**m)(size_t, const char *, int),
109         void *(**r)(void *, size_t, const char *, int),
110         void (**f)(void *))
111 {
112     if (m != NULL)
113         *m = malloc_wrapper;
114     if (r != NULL)
115         *r = realloc_wrapper;
116     if (f != NULL)
117         *f = free_wrapper;
118 }
119
120 void *CRYPTO_malloc(size_t num, const char *file, int line)
121 {
122     void *ret = NULL;
123
124     if (num <= 0)
125         return NULL;
126
127     allow_customize = 0;
128 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
129     if (call_malloc_debug) {
130         CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
131         ret = malloc(num);
132         CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
133     } else {
134         ret = malloc(num);
135     }
136 #else
137     (void)file;
138     (void)line;
139     ret = malloc(num);
140 #endif
141
142 #ifndef OPENSSL_CPUID_OBJ
143     /*
144      * Create a dependency on the value of 'cleanse_ctr' so our memory
145      * sanitisation function can't be optimised out. NB: We only do this for
146      * >2Kb so the overhead doesn't bother us.
147      */
148     if (ret && (num > 2048)) {
149         extern unsigned char cleanse_ctr;
150         ((unsigned char *)ret)[0] = cleanse_ctr;
151     }
152 #endif
153
154     return ret;
155 }
156
157 void *CRYPTO_zalloc(size_t num, const char *file, int line)
158 {
159     void *ret = CRYPTO_malloc(num, file, line);
160
161     if (ret != NULL)
162         memset(ret, 0, num);
163     return ret;
164 }
165
166 void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
167 {
168     if (str == NULL)
169         return CRYPTO_malloc(num, file, line);
170
171     if (num == 0) {
172         CRYPTO_free(str);
173         return NULL;
174     }
175
176     allow_customize = 0;
177 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
178     if (call_malloc_debug) {
179         void *ret;
180         CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
181         ret = realloc(str, num);
182         CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
183         return ret;
184     }
185 #else
186     (void)file;
187     (void)line;
188 #endif
189     return realloc(str, num);
190
191 }
192
193 void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
194                            const char *file, int line)
195 {
196     void *ret = NULL;
197
198     if (str == NULL)
199         return CRYPTO_malloc(num, file, line);
200
201     if (num == 0) {
202         CRYPTO_clear_free(str, old_len);
203         return NULL;
204     }
205
206     /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
207     if (num < old_len) {
208         memset((char*)str + num, 0, old_len - num);
209         return str;
210     }
211
212     /* Allocate new memory.  Call malloc and do a copy, so that we can
213      * cleanse the old buffer. */
214 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
215     if (call_malloc_debug) {
216         CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
217         ret = malloc(num);
218         CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
219     } else {
220         ret = malloc(num);
221     }
222 #else
223     (void)file;
224     (void)line;
225     ret = malloc(num);
226 #endif
227
228     if (ret)
229         memcpy(ret, str, old_len);
230     CRYPTO_clear_free(str, old_len);
231     return ret;
232 }
233
234 void CRYPTO_free(void *str)
235 {
236 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
237     if (call_malloc_debug) {
238         CRYPTO_mem_debug_free(str, 0);
239         free(str);
240         CRYPTO_mem_debug_free(str, 1);
241     } else {
242         free(str);
243     }
244 #else
245     free(str);
246 #endif
247 }
248
249 void CRYPTO_clear_free(void *str, size_t num)
250 {
251     if (str == NULL)
252         return;
253     if (num)
254         OPENSSL_cleanse(str, num);
255     CRYPTO_free(str);
256 }