145818ee1SMatthew Ahrens /*
245818ee1SMatthew Ahrens  * Platform-specific definitions for Skein hash function.
345818ee1SMatthew Ahrens  *
445818ee1SMatthew Ahrens  * Source code author: Doug Whiting, 2008.
545818ee1SMatthew Ahrens  *
645818ee1SMatthew Ahrens  * This algorithm and source code is released to the public domain.
745818ee1SMatthew Ahrens  *
845818ee1SMatthew Ahrens  * Many thanks to Brian Gladman for his portable header files.
945818ee1SMatthew Ahrens  *
1045818ee1SMatthew Ahrens  * To port Skein to an "unsupported" platform, change the definitions
1145818ee1SMatthew Ahrens  * in this file appropriately.
1245818ee1SMatthew Ahrens  */
1345818ee1SMatthew Ahrens /* Copyright 2013 Doug Whiting. This code is released to the public domain. */
1445818ee1SMatthew Ahrens 
1545818ee1SMatthew Ahrens #ifndef	_SKEIN_PORT_H_
1645818ee1SMatthew Ahrens #define	_SKEIN_PORT_H_
1745818ee1SMatthew Ahrens 
1845818ee1SMatthew Ahrens #include <sys/types.h>	/* get integer type definitions */
1945818ee1SMatthew Ahrens #include <sys/systm.h>	/* for bcopy() */
2045818ee1SMatthew Ahrens 
2145818ee1SMatthew Ahrens #ifndef	RotL_64
2245818ee1SMatthew Ahrens #define	RotL_64(x, N)	(((x) << (N)) | ((x) >> (64 - (N))))
2345818ee1SMatthew Ahrens #endif
2445818ee1SMatthew Ahrens 
2545818ee1SMatthew Ahrens /*
2645818ee1SMatthew Ahrens  * Skein is "natively" little-endian (unlike SHA-xxx), for optimal
2745818ee1SMatthew Ahrens  * performance on x86 CPUs. The Skein code requires the following
2845818ee1SMatthew Ahrens  * definitions for dealing with endianness:
2945818ee1SMatthew Ahrens  *
3045818ee1SMatthew Ahrens  *    SKEIN_NEED_SWAP:  0 for little-endian, 1 for big-endian
3145818ee1SMatthew Ahrens  *    Skein_Put64_LSB_First
3245818ee1SMatthew Ahrens  *    Skein_Get64_LSB_First
3345818ee1SMatthew Ahrens  *    Skein_Swap64
3445818ee1SMatthew Ahrens  *
3545818ee1SMatthew Ahrens  * If SKEIN_NEED_SWAP is defined at compile time, it is used here
3645818ee1SMatthew Ahrens  * along with the portable versions of Put64/Get64/Swap64, which
3745818ee1SMatthew Ahrens  * are slow in general.
3845818ee1SMatthew Ahrens  *
3945818ee1SMatthew Ahrens  * Otherwise, an "auto-detect" of endianness is attempted below.
4045818ee1SMatthew Ahrens  * If the default handling doesn't work well, the user may insert
4145818ee1SMatthew Ahrens  * platform-specific code instead (e.g., for big-endian CPUs).
4245818ee1SMatthew Ahrens  *
4345818ee1SMatthew Ahrens  */
4445818ee1SMatthew Ahrens #ifndef	SKEIN_NEED_SWAP		/* compile-time "override" for endianness? */
4545818ee1SMatthew Ahrens 
46*9d1ccc13SToomas Soome #ifndef	_STANDALONE
4745818ee1SMatthew Ahrens #include <sys/isa_defs.h>	/* get endianness selection */
48*9d1ccc13SToomas Soome #else
49*9d1ccc13SToomas Soome #include <sys/param.h>		/* get endianness selection */
50*9d1ccc13SToomas Soome #define	_ALIGNMENT_REQUIRED	1
51*9d1ccc13SToomas Soome /*
52*9d1ccc13SToomas Soome  * The STANDALONE build is using endian.h logic, where we have defined
53*9d1ccc13SToomas Soome  * macros _BIG_ENDIAN and _LITTLE_ENDIAN, and the current endian is set
54*9d1ccc13SToomas Soome  * in _BYTE_ORDER. To keep the changes minimal, we need to #undef the
55*9d1ccc13SToomas Soome  * other. Once we have kernel version of endian.h, we can have further
56*9d1ccc13SToomas Soome  * clean up.
57*9d1ccc13SToomas Soome  */
58*9d1ccc13SToomas Soome #if (_BYTE_ORDER == _LITTLE_ENDIAN)
59*9d1ccc13SToomas Soome #undef _BIG_ENDIAN
60*9d1ccc13SToomas Soome #else
61*9d1ccc13SToomas Soome #undef _LITTLE_ENDIAN
62*9d1ccc13SToomas Soome #endif
63*9d1ccc13SToomas Soome #endif
6445818ee1SMatthew Ahrens 
6545818ee1SMatthew Ahrens #define	PLATFORM_MUST_ALIGN	_ALIGNMENT_REQUIRED
6645818ee1SMatthew Ahrens #if	defined(_BIG_ENDIAN)
6745818ee1SMatthew Ahrens /* here for big-endian CPUs */
6845818ee1SMatthew Ahrens #define	SKEIN_NEED_SWAP   (1)
6945818ee1SMatthew Ahrens #else
7045818ee1SMatthew Ahrens /* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */
7145818ee1SMatthew Ahrens #define	SKEIN_NEED_SWAP   (0)
7245818ee1SMatthew Ahrens #if	PLATFORM_MUST_ALIGN == 0	/* ok to use "fast" versions? */
7345818ee1SMatthew Ahrens #define	Skein_Put64_LSB_First(dst08, src64, bCnt) bcopy(src64, dst08, bCnt)
7445818ee1SMatthew Ahrens #define	Skein_Get64_LSB_First(dst64, src08, wCnt) \
7545818ee1SMatthew Ahrens 	bcopy(src08, dst64, 8 * (wCnt))
7645818ee1SMatthew Ahrens #endif
7745818ee1SMatthew Ahrens #endif
7845818ee1SMatthew Ahrens 
7945818ee1SMatthew Ahrens #endif				/* ifndef SKEIN_NEED_SWAP */
8045818ee1SMatthew Ahrens 
8145818ee1SMatthew Ahrens /*
8245818ee1SMatthew Ahrens  * Provide any definitions still needed.
8345818ee1SMatthew Ahrens  */
8445818ee1SMatthew Ahrens #ifndef	Skein_Swap64	/* swap for big-endian, nop for little-endian */
8545818ee1SMatthew Ahrens #if	SKEIN_NEED_SWAP
8645818ee1SMatthew Ahrens #define	Skein_Swap64(w64)				\
8745818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) & 0xFF) << 56) |		\
8845818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 8) & 0xFF) << 48) |	\
8945818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 16) & 0xFF) << 40) |	\
9045818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 24) & 0xFF) << 32) |	\
9145818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 32) & 0xFF) << 24) |	\
9245818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 40) & 0xFF) << 16) |	\
9345818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 48) & 0xFF) << 8) |	\
9445818ee1SMatthew Ahrens 	(((((uint64_t)(w64)) >> 56) & 0xFF)))
9545818ee1SMatthew Ahrens #else
9645818ee1SMatthew Ahrens #define	Skein_Swap64(w64)  (w64)
9745818ee1SMatthew Ahrens #endif
9845818ee1SMatthew Ahrens #endif				/* ifndef Skein_Swap64 */
9945818ee1SMatthew Ahrens 
10045818ee1SMatthew Ahrens #ifndef	Skein_Put64_LSB_First
10145818ee1SMatthew Ahrens void
Skein_Put64_LSB_First(uint8_t * dst,const uint64_t * src,size_t bCnt)10245818ee1SMatthew Ahrens Skein_Put64_LSB_First(uint8_t *dst, const uint64_t *src, size_t bCnt)
10345818ee1SMatthew Ahrens #ifdef	SKEIN_PORT_CODE		/* instantiate the function code here? */
10445818ee1SMatthew Ahrens {
10545818ee1SMatthew Ahrens 	/*
10645818ee1SMatthew Ahrens 	 * this version is fully portable (big-endian or little-endian),
10745818ee1SMatthew Ahrens 	 * but slow
10845818ee1SMatthew Ahrens 	 */
10945818ee1SMatthew Ahrens 	size_t n;
11045818ee1SMatthew Ahrens 
11145818ee1SMatthew Ahrens 	for (n = 0; n < bCnt; n++)
11245818ee1SMatthew Ahrens 		dst[n] = (uint8_t)(src[n >> 3] >> (8 * (n & 7)));
11345818ee1SMatthew Ahrens }
11445818ee1SMatthew Ahrens #else
11545818ee1SMatthew Ahrens ;				/* output only the function prototype */
11645818ee1SMatthew Ahrens #endif
11745818ee1SMatthew Ahrens #endif				/* ifndef Skein_Put64_LSB_First */
11845818ee1SMatthew Ahrens 
11945818ee1SMatthew Ahrens #ifndef	Skein_Get64_LSB_First
12045818ee1SMatthew Ahrens void
Skein_Get64_LSB_First(uint64_t * dst,const uint8_t * src,size_t wCnt)12145818ee1SMatthew Ahrens Skein_Get64_LSB_First(uint64_t *dst, const uint8_t *src, size_t wCnt)
12245818ee1SMatthew Ahrens #ifdef	SKEIN_PORT_CODE		/* instantiate the function code here? */
12345818ee1SMatthew Ahrens {
12445818ee1SMatthew Ahrens 	/*
12545818ee1SMatthew Ahrens 	 * this version is fully portable (big-endian or little-endian),
12645818ee1SMatthew Ahrens 	 * but slow
12745818ee1SMatthew Ahrens 	 */
12845818ee1SMatthew Ahrens 	size_t n;
12945818ee1SMatthew Ahrens 
13045818ee1SMatthew Ahrens 	for (n = 0; n < 8 * wCnt; n += 8)
13145818ee1SMatthew Ahrens 		dst[n / 8] = (((uint64_t)src[n])) +
13245818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 1]) << 8) +
13345818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 2]) << 16) +
13445818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 3]) << 24) +
13545818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 4]) << 32) +
13645818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 5]) << 40) +
13745818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 6]) << 48) +
13845818ee1SMatthew Ahrens 		    (((uint64_t)src[n + 7]) << 56);
13945818ee1SMatthew Ahrens }
14045818ee1SMatthew Ahrens #else
14145818ee1SMatthew Ahrens ;				/* output only the function prototype */
14245818ee1SMatthew Ahrens #endif
14345818ee1SMatthew Ahrens #endif				/* ifndef Skein_Get64_LSB_First */
14445818ee1SMatthew Ahrens 
14545818ee1SMatthew Ahrens #endif	/* _SKEIN_PORT_H_ */
146