1 /* 2 * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #ifndef _CC_COMMON_H 8 #define _CC_COMMON_H 9 10 #include "cc_common_error.h" 11 12 13 #ifdef __cplusplus 14 extern "C" 15 { 16 #endif 17 18 19 /************************ Defines ******************************/ 20 21 /************************ Enums ********************************/ 22 23 /************************ Typedefs ****************************/ 24 25 /************************ Structs *****************************/ 26 27 /************************ Public Variables *********************/ 28 29 /************************ Public Functions *********************/ 30 31 32 33 /*********************************************************************** 34 ** 35 * @brief This function executes a reverse bytes copying from one buffer to another buffer. 36 * 37 * Overlapping of buffers is not allowed, excluding the case, when destination and source 38 * buffers are the same. 39 * Example of a 5 byte buffer: 40 * 41 * dst_ptr[4] = src_ptr[0] 42 * dst_ptr[3] = src_ptr[1] 43 * dst_ptr[2] = src_ptr[2] 44 * dst_ptr[1] = src_ptr[3] 45 * dst_ptr[0] = src_ptr[4] 46 * 47 * @param[in] dst_ptr - The pointer to destination buffer. 48 * @param[in] src_ptr - The pointer to source buffer. 49 * @param[in] size - The size in bytes. 50 * 51 */ 52 CCError_t CC_CommonReverseMemcpy( uint8_t *dst_ptr , uint8_t *src_ptr , uint32_t size ); 53 54 55 /*********************************************************************** 56 ** 57 * @brief This function converts aligned words array to bytes array/ 58 * 59 * 1. Assumed, that input buffer is aligned to 4-bytes word and 60 * bytes order is set according to machine endianness. 61 * 2. Output buffer receives data as bytes stream from LSB to MSB. 62 * For increasing performance on small buffers, the output data is given 63 * by rounded down pointer and alignment. 64 * 3. This implementation is given for both Big and Little endian machines. 65 * 66 * 67 * @param[in] in32_ptr - The pointer to aligned input buffer. 68 * @param[in] out32_ptr - The 32-bits pointer to output buffer (rounded down to 4 bytes) . 69 * @param[in] outAlignBits - The actual output data alignment; 70 * @param[in] sizeWords - The size in words (sizeWords >= 1). 71 * 72 * return - no return value. 73 */ 74 void CC_CommonAlignedWordsArrayToBytes( uint32_t *in32_ptr , uint32_t *out32_ptr , 75 uint32_t outAlignBits, uint32_t sizeWords ); 76 77 /***********************************************************************/ 78 /** 79 * @brief This function converts in place words byffer to bytes buffer with 80 * reversed endianity of output array. 81 * 82 * The function can convert: 83 * - big endian bytes array to words array with little endian order 84 * of words and backward. 85 * 86 * Note: 87 * 1. Endianness of each word in words buffer should be set allways 88 * according to processor used. 89 * 2. Implementation is given for both big and little endianness of 90 * processor. 91 * 92 * @param[in] buf_ptr - The 32-bits pointer to input/output buffer. 93 * @param[in] sizeWords - The size in words (sizeWords > 0). 94 * 95 * @return - no return value. 96 */ 97 void CC_CommonInPlaceConvertBytesWordsAndArrayEndianness( 98 uint32_t *buf_ptr, 99 uint32_t sizeWords); 100 101 102 /***********************************************************************/ 103 /** 104 * @brief This function converts big endianness bytes array to aligned words 105 * array with words order according to little endian. 106 * 107 * 1. Assumed, that input bytes order is set according 108 * to big endianness: MS Byte is most left, i.e. order is from 109 * Msb to Lsb. 110 * 2. Output words array should set according to 111 * little endianness words order: LSWord is most left, i.e. order 112 * is from Lsw to Msw. Order bytes in each word - according to 113 * processor endianness. 114 * 3. Owerlapping of buffers is not allowed, besides in 115 * place operation and size aligned to full words. 116 * 4. Implementation is given for both big and little 117 * endianness of processor. 118 * 119 * @param[out] out32_ptr - The 32-bits pointer to output buffer. 120 * @param[in] sizeOutBuffBytes - The size in bytes of output buffer, must be 121 * aligned to 4 bytes and not less than inpSizeInBytes. 122 * @param[in] in8_ptr - The pointer to input buffer. 123 * @param[in] inpSizeInBytes - The size in bytes of input data, where 124 * 0 < inpSizeInBytes < UINT32_MAX - CC_32BIT_WORD_SIZE. 125 * 126 * @return CCError_t - On success CC_OK is returned, on failure a 127 * value MODULE_* as defined in . 128 */ 129 CCError_t CC_CommonConvertMsbLsbBytesToLswMswWords( 130 uint32_t *out32_ptr, 131 uint32_t sizeOutBuffBytes, 132 const uint8_t *in8_ptr, 133 uint32_t inpSizeInBytes); 134 135 136 /***********************************************************************/ 137 /** 138 * @brief This function converts LE 32bit-words array to BE bytes array. 139 * 140 * 1. Assumed, that output bytes order is according 141 * to big endianness: MS Byte is most left, i.e. order is from 142 * Msb to Lsb. 143 * 2. Input words array should be set according to 144 * little endianness words order: LSWord is most left, i.e. order 145 * is from Lsw to Msw. Bytes order in each word - according to 146 * processor endianness. 147 * 3. Owerlapping of buffers is not allowed, besides in 148 * place operation and size aligned to full words. 149 * 4. Implementation is given for both big and little 150 * endianness of processor. 151 * 152 * @param[in] out32_ptr - The 32-bits pointer to output buffer. 153 * @param[in] sizeOutBuffBytes - The size in bytes of output buffer, must be 154 * not less than sizeInBytes. 155 * @param[out] in8_ptr - The pointer to input buffer. 156 * @param[in] sizeInBytes - The size in bytes. The size must be not 0 and 157 * aligned to 4 bytes word. 158 * 159 * @return CCError_t - On success CC_OK is returned, on failure a 160 * value MODULE_* as defined in . 161 */ 162 CCError_t CC_CommonConvertLswMswWordsToMsbLsbBytes( 163 uint8_t *out8_ptr, 164 size_t sizeOutBuffBytes, 165 uint32_t *in32_ptr, 166 uint32_t sizeInBytes); 167 168 169 /***********************************************************************/ 170 /** 171 * @brief VOS_GetGlobalData get the global random key hidden inside the function 172 * the global data implemented for now are random key buffer and AES secret key buffer 173 * 174 * When no_rtos is declared then we allow a global data. The random key/AES secret key are hidden as static inside the function 175 * 176 * 177 * @param[in] Globalid select the buffer 178 * @param[in] GlobalDataSizeWords - the global data buffer size needed in words - this value must be a predetermined value 179 * @param[out] GlobalData_ptr - Pointer to the global buffer returned. The buffer must be at least GlobalDataSizeWords size 180 * 181 * @return CCError_t - On success CC_OK is returned, on failure an Error as defined in VOS_error 182 */ 183 CCError_t CC_CommonGetGlobalData(uint16_t Globalid, uint32_t *GlobalData_ptr, uint16_t GlobalDataSizeWords); 184 185 186 /***********************************************************************/ 187 /** 188 * @brief CC_CommonStoreGlobalData store the global random key into the global buffer hidden inside the function 189 * the global data implemented for now are random key buffer and AES secret key buffer 190 * 191 * 192 * @param[in] Globalid - random key / AES secret key 193 * @param[in] GlobalDataSizeWords - the global data buffer size needed in words - this value must be a predetermined value 194 * @param[in] GlobalData_ptr - Pointer to the global buffer to be saved. The buffer must be at least GlobalDataSizeWords size 195 * 196 * Return Value: 197 */ 198 CCError_t CC_CommonStoreGlobalData(uint16_t Globalid, uint32_t *GlobalData_ptr, uint16_t GlobalDataSizeWords); 199 200 201 /***********************************************************************/ 202 /** 203 * @brief The CC_CommonCutAndSaveEndOfLliData() function saves the data from end of source 204 * memory, pointed by LLI table, to destination memory, and decreases the LLI table accordingly. 205 * 206 * The function executes the following major steps: 207 * 208 * 1. Starts copy bytes from last byte of last chunk of source LLI table into 209 * last byte of destination memory. 210 * 2. Continues copy bytes in reverse order while not completes copying of all amount of data. 211 * 3. If last chunk of source or destination data is not enough, the function crosses 212 * to next chunk of LLI table. 213 * 4. Decreases the Data size of last updated LLI entry and sets the LAST bit. 214 * 5. Exits with the OK code. 215 * 216 * 217 * @param[in] SrcLliTab_ptr - The pointer to the LLI table, containing pointers and sizes of 218 * chunks of source data. The table need to be aligned and placed 219 * in SEP SRAM. 220 * @param[in] SrcLliTabSize_ptr - The pointer to buffer, containing th size of the LLI table in words. 221 * @param[in] Dest_ptr - The destination address for copying the data. 222 * @param[in] DataSize - The count of bytes to copy. 223 * 224 * @return CCError_t - On success CC_OK is returned, 225 * - CC_COMMON_ERROR_IN_SAVING_LLI_DATA_ERROR 226 * 227 * NOTE: 1. Because the function is intended for internal using, it is presumed that all input parameters 228 * are valid. 229 * 2. Assumed, that copied source not may to take more than two last chunks of source memory. 230 */ 231 CCError_t CC_CommonCutAndSaveEndOfLliData( 232 uint32_t *SrcLliTab_ptr, 233 uint32_t *SrcLliTabSize_ptr, 234 uint8_t *Dst_ptr, 235 uint32_t DataSize); 236 237 /***********************************************************************/ 238 /** 239 * @brief The CC_CommonCutAndSaveBeginOfLliData() function saves the data from beginning of source 240 * memory, pointed by LLI table, to destination memory, and decreases the LLI table accordingly. 241 * 242 * The function executes the following major steps: 243 * 244 * 1. Starts copy bytes from first byte of first chunk of source LLI table into 245 * destination memory. 246 * 2. If first chunk of source is not enough, the function crosses 247 * to next chunk of LLI table. 248 * 3. Updates LLI table pointer and size according to copied amount of data. 249 * 5. Exits with the OK code. 250 * 251 * @param[in/out] SrcLliTab_ptr_ptr - The pointer to pointer to the LLI table, containing pointers and 252 * sizes of the chunks of source data. The table need to be aligned and 253 * placed in SRAM. 254 * @param[in/out] SrcLliTabSize_ptr - The pointer to buffer, containing th size of the LLI table in words. 255 * @param[in] Dest_ptr - The destination address for copying the data. 256 * @param[in] DataSize - The count of bytes to copy. 257 * 258 * @return - no return value. 259 * 260 * NOTE: 1. Because the function is intended for internal using, it is presumed that all input parameters 261 * are valid. 262 * 2. Assumed, that copied source not may to take more than two first chunks of source memory. 263 */ 264 void CC_CommonCutAndSaveBeginOfLliData( 265 uint32_t **SrcLliTab_ptr_ptr, 266 uint32_t *SrcLliTabSize_ptr, 267 uint8_t *Dst_ptr, 268 uint32_t DataSize); 269 270 /***********************************************************************/ 271 /** 272 * @brief This function converts 32-bit words array with little endian 273 * order of words to bytes array with little endian (LE) order of bytes. 274 * 275 * Assuming: no buffers overlapping, in/out pointers and sizes not equall to NULL, 276 the buffer size must be not less, than input data size. 277 * 278 * @param[out] out8Le - The bytes pointer to output buffer. 279 * @param[in] in32Le - The pointer to input 32-bit words buffer. 280 * @param[in] sizeInWords - The size in words of input data (sizeWords >= 0). 281 * 282 * @return CCError_t - On success CC_OK is returned, on failure a 283 * value MODULE_* as defined in . 284 */ 285 void CC_CommonConvertLswMswWordsToLsbMsbBytes( 286 uint8_t *out8Le, 287 const uint32_t *in32Le, 288 size_t sizeInWords); 289 290 291 /***********************************************************************/ 292 /** 293 * @brief This function converts bytes array with little endian (LE) order of 294 * bytes to 32-bit words array with little endian order of words and bytes. 295 * 296 * Assuming: No owerlapping of buffers; in/out pointers and sizes are not equall to NULL. 297 * If is in-place conversion, then the size must be multiple of 4 bytes. 298 * @param[out] out32Le - The 32-bits pointer to output buffer. The buffer size must be 299 * not less, than input data size. 300 * @param[in] in8Le - The pointer to input buffer. 301 * @param[in] sizeInBytes - The size in bytes of input data(sizeBytes > 0). 302 * 303 * @return CCError_t - On success CC_OK is returned, on failure a 304 * value MODULE_* as defined in . 305 */ 306 void CC_CommonConvertLsbMsbBytesToLswMswWords( 307 uint32_t *out32Le, 308 const uint8_t *in8Le, 309 size_t sizeInBytes); 310 311 312 #ifdef __cplusplus 313 } 314 #endif 315 316 #endif 317 318 319