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