Welcome to mirror list, hosted at ThFree Co, Russian Federation.

macros.h « Common « Inc « cryptographic « ble « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 167b5aaa8768fe3b2c4f0c3978532443997707ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
  ******************************************************************************
  * @file    macros.h
  * @author  MCD Application Team
  * @brief   Support Macros for functions of the Cryptolib
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2015 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Image license SLA0044,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        http://www.st.com/SLA0044
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CRL_MACROS_H__
#define __CRL_MACROS_H__

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
/* Exported types --------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/** @addtogroup InternalAPI Internal functions
  * @{
  */

/** @addtogroup Macros Macros
  * @{
  */

/**
  * @brief  This macro returns the number of 32-bit words required
  *                    to store a variable whose byte size is passed in input
  * @param  b Number of bytes
  * @retval Number of words required to store a variable of size b
  */
#define WORD_COUNT(b) ((int32_t) ((uint32_t) b + 3u) / 4u)

/**
  * @brief  This macro returns the maximum value between two inputs,
  *                    the evaluation is done with the ">" operator
  * @param  a  First input
  * @param  b  Second input
  * @retval Max(a,b)
  */
#define MAX(a,b)        ((((uint32_t)(a)) > ((uint32_t)(b)))  ? (a)  : (b))

/**
  * @brief  This macro returns the minumum value between two inputs,
  *                     the evaluation is done with the ">" operator
  * @param  a  First input
  * @param  b  Second input
  * @retval Min(a,b)
  */
#define MIN(a,b)        ((((uint32_t)(a)) > ((uint32_t)(b)))  ? (b)  : (a))


/**
  * @brief  This macro is used to rotate left a 32bit variable by bits bits
  * @param  value The 32 bit value to rotate left
  * @param  bits  The number of bits to rotate left
  * @retval Roteted 32 bit value
  */
#ifdef CL_ON_STM32
#define ROL(value, bits) (uint32_t) __ROR((value),32 - (bits))
#else
#define ROL(value, bits) (uint32_t) (((value) << (bits)) | ((value) >> (32 - (bits))))
#endif

/**
  * @brief  This macro is used to rotate right a 32bit variable by bits bits
  * @param  value The 32 bit value to rotate right
  * @param  bits  The number of bits to rotate right
  * @retval Roteted 32 bit value
  */
#ifdef CL_ON_STM32
#define ROR(value, bits) (uint32_t) __ROR((value),(bits))
#else
#define ROR(value, bits) (uint32_t) (((value) >> (bits)) | ((value) << (32 - (bits))))
#endif

/**
  * @brief  This macro returns the least significant byte from an int32_t
  * @param  P_word The 32-bit from which the least significant byte is taken
  * @retval Least significant byte of P_word
  */
#define BYTE_3(P_word) (uint8_t) (P_word & 0xFFu)

/**
  * @brief  This macro returns the second least significant byte from an int32_t
  * @param  P_word The 32-bit from which the second least significant byte is taken
  * @retval Second least significant byte of P_word
  */
#define BYTE_2(P_word) (uint8_t) ((P_word >> 8) & 0xFFu)

/**
  * @brief  This macro returns the second most significant byte from an int32_t
  * @param  P_word The 32-bit from which the second most significant byte is taken
  * @retval Second most significant byte of P_word
  */
#define BYTE_1(P_word) (uint8_t) ((P_word >> 16) & 0xFFu)

/**
  * @brief  This macro returns the most significant byte from an int32_t
  * @param  P_word The 32-bit from which the most significant byte is taken
  * @retval Most significant byte of x
  */
#define BYTE_0(P_word) (uint8_t) ((P_word >> 24) & 0xFFu)

/**
  * @brief  This macro returns a byte within an word
  * @param   P_word The 32-bit from which the byte
  * @param   P_n The index of the byte to be taken, 0 = MSB, 3 = LSB
  * @retval  Selected P_n byte from P_word
  */
#define BYTE_X(P_word, P_n) (uint8_t) ((P_word >> (24 - (8 * (P_n)))) & 0xFFu)


/**
  * @brief   This macro outputs the 4 octects that form the input 32bit integer
  * @param   [in]    P_x The input 32bit integer
  * @param   [out]   P_a The least significant byte of P_x
  * @param   [out]   P_b The second least significant byte of P_x
  * @param   [out]   P_c The second most significant byte of P_x
  * @param   P_d The most significant byte of P_x
  * @retval  none
  */
#define WORD32_TO_WORD8(P_x,P_a,P_b,P_c,P_d) P_a=(uint8_t)BYTE_3(P_x),P_b=(uint8_t)BYTE_2(P_x),P_c=(uint8_t)BYTE_1(P_x),P_d=(uint8_t)BYTE_0(P_x)


/**
  * @brief   This macro returns an integer from 4 octects
  * @param   P_b0 The most significant byte of the resulting integer
  * @param   P_b1 The second most byte of the resulting integer
  * @param   P_b2 The second least byte of the resulting integer
  * @param   P_b3 The least byte of the resulting integer
  * @retval  The resulting 32bit integer formed by P_b0 || P_b1 || P_b2 || P_b3
  */
#define WORD8_TO_WORD32(P_b0, P_b1, P_b2, P_b3) (uint32_t) ((uint32_t)(P_b0) << 24 | (uint32_t)(P_b1) << 16 | (uint32_t)(P_b2) << 8 | (P_b3))

#if CRL_ENDIANNESS==1

/**
  * @brief   This macro is used on little endian cpus to convert a
  *                        4 byte array into a 32 bit integer or viceversa.
  * @param   VariableToConvert The 32 bits value to be converted
  * @retval  The converted 32 bits value
  */
#ifdef CL_ON_STM32
#define LE_CONVERT_W32(VariableToConvert) (uint32_t) (__REV(VariableToConvert))
#else
#define LE_CONVERT_W32(VariableToConvert) (uint32_t) ((ROL( (uint32_t) VariableToConvert,24) & 0xFF00FF00u ) | (ROL( (uint32_t) VariableToConvert,8) & 0x00FF00FFu ))
#endif

#elif CRL_ENDIANNESS==2

/**
  * @brief   This macro doesn't do anything. It exist for compatibility with
  *                        little endian cpus.
  * @param   VariableToConvert The 32 bit value to be converted
  * @retval  VariableToConvert as it was passed
  * @note This macro simply returns its argument as it is.
  */
#define LE_CONVERT_W32(VariableToConvert) (VariableToConvert)
#else  /* CRL_ENDIANNESS NOT SET */
#error "Please select endianness in config.h"
#endif /* #if CRL_ENDIANNESS */


/* Support macro for rand.c for big endian cpus */
#if CRL_ENDIANNESS==2

/**
  * @brief   This macro is used on big endian cpus to convert a
  *                        a 32 bit integer into a little endian array of 4 bytes.
  * @param   VariableToConvert The 32 bits value to be converted
  * @retval  The converted 32 bits value
  */
#define BE_CONVERT_W32(VariableToConvert) ((ROL(VariableToConvert,24)&0xFF00FF00) | (ROL(VariableToConvert,8)&0x00FF00FF))
#endif

#ifdef CRL_CPU_SUPPORT_MISALIGNED

/**
  * @brief   This macro is used to read a 4 bytes from a byte array and return
  *                        it as a 32-bit word, taking care of system endianness
  * @param   P_pInputBuffer Pointer to the input byte buffer
  * @param   P_index        Index of P_pInputBuffer where the reading should start
  * @retval  The 32 bits value
  * @note    This macro is for systems where unaligned reading is allowed
  */
#define BUFFER_2_W32(P_pInputBuffer,P_index)   (LE_CONVERT_W32(*((const uint32_t *) ((P_pInputBuffer)+(P_index)))))
/**
* @brief   This macro is used to read a 4 bytes from a byte array and return
*                        it as a 32-bit word with reversed endiannes
* @param   P_pInputBuffer Pointer to the input byte buffer
* @param   P_index        Index of P_pInputBuffer where the reading should start
* @retval  The 32 bits value
* @note    This macro is for systems where unaligned reading is allowed
*/
#define BUFFER_2_LEW32(P_pInputBuffer,P_index)   ((*((const uint32_t *) ((P_pInputBuffer)+(P_index)))))

/**
  * @brief   This macro is used to write a 32bit integer into a byte buffer,
  *                        taking care of system endianness
  * @param   [out]   P_pOutputBuffer Pointer to the output byte buffer
  * @param   [in]    P_index         Index of P_pOutputBuffer where the writing should start
  * @param   [in]    P_word          The 32-bit word that will be written
  * @retval  none
  * @note    This macro is for systems where unaligned writing is allowed
  */

#define W32_2_BUFFER(P_pOutputBuffer,P_index,P_word) *((uint32_t*)(P_pOutputBuffer+(P_index)))=LE_CONVERT_W32(P_word)
/**
* @brief   This macro is used to write a 32bit integer into a byte buffer,
*                        with reversed endiannes
* @param   [out]   P_pOutputBuffer Pointer to the output byte buffer
* @param   [in]    P_index         Index of P_pOutputBuffer where the writing should start
* @param   [in]    P_word          The 32-bit word that will be written
* @retval  none
* @note    This macro is for systems where unaligned writing is allowed
*/
#define LEW32_2_BUFFER(P_pOutputBuffer,P_index,P_word) *((uint32_t*)(P_pOutputBuffer+(P_index)))=(P_word)
#else

/**
  * @brief   This macro is used to read a 4 bytes from a byte array and return
  *                        it as a 32-bit word
  * @param   P_pInputBuffer Pointer to the input byte buffer
  * @param   P_index        Index of P_pInputBuffer where the reading should start
  * @retval  The 32 bits value
  * @note    This macro is for systems where unaligned reading is \b not allowed
  */
#define BUFFER_2_W32(P_pInputBuffer,P_index)   (WORD8_TO_WORD32(P_pInputBuffer[(P_index)],P_pInputBuffer[(P_index)+1],P_pInputBuffer[(P_index)+2],P_pInputBuffer[(P_index)+3]))

/**
* @brief   This macro is used to read a 4 bytes from a byte array and return
*                        it as a 32-bit word with reversed endiannes
* @param   P_pInputBuffer Pointer to the input byte buffer
* @param   P_index        Index of P_pInputBuffer where the reading should start
* @retval  The 32 bits value
* @note    This macro is for systems where unaligned reading is \b not allowed
*/
#define BUFFER_2_LEW32(P_pInputBuffer,P_index)   (WORD8_TO_WORD32((P_pInputBuffer)[(P_index)+3],(P_pInputBuffer)[(P_index)+2],(P_pInputBuffer)[(P_index)+1],(P_pInputBuffer)[(P_index)]))

/**
  * @brief   This macro is used to write a 32bit integer into a byte buffer,
  *                        taking care of system endianness
  * @param   [out]   P_pOutputBuffer Pointer to the output byte buffer
  * @param   [in]    P_index         Index of P_pOutputBuffer where the writing should start
  * @param   [in]    P_word          The 32-bit word that will be written
  * @retval  none
  * @note    This macro is for systems where unaligned writing is \b  not allowed
  */
#define W32_2_BUFFER(P_pOutputBuffer,P_index,P_word) WORD32_TO_WORD8((P_word),P_pOutputBuffer[(P_index)+3],P_pOutputBuffer[(P_index)+2],P_pOutputBuffer[(P_index)+1],P_pOutputBuffer[(P_index)])

/**
* @brief   This macro is used to write a 32bit integer into a byte buffer,
*                        with reversed endiannes
* @param   [out]   P_pOutputBuffer Pointer to the output byte buffer
* @param   [in]    P_index         Index of P_pOutputBuffer where the writing should start
* @param   [in]    P_word          The 32-bit word that will be written
* @retval  none
* @note    This macro is for systems where unaligned writing is \b  not allowed
*/
#define LEW32_2_BUFFER(P_pOutputBuffer,P_index,P_word) WORD32_TO_WORD8((P_word),(P_pOutputBuffer)[(P_index)],(P_pOutputBuffer)[(P_index)+1],(P_pOutputBuffer)[(P_index)+2],(P_pOutputBuffer)[(P_index)+3])


#endif

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif
/* Exported functions ------------------------------------------------------- */

#endif /*__CRL_MACROS_H__*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/