cd89897b60e29b93a11b1eb144852410ec533647
[openssl.git] / doc / crypto / OPENSSL_malloc.pod
1 =pod
2
3 =head1 NAME
4
5 CRYPTO_MEM_CHECK_OFF, CRYPTO_MEM_CHECK_ON,
6 CRYPTO_MEM_CHECK_DISABLE, CRYPTO_MEM_CHECK_ENABLE,
7 OPENSSL_malloc_init,
8 OPENSSL_malloc, OPENSSL_zalloc, OPENSSL_realloc, OPENSSL_free,
9 OPENSSL_clear_realloc, OPENSSL_clear_free, OPENSSL_cleanse,
10 CRYPTO_malloc, CRYPTO_zalloc, CRYPTO_realloc, CRYPTO_free,
11 OPENSSL_strdup, OPENSSL_strndup,
12 OPENSSL_memdup, OPENSSL_strlcpy, OPENSSL_strlcat,
13 OPENSSL_hexstr2buf, OPENSSL_buf2hexstr, OPENSSL_hexchar2int,
14 CRYPTO_strdup, CRYPTO_strndup,
15 OPENSSL_mem_debug_push, OPENSSL_mem_debug_pop,
16 CRYPTO_mem_debug_push, CRYPTO_mem_debug_pop,
17 CRYPTO_clear_realloc, CRYPTO_clear_free,
18 CRYPTO_get_mem_functions, CRYPTO_set_mem_functions,
19 CRYPTO_set_mem_debug, CRYPTO_mem_ctrl,
20 CRYPTO_mem_leaks, CRYPTO_mem_leaks_fp - Memory allocation functions
21
22 =head1 SYNOPSIS
23
24  #include <openssl/crypto.h>
25
26  int OPENSSL_malloc_init(void)
27
28  void *OPENSSL_malloc(size_t num)
29  void *OPENSSL_zalloc(size_t num)
30  void *OPENSSL_realloc(void *addr, size_t num)
31  void OPENSSL_free(void *addr)
32  char *OPENSSL_strdup(const char *str)
33  char *OPENSSL_strndup(const char *str, size_t s)
34  size_t OPENSSL_strlcat(char *dst, const char *src, size_t size);
35  size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size);
36  void *OPENSSL_memdup(void *data, size_t s)
37  void *OPENSSL_clear_realloc(void *p, size_t old_len, size_t num)
38  void OPENSSL_clear_free(void *str, size_t num)
39  void OPENSSL_cleanse(void *ptr, size_t len);
40
41  unsigned char *OPENSSL_hexstr2buf(const char *str, long *len);
42  char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len);
43  int OPENSSL_hexchar2int(unsigned char c);
44
45  void *CRYPTO_malloc(size_t num, const char *file, int line)
46  void *CRYPTO_zalloc(size_t num, const char *file, int line)
47  void *CRYPTO_realloc(void *p, size_t num, const char *file, int line)
48  void CRYPTO_free(void *str, const char *, int)
49  char *CRYPTO_strdup(const char *p, const char *file, int line)
50  char *CRYPTO_strndup(const char *p, size_t num, const char *file, int line)
51  void *CRYPTO_clear_realloc(void *p, size_t old_len, size_t num, const char *file, int line)
52  void CRYPTO_clear_free(void *str, size_t num, const char *, int)
53
54  void CRYPTO_get_mem_functions(
55          void *(**m)(size_t, const char *, int),
56          void *(**r)(void *, size_t, const char *, int),
57          void (**f)(void *, const char *, int))
58  int CRYPTO_set_mem_functions(
59          void *(*m)(size_t, const char *, int),
60          void *(*r)(void *, size_t, const char *, int),
61          void (*f)(void *, const char *, int))
62
63  int CRYPTO_set_mem_debug(int onoff)
64
65  #define CRYPTO_MEM_CHECK_OFF
66  #define CRYPTO_MEM_CHECK_ON
67  #define CRYPTO_MEM_CHECK_DISABLE
68  #define CRYPTO_MEM_CHECK_ENABLE
69
70  int CRYPTO_mem_ctrl(int mode);
71
72  int OPENSSL_mem_debug_push(const char *info)
73  int OPENSSL_mem_debug_pop(void);
74
75  int CRYPTO_mem_debug_push(const char *info, const char *file, int line);
76  int CRYPTO_mem_debug_pop(void);
77
78  void CRYPTO_mem_leaks(BIO *b);
79  void CRYPTO_mem_leaks_fp(FILE *fp);
80
81 =head1 DESCRIPTION
82
83 OpenSSL memory allocation is handled by the B<OPENSSL_xxx> API. These are
84 generally macro's that add the standard C B<__FILE__> and B<__LINE__>
85 parameters and call a lower-level B<CRYPTO_xxx> API.
86 Some functions do not add those parameters, but exist for consistency.
87
88 OPENSSL_malloc_init() sets the lower-level memory allocation functions
89 to their default implementation.
90 It is generally not necessary to call this, except perhaps in certain
91 shared-library situations.
92
93 OPENSSL_malloc(), OPENSSL_realloc(), and OPENSSL_free() are like the
94 C malloc(), realloc(), and free() functions.
95 OPENSSL_zalloc() calls memset() to zero the memory before returning.
96
97 OPENSSL_clear_realloc() and OPENSSL_clear_free() should be used
98 when the buffer at B<addr> holds sensitive information.
99 The old buffer is filled with zero's by calling OPENSSL_cleanse()
100 before ultimately calling OPENSSL_free().
101
102 OPENSSL_cleanse() fills B<ptr> of size B<len> with a string of 0's.
103 Use OPENSSL_cleanse() with care if the memory is a mapping of a file.
104 If the storage controller uses write compression, then its possible
105 that sensitive tail bytes will survive zeroization because the block of
106 zeros will be compressed. If the storage controller uses wear levelling,
107 then the old sensitive data will not be overwritten; rather, a block of
108 0's will be written at a new physical location.
109
110 OPENSSL_strdup(), OPENSSL_strndup() and OPENSSL_memdup() are like the
111 equivalent C functions, except that memory is allocated by calling the
112 OPENSSL_malloc() and should be released by calling OPENSSL_free().
113
114 OPENSSL_strlcpy(),
115 OPENSSL_strlcat() and OPENSSL_strnlen() are equivalents of the common C
116 library functions and are provided for portability.
117
118 OPENSSL_hexstr2buf() parses B<str> as a hex string and returns a
119 pointer to the parsed value. The memory is allocated by calling
120 OPENSSL_malloc() and should be released by calling OPENSSL_free().
121 If B<len> is not NULL, it is filled in with the output length.
122 Colons between two-character hex "bytes" are ignored.
123 An odd number of hex digits is an error.
124
125 OPENSSL_buf2hexstr() takes the specified buffer and length, and returns
126 a hex string for value, or NULL on error.
127 B<Buffer> cannot be NULL; if B<len> is 0 an empty string is returned.
128
129 OPENSSL_hexchar2int() converts a character to the hexadecimal equivalent,
130 or returns -1 on error.
131
132 If no allocations have been done, it is possible to "swap out" the default
133 implementations for OPENSSL_malloc(), OPENSSL_realloc and OPENSSL_free()
134 and replace them with alternate versions (hooks).
135 CRYPTO_get_mem_functions() function fills in the given arguments with the
136 function pointers for the current implementations.
137 With CRYPTO_set_mem_functions(), you can specify a different set of functions.
138 If any of B<m>, B<r>, or B<f> are NULL, then the function is not changed.
139
140 The default implementation can include some debugging capability (if enabled
141 at build-time).
142 This adds some overhead by keeping a list of all memory allocations, and
143 removes items from the list when they are free'd.
144 This is most useful for identifying memory leaks.
145 CRYPTO_set_mem_debug() turns this tracking on and off.  It is normally
146 called at startup, but can be called at any time.
147
148 CRYPTO_mem_ctrl() provides fine-grained control of memory leak tracking.
149 To enable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
150 the B<CRYPTO_MEM_CHECK_ON>.
151 To disable tracking call CRYPTO_mem_ctrl() with a B<mode> argument of
152 the B<CRYPTO_MEM_CHECK_OFF>.
153 The B<CRYPTO_MEM_CHECK_DISABLE> and B<CRYPTO_MEM_CHECK_ENABLE> modes
154 are used internally within OpenSSL to temporarily suspend and resume
155 tracking.
156
157 While checking memory, it can be useful to store additional context
158 about what is being done.
159 For example, identifying the field names when parsing a complicated
160 data structure.
161 OPENSSL_mem_debug_push() (which calls CRYPTO_mem_debug_push())
162 attachs an identifying string to the allocation stack.
163 This must be a global or other static string; it is not copied.
164 OPENSSL_mem_debug_pop() removes identifying state from the stack.
165
166 At the end of the program, calling CRYPTO_mem_leaks() or
167 CRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it
168 to the specified BIO B<b> or FILE B<fp>. These functions return 1 if
169 there are no leaks, 0 if there are leaks and -1 if an error occurred.
170
171 =head1 RETURN VALUES
172
173 OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
174 CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions()
175 return no value.
176
177 CRYPTO_mem_leaks() and CRYPTO_mem_leaks_fp() return 1 if there
178 are no leaks, 0 if there are leaks and -1 if an error occurred.
179
180 OPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
181 OPENSSL_clear_realloc(),
182 CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),
183 CRYPTO_clear_realloc(),
184 OPENSSL_buf2hexstr(), OPENSSL_hexstr2buf(),
185 OPENSSL_strdup(), and OPENSSL_strndup()
186 return a pointer to allocated memory or NULL on error.
187
188 CRYPTO_set_mem_functions() and CRYPTO_set_mem_debug()
189 return 1 on success or 0 on failure (almost
190 always because allocations have already happened).
191
192 CRYPTO_mem_ctrl() returns the previous value of the mode.
193
194 OPENSSL_mem_debug_push() and OPENSSL_mem_debug_pop()
195 return 1 on success or 0 on failure.
196
197 =head1 NOTES
198
199 While it's permitted to swap out only a few and not all the functions
200 with CRYPTO_set_mem_functions(), it's recommended to swap them all out
201 at once.  I<This applies specially if OpenSSL was built with the
202 configuration option> C<crypto-mdebug> I<enabled.  In case, swapping out
203 only, say, the malloc() implementation is outright dangerous.>
204
205 =head1 COPYRIGHT
206
207 Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
208
209 Licensed under the OpenSSL license (the "License").  You may not use
210 this file except in compliance with the License.  You can obtain a copy
211 in the file LICENSE in the source distribution or at
212 L<https://www.openssl.org/source/license.html>.
213
214 =cut