Monero
skein_port.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifndef _SKEIN_PORT_H_
30 #define _SKEIN_PORT_H_
31 
32 #include <limits.h>
33 #include <stdint.h>
34 
35 #ifndef RETURN_VALUES
36 # define RETURN_VALUES
37 # if defined( DLL_EXPORT )
38 # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
39 # define VOID_RETURN __declspec( dllexport ) void __stdcall
40 # define INT_RETURN __declspec( dllexport ) int __stdcall
41 # elif defined( __GNUC__ )
42 # define VOID_RETURN __declspec( __dllexport__ ) void
43 # define INT_RETURN __declspec( __dllexport__ ) int
44 # else
45 # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
46 # endif
47 # elif defined( DLL_IMPORT )
48 # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
49 # define VOID_RETURN __declspec( dllimport ) void __stdcall
50 # define INT_RETURN __declspec( dllimport ) int __stdcall
51 # elif defined( __GNUC__ )
52 # define VOID_RETURN __declspec( __dllimport__ ) void
53 # define INT_RETURN __declspec( __dllimport__ ) int
54 # else
55 # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
56 # endif
57 # elif defined( __WATCOMC__ )
58 # define VOID_RETURN void __cdecl
59 # define INT_RETURN int __cdecl
60 # else
61 # define VOID_RETURN void
62 # define INT_RETURN int
63 # endif
64 #endif
65 
66 /* These defines are used to declare buffers in a way that allows
67  faster operations on longer variables to be used. In all these
68  defines 'size' must be a power of 2 and >= 8
69 
70  dec_unit_type(size,x) declares a variable 'x' of length
71  'size' bits
72 
73  dec_bufr_type(size,bsize,x) declares a buffer 'x' of length 'bsize'
74  bytes defined as an array of variables
75  each of 'size' bits (bsize must be a
76  multiple of size / 8)
77 
78  ptr_cast(x,size) casts a pointer to a pointer to a
79  varaiable of length 'size' bits
80 */
81 
82 #define ui_type(size) uint##size##_t
83 #define dec_unit_type(size,x) typedef ui_type(size) x
84 #define dec_bufr_type(size,bsize,x) typedef ui_type(size) x[bsize / (size >> 3)]
85 #define ptr_cast(x,size) ((ui_type(size)*)(x))
86 
87 typedef unsigned int uint_t; /* native unsigned integer */
88 typedef uint8_t u08b_t; /* 8-bit unsigned integer */
89 typedef uint64_t u64b_t; /* 64-bit unsigned integer */
90 
91 #ifndef RotL_64
92 #define RotL_64(x,N) (((x) << (N)) | ((x) >> (64-(N))))
93 #endif
94 
95 /*
96  * Skein is "natively" little-endian (unlike SHA-xxx), for optimal
97  * performance on x86 CPUs. The Skein code requires the following
98  * definitions for dealing with endianness:
99  *
100  * SKEIN_NEED_SWAP: 0 for little-endian, 1 for big-endian
101  * Skein_Put64_LSB_First
102  * Skein_Get64_LSB_First
103  * Skein_Swap64
104  *
105  * If SKEIN_NEED_SWAP is defined at compile time, it is used here
106  * along with the portable versions of Put64/Get64/Swap64, which
107  * are slow in general.
108  *
109  * Otherwise, an "auto-detect" of endianness is attempted below.
110  * If the default handling doesn't work well, the user may insert
111  * platform-specific code instead (e.g., for big-endian CPUs).
112  *
113  */
114 #ifndef SKEIN_NEED_SWAP /* compile-time "override" for endianness? */
115 
116 
117 #include "int-util.h"
118 
119 #define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
120 #define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
121 
122 #if BYTE_ORDER == LITTLE_ENDIAN
123 # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
124 #endif
125 
126 #if BYTE_ORDER == BIG_ENDIAN
127 # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
128 #endif
129 
130 /* special handler for IA64, which may be either endianness (?) */
131 /* here we assume little-endian, but this may need to be changed */
132 #if defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
133 # define PLATFORM_MUST_ALIGN (1)
134 #ifndef PLATFORM_BYTE_ORDER
135 # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
136 #endif
137 #endif
138 
139 #ifndef PLATFORM_MUST_ALIGN
140 # define PLATFORM_MUST_ALIGN (0)
141 #endif
142 
143 
144 #if PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
145  /* here for big-endian CPUs */
146 #define SKEIN_NEED_SWAP (1)
147 #elif PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN
148  /* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */
149 #define SKEIN_NEED_SWAP (0)
150 #if PLATFORM_MUST_ALIGN == 0 /* ok to use "fast" versions? */
151 #define Skein_Put64_LSB_First(dst08,src64,bCnt) memcpy(dst08,src64,bCnt)
152 #define Skein_Get64_LSB_First(dst64,src08,wCnt) memcpy(dst64,src08,8*(wCnt))
153 #endif
154 #else
155 #error "Skein needs endianness setting!"
156 #endif
157 
158 #endif /* ifndef SKEIN_NEED_SWAP */
159 
160 /*
161  ******************************************************************
162  * Provide any definitions still needed.
163  ******************************************************************
164  */
165 #ifndef Skein_Swap64 /* swap for big-endian, nop for little-endian */
166 #if SKEIN_NEED_SWAP
167 #define Skein_Swap64(w64) \
168  ( (( ((u64b_t)(w64)) & 0xFF) << 56) | \
169  (((((u64b_t)(w64)) >> 8) & 0xFF) << 48) | \
170  (((((u64b_t)(w64)) >>16) & 0xFF) << 40) | \
171  (((((u64b_t)(w64)) >>24) & 0xFF) << 32) | \
172  (((((u64b_t)(w64)) >>32) & 0xFF) << 24) | \
173  (((((u64b_t)(w64)) >>40) & 0xFF) << 16) | \
174  (((((u64b_t)(w64)) >>48) & 0xFF) << 8) | \
175  (((((u64b_t)(w64)) >>56) & 0xFF) ) )
176 #else
177 #define Skein_Swap64(w64) (w64)
178 #endif
179 #endif /* ifndef Skein_Swap64 */
180 
181 
182 #ifndef Skein_Put64_LSB_First
183 void Skein_Put64_LSB_First(u08b_t *dst,const u64b_t *src,size_t bCnt)
184 #ifdef SKEIN_PORT_CODE /* instantiate the function code here? */
185  { /* this version is fully portable (big-endian or little-endian), but slow */
186  size_t n;
187 
188  for (n=0;n<bCnt;n++)
189  dst[n] = (u08b_t) (src[n>>3] >> (8*(n&7)));
190  }
191 #else
192  ; /* output only the function prototype */
193 #endif
194 #endif /* ifndef Skein_Put64_LSB_First */
195 
196 
197 #ifndef Skein_Get64_LSB_First
198 void Skein_Get64_LSB_First(u64b_t *dst,const u08b_t *src,size_t wCnt)
199 #ifdef SKEIN_PORT_CODE /* instantiate the function code here? */
200  { /* this version is fully portable (big-endian or little-endian), but slow */
201  size_t n;
202 
203  for (n=0;n<8*wCnt;n+=8)
204  dst[n/8] = (((u64b_t) src[n ]) ) +
205  (((u64b_t) src[n+1]) << 8) +
206  (((u64b_t) src[n+2]) << 16) +
207  (((u64b_t) src[n+3]) << 24) +
208  (((u64b_t) src[n+4]) << 32) +
209  (((u64b_t) src[n+5]) << 40) +
210  (((u64b_t) src[n+6]) << 48) +
211  (((u64b_t) src[n+7]) << 56) ;
212  }
213 #else
214  ; /* output only the function prototype */
215 #endif
216 #endif /* ifndef Skein_Get64_LSB_First */
217 
218 #endif /* ifndef _SKEIN_PORT_H_ */
unsigned int uint_t
Definition: skein_port.h:87
uint8_t u08b_t
Definition: skein_port.h:88
#define Skein_Get64_LSB_First(dst64, src08, wCnt)
Definition: skein_port.h:152
unsigned char uint8_t
Definition: stdint.h:124
#define Skein_Put64_LSB_First(dst08, src64, bCnt)
Definition: skein_port.h:151
uint64_t u64b_t
Definition: skein_port.h:89
unsigned __int64 uint64_t
Definition: stdint.h:136