ZTS: Add Fedora 41, remove Fedora 39
[zfs.git] / module / icp / algs / skein / skein_port.h
blob96d1266d019e22eebf26ec48c4165ac3bbd8d9b3
1 /*
2 * Platform-specific definitions for Skein hash function.
4 * Source code author: Doug Whiting, 2008.
6 * This algorithm and source code is released to the public domain.
8 * Many thanks to Brian Gladman for his portable header files.
10 * To port Skein to an "unsupported" platform, change the definitions
11 * in this file appropriately.
13 /* Copyright 2013 Doug Whiting. This code is released to the public domain. */
15 #ifndef _SKEIN_PORT_H_
16 #define _SKEIN_PORT_H_
18 #include <sys/types.h> /* get integer type definitions */
20 #ifndef RotL_64
21 #define RotL_64(x, N) (((x) << (N)) | ((x) >> (64 - (N))))
22 #endif
25 * Skein is "natively" little-endian (unlike SHA-xxx), for optimal
26 * performance on x86 CPUs. The Skein code requires the following
27 * definitions for dealing with endianness:
29 * SKEIN_NEED_SWAP: 0 for little-endian, 1 for big-endian
30 * Skein_Put64_LSB_First
31 * Skein_Get64_LSB_First
32 * Skein_Swap64
34 * If SKEIN_NEED_SWAP is defined at compile time, it is used here
35 * along with the portable versions of Put64/Get64/Swap64, which
36 * are slow in general.
38 * Otherwise, an "auto-detect" of endianness is attempted below.
39 * If the default handling doesn't work well, the user may insert
40 * platform-specific code instead (e.g., for big-endian CPUs).
43 #ifndef SKEIN_NEED_SWAP /* compile-time "override" for endianness? */
45 #include <sys/isa_defs.h> /* get endianness selection */
47 #if defined(_ZFS_BIG_ENDIAN)
48 /* here for big-endian CPUs */
49 #define SKEIN_NEED_SWAP (1)
50 #else
51 /* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */
52 #define SKEIN_NEED_SWAP (0)
53 #define Skein_Put64_LSB_First(dst08, src64, bCnt) memcpy(dst08, src64, bCnt)
54 #define Skein_Get64_LSB_First(dst64, src08, wCnt) \
55 memcpy(dst64, src08, 8 * (wCnt))
56 #endif
58 #endif /* ifndef SKEIN_NEED_SWAP */
61 * Provide any definitions still needed.
63 #ifndef Skein_Swap64 /* swap for big-endian, nop for little-endian */
64 #if SKEIN_NEED_SWAP
65 #define Skein_Swap64(w64) \
66 (((((uint64_t)(w64)) & 0xFF) << 56) | \
67 (((((uint64_t)(w64)) >> 8) & 0xFF) << 48) | \
68 (((((uint64_t)(w64)) >> 16) & 0xFF) << 40) | \
69 (((((uint64_t)(w64)) >> 24) & 0xFF) << 32) | \
70 (((((uint64_t)(w64)) >> 32) & 0xFF) << 24) | \
71 (((((uint64_t)(w64)) >> 40) & 0xFF) << 16) | \
72 (((((uint64_t)(w64)) >> 48) & 0xFF) << 8) | \
73 (((((uint64_t)(w64)) >> 56) & 0xFF)))
74 #else
75 #define Skein_Swap64(w64) (w64)
76 #endif
77 #endif /* ifndef Skein_Swap64 */
79 #ifndef Skein_Put64_LSB_First
80 static inline void
81 Skein_Put64_LSB_First(uint8_t *dst, const uint64_t *src, size_t bCnt)
84 * this version is fully portable (big-endian or little-endian),
85 * but slow
87 size_t n;
89 for (n = 0; n < bCnt; n++)
90 dst[n] = (uint8_t)(src[n >> 3] >> (8 * (n & 7)));
92 #endif /* ifndef Skein_Put64_LSB_First */
94 #ifndef Skein_Get64_LSB_First
95 static inline void
96 Skein_Get64_LSB_First(uint64_t *dst, const uint8_t *src, size_t wCnt)
99 * this version is fully portable (big-endian or little-endian),
100 * but slow
102 size_t n;
104 for (n = 0; n < 8 * wCnt; n += 8)
105 dst[n / 8] = (((uint64_t)src[n])) +
106 (((uint64_t)src[n + 1]) << 8) +
107 (((uint64_t)src[n + 2]) << 16) +
108 (((uint64_t)src[n + 3]) << 24) +
109 (((uint64_t)src[n + 4]) << 32) +
110 (((uint64_t)src[n + 5]) << 40) +
111 (((uint64_t)src[n + 6]) << 48) +
112 (((uint64_t)src[n + 7]) << 56);
114 #endif /* ifndef Skein_Get64_LSB_First */
116 #endif /* _SKEIN_PORT_H_ */