• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <stdlib.h>
20 #include <string.h>
21 
22 /*!
23  * \file
24  * \brief Memory access interface.
25  * \addtogroup EpidCommon
26  * @{
27  */
28 
29 /// When enabled secrets are wiped out from the memory by EpidFree
30 #define EPID_ENABLE_EPID_ZERO_MEMORY_ON_FREE
31 
32 /// Clear information stored in block of memory pointer to by ptr
33 /*!
34 
35   \warning
36   This function may be optimized away by some compilers. If it is, you
37   should consider using a compiler or operating system specific memory
38   sanitization function (e.g. memcpy_s or SecureZeroMemory).
39 
40   \param[in] ptr
41   pointer to memory block
42   \param[in] size
43   number of bytes to clear
44   */
45 void EpidZeroMemory(void* ptr, size_t size);
46 
47 /// Allocates memory of size bytes
48 /*!
49   The content of memory is initialized with zeros.
50   Memory must be freed with EpidFree function.
51 
52   \param[in] size
53   number of bytes to allocate
54 
55   \returns pointer to allocated memory.
56  */
57 void* EpidAlloc(size_t size);
58 
59 /// Reallocates memory allocated by EpidAlloc
60 /*!
61   In case of error NULL pointer is returned and input memory block
62   is not changed.
63   Memory must be freed with EpidFree function.
64 
65   \param[in] ptr
66   pointer to memory block to reallocate
67   \param[in] new_size
68   number of bytes to reallocate for
69 
70   \returns pointer to allocated memory.
71  */
72 void* EpidRealloc(void* ptr, size_t new_size);
73 
74 /// Frees memory allocated by EpidAlloc
75 /*!
76   Clears information stored in the block of memory.
77 
78   \param[in] ptr
79   pointer to allocated memory block
80  */
81 void EpidFree(void* ptr);
82 
83 #if !defined(SAFE_ALLOC)
84 /// Allocates zero initalized block of memory
85 #define SAFE_ALLOC(size) EpidAlloc(size);
86 #endif  // !defined(SAFE_ALLOC)
87 #if !defined(SAFE_FREE)
88 /// Deallocates space allocated by SAFE_ALLOC() and nulls pointer
89 #define SAFE_FREE(ptr)   \
90   {                      \
91     if (NULL != (ptr)) { \
92       EpidFree(ptr);     \
93       (ptr) = NULL;      \
94     }                    \
95   }
96 #endif  // !defined(SAFE_FREE)
97 
98 #if !defined(SAFE_REALLOC)
99 /// Changes the size of the memory block pointed to by ptr
100 #define SAFE_REALLOC(ptr, size) EpidRealloc((ptr), (size))
101 #endif  // !defined(SAFE_REALLOC)
102 
103 /// Copies bytes between buffers with security ehancements
104 /*!
105   Copies count bytes from src to dest. If the source and destination
106   overlap, the behavior is undefined.
107 
108   \param[out] dest
109   pointer to the object to copy to
110   \param[in] destsz
111   max number of bytes to modify in the destination (typically the size
112   of the destination object)
113   \param[in] src
114   pointer to the object to copy from
115   \param[in] count
116   number of bytes to copy
117 
118   \returns zero on success and non-zero value on error.
119  */
120 int memcpy_S(void* dest, size_t destsz, void const* src, size_t count);
121 
122 /*! @} */
123 #endif  // EPID_COMMON_SRC_MEMORY_H_
124