Mistake on this page?
Report an issue in GitHub or email us
crys_common.h
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 
35 
36 
37 
38 #ifndef CRYS_COMMON_H
39 #define CRYS_COMMON_H
40 
41 #include "crys_common_error.h"
42 
43 
44 #ifdef __cplusplus
45 extern "C"
46 {
47 #endif
48 
49 
50 /************************ Defines ******************************/
51 
52 #define CRYS_AES_SECRET_KEY_SIZE_IN_WORDS 4
53 
54 /* the ROT13 definition - relevant only on SW low level engines compiled in the ROT mode */
55 #define CRYS_COMMON_ROT_13_OFFSET 13
56 
57 
58 
59 /************************ Enums ********************************/
60 
61 /************************ Typedefs ****************************/
62 
63 /************************ Structs *****************************/
64 
65 /************************ Public Variables *********************/
66 
67 /************************ Public Functions *********************/
68 
69 
70 
71 /***********************************************************************
72  **
73  * @brief This function executes a reverse bytes copying from one buffer to another buffer.
74  *
75  * Overlapping of buffers is not allowed, excluding the case, when destination and source
76  * buffers are the same.
77  * Example of a 5 byte buffer:
78  *
79  * dst_ptr[4] = src_ptr[0]
80  * dst_ptr[3] = src_ptr[1]
81  * dst_ptr[2] = src_ptr[2]
82  * dst_ptr[1] = src_ptr[3]
83  * dst_ptr[0] = src_ptr[4]
84  *
85  * @param[in] dst_ptr - The pointer to destination buffer.
86  * @param[in] src_ptr - The pointer to source buffer.
87  * @param[in] size - The size in bytes.
88  *
89  */
90  CRYSError_t CRYS_COMMON_ReverseMemcpy( uint8_t *dst_ptr , uint8_t *src_ptr , uint32_t size );
91 
92 
93 /***********************************************************************
94  **
95  * @brief This function converts aligned words array to bytes array/
96  *
97  * 1. Assumed, that input buffer is aligned to 4-bytes word and
98  * bytes order is set according to machine endianness.
99  * 2. Output buffer receives data as bytes stream from LSB to MSB.
100  * For increasing performance on small buffers, the output data is given
101  * by rounded down pointer and alignment.
102  * 3. This implementation is given for both Big and Little endian machines.
103  *
104  *
105  * @param[in] in32_ptr - The pointer to aligned input buffer.
106  * @param[in] out32_ptr - The 32-bits pointer to output buffer (rounded down to 4 bytes) .
107  * @param[in] outAlignBits - The actual output data alignment;
108  * @param[in] sizeWords - The size in words (sizeWords >= 1).
109  *
110  * return - no return value.
111  */
112  void CRYS_COMMON_AlignedWordsArrayToBytes( uint32_t *in32_ptr , uint32_t *out32_ptr ,
113  uint32_t outAlignBits, uint32_t sizeWords );
114 
115 /***********************************************************************/
116  /**
117  * @brief This function converts in place words byffer to bytes buffer with
118  * reversed endianity of output array.
119  *
120  * The function can convert:
121  * - big endian bytes array to words array with little endian order
122  * of words and backward.
123  *
124  * Note:
125  * 1. Endianness of each word in words buffer should be set allways
126  * according to processor used.
127  * 2. Implementation is given for both big and little endianness of
128  * processor.
129  *
130  * @param[in] buf_ptr - The 32-bits pointer to input/output buffer.
131  * @param[in] sizeWords - The size in words (sizeWords > 0).
132  *
133  * @return - no return value.
134  */
135 void CRYS_COMMON_InPlaceConvertBytesWordsAndArrayEndianness(
136  uint32_t *buf_ptr,
137  uint32_t sizeWords);
138 
139 
140 /***********************************************************************/
141  /**
142  * @brief This function converts big endianness bytes array to aligned words
143  * array with words order according to little endian /
144  *
145  * 1. Assumed, that input bytes order is set according
146  * to big endianness: MS Byte is most left, i.e. order is from
147  * Msb to Lsb.
148  * 2. Output words array should set according to
149  * little endianness words order: LSWord is most left, i.e. order
150  * is from Lsw to Msw. Order bytes in each word - according to
151  * 3. Implementation is given for both big and little
152  * endianness of processor.
153  *
154  * @param[out] out32_ptr - The 32-bits pointer to output buffer.
155  * @param[in] sizeOutBuffBytes - The size in bytes of output buffer, must be
156  * aligned to 4 bytes and not less than sizeInBytes.
157  * @param[in] in8_ptr - The pointer to input buffer.
158  * @param[in] sizeInBytes - The size in bytes of input data(sizeBytes >= 1).
159  *
160  * @return CRYSError_t - On success CRYS_OK is returned, on failure a
161  * value MODULE_* as defined in .
162  */
163 CRYSError_t CRYS_COMMON_ConvertMsbLsbBytesToLswMswWords(
164  uint32_t *out32_ptr,
165  uint32_t sizeOutBuffBytes,
166  const uint8_t *in8_ptr,
167  uint32_t sizeInBytes);
168 
169 
170 /***********************************************************************/
171  /**
172  * @brief This function converts LE 32bit-words array to BE bytes array.
173  *
174  * Note: The function allows output full size of the data and also output
175  * without leading zeros, if the user gives appropriate exact output
176  * size < input size.
177  *
178  * Assuming:
179  * 1. Output bytes order is according to big endianness:
180  * MS Byte is most left, i.e. order is from Msb to Lsb.
181  * 2. Input array words order is set according to
182  * little endianness words order: LSWord is most left, i.e. order
183  * is from Lsw to Msw. Bytes order in each word - according to
184  * processor endianness.
185  * 3. Owerlapping of buffers is not allowed, besides in
186  * place operation and size aligned to full words.
187  * 4. Implementation is given for both big and little
188  * endianness of processor.
189  *
190  * @param[in] out8_ptr - The bytes pointer to the output buffer.
191  * @param[in] sizeOutBuffBytes - The size of the data in bytes to output; must
192  * be not less, than sizeInBytes.
193  * @param[out] in32_ptr - The pointer to the input buffer.
194  * @param[in] sizeInpBytes - The size of the input data in bytes. The size must
195  * be > 0 and aligned to 4 bytes.
196  *
197  * @return CRYSError_t - On success CRYS_OK is returned, on failure a
198  * value MODULE_* as defined in .
199  */
200 CRYSError_t CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes(
201  uint8_t *out8_ptr,
202  uint32_t sizeOutBytes,
203  uint32_t *in32_ptr,
204  uint32_t sizeInpBytes);
205 
206 
207 /***********************************************************************/
208 /**
209  * @brief VOS_GetGlobalData get the global random key hidden inside the function
210  * the global data implemented for now are random key buffer and AES secret key buffer
211  *
212  * When no_rtos is declared then we allow a global data. The random key/AES secret key are hidden as static inside the function
213  *
214  *
215  * @param[in] Globalid select the buffer
216  * @param[in] GlobalDataSizeWords - the global data buffer size needed in words - this value must be a predetermined value
217  * @param[out] GlobalData_ptr - Pointer to the global buffer returned. The buffer must be at least GlobalDataSizeWords size
218  *
219  * @return CRYSError_t - On success CRYS_OK is returned, on failure an Error as defined in VOS_error
220  */
221 CRYSError_t CRYS_COMMON_GetGlobalData(uint16_t Globalid, uint32_t *GlobalData_ptr, uint16_t GlobalDataSizeWords);
222 
223 
224 /***********************************************************************/
225 /**
226 * @brief CRYS_COMMON_StoreGlobalData store the global random key into the global buffer hidden inside the function
227 * the global data implemented for now are random key buffer and AES secret key buffer
228 *
229 *
230 * @param[in] Globalid - random key / AES secret key
231 * @param[in] GlobalDataSizeWords - the global data buffer size needed in words - this value must be a predetermined value
232 * @param[in] GlobalData_ptr - Pointer to the global buffer to be saved. The buffer must be at least GlobalDataSizeWords size
233 *
234 * Return Value:
235 */
236 CRYSError_t CRYS_COMMON_StoreGlobalData(uint16_t Globalid, uint32_t *GlobalData_ptr, uint16_t GlobalDataSizeWords);
237 
238 
239 /***********************************************************************/
240 /**
241  * @brief The CRYS_COMMON_CutAndSaveEndOfLliData() function saves the data from end of source
242  * memory, pointed by LLI table, to destination memory, and decreases the LLI table accordingly.
243  *
244  * The function executes the following major steps:
245  *
246  * 1. Starts copy bytes from last byte of last chunk of source LLI table into
247  * last byte of destination memory.
248  * 2. Continues copy bytes in reverse order while not completes copying of all amount of data.
249  * 3. If last chunk of source or destination data is not enough, the function crosses
250  * to next chunk of LLI table.
251  * 4. Decreases the Data size of last updated LLI entry and sets the LAST bit.
252  * 5. Exits with the OK code.
253  *
254  *
255  * @param[in] SrcLliTab_ptr - The pointer to the LLI table, containing pointers and sizes of
256  * chunks of source data. The table need to be aligned and placed
257  * in SEP SRAM.
258  * @param[in] SrcLliTabSize_ptr - The pointer to buffer, containing th size of the LLI table in words.
259  * @param[in] Dest_ptr - The destination address for copying the data.
260  * @param[in] DataSize - The count of bytes to copy.
261  *
262  * @return CRYSError_t - On success CRYS_OK is returned,
263  * - CRYS_COMMON_ERROR_IN_SAVING_LLI_DATA_ERROR
264  *
265  * NOTE: 1. Because the function is intended for internal using, it is presumed that all input parameters
266  * are valid.
267  * 2. Assumed, that copied source not may to take more than two last chunks of source memory.
268  */
269  CRYSError_t CRYS_COMMON_CutAndSaveEndOfLliData(
270  uint32_t *SrcLliTab_ptr,
271  uint32_t *SrcLliTabSize_ptr,
272  uint8_t *Dst_ptr,
273  uint32_t DataSize);
274 
275 /***********************************************************************/
276 /**
277  * @brief The CRYS_COMMON_CutAndSaveBeginOfLliData() function saves the data from beginning of source
278  * memory, pointed by LLI table, to destination memory, and decreases the LLI table accordingly.
279  *
280  * The function executes the following major steps:
281  *
282  * 1. Starts copy bytes from first byte of first chunk of source LLI table into
283  * destination memory.
284  * 2. If first chunk of source is not enough, the function crosses
285  * to next chunk of LLI table.
286  * 3. Updates LLI table pointer and size according to copied amount of data.
287  * 5. Exits with the OK code.
288  *
289  * @param[in/out] SrcLliTab_ptr_ptr - The pointer to pointer to the LLI table, containing pointers and
290  * sizes of the chunks of source data. The table need to be aligned and
291  * placed in SRAM.
292  * @param[in/out] SrcLliTabSize_ptr - The pointer to buffer, containing th size of the LLI table in words.
293  * @param[in] Dest_ptr - The destination address for copying the data.
294  * @param[in] DataSize - The count of bytes to copy.
295  *
296  * @return - no return value.
297  *
298  * NOTE: 1. Because the function is intended for internal using, it is presumed that all input parameters
299  * are valid.
300  * 2. Assumed, that copied source not may to take more than two first chunks of source memory.
301  */
302  void CRYS_COMMON_CutAndSaveBeginOfLliData(
303  uint32_t **SrcLliTab_ptr_ptr,
304  uint32_t *SrcLliTabSize_ptr,
305  uint8_t *Dst_ptr,
306  uint32_t DataSize);
307 
308 /***********************************************************************/
309  /**
310  * @brief This function converts 32-bit words array with little endian
311  * order of words to bytes array with little endian (LE) order of bytes.
312  *
313  * Assuming: no buffers overlapping, in/out pointers and sizes not equall to NULL,
314  the buffer size must be not less, than input data size.
315  *
316  * @param[out] out8Le - The bytes pointer to output buffer.
317  * @param[in] in32Le - The pointer to input 32-bit words buffer.
318  * @param[in] sizeInWords - The size in words of input data (sizeWords >= 0).
319  *
320  * @return CRYSError_t - On success CRYS_OK is returned, on failure a
321  * value MODULE_* as defined in .
322  */
323 void CRYS_COMMON_ConvertLswMswWordsToLsbMsbBytes(
324  uint8_t *out8Le,
325  const uint32_t *in32Le,
326  size_t sizeInWords);
327 
328 
329 /***********************************************************************/
330 /**
331  * @brief This function converts bytes array with little endian (LE) order of
332  * bytes to 32-bit words array with little endian order of words and bytes.
333  *
334  * Assuming: No owerlapping of buffers; in/out pointers and sizes are not equall to NULL.
335  * If is in-place conversion, then the size must be multiple of 4 bytes.
336  * @param[out] out32Le - The 32-bits pointer to output buffer. The buffer size must be
337  * not less, than input data size.
338  * @param[in] in8Le - The pointer to input buffer.
339  * @param[in] sizeInBytes - The size in bytes of input data(sizeBytes > 0).
340  *
341  * @return CRYSError_t - On success CRYS_OK is returned, on failure a
342  * value MODULE_* as defined in .
343  */
344 void CRYS_COMMON_ConvertLsbMsbBytesToLswMswWords(
345  uint32_t *out32Le,
346  const uint8_t *in8Le,
347  size_t sizeInBytes);
348 
349 
350 /**
351  * The function compares value of byte vector to null.
352  *
353  * @author reuvenl (6/20/2016)
354  *
355  * @param vect - a pointer to bytes vector.
356  * @param sizeBytes - size of the vector.
357  *
358  * @return uint32_t - if vector's value iz zero, then returns 1, else - 0;
359  */
360 uint32_t CRYS_COMMON_CheckIsVectorZero(uint8_t *vect, uint32_t sizeBytes);
361 
362 
363 
364 
365 #ifdef __cplusplus
366 }
367 #endif
368 
369 #endif
370 
371 
uint32_t CRYSError_t
Definition: crys_error.h:253
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.