1 /*
2  * Platform-specific definitions for Skein hash function.
3  *
4  * Source code author: Doug Whiting, 2008.
5  *
6  * This algorithm and source code is released to the public domain.
7  *
8  * Many thanks to Brian Gladman for his portable header files.
9  *
10  * To port Skein to an "unsupported" platform, change the definitions
11  * in this file appropriately.
12  */
13 /* Copyright 2013 Doug Whiting. This code is released to the public domain. */
14 
15 #ifndef	_SKEIN_PORT_H_
16 #define	_SKEIN_PORT_H_
17 
18 #include <sys/types.h>	/* get integer type definitions */
19 
20 #ifndef	RotL_64
21 #define	RotL_64(x, N)	(((x) << (N)) | ((x) >> (64 - (N))))
22 #endif
23 
24 /*
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:
28  *
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
33  *
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.
37  *
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).
41  *
42  */
43 #ifndef	SKEIN_NEED_SWAP		/* compile-time "override" for endianness? */
44 
45 #include <sys/isa_defs.h>	/* get endianness selection */
46 
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
57 
58 #endif				/* ifndef SKEIN_NEED_SWAP */
59 
60 /*
61  * Provide any definitions still needed.
62  */
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 */
78 
79 #ifndef	Skein_Put64_LSB_First
80 static inline void
Skein_Put64_LSB_First(uint8_t * dst,const uint64_t * src,size_t bCnt)81 Skein_Put64_LSB_First(uint8_t *dst, const uint64_t *src, size_t bCnt)
82 {
83 	/*
84 	 * this version is fully portable (big-endian or little-endian),
85 	 * but slow
86 	 */
87 	size_t n;
88 
89 	for (n = 0; n < bCnt; n++)
90 		dst[n] = (uint8_t)(src[n >> 3] >> (8 * (n & 7)));
91 }
92 #endif				/* ifndef Skein_Put64_LSB_First */
93 
94 #ifndef	Skein_Get64_LSB_First
95 static inline void
Skein_Get64_LSB_First(uint64_t * dst,const uint8_t * src,size_t wCnt)96 Skein_Get64_LSB_First(uint64_t *dst, const uint8_t *src, size_t wCnt)
97 {
98 	/*
99 	 * this version is fully portable (big-endian or little-endian),
100 	 * but slow
101 	 */
102 	size_t n;
103 
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);
113 }
114 #endif				/* ifndef Skein_Get64_LSB_First */
115 
116 #endif	/* _SKEIN_PORT_H_ */
117