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