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 #include <sys/systm.h>	/* for bcopy() */
20 
21 #ifndef	RotL_64
22 #define	RotL_64(x, N)	(((x) << (N)) | ((x) >> (64 - (N))))
23 #endif
24 
25 /*
26  * Skein is "natively" little-endian (unlike SHA-xxx), for optimal
27  * performance on x86 CPUs. The Skein code requires the following
28  * definitions for dealing with endianness:
29  *
30  *    SKEIN_NEED_SWAP:  0 for little-endian, 1 for big-endian
31  *    Skein_Put64_LSB_First
32  *    Skein_Get64_LSB_First
33  *    Skein_Swap64
34  *
35  * If SKEIN_NEED_SWAP is defined at compile time, it is used here
36  * along with the portable versions of Put64/Get64/Swap64, which
37  * are slow in general.
38  *
39  * Otherwise, an "auto-detect" of endianness is attempted below.
40  * If the default handling doesn't work well, the user may insert
41  * platform-specific code instead (e.g., for big-endian CPUs).
42  *
43  */
44 #ifndef	SKEIN_NEED_SWAP		/* compile-time "override" for endianness? */
45 
46 #ifndef	_STANDALONE
47 #include <sys/isa_defs.h>	/* get endianness selection */
48 #else
49 #include <sys/param.h>		/* get endianness selection */
50 #define	_ALIGNMENT_REQUIRED	1
51 /*
52  * The STANDALONE build is using endian.h logic, where we have defined
53  * macros _BIG_ENDIAN and _LITTLE_ENDIAN, and the current endian is set
54  * in _BYTE_ORDER. To keep the changes minimal, we need to #undef the
55  * other. Once we have kernel version of endian.h, we can have further
56  * clean up.
57  */
58 #if (_BYTE_ORDER == _LITTLE_ENDIAN)
59 #undef _BIG_ENDIAN
60 #else
61 #undef _LITTLE_ENDIAN
62 #endif
63 #endif
64 
65 #define	PLATFORM_MUST_ALIGN	_ALIGNMENT_REQUIRED
66 #if	defined(_BIG_ENDIAN)
67 /* here for big-endian CPUs */
68 #define	SKEIN_NEED_SWAP   (1)
69 #else
70 /* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */
71 #define	SKEIN_NEED_SWAP   (0)
72 #if	PLATFORM_MUST_ALIGN == 0	/* ok to use "fast" versions? */
73 #define	Skein_Put64_LSB_First(dst08, src64, bCnt) bcopy(src64, dst08, bCnt)
74 #define	Skein_Get64_LSB_First(dst64, src08, wCnt) \
75 	bcopy(src08, dst64, 8 * (wCnt))
76 #endif
77 #endif
78 
79 #endif				/* ifndef SKEIN_NEED_SWAP */
80 
81 /*
82  * Provide any definitions still needed.
83  */
84 #ifndef	Skein_Swap64	/* swap for big-endian, nop for little-endian */
85 #if	SKEIN_NEED_SWAP
86 #define	Skein_Swap64(w64)				\
87 	(((((uint64_t)(w64)) & 0xFF) << 56) |		\
88 	(((((uint64_t)(w64)) >> 8) & 0xFF) << 48) |	\
89 	(((((uint64_t)(w64)) >> 16) & 0xFF) << 40) |	\
90 	(((((uint64_t)(w64)) >> 24) & 0xFF) << 32) |	\
91 	(((((uint64_t)(w64)) >> 32) & 0xFF) << 24) |	\
92 	(((((uint64_t)(w64)) >> 40) & 0xFF) << 16) |	\
93 	(((((uint64_t)(w64)) >> 48) & 0xFF) << 8) |	\
94 	(((((uint64_t)(w64)) >> 56) & 0xFF)))
95 #else
96 #define	Skein_Swap64(w64)  (w64)
97 #endif
98 #endif				/* ifndef Skein_Swap64 */
99 
100 #ifndef	Skein_Put64_LSB_First
101 void
Skein_Put64_LSB_First(uint8_t * dst,const uint64_t * src,size_t bCnt)102 Skein_Put64_LSB_First(uint8_t *dst, const uint64_t *src, size_t bCnt)
103 #ifdef	SKEIN_PORT_CODE		/* instantiate the function code here? */
104 {
105 	/*
106 	 * this version is fully portable (big-endian or little-endian),
107 	 * but slow
108 	 */
109 	size_t n;
110 
111 	for (n = 0; n < bCnt; n++)
112 		dst[n] = (uint8_t)(src[n >> 3] >> (8 * (n & 7)));
113 }
114 #else
115 ;				/* output only the function prototype */
116 #endif
117 #endif				/* ifndef Skein_Put64_LSB_First */
118 
119 #ifndef	Skein_Get64_LSB_First
120 void
Skein_Get64_LSB_First(uint64_t * dst,const uint8_t * src,size_t wCnt)121 Skein_Get64_LSB_First(uint64_t *dst, const uint8_t *src, size_t wCnt)
122 #ifdef	SKEIN_PORT_CODE		/* instantiate the function code here? */
123 {
124 	/*
125 	 * this version is fully portable (big-endian or little-endian),
126 	 * but slow
127 	 */
128 	size_t n;
129 
130 	for (n = 0; n < 8 * wCnt; n += 8)
131 		dst[n / 8] = (((uint64_t)src[n])) +
132 		    (((uint64_t)src[n + 1]) << 8) +
133 		    (((uint64_t)src[n + 2]) << 16) +
134 		    (((uint64_t)src[n + 3]) << 24) +
135 		    (((uint64_t)src[n + 4]) << 32) +
136 		    (((uint64_t)src[n + 5]) << 40) +
137 		    (((uint64_t)src[n + 6]) << 48) +
138 		    (((uint64_t)src[n + 7]) << 56);
139 }
140 #else
141 ;				/* output only the function prototype */
142 #endif
143 #endif				/* ifndef Skein_Get64_LSB_First */
144 
145 #endif	/* _SKEIN_PORT_H_ */
146