xref: /freebsd/sys/crypto/skein/skein_port.h (revision ed549cb0c53f8438c52593ce811f6fcc812248e9)
1 /*	$FreeBSD$	*/
2 #ifndef _SKEIN_PORT_H_
3 #define _SKEIN_PORT_H_
4 /*******************************************************************
5 **
6 ** Platform-specific definitions for Skein hash function.
7 **
8 ** Source code author: Doug Whiting, 2008.
9 **
10 ** This algorithm and source code is released to the public domain.
11 **
12 ** Many thanks to Brian Gladman for his portable header files.
13 **
14 ** To port Skein to an "unsupported" platform, change the definitions
15 ** in this file appropriately.
16 **
17 ********************************************************************/
18 
19 #include <sys/endian.h>
20 #include <sys/types.h>
21 
22 #ifndef _SPL_SYS_TYPES_H_ /* Avoid redefining this typedef */
23 typedef unsigned int    uint_t;             /* native unsigned integer */
24 #endif
25 typedef uint8_t         u08b_t;             /*  8-bit unsigned integer */
26 typedef uint32_t        uint_32t;           /* 32-bit unsigned integer */
27 typedef uint64_t        u64b_t;             /* 64-bit unsigned integer */
28 
29 #ifndef RotL_64
30 #define RotL_64(x,N)    (((x) << (N)) | ((x) >> (64-(N))))
31 #endif
32 
33 __BEGIN_DECLS
34 
35 /*
36  * Skein is "natively" little-endian (unlike SHA-xxx), for optimal
37  * performance on x86 CPUs.  The Skein code requires the following
38  * definitions for dealing with endianness:
39  *
40  *    SKEIN_NEED_SWAP:  0 for little-endian, 1 for big-endian
41  *    Skein_Put64_LSB_First
42  *    Skein_Get64_LSB_First
43  *    Skein_Swap64
44  *
45  * If SKEIN_NEED_SWAP is defined at compile time, it is used here
46  * along with the portable versions of Put64/Get64/Swap64, which
47  * are slow in general.
48  *
49  * Otherwise, an "auto-detect" of endianness is attempted below.
50  * If the default handling doesn't work well, the user may insert
51  * platform-specific code instead (e.g., for big-endian CPUs).
52  *
53  */
54 #ifndef SKEIN_NEED_SWAP /* compile-time "override" for endianness? */
55 
56 #if BYTE_ORDER == BIG_ENDIAN
57     /* here for big-endian CPUs */
58 #define SKEIN_NEED_SWAP   (1)
59 #ifdef  SKEIN_PORT_CODE
60 void    Skein_Put64_LSB_First(u08b_t *dst,const u64b_t *src,size_t bCnt);
61 void    Skein_Get64_LSB_First(u64b_t *dst,const u08b_t *src,size_t wCnt);
62 #endif /* ifdef SKEIN_PORT_CODE */
63 #elif BYTE_ORDER == LITTLE_ENDIAN
64     /* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */
65 #define SKEIN_NEED_SWAP   (0)
66 #define Skein_Put64_LSB_First(dst08,src64,bCnt) memcpy(dst08,src64,bCnt)
67 #define Skein_Get64_LSB_First(dst64,src08,wCnt) memcpy(dst64,src08,8*(wCnt))
68 #else
69 #error "Skein needs endianness setting!"
70 #endif
71 
72 #endif /* ifndef SKEIN_NEED_SWAP */
73 
74 /*
75  ******************************************************************
76  *      Provide any definitions still needed.
77  ******************************************************************
78  */
79 #ifndef Skein_Swap64  /* swap for big-endian, nop for little-endian */
80 #if     SKEIN_NEED_SWAP
81 #define Skein_Swap64(w64)  bswap64(w64)
82 #else
83 #define Skein_Swap64(w64)  (w64)
84 #endif
85 #endif  /* ifndef Skein_Swap64 */
86 
87 
88 #ifndef Skein_Put64_LSB_First
89 void    Skein_Put64_LSB_First(u08b_t *dst,const u64b_t *src,size_t bCnt)
90 #ifdef  SKEIN_PORT_CODE /* instantiate the function code here? */
91 {
92     size_t n;
93 
94     for (n = 0; n < bCnt / 8; n++)
95         le64enc(dst + n * 8, src[n]);
96 }
97 #else
98 ; /* output only the function prototype */
99 #endif
100 #endif   /* ifndef Skein_Put64_LSB_First */
101 
102 
103 #ifndef Skein_Get64_LSB_First
104 void    Skein_Get64_LSB_First(u64b_t *dst,const u08b_t *src,size_t wCnt)
105 #ifdef  SKEIN_PORT_CODE /* instantiate the function code here? */
106 {
107     size_t n;
108 
109     for (n = 0; n < wCnt; n++)
110         dst[n] = le64dec(src + n * 8);
111 }
112 #else
113 ; /* output only the function prototype */
114 #endif
115 #endif   /* ifndef Skein_Get64_LSB_First */
116 
117 /* Start FreeBSD libmd shims */
118 
119 /* Ensure libmd symbols do not clash with libcrypto */
120 #ifndef SKEIN256_Init
121 #define SKEIN256_Init		_libmd_SKEIN256_Init
122 #define SKEIN512_Init		_libmd_SKEIN512_Init
123 #define SKEIN1024_Init		_libmd_SKEIN1024_Init
124 #endif
125 #ifndef SKEIN256_Update
126 #define SKEIN256_Update		_libmd_SKEIN256_Update
127 #define SKEIN512_Update		_libmd_SKEIN512_Update
128 #define SKEIN1024_Update	_libmd_SKEIN1024_Update
129 #endif
130 #ifndef SKEIN256_Final
131 #define SKEIN256_Final		_libmd_SKEIN256_Final
132 #define SKEIN512_Final		_libmd_SKEIN512_Final
133 #define SKEIN1024_Final		_libmd_SKEIN1024_Final
134 #endif
135 #ifndef SKEIN256_End
136 #define SKEIN256_End		_libmd_SKEIN256_End
137 #define SKEIN512_End		_libmd_SKEIN512_End
138 #define SKEIN1024_End		_libmd_SKEIN1024_End
139 #endif
140 #ifndef SKEIN256_Fd
141 #define SKEIN256_Fd		_libmd_SKEIN256_Fd
142 #define SKEIN512_Fd		_libmd_SKEIN512_Fd
143 #define SKEIN1024_Fd		_libmd_SKEIN1024_Fd
144 #endif
145 #ifndef SKEIN256_FdChunk
146 #define SKEIN256_FdChunk	_libmd_SKEIN256_FdChunk
147 #define SKEIN512_FdChunk	_libmd_SKEIN512_FdChunk
148 #define SKEIN1024_FdChunk	_libmd_SKEIN1024_FdChunk
149 #endif
150 #ifndef SKEIN256_File
151 #define SKEIN256_File		_libmd_SKEIN256_File
152 #define SKEIN512_File		_libmd_SKEIN512_File
153 #define SKEIN1024_File		_libmd_SKEIN1024_File
154 #endif
155 #ifndef SKEIN256_FileChunk
156 #define SKEIN256_FileChunk	_libmd_SKEIN256_FileChunk
157 #define SKEIN512_FileChunk	_libmd_SKEIN512_FileChunk
158 #define SKEIN1024_FileChunk	_libmd_SKEIN1024_FileChunk
159 #endif
160 #ifndef SKEIN256_Data
161 #define SKEIN256_Data		_libmd_SKEIN256_Data
162 #define SKEIN512_Data		_libmd_SKEIN512_Data
163 #define SKEIN1024_Data		_libmd_SKEIN1024_Data
164 #endif
165 
166 __END_DECLS
167 
168 #endif   /* ifndef _SKEIN_PORT_H_ */
169