xref: /freebsd/crypto/openssl/doc/man3/OPENSSL_secure_malloc.pod (revision a7148ab39c03abd4d1a84997c70bf96f15dd2a09)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk KimCRYPTO_secure_malloc_init, CRYPTO_secure_malloc_initialized,
6e71b7053SJung-uk KimCRYPTO_secure_malloc_done, OPENSSL_secure_malloc, CRYPTO_secure_malloc,
7e71b7053SJung-uk KimOPENSSL_secure_zalloc, CRYPTO_secure_zalloc, OPENSSL_secure_free,
8e71b7053SJung-uk KimCRYPTO_secure_free, OPENSSL_secure_clear_free,
9e71b7053SJung-uk KimCRYPTO_secure_clear_free, OPENSSL_secure_actual_size,
1017f01e99SJung-uk KimCRYPTO_secure_allocated,
11e71b7053SJung-uk KimCRYPTO_secure_used - secure heap storage
12e71b7053SJung-uk Kim
13e71b7053SJung-uk Kim=head1 SYNOPSIS
14e71b7053SJung-uk Kim
15e71b7053SJung-uk Kim #include <openssl/crypto.h>
16e71b7053SJung-uk Kim
17b077aed3SPierre Pronchery int CRYPTO_secure_malloc_init(size_t size, size_t minsize);
18e71b7053SJung-uk Kim
19e71b7053SJung-uk Kim int CRYPTO_secure_malloc_initialized();
20e71b7053SJung-uk Kim
21e71b7053SJung-uk Kim int CRYPTO_secure_malloc_done();
22e71b7053SJung-uk Kim
23e71b7053SJung-uk Kim void *OPENSSL_secure_malloc(size_t num);
24e71b7053SJung-uk Kim void *CRYPTO_secure_malloc(size_t num, const char *file, int line);
25e71b7053SJung-uk Kim
26e71b7053SJung-uk Kim void *OPENSSL_secure_zalloc(size_t num);
27e71b7053SJung-uk Kim void *CRYPTO_secure_zalloc(size_t num, const char *file, int line);
28e71b7053SJung-uk Kim
29e71b7053SJung-uk Kim void OPENSSL_secure_free(void* ptr);
30e71b7053SJung-uk Kim void CRYPTO_secure_free(void *ptr, const char *, int);
31e71b7053SJung-uk Kim
32e71b7053SJung-uk Kim void OPENSSL_secure_clear_free(void* ptr, size_t num);
33e71b7053SJung-uk Kim void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *, int);
34e71b7053SJung-uk Kim
35e71b7053SJung-uk Kim size_t OPENSSL_secure_actual_size(const void *ptr);
36e71b7053SJung-uk Kim
3717f01e99SJung-uk Kim int CRYPTO_secure_allocated(const void *ptr);
38e71b7053SJung-uk Kim size_t CRYPTO_secure_used();
39e71b7053SJung-uk Kim
40e71b7053SJung-uk Kim=head1 DESCRIPTION
41e71b7053SJung-uk Kim
42e71b7053SJung-uk KimIn order to help protect applications (particularly long-running servers)
43e71b7053SJung-uk Kimfrom pointer overruns or underruns that could return arbitrary data from
44e71b7053SJung-uk Kimthe program's dynamic memory area, where keys and other sensitive
45e71b7053SJung-uk Kiminformation might be stored, OpenSSL supports the concept of a "secure heap."
46e71b7053SJung-uk KimThe level and type of security guarantees depend on the operating system.
47e71b7053SJung-uk KimIt is a good idea to review the code and see if it addresses your
48e71b7053SJung-uk Kimthreat model and concerns.
49e71b7053SJung-uk Kim
50e71b7053SJung-uk KimIf a secure heap is used, then private key B<BIGNUM> values are stored there.
51e71b7053SJung-uk KimThis protects long-term storage of private keys, but will not necessarily
52e71b7053SJung-uk Kimput all intermediate values and computations there.
53e71b7053SJung-uk Kim
54e71b7053SJung-uk KimCRYPTO_secure_malloc_init() creates the secure heap, with the specified
55e71b7053SJung-uk KimC<size> in bytes. The C<minsize> parameter is the minimum size to
56b077aed3SPierre Proncheryallocate from the heap or zero to use a reasonable default value.
57b077aed3SPierre ProncheryBoth C<size> and, if specified, C<minsize> must be a power of two and
58b077aed3SPierre ProncheryC<minsize> should generally be small, for example 16 or 32.
59b077aed3SPierre ProncheryC<minsize> must be less than a quarter of C<size> in any case.
60e71b7053SJung-uk Kim
61e71b7053SJung-uk KimCRYPTO_secure_malloc_initialized() indicates whether or not the secure
62e71b7053SJung-uk Kimheap as been initialized and is available.
63e71b7053SJung-uk Kim
64e71b7053SJung-uk KimCRYPTO_secure_malloc_done() releases the heap and makes the memory unavailable
65e71b7053SJung-uk Kimto the process if all secure memory has been freed.
66e71b7053SJung-uk KimIt can take noticeably long to complete.
67e71b7053SJung-uk Kim
68e71b7053SJung-uk KimOPENSSL_secure_malloc() allocates C<num> bytes from the heap.
69e71b7053SJung-uk KimIf CRYPTO_secure_malloc_init() is not called, this is equivalent to
70e71b7053SJung-uk Kimcalling OPENSSL_malloc().
71e71b7053SJung-uk KimIt is a macro that expands to
72e71b7053SJung-uk KimCRYPTO_secure_malloc() and adds the C<__FILE__> and C<__LINE__> parameters.
73e71b7053SJung-uk Kim
74e71b7053SJung-uk KimOPENSSL_secure_zalloc() and CRYPTO_secure_zalloc() are like
75e71b7053SJung-uk KimOPENSSL_secure_malloc() and CRYPTO_secure_malloc(), respectively,
76e71b7053SJung-uk Kimexcept that they call memset() to zero the memory before returning.
77e71b7053SJung-uk Kim
78e71b7053SJung-uk KimOPENSSL_secure_free() releases the memory at C<ptr> back to the heap.
79e71b7053SJung-uk KimIt must be called with a value previously obtained from
80e71b7053SJung-uk KimOPENSSL_secure_malloc().
81e71b7053SJung-uk KimIf CRYPTO_secure_malloc_init() is not called, this is equivalent to
82e71b7053SJung-uk Kimcalling OPENSSL_free().
83e71b7053SJung-uk KimIt exists for consistency with OPENSSL_secure_malloc() , and
84e71b7053SJung-uk Kimis a macro that expands to CRYPTO_secure_free() and adds the C<__FILE__>
85*a7148ab3SEnji Cooperand C<__LINE__> parameters..  If the argument to OPENSSL_secure_free()
86*a7148ab3SEnji Cooperis NULL, nothing is done.
87e71b7053SJung-uk Kim
88e71b7053SJung-uk KimOPENSSL_secure_clear_free() is similar to OPENSSL_secure_free() except
89e71b7053SJung-uk Kimthat it has an additional C<num> parameter which is used to clear
90e71b7053SJung-uk Kimthe memory if it was not allocated from the secure heap.
91e71b7053SJung-uk KimIf CRYPTO_secure_malloc_init() is not called, this is equivalent to
92*a7148ab3SEnji Coopercalling OPENSSL_clear_free(). If the argument to OPENSSL_secure_clear_free()
93*a7148ab3SEnji Cooperis NULL, nothing is done.
94e71b7053SJung-uk Kim
95e71b7053SJung-uk KimOPENSSL_secure_actual_size() tells the actual size allocated to the
96e71b7053SJung-uk Kimpointer; implementations may allocate more space than initially
97e71b7053SJung-uk Kimrequested, in order to "round up" and reduce secure heap fragmentation.
98e71b7053SJung-uk Kim
9917f01e99SJung-uk KimOPENSSL_secure_allocated() tells if a pointer is allocated in the secure heap.
10017f01e99SJung-uk Kim
101e71b7053SJung-uk KimCRYPTO_secure_used() returns the number of bytes allocated in the
102e71b7053SJung-uk Kimsecure heap.
103e71b7053SJung-uk Kim
104e71b7053SJung-uk Kim=head1 RETURN VALUES
105e71b7053SJung-uk Kim
106e71b7053SJung-uk KimCRYPTO_secure_malloc_init() returns 0 on failure, 1 if successful,
107e71b7053SJung-uk Kimand 2 if successful but the heap could not be protected by memory
108e71b7053SJung-uk Kimmapping.
109e71b7053SJung-uk Kim
110e71b7053SJung-uk KimCRYPTO_secure_malloc_initialized() returns 1 if the secure heap is
111e71b7053SJung-uk Kimavailable (that is, if CRYPTO_secure_malloc_init() has been called,
112e71b7053SJung-uk Kimbut CRYPTO_secure_malloc_done() has not been called or failed) or 0 if not.
113e71b7053SJung-uk Kim
114e71b7053SJung-uk KimOPENSSL_secure_malloc() and OPENSSL_secure_zalloc() return a pointer into
115e71b7053SJung-uk Kimthe secure heap of the requested size, or C<NULL> if memory could not be
116e71b7053SJung-uk Kimallocated.
117e71b7053SJung-uk Kim
118e71b7053SJung-uk KimCRYPTO_secure_allocated() returns 1 if the pointer is in the secure heap, or 0 if not.
119e71b7053SJung-uk Kim
120e71b7053SJung-uk KimCRYPTO_secure_malloc_done() returns 1 if the secure memory area is released, or 0 if not.
121e71b7053SJung-uk Kim
122e71b7053SJung-uk KimOPENSSL_secure_free() and OPENSSL_secure_clear_free() return no values.
123e71b7053SJung-uk Kim
124e71b7053SJung-uk Kim=head1 SEE ALSO
125e71b7053SJung-uk Kim
126e71b7053SJung-uk KimL<OPENSSL_malloc(3)>,
127e71b7053SJung-uk KimL<BN_new(3)>
128e71b7053SJung-uk Kim
129e71b7053SJung-uk Kim=head1 HISTORY
130e71b7053SJung-uk Kim
1316935a639SJung-uk KimThe OPENSSL_secure_clear_free() function was added in OpenSSL 1.1.0g.
132e71b7053SJung-uk Kim
133b077aed3SPierre ProncheryThe second argument to CRYPTO_secure_malloc_init() was changed from an B<int> to
134b077aed3SPierre Proncherya B<size_t> in OpenSSL 3.0.
135b077aed3SPierre Pronchery
136e71b7053SJung-uk Kim=head1 COPYRIGHT
137e71b7053SJung-uk Kim
138*a7148ab3SEnji CooperCopyright 2015-2024 The OpenSSL Project Authors. All Rights Reserved.
139e71b7053SJung-uk Kim
140b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
141e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
142e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
143e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
144e71b7053SJung-uk Kim
145e71b7053SJung-uk Kim=cut
146