mem functions cleanup
[openssl.git] / doc / crypto / OPENSSL_secure_malloc.pod
1 =pod
2
3 =head1 NAME
4
5 CRYPTO_secure_malloc_init, CRYPTO_secure_malloc_done, OPENSSL_secure_malloc,
6 OPENSSL_secure_free, OPENSSL_secure_allocated - secure heap storage
7
8 =head1 SYNOPSIS
9
10  #include <openssl/crypto.h>
11
12  int CRYPTO_secure_malloc_init(size_t size, int minsize);
13
14  int CRYPTO_secure_malloc_initialized();
15
16  void CRYPTO_secure_malloc_done();
17
18  void *OPENSSL_secure_malloc(int num);
19  void *CRYPTO_secure_malloc(int num, const char *file, int line);
20
21  void OPENSSL_secure_free(void* ptr);
22  void CRYPTO_secure_free(void *ptr);
23
24  size_t OPENSSL_secure_actual_size(const void *ptr);
25  int OPENSSL_secure_allocated(const void *ptr);
26
27  size_t CYRPTO_secure_malloc_used();
28
29 =head1 DESCRIPTION
30
31 In order to help protect applications (particularly long-running servers)
32 from pointer overruns or underruns that could return arbitrary data from
33 the program's dynamic memory area, where keys and other sensitive
34 information might be stored, OpenSSL supports the concept of a "secure heap."
35 The level and type of security guarantees depend on the operating system.
36 It is a good idea to review the code and see if it addresses your
37 threat model and concerns.
38
39 If a secure heap is used, then private key B<BIGNUM> values are stored there.
40 This protects long-term storage of private keys, but will not necessarily
41 put all intermediate values and computations there.
42
43 B<CRYPTO_secure_malloc_init> creates the secure heap, with the specified
44 C<size> in bytes. The C<minsize> parameter is the minimum size to
45 allocate from the heap. Both C<size> and C<minsize> must be a power
46 of two.  It is an error to call this after any B<OPENSSL_secure_malloc>
47 calls have been made.
48
49 B<CRYPTO_secure_malloc_initialized> indicates whether or not the secure
50 heap as been initialized and is available.
51
52 B<CRYPTO_secure_malloc_done> releases the heap and makes the memory unavailable
53 to the process. It can take noticeably long to complete.
54
55 B<OPENSSL_secure_malloc> allocates C<num> bytes from the heap.
56 If B<CRYPTO_secure_malloc_init> is not called, this is equivalent to
57 calling B<OPENSSL_malloc>.
58 It is a macro that expands to
59 B<CRYPTO_secure_malloc> and adds the B<__FILE__> and B<__LINE__> parameters.
60
61 B<OPENSSL_secure_free> releases the memory at C<ptr> back to the heap.
62 It must be called with a value previously obtained from
63 B<OPENSSL_secure_malloc>.
64 If B<CRYPTO_secure_malloc_init> is not called, this is equivalent to
65 calling B<OPENSSL_free>.
66 It exists for consistency with B<OPENSSL_secure_malloc> , and
67 is a macro that expands to B<CRYPTO_secure_free>.
68
69 B<OPENSSL_secure_allocated> tells whether or not a pointer is within
70 the secure heap.
71 B<OPENSSL_secure_actual_size> tells the actual size allocated to the
72 pointer; implementations may allocate more space than initially
73 requested, in order to "round up" and reduce secure heap fragmentation.
74
75 B<CRYPTO_secure_malloc_used> returns the number of bytes allocated in the
76 secure heap.
77
78 =head1 RETURN VALUES
79
80 B<CRYPTO_secure_malloc_init> returns 0 on failure, 1 if successful,
81 and 2 if successful but the heap could not be protected by memory
82 mapping.
83
84 B<CRYPTO_secure_malloc_initialized> returns 1 if the secure heap is
85 available (that is, if B<CRYPTO_secure_malloc_init> has been called,
86 but B<CRYPTO_secure_malloc_done> has not) or 0 if not.
87
88 B<OPENSSL_secure_malloc> returns a pointer into the secure heap of
89 the requested size, or C<NULL> if memory could not be allocated.
90
91 B<CRYPTO_secure_allocated> returns 1 if the pointer is in the
92 the secure heap, or 0 if not.
93
94 B<CRYPTO_secure_malloc_done> and B<OPENSSL_secure_free>
95 return no values.
96
97 =head1 BUGS
98
99 The size parameters should be B<size_t> not B<int> and will be changed
100 in a future release.
101
102 =head1 SEE ALSO
103
104 L<OPENSSL_malloc(3)>,
105 L<BN_new(3)>,
106 L<bn_internal(3)>.
107
108 =cut