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