memory.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*############################################################################
  2. # Copyright 2016 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. #ifndef EPID_COMMON_SRC_MEMORY_H_
  17. #define EPID_COMMON_SRC_MEMORY_H_
  18. #include <stdlib.h>
  19. #include <string.h>
  20. /*!
  21. * \file
  22. * \brief Memory access interface.
  23. * \addtogroup EpidCommon
  24. * @{
  25. */
  26. /// When enabled secrets are wiped out from the memory by EpidFree
  27. #define EPID_ENABLE_EPID_ZERO_MEMORY_ON_FREE
  28. /// Clear information stored in block of memory pointer to by ptr
  29. /*!
  30. \warning
  31. This function may be optimized away by some compilers. If it is, you
  32. should consider using a compiler or operating system specific memory
  33. sanitization function (e.g. memcpy_s or SecureZeroMemory).
  34. \param[in] ptr
  35. pointer to memory block
  36. \param[in] size
  37. number of bytes to clear
  38. */
  39. void EpidZeroMemory(void* ptr, size_t size);
  40. /// Allocates memory of size bytes
  41. /*!
  42. The content of memory is initialized with zeros.
  43. Memory must be freed with EpidFree function.
  44. \param[in] size
  45. number of bytes to allocate
  46. \returns pointer to allocated memory.
  47. */
  48. void* EpidAlloc(size_t size);
  49. /// Reallocates memory allocated by EpidAlloc
  50. /*!
  51. In case of error NULL pointer is returned and input memory block
  52. is not changed.
  53. Memory must be freed with EpidFree function.
  54. \param[in] ptr
  55. pointer to memory block to reallocate
  56. \param[in] new_size
  57. number of bytes to reallocate for
  58. \returns pointer to allocated memory.
  59. */
  60. void* EpidRealloc(void* ptr, size_t new_size);
  61. /// Frees memory allocated by EpidAlloc
  62. /*!
  63. Clears information stored in the block of memory.
  64. \param[in] ptr
  65. pointer to allocated memory block
  66. */
  67. void EpidFree(void* ptr);
  68. #if !defined(SAFE_ALLOC)
  69. /// Allocates zero initalized block of memory
  70. #define SAFE_ALLOC(size) EpidAlloc(size);
  71. #endif // !defined(SAFE_ALLOC)
  72. #if !defined(SAFE_FREE)
  73. /// Deallocates space allocated by SAFE_ALLOC() and nulls pointer
  74. #define SAFE_FREE(ptr) \
  75. { \
  76. if (NULL != (ptr)) { \
  77. EpidFree(ptr); \
  78. (ptr) = NULL; \
  79. } \
  80. }
  81. #endif // !defined(SAFE_FREE)
  82. #if !defined(SAFE_REALLOC)
  83. /// Changes the size of the memory block pointed to by ptr
  84. #define SAFE_REALLOC(ptr, size) EpidRealloc((ptr), (size))
  85. #endif // !defined(SAFE_REALLOC)
  86. /// Copies bytes between buffers with security ehancements
  87. /*!
  88. Copies count bytes from src to dest. If the source and destination
  89. overlap, the behavior is undefined.
  90. \param[out] dest
  91. pointer to the object to copy to
  92. \param[in] destsz
  93. max number of bytes to modify in the destination (typically the size
  94. of the destination object)
  95. \param[in] src
  96. pointer to the object to copy from
  97. \param[in] count
  98. number of bytes to copy
  99. \returns zero on success and non-zero value on error.
  100. */
  101. int memcpy_S(void* dest, size_t destsz, void const* src, size_t count);
  102. /*! @} */
  103. #endif // EPID_COMMON_SRC_MEMORY_H_