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