1 /*
2 * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7
8
9 /************* Include Files ****************/
10 #include "cc_pal_types.h"
11 #include "cc_pal_error.h"
12 #include "cc_pal_mem.h"
13
14 /************************ Defines ******************************/
15
16 /************************ Enums ******************************/
17
18
19 /************************ Typedefs ******************************/
20
21
22 /************************ Global Data ******************************/
23
24 /************************ Private Functions ******************************/
25
26
27 /************************ Public Functions ******************************/
28
29 /**
30 * @brief This function purpose is to perform secured memory comparison between two given
31 * buffers according to given size. The function will compare each byte till aSize
32 * number of bytes was compared even if the bytes are different.
33 * The function should be used to avoid security timing attacks.
34 *
35 *
36 * @param[in] aTarget - The target buffer to compare
37 * @param[in] aSource - The Source buffer to compare to
38 * @param[in] aSize - Number of bytes to compare
39 *
40 * @return The function will return CC_SUCCESS in case of success, else errors from
41 * cc_pal_error.h will be returned.
42 */
CC_PalSecMemCmp(const uint8_t * aTarget,const uint8_t * aSource,size_t aSize)43 CCError_t CC_PalSecMemCmp( const uint8_t* aTarget,
44 const uint8_t* aSource,
45 size_t aSize )
46 {
47 /* internal index */
48 uint32_t i = 0;
49
50 /* error return */
51 uint32_t error = CC_SUCCESS;
52
53 /*------------------
54 CODE
55 -------------------*/
56
57 /* Go over aTarget till aSize is reached (even if its not equal) */
58 for (i = 0; i < aSize; i++)
59 {
60 if (aTarget[i] != aSource[i])
61 {
62 if (error != CC_SUCCESS)
63 continue;
64 else
65 {
66 if (aTarget[i] < aSource[i])
67 error = CC_PAL_MEM_BUF2_GREATER;
68 else
69 error = CC_PAL_MEM_BUF1_GREATER;
70 }
71 }
72 }
73
74 return error;
75 }/* End of CC_PalSecMemCmp */
76
77
CC_PalMemCmpPlat(const void * aTarget,const void * aSource,size_t aSize)78 int32_t CC_PalMemCmpPlat( const void* aTarget, /*!< [in] The target buffer to compare. */
79 const void* aSource, /*!< [in] The Source buffer to compare to. */
80 size_t aSize /*!< [in] Number of bytes to compare. */)
81 {
82 return memcmp(aTarget, aSource, aSize);
83
84 }/* End of CC_PalMemCmpPlat */
85
CC_PalMemCopyPlat(void * aDestination,const void * aSource,size_t aSize)86 void* CC_PalMemCopyPlat( void* aDestination, /*!< [out] The destination buffer to copy bytes to. */
87 const void* aSource, /*!< [in] The Source buffer to copy from. */
88 size_t aSize /*!< [in] Number of bytes to copy. */ ){
89 return memmove( aDestination, aSource, aSize);
90 }/* End of CC_PalMemCopyPlat */
91
92
93 /*!
94 * @brief This function purpose is to copy aSize bytes from source buffer to destination buffer.
95 * This function Supports overlapped buffers.
96 *
97 * @return void.
98 */
CC_PalMemMovePlat(void * aDestination,const void * aSource,size_t aSize)99 void CC_PalMemMovePlat( void* aDestination, /*!< [out] The destination buffer to copy bytes to. */
100 const void* aSource, /*!< [in] The Source buffer to copy from. */
101 size_t aSize /*!< [in] Number of bytes to copy. */)
102 {
103 memmove(aDestination, aSource, aSize);
104 }/* End of CC_PalMemMovePlat */
105
106
107 /*!
108 * @brief This function purpose is to set aSize bytes in the given buffer with aChar.
109 *
110 * @return void.
111 */
CC_PalMemSetPlat(void * aTarget,uint8_t aChar,size_t aSize)112 void CC_PalMemSetPlat( void* aTarget, /*!< [out] The target buffer to set. */
113 uint8_t aChar, /*!< [in] The char to set into aTarget. */
114 size_t aSize /*!< [in] Number of bytes to set. */)
115 {
116 memset(aTarget, aChar, aSize);
117 }/* End of CC_PalMemSetPlat */
118
119 /*!
120 * @brief This function purpose is to set aSize bytes in the given buffer with zeroes.
121 *
122 * @return void.
123 */
CC_PalMemSetZeroPlat(void * aTarget,size_t aSize)124 void CC_PalMemSetZeroPlat( void* aTarget, /*!< [out] The target buffer to set. */
125 size_t aSize /*!< [in] Number of bytes to set. */)
126 {
127 memset(aTarget, 0x00, aSize);
128 }/* End of CC_PalMemSetZeroPlat */
129
130 /*!
131 * @brief This function purpose is to allocate a memory buffer according to aSize.
132 *
133 *
134 * @return The function returns a pointer to allocated buffer or NULL if allocation failed.
135 */
CC_PalMemMallocPlat(size_t aSize)136 void* CC_PalMemMallocPlat(size_t aSize /*!< [in] Number of bytes to allocate. */)
137 {
138 return malloc(aSize);
139 }/* End of CC_PalMemMallocPlat */
140
141 /*!
142 * @brief This function purpose is to reallocate a memory buffer according to aNewSize.
143 * The content of the old buffer is moved to the new location.
144 *
145 * @return The function returns a pointer to the newly allocated buffer or NULL if allocation failed.
146 */
CC_PalMemReallocPlat(void * aBuffer,size_t aNewSize)147 void* CC_PalMemReallocPlat( void* aBuffer, /*!< [in] Pointer to allocated buffer. */
148 size_t aNewSize /*!< [in] Number of bytes to reallocate. */)
149 {
150 return realloc(aBuffer, aNewSize);
151 }/* End of CC_PalMemReallocPlat */
152
153 /*!
154 * @brief This function purpose is to free allocated buffer.
155 *
156 *
157 * @return void.
158 */
CC_PalMemFreePlat(void * aBuffer)159 void CC_PalMemFreePlat(void* aBuffer /*!< [in] Pointer to allocated buffer.*/)
160 {
161 free(aBuffer);
162 }/* End of CC_PalMemFreePlat */
163