nrf_crypto_mem.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_CRYPTO_MEM_H__
  41. #define NRF_CRYPTO_MEM_H__
  42. /** @file
  43. *
  44. * @defgroup nrf_crypto_mem Dynamic memory management module
  45. * @{
  46. * @ingroup nrf_crypto
  47. *
  48. * @brief Module to manage dynamically allocated memory used by nrf_crypto APIs.
  49. *
  50. * @ref NRF_CRYPTO_ALLOCATOR definition is used to configure this module.
  51. */
  52. #include <stdint.h>
  53. #include "sdk_common.h"
  54. #include "sdk_config.h"
  55. #include "nrf_crypto_types.h"
  56. #include "sdk_alloca.h"
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. #ifndef __SDK_DOXYGEN__
  61. #define NRF_CRYPTO_ALLOCATOR_DEFAULT 0 /**< @internal @brief Value for NRF_CRYPTO_ALLOCATOR to select default memory allocation. */
  62. #define NRF_CRYPTO_ALLOCATOR_USER 1 /**< @internal @brief Value for NRF_CRYPTO_ALLOCATOR to select user defined memory allocation. */
  63. #define NRF_CRYPTO_ALLOCATOR_ALLOCA 2 /**< @internal @brief Value for NRF_CRYPTO_ALLOCATOR to select stack based memory allocation. */
  64. #define NRF_CRYPTO_ALLOCATOR_MALLOC 3 /**< @internal @brief Value for NRF_CRYPTO_ALLOCATOR to select stdlib's dynamic memory allocation. */
  65. #define NRF_CRYPTO_ALLOCATOR_NRF_MALLOC 4 /**< @internal @brief Value for NRF_CRYPTO_ALLOCATOR to select mem_manager for memory allocation. */
  66. #ifndef NRF_CRYPTO_ALLOCATOR
  67. #define NRF_CRYPTO_ALLOCATOR NRF_CRYPTO_ALLOCATOR_DEFAULT
  68. #endif
  69. #if NRF_CRYPTO_ALLOCATOR == NRF_CRYPTO_ALLOCATOR_DEFAULT
  70. #undef NRF_CRYPTO_ALLOCATOR
  71. #if SDK_ALLOCA_DEFINED && !NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
  72. #define NRF_CRYPTO_ALLOCATOR NRF_CRYPTO_ALLOCATOR_ALLOCA
  73. #else
  74. #define NRF_CRYPTO_ALLOCATOR NRF_CRYPTO_ALLOCATOR_NRF_MALLOC
  75. #endif
  76. #endif
  77. #if NRF_CRYPTO_ALLOCATOR == NRF_CRYPTO_ALLOCATOR_USER
  78. #include "nrf_crypto_allocator.h"
  79. #ifndef NRF_CRYPTO_ALLOC
  80. #error "User defined allocator for nrf_crypto does not define NRF_CRYPTO_ALLOC"
  81. #endif
  82. #ifndef NRF_CRYPTO_FREE
  83. #error "User defined allocator for nrf_crypto does not define NRF_CRYPTO_FREE"
  84. #endif
  85. #ifndef NRF_CRYPTO_ALLOC_ON_STACK
  86. #error "User defined allocator for nrf_crypto does not define NRF_CRYPTO_ALLOC_ON_STACK"
  87. #endif
  88. #elif NRF_CRYPTO_ALLOCATOR == NRF_CRYPTO_ALLOCATOR_ALLOCA
  89. #if !SDK_ALLOCA_DEFINED
  90. #warning "Stack based allocation is selected, but alloca() is not supported on this platform"
  91. #endif
  92. #define NRF_CRYPTO_ALLOC(size) (alloca((size_t)(size)))
  93. #define NRF_CRYPTO_FREE(p_buffer) // Empty
  94. #define NRF_CRYPTO_ALLOC_ON_STACK 1
  95. #elif NRF_CRYPTO_ALLOCATOR == NRF_CRYPTO_ALLOCATOR_MALLOC
  96. #include "stdlib.h"
  97. #define NRF_CRYPTO_ALLOC(size) (malloc((size_t)(size)))
  98. #define NRF_CRYPTO_FREE(p_buffer) (free((void *)(p_buffer)))
  99. #define NRF_CRYPTO_ALLOC_ON_STACK 0
  100. #elif NRF_CRYPTO_ALLOCATOR == NRF_CRYPTO_ALLOCATOR_NRF_MALLOC
  101. #include "mem_manager.h"
  102. #define NRF_CRYPTO_ALLOC(size) (nrf_malloc((uint32_t)(size)))
  103. #define NRF_CRYPTO_FREE(p_buffer) (nrf_free((void *)(p_buffer)))
  104. #define NRF_CRYPTO_ALLOC_ON_STACK 0
  105. #else
  106. #error "Invalid NRF_CRYPTO_ALLOCATOR configuration value"
  107. #endif
  108. #else // __SDK_DOXYGEN__
  109. /** @brief Defines memory allocation function for nrf_crypto.
  110. *
  111. * This macro is used internally by nrf_crypto library to allocate temporary memory. It is not
  112. * intended to be used outside nrf_crypto library. How memory is actually allocated is configured
  113. * by @ref NRF_CRYPTO_ALLOCATOR definition.
  114. *
  115. * If user macros are selected by @ref NRF_CRYPTO_ALLOCATOR then this macro have to be defined by
  116. * the user in "nrf_crypto_allocator.h" file. The file will be included into "nrf_crypto_mem.h".
  117. *
  118. * If @ref NRF_CRYPTO_ALLOC_ON_STACK is 1 then this function will allocate data on stack,
  119. * so make sure that returned pointer is not used outside the caller function.
  120. *
  121. * @param size Number of bytes to allocate.
  122. * @returns Pointer to newly allocated memory or NULL on error.
  123. */
  124. #define NRF_CRYPTO_ALLOC(size)
  125. /** @brief Defines memory deallocation function for nrf_crypto.
  126. *
  127. * This macro is used internally by nrf_crypto library to deallocate temporary memory. It is not
  128. * intended to be used outside nrf_crypto library.
  129. *
  130. * If user macros are selected by @ref NRF_CRYPTO_ALLOCATOR then this macro have to be defined by
  131. * the user in "nrf_crypto_allocator.h" file. The file will be included into "nrf_crypto_mem.h".
  132. *
  133. * @param p_buffer Pointer to memory buffer for deallocation.
  134. */
  135. #define NRF_CRYPTO_FREE(p_buffer)
  136. /** @brief Contains 1 if memory allocated by @ref NRF_CRYPTO_ALLOC is on stack or 0 otherwise.
  137. *
  138. * This definition is used internally by nrf_crypto library. It is not intended to be used outside
  139. * nrf_crypto library.
  140. *
  141. * If user macros are selected by @ref NRF_CRYPTO_ALLOCATOR then this macro have to be defined by
  142. * the user in "nrf_crypto_allocator.h" file. The file will be included into "nrf_crypto_mem.h".
  143. */
  144. #define NRF_CRYPTO_ALLOC_ON_STACK
  145. #endif // __SDK_DOXYGEN__
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. /**@} */
  150. #endif // NRF_CRYPTO_MEM_H__