xref: /titanic_51/usr/src/common/crypto/skein/skein_port.h (revision 45818ee124adeaaf947698996b4f4c722afc6d1f)
1*45818ee1SMatthew Ahrens /*
2*45818ee1SMatthew Ahrens  * Platform-specific definitions for Skein hash function.
3*45818ee1SMatthew Ahrens  *
4*45818ee1SMatthew Ahrens  * Source code author: Doug Whiting, 2008.
5*45818ee1SMatthew Ahrens  *
6*45818ee1SMatthew Ahrens  * This algorithm and source code is released to the public domain.
7*45818ee1SMatthew Ahrens  *
8*45818ee1SMatthew Ahrens  * Many thanks to Brian Gladman for his portable header files.
9*45818ee1SMatthew Ahrens  *
10*45818ee1SMatthew Ahrens  * To port Skein to an "unsupported" platform, change the definitions
11*45818ee1SMatthew Ahrens  * in this file appropriately.
12*45818ee1SMatthew Ahrens  */
13*45818ee1SMatthew Ahrens /* Copyright 2013 Doug Whiting. This code is released to the public domain. */
14*45818ee1SMatthew Ahrens 
15*45818ee1SMatthew Ahrens #ifndef	_SKEIN_PORT_H_
16*45818ee1SMatthew Ahrens #define	_SKEIN_PORT_H_
17*45818ee1SMatthew Ahrens 
18*45818ee1SMatthew Ahrens #include <sys/types.h>	/* get integer type definitions */
19*45818ee1SMatthew Ahrens #include <sys/systm.h>	/* for bcopy() */
20*45818ee1SMatthew Ahrens 
21*45818ee1SMatthew Ahrens #ifndef	RotL_64
22*45818ee1SMatthew Ahrens #define	RotL_64(x, N)	(((x) << (N)) | ((x) >> (64 - (N))))
23*45818ee1SMatthew Ahrens #endif
24*45818ee1SMatthew Ahrens 
25*45818ee1SMatthew Ahrens /*
26*45818ee1SMatthew Ahrens  * Skein is "natively" little-endian (unlike SHA-xxx), for optimal
27*45818ee1SMatthew Ahrens  * performance on x86 CPUs. The Skein code requires the following
28*45818ee1SMatthew Ahrens  * definitions for dealing with endianness:
29*45818ee1SMatthew Ahrens  *
30*45818ee1SMatthew Ahrens  *    SKEIN_NEED_SWAP:  0 for little-endian, 1 for big-endian
31*45818ee1SMatthew Ahrens  *    Skein_Put64_LSB_First
32*45818ee1SMatthew Ahrens  *    Skein_Get64_LSB_First
33*45818ee1SMatthew Ahrens  *    Skein_Swap64
34*45818ee1SMatthew Ahrens  *
35*45818ee1SMatthew Ahrens  * If SKEIN_NEED_SWAP is defined at compile time, it is used here
36*45818ee1SMatthew Ahrens  * along with the portable versions of Put64/Get64/Swap64, which
37*45818ee1SMatthew Ahrens  * are slow in general.
38*45818ee1SMatthew Ahrens  *
39*45818ee1SMatthew Ahrens  * Otherwise, an "auto-detect" of endianness is attempted below.
40*45818ee1SMatthew Ahrens  * If the default handling doesn't work well, the user may insert
41*45818ee1SMatthew Ahrens  * platform-specific code instead (e.g., for big-endian CPUs).
42*45818ee1SMatthew Ahrens  *
43*45818ee1SMatthew Ahrens  */
44*45818ee1SMatthew Ahrens #ifndef	SKEIN_NEED_SWAP		/* compile-time "override" for endianness? */
45*45818ee1SMatthew Ahrens 
46*45818ee1SMatthew Ahrens #include <sys/isa_defs.h>	/* get endianness selection */
47*45818ee1SMatthew Ahrens 
48*45818ee1SMatthew Ahrens #define	PLATFORM_MUST_ALIGN	_ALIGNMENT_REQUIRED
49*45818ee1SMatthew Ahrens #if	defined(_BIG_ENDIAN)
50*45818ee1SMatthew Ahrens /* here for big-endian CPUs */
51*45818ee1SMatthew Ahrens #define	SKEIN_NEED_SWAP   (1)
52*45818ee1SMatthew Ahrens #else
53*45818ee1SMatthew Ahrens /* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */
54*45818ee1SMatthew Ahrens #define	SKEIN_NEED_SWAP   (0)
55*45818ee1SMatthew Ahrens #if	PLATFORM_MUST_ALIGN == 0	/* ok to use "fast" versions? */
56*45818ee1SMatthew Ahrens #define	Skein_Put64_LSB_First(dst08, src64, bCnt) bcopy(src64, dst08, bCnt)
57*45818ee1SMatthew Ahrens #define	Skein_Get64_LSB_First(dst64, src08, wCnt) \
58*45818ee1SMatthew Ahrens 	bcopy(src08, dst64, 8 * (wCnt))
59*45818ee1SMatthew Ahrens #endif
60*45818ee1SMatthew Ahrens #endif
61*45818ee1SMatthew Ahrens 
62*45818ee1SMatthew Ahrens #endif				/* ifndef SKEIN_NEED_SWAP */
63*45818ee1SMatthew Ahrens 
64*45818ee1SMatthew Ahrens /*
65*45818ee1SMatthew Ahrens  * Provide any definitions still needed.
66*45818ee1SMatthew Ahrens  */
67*45818ee1SMatthew Ahrens #ifndef	Skein_Swap64	/* swap for big-endian, nop for little-endian */
68*45818ee1SMatthew Ahrens #if	SKEIN_NEED_SWAP
69*45818ee1SMatthew Ahrens #define	Skein_Swap64(w64)				\
70*45818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) & 0xFF) << 56) |		\
71*45818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 8) & 0xFF) << 48) |	\
72*45818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 16) & 0xFF) << 40) |	\
73*45818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 24) & 0xFF) << 32) |	\
74*45818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 32) & 0xFF) << 24) |	\
75*45818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 40) & 0xFF) << 16) |	\
76*45818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 48) & 0xFF) << 8) |	\
77*45818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 56) & 0xFF)))
78*45818ee1SMatthew Ahrens #else
79*45818ee1SMatthew Ahrens #define	Skein_Swap64(w64)  (w64)
80*45818ee1SMatthew Ahrens #endif
81*45818ee1SMatthew Ahrens #endif				/* ifndef Skein_Swap64 */
82*45818ee1SMatthew Ahrens 
83*45818ee1SMatthew Ahrens #ifndef	Skein_Put64_LSB_First
84*45818ee1SMatthew Ahrens void
85*45818ee1SMatthew Ahrens Skein_Put64_LSB_First(uint8_t *dst, const uint64_t *src, size_t bCnt)
86*45818ee1SMatthew Ahrens #ifdef	SKEIN_PORT_CODE		/* instantiate the function code here? */
87*45818ee1SMatthew Ahrens {
88*45818ee1SMatthew Ahrens 	/*
89*45818ee1SMatthew Ahrens 	 * this version is fully portable (big-endian or little-endian),
90*45818ee1SMatthew Ahrens 	 * but slow
91*45818ee1SMatthew Ahrens 	 */
92*45818ee1SMatthew Ahrens 	size_t n;
93*45818ee1SMatthew Ahrens 
94*45818ee1SMatthew Ahrens 	for (n = 0; n < bCnt; n++)
95*45818ee1SMatthew Ahrens 		dst[n] = (uint8_t)(src[n >> 3] >> (8 * (n & 7)));
96*45818ee1SMatthew Ahrens }
97*45818ee1SMatthew Ahrens #else
98*45818ee1SMatthew Ahrens ;				/* output only the function prototype */
99*45818ee1SMatthew Ahrens #endif
100*45818ee1SMatthew Ahrens #endif				/* ifndef Skein_Put64_LSB_First */
101*45818ee1SMatthew Ahrens 
102*45818ee1SMatthew Ahrens #ifndef	Skein_Get64_LSB_First
103*45818ee1SMatthew Ahrens void
104*45818ee1SMatthew Ahrens Skein_Get64_LSB_First(uint64_t *dst, const uint8_t *src, size_t wCnt)
105*45818ee1SMatthew Ahrens #ifdef	SKEIN_PORT_CODE		/* instantiate the function code here? */
106*45818ee1SMatthew Ahrens {
107*45818ee1SMatthew Ahrens 	/*
108*45818ee1SMatthew Ahrens 	 * this version is fully portable (big-endian or little-endian),
109*45818ee1SMatthew Ahrens 	 * but slow
110*45818ee1SMatthew Ahrens 	 */
111*45818ee1SMatthew Ahrens 	size_t n;
112*45818ee1SMatthew Ahrens 
113*45818ee1SMatthew Ahrens 	for (n = 0; n < 8 * wCnt; n += 8)
114*45818ee1SMatthew Ahrens 		dst[n / 8] = (((uint64_t)src[n])) +
115*45818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 1]) << 8) +
116*45818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 2]) << 16) +
117*45818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 3]) << 24) +
118*45818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 4]) << 32) +
119*45818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 5]) << 40) +
120*45818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 6]) << 48) +
121*45818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 7]) << 56);
122*45818ee1SMatthew Ahrens }
123*45818ee1SMatthew Ahrens #else
124*45818ee1SMatthew Ahrens ;				/* output only the function prototype */
125*45818ee1SMatthew Ahrens #endif
126*45818ee1SMatthew Ahrens #endif				/* ifndef Skein_Get64_LSB_First */
127*45818ee1SMatthew Ahrens 
128*45818ee1SMatthew Ahrens #endif	/* _SKEIN_PORT_H_ */
129