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