xref: /freebsd/crypto/openssl/doc/man3/OPENSSL_secure_malloc.pod (revision b4e38a41f584ad4391c04b8cfec81f46176b18b0)
1=pod
2
3=head1 NAME
4
5CRYPTO_secure_malloc_init, CRYPTO_secure_malloc_initialized,
6CRYPTO_secure_malloc_done, OPENSSL_secure_malloc, CRYPTO_secure_malloc,
7OPENSSL_secure_zalloc, CRYPTO_secure_zalloc, OPENSSL_secure_free,
8CRYPTO_secure_free, OPENSSL_secure_clear_free,
9CRYPTO_secure_clear_free, OPENSSL_secure_actual_size,
10CRYPTO_secure_allocated,
11CRYPTO_secure_used - secure heap storage
12
13=head1 SYNOPSIS
14
15 #include <openssl/crypto.h>
16
17 int CRYPTO_secure_malloc_init(size_t size, int minsize);
18
19 int CRYPTO_secure_malloc_initialized();
20
21 int CRYPTO_secure_malloc_done();
22
23 void *OPENSSL_secure_malloc(size_t num);
24 void *CRYPTO_secure_malloc(size_t num, const char *file, int line);
25
26 void *OPENSSL_secure_zalloc(size_t num);
27 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line);
28
29 void OPENSSL_secure_free(void* ptr);
30 void CRYPTO_secure_free(void *ptr, const char *, int);
31
32 void OPENSSL_secure_clear_free(void* ptr, size_t num);
33 void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *, int);
34
35 size_t OPENSSL_secure_actual_size(const void *ptr);
36
37 int CRYPTO_secure_allocated(const void *ptr);
38 size_t CRYPTO_secure_used();
39
40=head1 DESCRIPTION
41
42In order to help protect applications (particularly long-running servers)
43from pointer overruns or underruns that could return arbitrary data from
44the program's dynamic memory area, where keys and other sensitive
45information might be stored, OpenSSL supports the concept of a "secure heap."
46The level and type of security guarantees depend on the operating system.
47It is a good idea to review the code and see if it addresses your
48threat model and concerns.
49
50If a secure heap is used, then private key B<BIGNUM> values are stored there.
51This protects long-term storage of private keys, but will not necessarily
52put all intermediate values and computations there.
53
54CRYPTO_secure_malloc_init() creates the secure heap, with the specified
55C<size> in bytes. The C<minsize> parameter is the minimum size to
56allocate from the heap. Both C<size> and C<minsize> must be a power
57of two.
58
59CRYPTO_secure_malloc_initialized() indicates whether or not the secure
60heap as been initialized and is available.
61
62CRYPTO_secure_malloc_done() releases the heap and makes the memory unavailable
63to the process if all secure memory has been freed.
64It can take noticeably long to complete.
65
66OPENSSL_secure_malloc() allocates C<num> bytes from the heap.
67If CRYPTO_secure_malloc_init() is not called, this is equivalent to
68calling OPENSSL_malloc().
69It is a macro that expands to
70CRYPTO_secure_malloc() and adds the C<__FILE__> and C<__LINE__> parameters.
71
72OPENSSL_secure_zalloc() and CRYPTO_secure_zalloc() are like
73OPENSSL_secure_malloc() and CRYPTO_secure_malloc(), respectively,
74except that they call memset() to zero the memory before returning.
75
76OPENSSL_secure_free() releases the memory at C<ptr> back to the heap.
77It must be called with a value previously obtained from
78OPENSSL_secure_malloc().
79If CRYPTO_secure_malloc_init() is not called, this is equivalent to
80calling OPENSSL_free().
81It exists for consistency with OPENSSL_secure_malloc() , and
82is a macro that expands to CRYPTO_secure_free() and adds the C<__FILE__>
83and C<__LINE__> parameters..
84
85OPENSSL_secure_clear_free() is similar to OPENSSL_secure_free() except
86that it has an additional C<num> parameter which is used to clear
87the memory if it was not allocated from the secure heap.
88If CRYPTO_secure_malloc_init() is not called, this is equivalent to
89calling OPENSSL_clear_free().
90
91OPENSSL_secure_actual_size() tells the actual size allocated to the
92pointer; implementations may allocate more space than initially
93requested, in order to "round up" and reduce secure heap fragmentation.
94
95OPENSSL_secure_allocated() tells if a pointer is allocated in the secure heap.
96
97CRYPTO_secure_used() returns the number of bytes allocated in the
98secure heap.
99
100=head1 RETURN VALUES
101
102CRYPTO_secure_malloc_init() returns 0 on failure, 1 if successful,
103and 2 if successful but the heap could not be protected by memory
104mapping.
105
106CRYPTO_secure_malloc_initialized() returns 1 if the secure heap is
107available (that is, if CRYPTO_secure_malloc_init() has been called,
108but CRYPTO_secure_malloc_done() has not been called or failed) or 0 if not.
109
110OPENSSL_secure_malloc() and OPENSSL_secure_zalloc() return a pointer into
111the secure heap of the requested size, or C<NULL> if memory could not be
112allocated.
113
114CRYPTO_secure_allocated() returns 1 if the pointer is in the secure heap, or 0 if not.
115
116CRYPTO_secure_malloc_done() returns 1 if the secure memory area is released, or 0 if not.
117
118OPENSSL_secure_free() and OPENSSL_secure_clear_free() return no values.
119
120=head1 SEE ALSO
121
122L<OPENSSL_malloc(3)>,
123L<BN_new(3)>
124
125=head1 HISTORY
126
127The OPENSSL_secure_clear_free() function was added in OpenSSL 1.1.0g.
128
129=head1 COPYRIGHT
130
131Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
132
133Licensed under the OpenSSL license (the "License").  You may not use
134this file except in compliance with the License.  You can obtain a copy
135in the file LICENSE in the source distribution or at
136L<https://www.openssl.org/source/license.html>.
137
138=cut
139