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