1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd 4 */ 5 6 #ifndef _ROCKCHIP_CRYPTO_V2_UTIL_H_ 7 #define _ROCKCHIP_CRYPTO_V2_UTIL_H_ 8 9 #ifdef __cplusplus 10 extern "C" 11 { 12 #endif 13 14 /************************ Defines ******************************/ 15 16 /* invers the bytes on a word- used for output from HASH */ 17 #ifdef BIG__ENDIAN 18 #define UTIL_INVERSE_UINT32_BYTES(val) (val) 19 #else 20 #define UTIL_INVERSE_UINT32_BYTES(val) \ 21 (((val) >> 24) | (((val) & 0x00FF0000) >> 8) | \ 22 (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24)) 23 #endif 24 25 /* invers the bytes on a word - used for input data for HASH */ 26 #ifdef BIG__ENDIAN 27 #define UTIL_REVERT_UINT32_BYTES(val) \ 28 (((val) >> 24) | (((val) & 0x00FF0000) >> 8) | \ 29 (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24)) 30 #else 31 #define UTIL_REVERT_UINT32_BYTES(val) (val) 32 #endif 33 34 /* the minimum and maximum macros */ 35 #undef min 36 #define min(a, b) (((a) < (b)) ? (a) : (b)) 37 38 #undef max 39 #define max(a, b) (((a) > (b)) ? (a) : (b)) 40 41 /* MACRO to count one bits */ 42 #define COUNT_ONE_BITS(number, bit_count) \ 43 do { \ 44 u32 tmp_num = number; \ 45 bit_count = 0; \ 46 while (tmp_num) { \ 47 tmp_num = tmp_num & (tmp_num - 1); \ 48 bit_count = bit_count + 1; \ 49 } \ 50 } while (0) 51 52 /* ------------------------------------------------------------ 53 ** 54 * @brief This function executes a reversed words copy on a specified buffer. 55 * 56 * on a 6 words byffer: 57 * 58 * buff[5] <---> buff[0] 59 * buff[4] <---> buff[1] 60 * buff[3] <---> buff[2] 61 * 62 * @param[in] dst_ptr - The counter buffer. 63 * @param[in] size - The counter size in words. 64 * 65 */ 66 void util_reverse_words_buff(u32 *buff_ptr, u32 size_words); 67 68 /* ------------------------------------------------------------ 69 ** 70 * @brief This function executes a reversed byte copy on a specified buffer. 71 * 72 * on a 6 byte byffer: 73 * 74 * buff[5] <---> buff[0] 75 * buff[4] <---> buff[1] 76 * buff[3] <---> buff[2] 77 * 78 * @param[in] dst_ptr - The counter buffer. 79 * @param[in] src_ptr - The counter size in bytes. 80 * 81 */ 82 void util_reverse_buff(u8 *buff_ptr, u32 size); 83 84 /* ------------------------------------------------------------ 85 ** 86 * @brief This function executes a memory copy between 2 buffers. 87 * 88 * @param[in] dst_ptr - The first counter buffer. 89 * @param[in] src_ptr - The second counter buffer. 90 * @param[in] size - The counter size in words. 91 * 92 */ 93 void util_word_memcpy(u32 *dst_ptr, u32 *src_ptr, u32 size); 94 95 /* ------------------------------------------------------------ 96 ** 97 * @brief This function executes a memory set operation on a buffer. 98 * 99 * @param[in] buff_ptr - the buffer. 100 * @param[in] val - The value to set the buffer. 101 * @param[in] size - the buffers size in words. 102 * 103 */ 104 void util_word_memset(u32 *buff_ptr, u32 val, u32 size); 105 106 /* ------------------------------------------------------------ 107 ** 108 * @brief This function executes a reverse bytes copying from one buffer 109 * to another buffer. 110 * 111 * @param[in] dst_ptr - The pointer to destination buffer. 112 * @param[in] src_ptr - The pointer to source buffer. 113 * @param[in] size - The size in words. 114 * 115 */ 116 void util_reverse_word_memcpy(u32 *dst_ptr, u32 *src_ptr, u32 size); 117 118 /* ------------------------------------------------------------ 119 ** 120 * @brief This function executes a reverse bytes copying from one buffer 121 * to another buffer. 122 * 123 * @param[in] dst_ptr - The pointer to destination buffer. 124 * @param[in] src_ptr - The pointer to source buffer. 125 * @param[in] size - The size in bytes. 126 * 127 */ 128 void util_reverse_memcpy(u8 *dst_ptr, const u8 *src_ptr, u32 size); 129 130 #ifdef __cplusplus 131 } 132 #endif 133 134 #endif 135 136