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 */
21 #define RotL_64(x, N) (((x) << (N)) | ((x) >> (64 - (N))))
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
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)
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))
58 #endif /* ifndef SKEIN_NEED_SWAP */
61 * Provide any definitions still needed.
63 #ifndef Skein_Swap64 /* swap for big-endian, nop for little-endian */
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)))
75 #define Skein_Swap64(w64) (w64)
77 #endif /* ifndef Skein_Swap64 */
79 #ifndef Skein_Put64_LSB_First
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),
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
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),
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_ */