crys_common.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**************************************************************************************
  2. * Copyright (c) 2016-2017, ARM Limited or its affiliates. All rights reserved *
  3. * *
  4. * This file and the related binary are licensed under the following license: *
  5. * *
  6. * ARM Object Code and Header Files License, v1.0 Redistribution. *
  7. * *
  8. * Redistribution and use of object code, header files, and documentation, without *
  9. * modification, are permitted provided that the following conditions are met: *
  10. * *
  11. * 1) Redistributions must reproduce the above copyright notice and the *
  12. * following disclaimer in the documentation and/or other materials *
  13. * provided with the distribution. *
  14. * *
  15. * 2) Unless to the extent explicitly permitted by law, no reverse *
  16. * engineering, decompilation, or disassembly of is permitted. *
  17. * *
  18. * 3) Redistribution and use is permitted solely for the purpose of *
  19. * developing or executing applications that are targeted for use *
  20. * on an ARM-based product. *
  21. * *
  22. * DISCLAIMER. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
  23. * CONTRIBUTORS "AS IS." ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT *
  24. * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, *
  25. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
  26. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED *
  28. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
  29. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
  33. **************************************************************************************/
  34. #ifndef CRYS_COMMON_H
  35. #define CRYS_COMMON_H
  36. #include "crys_common_error.h"
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. /************************ Defines ******************************/
  42. #define CRYS_AES_SECRET_KEY_SIZE_IN_WORDS 4
  43. /* the ROT13 definition - relevant only on SW low level engines compiled in the ROT mode */
  44. #define CRYS_COMMON_ROT_13_OFFSET 13
  45. /************************ Enums ********************************/
  46. /************************ Typedefs ****************************/
  47. /************************ Structs *****************************/
  48. /************************ Public Variables *********************/
  49. /************************ Public Functions *********************/
  50. /***********************************************************************
  51. **
  52. * @brief This function executes a reverse bytes copying from one buffer to another buffer.
  53. *
  54. * Overlapping of buffers is not allowed, excluding the case, when destination and source
  55. * buffers are the same.
  56. * Example of a 5 byte buffer:
  57. *
  58. * dst_ptr[4] = src_ptr[0]
  59. * dst_ptr[3] = src_ptr[1]
  60. * dst_ptr[2] = src_ptr[2]
  61. * dst_ptr[1] = src_ptr[3]
  62. * dst_ptr[0] = src_ptr[4]
  63. *
  64. * @param[in] dst_ptr - The pointer to destination buffer.
  65. * @param[in] src_ptr - The pointer to source buffer.
  66. * @param[in] size - The size in bytes.
  67. *
  68. */
  69. CRYSError_t CRYS_COMMON_ReverseMemcpy( uint8_t *dst_ptr , uint8_t *src_ptr , uint32_t size );
  70. /***********************************************************************
  71. **
  72. * @brief This function converts aligned words array to bytes array/
  73. *
  74. * 1. Assumed, that input buffer is aligned to 4-bytes word and
  75. * bytes order is set according to machine endianness.
  76. * 2. Output buffer receives data as bytes stream from LSB to MSB.
  77. * For increasing performance on small buffers, the output data is given
  78. * by rounded down pointer and alignment.
  79. * 3. This implementation is given for both Big and Little endian machines.
  80. *
  81. *
  82. * @param[in] in32_ptr - The pointer to aligned input buffer.
  83. * @param[in] out32_ptr - The 32-bits pointer to output buffer (rounded down to 4 bytes) .
  84. * @param[in] outAlignBits - The actual output data alignment;
  85. * @param[in] sizeWords - The size in words (sizeWords >= 1).
  86. *
  87. * return - no return value.
  88. */
  89. void CRYS_COMMON_AlignedWordsArrayToBytes( uint32_t *in32_ptr , uint32_t *out32_ptr ,
  90. uint32_t outAlignBits, uint32_t sizeWords );
  91. /***********************************************************************/
  92. /**
  93. * @brief This function converts in place words byffer to bytes buffer with
  94. * reversed endianity of output array.
  95. *
  96. * The function can convert:
  97. * - big endian bytes array to words array with little endian order
  98. * of words and backward.
  99. *
  100. * Note:
  101. * 1. Endianness of each word in words buffer should be set allways
  102. * according to processor used.
  103. * 2. Implementation is given for both big and little endianness of
  104. * processor.
  105. *
  106. * @param[in] buf_ptr - The 32-bits pointer to input/output buffer.
  107. * @param[in] sizeWords - The size in words (sizeWords > 0).
  108. *
  109. * @return - no return value.
  110. */
  111. void CRYS_COMMON_InPlaceConvertBytesWordsAndArrayEndianness(
  112. uint32_t *buf_ptr,
  113. uint32_t sizeWords);
  114. /***********************************************************************/
  115. /**
  116. * @brief This function converts big endianness bytes array to aligned words
  117. * array with words order according to little endian /
  118. *
  119. * 1. Assumed, that input bytes order is set according
  120. * to big endianness: MS Byte is most left, i.e. order is from
  121. * Msb to Lsb.
  122. * 2. Output words array should set according to
  123. * little endianness words order: LSWord is most left, i.e. order
  124. * is from Lsw to Msw. Order bytes in each word - according to
  125. * 3. Implementation is given for both big and little
  126. * endianness of processor.
  127. *
  128. * @param[out] out32_ptr - The 32-bits pointer to output buffer.
  129. * @param[in] sizeOutBuffBytes - The size in bytes of output buffer, must be
  130. * aligned to 4 bytes and not less than sizeInBytes.
  131. * @param[in] in8_ptr - The pointer to input buffer.
  132. * @param[in] sizeInBytes - The size in bytes of input data(sizeBytes >= 1).
  133. *
  134. * @return CRYSError_t - On success CRYS_OK is returned, on failure a
  135. * value MODULE_* as defined in .
  136. */
  137. CRYSError_t CRYS_COMMON_ConvertMsbLsbBytesToLswMswWords(
  138. uint32_t *out32_ptr,
  139. uint32_t sizeOutBuffBytes,
  140. const uint8_t *in8_ptr,
  141. uint32_t sizeInBytes);
  142. /***********************************************************************/
  143. /**
  144. * @brief This function converts LE 32bit-words array to BE bytes array.
  145. *
  146. * Note: The function allows output full size of the data and also output
  147. * without leading zeros, if the user gives appropriate exact output
  148. * size < input size.
  149. *
  150. * Assuming:
  151. * 1. Output bytes order is according to big endianness:
  152. * MS Byte is most left, i.e. order is from Msb to Lsb.
  153. * 2. Input array words order is set according to
  154. * little endianness words order: LSWord is most left, i.e. order
  155. * is from Lsw to Msw. Bytes order in each word - according to
  156. * processor endianness.
  157. * 3. Owerlapping of buffers is not allowed, besides in
  158. * place operation and size aligned to full words.
  159. * 4. Implementation is given for both big and little
  160. * endianness of processor.
  161. *
  162. * @param[in] out8_ptr - The bytes pointer to the output buffer.
  163. * @param[in] sizeOutBuffBytes - The size of the data in bytes to output; must
  164. * be not less, than sizeInBytes.
  165. * @param[out] in32_ptr - The pointer to the input buffer.
  166. * @param[in] sizeInpBytes - The size of the input data in bytes. The size must
  167. * be > 0 and aligned to 4 bytes.
  168. *
  169. * @return CRYSError_t - On success CRYS_OK is returned, on failure a
  170. * value MODULE_* as defined in .
  171. */
  172. CRYSError_t CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes(
  173. uint8_t *out8_ptr,
  174. uint32_t sizeOutBytes,
  175. uint32_t *in32_ptr,
  176. uint32_t sizeInpBytes);
  177. /***********************************************************************/
  178. /**
  179. * @brief VOS_GetGlobalData get the global random key hidden inside the function
  180. * the global data implemented for now are random key buffer and AES secret key buffer
  181. *
  182. * When no_rtos is declared then we allow a global data. The random key/AES secret key are hidden as static inside the function
  183. *
  184. *
  185. * @param[in] Globalid select the buffer
  186. * @param[in] GlobalDataSizeWords - the global data buffer size needed in words - this value must be a predetermined value
  187. * @param[out] GlobalData_ptr - Pointer to the global buffer returned. The buffer must be at least GlobalDataSizeWords size
  188. *
  189. * @return CRYSError_t - On success CRYS_OK is returned, on failure an Error as defined in VOS_error
  190. */
  191. CRYSError_t CRYS_COMMON_GetGlobalData(uint16_t Globalid, uint32_t *GlobalData_ptr, uint16_t GlobalDataSizeWords);
  192. /***********************************************************************/
  193. /**
  194. * @brief CRYS_COMMON_StoreGlobalData store the global random key into the global buffer hidden inside the function
  195. * the global data implemented for now are random key buffer and AES secret key buffer
  196. *
  197. *
  198. * @param[in] Globalid - random key / AES secret key
  199. * @param[in] GlobalDataSizeWords - the global data buffer size needed in words - this value must be a predetermined value
  200. * @param[in] GlobalData_ptr - Pointer to the global buffer to be saved. The buffer must be at least GlobalDataSizeWords size
  201. *
  202. * Return Value:
  203. */
  204. CRYSError_t CRYS_COMMON_StoreGlobalData(uint16_t Globalid, uint32_t *GlobalData_ptr, uint16_t GlobalDataSizeWords);
  205. /***********************************************************************/
  206. /**
  207. * @brief The CRYS_COMMON_CutAndSaveEndOfLliData() function saves the data from end of source
  208. * memory, pointed by LLI table, to destination memory, and decreases the LLI table accordingly.
  209. *
  210. * The function executes the following major steps:
  211. *
  212. * 1. Starts copy bytes from last byte of last chunk of source LLI table into
  213. * last byte of destination memory.
  214. * 2. Continues copy bytes in reverse order while not completes copying of all amount of data.
  215. * 3. If last chunk of source or destination data is not enough, the function crosses
  216. * to next chunk of LLI table.
  217. * 4. Decreases the Data size of last updated LLI entry and sets the LAST bit.
  218. * 5. Exits with the OK code.
  219. *
  220. *
  221. * @param[in] SrcLliTab_ptr - The pointer to the LLI table, containing pointers and sizes of
  222. * chunks of source data. The table need to be aligned and placed
  223. * in SEP SRAM.
  224. * @param[in] SrcLliTabSize_ptr - The pointer to buffer, containing th size of the LLI table in words.
  225. * @param[in] Dest_ptr - The destination address for copying the data.
  226. * @param[in] DataSize - The count of bytes to copy.
  227. *
  228. * @return CRYSError_t - On success CRYS_OK is returned,
  229. * - CRYS_COMMON_ERROR_IN_SAVING_LLI_DATA_ERROR
  230. *
  231. * NOTE: 1. Because the function is intended for internal using, it is presumed that all input parameters
  232. * are valid.
  233. * 2. Assumed, that copied source not may to take more than two last chunks of source memory.
  234. */
  235. CRYSError_t CRYS_COMMON_CutAndSaveEndOfLliData(
  236. uint32_t *SrcLliTab_ptr,
  237. uint32_t *SrcLliTabSize_ptr,
  238. uint8_t *Dst_ptr,
  239. uint32_t DataSize);
  240. /***********************************************************************/
  241. /**
  242. * @brief The CRYS_COMMON_CutAndSaveBeginOfLliData() function saves the data from beginning of source
  243. * memory, pointed by LLI table, to destination memory, and decreases the LLI table accordingly.
  244. *
  245. * The function executes the following major steps:
  246. *
  247. * 1. Starts copy bytes from first byte of first chunk of source LLI table into
  248. * destination memory.
  249. * 2. If first chunk of source is not enough, the function crosses
  250. * to next chunk of LLI table.
  251. * 3. Updates LLI table pointer and size according to copied amount of data.
  252. * 5. Exits with the OK code.
  253. *
  254. * @param[in/out] SrcLliTab_ptr_ptr - The pointer to pointer to the LLI table, containing pointers and
  255. * sizes of the chunks of source data. The table need to be aligned and
  256. * placed in SRAM.
  257. * @param[in/out] SrcLliTabSize_ptr - The pointer to buffer, containing th size of the LLI table in words.
  258. * @param[in] Dest_ptr - The destination address for copying the data.
  259. * @param[in] DataSize - The count of bytes to copy.
  260. *
  261. * @return - no return value.
  262. *
  263. * NOTE: 1. Because the function is intended for internal using, it is presumed that all input parameters
  264. * are valid.
  265. * 2. Assumed, that copied source not may to take more than two first chunks of source memory.
  266. */
  267. void CRYS_COMMON_CutAndSaveBeginOfLliData(
  268. uint32_t **SrcLliTab_ptr_ptr,
  269. uint32_t *SrcLliTabSize_ptr,
  270. uint8_t *Dst_ptr,
  271. uint32_t DataSize);
  272. /***********************************************************************/
  273. /**
  274. * @brief This function converts 32-bit words array with little endian
  275. * order of words to bytes array with little endian (LE) order of bytes.
  276. *
  277. * Assuming: no buffers overlapping, in/out pointers and sizes not equall to NULL,
  278. the buffer size must be not less, than input data size.
  279. *
  280. * @param[out] out8Le - The bytes pointer to output buffer.
  281. * @param[in] in32Le - The pointer to input 32-bit words buffer.
  282. * @param[in] sizeInWords - The size in words of input data (sizeWords >= 0).
  283. *
  284. * @return CRYSError_t - On success CRYS_OK is returned, on failure a
  285. * value MODULE_* as defined in .
  286. */
  287. void CRYS_COMMON_ConvertLswMswWordsToLsbMsbBytes(
  288. uint8_t *out8Le,
  289. const uint32_t *in32Le,
  290. size_t sizeInWords);
  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 CRYSError_t - On success CRYS_OK is returned, on failure a
  304. * value MODULE_* as defined in .
  305. */
  306. void CRYS_COMMON_ConvertLsbMsbBytesToLswMswWords(
  307. uint32_t *out32Le,
  308. const uint8_t *in8Le,
  309. size_t sizeInBytes);
  310. /**
  311. * The function compares value of byte vector to null.
  312. *
  313. * @author reuvenl (6/20/2016)
  314. *
  315. * @param vect - a pointer to bytes vector.
  316. * @param sizeBytes - size of the vector.
  317. *
  318. * @return uint32_t - if vector's value iz zero, then returns 1, else - 0;
  319. */
  320. uint32_t CRYS_COMMON_CheckIsVectorZero(uint8_t *vect, uint32_t sizeBytes);
  321. #ifdef __cplusplus
  322. }
  323. #endif
  324. #endif