1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*!
\ingroup Memory
\brief This function is similar to malloc(), but calls the memory
allocation function which wolfSSL has been configured to use. By default,
wolfSSL uses malloc(). This can be changed using the wolfSSL memory
abstraction layer - see wolfSSL_SetAllocators(). Note wolfSSL_Malloc is not
called directly by wolfSSL, but instead called by macro XMALLOC.
For the default build only the size argument exists. If using
WOLFSSL_STATIC_MEMORY build then heap and type arguments are included.
\return pointer If successful, this function returns a pointer to
allocated memory.
\return error If there is an error, NULL will be returned.
\param size size, in bytes, of the memory to allocate
\param heap heap hint to use for memory. Can be NULL
\param type dynamic type (see DYNAMIC_TYPE_ list in types.h)
_Example_
\code
int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
\endcode
\sa wolfSSL_Free
\sa wolfSSL_Realloc
\sa wolfSSL_SetAllocators
\sa XMALLOC
\sa XFREE
\sa XREALLOC
*/
void* ;
/*!
\ingroup Memory
\brief This function is similar to free(), but calls the memory free
function which wolfSSL has been configured to use. By default, wolfSSL
uses free(). This can be changed using the wolfSSL memory abstraction
layer - see wolfSSL_SetAllocators(). Note wolfSSL_Free is not
called directly by wolfSSL, but instead called by macro XFREE.
For the default build only the ptr argument exists. If using
WOLFSSL_STATIC_MEMORY build then heap and type arguments are included.
\return none No returns.
\param ptr pointer to the memory to be freed.
\param heap heap hint to use for memory. Can be NULL
\param type dynamic type (see DYNAMIC_TYPE_ list in types.h)
_Example_
\code
int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
// process data as desired
...
if(tenInts) {
wolfSSL_Free(tenInts, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}
\endcode
\sa wolfSSL_Alloc
\sa wolfSSL_Realloc
\sa wolfSSL_SetAllocators
\sa XMALLOC
\sa XFREE
\sa XREALLOC
*/
void ;
/*!
\ingroup Memory
\brief This function is similar to realloc(), but calls the memory
re-allocation function which wolfSSL has been configured to use.
By default, wolfSSL uses realloc(). This can be changed using the
wolfSSL memory abstraction layer - see wolfSSL_SetAllocators().
Note wolfSSL_Realloc is not called directly by wolfSSL, but instead called
by macro XREALLOC. For the default build only the size argument exists.
If using WOLFSSL_STATIC_MEMORY build then heap and type arguments are included.
\return pointer If successful, this function returns a pointer to
re-allocated memory. This may be the same pointer as ptr, or a
new pointer location.
\return Null If there is an error, NULL will be returned.
\param ptr pointer to the previously-allocated memory, to be reallocated.
\param size number of bytes to allocate.
\param heap heap hint to use for memory. Can be NULL
\param type dynamic type (see DYNAMIC_TYPE_ list in types.h)
_Example_
\code
int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
int* twentyInts = (int*)wolfSSL_Realloc(tenInts, sizeof(int)*20);
\endcode
\sa wolfSSL_Free
\sa wolfSSL_Malloc
\sa wolfSSL_SetAllocators
\sa XMALLOC
\sa XFREE
\sa XREALLOC
*/
void* ;
/*!
\ingroup Memory
\brief This function registers the allocation functions used by wolfSSL.
By default, if the system supports it, malloc/free and realloc are used.
Using this function allows the user at runtime to install their own
memory handlers.
\return Success If successful this function will return 0.
\return BAD_FUNC_ARG is the error that will be returned if a
function pointer is not provided.
\param malloc_function memory allocation function for wolfSSL to use.
Function signature must match wolfSSL_Malloc_cb prototype, above.
\param free_function memory free function for wolfSSL to use. Function
signature must match wolfSSL_Free_cb prototype, above.
\param realloc_function memory re-allocation function for wolfSSL to use.
Function signature must match wolfSSL_Realloc_cb prototype, above.
_Example_
\code
static void* MyMalloc(size_t size)
{
// custom malloc function
}
static void MyFree(void* ptr)
{
// custom free function
}
static void* MyRealloc(void* ptr, size_t size)
{
// custom realloc function
}
// Register custom memory functions with wolfSSL
int ret = wolfSSL_SetAllocators(MyMalloc, MyFree, MyRealloc);
if (ret != 0) {
// failed to set memory functions
}
\endcode
\sa none
*/
int ;
/*!
\ingroup Memory
\brief This function is available when static memory feature is used
(--enable-staticmemory). It gives the optimum buffer size for memory
“buckets”. This allows for a way to compute buffer size so that no
extra unused memory is left at the end after it has been partitioned.
For the none _ex version of this function the default bucket and
distribution list set during compile time is used.
The returned value, if positive, is the computed buffer size to use.
\return Success On successfully completing buffer size calculations a
positive value is returned. This returned value is for optimum buffer size.
\return Failure All negative values are considered to be error cases.
\param buffer pointer to buffer
\param size size of buffer
\param type desired type of memory ie WOLFMEM_GENERAL or WOLFMEM_IO_POOL
_Example_
\code
byte buffer[1000];
word32 size = sizeof(buffer);
int optimum;
optimum = wolfSSL_StaticBufferSz(buffer, size, WOLFMEM_GENERAL);
if (optimum < 0) { //handle error case }
printf(“The optimum buffer size to make use of all memory is %d\n”,
optimum);
...
\endcode
\sa wolfSSL_Malloc
\sa wolfSSL_Free
*/
int ;
/*!
\ingroup Memory
\brief This function is available when static memory feature is used
(--enable-staticmemory). It gives the size of padding needed for each
partition of memory. This padding size will be the size needed to
contain a memory management structure along with any extra for
memory alignment.
\return On successfully memory padding calculation the return value will
be a positive value
\return All negative values are considered error cases.
\param none No parameters.
_Example_
\code
int padding;
padding = wolfSSL_MemoryPaddingSz();
if (padding < 0) { //handle error case }
printf(“The padding size needed for each \”bucket\” of memory is %d\n”,
padding);
// calculation of buffer for IO POOL size is number of buckets
// times (padding + WOLFMEM_IO_SZ)
...
\endcode
\sa wolfSSL_Malloc
\sa wolfSSL_Free
*/
int ;
/*!
\ingroup Memory
\brief This function is used to set aside static memory for a CTX.
Memory set aside is then used for the CTX’s lifetime and for any SSL objects created
from the CTX. By passing in a NULL ctx pointer and a wolfSSL_method_func function the creation
of the CTX itself will also use static memory. wolfSSL_method_func has the function signature
of WOLFSSL_METHOD* (*wolfSSL_method_func)(void* heap);.
Passing in 0 for max makes it behave as if not set and no max concurrent use restrictions
is in place.
The flag value passed in determines how the memory is used and behavior while operating.
Available flags are the following.
0 - default general memory
WOLFMEM_IO_POOL - used for input/output buffer when sending receiving messages.
Overrides general memory, so all memory in buffer passed in is used for IO.
WOLFMEM_IO_FIXED - same as WOLFMEM_IO_POOL but each SSL now keeps two
buffers to themselves for their lifetime.
WOLFMEM_TRACK_STATS - each SSL keeps track of memory stats while running.
\return If successful, SSL_SUCCESS will be returned.
\return All unsuccessful return values will be less than 0 or equal to SSL_FAILURE.
\param ctx address of pointer to a WOLFSSL_CTX structure.
\param method function to create protocol. (should be NULL if ctx is not also NULL)
\param buf memory to use for all operations.
\param sz size of memory buffer being passed in.
\param flag type of memory.
\param max max concurrent operations.
_Example_
\code
WOLFSSL_CTX* ctx;
WOLFSSL* ssl;
int ret;
unsigned char memory[MAX];
int memorySz = MAX;
unsigned char IO[MAX];
int IOSz = MAX;
int flag = WOLFMEM_IO_FIXED | WOLFMEM_TRACK_STATS;
...
// create ctx also using static memory, start with general memory to use
ctx = NULL:
ret = wolfSSL_CTX_load_static_memory(&ctx, wolfSSLv23_server_method_ex, memory, memorySz, 0,
MAX_CONCURRENT_HANDSHAKES);
if (ret != SSL_SUCCESS) {
// handle error case
}
// load in memory for use with IO
ret = wolfSSL_CTX_load_static_memory(&ctx, NULL, IO, IOSz, flag, MAX_CONCURRENT_IO);
if (ret != SSL_SUCCESS) {
// handle error case
}
...
\endcode
\sa wolfSSL_CTX_new
\sa wolfSSL_CTX_is_static_memory
\sa wolfSSL_is_static_memory
*/
int ;
/*!
\ingroup Memory
\brief This function does not change any of the connections behavior and is used only for
gathering information about the static memory usage.
\return A value of 1 is returned if using static memory for the CTX is true.
\return 0 is returned if not using static memory.
\param ctx a pointer to a WOLFSSL_CTX structure, created using wolfSSL_CTX_new().
\param mem_stats structure to hold information about staic memory usage.
_Example_
\code
WOLFSSL_CTX* ctx;
int ret;
WOLFSSL_MEM_STATS mem_stats;
...
//get information about static memory with CTX
ret = wolfSSL_CTX_is_static_memory(ctx, &mem_stats);
if (ret == 1) {
// handle case of is using static memory
// print out or inspect elements of mem_stats
}
if (ret == 0) {
//handle case of ctx not using static memory
}
...
\endcode
\sa wolfSSL_CTX_new
\sa wolfSSL_CTX_load_static_memory
\sa wolfSSL_is_static_memory
*/
int ;
/*!
\ingroup Memory
\brief wolfSSL_is_static_memory is used to gather information about a SSL’s static
memory usage. The return value indicates if static memory is being used and
WOLFSSL_MEM_CONN_STATS will be filled out if and only if the flag WOLFMEM_TRACK_STATS was
passed to the parent CTX when loading in static memory.
\return A value of 1 is returned if using static memory for the CTX is true.
\return 0 is returned if not using static memory.
\param ssl a pointer to a WOLFSSL structure, created using wolfSSL_new().
\param mem_stats structure to contain static memory usage
_Example_
\code
WOLFSSL* ssl;
int ret;
WOLFSSL_MEM_CONN_STATS mem_stats;
...
ret = wolfSSL_is_static_memory(ssl, mem_stats);
if (ret == 1) {
// handle case when is static memory
// investigate elements in mem_stats if WOLFMEM_TRACK_STATS flag
}
...
\endcode
\sa wolfSSL_new
\sa wolfSSL_CTX_is_static_memory
*/
int ;
/*!
\ingroup Memory
\brief This function is used to set aside static memory for wolfCrypt use. Memory can be
used by passing the created heap hint into functions. An example of this is when calling
wc_InitRng_ex. The flag value passed in determines how the memory is used and behavior
while operating, in general wolfCrypt operations will use memory from a WOLFMEM_GENERAL pool.
Available flags are the following.
WOLFMEM_GENERAL - default general memory
WOLFMEM_IO_POOL - used for input/output buffer when sending receiving messages.
Overrides general memory, so all memory in buffer passed in is used for IO.
WOLFMEM_IO_FIXED - same as WOLFMEM_IO_POOL but each SSL now keeps two
buffers to themselves for their lifetime.
WOLFMEM_TRACK_STATS - each SSL keeps track of memory stats while running
\return Returns 0 on success.
\return Returns a non-zero integer on failure.
\param pHint WOLFSSL_HEAP_HINT structure to use
\param buf memory to use for all operations.
\param sz size of memory buffer being passed in.
\param flag type of memory.
\param max max concurrent operations (handshakes, IO).
_Example_
\code
WOLFSSL_HEAP_HINT hint;
int ret;
unsigned char memory[MAX];
int memorySz = MAX;
int flag = WOLFMEM_GENERAL | WOLFMEM_TRACK_STATS;
...
// load in memory for use
ret = wc_LoadStaticMemory(&hint, memory, memorySz, flag, 0);
if (ret) {
// handle error case
}
...
ret = wc_InitRng_ex(&rng, hint, 0);
// check ret value
\endcode
\sa none
*/
int ;
/*!
\ingroup Memory
\brief This function is used to set aside static memory for wolfCrypt use with custom
bucket sizes and distributions. Memory can be used by passing the created heap hint
into functions. This extended version allows for custom bucket sizes and distributions
instead of using the default predefined sizes.
\return Returns 0 on success.
\return Returns a non-zero integer on failure.
\param pHint WOLFSSL_HEAP_HINT handle to initialize
\param listSz number of entries in the size and distribution lists
\param sizeList array of bucket sizes to use
\param distList distribution list matching sizeList
\param buf memory to use for all operations.
\param sz size of memory buffer being passed in.
\param flag type of memory.
\param max max concurrent operations (handshakes, IO).
_Example_
\code
WOLFSSL_HEAP_HINT hint;
int ret;
unsigned char memory[MAX];
int memorySz = MAX;
int flag = WOLFMEM_GENERAL | WOLFMEM_TRACK_STATS;
const word32 sizeList[] = {64, 128, 256, 512, 1024};
const word32 distList[] = {1, 1, 1, 1, 1};
unsigned int listSz = (unsigned int)(sizeof(sizeList)/
sizeof(sizeList[0]));
...
// load in memory for use with custom bucket sizes
ret = wc_LoadStaticMemory_ex(&hint, listSz, sizeList, distList,
memory, memorySz, flag, 0);
if (ret) {
// handle error case
}
...
ret = wc_InitRng_ex(&rng, hint, 0);
// check ret value
\endcode
\sa wc_LoadStaticMemory
\sa wc_UnloadStaticMemory
*/
int ;
/*!
\ingroup Memory
\brief This function sets a global heap hint that will be used when NULL heap hint
is passed to memory allocation functions. This allows for setting a default heap
hint that will be used across the entire application.
\return Returns the previous global heap hint that was set.
\param hint WOLFSSL_HEAP_HINT structure to use as the global heap hint
_Example_
\code
WOLFSSL_HEAP_HINT hint;
WOLFSSL_HEAP_HINT* prev_hint;
int ret;
unsigned char memory[MAX];
int memorySz = MAX;
...
// load in memory for use
ret = wc_LoadStaticMemory(&hint, memory, memorySz, WOLFMEM_GENERAL, 0);
if (ret != SSL_SUCCESS) {
// handle error case
}
// set as global heap hint
prev_hint = wolfSSL_SetGlobalHeapHint(&hint);
if (prev_hint != NULL) {
// there was a previous global heap hint
}
\endcode
\sa wolfSSL_GetGlobalHeapHint
\sa wc_LoadStaticMemory
*/
WOLFSSL_HEAP_HINT* ;
/*!
\ingroup Memory
\brief This function gets the current global heap hint that is used when NULL
heap hint is passed to memory allocation functions.
\return Returns the current global heap hint, or NULL if none is set.
\param none No parameters.
_Example_
\code
WOLFSSL_HEAP_HINT* current_hint;
...
current_hint = wolfSSL_GetGlobalHeapHint();
if (current_hint != NULL) {
// there is a global heap hint set
// can use current_hint for operations
}
\endcode
\sa wolfSSL_SetGlobalHeapHint
\sa wc_LoadStaticMemory
*/
WOLFSSL_HEAP_HINT* ;
/*!
\ingroup Memory
\brief This function sets a debug callback function for static memory allocation
tracking. Used with WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK build option. The callback
function will be called during memory allocation and deallocation operations to
provide debugging information.
\return If successful, 0 will be returned.
\return All unsuccessful return values will be less than 0.
\param cb debug callback function to set
_Example_
\code
static void debug_memory_cb(const char* func, const char* file, int line,
void* ptr, size_t size, int type)
{
printf("Memory %s: %s:%d ptr=%p size=%zu type=%d\n",
func, file, line, ptr, size, type);
}
...
// set debug callback
int ret = wolfSSL_SetDebugMemoryCb(debug_memory_cb);
if (ret != 0) {
// handle error case
}
\endcode
\sa none
*/
void ;
/*!
\ingroup Memory
\brief This function frees static memory heap and associated mutex. Should be
called when done using static memory allocation to properly clean up resources.
\return If successful, 0 will be returned.
\return All unsuccessful return values will be less than 0.
\param hint WOLFSSL_HEAP_HINT structure to unload
_Example_
\code
WOLFSSL_HEAP_HINT hint;
int ret;
unsigned char memory[MAX];
int memorySz = MAX;
...
// load in memory for use
ret = wc_LoadStaticMemory(&hint, memory, memorySz, WOLFMEM_GENERAL, 0);
if (ret != SSL_SUCCESS) {
// handle error case
}
// use memory for operations
...
// cleanup when done
wc_UnloadStaticMemory(&hint);
\endcode
\sa wc_LoadStaticMemory
\sa wc_LoadStaticMemory_ex
*/
void ;
/*!
\ingroup Memory
\brief This function calculates the required buffer size for static memory allocation
with custom bucket sizes and distributions. This extended version allows for custom
bucket sizes instead of using the default predefined sizes.
\return On successfully completing buffer size calculations a positive value is returned.
\return All negative values are considered to be error cases.
\param bucket_sizes array of bucket sizes to use
\param bucket_count number of bucket sizes in the array
\param flag desired type of memory ie WOLFMEM_GENERAL or WOLFMEM_IO_POOL
_Example_
\code
word32 sizeList[] = {64, 128, 256, 512, 1024};
word32 distList[] = {1, 2, 1, 1, 1};
int listSz = 5;
int optimum;
optimum = wolfSSL_StaticBufferSz_ex(listSz, sizeList, distList, NULL, 0,
WOLFMEM_GENERAL);
if (optimum < 0) { //handle error case }
printf("The optimum buffer size with custom buckets is %d\n", optimum);
...
\endcode
\sa wolfSSL_StaticBufferSz
\sa wc_LoadStaticMemory_ex
*/
int ;