OPENSSL_secure_malloc.3ossl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "OPENSSL_SECURE_MALLOC 3ossl"
  58. .TH OPENSSL_SECURE_MALLOC 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. CRYPTO_secure_malloc_init, CRYPTO_secure_malloc_initialized,
  65. CRYPTO_secure_malloc_done, OPENSSL_secure_malloc, CRYPTO_secure_malloc,
  66. OPENSSL_secure_zalloc, CRYPTO_secure_zalloc, OPENSSL_secure_free,
  67. CRYPTO_secure_free, OPENSSL_secure_clear_free,
  68. CRYPTO_secure_clear_free, OPENSSL_secure_actual_size,
  69. CRYPTO_secure_allocated,
  70. CRYPTO_secure_used \- secure heap storage
  71. .SH SYNOPSIS
  72. .IX Header "SYNOPSIS"
  73. .Vb 1
  74. \& #include <openssl/crypto.h>
  75. \&
  76. \& int CRYPTO_secure_malloc_init(size_t size, size_t minsize);
  77. \&
  78. \& int CRYPTO_secure_malloc_initialized();
  79. \&
  80. \& int CRYPTO_secure_malloc_done();
  81. \&
  82. \& void *OPENSSL_secure_malloc(size_t num);
  83. \& void *CRYPTO_secure_malloc(size_t num, const char *file, int line);
  84. \&
  85. \& void *OPENSSL_secure_zalloc(size_t num);
  86. \& void *CRYPTO_secure_zalloc(size_t num, const char *file, int line);
  87. \&
  88. \& void OPENSSL_secure_free(void* ptr);
  89. \& void CRYPTO_secure_free(void *ptr, const char *, int);
  90. \&
  91. \& void OPENSSL_secure_clear_free(void* ptr, size_t num);
  92. \& void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *, int);
  93. \&
  94. \& size_t OPENSSL_secure_actual_size(const void *ptr);
  95. \&
  96. \& int CRYPTO_secure_allocated(const void *ptr);
  97. \& size_t CRYPTO_secure_used();
  98. .Ve
  99. .SH DESCRIPTION
  100. .IX Header "DESCRIPTION"
  101. In order to help protect applications (particularly long-running servers)
  102. from pointer overruns or underruns that could return arbitrary data from
  103. the program's dynamic memory area, where keys and other sensitive
  104. information might be stored, OpenSSL supports the concept of a "secure heap."
  105. The level and type of security guarantees depend on the operating system.
  106. It is a good idea to review the code and see if it addresses your
  107. threat model and concerns.
  108. .PP
  109. If a secure heap is used, then private key \fBBIGNUM\fR values are stored there.
  110. This protects long-term storage of private keys, but will not necessarily
  111. put all intermediate values and computations there.
  112. .PP
  113. \&\fBCRYPTO_secure_malloc_init()\fR creates the secure heap, with the specified
  114. \&\f(CW\*(C`size\*(C'\fR in bytes. The \f(CW\*(C`minsize\*(C'\fR parameter is the minimum size to
  115. allocate from the heap or zero to use a reasonable default value.
  116. Both \f(CW\*(C`size\*(C'\fR and, if specified, \f(CW\*(C`minsize\*(C'\fR must be a power of two and
  117. \&\f(CW\*(C`minsize\*(C'\fR should generally be small, for example 16 or 32.
  118. \&\f(CW\*(C`minsize\*(C'\fR must be less than a quarter of \f(CW\*(C`size\*(C'\fR in any case.
  119. .PP
  120. \&\fBCRYPTO_secure_malloc_initialized()\fR indicates whether or not the secure
  121. heap as been initialized and is available.
  122. .PP
  123. \&\fBCRYPTO_secure_malloc_done()\fR releases the heap and makes the memory unavailable
  124. to the process if all secure memory has been freed.
  125. It can take noticeably long to complete.
  126. .PP
  127. \&\fBOPENSSL_secure_malloc()\fR allocates \f(CW\*(C`num\*(C'\fR bytes from the heap.
  128. If \fBCRYPTO_secure_malloc_init()\fR is not called, this is equivalent to
  129. calling \fBOPENSSL_malloc()\fR.
  130. It is a macro that expands to
  131. \&\fBCRYPTO_secure_malloc()\fR and adds the \f(CW\*(C`_\|_FILE_\|_\*(C'\fR and \f(CW\*(C`_\|_LINE_\|_\*(C'\fR parameters.
  132. .PP
  133. \&\fBOPENSSL_secure_zalloc()\fR and \fBCRYPTO_secure_zalloc()\fR are like
  134. \&\fBOPENSSL_secure_malloc()\fR and \fBCRYPTO_secure_malloc()\fR, respectively,
  135. except that they call \fBmemset()\fR to zero the memory before returning.
  136. .PP
  137. \&\fBOPENSSL_secure_free()\fR releases the memory at \f(CW\*(C`ptr\*(C'\fR back to the heap.
  138. It must be called with a value previously obtained from
  139. \&\fBOPENSSL_secure_malloc()\fR.
  140. If \fBCRYPTO_secure_malloc_init()\fR is not called, this is equivalent to
  141. calling \fBOPENSSL_free()\fR.
  142. It exists for consistency with \fBOPENSSL_secure_malloc()\fR , and
  143. is a macro that expands to \fBCRYPTO_secure_free()\fR and adds the \f(CW\*(C`_\|_FILE_\|_\*(C'\fR
  144. and \f(CW\*(C`_\|_LINE_\|_\*(C'\fR parameters.. If the argument to \fBOPENSSL_secure_free()\fR
  145. is NULL, nothing is done.
  146. .PP
  147. \&\fBOPENSSL_secure_clear_free()\fR is similar to \fBOPENSSL_secure_free()\fR except
  148. that it has an additional \f(CW\*(C`num\*(C'\fR parameter which is used to clear
  149. the memory if it was not allocated from the secure heap.
  150. If \fBCRYPTO_secure_malloc_init()\fR is not called, this is equivalent to
  151. calling \fBOPENSSL_clear_free()\fR. If the argument to \fBOPENSSL_secure_clear_free()\fR
  152. is NULL, nothing is done.
  153. .PP
  154. \&\fBOPENSSL_secure_actual_size()\fR tells the actual size allocated to the
  155. pointer; implementations may allocate more space than initially
  156. requested, in order to "round up" and reduce secure heap fragmentation.
  157. .PP
  158. \&\fBOPENSSL_secure_allocated()\fR tells if a pointer is allocated in the secure heap.
  159. .PP
  160. \&\fBCRYPTO_secure_used()\fR returns the number of bytes allocated in the
  161. secure heap.
  162. .SH "RETURN VALUES"
  163. .IX Header "RETURN VALUES"
  164. \&\fBCRYPTO_secure_malloc_init()\fR returns 0 on failure, 1 if successful,
  165. and 2 if successful but the heap could not be protected by memory
  166. mapping.
  167. .PP
  168. \&\fBCRYPTO_secure_malloc_initialized()\fR returns 1 if the secure heap is
  169. available (that is, if \fBCRYPTO_secure_malloc_init()\fR has been called,
  170. but \fBCRYPTO_secure_malloc_done()\fR has not been called or failed) or 0 if not.
  171. .PP
  172. \&\fBOPENSSL_secure_malloc()\fR and \fBOPENSSL_secure_zalloc()\fR return a pointer into
  173. the secure heap of the requested size, or \f(CW\*(C`NULL\*(C'\fR if memory could not be
  174. allocated.
  175. .PP
  176. \&\fBCRYPTO_secure_allocated()\fR returns 1 if the pointer is in the secure heap, or 0 if not.
  177. .PP
  178. \&\fBCRYPTO_secure_malloc_done()\fR returns 1 if the secure memory area is released, or 0 if not.
  179. .PP
  180. \&\fBOPENSSL_secure_free()\fR and \fBOPENSSL_secure_clear_free()\fR return no values.
  181. .SH "SEE ALSO"
  182. .IX Header "SEE ALSO"
  183. \&\fBOPENSSL_malloc\fR\|(3),
  184. \&\fBBN_new\fR\|(3)
  185. .SH HISTORY
  186. .IX Header "HISTORY"
  187. The \fBOPENSSL_secure_clear_free()\fR function was added in OpenSSL 1.1.0g.
  188. .PP
  189. The second argument to \fBCRYPTO_secure_malloc_init()\fR was changed from an \fBint\fR to
  190. a \fBsize_t\fR in OpenSSL 3.0.
  191. .SH COPYRIGHT
  192. .IX Header "COPYRIGHT"
  193. Copyright 2015\-2024 The OpenSSL Project Authors. All Rights Reserved.
  194. .PP
  195. Licensed under the Apache License 2.0 (the "License"). You may not use
  196. this file except in compliance with the License. You can obtain a copy
  197. in the file LICENSE in the source distribution or at
  198. <https://www.openssl.org/source/license.html>.