ASCON Suite
ascon-util.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef ASCON_UTIL_H
24 #define ASCON_UTIL_H
25 
26 #include <stddef.h>
27 #include <stdint.h>
28 
29 /* Figure out how to inline functions using this C compiler */
30 #if defined(__STDC__) && __STDC_VERSION__ >= 199901L
31 #define STATIC_INLINE static inline
32 #elif defined(__GNUC__) || defined(__clang__)
33 #define STATIC_INLINE static __inline__
34 #else
35 #define STATIC_INLINE static
36 #endif
37 
38 /* Try to figure out whether the CPU is little-endian or big-endian.
39  * May need to modify this to include new compiler-specific defines.
40  * Alternatively, define __LITTLE_ENDIAN__ or __BIG_ENDIAN__ in your
41  * compiler flags when you compile this library */
42 #if defined(__x86_64) || defined(__x86_64__) || \
43  defined(__i386) || defined(__i386__) || \
44  defined(__AVR__) || defined(__arm) || defined(__arm__) || \
45  defined(_M_AMD64) || defined(_M_X64) || defined(_M_IX86) || \
46  defined(_M_IA64) || defined(_M_ARM) || defined(_M_ARM_FP) || \
47  (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == 1234) || \
48  defined(__LITTLE_ENDIAN__)
49 #define LW_UTIL_LITTLE_ENDIAN 1
50 #elif (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == 4321) || \
51  defined(__BIG_ENDIAN__) || defined(__m68k__)
52 /* Big endian */
53 #else
54 #error "Cannot determine the endianess of this platform"
55 #endif
56 
57 /* Determine if we are compiling for a 64-bit CPU */
58 #if defined(__x86_64) || defined(__x86_64__) || \
59  defined(__aarch64__) || defined(__ARM_ARCH_ISA_A64) || \
60  defined(_M_AMD64) || defined(_M_X64) || defined(_M_IA64) || \
61  (defined(__riscv) && __riscv_xlen == 64)
62 #define LW_UTIL_CPU_IS_64BIT 1
63 #endif
64 
65 /* Helper macros to load and store values while converting endian-ness */
66 
67 /* Load a big-endian 32-bit word from a byte buffer */
68 #define be_load_word32(ptr) \
69  ((((uint32_t)((ptr)[0])) << 24) | \
70  (((uint32_t)((ptr)[1])) << 16) | \
71  (((uint32_t)((ptr)[2])) << 8) | \
72  ((uint32_t)((ptr)[3])))
73 
74 /* Store a big-endian 32-bit word into a byte buffer */
75 #define be_store_word32(ptr, x) \
76  do { \
77  uint32_t _x = (x); \
78  (ptr)[0] = (uint8_t)(_x >> 24); \
79  (ptr)[1] = (uint8_t)(_x >> 16); \
80  (ptr)[2] = (uint8_t)(_x >> 8); \
81  (ptr)[3] = (uint8_t)_x; \
82  } while (0)
83 
84 /* Load a little-endian 32-bit word from a byte buffer */
85 #define le_load_word32(ptr) \
86  ((((uint32_t)((ptr)[3])) << 24) | \
87  (((uint32_t)((ptr)[2])) << 16) | \
88  (((uint32_t)((ptr)[1])) << 8) | \
89  ((uint32_t)((ptr)[0])))
90 
91 /* Store a little-endian 32-bit word into a byte buffer */
92 #define le_store_word32(ptr, x) \
93  do { \
94  uint32_t _x = (x); \
95  (ptr)[0] = (uint8_t)_x; \
96  (ptr)[1] = (uint8_t)(_x >> 8); \
97  (ptr)[2] = (uint8_t)(_x >> 16); \
98  (ptr)[3] = (uint8_t)(_x >> 24); \
99  } while (0)
100 
101 /* Reverses the bytes in a 32-bit word */
102 #define reverse_word32(x) \
103  (((x) >> 24) | (((x) >> 8) & 0x0000FF00U) | \
104  (((x) << 8) & 0x00FF0000U) | ((x) << 24))
105 
106 /* Load a big-endian 64-bit word from a byte buffer */
107 #define be_load_word64(ptr) \
108  ((((uint64_t)((ptr)[0])) << 56) | \
109  (((uint64_t)((ptr)[1])) << 48) | \
110  (((uint64_t)((ptr)[2])) << 40) | \
111  (((uint64_t)((ptr)[3])) << 32) | \
112  (((uint64_t)((ptr)[4])) << 24) | \
113  (((uint64_t)((ptr)[5])) << 16) | \
114  (((uint64_t)((ptr)[6])) << 8) | \
115  ((uint64_t)((ptr)[7])))
116 
117 /* Store a big-endian 64-bit word into a byte buffer */
118 #define be_store_word64(ptr, x) \
119  do { \
120  uint64_t _x = (x); \
121  (ptr)[0] = (uint8_t)(_x >> 56); \
122  (ptr)[1] = (uint8_t)(_x >> 48); \
123  (ptr)[2] = (uint8_t)(_x >> 40); \
124  (ptr)[3] = (uint8_t)(_x >> 32); \
125  (ptr)[4] = (uint8_t)(_x >> 24); \
126  (ptr)[5] = (uint8_t)(_x >> 16); \
127  (ptr)[6] = (uint8_t)(_x >> 8); \
128  (ptr)[7] = (uint8_t)_x; \
129  } while (0)
130 
131 /* Load a little-endian 64-bit word from a byte buffer */
132 #define le_load_word64(ptr) \
133  ((((uint64_t)((ptr)[7])) << 56) | \
134  (((uint64_t)((ptr)[6])) << 48) | \
135  (((uint64_t)((ptr)[5])) << 40) | \
136  (((uint64_t)((ptr)[4])) << 32) | \
137  (((uint64_t)((ptr)[3])) << 24) | \
138  (((uint64_t)((ptr)[2])) << 16) | \
139  (((uint64_t)((ptr)[1])) << 8) | \
140  ((uint64_t)((ptr)[0])))
141 
142 /* Store a little-endian 64-bit word into a byte buffer */
143 #define le_store_word64(ptr, x) \
144  do { \
145  uint64_t _x = (x); \
146  (ptr)[0] = (uint8_t)_x; \
147  (ptr)[1] = (uint8_t)(_x >> 8); \
148  (ptr)[2] = (uint8_t)(_x >> 16); \
149  (ptr)[3] = (uint8_t)(_x >> 24); \
150  (ptr)[4] = (uint8_t)(_x >> 32); \
151  (ptr)[5] = (uint8_t)(_x >> 40); \
152  (ptr)[6] = (uint8_t)(_x >> 48); \
153  (ptr)[7] = (uint8_t)(_x >> 56); \
154  } while (0)
155 
156 /* Load a big-endian 16-bit word from a byte buffer */
157 #define be_load_word16(ptr) \
158  ((((uint16_t)((ptr)[0])) << 8) | \
159  ((uint16_t)((ptr)[1])))
160 
161 /* Store a big-endian 16-bit word into a byte buffer */
162 #define be_store_word16(ptr, x) \
163  do { \
164  uint16_t _x = (x); \
165  (ptr)[0] = (uint8_t)(_x >> 8); \
166  (ptr)[1] = (uint8_t)_x; \
167  } while (0)
168 
169 /* Load a little-endian 16-bit word from a byte buffer */
170 #define le_load_word16(ptr) \
171  ((((uint16_t)((ptr)[1])) << 8) | \
172  ((uint16_t)((ptr)[0])))
173 
174 /* Store a little-endian 16-bit word into a byte buffer */
175 #define le_store_word16(ptr, x) \
176  do { \
177  uint16_t _x = (x); \
178  (ptr)[0] = (uint8_t)_x; \
179  (ptr)[1] = (uint8_t)(_x >> 8); \
180  } while (0)
181 
182 /* XOR a source byte buffer against a destination */
183 #define lw_xor_block(dest, src, len) \
184  do { \
185  unsigned char *_dest = (dest); \
186  const unsigned char *_src = (src); \
187  unsigned _len = (len); \
188  while (_len > 0) { \
189  *_dest++ ^= *_src++; \
190  --_len; \
191  } \
192  } while (0)
193 
194 /* XOR two source byte buffers and put the result in a destination buffer */
195 #define lw_xor_block_2_src(dest, src1, src2, len) \
196  do { \
197  unsigned char *_dest = (dest); \
198  const unsigned char *_src1 = (src1); \
199  const unsigned char *_src2 = (src2); \
200  unsigned _len = (len); \
201  while (_len > 0) { \
202  *_dest++ = *_src1++ ^ *_src2++; \
203  --_len; \
204  } \
205  } while (0)
206 
207 /* XOR a source byte buffer against a destination and write to another
208  * destination at the same time */
209 #define lw_xor_block_2_dest(dest2, dest, src, len) \
210  do { \
211  unsigned char *_dest2 = (dest2); \
212  unsigned char *_dest = (dest); \
213  const unsigned char *_src = (src); \
214  unsigned _len = (len); \
215  while (_len > 0) { \
216  *_dest2++ = (*_dest++ ^= *_src++); \
217  --_len; \
218  } \
219  } while (0)
220 
221 /* XOR two byte buffers and write to a destination which at the same
222  * time copying the contents of src2 to dest2 */
223 #define lw_xor_block_copy_src(dest2, dest, src1, src2, len) \
224  do { \
225  unsigned char *_dest2 = (dest2); \
226  unsigned char *_dest = (dest); \
227  const unsigned char *_src1 = (src1); \
228  const unsigned char *_src2 = (src2); \
229  unsigned _len = (len); \
230  while (_len > 0) { \
231  unsigned char _temp = *_src2++; \
232  *_dest2++ = _temp; \
233  *_dest++ = *_src1++ ^ _temp; \
234  --_len; \
235  } \
236  } while (0)
237 
238 /* XOR a source byte buffer against a destination and write to another
239  * destination at the same time. This version swaps the source value
240  * into the "dest" buffer */
241 #define lw_xor_block_swap(dest2, dest, src, len) \
242  do { \
243  unsigned char *_dest2 = (dest2); \
244  unsigned char *_dest = (dest); \
245  const unsigned char *_src = (src); \
246  unsigned _len = (len); \
247  while (_len > 0) { \
248  unsigned char _temp = *_src++; \
249  *_dest2++ = *_dest ^ _temp; \
250  *_dest++ = _temp; \
251  --_len; \
252  } \
253  } while (0)
254 
255 /* Rotation functions need to be optimised for best performance on AVR.
256  * The most efficient rotations are where the number of bits is 1 or a
257  * multiple of 8, so we compose the efficient rotations to produce all
258  * other rotation counts of interest. */
259 
260 #if defined(__AVR__)
261 #define LW_CRYPTO_ROTATE32_COMPOSED 1
262 #else
263 #define LW_CRYPTO_ROTATE32_COMPOSED 0
264 #endif
265 
266 /* Rotation macros for 32-bit arguments */
267 
268 /* Generic left rotate */
269 #define leftRotate(a, bits) \
270  (__extension__ ({ \
271  uint32_t _temp = (a); \
272  (_temp << (bits)) | (_temp >> (32 - (bits))); \
273  }))
274 
275 /* Generic right rotate */
276 #define rightRotate(a, bits) \
277  (__extension__ ({ \
278  uint32_t _temp = (a); \
279  (_temp >> (bits)) | (_temp << (32 - (bits))); \
280  }))
281 
282 #if !LW_CRYPTO_ROTATE32_COMPOSED
283 
284 /* Left rotate by a specific number of bits. These macros may be replaced
285  * with more efficient ones on platforms that lack a barrel shifter */
286 #define leftRotate1(a) (leftRotate((a), 1))
287 #define leftRotate2(a) (leftRotate((a), 2))
288 #define leftRotate3(a) (leftRotate((a), 3))
289 #define leftRotate4(a) (leftRotate((a), 4))
290 #define leftRotate5(a) (leftRotate((a), 5))
291 #define leftRotate6(a) (leftRotate((a), 6))
292 #define leftRotate7(a) (leftRotate((a), 7))
293 #define leftRotate8(a) (leftRotate((a), 8))
294 #define leftRotate9(a) (leftRotate((a), 9))
295 #define leftRotate10(a) (leftRotate((a), 10))
296 #define leftRotate11(a) (leftRotate((a), 11))
297 #define leftRotate12(a) (leftRotate((a), 12))
298 #define leftRotate13(a) (leftRotate((a), 13))
299 #define leftRotate14(a) (leftRotate((a), 14))
300 #define leftRotate15(a) (leftRotate((a), 15))
301 #define leftRotate16(a) (leftRotate((a), 16))
302 #define leftRotate17(a) (leftRotate((a), 17))
303 #define leftRotate18(a) (leftRotate((a), 18))
304 #define leftRotate19(a) (leftRotate((a), 19))
305 #define leftRotate20(a) (leftRotate((a), 20))
306 #define leftRotate21(a) (leftRotate((a), 21))
307 #define leftRotate22(a) (leftRotate((a), 22))
308 #define leftRotate23(a) (leftRotate((a), 23))
309 #define leftRotate24(a) (leftRotate((a), 24))
310 #define leftRotate25(a) (leftRotate((a), 25))
311 #define leftRotate26(a) (leftRotate((a), 26))
312 #define leftRotate27(a) (leftRotate((a), 27))
313 #define leftRotate28(a) (leftRotate((a), 28))
314 #define leftRotate29(a) (leftRotate((a), 29))
315 #define leftRotate30(a) (leftRotate((a), 30))
316 #define leftRotate31(a) (leftRotate((a), 31))
317 
318 /* Right rotate by a specific number of bits. These macros may be replaced
319  * with more efficient ones on platforms that lack a barrel shifter */
320 #define rightRotate1(a) (rightRotate((a), 1))
321 #define rightRotate2(a) (rightRotate((a), 2))
322 #define rightRotate3(a) (rightRotate((a), 3))
323 #define rightRotate4(a) (rightRotate((a), 4))
324 #define rightRotate5(a) (rightRotate((a), 5))
325 #define rightRotate6(a) (rightRotate((a), 6))
326 #define rightRotate7(a) (rightRotate((a), 7))
327 #define rightRotate8(a) (rightRotate((a), 8))
328 #define rightRotate9(a) (rightRotate((a), 9))
329 #define rightRotate10(a) (rightRotate((a), 10))
330 #define rightRotate11(a) (rightRotate((a), 11))
331 #define rightRotate12(a) (rightRotate((a), 12))
332 #define rightRotate13(a) (rightRotate((a), 13))
333 #define rightRotate14(a) (rightRotate((a), 14))
334 #define rightRotate15(a) (rightRotate((a), 15))
335 #define rightRotate16(a) (rightRotate((a), 16))
336 #define rightRotate17(a) (rightRotate((a), 17))
337 #define rightRotate18(a) (rightRotate((a), 18))
338 #define rightRotate19(a) (rightRotate((a), 19))
339 #define rightRotate20(a) (rightRotate((a), 20))
340 #define rightRotate21(a) (rightRotate((a), 21))
341 #define rightRotate22(a) (rightRotate((a), 22))
342 #define rightRotate23(a) (rightRotate((a), 23))
343 #define rightRotate24(a) (rightRotate((a), 24))
344 #define rightRotate25(a) (rightRotate((a), 25))
345 #define rightRotate26(a) (rightRotate((a), 26))
346 #define rightRotate27(a) (rightRotate((a), 27))
347 #define rightRotate28(a) (rightRotate((a), 28))
348 #define rightRotate29(a) (rightRotate((a), 29))
349 #define rightRotate30(a) (rightRotate((a), 30))
350 #define rightRotate31(a) (rightRotate((a), 31))
351 
352 #else /* LW_CRYPTO_ROTATE32_COMPOSED */
353 
354 /* Composed rotation macros where 1 and 8 are fast, but others are slow */
355 
356 /* Left rotate by 1 */
357 #define leftRotate1(a) (leftRotate((a), 1))
358 
359 /* Left rotate by 2 */
360 #define leftRotate2(a) (leftRotate(leftRotate((a), 1), 1))
361 
362 /* Left rotate by 3 */
363 #define leftRotate3(a) (leftRotate(leftRotate(leftRotate((a), 1), 1), 1))
364 
365 /* Left rotate by 4 */
366 #define leftRotate4(a) (leftRotate(leftRotate(leftRotate(leftRotate((a), 1), 1), 1), 1))
367 
368 /* Left rotate by 5: Rotate left by 8, then right by 3 */
369 #define leftRotate5(a) (rightRotate(rightRotate(rightRotate(leftRotate((a), 8), 1), 1), 1))
370 
371 /* Left rotate by 6: Rotate left by 8, then right by 2 */
372 #define leftRotate6(a) (rightRotate(rightRotate(leftRotate((a), 8), 1), 1))
373 
374 /* Left rotate by 7: Rotate left by 8, then right by 1 */
375 #define leftRotate7(a) (rightRotate(leftRotate((a), 8), 1))
376 
377 /* Left rotate by 8 */
378 #define leftRotate8(a) (leftRotate((a), 8))
379 
380 /* Left rotate by 9: Rotate left by 8, then left by 1 */
381 #define leftRotate9(a) (leftRotate(leftRotate((a), 8), 1))
382 
383 /* Left rotate by 10: Rotate left by 8, then left by 2 */
384 #define leftRotate10(a) (leftRotate(leftRotate(leftRotate((a), 8), 1), 1))
385 
386 /* Left rotate by 11: Rotate left by 8, then left by 3 */
387 #define leftRotate11(a) (leftRotate(leftRotate(leftRotate(leftRotate((a), 8), 1), 1), 1))
388 
389 /* Left rotate by 12: Rotate left by 16, then right by 4 */
390 #define leftRotate12(a) (rightRotate(rightRotate(rightRotate(rightRotate(leftRotate((a), 16), 1), 1), 1), 1))
391 
392 /* Left rotate by 13: Rotate left by 16, then right by 3 */
393 #define leftRotate13(a) (rightRotate(rightRotate(rightRotate(leftRotate((a), 16), 1), 1), 1))
394 
395 /* Left rotate by 14: Rotate left by 16, then right by 2 */
396 #define leftRotate14(a) (rightRotate(rightRotate(leftRotate((a), 16), 1), 1))
397 
398 /* Left rotate by 15: Rotate left by 16, then right by 1 */
399 #define leftRotate15(a) (rightRotate(leftRotate((a), 16), 1))
400 
401 /* Left rotate by 16 */
402 #define leftRotate16(a) (leftRotate((a), 16))
403 
404 /* Left rotate by 17: Rotate left by 16, then left by 1 */
405 #define leftRotate17(a) (leftRotate(leftRotate((a), 16), 1))
406 
407 /* Left rotate by 18: Rotate left by 16, then left by 2 */
408 #define leftRotate18(a) (leftRotate(leftRotate(leftRotate((a), 16), 1), 1))
409 
410 /* Left rotate by 19: Rotate left by 16, then left by 3 */
411 #define leftRotate19(a) (leftRotate(leftRotate(leftRotate(leftRotate((a), 16), 1), 1), 1))
412 
413 /* Left rotate by 20: Rotate left by 16, then left by 4 */
414 #define leftRotate20(a) (leftRotate(leftRotate(leftRotate(leftRotate(leftRotate((a), 16), 1), 1), 1), 1))
415 
416 /* Left rotate by 21: Rotate left by 24, then right by 3 */
417 #define leftRotate21(a) (rightRotate(rightRotate(rightRotate(leftRotate((a), 24), 1), 1), 1))
418 
419 /* Left rotate by 22: Rotate left by 24, then right by 2 */
420 #define leftRotate22(a) (rightRotate(rightRotate(leftRotate((a), 24), 1), 1))
421 
422 /* Left rotate by 23: Rotate left by 24, then right by 1 */
423 #define leftRotate23(a) (rightRotate(leftRotate((a), 24), 1))
424 
425 /* Left rotate by 24 */
426 #define leftRotate24(a) (leftRotate((a), 24))
427 
428 /* Left rotate by 25: Rotate left by 24, then left by 1 */
429 #define leftRotate25(a) (leftRotate(leftRotate((a), 24), 1))
430 
431 /* Left rotate by 26: Rotate left by 24, then left by 2 */
432 #define leftRotate26(a) (leftRotate(leftRotate(leftRotate((a), 24), 1), 1))
433 
434 /* Left rotate by 27: Rotate left by 24, then left by 3 */
435 #define leftRotate27(a) (leftRotate(leftRotate(leftRotate(leftRotate((a), 24), 1), 1), 1))
436 
437 /* Left rotate by 28: Rotate right by 4 */
438 #define leftRotate28(a) (rightRotate(rightRotate(rightRotate(rightRotate((a), 1), 1), 1), 1))
439 
440 /* Left rotate by 29: Rotate right by 3 */
441 #define leftRotate29(a) (rightRotate(rightRotate(rightRotate((a), 1), 1), 1))
442 
443 /* Left rotate by 30: Rotate right by 2 */
444 #define leftRotate30(a) (rightRotate(rightRotate((a), 1), 1))
445 
446 /* Left rotate by 31: Rotate right by 1 */
447 #define leftRotate31(a) (rightRotate((a), 1))
448 
449 /* Define the 32-bit right rotations in terms of left rotations */
450 #define rightRotate1(a) (leftRotate31((a)))
451 #define rightRotate2(a) (leftRotate30((a)))
452 #define rightRotate3(a) (leftRotate29((a)))
453 #define rightRotate4(a) (leftRotate28((a)))
454 #define rightRotate5(a) (leftRotate27((a)))
455 #define rightRotate6(a) (leftRotate26((a)))
456 #define rightRotate7(a) (leftRotate25((a)))
457 #define rightRotate8(a) (leftRotate24((a)))
458 #define rightRotate9(a) (leftRotate23((a)))
459 #define rightRotate10(a) (leftRotate22((a)))
460 #define rightRotate11(a) (leftRotate21((a)))
461 #define rightRotate12(a) (leftRotate20((a)))
462 #define rightRotate13(a) (leftRotate19((a)))
463 #define rightRotate14(a) (leftRotate18((a)))
464 #define rightRotate15(a) (leftRotate17((a)))
465 #define rightRotate16(a) (leftRotate16((a)))
466 #define rightRotate17(a) (leftRotate15((a)))
467 #define rightRotate18(a) (leftRotate14((a)))
468 #define rightRotate19(a) (leftRotate13((a)))
469 #define rightRotate20(a) (leftRotate12((a)))
470 #define rightRotate21(a) (leftRotate11((a)))
471 #define rightRotate22(a) (leftRotate10((a)))
472 #define rightRotate23(a) (leftRotate9((a)))
473 #define rightRotate24(a) (leftRotate8((a)))
474 #define rightRotate25(a) (leftRotate7((a)))
475 #define rightRotate26(a) (leftRotate6((a)))
476 #define rightRotate27(a) (leftRotate5((a)))
477 #define rightRotate28(a) (leftRotate4((a)))
478 #define rightRotate29(a) (leftRotate3((a)))
479 #define rightRotate30(a) (leftRotate2((a)))
480 #define rightRotate31(a) (leftRotate1((a)))
481 
482 #endif /* LW_CRYPTO_ROTATE32_COMPOSED */
483 
484 /* Rotation macros for 64-bit arguments */
485 
486 /* Generic left rotate */
487 #define leftRotate_64(a, bits) \
488  (__extension__ ({ \
489  uint64_t _temp = (a); \
490  (_temp << (bits)) | (_temp >> (64 - (bits))); \
491  }))
492 
493 /* Generic right rotate */
494 #define rightRotate_64(a, bits) \
495  (__extension__ ({ \
496  uint64_t _temp = (a); \
497  (_temp >> (bits)) | (_temp << (64 - (bits))); \
498  }))
499 
500 /* Left rotate by a specific number of bits. These macros may be replaced
501  * with more efficient ones on platforms that lack a barrel shifter */
502 #define leftRotate1_64(a) (leftRotate_64((a), 1))
503 #define leftRotate2_64(a) (leftRotate_64((a), 2))
504 #define leftRotate3_64(a) (leftRotate_64((a), 3))
505 #define leftRotate4_64(a) (leftRotate_64((a), 4))
506 #define leftRotate5_64(a) (leftRotate_64((a), 5))
507 #define leftRotate6_64(a) (leftRotate_64((a), 6))
508 #define leftRotate7_64(a) (leftRotate_64((a), 7))
509 #define leftRotate8_64(a) (leftRotate_64((a), 8))
510 #define leftRotate9_64(a) (leftRotate_64((a), 9))
511 #define leftRotate10_64(a) (leftRotate_64((a), 10))
512 #define leftRotate11_64(a) (leftRotate_64((a), 11))
513 #define leftRotate12_64(a) (leftRotate_64((a), 12))
514 #define leftRotate13_64(a) (leftRotate_64((a), 13))
515 #define leftRotate14_64(a) (leftRotate_64((a), 14))
516 #define leftRotate15_64(a) (leftRotate_64((a), 15))
517 #define leftRotate16_64(a) (leftRotate_64((a), 16))
518 #define leftRotate17_64(a) (leftRotate_64((a), 17))
519 #define leftRotate18_64(a) (leftRotate_64((a), 18))
520 #define leftRotate19_64(a) (leftRotate_64((a), 19))
521 #define leftRotate20_64(a) (leftRotate_64((a), 20))
522 #define leftRotate21_64(a) (leftRotate_64((a), 21))
523 #define leftRotate22_64(a) (leftRotate_64((a), 22))
524 #define leftRotate23_64(a) (leftRotate_64((a), 23))
525 #define leftRotate24_64(a) (leftRotate_64((a), 24))
526 #define leftRotate25_64(a) (leftRotate_64((a), 25))
527 #define leftRotate26_64(a) (leftRotate_64((a), 26))
528 #define leftRotate27_64(a) (leftRotate_64((a), 27))
529 #define leftRotate28_64(a) (leftRotate_64((a), 28))
530 #define leftRotate29_64(a) (leftRotate_64((a), 29))
531 #define leftRotate30_64(a) (leftRotate_64((a), 30))
532 #define leftRotate31_64(a) (leftRotate_64((a), 31))
533 #define leftRotate32_64(a) (leftRotate_64((a), 32))
534 #define leftRotate33_64(a) (leftRotate_64((a), 33))
535 #define leftRotate34_64(a) (leftRotate_64((a), 34))
536 #define leftRotate35_64(a) (leftRotate_64((a), 35))
537 #define leftRotate36_64(a) (leftRotate_64((a), 36))
538 #define leftRotate37_64(a) (leftRotate_64((a), 37))
539 #define leftRotate38_64(a) (leftRotate_64((a), 38))
540 #define leftRotate39_64(a) (leftRotate_64((a), 39))
541 #define leftRotate40_64(a) (leftRotate_64((a), 40))
542 #define leftRotate41_64(a) (leftRotate_64((a), 41))
543 #define leftRotate42_64(a) (leftRotate_64((a), 42))
544 #define leftRotate43_64(a) (leftRotate_64((a), 43))
545 #define leftRotate44_64(a) (leftRotate_64((a), 44))
546 #define leftRotate45_64(a) (leftRotate_64((a), 45))
547 #define leftRotate46_64(a) (leftRotate_64((a), 46))
548 #define leftRotate47_64(a) (leftRotate_64((a), 47))
549 #define leftRotate48_64(a) (leftRotate_64((a), 48))
550 #define leftRotate49_64(a) (leftRotate_64((a), 49))
551 #define leftRotate50_64(a) (leftRotate_64((a), 50))
552 #define leftRotate51_64(a) (leftRotate_64((a), 51))
553 #define leftRotate52_64(a) (leftRotate_64((a), 52))
554 #define leftRotate53_64(a) (leftRotate_64((a), 53))
555 #define leftRotate54_64(a) (leftRotate_64((a), 54))
556 #define leftRotate55_64(a) (leftRotate_64((a), 55))
557 #define leftRotate56_64(a) (leftRotate_64((a), 56))
558 #define leftRotate57_64(a) (leftRotate_64((a), 57))
559 #define leftRotate58_64(a) (leftRotate_64((a), 58))
560 #define leftRotate59_64(a) (leftRotate_64((a), 59))
561 #define leftRotate60_64(a) (leftRotate_64((a), 60))
562 #define leftRotate61_64(a) (leftRotate_64((a), 61))
563 #define leftRotate62_64(a) (leftRotate_64((a), 62))
564 #define leftRotate63_64(a) (leftRotate_64((a), 63))
565 
566 /* Right rotate by a specific number of bits. These macros may be replaced
567  * with more efficient ones on platforms that lack a barrel shifter */
568 #define rightRotate1_64(a) (rightRotate_64((a), 1))
569 #define rightRotate2_64(a) (rightRotate_64((a), 2))
570 #define rightRotate3_64(a) (rightRotate_64((a), 3))
571 #define rightRotate4_64(a) (rightRotate_64((a), 4))
572 #define rightRotate5_64(a) (rightRotate_64((a), 5))
573 #define rightRotate6_64(a) (rightRotate_64((a), 6))
574 #define rightRotate7_64(a) (rightRotate_64((a), 7))
575 #define rightRotate8_64(a) (rightRotate_64((a), 8))
576 #define rightRotate9_64(a) (rightRotate_64((a), 9))
577 #define rightRotate10_64(a) (rightRotate_64((a), 10))
578 #define rightRotate11_64(a) (rightRotate_64((a), 11))
579 #define rightRotate12_64(a) (rightRotate_64((a), 12))
580 #define rightRotate13_64(a) (rightRotate_64((a), 13))
581 #define rightRotate14_64(a) (rightRotate_64((a), 14))
582 #define rightRotate15_64(a) (rightRotate_64((a), 15))
583 #define rightRotate16_64(a) (rightRotate_64((a), 16))
584 #define rightRotate17_64(a) (rightRotate_64((a), 17))
585 #define rightRotate18_64(a) (rightRotate_64((a), 18))
586 #define rightRotate19_64(a) (rightRotate_64((a), 19))
587 #define rightRotate20_64(a) (rightRotate_64((a), 20))
588 #define rightRotate21_64(a) (rightRotate_64((a), 21))
589 #define rightRotate22_64(a) (rightRotate_64((a), 22))
590 #define rightRotate23_64(a) (rightRotate_64((a), 23))
591 #define rightRotate24_64(a) (rightRotate_64((a), 24))
592 #define rightRotate25_64(a) (rightRotate_64((a), 25))
593 #define rightRotate26_64(a) (rightRotate_64((a), 26))
594 #define rightRotate27_64(a) (rightRotate_64((a), 27))
595 #define rightRotate28_64(a) (rightRotate_64((a), 28))
596 #define rightRotate29_64(a) (rightRotate_64((a), 29))
597 #define rightRotate30_64(a) (rightRotate_64((a), 30))
598 #define rightRotate31_64(a) (rightRotate_64((a), 31))
599 #define rightRotate32_64(a) (rightRotate_64((a), 32))
600 #define rightRotate33_64(a) (rightRotate_64((a), 33))
601 #define rightRotate34_64(a) (rightRotate_64((a), 34))
602 #define rightRotate35_64(a) (rightRotate_64((a), 35))
603 #define rightRotate36_64(a) (rightRotate_64((a), 36))
604 #define rightRotate37_64(a) (rightRotate_64((a), 37))
605 #define rightRotate38_64(a) (rightRotate_64((a), 38))
606 #define rightRotate39_64(a) (rightRotate_64((a), 39))
607 #define rightRotate40_64(a) (rightRotate_64((a), 40))
608 #define rightRotate41_64(a) (rightRotate_64((a), 41))
609 #define rightRotate42_64(a) (rightRotate_64((a), 42))
610 #define rightRotate43_64(a) (rightRotate_64((a), 43))
611 #define rightRotate44_64(a) (rightRotate_64((a), 44))
612 #define rightRotate45_64(a) (rightRotate_64((a), 45))
613 #define rightRotate46_64(a) (rightRotate_64((a), 46))
614 #define rightRotate47_64(a) (rightRotate_64((a), 47))
615 #define rightRotate48_64(a) (rightRotate_64((a), 48))
616 #define rightRotate49_64(a) (rightRotate_64((a), 49))
617 #define rightRotate50_64(a) (rightRotate_64((a), 50))
618 #define rightRotate51_64(a) (rightRotate_64((a), 51))
619 #define rightRotate52_64(a) (rightRotate_64((a), 52))
620 #define rightRotate53_64(a) (rightRotate_64((a), 53))
621 #define rightRotate54_64(a) (rightRotate_64((a), 54))
622 #define rightRotate55_64(a) (rightRotate_64((a), 55))
623 #define rightRotate56_64(a) (rightRotate_64((a), 56))
624 #define rightRotate57_64(a) (rightRotate_64((a), 57))
625 #define rightRotate58_64(a) (rightRotate_64((a), 58))
626 #define rightRotate59_64(a) (rightRotate_64((a), 59))
627 #define rightRotate60_64(a) (rightRotate_64((a), 60))
628 #define rightRotate61_64(a) (rightRotate_64((a), 61))
629 #define rightRotate62_64(a) (rightRotate_64((a), 62))
630 #define rightRotate63_64(a) (rightRotate_64((a), 63))
631 
632 /* Rotate a 16-bit value left by a number of bits */
633 #define leftRotate_16(a, bits) \
634  (__extension__ ({ \
635  uint16_t _temp = (a); \
636  (_temp << (bits)) | (_temp >> (16 - (bits))); \
637  }))
638 
639 /* Rotate a 16-bit value right by a number of bits */
640 #define rightRotate_16(a, bits) \
641  (__extension__ ({ \
642  uint16_t _temp = (a); \
643  (_temp >> (bits)) | (_temp << (16 - (bits))); \
644  }))
645 
646 /* Left rotate by a specific number of bits. These macros may be replaced
647  * with more efficient ones on platforms that lack a barrel shifter */
648 #define leftRotate1_16(a) (leftRotate_16((a), 1))
649 #define leftRotate2_16(a) (leftRotate_16((a), 2))
650 #define leftRotate3_16(a) (leftRotate_16((a), 3))
651 #define leftRotate4_16(a) (leftRotate_16((a), 4))
652 #define leftRotate5_16(a) (leftRotate_16((a), 5))
653 #define leftRotate6_16(a) (leftRotate_16((a), 6))
654 #define leftRotate7_16(a) (leftRotate_16((a), 7))
655 #define leftRotate8_16(a) (leftRotate_16((a), 8))
656 #define leftRotate9_16(a) (leftRotate_16((a), 9))
657 #define leftRotate10_16(a) (leftRotate_16((a), 10))
658 #define leftRotate11_16(a) (leftRotate_16((a), 11))
659 #define leftRotate12_16(a) (leftRotate_16((a), 12))
660 #define leftRotate13_16(a) (leftRotate_16((a), 13))
661 #define leftRotate14_16(a) (leftRotate_16((a), 14))
662 #define leftRotate15_16(a) (leftRotate_16((a), 15))
663 
664 /* Right rotate by a specific number of bits. These macros may be replaced
665  * with more efficient ones on platforms that lack a barrel shifter */
666 #define rightRotate1_16(a) (rightRotate_16((a), 1))
667 #define rightRotate2_16(a) (rightRotate_16((a), 2))
668 #define rightRotate3_16(a) (rightRotate_16((a), 3))
669 #define rightRotate4_16(a) (rightRotate_16((a), 4))
670 #define rightRotate5_16(a) (rightRotate_16((a), 5))
671 #define rightRotate6_16(a) (rightRotate_16((a), 6))
672 #define rightRotate7_16(a) (rightRotate_16((a), 7))
673 #define rightRotate8_16(a) (rightRotate_16((a), 8))
674 #define rightRotate9_16(a) (rightRotate_16((a), 9))
675 #define rightRotate10_16(a) (rightRotate_16((a), 10))
676 #define rightRotate11_16(a) (rightRotate_16((a), 11))
677 #define rightRotate12_16(a) (rightRotate_16((a), 12))
678 #define rightRotate13_16(a) (rightRotate_16((a), 13))
679 #define rightRotate14_16(a) (rightRotate_16((a), 14))
680 #define rightRotate15_16(a) (rightRotate_16((a), 15))
681 
682 /* Rotate an 8-bit value left by a number of bits */
683 #define leftRotate_8(a, bits) \
684  (__extension__ ({ \
685  uint8_t _temp = (a); \
686  (_temp << (bits)) | (_temp >> (8 - (bits))); \
687  }))
688 
689 /* Rotate an 8-bit value right by a number of bits */
690 #define rightRotate_8(a, bits) \
691  (__extension__ ({ \
692  uint8_t _temp = (a); \
693  (_temp >> (bits)) | (_temp << (8 - (bits))); \
694  }))
695 
696 /* Left rotate by a specific number of bits. These macros may be replaced
697  * with more efficient ones on platforms that lack a barrel shifter */
698 #define leftRotate1_8(a) (leftRotate_8((a), 1))
699 #define leftRotate2_8(a) (leftRotate_8((a), 2))
700 #define leftRotate3_8(a) (leftRotate_8((a), 3))
701 #define leftRotate4_8(a) (leftRotate_8((a), 4))
702 #define leftRotate5_8(a) (leftRotate_8((a), 5))
703 #define leftRotate6_8(a) (leftRotate_8((a), 6))
704 #define leftRotate7_8(a) (leftRotate_8((a), 7))
705 
706 /* Right rotate by a specific number of bits. These macros may be replaced
707  * with more efficient ones on platforms that lack a barrel shifter */
708 #define rightRotate1_8(a) (rightRotate_8((a), 1))
709 #define rightRotate2_8(a) (rightRotate_8((a), 2))
710 #define rightRotate3_8(a) (rightRotate_8((a), 3))
711 #define rightRotate4_8(a) (rightRotate_8((a), 4))
712 #define rightRotate5_8(a) (rightRotate_8((a), 5))
713 #define rightRotate6_8(a) (rightRotate_8((a), 6))
714 #define rightRotate7_8(a) (rightRotate_8((a), 7))
715 
716 #endif