Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crys_common.h Source File

crys_common.h

00001 /**************************************************************************************
00002 * Copyright (c) 2016-2017, ARM Limited or its affiliates. All rights reserved         *
00003 *                                                                                     *
00004 * This file and the related binary are licensed under the following license:          *
00005 *                                                                                     *
00006 * ARM Object Code and Header Files License, v1.0 Redistribution.                      *
00007 *                                                                                     *
00008 * Redistribution and use of object code, header files, and documentation, without     *
00009 * modification, are permitted provided that the following conditions are met:         *
00010 *                                                                                     *
00011 * 1) Redistributions must reproduce the above copyright notice and the                *
00012 *    following disclaimer in the documentation and/or other materials                 *
00013 *    provided with the distribution.                                                  *
00014 *                                                                                     *
00015 * 2) Unless to the extent explicitly permitted by law, no reverse                     *
00016 *    engineering, decompilation, or disassembly of is permitted.                      *
00017 *                                                                                     *
00018 * 3) Redistribution and use is permitted solely for the purpose of                    *
00019 *    developing or executing applications that are targeted for use                   *
00020 *    on an ARM-based product.                                                         *
00021 *                                                                                     *
00022 * DISCLAIMER. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND                  *
00023 * CONTRIBUTORS "AS IS." ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT             *
00024 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT,        *
00025 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE          *
00026 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   *
00027 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED            *
00028 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR              *
00029 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF              *
00030 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING                *
00031 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS                  *
00032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                        *
00033 **************************************************************************************/
00034 
00035 
00036 
00037 
00038 #ifndef CRYS_COMMON_H
00039 #define CRYS_COMMON_H
00040 
00041 #include "crys_common_error.h"
00042 
00043 
00044 #ifdef __cplusplus
00045 extern "C"
00046 {
00047 #endif
00048 
00049 
00050 /************************ Defines ******************************/
00051 
00052 #define CRYS_AES_SECRET_KEY_SIZE_IN_WORDS           4
00053 
00054 /* the ROT13 definition - relevant only on SW low level engines compiled in the ROT mode */
00055 #define CRYS_COMMON_ROT_13_OFFSET                   13
00056 
00057 
00058 
00059 /************************ Enums ********************************/
00060 
00061 /************************ Typedefs  ****************************/
00062 
00063 /************************ Structs  *****************************/
00064 
00065 /************************ Public Variables *********************/
00066 
00067 /************************ Public Functions *********************/
00068 
00069 
00070 
00071 /***********************************************************************
00072  **
00073  * @brief This function executes a reverse bytes copying from one buffer to another buffer.
00074  *
00075  *        Overlapping of buffers is not allowed, excluding the case, when destination and source
00076  *        buffers are the same.
00077  *        Example of a 5 byte buffer:
00078  *
00079  *        dst_ptr[4] = src_ptr[0]
00080  *        dst_ptr[3] = src_ptr[1]
00081  *        dst_ptr[2] = src_ptr[2]
00082  *        dst_ptr[1] = src_ptr[3]
00083  *        dst_ptr[0] = src_ptr[4]
00084  *
00085  * @param[in] dst_ptr - The pointer to destination buffer.
00086  * @param[in] src_ptr - The pointer to source buffer.
00087  * @param[in] size    - The size in bytes.
00088  *
00089  */
00090  CRYSError_t  CRYS_COMMON_ReverseMemcpy( uint8_t *dst_ptr , uint8_t *src_ptr , uint32_t size );
00091 
00092 
00093 /***********************************************************************
00094  **
00095  * @brief This function converts aligned words array to bytes array/
00096  *
00097  *            1. Assumed, that input buffer is aligned to 4-bytes word and
00098  *               bytes order is set according to machine endianness.
00099  *            2. Output buffer receives data as bytes stream from LSB to MSB.
00100  *               For increasing performance on small buffers, the output data is given
00101  *               by rounded down pointer and alignment.
00102  *            3. This implementation is given for both Big and Little endian machines.
00103  *
00104  *
00105  * @param[in] in32_ptr - The pointer to aligned input buffer.
00106  * @param[in] out32_ptr - The 32-bits pointer to output buffer (rounded down to 4 bytes) .
00107  * @param[in] outAlignBits - The actual output data alignment;
00108  * @param[in] sizeWords - The size in words (sizeWords >= 1).
00109  *
00110  *  return - no return value.
00111  */
00112  void CRYS_COMMON_AlignedWordsArrayToBytes( uint32_t *in32_ptr , uint32_t *out32_ptr ,
00113                                             uint32_t outAlignBits, uint32_t sizeWords );
00114 
00115 /***********************************************************************/
00116  /**
00117   * @brief This function converts in place words byffer to bytes buffer with
00118   *        reversed endianity of output array.
00119   *
00120   *        The function can convert:
00121   *           - big endian bytes array to words array with little endian order
00122   *             of words and backward.
00123   *
00124   *      Note:
00125   *      1. Endianness of each word in words buffer should be set allways
00126   *      according to processor used.
00127   *      2. Implementation is given for both big and little endianness of
00128   *      processor.
00129   *
00130   * @param[in]  buf_ptr - The 32-bits pointer to input/output buffer.
00131   * @param[in]  sizeWords - The size in words (sizeWords > 0).
00132   *
00133   * @return - no return value.
00134   */
00135 void CRYS_COMMON_InPlaceConvertBytesWordsAndArrayEndianness(
00136                     uint32_t *buf_ptr,
00137                     uint32_t  sizeWords);
00138 
00139 
00140 /***********************************************************************/
00141   /**
00142   * @brief This function converts big endianness bytes array to aligned words
00143   *        array with words order according to little endian /
00144   *
00145   *            1. Assumed, that input bytes order is set according
00146   *           to big endianness: MS Byte is most left, i.e. order is from
00147   *           Msb to Lsb.
00148   *            2. Output words array should set according to
00149   *           little endianness words order: LSWord is most left, i.e. order
00150   *           is from Lsw to Msw. Order bytes in each word - according to
00151   *            3. Implementation is given for both big and little
00152   *           endianness of processor.
00153   *
00154   * @param[out] out32_ptr - The 32-bits pointer to output buffer.
00155   * @param[in] sizeOutBuffBytes - The size in bytes of output buffer, must be
00156   *            aligned to 4 bytes and not less than sizeInBytes.
00157   * @param[in] in8_ptr - The pointer to input buffer.
00158   * @param[in] sizeInBytes - The size in bytes of input data(sizeBytes >= 1).
00159   *
00160   * @return CRYSError_t - On success CRYS_OK is returned, on failure a
00161   *                        value MODULE_* as defined in .
00162   */
00163 CRYSError_t  CRYS_COMMON_ConvertMsbLsbBytesToLswMswWords(
00164                     uint32_t *out32_ptr,
00165                     uint32_t  sizeOutBuffBytes,
00166                     const uint8_t  *in8_ptr,
00167                     uint32_t  sizeInBytes);
00168 
00169 
00170 /***********************************************************************/
00171   /**
00172   * @brief This function converts LE 32bit-words array to BE bytes array.
00173   *
00174   *     Note: The function allows output full size of the data and also output
00175   *           without leading zeros, if the user gives appropriate exact output
00176   *           size < input size.
00177   *
00178   *     Assuming:
00179   *            1. Output bytes order is according to big endianness:
00180   *               MS Byte is most left, i.e. order is from Msb to Lsb.
00181   *            2. Input array words order is set according to
00182   *           little endianness words order: LSWord is most left, i.e. order
00183   *           is from Lsw to Msw. Bytes order in each word - according to
00184   *           processor endianness.
00185   *            3. Owerlapping of buffers is not allowed, besides in
00186   *           place operation and size aligned to full words.
00187   *            4. Implementation is given for both big and little
00188   *               endianness of processor.
00189   *
00190   * @param[in] out8_ptr - The bytes pointer to the output buffer.
00191   * @param[in] sizeOutBuffBytes - The size of the data in bytes to output; must
00192   *       be not less, than sizeInBytes.
00193   * @param[out] in32_ptr - The pointer to the input buffer.
00194   * @param[in] sizeInpBytes - The size of the input data in bytes. The size must
00195   *       be > 0 and aligned to 4 bytes.
00196   *
00197   * @return CRYSError_t - On success CRYS_OK is returned, on failure a
00198   *                        value MODULE_* as defined in .
00199   */
00200 CRYSError_t  CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes(
00201                     uint8_t  *out8_ptr,
00202                     uint32_t  sizeOutBytes,
00203                     uint32_t *in32_ptr,
00204                     uint32_t  sizeInpBytes);
00205 
00206 
00207 /***********************************************************************/
00208 /**
00209  * @brief VOS_GetGlobalData get the global random key hidden inside the function
00210  *  the global data implemented for now are random key buffer and AES secret key buffer
00211  *
00212  * When no_rtos is declared then we allow a global data. The random key/AES secret key are hidden as static inside the function
00213  *
00214  *
00215  * @param[in] Globalid     select the buffer
00216  * @param[in] GlobalDataSizeWords      - the global data buffer size needed in words - this value must be a predetermined value
00217  * @param[out] GlobalData_ptr - Pointer to the global buffer returned. The buffer must be at least GlobalDataSizeWords size
00218  *
00219  * @return CRYSError_t - On success CRYS_OK is returned, on failure an Error as defined in VOS_error
00220  */
00221 CRYSError_t  CRYS_COMMON_GetGlobalData(uint16_t Globalid, uint32_t *GlobalData_ptr, uint16_t GlobalDataSizeWords);
00222 
00223 
00224 /***********************************************************************/
00225 /**
00226 * @brief CRYS_COMMON_StoreGlobalData store the global random key into the global buffer hidden inside the function
00227 *   the global data implemented for now are random key buffer and AES secret key buffer
00228 *
00229 *
00230 * @param[in] Globalid      - random key / AES secret key
00231 * @param[in] GlobalDataSizeWords       - the global data buffer size needed in words - this value must be a predetermined value
00232 * @param[in] GlobalData_ptr - Pointer to the global buffer to be saved. The buffer must be at least GlobalDataSizeWords size
00233 *
00234 *   Return Value:
00235 */
00236 CRYSError_t  CRYS_COMMON_StoreGlobalData(uint16_t Globalid, uint32_t *GlobalData_ptr, uint16_t GlobalDataSizeWords);
00237 
00238 
00239 /***********************************************************************/
00240 /**
00241  * @brief The CRYS_COMMON_CutAndSaveEndOfLliData() function saves the data from end of source
00242  *        memory, pointed by LLI table, to destination memory, and decreases the LLI table accordingly.
00243  *
00244  *        The function executes the following major steps:
00245  *
00246  *        1. Starts copy bytes from last byte of last chunk of source LLI table into
00247  *           last byte of destination memory.
00248  *        2. Continues copy bytes in reverse order while not completes copying of all amount of data.
00249  *        3. If last chunk of source or destination data is not enough, the function crosses
00250  *           to next chunk of LLI table.
00251  *        4. Decreases the Data size of last updated LLI entry and sets the LAST bit.
00252  *        5. Exits with the OK code.
00253  *
00254  *
00255  * @param[in] SrcLliTab_ptr - The pointer to the LLI table, containing pointers and sizes of
00256  *                            chunks of source data. The table need to be aligned and placed
00257  *                            in SEP SRAM.
00258  * @param[in] SrcLliTabSize_ptr -   The pointer to buffer, containing th size of the LLI table in words.
00259  * @param[in] Dest_ptr  -  The destination address for copying the data.
00260  * @param[in] DataSize  -  The count of bytes to copy.
00261  *
00262  * @return CRYSError_t - On success CRYS_OK is returned,
00263  *                     - CRYS_COMMON_ERROR_IN_SAVING_LLI_DATA_ERROR
00264  *
00265  * NOTE: 1. Because the function is intended for internal using, it is presumed that all input parameters
00266  *          are valid.
00267  *       2. Assumed, that copied source not may to take more than two last chunks of source memory.
00268  */
00269  CRYSError_t   CRYS_COMMON_CutAndSaveEndOfLliData(
00270                                          uint32_t   *SrcLliTab_ptr,
00271                                          uint32_t   *SrcLliTabSize_ptr,
00272                                          uint8_t    *Dst_ptr,
00273                                          uint32_t    DataSize);
00274 
00275 /***********************************************************************/
00276 /**
00277  * @brief The CRYS_COMMON_CutAndSaveBeginOfLliData() function saves the data from beginning of source
00278  *        memory, pointed by LLI table, to destination memory, and decreases the LLI table accordingly.
00279  *
00280  *        The function executes the following major steps:
00281  *
00282  *        1. Starts copy bytes from first byte of first chunk of source LLI table into
00283  *           destination memory.
00284  *        2. If first chunk of source is not enough, the function crosses
00285  *           to next chunk of LLI table.
00286  *        3. Updates LLI table pointer and size according to copied amount of data.
00287  *        5. Exits with the OK code.
00288  *
00289  * @param[in/out] SrcLliTab_ptr_ptr - The pointer to pointer to the LLI table, containing pointers and
00290  *                            sizes of the chunks of source data. The table need to be aligned and
00291  *                            placed in SRAM.
00292  * @param[in/out] SrcLliTabSize_ptr -   The pointer to buffer, containing th size of the LLI table in words.
00293  * @param[in] Dest_ptr  -  The destination address for copying the data.
00294  * @param[in] DataSize  -  The count of bytes to copy.
00295  *
00296  * @return - no return value.
00297  *
00298  * NOTE: 1. Because the function is intended for internal using, it is presumed that all input parameters
00299  *          are valid.
00300  *       2. Assumed, that copied source not may to take more than two first chunks of source memory.
00301  */
00302  void  CRYS_COMMON_CutAndSaveBeginOfLliData(
00303                                          uint32_t  **SrcLliTab_ptr_ptr,
00304                                          uint32_t   *SrcLliTabSize_ptr,
00305                                          uint8_t    *Dst_ptr,
00306                                          uint32_t    DataSize);
00307 
00308 /***********************************************************************/
00309   /**
00310   * @brief This function converts 32-bit words array with little endian
00311   *        order of words to bytes array with little endian (LE) order of bytes.
00312   *
00313   *    Assuming: no buffers overlapping, in/out pointers and sizes not equall to NULL,
00314                  the buffer size must be not less, than input data size.
00315   *
00316   * @param[out] out8Le - The bytes pointer to output buffer.
00317   * @param[in] in32Le - The pointer to input 32-bit words buffer.
00318   * @param[in] sizeInWords - The size in words of input data (sizeWords >= 0).
00319   *
00320   * @return CRYSError_t - On success CRYS_OK is returned, on failure a
00321   *                        value MODULE_* as defined in .
00322   */
00323 void CRYS_COMMON_ConvertLswMswWordsToLsbMsbBytes(
00324                     uint8_t  *out8Le,
00325                     const uint32_t *in32Le,
00326                     size_t  sizeInWords);
00327 
00328 
00329 /***********************************************************************/
00330 /**
00331  * @brief This function converts bytes array with little endian (LE) order of
00332  *        bytes to 32-bit words array with little endian order of words and bytes.
00333  *
00334  *   Assuming:  No owerlapping of buffers; in/out pointers and sizes are not equall to NULL.
00335  *              If is in-place conversion, then the size must be multiple of 4 bytes.
00336  * @param[out] out32Le - The 32-bits pointer to output buffer. The buffer size must be
00337  *                       not less, than input data size.
00338  * @param[in] in8Le - The pointer to input buffer.
00339  * @param[in] sizeInBytes - The size in bytes of input data(sizeBytes > 0).
00340  *
00341  * @return CRYSError_t - On success CRYS_OK is returned, on failure a
00342  *                        value MODULE_* as defined in .
00343  */
00344 void CRYS_COMMON_ConvertLsbMsbBytesToLswMswWords(
00345                     uint32_t *out32Le,
00346                     const uint8_t  *in8Le,
00347                     size_t  sizeInBytes);
00348 
00349 
00350 /**
00351  * The function compares value of byte vector to null.
00352  *
00353  * @author reuvenl (6/20/2016)
00354  *
00355  * @param vect - a pointer to bytes vector.
00356  * @param sizeBytes - size of the vector.
00357  *
00358  * @return uint32_t - if vector's value iz zero, then returns 1, else - 0;
00359  */
00360 uint32_t  CRYS_COMMON_CheckIsVectorZero(uint8_t *vect, uint32_t sizeBytes);
00361 
00362 
00363 
00364 
00365 #ifdef __cplusplus
00366 }
00367 #endif
00368 
00369 #endif
00370 
00371