2
0

memory.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*!
  2. \ingroup Memory
  3. \brief This function is similar to malloc(), but calls the memory
  4. allocation function which wolfSSL has been configured to use. By default,
  5. wolfSSL uses malloc(). This can be changed using the wolfSSL memory
  6. abstraction layer - see wolfSSL_SetAllocators(). Note wolfSSL_Malloc is not
  7. called directly by wolfSSL, but instead called by macro XMALLOC.
  8. For the default build only the size argument exists. If using
  9. WOLFSSL_STATIC_MEMORY build then heap and type arguments are included.
  10. \return pointer If successful, this function returns a pointer to
  11. allocated memory.
  12. \return error If there is an error, NULL will be returned.
  13. \param size size, in bytes, of the memory to allocate
  14. \param heap heap hint to use for memory. Can be NULL
  15. \param type dynamic type (see DYNAMIC_TYPE_ list in types.h)
  16. _Example_
  17. \code
  18. int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
  19. \endcode
  20. \sa wolfSSL_Free
  21. \sa wolfSSL_Realloc
  22. \sa wolfSSL_SetAllocators
  23. \sa XMALLOC
  24. \sa XFREE
  25. \sa XREALLOC
  26. */
  27. void* wolfSSL_Malloc(size_t size, void* heap, int type);
  28. /*!
  29. \ingroup Memory
  30. \brief This function is similar to free(), but calls the memory free
  31. function which wolfSSL has been configured to use. By default, wolfSSL
  32. uses free(). This can be changed using the wolfSSL memory abstraction
  33. layer - see wolfSSL_SetAllocators(). Note wolfSSL_Free is not
  34. called directly by wolfSSL, but instead called by macro XFREE.
  35. For the default build only the ptr argument exists. If using
  36. WOLFSSL_STATIC_MEMORY build then heap and type arguments are included.
  37. \return none No returns.
  38. \param ptr pointer to the memory to be freed.
  39. \param heap heap hint to use for memory. Can be NULL
  40. \param type dynamic type (see DYNAMIC_TYPE_ list in types.h)
  41. _Example_
  42. \code
  43. int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
  44. // process data as desired
  45. ...
  46. if(tenInts) {
  47. wolfSSL_Free(tenInts);
  48. }
  49. \endcode
  50. \sa wolfSSL_Alloc
  51. \sa wolfSSL_Realloc
  52. \sa wolfSSL_SetAllocators
  53. \sa XMALLOC
  54. \sa XFREE
  55. \sa XREALLOC
  56. */
  57. void wolfSSL_Free(void *ptr, void* heap, int type);
  58. /*!
  59. \ingroup Memory
  60. \brief This function is similar to realloc(), but calls the memory
  61. re-allocation function which wolfSSL has been configured to use.
  62. By default, wolfSSL uses realloc(). This can be changed using the
  63. wolfSSL memory abstraction layer - see wolfSSL_SetAllocators().
  64. Note wolfSSL_Realloc is not called directly by wolfSSL, but instead called
  65. by macro XREALLOC. For the default build only the size argument exists.
  66. If using WOLFSSL_STATIC_MEMORY build then heap and type arguments are included.
  67. \return pointer If successful, this function returns a pointer to
  68. re-allocated memory. This may be the same pointer as ptr, or a
  69. new pointer location.
  70. \return Null If there is an error, NULL will be returned.
  71. \param ptr pointer to the previously-allocated memory, to be reallocated.
  72. \param size number of bytes to allocate.
  73. \param heap heap hint to use for memory. Can be NULL
  74. \param type dynamic type (see DYNAMIC_TYPE_ list in types.h)
  75. _Example_
  76. \code
  77. int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
  78. int* twentyInts = (int*)wolfSSL_Realloc(tenInts, sizeof(int)*20);
  79. \endcode
  80. \sa wolfSSL_Free
  81. \sa wolfSSL_Malloc
  82. \sa wolfSSL_SetAllocators
  83. \sa XMALLOC
  84. \sa XFREE
  85. \sa XREALLOC
  86. */
  87. void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type);
  88. /*!
  89. \ingroup Memory
  90. \brief This function registers the allocation functions used by wolfSSL.
  91. By default, if the system supports it, malloc/free and realloc are used.
  92. Using this function allows the user at runtime to install their own
  93. memory handlers.
  94. \return Success If successful this function will return 0.
  95. \return BAD_FUNC_ARG is the error that will be returned if a
  96. function pointer is not provided.
  97. \param malloc_function memory allocation function for wolfSSL to use.
  98. Function signature must match wolfSSL_Malloc_cb prototype, above.
  99. \param free_function memory free function for wolfSSL to use. Function
  100. signature must match wolfSSL_Free_cb prototype, above.
  101. \param realloc_function memory re-allocation function for wolfSSL to use.
  102. Function signature must match wolfSSL_Realloc_cb prototype, above.
  103. _Example_
  104. \code
  105. static void* MyMalloc(size_t size)
  106. {
  107. // custom malloc function
  108. }
  109. static void MyFree(void* ptr)
  110. {
  111. // custom free function
  112. }
  113. static void* MyRealloc(void* ptr, size_t size)
  114. {
  115. // custom realloc function
  116. }
  117. // Register custom memory functions with wolfSSL
  118. int ret = wolfSSL_SetAllocators(MyMalloc, MyFree, MyRealloc);
  119. if (ret != 0) {
  120. // failed to set memory functions
  121. }
  122. \endcode
  123. \sa none
  124. */
  125. int wolfSSL_SetAllocators(wolfSSL_Malloc_cb,
  126. wolfSSL_Free_cb,
  127. wolfSSL_Realloc_cb);
  128. /*!
  129. \ingroup Memory
  130. \brief This function is available when static memory feature is used
  131. (--enable-staticmemory). It gives the optimum buffer size for memory
  132. “buckets”. This allows for a way to compute buffer size so that no
  133. extra unused memory is left at the end after it has been partitioned.
  134. The returned value, if positive, is the computed buffer size to use.
  135. \return Success On successfully completing buffer size calculations a
  136. positive value is returned. This returned value is for optimum buffer size.
  137. \return Failure All negative values are considered to be error cases.
  138. \param buffer pointer to buffer
  139. \param size size of buffer
  140. \param type desired type of memory ie WOLFMEM_GENERAL or WOLFMEM_IO_POOL
  141. _Example_
  142. \code
  143. byte buffer[1000];
  144. word32 size = sizeof(buffer);
  145. int optimum;
  146. optimum = wolfSSL_StaticBufferSz(buffer, size, WOLFMEM_GENERAL);
  147. if (optimum < 0) { //handle error case }
  148. printf(“The optimum buffer size to make use of all memory is %d\n”,
  149. optimum);
  150. ...
  151. \endcode
  152. \sa wolfSSL_Malloc
  153. \sa wolfSSL_Free
  154. */
  155. int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag);
  156. /*!
  157. \ingroup Memory
  158. \brief This function is available when static memory feature is used
  159. (--enable-staticmemory). It gives the size of padding needed for each
  160. partition of memory. This padding size will be the size needed to
  161. contain a memory management structure along with any extra for
  162. memory alignment.
  163. \return On successfully memory padding calculation the return value will
  164. be a positive value
  165. \return All negative values are considered error cases.
  166. \param none No parameters.
  167. _Example_
  168. \code
  169. int padding;
  170. padding = wolfSSL_MemoryPaddingSz();
  171. if (padding < 0) { //handle error case }
  172. printf(“The padding size needed for each \”bucket\” of memory is %d\n”,
  173. padding);
  174. // calculation of buffer for IO POOL size is number of buckets
  175. // times (padding + WOLFMEM_IO_SZ)
  176. ...
  177. \endcode
  178. \sa wolfSSL_Malloc
  179. \sa wolfSSL_Free
  180. */
  181. int wolfSSL_MemoryPaddingSz(void);
  182. /*!
  183. \ingroup Memory
  184. \brief This function is used to set aside static memory for a CTX.
  185. Memory set aside is then used for the CTX’s lifetime and for any SSL objects created
  186. from the CTX. By passing in a NULL ctx pointer and a wolfSSL_method_func function the creation
  187. of the CTX itself will also use static memory. wolfSSL_method_func has the function signature
  188. of WOLFSSL_METHOD* (*wolfSSL_method_func)(void* heap);.
  189. Passing in 0 for max makes it behave as if not set and no max concurrent use restrictions
  190. is in place.
  191. The flag value passed in determines how the memory is used and behavior while operating.
  192. Available flags are the following.
  193. 0 - default general memory
  194. WOLFMEM_IO_POOL - used for input/output buffer when sending receiving messages.
  195. Overrides general memory, so all memory in buffer passed in is used for IO.
  196. WOLFMEM_IO_FIXED - same as WOLFMEM_IO_POOL but each SSL now keeps two
  197. buffers to themselves for their lifetime.
  198. WOLFMEM_TRACK_STATS - each SSL keeps track of memory stats while running.
  199. \return If successful, SSL_SUCCESS will be returned.
  200. \return All unsuccessful return values will be less than 0 or equal to SSL_FAILURE.
  201. \param ctx address of pointer to a WOLFSSL_CTX structure.
  202. \param method function to create protocol. (should be NULL if ctx is not also NULL)
  203. \param buf memory to use for all operations.
  204. \param sz size of memory buffer being passed in.
  205. \param flag type of memory.
  206. \param max max concurrent operations.
  207. _Example_
  208. \code
  209. WOLFSSL_CTX* ctx;
  210. WOLFSSL* ssl;
  211. int ret;
  212. unsigned char memory[MAX];
  213. int memorySz = MAX;
  214. unsigned char IO[MAX];
  215. int IOSz = MAX;
  216. int flag = WOLFMEM_IO_FIXED | WOLFMEM_TRACK_STATS;
  217. ...
  218. // create ctx also using static memory, start with general memory to use
  219. ctx = NULL:
  220. ret = wolfSSL_CTX_load_static_memory(&ctx, wolfSSLv23_server_method_ex, memory, memorySz, 0,
  221. MAX_CONCURRENT_HANDSHAKES);
  222. if (ret != SSL_SUCCESS) {
  223. // handle error case
  224. }
  225. // load in memory for use with IO
  226. ret = wolfSSL_CTX_load_static_memory(&ctx, NULL, IO, IOSz, flag, MAX_CONCURRENT_IO);
  227. if (ret != SSL_SUCCESS) {
  228. // handle error case
  229. }
  230. ...
  231. \endcode
  232. \sa wolfSSL_CTX_new
  233. \sa wolfSSL_CTX_is_static_memory
  234. \sa wolfSSL_is_static_memory
  235. */
  236. int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx, wolfSSL_method_func method,
  237. unsigned char* buf, unsigned int sz, int flag, int max);
  238. /*!
  239. \ingroup Memory
  240. \brief This function does not change any of the connections behavior and is used only for
  241. gathering information about the static memory usage.
  242. \return A value of 1 is returned if using static memory for the CTX is true.
  243. \return 0 is returned if not using static memory.
  244. \param ctx a pointer to a WOLFSSL_CTX structure, created using wolfSSL_CTX_new().
  245. \param mem_stats structure to hold information about staic memory usage.
  246. _Example_
  247. \code
  248. WOLFSSL_CTX* ctx;
  249. int ret;
  250. WOLFSSL_MEM_STATS mem_stats;
  251. ...
  252. //get information about static memory with CTX
  253. ret = wolfSSL_CTX_is_static_memory(ctx, &mem_stats);
  254. if (ret == 1) {
  255. // handle case of is using static memory
  256. // print out or inspect elements of mem_stats
  257. }
  258. if (ret == 0) {
  259. //handle case of ctx not using static memory
  260. }
  261. ...
  262. \endcode
  263. \sa wolfSSL_CTX_new
  264. \sa wolfSSL_CTX_load_static_memory
  265. \sa wolfSSL_is_static_memory
  266. */
  267. int wolfSSL_CTX_is_static_memory(WOLFSSL_CTX* ctx, WOLFSSL_MEM_STATS* mem_stats);
  268. /*!
  269. \ingroup Memory
  270. \brief wolfSSL_is_static_memory is used to gather information about a SSL’s static
  271. memory usage. The return value indicates if static memory is being used and
  272. WOLFSSL_MEM_CONN_STATS will be filled out if and only if the flag WOLFMEM_TRACK_STATS was
  273. passed to the parent CTX when loading in static memory.
  274. \return A value of 1 is returned if using static memory for the CTX is true.
  275. \return 0 is returned if not using static memory.
  276. \param ssl a pointer to a WOLFSSL structure, created using wolfSSL_new().
  277. \param mem_stats structure to contain static memory usage
  278. _Example_
  279. \code
  280. WOLFSSL* ssl;
  281. int ret;
  282. WOLFSSL_MEM_CONN_STATS mem_stats;
  283. ...
  284. ret = wolfSSL_is_static_memory(ssl, mem_stats);
  285. if (ret == 1) {
  286. // handle case when is static memory
  287. // investigate elements in mem_stats if WOLFMEM_TRACK_STATS flag
  288. }
  289. ...
  290. \endcode
  291. \sa wolfSSL_new
  292. \sa wolfSSL_CTX_is_static_memory
  293. */
  294. int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats);
  295. /*!
  296. \ingroup Memory
  297. \brief This function is used to set aside static memory for wolfCrypt use. Memory can be
  298. used by passing the created heap hint into functions. An example of this is when calling
  299. wc_InitRng_ex. The flag value passed in determines how the memory is used and behavior
  300. while operating, in general wolfCrypt operations will use memory from a WOLFMEM_GENERAL pool.
  301. Available flags are the following.
  302. WOLFMEM_GENERAL - default general memory
  303. WOLFMEM_IO_POOL - used for input/output buffer when sending receiving messages.
  304. Overrides general memory, so all memory in buffer passed in is used for IO.
  305. WOLFMEM_IO_FIXED - same as WOLFMEM_IO_POOL but each SSL now keeps two
  306. buffers to themselves for their lifetime.
  307. WOLFMEM_TRACK_STATS - each SSL keeps track of memory stats while running
  308. \return If successful, 0 will be returned.
  309. \return All unsuccessful return values will be less than 0.
  310. \param hint WOLFSSL_HEAP_HINT structure to use
  311. \param buf memory to use for all operations.
  312. \param sz size of memory buffer being passed in.
  313. \param flag type of memory.
  314. \param max max concurrent operations (handshakes, IO).
  315. _Example_
  316. \code
  317. WOLFSSL_HEAP_HINT hint;
  318. int ret;
  319. unsigned char memory[MAX];
  320. int memorySz = MAX;
  321. int flag = WOLFMEM_GENERAL | WOLFMEM_TRACK_STATS;
  322. ...
  323. // load in memory for use
  324. ret = wc_LoadStaticMemory(&hint, memory, memorySz, flag, 0);
  325. if (ret != SSL_SUCCESS) {
  326. // handle error case
  327. }
  328. ...
  329. ret = wc_InitRng_ex(&rng, hint, 0);
  330. // check ret value
  331. \endcode
  332. \sa none
  333. */
  334. int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT* hint, unsigned char* buf, unsigned int sz,
  335. int flag, int max);