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